*: simplify Ethernet header includes
[oweals/busybox.git] / networking / udhcp / packet.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Packet ops
4  *
5  * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
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 void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type)
16 {
17         memset(packet, 0, sizeof(*packet));
18         packet->op = BOOTREQUEST; /* if client to a server */
19         switch (type) {
20         case DHCPOFFER:
21         case DHCPACK:
22         case DHCPNAK:
23                 packet->op = BOOTREPLY; /* if server to client */
24         }
25         packet->htype = 1; /* ethernet */
26         packet->hlen = 6;
27         packet->cookie = htonl(DHCP_MAGIC);
28         if (DHCP_END != 0)
29                 packet->options[0] = DHCP_END;
30         udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
31 }
32
33 #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
34 void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet)
35 {
36         char buf[sizeof(packet->chaddr)*2 + 1];
37
38         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"
50                 " yiaddr %x"
51                 " 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         );
75         *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
76         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 */
81 int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd)
82 {
83         int bytes;
84         unsigned char *vendor;
85
86         memset(packet, 0, sizeof(*packet));
87         bytes = safe_read(fd, packet, sizeof(*packet));
88         if (bytes < 0) {
89                 log1("Packet read error, ignoring");
90                 return bytes; /* returns -1 */
91         }
92
93         if (packet->cookie != htonl(DHCP_MAGIC)) {
94                 bb_info_msg("Packet with bad magic, ignoring");
95                 return -2;
96         }
97         log1("Received a packet");
98         udhcp_dump_packet(packet);
99
100         if (packet->op == BOOTREQUEST) {
101                 vendor = udhcp_get_option(packet, DHCP_VENDOR);
102                 if (vendor) {
103 #if 0
104                         static const char broken_vendors[][8] = {
105                                 "MSFT 98",
106                                 ""
107                         };
108                         int i;
109                         for (i = 0; broken_vendors[i][0]; i++) {
110                                 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
112                                 ) {
113                                         log1("Broken client (%s), forcing broadcast replies",
114                                                 broken_vendors[i]);
115                                         packet->flags |= htons(BROADCAST_FLAG);
116                                 }
117                         }
118 #else
119                         if (vendor[OPT_LEN - OPT_DATA] == (uint8_t)(sizeof("MSFT 98")-1)
120                          && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
121                         ) {
122                                 log1("Broken client (%s), forcing broadcast replies", "MSFT 98");
123                                 packet->flags |= htons(BROADCAST_FLAG);
124                         }
125 #endif
126                 }
127         }
128
129         return bytes;
130 }
131
132 uint16_t FAST_FUNC udhcp_checksum(void *addr, int count)
133 {
134         /* Compute Internet Checksum for "count" bytes
135          * beginning at location "addr".
136          */
137         int32_t sum = 0;
138         uint16_t *source = (uint16_t *) addr;
139
140         while (count > 1)  {
141                 /*  This is the inner loop */
142                 sum += *source++;
143                 count -= 2;
144         }
145
146         /*  Add left-over byte, if any */
147         if (count > 0) {
148                 /* Make sure that the left-over byte is added correctly both
149                  * with little and big endian hosts */
150                 uint16_t tmp = 0;
151                 *(uint8_t*)&tmp = *(uint8_t*)source;
152                 sum += tmp;
153         }
154         /*  Fold 32-bit sum to 16 bits */
155         while (sum >> 16)
156                 sum = (sum & 0xffff) + (sum >> 16);
157
158         return ~sum;
159 }
160
161 /* Construct a ip/udp header for a packet, send packet */
162 int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
163                 uint32_t source_nip, int source_port,
164                 uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
165                 int ifindex)
166 {
167         struct sockaddr_ll dest_sll;
168         struct ip_udp_dhcp_packet packet;
169         unsigned padding;
170         int fd;
171         int result = -1;
172         const char *msg;
173
174         fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
175         if (fd < 0) {
176                 msg = "socket(%s)";
177                 goto ret_msg;
178         }
179
180         memset(&dest_sll, 0, sizeof(dest_sll));
181         memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
182         packet.data = *dhcp_pkt; /* struct copy */
183
184         dest_sll.sll_family = AF_PACKET;
185         dest_sll.sll_protocol = htons(ETH_P_IP);
186         dest_sll.sll_ifindex = ifindex;
187         dest_sll.sll_halen = 6;
188         memcpy(dest_sll.sll_addr, dest_arp, 6);
189
190         if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
191                 msg = "bind(%s)";
192                 goto ret_close;
193         }
194
195         /* We were sending full-sized DHCP packets (zero padded),
196          * but some badly configured servers were seen dropping them.
197          * Apparently they drop all DHCP packets >576 *ethernet* octets big,
198          * whereas they may only drop packets >576 *IP* octets big
199          * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
200          *
201          * In order to work with those buggy servers,
202          * we truncate packets after end option byte.
203          */
204         padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
205
206         packet.ip.protocol = IPPROTO_UDP;
207         packet.ip.saddr = source_nip;
208         packet.ip.daddr = dest_nip;
209         packet.udp.source = htons(source_port);
210         packet.udp.dest = htons(dest_port);
211         /* size, excluding IP header: */
212         packet.udp.len = htons(UDP_DHCP_SIZE - padding);
213         /* for UDP checksumming, ip.len is set to UDP packet len */
214         packet.ip.tot_len = packet.udp.len;
215         packet.udp.check = udhcp_checksum(&packet, IP_UDP_DHCP_SIZE - padding);
216         /* but for sending, it is set to IP packet len */
217         packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
218         packet.ip.ihl = sizeof(packet.ip) >> 2;
219         packet.ip.version = IPVERSION;
220         packet.ip.ttl = IPDEFTTL;
221         packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
222
223         udhcp_dump_packet(dhcp_pkt);
224         result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
225                         (struct sockaddr *) &dest_sll, sizeof(dest_sll));
226         msg = "sendto";
227  ret_close:
228         close(fd);
229         if (result < 0) {
230  ret_msg:
231                 bb_perror_msg(msg, "PACKET");
232         }
233         return result;
234 }
235
236 /* Let the kernel do all the work for packet generation */
237 int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
238                 uint32_t source_nip, int source_port,
239                 uint32_t dest_nip, int dest_port)
240 {
241         struct sockaddr_in client;
242         unsigned padding;
243         int fd;
244         int result = -1;
245         const char *msg;
246
247         fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
248         if (fd < 0) {
249                 msg = "socket(%s)";
250                 goto ret_msg;
251         }
252         setsockopt_reuseaddr(fd);
253
254         memset(&client, 0, sizeof(client));
255         client.sin_family = AF_INET;
256         client.sin_port = htons(source_port);
257         client.sin_addr.s_addr = source_nip;
258         if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
259                 msg = "bind(%s)";
260                 goto ret_close;
261         }
262
263         memset(&client, 0, sizeof(client));
264         client.sin_family = AF_INET;
265         client.sin_port = htons(dest_port);
266         client.sin_addr.s_addr = dest_nip;
267         if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
268                 msg = "connect";
269                 goto ret_close;
270         }
271
272         udhcp_dump_packet(dhcp_pkt);
273
274         padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
275         result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
276         msg = "write";
277  ret_close:
278         close(fd);
279         if (result < 0) {
280  ret_msg:
281                 bb_perror_msg(msg, "UDP");
282         }
283         return result;
284 }