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_U16, 0x39 }, /* max-size */
48 { OPTION_STRING, 0x3c }, /* vendor */
49 { OPTION_STRING, 0x3d }, /* client-id */
50 { OPTION_UNKNOWN, 0x00 },
53 GDHCPOptionType dhcp_get_code_type(uint8_t code)
57 for (i = 0; client_options[i].code; i++) {
58 if (client_options[i].code == code)
59 return client_options[i].type;
62 return OPTION_UNKNOWN;
65 uint8_t *dhcp_get_option(struct dhcp_packet *packet, int code)
71 /* option bytes: [code][len][data1][data2]..[dataLEN] */
72 optionptr = packet->options;
73 rem = sizeof(packet->options);
77 /* Bad packet, malformed option field */
80 if (optionptr[OPT_CODE] == DHCP_PADDING) {
87 if (optionptr[OPT_CODE] == DHCP_END) {
88 if (overload & FILE_FIELD) {
89 overload &= ~FILE_FIELD;
91 optionptr = packet->file;
92 rem = sizeof(packet->file);
95 } else if (overload & SNAME_FIELD) {
96 overload &= ~SNAME_FIELD;
98 optionptr = packet->sname;
99 rem = sizeof(packet->sname);
107 len = 2 + optionptr[OPT_LEN];
111 continue; /* complain and return NULL */
113 if (optionptr[OPT_CODE] == code)
114 return optionptr + OPT_DATA;
116 if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD)
117 overload |= optionptr[OPT_DATA];
125 int dhcp_end_option(uint8_t *optionptr)
129 while (optionptr[i] != DHCP_END) {
130 if (optionptr[i] != DHCP_PADDING)
131 i += optionptr[i + OPT_LEN] + OPT_DATA - 1;
140 * Add an option (supplied in binary form) to the options.
141 * Option format: [code][len][data1][data2]..[dataLEN]
143 void dhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt)
146 uint8_t *optionptr = packet->options;
147 unsigned end = dhcp_end_option(optionptr);
149 len = OPT_DATA + addopt[OPT_LEN];
151 /* end position + (option code/length + addopt length) + end option */
152 if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE)
153 /* option did not fit into the packet */
156 memcpy(optionptr + end, addopt, len);
158 optionptr[end + len] = DHCP_END;
161 void dhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code,
164 uint8_t option[6], len;
165 GDHCPOptionType type = dhcp_get_code_type(code);
167 if (type == OPTION_UNKNOWN)
170 option[OPT_CODE] = code;
172 len = dhcp_option_lengths[type & OPTION_TYPE_MASK];
173 option[OPT_LEN] = len;
175 #if __BYTE_ORDER == __BIG_ENDIAN
176 data <<= 8 * (4 - len);
179 dhcp_put_unaligned(data, (uint32_t *) &option[OPT_DATA]);
180 dhcp_add_binary_option(packet, option);
185 void dhcp_init_header(struct dhcp_packet *packet, char type)
187 memset(packet, 0, sizeof(*packet));
189 packet->op = BOOTREQUEST;
195 packet->op = BOOTREPLY;
200 packet->cookie = htonl(DHCP_MAGIC);
201 packet->options[0] = DHCP_END;
203 dhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
206 static gboolean check_vendor(uint8_t *option_vendor, const char *vendor)
208 uint8_t vendor_length = sizeof(vendor) - 1;
210 if (option_vendor[OPT_LEN - OPT_DATA] != vendor_length)
213 if (memcmp(option_vendor, vendor, vendor_length) != 0)
219 static void check_broken_vendor(struct dhcp_packet *packet)
223 if (packet->op != BOOTREQUEST)
226 vendor = dhcp_get_option(packet, DHCP_VENDOR);
230 if (check_vendor(vendor, "MSFT 98") == TRUE)
231 packet->flags |= htons(BROADCAST_FLAG);
234 int dhcp_recv_l3_packet(struct dhcp_packet *packet, int fd)
238 memset(packet, 0, sizeof(*packet));
240 n = read(fd, packet, sizeof(*packet));
244 if (packet->cookie != htonl(DHCP_MAGIC))
247 check_broken_vendor(packet);
252 /* TODO: Use glib checksum */
253 uint16_t dhcp_checksum(void *addr, int count)
256 * Compute Internet Checksum for "count" bytes
257 * beginning at location "addr".
260 uint16_t *source = (uint16_t *) addr;
263 /* This is the inner loop */
268 /* Add left-over byte, if any */
270 /* Make sure that the left-over byte is added correctly both
271 * with little and big endian hosts */
273 *(uint8_t *) &tmp = *(uint8_t *) source;
276 /* Fold 32-bit sum to 16 bits */
278 sum = (sum & 0xffff) + (sum >> 16);
283 int dhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
284 uint32_t source_ip, int source_port, uint32_t dest_ip,
285 int dest_port, const uint8_t *dest_arp, int ifindex)
287 struct sockaddr_ll dest;
288 struct ip_udp_dhcp_packet packet;
292 IP_UPD_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) -
293 EXTEND_FOR_BUGGY_SERVERS,
294 UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE -
295 offsetof(struct ip_udp_dhcp_packet, udp),
298 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
302 memset(&dest, 0, sizeof(dest));
303 memset(&packet, 0, sizeof(packet));
304 packet.data = *dhcp_pkt;
306 dest.sll_family = AF_PACKET;
307 dest.sll_protocol = htons(ETH_P_IP);
308 dest.sll_ifindex = ifindex;
310 memcpy(dest.sll_addr, dest_arp, 6);
311 if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
316 packet.ip.protocol = IPPROTO_UDP;
317 packet.ip.saddr = source_ip;
318 packet.ip.daddr = dest_ip;
319 packet.udp.source = htons(source_port);
320 packet.udp.dest = htons(dest_port);
321 /* size, excluding IP header: */
322 packet.udp.len = htons(UPD_DHCP_SIZE);
323 /* for UDP checksumming, ip.len is set to UDP packet len */
324 packet.ip.tot_len = packet.udp.len;
325 packet.udp.check = dhcp_checksum(&packet, IP_UPD_DHCP_SIZE);
326 /* but for sending, it is set to IP packet len */
327 packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE);
328 packet.ip.ihl = sizeof(packet.ip) >> 2;
329 packet.ip.version = IPVERSION;
330 packet.ip.ttl = IPDEFTTL;
331 packet.ip.check = dhcp_checksum(&packet.ip, sizeof(packet.ip));
334 * Currently we send full-sized DHCP packets (zero padded).
335 * If you need to change this: last byte of the packet is
336 * packet.data.options[dhcp_end_option(packet.data.options)]
338 n = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
339 (struct sockaddr *) &dest, sizeof(dest));
348 int dhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
349 uint32_t source_ip, int source_port,
350 uint32_t dest_ip, int dest_port)
352 struct sockaddr_in client;
356 DHCP_SIZE = sizeof(struct dhcp_packet) -
357 EXTEND_FOR_BUGGY_SERVERS,
360 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
364 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
366 memset(&client, 0, sizeof(client));
367 client.sin_family = AF_INET;
368 client.sin_port = htons(source_port);
369 client.sin_addr.s_addr = source_ip;
370 if (bind(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
375 memset(&client, 0, sizeof(client));
376 client.sin_family = AF_INET;
377 client.sin_port = htons(dest_port);
378 client.sin_addr.s_addr = dest_ip;
379 if (connect(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
384 n = write(fd, dhcp_pkt, DHCP_SIZE);
394 int dhcp_l3_socket(int port, const char *interface)
397 struct sockaddr_in addr;
399 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
401 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
403 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
404 interface, strlen(interface) + 1) < 0) {
409 memset(&addr, 0, sizeof(addr));
410 addr.sin_family = AF_INET;
411 addr.sin_port = htons(port);
412 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {