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
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* 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, 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (C) 2005 - 2010 Red Hat, Inc.
*
*/
#include <config.h>
#include <glib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <uuid/uuid.h>
#include "NetworkManagerUtils.h"
#include "nm-utils.h"
#include "nm-logging.h"
#include "nm-dbus-glib-types.h"
#include "nm-dhcp-client.h"
typedef struct {
char * iface;
GByteArray * hwaddr;
gboolean ipv6;
char * uuid;
guint priority;
guint32 timeout;
GByteArray * duid;
guchar state;
GPid pid;
gboolean dead;
guint timeout_id;
guint watch_id;
guint32 remove_id;
GHashTable * options;
gboolean info_only;
} NMDHCPClientPrivate;
#define NM_DHCP_CLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP_CLIENT, NMDHCPClientPrivate))
G_DEFINE_TYPE_EXTENDED (NMDHCPClient, nm_dhcp_client, G_TYPE_OBJECT, G_TYPE_FLAG_ABSTRACT, {})
enum {
SIGNAL_STATE_CHANGED,
SIGNAL_TIMEOUT,
SIGNAL_REMOVE,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
enum {
PROP_0,
PROP_IFACE,
PROP_HWADDR,
PROP_IPV6,
PROP_UUID,
PROP_PRIORITY,
PROP_TIMEOUT,
LAST_PROP
};
/********************************************/
GPid
nm_dhcp_client_get_pid (NMDHCPClient *self)
{
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), -1);
return NM_DHCP_CLIENT_GET_PRIVATE (self)->pid;
}
const char *
nm_dhcp_client_get_iface (NMDHCPClient *self)
{
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), NULL);
return NM_DHCP_CLIENT_GET_PRIVATE (self)->iface;
}
gboolean
nm_dhcp_client_get_ipv6 (NMDHCPClient *self)
{
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), FALSE);
return NM_DHCP_CLIENT_GET_PRIVATE (self)->ipv6;
}
const char *
nm_dhcp_client_get_uuid (NMDHCPClient *self)
{
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), NULL);
return NM_DHCP_CLIENT_GET_PRIVATE (self)->uuid;
}
/********************************************/
static void
timeout_cleanup (NMDHCPClient *self)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
if (priv->timeout_id) {
g_source_remove (priv->timeout_id);
priv->timeout_id = 0;
}
}
static void
watch_cleanup (NMDHCPClient *self)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
if (priv->watch_id) {
g_source_remove (priv->watch_id);
priv->watch_id = 0;
}
}
void
nm_dhcp_client_stop_pid (GPid pid, const char *iface)
{
int i = 5; /* roughly 0.5 seconds */
g_return_if_fail (pid > 0);
/* Tell it to quit; maybe it wants to send out a RELEASE message */
kill (pid, SIGTERM);
while (i-- > 0) {
gint child_status;
int ret;
ret = waitpid (pid, &child_status, WNOHANG);
if (ret > 0)
break;
if (ret == -1) {
/* Child already exited */
if (errno == ECHILD) {
/* Was it really our child and it exited? */
if (kill (pid, 0) < 0 && errno == ESRCH)
break;
} else {
/* Took too long; shoot it in the head */
i = 0;
break;
}
}
g_usleep (G_USEC_PER_SEC / 10);
}
if (i <= 0) {
if (iface) {
nm_log_warn (LOGD_DHCP, "(%s): DHCP client pid %d didn't exit, will kill it.",
iface, pid);
}
kill (pid, SIGKILL);
nm_log_dbg (LOGD_DHCP, "waiting for DHCP client pid %d to exit", pid);
waitpid (pid, NULL, 0);
nm_log_dbg (LOGD_DHCP, "DHCP client pid %d cleaned up", pid);
}
}
static void
stop (NMDHCPClient *self, gboolean release, const GByteArray *duid)
{
NMDHCPClientPrivate *priv;
g_return_if_fail (NM_IS_DHCP_CLIENT (self));
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
g_return_if_fail (priv->pid > 0);
/* Clean up the watch handler since we're explicitly killing the daemon */
watch_cleanup (self);
nm_dhcp_client_stop_pid (priv->pid, priv->iface);
priv->info_only = FALSE;
}
static gboolean
daemon_timeout (gpointer user_data)
{
NMDHCPClient *self = NM_DHCP_CLIENT (user_data);
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
if (priv->ipv6) {
nm_log_warn (LOGD_DHCP6, "(%s): DHCPv6 request timed out.", priv->iface);
} else {
nm_log_warn (LOGD_DHCP4, "(%s): DHCPv4 request timed out.", priv->iface);
}
g_signal_emit (G_OBJECT (self), signals[SIGNAL_TIMEOUT], 0);
return FALSE;
}
static gboolean
signal_remove (gpointer user_data)
{
NMDHCPClient *self = NM_DHCP_CLIENT (user_data);
NM_DHCP_CLIENT_GET_PRIVATE (self)->remove_id = 0;
g_signal_emit (G_OBJECT (self), signals[SIGNAL_REMOVE], 0);
return FALSE;
}
static void
dhcp_client_set_state (NMDHCPClient *self,
NMDHCPState state,
gboolean emit_state,
gboolean remove_now)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
priv->state = state;
if (emit_state)
g_signal_emit (G_OBJECT (self), signals[SIGNAL_STATE_CHANGED], 0, priv->state);
if (state == DHC_END || state == DHC_ABEND) {
/* Start the remove signal timer */
if (remove_now) {
g_signal_emit (G_OBJECT (self), signals[SIGNAL_REMOVE], 0);
} else {
if (!priv->remove_id)
priv->remove_id = g_timeout_add_seconds (5, signal_remove, self);
}
}
}
static void
daemon_watch_cb (GPid pid, gint status, gpointer user_data)
{
NMDHCPClient *self = NM_DHCP_CLIENT (user_data);
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
NMDHCPState new_state;
guint64 log_domain;
guint ip_ver;
log_domain = priv->ipv6 ? LOGD_DHCP6 : LOGD_DHCP4;
ip_ver = priv->ipv6 ? 6 : 4;
if (WIFEXITED (status))
nm_log_info (log_domain, "(%s): DHCPv%d client pid %d exited with status %d",
priv->iface, ip_ver, pid, WEXITSTATUS (status));
else if (WIFSIGNALED (status))
nm_log_info (log_domain, "(%s): DHCPv%d client pid %d killed by signal %d",
priv->iface, ip_ver, pid, WTERMSIG (status));
else if (WIFSTOPPED(status))
nm_log_info (log_domain, "(%s): DHCPv%d client pid %d stopped by signal %d",
priv->iface, ip_ver, pid, WSTOPSIG (status));
else if (WIFCONTINUED (status))
nm_log_info (log_domain, "(%s): DHCPv%d client pid %d resumed (by SIGCONT)",
priv->iface, ip_ver, pid);
else
nm_log_warn (LOGD_DHCP, "DHCP client died abnormally");
if (!WIFEXITED (status))
new_state = DHC_ABEND;
else
new_state = DHC_END;
watch_cleanup (self);
timeout_cleanup (self);
priv->dead = TRUE;
dhcp_client_set_state (self, new_state, TRUE, FALSE);
}
static void
start_monitor (NMDHCPClient *self)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
g_return_if_fail (priv->pid > 0);
/* Set up a timeout on the transaction to kill it after the timeout */
priv->timeout_id = g_timeout_add_seconds (priv->timeout,
daemon_timeout,
self);
priv->watch_id = g_child_watch_add (priv->pid,
(GChildWatchFunc) daemon_watch_cb,
self);
}
gboolean
nm_dhcp_client_start_ip4 (NMDHCPClient *self,
const char *dhcp_client_id,
GByteArray *dhcp_anycast_addr,
const char *hostname)
{
NMDHCPClientPrivate *priv;
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), FALSE);
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
g_return_val_if_fail (priv->pid == -1, FALSE);
g_return_val_if_fail (priv->ipv6 == FALSE, FALSE);
g_return_val_if_fail (priv->uuid != NULL, FALSE);
nm_log_info (LOGD_DHCP, "Activation (%s) Beginning DHCPv4 transaction (timeout in %d seconds)",
priv->iface, priv->timeout);
priv->pid = NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, dhcp_client_id, dhcp_anycast_addr, hostname);
if (priv->pid)
start_monitor (self);
return priv->pid ? TRUE : FALSE;
}
/* uuid_parse does not work for machine-id, so we use our own converter */
static gboolean
machine_id_parse (const char *in, uuid_t uu)
{
const char *cp;
int i;
char buf[3];
g_return_val_if_fail (in != NULL, FALSE);
g_return_val_if_fail (strlen (in) == 32, FALSE);
for (i = 0; i < 32; i++) {
if (!g_ascii_isxdigit (in[i]))
return FALSE;
}
buf[2] = 0;
cp = in;
for (i = 0; i < 16; i++) {
buf[0] = *cp++;
buf[1] = *cp++;
uu[i] = ((unsigned char) strtoul (buf, NULL, 16)) & 0xFF;
}
return TRUE;
}
static GByteArray *
generate_duid_from_machine_id (void)
{
GByteArray *duid;
char *contents = NULL;
GChecksum *sum;
guint8 buffer[32]; /* SHA256 digest size */
gsize sumlen = sizeof (buffer);
const guint16 duid_type = g_htons (4);
uuid_t uuid;
GRand *generator;
guint i;
gboolean success = FALSE;
/* Get the machine ID from /etc/machine-id; it's always in /etc no matter
* where our configured SYSCONFDIR is. Alternatively, it might be in
* LOCALSTATEDIR /lib/dbus/machine-id.
*/
if ( g_file_get_contents ("/etc/machine-id", &contents, NULL, NULL)
|| g_file_get_contents (LOCALSTATEDIR "/lib/dbus/machine-id", &contents, NULL, NULL)) {
contents = g_strstrip (contents);
success = machine_id_parse (contents, uuid);
if (success) {
/* Hash the machine ID so it's not leaked to the network */
sum = g_checksum_new (G_CHECKSUM_SHA256);
g_checksum_update (sum, (const guchar *) &uuid, sizeof (uuid));
g_checksum_get_digest (sum, buffer, &sumlen);
g_checksum_free (sum);
}
g_free (contents);
}
if (!success) {
nm_log_warn (LOGD_DHCP6, "Failed to read " SYSCONFDIR "/machine-id "
"or " LOCALSTATEDIR "/lib/dbus/machine-id to generate "
"DHCPv6 DUID; creating non-persistent random DUID.");
generator = g_rand_new ();
for (i = 0; i < sizeof (buffer) / sizeof (guint32); i++)
((guint32 *) buffer)[i] = g_rand_int (generator);
g_rand_free (generator);
}
/* Generate a DHCP Unique Identifier for DHCPv6 using the
* DUID-UUID method (see RFC 6355 section 4). Format is:
*
* u16: type (DUID-UUID = 4)
* u8[16]: UUID bytes
*/
duid = g_byte_array_sized_new (18);
g_byte_array_append (duid, (guint8 *) &duid_type, sizeof (duid_type));
/* Since SHA256 is 256 bits, but UUID is 128 bits, we just take the first
* 128 bits of the SHA256 as the DUID-UUID.
*/
g_byte_array_append (duid, buffer, 16);
return duid;
}
static char *
escape_duid (const GByteArray *duid)
{
guint32 i = 0;
GString *s;
g_return_val_if_fail (duid != NULL, NULL);
s = g_string_sized_new (40);
while (i < duid->len) {
if (s->len)
g_string_append_c (s, ':');
g_string_append_printf (s, "%02x", duid->data[i++]);
}
return g_string_free (s, FALSE);
}
static GByteArray *
get_duid (NMDHCPClient *self)
{
static GByteArray *duid = NULL;
GByteArray *copy = NULL;
char *escaped;
if (G_UNLIKELY (duid == NULL)) {
duid = generate_duid_from_machine_id ();
g_assert (duid);
if (nm_logging_enabled (LOGL_DEBUG, LOGD_DHCP6)) {
escaped = escape_duid (duid);
nm_log_dbg (LOGD_DHCP6, "Generated DUID %s", escaped);
g_free (escaped);
}
}
if (G_LIKELY (duid)) {
copy = g_byte_array_sized_new (duid->len);
g_byte_array_append (copy, duid->data, duid->len);
}
return copy;
}
gboolean
nm_dhcp_client_start_ip6 (NMDHCPClient *self,
GByteArray *dhcp_anycast_addr,
const char *hostname,
gboolean info_only)
{
NMDHCPClientPrivate *priv;
char *escaped;
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), FALSE);
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
g_return_val_if_fail (priv->pid == -1, FALSE);
g_return_val_if_fail (priv->ipv6 == TRUE, FALSE);
g_return_val_if_fail (priv->uuid != NULL, FALSE);
/* If we don't have one yet, read the default DUID for this DHCPv6 client
* from the client-specific persistent configuration.
*/
if (!priv->duid)
priv->duid = NM_DHCP_CLIENT_GET_CLASS (self)->get_duid (self);
if (nm_logging_enabled (LOGL_DEBUG, LOGD_DHCP)) {
escaped = escape_duid (priv->duid);
nm_log_dbg (LOGD_DHCP, "(%s): DHCPv6 DUID is '%s'", priv->iface, escaped);
g_free (escaped);
}
priv->info_only = info_only;
nm_log_info (LOGD_DHCP, "Activation (%s) Beginning DHCPv6 transaction (timeout in %d seconds)",
priv->iface, priv->timeout);
priv->pid = NM_DHCP_CLIENT_GET_CLASS (self)->ip6_start (self,
dhcp_anycast_addr,
hostname,
info_only,
priv->duid);
if (priv->pid > 0)
start_monitor (self);
return priv->pid ? TRUE : FALSE;
}
void
nm_dhcp_client_stop_existing (const char *pid_file, const char *binary_name)
{
char *pid_contents = NULL, *proc_contents = NULL, *proc_path = NULL;
long int tmp;
/* Check for an existing instance and stop it */
if (!g_file_get_contents (pid_file, &pid_contents, NULL, NULL))
return;
errno = 0;
tmp = strtol (pid_contents, NULL, 10);
if ((errno == 0) && (tmp > 1)) {
const char *exe;
/* Ensure the process is a DHCP client */
proc_path = g_strdup_printf ("/proc/%ld/cmdline", tmp);
if (g_file_get_contents (proc_path, &proc_contents, NULL, NULL)) {
exe = strrchr (proc_contents, '/');
if (exe)
exe++;
else
exe = proc_contents;
if (!strcmp (exe, binary_name))
nm_dhcp_client_stop_pid ((GPid) tmp, NULL);
}
}
if (remove (pid_file) == -1)
nm_log_dbg (LOGD_DHCP, "Could not remove dhcp pid file \"%s\": %d (%s)", pid_file, errno, g_strerror (errno));
g_free (proc_path);
g_free (pid_contents);
g_free (proc_contents);
}
void
nm_dhcp_client_stop (NMDHCPClient *self, gboolean release)
{
NMDHCPClientPrivate *priv;
g_return_if_fail (NM_IS_DHCP_CLIENT (self));
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
/* Kill the DHCP client */
if (!priv->dead) {
NM_DHCP_CLIENT_GET_CLASS (self)->stop (self, release, priv->duid);
priv->dead = TRUE;
nm_log_info (LOGD_DHCP, "(%s): canceled DHCP transaction, DHCP client pid %d",
priv->iface, priv->pid);
}
/* And clean stuff up */
priv->pid = -1;
dhcp_client_set_state (self, DHC_END, FALSE, TRUE);
g_hash_table_remove_all (priv->options);
timeout_cleanup (self);
watch_cleanup (self);
}
/********************************************/
static gboolean
state_is_bound (guint32 state)
{
if ( (state == DHC_BOUND4)
|| (state == DHC_BOUND6)
|| (state == DHC_RENEW4)
|| (state == DHC_RENEW6)
|| (state == DHC_REBOOT)
|| (state == DHC_REBIND4)
|| (state == DHC_REBIND6)
|| (state == DHC_IPV4LL))
return TRUE;
return FALSE;
}
static const char *state_table[] = {
[DHC_NBI] = "nbi",
[DHC_PREINIT] = "preinit",
[DHC_PREINIT6] = "preinit6",
[DHC_BOUND4] = "bound",
[DHC_BOUND6] = "bound6",
[DHC_IPV4LL] = "ipv4ll",
[DHC_RENEW4] = "renew",
[DHC_RENEW6] = "renew6",
[DHC_REBOOT] = "reboot",
[DHC_REBIND4] = "rebind",
[DHC_REBIND6] = "rebind6",
[DHC_DEPREF6] = "depref6",
[DHC_STOP] = "stop",
[DHC_STOP6] = "stop6",
[DHC_MEDIUM] = "medium",
[DHC_TIMEOUT] = "timeout",
[DHC_FAIL] = "fail",
[DHC_EXPIRE] = "expire",
[DHC_EXPIRE6] = "expire6",
[DHC_RELEASE] = "release",
[DHC_RELEASE6] = "release6",
[DHC_START] = "start",
[DHC_ABEND] = "abend",
[DHC_END] = "end",
};
static const char *
state_to_string (NMDHCPState state)
{
if (state >= 0 && state < G_N_ELEMENTS (state_table))
return state_table[state];
return NULL;
}
static NMDHCPState
string_to_state (const char *name)
{
int i;
if (name) {
for (i = 0; i < G_N_ELEMENTS (state_table); i++) {
const char *n = state_table[i];
if (n && !strcasecmp (name, n))
return i;
}
}
return 255;
}
static char *
garray_to_string (GArray *array, const char *key)
{
GString *str;
int i;
unsigned char c;
char *converted = NULL;
g_return_val_if_fail (array != NULL, NULL);
/* Since the DHCP options come through environment variables, they should
* already be UTF-8 safe, but just make sure.
*/
str = g_string_sized_new (array->len);
for (i = 0; i < array->len; i++) {
c = array->data[i];
/* Convert NULLs to spaces and non-ASCII characters to ? */
if (c == '\0')
c = ' ';
else if (c > 127)
c = '?';
str = g_string_append_c (str, c);
}
str = g_string_append_c (str, '\0');
converted = str->str;
if (!g_utf8_validate (converted, -1, NULL))
nm_log_warn (LOGD_DHCP, "DHCP option '%s' couldn't be converted to UTF-8", key);
g_string_free (str, FALSE);
return converted;
}
static void
copy_option (gpointer key,
gpointer value,
gpointer user_data)
{
GHashTable *hash = user_data;
const char *str_key = (const char *) key;
char *str_value = NULL;
if (G_VALUE_TYPE (value) != DBUS_TYPE_G_UCHAR_ARRAY) {
nm_log_warn (LOGD_DHCP, "unexpected key %s value type was not "
"DBUS_TYPE_G_UCHAR_ARRAY",
str_key);
return;
}
str_value = garray_to_string ((GArray *) g_value_get_boxed (value), str_key);
if (str_value)
g_hash_table_insert (hash, g_strdup (str_key), str_value);
}
void
nm_dhcp_client_new_options (NMDHCPClient *self,
GHashTable *options,
const char *reason)
{
NMDHCPClientPrivate *priv;
guint32 old_state;
guint32 new_state;
g_return_if_fail (NM_IS_DHCP_CLIENT (self));
g_return_if_fail (options != NULL);
g_return_if_fail (reason != NULL);
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
old_state = priv->state;
new_state = string_to_state (reason);
/* Clear old and save new DHCP options */
g_hash_table_remove_all (priv->options);
g_hash_table_foreach (options, copy_option, priv->options);
if (old_state == new_state) {
/* dhclient will stay in the same state (or, really, provide the same
* reason) for operations like RENEW and REBIND. We need to ensure
* that triggers various DHCP lease change code, so we need to pass
* along same-state transitions for these states.
*/
if ( new_state != DHC_BOUND4
&& new_state != DHC_RENEW4
&& new_state != DHC_REBIND4
&& new_state != DHC_BOUND6
&& new_state != DHC_RENEW6
&& new_state != DHC_REBIND6)
return;
}
/* Handle changed device state */
if (state_is_bound (new_state)) {
/* Cancel the timeout if the DHCP client is now bound */
timeout_cleanup (self);
}
if (priv->ipv6) {
nm_log_info (LOGD_DHCP6, "(%s): DHCPv6 state changed %s -> %s",
priv->iface,
state_to_string (old_state),
state_to_string (new_state));
} else {
nm_log_info (LOGD_DHCP4, "(%s): DHCPv4 state changed %s -> %s",
priv->iface,
state_to_string (old_state),
state_to_string (new_state));
}
dhcp_client_set_state (self, new_state, TRUE, FALSE);
}
#define NEW_TAG "new_"
#define OLD_TAG "old_"
gboolean
nm_dhcp_client_foreach_option (NMDHCPClient *self,
GHFunc func,
gpointer user_data)
{
NMDHCPClientPrivate *priv;
GHashTableIter iter;
gpointer iterkey, itervalue;
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), FALSE);
g_return_val_if_fail (func != NULL, FALSE);
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
if (!state_is_bound (priv->state)) {
if (priv->ipv6) {
nm_log_warn (LOGD_DHCP6, "(%s): DHCPv6 client didn't bind to a lease.", priv->iface);
} else {
nm_log_warn (LOGD_DHCP4, "(%s): DHCPv4 client didn't bind to a lease.", priv->iface);
}
}
g_hash_table_iter_init (&iter, priv->options);
while (g_hash_table_iter_next (&iter, &iterkey, &itervalue)) {
const char *key = iterkey, *value = itervalue;
const char **p;
static const char *filter_options[] = {
"interface", "pid", "reason", "dhcp_message_type", NULL
};
gboolean ignore = FALSE;
/* Filter out stuff that's not actually new DHCP options */
for (p = filter_options; *p; p++) {
if (!strcmp (*p, key) || !strncmp (key, OLD_TAG, strlen (OLD_TAG))) {
ignore = TRUE;
break;
}
}
if (!ignore) {
const char *tmp_key = key;
/* Remove the "new_" prefix that dhclient passes back */
if (!strncmp (key, NEW_TAG, strlen (NEW_TAG)))
tmp_key = key + strlen (NEW_TAG);
func ((gpointer) tmp_key, (gpointer) value, user_data);
}
}
return TRUE;
}
/********************************************/
static gboolean
ip4_process_dhcpcd_rfc3442_routes (NMDHCPClient *self,
const char *str,
NMIP4Config *ip4_config,
guint32 *gwaddr)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
char **routes, **r;
gboolean have_routes = FALSE;
routes = g_strsplit (str, " ", 0);
if (g_strv_length (routes) == 0)
goto out;
if ((g_strv_length (routes) % 2) != 0) {
nm_log_warn (LOGD_DHCP4, " classless static routes provided, but invalid");
goto out;
}
for (r = routes; *r; r += 2) {
char *slash;
NMPlatformIP4Route route;
int rt_cidr = 32;
guint32 rt_addr, rt_route;
slash = strchr(*r, '/');
if (slash) {
*slash = '\0';
errno = 0;
rt_cidr = strtol (slash + 1, NULL, 10);
if ((errno == EINVAL) || (errno == ERANGE)) {
nm_log_warn (LOGD_DHCP4, "DHCP provided invalid classless static route cidr: '%s'", slash + 1);
continue;
}
}
if (inet_pton (AF_INET, *r, &rt_addr) <= 0) {
nm_log_warn (LOGD_DHCP4, "DHCP provided invalid classless static route address: '%s'", *r);
continue;
}
if (inet_pton (AF_INET, *(r + 1), &rt_route) <= 0) {
nm_log_warn (LOGD_DHCP4, "DHCP provided invalid classless static route gateway: '%s'", *(r + 1));
continue;
}
have_routes = TRUE;
if (rt_cidr == 0 && rt_addr == 0) {
/* FIXME: how to handle multiple routers? */
*gwaddr = rt_route;
} else {
nm_log_info (LOGD_DHCP4, " classless static route %s/%d gw %s", *r, rt_cidr, *(r + 1));
memset (&route, 0, sizeof (route));
route.network = rt_addr;
route.plen = rt_cidr;
route.gateway = rt_route;
route.source = NM_PLATFORM_SOURCE_DHCP;
route.metric = priv->priority;
nm_ip4_config_add_route (ip4_config, &route);
}
}
out:
g_strfreev (routes);
return have_routes;
}
static const char **
process_dhclient_rfc3442_route (const char **octets, NMPlatformIP4Route *route, gboolean *success)
{
const char **o = octets;
int addr_len = 0, i = 0;
long int tmp;
char *next_hop;
guint32 tmp_addr;
*success = FALSE;
if (!*o)
return o; /* no prefix */
tmp = strtol (*o, NULL, 10);
if (tmp < 0 || tmp > 32) /* 32 == max IP4 prefix length */
return o;
memset (route, 0, sizeof (*route));
route->plen = tmp;
o++;
if (tmp > 0)
addr_len = ((tmp - 1) / 8) + 1;
/* ensure there's at least the address + next hop left */
if (g_strv_length ((char **) o) < addr_len + 4)
goto error;
if (tmp) {
const char *addr[4] = { "0", "0", "0", "0" };
char *str_addr;
for (i = 0; i < addr_len; i++)
addr[i] = *o++;
str_addr = g_strjoin (".", addr[0], addr[1], addr[2], addr[3], NULL);
if (inet_pton (AF_INET, str_addr, &tmp_addr) <= 0) {
g_free (str_addr);
goto error;
}
tmp_addr &= nm_utils_ip4_prefix_to_netmask ((guint32) tmp);
route->network = tmp_addr;
}
/* Handle next hop */
next_hop = g_strjoin (".", o[0], o[1], o[2], o[3], NULL);
if (inet_pton (AF_INET, next_hop, &tmp_addr) <= 0) {
g_free (next_hop);
goto error;
}
route->gateway = tmp_addr;
g_free (next_hop);
*success = TRUE;
return o + 4; /* advance to past the next hop */
error:
return o;
}
static gboolean
ip4_process_dhclient_rfc3442_routes (NMDHCPClient *self,
const char *str,
NMIP4Config *ip4_config,
guint32 *gwaddr)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
char **octets, **o;
gboolean have_routes = FALSE;
NMPlatformIP4Route route;
gboolean success;
o = octets = g_strsplit_set (str, " .", 0);
if (g_strv_length (octets) < 5) {
nm_log_warn (LOGD_DHCP4, "ignoring invalid classless static routes '%s'", str);
goto out;
}
while (*o) {
memset (&route, 0, sizeof (route));
o = (char **) process_dhclient_rfc3442_route ((const char **) o, &route, &success);
if (!success) {
nm_log_warn (LOGD_DHCP4, "ignoring invalid classless static routes");
break;
}
have_routes = TRUE;
if (!route.plen) {
/* gateway passed as classless static route */
*gwaddr = route.gateway;
} else {
char addr[INET_ADDRSTRLEN];
/* normal route */
route.source = NM_PLATFORM_SOURCE_DHCP;
route.metric = priv->priority;
nm_ip4_config_add_route (ip4_config, &route);
nm_log_info (LOGD_DHCP4, " classless static route %s/%d gw %s",
nm_utils_inet4_ntop (route.network, addr), route.plen,
nm_utils_inet4_ntop (route.gateway, NULL));
}
}
out:
g_strfreev (octets);
return have_routes;
}
static gboolean
ip4_process_classless_routes (NMDHCPClient *self,
GHashTable *options,
NMIP4Config *ip4_config,
guint32 *gwaddr)
{
const char *str, *p;
g_return_val_if_fail (options != NULL, FALSE);
g_return_val_if_fail (ip4_config != NULL, FALSE);
*gwaddr = 0;
/* dhcpd/dhclient in Fedora has support for rfc3442 implemented using a
* slightly different format:
*
* option classless-static-routes = array of (destination-descriptor ip-address);
*
* which results in:
*
* 0 192.168.0.113 25.129.210.177.132 192.168.0.113 7.2 10.34.255.6
*
* dhcpcd supports classless static routes natively and uses this same
* option identifier with the following format:
*
* 192.168.10.0/24 192.168.1.1 10.0.0.0/8 10.17.66.41
*/
str = g_hash_table_lookup (options, "new_classless_static_routes");
/* dhclient doesn't have actual support for rfc3442 classless static routes
* upstream. Thus, people resort to defining the option in dhclient.conf
* and using arbitrary formats like so:
*
* option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;
*
* See https://lists.isc.org/pipermail/dhcp-users/2008-December/007629.html
*/
if (!str)
str = g_hash_table_lookup (options, "new_rfc3442_classless_static_routes");
/* Microsoft version; same as rfc3442 but with a different option # (249) */
if (!str)
str = g_hash_table_lookup (options, "new_ms_classless_static_routes");
if (!str || !strlen (str))
return FALSE;
p = str;
while (*p) {
if (!g_ascii_isdigit (*p) && (*p != ' ') && (*p != '.') && (*p != '/')) {
nm_log_warn (LOGD_DHCP4, "ignoring invalid classless static routes '%s'", str);
return FALSE;
}
p++;
};
if (strchr (str, '/')) {
/* dhcpcd format */
return ip4_process_dhcpcd_rfc3442_routes (self, str, ip4_config, gwaddr);
}
return ip4_process_dhclient_rfc3442_routes (self, str, ip4_config, gwaddr);
}
static void
process_classful_routes (NMDHCPClient *self, GHashTable *options, NMIP4Config *ip4_config)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
const char *str;
char **searches, **s;
str = g_hash_table_lookup (options, "new_static_routes");
if (!str)
return;
searches = g_strsplit (str, " ", 0);
if ((g_strv_length (searches) % 2)) {
nm_log_info (LOGD_DHCP, " static routes provided, but invalid");
goto out;
}
for (s = searches; *s; s += 2) {
NMPlatformIP4Route route;
guint32 rt_addr, rt_route;
if (inet_pton (AF_INET, *s, &rt_addr) <= 0) {
nm_log_warn (LOGD_DHCP, "DHCP provided invalid static route address: '%s'", *s);
continue;
}
if (inet_pton (AF_INET, *(s + 1), &rt_route) <= 0) {
nm_log_warn (LOGD_DHCP, "DHCP provided invalid static route gateway: '%s'", *(s + 1));
continue;
}
// FIXME: ensure the IP address and route are sane
memset (&route, 0, sizeof (route));
route.network = rt_addr;
/* RFC 2132, updated by RFC 3442:
The Static Routes option (option 33) does not provide a subnet mask
for each route - it is assumed that the subnet mask is implicit in
whatever network number is specified in each route entry */
route.plen = nm_utils_ip4_get_default_prefix (rt_addr);
if (rt_addr & ~nm_utils_ip4_prefix_to_netmask (route.plen)) {
/* RFC 943: target not "this network"; using host routing */
route.plen = 32;
}
route.gateway = rt_route;
route.source = NM_PLATFORM_SOURCE_DHCP;
route.metric = priv->priority;
nm_ip4_config_add_route (ip4_config, &route);
nm_log_info (LOGD_DHCP, " static route %s",
nm_platform_ip4_route_to_string (&route));
}
out:
g_strfreev (searches);
}
static void
process_domain_search (const char *str, GFunc add_func, gpointer user_data)
{
char **searches, **s;
char *unescaped, *p;
int i;
g_return_if_fail (str != NULL);
g_return_if_fail (add_func != NULL);
p = unescaped = g_strdup (str);
do {
p = strstr (p, "\\032");
if (!p)
break;
/* Clear the escaped space with real spaces */
for (i = 0; i < 4; i++)
*p++ = ' ';
} while (*p++);
if (strchr (unescaped, '\\')) {
nm_log_warn (LOGD_DHCP, " invalid domain search: '%s'", unescaped);
goto out;
}
searches = g_strsplit (unescaped, " ", 0);
for (s = searches; *s; s++) {
if (strlen (*s)) {
nm_log_info (LOGD_DHCP, " domain search '%s'", *s);
add_func (*s, user_data);
}
}
g_strfreev (searches);
out:
g_free (unescaped);
}
static void
ip4_add_domain_search (gpointer data, gpointer user_data)
{
nm_ip4_config_add_search (NM_IP4_CONFIG (user_data), (const char *) data);
}
/* Given a table of DHCP options from the client, convert into an IP4Config */
static NMIP4Config *
ip4_options_to_config (NMDHCPClient *self)
{
NMDHCPClientPrivate *priv;
NMIP4Config *ip4_config = NULL;
guint32 tmp_addr;
NMPlatformIP4Address address;
char *str = NULL;
guint32 gwaddr = 0, plen = 0;
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), NULL);
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
g_return_val_if_fail (priv->options != NULL, NULL);
ip4_config = nm_ip4_config_new ();
memset (&address, 0, sizeof (address));
address.timestamp = nm_utils_get_monotonic_timestamp_s ();
str = g_hash_table_lookup (priv->options, "new_ip_address");
if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) {
address.address = tmp_addr;
nm_log_info (LOGD_DHCP4, " address %s", str);
} else
goto error;
str = g_hash_table_lookup (priv->options, "new_subnet_mask");
if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) {
plen = nm_utils_ip4_netmask_to_prefix (tmp_addr);
nm_log_info (LOGD_DHCP4, " plen %d (%s)", plen, str);
} else {
/* Get default netmask for the IP according to appropriate class. */
plen = nm_utils_ip4_get_default_prefix (address.address);
nm_log_info (LOGD_DHCP4, " plen %d (default)", plen);
}
address.plen = plen;
/* Routes: if the server returns classless static routes, we MUST ignore
* the 'static_routes' option.
*/
if (!ip4_process_classless_routes (self, priv->options, ip4_config, &gwaddr))
process_classful_routes (self, priv->options, ip4_config);
if (gwaddr) {
nm_log_info (LOGD_DHCP4, " gateway %s", nm_utils_inet4_ntop (gwaddr, NULL));
nm_ip4_config_set_gateway (ip4_config, gwaddr);
} else {
/* If the gateway wasn't provided as a classless static route with a
* subnet length of 0, try to find it using the old-style 'routers' option.
*/
str = g_hash_table_lookup (priv->options, "new_routers");
if (str) {
char **routers = g_strsplit (str, " ", 0);
char **s;
for (s = routers; *s; s++) {
/* FIXME: how to handle multiple routers? */
if (inet_pton (AF_INET, *s, &gwaddr) > 0) {
nm_ip4_config_set_gateway (ip4_config, gwaddr);
nm_log_info (LOGD_DHCP4, " gateway %s", *s);
break;
} else
nm_log_warn (LOGD_DHCP4, "ignoring invalid gateway '%s'", *s);
}
g_strfreev (routers);
}
}
/*
* RFC 2132, section 9.7
* DHCP clients use the contents of the 'server identifier' field
* as the destination address for any DHCP messages unicast to
* the DHCP server.
*
* Some ISP's provide leases from central servers that are on
* different subnets that the address offered. If the host
* does not configure the interface as the default route, the
* dhcp server may not be reachable via unicast, and a host
* specific route is needed.
**/
str = g_hash_table_lookup (priv->options, "new_dhcp_server_identifier");
if (str) {
if (inet_pton (AF_INET, str, &tmp_addr) > 0) {
NMPlatformIP4Route route;
guint32 mask = nm_utils_ip4_prefix_to_netmask (address.plen);
nm_log_info (LOGD_DHCP4, " server identifier %s", str);
if ((tmp_addr & mask) != (address.address & mask)) {
/* DHCP server not on assigned subnet, route needed */
memset (&route, 0, sizeof (route));
route.network = tmp_addr;
route.plen = 32;
/* this will be a device route if gwaddr is 0 */
route.gateway = gwaddr;
route.source = NM_PLATFORM_SOURCE_DHCP;
route.metric = priv->priority;
nm_ip4_config_add_route (ip4_config, &route);
nm_log_dbg (LOGD_IP, "adding route for server identifier: %s",
nm_platform_ip4_route_to_string (&route));
}
}
else
nm_log_warn (LOGD_DHCP4, "ignoring invalid server identifier '%s'", str);
}
str = g_hash_table_lookup (priv->options, "new_dhcp_lease_time");
if (str) {
address.lifetime = address.preferred = strtoul (str, NULL, 10);
nm_log_info (LOGD_DHCP4, " lease time %d", address.lifetime);
}
address.source = NM_PLATFORM_SOURCE_DHCP;
nm_ip4_config_add_address (ip4_config, &address);
str = g_hash_table_lookup (priv->options, "new_host_name");
if (str)
nm_log_info (LOGD_DHCP4, " hostname '%s'", str);
str = g_hash_table_lookup (priv->options, "new_domain_name_servers");
if (str) {
char **searches = g_strsplit (str, " ", 0);
char **s;
for (s = searches; *s; s++) {
if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
nm_ip4_config_add_nameserver (ip4_config, tmp_addr);
nm_log_info (LOGD_DHCP4, " nameserver '%s'", *s);
} else
nm_log_warn (LOGD_DHCP4, "ignoring invalid nameserver '%s'", *s);
}
g_strfreev (searches);
}
str = g_hash_table_lookup (priv->options, "new_domain_name");
if (str) {
char **domains = g_strsplit (str, " ", 0);
char **s;
for (s = domains; *s; s++) {
nm_log_info (LOGD_DHCP4, " domain name '%s'", *s);
nm_ip4_config_add_domain (ip4_config, *s);
}
g_strfreev (domains);
}
str = g_hash_table_lookup (priv->options, "new_domain_search");
if (str)
process_domain_search (str, ip4_add_domain_search, ip4_config);
str = g_hash_table_lookup (priv->options, "new_netbios_name_servers");
if (str) {
char **searches = g_strsplit (str, " ", 0);
char **s;
for (s = searches; *s; s++) {
if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
nm_ip4_config_add_wins (ip4_config, tmp_addr);
nm_log_info (LOGD_DHCP4, " wins '%s'", *s);
} else
nm_log_warn (LOGD_DHCP4, "ignoring invalid WINS server '%s'", *s);
}
g_strfreev (searches);
}
str = g_hash_table_lookup (priv->options, "new_interface_mtu");
if (str) {
int int_mtu;
errno = 0;
int_mtu = strtol (str, NULL, 10);
if ((errno == EINVAL) || (errno == ERANGE))
goto error;
if (int_mtu > 576)
nm_ip4_config_set_mtu (ip4_config, int_mtu);
}
str = g_hash_table_lookup (priv->options, "new_nis_domain");
if (str) {
nm_log_info (LOGD_DHCP4, " NIS domain '%s'", str);
nm_ip4_config_set_nis_domain (ip4_config, str);
}
str = g_hash_table_lookup (priv->options, "new_nis_servers");
if (str) {
char **searches = g_strsplit (str, " ", 0);
char **s;
for (s = searches; *s; s++) {
if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
nm_ip4_config_add_nis_server (ip4_config, tmp_addr);
nm_log_info (LOGD_DHCP4, " nis '%s'", *s);
} else
nm_log_warn (LOGD_DHCP4, "ignoring invalid NIS server '%s'", *s);
}
g_strfreev (searches);
}
return ip4_config;
error:
g_object_unref (ip4_config);
return NULL;
}
NMIP4Config *
nm_dhcp_client_get_ip4_config (NMDHCPClient *self, gboolean test)
{
NMDHCPClientPrivate *priv;
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), NULL);
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
if (test && !state_is_bound (priv->state)) {
nm_log_warn (LOGD_DHCP4, "(%s): DHCPv4 client didn't bind to a lease.", priv->iface);
return NULL;
}
if (!g_hash_table_size (priv->options)) {
/* We never got a response from the DHCP client */
return NULL;
}
return ip4_options_to_config (self);
}
/********************************************/
static void
ip6_add_domain_search (gpointer data, gpointer user_data)
{
nm_ip6_config_add_search (NM_IP6_CONFIG (user_data), (const char *) data);
}
/* Given a table of DHCP options from the client, convert into an IP6Config */
static NMIP6Config *
ip6_options_to_config (NMDHCPClient *self)
{
NMDHCPClientPrivate *priv;
NMIP6Config *ip6_config = NULL;
struct in6_addr tmp_addr;
NMPlatformIP6Address address;
char *str = NULL;
GHashTableIter iter;
gpointer key, value;
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), NULL);
memset (&address, 0, sizeof (address));
address.plen = 128;
address.timestamp = nm_utils_get_monotonic_timestamp_s ();
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
g_return_val_if_fail (priv->options != NULL, NULL);
g_hash_table_iter_init (&iter, priv->options);
while (g_hash_table_iter_next (&iter, &key, &value)) {
nm_log_dbg (LOGD_DHCP6, "(%s): option '%s'=>'%s'",
priv->iface, (const char *) key, (const char *) value);
}
ip6_config = nm_ip6_config_new ();
str = g_hash_table_lookup (priv->options, "new_max_life");
if (str) {
address.lifetime = strtoul (str, NULL, 10);
nm_log_info (LOGD_DHCP6, " valid_lft %d", address.lifetime);
}
str = g_hash_table_lookup (priv->options, "new_preferred_life");
if (str) {
address.preferred = strtoul (str, NULL, 10);
nm_log_info (LOGD_DHCP6, " preferred_lft %d", address.preferred);
}
str = g_hash_table_lookup (priv->options, "new_ip6_address");
if (str) {
if (!inet_pton (AF_INET6, str, &tmp_addr)) {
nm_log_warn (LOGD_DHCP6, "(%s): DHCP returned invalid address '%s'",
priv->iface, str);
goto error;
}
address.address = tmp_addr;
address.source = NM_PLATFORM_SOURCE_DHCP;
nm_ip6_config_add_address (ip6_config, &address);
nm_log_info (LOGD_DHCP6, " address %s", str);
} else if (priv->info_only == FALSE) {
/* No address in Managed mode is a hard error */
goto error;
}
str = g_hash_table_lookup (priv->options, "new_host_name");
if (str)
nm_log_info (LOGD_DHCP6, " hostname '%s'", str);
str = g_hash_table_lookup (priv->options, "new_dhcp6_name_servers");
if (str) {
char **searches = g_strsplit (str, " ", 0);
char **s;
for (s = searches; *s; s++) {
if (inet_pton (AF_INET6, *s, &tmp_addr) > 0) {
nm_ip6_config_add_nameserver (ip6_config, &tmp_addr);
nm_log_info (LOGD_DHCP6, " nameserver '%s'", *s);
} else
nm_log_warn (LOGD_DHCP6, "ignoring invalid nameserver '%s'", *s);
}
g_strfreev (searches);
}
str = g_hash_table_lookup (priv->options, "new_dhcp6_domain_search");
if (str)
process_domain_search (str, ip6_add_domain_search, ip6_config);
return ip6_config;
error:
g_object_unref (ip6_config);
return NULL;
}
NMIP6Config *
nm_dhcp_client_get_ip6_config (NMDHCPClient *self, gboolean test)
{
NMDHCPClientPrivate *priv;
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), NULL);
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
if (test && !state_is_bound (priv->state)) {
nm_log_warn (LOGD_DHCP6, "(%s): DHCPv6 client didn't bind to a lease.", priv->iface);
return NULL;
}
if (!g_hash_table_size (priv->options)) {
/* We never got a response from the DHCP client */
return NULL;
}
return ip6_options_to_config (self);
}
/********************************************/
static void
nm_dhcp_client_init (NMDHCPClient *self)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
priv->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
priv->pid = -1;
}
static void
get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (object);
switch (prop_id) {
case PROP_IFACE:
g_value_set_string (value, priv->iface);
break;
case PROP_HWADDR:
g_value_set_boxed (value, priv->hwaddr);
break;
case PROP_IPV6:
g_value_set_boolean (value, priv->ipv6);
break;
case PROP_UUID:
g_value_set_string (value, priv->uuid);
break;
case PROP_PRIORITY:
g_value_set_uint (value, priv->priority);
break;
case PROP_TIMEOUT:
g_value_set_uint (value, priv->timeout);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (object);
switch (prop_id) {
case PROP_IFACE:
/* construct-only */
priv->iface = g_strdup (g_value_get_string (value));
break;
case PROP_HWADDR:
/* construct only */
priv->hwaddr = g_value_dup_boxed (value);
break;
case PROP_IPV6:
/* construct-only */
priv->ipv6 = g_value_get_boolean (value);
break;
case PROP_UUID:
/* construct-only */
priv->uuid = g_value_dup_string (value);
break;
case PROP_PRIORITY:
/* construct-only */
priv->priority = g_value_get_uint (value);
break;
case PROP_TIMEOUT:
priv->timeout = g_value_get_uint (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
dispose (GObject *object)
{
NMDHCPClient *self = NM_DHCP_CLIENT (object);
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
/* Stopping the client is left up to the controlling device
* explicitly since we may want to quit NetworkManager but not terminate
* the DHCP client.
*/
if (priv->remove_id) {
g_source_remove (priv->remove_id);
priv->remove_id = 0;
}
if (priv->options) {
g_hash_table_destroy (priv->options);
priv->options = NULL;
}
g_clear_pointer (&priv->iface, g_free);
if (priv->hwaddr) {
g_byte_array_free (priv->hwaddr, TRUE);
priv->hwaddr = NULL;
}
if (priv->duid) {
g_byte_array_free (priv->duid, TRUE);
priv->duid = NULL;
}
G_OBJECT_CLASS (nm_dhcp_client_parent_class)->dispose (object);
}
static void
nm_dhcp_client_class_init (NMDHCPClientClass *client_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (client_class);
g_type_class_add_private (client_class, sizeof (NMDHCPClientPrivate));
/* virtual methods */
object_class->dispose = dispose;
object_class->get_property = get_property;
object_class->set_property = set_property;
client_class->stop = stop;
client_class->get_duid = get_duid;
g_object_class_install_property
(object_class, PROP_IFACE,
g_param_spec_string (NM_DHCP_CLIENT_INTERFACE,
"iface",
"Interface",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property
(object_class, PROP_HWADDR,
g_param_spec_boxed (NM_DHCP_CLIENT_HWADDR,
"hwaddr",
"hardware address",
G_TYPE_BYTE_ARRAY,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property
(object_class, PROP_IPV6,
g_param_spec_boolean (NM_DHCP_CLIENT_IPV6,
"ipv6",
"IPv6",
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property
(object_class, PROP_UUID,
g_param_spec_string (NM_DHCP_CLIENT_UUID,
"uuid",
"UUID",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property
(object_class, PROP_PRIORITY,
g_param_spec_uint (NM_DHCP_CLIENT_PRIORITY,
"priority",
"Priority",
0, G_MAXUINT, 0,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property
(object_class, PROP_TIMEOUT,
g_param_spec_uint (NM_DHCP_CLIENT_TIMEOUT,
"timeout",
"Timeout",
0, G_MAXUINT, 45,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
/* signals */
signals[SIGNAL_STATE_CHANGED] =
g_signal_new (NM_DHCP_CLIENT_SIGNAL_STATE_CHANGED,
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (NMDHCPClientClass, state_changed),
NULL, NULL,
g_cclosure_marshal_VOID__UINT,
G_TYPE_NONE, 1, G_TYPE_UINT);
signals[SIGNAL_TIMEOUT] =
g_signal_new (NM_DHCP_CLIENT_SIGNAL_TIMEOUT,
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (NMDHCPClientClass, timeout),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
signals[SIGNAL_REMOVE] =
g_signal_new (NM_DHCP_CLIENT_SIGNAL_REMOVE,
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (NMDHCPClientClass, remove),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
|