blob: f12ecd6ec6bdb082f29119d05e4efbb132857300 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002#include <unistd.h>
3#include <string.h>
4#include <netinet/in.h>
5#include <sys/types.h>
6#include <sys/socket.h>
7#include <features.h>
8#if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || defined _NEWLIB_VERSION
9#include <netpacket/packet.h>
10#include <net/ethernet.h>
11#else
12#include <asm/types.h>
13#include <linux/if_packet.h>
14#include <linux/if_ether.h>
15#endif
16#include <errno.h>
17
18#include "common.h"
19#include "packet.h"
20#include "dhcpd.h"
21#include "options.h"
22
23
Rob Landley3f785612006-05-28 01:06:36 +000024void udhcp_init_header(struct dhcpMessage *packet, char type)
Mike Frysinger7031f622006-05-08 03:20:50 +000025{
26 memset(packet, 0, sizeof(struct dhcpMessage));
27 switch (type) {
28 case DHCPDISCOVER:
29 case DHCPREQUEST:
30 case DHCPRELEASE:
31 case DHCPINFORM:
32 packet->op = BOOTREQUEST;
33 break;
34 case DHCPOFFER:
35 case DHCPACK:
36 case DHCPNAK:
37 packet->op = BOOTREPLY;
38 }
39 packet->htype = ETH_10MB;
40 packet->hlen = ETH_10MB_LEN;
41 packet->cookie = htonl(DHCP_MAGIC);
42 packet->options[0] = DHCP_END;
43 add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
44}
45
46
47/* read a packet from socket fd, return -1 on read error, -2 on packet error */
Rob Landley3f785612006-05-28 01:06:36 +000048int udhcp_get_packet(struct dhcpMessage *packet, int fd)
Mike Frysinger7031f622006-05-08 03:20:50 +000049{
50 static const char broken_vendors[][8] = {
51 "MSFT 98",
52 ""
53 };
54 int bytes;
55 int i;
56 char unsigned *vendor;
57
58 memset(packet, 0, sizeof(struct dhcpMessage));
59 bytes = read(fd, packet, sizeof(struct dhcpMessage));
60 if (bytes < 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000061 DEBUG("cannot read on listening socket, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +000062 return -1;
63 }
64
65 if (ntohl(packet->cookie) != DHCP_MAGIC) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000066 bb_error_msg("received bogus message, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +000067 return -2;
68 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000069 DEBUG("Received a packet");
Mike Frysinger7031f622006-05-08 03:20:50 +000070
71 if (packet->op == BOOTREQUEST && (vendor = get_option(packet, DHCP_VENDOR))) {
72 for (i = 0; broken_vendors[i][0]; i++) {
73 if (vendor[OPT_LEN - 2] == (uint8_t) strlen(broken_vendors[i]) &&
74 !strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2])) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000075 DEBUG("broken client (%s), forcing broadcast",
Mike Frysinger7031f622006-05-08 03:20:50 +000076 broken_vendors[i]);
77 packet->flags |= htons(BROADCAST_FLAG);
78 }
79 }
80 }
81
82 return bytes;
83}
84
85
Rob Landley3f785612006-05-28 01:06:36 +000086uint16_t udhcp_checksum(void *addr, int count)
Mike Frysinger7031f622006-05-08 03:20:50 +000087{
88 /* Compute Internet Checksum for "count" bytes
89 * beginning at location "addr".
90 */
"Robert P. J. Day"68229832006-07-01 13:08:46 +000091 int32_t sum = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000092 uint16_t *source = (uint16_t *) addr;
93
94 while (count > 1) {
95 /* This is the inner loop */
96 sum += *source++;
97 count -= 2;
98 }
99
100 /* Add left-over byte, if any */
101 if (count > 0) {
102 /* Make sure that the left-over byte is added correctly both
103 * with little and big endian hosts */
104 uint16_t tmp = 0;
105 *(uint8_t *) (&tmp) = * (uint8_t *) source;
106 sum += tmp;
107 }
108 /* Fold 32-bit sum to 16 bits */
109 while (sum >> 16)
110 sum = (sum & 0xffff) + (sum >> 16);
111
112 return ~sum;
113}
114
115
116/* Construct a ip/udp header for a packet, and specify the source and dest hardware address */
Rob Landley3f785612006-05-28 01:06:36 +0000117int udhcp_raw_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port,
Mike Frysinger7031f622006-05-08 03:20:50 +0000118 uint32_t dest_ip, int dest_port, uint8_t *dest_arp, int ifindex)
119{
120 int fd;
121 int result;
122 struct sockaddr_ll dest;
123 struct udp_dhcp_packet packet;
124
125 if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000126 bb_perror_msg("socket");
Mike Frysinger7031f622006-05-08 03:20:50 +0000127 return -1;
128 }
129
130 memset(&dest, 0, sizeof(dest));
131 memset(&packet, 0, sizeof(packet));
132
133 dest.sll_family = AF_PACKET;
134 dest.sll_protocol = htons(ETH_P_IP);
135 dest.sll_ifindex = ifindex;
136 dest.sll_halen = 6;
137 memcpy(dest.sll_addr, dest_arp, 6);
138 if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000139 bb_perror_msg("bind");
Mike Frysinger7031f622006-05-08 03:20:50 +0000140 close(fd);
141 return -1;
142 }
143
144 packet.ip.protocol = IPPROTO_UDP;
145 packet.ip.saddr = source_ip;
146 packet.ip.daddr = dest_ip;
147 packet.udp.source = htons(source_port);
148 packet.udp.dest = htons(dest_port);
149 packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage)); /* cheat on the psuedo-header */
150 packet.ip.tot_len = packet.udp.len;
151 memcpy(&(packet.data), payload, sizeof(struct dhcpMessage));
Rob Landley3f785612006-05-28 01:06:36 +0000152 packet.udp.check = udhcp_checksum(&packet, sizeof(struct udp_dhcp_packet));
Mike Frysinger7031f622006-05-08 03:20:50 +0000153
154 packet.ip.tot_len = htons(sizeof(struct udp_dhcp_packet));
155 packet.ip.ihl = sizeof(packet.ip) >> 2;
156 packet.ip.version = IPVERSION;
157 packet.ip.ttl = IPDEFTTL;
Rob Landley3f785612006-05-28 01:06:36 +0000158 packet.ip.check = udhcp_checksum(&(packet.ip), sizeof(packet.ip));
Mike Frysinger7031f622006-05-08 03:20:50 +0000159
160 result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0, (struct sockaddr *) &dest, sizeof(dest));
161 if (result <= 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000162 bb_perror_msg("sendto");
Mike Frysinger7031f622006-05-08 03:20:50 +0000163 }
164 close(fd);
165 return result;
166}
167
168
169/* Let the kernel do all the work for packet generation */
Rob Landley3f785612006-05-28 01:06:36 +0000170int udhcp_kernel_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port,
Mike Frysinger7031f622006-05-08 03:20:50 +0000171 uint32_t dest_ip, int dest_port)
172{
173 int n = 1;
174 int fd, result;
175 struct sockaddr_in client;
176
177 if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
178 return -1;
179
180 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) {
181 close(fd);
182 return -1;
183 }
184
185 memset(&client, 0, sizeof(client));
186 client.sin_family = AF_INET;
187 client.sin_port = htons(source_port);
188 client.sin_addr.s_addr = source_ip;
189
190 if (bind(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) {
191 close(fd);
192 return -1;
193 }
194
195 memset(&client, 0, sizeof(client));
196 client.sin_family = AF_INET;
197 client.sin_port = htons(dest_port);
198 client.sin_addr.s_addr = dest_ip;
199
200 if (connect(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) {
201 close(fd);
202 return -1;
203 }
204
205 result = write(fd, payload, sizeof(struct dhcpMessage));
206 close(fd);
207 return result;
208}