blob: fe74b828f30c5d635830599642f84b99aa60da0d [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 Dill76729b82003-12-16 20:44:15 +000017#include "packet.h"
Russ Dill61fb4892002-10-14 21:41:28 +000018#include "dhcpd.h"
19#include "options.h"
Glenn L McGrath24833432003-06-10 17:22:49 +000020#include "common.h"
Russ Dill61fb4892002-10-14 21:41:28 +000021
22
23void init_header(struct dhcpMessage *packet, char type)
24{
25 memset(packet, 0, sizeof(struct dhcpMessage));
26 switch (type) {
27 case DHCPDISCOVER:
28 case DHCPREQUEST:
29 case DHCPRELEASE:
30 case DHCPINFORM:
31 packet->op = BOOTREQUEST;
32 break;
33 case DHCPOFFER:
34 case DHCPACK:
35 case DHCPNAK:
36 packet->op = BOOTREPLY;
37 }
38 packet->htype = ETH_10MB;
39 packet->hlen = ETH_10MB_LEN;
40 packet->cookie = htonl(DHCP_MAGIC);
41 packet->options[0] = DHCP_END;
42 add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
43}
44
45
46/* read a packet from socket fd, return -1 on read error, -2 on packet error */
47int get_packet(struct dhcpMessage *packet, int fd)
48{
Rob Landley0a7c8ef2006-02-22 17:01:00 +000049 static const char broken_vendors[][8] = {
Russ Dill61fb4892002-10-14 21:41:28 +000050 "MSFT 98",
51 ""
52 };
Rob Landley0a7c8ef2006-02-22 17:01:00 +000053
54 int bytes;
55 int i;
Russ Dill61fb4892002-10-14 21:41:28 +000056 char unsigned *vendor;
57
58 memset(packet, 0, sizeof(struct dhcpMessage));
59 bytes = read(fd, packet, sizeof(struct dhcpMessage));
60 if (bytes < 0) {
61 DEBUG(LOG_INFO, "couldn't read on listening socket, ignoring");
62 return -1;
63 }
64
65 if (ntohl(packet->cookie) != DHCP_MAGIC) {
66 LOG(LOG_ERR, "received bogus message, ignoring");
67 return -2;
68 }
69 DEBUG(LOG_INFO, "Received a packet");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000070
Russ Dill61fb4892002-10-14 21:41:28 +000071 if (packet->op == BOOTREQUEST && (vendor = get_option(packet, DHCP_VENDOR))) {
72 for (i = 0; broken_vendors[i][0]; i++) {
Eric Andersenad953732004-01-30 23:45:53 +000073 if (vendor[OPT_LEN - 2] == (uint8_t) strlen(broken_vendors[i]) &&
Eric Andersena68ea1c2006-01-30 22:48:39 +000074 !strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2])) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000075 DEBUG(LOG_INFO, "broken client (%s), forcing broadcast",
76 broken_vendors[i]);
77 packet->flags |= htons(BROADCAST_FLAG);
Russ Dill61fb4892002-10-14 21:41:28 +000078 }
79 }
80 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000081
Russ Dill61fb4892002-10-14 21:41:28 +000082
83 return bytes;
84}
85
86
Eric Andersenad953732004-01-30 23:45:53 +000087uint16_t checksum(void *addr, int count)
Russ Dill61fb4892002-10-14 21:41:28 +000088{
89 /* Compute Internet Checksum for "count" bytes
90 * beginning at location "addr".
91 */
92 register int32_t sum = 0;
Eric Andersenad953732004-01-30 23:45:53 +000093 uint16_t *source = (uint16_t *) addr;
Russ Dill61fb4892002-10-14 21:41:28 +000094
95 while (count > 1) {
96 /* This is the inner loop */
97 sum += *source++;
98 count -= 2;
99 }
100
101 /* Add left-over byte, if any */
102 if (count > 0) {
103 /* Make sure that the left-over byte is added correctly both
104 * with little and big endian hosts */
Eric Andersenad953732004-01-30 23:45:53 +0000105 uint16_t tmp = 0;
106 *(uint8_t *) (&tmp) = * (uint8_t *) source;
Russ Dill61fb4892002-10-14 21:41:28 +0000107 sum += tmp;
108 }
109 /* Fold 32-bit sum to 16 bits */
110 while (sum >> 16)
111 sum = (sum & 0xffff) + (sum >> 16);
112
113 return ~sum;
114}
115
116
Eric Andersenaff114c2004-04-14 17:51:38 +0000117/* Construct a ip/udp header for a packet, and specify the source and dest hardware address */
Eric Andersenad953732004-01-30 23:45:53 +0000118int raw_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port,
119 uint32_t dest_ip, int dest_port, uint8_t *dest_arp, int ifindex)
Russ Dill61fb4892002-10-14 21:41:28 +0000120{
121 int fd;
122 int result;
123 struct sockaddr_ll dest;
124 struct udp_dhcp_packet packet;
125
126 if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000127 DEBUG(LOG_ERR, "socket call failed: %m");
Russ Dill61fb4892002-10-14 21:41:28 +0000128 return -1;
129 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000130
Russ Dill61fb4892002-10-14 21:41:28 +0000131 memset(&dest, 0, sizeof(dest));
132 memset(&packet, 0, sizeof(packet));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000133
Russ Dill61fb4892002-10-14 21:41:28 +0000134 dest.sll_family = AF_PACKET;
135 dest.sll_protocol = htons(ETH_P_IP);
136 dest.sll_ifindex = ifindex;
137 dest.sll_halen = 6;
138 memcpy(dest.sll_addr, dest_arp, 6);
139 if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000140 DEBUG(LOG_ERR, "bind call failed: %m");
Russ Dill61fb4892002-10-14 21:41:28 +0000141 close(fd);
142 return -1;
143 }
144
145 packet.ip.protocol = IPPROTO_UDP;
146 packet.ip.saddr = source_ip;
147 packet.ip.daddr = dest_ip;
148 packet.udp.source = htons(source_port);
149 packet.udp.dest = htons(dest_port);
150 packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage)); /* cheat on the psuedo-header */
151 packet.ip.tot_len = packet.udp.len;
152 memcpy(&(packet.data), payload, sizeof(struct dhcpMessage));
153 packet.udp.check = checksum(&packet, sizeof(struct udp_dhcp_packet));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000154
Russ Dill61fb4892002-10-14 21:41:28 +0000155 packet.ip.tot_len = htons(sizeof(struct udp_dhcp_packet));
156 packet.ip.ihl = sizeof(packet.ip) >> 2;
157 packet.ip.version = IPVERSION;
158 packet.ip.ttl = IPDEFTTL;
159 packet.ip.check = checksum(&(packet.ip), sizeof(packet.ip));
160
161 result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0, (struct sockaddr *) &dest, sizeof(dest));
162 if (result <= 0) {
Glenn L McGrath24833432003-06-10 17:22:49 +0000163 DEBUG(LOG_ERR, "write on socket failed: %m");
Russ Dill61fb4892002-10-14 21:41:28 +0000164 }
165 close(fd);
166 return result;
167}
168
169
170/* Let the kernel do all the work for packet generation */
Eric Andersenad953732004-01-30 23:45:53 +0000171int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port,
172 uint32_t dest_ip, int dest_port)
Russ Dill61fb4892002-10-14 21:41:28 +0000173{
174 int n = 1;
175 int fd, result;
176 struct sockaddr_in client;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000177
Russ Dill61fb4892002-10-14 21:41:28 +0000178 if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
179 return -1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000180
Rob Landleye8f504e2006-02-22 02:10:34 +0000181 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) {
182 close(fd);
Russ Dill61fb4892002-10-14 21:41:28 +0000183 return -1;
Rob Landleye8f504e2006-02-22 02:10:34 +0000184 }
Russ Dill61fb4892002-10-14 21:41:28 +0000185
186 memset(&client, 0, sizeof(client));
187 client.sin_family = AF_INET;
188 client.sin_port = htons(source_port);
189 client.sin_addr.s_addr = source_ip;
190
Rob Landleye8f504e2006-02-22 02:10:34 +0000191 if (bind(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) {
192 close(fd);
Russ Dill61fb4892002-10-14 21:41:28 +0000193 return -1;
Rob Landleye8f504e2006-02-22 02:10:34 +0000194 }
Russ Dill61fb4892002-10-14 21:41:28 +0000195
196 memset(&client, 0, sizeof(client));
197 client.sin_family = AF_INET;
198 client.sin_port = htons(dest_port);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000199 client.sin_addr.s_addr = dest_ip;
Russ Dill61fb4892002-10-14 21:41:28 +0000200
Rob Landleye8f504e2006-02-22 02:10:34 +0000201 if (connect(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) {
202 close(fd);
Russ Dill61fb4892002-10-14 21:41:28 +0000203 return -1;
Rob Landleye8f504e2006-02-22 02:10:34 +0000204 }
Russ Dill61fb4892002-10-14 21:41:28 +0000205
206 result = write(fd, payload, sizeof(struct dhcpMessage));
207 close(fd);
208 return result;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000209}