c3890229f406c0ca9d035709c3475ba470ed262a
[oweals/busybox.git] / networking / udhcp / packet.c
1 /* vi: set sw=4 ts=4: */
2
3 #include <netinet/in.h>
4 #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
5 #include <netpacket/packet.h>
6 #include <net/ethernet.h>
7 #else
8 #include <asm/types.h>
9 #include <linux/if_packet.h>
10 #include <linux/if_ether.h>
11 #endif
12
13 #include "common.h"
14 #include "dhcpd.h"
15 #include "options.h"
16
17
18 void udhcp_init_header(struct dhcpMessage *packet, char type)
19 {
20         memset(packet, 0, sizeof(struct dhcpMessage));
21         switch (type) {
22         case DHCPDISCOVER:
23         case DHCPREQUEST:
24         case DHCPRELEASE:
25         case DHCPINFORM:
26                 packet->op = BOOTREQUEST;
27                 break;
28         case DHCPOFFER:
29         case DHCPACK:
30         case DHCPNAK:
31                 packet->op = BOOTREPLY;
32         }
33         packet->htype = ETH_10MB;
34         packet->hlen = ETH_10MB_LEN;
35         packet->cookie = htonl(DHCP_MAGIC);
36         packet->options[0] = DHCP_END;
37         add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
38 }
39
40
41 /* read a packet from socket fd, return -1 on read error, -2 on packet error */
42 int udhcp_recv_packet(struct dhcpMessage *packet, int fd)
43 {
44 #if 0
45         static const char broken_vendors[][8] = {
46                 "MSFT 98",
47                 ""
48         };
49 #endif
50         int bytes;
51         unsigned char *vendor;
52
53         memset(packet, 0, sizeof(*packet));
54         bytes = read(fd, packet, sizeof(*packet));
55         if (bytes < 0) {
56                 DEBUG("cannot read on listening socket, ignoring");
57                 return -1;
58         }
59
60         if (packet->cookie != htonl(DHCP_MAGIC)) {
61                 bb_error_msg("received bogus message, ignoring");
62                 return -2;
63         }
64         DEBUG("Received a packet");
65
66         if (packet->op == BOOTREQUEST) {
67                 vendor = get_option(packet, DHCP_VENDOR);
68                 if (vendor) {
69 #if 0
70                         int i;
71                         for (i = 0; broken_vendors[i][0]; i++) {
72                                 if (vendor[OPT_LEN - 2] == (uint8_t)strlen(broken_vendors[i])
73                                  && !strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2])
74                                 ) {
75                                         DEBUG("broken client (%s), forcing broadcast",
76                                                 broken_vendors[i]);
77                                         packet->flags |= htons(BROADCAST_FLAG);
78                                 }
79                         }
80 #else
81                         if (vendor[OPT_LEN - 2] == (uint8_t)(sizeof("MSFT 98")-1)
82                          && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
83                         ) {
84                                 DEBUG("broken client (%s), forcing broadcast", "MSFT 98");
85                                 packet->flags |= htons(BROADCAST_FLAG);
86                         }
87 #endif
88                 }
89         }
90
91         return bytes;
92 }
93
94
95 uint16_t udhcp_checksum(void *addr, int count)
96 {
97         /* Compute Internet Checksum for "count" bytes
98          *         beginning at location "addr".
99          */
100         int32_t sum = 0;
101         uint16_t *source = (uint16_t *) addr;
102
103         while (count > 1)  {
104                 /*  This is the inner loop */
105                 sum += *source++;
106                 count -= 2;
107         }
108
109         /*  Add left-over byte, if any */
110         if (count > 0) {
111                 /* Make sure that the left-over byte is added correctly both
112                  * with little and big endian hosts */
113                 uint16_t tmp = 0;
114                 *(uint8_t*)&tmp = *(uint8_t*)source;
115                 sum += tmp;
116         }
117         /*  Fold 32-bit sum to 16 bits */
118         while (sum >> 16)
119                 sum = (sum & 0xffff) + (sum >> 16);
120
121         return ~sum;
122 }
123
124
125 /* Construct a ip/udp header for a packet, and specify the source and dest hardware address */
126 int udhcp_send_raw_packet(struct dhcpMessage *payload,
127                 uint32_t source_ip, int source_port,
128                 uint32_t dest_ip, int dest_port, const uint8_t *dest_arp, int ifindex)
129 {
130         int fd;
131         int result;
132         struct sockaddr_ll dest;
133         struct udp_dhcp_packet packet;
134
135         enum {
136                 IP_UPD_DHCP_SIZE = sizeof(struct udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
137                 UPD_DHCP_SIZE    = IP_UPD_DHCP_SIZE - offsetof(struct udp_dhcp_packet, udp),
138         };
139
140         fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
141         if (fd < 0) {
142                 bb_perror_msg("socket");
143                 return -1;
144         }
145
146         memset(&dest, 0, sizeof(dest));
147         memset(&packet, 0, sizeof(packet));
148         packet.data = *payload; /* struct copy */
149
150         dest.sll_family = AF_PACKET;
151         dest.sll_protocol = htons(ETH_P_IP);
152         dest.sll_ifindex = ifindex;
153         dest.sll_halen = 6;
154         memcpy(dest.sll_addr, dest_arp, 6);
155         if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) {
156                 bb_perror_msg("bind");
157                 close(fd);
158                 return -1;
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         result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0, (struct sockaddr *) &dest, sizeof(dest));
179         if (result <= 0) {
180                 bb_perror_msg("sendto");
181         }
182         close(fd);
183         return result;
184 }
185
186
187 /* Let the kernel do all the work for packet generation */
188 int udhcp_send_kernel_packet(struct dhcpMessage *payload,
189                 uint32_t source_ip, int source_port,
190                 uint32_t dest_ip, int dest_port)
191 {
192         int fd, result;
193         struct sockaddr_in client;
194
195         enum {
196                 DHCP_SIZE = sizeof(struct dhcpMessage) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
197         };
198
199         fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
200         if (fd < 0)
201                 return -1;
202
203         setsockopt_reuseaddr(fd);
204
205         memset(&client, 0, sizeof(client));
206         client.sin_family = AF_INET;
207         client.sin_port = htons(source_port);
208         client.sin_addr.s_addr = source_ip;
209
210         if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
211                 close(fd);
212                 return -1;
213         }
214
215         memset(&client, 0, sizeof(client));
216         client.sin_family = AF_INET;
217         client.sin_port = htons(dest_port);
218         client.sin_addr.s_addr = dest_ip;
219
220         if (connect(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) {
221                 close(fd);
222                 return -1;
223         }
224
225         result = write(fd, payload, DHCP_SIZE);
226         close(fd);
227         return result;
228 }