2 * DHCP library with GLib integration
4 * Copyright (C) 2007-2010 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
30 #include <netpacket/packet.h>
31 #include <net/ethernet.h>
36 static const DHCPOption client_options[] = {
37 { OPTION_IP, 0x01 }, /* subnet-mask */
38 { OPTION_IP | OPTION_LIST, 0x03 }, /* routers */
39 { OPTION_IP | OPTION_LIST, 0x06 }, /* domain-name-servers */
40 { OPTION_STRING, 0x0c }, /* hostname */
41 { OPTION_STRING, 0x0f }, /* domain-name */
42 { OPTION_IP | OPTION_LIST, 0x2a }, /* ntp-servers */
43 { OPTION_U32, 0x33 }, /* dhcp-lease-time */
44 /* Options below will not be exposed to user */
45 { OPTION_IP, 0x32 }, /* requested-ip */
46 { OPTION_U8, 0x35 }, /* message-type */
47 { OPTION_U32, 0x36 }, /* server-id */
48 { OPTION_U16, 0x39 }, /* max-size */
49 { OPTION_STRING, 0x3c }, /* vendor */
50 { OPTION_STRING, 0x3d }, /* client-id */
51 { OPTION_UNKNOWN, 0x00 },
54 GDHCPOptionType dhcp_get_code_type(uint8_t code)
58 for (i = 0; client_options[i].code; i++) {
59 if (client_options[i].code == code)
60 return client_options[i].type;
63 return OPTION_UNKNOWN;
66 uint8_t *dhcp_get_option(struct dhcp_packet *packet, int code)
72 /* option bytes: [code][len][data1][data2]..[dataLEN] */
73 optionptr = packet->options;
74 rem = sizeof(packet->options);
78 /* Bad packet, malformed option field */
81 if (optionptr[OPT_CODE] == DHCP_PADDING) {
88 if (optionptr[OPT_CODE] == DHCP_END) {
89 if (overload & FILE_FIELD) {
90 overload &= ~FILE_FIELD;
92 optionptr = packet->file;
93 rem = sizeof(packet->file);
96 } else if (overload & SNAME_FIELD) {
97 overload &= ~SNAME_FIELD;
99 optionptr = packet->sname;
100 rem = sizeof(packet->sname);
108 len = 2 + optionptr[OPT_LEN];
112 continue; /* complain and return NULL */
114 if (optionptr[OPT_CODE] == code)
115 return optionptr + OPT_DATA;
117 if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD)
118 overload |= optionptr[OPT_DATA];
126 int dhcp_end_option(uint8_t *optionptr)
130 while (optionptr[i] != DHCP_END) {
131 if (optionptr[i] != DHCP_PADDING)
132 i += optionptr[i + OPT_LEN] + OPT_DATA - 1;
141 * Add an option (supplied in binary form) to the options.
142 * Option format: [code][len][data1][data2]..[dataLEN]
144 void dhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt)
147 uint8_t *optionptr = packet->options;
148 unsigned end = dhcp_end_option(optionptr);
150 len = OPT_DATA + addopt[OPT_LEN];
152 /* end position + (option code/length + addopt length) + end option */
153 if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE)
154 /* option did not fit into the packet */
157 memcpy(optionptr + end, addopt, len);
159 optionptr[end + len] = DHCP_END;
162 void dhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code,
165 uint8_t option[6], len;
166 GDHCPOptionType type = dhcp_get_code_type(code);
168 if (type == OPTION_UNKNOWN)
171 option[OPT_CODE] = code;
173 len = dhcp_option_lengths[type & OPTION_TYPE_MASK];
174 option[OPT_LEN] = len;
176 #if __BYTE_ORDER == __BIG_ENDIAN
177 data <<= 8 * (4 - len);
180 dhcp_put_unaligned(data, (uint32_t *) &option[OPT_DATA]);
181 dhcp_add_binary_option(packet, option);
186 void dhcp_init_header(struct dhcp_packet *packet, char type)
188 memset(packet, 0, sizeof(*packet));
190 packet->op = BOOTREQUEST;
196 packet->op = BOOTREPLY;
201 packet->cookie = htonl(DHCP_MAGIC);
202 packet->options[0] = DHCP_END;
204 dhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
207 static gboolean check_vendor(uint8_t *option_vendor, const char *vendor)
209 uint8_t vendor_length = sizeof(vendor) - 1;
211 if (option_vendor[OPT_LEN - OPT_DATA] != vendor_length)
214 if (memcmp(option_vendor, vendor, vendor_length) != 0)
220 static void check_broken_vendor(struct dhcp_packet *packet)
224 if (packet->op != BOOTREQUEST)
227 vendor = dhcp_get_option(packet, DHCP_VENDOR);
231 if (check_vendor(vendor, "MSFT 98") == TRUE)
232 packet->flags |= htons(BROADCAST_FLAG);
235 int dhcp_recv_l3_packet(struct dhcp_packet *packet, int fd)
239 memset(packet, 0, sizeof(*packet));
241 n = read(fd, packet, sizeof(*packet));
245 if (packet->cookie != htonl(DHCP_MAGIC))
248 check_broken_vendor(packet);
253 /* TODO: Use glib checksum */
254 uint16_t dhcp_checksum(void *addr, int count)
257 * Compute Internet Checksum for "count" bytes
258 * beginning at location "addr".
261 uint16_t *source = (uint16_t *) addr;
264 /* This is the inner loop */
269 /* Add left-over byte, if any */
271 /* Make sure that the left-over byte is added correctly both
272 * with little and big endian hosts */
274 *(uint8_t *) &tmp = *(uint8_t *) source;
277 /* Fold 32-bit sum to 16 bits */
279 sum = (sum & 0xffff) + (sum >> 16);
284 int dhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
285 uint32_t source_ip, int source_port, uint32_t dest_ip,
286 int dest_port, const uint8_t *dest_arp, int ifindex)
288 struct sockaddr_ll dest;
289 struct ip_udp_dhcp_packet packet;
293 IP_UPD_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) -
294 EXTEND_FOR_BUGGY_SERVERS,
295 UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE -
296 offsetof(struct ip_udp_dhcp_packet, udp),
299 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
303 memset(&dest, 0, sizeof(dest));
304 memset(&packet, 0, sizeof(packet));
305 packet.data = *dhcp_pkt;
307 dest.sll_family = AF_PACKET;
308 dest.sll_protocol = htons(ETH_P_IP);
309 dest.sll_ifindex = ifindex;
311 memcpy(dest.sll_addr, dest_arp, 6);
312 if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
317 packet.ip.protocol = IPPROTO_UDP;
318 packet.ip.saddr = source_ip;
319 packet.ip.daddr = dest_ip;
320 packet.udp.source = htons(source_port);
321 packet.udp.dest = htons(dest_port);
322 /* size, excluding IP header: */
323 packet.udp.len = htons(UPD_DHCP_SIZE);
324 /* for UDP checksumming, ip.len is set to UDP packet len */
325 packet.ip.tot_len = packet.udp.len;
326 packet.udp.check = dhcp_checksum(&packet, IP_UPD_DHCP_SIZE);
327 /* but for sending, it is set to IP packet len */
328 packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE);
329 packet.ip.ihl = sizeof(packet.ip) >> 2;
330 packet.ip.version = IPVERSION;
331 packet.ip.ttl = IPDEFTTL;
332 packet.ip.check = dhcp_checksum(&packet.ip, sizeof(packet.ip));
335 * Currently we send full-sized DHCP packets (zero padded).
336 * If you need to change this: last byte of the packet is
337 * packet.data.options[dhcp_end_option(packet.data.options)]
339 n = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
340 (struct sockaddr *) &dest, sizeof(dest));
349 int dhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
350 uint32_t source_ip, int source_port,
351 uint32_t dest_ip, int dest_port)
353 struct sockaddr_in client;
357 DHCP_SIZE = sizeof(struct dhcp_packet) -
358 EXTEND_FOR_BUGGY_SERVERS,
361 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
365 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
367 memset(&client, 0, sizeof(client));
368 client.sin_family = AF_INET;
369 client.sin_port = htons(source_port);
370 client.sin_addr.s_addr = source_ip;
371 if (bind(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
376 memset(&client, 0, sizeof(client));
377 client.sin_family = AF_INET;
378 client.sin_port = htons(dest_port);
379 client.sin_addr.s_addr = dest_ip;
380 if (connect(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
385 n = write(fd, dhcp_pkt, DHCP_SIZE);
395 int dhcp_l3_socket(int port, const char *interface)
398 struct sockaddr_in addr;
400 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
402 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
404 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
405 interface, strlen(interface) + 1) < 0) {
410 memset(&addr, 0, sizeof(addr));
411 addr.sin_family = AF_INET;
412 addr.sin_port = htons(port);
413 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {