3 * Packet generation and dispatching functions for the DHCP client.
5 * Russ Dill <Russ.Dill@asu.edu> July 2001
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.
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.
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.
23 #include <sys/socket.h>
25 #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
26 #include <netpacket/packet.h>
27 #include <net/ethernet.h>
29 #include <asm/types.h>
30 #include <linux/if_packet.h>
31 #include <linux/if_ether.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
47 /* Create a random xid */
48 unsigned long random_xid(void)
50 static int initialized;
59 /* initialize a packet with the proper defaults */
60 static void init_packet(struct dhcpMessage *packet, char type)
63 char VIinfo[VENDOR_IDENTIFYING_INFO_LEN];
67 char str[sizeof("uDHCP "VERSION)];
68 } vendor_id = { DHCP_VENDOR, sizeof("uDHCP "VERSION) - 1, "uDHCP "VERSION};
70 memset(packet, 0, sizeof(struct dhcpMessage));
72 packet->op = BOOTREQUEST;
73 packet->htype = ETH_10MB;
74 packet->hlen = ETH_10MB_LEN;
75 packet->cookie = htonl(DHCP_MAGIC);
76 packet->options[0] = DHCP_END;
77 memcpy(packet->chaddr, client_config.arp, 6);
78 add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
79 if (client_config.clientid) add_option_string(packet->options, client_config.clientid);
80 if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
82 if (strlen(vendor_class_id)) {
83 vendor_id.length = strlen(vendor_class_id);
84 sprintf(vendor_id.str, "%s", vendor_class_id);
86 add_option_string(packet->options, (char *) &vendor_id);
87 if (createVIoption(VENDOR_IDENTIFYING_FOR_DEVICE,VIinfo) != -1)
88 add_option_string(packet->options,VIinfo);
92 /* Add a paramater request list for stubborn DHCP servers */
93 static void add_requests(struct dhcpMessage *packet)
95 char request_list[] = {DHCP_PARAM_REQ, 0, PARM_REQUESTS};
97 request_list[OPT_LEN] = sizeof(request_list) - 2;
98 add_option_string(packet->options, request_list);
102 /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
103 int send_discover(unsigned long xid, unsigned long requested)
105 struct dhcpMessage packet;
107 init_packet(&packet, DHCPDISCOVER);
110 add_simple_option(packet.options, DHCP_REQUESTED_IP, ntohl(requested));
112 add_requests(&packet);
114 // LOG(LOG_DEBUG, "Sending discover...");
115 return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
116 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
120 /* Broadcasts a DHCP request message */
121 int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
123 struct dhcpMessage packet;
126 init_packet(&packet, DHCPREQUEST);
129 /* expects host order */
130 add_simple_option(packet.options, DHCP_REQUESTED_IP, ntohl(requested));
132 /* expects host order */
133 add_simple_option(packet.options, DHCP_SERVER_ID, ntohl(server));
135 add_requests(&packet);
136 addr.s_addr = requested;
138 //LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr));
139 return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
140 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
144 /* Unicasts or broadcasts a DHCP renew message */
145 int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
147 struct dhcpMessage packet;
150 init_packet(&packet, DHCPREQUEST);
152 packet.ciaddr = ciaddr;
154 LOG(LOG_DEBUG, "Sending renew...");
156 ret = kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
157 else ret = raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
158 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
163 /* Unicasts a DHCP release message */
164 int send_release(unsigned long server, unsigned long ciaddr)
166 struct dhcpMessage packet;
168 init_packet(&packet, DHCPRELEASE);
169 packet.xid = random_xid();
170 packet.ciaddr = ciaddr;
172 /* expects host order */
173 add_simple_option(packet.options, DHCP_REQUESTED_IP, ntohl(ciaddr));
174 add_simple_option(packet.options, DHCP_SERVER_ID, ntohl(server));
176 LOG(LOG_DEBUG, "Sending release...");
177 return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
181 int get_raw_packet(struct dhcpMessage *payload, int fd)
184 struct udp_dhcp_packet packet;
185 u_int32_t source, dest;
188 memset(&packet, 0, sizeof(struct udp_dhcp_packet));
189 bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
191 DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring");
192 usleep(500000); /* possible down interface, looping condition */
196 if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
197 DEBUG(LOG_INFO, "message too short, ignoring");
201 /* Make sure its the right packet for us, and that it passes sanity checks */
202 if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION ||
203 packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
204 ntohs(packet.ip.tot_len) != bytes || bytes > (int) sizeof(struct udp_dhcp_packet) ||
205 ntohs(packet.udp.len) != (short) (bytes - sizeof(packet.ip))) {
206 DEBUG(LOG_INFO, "unrelated/bogus packet");
210 /* check IP checksum */
211 check = packet.ip.check;
213 if (check != checksum(&(packet.ip), sizeof(packet.ip))) {
214 DEBUG(LOG_INFO, "bad IP header checksum, ignoring");
218 /* verify the UDP checksum by replacing the header with a psuedo header */
219 source = packet.ip.saddr;
220 dest = packet.ip.daddr;
221 check = packet.udp.check;
222 packet.udp.check = 0;
223 memset(&packet.ip, 0, sizeof(packet.ip));
225 packet.ip.protocol = IPPROTO_UDP;
226 packet.ip.saddr = source;
227 packet.ip.daddr = dest;
228 packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
231 if (check != checksum(&packet, bytes)) {
232 DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring");
238 memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
240 if (ntohl(payload->cookie) != DHCP_MAGIC) {
241 LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring");
244 DEBUG(LOG_INFO, "oooooh!!! got some!");
245 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));