1 | #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) || defined _NEWLIB_VERSION
|
---|
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 |
|
---|
17 | #include "common.h"
|
---|
18 | #include "packet.h"
|
---|
19 | #include "dhcpd.h"
|
---|
20 | #include "options.h"
|
---|
21 |
|
---|
22 |
|
---|
23 | void udhcp_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 */
|
---|
47 | int udhcp_get_packet(struct dhcpMessage *packet, int fd)
|
---|
48 | {
|
---|
49 | static const char broken_vendors[][8] = {
|
---|
50 | "MSFT 98",
|
---|
51 | ""
|
---|
52 | };
|
---|
53 | int bytes;
|
---|
54 | int i;
|
---|
55 | char unsigned *vendor;
|
---|
56 |
|
---|
57 | memset(packet, 0, sizeof(struct dhcpMessage));
|
---|
58 | bytes = read(fd, packet, sizeof(struct dhcpMessage));
|
---|
59 | if (bytes < 0) {
|
---|
60 | DEBUG(LOG_INFO, "couldn't read on listening socket, ignoring");
|
---|
61 | return -1;
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (ntohl(packet->cookie) != DHCP_MAGIC) {
|
---|
65 | LOG(LOG_ERR, "received bogus message, ignoring");
|
---|
66 | return -2;
|
---|
67 | }
|
---|
68 | DEBUG(LOG_INFO, "Received a packet");
|
---|
69 |
|
---|
70 | if (packet->op == BOOTREQUEST && (vendor = get_option(packet, DHCP_VENDOR))) {
|
---|
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 | DEBUG(LOG_INFO, "broken client (%s), forcing broadcast",
|
---|
75 | broken_vendors[i]);
|
---|
76 | packet->flags |= htons(BROADCAST_FLAG);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | return bytes;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | uint16_t udhcp_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 | uint16_t *source = (uint16_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 | uint16_t tmp = 0;
|
---|
104 | *(uint8_t *) (&tmp) = * (uint8_t *) 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 | /* Construct a ip/udp header for a packet, and specify the source and dest hardware address */
|
---|
116 | int udhcp_raw_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port,
|
---|
117 | uint32_t dest_ip, int dest_port, uint8_t *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) {
|
---|
125 | DEBUG(LOG_ERR, "socket call failed: %m");
|
---|
126 | 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) {
|
---|
138 | DEBUG(LOG_ERR, "bind call failed: %m");
|
---|
139 | 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 = udhcp_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 = udhcp_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) {
|
---|
161 | DEBUG(LOG_ERR, "write on socket failed: %m");
|
---|
162 | }
|
---|
163 | close(fd);
|
---|
164 | return result;
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | /* Let the kernel do all the work for packet generation */
|
---|
169 | int udhcp_kernel_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port,
|
---|
170 | uint32_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 | close(fd);
|
---|
181 | return -1;
|
---|
182 | }
|
---|
183 |
|
---|
184 | memset(&client, 0, sizeof(client));
|
---|
185 | client.sin_family = AF_INET;
|
---|
186 | client.sin_port = htons(source_port);
|
---|
187 | client.sin_addr.s_addr = source_ip;
|
---|
188 |
|
---|
189 | if (bind(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) {
|
---|
190 | close(fd);
|
---|
191 | return -1;
|
---|
192 | }
|
---|
193 |
|
---|
194 | memset(&client, 0, sizeof(client));
|
---|
195 | client.sin_family = AF_INET;
|
---|
196 | client.sin_port = htons(dest_port);
|
---|
197 | client.sin_addr.s_addr = dest_ip;
|
---|
198 |
|
---|
199 | if (connect(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) {
|
---|
200 | close(fd);
|
---|
201 | return -1;
|
---|
202 | }
|
---|
203 |
|
---|
204 | result = write(fd, payload, sizeof(struct dhcpMessage));
|
---|
205 | close(fd);
|
---|
206 | return result;
|
---|
207 | }
|
---|