00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "lwip/opt.h"
00043
00044 #include "lwip/def.h"
00045 #include "lwip/mem.h"
00046 #include "lwip/ip.h"
00047 #include "lwip/inet.h"
00048 #include "lwip/netif.h"
00049 #include "lwip/icmp.h"
00050 #include "lwip/udp.h"
00051 #include "lwip/tcp.h"
00052
00053 #include "lwip/stats.h"
00054
00055 #include "arch/perf.h"
00056
00057
00058
00059
00060
00061
00062 void
00063 ip_init(void)
00064 {
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074 struct netif *
00075 ip_route(struct ip_addr *dest)
00076 {
00077 struct netif *netif;
00078
00079 for(netif = netif_list; netif != NULL; netif = netif->next) {
00080 if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
00081 return netif;
00082 }
00083 }
00084
00085 return netif_default;
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095 static void
00096 ip_forward(struct pbuf *p, struct ip_hdr *iphdr)
00097 {
00098 struct netif *netif;
00099
00100 PERF_START;
00101
00102 if ((netif = ip_route((struct ip_addr *)&(iphdr->dest))) == NULL) {
00103
00104 LWIP_DEBUGF(IP_DEBUG, ("ip_input: no forwarding route found for "));
00105 #if IP_DEBUG
00106 ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
00107 #endif
00108 LWIP_DEBUGF(IP_DEBUG, ("\n"));
00109 pbuf_free(p);
00110 return;
00111 }
00112
00113 if (--iphdr->hoplim == 0) {
00114 #if LWIP_ICMP
00115
00116 if (iphdr->nexthdr != IP_PROTO_ICMP) {
00117 icmp_time_exceeded(p, ICMP_TE_TTL);
00118 }
00119 #endif
00120 pbuf_free(p);
00121 return;
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to "));
00133 #if IP_DEBUG
00134 ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
00135 #endif
00136 LWIP_DEBUGF(IP_DEBUG, ("\n"));
00137
00138 IP_STATS_INC(ip.fw);
00139 IP_STATS_INC(ip.xmit);
00140
00141 PERF_STOP("ip_forward");
00142
00143 netif->output(netif, p, (struct ip_addr *)&(iphdr->dest));
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 void
00157 ip_input(struct pbuf *p, struct netif *inp) {
00158 struct ip_hdr *iphdr;
00159 struct netif *netif;
00160
00161
00162 PERF_START;
00163
00164 #if IP_DEBUG
00165 ip_debug_print(p);
00166 #endif
00167
00168
00169 IP_STATS_INC(ip.recv);
00170
00171
00172 iphdr = p->payload;
00173
00174
00175 if (iphdr->v != 6) {
00176 LWIP_DEBUGF(IP_DEBUG, ("IP packet dropped due to bad version number\n"));
00177 #if IP_DEBUG
00178 ip_debug_print(p);
00179 #endif
00180 pbuf_free(p);
00181 IP_STATS_INC(ip.err);
00182 IP_STATS_INC(ip.drop);
00183 return;
00184 }
00185
00186
00187 for(netif = netif_list; netif != NULL; netif = netif->next) {
00188 #if IP_DEBUG
00189 LWIP_DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest "));
00190 ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
00191 LWIP_DEBUGF(IP_DEBUG, ("netif->ip_addr "));
00192 ip_addr_debug_print(IP_DEBUG, ((struct ip_addr *)&(iphdr->dest)));
00193 LWIP_DEBUGF(IP_DEBUG, ("\n"));
00194 #endif
00195 if (ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr))) {
00196 break;
00197 }
00198 }
00199
00200
00201 if (netif == NULL) {
00202
00203 #if IP_FORWARD
00204 ip_forward(p, iphdr);
00205 #endif
00206 pbuf_free(p);
00207 return;
00208 }
00209
00210 pbuf_realloc(p, IP_HLEN + ntohs(iphdr->len));
00211
00212
00213 #if IP_DEBUG
00214
00215
00216
00217 #endif
00218
00219 if(pbuf_header(p, -IP_HLEN)) {
00220 LWIP_ASSERT("Can't move over header in packet", 0);
00221 return;
00222 }
00223
00224 switch (iphdr->nexthdr) {
00225 case IP_PROTO_UDP:
00226 udp_input(p, inp);
00227 break;
00228 case IP_PROTO_TCP:
00229 tcp_input(p, inp);
00230 break;
00231 #if LWIP_ICMP
00232 case IP_PROTO_ICMP:
00233 icmp_input(p, inp);
00234 break;
00235 #endif
00236 default:
00237 #if LWIP_ICMP
00238
00239 icmp_dest_unreach(p, ICMP_DUR_PROTO);
00240 #endif
00241 pbuf_free(p);
00242 LWIP_DEBUGF(IP_DEBUG, ("Unsupported transport protocol %"U16_F"\n",
00243 iphdr->nexthdr));
00244
00245 IP_STATS_INC(ip.proterr);
00246 IP_STATS_INC(ip.drop);
00247 }
00248 PERF_STOP("ip_input");
00249 }
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 err_t
00260 ip_output_if (struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
00261 u8_t ttl,
00262 u8_t proto, struct netif *netif)
00263 {
00264 struct ip_hdr *iphdr;
00265
00266 PERF_START;
00267
00268 LWIP_DEBUGF(IP_DEBUG, ("len %"U16_F" tot_len %"U16_F"\n", p->len, p->tot_len));
00269 if (pbuf_header(p, IP_HLEN)) {
00270 LWIP_DEBUGF(IP_DEBUG, ("ip_output: not enough room for IP header in pbuf\n"));
00271 IP_STATS_INC(ip.err);
00272
00273 return ERR_BUF;
00274 }
00275 LWIP_DEBUGF(IP_DEBUG, ("len %"U16_F" tot_len %"U16_F"\n", p->len, p->tot_len));
00276
00277 iphdr = p->payload;
00278
00279
00280 if (dest != IP_HDRINCL) {
00281 LWIP_DEBUGF(IP_DEBUG, ("!IP_HDRLINCL\n"));
00282 iphdr->hoplim = ttl;
00283 iphdr->nexthdr = proto;
00284 iphdr->len = htons(p->tot_len - IP_HLEN);
00285 ip_addr_set(&(iphdr->dest), dest);
00286
00287 iphdr->v = 6;
00288
00289 if (ip_addr_isany(src)) {
00290 ip_addr_set(&(iphdr->src), &(netif->ip_addr));
00291 } else {
00292 ip_addr_set(&(iphdr->src), src);
00293 }
00294
00295 } else {
00296 dest = &(iphdr->dest);
00297 }
00298
00299 IP_STATS_INC(ip.xmit);
00300
00301 LWIP_DEBUGF(IP_DEBUG, ("ip_output_if: %c%c (len %"U16_F")\n", netif->name[0], netif->name[1], p->tot_len));
00302 #if IP_DEBUG
00303 ip_debug_print(p);
00304 #endif
00305
00306 PERF_STOP("ip_output_if");
00307 return netif->output(netif, p, dest);
00308 }
00309
00310
00311
00312
00313
00314
00315
00316 err_t
00317 ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
00318 u8_t ttl, u8_t proto)
00319 {
00320 struct netif *netif;
00321 if ((netif = ip_route(dest)) == NULL) {
00322 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
00323 IP_STATS_INC(ip.rterr);
00324 return ERR_RTE;
00325 }
00326
00327 return ip_output_if (p, src, dest, ttl, proto, netif);
00328 }
00329
00330 #if LWIP_NETIF_HWADDRHINT
00331 err_t
00332 ip_output_hinted(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
00333 u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
00334 {
00335 struct netif *netif;
00336 err_t err;
00337
00338 if ((netif = ip_route(dest)) == NULL) {
00339 LWIP_DEBUGF(IP_DEBUG, ("ip_output: No route to 0x%"X32_F"\n", dest->addr));
00340 IP_STATS_INC(ip.rterr);
00341 return ERR_RTE;
00342 }
00343
00344 netif->addr_hint = addr_hint;
00345 err = ip_output_if(p, src, dest, ttl, tos, proto, netif);
00346 netif->addr_hint = NULL;
00347
00348 return err;
00349 }
00350 #endif
00351
00352 #if IP_DEBUG
00353 void
00354 ip_debug_print(struct pbuf *p)
00355 {
00356 struct ip_hdr *iphdr = p->payload;
00357
00358 LWIP_DEBUGF(IP_DEBUG, ("IP header:\n"));
00359 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00360 LWIP_DEBUGF(IP_DEBUG, ("|%2"S16_F" | %"X16_F"%"X16_F" | %"X16_F"%"X16_F" | (v, traffic class, flow label)\n",
00361 iphdr->v,
00362 iphdr->tclass1, iphdr->tclass2,
00363 iphdr->flow1, iphdr->flow2));
00364 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00365 LWIP_DEBUGF(IP_DEBUG, ("| %5"U16_F" | %2"U16_F" | %2"U16_F" | (len, nexthdr, hoplim)\n",
00366 ntohs(iphdr->len),
00367 iphdr->nexthdr,
00368 iphdr->hoplim));
00369 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00370 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (src)\n",
00371 (ntohl(iphdr->src.addr[0]) >> 16) & 0xffff,
00372 ntohl(iphdr->src.addr[0]) & 0xffff));
00373 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (src)\n",
00374 (ntohl(iphdr->src.addr[1]) >> 16) & 0xffff,
00375 ntohl(iphdr->src.addr[1]) & 0xffff));
00376 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (src)\n",
00377 (ntohl(iphdr->src.addr[2]) >> 16) & 0xffff,
00378 ntohl(iphdr->src.addr[2]) & 0xffff));
00379 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (src)\n",
00380 (ntohl(iphdr->src.addr[3]) >> 16) & 0xffff,
00381 ntohl(iphdr->src.addr[3]) & 0xffff));
00382 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00383 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (dest)\n",
00384 (ntohl(iphdr->dest.addr[0]) >> 16) & 0xffff,
00385 ntohl(iphdr->dest.addr[0]) & 0xffff));
00386 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (dest)\n",
00387 (ntohl(iphdr->dest.addr[1]) >> 16) & 0xffff,
00388 ntohl(iphdr->dest.addr[1]) & 0xffff));
00389 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (dest)\n",
00390 (ntohl(iphdr->dest.addr[2]) >> 16) & 0xffff,
00391 ntohl(iphdr->dest.addr[2]) & 0xffff));
00392 LWIP_DEBUGF(IP_DEBUG, ("| %4"X32_F" | %4"X32_F" | (dest)\n",
00393 (ntohl(iphdr->dest.addr[3]) >> 16) & 0xffff,
00394 ntohl(iphdr->dest.addr[3]) & 0xffff));
00395 LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
00396 }
00397 #endif