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, 0x0f }, /* domain-name */
41 { OPTION_IP | OPTION_LIST, 0x2a }, /* ntp-servers */
42 { OPTION_U32, 0x33 }, /* dhcp-lease-time */
43 /* Options below will not be exposed to user */
44 { OPTION_IP, 0x32 }, /* requested-ip */
45 { OPTION_U8, 0x35 }, /* message-type */
46 { OPTION_U16, 0x39 }, /* max-size */
47 { OPTION_STRING, 0x3c }, /* vendor */
48 { OPTION_STRING, 0x3d }, /* client-id */
49 { OPTION_UNKNOWN, 0x00 },
52 GDHCPOptionType dhcp_get_code_type(uint8_t code)
56 for (i = 0; client_options[i].code; i++) {
57 if (client_options[i].code == code)
58 return client_options[i].type;
61 return OPTION_UNKNOWN;
64 uint8_t *dhcp_get_option(struct dhcp_packet *packet, int code)
70 /* option bytes: [code][len][data1][data2]..[dataLEN] */
71 optionptr = packet->options;
72 rem = sizeof(packet->options);
76 /* Bad packet, malformed option field */
79 if (optionptr[OPT_CODE] == DHCP_PADDING) {
86 if (optionptr[OPT_CODE] == DHCP_END) {
87 if (overload & FILE_FIELD) {
88 overload &= ~FILE_FIELD;
90 optionptr = packet->file;
91 rem = sizeof(packet->file);
94 } else if (overload & SNAME_FIELD) {
95 overload &= ~SNAME_FIELD;
97 optionptr = packet->sname;
98 rem = sizeof(packet->sname);
106 len = 2 + optionptr[OPT_LEN];
110 continue; /* complain and return NULL */
112 if (optionptr[OPT_CODE] == code)
113 return optionptr + OPT_DATA;
115 if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD)
116 overload |= optionptr[OPT_DATA];
124 int dhcp_end_option(uint8_t *optionptr)
128 while (optionptr[i] != DHCP_END) {
129 if (optionptr[i] != DHCP_PADDING)
130 i += optionptr[i + OPT_LEN] + OPT_DATA - 1;
139 * Add an option (supplied in binary form) to the options.
140 * Option format: [code][len][data1][data2]..[dataLEN]
142 void dhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt)
145 uint8_t *optionptr = packet->options;
146 unsigned end = dhcp_end_option(optionptr);
148 len = OPT_DATA + addopt[OPT_LEN];
150 /* end position + (option code/length + addopt length) + end option */
151 if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE)
152 /* option did not fit into the packet */
155 memcpy(optionptr + end, addopt, len);
157 optionptr[end + len] = DHCP_END;
160 void dhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code,
163 uint8_t option[6], len;
164 GDHCPOptionType type = dhcp_get_code_type(code);
166 if (type == OPTION_UNKNOWN)
169 option[OPT_CODE] = code;
171 len = dhcp_option_lengths[type & OPTION_TYPE_MASK];
172 option[OPT_LEN] = len;
174 #if __BYTE_ORDER == __BIG_ENDIAN
175 data <<= 8 * (4 - len);
178 dhcp_put_unaligned(data, (uint32_t *) &option[OPT_DATA]);
179 dhcp_add_binary_option(packet, option);
184 void dhcp_init_header(struct dhcp_packet *packet, char type)
186 memset(packet, 0, sizeof(*packet));
188 packet->op = BOOTREQUEST;
194 packet->op = BOOTREPLY;
199 packet->cookie = htonl(DHCP_MAGIC);
200 packet->options[0] = DHCP_END;
202 dhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
205 static gboolean check_vendor(uint8_t *option_vendor, const char *vendor)
207 uint8_t vendor_length = sizeof(vendor) - 1;
209 if (option_vendor[OPT_LEN - OPT_DATA] != vendor_length)
212 if (memcmp(option_vendor, vendor, vendor_length) != 0)
218 static void check_broken_vendor(struct dhcp_packet *packet)
222 if (packet->op != BOOTREQUEST)
225 vendor = dhcp_get_option(packet, DHCP_VENDOR);
229 if (check_vendor(vendor, "MSFT 98") == TRUE)
230 packet->flags |= htons(BROADCAST_FLAG);
233 int dhcp_recv_l3_packet(struct dhcp_packet *packet, int fd)
237 memset(packet, 0, sizeof(*packet));
239 n = read(fd, packet, sizeof(*packet));
243 if (packet->cookie != htonl(DHCP_MAGIC))
246 check_broken_vendor(packet);
251 /* TODO: Use glib checksum */
252 uint16_t dhcp_checksum(void *addr, int count)
255 * Compute Internet Checksum for "count" bytes
256 * beginning at location "addr".
259 uint16_t *source = (uint16_t *) addr;
262 /* This is the inner loop */
267 /* Add left-over byte, if any */
269 /* Make sure that the left-over byte is added correctly both
270 * with little and big endian hosts */
272 *(uint8_t *) &tmp = *(uint8_t *) source;
275 /* Fold 32-bit sum to 16 bits */
277 sum = (sum & 0xffff) + (sum >> 16);
282 int dhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
283 uint32_t source_ip, int source_port, uint32_t dest_ip,
284 int dest_port, const uint8_t *dest_arp, int ifindex)
286 struct sockaddr_ll dest;
287 struct ip_udp_dhcp_packet packet;
291 IP_UPD_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) -
292 EXTEND_FOR_BUGGY_SERVERS,
293 UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE -
294 offsetof(struct ip_udp_dhcp_packet, udp),
297 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
301 memset(&dest, 0, sizeof(dest));
302 memset(&packet, 0, sizeof(packet));
303 packet.data = *dhcp_pkt;
305 dest.sll_family = AF_PACKET;
306 dest.sll_protocol = htons(ETH_P_IP);
307 dest.sll_ifindex = ifindex;
309 memcpy(dest.sll_addr, dest_arp, 6);
310 if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
315 packet.ip.protocol = IPPROTO_UDP;
316 packet.ip.saddr = source_ip;
317 packet.ip.daddr = dest_ip;
318 packet.udp.source = htons(source_port);
319 packet.udp.dest = htons(dest_port);
320 /* size, excluding IP header: */
321 packet.udp.len = htons(UPD_DHCP_SIZE);
322 /* for UDP checksumming, ip.len is set to UDP packet len */
323 packet.ip.tot_len = packet.udp.len;
324 packet.udp.check = dhcp_checksum(&packet, IP_UPD_DHCP_SIZE);
325 /* but for sending, it is set to IP packet len */
326 packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE);
327 packet.ip.ihl = sizeof(packet.ip) >> 2;
328 packet.ip.version = IPVERSION;
329 packet.ip.ttl = IPDEFTTL;
330 packet.ip.check = dhcp_checksum(&packet.ip, sizeof(packet.ip));
333 * Currently we send full-sized DHCP packets (zero padded).
334 * If you need to change this: last byte of the packet is
335 * packet.data.options[dhcp_end_option(packet.data.options)]
337 n = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
338 (struct sockaddr *) &dest, sizeof(dest));
347 int dhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
348 uint32_t source_ip, int source_port,
349 uint32_t dest_ip, int dest_port)
351 struct sockaddr_in client;
355 DHCP_SIZE = sizeof(struct dhcp_packet) -
356 EXTEND_FOR_BUGGY_SERVERS,
359 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
363 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
365 memset(&client, 0, sizeof(client));
366 client.sin_family = AF_INET;
367 client.sin_port = htons(source_port);
368 client.sin_addr.s_addr = source_ip;
369 if (bind(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
374 memset(&client, 0, sizeof(client));
375 client.sin_family = AF_INET;
376 client.sin_port = htons(dest_port);
377 client.sin_addr.s_addr = dest_ip;
378 if (connect(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
383 n = write(fd, dhcp_pkt, DHCP_SIZE);
393 int dhcp_l3_socket(int port, const char *interface)
396 struct sockaddr_in addr;
398 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
400 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
402 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
403 interface, strlen(interface) + 1) < 0) {
408 memset(&addr, 0, sizeof(addr));
409 addr.sin_family = AF_INET;
410 addr.sin_port = htons(port);
411 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {