1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7
8 #include "dhcp.h"
9 #include "config.h"
10
11 #ifndef DEFAULT_LEASETIME
12 #define DEFAULT_LEASETIME 3600 /* 1 hour */
13 #endif
14
15 #define REQUEST (1 << 0)
16 #define UINT8 (1 << 1)
17 #define UINT16 (1 << 2)
18 #define SINT16 (1 << 3)
19 #define UINT32 (1 << 4)
20 #define SINT32 (1 << 5)
21 #define IPV4 (1 << 6)
22 #define STRING (1 << 7)
23 #define PAIR (1 << 8)
24 #define ARRAY (1 << 9)
25 #define RFC3361 (1 << 10)
26 #define RFC3397 (1 << 11)
27 #define RFC3442 (1 << 12)
28
29 struct dhcp_opt {
30 uint8_t option;
31 int type;
32 const char *var;
33 };
34
35 static const struct dhcp_opt const dhcp_opts[] = {
36 { 1, IPV4 | REQUEST, "subnet_mask" },
37 { 2, UINT32, "time_offset" },
38 { 3, IPV4 | ARRAY | REQUEST, "routers" },
39 { 4, IPV4 | ARRAY, "time_servers" },
40 { 5, IPV4 | ARRAY, "ien116_name_servers" },
41 { 6, IPV4 | ARRAY, "domain_name_servers" },
42 { 7, IPV4 | ARRAY, "log_servers" },
43 { 8, IPV4 | ARRAY, "cookie_servers" },
44 { 9, IPV4 | ARRAY, "lpr_servers" },
45 { 10, IPV4 | ARRAY, "impress_servers" },
46 { 11, IPV4 | ARRAY, "resource_location_servers" },
47 { 12, STRING, "host_name" },
48 { 13, UINT16, "boot_size" },
49 { 14, STRING, "merit_dump" },
50 { 15, STRING, "domain_name" },
51 { 16, IPV4, "swap_server" },
52 { 17, STRING, "root_path" },
53 { 18, STRING, "extensions_path" },
54 { 19, UINT8, "ip_forwarding" },
55 { 20, UINT8, "non_local_source_routing" },
56 { 21, IPV4 | ARRAY, "policy_filter" },
57 { 22, SINT16, "max_dgram_reassembly" },
58 { 23, UINT16, "default_ip_ttl" },
59 { 24, UINT32, "path_mtu_aging_timeout" },
60 { 25, UINT16 | ARRAY, "path_mtu_plateau_table" },
61 { 26, UINT16, "interface_mtu" },
62 { 27, UINT8, "all_subnets_local" },
63 { 28, IPV4 | REQUEST, "broadcast_address" },
64 { 29, UINT8, "perform_mask_discovery" },
65 { 30, UINT8, "mask_supplier" },
66 { 31, UINT8, "router_discovery" },
67 { 32, IPV4, "router_solicitation_address" },
68 { 33, IPV4 | ARRAY | REQUEST, "static_routes" },
69 { 34, UINT8, "trailer_encapsulation" },
70 { 35, UINT32, "arp_cache_timeout" },
71 { 36, UINT16, "ieee802_3_encapsulation" },
72 { 37, UINT8, "default_tcp_ttl" },
73 { 38, UINT32, "tcp_keepalive_interval" },
74 { 39, UINT8, "tcp_keepalive_garbage" },
75 { 40, STRING, "nis_domain" },
76 { 41, IPV4 | ARRAY, "nis_servers" },
77 { 42, IPV4 | ARRAY, "ntp_servers" },
78 { 43, STRING, "vendor_encapsulated_options" },
79 { 44, IPV4 | ARRAY, "netbios_name_servers" },
80 { 45, IPV4, "netbios_dd_server" },
81 { 46, UINT8, "netbios_node_type" },
82 { 47, STRING, "netbios_scope" },
83 { 48, IPV4 | ARRAY, "font_servers" },
84 { 49, IPV4 | ARRAY, "x_display_manager" },
85 { 50, IPV4, "dhcp_requested_address" },
86 { 51, UINT32 | REQUEST, "dhcp_lease_time" },
87 { 52, UINT8, "dhcp_option_overload" },
88 { 53, UINT8, "dhcp_message_type" },
89 { 54, IPV4, "dhcp_server_identifier" },
90 { 55, UINT8 | ARRAY, "dhcp_parameter_request_list" },
91 { 56, STRING, "dhcp_message" },
92 { 57, UINT16, "dhcp_max_message_size" },
93 { 58, UINT32 | REQUEST, "dhcp_renewal_time" },
94 { 59, UINT32 | REQUEST, "dhcp_rebinding_time" },
95 { 64, STRING, "nisplus_domain" },
96 { 65, IPV4 | ARRAY, "nisplus_servers" },
97 { 66, STRING, "tftp_server_name" },
98 { 67, STRING, "bootfile_name" },
99 { 68, IPV4 | ARRAY, "mobile_ip_home_agent" },
100 { 69, IPV4 | ARRAY, "smtp_server" },
101 { 70, IPV4 | ARRAY, "pop_server" },
102 { 71, IPV4 | ARRAY, "nntp_server" },
103 { 72, IPV4 | ARRAY, "www_server" },
104 { 73, IPV4 | ARRAY, "finger_server" },
105 { 74, IPV4 | ARRAY, "irc_server" },
106 { 75, IPV4 | ARRAY, "streettalk_server" },
107 { 76, IPV4 | ARRAY, "streettalk_directory_assistance_server" },
108 { 77, STRING, "user_class" },
109 { 85, IPV4 | ARRAY, "nds_servers" },
110 { 86, STRING, "nds_tree_name" },
111 { 87, STRING, "nds_context" },
112 { 88, STRING | RFC3397, "bcms_controller_names" },
113 { 89, IPV4 | ARRAY, "bcms_controller_address" },
114 { 91, UINT32, "client_last_transaction_time" },
115 { 92, IPV4 | ARRAY, "associated_ip" },
116 { 98, STRING, "uap_servers" },
117 { 112, IPV4 | ARRAY, "netinfo_server_address" },
118 { 113, STRING, "netinfo_server_tag" },
119 { 114, STRING, "default_url" },
120 { 118, IPV4, "subnet_selection" },
121 { 119, STRING | RFC3397, "domain_search" },
122 { 121, RFC3442 | REQUEST, "classless_static_routes" },
123 { 249, RFC3442, "ms-classless_static_routes" },
124 { 0, 0, NULL }
125 };
126
127 struct dhcp_message *
get_lease_from_file(const char * leasefile)128 get_lease_from_file(const char *leasefile)
129 {
130 int fd;
131 struct dhcp_message *dhcp;
132 ssize_t bytes;
133
134 fd = open(leasefile, O_RDONLY);
135 if (fd == -1)
136 return NULL;
137 dhcp = malloc(sizeof(*dhcp));
138 memset(dhcp, 0, sizeof(*dhcp));
139 bytes = read(fd, dhcp, sizeof(*dhcp));
140 close(fd);
141 if (bytes < 0) {
142 free(dhcp);
143 dhcp = NULL;
144 }
145 return dhcp;
146 }
147
148 static uint8_t *dhcp_opt_buffer = NULL;
149
150 static int
valid_length(uint8_t option,int dl,int * type)151 valid_length(uint8_t option, int dl, int *type)
152 {
153 const struct dhcp_opt *opt;
154 ssize_t sz;
155
156 if (dl == 0)
157 return -1;
158
159 for (opt = dhcp_opts; opt->option; opt++) {
160 if (opt->option != option)
161 continue;
162
163 if (type)
164 *type = opt->type;
165
166 if (opt->type == 0 || opt->type & STRING || opt->type & RFC3442)
167 return 0;
168
169 sz = 0;
170 if (opt->type & UINT32 || opt->type & IPV4)
171 sz = sizeof(uint32_t);
172 if (opt->type & UINT16)
173 sz = sizeof(uint16_t);
174 if (opt->type & UINT8)
175 sz = sizeof(uint8_t);
176 if (opt->type & IPV4 || opt->type & ARRAY)
177 return dl % sz;
178 return (dl == sz ? 0 : -1);
179 }
180
181 /* unknown option, so let it pass */
182 return 0;
183 }
184
185 static void
free_option_buffer(void)186 free_option_buffer(void)
187 {
188 free(dhcp_opt_buffer);
189 }
190
191
192 #define get_option_raw(dhcp, opt) get_option(dhcp, opt, NULL, NULL)
193 static const uint8_t *
get_option(const struct dhcp_message * dhcp,uint8_t opt,int * len,int * type)194 get_option(const struct dhcp_message *dhcp, uint8_t opt, int *len, int *type)
195 {
196 const uint8_t *p = dhcp->options;
197 const uint8_t *e = p + sizeof(dhcp->options);
198 uint8_t l, ol = 0;
199 uint8_t o = 0;
200 uint8_t overl = 0;
201 uint8_t *bp = NULL;
202 const uint8_t *op = NULL;
203 int bl = 0;
204
205 while (p < e) {
206 o = *p++;
207 if (o == opt) {
208 if (op) {
209 if (!dhcp_opt_buffer) {
210 dhcp_opt_buffer = malloc(sizeof(struct dhcp_message));
211 atexit(free_option_buffer);
212 }
213 if (!bp)
214 bp = dhcp_opt_buffer;
215 memcpy(bp, op, ol);
216 bp += ol;
217 }
218 ol = *p;
219 op = p + 1;
220 bl += ol;
221 }
222 switch (o) {
223 case DHO_PAD:
224 continue;
225 case DHO_END:
226 if (overl & 1) {
227 /* bit 1 set means parse boot file */
228 overl &= ~1;
229 p = dhcp->bootfile;
230 e = p + sizeof(dhcp->bootfile);
231 } else if (overl & 2) {
232 /* bit 2 set means parse server name */
233 overl &= ~2;
234 p = dhcp->servername;
235 e = p + sizeof(dhcp->servername);
236 } else
237 goto exit;
238 break;
239 case DHO_OPTIONSOVERLOADED:
240 /* Ensure we only get this option once */
241 if (!overl)
242 overl = p[1];
243 break;
244 }
245 l = *p++;
246 p += l;
247 }
248
249 exit:
250 if (valid_length(o, bl, type) == -1) {
251 errno = EINVAL;
252 return NULL;
253 }
254 if (len)
255 *len = bl;
256 if (bp) {
257 memcpy(bp, op, ol);
258 return (const uint8_t *)&dhcp_opt_buffer;
259 }
260 if (op)
261 return op;
262 errno = ENOENT;
263 return NULL;
264 }
265
266 int
get_option_addr32(uint32_t * a,const struct dhcp_message * dhcp,uint8_t option)267 get_option_addr32(uint32_t *a, const struct dhcp_message *dhcp, uint8_t option)
268 {
269 const uint8_t *p = get_option_raw(dhcp, option);
270
271 if (!p)
272 return -1;
273 memcpy(a, p, sizeof(*a));
274 return 0;
275 }
276
277 int
get_option_uint32(uint32_t * i,const struct dhcp_message * dhcp,uint8_t option)278 get_option_uint32(uint32_t *i, const struct dhcp_message *dhcp, uint8_t option)
279 {
280 uint32_t a;
281
282 if (get_option_addr32(&a, dhcp, option) == -1)
283 return -1;
284
285 *i = ntohl(a);
286 return 0;
287 }
288
289 uint32_t
get_netmask(uint32_t addr)290 get_netmask(uint32_t addr)
291 {
292 uint32_t dst;
293
294 if (addr == 0)
295 return 0;
296
297 dst = htonl(addr);
298 if (IN_CLASSA(dst))
299 return ntohl(IN_CLASSA_NET);
300 if (IN_CLASSB (dst))
301 return ntohl(IN_CLASSB_NET);
302 if (IN_CLASSC (dst))
303 return ntohl(IN_CLASSC_NET);
304
305 return 0;
306 }
307
showlease(struct dhcp_lease * lease)308 void showlease(struct dhcp_lease *lease)
309 {
310 printf("addr: %s\n", inet_ntoa(lease->addr));
311 printf("net: %s\n", inet_ntoa(lease->net));
312 printf("leasetime: %d\n", lease->leasetime);
313 printf("renew: %d\n", lease->renewaltime);
314 printf("rebind: %d\n", lease->rebindtime);
315 printf("server: %s\n", inet_ntoa(lease->server));
316 }
317 #define MAX_LEASETIME 2147460
318
319 int
main(int argc,char * argv[])320 main(int argc, char *argv[])
321 {
322 struct dhcp_message *dhcp;
323 struct dhcp_lease *lease;
324 char leasefile[PATH_MAX];
325
326 if (argc < 2) {
327 fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
328 exit(1);
329 }
330 snprintf(leasefile, PATH_MAX, LEASEFILE, argv[1]);
331 if ((dhcp = get_lease_from_file(leasefile)) == NULL) {
332 fprintf(stderr, "Couldn't read lease file: %s\n", strerror(errno));
333 exit(1);
334 }
335 lease = malloc(sizeof(*lease));
336 lease->frominfo = 0;
337 lease->addr.s_addr = dhcp->yiaddr;
338
339 if (get_option_addr32(&lease->net.s_addr, dhcp, DHO_SUBNETMASK) == -1)
340 lease->net.s_addr = get_netmask(dhcp->yiaddr);
341 if (get_option_uint32(&lease->leasetime, dhcp, DHO_LEASETIME) != 0)
342 lease->leasetime = DEFAULT_LEASETIME;
343 get_option_addr32(&lease->server.s_addr, dhcp, DHO_SERVERID);
344 /* Dm: limit lease time value to avoid negative numbers when
345 converting to milliseconds */
346 if ((lease->leasetime != ~0U) && (lease->leasetime > MAX_LEASETIME))
347 lease->leasetime = MAX_LEASETIME;
348 if (get_option_uint32(&lease->renewaltime, dhcp, DHO_RENEWALTIME) != 0)
349 lease->renewaltime = 0;
350 if (get_option_uint32(&lease->rebindtime, dhcp, DHO_REBINDTIME) != 0)
351 lease->rebindtime = 0;
352 showlease(lease);
353 free(lease);
354 return 0;
355 }
356