source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/hardware/arduino/avr/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/SERVICES/LWIP/lwip-1.3.2/src/core/ipv4/ip.c @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 24.5 KB
Line 
1/* This source file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */
2
3/**
4 * @file
5 * This is the IPv4 layer implementation for incoming and outgoing IP traffic.
6 *
7 * @see ip_frag.c
8 *
9 */
10
11/*
12 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
13 * All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without modification,
16 * are permitted provided that the following conditions are met:
17 *
18 * 1. Redistributions of source code must retain the above copyright notice,
19 *    this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright notice,
21 *    this list of conditions and the following disclaimer in the documentation
22 *    and/or other materials provided with the distribution.
23 * 3. The name of the author may not be used to endorse or promote products
24 *    derived from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
29 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
31 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 * OF SUCH DAMAGE.
36 *
37 * This file is part of the lwIP TCP/IP stack.
38 *
39 * Author: Adam Dunkels <adam@sics.se>
40 *
41 */
42
43#include "lwip/opt.h"
44#include "lwip/ip.h"
45#include "lwip/def.h"
46#include "lwip/mem.h"
47#include "lwip/ip_frag.h"
48#include "lwip/inet.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/tcp.h"
56#include "lwip/snmp.h"
57#include "lwip/dhcp.h"
58#include "lwip/stats.h"
59#include "arch/perf.h"
60
61#include <string.h>
62
63/**
64 * The interface that provided the packet for the current callback
65 * invocation.
66 */
67struct netif *current_netif;
68
69/**
70 * Header of the input packet currently being processed.
71 */
72const struct ip_hdr *current_header;
73
74/**
75 * Finds the appropriate network interface for a given IP address. It
76 * searches the list of network interfaces linearly. A match is found
77 * if the masked IP address of the network interface equals the masked
78 * IP address given to the function.
79 *
80 * @param dest the destination IP address for which to find the route
81 * @return the netif on which to send to reach dest
82 */
83struct netif *
84ip_route(struct ip_addr *dest)
85{
86  struct netif *netif;
87
88  /* iterate through netifs */
89  for(netif = netif_list; netif != NULL; netif = netif->next) {
90    /* network mask matches? */
91    if (netif_is_up(netif)) {
92      if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
93        /* return netif on which to forward IP packet */
94        return netif;
95      }
96    }
97  }
98  if ((netif_default == NULL) || (!netif_is_up(netif_default))) {
99    LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to 0x%"X32_F"\n", dest->addr));
100    IP_STATS_INC(ip.rterr);
101    snmp_inc_ipoutnoroutes();
102    return NULL;
103  }
104  /* no matching netif found, use default netif */
105  return netif_default;
106}
107
108#if IP_FORWARD
109/**
110 * Forwards an IP packet. It finds an appropriate route for the
111 * packet, decrements the TTL value of the packet, adjusts the
112 * checksum and outputs the packet on the appropriate interface.
113 *
114 * @param p the packet to forward (p->payload points to IP header)
115 * @param iphdr the IP header of the input packet
116 * @param inp the netif on which this packet was received
117 * @return the netif on which the packet was sent (NULL if it wasn't sent)
118 */
119static struct netif *
120ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
121{
122  struct netif *netif;
123
124  PERF_START;
125  /* Find network interface where to forward this IP packet to. */
126  netif = ip_route((struct ip_addr *)&(iphdr->dest));
127  if (netif == NULL) {
128    LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for 0x%"X32_F" found\n",
129                      iphdr->dest.addr));
130    snmp_inc_ipoutnoroutes();
131    return (struct netif *)NULL;
132  }
133  /* Do not forward packets onto the same network interface on which
134   * they arrived. */
135  if (netif == inp) {
136    LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
137    snmp_inc_ipoutnoroutes();
138    return (struct netif *)NULL;
139  }
140
141  /* decrement TTL */
142  IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
143  /* send ICMP if TTL == 0 */
144  if (IPH_TTL(iphdr) == 0) {
145    snmp_inc_ipinhdrerrors();
146#if LWIP_ICMP
147    /* Don't send ICMP messages in response to ICMP messages */
148    if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
149      icmp_time_exceeded(p, ICMP_TE_TTL);
150    }
151#endif /* LWIP_ICMP */
152    return (struct netif *)NULL;
153  }
154
155  /* Incrementally update the IP checksum. */
156  if (IPH_CHKSUM(iphdr) >= htons(0xffff - 0x100)) {
157    IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100) + 1);
158  } else {
159    IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + htons(0x100));
160  }
161
162  LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to 0x%"X32_F"\n",
163                    iphdr->dest.addr));
164
165  IP_STATS_INC(ip.fw);
166  IP_STATS_INC(ip.xmit);
167  snmp_inc_ipforwdatagrams();
168
169  PERF_STOP("ip_forward");
170  /* transmit pbuf on chosen interface */
171  netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));
172  return netif;
173}
174#endif /* IP_FORWARD */
175
176/**
177 * This function is called by the network interface device driver when
178 * an IP packet is received. The function does the basic checks of the
179 * IP header such as packet size being at least larger than the header
180 * size etc. If the packet was not destined for us, the packet is
181 * forwarded (using ip_forward). The IP checksum is always checked.
182 *
183 * Finally, the packet is sent to the upper layer protocol input function.
184 *
185 * @param p the received IP packet (p->payload points to IP header)
186 * @param inp the netif on which this packet was received
187 * @return ERR_OK if the packet was processed (could return ERR_* if it wasn't
188 *         processed, but currently always returns ERR_OK)
189 */
190err_t
191ip_input(struct pbuf *p, struct netif *inp)
192{
193  struct ip_hdr *iphdr;
194  struct netif *netif;
195  u16_t iphdr_hlen;
196  u16_t iphdr_len;
197#if LWIP_DHCP
198  int check_ip_src=1;
199#endif /* LWIP_DHCP */
200
201  IP_STATS_INC(ip.recv);
202  snmp_inc_ipinreceives();
203
204  /* identify the IP header */
205  iphdr = p->payload;
206  if (IPH_V(iphdr) != 4) {
207    LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_WARNING, ("IP packet dropped due to bad version number %"U16_F"\n", IPH_V(iphdr)));
208    ip_debug_print(p);
209    pbuf_free(p);
210    IP_STATS_INC(ip.err);
211    IP_STATS_INC(ip.drop);
212    snmp_inc_ipinhdrerrors();
213    return ERR_OK;
214  }
215
216  /* obtain IP header length in number of 32-bit words */
217  iphdr_hlen = IPH_HL(iphdr);
218  /* calculate IP header length in bytes */
219  iphdr_hlen *= 4;
220  /* obtain ip length in bytes */
221  iphdr_len = ntohs(IPH_LEN(iphdr));
222
223  /* header length exceeds first pbuf length, or ip length exceeds total pbuf length? */
224  if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) {
225    if (iphdr_hlen > p->len) {
226      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
227        ("IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
228        iphdr_hlen, p->len));
229    }
230    if (iphdr_len > p->tot_len) {
231      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
232        ("IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
233        iphdr_len, p->tot_len));
234    }
235    /* free (drop) packet pbufs */
236    pbuf_free(p);
237    IP_STATS_INC(ip.lenerr);
238    IP_STATS_INC(ip.drop);
239    snmp_inc_ipindiscards();
240    return ERR_OK;
241  }
242
243  /* verify checksum */
244#if CHECKSUM_CHECK_IP
245  if (inet_chksum(iphdr, iphdr_hlen) != 0) {
246
247    LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
248      ("Checksum (0x%"X16_F") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
249    ip_debug_print(p);
250    pbuf_free(p);
251    IP_STATS_INC(ip.chkerr);
252    IP_STATS_INC(ip.drop);
253    snmp_inc_ipinhdrerrors();
254    return ERR_OK;
255  }
256#endif
257
258  /* Trim pbuf. This should have been done at the netif layer,
259   * but we'll do it anyway just to be sure that its done. */
260  pbuf_realloc(p, iphdr_len);
261
262  /* match packet against an interface, i.e. is this packet for us? */
263#if LWIP_IGMP
264  if (ip_addr_ismulticast(&(iphdr->dest))) {
265    if ((inp->flags & NETIF_FLAG_IGMP) && (igmp_lookfor_group(inp, &(iphdr->dest)))) {
266      netif = inp;
267    } else {
268      netif = NULL;
269    }
270  } else
271#endif /* LWIP_IGMP */
272  {
273    /* start trying with inp. if that's not acceptable, start walking the
274       list of configured netifs.
275       'first' is used as a boolean to mark whether we started walking the list */
276    int first = 1;
277    netif = inp;
278    do {
279      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",
280          iphdr->dest.addr, netif->ip_addr.addr,
281          iphdr->dest.addr & netif->netmask.addr,
282          netif->ip_addr.addr & netif->netmask.addr,
283          iphdr->dest.addr & ~(netif->netmask.addr)));
284
285      /* interface is up and configured? */
286      if ((netif_is_up(netif)) && (!ip_addr_isany(&(netif->ip_addr)))) {
287        /* unicast to this interface address? */
288        if (ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr)) ||
289            /* or broadcast on this interface network address? */
290            ip_addr_isbroadcast(&(iphdr->dest), netif)) {
291          LWIP_DEBUGF(IP_DEBUG, ("ip_input: packet accepted on interface %c%c\n",
292              netif->name[0], netif->name[1]));
293          /* break out of for loop */
294          break;
295        }
296      }
297      if (first) {
298        first = 0;
299        netif = netif_list;
300      } else {
301        netif = netif->next;
302      }
303      if (netif == inp) {
304        netif = netif->next;
305      }
306    } while(netif != NULL);
307  }
308
309#if LWIP_DHCP
310  /* Pass DHCP messages regardless of destination address. DHCP traffic is addressed
311   * using link layer addressing (such as Ethernet MAC) so we must not filter on IP.
312   * According to RFC 1542 section 3.1.1, referred by RFC 2131).
313   */
314  if (netif == NULL) {
315    /* remote port is DHCP server? */
316    if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
317      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: UDP packet to DHCP client port %"U16_F"\n",
318        ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest)));
319      if (ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest) == DHCP_CLIENT_PORT) {
320        LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: DHCP packet accepted.\n"));
321        netif = inp;
322        check_ip_src = 0;
323      }
324    }
325  }
326#endif /* LWIP_DHCP */
327
328  /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
329#if LWIP_DHCP
330  /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
331  if (check_ip_src && (iphdr->src.addr != 0))
332#endif /* LWIP_DHCP */
333  {  if ((ip_addr_isbroadcast(&(iphdr->src), inp)) ||
334         (ip_addr_ismulticast(&(iphdr->src)))) {
335      /* packet source is not valid */
336      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("ip_input: packet source is not valid.\n"));
337      /* free (drop) packet pbufs */
338      pbuf_free(p);
339      IP_STATS_INC(ip.drop);
340      snmp_inc_ipinaddrerrors();
341      snmp_inc_ipindiscards();
342      return ERR_OK;
343    }
344  }
345
346  /* packet not for us? */
347  if (netif == NULL) {
348    /* packet not for us, route or discard */
349    LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: packet not for us.\n"));
350#if IP_FORWARD
351    /* non-broadcast packet? */
352    if (!ip_addr_isbroadcast(&(iphdr->dest), inp)) {
353      /* try to forward IP packet on (other) interfaces */
354      ip_forward(p, iphdr, inp);
355    } else
356#endif /* IP_FORWARD */
357    {
358      snmp_inc_ipinaddrerrors();
359      snmp_inc_ipindiscards();
360    }
361    pbuf_free(p);
362    return ERR_OK;
363  }
364  /* packet consists of multiple fragments? */
365  if ((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {
366#if IP_REASSEMBLY /* packet fragment reassembly code present? */
367    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",
368      ntohs(IPH_ID(iphdr)), p->tot_len, ntohs(IPH_LEN(iphdr)), !!(IPH_OFFSET(iphdr) & htons(IP_MF)), (ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)*8));
369    /* reassemble the packet*/
370    p = ip_reass(p);
371    /* packet not fully reassembled yet? */
372    if (p == NULL) {
373      return ERR_OK;
374    }
375    iphdr = p->payload;
376#else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
377    pbuf_free(p);
378    LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
379      ntohs(IPH_OFFSET(iphdr))));
380    IP_STATS_INC(ip.opterr);
381    IP_STATS_INC(ip.drop);
382    /* unsupported protocol feature */
383    snmp_inc_ipinunknownprotos();
384    return ERR_OK;
385#endif /* IP_REASSEMBLY */
386  }
387
388#if IP_OPTIONS_ALLOWED == 0 /* no support for IP options in the IP header? */
389
390#if LWIP_IGMP
391  /* there is an extra "router alert" option in IGMP messages which we allow for but do not police */
392  if((iphdr_hlen > IP_HLEN &&  (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
393#else
394  if (iphdr_hlen > IP_HLEN) {
395#endif /* LWIP_IGMP */
396    LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since there were IP options (while IP_OPTIONS_ALLOWED == 0).\n"));
397    pbuf_free(p);
398    IP_STATS_INC(ip.opterr);
399    IP_STATS_INC(ip.drop);
400    /* unsupported protocol feature */
401    snmp_inc_ipinunknownprotos();
402    return ERR_OK;
403  }
404#endif /* IP_OPTIONS_ALLOWED == 0 */
405
406  /* send to upper layers */
407  LWIP_DEBUGF(IP_DEBUG, ("ip_input: \n"));
408  ip_debug_print(p);
409  LWIP_DEBUGF(IP_DEBUG, ("ip_input: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len));
410
411  current_netif = inp;
412  current_header = iphdr;
413
414#if LWIP_RAW
415  /* raw input did not eat the packet? */
416  if (raw_input(p, inp) == 0)
417#endif /* LWIP_RAW */
418  {
419
420    switch (IPH_PROTO(iphdr)) {
421#if LWIP_UDP
422    case IP_PROTO_UDP:
423#if LWIP_UDPLITE
424    case IP_PROTO_UDPLITE:
425#endif /* LWIP_UDPLITE */
426      snmp_inc_ipindelivers();
427      udp_input(p, inp);
428      break;
429#endif /* LWIP_UDP */
430#if LWIP_TCP
431    case IP_PROTO_TCP:
432      snmp_inc_ipindelivers();
433      tcp_input(p, inp);
434      break;
435#endif /* LWIP_TCP */
436#if LWIP_ICMP
437    case IP_PROTO_ICMP:
438      snmp_inc_ipindelivers();
439      icmp_input(p, inp);
440      break;
441#endif /* LWIP_ICMP */
442#if LWIP_IGMP
443    case IP_PROTO_IGMP:
444      igmp_input(p,inp,&(iphdr->dest));
445      break;
446#endif /* LWIP_IGMP */
447    default:
448#if LWIP_ICMP
449      /* send ICMP destination protocol unreachable unless is was a broadcast */
450      if (!ip_addr_isbroadcast(&(iphdr->dest), inp) &&
451          !ip_addr_ismulticast(&(iphdr->dest))) {
452        p->payload = iphdr;
453        icmp_dest_unreach(p, ICMP_DUR_PROTO);
454      }
455#endif /* LWIP_ICMP */
456      pbuf_free(p);
457
458      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("Unsupported transport protocol %"U16_F"\n", IPH_PROTO(iphdr)));
459
460      IP_STATS_INC(ip.proterr);
461      IP_STATS_INC(ip.drop);
462      snmp_inc_ipinunknownprotos();
463    }
464  }
465
466  current_netif = NULL;
467  current_header = NULL;
468
469  return ERR_OK;
470}
471
472/**
473 * Sends an IP packet on a network interface. This function constructs
474 * the IP header and calculates the IP header checksum. If the source
475 * IP address is NULL, the IP address of the outgoing network
476 * interface is filled in as source address.
477 * If the destination IP address is IP_HDRINCL, p is assumed to already
478 * include an IP header and p->payload points to it instead of the data.
479 *
480 * @param p the packet to send (p->payload points to the data, e.g. next
481            protocol header; if dest == IP_HDRINCL, p already includes an IP
482            header and p->payload points to that IP header)
483 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
484 *         IP  address of the netif used to send is used as source address)
485 * @param dest the destination IP address to send the packet to
486 * @param ttl the TTL value to be set in the IP header
487 * @param tos the TOS value to be set in the IP header
488 * @param proto the PROTOCOL to be set in the IP header
489 * @param netif the netif on which to send this packet
490 * @return ERR_OK if the packet was sent OK
491 *         ERR_BUF if p doesn't have enough space for IP/LINK headers
492 *         returns errors returned by netif->output
493 *
494 * @note ip_id: RFC791 "some host may be able to simply use
495 *  unique identifiers independent of destination"
496 */
497err_t
498ip_output_if(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
499             u8_t ttl, u8_t tos,
500             u8_t proto, struct netif *netif)
501{
502#if IP_OPTIONS_SEND
503  return ip_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
504}
505
506/**
507 * Same as ip_output_if() but with the possibility to include IP options:
508 *
509 * @ param ip_options pointer to the IP options, copied into the IP header
510 * @ param optlen length of ip_options
511 */
512err_t ip_output_if_opt(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
513       u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
514       u16_t optlen)
515{
516#endif /* IP_OPTIONS_SEND */
517  struct ip_hdr *iphdr;
518  static u16_t ip_id = 0;
519
520  snmp_inc_ipoutrequests();
521
522  /* Should the IP header be generated or is it already included in p? */
523  if (dest != IP_HDRINCL) {
524    u16_t ip_hlen = IP_HLEN;
525#if IP_OPTIONS_SEND
526    u16_t optlen_aligned = 0;
527    if (optlen != 0) {
528      /* round up to a multiple of 4 */
529      optlen_aligned = ((optlen + 3) & ~3);
530      ip_hlen += optlen_aligned;
531      /* First write in the IP options */
532      if (pbuf_header(p, optlen_aligned)) {
533        LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output_if_opt: not enough room for IP options in pbuf\n"));
534        IP_STATS_INC(ip.err);
535        snmp_inc_ipoutdiscards();
536        return ERR_BUF;
537      }
538      MEMCPY(p->payload, ip_options, optlen);
539      if (optlen < optlen_aligned) {
540        /* zero the remaining bytes */
541        memset(((char*)p->payload) + optlen, 0, optlen_aligned - optlen);
542      }
543    }
544#endif /* IP_OPTIONS_SEND */
545    /* generate IP header */
546    if (pbuf_header(p, IP_HLEN)) {
547      LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_output: not enough room for IP header in pbuf\n"));
548
549      IP_STATS_INC(ip.err);
550      snmp_inc_ipoutdiscards();
551      return ERR_BUF;
552    }
553
554    iphdr = p->payload;
555    LWIP_ASSERT("check that first pbuf can hold struct ip_hdr",
556               (p->len >= sizeof(struct ip_hdr)));
557
558    IPH_TTL_SET(iphdr, ttl);
559    IPH_PROTO_SET(iphdr, proto);
560
561    ip_addr_set(&(iphdr->dest), dest);
562
563    IPH_VHLTOS_SET(iphdr, 4, ip_hlen / 4, tos);
564    IPH_LEN_SET(iphdr, htons(p->tot_len));
565    IPH_OFFSET_SET(iphdr, 0);
566    IPH_ID_SET(iphdr, htons(ip_id));
567    ++ip_id;
568
569    if (ip_addr_isany(src)) {
570      ip_addr_set(&(iphdr->src), &(netif->ip_addr));
571    } else {
572      ip_addr_set(&(iphdr->src), src);
573    }
574
575    IPH_CHKSUM_SET(iphdr, 0);
576#if CHECKSUM_GEN_IP
577    IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
578#endif
579  } else {
580    /* IP header already included in p */
581    iphdr = p->payload;
582    dest = &(iphdr->dest);
583  }
584
585  IP_STATS_INC(ip.xmit);
586
587  LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c%"U16_F"\n", netif->name[0], netif->name[1], netif->num));
588  ip_debug_print(p);
589
590#if ENABLE_LOOPBACK
591  if (ip_addr_cmp(dest, &netif->ip_addr)) {
592    /* Packet to self, enqueue it for loopback */
593    LWIP_DEBUGF(IP_DEBUG, ("netif_loop_output()"));
594    return netif_loop_output(netif, p, dest);
595  }
596#endif /* ENABLE_LOOPBACK */
597#if IP_FRAG
598  /* don't fragment if interface has mtu set to 0 [loopif] */
599  if (netif->mtu && (p->tot_len > netif->mtu)) {
600    return ip_frag(p,netif,dest);
601  }
602#endif
603
604  LWIP_DEBUGF(IP_DEBUG, ("netif->output()"));
605  return netif->output(netif, p, dest);
606}
607
608/**
609 * Simple interface to ip_output_if. It finds the outgoing network
610 * interface and calls upon ip_output_if to do the actual work.
611 *
612 * @param p the packet to send (p->payload points to the data, e.g. next
613            protocol header; if dest == IP_HDRINCL, p already includes an IP
614            header and p->payload points to that IP header)
615 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
616 *         IP  address of the netif used to send is used as source address)
617 * @param dest the destination IP address to send the packet to
618 * @param ttl the TTL value to be set in the IP header
619 * @param tos the TOS value to be set in the IP header
620 * @param proto the PROTOCOL to be set in the IP header
621 *
622 * @return ERR_RTE if no route is found
623 *         see ip_output_if() for more return values
624 */
625err_t
626ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
627          u8_t ttl, u8_t tos, u8_t proto)
628{
629  struct netif *netif;
630
631  if ((netif = ip_route(dest)) == NULL) {
632    LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
633    IP_STATS_INC(ip.rterr);
634    return ERR_RTE;
635  }
636
637  return ip_output_if(p, src, dest, ttl, tos, proto, netif);
638}
639
640#if LWIP_NETIF_HWADDRHINT
641/** Like ip_output, but takes and addr_hint pointer that is passed on to netif->addr_hint
642 *  before calling ip_output_if.
643 *
644 * @param p the packet to send (p->payload points to the data, e.g. next
645            protocol header; if dest == IP_HDRINCL, p already includes an IP
646            header and p->payload points to that IP header)
647 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
648 *         IP  address of the netif used to send is used as source address)
649 * @param dest the destination IP address to send the packet to
650 * @param ttl the TTL value to be set in the IP header
651 * @param tos the TOS value to be set in the IP header
652 * @param proto the PROTOCOL to be set in the IP header
653 * @param addr_hint address hint pointer set to netif->addr_hint before
654 *        calling ip_output_if()
655 *
656 * @return ERR_RTE if no route is found
657 *         see ip_output_if() for more return values
658 */
659err_t
660ip_output_hinted(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
661          u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
662{
663  struct netif *netif;
664  err_t err;
665
666  if ((netif = ip_route(dest)) == NULL) {
667    LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
668    IP_STATS_INC(ip.rterr);
669    return ERR_RTE;
670  }
671
672  netif->addr_hint = addr_hint;
673  err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
674  netif->addr_hint = NULL;
675
676  return err;
677}
678#endif /* LWIP_NETIF_HWADDRHINT*/
679
680#if IP_DEBUG
681/* Print an IP header by using LWIP_DEBUGF
682 * @param p an IP packet, p->payload pointing to the IP header
683 */
684void
685ip_debug_print(struct pbuf *p)
686{
687  struct ip_hdr *iphdr = p->payload;
688  u8_t *payload;
689
690  payload = (u8_t *)iphdr + IP_HLEN;
691
692  LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
693  LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
694  LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" |%2"S16_F" |  0x%02"X16_F" |     %5"U16_F"     | (v, hl, tos, len)\n",
695                    IPH_V(iphdr),
696                    IPH_HL(iphdr),
697                    IPH_TOS(iphdr),
698                    ntohs(IPH_LEN(iphdr))));
699  LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
700  LWIP_DEBUGF(IP_DEBUG, ("|    %5"U16_F"      |%"U16_F"%"U16_F"%"U16_F"|    %4"U16_F"   | (id, flags, offset)\n",
701                    ntohs(IPH_ID(iphdr)),
702                    ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
703                    ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
704                    ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
705                    ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
706  LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
707  LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |    0x%04"X16_F"     | (ttl, proto, chksum)\n",
708                    IPH_TTL(iphdr),
709                    IPH_PROTO(iphdr),
710                    ntohs(IPH_CHKSUM(iphdr))));
711  LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
712  LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (src)\n",
713                    ip4_addr1(&iphdr->src),
714                    ip4_addr2(&iphdr->src),
715                    ip4_addr3(&iphdr->src),
716                    ip4_addr4(&iphdr->src)));
717  LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
718  LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  | (dest)\n",
719                    ip4_addr1(&iphdr->dest),
720                    ip4_addr2(&iphdr->dest),
721                    ip4_addr3(&iphdr->dest),
722                    ip4_addr4(&iphdr->dest)));
723  LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
724}
725#endif /* IP_DEBUG */
Note: See TracBrowser for help on using the repository browser.