1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
|
/**
* @file
*
* Dynamic Host Configuration Protocol client
*/
/* This is part of LWIPv6
* Developed for the Ale4NET project
* Application Level Environment for Networking
*
* Copyright 2004 Diego Billi - Italy
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
*
* Copyright (c) 2001-2004 Leon Woestenberg <leon.woestenberg@gmx.net>
* Copyright (c) 2001-2004 Axon Digital Design B.V., The Netherlands.
* 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.
*
* This file is a contribution to the lwIP TCP/IP stack.
* The Swedish Institute of Computer Science and Adam Dunkels
* are specifically granted permission to redistribute this
* source code.
*
* Author: Leon Woestenberg <leon.woestenberg@gmx.net>
*
* This is a DHCP client for the lwIP TCP/IP stack. It aims to conform
* with RFC 2131 and RFC 2132.
*
* TODO:
* - Proper parsing of DHCP messages exploiting file/sname field overloading.
* - Add JavaDoc style documentation (API, internals).
* - Support for interfaces other than Ethernet (SLIP, PPP, ...)
*
* Please coordinate changes and requests with Leon Woestenberg
* <leon.woestenberg@gmx.net>
*
* Integration with your code:
*
* In lwip/dhcp.h
* #define DHCP_COARSE_TIMER_SECS (recommended 60 which is a minute)
* #define DHCP_FINE_TIMER_MSECS (recommended 500 which equals TCP coarse timer)
*
* Then have your application call dhcp_coarse_tmr() and
* dhcp_fine_tmr() on the defined intervals.
*
* dhcp_start(struct netif *netif);
* starts a DHCP client instance which configures the interface by
* obtaining an IP address lease and maintaining it.
*
* Use dhcp_release(netif) to end the lease and use dhcp_stop(netif)
* to remove the DHCP client.
*
*/
#include "lwip/opt.h"
#if LWIP_DHCP
#include "lwip/sys.h"
#include "lwip/stats.h"
#include "lwip/mem.h"
#include "lwip/netif.h"
#include "lwip/udp.h"
#include "lwip/ip_addr.h"
#include "netif/etharp.h"
#include "lwip/sockets.h"
#include "lwip/inet.h"
#include "lwip/dhcp.h"
/*---------------------------------------------------------------------------*/
//#ifndef DHCP_DEBUG
//#define DHCP_DEBUG DBG_ON
//#endif
/*---------------------------------------------------------------------------*/
#define U16_F "hu"
#define S16_F "hd"
#define X16_F "hx"
#define U32_F "lu"
#define S32_F "ld"
#define X32_F "lx"
/*---------------------------------------------------------------------------*/
err_t
udp_sendto_netif(struct udp_pcb *pcb, struct pbuf *p,
struct ip_addr *dst_ip, u16_t dst_port, struct netif *netif);
err_t
udp_send_netif(struct udp_pcb *pcb, struct pbuf *p, struct netif *netif);
/*---------------------------------------------------------------------------*/
/** global transaction identifier, must be
* unique for each DHCP request. We simply increment, starting
* with this value (easy to match with a packet analyzer) */
static u32_t xid = 0xABCD0000;
/** DHCP client state machine functions */
static void dhcp_handle_ack(struct netif *netif);
static void dhcp_handle_nak(struct netif *netif);
static void dhcp_handle_offer(struct netif *netif);
static err_t dhcp_discover(struct netif *netif);
static err_t dhcp_select(struct netif *netif);
static void dhcp_check(struct netif *netif);
static void dhcp_bind(struct netif *netif);
static err_t dhcp_decline(struct netif *netif);
static err_t dhcp_rebind(struct netif *netif);
static void dhcp_set_state(struct dhcp *dhcp, u8_t new_state);
/** receive, unfold, parse and free incoming messages */
static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip4_addr *addr, u16_t port);
static void dhcp_recv_wrapper(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port);
static err_t dhcp_unfold_reply(struct dhcp *dhcp);
static u8_t *dhcp_get_option_ptr(struct dhcp *dhcp, u8_t option_type);
static u8_t dhcp_get_option_byte(u8_t *ptr);
#if 0
static u16_t dhcp_get_option_short(u8_t *ptr);
#endif
static u32_t dhcp_get_option_long(u8_t *ptr);
static void dhcp_free_reply(struct dhcp *dhcp);
/** set the DHCP timers */
static void dhcp_timeout(struct netif *netif);
static void dhcp_t1_timeout(struct netif *netif);
static void dhcp_t2_timeout(struct netif *netif);
/** build outgoing messages */
/** create a DHCP request, fill in common headers */
static err_t dhcp_create_request(struct netif *netif);
/** free a DHCP request */
static void dhcp_delete_request(struct netif *netif);
/** add a DHCP option (type, then length in bytes) */
static void dhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len);
/** add option values */
static void dhcp_option_byte(struct dhcp *dhcp, u8_t value);
static void dhcp_option_short(struct dhcp *dhcp, u16_t value);
static void dhcp_option_long(struct dhcp *dhcp, u32_t value);
/** always add the DHCP options trailer to end and pad */
static void dhcp_option_trailer(struct dhcp *dhcp);
/**
* Back-off the DHCP client (because of a received NAK response).
*
* Back-off the DHCP client because of a received NAK. Receiving a
* NAK means the client asked for something non-sensible, for
* example when it tries to renew a lease obtained on another network.
*
* We back-off and will end up restarting a fresh DHCP negotiation later.
*
* @param state pointer to DHCP state structure
*/
static void dhcp_handle_nak(struct netif *netif) {
struct dhcp *dhcp = netif->dhcp;
u16_t msecs = 10 * 1000;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_handle_nak(netif=%p) %c%c%"U16_F"\n",
(void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_handle_nak(): set request timeout %"U16_F" msecs\n", msecs));
dhcp_set_state(dhcp, DHCP_BACKING_OFF);
}
/**
* Checks if the offered IP address is already in use.
*
* It does so by sending an ARP request for the offered address and
* entering CHECKING state. If no ARP reply is received within a small
* interval, the address is assumed to be free for use by us.
*/
static void dhcp_check(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
err_t result;
u16_t msecs;
/* Added by Diego Billi */
struct ip_addr_list *al;
struct ip_addr offered_addr;
IP64_CONV (&offered_addr, &dhcp->offered_ip_addr );
if ((al=ip_addr_list_maskfind(netif->addrs, &offered_addr)) == NULL)
// FIX: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
return;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_check(netif=%p) %c%c\n", (void *)netif, (s16_t)netif->name[0],
(s16_t)netif->name[1]));
/* create an ARP query for the offered IP address, expecting that no host
responds, as the IP address should not be in use. */
///CHANGED result = etharp_query(netif, &dhcp->offered_ip_addr, NULL);
result = etharp_query(al, &offered_addr, NULL);
if (result != ERR_OK) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_check: could not perform ARP query\n"));
}
dhcp->tries++;
msecs = 500;
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_check(): set request timeout %"U16_F" msecs\n", msecs));
dhcp_set_state(dhcp, DHCP_CHECKING);
}
/**
* Remember the configuration offered by a DHCP server.
*
* @param state pointer to DHCP state structure
*/
static void dhcp_handle_offer(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
/* obtain the server address */
u8_t *option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_SERVER_ID);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_handle_offer(netif=%p) %c%c%d\n",
(void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
if (option_ptr != NULL)
{
dhcp->server_ip_addr.addr = htonl(dhcp_get_option_long(&option_ptr[2]));
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("dhcp_handle_offer(): server 0x%08"X32_F"\n", dhcp->server_ip_addr.addr));
/* remember offered address */
///CHANGED ip_addr_set(&dhcp->offered_ip_addr, (struct ip4_addr *)&dhcp->msg_in->yiaddr);
ip4_addr_set(&dhcp->offered_ip_addr, (struct ip4_addr *)&dhcp->msg_in->yiaddr);
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("dhcp_handle_offer(): offer for 0x%08"X32_F"\n", dhcp->offered_ip_addr.addr));
dhcp_select(netif);
}
}
/**
* Select a DHCP server offer out of all offers.
*
* Simply select the first offer received.
*
* @param netif the netif under DHCP control
* @return lwIP specific error (see error.h)
*/
static err_t dhcp_select(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
err_t result;
u32_t msecs;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_select(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
/* create and initialize the DHCP message header */
result = dhcp_create_request(netif);
if (result == ERR_OK)
{
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
dhcp_option_byte(dhcp, DHCP_REQUEST);
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
dhcp_option_short(dhcp, 576);
/* MUST request the offered IP address */
dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 4/*num options*/);
dhcp_option_byte(dhcp, DHCP_OPTION_SUBNET_MASK);
dhcp_option_byte(dhcp, DHCP_OPTION_ROUTER);
dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);
dhcp_option_byte(dhcp, DHCP_OPTION_DNS_SERVER);
dhcp_option_trailer(dhcp);
/* shrink the pbuf to the actual content length */
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
/* TODO: we really should bind to a specific local interface here
but we cannot specify an unconfigured netif as it is addressless */
udp_bind(dhcp->pcb, IP4_ADDR_ANY, DHCP_CLIENT_PORT, NULL);
/* send broadcast to any DHCP server */
{
///udp_connect(dhcp->pcb, IP4_ADDR_BROADCAST, DHCP_SERVER_PORT);
struct ip_addr broadcast;
IP64_CONV(&broadcast, IP4_ADDR_BROADCAST);
udp_connect(dhcp->pcb, &broadcast, DHCP_SERVER_PORT);
}
udp_send_netif(dhcp->pcb, dhcp->p_out, netif);
/* reconnect to any (or to server here?!) */
udp_connect(dhcp->pcb, IP4_ADDR_ANY, DHCP_SERVER_PORT);
dhcp_delete_request(netif);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_select: REQUESTING\n"));
dhcp_set_state(dhcp, DHCP_REQUESTING);
} else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_select: could not allocate DHCP request\n"));
}
dhcp->tries++;
msecs = dhcp->tries < 4 ? dhcp->tries * 1000 : 4 * 1000;
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("dhcp_select(): set request timeout %"U32_F" msecs\n", msecs));
return result;
}
/**
* The DHCP timer that checks for lease renewal/rebind timeouts.
*
*/
void dhcp_coarse_tmr(void *arg)
{
struct stack *stack = (struct stack *) arg;
struct netif *netif = stack->netif_list;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_coarse_tmr()\n"));
//printf("dhcp coarse\n");
/* iterate through all network interfaces */
while (netif != NULL) {
/* only act on DHCP configured interfaces */
if (netif->dhcp != NULL) {
/* timer is active (non zero), and triggers (zeroes) now? */
if (netif->dhcp->t2_timeout-- == 1) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_coarse_tmr(): t2 timeout\n"));
/* this clients' rebind timeout triggered */
dhcp_t2_timeout(netif);
/* timer is active (non zero), and triggers (zeroes) now */
}
else if (netif->dhcp->t1_timeout-- == 1) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_coarse_tmr(): t1 timeout\n"));
/* this clients' renewal timeout triggered */
dhcp_t1_timeout(netif);
}
}
/* proceed to next netif */
netif = netif->next;
}
sys_timeout(DHCP_COARSE_TIMER_SECS * 1000, dhcp_coarse_tmr , stack);
}
/**
* DHCP transaction timeout handling
*
* A DHCP server is expected to respond within a short period of time.
* This timer checks whether an outstanding DHCP request is timed out.
*
*/
void dhcp_fine_tmr(void *arg)
{
struct stack *stack = (struct stack *)arg;
struct netif *netif = stack->netif_list;
//printf("dhcp fine\n");
/* loop through netif's */
while (netif != NULL) {
/* only act on DHCP configured interfaces */
if (netif->dhcp != NULL) {
/* timer is active (non zero), and is about to trigger now */
if (netif->dhcp->request_timeout-- == 1) {
/* { netif->dhcp->request_timeout == 0 } */
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_fine_tmr(): request timeout\n"));
/* this clients' request timeout triggered */
dhcp_timeout(netif);
}
}
/* proceed to next network interface */
netif = netif->next;
}
sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_fine_tmr , stack);
}
#if 0
void dhcp_init(struct stack *stack)
{
sys_timeout(DHCP_FINE_TIMER_MSECS , dhcp_fine_tmr , (void*)stack);
sys_timeout(DHCP_COARSE_TIMER_SECS * 1000, dhcp_coarse_tmr , (void*)stack);
}
#endif
/* start timers if there are no interfaces requiring DHCP,
call this just before assigning netif->dhcp */
static void dhcp_starttimers(struct stack *stack)
{
struct netif *netif;
for(netif = stack->netif_list; netif != NULL; netif = netif->next)
if (netif->dhcp != NULL) break;
if (netif==NULL) {
sys_timeout(DHCP_FINE_TIMER_MSECS , dhcp_fine_tmr , (void*)stack);
sys_timeout(DHCP_COARSE_TIMER_SECS * 1000, dhcp_coarse_tmr , (void*)stack);
}
}
/* stop timers if there are no interfaces requiring DHCP,
call this just after assigning netif->dhcp=NULL */
static void dhcp_stoptimers(struct stack *stack)
{
struct netif *netif;
for(netif = stack->netif_list; netif != NULL; netif = netif->next)
if (netif->dhcp != NULL) break;
if (netif==NULL) {
sys_untimeout(dhcp_fine_tmr , (void*)stack);
sys_untimeout(dhcp_coarse_tmr , (void*)stack);
}
}
/**
* A DHCP negotiation transaction, or ARP request, has timed out.
*
* The timer that was started with the DHCP or ARP request has
* timed out, indicating no response was received in time.
*
* @param netif the netif under DHCP control
*
*/
static void dhcp_timeout(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_timeout()\n"));
/* back-off period has passed, or server selection timed out */
if ((dhcp->state == DHCP_BACKING_OFF) || (dhcp->state == DHCP_SELECTING)) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_timeout(): restarting discovery\n"));
dhcp_discover(netif);
/* receiving the requested lease timed out */
}
else if (dhcp->state == DHCP_REQUESTING) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_timeout(): REQUESTING, DHCP request timed out\n"));
if (dhcp->tries <= 5) {
dhcp_select(netif);
}
else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_timeout(): REQUESTING, releasing, restarting\n"));
dhcp_release(netif);
dhcp_discover(netif);
}
/* received no ARP reply for the offered address (which is good) */
}
else if (dhcp->state == DHCP_CHECKING) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_timeout(): CHECKING, ARP request timed out\n"));
if (dhcp->tries <= 1) {
dhcp_check(netif);
/* no ARP replies on the offered address,
looks like the IP address is indeed free */
}
else {
/* bind the interface to the offered address */
dhcp_bind(netif);
}
}
/* did not get response to renew request? */
else if (dhcp->state == DHCP_RENEWING) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_timeout(): RENEWING, DHCP request timed out\n"));
/* just retry renewal */
/* note that the rebind timer will eventually time-out if renew does not work */
dhcp_renew(netif);
/* did not get response to rebind request? */
}
else if (dhcp->state == DHCP_REBINDING) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_timeout(): REBINDING, DHCP request timed out\n"));
if (dhcp->tries <= 8) {
dhcp_rebind(netif);
}
else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_timeout(): RELEASING, DISCOVERING\n"));
dhcp_release(netif);
dhcp_discover(netif);
}
}
}
/**
* The renewal period has timed out.
*
* @param netif the netif under DHCP control
*/
static void dhcp_t1_timeout(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("dhcp_t1_timeout()\n"));
if ((dhcp->state == DHCP_REQUESTING) || (dhcp->state == DHCP_BOUND) || (dhcp->state == DHCP_RENEWING)) {
/* just retry to renew - note that the rebind timer (t2) will
* eventually time-out if renew tries fail. */
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_t1_timeout(): must renew\n"));
dhcp_renew(netif);
}
}
/**
* The rebind period has timed out.
*
*/
static void dhcp_t2_timeout(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_t2_timeout()\n"));
if ((dhcp->state == DHCP_REQUESTING) || (dhcp->state == DHCP_BOUND) || (dhcp->state == DHCP_RENEWING)) {
/* just retry to rebind */
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_t2_timeout(): must rebind\n"));
dhcp_rebind(netif);
}
}
/**
*
* @param netif the netif under DHCP control
*/
static void dhcp_handle_ack(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
u8_t *option_ptr;
/* clear options we might not get from the ACK */
dhcp->offered_sn_mask.addr = 0;
dhcp->offered_gw_addr.addr = 0;
dhcp->offered_bc_addr.addr = 0;
/* lease time given? */
option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_LEASE_TIME);
if (option_ptr != NULL) {
/* remember offered lease time */
dhcp->offered_t0_lease = dhcp_get_option_long(option_ptr + 2);
}
/* renewal period given? */
option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_T1);
if (option_ptr != NULL) {
/* remember given renewal period */
dhcp->offered_t1_renew = dhcp_get_option_long(option_ptr + 2);
}
else {
/* calculate safe periods for renewal */
dhcp->offered_t1_renew = dhcp->offered_t0_lease / 2;
}
/* renewal period given? */
option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_T2);
if (option_ptr != NULL) {
/* remember given rebind period */
dhcp->offered_t2_rebind = dhcp_get_option_long(option_ptr + 2);
}
else {
/* calculate safe periods for rebinding */
dhcp->offered_t2_rebind = dhcp->offered_t0_lease;
}
/* (y)our internet address */
///CHANGED ip_addr_set(&dhcp->offered_ip_addr, &dhcp->msg_in->yiaddr);
ip4_addr_set(&dhcp->offered_ip_addr, &dhcp->msg_in->yiaddr);
/**
* Patch #1308
* TODO: we must check if the file field is not overloaded by DHCP options!
*/
#if 0
/* boot server address */
ip_addr_set(&dhcp->offered_si_addr, &dhcp->msg_in->siaddr);
/* boot file name */
if (dhcp->msg_in->file[0]) {
dhcp->boot_file_name = mem_malloc(strlen(dhcp->msg_in->file) + 1);
strcpy(dhcp->boot_file_name, dhcp->msg_in->file);
}
#endif
/* subnet mask */
option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_SUBNET_MASK);
/* subnet mask given? */
if (option_ptr != NULL) {
dhcp->offered_sn_mask.addr = htonl(dhcp_get_option_long(&option_ptr[2]));
}
/* gateway router */
option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_ROUTER);
if (option_ptr != NULL) {
dhcp->offered_gw_addr.addr = htonl(dhcp_get_option_long(&option_ptr[2]));
}
/* broadcast address */
option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_BROADCAST);
if (option_ptr != NULL) {
dhcp->offered_bc_addr.addr = htonl(dhcp_get_option_long(&option_ptr[2]));
}
/* DNS servers */
option_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_DNS_SERVER);
if (option_ptr != NULL) {
u8_t n;
dhcp->dns_count = dhcp_get_option_byte(&option_ptr[1]) / (u32_t)sizeof(struct ip4_addr);
/* limit to at most DHCP_MAX_DNS DNS servers */
if (dhcp->dns_count > DHCP_MAX_DNS)
dhcp->dns_count = DHCP_MAX_DNS;
for (n = 0; n < dhcp->dns_count; n++) {
dhcp->offered_dns_addr[n].addr = htonl(dhcp_get_option_long(&option_ptr[2 + n * 4]));
}
}
}
/**
* Start DHCP negotiation for a network interface.
*
* If no DHCP client instance was attached to this interface,
* a new client is created first. If a DHCP client instance
* was already present, it restarts negotiation.
*
* @param netif The lwIP network interface
* @return lwIP error code
* - ERR_OK - No error
* - ERR_MEM - Out of memory
*
*/
err_t dhcp_start(struct netif *netif)
{
struct stack *stack = netif->stack;
struct dhcp *dhcp = netif->dhcp;
err_t result = ERR_OK;
LWIP_ASSERT("netif != NULL", netif != NULL);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_start(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
netif->flags &= ~NETIF_FLAG_DHCP;
/* no DHCP client attached yet? */
if (dhcp == NULL) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_start(): starting new DHCP client\n"));
dhcp = mem_malloc(sizeof(struct dhcp));
if (dhcp == NULL) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_start(): could not allocate dhcp\n"));
return ERR_MEM;
}
dhcp_starttimers(stack);
/* store this dhcp client in the netif */
netif->dhcp = dhcp;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_start(): allocated dhcp"));
}
/* already has DHCP client attached */
else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE | 3, ("dhcp_start(): restarting DHCP configuration\n"));
}
/// /* FIX FIX FIX: I must give an address to the interface otherwise it does not work */
/// {
/// struct ip_addr ip,netmask;
/// IP64_ADDR(&ip, 0,0,0,0);
/// IP64_MASKADDR(&netmask, 0,0,0,0);
/// netif_add_addr(netif, &ip, &netmask);
/// //ip_route_list_add(&ip, &netmask, &netmask, netif, 0);
/// }
/* clear data structure */
memset(dhcp, 0, sizeof(struct dhcp));
/* allocate UDP PCB */
dhcp->pcb = udp_new(stack);
if (dhcp->pcb == NULL) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_start(): could not obtain pcb\n"));
mem_free((void *)dhcp);
netif->dhcp = dhcp = NULL;
dhcp_stoptimers(stack);
return ERR_MEM;
}
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_start(): starting DHCP configuration\n"));
/* (re)start the DHCP negotiation */
result = dhcp_discover(netif);
if (result != ERR_OK) {
/* free resources allocated above */
dhcp_stop(netif);
return ERR_MEM;
}
netif->flags |= NETIF_FLAG_DHCP;
return result;
}
/**
* Inform a DHCP server of our manual configuration.
*
* This informs DHCP servers of our fixed IP address configuration
* by sending an INFORM message. It does not involve DHCP address
* configuration, it is just here to be nice to the network.
*
* @param netif The lwIP network interface
*
*/
void dhcp_inform(struct netif *netif)
{
struct stack *stack = netif->stack;
struct dhcp *dhcp;
err_t result = ERR_OK;
dhcp = mem_malloc(sizeof(struct dhcp));
if (dhcp == NULL) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_inform(): could not allocate dhcp\n"));
return;
}
dhcp_starttimers(stack);
netif->dhcp = dhcp;
memset(dhcp, 0, sizeof(struct dhcp));
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_inform(): allocated dhcp\n"));
dhcp->pcb = udp_new(stack);
if (dhcp->pcb == NULL) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_inform(): could not obtain pcb"));
netif->dhcp=NULL;
mem_free((void *)dhcp);
dhcp_stoptimers(stack);
return;
}
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_inform(): created new udp pcb\n"));
/* create and initialize the DHCP message header */
result = dhcp_create_request(netif);
if (result == ERR_OK) {
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
dhcp_option_byte(dhcp, DHCP_INFORM);
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
/* TODO: use netif->mtu ?! */
dhcp_option_short(dhcp, 576);
dhcp_option_trailer(dhcp);
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
udp_bind(dhcp->pcb, IP4_ADDR_ANY, DHCP_CLIENT_PORT, NULL);
{
///udp_connect(dhcp->pcb, IP4_ADDR_BROADCAST, DHCP_SERVER_PORT);
struct ip_addr broadcast;
IP64_CONV(&broadcast, IP4_ADDR_BROADCAST);
udp_connect(dhcp->pcb, &broadcast, DHCP_SERVER_PORT);
}
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_inform: INFORMING\n"));
udp_send_netif(dhcp->pcb, dhcp->p_out, netif);
udp_connect(dhcp->pcb, IP4_ADDR_ANY, DHCP_SERVER_PORT);
dhcp_delete_request(netif);
}
else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_inform: could not allocate DHCP request\n"));
}
if (dhcp != NULL) {
if (dhcp->pcb != NULL) udp_remove(dhcp->pcb);
dhcp->pcb = NULL;
netif->dhcp = NULL;
mem_free((void *)dhcp);
dhcp_stoptimers(stack);
}
}
#if DHCP_DOES_ARP_CHECK
/**
* Match an ARP reply with the offered IP address.
*
* @param addr The IP address we received a reply from
*
*/
void dhcp_arp_reply(struct netif *netif, struct ip4_addr *addr)
{
LWIP_ASSERT("netif != NULL", netif != NULL);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_arp_reply()\n"));
/* is a DHCP client doing an ARP check? */
if ((netif->dhcp != NULL) && (netif->dhcp->state == DHCP_CHECKING)) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_arp_reply(): CHECKING, arp reply for 0x%08"X32_F"\n", addr->addr));
/* did a host respond with the address we
were offered by the DHCP server? */
///CHANGED if (ip_addr_cmp(addr, &netif->dhcp->offered_ip_addr)) {
if (ip4_addr_cmp(addr, &netif->dhcp->offered_ip_addr)) {
/* we will not accept the offered address */
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE | 1, ("dhcp_arp_reply(): arp reply matched with offered address, declining\n"));
dhcp_decline(netif);
}
}
}
/**
* Decline an offered lease.
*
* Tell the DHCP server we do not accept the offered address.
* One reason to decline the lease is when we find out the address
* is already in use by another host (through ARP).
*/
static err_t dhcp_decline(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
err_t result = ERR_OK;
u16_t msecs;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_decline()\n"));
dhcp_set_state(dhcp, DHCP_BACKING_OFF);
/* create and initialize the DHCP message header */
result = dhcp_create_request(netif);
if (result == ERR_OK)
{
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
dhcp_option_byte(dhcp, DHCP_DECLINE);
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
dhcp_option_short(dhcp, 576);
dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
dhcp_option_trailer(dhcp);
/* resize pbuf to reflect true size of options */
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
udp_bind(dhcp->pcb, IP4_ADDR_ANY, DHCP_CLIENT_PORT, NULL);
/* @todo: should we really connect here? we are performing sendto() */
udp_connect(dhcp->pcb, IP4_ADDR_ANY, DHCP_SERVER_PORT);
/* per section 4.4.4, broadcast DECLINE messages */
{
///udp_sendto(dhcp->pcb, dhcp->p_out, IP4_ADDR_BROADCAST, DHCP_SERVER_PORT);
struct ip_addr broadcast;
IP64_CONV(&broadcast, IP4_ADDR_BROADCAST);
udp_sendto_netif(dhcp->pcb, dhcp->p_out, &broadcast, DHCP_SERVER_PORT, netif);
}
dhcp_delete_request(netif);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_decline: BACKING OFF\n"));
}
else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_decline: could not allocate DHCP request\n"));
}
dhcp->tries++;
msecs = 10*1000;
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_decline(): set request timeout %"U16_F" msecs\n", msecs));
return result;
}
#endif
/**
* Start the DHCP process, discover a DHCP server.
*
*/
static err_t dhcp_discover(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
err_t result = ERR_OK;
u16_t msecs;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_discover()\n"));
///CHANGED ip_addr_set(&dhcp->offered_ip_addr, IP4_ADDR_ANY);
ip64_addr_set(&dhcp->offered_ip_addr, IP4_ADDR_ANY);
/* create and initialize the DHCP message header */
result = dhcp_create_request(netif);
if (result == ERR_OK) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_discover: making request\n"));
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
dhcp_option_byte(dhcp, DHCP_DISCOVER);
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
dhcp_option_short(dhcp, 576);
dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 4/*num options*/);
dhcp_option_byte(dhcp, DHCP_OPTION_SUBNET_MASK);
dhcp_option_byte(dhcp, DHCP_OPTION_ROUTER);
dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);
dhcp_option_byte(dhcp, DHCP_OPTION_DNS_SERVER);
dhcp_option_trailer(dhcp);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_discover: realloc()ing\n"));
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
/* set receive callback function with netif as user data */
///CHANGED udp_recv(dhcp->pcb, dhcp_recv, netif);
udp_recv(dhcp->pcb, dhcp_recv_wrapper, netif);
udp_bind(dhcp->pcb, IP4_ADDR_ANY, DHCP_CLIENT_PORT, NULL);
udp_connect(dhcp->pcb, IP4_ADDR_ANY, DHCP_SERVER_PORT);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_discover: sendto(DISCOVER, IP4_ADDR_BROADCAST, DHCP_SERVER_PORT)\n"));
{
///udp_sendto(dhcp->pcb, dhcp->p_out, IP4_ADDR_BROADCAST, DHCP_SERVER_PORT);
struct ip_addr broadcast;
IP64_CONV(&broadcast, IP4_ADDR_BROADCAST);
udp_sendto_netif(dhcp->pcb, dhcp->p_out, &broadcast, DHCP_SERVER_PORT, netif);
}
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_discover: deleting()ing\n"));
dhcp_delete_request(netif);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_discover: SELECTING\n"));
dhcp_set_state(dhcp, DHCP_SELECTING);
}
else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_discover: could not allocate DHCP request\n"));
}
dhcp->tries++;
msecs = dhcp->tries < 4 ? (dhcp->tries + 1) * 1000 : 10 * 1000;
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_discover(): set request timeout %"U16_F" msecs\n", msecs));
return result;
}
/**
* Bind the interface to the offered IP address.
*
* @param netif network interface to bind to the offered address
*/
static void dhcp_bind(struct netif *netif)
{
struct stack *stack = netif->stack;
struct dhcp *dhcp = netif->dhcp;
//struct ip4_addr sn_mask, gw_addr;
struct ip_addr ip;
struct ip_addr sn_mask, gw_addr;
LWIP_ASSERT("dhcp_bind: netif != NULL", netif != NULL);
LWIP_ASSERT("dhcp_bind: dhcp != NULL", dhcp != NULL);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_bind(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
/* temporary DHCP lease? */
if (dhcp->offered_t1_renew != 0xffffffffUL) {
/* set renewal period timer */
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_bind(): t1 renewal timer %"U32_F" secs\n", dhcp->offered_t1_renew));
dhcp->t1_timeout = (dhcp->offered_t1_renew + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
if (dhcp->t1_timeout == 0) dhcp->t1_timeout = 1;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t1_renew*1000));
}
/* set renewal period timer */
if (dhcp->offered_t2_rebind != 0xffffffffUL) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_bind(): t2 rebind timer %"U32_F" secs\n", dhcp->offered_t2_rebind));
dhcp->t2_timeout = (dhcp->offered_t2_rebind + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
if (dhcp->t2_timeout == 0) dhcp->t2_timeout = 1;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t2_rebind*1000));
}
/* copy offered network mask */
///CHANGED ip_addr_set(&sn_mask, &dhcp->offered_sn_mask);
IP64_MASKCONV(&sn_mask, &dhcp->offered_sn_mask);
/* subnet mask not given? */
/* TODO: this is not a valid check. what if the network mask is 0? */
if (sn_mask.addr[3] == 0) {
/* choose a safe subnet mask given the network class */
u8_t first_octet = ip4_addr1(&sn_mask);
if (first_octet <= 127) sn_mask.addr[3] = htonl(0xff000000);
else if (first_octet >= 192) sn_mask.addr[3] = htonl(0xffffff00);
else sn_mask.addr[3] = htonl(0xffff0000);
}
/// CHANGED ip_addr_set(&gw_addr, &dhcp->offered_gw_addr);
IP64_CONV(&gw_addr, &dhcp->offered_gw_addr);
/* gateway address not given? */
if (gw_addr.addr[3] == 0) {
/* copy network address */
gw_addr.addr[3] = (dhcp->offered_ip_addr.addr & sn_mask.addr[3]);
/* use first host address on network as gateway */
gw_addr.addr[3] |= htonl(0x00000001);
}
///LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("dhcp_bind(): IP: 0x%08"X32_F"\n", dhcp->offered_ip_addr.addr));
///netif_set_ipaddr(netif, &dhcp->offered_ip_addr);
///LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("dhcp_bind(): SN: 0x%08"X32_F"\n", sn_mask.addr));
///netif_set_netmask(netif, &sn_mask);
///LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("dhcp_bind(): GW: 0x%08"X32_F"\n", gw_addr.addr));
///netif_set_gw(netif, &gw_addr);
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("dhcp_bind()"));
IP64_CONV(&ip, (struct ip4_addr*)&dhcp->offered_ip_addr.addr);
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("\nIP: "));
ip_addr_debug_print(DHCP_DEBUG | DBG_STATE, &ip);
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("\nSN: "));
ip_addr_debug_print(DHCP_DEBUG | DBG_STATE, &sn_mask);
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("\nGW: "));
ip_addr_debug_print(DHCP_DEBUG | DBG_STATE, &gw_addr);
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("\n"));
/// /* FIX FIX FIX FIX: remove the fake address */
/// {
/// struct ip_addr ip2,netmask2;
/// IP64_ADDR(&ip2, 0,0,0,0);
/// IP64_MASKADDR(&netmask2, 0,0,0,0);
/// netif_del_addr(netif, &ip2, &netmask2);
/// //ip_route_list_add(&ip, &netmask, &netmask, netif, 0);
/// }
netif_add_addr(netif, &ip, &sn_mask);
IP64_ADDR(&ip, 0,0,0,0);
IP64_MASKADDR(&sn_mask, 0,0,0,0);
ip_route_list_add(stack, &ip, &sn_mask, &gw_addr, netif, 0);
/* bring the interface up */
//netif_set_up_low(netif);
/* netif is now bound to DHCP leased address */
dhcp_set_state(dhcp, DHCP_BOUND);
}
/**
* Renew an existing DHCP lease at the involved DHCP server.
*
* @param netif network interface which must renew its lease
*/
err_t dhcp_renew(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
err_t result;
u16_t msecs;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_renew()\n"));
dhcp_set_state(dhcp, DHCP_RENEWING);
/* create and initialize the DHCP message header */
result = dhcp_create_request(netif);
if (result == ERR_OK) {
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
dhcp_option_byte(dhcp, DHCP_REQUEST);
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
/* TODO: use netif->mtu in some way */
dhcp_option_short(dhcp, 576);
#if 0
dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
#endif
#if 0
dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
#endif
/* append DHCP message trailer */
dhcp_option_trailer(dhcp);
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
udp_bind(dhcp->pcb, IP4_ADDR_ANY, DHCP_CLIENT_PORT, NULL);
{ //
struct ip_addr serverip;
IP64_CONV(&serverip, &dhcp->server_ip_addr);
///CHANGED udp_connect(dhcp->pcb, &dhcp->server_ip_addr, DHCP_SERVER_PORT);
udp_connect(dhcp->pcb, &serverip, DHCP_SERVER_PORT);
}
udp_send_netif(dhcp->pcb, dhcp->p_out, netif);
dhcp_delete_request(netif);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_renew: RENEWING\n"));
}
else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_renew: could not allocate DHCP request\n"));
}
dhcp->tries++;
/* back-off on retries, but to a maximum of 20 seconds */
msecs = dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000;
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_renew(): set request timeout %"U16_F" msecs\n", msecs));
return result;
}
/**
* Rebind with a DHCP server for an existing DHCP lease.
*
* @param netif network interface which must rebind with a DHCP server
*/
static err_t dhcp_rebind(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
err_t result;
u16_t msecs;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_rebind()\n"));
dhcp_set_state(dhcp, DHCP_REBINDING);
/* create and initialize the DHCP message header */
result = dhcp_create_request(netif);
if (result == ERR_OK)
{
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
dhcp_option_byte(dhcp, DHCP_REQUEST);
dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
dhcp_option_short(dhcp, 576);
#if 0
dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
#endif
dhcp_option_trailer(dhcp);
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
/* set remote IP association to any DHCP server */
udp_bind(dhcp->pcb, IP4_ADDR_ANY, DHCP_CLIENT_PORT, NULL);
udp_connect(dhcp->pcb, IP4_ADDR_ANY, DHCP_SERVER_PORT);
/* broadcast to server */
{
///udp_sendto(dhcp->pcb, dhcp->p_out, IP4_ADDR_BROADCAST, DHCP_SERVER_PORT);
struct ip_addr broadcast;
IP64_CONV(&broadcast, IP4_ADDR_BROADCAST);
udp_sendto_netif(dhcp->pcb, dhcp->p_out, &broadcast, DHCP_SERVER_PORT, netif);
}
dhcp_delete_request(netif);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_rebind: REBINDING\n"));
}
else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_rebind: could not allocate DHCP request\n"));
}
dhcp->tries++;
msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_rebind(): set request timeout %"U16_F" msecs\n", msecs));
return result;
}
/**
* Release a DHCP lease.
*
* @param netif network interface which must release its lease
*/
err_t dhcp_release(struct netif *netif)
{
struct stack *stack = netif->stack;
struct dhcp *dhcp = netif->dhcp;
err_t result;
u16_t msecs;
if (dhcp == NULL) {
/* FIX: should not append */
return ERR_OK;
}
struct ip_addr ip, netmask, gw;
IP64_CONV( &ip, (struct ip4_addr *)&dhcp->offered_ip_addr.addr);
IP64_MASKCONV( &netmask, (struct ip4_addr *)&dhcp->offered_sn_mask.addr);
IP64_CONV( &gw, (struct ip4_addr *)&dhcp->offered_gw_addr.addr);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_release()\n"));
/* idle DHCP client */
dhcp_set_state(dhcp, DHCP_OFF);
/* clean old DHCP offer */
dhcp->server_ip_addr.addr = 0;
dhcp->offered_ip_addr.addr = dhcp->offered_sn_mask.addr = 0;
dhcp->offered_gw_addr.addr = dhcp->offered_bc_addr.addr = 0;
dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0;
dhcp->dns_count = 0;
/* create and initialize the DHCP message header */
result = dhcp_create_request(netif);
if (result == ERR_OK) {
dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
dhcp_option_byte(dhcp, DHCP_RELEASE);
dhcp_option_trailer(dhcp);
pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
udp_bind(dhcp->pcb, IP4_ADDR_ANY, DHCP_CLIENT_PORT, NULL);
{ //
struct ip_addr serverip;
IP64_CONV(&serverip, &dhcp->server_ip_addr);
///CHANGED udp_connect(dhcp->pcb, &dhcp->server_ip_addr, DHCP_SERVER_PORT);
udp_connect(dhcp->pcb, &serverip, DHCP_SERVER_PORT);
}
udp_send_netif(dhcp->pcb, dhcp->p_out, netif);
dhcp_delete_request(netif);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_release: RELEASED, DHCP_OFF\n"));
}
else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_release: could not allocate DHCP request\n"));
}
dhcp->tries++;
msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | DBG_STATE, ("dhcp_release(): set request timeout %"U16_F" msecs\n", msecs));
/* bring the interface down */
//netif_set_down(netif);
/* remove IP address from interface */
///netif_set_ipaddr(netif, IP_ADDR_ANY);
///netif_set_gw(netif, IP_ADDR_ANY);
///netif_set_netmask(netif, IP_ADDR_ANY);
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("dhcp_release()"));
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("\n\tIP: "));
ip_addr_debug_print(DHCP_DEBUG | DBG_STATE, &ip);
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("\n\tSN: "));
ip_addr_debug_print(DHCP_DEBUG | DBG_STATE, &netmask);
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("\n\tGW: "));
ip_addr_debug_print(DHCP_DEBUG | DBG_STATE, &gw);
LWIP_DEBUGF(DHCP_DEBUG | DBG_STATE, ("\n"));
{
struct ip_addr ip2, netmask2;
IP64_ADDR(&ip2, 0,0,0,0);
IP64_MASKADDR(&netmask2, 0,0,0,0);
ip_route_list_del(stack, &ip2, &netmask2, &gw, netif, 0);
}
netif_del_addr(netif, &ip, &netmask);
/* TODO: netif_down(netif); */
return result;
}
/**
* Remove the DHCP client from the interface.
*
* @param netif The network interface to stop DHCP on
*/
void dhcp_stop(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
LWIP_ASSERT("dhcp_stop: netif != NULL", netif != NULL);
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_stop()\n"));
/* netif is DHCP configured? */
if (dhcp != NULL) {
if (dhcp->pcb != NULL) {
udp_remove(dhcp->pcb);
dhcp->pcb = NULL;
}
if (dhcp->p != NULL) {
pbuf_free(dhcp->p);
dhcp->p = NULL;
}
/* free unfolded reply */
dhcp_free_reply(dhcp);
mem_free((void *)dhcp);
netif->flags &= ~NETIF_FLAG_DHCP;
netif->dhcp = NULL;
dhcp_stoptimers(netif->stack);
}
}
/*
* Set the DHCP state of a DHCP client.
*
* If the state changed, reset the number of tries.
*
* TODO: we might also want to reset the timeout here?
*/
static void dhcp_set_state(struct dhcp *dhcp, u8_t new_state)
{
if (new_state != dhcp->state)
{
dhcp->state = new_state;
dhcp->tries = 0;
}
}
/*
* Concatenate an option type and length field to the outgoing
* DHCP message.
*
*/
static void dhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len)
{
LWIP_ASSERT("dhcp_option_short: dhcp->options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN);
dhcp->msg_out->options[dhcp->options_out_len++] = option_type;
dhcp->msg_out->options[dhcp->options_out_len++] = option_len;
}
/*
* Concatenate a single byte to the outgoing DHCP message.
*
*/
static void dhcp_option_byte(struct dhcp *dhcp, u8_t value)
{
LWIP_ASSERT("dhcp_option_short: dhcp->options_out_len < DHCP_OPTIONS_LEN", dhcp->options_out_len < DHCP_OPTIONS_LEN);
dhcp->msg_out->options[dhcp->options_out_len++] = value;
}
static void dhcp_option_short(struct dhcp *dhcp, u16_t value)
{
LWIP_ASSERT("dhcp_option_short: dhcp->options_out_len + 2 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2 <= DHCP_OPTIONS_LEN);
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0xff00U) >> 8;
dhcp->msg_out->options[dhcp->options_out_len++] = value & 0x00ffU;
}
static void dhcp_option_long(struct dhcp *dhcp, u32_t value)
{
LWIP_ASSERT("dhcp_option_long: dhcp->options_out_len + 4 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 4 <= DHCP_OPTIONS_LEN);
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0xff000000UL) >> 24;
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0x00ff0000UL) >> 16;
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0x0000ff00UL) >> 8;
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0x000000ffUL);
}
/**
* Extract the DHCP message and the DHCP options.
*
* Extract the DHCP message and the DHCP options, each into a contiguous
* piece of memory. As a DHCP message is variable sized by its options,
* and also allows overriding some fields for options, the easy approach
* is to first unfold the options into a conitguous piece of memory, and
* use that further on.
*
*/
static err_t dhcp_unfold_reply(struct dhcp *dhcp)
{
struct pbuf *p = dhcp->p;
u8_t *ptr;
u16_t i;
u16_t j = 0;
LWIP_ASSERT("dhcp->p != NULL", dhcp->p != NULL);
/* free any left-overs from previous unfolds */
dhcp_free_reply(dhcp);
/* options present? */
if (dhcp->p->tot_len > (sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN)) {
dhcp->options_in_len = dhcp->p->tot_len - (sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN);
dhcp->options_in = mem_malloc(dhcp->options_in_len);
if (dhcp->options_in == NULL) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_unfold_reply(): could not allocate dhcp->options\n"));
return ERR_MEM;
}
}
dhcp->msg_in = mem_malloc(sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN);
if (dhcp->msg_in == NULL) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_unfold_reply(): could not allocate dhcp->msg_in\n"));
mem_free((void *)dhcp->options_in);
dhcp->options_in = NULL;
return ERR_MEM;
}
ptr = (u8_t *)dhcp->msg_in;
/* proceed through struct dhcp_msg */
for (i = 0; i < sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN; i++) {
*ptr++ = ((u8_t *)p->payload)[j++];
/* reached end of pbuf? */
if (j == p->len) {
/* proceed to next pbuf in chain */
p = p->next;
j = 0;
}
}
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_unfold_reply(): copied %"U16_F" bytes into dhcp->msg_in[]\n", i));
if (dhcp->options_in != NULL) {
ptr = (u8_t *)dhcp->options_in;
/* proceed through options */
for (i = 0; i < dhcp->options_in_len; i++) {
*ptr++ = ((u8_t *)p->payload)[j++];
/* reached end of pbuf? */
if (j == p->len) {
/* proceed to next pbuf in chain */
p = p->next;
j = 0;
}
}
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("dhcp_unfold_reply(): copied %"U16_F" bytes to dhcp->options_in[]\n", i));
}
return ERR_OK;
}
/**
* Free the incoming DHCP message including contiguous copy of
* its DHCP options.
*
*/
static void dhcp_free_reply(struct dhcp *dhcp)
{
if (dhcp->msg_in != NULL) {
mem_free((void *)dhcp->msg_in);
dhcp->msg_in = NULL;
}
if (dhcp->options_in) {
mem_free((void *)dhcp->options_in);
dhcp->options_in = NULL;
dhcp->options_in_len = 0;
}
LWIP_DEBUGF(DHCP_DEBUG, ("dhcp_free_reply(): free'd\n"));
}
/**
* If an incoming DHCP message is in response to us, then trigger the state machine
*/
static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip4_addr *addr, u16_t port)
{
struct netif *netif = (struct netif *)arg;
struct dhcp *dhcp = netif->dhcp;
struct dhcp_msg *reply_msg = (struct dhcp_msg *)p->payload;
u8_t *options_ptr;
u8_t msg_type;
u8_t i;
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 3, ("dhcp_recv(pbuf = %p) from DHCP server %"U16_F".%"U16_F".%"U16_F".%"U16_F" port %"U16_F"\n", (void*)p,
(u16_t)(ntohl(addr->addr) >> 24 & 0xff), (u16_t)(ntohl(addr->addr) >> 16 & 0xff),
(u16_t)(ntohl(addr->addr) >> 8 & 0xff), (u16_t)(ntohl(addr->addr) & 0xff), port));
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("pbuf->len = %"U16_F"\n", p->len));
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("pbuf->tot_len = %"U16_F"\n", p->tot_len));
/* prevent warnings about unused arguments */
(void)pcb; (void)addr; (void)port;
dhcp->p = p;
/* TODO: check packet length before reading them */
if (reply_msg->op != DHCP_BOOTREPLY) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 1, ("not a DHCP reply message, but type %"U16_F"\n", (u16_t)reply_msg->op));
pbuf_free(p);
dhcp->p = NULL;
return;
}
/* iterate through hardware address and match against DHCP message */
for (i = 0; i < netif->hwaddr_len; i++) {
if (netif->hwaddr[i] != reply_msg->chaddr[i]) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("netif->hwaddr[%"U16_F"]==%02"X16_F" != reply_msg->chaddr[%"U16_F"]==%02"X16_F"\n",
(u16_t)i, (u16_t)netif->hwaddr[i], (u16_t)i, (u16_t)reply_msg->chaddr[i]));
pbuf_free(p);
dhcp->p = NULL;
return;
}
}
/* match transaction ID against what we expected */
if (ntohl(reply_msg->xid) != dhcp->xid) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("transaction id mismatch\n"));
pbuf_free(p);
dhcp->p = NULL;
return;
}
/* option fields could be unfold? */
if (dhcp_unfold_reply(dhcp) != ERR_OK) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("problem unfolding DHCP message - too short on memory?\n"));
pbuf_free(p);
dhcp->p = NULL;
return;
}
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("searching DHCP_OPTION_MESSAGE_TYPE\n"));
/* obtain pointer to DHCP message type */
options_ptr = dhcp_get_option_ptr(dhcp, DHCP_OPTION_MESSAGE_TYPE);
if (options_ptr == NULL) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 1, ("DHCP_OPTION_MESSAGE_TYPE option not found\n"));
pbuf_free(p);
dhcp->p = NULL;
return;
}
/* read DHCP message type */
msg_type = dhcp_get_option_byte(options_ptr + 2);
/* message type is DHCP ACK? */
if (msg_type == DHCP_ACK) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 1, ("DHCP_ACK received\n"));
/* in requesting state? */
if (dhcp->state == DHCP_REQUESTING) {
dhcp_handle_ack(netif);
dhcp->request_timeout = 0;
#if DHCP_DOES_ARP_CHECK
/* check if the acknowledged lease address is already in use */
dhcp_check(netif);
#else
/* bind interface to the acknowledged lease address */
dhcp_bind(netif);
#endif
}
/* already bound to the given lease address? */
else if ((dhcp->state == DHCP_REBOOTING) || (dhcp->state == DHCP_REBINDING) || (dhcp->state == DHCP_RENEWING)) {
dhcp->request_timeout = 0;
dhcp_bind(netif);
}
}
/* received a DHCP_NAK in appropriate state? */
else if ((msg_type == DHCP_NAK) &&
((dhcp->state == DHCP_REBOOTING) || (dhcp->state == DHCP_REQUESTING) ||
(dhcp->state == DHCP_REBINDING) || (dhcp->state == DHCP_RENEWING ))) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 1, ("DHCP_NAK received\n"));
dhcp->request_timeout = 0;
dhcp_handle_nak(netif);
}
/* received a DHCP_OFFER in DHCP_SELECTING state? */
else if ((msg_type == DHCP_OFFER) && (dhcp->state == DHCP_SELECTING)) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 1, ("DHCP_OFFER received in DHCP_SELECTING state\n"));
dhcp->request_timeout = 0;
/* remember offered lease */
dhcp_handle_offer(netif);
}
pbuf_free(p);
dhcp->p = NULL;
}
/* Work around for original dhcp_recv() */
static void dhcp_recv_wrapper(void *arg, struct udp_pcb *pcb, struct pbuf *p,
struct ip_addr *addr, u16_t port)
{
struct ip4_addr ip;
ip64_addr_set(&ip, addr);
dhcp_recv(arg, pcb, p, &ip, port);
}
static err_t dhcp_create_request(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
u16_t i;
LWIP_ASSERT("dhcp_create_request: dhcp->p_out == NULL", dhcp->p_out == NULL);
LWIP_ASSERT("dhcp_create_request: dhcp->msg_out == NULL", dhcp->msg_out == NULL);
dhcp->p_out = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcp_msg), PBUF_RAM);
if (dhcp->p_out == NULL) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("dhcp_create_request(): could not allocate pbuf\n"));
return ERR_MEM;
}
/* give unique transaction identifier to this request */
dhcp->xid = xid++;
dhcp->msg_out = (struct dhcp_msg *)dhcp->p_out->payload;
dhcp->msg_out->op = DHCP_BOOTREQUEST;
/* TODO: make link layer independent */
dhcp->msg_out->htype = DHCP_HTYPE_ETH;
/* TODO: make link layer independent */
dhcp->msg_out->hlen = DHCP_HLEN_ETH;
dhcp->msg_out->hops = 0;
dhcp->msg_out->xid = htonl(dhcp->xid);
dhcp->msg_out->secs = 0;
dhcp->msg_out->flags = 0;
{
///dhcp->msg_out->ciaddr.addr = netif->ip_addr.addr;
struct ip_addr_list *al;
struct ip_addr ip6_server;
IP64_ADDR(&ip6_server, 0,0,0,0);
// FIX: is this correct?
if ((al=ip_addr_list_maskfind(netif->addrs, &ip6_server)) == NULL)
ip64_addr_set( (struct ip4_addr *)(& dhcp->msg_out->ciaddr.addr), &ip6_server) ;
else
ip64_addr_set( (struct ip4_addr *)(& dhcp->msg_out->ciaddr.addr), &al->ipaddr );
}
dhcp->msg_out->yiaddr.addr = 0;
dhcp->msg_out->siaddr.addr = 0;
dhcp->msg_out->giaddr.addr = 0;
for (i = 0; i < DHCP_CHADDR_LEN; i++) {
/* copy netif hardware address, pad with zeroes */
dhcp->msg_out->chaddr[i] = (i < netif->hwaddr_len) ? netif->hwaddr[i] : 0/* pad byte*/;
}
for (i = 0; i < DHCP_SNAME_LEN; i++) dhcp->msg_out->sname[i] = 0;
for (i = 0; i < DHCP_FILE_LEN; i++) dhcp->msg_out->file[i] = 0;
dhcp->msg_out->cookie = htonl(0x63825363UL);
dhcp->options_out_len = 0;
/* fill options field with an incrementing array (for debugging purposes) */
for (i = 0; i < DHCP_OPTIONS_LEN; i++) dhcp->msg_out->options[i] = i;
return ERR_OK;
}
static void dhcp_delete_request(struct netif *netif)
{
struct dhcp *dhcp = netif->dhcp;
LWIP_ASSERT("dhcp_free_msg: dhcp->p_out != NULL", dhcp->p_out != NULL);
LWIP_ASSERT("dhcp_free_msg: dhcp->msg_out != NULL", dhcp->msg_out != NULL);
pbuf_free(dhcp->p_out);
dhcp->p_out = NULL;
dhcp->msg_out = NULL;
}
/**
* Add a DHCP message trailer
*
* Adds the END option to the DHCP message, and if
* necessary, up to three padding bytes.
*/
static void dhcp_option_trailer(struct dhcp *dhcp)
{
LWIP_ASSERT("dhcp_option_trailer: dhcp->msg_out != NULL\n", dhcp->msg_out != NULL);
LWIP_ASSERT("dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
dhcp->msg_out->options[dhcp->options_out_len++] = DHCP_OPTION_END;
/* packet is too small, or not 4 byte aligned? */
while ((dhcp->options_out_len < DHCP_MIN_OPTIONS_LEN) || (dhcp->options_out_len & 3)) {
/* LWIP_DEBUGF(DHCP_DEBUG,("dhcp_option_trailer:dhcp->options_out_len=%"U16_F", DHCP_OPTIONS_LEN=%"U16_F, dhcp->options_out_len, DHCP_OPTIONS_LEN)); */
LWIP_ASSERT("dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
/* add a fill/padding byte */
dhcp->msg_out->options[dhcp->options_out_len++] = 0;
}
}
/**
* Find the offset of a DHCP option inside the DHCP message.
*
* @param client DHCP client
* @param option_type
*
* @return a byte offset into the UDP message where the option was found, or
* zero if the given option was not found.
*/
static u8_t *dhcp_get_option_ptr(struct dhcp *dhcp, u8_t option_type)
{
u8_t overload = DHCP_OVERLOAD_NONE;
/* options available? */
if ((dhcp->options_in != NULL) && (dhcp->options_in_len > 0)) {
/* start with options field */
u8_t *options = (u8_t *)dhcp->options_in;
u16_t offset = 0;
/* at least 1 byte to read and no end marker, then at least 3 bytes to read? */
while ((offset < dhcp->options_in_len) && (options[offset] != DHCP_OPTION_END)) {
/* LWIP_DEBUGF(DHCP_DEBUG, ("msg_offset=%"U16_F", q->len=%"U16_F, msg_offset, q->len)); */
/* are the sname and/or file field overloaded with options? */
if (options[offset] == DHCP_OPTION_OVERLOAD) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 2, ("overloaded message detected\n"));
/* skip option type and length */
offset += 2;
overload = options[offset++];
}
/* requested option found */
else if (options[offset] == option_type) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("option found at offset %"U16_F" in options\n", offset));
return &options[offset];
}
/* skip option */
else {
LWIP_DEBUGF(DHCP_DEBUG, ("skipping option %"U16_F" in options\n", options[offset]));
/* skip option type */
offset++;
/* skip option length, and then length bytes */
offset += 1 + options[offset];
}
}
/* is this an overloaded message? */
if (overload != DHCP_OVERLOAD_NONE) {
u16_t field_len;
if (overload == DHCP_OVERLOAD_FILE) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 1, ("overloaded file field\n"));
options = (u8_t *)&dhcp->msg_in->file;
field_len = DHCP_FILE_LEN;
}
else if (overload == DHCP_OVERLOAD_SNAME) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 1, ("overloaded sname field\n"));
options = (u8_t *)&dhcp->msg_in->sname;
field_len = DHCP_SNAME_LEN;
}
/* TODO: check if else if () is necessary */
else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE | 1, ("overloaded sname and file field\n"));
options = (u8_t *)&dhcp->msg_in->sname;
field_len = DHCP_FILE_LEN + DHCP_SNAME_LEN;
}
offset = 0;
/* at least 1 byte to read and no end marker */
while ((offset < field_len) && (options[offset] != DHCP_OPTION_END)) {
if (options[offset] == option_type) {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("option found at offset=%"U16_F"\n", offset));
return &options[offset];
}
/* skip option */
else {
LWIP_DEBUGF(DHCP_DEBUG | DBG_TRACE, ("skipping option %"U16_F"\n", options[offset]));
/* skip option type */
offset++;
offset += 1 + options[offset];
}
}
}
}
return NULL;
}
/**
* Return the byte of DHCP option data.
*
* @param client DHCP client.
* @param ptr pointer obtained by dhcp_get_option_ptr().
*
* @return byte value at the given address.
*/
static u8_t dhcp_get_option_byte(u8_t *ptr)
{
LWIP_DEBUGF(DHCP_DEBUG, ("option byte value=%"U16_F"\n", (u16_t)(*ptr)));
return *ptr;
}
#if 0
/**
* Return the 16-bit value of DHCP option data.
*
* @param client DHCP client.
* @param ptr pointer obtained by dhcp_get_option_ptr().
*
* @return byte value at the given address.
*/
static u16_t dhcp_get_option_short(u8_t *ptr)
{
u16_t value;
value = *ptr++ << 8;
value |= *ptr;
LWIP_DEBUGF(DHCP_DEBUG, ("option short value=%"U16_F"\n", value));
return value;
}
#endif
/**
* Return the 32-bit value of DHCP option data.
*
* @param client DHCP client.
* @param ptr pointer obtained by dhcp_get_option_ptr().
*
* @return byte value at the given address.
*/
static u32_t dhcp_get_option_long(u8_t *ptr)
{
u32_t value;
value = (u32_t)(*ptr++) << 24;
value |= (u32_t)(*ptr++) << 16;
value |= (u32_t)(*ptr++) << 8;
value |= (u32_t)(*ptr++);
LWIP_DEBUGF(DHCP_DEBUG, ("option long value=%"U32_F"\n", value));
return value;
}
err_t
udp_send_netif(struct udp_pcb *pcb, struct pbuf *p, struct netif *netif)
{
struct stack *stack = pcb->stack;
struct udp_hdr *udphdr;
struct ip_addr *src_ip;
err_t err;
struct pbuf *q; /* q will be sent down the stack */
//struct ip_addr *nexthop;
int flags;
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_send\n"));
/* if the PCB is not yet bound to a port, bind it here */
if (pcb->local_port == 0) {
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: not yet bound to a port, binding now\n"));
err = udp_bind(pcb, &pcb->local_ip, pcb->local_port, NULL);
if (err != ERR_OK) {
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: forced port bind failed\n"));
return err;
}
}
if (pbuf_header(p, UDP_HLEN)) {
q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
if (q == NULL) {
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: could not allocate header\n"));
return ERR_MEM;
}
pbuf_chain(q, p);
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
} else {
q = p;
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
}
/* { q now represents the packet to be sent } */
udphdr = q->payload;
udphdr->src = htons(pcb->local_port);
udphdr->dest = htons(pcb->remote_port);
udphdr->chksum = 0x0000;
#if 0
/* PCB local address is IP_ANY_ADDR? */
if (ip_addr_isany(&pcb->local_ip)) {
/* use outgoing network interface IP address as source address */
/*src_ip = &(netif->ip_addr);*/
struct ip_addr_list *el;
///if ((el=ip_addr_list_maskfind(netif->addrs,nexthop)) == NULL)
/// return err;
///src_ip = &(el->ipaddr);
/* Added by Diego Billi */
/* Get source address */
if (ip_addr_is_v4comp(&pcb->remote_ip)) {
if ((el=ip_addr_list_maskfind(netif->addrs, &pcb->remote_ip)) != NULL)
src_ip = &(el->ipaddr);
else
src_ip = &pcb->local_ip;
}
else {
if ((el=ip_route_ipv6_select_source(netif, &pcb->remote_ip)) != NULL)
src_ip = &(el->ipaddr);
else
src_ip = &pcb->local_ip;
}
}
else {
/* use UDP PCB local IP address as source address */
src_ip = &(pcb->local_ip);
}
#endif
src_ip = &(pcb->local_ip);
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %u\n", q->tot_len));
if (pcb->flags & UDP_FLAGS_UDPLITE) {
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %u\n", q->tot_len));
udphdr->len = htons(pcb->chksum_len);
udphdr->chksum = inet6_chksum_pseudo(q, src_ip, &(pcb->remote_ip), IP_PROTO_UDP, pcb->chksum_len);
if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n"));
err = ip_output_if (stack, q, src_ip, &pcb->remote_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif, &pcb->remote_ip, flags);
}
else {
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %u\n", q->tot_len));
udphdr->len = htons(q->tot_len);
if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
udphdr->chksum = inet6_chksum_pseudo(q, src_ip, &pcb->remote_ip, IP_PROTO_UDP, q->tot_len);
if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;
}
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04x\n", udphdr->chksum));
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n"));
/* output to IP */
err = ip_output_if(stack, q, src_ip, &pcb->remote_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif, &pcb->remote_ip, flags);
}
/* did we chain a seperate header pbuf earlier? */
if (q != p) {
/* free the header pbuf */
pbuf_free(q); q = NULL;
/* { p is still referenced by the caller, and will live on } */
}
UDP_STATS_INC(udp.xmit);
return err;
}
err_t
udp_sendto_netif(struct udp_pcb *pcb, struct pbuf *p,
struct ip_addr *dst_ip, u16_t dst_port, struct netif *netif)
{
err_t err;
/* temporary space for current PCB remote address */
struct ip_addr pcb_remote_ip;
u16_t pcb_remote_port;
/* remember current remote peer address of PCB */
memcpy(&(pcb_remote_ip.addr), &(pcb->remote_ip.addr), sizeof(struct ip_addr));
pcb_remote_port = pcb->remote_port;
/* copy packet destination address to PCB remote peer address */
memcpy(&(pcb->remote_ip.addr), &(dst_ip->addr), sizeof(struct ip_addr));
pcb->remote_port = dst_port;
/* send to the packet destination address */
err = udp_send_netif(pcb, p, netif);
/* restore PCB remote peer address */
memcpy(&(pcb->remote_ip.addr), &(pcb_remote_ip.addr), sizeof(struct ip_addr));
pcb->remote_port = pcb_remote_port;
return err;
}
#endif /* LWIP_DHCP */
|