81 #ifdef LWIP_HOOK_FILENAME 82 #include LWIP_HOOK_FILENAME 87 #ifndef LWIP_INLINE_IP_CHKSUM 88 #if LWIP_CHECKSUM_CTRL_PER_NETIF 89 #define LWIP_INLINE_IP_CHKSUM 0 91 #define LWIP_INLINE_IP_CHKSUM 1 95 #if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP 96 #define CHECKSUM_GEN_IP_INLINE 1 98 #define CHECKSUM_GEN_IP_INLINE 0 101 #if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT) 102 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 1 109 #if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) 111 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(DHCP_CLIENT_PORT)) \ 112 || (LWIP_IP_ACCEPT_UDP_PORT(port))) 113 #elif defined(LWIP_IP_ACCEPT_UDP_PORT) 115 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(port)) 118 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(DHCP_CLIENT_PORT)) 122 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 0 128 #if LWIP_MULTICAST_TX_OPTIONS 130 static struct netif* ip4_default_multicast_netif;
136 ip4_set_default_multicast_netif(
struct netif* default_multicast_netif)
138 ip4_default_multicast_netif = default_multicast_netif;
142 #ifdef LWIP_HOOK_IP4_ROUTE_SRC 148 ip4_route_src(
const ip4_addr_t *dest,
const ip4_addr_t *src)
152 struct netif *
netif = LWIP_HOOK_IP4_ROUTE_SRC(dest, src);
157 return ip4_route(dest);
171 ip4_route(
const ip4_addr_t *dest)
175 #if LWIP_MULTICAST_TX_OPTIONS 177 if (ip4_addr_ismulticast(dest) && ip4_default_multicast_netif) {
178 return ip4_default_multicast_netif;
187 if (ip4_addr_netcmp(dest, netif_ip4_addr(netif), netif_ip4_netmask(netif))) {
199 #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF 201 if (ip4_addr_isloopback(dest)) {
216 #ifdef LWIP_HOOK_IP4_ROUTE_SRC 217 netif = LWIP_HOOK_IP4_ROUTE_SRC(dest, NULL);
221 #elif defined(LWIP_HOOK_IP4_ROUTE) 222 netif = LWIP_HOOK_IP4_ROUTE(dest);
233 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
234 IP_STATS_INC(ip.rterr);
235 MIB2_STATS_INC(mib2.ipoutnoroutes);
251 ip4_canforward(
struct pbuf *p)
253 u32_t addr = lwip_htonl(ip4_addr_get_u32(ip4_current_dest_addr()));
264 if (IP_EXPERIMENTAL(addr)) {
267 if (IP_CLASSA(addr)) {
268 u32_t net = addr & IP_CLASSA_NET;
269 if ((net == 0) || (net == ((u32_t)IP_LOOPBACKNET << IP_CLASSA_NSHIFT))) {
287 ip4_forward(
struct pbuf *p,
struct ip_hdr *iphdr,
struct netif *inp)
294 if (!ip4_canforward(p)) {
299 if (ip4_addr_islinklocal(ip4_current_dest_addr())) {
300 LWIP_DEBUGF(
IP_DEBUG, (
"ip4_forward: not forwarding LLA %"U16_F
".%"U16_F
".%"U16_F
".%"U16_F
"\n",
301 ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
302 ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
307 netif = ip4_route_src(ip4_current_dest_addr(), ip4_current_src_addr());
309 LWIP_DEBUGF(
IP_DEBUG, (
"ip4_forward: no forwarding route for %"U16_F
".%"U16_F
".%"U16_F
".%"U16_F
" found\n",
310 ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
311 ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
315 #if !IP_FORWARD_ALLOW_TX_ON_RX_NETIF 319 LWIP_DEBUGF(
IP_DEBUG, (
"ip4_forward: not bouncing packets back on incoming interface.\n"));
325 IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);
327 if (IPH_TTL(iphdr) == 0) {
328 MIB2_STATS_INC(mib2.ipinhdrerrors);
331 if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) {
339 if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) {
340 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1);
342 IPH_CHKSUM_SET(iphdr, IPH_CHKSUM(iphdr) + PP_HTONS(0x100));
345 LWIP_DEBUGF(
IP_DEBUG, (
"ip4_forward: forwarding packet to %"U16_F
".%"U16_F
".%"U16_F
".%"U16_F
"\n",
346 ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
347 ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
350 MIB2_STATS_INC(mib2.ipforwdatagrams);
351 IP_STATS_INC(ip.xmit);
353 PERF_STOP(
"ip4_forward");
356 if ((IPH_OFFSET(iphdr) & PP_NTOHS(IP_DF)) == 0) {
358 ip4_frag(p, netif, ip4_current_dest_addr());
371 netif->output(netif, p, ip4_current_dest_addr());
374 MIB2_STATS_INC(mib2.ipoutnoroutes);
393 ip4_input(
struct pbuf *p,
struct netif *inp)
399 #if IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP 400 int check_ip_src = 1;
403 IP_STATS_INC(ip.recv);
404 MIB2_STATS_INC(mib2.ipinreceives);
408 if (IPH_V(iphdr) != 4) {
412 IP_STATS_INC(ip.err);
413 IP_STATS_INC(ip.drop);
414 MIB2_STATS_INC(mib2.ipinhdrerrors);
418 #ifdef LWIP_HOOK_IP4_INPUT 419 if (LWIP_HOOK_IP4_INPUT(p, inp)) {
426 iphdr_hlen = IPH_HL(iphdr);
430 iphdr_len = lwip_ntohs(IPH_LEN(iphdr));
433 if (iphdr_len < p->tot_len) {
438 if ((iphdr_hlen > p->
len) || (iphdr_len > p->
tot_len) || (iphdr_hlen < IP_HLEN)) {
439 if (iphdr_hlen < IP_HLEN) {
441 (
"ip4_input: short IP header (%"U16_F
" bytes) received, IP packet dropped\n", iphdr_hlen));
443 if (iphdr_hlen > p->
len) {
445 (
"IP header (len %"U16_F
") does not fit in first pbuf (len %"U16_F
"), IP packet dropped.\n",
446 iphdr_hlen, p->
len));
450 (
"IP (len %"U16_F
") is longer than pbuf (len %"U16_F
"), IP packet dropped.\n",
455 IP_STATS_INC(ip.lenerr);
456 IP_STATS_INC(ip.drop);
457 MIB2_STATS_INC(mib2.ipindiscards);
462 #if CHECKSUM_CHECK_IP 463 IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_IP) {
464 if (inet_chksum(iphdr, iphdr_hlen) != 0) {
467 (
"Checksum (0x%"X16_F
") failed, IP packet dropped.\n", inet_chksum(iphdr, iphdr_hlen)));
470 IP_STATS_INC(ip.chkerr);
471 IP_STATS_INC(ip.drop);
472 MIB2_STATS_INC(mib2.ipinhdrerrors);
483 if (ip4_addr_ismulticast(ip4_current_dest_addr())) {
487 ip4_addr_t allsystems;
488 IP4_ADDR(&allsystems, 224, 0, 0, 1);
489 if (ip4_addr_cmp(ip4_current_dest_addr(), &allsystems) &&
490 ip4_addr_isany(ip4_current_src_addr())) {
498 if ((
netif_is_up(inp)) && (!ip4_addr_isany_val(*netif_ip4_addr(inp)))) {
511 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",
512 ip4_addr_get_u32(&iphdr->dest), ip4_addr_get_u32(netif_ip4_addr(netif)),
513 ip4_addr_get_u32(&iphdr->dest) & ip4_addr_get_u32(netif_ip4_netmask(netif)),
514 ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif)),
515 ip4_addr_get_u32(&iphdr->dest) & ~ip4_addr_get_u32(netif_ip4_netmask(netif))));
518 if ((
netif_is_up(netif)) && (!ip4_addr_isany_val(*netif_ip4_addr(netif)))) {
520 if (ip4_addr_cmp(ip4_current_dest_addr(), netif_ip4_addr(netif)) ||
522 ip4_addr_isbroadcast(ip4_current_dest_addr(), netif)
524 || (ip4_addr_get_u32(ip4_current_dest_addr()) == PP_HTONL(IPADDR_LOOPBACK))
535 if (autoip_accept_packet(netif, ip4_current_dest_addr())) {
544 #if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF 548 if (ip4_addr_isloopback(ip4_current_dest_addr())) {
561 }
while (netif != NULL);
564 #if IP_ACCEPT_LINK_LAYER_ADDRESSING 576 if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
577 struct udp_hdr *udphdr = (
struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen);
579 lwip_ntohs(udphdr->dest)));
580 if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {
590 #if LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING 592 #
if IP_ACCEPT_LINK_LAYER_ADDRESSING
594 && !ip4_addr_isany_val(*ip4_current_src_addr())
599 if ((ip4_addr_isbroadcast(ip4_current_src_addr(), inp)) ||
600 (ip4_addr_ismulticast(ip4_current_src_addr()))) {
605 IP_STATS_INC(ip.drop);
606 MIB2_STATS_INC(mib2.ipinaddrerrors);
607 MIB2_STATS_INC(mib2.ipindiscards);
618 if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), inp)) {
620 ip4_forward(p, iphdr, inp);
624 IP_STATS_INC(ip.drop);
625 MIB2_STATS_INC(mib2.ipinaddrerrors);
626 MIB2_STATS_INC(mib2.ipindiscards);
632 if ((IPH_OFFSET(iphdr) & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
634 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",
635 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)));
646 lwip_ntohs(IPH_OFFSET(iphdr))));
647 IP_STATS_INC(ip.opterr);
648 IP_STATS_INC(ip.drop);
650 MIB2_STATS_INC(mib2.ipinunknownprotos);
655 #if IP_OPTIONS_ALLOWED == 0 659 if ((iphdr_hlen > IP_HLEN) && (IPH_PROTO(iphdr) != IP_PROTO_IGMP)) {
661 if (iphdr_hlen > IP_HLEN) {
665 IP_STATS_INC(ip.opterr);
666 IP_STATS_INC(ip.drop);
668 MIB2_STATS_INC(mib2.ipinunknownprotos);
680 ip_data.current_ip4_header = iphdr;
685 if (raw_input(p, inp) == 0)
690 switch (IPH_PROTO(iphdr)) {
694 case IP_PROTO_UDPLITE:
696 MIB2_STATS_INC(mib2.ipindelivers);
702 MIB2_STATS_INC(mib2.ipindelivers);
708 MIB2_STATS_INC(mib2.ipindelivers);
714 igmp_input(p, inp, ip4_current_dest_addr());
720 if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), netif) &&
721 !ip4_addr_ismulticast(ip4_current_dest_addr())) {
731 IP_STATS_INC(ip.proterr);
732 IP_STATS_INC(ip.drop);
733 MIB2_STATS_INC(mib2.ipinunknownprotos);
740 ip_data.current_ip4_header = NULL;
742 ip4_addr_set_any(ip4_current_src_addr());
743 ip4_addr_set_any(ip4_current_dest_addr());
774 ip4_output_if(
struct pbuf *p,
const ip4_addr_t *src,
const ip4_addr_t *dest,
776 u8_t proto,
struct netif *netif)
779 return ip4_output_if_opt(p, src, dest, ttl, tos, proto, netif, NULL, 0);
789 ip4_output_if_opt(
struct pbuf *p,
const ip4_addr_t *src,
const ip4_addr_t *dest,
790 u8_t ttl, u8_t tos, u8_t proto,
struct netif *netif,
void *ip_options,
794 const ip4_addr_t *src_used = src;
795 if (dest != LWIP_IP_HDRINCL) {
796 if (ip4_addr_isany(src)) {
797 src_used = netif_ip4_addr(netif);
802 return ip4_output_if_opt_src(p, src_used, dest, ttl, tos, proto, netif,
805 return ip4_output_if_src(p, src_used, dest, ttl, tos, proto, netif);
814 ip4_output_if_src(
struct pbuf *p,
const ip4_addr_t *src,
const ip4_addr_t *dest,
816 u8_t proto,
struct netif *netif)
819 return ip4_output_if_opt_src(p, src, dest, ttl, tos, proto, netif, NULL, 0);
827 ip4_output_if_opt_src(
struct pbuf *p,
const ip4_addr_t *src,
const ip4_addr_t *dest,
828 u8_t ttl, u8_t tos, u8_t proto,
struct netif *netif,
void *ip_options,
833 ip4_addr_t dest_addr;
834 #if CHECKSUM_GEN_IP_INLINE 840 MIB2_STATS_INC(mib2.ipoutrequests);
843 if (dest != LWIP_IP_HDRINCL) {
844 u16_t ip_hlen = IP_HLEN;
846 u16_t optlen_aligned = 0;
848 #if CHECKSUM_GEN_IP_INLINE 852 optlen_aligned = ((optlen + 3) & ~3);
853 ip_hlen += optlen_aligned;
857 IP_STATS_INC(ip.err);
858 MIB2_STATS_INC(mib2.ipoutdiscards);
861 MEMCPY(p->
payload, ip_options, optlen);
862 if (optlen < optlen_aligned) {
864 memset(((
char*)p->
payload) + optlen, 0, optlen_aligned - optlen);
866 #if CHECKSUM_GEN_IP_INLINE 867 for (i = 0; i < optlen_aligned/2; i++) {
868 chk_sum += ((u16_t*)p->
payload)[i];
877 IP_STATS_INC(ip.err);
878 MIB2_STATS_INC(mib2.ipoutdiscards);
883 LWIP_ASSERT(
"check that first pbuf can hold struct ip_hdr",
886 IPH_TTL_SET(iphdr, ttl);
887 IPH_PROTO_SET(iphdr, proto);
888 #if CHECKSUM_GEN_IP_INLINE 889 chk_sum += PP_NTOHS(proto | (ttl << 8));
893 ip4_addr_copy(iphdr->dest, *dest);
894 #if CHECKSUM_GEN_IP_INLINE 895 chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
896 chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
899 IPH_VHL_SET(iphdr, 4, ip_hlen / 4);
900 IPH_TOS_SET(iphdr, tos);
901 #if CHECKSUM_GEN_IP_INLINE 902 chk_sum += PP_NTOHS(tos | (iphdr->_v_hl << 8));
904 IPH_LEN_SET(iphdr, lwip_htons(p->
tot_len));
905 #if CHECKSUM_GEN_IP_INLINE 906 chk_sum += iphdr->_len;
908 IPH_OFFSET_SET(iphdr, 0);
909 IPH_ID_SET(iphdr, lwip_htons(ip_id));
910 #if CHECKSUM_GEN_IP_INLINE 911 chk_sum += iphdr->_id;
916 ip4_addr_copy(iphdr->src, *IP4_ADDR_ANY4);
919 ip4_addr_copy(iphdr->src, *src);
922 #if CHECKSUM_GEN_IP_INLINE 923 chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
924 chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
925 chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
926 chk_sum = (chk_sum >> 16) + chk_sum;
928 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
929 iphdr->_chksum = (u16_t)chk_sum;
931 #if LWIP_CHECKSUM_CTRL_PER_NETIF 933 IPH_CHKSUM_SET(iphdr, 0);
937 IPH_CHKSUM_SET(iphdr, 0);
939 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
940 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, ip_hlen));
947 ip4_addr_copy(dest_addr, iphdr->dest);
951 IP_STATS_INC(ip.xmit);
957 if (ip4_addr_cmp(dest, netif_ip4_addr(netif))
959 || ip4_addr_isloopback(dest)
964 return netif_loop_output(netif, p);
966 #if LWIP_MULTICAST_TX_OPTIONS 968 netif_loop_output(netif, p);
975 return ip4_frag(p, netif, dest);
980 return netif->output(netif, p, dest);
1001 ip4_output(
struct pbuf *p,
const ip4_addr_t *src,
const ip4_addr_t *dest,
1002 u8_t ttl, u8_t tos, u8_t proto)
1004 struct netif *netif;
1008 if ((netif = ip4_route_src(dest, src)) == NULL) {
1010 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
1011 IP_STATS_INC(ip.rterr);
1015 return ip4_output_if(p, src, dest, ttl, tos, proto, netif);
1018 #if LWIP_NETIF_HWADDRHINT 1038 ip4_output_hinted(
struct pbuf *p,
const ip4_addr_t *src,
const ip4_addr_t *dest,
1039 u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint)
1041 struct netif *netif;
1046 if ((netif = ip4_route_src(dest, src)) == NULL) {
1048 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
1049 IP_STATS_INC(ip.rterr);
1053 NETIF_SET_HWADDRHINT(netif, addr_hint);
1054 err = ip4_output_if(p, src, dest, ttl, tos, proto, netif);
1055 NETIF_SET_HWADDRHINT(netif, NULL);
1066 ip4_debug_print(
struct pbuf *p)
1072 LWIP_DEBUGF(
IP_DEBUG, (
"|%2"S16_F
" |%2"S16_F
" | 0x%02"X16_F
" | %5"U16_F
" | (v, hl, tos, len)\n",
1073 (u16_t)IPH_V(iphdr),
1074 (u16_t)IPH_HL(iphdr),
1075 (u16_t)IPH_TOS(iphdr),
1076 lwip_ntohs(IPH_LEN(iphdr))));
1078 LWIP_DEBUGF(
IP_DEBUG, (
"| %5"U16_F
" |%"U16_F
"%"U16_F
"%"U16_F
"| %4"U16_F
" | (id, flags, offset)\n",
1079 lwip_ntohs(IPH_ID(iphdr)),
1080 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 15 & 1),
1081 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 14 & 1),
1082 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) >> 13 & 1),
1083 (u16_t)(lwip_ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK)));
1086 (u16_t)IPH_TTL(iphdr),
1087 (u16_t)IPH_PROTO(iphdr),
1088 lwip_ntohs(IPH_CHKSUM(iphdr))));
1091 ip4_addr1_16(&iphdr->src),
1092 ip4_addr2_16(&iphdr->src),
1093 ip4_addr3_16(&iphdr->src),
1094 ip4_addr4_16(&iphdr->src)));
1097 ip4_addr1_16(&iphdr->dest),
1098 ip4_addr2_16(&iphdr->dest),
1099 ip4_addr3_16(&iphdr->dest),
1100 ip4_addr4_16(&iphdr->dest)));
void pbuf_realloc(struct pbuf *p, u16_t new_len)
struct netif * netif_list
struct netif * netif_default
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
#define netif_is_link_up(netif)
#define PBUF_FLAG_LLBCAST
#define LWIP_DBG_LEVEL_SERIOUS
#define NETIF_FLAG_BROADCAST
#define PBUF_FLAG_MCASTLOOP
struct netif * current_netif
ip_addr_t current_iphdr_dest
u16_t current_ip_header_tot_len
#define PBUF_FLAG_LLMCAST
#define LWIP_DEBUGF(debug, message)
#define netif_is_up(netif)
#define LWIP_DBG_LEVEL_WARNING
#define LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p)
u8_t pbuf_header_force(struct pbuf *p, s16_t header_size_increment)
#define LWIP_UNUSED_ARG(x)
u8_t pbuf_free(struct pbuf *p)
struct netif * current_input_netif
ip_addr_t current_iphdr_src