Official mbed lwIP library (version 1.4.0)

Dependents:   LwIPNetworking NetServicesMin EthernetInterface EthernetInterface_RSF ... more

Legacy Networking Libraries

This is an mbed 2 networking library. For mbed OS 5, lwip has been integrated with built-in networking interfaces. The networking libraries have been revised to better support additional network stacks and thread safety here.

This library is based on the code of lwIP v1.4.0

Copyright (c) 2001, 2002 Swedish Institute of Computer Science.
All rights reserved. 

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
Committer:
mbed_official
Date:
Fri Jun 22 09:25:39 2012 +0000
Revision:
0:51ac1d130fd4
Initial import from lwip-1.4.0: http://download.savannah.gnu.org/releases/lwip/lwip-1.4.0.zip

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:51ac1d130fd4 1 /*
mbed_official 0:51ac1d130fd4 2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
mbed_official 0:51ac1d130fd4 3 * All rights reserved.
mbed_official 0:51ac1d130fd4 4 *
mbed_official 0:51ac1d130fd4 5 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 0:51ac1d130fd4 6 * are permitted provided that the following conditions are met:
mbed_official 0:51ac1d130fd4 7 *
mbed_official 0:51ac1d130fd4 8 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 0:51ac1d130fd4 9 * this list of conditions and the following disclaimer.
mbed_official 0:51ac1d130fd4 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 0:51ac1d130fd4 11 * this list of conditions and the following disclaimer in the documentation
mbed_official 0:51ac1d130fd4 12 * and/or other materials provided with the distribution.
mbed_official 0:51ac1d130fd4 13 * 3. The name of the author may not be used to endorse or promote products
mbed_official 0:51ac1d130fd4 14 * derived from this software without specific prior written permission.
mbed_official 0:51ac1d130fd4 15 *
mbed_official 0:51ac1d130fd4 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
mbed_official 0:51ac1d130fd4 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
mbed_official 0:51ac1d130fd4 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
mbed_official 0:51ac1d130fd4 19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
mbed_official 0:51ac1d130fd4 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
mbed_official 0:51ac1d130fd4 21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
mbed_official 0:51ac1d130fd4 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
mbed_official 0:51ac1d130fd4 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
mbed_official 0:51ac1d130fd4 24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
mbed_official 0:51ac1d130fd4 25 * OF SUCH DAMAGE.
mbed_official 0:51ac1d130fd4 26 *
mbed_official 0:51ac1d130fd4 27 * This file is part of the lwIP TCP/IP stack.
mbed_official 0:51ac1d130fd4 28 *
mbed_official 0:51ac1d130fd4 29 * Author: Adam Dunkels <adam@sics.se>
mbed_official 0:51ac1d130fd4 30 *
mbed_official 0:51ac1d130fd4 31 */
mbed_official 0:51ac1d130fd4 32
mbed_official 0:51ac1d130fd4 33 /* Some ICMP messages should be passed to the transport protocols. This
mbed_official 0:51ac1d130fd4 34 is not implemented. */
mbed_official 0:51ac1d130fd4 35
mbed_official 0:51ac1d130fd4 36 #include "lwip/opt.h"
mbed_official 0:51ac1d130fd4 37
mbed_official 0:51ac1d130fd4 38 #if LWIP_ICMP /* don't build if not configured for use in lwipopts.h */
mbed_official 0:51ac1d130fd4 39
mbed_official 0:51ac1d130fd4 40 #include "lwip/icmp.h"
mbed_official 0:51ac1d130fd4 41 #include "lwip/inet.h"
mbed_official 0:51ac1d130fd4 42 #include "lwip/ip.h"
mbed_official 0:51ac1d130fd4 43 #include "lwip/def.h"
mbed_official 0:51ac1d130fd4 44 #include "lwip/stats.h"
mbed_official 0:51ac1d130fd4 45
mbed_official 0:51ac1d130fd4 46 void
mbed_official 0:51ac1d130fd4 47 icmp_input(struct pbuf *p, struct netif *inp)
mbed_official 0:51ac1d130fd4 48 {
mbed_official 0:51ac1d130fd4 49 u8_t type;
mbed_official 0:51ac1d130fd4 50 struct icmp_echo_hdr *iecho;
mbed_official 0:51ac1d130fd4 51 struct ip_hdr *iphdr;
mbed_official 0:51ac1d130fd4 52 struct ip_addr tmpaddr;
mbed_official 0:51ac1d130fd4 53
mbed_official 0:51ac1d130fd4 54 ICMP_STATS_INC(icmp.recv);
mbed_official 0:51ac1d130fd4 55
mbed_official 0:51ac1d130fd4 56 /* TODO: check length before accessing payload! */
mbed_official 0:51ac1d130fd4 57
mbed_official 0:51ac1d130fd4 58 type = ((u8_t *)p->payload)[0];
mbed_official 0:51ac1d130fd4 59
mbed_official 0:51ac1d130fd4 60 switch (type) {
mbed_official 0:51ac1d130fd4 61 case ICMP6_ECHO:
mbed_official 0:51ac1d130fd4 62 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
mbed_official 0:51ac1d130fd4 63
mbed_official 0:51ac1d130fd4 64 if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
mbed_official 0:51ac1d130fd4 65 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
mbed_official 0:51ac1d130fd4 66
mbed_official 0:51ac1d130fd4 67 pbuf_free(p);
mbed_official 0:51ac1d130fd4 68 ICMP_STATS_INC(icmp.lenerr);
mbed_official 0:51ac1d130fd4 69 return;
mbed_official 0:51ac1d130fd4 70 }
mbed_official 0:51ac1d130fd4 71 iecho = p->payload;
mbed_official 0:51ac1d130fd4 72 iphdr = (struct ip_hdr *)((u8_t *)p->payload - IP_HLEN);
mbed_official 0:51ac1d130fd4 73 if (inet_chksum_pbuf(p) != 0) {
mbed_official 0:51ac1d130fd4 74 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo (%"X16_F")\n", inet_chksum_pseudo(p, &(iphdr->src), &(iphdr->dest), IP_PROTO_ICMP, p->tot_len)));
mbed_official 0:51ac1d130fd4 75 ICMP_STATS_INC(icmp.chkerr);
mbed_official 0:51ac1d130fd4 76 /* return;*/
mbed_official 0:51ac1d130fd4 77 }
mbed_official 0:51ac1d130fd4 78 LWIP_DEBUGF(ICMP_DEBUG, ("icmp: p->len %"S16_F" p->tot_len %"S16_F"\n", p->len, p->tot_len));
mbed_official 0:51ac1d130fd4 79 ip_addr_set(&tmpaddr, &(iphdr->src));
mbed_official 0:51ac1d130fd4 80 ip_addr_set(&(iphdr->src), &(iphdr->dest));
mbed_official 0:51ac1d130fd4 81 ip_addr_set(&(iphdr->dest), &tmpaddr);
mbed_official 0:51ac1d130fd4 82 iecho->type = ICMP6_ER;
mbed_official 0:51ac1d130fd4 83 /* adjust the checksum */
mbed_official 0:51ac1d130fd4 84 if (iecho->chksum >= htons(0xffff - (ICMP6_ECHO << 8))) {
mbed_official 0:51ac1d130fd4 85 iecho->chksum += htons(ICMP6_ECHO << 8) + 1;
mbed_official 0:51ac1d130fd4 86 } else {
mbed_official 0:51ac1d130fd4 87 iecho->chksum += htons(ICMP6_ECHO << 8);
mbed_official 0:51ac1d130fd4 88 }
mbed_official 0:51ac1d130fd4 89 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo (%"X16_F")\n", inet_chksum_pseudo(p, &(iphdr->src), &(iphdr->dest), IP_PROTO_ICMP, p->tot_len)));
mbed_official 0:51ac1d130fd4 90 ICMP_STATS_INC(icmp.xmit);
mbed_official 0:51ac1d130fd4 91
mbed_official 0:51ac1d130fd4 92 /* LWIP_DEBUGF("icmp: p->len %"U16_F" p->tot_len %"U16_F"\n", p->len, p->tot_len);*/
mbed_official 0:51ac1d130fd4 93 ip_output_if (p, &(iphdr->src), IP_HDRINCL,
mbed_official 0:51ac1d130fd4 94 iphdr->hoplim, IP_PROTO_ICMP, inp);
mbed_official 0:51ac1d130fd4 95 break;
mbed_official 0:51ac1d130fd4 96 default:
mbed_official 0:51ac1d130fd4 97 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %"S16_F" not supported.\n", (s16_t)type));
mbed_official 0:51ac1d130fd4 98 ICMP_STATS_INC(icmp.proterr);
mbed_official 0:51ac1d130fd4 99 ICMP_STATS_INC(icmp.drop);
mbed_official 0:51ac1d130fd4 100 }
mbed_official 0:51ac1d130fd4 101
mbed_official 0:51ac1d130fd4 102 pbuf_free(p);
mbed_official 0:51ac1d130fd4 103 }
mbed_official 0:51ac1d130fd4 104
mbed_official 0:51ac1d130fd4 105 void
mbed_official 0:51ac1d130fd4 106 icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
mbed_official 0:51ac1d130fd4 107 {
mbed_official 0:51ac1d130fd4 108 struct pbuf *q;
mbed_official 0:51ac1d130fd4 109 struct ip_hdr *iphdr;
mbed_official 0:51ac1d130fd4 110 struct icmp_dur_hdr *idur;
mbed_official 0:51ac1d130fd4 111
mbed_official 0:51ac1d130fd4 112 /* @todo: can this be PBUF_LINK instead of PBUF_IP? */
mbed_official 0:51ac1d130fd4 113 q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM);
mbed_official 0:51ac1d130fd4 114 /* ICMP header + IP header + 8 bytes of data */
mbed_official 0:51ac1d130fd4 115 if (q == NULL) {
mbed_official 0:51ac1d130fd4 116 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_dest_unreach: failed to allocate pbuf for ICMP packet.\n"));
mbed_official 0:51ac1d130fd4 117 pbuf_free(p);
mbed_official 0:51ac1d130fd4 118 return;
mbed_official 0:51ac1d130fd4 119 }
mbed_official 0:51ac1d130fd4 120 LWIP_ASSERT("check that first pbuf can hold icmp message",
mbed_official 0:51ac1d130fd4 121 (q->len >= (8 + IP_HLEN + 8)));
mbed_official 0:51ac1d130fd4 122
mbed_official 0:51ac1d130fd4 123 iphdr = p->payload;
mbed_official 0:51ac1d130fd4 124
mbed_official 0:51ac1d130fd4 125 idur = q->payload;
mbed_official 0:51ac1d130fd4 126 idur->type = (u8_t)ICMP6_DUR;
mbed_official 0:51ac1d130fd4 127 idur->icode = (u8_t)t;
mbed_official 0:51ac1d130fd4 128
mbed_official 0:51ac1d130fd4 129 SMEMCPY((u8_t *)q->payload + 8, p->payload, IP_HLEN + 8);
mbed_official 0:51ac1d130fd4 130
mbed_official 0:51ac1d130fd4 131 /* calculate checksum */
mbed_official 0:51ac1d130fd4 132 idur->chksum = 0;
mbed_official 0:51ac1d130fd4 133 idur->chksum = inet_chksum(idur, q->len);
mbed_official 0:51ac1d130fd4 134 ICMP_STATS_INC(icmp.xmit);
mbed_official 0:51ac1d130fd4 135
mbed_official 0:51ac1d130fd4 136 ip_output(q, NULL,
mbed_official 0:51ac1d130fd4 137 (struct ip_addr *)&(iphdr->src), ICMP_TTL, IP_PROTO_ICMP);
mbed_official 0:51ac1d130fd4 138 pbuf_free(q);
mbed_official 0:51ac1d130fd4 139 }
mbed_official 0:51ac1d130fd4 140
mbed_official 0:51ac1d130fd4 141 void
mbed_official 0:51ac1d130fd4 142 icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
mbed_official 0:51ac1d130fd4 143 {
mbed_official 0:51ac1d130fd4 144 struct pbuf *q;
mbed_official 0:51ac1d130fd4 145 struct ip_hdr *iphdr;
mbed_official 0:51ac1d130fd4 146 struct icmp_te_hdr *tehdr;
mbed_official 0:51ac1d130fd4 147
mbed_official 0:51ac1d130fd4 148 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded\n"));
mbed_official 0:51ac1d130fd4 149
mbed_official 0:51ac1d130fd4 150 /* @todo: can this be PBUF_LINK instead of PBUF_IP? */
mbed_official 0:51ac1d130fd4 151 q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM);
mbed_official 0:51ac1d130fd4 152 /* ICMP header + IP header + 8 bytes of data */
mbed_official 0:51ac1d130fd4 153 if (q == NULL) {
mbed_official 0:51ac1d130fd4 154 LWIP_DEBUGF(ICMP_DEBUG, ("icmp_dest_unreach: failed to allocate pbuf for ICMP packet.\n"));
mbed_official 0:51ac1d130fd4 155 pbuf_free(p);
mbed_official 0:51ac1d130fd4 156 return;
mbed_official 0:51ac1d130fd4 157 }
mbed_official 0:51ac1d130fd4 158 LWIP_ASSERT("check that first pbuf can hold icmp message",
mbed_official 0:51ac1d130fd4 159 (q->len >= (8 + IP_HLEN + 8)));
mbed_official 0:51ac1d130fd4 160
mbed_official 0:51ac1d130fd4 161 iphdr = p->payload;
mbed_official 0:51ac1d130fd4 162
mbed_official 0:51ac1d130fd4 163 tehdr = q->payload;
mbed_official 0:51ac1d130fd4 164 tehdr->type = (u8_t)ICMP6_TE;
mbed_official 0:51ac1d130fd4 165 tehdr->icode = (u8_t)t;
mbed_official 0:51ac1d130fd4 166
mbed_official 0:51ac1d130fd4 167 /* copy fields from original packet */
mbed_official 0:51ac1d130fd4 168 SMEMCPY((u8_t *)q->payload + 8, (u8_t *)p->payload, IP_HLEN + 8);
mbed_official 0:51ac1d130fd4 169
mbed_official 0:51ac1d130fd4 170 /* calculate checksum */
mbed_official 0:51ac1d130fd4 171 tehdr->chksum = 0;
mbed_official 0:51ac1d130fd4 172 tehdr->chksum = inet_chksum(tehdr, q->len);
mbed_official 0:51ac1d130fd4 173 ICMP_STATS_INC(icmp.xmit);
mbed_official 0:51ac1d130fd4 174 ip_output(q, NULL,
mbed_official 0:51ac1d130fd4 175 (struct ip_addr *)&(iphdr->src), ICMP_TTL, IP_PROTO_ICMP);
mbed_official 0:51ac1d130fd4 176 pbuf_free(q);
mbed_official 0:51ac1d130fd4 177 }
mbed_official 0:51ac1d130fd4 178
mbed_official 0:51ac1d130fd4 179 #endif /* LWIP_ICMP */