blob: 148f52551a129e179ea8fe031ddb36a81eaa09a2 [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;
Mike Frysinger7031f622006-05-08 03:20:50 +000084
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000085 memset(packet, 0, sizeof(*packet));
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +000086 bytes = safe_read(fd, packet, sizeof(*packet));
Mike Frysinger7031f622006-05-08 03:20:50 +000087 if (bytes < 0) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020088 log1("Packet read error, ignoring");
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +000089 return bytes; /* returns -1 */
Mike Frysinger7031f622006-05-08 03:20:50 +000090 }
91
Denys Vlasenko3b46fcb2011-11-05 01:26:18 +010092 if (bytes < offsetof(struct dhcp_packet, options)
93 || packet->cookie != htonl(DHCP_MAGIC)
94 ) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020095 bb_info_msg("Packet with bad magic, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +000096 return -2;
97 }
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020098 log1("Received a packet");
99 udhcp_dump_packet(packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000100
Mike Frysinger7031f622006-05-08 03:20:50 +0000101 return bytes;
102}
103
Denis Vlasenko61987922007-12-21 16:32:30 +0000104/* Construct a ip/udp header for a packet, send packet */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200105int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200106 uint32_t source_nip, int source_port,
107 uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
Denis Vlasenkoc321b512008-09-26 16:29:12 +0000108 int ifindex)
Mike Frysinger7031f622006-05-08 03:20:50 +0000109{
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200110 struct sockaddr_ll dest_sll;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200111 struct ip_udp_dhcp_packet packet;
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200112 unsigned padding;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000113 int fd;
114 int result = -1;
115 const char *msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000116
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000117 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
118 if (fd < 0) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000119 msg = "socket(%s)";
120 goto ret_msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000121 }
122
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200123 memset(&dest_sll, 0, sizeof(dest_sll));
124 memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200125 packet.data = *dhcp_pkt; /* struct copy */
Mike Frysinger7031f622006-05-08 03:20:50 +0000126
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200127 dest_sll.sll_family = AF_PACKET;
128 dest_sll.sll_protocol = htons(ETH_P_IP);
129 dest_sll.sll_ifindex = ifindex;
130 dest_sll.sll_halen = 6;
131 memcpy(dest_sll.sll_addr, dest_arp, 6);
132
133 if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000134 msg = "bind(%s)";
135 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000136 }
137
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200138 /* We were sending full-sized DHCP packets (zero padded),
139 * but some badly configured servers were seen dropping them.
140 * Apparently they drop all DHCP packets >576 *ethernet* octets big,
141 * whereas they may only drop packets >576 *IP* octets big
142 * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
143 *
144 * In order to work with those buggy servers,
145 * we truncate packets after end option byte.
Johannes Stezenbach2d576e22013-10-28 23:27:37 +0100146 *
147 * However, RFC 1542 says "The IP Total Length and UDP Length
148 * must be large enough to contain the minimal BOOTP header of 300 octets".
149 * Thus, we retain enough padding to not go below 300 BOOTP bytes.
150 * Some devices have filters which drop DHCP packets shorter than that.
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200151 */
152 padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
Johannes Stezenbach2d576e22013-10-28 23:27:37 +0100153 if (padding > DHCP_SIZE - 300)
154 padding = DHCP_SIZE - 300;
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200155
Mike Frysinger7031f622006-05-08 03:20:50 +0000156 packet.ip.protocol = IPPROTO_UDP;
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200157 packet.ip.saddr = source_nip;
158 packet.ip.daddr = dest_nip;
Mike Frysinger7031f622006-05-08 03:20:50 +0000159 packet.udp.source = htons(source_port);
160 packet.udp.dest = htons(dest_port);
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000161 /* size, excluding IP header: */
Denys Vlasenko2c3b71a2010-10-20 18:04:36 +0200162 packet.udp.len = htons(UDP_DHCP_SIZE - padding);
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000163 /* for UDP checksumming, ip.len is set to UDP packet len */
Mike Frysinger7031f622006-05-08 03:20:50 +0000164 packet.ip.tot_len = packet.udp.len;
Baruch Siache8f36332011-09-07 17:52:37 +0200165 packet.udp.check = inet_cksum((uint16_t *)&packet,
166 IP_UDP_DHCP_SIZE - padding);
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000167 /* but for sending, it is set to IP packet len */
Denys Vlasenko2c3b71a2010-10-20 18:04:36 +0200168 packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
Mike Frysinger7031f622006-05-08 03:20:50 +0000169 packet.ip.ihl = sizeof(packet.ip) >> 2;
170 packet.ip.version = IPVERSION;
171 packet.ip.ttl = IPDEFTTL;
Baruch Siache8f36332011-09-07 17:52:37 +0200172 packet.ip.check = inet_cksum((uint16_t *)&packet.ip, sizeof(packet.ip));
Mike Frysinger7031f622006-05-08 03:20:50 +0000173
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200174 udhcp_dump_packet(dhcp_pkt);
Denys Vlasenko2c3b71a2010-10-20 18:04:36 +0200175 result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200176 (struct sockaddr *) &dest_sll, sizeof(dest_sll));
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000177 msg = "sendto";
178 ret_close:
Mike Frysinger7031f622006-05-08 03:20:50 +0000179 close(fd);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000180 if (result < 0) {
181 ret_msg:
182 bb_perror_msg(msg, "PACKET");
183 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000184 return result;
185}
186
Mike Frysinger7031f622006-05-08 03:20:50 +0000187/* Let the kernel do all the work for packet generation */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200188int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
Denys Vlasenkob7d19cc2010-05-30 23:41:23 +0200189 uint32_t source_nip, int source_port,
190 uint32_t dest_nip, int dest_port)
Mike Frysinger7031f622006-05-08 03:20:50 +0000191{
Denys Vlasenko50089fc2011-11-07 15:44:46 +0100192 struct sockaddr_in sa;
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200193 unsigned padding;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000194 int fd;
195 int result = -1;
196 const char *msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000197
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000198 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000199 if (fd < 0) {
200 msg = "socket(%s)";
201 goto ret_msg;
202 }
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000203 setsockopt_reuseaddr(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000204
Denys Vlasenko50089fc2011-11-07 15:44:46 +0100205 memset(&sa, 0, sizeof(sa));
206 sa.sin_family = AF_INET;
207 sa.sin_port = htons(source_port);
208 sa.sin_addr.s_addr = source_nip;
209 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000210 msg = "bind(%s)";
211 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000212 }
213
Denys Vlasenko50089fc2011-11-07 15:44:46 +0100214 memset(&sa, 0, sizeof(sa));
215 sa.sin_family = AF_INET;
216 sa.sin_port = htons(dest_port);
217 sa.sin_addr.s_addr = dest_nip;
218 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000219 msg = "connect";
220 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000221 }
222
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200223 udhcp_dump_packet(dhcp_pkt);
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200224 padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
Johannes Stezenbach2d576e22013-10-28 23:27:37 +0100225 if (padding > DHCP_SIZE - 300)
226 padding = DHCP_SIZE - 300;
Denys Vlasenkob8b72f02010-05-31 00:45:09 +0200227 result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000228 msg = "write";
229 ret_close:
Mike Frysinger7031f622006-05-08 03:20:50 +0000230 close(fd);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000231 if (result < 0) {
232 ret_msg:
233 bb_perror_msg(msg, "UDP");
234 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000235 return result;
236}