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>
42 static const DHCPOption client_options[] = {
43 { OPTION_IP, 0x01 }, /* subnet-mask */
44 { OPTION_IP | OPTION_LIST, 0x03 }, /* routers */
45 { OPTION_IP | OPTION_LIST, 0x06 }, /* domain-name-servers */
46 { OPTION_STRING, 0x0c }, /* hostname */
47 { OPTION_STRING, 0x0f }, /* domain-name */
48 { OPTION_IP | OPTION_LIST, 0x2a }, /* ntp-servers */
49 { OPTION_U32, 0x33 }, /* dhcp-lease-time */
50 /* Options below will not be exposed to user */
51 { OPTION_IP, 0x32 }, /* requested-ip */
52 { OPTION_U8, 0x35 }, /* message-type */
53 { OPTION_U32, 0x36 }, /* server-id */
54 { OPTION_U16, 0x39 }, /* max-size */
55 { OPTION_STRING, 0x3c }, /* vendor */
56 { OPTION_STRING, 0x3d }, /* client-id */
57 { OPTION_STRING, 0xfc }, /* UNOFFICIAL proxy-pac */
58 { OPTION_UNKNOWN, 0x00 },
61 GDHCPOptionType dhcp_get_code_type(uint8_t code)
65 for (i = 0; client_options[i].code; i++) {
66 if (client_options[i].code == code)
67 return client_options[i].type;
70 return OPTION_UNKNOWN;
73 uint8_t *dhcp_get_option(struct dhcp_packet *packet, int code)
79 /* option bytes: [code][len][data1][data2]..[dataLEN] */
80 optionptr = packet->options;
81 rem = sizeof(packet->options);
85 /* Bad packet, malformed option field */
88 if (optionptr[OPT_CODE] == DHCP_PADDING) {
95 if (optionptr[OPT_CODE] == DHCP_END) {
96 if (overload & FILE_FIELD) {
97 overload &= ~FILE_FIELD;
99 optionptr = packet->file;
100 rem = sizeof(packet->file);
103 } else if (overload & SNAME_FIELD) {
104 overload &= ~SNAME_FIELD;
106 optionptr = packet->sname;
107 rem = sizeof(packet->sname);
115 len = 2 + optionptr[OPT_LEN];
119 continue; /* complain and return NULL */
121 if (optionptr[OPT_CODE] == code)
122 return optionptr + OPT_DATA;
124 if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD)
125 overload |= optionptr[OPT_DATA];
133 int dhcp_end_option(uint8_t *optionptr)
137 while (optionptr[i] != DHCP_END) {
138 if (optionptr[i] != DHCP_PADDING)
139 i += optionptr[i + OPT_LEN] + OPT_DATA - 1;
147 /* get a rough idea of how long an option will be */
148 static const uint8_t len_of_option_as_string[] = {
149 [OPTION_IP] = sizeof("255.255.255.255 "),
151 [OPTION_U8] = sizeof("255 "),
152 [OPTION_U16] = sizeof("65535 "),
153 [OPTION_U32] = sizeof("4294967295 "),
156 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
158 return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
161 /* Create "opt_value1 option_value2 ..." string */
162 char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
164 unsigned upper_length;
168 len = option[OPT_LEN - OPT_DATA];
169 type &= OPTION_TYPE_MASK;
170 optlen = dhcp_option_lengths[type];
173 upper_length = len_of_option_as_string[type] *
174 ((unsigned)len / (unsigned)optlen);
175 dest = ret = g_malloc(upper_length + 1);
179 while (len >= optlen) {
182 dest += sprint_nip(dest, "", option);
185 uint16_t val_u16 = get_be16(option);
186 dest += sprintf(dest, "%u", val_u16);
190 uint32_t val_u32 = get_be32(option);
191 dest += sprintf(dest, "%u", val_u32);
195 memcpy(dest, option, len);
212 uint8_t *dhcpv6_get_option(struct dhcpv6_packet *packet, uint16_t pkt_len,
213 int code, uint16_t *option_len, int *option_count)
216 uint8_t *optionptr, *found = NULL;
217 uint16_t opt_code, opt_len, len;
219 optionptr = packet->options;
220 rem = pkt_len - 1 - 3;
226 opt_code = optionptr[0] << 8 | optionptr[1];
227 opt_len = len = optionptr[2] << 8 | optionptr[3];
228 len += 2 + 2; /* skip code and len */
237 if (opt_code == code) {
239 *option_len = opt_len;
240 found = optionptr + 2 + 2;
251 *option_count = count;
263 uint8_t *dhcpv6_get_sub_option(unsigned char *option, uint16_t max_len,
264 uint16_t *option_code, uint16_t *option_len)
269 rem = max_len - 2 - 2;
275 code = option[0] << 8 | option[1];
276 len = option[2] << 8 | option[3];
289 * Add an option (supplied in binary form) to the options.
290 * Option format: [code][len][data1][data2]..[dataLEN]
292 void dhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt)
295 uint8_t *optionptr = packet->options;
296 unsigned end = dhcp_end_option(optionptr);
298 len = OPT_DATA + addopt[OPT_LEN];
300 /* end position + (option code/length + addopt length) + end option */
301 if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE)
302 /* option did not fit into the packet */
305 memcpy(optionptr + end, addopt, len);
307 optionptr[end + len] = DHCP_END;
311 * Add an option (supplied in binary form) to the options.
312 * Option format: [code][len][data1][data2]..[dataLEN]
314 void dhcpv6_add_binary_option(struct dhcpv6_packet *packet, uint16_t max_len,
315 uint16_t *pkt_len, uint8_t *addopt)
318 uint8_t *optionptr = packet->options;
320 len = 2 + 2 + (addopt[2] << 8 | addopt[3]);
322 /* end position + (option code/length + addopt length) */
323 if (*pkt_len + len >= max_len)
324 /* option did not fit into the packet */
327 memcpy(optionptr + *pkt_len, addopt, len);
331 static GDHCPOptionType check_option(uint8_t code, uint8_t data_len)
333 GDHCPOptionType type = dhcp_get_code_type(code);
336 if (type == OPTION_UNKNOWN)
339 len = dhcp_option_lengths[type & OPTION_TYPE_MASK];
340 if (len != data_len) {
341 printf("Invalid option len %d (expecting %d) for code 0x%x\n",
342 data_len, len, code);
343 return OPTION_UNKNOWN;
349 void dhcp_add_option_uint32(struct dhcp_packet *packet, uint8_t code,
354 if (check_option(code, sizeof(data)) == OPTION_UNKNOWN)
357 option[OPT_CODE] = code;
358 option[OPT_LEN] = sizeof(data);
359 put_be32(data, option + OPT_DATA);
361 dhcp_add_binary_option(packet, option);
366 void dhcp_add_option_uint16(struct dhcp_packet *packet, uint8_t code,
371 if (check_option(code, sizeof(data)) == OPTION_UNKNOWN)
374 option[OPT_CODE] = code;
375 option[OPT_LEN] = sizeof(data);
376 put_be16(data, option + OPT_DATA);
378 dhcp_add_binary_option(packet, option);
383 void dhcp_add_option_uint8(struct dhcp_packet *packet, uint8_t code,
388 if (check_option(code, sizeof(data)) == OPTION_UNKNOWN)
391 option[OPT_CODE] = code;
392 option[OPT_LEN] = sizeof(data);
393 option[OPT_DATA] = data;
395 dhcp_add_binary_option(packet, option);
400 void dhcp_init_header(struct dhcp_packet *packet, char type)
402 memset(packet, 0, sizeof(*packet));
404 packet->op = BOOTREQUEST;
410 packet->op = BOOTREPLY;
415 packet->cookie = htonl(DHCP_MAGIC);
416 packet->options[0] = DHCP_END;
418 dhcp_add_option_uint8(packet, DHCP_MESSAGE_TYPE, type);
421 void dhcpv6_init_header(struct dhcpv6_packet *packet, uint8_t type)
425 memset(packet, 0, sizeof(*packet));
427 packet->message = type;
431 packet->transaction_id[0] = (id >> 16) & 0xff;
432 packet->transaction_id[1] = (id >> 8) & 0xff;
433 packet->transaction_id[2] = id & 0xff;
436 int dhcp_recv_l3_packet(struct dhcp_packet *packet, int fd)
440 memset(packet, 0, sizeof(*packet));
442 n = read(fd, packet, sizeof(*packet));
446 if (packet->cookie != htonl(DHCP_MAGIC))
452 int dhcpv6_recv_l3_packet(struct dhcpv6_packet **packet, unsigned char *buf,
457 n = read(fd, buf, buf_len);
461 *packet = (struct dhcpv6_packet *)buf;
466 /* TODO: Use glib checksum */
467 uint16_t dhcp_checksum(void *addr, int count)
470 * Compute Internet Checksum for "count" bytes
471 * beginning at location "addr".
474 uint16_t *source = (uint16_t *) addr;
477 /* This is the inner loop */
482 /* Add left-over byte, if any */
484 /* Make sure that the left-over byte is added correctly both
485 * with little and big endian hosts */
487 *(uint8_t *) &tmp = *(uint8_t *) source;
490 /* Fold 32-bit sum to 16 bits */
492 sum = (sum & 0xffff) + (sum >> 16);
497 #define IN6ADDR_ALL_DHCP_RELAY_AGENTS_AND_SERVERS_MC_INIT \
498 { { { 0xff,0x02,0,0,0,0,0,0,0,0,0,0,0,0x1,0,0x2 } } } /* ff02::1:2 */
499 static const struct in6_addr in6addr_all_dhcp_relay_agents_and_servers_mc =
500 IN6ADDR_ALL_DHCP_RELAY_AGENTS_AND_SERVERS_MC_INIT;
502 /* from netinet/in.h */
504 struct in6_addr ipi6_addr; /* src/dst IPv6 address */
505 unsigned int ipi6_ifindex; /* send/recv interface index */
508 int dhcpv6_send_packet(int index, struct dhcpv6_packet *dhcp_pkt, int len)
512 struct in6_pktinfo *pktinfo;
513 struct cmsghdr *cmsg;
515 struct sockaddr_in6 dst;
517 size_t control_buf_len;
519 fd = socket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
523 memset(&dst, 0, sizeof(dst));
524 dst.sin6_family = AF_INET6;
525 dst.sin6_port = htons(DHCPV6_SERVER_PORT);
527 dst.sin6_addr = in6addr_all_dhcp_relay_agents_and_servers_mc;
529 control_buf_len = CMSG_SPACE(sizeof(struct in6_pktinfo));
530 control_buf = g_try_malloc0(control_buf_len);
536 memset(&m, 0, sizeof(m));
537 memset(&v, 0, sizeof(v));
540 m.msg_namelen = sizeof(dst);
542 v.iov_base = (char *)dhcp_pkt;
547 m.msg_control = control_buf;
548 m.msg_controllen = control_buf_len;
549 cmsg = CMSG_FIRSTHDR(&m);
550 cmsg->cmsg_level = IPPROTO_IPV6;
551 cmsg->cmsg_type = IPV6_PKTINFO;
552 cmsg->cmsg_len = CMSG_LEN(sizeof(*pktinfo));
554 pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
555 memset(pktinfo, 0, sizeof(*pktinfo));
556 pktinfo->ipi6_ifindex = index;
557 m.msg_controllen = cmsg->cmsg_len;
559 ret = sendmsg(fd, &m, 0);
561 char *msg = "DHCPv6 msg send failed";
563 if (errno == EADDRNOTAVAIL) {
564 char *str = g_strdup_printf("%s (index %d)",
578 int dhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
579 uint32_t source_ip, int source_port,
580 uint32_t dest_ip, int dest_port,
581 const uint8_t *dest_arp, int ifindex, bool bcast)
583 struct sockaddr_ll dest;
584 struct ip_udp_dhcp_packet packet;
588 IP_UPD_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) -
589 EXTEND_FOR_BUGGY_SERVERS,
590 UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE -
591 offsetof(struct ip_udp_dhcp_packet, udp),
594 fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
599 dhcp_pkt->flags |= htons(BROADCAST_FLAG);
601 memset(&dest, 0, sizeof(dest));
602 memset(&packet, 0, sizeof(packet));
603 packet.data = *dhcp_pkt;
605 dest.sll_family = AF_PACKET;
606 dest.sll_protocol = htons(ETH_P_IP);
607 dest.sll_ifindex = ifindex;
609 memcpy(dest.sll_addr, dest_arp, 6);
610 if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
615 packet.ip.protocol = IPPROTO_UDP;
616 packet.ip.saddr = source_ip;
617 packet.ip.daddr = dest_ip;
618 packet.udp.source = htons(source_port);
619 packet.udp.dest = htons(dest_port);
620 /* size, excluding IP header: */
621 packet.udp.len = htons(UPD_DHCP_SIZE);
622 /* for UDP checksumming, ip.len is set to UDP packet len */
623 packet.ip.tot_len = packet.udp.len;
624 packet.udp.check = dhcp_checksum(&packet, IP_UPD_DHCP_SIZE);
625 /* but for sending, it is set to IP packet len */
626 packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE);
627 packet.ip.ihl = sizeof(packet.ip) >> 2;
628 packet.ip.version = IPVERSION;
629 packet.ip.ttl = IPDEFTTL;
630 packet.ip.check = dhcp_checksum(&packet.ip, sizeof(packet.ip));
633 * Currently we send full-sized DHCP packets (zero padded).
634 * If you need to change this: last byte of the packet is
635 * packet.data.options[dhcp_end_option(packet.data.options)]
637 n = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
638 (struct sockaddr *) &dest, sizeof(dest));
647 int dhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
648 uint32_t source_ip, int source_port,
649 uint32_t dest_ip, int dest_port)
651 struct sockaddr_in client;
655 DHCP_SIZE = sizeof(struct dhcp_packet) -
656 EXTEND_FOR_BUGGY_SERVERS,
659 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
663 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
665 memset(&client, 0, sizeof(client));
666 client.sin_family = AF_INET;
667 client.sin_port = htons(source_port);
668 client.sin_addr.s_addr = htonl(source_ip);
669 if (bind(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
674 memset(&client, 0, sizeof(client));
675 client.sin_family = AF_INET;
676 client.sin_port = htons(dest_port);
677 client.sin_addr.s_addr = htonl(dest_ip);
678 if (connect(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
683 n = write(fd, dhcp_pkt, DHCP_SIZE);
693 int dhcp_l3_socket(int port, const char *interface, int family)
695 int fd, opt = 1, len;
696 struct sockaddr_in addr4;
697 struct sockaddr_in6 addr6;
698 struct sockaddr *addr;
700 fd = socket(family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
704 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
706 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
707 interface, strlen(interface) + 1) < 0) {
712 if (family == AF_INET) {
713 memset(&addr4, 0, sizeof(addr4));
714 addr4.sin_family = family;
715 addr4.sin_port = htons(port);
716 addr = (struct sockaddr *)&addr4;
718 } else if (family == AF_INET6) {
719 memset(&addr6, 0, sizeof(addr6));
720 addr6.sin6_family = family;
721 addr6.sin6_port = htons(port);
722 addr = (struct sockaddr *)&addr6;
729 if (bind(fd, addr, len) != 0) {
737 char *get_interface_name(int index)
745 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
747 perror("Open socket error");
751 memset(&ifr, 0, sizeof(ifr));
752 ifr.ifr_ifindex = index;
754 err = ioctl(sk, SIOCGIFNAME, &ifr);
756 perror("Get interface name error");
763 return g_strdup(ifr.ifr_name);
766 bool interface_is_up(int index)
772 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
774 perror("Open socket error");
778 memset(&ifr, 0, sizeof(ifr));
779 ifr.ifr_ifindex = index;
781 err = ioctl(sk, SIOCGIFNAME, &ifr);
783 perror("Get interface name error");
787 err = ioctl(sk, SIOCGIFFLAGS, &ifr);
789 perror("Get interface flags error");
793 if (ifr.ifr_flags & IFF_UP)