1
2
3
4
5
6
7
8
9#include "common.h"
10#include "dhcpd.h"
11#include <netinet/in.h>
12#include <netinet/if_ether.h>
13#include <netpacket/packet.h>
14
15#if ENABLE_UDHCPC || ENABLE_UDHCPD
16void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type)
17{
18 memset(packet, 0, sizeof(*packet));
19 packet->op = BOOTREQUEST;
20 switch (type) {
21 case DHCPOFFER:
22 case DHCPACK:
23 case DHCPNAK:
24 packet->op = BOOTREPLY;
25 }
26 packet->htype = 1;
27 packet->hlen = 6;
28 packet->cookie = htonl(DHCP_MAGIC);
29 if (DHCP_END != 0)
30 packet->options[0] = DHCP_END;
31 udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
32}
33#endif
34
35#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
36void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet)
37{
38 char buf[sizeof(packet->chaddr)*2 + 1];
39
40 if (dhcp_verbose < 2)
41 return;
42
43 bb_info_msg(
44
45
46 " hlen %x"
47
48 " xid %x"
49
50
51 " ciaddr %x"
52 " yiaddr %x"
53 " siaddr %x"
54 " giaddr %x"
55
56
57
58
59
60
61 , packet->hlen
62
63 , packet->xid
64
65
66 , packet->ciaddr
67 , packet->yiaddr
68 , packet->siaddr_nip
69 , packet->gateway_nip
70
71
72
73
74 );
75 *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
76 bb_info_msg(" chaddr %s", buf);
77}
78#endif
79
80
81int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd)
82{
83 int bytes;
84
85 memset(packet, 0, sizeof(*packet));
86 bytes = safe_read(fd, packet, sizeof(*packet));
87 if (bytes < 0) {
88 log1s("packet read error, ignoring");
89 return bytes;
90 }
91
92 if (bytes < offsetof(struct dhcp_packet, options)
93 || packet->cookie != htonl(DHCP_MAGIC)
94 ) {
95 bb_simple_info_msg("packet with bad magic, ignoring");
96 return -2;
97 }
98 log2("received %s", "a packet");
99
100 udhcp_dump_packet(packet);
101
102 return bytes;
103}
104
105
106int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
107 uint32_t source_nip, int source_port,
108 uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
109 int ifindex)
110{
111 struct sockaddr_ll dest_sll;
112 struct ip_udp_dhcp_packet packet;
113 unsigned padding;
114 int fd;
115 int result = -1;
116 const char *msg;
117
118 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
119 if (fd < 0) {
120 msg = "socket(%s)";
121 goto ret_msg;
122 }
123
124 memset(&dest_sll, 0, sizeof(dest_sll));
125 memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
126 packet.data = *dhcp_pkt;
127
128 dest_sll.sll_family = AF_PACKET;
129 dest_sll.sll_protocol = htons(ETH_P_IP);
130 dest_sll.sll_ifindex = ifindex;
131
132
133 dest_sll.sll_halen = 6;
134 memcpy(dest_sll.sll_addr, dest_arp, 6);
135
136 if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
137 msg = "bind(%s)";
138 goto ret_close;
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
156 if (padding > DHCP_SIZE - 300)
157 padding = DHCP_SIZE - 300;
158
159 packet.ip.protocol = IPPROTO_UDP;
160 packet.ip.saddr = source_nip;
161 packet.ip.daddr = dest_nip;
162 packet.udp.source = htons(source_port);
163 packet.udp.dest = htons(dest_port);
164
165 packet.udp.len = htons(UDP_DHCP_SIZE - padding);
166
167 packet.ip.tot_len = packet.udp.len;
168 packet.udp.check = inet_cksum(&packet,
169 IP_UDP_DHCP_SIZE - padding);
170
171 packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
172 packet.ip.ihl = sizeof(packet.ip) >> 2;
173 packet.ip.version = IPVERSION;
174 packet.ip.ttl = IPDEFTTL;
175 packet.ip.check = inet_cksum(&packet.ip, sizeof(packet.ip));
176
177 udhcp_dump_packet(dhcp_pkt);
178 result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, 0,
179 (struct sockaddr *) &dest_sll, sizeof(dest_sll));
180 msg = "sendto";
181 ret_close:
182 close(fd);
183 if (result < 0) {
184 ret_msg:
185 bb_perror_msg(msg, "PACKET");
186 }
187 return result;
188}
189
190
191int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
192 uint32_t source_nip, int source_port,
193 uint32_t dest_nip, int dest_port,
194 const char *ifname)
195{
196 struct sockaddr_in sa;
197 unsigned padding;
198 int fd;
199 int result = -1;
200 const char *msg;
201
202 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
203 if (fd < 0) {
204 msg = "socket(%s)";
205 goto ret_msg;
206 }
207 setsockopt_reuseaddr(fd);
208
209
210
211
212
213
214
215
216
217 if (ifname) {
218 if (setsockopt_bindtodevice(fd, ifname) < 0) {
219 msg = "bindtodevice";
220 goto ret_close;
221 }
222 }
223
224 memset(&sa, 0, sizeof(sa));
225 sa.sin_family = AF_INET;
226 sa.sin_port = htons(source_port);
227 sa.sin_addr.s_addr = source_nip;
228 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
229 msg = "bind(%s)";
230 goto ret_close;
231 }
232
233 memset(&sa, 0, sizeof(sa));
234 sa.sin_family = AF_INET;
235 sa.sin_port = htons(dest_port);
236 sa.sin_addr.s_addr = dest_nip;
237 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
238 msg = "connect";
239 goto ret_close;
240 }
241
242 udhcp_dump_packet(dhcp_pkt);
243 padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
244 if (padding > DHCP_SIZE - 300)
245 padding = DHCP_SIZE - 300;
246 result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
247 msg = "write";
248 ret_close:
249 close(fd);
250 if (result < 0) {
251 ret_msg:
252 bb_perror_msg(msg, "UDP");
253 }
254 return result;
255}
256