- Fix the stupid mess we created when making this from the cmake branch
[reactos.git] / lib / drivers / lwip / src / core / ipv4 / ip.c
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 #include "lwip/ip.h"
43 #include "lwip/def.h"
44 #include "lwip/mem.h"
45 #include "lwip/ip_frag.h"
46 #include "lwip/inet_chksum.h"
47 #include "lwip/netif.h"
48 #include "lwip/icmp.h"
49 #include "lwip/igmp.h"
50 #include "lwip/raw.h"
51 #include "lwip/udp.h"
52 #include "lwip/tcp_impl.h"
53 #include "lwip/snmp.h"
54 #include "lwip/dhcp.h"
55 #include "lwip/autoip.h"
56 #include "lwip/stats.h"
57 #include "arch/perf.h"
58
59 #include <string.h>
60
61 /** Set this to 0 in the rare case of wanting to call an extra function to
62 * generate the IP checksum (in contrast to calculating it on-the-fly). */
63 #ifndef LWIP_INLINE_IP_CHKSUM
64 #define LWIP_INLINE_IP_CHKSUM 1
65 #endif
66 #if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP
67 #define CHECKSUM_GEN_IP_INLINE 1
68 #else
69 #define CHECKSUM_GEN_IP_INLINE 0
70 #endif
71
72 #if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT)
73 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 1
74
75 /** Some defines for DHCP to let link-layer-addressed packets through while the
76 * netif is down.
77 * To use this in your own application/protocol, define LWIP_IP_ACCEPT_UDP_PORT
78 * to return 1 if the port is accepted and 0 if the port is not accepted.
79 */
80 #if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT)
81 /* accept DHCP client port and custom port */
82 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(DHCP_CLIENT_PORT)) \
83 || (LWIP_IP_ACCEPT_UDP_PORT(port)))
84 #elif defined(LWIP_IP_ACCEPT_UDP_PORT) /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
85 /* accept custom port only */
86 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(dst_port))
87 #else /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
88 /* accept DHCP client port only */
89 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(DHCP_CLIENT_PORT))
90 #endif /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
91
92 #else /* LWIP_DHCP */
93 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 0
94 #endif /* LWIP_DHCP */
95
96 /**
97 * The interface that provided the packet for the current callback
98 * invocation.
99 */
100 struct netif *current_netif;
101
102 /**
103 * Header of the input packet currently being processed.
104 */
105 const struct ip_hdr *current_header;
106 /** Source IP address of current_header */
107 ip_addr_t current_iphdr_src;
108 /** Destination IP address of current_header */
109 ip_addr_t current_iphdr_dest;
110
111 /** The IP header ID of the next outgoing IP packet */
112 static u16_t ip_id;
113
114 /**
115 * Finds the appropriate network interface for a given IP address. It
116 * searches the list of network interfaces linearly. A match is found
117 * if the masked IP address of the network interface equals the masked
118 * IP address given to the function.
119 *
120 * @param dest the destination IP address for which to find the route
121 * @return the netif on which to send to reach dest
122 */
123 struct netif *
124 ip_route(ip_addr_t *dest)
125 {
126 struct netif *netif;
127
128 /* iterate through netifs */
129 for(netif = netif_list; netif != NULL; netif = netif->next) {
130 /* network mask matches? */
131 if (netif_is_up(netif)) {
132 if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
133 /* return netif on which to forward IP packet */
134 return netif;
135 }
136 }
137 }
138 if ((netif_default == NULL) || (!netif_is_up(netif_default))) {
139 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
140 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
141 IP_STATS_INC(ip.rterr);
142 snmp_inc_ipoutnoroutes();
143 return NULL;
144 }
145 /* no matching netif found, use default netif */
146 return netif_default;
147 }
148
149 #if IP_FORWARD
150 /**
151 * Forwards an IP packet. It finds an appropriate route for the
152 * packet, decrements the TTL value of the packet, adjusts the
153 * checksum and outputs the packet on the appropriate interface.
154 *
155 * @param p the packet to forward (p->payload points to IP header)
156 * @param iphdr the IP header of the input packet
157 * @param inp the netif on which this packet was received
158 */
159 static void
160 ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
161 {
162 struct netif *netif;
163
164 PERF_START;
165
166 /* RFC3927 2.7: do not forward link-local addresses */
167 if (ip_addr_islinklocal(&current_iphdr_dest)) {
168 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
169 ip4_addr1_16(&current_iphdr_dest), ip4_addr2_16(&current_iphdr_dest),
170 ip4_addr3_16(&current_iphdr_dest), ip4_addr4_16(&current_iphdr_dest)));
171 goto return_noroute;
172 }
173
174 /* Find network interface where to forward this IP packet to. */
175 netif = ip_route(&current_iphdr_dest);
176 if (netif == NULL) {
177 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n",
178 ip4_addr1_16(&current_iphdr_dest), ip4_addr2_16(&current_iphdr_dest),
179 ip4_addr3_16(&current_iphdr_dest), ip4_addr4_16(&current_iphdr_dest)));
180 goto return_noroute;
181 }
182 /* Do not forward packets onto the same network interface on which
183 * they arrived. */
184 if (netif == inp) {
185 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
186 goto return_noroute;
187 }
188
189 /* decrement TTL */
190 IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
191 /* send ICMP if TTL == 0 */
192 if (IPH_TTL(iphdr) == 0) {
193 snmp_inc_ipinhdrerrors();
194 #if LWIP_ICMP
195 /* Don't send ICMP messages in response to ICMP messages */
196 if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
197 icmp_time_exceeded(p, ICMP_TE_TTL);
198 }
199 #endif /* LWIP_ICMP */
200 return;
201 }
202
203 /* Incrementally update the IP checksum. */
204 if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) {
205 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1);
206 } else {
207 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100));
208 }
209
210 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
211 ip4_addr1_16(&current_iphdr_dest), ip4_addr2_16(&current_iphdr_dest),
212 ip4_addr3_16(&current_iphdr_dest), ip4_addr4_16(&current_iphdr_dest)));
213
214 IP_STATS_INC(ip.fw);
215 IP_STATS_INC(ip.xmit);
216 snmp_inc_ipforwdatagrams();
217
218 PERF_STOP("ip_forward");
219 /* transmit pbuf on chosen interface */
220 netif->output(netif, p, &current_iphdr_dest);
221 return;
222 return_noroute:
223 snmp_inc_ipoutnoroutes();
224 }
225 #endif /* IP_FORWARD */
226
227 /**
228 * This function is called by the network interface device driver when
229 * an IP packet is received. The function does the basic checks of the
230 * IP header such as packet size being at least larger than the header
231 * size etc. If the packet was not destined for us, the packet is
232 * forwarded (using ip_forward). The IP checksum is always checked.
233 *
234 * Finally, the packet is sent to the upper layer protocol input function.
235 *
236 * @param p the received IP packet (p->payload points to IP header)
237 * @param inp the netif on which this packet was received
238 * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
239 * processed, but currently always returns ERR_OK)
240 */
241 err_t
242 ip_input(struct pbuf *p, struct netif *inp)
243 {
244 struct ip_hdr *iphdr;
245 struct netif *netif;
246 u16_t iphdr_hlen;
247 u16_t iphdr_len;
248 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
249 int check_ip_src=1;
250 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
251
252 IP_STATS_INC(ip.recv);
253 snmp_inc_ipinreceives();
254
255 /* identify the IP header */
256 iphdr = (struct ip_hdr *)p->payload;
257 if (IPH_V(iphdr) != 4) {
258 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", IPH_V(iphdr)));
259 ip_debug_print(p);
260 pbuf_free(p);
261 IP_STATS_INC(ip.err);
262 IP_STATS_INC(ip.drop);
263 snmp_inc_ipinhdrerrors();
264 return ERR_OK;
265 }
266
267 /* obtain IP header length in number of 32-bit words */
268 iphdr_hlen = IPH_HL(iphdr);
269 /* calculate IP header length in bytes */
270 iphdr_hlen *= 4;
271 /* obtain ip length in bytes */
272 iphdr_len = ntohs(IPH_LEN(iphdr));
273
274 /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
275 if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) {
276 if (iphdr_hlen > p->len) {
277 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
278 ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
279 iphdr_hlen, p->len));
280 }
281 if (iphdr_len > p->tot_len) {
282 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
283 ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
284 iphdr_len, p->tot_len));
285 }
286 /* free (drop) packet pbufs */
287 pbuf_free(p);
288 IP_STATS_INC(ip.lenerr);
289 IP_STATS_INC(ip.drop);
290 snmp_inc_ipindiscards();
291 return ERR_OK;
292 }
293
294 /* verify checksum */
295 #if CHECKSUM_CHECK_IP
296 if (inet_chksum(iphdr, iphdr_hlen) != 0) {
297
298 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
299 ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
300 ip_debug_print(p);
301 pbuf_free(p);
302 IP_STATS_INC(ip.chkerr);
303 IP_STATS_INC(ip.drop);
304 snmp_inc_ipinhdrerrors();
305 return ERR_OK;
306 }
307 #endif
308
309 /* Trim pbuf. This should have been done at the netif layer,
310 * but we'll do it anyway just to be sure that its done. */
311 pbuf_realloc(p, iphdr_len);
312
313 /* copy IP addresses to aligned ip_addr_t */
314 ip_addr_copy(current_iphdr_dest, iphdr->dest);
315 ip_addr_copy(current_iphdr_src, iphdr->src);
316
317 /* match packet against an interface, i.e. is this packet for us? */
318 #if LWIP_IGMP
319 if (ip_addr_ismulticast(&current_iphdr_dest)) {
320 if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, &current_iphdr_dest))) {
321 netif = inp;
322 } else {
323 netif = NULL;
324 }
325 } else
326 #endif /* LWIP_IGMP */
327 {
328 /* start trying with inp. if that's not acceptable, start walking the
329 list of configured netifs.
330 'first' is used as a boolean to mark whether we started walking the list */
331 int first = 1;
332 netif = inp;
333 do {
334 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",
335 ip4_addr_get_u32(&iphdr->dest), ip4_addr_get_u32(&netif->ip_addr),
336 ip4_addr_get_u32(&iphdr->dest) & ip4_addr_get_u32(&netif->netmask),
337 ip4_addr_get_u32(&netif->ip_addr) & ip4_addr_get_u32(&netif->netmask),
338 ip4_addr_get_u32(&iphdr->dest) & ~ip4_addr_get_u32(&netif->netmask)));
339
340 /* interface is up and configured? */
341 if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr)))) {
342 /* unicast to this interface address? */
343 if (ip_addr_cmp(&current_iphdr_dest, &(netif->ip_addr)) ||
344 /* or broadcast on this interface network address? */
345 ip_addr_isbroadcast(&current_iphdr_dest, netif)) {
346 LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",
347 netif->name[0], netif->name[1]));
348 /* break out of for loop */
349 break;
350 }
351 #if LWIP_AUTOIP
352 /* connections to link-local addresses must persist after changing
353 the netif's address (RFC3927 ch. 1.9) */
354 if ((netif->autoip != NULL) &&
355 ip_addr_cmp(&current_iphdr_dest, &(netif->autoip->llipaddr))) {
356 LWIP_DEBUGF(IP_DEBUG, ("ip_input: LLA packet accepted on interface %c%c\n",
357 netif->name[0], netif->name[1]));
358 /* break out of for loop */
359 break;
360 }
361 #endif /* LWIP_AUTOIP */
362 }
363 if (first) {
364 first = 0;
365 netif = netif_list;
366 } else {
367 netif = netif->next;
368 }
369 if (netif == inp) {
370 netif = netif->next;
371 }
372 } while(netif != NULL);
373 }
374
375 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
376 /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
377 * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
378 * According to RFC 1542 section 3.1.1, referred by RFC 2131).
379 *
380 * If you want to accept private broadcast communication while a netif is down,
381 * define LWIP_IP_ACCEPT_UDP_PORT(dst_port), e.g.:
382 *
383 * #define LWIP_IP_ACCEPT_UDP_PORT(dst_port) ((dst_port) == PP_NTOHS(12345))
384 */
385 if (netif == NULL) {
386 /* remote port is DHCP server? */
387 if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
388 struct udp_hdr *udphdr = (struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen);
389 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",
390 ntohs(udphdr->dest)));
391 if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {
392 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: DHCP packet accepted.\n"));
393 netif = inp;
394 check_ip_src = 0;
395 }
396 }
397 }
398 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
399
400 /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
401 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
402 /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
403 if (check_ip_src && !ip_addr_isany(&current_iphdr_src))
404 #endif /* IP_ACCEPT_LINK_LAYER_ADDRESSING */
405 { if ((ip_addr_isbroadcast(&current_iphdr_src, inp)) ||
406 (ip_addr_ismulticast(&current_iphdr_src))) {
407 /* packet source is not valid */
408 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip_input: packet source is not valid.\n"));
409 /* free (drop) packet pbufs */
410 pbuf_free(p);
411 IP_STATS_INC(ip.drop);
412 snmp_inc_ipinaddrerrors();
413 snmp_inc_ipindiscards();
414 return ERR_OK;
415 }
416 }
417
418 /* packet not for us? */
419 if (netif == NULL) {
420 /* packet not for us, route or discard */
421 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
422 #if IP_FORWARD
423 /* non-broadcast packet? */
424 if (!ip_addr_isbroadcast(&current_iphdr_dest, inp)) {
425 /* try to forward IP packet on (other) interfaces */
426 ip_forward(p, iphdr, inp);
427 } else
428 #endif /* IP_FORWARD */
429 {
430 snmp_inc_ipinaddrerrors();
431 snmp_inc_ipindiscards();
432 }
433 pbuf_free(p);
434 return ERR_OK;
435 }
436 /* packet consists of multiple fragments? */
437 if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
438 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
439 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 ip_reass()\n",
440 ntohs(IPH_ID(iphdr)), p->tot_len, ntohs(IPH_LEN(iphdr)), !!(IPH_OFFSET(iphdr) & PP_HTONS(IP_MF)), (ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)*8));
441 /* reassemble the packet*/
442 p = ip_reass(p);
443 /* packet not fully reassembled yet? */
444 if (p == NULL) {
445 return ERR_OK;
446 }
447 iphdr = (struct ip_hdr *)p->payload;
448 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
449 pbuf_free(p);
450 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
451 ntohs(IPH_OFFSET(iphdr))));
452 IP_STATS_INC(ip.opterr);
453 IP_STATS_INC(ip.drop);
454 /* unsupported protocol feature */
455 snmp_inc_ipinunknownprotos();
456 return ERR_OK;
457 #endif /* IP_REASSEMBLY */
458 }
459
460 #if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
461
462 #if LWIP_IGMP
463 /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
464 if((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
465 #else
466 if (iphdr_hlen > IP_HLEN) {
467 #endif /* LWIP_IGMP */
468 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
469 pbuf_free(p);
470 IP_STATS_INC(ip.opterr);
471 IP_STATS_INC(ip.drop);
472 /* unsupported protocol feature */
473 snmp_inc_ipinunknownprotos();
474 return ERR_OK;
475 }
476 #endif /* IP_OPTIONS_ALLOWED == 0 */
477
478 /* send to upper layers */
479 LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n"));
480 ip_debug_print(p);
481 LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
482
483 current_netif = inp;
484 current_header = iphdr;
485
486 #if LWIP_RAW
487 /* raw input did not eat the packet? */
488 if (raw_input(p, inp) == 0)
489 #endif /* LWIP_RAW */
490 {
491
492 switch (IPH_PROTO(iphdr)) {
493 #if LWIP_UDP
494 case IP_PROTO_UDP:
495 #if LWIP_UDPLITE
496 case IP_PROTO_UDPLITE:
497 #endif /* LWIP_UDPLITE */
498 snmp_inc_ipindelivers();
499 udp_input(p, inp);
500 break;
501 #endif /* LWIP_UDP */
502 #if LWIP_TCP
503 case IP_PROTO_TCP:
504 snmp_inc_ipindelivers();
505 tcp_input(p, inp);
506 break;
507 #endif /* LWIP_TCP */
508 #if LWIP_ICMP
509 case IP_PROTO_ICMP:
510 snmp_inc_ipindelivers();
511 icmp_input(p, inp);
512 break;
513 #endif /* LWIP_ICMP */
514 #if LWIP_IGMP
515 case IP_PROTO_IGMP:
516 igmp_input(p, inp, &current_iphdr_dest);
517 break;
518 #endif /* LWIP_IGMP */
519 default:
520 #if LWIP_ICMP
521 /* send ICMP destination protocol unreachable unless is was a broadcast */
522 if (!ip_addr_isbroadcast(&current_iphdr_dest, inp) &&
523 !ip_addr_ismulticast(&current_iphdr_dest)) {
524 p->payload = iphdr;
525 icmp_dest_unreach(p, ICMP_DUR_PROTO);
526 }
527 #endif /* LWIP_ICMP */
528 pbuf_free(p);
529
530 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr)));
531
532 IP_STATS_INC(ip.proterr);
533 IP_STATS_INC(ip.drop);
534 snmp_inc_ipinunknownprotos();
535 }
536 }
537
538 current_netif = NULL;
539 current_header = NULL;
540 ip_addr_set_any(&current_iphdr_src);
541 ip_addr_set_any(&current_iphdr_dest);
542
543 return ERR_OK;
544 }
545
546 /**
547 * Sends an IP packet on a network interface. This function constructs
548 * the IP header and calculates the IP header checksum. If the source
549 * IP address is NULL, the IP address of the outgoing network
550 * interface is filled in as source address.
551 * If the destination IP address is IP_HDRINCL, p is assumed to already
552 * include an IP header and p->payload points to it instead of the data.
553 *
554 * @param p the packet to send (p->payload points to the data, e.g. next
555 protocol header; if dest == IP_HDRINCL, p already includes an IP
556 header and p->payload points to that IP header)
557 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
558 * IP address of the netif used to send is used as source address)
559 * @param dest the destination IP address to send the packet to
560 * @param ttl the TTL value to be set in the IP header
561 * @param tos the TOS value to be set in the IP header
562 * @param proto the PROTOCOL to be set in the IP header
563 * @param netif the netif on which to send this packet
564 * @return ERR_OK if the packet was sent OK
565 * ERR_BUF if p doesn't have enough space for IP/LINK headers
566 * returns errors returned by netif->output
567 *
568 * @note ip_id: RFC791 "some host may be able to simply use
569 * unique identifiers independent of destination"
570 */
571 err_t
572 ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
573 u8_t ttl, u8_t tos,
574 u8_t proto, struct netif *netif)
575 {
576 #if IP_OPTIONS_SEND
577 return ip_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
578 }
579
580 /**
581 * Same as ip_output_if() but with the possibility to include IP options:
582 *
583 * @ param ip_options pointer to the IP options, copied into the IP header
584 * @ param optlen length of ip_options
585 */
586 err_t ip_output_if_opt(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
587 u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
588 u16_t optlen)
589 {
590 #endif /* IP_OPTIONS_SEND */
591 struct ip_hdr *iphdr;
592 ip_addr_t dest_addr;
593 #if CHECKSUM_GEN_IP_INLINE
594 u32_t chk_sum = 0;
595 #endif /* CHECKSUM_GEN_IP_INLINE */
596
597 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
598 gets altered as the packet is passed down the stack */
599 LWIP_ASSERT("p->ref == 1", p->ref == 1);
600
601 snmp_inc_ipoutrequests();
602
603 /* Should the IP header be generated or is it already included in p? */
604 if (dest != IP_HDRINCL) {
605 u16_t ip_hlen = IP_HLEN;
606 #if IP_OPTIONS_SEND
607 u16_t optlen_aligned = 0;
608 if (optlen != 0) {
609 #if CHECKSUM_GEN_IP_INLINE
610 int i;
611 #endif /* CHECKSUM_GEN_IP_INLINE */
612 /* round up to a multiple of 4 */
613 optlen_aligned = ((optlen + 3) & ~3);
614 ip_hlen += optlen_aligned;
615 /* First write in the IP options */
616 if (pbuf_header(p, optlen_aligned)) {
617 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output_if_opt: not enough room for IP options in pbuf\n"));
618 IP_STATS_INC(ip.err);
619 snmp_inc_ipoutdiscards();
620 return ERR_BUF;
621 }
622 MEMCPY(p->payload, ip_options, optlen);
623 if (optlen < optlen_aligned) {
624 /* zero the remaining bytes */
625 memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen);
626 }
627 #if CHECKSUM_GEN_IP_INLINE
628 for (i = 0; i < optlen_aligned/2; i++) {
629 chk_sum += ((u16_t*)p->payload)[i];
630 }
631 #endif /* CHECKSUM_GEN_IP_INLINE */
632 }
633 #endif /* IP_OPTIONS_SEND */
634 /* generate IP header */
635 if (pbuf_header(p, IP_HLEN)) {
636 LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output: not enough room for IP header in pbuf\n"));
637
638 IP_STATS_INC(ip.err);
639 snmp_inc_ipoutdiscards();
640 return ERR_BUF;
641 }
642
643 iphdr = (struct ip_hdr *)p->payload;
644 LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
645 (p->len >= sizeof(struct ip_hdr)));
646
647 IPH_TTL_SET(iphdr, ttl);
648 IPH_PROTO_SET(iphdr, proto);
649 #if CHECKSUM_GEN_IP_INLINE
650 chk_sum += LWIP_MAKE_U16(proto, ttl);
651 #endif /* CHECKSUM_GEN_IP_INLINE */
652
653 /* dest cannot be NULL here */
654 ip_addr_copy(iphdr->dest, *dest);
655 #if CHECKSUM_GEN_IP_INLINE
656 chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
657 chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
658 #endif /* CHECKSUM_GEN_IP_INLINE */
659
660 IPH_VHLTOS_SET(iphdr, 4, ip_hlen / 4, tos);
661 #if CHECKSUM_GEN_IP_INLINE
662 chk_sum += iphdr->_v_hl_tos;
663 #endif /* CHECKSUM_GEN_IP_INLINE */
664 IPH_LEN_SET(iphdr, htons(p->tot_len));
665 #if CHECKSUM_GEN_IP_INLINE
666 chk_sum += iphdr->_len;
667 #endif /* CHECKSUM_GEN_IP_INLINE */
668 IPH_OFFSET_SET(iphdr, 0);
669 IPH_ID_SET(iphdr, htons(ip_id));
670 #if CHECKSUM_GEN_IP_INLINE
671 chk_sum += iphdr->_id;
672 #endif /* CHECKSUM_GEN_IP_INLINE */
673 ++ip_id;
674
675 if (ip_addr_isany(src)) {
676 ip_addr_copy(iphdr->src, netif->ip_addr);
677 } else {
678 /* src cannot be NULL here */
679 ip_addr_copy(iphdr->src, *src);
680 }
681
682 #if CHECKSUM_GEN_IP_INLINE
683 chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
684 chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
685 chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
686 chk_sum = (chk_sum >> 16) + chk_sum;
687 chk_sum = ~chk_sum;
688 iphdr->_chksum = chk_sum; /* network order */
689 #else /* CHECKSUM_GEN_IP_INLINE */
690 IPH_CHKSUM_SET(iphdr, 0);
691 #if CHECKSUM_GEN_IP
692 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
693 #endif
694 #endif /* CHECKSUM_GEN_IP_INLINE */
695 } else {
696 /* IP header already included in p */
697 iphdr = (struct ip_hdr *)p->payload;
698 ip_addr_copy(dest_addr, iphdr->dest);
699 dest = &dest_addr;
700 }
701
702 IP_STATS_INC(ip.xmit);
703
704 LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
705 ip_debug_print(p);
706
707 #if ENABLE_LOOPBACK
708 if (ip_addr_cmp(dest, &netif->ip_addr)) {
709 /* Packet to self, enqueue it for loopback */
710 LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
711 return netif_loop_output(netif, p, dest);
712 }
713 #if LWIP_IGMP
714 if ((p->flags & PBUF_FLAG_MCASTLOOP) != 0) {
715 netif_loop_output(netif, p, dest);
716 }
717 #endif /* LWIP_IGMP */
718 #endif /* ENABLE_LOOPBACK */
719 #if IP_FRAG
720 /* don't fragment if interface has mtu set to 0 [loopif] */
721 if (netif->mtu && (p->tot_len > netif->mtu)) {
722 return ip_frag(p, netif, dest);
723 }
724 #endif /* IP_FRAG */
725
726 LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
727 return netif->output(netif, p, dest);
728 }
729
730 /**
731 * Simple interface to ip_output_if. It finds the outgoing network
732 * interface and calls upon ip_output_if to do the actual work.
733 *
734 * @param p the packet to send (p->payload points to the data, e.g. next
735 protocol header; if dest == IP_HDRINCL, p already includes an IP
736 header and p->payload points to that IP header)
737 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
738 * IP address of the netif used to send is used as source address)
739 * @param dest the destination IP address to send the packet to
740 * @param ttl the TTL value to be set in the IP header
741 * @param tos the TOS value to be set in the IP header
742 * @param proto the PROTOCOL to be set in the IP header
743 *
744 * @return ERR_RTE if no route is found
745 * see ip_output_if() for more return values
746 */
747 err_t
748 ip_output(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
749 u8_t ttl, u8_t tos, u8_t proto)
750 {
751 struct netif *netif;
752
753 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
754 gets altered as the packet is passed down the stack */
755 LWIP_ASSERT("p->ref == 1", p->ref == 1);
756
757 if ((netif = ip_route(dest)) == NULL) {
758 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
759 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
760 IP_STATS_INC(ip.rterr);
761 return ERR_RTE;
762 }
763
764 return ip_output_if(p, src, dest, ttl, tos, proto, netif);
765 }
766
767 #if LWIP_NETIF_HWADDRHINT
768 /** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
769 * before calling ip_output_if.
770 *
771 * @param p the packet to send (p->payload points to the data, e.g. next
772 protocol header; if dest == IP_HDRINCL, p already includes an IP
773 header and p->payload points to that IP header)
774 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
775 * IP address of the netif used to send is used as source address)
776 * @param dest the destination IP address to send the packet to
777 * @param ttl the TTL value to be set in the IP header
778 * @param tos the TOS value to be set in the IP header
779 * @param proto the PROTOCOL to be set in the IP header
780 * @param addr_hint address hint pointer set to netif->addr_hint before
781 * calling ip_output_if()
782 *
783 * @return ERR_RTE if no route is found
784 * see ip_output_if() for more return values
785 */
786 err_t
787 ip_output_hinted(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
788 u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
789 {
790 struct netif *netif;
791 err_t err;
792
793 /* pbufs passed to IP must have a ref-count of 1 as their payload pointer
794 gets altered as the packet is passed down the stack */
795 LWIP_ASSERT("p->ref == 1", p->ref == 1);
796
797 if ((netif = ip_route(dest)) == NULL) {
798 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
799 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
800 IP_STATS_INC(ip.rterr);
801 return ERR_RTE;
802 }
803
804 netif->addr_hint = addr_hint;
805 err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
806 netif->addr_hint = NULL;
807
808 return err;
809 }
810 #endif /* LWIP_NETIF_HWADDRHINT*/
811
812 #if IP_DEBUG
813 /* Print an IP header by using LWIP_DEBUGF
814 * @param p an IP packet, p->payload pointing to the IP header
815 */
816 void
817 ip_debug_print(struct pbuf *p)
818 {
819 struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
820 u8_t *payload;
821
822 payload = (u8_t *)iphdr + IP_HLEN;
823
824 LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
825 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
826 LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n",
827 IPH_V(iphdr),
828 IPH_HL(iphdr),
829 IPH_TOS(iphdr),
830 ntohs(IPH_LEN(iphdr))));
831 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
832 LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n",
833 ntohs(IPH_ID(iphdr)),
834 ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
835 ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
836 ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
837 ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
838 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
839 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n",
840 IPH_TTL(iphdr),
841 IPH_PROTO(iphdr),
842 ntohs(IPH_CHKSUM(iphdr))));
843 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
844 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
845 ip4_addr1_16(&iphdr->src),
846 ip4_addr2_16(&iphdr->src),
847 ip4_addr3_16(&iphdr->src),
848 ip4_addr4_16(&iphdr->src)));
849 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
850 LWIP_DEBUGF(IP_DEBUG, ("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
851 ip4_addr1_16(&iphdr->dest),
852 ip4_addr2_16(&iphdr->dest),
853 ip4_addr3_16(&iphdr->dest),
854 ip4_addr4_16(&iphdr->dest)));
855 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
856 }
857 #endif /* IP_DEBUG */