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 log1("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_info_msg("packet with bad magic, ignoring");
96 return -2;
97 }
98 log1("received %s", "a packet");
99 udhcp_dump_packet(packet);
100
101 return bytes;
102}
103
104
105int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
106 uint32_t source_nip, int source_port,
107 uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
108 int ifindex)
109{
110 struct sockaddr_ll dest_sll;
111 struct ip_udp_dhcp_packet packet;
112 unsigned padding;
113 int fd;
114 int result = -1;
115 const char *msg;
116
117 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
118 if (fd < 0) {
119 msg = "socket(%s)";
120 goto ret_msg;
121 }
122
123 memset(&dest_sll, 0, sizeof(dest_sll));
124 memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
125 packet.data = *dhcp_pkt;
126
127 dest_sll.sll_family = AF_PACKET;
128 dest_sll.sll_protocol = htons(ETH_P_IP);
129 dest_sll.sll_ifindex = ifindex;
130
131
132 dest_sll.sll_halen = 6;
133 memcpy(dest_sll.sll_addr, dest_arp, 6);
134
135 if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
136 msg = "bind(%s)";
137 goto ret_close;
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
155 if (padding > DHCP_SIZE - 300)
156 padding = DHCP_SIZE - 300;
157
158 packet.ip.protocol = IPPROTO_UDP;
159 packet.ip.saddr = source_nip;
160 packet.ip.daddr = dest_nip;
161 packet.udp.source = htons(source_port);
162 packet.udp.dest = htons(dest_port);
163
164 packet.udp.len = htons(UDP_DHCP_SIZE - padding);
165
166 packet.ip.tot_len = packet.udp.len;
167 packet.udp.check = inet_cksum((uint16_t *)&packet,
168 IP_UDP_DHCP_SIZE - padding);
169
170 packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
171 packet.ip.ihl = sizeof(packet.ip) >> 2;
172 packet.ip.version = IPVERSION;
173 packet.ip.ttl = IPDEFTTL;
174 packet.ip.check = inet_cksum((uint16_t *)&packet.ip, sizeof(packet.ip));
175
176 udhcp_dump_packet(dhcp_pkt);
177 result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, 0,
178 (struct sockaddr *) &dest_sll, sizeof(dest_sll));
179 msg = "sendto";
180 ret_close:
181 close(fd);
182 if (result < 0) {
183 ret_msg:
184 bb_perror_msg(msg, "PACKET");
185 }
186 return result;
187}
188
189
190int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
191 uint32_t source_nip, int source_port,
192 uint32_t dest_nip, int dest_port)
193{
194 struct sockaddr_in sa;
195 unsigned padding;
196 int fd;
197 int result = -1;
198 const char *msg;
199
200 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
201 if (fd < 0) {
202 msg = "socket(%s)";
203 goto ret_msg;
204 }
205 setsockopt_reuseaddr(fd);
206
207 memset(&sa, 0, sizeof(sa));
208 sa.sin_family = AF_INET;
209 sa.sin_port = htons(source_port);
210 sa.sin_addr.s_addr = source_nip;
211 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
212 msg = "bind(%s)";
213 goto ret_close;
214 }
215
216 memset(&sa, 0, sizeof(sa));
217 sa.sin_family = AF_INET;
218 sa.sin_port = htons(dest_port);
219 sa.sin_addr.s_addr = dest_nip;
220 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
221 msg = "connect";
222 goto ret_close;
223 }
224
225 udhcp_dump_packet(dhcp_pkt);
226 padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
227 if (padding > DHCP_SIZE - 300)
228 padding = DHCP_SIZE - 300;
229 result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
230 msg = "write";
231 ret_close:
232 close(fd);
233 if (result < 0) {
234 ret_msg:
235 bb_perror_msg(msg, "UDP");
236 }
237 return result;
238}
239