blob: 0f2a3bc6f19c0d0a62fd2b786eff0804956fabbd [file] [log] [blame]
Russ Dill61fb4892002-10-14 21:41:28 +00001#include <unistd.h>
2#include <string.h>
3#include <netinet/in.h>
4#include <sys/types.h>
5#include <sys/socket.h>
6#include <features.h>
7#if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
8#include <netpacket/packet.h>
9#include <net/ethernet.h>
10#else
11#include <asm/types.h>
12#include <linux/if_packet.h>
13#include <linux/if_ether.h>
14#endif
15#include <errno.h>
16
Russ Dill61fb4892002-10-14 21:41:28 +000017#include "dhcpd.h"
18#include "options.h"
Glenn L McGrath24833432003-06-10 17:22:49 +000019#include "common.h"
Russ Dill61fb4892002-10-14 21:41:28 +000020
21
22void init_header(struct dhcpMessage *packet, char type)
23{
24 memset(packet, 0, sizeof(struct dhcpMessage));
25 switch (type) {
26 case DHCPDISCOVER:
27 case DHCPREQUEST:
28 case DHCPRELEASE:
29 case DHCPINFORM:
30 packet->op = BOOTREQUEST;
31 break;
32 case DHCPOFFER:
33 case DHCPACK:
34 case DHCPNAK:
35 packet->op = BOOTREPLY;
36 }
37 packet->htype = ETH_10MB;
38 packet->hlen = ETH_10MB_LEN;
39 packet->cookie = htonl(DHCP_MAGIC);
40 packet->options[0] = DHCP_END;
41 add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
42}
43
44
45/* read a packet from socket fd, return -1 on read error, -2 on packet error */
46int get_packet(struct dhcpMessage *packet, int fd)
47{
48 int bytes;
49 int i;
50 const char broken_vendors[][8] = {
51 "MSFT 98",
52 ""
53 };
54 char unsigned *vendor;
55
56 memset(packet, 0, sizeof(struct dhcpMessage));
57 bytes = read(fd, packet, sizeof(struct dhcpMessage));
58 if (bytes < 0) {
59 DEBUG(LOG_INFO, "couldn't read on listening socket, ignoring");
60 return -1;
61 }
62
63 if (ntohl(packet->cookie) != DHCP_MAGIC) {
64 LOG(LOG_ERR, "received bogus message, ignoring");
65 return -2;
66 }
67 DEBUG(LOG_INFO, "Received a packet");
68
69 if (packet->op == BOOTREQUEST && (vendor = get_option(packet, DHCP_VENDOR))) {
70 for (i = 0; broken_vendors[i][0]; i++) {
71 if (vendor[OPT_LEN - 2] == (unsigned char) strlen(broken_vendors[i]) &&
72 !strncmp(vendor, broken_vendors[i], vendor[OPT_LEN - 2])) {
73 DEBUG(LOG_INFO, "broken client (%s), forcing broadcast",
74 broken_vendors[i]);
75 packet->flags |= htons(BROADCAST_FLAG);
76 }
77 }
78 }
79
80
81 return bytes;
82}
83
84
85u_int16_t checksum(void *addr, int count)
86{
87 /* Compute Internet Checksum for "count" bytes
88 * beginning at location "addr".
89 */
90 register int32_t sum = 0;
91 u_int16_t *source = (u_int16_t *) addr;
92
93 while (count > 1) {
94 /* This is the inner loop */
95 sum += *source++;
96 count -= 2;
97 }
98
99 /* Add left-over byte, if any */
100 if (count > 0) {
101 /* Make sure that the left-over byte is added correctly both
102 * with little and big endian hosts */
103 u_int16_t tmp = 0;
104 *(unsigned char *) (&tmp) = * (unsigned char *) source;
105 sum += tmp;
106 }
107 /* Fold 32-bit sum to 16 bits */
108 while (sum >> 16)
109 sum = (sum & 0xffff) + (sum >> 16);
110
111 return ~sum;
112}
113
114
115/* Constuct a ip/udp header for a packet, and specify the source and dest hardware address */
116int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port,
117 u_int32_t dest_ip, int dest_port, unsigned char *dest_arp, int ifindex)
118{
119 int fd;
120 int result;
121 struct sockaddr_ll dest;
122 struct udp_dhcp_packet packet;
123
124 if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000125 DEBUG(LOG_ERR, "socket call failed: %m");
Russ Dill61fb4892002-10-14 21:41:28 +0000126 return -1;
127 }
128
129 memset(&dest, 0, sizeof(dest));
130 memset(&packet, 0, sizeof(packet));
131
132 dest.sll_family = AF_PACKET;
133 dest.sll_protocol = htons(ETH_P_IP);
134 dest.sll_ifindex = ifindex;
135 dest.sll_halen = 6;
136 memcpy(dest.sll_addr, dest_arp, 6);
137 if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000138 DEBUG(LOG_ERR, "bind call failed: %m");
Russ Dill61fb4892002-10-14 21:41:28 +0000139 close(fd);
140 return -1;
141 }
142
143 packet.ip.protocol = IPPROTO_UDP;
144 packet.ip.saddr = source_ip;
145 packet.ip.daddr = dest_ip;
146 packet.udp.source = htons(source_port);
147 packet.udp.dest = htons(dest_port);
148 packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage)); /* cheat on the psuedo-header */
149 packet.ip.tot_len = packet.udp.len;
150 memcpy(&(packet.data), payload, sizeof(struct dhcpMessage));
151 packet.udp.check = checksum(&packet, sizeof(struct udp_dhcp_packet));
152
153 packet.ip.tot_len = htons(sizeof(struct udp_dhcp_packet));
154 packet.ip.ihl = sizeof(packet.ip) >> 2;
155 packet.ip.version = IPVERSION;
156 packet.ip.ttl = IPDEFTTL;
157 packet.ip.check = checksum(&(packet.ip), sizeof(packet.ip));
158
159 result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0, (struct sockaddr *) &dest, sizeof(dest));
160 if (result <= 0) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000161 DEBUG(LOG_ERR, "write on socket failed: %m");
Russ Dill61fb4892002-10-14 21:41:28 +0000162 }
163 close(fd);
164 return result;
165}
166
167
168/* Let the kernel do all the work for packet generation */
169int kernel_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port,
170 u_int32_t dest_ip, int dest_port)
171{
172 int n = 1;
173 int fd, result;
174 struct sockaddr_in client;
175
176 if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
177 return -1;
178
179 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1)
180 return -1;
181
182 memset(&client, 0, sizeof(client));
183 client.sin_family = AF_INET;
184 client.sin_port = htons(source_port);
185 client.sin_addr.s_addr = source_ip;
186
187 if (bind(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1)
188 return -1;
189
190 memset(&client, 0, sizeof(client));
191 client.sin_family = AF_INET;
192 client.sin_port = htons(dest_port);
193 client.sin_addr.s_addr = dest_ip;
194
195 if (connect(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1)
196 return -1;
197
198 result = write(fd, payload, sizeof(struct dhcpMessage));
199 close(fd);
200 return result;
201}