blob: 4d5ff0676eb18123be50c8cb0f6bcac99736d39b [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00002/*
Denys Vlasenko385b4562010-03-26 10:09:34 +01003 * Packet ops
4 *
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00005 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00008 */
Mike Frysinger7031f622006-05-08 03:20:50 +00009#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000010#include "dhcpd.h"
Dan Fandrichf533ec82011-06-10 05:17:59 +020011#include <netinet/in.h>
12#include <netinet/if_ether.h>
13#include <netpacket/packet.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000014
Denys Vlasenko31af3d52009-06-17 11:57:09 +020015void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type)
Mike Frysinger7031f622006-05-08 03:20:50 +000016{
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010017 memset(packet, 0, sizeof(*packet));
Denis Vlasenko739e30f2008-09-26 23:45:20 +000018 packet->op = BOOTREQUEST; /* if client to a server */
Mike Frysinger7031f622006-05-08 03:20:50 +000019 switch (type) {
Mike Frysinger7031f622006-05-08 03:20:50 +000020 case DHCPOFFER:
21 case DHCPACK:
22 case DHCPNAK:
Denis Vlasenko739e30f2008-09-26 23:45:20 +000023 packet->op = BOOTREPLY; /* if server to client */
Mike Frysinger7031f622006-05-08 03:20:50 +000024 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010025 packet->htype = 1; /* ethernet */
26 packet->hlen = 6;
Mike Frysinger7031f622006-05-08 03:20:50 +000027 packet->cookie = htonl(DHCP_MAGIC);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010028 if (DHCP_END != 0)
29 packet->options[0] = DHCP_END;
Denys Vlasenko7724c762010-03-26 09:32:09 +010030 udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
Mike Frysinger7031f622006-05-08 03:20:50 +000031}
32
Denys Vlasenko7a76eba2009-06-19 13:51:29 +020033#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
Denys Vlasenko31af3d52009-06-17 11:57:09 +020034void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020035{
36 char buf[sizeof(packet->chaddr)*2 + 1];
Mike Frysinger7031f622006-05-08 03:20:50 +000037
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020038 if (dhcp_verbose < 2)
39 return;
40
41 bb_info_msg(
42 //" op %x"
43 //" htype %x"
44 " hlen %x"
45 //" hops %x"
46 " xid %x"
47 //" secs %x"
48 //" flags %x"
49 " ciaddr %x"
Denys Vlasenko9038d6f2009-07-15 20:02:19 +020050 " yiaddr %x"
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020051 " siaddr %x"
52 " giaddr %x"
53 //" chaddr %s"
54 //" sname %s"
55 //" file %s"
56 //" cookie %x"
57 //" options %s"
58 //, packet->op
59 //, packet->htype
60 , packet->hlen
61 //, packet->hops
62 , packet->xid
63 //, packet->secs
64 //, packet->flags
65 , packet->ciaddr
66 , packet->yiaddr
67 , packet->siaddr_nip
68 , packet->gateway_nip
69 //, packet->chaddr[16]
70 //, packet->sname[64]
71 //, packet->file[128]
72 //, packet->cookie
73 //, packet->options[]
74 );
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020075 *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020076 bb_info_msg(" chaddr %s", buf);
77}
78#endif
79
80/* Read a packet from socket fd, return -1 on read error, -2 on packet error */
Denys Vlasenko31af3d52009-06-17 11:57:09 +020081int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd)
Mike Frysinger7031f622006-05-08 03:20:50 +000082{
Mike Frysinger7031f622006-05-08 03:20:50 +000083 int bytes;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000084 unsigned char *vendor;
Mike Frysinger7031f622006-05-08 03:20:50 +000085
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000086 memset(packet, 0, sizeof(*packet));
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +000087 bytes = safe_read(fd, packet, sizeof(*packet));
Mike Frysinger7031f622006-05-08 03:20:50 +000088 if (bytes < 0) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020089 log1("Packet read error, ignoring");
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +000090 return bytes; /* returns -1 */
Mike Frysinger7031f622006-05-08 03:20:50 +000091 }
92
Denis Vlasenko6884f662007-11-23 00:08:54 +000093 if (packet->cookie != htonl(DHCP_MAGIC)) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020094 bb_info_msg("Packet with bad magic, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +000095 return -2;
96 }
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020097 log1("Received a packet");
98 udhcp_dump_packet(packet);
Mike Frysinger7031f622006-05-08 03:20:50 +000099
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000100 if (packet->op == BOOTREQUEST) {
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100101 vendor = udhcp_get_option(packet, DHCP_VENDOR);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000102 if (vendor) {
103#if 0
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000104 static const char broken_vendors[][8] = {
105 "MSFT 98",
106 ""
107 };
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000108 int i;
109 for (i = 0; broken_vendors[i][0]; i++) {
Denys Vlasenko37a658c2010-03-23 15:43:08 +0100110 if (vendor[OPT_LEN - OPT_DATA] == (uint8_t)strlen(broken_vendors[i])
111 && strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - OPT_DATA]) == 0
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000112 ) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200113 log1("Broken client (%s), forcing broadcast replies",
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000114 broken_vendors[i]);
115 packet->flags |= htons(BROADCAST_FLAG);
116 }
117 }
118#else
Denys Vlasenko37a658c2010-03-23 15:43:08 +0100119 if (vendor[OPT_LEN - OPT_DATA] == (uint8_t)(sizeof("MSFT 98")-1)
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000120 && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000121 ) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200122 log1("Broken client (%s), forcing broadcast replies", "MSFT 98");
Mike Frysinger7031f622006-05-08 03:20:50 +0000123 packet->flags |= htons(BROADCAST_FLAG);
124 }
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000125#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000126 }
127 }
128
129 return bytes;
130}
131
Denis Vlasenko61987922007-12-21 16:32:30 +0000132/* Construct a ip/udp header for a packet, send packet */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200133int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200134 uint32_t source_nip, int source_port,
135 uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
Denis Vlasenkoc321b512008-09-26 16:29:12 +0000136 int ifindex)
Mike Frysinger7031f622006-05-08 03:20:50 +0000137{
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200138 struct sockaddr_ll dest_sll;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200139 struct ip_udp_dhcp_packet packet;
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200140 unsigned padding;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000141 int fd;
142 int result = -1;
143 const char *msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000144
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000145 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
146 if (fd < 0) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000147 msg = "socket(%s)";
148 goto ret_msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000149 }
150
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200151 memset(&dest_sll, 0, sizeof(dest_sll));
152 memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200153 packet.data = *dhcp_pkt; /* struct copy */
Mike Frysinger7031f622006-05-08 03:20:50 +0000154
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200155 dest_sll.sll_family = AF_PACKET;
156 dest_sll.sll_protocol = htons(ETH_P_IP);
157 dest_sll.sll_ifindex = ifindex;
158 dest_sll.sll_halen = 6;
159 memcpy(dest_sll.sll_addr, dest_arp, 6);
160
161 if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000162 msg = "bind(%s)";
163 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000164 }
165
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200166 /* We were sending full-sized DHCP packets (zero padded),
167 * but some badly configured servers were seen dropping them.
168 * Apparently they drop all DHCP packets >576 *ethernet* octets big,
169 * whereas they may only drop packets >576 *IP* octets big
170 * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
171 *
172 * In order to work with those buggy servers,
173 * we truncate packets after end option byte.
174 */
175 padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
176
Mike Frysinger7031f622006-05-08 03:20:50 +0000177 packet.ip.protocol = IPPROTO_UDP;
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200178 packet.ip.saddr = source_nip;
179 packet.ip.daddr = dest_nip;
Mike Frysinger7031f622006-05-08 03:20:50 +0000180 packet.udp.source = htons(source_port);
181 packet.udp.dest = htons(dest_port);
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000182 /* size, excluding IP header: */
Denys Vlasenko2c3b71a2010-10-20 18:04:36 +0200183 packet.udp.len = htons(UDP_DHCP_SIZE - padding);
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000184 /* for UDP checksumming, ip.len is set to UDP packet len */
Mike Frysinger7031f622006-05-08 03:20:50 +0000185 packet.ip.tot_len = packet.udp.len;
Baruch Siache8f36332011-09-07 17:52:37 +0200186 packet.udp.check = inet_cksum((uint16_t *)&packet,
187 IP_UDP_DHCP_SIZE - padding);
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000188 /* but for sending, it is set to IP packet len */
Denys Vlasenko2c3b71a2010-10-20 18:04:36 +0200189 packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
Mike Frysinger7031f622006-05-08 03:20:50 +0000190 packet.ip.ihl = sizeof(packet.ip) >> 2;
191 packet.ip.version = IPVERSION;
192 packet.ip.ttl = IPDEFTTL;
Baruch Siache8f36332011-09-07 17:52:37 +0200193 packet.ip.check = inet_cksum((uint16_t *)&packet.ip, sizeof(packet.ip));
Mike Frysinger7031f622006-05-08 03:20:50 +0000194
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200195 udhcp_dump_packet(dhcp_pkt);
Denys Vlasenko2c3b71a2010-10-20 18:04:36 +0200196 result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200197 (struct sockaddr *) &dest_sll, sizeof(dest_sll));
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000198 msg = "sendto";
199 ret_close:
Mike Frysinger7031f622006-05-08 03:20:50 +0000200 close(fd);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000201 if (result < 0) {
202 ret_msg:
203 bb_perror_msg(msg, "PACKET");
204 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000205 return result;
206}
207
Mike Frysinger7031f622006-05-08 03:20:50 +0000208/* Let the kernel do all the work for packet generation */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200209int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200210 uint32_t source_nip, int source_port,
211 uint32_t dest_nip, int dest_port)
Mike Frysinger7031f622006-05-08 03:20:50 +0000212{
Mike Frysinger7031f622006-05-08 03:20:50 +0000213 struct sockaddr_in client;
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200214 unsigned padding;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000215 int fd;
216 int result = -1;
217 const char *msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000218
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000219 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000220 if (fd < 0) {
221 msg = "socket(%s)";
222 goto ret_msg;
223 }
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000224 setsockopt_reuseaddr(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000225
226 memset(&client, 0, sizeof(client));
227 client.sin_family = AF_INET;
228 client.sin_port = htons(source_port);
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200229 client.sin_addr.s_addr = source_nip;
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000230 if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000231 msg = "bind(%s)";
232 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000233 }
234
235 memset(&client, 0, sizeof(client));
236 client.sin_family = AF_INET;
237 client.sin_port = htons(dest_port);
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200238 client.sin_addr.s_addr = dest_nip;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000239 if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
240 msg = "connect";
241 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000242 }
243
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200244 udhcp_dump_packet(dhcp_pkt);
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200245
246 padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
247 result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000248 msg = "write";
249 ret_close:
Mike Frysinger7031f622006-05-08 03:20:50 +0000250 close(fd);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000251 if (result < 0) {
252 ret_msg:
253 bb_perror_msg(msg, "UDP");
254 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000255 return result;
256}