diff options
Diffstat (limited to 'networking/udhcp/clientpacket.c')
-rw-r--r-- | networking/udhcp/clientpacket.c | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/networking/udhcp/clientpacket.c b/networking/udhcp/clientpacket.c new file mode 100644 index 000000000..88d4d88dc --- /dev/null +++ b/networking/udhcp/clientpacket.c | |||
@@ -0,0 +1,245 @@ | |||
1 | /* clientpacket.c | ||
2 | * | ||
3 | * Packet generation and dispatching functions for the DHCP client. | ||
4 | * | ||
5 | * Russ Dill <Russ.Dill@asu.edu> July 2001 | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation; either version 2 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
20 | */ | ||
21 | |||
22 | #include <string.h> | ||
23 | #include <sys/socket.h> | ||
24 | #include <features.h> | ||
25 | #if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || defined _NEWLIB_VERSION | ||
26 | #include <netpacket/packet.h> | ||
27 | #include <net/ethernet.h> | ||
28 | #else | ||
29 | #include <asm/types.h> | ||
30 | #include <linux/if_packet.h> | ||
31 | #include <linux/if_ether.h> | ||
32 | #endif | ||
33 | #include <stdlib.h> | ||
34 | #include <time.h> | ||
35 | #include <unistd.h> | ||
36 | #include <netinet/in.h> | ||
37 | #include <arpa/inet.h> | ||
38 | #include <fcntl.h> | ||
39 | |||
40 | |||
41 | #include "dhcpd.h" | ||
42 | #include "clientpacket.h" | ||
43 | #include "options.h" | ||
44 | #include "dhcpc.h" | ||
45 | #include "common.h" | ||
46 | |||
47 | |||
48 | /* Create a random xid */ | ||
49 | unsigned long random_xid(void) | ||
50 | { | ||
51 | static int initialized; | ||
52 | if (!initialized) { | ||
53 | int fd; | ||
54 | unsigned long seed; | ||
55 | |||
56 | fd = open("/dev/urandom", 0); | ||
57 | if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) { | ||
58 | LOG(LOG_WARNING, "Could not load seed from /dev/urandom: %m"); | ||
59 | seed = time(0); | ||
60 | } | ||
61 | if (fd >= 0) close(fd); | ||
62 | srand(seed); | ||
63 | initialized++; | ||
64 | } | ||
65 | return rand(); | ||
66 | } | ||
67 | |||
68 | |||
69 | /* initialize a packet with the proper defaults */ | ||
70 | static void init_packet(struct dhcpMessage *packet, char type) | ||
71 | { | ||
72 | init_header(packet, type); | ||
73 | memcpy(packet->chaddr, client_config.arp, 6); | ||
74 | if (client_config.clientid) | ||
75 | add_option_string(packet->options, client_config.clientid); | ||
76 | if (client_config.hostname) add_option_string(packet->options, client_config.hostname); | ||
77 | if (client_config.fqdn) add_option_string(packet->options, client_config.fqdn); | ||
78 | add_option_string(packet->options, client_config.vendorclass); | ||
79 | } | ||
80 | |||
81 | |||
82 | /* Add a parameter request list for stubborn DHCP servers. Pull the data | ||
83 | * from the struct in options.c. Don't do bounds checking here because it | ||
84 | * goes towards the head of the packet. */ | ||
85 | static void add_requests(struct dhcpMessage *packet) | ||
86 | { | ||
87 | int end = end_option(packet->options); | ||
88 | int i, len = 0; | ||
89 | |||
90 | packet->options[end + OPT_CODE] = DHCP_PARAM_REQ; | ||
91 | for (i = 0; dhcp_options[i].code; i++) | ||
92 | if (dhcp_options[i].flags & OPTION_REQ) | ||
93 | packet->options[end + OPT_DATA + len++] = dhcp_options[i].code; | ||
94 | packet->options[end + OPT_LEN] = len; | ||
95 | packet->options[end + OPT_DATA + len] = DHCP_END; | ||
96 | |||
97 | } | ||
98 | |||
99 | |||
100 | /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */ | ||
101 | int send_discover(unsigned long xid, unsigned long requested) | ||
102 | { | ||
103 | struct dhcpMessage packet; | ||
104 | |||
105 | init_packet(&packet, DHCPDISCOVER); | ||
106 | packet.xid = xid; | ||
107 | if (requested) | ||
108 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); | ||
109 | |||
110 | add_requests(&packet); | ||
111 | LOG(LOG_DEBUG, "Sending discover..."); | ||
112 | return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, | ||
113 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); | ||
114 | } | ||
115 | |||
116 | |||
117 | /* Broadcasts a DHCP request message */ | ||
118 | int send_selecting(unsigned long xid, unsigned long server, unsigned long requested) | ||
119 | { | ||
120 | struct dhcpMessage packet; | ||
121 | struct in_addr addr; | ||
122 | |||
123 | init_packet(&packet, DHCPREQUEST); | ||
124 | packet.xid = xid; | ||
125 | |||
126 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); | ||
127 | add_simple_option(packet.options, DHCP_SERVER_ID, server); | ||
128 | |||
129 | add_requests(&packet); | ||
130 | addr.s_addr = requested; | ||
131 | LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr)); | ||
132 | return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, | ||
133 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); | ||
134 | } | ||
135 | |||
136 | |||
137 | /* Unicasts or broadcasts a DHCP renew message */ | ||
138 | int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr) | ||
139 | { | ||
140 | struct dhcpMessage packet; | ||
141 | int ret = 0; | ||
142 | |||
143 | init_packet(&packet, DHCPREQUEST); | ||
144 | packet.xid = xid; | ||
145 | packet.ciaddr = ciaddr; | ||
146 | |||
147 | add_requests(&packet); | ||
148 | LOG(LOG_DEBUG, "Sending renew..."); | ||
149 | if (server) | ||
150 | ret = kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); | ||
151 | else ret = raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, | ||
152 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); | ||
153 | return ret; | ||
154 | } | ||
155 | |||
156 | |||
157 | /* Unicasts a DHCP release message */ | ||
158 | int send_release(unsigned long server, unsigned long ciaddr) | ||
159 | { | ||
160 | struct dhcpMessage packet; | ||
161 | |||
162 | init_packet(&packet, DHCPRELEASE); | ||
163 | packet.xid = random_xid(); | ||
164 | packet.ciaddr = ciaddr; | ||
165 | |||
166 | add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr); | ||
167 | add_simple_option(packet.options, DHCP_SERVER_ID, server); | ||
168 | |||
169 | LOG(LOG_DEBUG, "Sending release..."); | ||
170 | return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); | ||
171 | } | ||
172 | |||
173 | |||
174 | /* return -1 on errors that are fatal for the socket, -2 for those that aren't */ | ||
175 | int get_raw_packet(struct dhcpMessage *payload, int fd) | ||
176 | { | ||
177 | int bytes; | ||
178 | struct udp_dhcp_packet packet; | ||
179 | uint32_t source, dest; | ||
180 | uint16_t check; | ||
181 | |||
182 | memset(&packet, 0, sizeof(struct udp_dhcp_packet)); | ||
183 | bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet)); | ||
184 | if (bytes < 0) { | ||
185 | DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring"); | ||
186 | usleep(500000); /* possible down interface, looping condition */ | ||
187 | return -1; | ||
188 | } | ||
189 | |||
190 | if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) { | ||
191 | DEBUG(LOG_INFO, "message too short, ignoring"); | ||
192 | return -2; | ||
193 | } | ||
194 | |||
195 | if (bytes < ntohs(packet.ip.tot_len)) { | ||
196 | DEBUG(LOG_INFO, "Truncated packet"); | ||
197 | return -2; | ||
198 | } | ||
199 | |||
200 | /* ignore any extra garbage bytes */ | ||
201 | bytes = ntohs(packet.ip.tot_len); | ||
202 | |||
203 | /* Make sure its the right packet for us, and that it passes sanity checks */ | ||
204 | if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION || | ||
205 | packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) || | ||
206 | bytes > (int) sizeof(struct udp_dhcp_packet) || | ||
207 | ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) { | ||
208 | DEBUG(LOG_INFO, "unrelated/bogus packet"); | ||
209 | return -2; | ||
210 | } | ||
211 | |||
212 | /* check IP checksum */ | ||
213 | check = packet.ip.check; | ||
214 | packet.ip.check = 0; | ||
215 | if (check != checksum(&(packet.ip), sizeof(packet.ip))) { | ||
216 | DEBUG(LOG_INFO, "bad IP header checksum, ignoring"); | ||
217 | return -1; | ||
218 | } | ||
219 | |||
220 | /* verify the UDP checksum by replacing the header with a psuedo header */ | ||
221 | source = packet.ip.saddr; | ||
222 | dest = packet.ip.daddr; | ||
223 | check = packet.udp.check; | ||
224 | packet.udp.check = 0; | ||
225 | memset(&packet.ip, 0, sizeof(packet.ip)); | ||
226 | |||
227 | packet.ip.protocol = IPPROTO_UDP; | ||
228 | packet.ip.saddr = source; | ||
229 | packet.ip.daddr = dest; | ||
230 | packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */ | ||
231 | if (check && check != checksum(&packet, bytes)) { | ||
232 | DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring"); | ||
233 | return -2; | ||
234 | } | ||
235 | |||
236 | memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp))); | ||
237 | |||
238 | if (ntohl(payload->cookie) != DHCP_MAGIC) { | ||
239 | LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring"); | ||
240 | return -2; | ||
241 | } | ||
242 | DEBUG(LOG_INFO, "oooooh!!! got some!"); | ||
243 | return bytes - (sizeof(packet.ip) + sizeof(packet.udp)); | ||
244 | |||
245 | } | ||