2 * DHCP library with GLib integration
4 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/ioctl.h>
33 #include <net/if_arp.h>
35 #include <netpacket/packet.h>
36 #include <net/ethernet.h>
37 #include <arpa/inet.h>
43 static const DHCPOption client_options[] = {
44 { OPTION_IP, 0x01 }, /* subnet-mask */
45 { OPTION_IP | OPTION_LIST, 0x03 }, /* routers */
46 { OPTION_IP | OPTION_LIST, 0x06 }, /* domain-name-servers */
47 { OPTION_STRING, 0x0c }, /* hostname */
48 { OPTION_STRING, 0x0f }, /* domain-name */
49 { OPTION_IP | OPTION_LIST, 0x2a }, /* ntp-servers */
50 { OPTION_U32, 0x33 }, /* dhcp-lease-time */
51 /* Options below will not be exposed to user */
52 { OPTION_IP, 0x32 }, /* requested-ip */
53 { OPTION_U8, 0x35 }, /* message-type */
54 { OPTION_U32, 0x36 }, /* server-id */
55 { OPTION_U16, 0x39 }, /* max-size */
56 { OPTION_STRING, 0x3c }, /* vendor */
57 { OPTION_STRING, 0x3d }, /* client-id */
58 { OPTION_STRING, 0xfc }, /* UNOFFICIAL proxy-pac */
59 { OPTION_UNKNOWN, 0x00 },
62 #define URANDOM "/dev/urandom"
63 static int random_fd = -1;
65 int dhcp_get_random(uint64_t *val)
70 random_fd = open(URANDOM, O_RDONLY);
79 if (read(random_fd, val, sizeof(uint64_t)) < 0) {
89 void dhcp_cleanup_random(void)
98 GDHCPOptionType dhcp_get_code_type(uint8_t code)
102 for (i = 0; client_options[i].code; i++) {
103 if (client_options[i].code == code)
104 return client_options[i].type;
107 return OPTION_UNKNOWN;
110 uint8_t *dhcp_get_option(struct dhcp_packet *packet, int code)
114 uint8_t overload = 0;
116 /* option bytes: [code][len][data1][data2]..[dataLEN] */
117 optionptr = packet->options;
118 rem = sizeof(packet->options);
122 /* Bad packet, malformed option field */
125 if (optionptr[OPT_CODE] == DHCP_PADDING) {
132 if (optionptr[OPT_CODE] == DHCP_END) {
133 if (overload & FILE_FIELD) {
134 overload &= ~FILE_FIELD;
136 optionptr = packet->file;
137 rem = sizeof(packet->file);
140 } else if (overload & SNAME_FIELD) {
141 overload &= ~SNAME_FIELD;
143 optionptr = packet->sname;
144 rem = sizeof(packet->sname);
152 len = 2 + optionptr[OPT_LEN];
156 continue; /* complain and return NULL */
158 if (optionptr[OPT_CODE] == code)
159 return optionptr + OPT_DATA;
161 if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD)
162 overload |= optionptr[OPT_DATA];
170 int dhcp_end_option(uint8_t *optionptr)
174 while (optionptr[i] != DHCP_END) {
175 if (optionptr[i] != DHCP_PADDING)
176 i += optionptr[i + OPT_LEN] + OPT_DATA - 1;
184 uint8_t *dhcpv6_get_option(struct dhcpv6_packet *packet, uint16_t pkt_len,
185 int code, uint16_t *option_len, int *option_count)
188 uint8_t *optionptr, *found = NULL;
189 uint16_t opt_code, opt_len, len;
191 optionptr = packet->options;
192 rem = pkt_len - 1 - 3;
198 opt_code = optionptr[0] << 8 | optionptr[1];
199 opt_len = len = optionptr[2] << 8 | optionptr[3];
200 len += 2 + 2; /* skip code and len */
209 if (opt_code == code) {
211 *option_len = opt_len;
212 found = optionptr + 2 + 2;
223 *option_count = count;
235 uint8_t *dhcpv6_get_sub_option(unsigned char *option, uint16_t max_len,
236 uint16_t *option_code, uint16_t *option_len)
241 rem = max_len - 2 - 2;
247 code = option[0] << 8 | option[1];
248 len = option[2] << 8 | option[3];
261 * Add an option (supplied in binary form) to the options.
262 * Option format: [code][len][data1][data2]..[dataLEN]
264 void dhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt)
267 uint8_t *optionptr = packet->options;
268 unsigned end = dhcp_end_option(optionptr);
270 len = OPT_DATA + addopt[OPT_LEN];
272 /* end position + (option code/length + addopt length) + end option */
273 if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE)
274 /* option did not fit into the packet */
277 memcpy(optionptr + end, addopt, len);
279 optionptr[end + len] = DHCP_END;
283 * Add an option (supplied in binary form) to the options.
284 * Option format: [code][len][data1][data2]..[dataLEN]
286 void dhcpv6_add_binary_option(struct dhcpv6_packet *packet, uint16_t max_len,
287 uint16_t *pkt_len, uint8_t *addopt)
290 uint8_t *optionptr = packet->options;
292 len = 2 + 2 + (addopt[2] << 8 | addopt[3]);
294 /* end position + (option code/length + addopt length) */
295 if (*pkt_len + len >= max_len)
296 /* option did not fit into the packet */
299 memcpy(optionptr + *pkt_len, addopt, len);
303 static GDHCPOptionType check_option(uint8_t code, uint8_t data_len)
305 GDHCPOptionType type = dhcp_get_code_type(code);
308 if (type == OPTION_UNKNOWN)
311 len = dhcp_option_lengths[type & OPTION_TYPE_MASK];
312 if (len != data_len) {
313 printf("Invalid option len %d (expecting %d) for code 0x%x\n",
314 data_len, len, code);
315 return OPTION_UNKNOWN;
321 void dhcp_add_option_uint32(struct dhcp_packet *packet, uint8_t code,
326 if (check_option(code, sizeof(data)) == OPTION_UNKNOWN)
329 option[OPT_CODE] = code;
330 option[OPT_LEN] = sizeof(data);
331 put_be32(data, option + OPT_DATA);
333 dhcp_add_binary_option(packet, option);
338 void dhcp_add_option_uint16(struct dhcp_packet *packet, uint8_t code,
343 if (check_option(code, sizeof(data)) == OPTION_UNKNOWN)
346 option[OPT_CODE] = code;
347 option[OPT_LEN] = sizeof(data);
348 put_be16(data, option + OPT_DATA);
350 dhcp_add_binary_option(packet, option);
355 void dhcp_add_option_uint8(struct dhcp_packet *packet, uint8_t code,
360 if (check_option(code, sizeof(data)) == OPTION_UNKNOWN)
363 option[OPT_CODE] = code;
364 option[OPT_LEN] = sizeof(data);
365 option[OPT_DATA] = data;
367 dhcp_add_binary_option(packet, option);
372 void dhcp_init_header(struct dhcp_packet *packet, char type)
374 memset(packet, 0, sizeof(*packet));
376 packet->op = BOOTREQUEST;
382 packet->op = BOOTREPLY;
387 packet->cookie = htonl(DHCP_MAGIC);
388 packet->options[0] = DHCP_END;
390 dhcp_add_option_uint8(packet, DHCP_MESSAGE_TYPE, type);
393 void dhcpv6_init_header(struct dhcpv6_packet *packet, uint8_t type)
398 memset(packet, 0, sizeof(*packet));
400 packet->message = type;
402 dhcp_get_random(&rand);
405 packet->transaction_id[0] = (id >> 16) & 0xff;
406 packet->transaction_id[1] = (id >> 8) & 0xff;
407 packet->transaction_id[2] = id & 0xff;
410 int dhcp_recv_l3_packet(struct dhcp_packet *packet, int fd)
414 memset(packet, 0, sizeof(*packet));
416 n = read(fd, packet, sizeof(*packet));
420 if (packet->cookie != htonl(DHCP_MAGIC))
426 int dhcpv6_recv_l3_packet(struct dhcpv6_packet **packet, unsigned char *buf,
431 n = read(fd, buf, buf_len);
435 *packet = (struct dhcpv6_packet *)buf;
440 /* TODO: Use glib checksum */
441 uint16_t dhcp_checksum(void *addr, int count)
444 * Compute Internet Checksum for "count" bytes
445 * beginning at location "addr".
448 uint16_t *source = (uint16_t *) addr;
451 /* This is the inner loop */
456 /* Add left-over byte, if any */
458 /* Make sure that the left-over byte is added correctly both
459 * with little and big endian hosts */
461 *(uint8_t *) &tmp = *(uint8_t *) source;
464 /* Fold 32-bit sum to 16 bits */
466 sum = (sum & 0xffff) + (sum >> 16);
471 #define IN6ADDR_ALL_DHCP_RELAY_AGENTS_AND_SERVERS_MC_INIT \
472 { { { 0xff,0x02,0,0,0,0,0,0,0,0,0,0,0,0x1,0,0x2 } } } /* ff02::1:2 */
473 static const struct in6_addr in6addr_all_dhcp_relay_agents_and_servers_mc =
474 IN6ADDR_ALL_DHCP_RELAY_AGENTS_AND_SERVERS_MC_INIT;
476 int dhcpv6_send_packet(int index, struct dhcpv6_packet *dhcp_pkt, int len)
480 struct in6_pktinfo *pktinfo;
481 struct cmsghdr *cmsg;
482 int fd, ret, opt = 1;
483 struct sockaddr_in6 src;
484 struct sockaddr_in6 dst;
486 size_t control_buf_len;
488 fd = socket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
492 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
494 memset(&src, 0, sizeof(src));
495 src.sin6_family = AF_INET6;
496 src.sin6_port = htons(DHCPV6_CLIENT_PORT);
498 if (bind(fd, (struct sockaddr *) &src, sizeof(src)) <0) {
503 memset(&dst, 0, sizeof(dst));
504 dst.sin6_family = AF_INET6;
505 dst.sin6_port = htons(DHCPV6_SERVER_PORT);
507 dst.sin6_addr = in6addr_all_dhcp_relay_agents_and_servers_mc;
509 control_buf_len = CMSG_SPACE(sizeof(struct in6_pktinfo));
510 control_buf = g_try_malloc0(control_buf_len);
516 memset(&m, 0, sizeof(m));
517 memset(&v, 0, sizeof(v));
520 m.msg_namelen = sizeof(dst);
522 v.iov_base = (char *)dhcp_pkt;
527 m.msg_control = control_buf;
528 m.msg_controllen = control_buf_len;
529 cmsg = CMSG_FIRSTHDR(&m);
530 cmsg->cmsg_level = IPPROTO_IPV6;
531 cmsg->cmsg_type = IPV6_PKTINFO;
532 cmsg->cmsg_len = CMSG_LEN(sizeof(*pktinfo));
534 pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
535 memset(pktinfo, 0, sizeof(*pktinfo));
536 pktinfo->ipi6_ifindex = index;
537 m.msg_controllen = cmsg->cmsg_len;
539 ret = sendmsg(fd, &m, 0);
541 char *msg = "DHCPv6 msg send failed";
543 if (errno == EADDRNOTAVAIL) {
544 char *str = g_strdup_printf("%s (index %d)",
558 int dhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
559 uint32_t source_ip, int source_port,
560 uint32_t dest_ip, int dest_port,
561 const uint8_t *dest_arp, int ifindex, bool bcast)
563 struct sockaddr_ll dest;
564 struct ip_udp_dhcp_packet packet;
568 IP_UPD_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) -
569 EXTEND_FOR_BUGGY_SERVERS,
570 UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE -
571 offsetof(struct ip_udp_dhcp_packet, udp),
574 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
579 dhcp_pkt->flags |= htons(BROADCAST_FLAG);
581 memset(&dest, 0, sizeof(dest));
582 memset(&packet, 0, sizeof(packet));
583 packet.data = *dhcp_pkt;
585 dest.sll_family = AF_PACKET;
586 dest.sll_protocol = htons(ETH_P_IP);
587 dest.sll_ifindex = ifindex;
589 memcpy(dest.sll_addr, dest_arp, 6);
590 if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
595 packet.ip.protocol = IPPROTO_UDP;
596 packet.ip.saddr = source_ip;
597 packet.ip.daddr = dest_ip;
598 packet.udp.source = htons(source_port);
599 packet.udp.dest = htons(dest_port);
600 /* size, excluding IP header: */
601 packet.udp.len = htons(UPD_DHCP_SIZE);
602 /* for UDP checksumming, ip.len is set to UDP packet len */
603 packet.ip.tot_len = packet.udp.len;
604 packet.udp.check = dhcp_checksum(&packet, IP_UPD_DHCP_SIZE);
605 /* but for sending, it is set to IP packet len */
606 packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE);
607 packet.ip.ihl = sizeof(packet.ip) >> 2;
608 packet.ip.version = IPVERSION;
609 packet.ip.ttl = IPDEFTTL;
610 packet.ip.check = dhcp_checksum(&packet.ip, sizeof(packet.ip));
613 * Currently we send full-sized DHCP packets (zero padded).
614 * If you need to change this: last byte of the packet is
615 * packet.data.options[dhcp_end_option(packet.data.options)]
617 n = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
618 (struct sockaddr *) &dest, sizeof(dest));
627 int dhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
628 uint32_t source_ip, int source_port,
629 uint32_t dest_ip, int dest_port)
631 struct sockaddr_in client;
635 DHCP_SIZE = sizeof(struct dhcp_packet) -
636 EXTEND_FOR_BUGGY_SERVERS,
639 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
643 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
645 memset(&client, 0, sizeof(client));
646 client.sin_family = AF_INET;
647 client.sin_port = htons(source_port);
648 client.sin_addr.s_addr = htonl(source_ip);
649 if (bind(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
654 memset(&client, 0, sizeof(client));
655 client.sin_family = AF_INET;
656 client.sin_port = htons(dest_port);
657 client.sin_addr.s_addr = htonl(dest_ip);
658 if (connect(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
663 n = write(fd, dhcp_pkt, DHCP_SIZE);
673 int dhcp_l3_socket(int port, const char *interface, int family)
675 int fd, opt = 1, len;
676 struct sockaddr_in addr4;
677 struct sockaddr_in6 addr6;
678 struct sockaddr *addr;
680 fd = socket(family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
684 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
686 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
687 interface, strlen(interface) + 1) < 0) {
692 if (family == AF_INET) {
693 memset(&addr4, 0, sizeof(addr4));
694 addr4.sin_family = family;
695 addr4.sin_port = htons(port);
696 addr = (struct sockaddr *)&addr4;
698 } else if (family == AF_INET6) {
699 memset(&addr6, 0, sizeof(addr6));
700 addr6.sin6_family = family;
701 addr6.sin6_port = htons(port);
702 addr = (struct sockaddr *)&addr6;
709 if (bind(fd, addr, len) != 0) {
717 char *get_interface_name(int index)
725 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
727 perror("Open socket error");
731 memset(&ifr, 0, sizeof(ifr));
732 ifr.ifr_ifindex = index;
734 err = ioctl(sk, SIOCGIFNAME, &ifr);
736 perror("Get interface name error");
743 return g_strdup(ifr.ifr_name);
746 bool interface_is_up(int index)
752 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
754 perror("Open socket error");
758 memset(&ifr, 0, sizeof(ifr));
759 ifr.ifr_ifindex = index;
761 err = ioctl(sk, SIOCGIFNAME, &ifr);
763 perror("Get interface name error");
767 err = ioctl(sk, SIOCGIFFLAGS, &ifr);
769 perror("Get interface flags error");
773 if (ifr.ifr_flags & IFF_UP)