1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32 #ifndef __LWIP_IP_H__
33 #define __LWIP_IP_H__
34 
35 #include "lwip/opt.h"
36 
37 #include "lwip/def.h"
38 #include "lwip/pbuf.h"
39 #include "lwip/ip_addr.h"
40 #include "lwip/err.h"
41 #include "lwip/netif.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 /** Currently, the function ip_output_if_opt() is only used with IGMP */
48 #define IP_OPTIONS_SEND   LWIP_IGMP
49 
50 #define IP_HLEN 20
51 
52 #define IP_PROTO_ICMP    1
53 #define IP_PROTO_IGMP    2
54 #define IP_PROTO_UDP     17
55 #define IP_PROTO_UDPLITE 136
56 #define IP_PROTO_TCP     6
57 
58 /* This is passed as the destination address to ip_output_if (not
59    to ip_output), meaning that an IP header already is constructed
60    in the pbuf. This is used when TCP retransmits. */
61 #ifdef IP_HDRINCL
62 #undef IP_HDRINCL
63 #endif /* IP_HDRINCL */
64 #define IP_HDRINCL  NULL
65 
66 #if LWIP_NETIF_HWADDRHINT
67 #define IP_PCB_ADDRHINT ;u8_t addr_hint
68 #else
69 #define IP_PCB_ADDRHINT
70 #endif /* LWIP_NETIF_HWADDRHINT */
71 
72 /* This is the common part of all PCB types. It needs to be at the
73    beginning of a PCB type definition. It is located here so that
74    changes to this common part are made in one location instead of
75    having to change all PCB structs. */
76 #define IP_PCB \
77   /* ip addresses in network byte order */ \
78   ip_addr_t local_ip; \
79   ip_addr_t remote_ip; \
80    /* Socket options */  \
81   u8_t so_options;      \
82    /* Type Of Service */ \
83   u8_t tos;              \
84   /* Time To Live */     \
85   u8_t ttl               \
86   /* link layer address resolution hint */ \
87   IP_PCB_ADDRHINT
88 
89 struct ip_pcb {
90 /* Common members of all PCB types */
91   IP_PCB;
92 };
93 
94 /*
95  * Option flags per-socket. These are the same like SO_XXX.
96  */
97 /*#define SOF_DEBUG       0x01U     Unimplemented: turn on debugging info recording */
98 #define SOF_ACCEPTCONN    0x02U  /* socket has had listen() */
99 #define SOF_REUSEADDR     0x04U  /* allow local address reuse */
100 #define SOF_KEEPALIVE     0x08U  /* keep connections alive */
101 /*#define SOF_DONTROUTE   0x10U     Unimplemented: just use interface addresses */
102 #define SOF_BROADCAST     0x20U  /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
103 /*#define SOF_USELOOPBACK 0x40U     Unimplemented: bypass hardware when possible */
104 #define SOF_LINGER        0x80U  /* linger on close if data present */
105 /*#define SOF_OOBINLINE   0x0100U   Unimplemented: leave received OOB data in line */
106 /*#define SOF_REUSEPORT   0x0200U   Unimplemented: allow local address & port reuse */
107 
108 /* These flags are inherited (e.g. from a listen-pcb to a connection-pcb): */
109 #define SOF_INHERITED   (SOF_REUSEADDR|SOF_KEEPALIVE|SOF_LINGER/*|SOF_DEBUG|SOF_DONTROUTE|SOF_OOBINLINE*/)
110 
111 
112 #ifdef PACK_STRUCT_USE_INCLUDES
113 #  include "arch/bpstruct.h"
114 #endif
115 PACK_STRUCT_BEGIN
116 struct ip_hdr {
117   /* version / header length */
118   PACK_STRUCT_FIELD(u8_t _v_hl);
119   /* type of service */
120   PACK_STRUCT_FIELD(u8_t _tos);
121   /* total length */
122   PACK_STRUCT_FIELD(u16_t _len);
123   /* identification */
124   PACK_STRUCT_FIELD(u16_t _id);
125   /* fragment offset field */
126   PACK_STRUCT_FIELD(u16_t _offset);
127 #define IP_RF 0x8000U        /* reserved fragment flag */
128 #define IP_DF 0x4000U        /* dont fragment flag */
129 #define IP_MF 0x2000U        /* more fragments flag */
130 #define IP_OFFMASK 0x1fffU   /* mask for fragmenting bits */
131   /* time to live */
132   PACK_STRUCT_FIELD(u8_t _ttl);
133   /* protocol*/
134   PACK_STRUCT_FIELD(u8_t _proto);
135   /* checksum */
136   PACK_STRUCT_FIELD(u16_t _chksum);
137   /* source and destination IP addresses */
138   PACK_STRUCT_FIELD(ip_addr_p_t src);
139   PACK_STRUCT_FIELD(ip_addr_p_t dest);
140 } PACK_STRUCT_STRUCT;
141 PACK_STRUCT_END
142 #ifdef PACK_STRUCT_USE_INCLUDES
143 #  include "arch/epstruct.h"
144 #endif
145 
146 #define IPH_V(hdr)  ((hdr)->_v_hl >> 4)
147 #define IPH_HL(hdr) ((hdr)->_v_hl & 0x0f)
148 #define IPH_TOS(hdr) ((hdr)->_tos)
149 #define IPH_LEN(hdr) ((hdr)->_len)
150 #define IPH_ID(hdr) ((hdr)->_id)
151 #define IPH_OFFSET(hdr) ((hdr)->_offset)
152 #define IPH_TTL(hdr) ((hdr)->_ttl)
153 #define IPH_PROTO(hdr) ((hdr)->_proto)
154 #define IPH_CHKSUM(hdr) ((hdr)->_chksum)
155 
156 #define IPH_VHL_SET(hdr, v, hl) (hdr)->_v_hl = (((v) << 4) | (hl))
157 #define IPH_TOS_SET(hdr, tos) (hdr)->_tos = (tos)
158 #define IPH_LEN_SET(hdr, len) (hdr)->_len = (len)
159 #define IPH_ID_SET(hdr, id) (hdr)->_id = (id)
160 #define IPH_OFFSET_SET(hdr, off) (hdr)->_offset = (off)
161 #define IPH_TTL_SET(hdr, ttl) (hdr)->_ttl = (u8_t)(ttl)
162 #define IPH_PROTO_SET(hdr, proto) (hdr)->_proto = (u8_t)(proto)
163 #define IPH_CHKSUM_SET(hdr, chksum) (hdr)->_chksum = (chksum)
164 
165 /** The interface that provided the packet for the current callback invocation. */
166 extern struct netif *current_netif;
167 /** Header of the input packet currently being processed. */
168 extern const struct ip_hdr *current_header;
169 /** Source IP address of current_header */
170 extern ip_addr_t current_iphdr_src;
171 /** Destination IP address of current_header */
172 extern ip_addr_t current_iphdr_dest;
173 
174 #define ip_init() /* Compatibility define, not init needed. */
175 struct netif *ip_route(ip_addr_t *dest);
176 err_t ip_input(struct pbuf *p, struct netif *inp);
177 err_t ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
178        u8_t ttl, u8_t tos, u8_t proto);
179 err_t ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
180        u8_t ttl, u8_t tos, u8_t proto,
181        struct netif *netif);
182 #if LWIP_NETIF_HWADDRHINT
183 err_t ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
184        u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint);
185 #endif /* LWIP_NETIF_HWADDRHINT */
186 #if IP_OPTIONS_SEND
187 err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
188        u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
189        u16_t optlen);
190 #endif /* IP_OPTIONS_SEND */
191 /** Get the interface that received the current packet.
192  * This function must only be called from a receive callback (udp_recv,
193  * raw_recv, tcp_accept). It will return NULL otherwise. */
194 #define ip_current_netif()  (current_netif)
195 /** Get the IP header of the current packet.
196  * This function must only be called from a receive callback (udp_recv,
197  * raw_recv, tcp_accept). It will return NULL otherwise. */
198 #define ip_current_header() (current_header)
199 /** Source IP address of current_header */
200 #define ip_current_src_addr()  (&current_iphdr_src)
201 /** Destination IP address of current_header */
202 #define ip_current_dest_addr() (&current_iphdr_dest)
203 
204 /** Gets an IP pcb option (SOF_* flags) */
205 #define ip_get_option(pcb, opt)   ((pcb)->so_options & (opt))
206 /** Sets an IP pcb option (SOF_* flags) */
207 #define ip_set_option(pcb, opt)   ((pcb)->so_options |= (opt))
208 /** Resets an IP pcb option (SOF_* flags) */
209 #define ip_reset_option(pcb, opt) ((pcb)->so_options &= ~(opt))
210 
211 #if IP_DEBUG
212 void ip_debug_print(struct pbuf *p);
213 #else
214 #define ip_debug_print(p)
215 #endif /* IP_DEBUG */
216 
217 #ifdef __cplusplus
218 }
219 #endif
220 
221 #endif /* __LWIP_IP_H__ */
222 
223 
224