62 #ifdef LWIP_HOOK_FILENAME
63 #include LWIP_HOOK_FILENAME
68 #ifndef LWIP_INLINE_IP_CHKSUM
69 #if LWIP_CHECKSUM_CTRL_PER_NETIF
70 #define LWIP_INLINE_IP_CHKSUM 0
72 #define LWIP_INLINE_IP_CHKSUM 1
76 #if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP
77 #define CHECKSUM_GEN_IP_INLINE 1
79 #define CHECKSUM_GEN_IP_INLINE 0
82 #if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT)
83 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 1
90 #if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT)
92 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(DHCP_CLIENT_PORT)) \
93 || (LWIP_IP_ACCEPT_UDP_PORT(port)))
94 #elif defined(LWIP_IP_ACCEPT_UDP_PORT)
96 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(port))
99 #define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(DHCP_CLIENT_PORT))
103 #define IP_ACCEPT_LINK_LAYER_ADDRESSING 0
109 #if LWIP_MULTICAST_TX_OPTIONS
111 static struct netif* ip4_default_multicast_netif;
117 ip4_set_default_multicast_netif(
struct netif* default_multicast_netif)
119 ip4_default_multicast_netif = default_multicast_netif;
123 #ifdef LWIP_HOOK_IP4_ROUTE_SRC
129 ip4_route_src(
const ip4_addr_t *dest,
const ip4_addr_t *src)
133 struct netif *
netif = LWIP_HOOK_IP4_ROUTE_SRC(dest, src);
138 return ip4_route(dest);
152 ip4_route(
const ip4_addr_t *dest)
156 #if LWIP_MULTICAST_TX_OPTIONS
158 if (ip4_addr_ismulticast(dest) && ip4_default_multicast_netif) {
159 return ip4_default_multicast_netif;
168 if (ip4_addr_netcmp(dest, netif_ip4_addr(
netif), netif_ip4_netmask(
netif))) {
180 #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF
182 if (ip4_addr_isloopback(dest)) {
197 #ifdef LWIP_HOOK_IP4_ROUTE_SRC
198 netif = LWIP_HOOK_IP4_ROUTE_SRC(dest,
NULL);
202 #elif defined(LWIP_HOOK_IP4_ROUTE)
203 netif = LWIP_HOOK_IP4_ROUTE(dest);
214 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
232 ip4_canforward(
struct pbuf *p)
245 if (IP_EXPERIMENTAL(addr)) {
248 if (IP_CLASSA(addr)) {
250 if ((
net == 0) || (
net == ((
u32_t)IP_LOOPBACKNET << IP_CLASSA_NSHIFT))) {
275 if (!ip4_canforward(p)) {
280 if (ip4_addr_islinklocal(ip4_current_dest_addr())) {
282 ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
283 ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
288 netif = ip4_route_src(ip4_current_dest_addr(), ip4_current_src_addr());
291 ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
292 ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
296 #if !IP_FORWARD_ALLOW_TX_ON_RX_NETIF
300 LWIP_DEBUGF(
IP_DEBUG, (
"ip4_forward: not bouncing packets back on incoming interface.\n"));
327 ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()),
328 ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr())));
339 ip4_frag(p,
netif, ip4_current_dest_addr());
352 netif->output(
netif, p, ip4_current_dest_addr());
374 ip4_input(
struct pbuf *p,
struct netif *inp)
380 #if IP_ACCEPT_LINK_LAYER_ADDRESSING || LWIP_IGMP
381 int check_ip_src = 1;
389 if (
IPH_V(iphdr) != 4) {
399 #ifdef LWIP_HOOK_IP4_INPUT
400 if (LWIP_HOOK_IP4_INPUT(p, inp)) {
407 iphdr_hlen =
IPH_HL(iphdr);
414 if (iphdr_len < p->tot_len) {
419 if ((iphdr_hlen > p->
len) || (iphdr_len > p->
tot_len) || (iphdr_hlen <
IP_HLEN)) {
422 (
"ip4_input: short IP header (%"U16_F" bytes) received, IP packet dropped\n", iphdr_hlen));
424 if (iphdr_hlen > p->
len) {
426 (
"IP header (len %"U16_F") does not fit in first pbuf (len %"U16_F"), IP packet dropped.\n",
427 iphdr_hlen, p->
len));
431 (
"IP (len %"U16_F") is longer than pbuf (len %"U16_F"), IP packet dropped.\n",
443 #if CHECKSUM_CHECK_IP
448 (
"Checksum (0x%"X16_F") failed, IP packet dropped.\n",
inet_chksum(iphdr, iphdr_hlen)));
464 if (ip4_addr_ismulticast(ip4_current_dest_addr())) {
469 ip4_addr_t allsystems;
470 IP4_ADDR(&allsystems, 224, 0, 0, 1);
471 if (ip4_addr_cmp(ip4_current_dest_addr(), &allsystems) &&
472 ip4_addr_isany(ip4_current_src_addr())) {
481 if ((
netif_is_up(inp)) && (!ip4_addr_isany_val(*netif_ip4_addr(inp)))) {
494 if (ip4_addr_get_u32(&iphdr->dest) == ip4_addr_get_u32(netif_ip4_addr(
netif)))
496 ip4_addr_get_u32(&iphdr->dest), ip4_addr_get_u32(netif_ip4_addr(
netif)),
497 ip4_addr_get_u32(&iphdr->dest) & ip4_addr_get_u32(netif_ip4_netmask(
netif)),
498 ip4_addr_get_u32(netif_ip4_addr(
netif)) & ip4_addr_get_u32(netif_ip4_netmask(
netif)),
499 ip4_addr_get_u32(&iphdr->dest) & ~ip4_addr_get_u32(netif_ip4_netmask(
netif))));
504 if (ip4_addr_cmp(ip4_current_dest_addr(), netif_ip4_addr(
netif)) ||
506 ip4_addr_isbroadcast(ip4_current_dest_addr(),
netif)
508 || (ip4_addr_get_u32(ip4_current_dest_addr()) ==
PP_HTONL(IPADDR_LOOPBACK))
519 if (autoip_accept_packet(
netif, ip4_current_dest_addr())) {
528 #if !LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF
532 if (ip4_addr_isloopback(ip4_current_dest_addr())) {
549 #if IP_ACCEPT_LINK_LAYER_ADDRESSING
565 if (IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(udphdr->dest)) {
575 #if LWIP_IGMP || IP_ACCEPT_LINK_LAYER_ADDRESSING
577 #
if IP_ACCEPT_LINK_LAYER_ADDRESSING
579 && !ip4_addr_isany_val(*ip4_current_src_addr())
584 if ((ip4_addr_isbroadcast(ip4_current_src_addr(), inp)) ||
585 (ip4_addr_ismulticast(ip4_current_src_addr()))) {
603 if (!ip4_addr_isbroadcast(ip4_current_dest_addr(), inp)) {
605 ip4_forward(p, iphdr, inp);
640 #if IP_OPTIONS_ALLOWED == 0
665 ip_data.current_ip4_header = iphdr;
670 if (raw_input(p, inp) == 0)
700 igmp_input(p, inp, ip4_current_dest_addr());
706 if (!ip4_addr_isbroadcast(ip4_current_dest_addr(),
netif) &&
707 !ip4_addr_ismulticast(ip4_current_dest_addr())) {
728 ip4_addr_set_any(ip4_current_src_addr());
729 ip4_addr_set_any(ip4_current_dest_addr());
760 ip4_output_if(
struct pbuf *p,
const ip4_addr_t *src,
const ip4_addr_t *dest,
765 return ip4_output_if_opt(p, src, dest, ttl, tos, proto,
netif,
NULL, 0);
775 ip4_output_if_opt(
struct pbuf *p,
const ip4_addr_t *src,
const ip4_addr_t *dest,
780 const ip4_addr_t *src_used = src;
782 if (ip4_addr_isany(src)) {
783 src_used = netif_ip4_addr(
netif);
788 return ip4_output_if_opt_src(p, src_used, dest, ttl, tos, proto,
netif,
791 return ip4_output_if_src(p, src_used, dest, ttl, tos, proto,
netif);
800 ip4_output_if_src(
struct pbuf *p,
const ip4_addr_t *src,
const ip4_addr_t *dest,
805 return ip4_output_if_opt_src(p, src, dest, ttl, tos, proto,
netif,
NULL, 0);
813 ip4_output_if_opt_src(
struct pbuf *p,
const ip4_addr_t *src,
const ip4_addr_t *dest,
819 ip4_addr_t dest_addr;
820 #if CHECKSUM_GEN_IP_INLINE
832 u16_t optlen_aligned = 0;
834 #if CHECKSUM_GEN_IP_INLINE
838 optlen_aligned = ((optlen + 3) & ~3);
839 ip_hlen += optlen_aligned;
848 if (optlen < optlen_aligned) {
850 memset(((
char*)p->
payload) + optlen, 0, optlen_aligned - optlen);
852 #if CHECKSUM_GEN_IP_INLINE
853 for (i = 0; i < optlen_aligned/2; i++) {
869 LWIP_ASSERT(
"check that first pbuf can hold struct ip_hdr",
874 #if CHECKSUM_GEN_IP_INLINE
875 chk_sum +=
PP_NTOHS(proto | (ttl << 8));
879 ip4_addr_copy(iphdr->dest, *dest);
880 #if CHECKSUM_GEN_IP_INLINE
881 chk_sum += ip4_addr_get_u32(&iphdr->dest) & 0xFFFF;
882 chk_sum += ip4_addr_get_u32(&iphdr->dest) >> 16;
887 #if CHECKSUM_GEN_IP_INLINE
888 chk_sum +=
PP_NTOHS(tos | (iphdr->_v_hl << 8));
891 #if CHECKSUM_GEN_IP_INLINE
892 chk_sum += iphdr->_len;
896 #if CHECKSUM_GEN_IP_INLINE
897 chk_sum += iphdr->_id;
902 ip4_addr_copy(iphdr->src, *IP4_ADDR_ANY4);
905 ip4_addr_copy(iphdr->src, *src);
908 #if CHECKSUM_GEN_IP_INLINE
909 chk_sum += ip4_addr_get_u32(&iphdr->src) & 0xFFFF;
910 chk_sum += ip4_addr_get_u32(&iphdr->src) >> 16;
911 chk_sum = (chk_sum >> 16) + (chk_sum & 0xFFFF);
912 chk_sum = (chk_sum >> 16) + chk_sum;
915 iphdr->_chksum = (
u16_t)chk_sum;
917 #if LWIP_CHECKSUM_CTRL_PER_NETIF
933 ip4_addr_copy(dest_addr, iphdr->dest);
943 if (ip4_addr_cmp(dest, netif_ip4_addr(
netif))
945 || ip4_addr_isloopback(dest)
950 return netif_loop_output(
netif, p);
952 #if LWIP_MULTICAST_TX_OPTIONS
954 netif_loop_output(
netif, p);
961 return ip4_frag(p,
netif, dest);
987 ip4_output(
struct pbuf *p,
const ip4_addr_t *src,
const ip4_addr_t *dest,
994 if ((
netif = ip4_route_src(dest, src)) ==
NULL) {
996 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
1001 return ip4_output_if(p, src, dest, ttl, tos, proto,
netif);
1004 #if LWIP_NETIF_HWADDRHINT
1024 ip4_output_hinted(
struct pbuf *p,
const ip4_addr_t *src,
const ip4_addr_t *dest,
1032 if ((
netif = ip4_route_src(dest, src)) ==
NULL) {
1034 ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
1040 err = ip4_output_if(p, src, dest, ttl, tos, proto,
netif);
1052 ip4_debug_print(
struct pbuf *p)
1077 ip4_addr1_16(&iphdr->src),
1078 ip4_addr2_16(&iphdr->src),
1079 ip4_addr3_16(&iphdr->src),
1080 ip4_addr4_16(&iphdr->src)));
1083 ip4_addr1_16(&iphdr->dest),
1084 ip4_addr2_16(&iphdr->dest),
1085 ip4_addr3_16(&iphdr->dest),
1086 ip4_addr4_16(&iphdr->dest)));