Changeset 262 for uKadecot/trunk/uip/net/ipv4/uip.c
- Timestamp:
- Nov 18, 2016, 2:58:30 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uKadecot/trunk/uip/net/ipv4/uip.c
r158 r262 99 99 #if UIP_FIXEDADDR > 0 100 100 const uip_ipaddr_t uip_hostaddr = 101 {HTONS((UIP_IPADDR0 << 8) | UIP_IPADDR1), 102 HTONS((UIP_IPADDR2 << 8) | UIP_IPADDR3)}; 101 { UIP_IPADDR0, UIP_IPADDR1, UIP_IPADDR2, UIP_IPADDR3 }; 103 102 const uip_ipaddr_t uip_draddr = 104 {HTONS((UIP_DRIPADDR0 << 8) | UIP_DRIPADDR1), 105 HTONS((UIP_DRIPADDR2 << 8) | UIP_DRIPADDR3)}; 103 { UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3 }; 106 104 const uip_ipaddr_t uip_netmask = 107 {HTONS((UIP_NETMASK0 << 8) | UIP_NETMASK1), 108 HTONS((UIP_NETMASK2 << 8) | UIP_NETMASK3)}; 105 { UIP_NETMASK0, UIP_NETMASK1, UIP_NETMASK2, UIP_NETMASK3 }; 109 106 #else 110 107 uip_ipaddr_t uip_hostaddr, uip_draddr, uip_netmask; 111 108 #endif /* UIP_FIXEDADDR */ 112 109 113 static const uip_ipaddr_t all_ones_addr =110 const uip_ipaddr_t uip_broadcast_addr = 114 111 #if UIP_CONF_IPV6 115 {0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff}; 112 { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 113 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }; 116 114 #else /* UIP_CONF_IPV6 */ 117 { 0xffff,0xffff};115 { { 0xff, 0xff, 0xff, 0xff } }; 118 116 #endif /* UIP_CONF_IPV6 */ 119 static const uip_ipaddr_t all_zeroes_addr = 120 #if UIP_CONF_IPV6 121 {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000}; 122 #else /* UIP_CONF_IPV6 */ 123 {0x0000,0x0000}; 124 #endif /* UIP_CONF_IPV6 */ 125 117 const uip_ipaddr_t uip_all_zeroes_addr = { { 0x0, /* rest is 0 */ } }; 126 118 127 119 #if UIP_FIXEDETHADDR 128 const struct uip_eth_addr uip_ ethaddr = {{UIP_ETHADDR0,120 const struct uip_eth_addr uip_lladdr = {{UIP_ETHADDR0, 129 121 UIP_ETHADDR1, 130 122 UIP_ETHADDR2, … … 133 125 UIP_ETHADDR5}}; 134 126 #else 135 struct uip_eth_addr uip_ ethaddr = {{0,0,0,0,0,0}};127 struct uip_eth_addr uip_lladdr = {{0,0,0,0,0,0}}; 136 128 #endif 137 129 … … 311 303 uip_chksum(uint16_t *data, uint16_t len) 312 304 { 313 return htons(chksum(0, (uint8_t *)data, len));305 return uip_htons(chksum(0, (uint8_t *)data, len)); 314 306 } 315 307 /*---------------------------------------------------------------------------*/ … … 322 314 sum = chksum(0, &uip_buf[UIP_LLH_LEN], UIP_IPH_LEN); 323 315 DEBUG_PRINTF("uip_ipchksum: sum 0x%04x\n", sum); 324 return (sum == 0) ? 0xffff : htons(sum);316 return (sum == 0) ? 0xffff : uip_htons(sum); 325 317 } 326 318 #endif … … 343 335 sum = upper_layer_len + proto; 344 336 /* Sum IP source and destination addresses. */ 345 sum = chksum(sum, (uint8_t *)&BUF->srcipaddr [0], 2 * sizeof(uip_ipaddr_t));337 sum = chksum(sum, (uint8_t *)&BUF->srcipaddr, 2 * sizeof(uip_ipaddr_t)); 346 338 347 339 /* Sum TCP header and data. */ … … 349 341 upper_layer_len); 350 342 351 return (sum == 0) ? 0xffff : htons(sum);343 return (sum == 0) ? 0xffff : uip_htons(sum); 352 344 } 353 345 /*---------------------------------------------------------------------------*/ … … 375 367 #endif /* UIP_UDP_CHECKSUMS */ 376 368 #endif /* UIP_ARCH_CHKSUM */ 377 uint8_t378 uip_ismulticast(uip_ipaddr_t ipaddr)379 {380 #if UIP_CONF_IPV6381 return 0;382 #else383 static const uint16_t multicast_ipaddr[2] = { 0x00e0, 0x0000 };384 static const uint16_t multicast_mask[2] = { 0x00f0, 0x0000 };385 return uip_ipaddr_maskcmp(ipaddr, multicast_ipaddr, multicast_mask);386 #endif387 }388 369 /*---------------------------------------------------------------------------*/ 389 370 void … … 433 414 conn = &uip_conns[c]; 434 415 if(conn->tcpstateflags != UIP_CLOSED && 435 conn->lport == htons(lastport)) {416 conn->lport == uip_htons(lastport)) { 436 417 goto again; 437 418 } … … 472 453 conn->sa = 0; 473 454 conn->sv = 16; /* Initial value of the RTT variance. */ 474 conn->lport = htons(lastport);455 conn->lport = uip_htons(lastport); 475 456 conn->rport = rport; 476 457 uip_ipaddr_copy(&conn->ripaddr, ripaddr); … … 482 463 #if UIP_UDP 483 464 struct uip_udp_conn * 484 uip_udp_new( uip_ipaddr_t *ripaddr, uint16_t rport)465 uip_udp_new(const uip_ipaddr_t *ripaddr, uint16_t rport) 485 466 { 486 467 register struct uip_udp_conn *conn; … … 495 476 496 477 for(c = 0; c < UIP_UDP_CONNS; ++c) { 497 if(uip_udp_conns[c].lport == htons(lastport)) {478 if(uip_udp_conns[c].lport == uip_htons(lastport)) { 498 479 goto again; 499 480 } … … 513 494 } 514 495 515 conn->lport = HTONS(lastport);496 conn->lport = UIP_HTONS(lastport); 516 497 conn->rport = rport; 517 498 if(ripaddr == NULL) { 518 memset( conn->ripaddr, 0, sizeof(uip_ipaddr_t));499 memset(&conn->ripaddr, 0, sizeof(uip_ipaddr_t)); 519 500 } else { 520 501 uip_ipaddr_copy(&conn->ripaddr, ripaddr); … … 748 729 connection's timer and see if it has reached the RTO value 749 730 in which case we retransmit. */ 731 750 732 if(uip_outstanding(uip_connr)) { 751 733 if(uip_connr->timer-- == 0) { … … 901 883 #endif /* UIP_CONF_IPV6 */ 902 884 903 if(uip_ipaddr_cmp( uip_hostaddr,all_zeroes_addr)) {885 if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr)) { 904 886 /* If we are configured to use ping IP address configuration and 905 887 hasn't been assigned an IP address yet, we accept all ICMP … … 921 903 DEBUG_PRINTF("UDP IP checksum 0x%04x\n", uip_ipchksum()); 922 904 if(BUF->proto == UIP_PROTO_UDP && 923 (uip_ipaddr_cmp(BUF->destipaddr, all_ones_addr) || 924 uip_ismulticast(BUF->destipaddr)) 925 /*&& 926 uip_ipchksum() == 0xffff*/) { 905 (uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr) || 906 (BUF->destipaddr.u8[0] & 224) == 224)) { /* XXX this is a 907 hack to be able 908 to receive UDP 909 multicast 910 packets. We check 911 for the bit 912 pattern of the 913 multicast 914 prefix. */ 927 915 goto udp_input; 928 916 } … … 931 919 /* Check if the packet is destined for our IP address. */ 932 920 #if !UIP_CONF_IPV6 933 if(!uip_ipaddr_cmp( BUF->destipaddr,uip_hostaddr)) {921 if(!uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr)) { 934 922 UIP_STAT(++uip_stat.ip.drop); 935 923 goto drop; … … 941 929 address) as well. However, we will cheat here and accept all 942 930 multicast packets that are sent to the ff02::/16 addresses. */ 943 if(!uip_ipaddr_cmp( BUF->destipaddr,uip_hostaddr) &&944 BUF->destipaddr [0] !=HTONS(0xff02)) {931 if(!uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr) && 932 BUF->destipaddr.u16[0] != UIP_HTONS(0xff02)) { 945 933 UIP_STAT(++uip_stat.ip.drop); 946 934 goto drop; … … 1000 988 ourself. */ 1001 989 #if UIP_PINGADDRCONF 1002 if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) { 1003 uip_hostaddr[0] = BUF->destipaddr[0]; 1004 uip_hostaddr[1] = BUF->destipaddr[1]; 990 if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr)) { 991 uip_hostaddr = BUF->destipaddr; 1005 992 } 1006 993 #endif /* UIP_PINGADDRCONF */ … … 1008 995 ICMPBUF->type = ICMP_ECHO_REPLY; 1009 996 1010 if(ICMPBUF->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8))) {1011 ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;997 if(ICMPBUF->icmpchksum >= UIP_HTONS(0xffff - (ICMP_ECHO << 8))) { 998 ICMPBUF->icmpchksum += UIP_HTONS(ICMP_ECHO << 8) + 1; 1012 999 } else { 1013 ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8);1000 ICMPBUF->icmpchksum += UIP_HTONS(ICMP_ECHO << 8); 1014 1001 } 1015 1002 1016 1003 /* Swap IP addresses. */ 1017 uip_ipaddr_copy( BUF->destipaddr,BUF->srcipaddr);1018 uip_ipaddr_copy( BUF->srcipaddr,uip_hostaddr);1004 uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr); 1005 uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr); 1019 1006 1020 1007 UIP_STAT(++uip_stat.icmp.sent); 1021 goto send; 1008 BUF->ttl = UIP_TTL; 1009 goto ip_send_nolen; 1022 1010 1023 1011 /* End of IPv4 input header processing code. */ … … 1040 1028 a neighbor advertisement message back. */ 1041 1029 if(ICMPBUF->type == ICMP6_NEIGHBOR_SOLICITATION) { 1042 if(uip_ipaddr_cmp( ICMPBUF->icmp6data,uip_hostaddr)) {1030 if(uip_ipaddr_cmp(&ICMPBUF->icmp6data, &uip_hostaddr)) { 1043 1031 1044 1032 if(ICMPBUF->options[0] == ICMP6_OPTION_SOURCE_LINK_ADDRESS) { 1045 1033 /* Save the sender's address in our neighbor list. */ 1046 uip_neighbor_add( ICMPBUF->srcipaddr, &(ICMPBUF->options[2]));1034 uip_neighbor_add(&ICMPBUF->srcipaddr, &(ICMPBUF->options[2])); 1047 1035 } 1048 1036 … … 1054 1042 ICMPBUF->reserved1 = ICMPBUF->reserved2 = ICMPBUF->reserved3 = 0; 1055 1043 1056 uip_ipaddr_copy( ICMPBUF->destipaddr,ICMPBUF->srcipaddr);1057 uip_ipaddr_copy( ICMPBUF->srcipaddr,uip_hostaddr);1044 uip_ipaddr_copy(&ICMPBUF->destipaddr, &ICMPBUF->srcipaddr); 1045 uip_ipaddr_copy(&ICMPBUF->srcipaddr, &uip_hostaddr); 1058 1046 ICMPBUF->options[0] = ICMP6_OPTION_TARGET_LINK_ADDRESS; 1059 1047 ICMPBUF->options[1] = 1; /* Options length, 1 = 8 bytes. */ 1060 memcpy(&(ICMPBUF->options[2]), &uip_ ethaddr, sizeof(uip_ethaddr));1048 memcpy(&(ICMPBUF->options[2]), &uip_lladdr, sizeof(uip_lladdr)); 1061 1049 ICMPBUF->icmpchksum = 0; 1062 1050 ICMPBUF->icmpchksum = ~uip_icmp6chksum(); 1051 1063 1052 goto send; 1064 1053 … … 1072 1061 ICMPBUF->type = ICMP6_ECHO_REPLY; 1073 1062 1074 uip_ipaddr_copy( BUF->destipaddr,BUF->srcipaddr);1075 uip_ipaddr_copy( BUF->srcipaddr,uip_hostaddr);1063 uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr); 1064 uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr); 1076 1065 ICMPBUF->icmpchksum = 0; 1077 1066 ICMPBUF->icmpchksum = ~uip_icmp6chksum(); … … 1110 1099 uip_len = uip_len - UIP_IPUDPH_LEN; 1111 1100 #endif /* UIP_UDP_CHECKSUMS */ 1101 1102 /* Make sure that the UDP destination port number is not zero. */ 1103 if(UDPBUF->destport == 0) { 1104 UIP_LOG("udp: zero port."); 1105 goto drop; 1106 } 1112 1107 1113 1108 /* Demultiplex this UDP packet between the UDP "connections". */ … … 1126 1121 (uip_udp_conn->rport == 0 || 1127 1122 UDPBUF->srcport == uip_udp_conn->rport) && 1128 (uip_ipaddr_cmp( uip_udp_conn->ripaddr,all_zeroes_addr) ||1129 uip_ipaddr_cmp( uip_udp_conn->ripaddr, all_ones_addr) ||1130 uip_ipaddr_cmp( BUF->srcipaddr,uip_udp_conn->ripaddr))) {1123 (uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_all_zeroes_addr) || 1124 uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_broadcast_addr) || 1125 uip_ipaddr_cmp(&BUF->srcipaddr, &uip_udp_conn->ripaddr))) { 1131 1126 goto udp_found; 1132 1127 } … … 1141 1136 uip_slen = 0; 1142 1137 UIP_UDP_APPCALL(); 1138 1143 1139 udp_send: 1144 1140 if(uip_slen == 0) { … … 1160 1156 BUF->proto = UIP_PROTO_UDP; 1161 1157 1162 UDPBUF->udplen = HTONS(uip_slen + UIP_UDPH_LEN);1158 UDPBUF->udplen = UIP_HTONS(uip_slen + UIP_UDPH_LEN); 1163 1159 UDPBUF->udpchksum = 0; 1164 1160 … … 1166 1162 BUF->destport = uip_udp_conn->rport; 1167 1163 1168 uip_ipaddr_copy( BUF->srcipaddr,uip_hostaddr);1169 uip_ipaddr_copy( BUF->destipaddr,uip_udp_conn->ripaddr);1164 uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr); 1165 uip_ipaddr_copy(&BUF->destipaddr, &uip_udp_conn->ripaddr); 1170 1166 1171 1167 uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN]; … … 1196 1192 } 1197 1193 1194 /* Make sure that the TCP port number is not zero. */ 1195 if(BUF->destport == 0 || BUF->srcport == 0) { 1196 UIP_LOG("tcp: zero port."); 1197 goto drop; 1198 } 1198 1199 1199 1200 /* Demultiplex this segment. */ … … 1204 1205 BUF->destport == uip_connr->lport && 1205 1206 BUF->srcport == uip_connr->rport && 1206 uip_ipaddr_cmp( BUF->srcipaddr,uip_connr->ripaddr)) {1207 uip_ipaddr_cmp(&BUF->srcipaddr, &uip_connr->ripaddr)) { 1207 1208 goto found; 1208 1209 } … … 1220 1221 /* Next, check listening connections. */ 1221 1222 for(c = 0; c < UIP_LISTENPORTS; ++c) { 1222 if(tmp16 == uip_listenports[c]) 1223 if(tmp16 == uip_listenports[c]) { 1223 1224 goto found_listen; 1225 } 1224 1226 } 1225 1227 1226 1228 /* No matching connection found, so we send a RST packet. */ 1227 1229 UIP_STAT(++uip_stat.tcp.synrst); 1230 1228 1231 reset: 1229 1230 1232 /* We do not send resets in response to resets. */ 1231 1233 if(BUF->flags & TCP_RST) { … … 1273 1275 1274 1276 /* Swap IP addresses. */ 1275 uip_ipaddr_copy( BUF->destipaddr,BUF->srcipaddr);1276 uip_ipaddr_copy( BUF->srcipaddr,uip_hostaddr);1277 uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr); 1278 uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr); 1277 1279 1278 1280 /* And send out the RST packet! */ … … 1283 1285 connection and send a SYNACK in return. */ 1284 1286 found_listen: 1285 /* First we check if there are any connections ava liable. Unused1287 /* First we check if there are any connections available. Unused 1286 1288 connections are kept in the same table as used connections, but 1287 1289 unused ones have the tcpstate set to CLOSED. Also, connections in … … 1320 1322 uip_connr->lport = BUF->destport; 1321 1323 uip_connr->rport = BUF->srcport; 1322 uip_ipaddr_copy( uip_connr->ripaddr,BUF->srcipaddr);1324 uip_ipaddr_copy(&uip_connr->ripaddr, &BUF->srcipaddr); 1323 1325 uip_connr->tcpstateflags = UIP_SYN_RCVD; 1324 1326 … … 1406 1408 goto drop; 1407 1409 } 1408 /* Calculate dthe length of the data, if the application has sent1410 /* Calculate the length of the data, if the application has sent 1409 1411 any data to us. */ 1410 1412 c = (BUF->tcpoffset >> 4) << 2; … … 1416 1418 /* First, check if the sequence number of the incoming packet is 1417 1419 what we're expecting next. If not, we send out an ACK with the 1418 correct numbers in. */ 1419 if(!(((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) && 1420 ((BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)))) { 1420 correct numbers in, unless we are in the SYN_RCVD state and 1421 receive a SYN, in which case we should retransmit our SYNACK 1422 (which is done futher down). */ 1423 if(!((((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) && 1424 ((BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK))) || 1425 (((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_RCVD) && 1426 ((BUF->flags & TCP_CTL) == TCP_SYN)))) { 1421 1427 if((uip_len > 0 || ((BUF->flags & (TCP_SYN | TCP_FIN)) != 0)) && 1422 1428 (BUF->seqno[0] != uip_connr->rcv_nxt[0] || … … 1444 1450 uip_connr->snd_nxt[2] = uip_acc32[2]; 1445 1451 uip_connr->snd_nxt[3] = uip_acc32[3]; 1446 1447 1452 1448 1453 /* Do RTT estimation, unless we have done retransmissions. */ … … 1495 1500 goto appsend; 1496 1501 } 1502 /* We need to retransmit the SYNACK */ 1503 if((BUF->flags & TCP_CTL) == TCP_SYN) { 1504 goto tcp_send_synack; 1505 } 1497 1506 goto drop; 1498 1507 #if UIP_ACTIVE_OPEN … … 1798 1807 goto drop; 1799 1808 1800 1801 1809 /* We jump here when we are ready to send the packet, and just want 1802 1810 to set the appropriate TCP sequence numbers in the TCP header. */ 1803 1811 tcp_send_ack: 1804 1812 BUF->flags = TCP_ACK; 1813 1805 1814 tcp_send_nodata: 1806 1815 uip_len = UIP_IPTCPH_LEN; 1816 1807 1817 tcp_send_noopts: 1808 1818 BUF->tcpoffset = (UIP_TCPH_LEN / 4) << 4; 1809 tcp_send: 1819 1810 1820 /* We're done with the input processing. We are now ready to send a 1811 1821 reply. Our job is to fill in all the fields of the TCP and IP 1812 1822 headers before calculating the checksum and finally send the 1813 1823 packet. */ 1824 tcp_send: 1814 1825 BUF->ackno[0] = uip_connr->rcv_nxt[0]; 1815 1826 BUF->ackno[1] = uip_connr->rcv_nxt[1]; … … 1827 1838 BUF->destport = uip_connr->rport; 1828 1839 1829 uip_ipaddr_copy( BUF->srcipaddr,uip_hostaddr);1830 uip_ipaddr_copy( BUF->destipaddr,uip_connr->ripaddr);1840 uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr); 1841 uip_ipaddr_copy(&BUF->destipaddr, &uip_connr->ripaddr); 1831 1842 1832 1843 if(uip_connr->tcpstateflags & UIP_STOPPED) { … … 1858 1869 1859 1870 ip_send_nolen: 1860 1861 1871 #if UIP_CONF_IPV6 1862 1872 BUF->vtc = 0x60; … … 1875 1885 DEBUG_PRINTF("uip ip_send_nolen: chkecum 0x%04x\n", uip_ipchksum()); 1876 1886 #endif /* UIP_CONF_IPV6 */ 1877 1878 1887 UIP_STAT(++uip_stat.tcp.sent); 1888 #if UIP_CONF_IPV6 1879 1889 send: 1890 #endif /* UIP_CONF_IPV6 */ 1880 1891 DEBUG_PRINTF("Sending packet with length %d (%d)\n", uip_len, 1881 1892 (BUF->len[0] << 8) | BUF->len[1]); … … 1885 1896 uip_flags = 0; 1886 1897 return; 1898 1887 1899 drop: 1888 1900 uip_len = 0; … … 1892 1904 /*---------------------------------------------------------------------------*/ 1893 1905 uint16_t 1894 htons(uint16_t val)1906 uip_htons(uint16_t val) 1895 1907 { 1896 return HTONS(val);1908 return UIP_HTONS(val); 1897 1909 } 1898 1910 /*---------------------------------------------------------------------------*/
Note:
See TracChangeset
for help on using the changeset viewer.