dnsd: fix a number of bugs. Ideas by Ming-Ching Tiew (mctiew AT yahoo.com)
[oweals/busybox.git] / networking / udhcp / packet.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * packet.c -- packet ops
4  * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
5  *
6  * Licensed under GPLv2, see file LICENSE in this tarball for details.
7  */
8
9 #include <netinet/in.h>
10 #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
11 #include <netpacket/packet.h>
12 #include <net/ethernet.h>
13 #else
14 #include <asm/types.h>
15 #include <linux/if_packet.h>
16 #include <linux/if_ether.h>
17 #endif
18
19 #include "common.h"
20 #include "dhcpd.h"
21 #include "options.h"
22
23
24 void FAST_FUNC udhcp_init_header(struct dhcpMessage *packet, char type)
25 {
26         memset(packet, 0, sizeof(struct dhcpMessage));
27         packet->op = BOOTREQUEST; /* if client to a server */
28         switch (type) {
29         case DHCPOFFER:
30         case DHCPACK:
31         case DHCPNAK:
32                 packet->op = BOOTREPLY; /* if server to client */
33         }
34         packet->htype = ETH_10MB;
35         packet->hlen = ETH_10MB_LEN;
36         packet->cookie = htonl(DHCP_MAGIC);
37         packet->options[0] = DHCP_END;
38         add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
39 }
40
41
42 /* read a packet from socket fd, return -1 on read error, -2 on packet error */
43 int FAST_FUNC udhcp_recv_kernel_packet(struct dhcpMessage *packet, int fd)
44 {
45         int bytes;
46         unsigned char *vendor;
47
48         memset(packet, 0, sizeof(*packet));
49         bytes = safe_read(fd, packet, sizeof(*packet));
50         if (bytes < 0) {
51                 DEBUG("cannot read on listening socket, ignoring");
52                 return bytes; /* returns -1 */
53         }
54
55         if (packet->cookie != htonl(DHCP_MAGIC)) {
56                 bb_error_msg("received bogus message, ignoring");
57                 return -2;
58         }
59         DEBUG("Received a packet");
60
61         if (packet->op == BOOTREQUEST) {
62                 vendor = get_option(packet, DHCP_VENDOR);
63                 if (vendor) {
64 #if 0
65                         static const char broken_vendors[][8] = {
66                                 "MSFT 98",
67                                 ""
68                         };
69                         int i;
70                         for (i = 0; broken_vendors[i][0]; i++) {
71                                 if (vendor[OPT_LEN - 2] == (uint8_t)strlen(broken_vendors[i])
72                                  && !strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2])
73                                 ) {
74                                         DEBUG("broken client (%s), forcing broadcast replies",
75                                                 broken_vendors[i]);
76                                         packet->flags |= htons(BROADCAST_FLAG);
77                                 }
78                         }
79 #else
80                         if (vendor[OPT_LEN - 2] == (uint8_t)(sizeof("MSFT 98")-1)
81                          && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
82                         ) {
83                                 DEBUG("broken client (%s), forcing broadcast replies", "MSFT 98");
84                                 packet->flags |= htons(BROADCAST_FLAG);
85                         }
86 #endif
87                 }
88         }
89
90         return bytes;
91 }
92
93
94 uint16_t FAST_FUNC udhcp_checksum(void *addr, int count)
95 {
96         /* Compute Internet Checksum for "count" bytes
97          *         beginning at location "addr".
98          */
99         int32_t sum = 0;
100         uint16_t *source = (uint16_t *) addr;
101
102         while (count > 1)  {
103                 /*  This is the inner loop */
104                 sum += *source++;
105                 count -= 2;
106         }
107
108         /*  Add left-over byte, if any */
109         if (count > 0) {
110                 /* Make sure that the left-over byte is added correctly both
111                  * with little and big endian hosts */
112                 uint16_t tmp = 0;
113                 *(uint8_t*)&tmp = *(uint8_t*)source;
114                 sum += tmp;
115         }
116         /*  Fold 32-bit sum to 16 bits */
117         while (sum >> 16)
118                 sum = (sum & 0xffff) + (sum >> 16);
119
120         return ~sum;
121 }
122
123
124 /* Construct a ip/udp header for a packet, send packet */
125 int FAST_FUNC udhcp_send_raw_packet(struct dhcpMessage *payload,
126                 uint32_t source_ip, int source_port,
127                 uint32_t dest_ip, int dest_port, const uint8_t *dest_arp,
128                 int ifindex)
129 {
130         struct sockaddr_ll dest;
131         struct udp_dhcp_packet packet;
132         int fd;
133         int result = -1;
134         const char *msg;
135
136         enum {
137                 IP_UPD_DHCP_SIZE = sizeof(struct udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
138                 UPD_DHCP_SIZE    = IP_UPD_DHCP_SIZE - offsetof(struct udp_dhcp_packet, udp),
139         };
140
141         fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
142         if (fd < 0) {
143                 msg = "socket(%s)";
144                 goto ret_msg;
145         }
146
147         memset(&dest, 0, sizeof(dest));
148         memset(&packet, 0, sizeof(packet));
149         packet.data = *payload; /* struct copy */
150
151         dest.sll_family = AF_PACKET;
152         dest.sll_protocol = htons(ETH_P_IP);
153         dest.sll_ifindex = ifindex;
154         dest.sll_halen = 6;
155         memcpy(dest.sll_addr, dest_arp, 6);
156         if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
157                 msg = "bind(%s)";
158                 goto ret_close;
159         }
160
161         packet.ip.protocol = IPPROTO_UDP;
162         packet.ip.saddr = source_ip;
163         packet.ip.daddr = dest_ip;
164         packet.udp.source = htons(source_port);
165         packet.udp.dest = htons(dest_port);
166         /* size, excluding IP header: */
167         packet.udp.len = htons(UPD_DHCP_SIZE);
168         /* for UDP checksumming, ip.len is set to UDP packet len */
169         packet.ip.tot_len = packet.udp.len;
170         packet.udp.check = udhcp_checksum(&packet, IP_UPD_DHCP_SIZE);
171         /* but for sending, it is set to IP packet len */
172         packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE);
173         packet.ip.ihl = sizeof(packet.ip) >> 2;
174         packet.ip.version = IPVERSION;
175         packet.ip.ttl = IPDEFTTL;
176         packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
177
178         /* Currently we send full-sized DHCP packets (zero padded).
179          * If you need to change this: last byte of the packet is
180          * packet.data.options[end_option(packet.data.options)]
181          */
182         result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
183                                 (struct sockaddr *) &dest, sizeof(dest));
184         msg = "sendto";
185  ret_close:
186         close(fd);
187         if (result < 0) {
188  ret_msg:
189                 bb_perror_msg(msg, "PACKET");
190         }
191         return result;
192 }
193
194
195 /* Let the kernel do all the work for packet generation */
196 int FAST_FUNC udhcp_send_kernel_packet(struct dhcpMessage *payload,
197                 uint32_t source_ip, int source_port,
198                 uint32_t dest_ip, int dest_port)
199 {
200         struct sockaddr_in client;
201         int fd;
202         int result = -1;
203         const char *msg;
204
205         enum {
206                 DHCP_SIZE = sizeof(struct dhcpMessage) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
207         };
208
209         fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
210         if (fd < 0) {
211                 msg = "socket(%s)";
212                 goto ret_msg;
213         }
214         setsockopt_reuseaddr(fd);
215
216         memset(&client, 0, sizeof(client));
217         client.sin_family = AF_INET;
218         client.sin_port = htons(source_port);
219         client.sin_addr.s_addr = source_ip;
220         if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
221                 msg = "bind(%s)";
222                 goto ret_close;
223         }
224
225         memset(&client, 0, sizeof(client));
226         client.sin_family = AF_INET;
227         client.sin_port = htons(dest_port);
228         client.sin_addr.s_addr = dest_ip;
229         if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
230                 msg = "connect";
231                 goto ret_close;
232         }
233
234         /* Currently we send full-sized DHCP packets (see above) */
235         result = safe_write(fd, payload, DHCP_SIZE);
236         msg = "write";
237  ret_close:
238         close(fd);
239         if (result < 0) {
240  ret_msg:
241                 bb_perror_msg(msg, "UDP");
242         }
243         return result;
244 }