BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file |
||
3 | * This is the IPv4 layer implementation for incoming and outgoing IP traffic. |
||
4 | * |
||
5 | * @see ip_frag.c |
||
6 | * |
||
7 | */ |
||
8 | |||
9 | /* |
||
10 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
||
11 | * All rights reserved. |
||
12 | * |
||
13 | * Redistribution and use in source and binary forms, with or without modification, |
||
14 | * are permitted provided that the following conditions are met: |
||
15 | * |
||
16 | * 1. Redistributions of source code must retain the above copyright notice, |
||
17 | * this list of conditions and the following disclaimer. |
||
18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
||
19 | * this list of conditions and the following disclaimer in the documentation |
||
20 | * and/or other materials provided with the distribution. |
||
21 | * 3. The name of the author may not be used to endorse or promote products |
||
22 | * derived from this software without specific prior written permission. |
||
23 | * |
||
24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
||
25 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
||
26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
||
27 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||
28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
||
29 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
||
32 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
||
33 | * OF SUCH DAMAGE. |
||
34 | * |
||
35 | * This file is part of the lwIP TCP/IP stack. |
||
36 | * |
||
37 | * Author: Adam Dunkels <adam@sics.se> |
||
38 | * |
||
39 | */ |
||
40 | |||
41 | #include "lwip/opt.h" |
||
42 | |||
43 | #if LWIP_IPV4 |
||
44 | |||
45 | #include "lwip/ip.h" |
||
46 | #include "lwip/def.h" |
||
47 | #include "lwip/mem.h" |
||
48 | #include "lwip/ip4_frag.h" |
||
49 | #include "lwip/inet_chksum.h" |
||
50 | #include "lwip/netif.h" |
||
51 | #include "lwip/icmp.h" |
||
52 | #include "lwip/igmp.h" |
||
53 | #include "lwip/raw.h" |
||
54 | #include "lwip/udp.h" |
||
55 | #include "lwip/priv/tcp_priv.h" |
||
56 | #include "lwip/autoip.h" |
||
57 | #include "lwip/stats.h" |
||
58 | #include "lwip/prot/iana.h" |
||
59 | |||
60 | #include <string.h> |
||
61 | |||
62 | #ifdef LWIP_HOOK_FILENAME |
||
63 | #include LWIP_HOOK_FILENAME |
||
64 | #endif |
||
65 | |||
66 | /** Set this to 0 in the rare case of wanting to call an extra function to |
||
67 | * generate the IP checksum (in contrast to calculating it on-the-fly). */ |
||
68 | #ifndef LWIP_INLINE_IP_CHKSUM |
||
69 | #if LWIP_CHECKSUM_CTRL_PER_NETIF |
||
70 | #define LWIP_INLINE_IP_CHKSUM 0 |
||
71 | #else /* LWIP_CHECKSUM_CTRL_PER_NETIF */ |
||
72 | #define LWIP_INLINE_IP_CHKSUM 1 |
||
73 | #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */ |
||
74 | #endif |
||
75 | |||
76 | #if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP |
||
77 | #define CHECKSUM_GEN_IP_INLINE 1 |
||
78 | #else |
||
79 | #define CHECKSUM_GEN_IP_INLINE 0 |
||
80 | #endif |
||
81 | |||
82 | #if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT) |
||
83 | #define IP_ACCEPT_LINK_LAYER_ADDRESSING 1 |
||
84 | |||
85 | /** Some defines for DHCP to let link-layer-addressed packets through while the |
||
86 | * netif is down. |
||
87 | * To use this in your own application/protocol, define LWIP_IP_ACCEPT_UDP_PORT(port) |
||
88 | * to return 1 if the port is accepted and 0 if the port is not accepted. |
||
89 | */ |
||
90 | #if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) |
||
91 | /* accept DHCP client port and custom port */ |
||
92 | #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(LWIP_IANA_PORT_DHCP_CLIENT)) \ |
||
93 | || (LWIP_IP_ACCEPT_UDP_PORT(port))) |
||
94 | #elif defined(LWIP_IP_ACCEPT_UDP_PORT) /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */ |
||
95 | /* accept custom port only */ |
||
96 | #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(port)) |
||
97 | #else /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */ |
||
98 | /* accept DHCP client port only */ |
||
99 | #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(LWIP_IANA_PORT_DHCP_CLIENT)) |
||
100 | #endif /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */ |
||
101 | |||
102 | #else /* LWIP_DHCP */ |
||
103 | #define IP_ACCEPT_LINK_LAYER_ADDRESSING 0 |
||
104 | #endif /* LWIP_DHCP */ |
||
105 | |||
106 | /** The IP header ID of the next outgoing IP packet */ |
||
107 | static u16_t ip_id; |
||
108 | |||
109 | #if LWIP_MULTICAST_TX_OPTIONS |
||
110 | /** The default netif used for multicast */ |
||
111 | static struct netif *ip4_default_multicast_netif; |
||
112 | |||
113 | /** |
||
114 | * @ingroup ip4 |
||
115 | * Set a default netif for IPv4 multicast. */ |
||
116 | void |
||
117 | ip4_set_default_multicast_netif(struct netif *default_multicast_netif) |
||
118 | { |
||
119 | ip4_default_multicast_netif = default_multicast_netif; |
||
120 | } |
||
121 | #endif /* LWIP_MULTICAST_TX_OPTIONS */ |
||
122 | |||
123 | #ifdef LWIP_HOOK_IP4_ROUTE_SRC |
||
124 | /** |
||
125 | * Source based IPv4 routing must be fully implemented in |
||
126 | * LWIP_HOOK_IP4_ROUTE_SRC(). This function only provides the parameters. |
||
127 | */ |
||
128 | struct netif * |
||
129 | ip4_route_src(const ip4_addr_t *src, const ip4_addr_t *dest) |
||
130 | { |
||
131 | if (src != NULL) { |
||
132 | /* when src==NULL, the hook is called from ip4_route(dest) */ |
||
133 | struct netif *netif = LWIP_HOOK_IP4_ROUTE_SRC(src, dest); |
||
134 | if (netif != NULL) { |
||
135 | return netif; |
||
136 | } |
||
137 | } |
||
138 | return ip4_route(dest); |
||
139 | } |
||
140 | #endif /* LWIP_HOOK_IP4_ROUTE_SRC */ |
||
141 | |||
142 | /** |
||
143 | * Finds the appropriate network interface for a given IP address. It |
||
144 | * searches the list of network interfaces linearly. A match is found |
||
145 | * if the masked IP address of the network interface equals the masked |
||
146 | * IP address given to the function. |
||
147 | * |
||
148 | * @param dest the destination IP address for which to find the route |
||
149 | * @return the netif on which to send to reach dest |
||
150 | */ |
||
151 | struct netif * |
||
152 | ip4_route(const ip4_addr_t *dest) |
||
153 | { |
||
154 | #if !LWIP_SINGLE_NETIF |
||
155 | struct netif *netif; |
||
156 | |||
157 | #if LWIP_MULTICAST_TX_OPTIONS |
||
158 | /* Use administratively selected interface for multicast by default */ |
||
159 | if (ip4_addr_ismulticast(dest) && ip4_default_multicast_netif) { |
||
160 | return ip4_default_multicast_netif; |
||
161 | } |
||
162 | #endif /* LWIP_MULTICAST_TX_OPTIONS */ |
||
163 | |||
164 | /* iterate through netifs */ |
||
165 | for (netif = netif_list; netif != NULL; netif = netif->next) { |
||
166 | /* is the netif up, does it have a link and a valid address? */ |
||
167 | if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif))) { |
||
168 | /* network mask matches? */ |
||
169 | if (ip4_addr_netcmp(dest, netif_ip4_addr(netif), netif_ip4_netmask(netif))) { |
||
170 | /* return netif on which to forward IP packet */ |
||
171 | return netif; |
||
172 | } |
||
173 | /* gateway matches on a non broadcast interface? (i.e. peer in a point to point interface) */ |
||
174 | if (((netif->flags & NETIF_FLAG_BROADCAST) == 0) && ip4_addr_cmp(dest, netif_ip4_gw(netif))) { |
||
175 | /* return netif on which to forward IP packet */ |
||
176 | return netif; |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | |||
181 | #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF |
||
182 | /* loopif is disabled, looopback traffic is passed through any netif */ |
||
183 | if (ip4_addr_isloopback(dest)) { |
||
184 | /* don't check for link on loopback traffic */ |
||
185 | if (netif_default != NULL && netif_is_up(netif_default)) { |
||
186 | return netif_default; |
||
187 | } |
||
188 | /* default netif is not up, just use any netif for loopback traffic */ |
||
189 | for (netif = netif_list; netif != NULL; netif = netif->next) { |
||
190 | if (netif_is_up(netif)) { |
||
191 | return netif; |
||
192 | } |
||
193 | } |
||
194 | return NULL; |
||
195 | } |
||
196 | #endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */ |
||
197 | |||
198 | #ifdef LWIP_HOOK_IP4_ROUTE_SRC |
||
199 | netif = LWIP_HOOK_IP4_ROUTE_SRC(NULL, dest); |
||
200 | if (netif != NULL) { |
||
201 | return netif; |
||
202 | } |
||
203 | #elif defined(LWIP_HOOK_IP4_ROUTE) |
||
204 | netif = LWIP_HOOK_IP4_ROUTE(dest); |
||
205 | if (netif != NULL) { |
||
206 | return netif; |
||
207 | } |
||
208 | #endif |
||
209 | #endif /* !LWIP_SINGLE_NETIF */ |
||
210 | |||
211 | if ((netif_default == NULL) || !netif_is_up(netif_default) || !netif_is_link_up(netif_default) |
||
212 | /* Yes we are binding to any addr */ |
||
213 | // || ip4_addr_isany_val(*netif_ip4_addr(netif_default)) |
||
214 | ) { |
||
215 | /* No matching netif found and default netif is not usable. |
||
216 | If this is not good enough for you, use LWIP_HOOK_IP4_ROUTE() */ |
||
217 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
||
218 | ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest))); |
||
219 | IP_STATS_INC(ip.rterr); |
||
220 | MIB2_STATS_INC(mib2.ipoutnoroutes); |
||
221 | return NULL; |
||
222 | } |
||
223 | |||
224 | return netif_default; |
||
225 | } |
||
226 | |||
227 | #if IP_FORWARD |
||
228 | /** |
||
229 | * Determine whether an IP address is in a reserved set of addresses |
||
230 | * that may not be forwarded, or whether datagrams to that destination |
||
231 | * may be forwarded. |
||
232 | * @param p the packet to forward |
||
233 | * @return 1: can forward 0: discard |
||
234 | */ |
||
235 | static int |
||
236 | ip4_canforward(struct pbuf *p) |
||
237 | { |
||
238 | u32_t addr = lwip_htonl(ip4_addr_get_u32(ip4_current_dest_addr())); |
||
239 | |||
240 | if (p->flags & PBUF_FLAG_LLBCAST) { |
||
241 | /* don't route link-layer broadcasts */ |
||
242 | return 0; |
||
243 | } |
||
244 | if ((p->flags & PBUF_FLAG_LLMCAST) && !IP_MULTICAST(addr)) { |
||
245 | /* don't route link-layer multicasts unless the destination address is an IP |
||
246 | multicast address */ |
||
247 | return 0; |
||
248 | } |
||
249 | if (IP_EXPERIMENTAL(addr)) { |
||
250 | return 0; |
||
251 | } |
||
252 | if (IP_CLASSA(addr)) { |
||
253 | u32_t net = addr & IP_CLASSA_NET; |
||
254 | if ((net == 0) || (net == ((u32_t)IP_LOOPBACKNET << IP_CLASSA_NSHIFT))) { |
||
255 | /* don't route loopback packets */ |
||
256 | return 0; |
||
257 | } |
||
258 | } |
||
259 | return 1; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Forwards an IP packet. It finds an appropriate route for the |
||
264 | * packet, decrements the TTL value of the packet, adjusts the |
||
265 | * checksum and outputs the packet on the appropriate interface. |
||
266 | * |
||
267 | * @param p the packet to forward (p->payload points to IP header) |
||
268 | * @param iphdr the IP header of the input packet |
||
269 | * @param inp the netif on which this packet was received |
||
270 | */ |
||
271 | static void |
||
272 | ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) |
||
273 | { |
||
274 | struct netif *netif; |
||
275 | |||
276 | PERF_START; |
||
277 | LWIP_UNUSED_ARG(inp); |
||
278 | |||
279 | if (!ip4_canforward(p)) { |
||
280 | goto return_noroute; |
||
281 | } |
||
282 | |||
283 | /* RFC3927 2.7: do not forward link-local addresses */ |
||
284 | if (ip4_addr_islinklocal(ip4_current_dest_addr())) { |
||
285 | LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
||
286 | ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), |
||
287 | ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); |
||
288 | goto return_noroute; |
||
289 | } |
||
290 | |||
291 | /* Find network interface where to forward this IP packet to. */ |
||
292 | netif = ip4_route_src(ip4_current_src_addr(), ip4_current_dest_addr()); |
||
293 | if (netif == NULL) { |
||
294 | LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n", |
||
295 | ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), |
||
296 | ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); |
||
297 | /* @todo: send ICMP_DUR_NET? */ |
||
298 | goto return_noroute; |
||
299 | } |
||
300 | #if !IP_FORWARD_ALLOW_TX_ON_RX_NETIF |
||
301 | /* Do not forward packets onto the same network interface on which |
||
302 | * they arrived. */ |
||
303 | if (netif == inp) { |
||
304 | LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not bouncing packets back on incoming interface.\n")); |
||
305 | goto return_noroute; |
||
306 | } |
||
307 | #endif /* IP_FORWARD_ALLOW_TX_ON_RX_NETIF */ |
||
308 | |||
309 | /* decrement TTL */ |
||
310 | IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1); |
||
311 | /* send ICMP if TTL == 0 */ |
||
312 | if (IPH_TTL(iphdr) == 0) { |
||
313 | MIB2_STATS_INC(mib2.ipinhdrerrors); |
||
314 | #if LWIP_ICMP |
||
315 | /* Don't send ICMP messages in response to ICMP messages */ |
||
316 | if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) { |
||
317 | icmp_time_exceeded(p, ICMP_TE_TTL); |
||
318 | } |
||
319 | #endif /* LWIP_ICMP */ |
||
320 | return; |
||
321 | } |
||
322 | |||
323 | /* Incrementally update the IP checksum. */ |
||
324 | if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) { |
||
325 | IPH_CHKSUM_SET(iphdr, (u16_t)(IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1)); |
||
326 | } else { |
||
327 | IPH_CHKSUM_SET(iphdr, (u16_t)(IPH_CHKSUM(iphdr) + PP_HTONS(0x100))); |
||
328 | } |
||
329 | |||
330 | LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
||
331 | ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), |
||
332 | ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); |
||
333 | |||
334 | IP_STATS_INC(ip.fw); |
||
335 | MIB2_STATS_INC(mib2.ipforwdatagrams); |
||
336 | IP_STATS_INC(ip.xmit); |
||
337 | |||
338 | PERF_STOP("ip4_forward"); |
||
339 | /* don't fragment if interface has mtu set to 0 [loopif] */ |
||
340 | if (netif->mtu && (p->tot_len > netif->mtu)) { |
||
341 | if ((IPH_OFFSET(iphdr) & PP_NTOHS(IP_DF)) == 0) { |
||
342 | #if IP_FRAG |
||
343 | ip4_frag(p, netif, ip4_current_dest_addr()); |
||
344 | #else /* IP_FRAG */ |
||
345 | /* @todo: send ICMP Destination Unreachable code 13 "Communication administratively prohibited"? */ |
||
346 | #endif /* IP_FRAG */ |
||
347 | } else { |
||
348 | #if LWIP_ICMP |
||
349 | /* send ICMP Destination Unreachable code 4: "Fragmentation Needed and DF Set" */ |
||
350 | icmp_dest_unreach(p, ICMP_DUR_FRAG); |
||
351 | #endif /* LWIP_ICMP */ |
||
352 | } |
||
353 | return; |
||
354 | } |
||
355 | /* transmit pbuf on chosen interface */ |
||
356 | netif->output(netif, p, ip4_current_dest_addr()); |
||
357 | return; |
||
358 | return_noroute: |
||
359 | MIB2_STATS_INC(mib2.ipoutnoroutes); |
||
360 | } |
||
361 | #endif /* IP_FORWARD */ |
||
362 | |||
363 | /** Return true if the current input packet should be accepted on this netif */ |
||
364 | static int |
||
365 | ip4_input_accept(struct netif *netif) |
||
366 | { |
||
367 | LWIP_DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest 0x%"X32_F" netif->ip_addr 0x%"X32_F" (0x%"X32_F", 0x%"X32_F", 0x%"X32_F")\n", |
||
368 | ip4_addr_get_u32(ip4_current_dest_addr()), ip4_addr_get_u32(netif_ip4_addr(netif)), |
||
369 | ip4_addr_get_u32(ip4_current_dest_addr()) & ip4_addr_get_u32(netif_ip4_netmask(netif)), |
||
370 | ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif)), |
||
371 | ip4_addr_get_u32(ip4_current_dest_addr()) & ~ip4_addr_get_u32(netif_ip4_netmask(netif)))); |
||
372 | |||
373 | /* interface is up and configured? */ |
||
374 | if ((netif_is_up(netif)) && (!ip4_addr_isany_val(*netif_ip4_addr(netif)))) { |
||
375 | /* unicast to this interface address? */ |
||
376 | if (ip4_addr_cmp(ip4_current_dest_addr(), netif_ip4_addr(netif)) || |
||
377 | /* or broadcast on this interface network address? */ |
||
378 | ip4_addr_isbroadcast(ip4_current_dest_addr(), netif) |
||
379 | #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF |
||
380 | || (ip4_addr_get_u32(ip4_current_dest_addr()) == PP_HTONL(IPADDR_LOOPBACK)) |
||
381 | #endif /* LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF */ |
||
382 | ) { |
||
383 | LWIP_DEBUGF(IP_DEBUG, ("ip4_input: packet accepted on interface %c%c\n", |
||
384 | netif->name[0], netif->name[1])); |
||
385 | /* accept on this netif */ |
||
386 | return 1; |
||
387 | } |
||
388 | #if LWIP_AUTOIP |
||
389 | /* connections to link-local addresses must persist after changing |
||
390 | the netif's address (RFC3927 ch. 1.9) */ |
||
391 | if (autoip_accept_packet(netif, ip4_current_dest_addr())) { |
||
392 | LWIP_DEBUGF(IP_DEBUG, ("ip4_input: LLA packet accepted on interface %c%c\n", |
||
393 | netif->name[0], netif->name[1])); |
||
394 | /* accept on this netif */ |
||
395 | return 1; |
||
396 | } |
||
397 | #endif /* LWIP_AUTOIP */ |
||
398 | } |
||
399 | return 0; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * This function is called by the network interface device driver when |
||
404 | * an IP packet is received. The function does the basic checks of the |
||
405 | * IP header such as packet size being at least larger than the header |
||
406 | * size etc. If the packet was not destined for us, the packet is |
||
407 | * forwarded (using ip_forward). The IP checksum is always checked. |
||
408 | * |
||
409 | * Finally, the packet is sent to the upper layer protocol input function. |
||
410 | * |
||
411 | * @param p the received IP packet (p->payload points to IP header) |
||
412 | * @param inp the netif on which this packet was received |
||
413 | * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't |
||
414 | * processed, but currently always returns ERR_OK) |
||
415 | */ |
||
416 | err_t |
||
417 | ip4_input(struct pbuf *p, struct netif *inp) |
||
418 | { |
||
419 | const struct ip_hdr *iphdr; |
||
420 | struct netif *netif; |
||
421 | u16_t iphdr_hlen; |
||
422 | u16_t iphdr_len; |
||
423 | #if IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP |
||
424 | int check_ip_src = 1; |
||
425 | #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP */ |
||
426 | |||
427 | IP_STATS_INC(ip.recv); |
||
428 | MIB2_STATS_INC(mib2.ipinreceives); |
||
429 | |||
430 | /* identify the IP header */ |
||
431 | iphdr = (struct ip_hdr *)p->payload; |
||
432 | if (IPH_V(iphdr) != 4) { |
||
433 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", (u16_t)IPH_V(iphdr))); |
||
434 | ip4_debug_print(p); |
||
435 | pbuf_free(p); |
||
436 | IP_STATS_INC(ip.err); |
||
437 | IP_STATS_INC(ip.drop); |
||
438 | MIB2_STATS_INC(mib2.ipinhdrerrors); |
||
439 | return ERR_OK; |
||
440 | } |
||
441 | |||
442 | #ifdef LWIP_HOOK_IP4_INPUT |
||
443 | if (LWIP_HOOK_IP4_INPUT(p, inp)) { |
||
444 | /* the packet has been eaten */ |
||
445 | return ERR_OK; |
||
446 | } |
||
447 | #endif |
||
448 | |||
449 | /* obtain IP header length in bytes */ |
||
450 | iphdr_hlen = IPH_HL_BYTES(iphdr); |
||
451 | /* obtain ip length in bytes */ |
||
452 | iphdr_len = lwip_ntohs(IPH_LEN(iphdr)); |
||
453 | |||
454 | /* Trim pbuf. This is especially required for packets < 60 bytes. */ |
||
455 | if (iphdr_len < p->tot_len) { |
||
456 | pbuf_realloc(p, iphdr_len); |
||
457 | } |
||
458 | |||
459 | /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */ |
||
460 | if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len) || (iphdr_hlen < IP_HLEN)) { |
||
461 | if (iphdr_hlen < IP_HLEN) { |
||
462 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
||
463 | ("ip4_input: short IP header (%"U16_F" bytes) received, IP packet dropped\n", iphdr_hlen)); |
||
464 | } |
||
465 | if (iphdr_hlen > p->len) { |
||
466 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
||
467 | ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n", |
||
468 | iphdr_hlen, p->len)); |
||
469 | } |
||
470 | if (iphdr_len > p->tot_len) { |
||
471 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
||
472 | ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n", |
||
473 | iphdr_len, p->tot_len)); |
||
474 | } |
||
475 | /* free (drop) packet pbufs */ |
||
476 | pbuf_free(p); |
||
477 | IP_STATS_INC(ip.lenerr); |
||
478 | IP_STATS_INC(ip.drop); |
||
479 | MIB2_STATS_INC(mib2.ipindiscards); |
||
480 | return ERR_OK; |
||
481 | } |
||
482 | |||
483 | /* verify checksum */ |
||
484 | #if CHECKSUM_CHECK_IP |
||
485 | IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_IP) { |
||
486 | if (inet_chksum(iphdr, iphdr_hlen) != 0) { |
||
487 | |||
488 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, |
||
489 | ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen))); |
||
490 | ip4_debug_print(p); |
||
491 | pbuf_free(p); |
||
492 | IP_STATS_INC(ip.chkerr); |
||
493 | IP_STATS_INC(ip.drop); |
||
494 | MIB2_STATS_INC(mib2.ipinhdrerrors); |
||
495 | return ERR_OK; |
||
496 | } |
||
497 | } |
||
498 | #endif |
||
499 | |||
500 | /* copy IP addresses to aligned ip_addr_t */ |
||
501 | ip_addr_copy_from_ip4(ip_data.current_iphdr_dest, iphdr->dest); |
||
502 | ip_addr_copy_from_ip4(ip_data.current_iphdr_src, iphdr->src); |
||
503 | |||
504 | /* match packet against an interface, i.e. is this packet for us? */ |
||
505 | if (ip4_addr_ismulticast(ip4_current_dest_addr())) { |
||
506 | #if LWIP_IGMP |
||
507 | if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, ip4_current_dest_addr()))) { |
||
508 | /* IGMP snooping switches need 0.0.0.0 to be allowed as source address (RFC 4541) */ |
||
509 | ip4_addr_t allsystems; |
||
510 | IP4_ADDR(&allsystems, 224, 0, 0, 1); |
||
511 | if (ip4_addr_cmp(ip4_current_dest_addr(), &allsystems) && |
||
512 | ip4_addr_isany(ip4_current_src_addr())) { |
||
513 | check_ip_src = 0; |
||
514 | } |
||
515 | netif = inp; |
||
516 | } else { |
||
517 | netif = NULL; |
||
518 | } |
||
519 | #else /* LWIP_IGMP */ |
||
520 | if ((netif_is_up(inp)) && (!ip4_addr_isany_val(*netif_ip4_addr(inp)))) { |
||
521 | netif = inp; |
||
522 | } else { |
||
523 | netif = NULL; |
||
524 | } |
||
525 | #endif /* LWIP_IGMP */ |
||
526 | } else { |
||
527 | /* start trying with inp. if that's not acceptable, start walking the |
||
528 | list of configured netifs. */ |
||
529 | if (ip4_input_accept(inp)) { |
||
530 | netif = inp; |
||
531 | } else { |
||
532 | netif = NULL; |
||
533 | #if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF |
||
534 | /* Packets sent to the loopback address must not be accepted on an |
||
535 | * interface that does not have the loopback address assigned to it, |
||
536 | * unless a non-loopback interface is used for loopback traffic. */ |
||
537 | if (!ip4_addr_isloopback(ip4_current_dest_addr())) |
||
538 | #endif /* !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF */ |
||
539 | { |
||
540 | #if !LWIP_SINGLE_NETIF |
||
541 | NETIF_FOREACH(netif) { |
||
542 | if (netif == inp) { |
||
543 | /* we checked that before already */ |
||
544 | continue; |
||
545 | } |
||
546 | if (ip4_input_accept(netif)) { |
||
547 | break; |
||
548 | } |
||
549 | } |
||
550 | #endif /* !LWIP_SINGLE_NETIF */ |
||
551 | } |
||
552 | } |
||
553 | } |
||
554 | |||
555 | #if IP_ACCEPT_LINK_LAYER_ADDRESSING |
||
556 | /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed |
||
557 | * using link layer addressing (such as Ethernet MAC) so we must not filter on IP. |
||
558 | * According to RFC 1542 section 3.1.1, referred by RFC 2131). |
||
559 | * |
||
560 | * If you want to accept private broadcast communication while a netif is down, |
||
561 | * define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.: |
||
562 | * |
||
563 | * #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345)) |
||
564 | */ |
||
565 | if (netif == NULL) { |
||
566 | /* remote port is DHCP server? */ |
||
567 | if (IPH_PROTO(iphdr) == IP_PROTO_UDP) { |
||
568 | const struct udp_hdr *udphdr = (const struct udp_hdr *)((const u8_t *)iphdr + iphdr_hlen); |
||
569 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: UDP packet to DHCP client port %"U16_F"\n", |
||
570 | lwip_ntohs(udphdr->dest))); |
||
571 | if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) { |
||
572 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: DHCP packet accepted.\n")); |
||
573 | netif = inp; |
||
574 | check_ip_src = 0; |
||
575 | } |
||
576 | } |
||
577 | } |
||
578 | #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */ |
||
579 | |||
580 | /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */ |
||
581 | #if LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING |
||
582 | if (check_ip_src |
||
583 | #if IP_ACCEPT_LINK_LAYER_ADDRESSING |
||
584 | /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */ |
||
585 | && !ip4_addr_isany_val(*ip4_current_src_addr()) |
||
586 | #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */ |
||
587 | ) |
||
588 | #endif /* LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING */ |
||
589 | { |
||
590 | if ((ip4_addr_isbroadcast(ip4_current_src_addr(), inp)) || |
||
591 | (ip4_addr_ismulticast(ip4_current_src_addr()))) { |
||
592 | /* packet source is not valid */ |
||
593 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip4_input: packet source is not valid.\n")); |
||
594 | /* free (drop) packet pbufs */ |
||
595 | pbuf_free(p); |
||
596 | IP_STATS_INC(ip.drop); |
||
597 | MIB2_STATS_INC(mib2.ipinaddrerrors); |
||
598 | MIB2_STATS_INC(mib2.ipindiscards); |
||
599 | return ERR_OK; |
||
600 | } |
||
601 | } |
||
602 | |||
603 | /* if we're pretending we are everyone for TCP, assume the packet is for source interface if it |
||
604 | isn't for a local address */ |
||
605 | if (netif == NULL && (inp->flags & NETIF_FLAG_PRETEND_TCP) && IPH_PROTO(iphdr) == IP_PROTO_TCP) { |
||
606 | netif = inp; |
||
607 | } |
||
608 | |||
609 | /* packet not for us? */ |
||
610 | if (netif == NULL) { |
||
611 | /* packet not for us, route or discard */ |
||
612 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip4_input: packet not for us.\n")); |
||
613 | #if IP_FORWARD |
||
614 | /* non-broadcast packet? */ |
||
615 | if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), inp)) { |
||
616 | /* try to forward IP packet on (other) interfaces */ |
||
617 | ip4_forward(p, (struct ip_hdr *)p->payload, inp); |
||
618 | } else |
||
619 | #endif /* IP_FORWARD */ |
||
620 | { |
||
621 | IP_STATS_INC(ip.drop); |
||
622 | MIB2_STATS_INC(mib2.ipinaddrerrors); |
||
623 | MIB2_STATS_INC(mib2.ipindiscards); |
||
624 | } |
||
625 | pbuf_free(p); |
||
626 | return ERR_OK; |
||
627 | } |
||
628 | /* packet consists of multiple fragments? */ |
||
629 | if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) { |
||
630 | #if IP_REASSEMBLY /* packet fragment reassembly code present? */ |
||
631 | LWIP_DEBUGF(IP_DEBUG, ("IP packet is a fragment (id=0x%04"X16_F" tot_len=%"U16_F" len=%"U16_F" MF=%"U16_F" offset=%"U16_F"), calling ip4_reass()\n", |
||
632 | lwip_ntohs(IPH_ID(iphdr)), p->tot_len, lwip_ntohs(IPH_LEN(iphdr)), (u16_t)!!(IPH_OFFSET(iphdr) & PP_HTONS(IP_MF)), (u16_t)((lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK) * 8))); |
||
633 | /* reassemble the packet*/ |
||
634 | p = ip4_reass(p); |
||
635 | /* packet not fully reassembled yet? */ |
||
636 | if (p == NULL) { |
||
637 | return ERR_OK; |
||
638 | } |
||
639 | iphdr = (const struct ip_hdr *)p->payload; |
||
640 | #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */ |
||
641 | pbuf_free(p); |
||
642 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n", |
||
643 | lwip_ntohs(IPH_OFFSET(iphdr)))); |
||
644 | IP_STATS_INC(ip.opterr); |
||
645 | IP_STATS_INC(ip.drop); |
||
646 | /* unsupported protocol feature */ |
||
647 | MIB2_STATS_INC(mib2.ipinunknownprotos); |
||
648 | return ERR_OK; |
||
649 | #endif /* IP_REASSEMBLY */ |
||
650 | } |
||
651 | |||
652 | #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */ |
||
653 | |||
654 | #if LWIP_IGMP |
||
655 | /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */ |
||
656 | if ((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) { |
||
657 | #else |
||
658 | if (iphdr_hlen > IP_HLEN) { |
||
659 | #endif /* LWIP_IGMP */ |
||
660 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n")); |
||
661 | pbuf_free(p); |
||
662 | IP_STATS_INC(ip.opterr); |
||
663 | IP_STATS_INC(ip.drop); |
||
664 | /* unsupported protocol feature */ |
||
665 | MIB2_STATS_INC(mib2.ipinunknownprotos); |
||
666 | return ERR_OK; |
||
667 | } |
||
668 | #endif /* IP_OPTIONS_ALLOWED == 0 */ |
||
669 | |||
670 | /* send to upper layers */ |
||
671 | LWIP_DEBUGF(IP_DEBUG, ("ip4_input: \n")); |
||
672 | ip4_debug_print(p); |
||
673 | LWIP_DEBUGF(IP_DEBUG, ("ip4_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len)); |
||
674 | |||
675 | ip_data.current_netif = netif; |
||
676 | ip_data.current_input_netif = inp; |
||
677 | ip_data.current_ip4_header = iphdr; |
||
678 | ip_data.current_ip_header_tot_len = IPH_HL_BYTES(iphdr); |
||
679 | |||
680 | #if LWIP_RAW |
||
681 | /* raw input did not eat the packet? */ |
||
682 | if (raw_input(p, inp) == 0) |
||
683 | #endif /* LWIP_RAW */ |
||
684 | { |
||
685 | pbuf_remove_header(p, iphdr_hlen); /* Move to payload, no check necessary. */ |
||
686 | |||
687 | switch (IPH_PROTO(iphdr)) { |
||
688 | #if LWIP_UDP |
||
689 | case IP_PROTO_UDP: |
||
690 | #if LWIP_UDPLITE |
||
691 | case IP_PROTO_UDPLITE: |
||
692 | #endif /* LWIP_UDPLITE */ |
||
693 | MIB2_STATS_INC(mib2.ipindelivers); |
||
694 | udp_input(p, inp); |
||
695 | break; |
||
696 | #endif /* LWIP_UDP */ |
||
697 | #if LWIP_TCP |
||
698 | case IP_PROTO_TCP: |
||
699 | MIB2_STATS_INC(mib2.ipindelivers); |
||
700 | tcp_input(p, inp); |
||
701 | break; |
||
702 | #endif /* LWIP_TCP */ |
||
703 | #if LWIP_ICMP |
||
704 | case IP_PROTO_ICMP: |
||
705 | MIB2_STATS_INC(mib2.ipindelivers); |
||
706 | icmp_input(p, inp); |
||
707 | break; |
||
708 | #endif /* LWIP_ICMP */ |
||
709 | #if LWIP_IGMP |
||
710 | case IP_PROTO_IGMP: |
||
711 | igmp_input(p, inp, ip4_current_dest_addr()); |
||
712 | break; |
||
713 | #endif /* LWIP_IGMP */ |
||
714 | default: |
||
715 | #if LWIP_ICMP |
||
716 | /* send ICMP destination protocol unreachable unless is was a broadcast */ |
||
717 | if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), netif) && |
||
718 | !ip4_addr_ismulticast(ip4_current_dest_addr())) { |
||
719 | pbuf_header_force(p, (s16_t)iphdr_hlen); /* Move to ip header, no check necessary. */ |
||
720 | icmp_dest_unreach(p, ICMP_DUR_PROTO); |
||
721 | } |
||
722 | #endif /* LWIP_ICMP */ |
||
723 | pbuf_free(p); |
||
724 | |||
725 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", (u16_t)IPH_PROTO(iphdr))); |
||
726 | |||
727 | IP_STATS_INC(ip.proterr); |
||
728 | IP_STATS_INC(ip.drop); |
||
729 | MIB2_STATS_INC(mib2.ipinunknownprotos); |
||
730 | } |
||
731 | } |
||
732 | |||
733 | /* @todo: this is not really necessary... */ |
||
734 | ip_data.current_netif = NULL; |
||
735 | ip_data.current_input_netif = NULL; |
||
736 | ip_data.current_ip4_header = NULL; |
||
737 | ip_data.current_ip_header_tot_len = 0; |
||
738 | ip4_addr_set_any(ip4_current_src_addr()); |
||
739 | ip4_addr_set_any(ip4_current_dest_addr()); |
||
740 | |||
741 | return ERR_OK; |
||
742 | } |
||
743 | |||
744 | /** |
||
745 | * Sends an IP packet on a network interface. This function constructs |
||
746 | * the IP header and calculates the IP header checksum. If the source |
||
747 | * IP address is NULL, the IP address of the outgoing network |
||
748 | * interface is filled in as source address. |
||
749 | * If the destination IP address is LWIP_IP_HDRINCL, p is assumed to already |
||
750 | * include an IP header and p->payload points to it instead of the data. |
||
751 | * |
||
752 | * @param p the packet to send (p->payload points to the data, e.g. next |
||
753 | protocol header; if dest == LWIP_IP_HDRINCL, p already includes an |
||
754 | IP header and p->payload points to that IP header) |
||
755 | * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the |
||
756 | * IP address of the netif used to send is used as source address) |
||
757 | * @param dest the destination IP address to send the packet to |
||
758 | * @param ttl the TTL value to be set in the IP header |
||
759 | * @param tos the TOS value to be set in the IP header |
||
760 | * @param proto the PROTOCOL to be set in the IP header |
||
761 | * @param netif the netif on which to send this packet |
||
762 | * @return ERR_OK if the packet was sent OK |
||
763 | * ERR_BUF if p doesn't have enough space for IP/LINK headers |
||
764 | * returns errors returned by netif->output |
||
765 | * |
||
766 | * @note ip_id: RFC791 "some host may be able to simply use |
||
767 | * unique identifiers independent of destination" |
||
768 | */ |
||
769 | err_t |
||
770 | ip4_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, |
||
771 | u8_t ttl, u8_t tos, |
||
772 | u8_t proto, struct netif *netif) |
||
773 | { |
||
774 | #if IP_OPTIONS_SEND |
||
775 | return ip4_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0); |
||
776 | } |
||
777 | |||
778 | /** |
||
779 | * Same as ip_output_if() but with the possibility to include IP options: |
||
780 | * |
||
781 | * @ param ip_options pointer to the IP options, copied into the IP header |
||
782 | * @ param optlen length of ip_options |
||
783 | */ |
||
784 | err_t |
||
785 | ip4_output_if_opt(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, |
||
786 | u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options, |
||
787 | u16_t optlen) |
||
788 | { |
||
789 | #endif /* IP_OPTIONS_SEND */ |
||
790 | const ip4_addr_t *src_used = src; |
||
791 | if (dest != LWIP_IP_HDRINCL) { |
||
792 | if (ip4_addr_isany(src)) { |
||
793 | src_used = netif_ip4_addr(netif); |
||
794 | } |
||
795 | } |
||
796 | |||
797 | #if IP_OPTIONS_SEND |
||
798 | return ip4_output_if_opt_src(p, src_used, dest, ttl, tos, proto, netif, |
||
799 | ip_options, optlen); |
||
800 | #else /* IP_OPTIONS_SEND */ |
||
801 | return ip4_output_if_src(p, src_used, dest, ttl, tos, proto, netif); |
||
802 | #endif /* IP_OPTIONS_SEND */ |
||
803 | } |
||
804 | |||
805 | /** |
||
806 | * Same as ip_output_if() but 'src' address is not replaced by netif address |
||
807 | * when it is 'any'. |
||
808 | */ |
||
809 | err_t |
||
810 | ip4_output_if_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, |
||
811 | u8_t ttl, u8_t tos, |
||
812 | u8_t proto, struct netif *netif) |
||
813 | { |
||
814 | #if IP_OPTIONS_SEND |
||
815 | return ip4_output_if_opt_src(p, src, dest, ttl, tos, proto, netif, NULL, 0); |
||
816 | } |
||
817 | |||
818 | /** |
||
819 | * Same as ip_output_if_opt() but 'src' address is not replaced by netif address |
||
820 | * when it is 'any'. |
||
821 | */ |
||
822 | err_t |
||
823 | ip4_output_if_opt_src(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, |
||
824 | u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options, |
||
825 | u16_t optlen) |
||
826 | { |
||
827 | #endif /* IP_OPTIONS_SEND */ |
||
828 | struct ip_hdr *iphdr; |
||
829 | ip4_addr_t dest_addr; |
||
830 | #if CHECKSUM_GEN_IP_INLINE |
||
831 | u32_t chk_sum = 0; |
||
832 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
||
833 | |||
834 | LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p); |
||
835 | |||
836 | MIB2_STATS_INC(mib2.ipoutrequests); |
||
837 | |||
838 | /* Should the IP header be generated or is it already included in p? */ |
||
839 | if (dest != LWIP_IP_HDRINCL) { |
||
840 | u16_t ip_hlen = IP_HLEN; |
||
841 | #if IP_OPTIONS_SEND |
||
842 | u16_t optlen_aligned = 0; |
||
843 | if (optlen != 0) { |
||
844 | #if CHECKSUM_GEN_IP_INLINE |
||
845 | int i; |
||
846 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
||
847 | if (optlen > (IP_HLEN_MAX - IP_HLEN)) { |
||
848 | /* optlen too long */ |
||
849 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: optlen too long\n")); |
||
850 | IP_STATS_INC(ip.err); |
||
851 | MIB2_STATS_INC(mib2.ipoutdiscards); |
||
852 | return ERR_VAL; |
||
853 | } |
||
854 | /* round up to a multiple of 4 */ |
||
855 | optlen_aligned = (u16_t)((optlen + 3) & ~3); |
||
856 | ip_hlen = (u16_t)(ip_hlen + optlen_aligned); |
||
857 | /* First write in the IP options */ |
||
858 | if (pbuf_add_header(p, optlen_aligned)) { |
||
859 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output_if_opt: not enough room for IP options in pbuf\n")); |
||
860 | IP_STATS_INC(ip.err); |
||
861 | MIB2_STATS_INC(mib2.ipoutdiscards); |
||
862 | return ERR_BUF; |
||
863 | } |
||
864 | MEMCPY(p->payload, ip_options, optlen); |
||
865 | if (optlen < optlen_aligned) { |
||
866 | /* zero the remaining bytes */ |
||
867 | memset(((char *)p->payload) + optlen, 0, (size_t)(optlen_aligned - optlen)); |
||
868 | } |
||
869 | #if CHECKSUM_GEN_IP_INLINE |
||
870 | for (i = 0; i < optlen_aligned / 2; i++) { |
||
871 | chk_sum += ((u16_t *)p->payload)[i]; |
||
872 | } |
||
873 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
||
874 | } |
||
875 | #endif /* IP_OPTIONS_SEND */ |
||
876 | /* generate IP header */ |
||
877 | if (pbuf_add_header(p, IP_HLEN)) { |
||
878 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: not enough room for IP header in pbuf\n")); |
||
879 | |||
880 | IP_STATS_INC(ip.err); |
||
881 | MIB2_STATS_INC(mib2.ipoutdiscards); |
||
882 | return ERR_BUF; |
||
883 | } |
||
884 | |||
885 | iphdr = (struct ip_hdr *)p->payload; |
||
886 | LWIP_ASSERT("check that first pbuf can hold struct ip_hdr", |
||
887 | (p->len >= sizeof(struct ip_hdr))); |
||
888 | |||
889 | IPH_TTL_SET(iphdr, ttl); |
||
890 | IPH_PROTO_SET(iphdr, proto); |
||
891 | #if CHECKSUM_GEN_IP_INLINE |
||
892 | chk_sum += PP_NTOHS(proto | (ttl << 8)); |
||
893 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
||
894 | |||
895 | /* dest cannot be NULL here */ |
||
896 | ip4_addr_copy(iphdr->dest, *dest); |
||
897 | #if CHECKSUM_GEN_IP_INLINE |
||
898 | chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF; |
||
899 | chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16; |
||
900 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
||
901 | |||
902 | IPH_VHL_SET(iphdr, 4, ip_hlen / 4); |
||
903 | IPH_TOS_SET(iphdr, tos); |
||
904 | #if CHECKSUM_GEN_IP_INLINE |
||
905 | chk_sum += PP_NTOHS(tos | (iphdr->_v_hl << 8)); |
||
906 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
||
907 | IPH_LEN_SET(iphdr, lwip_htons(p->tot_len)); |
||
908 | #if CHECKSUM_GEN_IP_INLINE |
||
909 | chk_sum += iphdr->_len; |
||
910 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
||
911 | IPH_OFFSET_SET(iphdr, 0); |
||
912 | IPH_ID_SET(iphdr, lwip_htons(ip_id)); |
||
913 | #if CHECKSUM_GEN_IP_INLINE |
||
914 | chk_sum += iphdr->_id; |
||
915 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
||
916 | ++ip_id; |
||
917 | |||
918 | if (src == NULL) { |
||
919 | ip4_addr_copy(iphdr->src, *IP4_ADDR_ANY4); |
||
920 | } else { |
||
921 | /* src cannot be NULL here */ |
||
922 | ip4_addr_copy(iphdr->src, *src); |
||
923 | } |
||
924 | |||
925 | #if CHECKSUM_GEN_IP_INLINE |
||
926 | chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF; |
||
927 | chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16; |
||
928 | chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF); |
||
929 | chk_sum = (chk_sum >> 16) + chk_sum; |
||
930 | chk_sum = ~chk_sum; |
||
931 | IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) { |
||
932 | iphdr->_chksum = (u16_t)chk_sum; /* network order */ |
||
933 | } |
||
934 | #if LWIP_CHECKSUM_CTRL_PER_NETIF |
||
935 | else { |
||
936 | IPH_CHKSUM_SET(iphdr, 0); |
||
937 | } |
||
938 | #endif /* LWIP_CHECKSUM_CTRL_PER_NETIF*/ |
||
939 | #else /* CHECKSUM_GEN_IP_INLINE */ |
||
940 | IPH_CHKSUM_SET(iphdr, 0); |
||
941 | #if CHECKSUM_GEN_IP |
||
942 | IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) { |
||
943 | IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen)); |
||
944 | } |
||
945 | #endif /* CHECKSUM_GEN_IP */ |
||
946 | #endif /* CHECKSUM_GEN_IP_INLINE */ |
||
947 | } else { |
||
948 | /* IP header already included in p */ |
||
949 | if (p->len < IP_HLEN) { |
||
950 | LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip4_output: LWIP_IP_HDRINCL but pbuf is too short\n")); |
||
951 | IP_STATS_INC(ip.err); |
||
952 | MIB2_STATS_INC(mib2.ipoutdiscards); |
||
953 | return ERR_BUF; |
||
954 | } |
||
955 | iphdr = (struct ip_hdr *)p->payload; |
||
956 | ip4_addr_copy(dest_addr, iphdr->dest); |
||
957 | dest = &dest_addr; |
||
958 | } |
||
959 | |||
960 | IP_STATS_INC(ip.xmit); |
||
961 | |||
962 | LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], (u16_t)netif->num)); |
||
963 | ip4_debug_print(p); |
||
964 | |||
965 | #if ENABLE_LOOPBACK |
||
966 | if (ip4_addr_cmp(dest, netif_ip4_addr(netif)) |
||
967 | #if !LWIP_HAVE_LOOPIF |
||
968 | || ip4_addr_isloopback(dest) |
||
969 | #endif /* !LWIP_HAVE_LOOPIF */ |
||
970 | ) { |
||
971 | /* Packet to self, enqueue it for loopback */ |
||
972 | LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()")); |
||
973 | return netif_loop_output(netif, p); |
||
974 | } |
||
975 | #if LWIP_MULTICAST_TX_OPTIONS |
||
976 | if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) { |
||
977 | netif_loop_output(netif, p); |
||
978 | } |
||
979 | #endif /* LWIP_MULTICAST_TX_OPTIONS */ |
||
980 | #endif /* ENABLE_LOOPBACK */ |
||
981 | #if IP_FRAG |
||
982 | /* don't fragment if interface has mtu set to 0 [loopif] */ |
||
983 | if (netif->mtu && (p->tot_len > netif->mtu)) { |
||
984 | return ip4_frag(p, netif, dest); |
||
985 | } |
||
986 | #endif /* IP_FRAG */ |
||
987 | |||
988 | LWIP_DEBUGF(IP_DEBUG, ("ip4_output_if: call netif->output()\n")); |
||
989 | return netif->output(netif, p, dest); |
||
990 | } |
||
991 | |||
992 | /** |
||
993 | * Simple interface to ip_output_if. It finds the outgoing network |
||
994 | * interface and calls upon ip_output_if to do the actual work. |
||
995 | * |
||
996 | * @param p the packet to send (p->payload points to the data, e.g. next |
||
997 | protocol header; if dest == LWIP_IP_HDRINCL, p already includes an |
||
998 | IP header and p->payload points to that IP header) |
||
999 | * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the |
||
1000 | * IP address of the netif used to send is used as source address) |
||
1001 | * @param dest the destination IP address to send the packet to |
||
1002 | * @param ttl the TTL value to be set in the IP header |
||
1003 | * @param tos the TOS value to be set in the IP header |
||
1004 | * @param proto the PROTOCOL to be set in the IP header |
||
1005 | * |
||
1006 | * @return ERR_RTE if no route is found |
||
1007 | * see ip_output_if() for more return values |
||
1008 | */ |
||
1009 | err_t |
||
1010 | ip4_output(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, |
||
1011 | u8_t ttl, u8_t tos, u8_t proto) |
||
1012 | { |
||
1013 | struct netif *netif; |
||
1014 | |||
1015 | LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p); |
||
1016 | |||
1017 | if ((netif = ip4_route_src(src, dest)) == NULL) { |
||
1018 | LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
||
1019 | ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest))); |
||
1020 | IP_STATS_INC(ip.rterr); |
||
1021 | return ERR_RTE; |
||
1022 | } |
||
1023 | |||
1024 | return ip4_output_if(p, src, dest, ttl, tos, proto, netif); |
||
1025 | } |
||
1026 | |||
1027 | #if LWIP_NETIF_USE_HINTS |
||
1028 | /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint |
||
1029 | * before calling ip_output_if. |
||
1030 | * |
||
1031 | * @param p the packet to send (p->payload points to the data, e.g. next |
||
1032 | protocol header; if dest == LWIP_IP_HDRINCL, p already includes an |
||
1033 | IP header and p->payload points to that IP header) |
||
1034 | * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the |
||
1035 | * IP address of the netif used to send is used as source address) |
||
1036 | * @param dest the destination IP address to send the packet to |
||
1037 | * @param ttl the TTL value to be set in the IP header |
||
1038 | * @param tos the TOS value to be set in the IP header |
||
1039 | * @param proto the PROTOCOL to be set in the IP header |
||
1040 | * @param netif_hint netif output hint pointer set to netif->hint before |
||
1041 | * calling ip_output_if() |
||
1042 | * |
||
1043 | * @return ERR_RTE if no route is found |
||
1044 | * see ip_output_if() for more return values |
||
1045 | */ |
||
1046 | err_t |
||
1047 | ip4_output_hinted(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, |
||
1048 | u8_t ttl, u8_t tos, u8_t proto, struct netif_hint *netif_hint) |
||
1049 | { |
||
1050 | struct netif *netif; |
||
1051 | err_t err; |
||
1052 | |||
1053 | LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p); |
||
1054 | |||
1055 | if ((netif = ip4_route_src(src, dest)) == NULL) { |
||
1056 | LWIP_DEBUGF(IP_DEBUG, ("ip4_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
||
1057 | ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest))); |
||
1058 | IP_STATS_INC(ip.rterr); |
||
1059 | return ERR_RTE; |
||
1060 | } |
||
1061 | |||
1062 | NETIF_SET_HINTS(netif, netif_hint); |
||
1063 | err = ip4_output_if(p, src, dest, ttl, tos, proto, netif); |
||
1064 | NETIF_RESET_HINTS(netif); |
||
1065 | |||
1066 | return err; |
||
1067 | } |
||
1068 | #endif /* LWIP_NETIF_USE_HINTS*/ |
||
1069 | |||
1070 | #if IP_DEBUG |
||
1071 | /* Print an IP header by using LWIP_DEBUGF |
||
1072 | * @param p an IP packet, p->payload pointing to the IP header |
||
1073 | */ |
||
1074 | void |
||
1075 | ip4_debug_print(struct pbuf *p) |
||
1076 | { |
||
1077 | struct ip_hdr *iphdr = (struct ip_hdr *)p->payload; |
||
1078 | |||
1079 | LWIP_DEBUGF(IP_DEBUG, ("IP header:\n")); |
||
1080 | LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); |
||
1081 | LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n", |
||
1082 | (u16_t)IPH_V(iphdr), |
||
1083 | (u16_t)IPH_HL(iphdr), |
||
1084 | (u16_t)IPH_TOS(iphdr), |
||
1085 | lwip_ntohs(IPH_LEN(iphdr)))); |
||
1086 | LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); |
||
1087 | LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n", |
||
1088 | lwip_ntohs(IPH_ID(iphdr)), |
||
1089 | (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 15 & 1), |
||
1090 | (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 14 & 1), |
||
1091 | (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 13 & 1), |
||
1092 | (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK))); |
||
1093 | LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); |
||
1094 | LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n", |
||
1095 | (u16_t)IPH_TTL(iphdr), |
||
1096 | (u16_t)IPH_PROTO(iphdr), |
||
1097 | lwip_ntohs(IPH_CHKSUM(iphdr)))); |
||
1098 | LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); |
||
1099 | LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n", |
||
1100 | ip4_addr1_16(&iphdr->src), |
||
1101 | ip4_addr2_16(&iphdr->src), |
||
1102 | ip4_addr3_16(&iphdr->src), |
||
1103 | ip4_addr4_16(&iphdr->src))); |
||
1104 | LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); |
||
1105 | LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n", |
||
1106 | ip4_addr1_16(&iphdr->dest), |
||
1107 | ip4_addr2_16(&iphdr->dest), |
||
1108 | ip4_addr3_16(&iphdr->dest), |
||
1109 | ip4_addr4_16(&iphdr->dest))); |
||
1110 | LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n")); |
||
1111 | } |
||
1112 | #endif /* IP_DEBUG */ |
||
1113 | |||
1114 | #endif /* LWIP_IPV4 */ |