blob: ad0028bd04f6a399732732e5e1f7ab905bc3361c [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 Vlasenko4ee824f2017-07-03 01:22:13 +020015#if ENABLE_UDHCPC || ENABLE_UDHCPD
Denys Vlasenko31af3d52009-06-17 11:57:09 +020016void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type)
Mike Frysinger7031f622006-05-08 03:20:50 +000017{
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010018 memset(packet, 0, sizeof(*packet));
Denis Vlasenko739e30f2008-09-26 23:45:20 +000019 packet->op = BOOTREQUEST; /* if client to a server */
Mike Frysinger7031f622006-05-08 03:20:50 +000020 switch (type) {
Mike Frysinger7031f622006-05-08 03:20:50 +000021 case DHCPOFFER:
22 case DHCPACK:
23 case DHCPNAK:
Denis Vlasenko739e30f2008-09-26 23:45:20 +000024 packet->op = BOOTREPLY; /* if server to client */
Mike Frysinger7031f622006-05-08 03:20:50 +000025 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010026 packet->htype = 1; /* ethernet */
27 packet->hlen = 6;
Mike Frysinger7031f622006-05-08 03:20:50 +000028 packet->cookie = htonl(DHCP_MAGIC);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010029 if (DHCP_END != 0)
30 packet->options[0] = DHCP_END;
Denys Vlasenko7724c762010-03-26 09:32:09 +010031 udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
Mike Frysinger7031f622006-05-08 03:20:50 +000032}
Denys Vlasenko4ee824f2017-07-03 01:22:13 +020033#endif
Mike Frysinger7031f622006-05-08 03:20:50 +000034
Denys Vlasenko7a76eba2009-06-19 13:51:29 +020035#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
Denys Vlasenko31af3d52009-06-17 11:57:09 +020036void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020037{
38 char buf[sizeof(packet->chaddr)*2 + 1];
Mike Frysinger7031f622006-05-08 03:20:50 +000039
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020040 if (dhcp_verbose < 2)
41 return;
42
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +020043 bb_error_msg(
44 //"op %x"
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020045 //" htype %x"
46 " hlen %x"
47 //" hops %x"
48 " xid %x"
49 //" secs %x"
50 //" flags %x"
51 " ciaddr %x"
Denys Vlasenko9038d6f2009-07-15 20:02:19 +020052 " yiaddr %x"
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020053 " siaddr %x"
54 " giaddr %x"
55 //" chaddr %s"
56 //" sname %s"
57 //" file %s"
58 //" cookie %x"
59 //" options %s"
60 //, packet->op
61 //, packet->htype
62 , packet->hlen
63 //, packet->hops
64 , packet->xid
65 //, packet->secs
66 //, packet->flags
67 , packet->ciaddr
68 , packet->yiaddr
69 , packet->siaddr_nip
70 , packet->gateway_nip
71 //, packet->chaddr[16]
72 //, packet->sname[64]
73 //, packet->file[128]
74 //, packet->cookie
75 //, packet->options[]
76 );
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020077 *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +020078 bb_error_msg("chaddr %s", buf);
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020079}
80#endif
81
82/* Read a packet from socket fd, return -1 on read error, -2 on packet error */
Denys Vlasenko31af3d52009-06-17 11:57:09 +020083int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd)
Mike Frysinger7031f622006-05-08 03:20:50 +000084{
Mike Frysinger7031f622006-05-08 03:20:50 +000085 int bytes;
Mike Frysinger7031f622006-05-08 03:20:50 +000086
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000087 memset(packet, 0, sizeof(*packet));
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +000088 bytes = safe_read(fd, packet, sizeof(*packet));
Mike Frysinger7031f622006-05-08 03:20:50 +000089 if (bytes < 0) {
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +020090 log1("packet read error, ignoring");
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +000091 return bytes; /* returns -1 */
Mike Frysinger7031f622006-05-08 03:20:50 +000092 }
93
Denys Vlasenko3b46fcb2011-11-05 01:26:18 +010094 if (bytes < offsetof(struct dhcp_packet, options)
95 || packet->cookie != htonl(DHCP_MAGIC)
96 ) {
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +020097 bb_error_msg("packet with bad magic, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +000098 return -2;
99 }
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +0200100 log1("received %s", "a packet");
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200101 udhcp_dump_packet(packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000102
Mike Frysinger7031f622006-05-08 03:20:50 +0000103 return bytes;
104}
105
Denis Vlasenko61987922007-12-21 16:32:30 +0000106/* Construct a ip/udp header for a packet, send packet */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200107int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200108 uint32_t source_nip, int source_port,
109 uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
Denis Vlasenkoc321b512008-09-26 16:29:12 +0000110 int ifindex)
Mike Frysinger7031f622006-05-08 03:20:50 +0000111{
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200112 struct sockaddr_ll dest_sll;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200113 struct ip_udp_dhcp_packet packet;
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200114 unsigned padding;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000115 int fd;
116 int result = -1;
117 const char *msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000118
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000119 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
120 if (fd < 0) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000121 msg = "socket(%s)";
122 goto ret_msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000123 }
124
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200125 memset(&dest_sll, 0, sizeof(dest_sll));
126 memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200127 packet.data = *dhcp_pkt; /* struct copy */
Mike Frysinger7031f622006-05-08 03:20:50 +0000128
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200129 dest_sll.sll_family = AF_PACKET;
130 dest_sll.sll_protocol = htons(ETH_P_IP);
131 dest_sll.sll_ifindex = ifindex;
Denys Vlasenko2b9acc62017-09-29 14:09:02 +0200132 /*dest_sll.sll_hatype = ARPHRD_???;*/
133 /*dest_sll.sll_pkttype = PACKET_???;*/
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200134 dest_sll.sll_halen = 6;
135 memcpy(dest_sll.sll_addr, dest_arp, 6);
136
137 if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000138 msg = "bind(%s)";
139 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000140 }
141
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200142 /* We were sending full-sized DHCP packets (zero padded),
143 * but some badly configured servers were seen dropping them.
144 * Apparently they drop all DHCP packets >576 *ethernet* octets big,
145 * whereas they may only drop packets >576 *IP* octets big
146 * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
147 *
148 * In order to work with those buggy servers,
149 * we truncate packets after end option byte.
Johannes Stezenbach2d576e22013-10-28 23:27:37 +0100150 *
151 * However, RFC 1542 says "The IP Total Length and UDP Length
152 * must be large enough to contain the minimal BOOTP header of 300 octets".
153 * Thus, we retain enough padding to not go below 300 BOOTP bytes.
154 * Some devices have filters which drop DHCP packets shorter than that.
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200155 */
156 padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
Johannes Stezenbach2d576e22013-10-28 23:27:37 +0100157 if (padding > DHCP_SIZE - 300)
158 padding = DHCP_SIZE - 300;
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200159
Mike Frysinger7031f622006-05-08 03:20:50 +0000160 packet.ip.protocol = IPPROTO_UDP;
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200161 packet.ip.saddr = source_nip;
162 packet.ip.daddr = dest_nip;
Mike Frysinger7031f622006-05-08 03:20:50 +0000163 packet.udp.source = htons(source_port);
164 packet.udp.dest = htons(dest_port);
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000165 /* size, excluding IP header: */
Denys Vlasenko2c3b71a2010-10-20 18:04:36 +0200166 packet.udp.len = htons(UDP_DHCP_SIZE - padding);
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000167 /* for UDP checksumming, ip.len is set to UDP packet len */
Mike Frysinger7031f622006-05-08 03:20:50 +0000168 packet.ip.tot_len = packet.udp.len;
Baruch Siache8f36332011-09-07 17:52:37 +0200169 packet.udp.check = inet_cksum((uint16_t *)&packet,
170 IP_UDP_DHCP_SIZE - padding);
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000171 /* but for sending, it is set to IP packet len */
Denys Vlasenko2c3b71a2010-10-20 18:04:36 +0200172 packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
Mike Frysinger7031f622006-05-08 03:20:50 +0000173 packet.ip.ihl = sizeof(packet.ip) >> 2;
174 packet.ip.version = IPVERSION;
175 packet.ip.ttl = IPDEFTTL;
Baruch Siache8f36332011-09-07 17:52:37 +0200176 packet.ip.check = inet_cksum((uint16_t *)&packet.ip, sizeof(packet.ip));
Mike Frysinger7031f622006-05-08 03:20:50 +0000177
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200178 udhcp_dump_packet(dhcp_pkt);
Denys Vlasenko2c3b71a2010-10-20 18:04:36 +0200179 result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200180 (struct sockaddr *) &dest_sll, sizeof(dest_sll));
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000181 msg = "sendto";
182 ret_close:
Mike Frysinger7031f622006-05-08 03:20:50 +0000183 close(fd);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000184 if (result < 0) {
185 ret_msg:
186 bb_perror_msg(msg, "PACKET");
187 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000188 return result;
189}
190
Mike Frysinger7031f622006-05-08 03:20:50 +0000191/* Let the kernel do all the work for packet generation */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200192int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200193 uint32_t source_nip, int source_port,
Denys Vlasenkoa6a3ad32017-09-29 15:55:24 +0200194 uint32_t dest_nip, int dest_port,
195 int send_flags)
Mike Frysinger7031f622006-05-08 03:20:50 +0000196{
Denys Vlasenko50089fc2011-11-07 15:44:46 +0100197 struct sockaddr_in sa;
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200198 unsigned padding;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000199 int fd;
200 int result = -1;
201 const char *msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000202
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000203 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000204 if (fd < 0) {
205 msg = "socket(%s)";
206 goto ret_msg;
207 }
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000208 setsockopt_reuseaddr(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000209
Denys Vlasenko50089fc2011-11-07 15:44:46 +0100210 memset(&sa, 0, sizeof(sa));
211 sa.sin_family = AF_INET;
212 sa.sin_port = htons(source_port);
213 sa.sin_addr.s_addr = source_nip;
214 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000215 msg = "bind(%s)";
216 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000217 }
218
Denys Vlasenko50089fc2011-11-07 15:44:46 +0100219 memset(&sa, 0, sizeof(sa));
220 sa.sin_family = AF_INET;
221 sa.sin_port = htons(dest_port);
222 sa.sin_addr.s_addr = dest_nip;
223 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000224 msg = "connect";
225 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000226 }
227
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200228 udhcp_dump_packet(dhcp_pkt);
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200229 padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
Johannes Stezenbach2d576e22013-10-28 23:27:37 +0100230 if (padding > DHCP_SIZE - 300)
231 padding = DHCP_SIZE - 300;
Denys Vlasenkoa6a3ad32017-09-29 15:55:24 +0200232 result = send(fd, dhcp_pkt, DHCP_SIZE - padding, send_flags);
233 msg = "send";
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000234 ret_close:
Mike Frysinger7031f622006-05-08 03:20:50 +0000235 close(fd);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000236 if (result < 0) {
237 ret_msg:
238 bb_perror_msg(msg, "UDP");
239 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000240 return result;
241}