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
|
/*
* Embedded Linux library
* Copyright (C) 2018 Intel Corporation
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <netinet/ip.h>
#include <net/ethernet.h>
#include <linux/types.h>
#include <net/if_arp.h>
#include <errno.h>
#include <arpa/inet.h>
#include "private.h"
#include "useful.h"
#include "random.h"
#include "time.h"
#include "time-private.h"
#include "net.h"
#include "timeout.h"
#include "dhcp.h"
#include "dhcp-private.h"
#include "netlink.h"
#include "rtnl.h"
#include "acd.h"
#include "log.h"
#define CLIENT_LOG(priority, fmt, args...) \
if (priority <= client->debug_level) \
l_util_debug(client->debug_handler, client->debug_data, \
"%s:%i " fmt, __func__, __LINE__, ## args)
#define CLIENT_DEBUG(fmt, args...) \
CLIENT_LOG(L_LOG_DEBUG, fmt, ## args)
#define CLIENT_INFO(fmt, args...) \
CLIENT_LOG(L_LOG_INFO, fmt, ## args)
#define CLIENT_WARN(fmt, args...) \
CLIENT_LOG(L_LOG_WARNING, fmt, ## args)
#define CLIENT_ENTER_STATE(s) \
CLIENT_INFO("Entering state: " #s); \
client->state = (s)
#define BITS_PER_LONG (sizeof(unsigned long) * 8)
#define CLIENT_MAX_ATTEMPT_LIMIT 30
#define CLIENT_MIN_ATTEMPT_LIMIT 3
enum dhcp_state {
DHCP_STATE_INIT,
DHCP_STATE_SELECTING,
DHCP_STATE_INIT_REBOOT,
DHCP_STATE_REBOOTING,
DHCP_STATE_REQUESTING,
DHCP_STATE_BOUND,
DHCP_STATE_RENEWING,
DHCP_STATE_REBINDING,
};
const char *_dhcp_message_type_to_string(uint8_t type)
{
switch(type) {
case DHCP_MESSAGE_TYPE_DISCOVER:
return "DHCPDISCOVER";
case DHCP_MESSAGE_TYPE_OFFER:
return "DHCPOFFER";
case DHCP_MESSAGE_TYPE_REQUEST:
return "DHCPREQUEST";
case DHCP_MESSAGE_TYPE_DECLINE:
return "DHCPDECLINE";
case DHCP_MESSAGE_TYPE_ACK:
return "DHCPACK";
case DHCP_MESSAGE_TYPE_NAK:
return "DHCPNAK";
case DHCP_MESSAGE_TYPE_RELEASE:
return "DHCPRELEASE";
default:
return "unknown";
}
}
const char *_dhcp_option_to_string(uint8_t option)
{
switch (option) {
case DHCP_OPTION_PAD:
return "Pad";
case L_DHCP_OPTION_SUBNET_MASK:
return "Subnet Mask";
case L_DHCP_OPTION_ROUTER:
return "Router";
case L_DHCP_OPTION_DOMAIN_NAME_SERVER:
return "Domain Name Server";
case L_DHCP_OPTION_HOST_NAME:
return "Host Name";
case L_DHCP_OPTION_DOMAIN_NAME:
return "Domain Name";
case L_DHCP_OPTION_BROADCAST_ADDRESS:
return "Broadcast Address";
case L_DHCP_OPTION_NTP_SERVERS:
return "NTP Servers";
case L_DHCP_OPTION_REQUESTED_IP_ADDRESS:
return "IP Address";
case L_DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
return "IP Address Lease Time";
case DHCP_OPTION_OVERLOAD:
return "Overload";
case DHCP_OPTION_MESSAGE_TYPE:
return "DHCP Message Type";
case L_DHCP_OPTION_SERVER_IDENTIFIER:
return "Server Identifier";
case DHCP_OPTION_PARAMETER_REQUEST_LIST:
return "Parameter Request List";
case DHCP_OPTION_MAXIMUM_MESSAGE_SIZE:
return "Maximum Message Size";
case L_DHCP_OPTION_RENEWAL_T1_TIME:
return "Renewal Time";
case L_DHCP_OPTION_REBINDING_T2_TIME:
return "Rebinding Time";
case DHCP_OPTION_CLIENT_IDENTIFIER:
return "Client Identifier";
case DHCP_OPTION_END:
return "End";
default:
return "unknown";
}
}
static void dhcp_message_set_address_type(struct dhcp_message *message,
uint8_t addr_type,
uint8_t addr_len)
{
message->htype = addr_type;
switch (addr_type) {
case ARPHRD_ETHER:
message->hlen = addr_len;
break;
default:
message->hlen = 0;
}
}
struct l_dhcp_client {
enum dhcp_state state;
unsigned long request_options[256 / BITS_PER_LONG];
uint32_t ifindex;
char *ifname;
uint8_t addr[6];
uint8_t addr_len;
uint8_t addr_type;
char *hostname;
uint32_t xid;
struct dhcp_transport *transport;
uint64_t start_t;
struct l_timeout *timeout_resend;
struct l_timeout *timeout_lease;
struct l_dhcp_lease *lease;
struct l_netlink *rtnl;
uint32_t rtnl_add_cmdid;
struct l_rtnl_address *rtnl_configured_address;
uint8_t attempt;
uint8_t max_attempts;
l_dhcp_client_event_cb_t event_handler;
void *event_data;
l_dhcp_destroy_cb_t event_destroy;
l_dhcp_debug_cb_t debug_handler;
l_dhcp_destroy_cb_t debug_destroy;
int debug_level;
struct l_acd *acd;
void *debug_data;
bool have_addr : 1;
bool override_xid : 1;
};
static inline void dhcp_enable_option(struct l_dhcp_client *client,
uint8_t option)
{
client->request_options[option / BITS_PER_LONG] |=
1UL << (option % BITS_PER_LONG);
}
static uint16_t dhcp_attempt_secs(uint64_t start)
{
uint64_t now = l_time_now();
uint64_t elapsed = l_time_to_secs(now - start);
if (elapsed == 0)
return 1;
if (elapsed > UINT16_MAX)
return UINT16_MAX;
return elapsed;
}
/*
* Takes a time in seconds and produces a fuzzed value that can be directly
* used by l_timeout_modify_ms
*/
static uint64_t dhcp_fuzz_secs(uint32_t secs)
{
/*
* RFC2132, Section 4.1:
* DHCP clients are responsible for all message retransmission. The
* client MUST adopt a retransmission strategy that incorporates a
* randomized exponential backoff algorithm to determine the delay
* between retransmissions.
*
* and later in the same paragraph:
* For example, in a 10Mb/sec Ethernet internetwork, the delay before
* the first retransmission SHOULD be 4 seconds randomized by the
* value of a uniform random number chosen from the range -1 to +1.
* Clients with clocks that provide resolution granularity of less than
* one second may choose a non-integer randomization value.
*/
return _time_fuzz_secs(secs, 1);
}
/*
* Takes a time in milliseconds and produces a fuzzed value that can be directly
* used by l_timeout_modify_ms. The fluctuation of the random noise added is
* from -63 to 63 milliseconds.
*/
static uint64_t dhcp_fuzz_msecs(uint64_t ms)
{
uint32_t r = l_getrandom_uint32();
if (r & 0x80000000)
ms += r & 0x3f;
else
ms -= r & 0x3f;
return ms;
}
static uint32_t dhcp_rebind_renew_retry_time(uint64_t start_t, uint32_t expiry)
{
uint64_t now = l_time_now();
uint32_t relative_now;
uint32_t retry_time;
/*
* RFC 2131, Section 4.4.5:
* " In both RENEWING and REBINDING states, if the client receives no
* response to its DHCPREQUEST message, the client SHOULD wait one-half
* of the remaining time until T2 (in RENEWING state) and one-half of
* the remaining lease time (in REBINDING state), down to a minimum of
* 60 seconds, before retransmitting the DHCPREQUEST message.
*/
relative_now = l_time_to_secs(now - start_t);
retry_time = (expiry - relative_now) / 2;
if (retry_time < 60)
retry_time = 60;
return retry_time;
}
static int client_message_init(struct l_dhcp_client *client,
struct dhcp_message *message,
struct dhcp_message_builder *builder)
{
uint16_t max_size;
message->op = DHCP_OP_CODE_BOOTREQUEST;
message->xid = L_CPU_TO_BE32(client->xid);
message->magic = L_CPU_TO_BE32(DHCP_MAGIC);
dhcp_message_set_address_type(message, client->addr_type,
client->addr_len);
/*
* RFC2132 section 4.1.1:
* The client MUST include its hardware address in the ’chaddr’ field,
* if necessary for delivery of DHCP reply messages. Non-Ethernet
* interfaces will leave 'chaddr' empty and use the client identifier
* instead
*/
if (client->addr_type == ARPHRD_ETHER)
memcpy(message->chaddr, &client->addr, client->addr_len);
/*
* Althrough RFC 2131 says that secs should be initialized to 0,
* some servers refuse to give us a lease unless we set this to a
* non-zero value
*/
message->secs = L_CPU_TO_BE16(dhcp_attempt_secs(client->start_t));
if (!_dhcp_message_builder_append_prl(builder,
client->request_options))
return -EINVAL;
/*
* Set the maximum DHCP message size to the minimum legal value. This
* helps some buggy DHCP servers to not send bigger packets
*/
max_size = L_CPU_TO_BE16(576);
if (!_dhcp_message_builder_append(builder,
DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
2, &max_size))
return -EINVAL;
return 0;
}
static void dhcp_client_event_notify(struct l_dhcp_client *client,
enum l_dhcp_client_event event)
{
if (client->event_handler)
client->event_handler(client, event, client->event_data);
}
static int dhcp_client_send_discover(struct l_dhcp_client *client)
{
struct dhcp_message_builder builder;
size_t optlen = DHCP_MIN_OPTIONS_SIZE;
size_t len = sizeof(struct dhcp_message) + optlen;
L_AUTO_FREE_VAR(struct dhcp_message *, discover);
int err;
CLIENT_DEBUG("");
discover = (struct dhcp_message *) l_new(uint8_t, len);
_dhcp_message_builder_init(&builder, discover, len,
DHCP_MESSAGE_TYPE_DISCOVER);
err = client_message_init(client, discover, &builder);
if (err < 0)
return err;
if (client->hostname)
if (!_dhcp_message_builder_append(&builder,
L_DHCP_OPTION_HOST_NAME,
strlen(client->hostname),
client->hostname))
return -EINVAL;
_dhcp_message_builder_append(&builder, DHCP_OPTION_RAPID_COMMIT,
0, "");
_dhcp_message_builder_finalize(&builder, &len);
return client->transport->l2_send(client->transport,
INADDR_ANY, DHCP_PORT_CLIENT,
INADDR_BROADCAST, DHCP_PORT_SERVER,
NULL,
discover, len);
}
static int dhcp_client_send_unicast(struct l_dhcp_client *client,
struct dhcp_message *request,
unsigned int len)
{
struct sockaddr_in si;
int r;
memset(&si, 0, sizeof(si));
si.sin_family = AF_INET;
si.sin_port = L_CPU_TO_BE16(DHCP_PORT_SERVER);
si.sin_addr.s_addr = client->lease->server_address;
/*
* sendto() might fail with an EPERM error, which most likely means
* that the unicast was prevented by netfilter. Ignore this case
* and assume that once the REBINDING timeout is hit, a broadcast
* will go through which will have a chance of renewing the lease
*/
r = client->transport->send(client->transport, &si, request, len);
if (r == -EPERM) {
CLIENT_DEBUG("transport->send() failed with EPERM -> ignore");
CLIENT_DEBUG("Is a firewall denying unicast DHCP packets?");
return 0;
}
return r;
}
static int dhcp_client_send_request(struct l_dhcp_client *client)
{
struct dhcp_message_builder builder;
size_t optlen = DHCP_MIN_OPTIONS_SIZE;
size_t len = sizeof(struct dhcp_message) + optlen;
L_AUTO_FREE_VAR(struct dhcp_message *, request);
int err;
CLIENT_DEBUG("");
request = (struct dhcp_message *) l_new(uint8_t, len);
_dhcp_message_builder_init(&builder, request, len,
DHCP_MESSAGE_TYPE_REQUEST);
err = client_message_init(client, request, &builder);
if (err < 0)
return err;
switch (client->state) {
case DHCP_STATE_REQUESTING:
/*
* RFC 2131, Section 4.3.2:
* "If the DHCPREQUEST message contains a 'server identifier'
* option, the message is in response to a DHCPOFFER message."
*
* and
*
* "DHCPREQUEST generated during SELECTING state:
* Client inserts the address of the selected server in
* 'server identifier', 'ciaddr' MUST be zero, 'requested IP
* address' MUST be filled in with the yiaddr value from the
* chosen DHCPOFFER."
*
* NOTE: 'SELECTING' is meant to be 'REQUESTING' in the RFC
*/
if (!_dhcp_message_builder_append(&builder,
L_DHCP_OPTION_SERVER_IDENTIFIER,
4, &client->lease->server_address)) {
CLIENT_WARN("Failed to append server ID");
return -EINVAL;
}
if (!_dhcp_message_builder_append(&builder,
L_DHCP_OPTION_REQUESTED_IP_ADDRESS,
4, &client->lease->address)) {
CLIENT_WARN("Failed to append requested IP");
return -EINVAL;
}
break;
case DHCP_STATE_RENEWING:
case DHCP_STATE_REBINDING:
request->ciaddr = client->lease->address;
break;
case DHCP_STATE_INIT:
case DHCP_STATE_SELECTING:
case DHCP_STATE_INIT_REBOOT:
case DHCP_STATE_REBOOTING:
case DHCP_STATE_BOUND:
return -EINVAL;
}
if (client->hostname) {
if (!_dhcp_message_builder_append(&builder,
L_DHCP_OPTION_HOST_NAME,
strlen(client->hostname),
client->hostname)) {
CLIENT_WARN("Failed to append host name");
return -EINVAL;
}
}
_dhcp_message_builder_finalize(&builder, &len);
/*
* RFC2131, Section 4.1:
* "DHCP clients MUST use the IP address provided in the
* 'server identifier' option for any unicast requests to the DHCP
* server.
*/
if (client->state == DHCP_STATE_RENEWING)
return dhcp_client_send_unicast(client, request, len);
return client->transport->l2_send(client->transport,
INADDR_ANY, DHCP_PORT_CLIENT,
INADDR_BROADCAST, DHCP_PORT_SERVER,
NULL, request, len);
}
static void dhcp_client_send_release(struct l_dhcp_client *client)
{
struct dhcp_message_builder builder;
size_t optlen = DHCP_MIN_OPTIONS_SIZE;
size_t len = sizeof(struct dhcp_message) + optlen;
L_AUTO_FREE_VAR(struct dhcp_message *, request);
int err;
struct sockaddr_in si;
CLIENT_DEBUG("");
memset(&si, 0, sizeof(si));
si.sin_family = AF_INET;
si.sin_port = L_CPU_TO_BE16(DHCP_PORT_SERVER);
si.sin_addr.s_addr = client->lease->server_address;
request = (struct dhcp_message *) l_new(uint8_t, len);
_dhcp_message_builder_init(&builder, request, len,
DHCP_MESSAGE_TYPE_RELEASE);
err = client_message_init(client, request, &builder);
if (err < 0)
return;
request->ciaddr = client->lease->address;
if (!_dhcp_message_builder_append(&builder,
L_DHCP_OPTION_SERVER_IDENTIFIER,
4, &client->lease->server_address)) {
CLIENT_WARN("Failed to append server ID");
return;
}
_dhcp_message_builder_finalize(&builder, &len);
dhcp_client_send_unicast(client, request, len);
}
static void dhcp_client_timeout_resend(struct l_timeout *timeout,
void *user_data)
{
struct l_dhcp_client *client = user_data;
struct l_dhcp_lease *lease = client->lease;
unsigned int next_timeout = 0;
int r;
CLIENT_DEBUG("");
switch (client->state) {
case DHCP_STATE_SELECTING:
r = dhcp_client_send_discover(client);
if (r < 0) {
CLIENT_WARN("Sending discover failed: %s",
strerror(-r));
goto error;
}
break;
case DHCP_STATE_RENEWING:
case DHCP_STATE_REQUESTING:
case DHCP_STATE_REBINDING:
r = dhcp_client_send_request(client);
if (r < 0) {
CLIENT_WARN("Sending Request failed: %s",
strerror(-r));
goto error;
}
break;
case DHCP_STATE_INIT:
case DHCP_STATE_INIT_REBOOT:
case DHCP_STATE_REBOOTING:
case DHCP_STATE_BOUND:
break;
}
switch (client->state) {
case DHCP_STATE_RENEWING:
next_timeout = dhcp_rebind_renew_retry_time(lease->bound_time,
lease->t2);
break;
case DHCP_STATE_REBINDING:
next_timeout = dhcp_rebind_renew_retry_time(lease->bound_time,
lease->lifetime);
break;
case DHCP_STATE_REQUESTING:
case DHCP_STATE_SELECTING:
/*
* RFC 2131 Section 4.1:
* "The retransmission delay SHOULD be doubled with subsequent
* retransmissions up to a maximum of 64 seconds.
*/
if (client->attempt < client->max_attempts) {
next_timeout = minsize(2 << client->attempt++, 64);
break;
}
CLIENT_DEBUG("Max request/discover attempts reached");
dhcp_client_event_notify(client,
L_DHCP_CLIENT_EVENT_MAX_ATTEMPTS_REACHED);
return;
case DHCP_STATE_INIT:
case DHCP_STATE_INIT_REBOOT:
case DHCP_STATE_REBOOTING:
case DHCP_STATE_BOUND:
break;
}
if (next_timeout)
l_timeout_modify_ms(timeout, dhcp_fuzz_secs(next_timeout));
return;
error:
l_dhcp_client_stop(client);
}
static void dhcp_client_lease_expired(struct l_timeout *timeout,
void *user_data)
{
struct l_dhcp_client *client = user_data;
CLIENT_DEBUG("");
l_dhcp_client_stop(client);
dhcp_client_event_notify(client, L_DHCP_CLIENT_EVENT_LEASE_EXPIRED);
}
static void dhcp_client_t2_expired(struct l_timeout *timeout, void *user_data)
{
struct l_dhcp_client *client = user_data;
uint32_t next_timeout = client->lease->lifetime - client->lease->t2;
CLIENT_DEBUG("");
/*
* If we got here, then resend_timeout is active, with a timeout
* set originally for ~60 seconds. So we simply set the new state
* and wait for the timer to fire
*/
CLIENT_ENTER_STATE(DHCP_STATE_REBINDING);
l_timeout_modify_ms(client->timeout_lease,
dhcp_fuzz_secs(next_timeout));
l_timeout_set_callback(client->timeout_lease,
dhcp_client_lease_expired, client, NULL);
}
static void dhcp_client_t1_expired(struct l_timeout *timeout, void *user_data)
{
struct l_dhcp_client *client = user_data;
uint32_t next_timeout;
int r;
CLIENT_DEBUG("");
CLIENT_ENTER_STATE(DHCP_STATE_RENEWING);
client->attempt = 1;
r = dhcp_client_send_request(client);
if (r < 0) {
CLIENT_WARN("Sending request failed: %s", strerror(-r));
goto error;
}
next_timeout = client->lease->t2 - client->lease->t1;
l_timeout_modify_ms(client->timeout_lease,
dhcp_fuzz_secs(next_timeout));
l_timeout_set_callback(client->timeout_lease, dhcp_client_t2_expired,
client, NULL);
next_timeout = dhcp_rebind_renew_retry_time(client->lease->bound_time,
client->lease->t2);
client->timeout_resend =
l_timeout_create_ms(dhcp_fuzz_secs(next_timeout),
dhcp_client_timeout_resend,
client, NULL);
return;
error:
l_dhcp_client_stop(client);
}
static void dhcp_client_address_add_cb(int error, uint16_t type,
const void *data, uint32_t len,
void *user_data)
{
struct l_dhcp_client *client = user_data;
client->rtnl_add_cmdid = 0;
if (error < 0 && error != -EEXIST) {
l_rtnl_address_free(client->rtnl_configured_address);
client->rtnl_configured_address = NULL;
CLIENT_WARN("Unable to set address on ifindex: %u: %d(%s)",
client->ifindex, error,
strerror(-error));
return;
}
}
static int dhcp_client_receive_ack(struct l_dhcp_client *client,
const uint8_t *saddr,
const struct dhcp_message *ack,
size_t len, uint64_t timestamp)
{
struct dhcp_message_iter iter;
struct l_dhcp_lease *lease;
int r;
CLIENT_DEBUG("");
if (ack->yiaddr == 0)
return -ENOMSG;
if (!_dhcp_message_iter_init(&iter, ack, len))
return -EINVAL;
lease = _dhcp_lease_parse_options(&iter);
if (!lease) {
CLIENT_WARN("Failed to parse DHCP options.");
return -ENOMSG;
}
lease->address = ack->yiaddr;
if (saddr)
memcpy(lease->server_mac, saddr, ETH_ALEN);
r = L_DHCP_CLIENT_EVENT_LEASE_RENEWED;
if (client->lease) {
if (client->lease->subnet_mask != lease->subnet_mask ||
client->lease->address != lease->address ||
client->lease->router != lease->router)
r = L_DHCP_CLIENT_EVENT_IP_CHANGED;
_dhcp_lease_free(client->lease);
}
client->lease = lease;
/* In case this is an initial request, override to LEASE_OBTAINED */
if (client->state == DHCP_STATE_REQUESTING ||
client->state == DHCP_STATE_REBOOTING)
r = L_DHCP_CLIENT_EVENT_LEASE_OBTAINED;
if (client->rtnl) {
struct l_rtnl_address *a;
L_AUTO_FREE_VAR(char *, ip) =
l_dhcp_lease_get_address(client->lease);
uint8_t prefix_len;
uint32_t l = l_dhcp_lease_get_lifetime(client->lease);
L_AUTO_FREE_VAR(char *, broadcast) =
l_dhcp_lease_get_broadcast(client->lease);
uint64_t et = timestamp + l * L_USEC_PER_SEC;
prefix_len = l_dhcp_lease_get_prefix_length(client->lease);
if (!prefix_len)
prefix_len = 24;
a = l_rtnl_address_new(ip, prefix_len);
l_rtnl_address_set_noprefixroute(a, true);
l_rtnl_address_set_lifetimes(a, l, l);
l_rtnl_address_set_expiry(a, et, et);
l_rtnl_address_set_broadcast(a, broadcast);
client->rtnl_add_cmdid =
l_rtnl_ifaddr_add(client->rtnl, client->ifindex, a,
dhcp_client_address_add_cb,
client, NULL);
if (client->rtnl_add_cmdid)
client->rtnl_configured_address = a;
else {
CLIENT_WARN("Configuring address via RTNL failed");
l_rtnl_address_free(a);
}
}
return r;
}
static int dhcp_client_receive_offer(struct l_dhcp_client *client,
const struct dhcp_message *offer,
size_t len)
{
struct dhcp_message_iter iter;
struct l_dhcp_lease *lease;
CLIENT_DEBUG("");
if (offer->yiaddr == 0)
return -ENOMSG;
if (!_dhcp_message_iter_init(&iter, offer, len))
return -EINVAL;
lease = _dhcp_lease_parse_options(&iter);
if (!lease)
return -ENOMSG;
/*
* Received another offer. In the case of multiple DHCP servers we want
* to ignore it and continue using the first offer. If this is from the
* same server its likely a buggy DHCP implementation and we should
* use the last offer it sends.
*/
if (client->lease) {
if (client->lease->server_address != lease->server_address) {
_dhcp_lease_free(lease);
return -ENOMSG;
}
CLIENT_INFO("Server sent another offer, using it instead");
_dhcp_lease_free(client->lease);
}
client->lease = lease;
client->lease->address = offer->yiaddr;
return 0;
}
static bool dhcp_client_handle_offer(struct l_dhcp_client *client,
const struct dhcp_message *message,
size_t len)
{
if (dhcp_client_receive_offer(client, message, len) < 0)
return false;
CLIENT_ENTER_STATE(DHCP_STATE_REQUESTING);
client->attempt = 1;
if (dhcp_client_send_request(client) < 0) {
l_dhcp_client_stop(client);
return false;
}
l_timeout_modify_ms(client->timeout_resend, dhcp_fuzz_secs(4));
return true;
}
static void dhcp_client_rx_message(const void *data, size_t len, void *userdata,
const uint8_t *saddr,
uint64_t timestamp)
{
struct l_dhcp_client *client = userdata;
const struct dhcp_message *message = data;
struct dhcp_message_iter iter;
char buf[INET_ADDRSTRLEN];
uint8_t msg_type = 0;
uint8_t t, l;
const void *v;
int r, e;
struct in_addr ia;
enum l_dhcp_client_event event = L_DHCP_CLIENT_EVENT_LEASE_EXPIRED;
CLIENT_DEBUG("");
if (len < sizeof(struct dhcp_message))
return;
if (message->op != DHCP_OP_CODE_BOOTREPLY)
return;
if (L_BE32_TO_CPU(message->xid) != client->xid)
return;
if (memcmp(message->chaddr, client->addr, client->addr_len))
return;
if (!_dhcp_message_iter_init(&iter, message, len))
return;
while (_dhcp_message_iter_next(&iter, &t, &l, &v) && !msg_type) {
switch (t) {
case DHCP_OPTION_MESSAGE_TYPE:
if (l == 1)
msg_type = l_get_u8(v);
break;
}
}
switch (client->state) {
case DHCP_STATE_INIT:
return;
case DHCP_STATE_SELECTING:
if (msg_type == DHCP_MESSAGE_TYPE_ACK) {
_dhcp_message_iter_init(&iter, message, len);
while (_dhcp_message_iter_next(&iter, &t, &l, &v))
if (t == DHCP_OPTION_RAPID_COMMIT) {
CLIENT_ENTER_STATE(
DHCP_STATE_REQUESTING);
goto receive_rapid_commit;
}
}
if (msg_type != DHCP_MESSAGE_TYPE_OFFER)
return;
if (!dhcp_client_handle_offer(client, message, len))
return;
break;
case DHCP_STATE_REQUESTING:
if (msg_type == DHCP_MESSAGE_TYPE_OFFER) {
dhcp_client_handle_offer(client, message, len);
return;
}
event = L_DHCP_CLIENT_EVENT_NO_LEASE;
/* Fall through */
case DHCP_STATE_RENEWING:
case DHCP_STATE_REBINDING:
receive_rapid_commit:
if (msg_type == DHCP_MESSAGE_TYPE_NAK) {
CLIENT_INFO("Received NAK, Stopping...");
l_dhcp_client_stop(client);
dhcp_client_event_notify(client, event);
return;
}
if (msg_type != DHCP_MESSAGE_TYPE_ACK)
return;
r = dhcp_client_receive_ack(client, saddr, message, len,
timestamp);
if (r < 0)
return;
CLIENT_ENTER_STATE(DHCP_STATE_BOUND);
l_timeout_remove(client->timeout_resend);
client->timeout_resend = NULL;
client->lease->bound_time = timestamp;
if (client->transport->bind) {
e = client->transport->bind(client->transport,
client->lease->address);
if (e < 0) {
CLIENT_WARN("Failed to bind dhcp socket. "
"Error %d: %s", e, strerror(-e));
}
}
dhcp_client_event_notify(client, r);
/*
* Start T1, once it expires we will start the T2 timer. If
* we renew the lease, we will end up back here.
*
* RFC2131, Section 4.4.5 states:
* "Times T1 and T2 SHOULD be chosen with some random "fuzz"
* around a fixed value, to avoid synchronization of client
* reacquisition."
*/
l_timeout_remove(client->timeout_lease);
client->timeout_lease = NULL;
/* Infinite lease, no need to start t1 */
if (client->lease->lifetime != 0xffffffffu) {
uint32_t next_timeout =
dhcp_fuzz_secs(client->lease->t1);
CLIENT_INFO("T1 expiring in %u ms", next_timeout);
client->timeout_lease =
l_timeout_create_ms(next_timeout,
dhcp_client_t1_expired,
client, NULL);
}
/* ACD is already running, no need to re-announce */
if (client->acd)
break;
client->acd = l_acd_new(client->ifindex);
if (client->debug_handler && client->debug_level == L_LOG_DEBUG)
l_acd_set_debug(client->acd, client->debug_handler,
client->debug_data,
client->debug_destroy);
/*
* TODO: There is no mechanism yet to deal with IPs leased by
* the DHCP server which conflict with other devices. For now
* the ACD object is being initialized to defend infinitely
* which is effectively no different than the non-ACD behavior
* (ignore conflicts and continue using address). The only
* change is that announcements will be sent if conflicts are
* found.
*/
l_acd_set_defend_policy(client->acd,
L_ACD_DEFEND_POLICY_INFINITE);
l_acd_set_skip_probes(client->acd, true);
ia.s_addr = client->lease->address;
inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN);
/* For unit testing we don't want this to be a fatal error */
if (!l_acd_start(client->acd, buf)) {
CLIENT_WARN("Failed to start ACD on %s, continuing",
buf);
l_acd_destroy(client->acd);
client->acd = NULL;
}
break;
case DHCP_STATE_INIT_REBOOT:
case DHCP_STATE_REBOOTING:
case DHCP_STATE_BOUND:
break;
}
}
LIB_EXPORT struct l_dhcp_client *l_dhcp_client_new(uint32_t ifindex)
{
struct l_dhcp_client *client;
client = l_new(struct l_dhcp_client, 1);
client->state = DHCP_STATE_INIT;
client->ifindex = ifindex;
client->max_attempts = CLIENT_MAX_ATTEMPT_LIMIT;
/* Enable these options by default */
dhcp_enable_option(client, L_DHCP_OPTION_SUBNET_MASK);
dhcp_enable_option(client, L_DHCP_OPTION_ROUTER);
dhcp_enable_option(client, L_DHCP_OPTION_HOST_NAME);
dhcp_enable_option(client, L_DHCP_OPTION_DOMAIN_NAME);
dhcp_enable_option(client, L_DHCP_OPTION_DOMAIN_NAME_SERVER);
dhcp_enable_option(client, L_DHCP_OPTION_NTP_SERVERS);
return client;
}
LIB_EXPORT void l_dhcp_client_destroy(struct l_dhcp_client *client)
{
if (unlikely(!client))
return;
if (client->state != DHCP_STATE_INIT)
l_dhcp_client_stop(client);
if (client->event_destroy)
client->event_destroy(client->event_data);
_dhcp_transport_free(client->transport);
l_free(client->ifname);
l_free(client->hostname);
l_free(client);
}
LIB_EXPORT bool l_dhcp_client_add_request_option(struct l_dhcp_client *client,
uint8_t option)
{
if (unlikely(!client))
return false;
if (unlikely(client->state != DHCP_STATE_INIT))
return false;
switch (option) {
case DHCP_OPTION_PAD:
case DHCP_OPTION_END:
case DHCP_OPTION_OVERLOAD:
case DHCP_OPTION_MESSAGE_TYPE:
case DHCP_OPTION_PARAMETER_REQUEST_LIST:
return false;
}
dhcp_enable_option(client, option);
return true;
}
LIB_EXPORT bool l_dhcp_client_set_address(struct l_dhcp_client *client,
uint8_t type,
const uint8_t *addr,
size_t addr_len)
{
if (unlikely(!client))
return false;
switch (type) {
case ARPHRD_ETHER:
if (addr_len != ETH_ALEN)
return false;
break;
default:
return false;
}
client->addr_len = addr_len;
memcpy(client->addr, addr, addr_len);
client->addr_type = type;
client->have_addr = true;
return true;
}
LIB_EXPORT bool l_dhcp_client_set_interface_name(struct l_dhcp_client *client,
const char *ifname)
{
if (unlikely(!client))
return false;
if (unlikely(client->state != DHCP_STATE_INIT))
return false;
l_free(client->ifname);
client->ifname = l_strdup(ifname);
return true;
}
LIB_EXPORT bool l_dhcp_client_set_hostname(struct l_dhcp_client *client,
const char *hostname)
{
if (unlikely(!client))
return false;
if (unlikely(client->state != DHCP_STATE_INIT))
return false;
if (!hostname)
goto done;
if (client->hostname && !strcmp(client->hostname, hostname))
return true;
done:
l_free(client->hostname);
client->hostname = l_strdup(hostname);
return true;
}
bool _dhcp_client_set_transport(struct l_dhcp_client *client,
struct dhcp_transport *transport)
{
if (unlikely(!client))
return false;
if (unlikely(client->state != DHCP_STATE_INIT))
return false;
if (client->transport)
_dhcp_transport_free(client->transport);
client->transport = transport;
return true;
}
struct dhcp_transport *_dhcp_client_get_transport(struct l_dhcp_client *client)
{
if (unlikely(!client))
return NULL;
return client->transport;
}
void _dhcp_client_override_xid(struct l_dhcp_client *client, uint32_t xid)
{
client->override_xid = true;
client->xid = xid;
}
LIB_EXPORT const struct l_dhcp_lease *l_dhcp_client_get_lease(
const struct l_dhcp_client *client)
{
if (unlikely(!client))
return NULL;
return client->lease;
}
LIB_EXPORT bool l_dhcp_client_start(struct l_dhcp_client *client)
{
int err;
if (unlikely(!client))
return false;
if (unlikely(client->state != DHCP_STATE_INIT))
return false;
if (!client->have_addr) {
uint8_t mac[6];
if (!l_net_get_mac_address(client->ifindex, mac))
return false;
l_dhcp_client_set_address(client, ARPHRD_ETHER, mac, 6);
}
if (!client->ifname) {
client->ifname = l_net_get_name(client->ifindex);
if (!client->ifname)
return false;
}
if (!client->transport) {
client->transport =
_dhcp_default_transport_new(client->ifindex,
client->ifname,
DHCP_PORT_CLIENT);
if (!client->transport)
return false;
}
if (!client->override_xid)
l_getrandom(&client->xid, sizeof(client->xid));
if (client->transport->open)
if (client->transport->open(client->transport,
client->xid) < 0)
return false;
_dhcp_transport_set_rx_callback(client->transport,
dhcp_client_rx_message,
client);
client->start_t = l_time_now();
err = dhcp_client_send_discover(client);
if (err < 0)
return false;
client->timeout_resend = l_timeout_create_ms(dhcp_fuzz_msecs(600),
dhcp_client_timeout_resend,
client, NULL);
CLIENT_ENTER_STATE(DHCP_STATE_SELECTING);
client->attempt = 1;
return true;
}
LIB_EXPORT bool l_dhcp_client_stop(struct l_dhcp_client *client)
{
if (unlikely(!client))
return false;
/*
* RFC 2131 Section 4.4.6
* "If the client no longer requires use of its assigned network address
* (e.g., the client is gracefully shut down), the client sends a
* DHCPRELEASE message to the server.""
*/
if (client->state == DHCP_STATE_BOUND ||
client->state == DHCP_STATE_RENEWING ||
client->state == DHCP_STATE_REBINDING)
dhcp_client_send_release(client);
if (client->rtnl_add_cmdid) {
l_netlink_cancel(client->rtnl, client->rtnl_add_cmdid);
client->rtnl_add_cmdid = 0;
}
if (client->rtnl_configured_address) {
l_rtnl_ifaddr_delete(client->rtnl, client->ifindex,
client->rtnl_configured_address,
NULL, NULL, NULL);
l_rtnl_address_free(client->rtnl_configured_address);
client->rtnl_configured_address = NULL;
}
l_timeout_remove(client->timeout_resend);
client->timeout_resend = NULL;
l_timeout_remove(client->timeout_lease);
client->timeout_lease = NULL;
if (client->transport && client->transport->close)
client->transport->close(client->transport);
client->start_t = 0;
CLIENT_ENTER_STATE(DHCP_STATE_INIT);
_dhcp_lease_free(client->lease);
client->lease = NULL;
if (client->acd) {
l_acd_destroy(client->acd);
client->acd = NULL;
}
return true;
}
LIB_EXPORT bool l_dhcp_client_set_event_handler(struct l_dhcp_client *client,
l_dhcp_client_event_cb_t handler,
void *userdata,
l_dhcp_destroy_cb_t destroy)
{
if (unlikely(!client))
return false;
if (client->event_destroy)
client->event_destroy(client->event_data);
client->event_handler = handler;
client->event_data = userdata;
client->event_destroy = destroy;
return true;
}
LIB_EXPORT bool l_dhcp_client_set_debug(struct l_dhcp_client *client,
l_dhcp_debug_cb_t function,
void *user_data,
l_dhcp_destroy_cb_t destroy,
int priority)
{
if (unlikely(!client))
return false;
if (client->debug_destroy)
client->debug_destroy(client->debug_data);
client->debug_handler = function;
client->debug_destroy = destroy;
client->debug_data = user_data;
client->debug_level = priority;
return true;
}
LIB_EXPORT bool l_dhcp_client_set_rtnl(struct l_dhcp_client *client,
struct l_netlink *rtnl)
{
if (unlikely(!client))
return false;
if (unlikely(client->state != DHCP_STATE_INIT))
return false;
client->rtnl = rtnl;
return true;
}
LIB_EXPORT bool l_dhcp_client_set_max_attempts(struct l_dhcp_client *client,
uint8_t attempts)
{
if (unlikely(!client))
return false;
if (unlikely(client->state != DHCP_STATE_INIT))
return false;
if (attempts < CLIENT_MIN_ATTEMPT_LIMIT ||
attempts > CLIENT_MAX_ATTEMPT_LIMIT)
return false;
client->max_attempts = attempts;
return true;
}
|