Mercurial > hg > toybox
comparison toys/pending/dhcp.c @ 1005:03f72b57a092
DHCP client and server, from Ashwini Sharma.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 14 Aug 2013 19:09:33 -0500 |
parents | |
children | d3f9e55e350a |
comparison
equal
deleted
inserted
replaced
1004:13ac68b51d3d | 1005:03f72b57a092 |
---|---|
1 /* dhcp.c - DHCP client for dynamic network configuration. | |
2 * | |
3 * Copyright 2012 Madhur Verma <mad.flexi@gmail.com> | |
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com> | |
5 * | |
6 * Not in SUSv4. | |
7 USE_DHCP(NEWTOY(dhcp, "V:H:F:x*r:O*A#<0T#<0t#<0s:p:i:SBRCaovqnbf", TOYFLAG_SBIN|TOYFLAG_ROOTONLY)) | |
8 | |
9 config DHCP | |
10 bool "dhcp" | |
11 default n | |
12 help | |
13 usage: dhcp [-fbnqvoCRB] [-i IFACE] [-r IP] [-s PROG] [-p PIDFILE] | |
14 [-H HOSTNAME] [-V VENDOR] [-x OPT:VAL] [-O OPT] | |
15 | |
16 Configure network dynamicaly using DHCP. | |
17 | |
18 -i Interface to use (default eth0) | |
19 -p Create pidfile | |
20 -s Run PROG at DHCP events (default /usr/share/dhcp/default.script) | |
21 -B Request broadcast replies | |
22 -t Send up to N discover packets | |
23 -T Pause between packets (default 3 seconds) | |
24 -A Wait N seconds after failure (default 20) | |
25 -f Run in foreground | |
26 -b Background if lease is not obtained | |
27 -n Exit if lease is not obtained | |
28 -q Exit after obtaining lease | |
29 -R Release IP on exit | |
30 -S Log to syslog too | |
31 -a Use arping to validate offered address | |
32 -O Request option OPT from server (cumulative) | |
33 -o Don't request any options (unless -O is given) | |
34 -r Request this IP address | |
35 -x OPT:VAL Include option OPT in sent packets (cumulative) | |
36 -F Ask server to update DNS mapping for NAME | |
37 -H Send NAME as client hostname (default none) | |
38 -V VENDOR Vendor identifier (default 'toybox VERSION') | |
39 -C Don't send MAC as client identifier | |
40 -v Verbose | |
41 | |
42 Signals: | |
43 USR1 Renew current lease | |
44 USR2 Release current lease | |
45 | |
46 */ | |
47 | |
48 #define FOR_dhcp | |
49 #include "toys.h" | |
50 #include "toynet.h" | |
51 | |
52 // TODO: headers not in posix: | |
53 #include <netinet/ip.h> | |
54 #include <netinet/udp.h> | |
55 #include <netpacket/packet.h> | |
56 | |
57 #include <linux/filter.h> //FIXME: linux specific. fix for other OS ports | |
58 #include <linux/if_ether.h> | |
59 | |
60 GLOBALS( | |
61 char *iface; | |
62 char *pidfile; | |
63 char *script; | |
64 long retries; | |
65 long timeout; | |
66 long tryagain; | |
67 struct arg_list *req_opt; | |
68 char *req_ip; | |
69 struct arg_list *pkt_opt; | |
70 char *fdn_name; | |
71 char *hostname; | |
72 char *vendor_cls; | |
73 ) | |
74 | |
75 #define flag_get(f,v,d) ((toys.optflags & f) ? v : d) | |
76 #define flag_chk(f) ((toys.optflags & f) ? 1 : 0) | |
77 | |
78 #define STATE_INIT 0 | |
79 #define STATE_REQUESTING 1 | |
80 #define STATE_BOUND 2 | |
81 #define STATE_RENEWING 3 | |
82 #define STATE_REBINDING 4 | |
83 #define STATE_RENEW_REQUESTED 5 | |
84 #define STATE_RELEASED 6 | |
85 | |
86 #define BOOTP_BROADCAST 0x8000 | |
87 #define DHCP_MAGIC 0x63825363 | |
88 | |
89 #define DHCP_REQUEST 1 | |
90 #define DHCP_REPLY 2 | |
91 #define DHCP_HTYPE_ETHERNET 1 | |
92 | |
93 #define DHCPC_SERVER_PORT 67 | |
94 #define DHCPC_CLIENT_PORT 68 | |
95 | |
96 #define DHCPDISCOVER 1 | |
97 #define DHCPOFFER 2 | |
98 #define DHCPREQUEST 3 | |
99 #define DHCPACK 5 | |
100 #define DHCPNAK 6 | |
101 #define DHCPRELEASE 7 | |
102 | |
103 #define DHCP_OPTION_PADDING 0x00 | |
104 #define DHCP_OPTION_SUBNET_MASK 0x01 | |
105 #define DHCP_OPTION_ROUTER 0x03 | |
106 #define DHCP_OPTION_DNS_SERVER 0x06 | |
107 #define DHCP_OPTION_HOST_NAME 0x0c | |
108 #define DHCP_OPTION_BROADCAST 0x1c | |
109 #define DHCP_OPTION_REQ_IPADDR 0x32 | |
110 #define DHCP_OPTION_LEASE_TIME 0x33 | |
111 #define DHCP_OPTION_OVERLOAD 0x34 | |
112 #define DHCP_OPTION_MSG_TYPE 0x35 | |
113 #define DHCP_OPTION_SERVER_ID 0x36 | |
114 #define DHCP_OPTION_REQ_LIST 0x37 | |
115 #define DHCP_OPTION_MAX_SIZE 0x39 | |
116 #define DHCP_OPTION_CLIENTID 0x3D | |
117 #define DHCP_OPTION_VENDOR 0x3C | |
118 #define DHCP_OPTION_FQDN 0x51 | |
119 #define DHCP_OPTION_END 0xFF | |
120 | |
121 #define DHCP_NUM8 (1<<8) | |
122 #define DHCP_NUM16 (1<<9) | |
123 #define DHCP_NUM32 DHCP_NUM16 | DHCP_NUM8 | |
124 #define DHCP_STRING (1<<10) | |
125 #define DHCP_STRLST (1<<11) | |
126 #define DHCP_IP (1<<12) | |
127 #define DHCP_IPLIST (1<<13) | |
128 #define DHCP_IPPLST (1<<14) | |
129 #define DHCP_STCRTS (1<<15) | |
130 | |
131 #define LOG_SILENT 0x0 | |
132 #define LOG_CONSOLE 0x1 | |
133 #define LOG_SYSTEM 0x2 | |
134 | |
135 #define MODE_OFF 0 | |
136 #define MODE_RAW 1 | |
137 #define MODE_APP 2 | |
138 | |
139 static void (*dbg)(char *format, ...); | |
140 static void dummy(char *format, ...){ | |
141 return; | |
142 } | |
143 | |
144 typedef struct dhcpc_result_s { | |
145 struct in_addr serverid; | |
146 struct in_addr ipaddr; | |
147 struct in_addr netmask; | |
148 struct in_addr dnsaddr; | |
149 struct in_addr default_router; | |
150 uint32_t lease_time; | |
151 } dhcpc_result_t; | |
152 | |
153 typedef struct __attribute__((packed)) dhcp_msg_s { | |
154 uint8_t op; | |
155 uint8_t htype; | |
156 uint8_t hlen; | |
157 uint8_t hops; | |
158 uint32_t xid; | |
159 uint16_t secs; | |
160 uint16_t flags; | |
161 uint32_t ciaddr; | |
162 uint32_t yiaddr; | |
163 uint32_t nsiaddr; | |
164 uint32_t ngiaddr; | |
165 uint8_t chaddr[16]; | |
166 uint8_t sname[64]; | |
167 uint8_t file[128]; | |
168 uint32_t cookie; | |
169 uint8_t options[308]; | |
170 } dhcp_msg_t; | |
171 | |
172 typedef struct __attribute__((packed)) dhcp_raw_s { | |
173 struct iphdr iph; | |
174 struct udphdr udph; | |
175 dhcp_msg_t dhcp; | |
176 } dhcp_raw_t; | |
177 | |
178 typedef struct dhcpc_state_s { | |
179 uint8_t macaddr[6]; | |
180 char *iface; | |
181 int ifindex; | |
182 int sockfd; | |
183 int status; | |
184 int mode; | |
185 uint32_t mask; | |
186 struct in_addr ipaddr; | |
187 struct in_addr serverid; | |
188 dhcp_msg_t pdhcp; | |
189 } dhcpc_state_t; | |
190 | |
191 typedef struct option_val_s { | |
192 char *key; | |
193 uint16_t code; | |
194 void *val; | |
195 size_t len; | |
196 } option_val_t; | |
197 | |
198 struct fd_pair { int rd; int wr; }; | |
199 static uint32_t xid; | |
200 static dhcpc_state_t *state; | |
201 static struct fd_pair sigfd; | |
202 uint8_t bmacaddr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; | |
203 int set = 1; | |
204 uint8_t infomode = LOG_CONSOLE; | |
205 uint8_t raw_opt[29]; | |
206 int raw_optcount = 0; | |
207 struct arg_list *x_opt; | |
208 in_addr_t server = 0; | |
209 | |
210 static option_val_t *msgopt_list = NULL; | |
211 static option_val_t options_list[] = { | |
212 {"lease" , DHCP_NUM32 | 0x33, NULL, 0}, | |
213 {"subnet" , DHCP_IP | 0x01, NULL, 0}, | |
214 {"broadcast" , DHCP_IP | 0x1c, NULL, 0}, | |
215 {"router" , DHCP_IP | 0x03, NULL, 0}, | |
216 {"ipttl" , DHCP_NUM8 | 0x17, NULL, 0}, | |
217 {"mtu" , DHCP_NUM16 | 0x1a, NULL, 0}, | |
218 {"hostname" , DHCP_STRING | 0x0c, NULL, 0}, | |
219 {"domain" , DHCP_STRING | 0x0f, NULL, 0}, | |
220 {"search" , DHCP_STRLST | 0x77, NULL, 0}, | |
221 {"nisdomain" , DHCP_STRING | 0x28, NULL, 0}, | |
222 {"timezone" , DHCP_NUM32 | 0x02, NULL, 0}, | |
223 {"tftp" , DHCP_STRING | 0x42, NULL, 0}, | |
224 {"bootfile" , DHCP_STRING | 0x43, NULL, 0}, | |
225 {"bootsize" , DHCP_NUM16 | 0x0d, NULL, 0}, | |
226 {"rootpath" , DHCP_STRING | 0x11, NULL, 0}, | |
227 {"wpad" , DHCP_STRING | 0xfc, NULL, 0}, | |
228 {"serverid" , DHCP_IP | 0x36, NULL, 0}, | |
229 {"message" , DHCP_STRING | 0x38, NULL, 0}, | |
230 {"vlanid" , DHCP_NUM32 | 0x84, NULL, 0}, | |
231 {"vlanpriority" , DHCP_NUM32 | 0x85, NULL, 0}, | |
232 {"dns" , DHCP_IPLIST | 0x06, NULL, 0}, | |
233 {"wins" , DHCP_IPLIST | 0x2c, NULL, 0}, | |
234 {"nissrv" , DHCP_IPLIST | 0x29, NULL, 0}, | |
235 {"ntpsrv" , DHCP_IPLIST | 0x2a, NULL, 0}, | |
236 {"lprsrv" , DHCP_IPLIST | 0x09, NULL, 0}, | |
237 {"swapsrv" , DHCP_IP | 0x10, NULL, 0}, | |
238 {"routes" , DHCP_STCRTS | 0x21, NULL, 0}, | |
239 {"staticroutes" , DHCP_STCRTS | 0x79, NULL, 0}, | |
240 {"msstaticroutes" , DHCP_STCRTS | 0xf9, NULL, 0}, | |
241 }; | |
242 | |
243 static struct sock_filter filter_instr[] = { | |
244 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9), | |
245 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 0, 6), | |
246 BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 6), | |
247 BPF_JUMP(BPF_JMP|BPF_JSET|BPF_K, 0x1fff, 4, 0), | |
248 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), BPF_STMT(BPF_LD|BPF_H|BPF_IND, 2), | |
249 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 68, 0, 1), | |
250 BPF_STMT(BPF_RET|BPF_K, 0xffffffff), BPF_STMT(BPF_RET|BPF_K, 0), | |
251 }; | |
252 | |
253 static struct sock_fprog filter_prog = { | |
254 .len = ARRAY_LEN(filter_instr), | |
255 .filter = (struct sock_filter *) filter_instr, | |
256 }; | |
257 | |
258 // calculate options size. | |
259 static int dhcp_opt_size(uint8_t *optionptr) | |
260 { | |
261 int i = 0; | |
262 for(;optionptr[i] != 0xff; i++) if(optionptr[i] != 0x00) i += optionptr[i + 1] + 2 -1; | |
263 return i; | |
264 } | |
265 | |
266 // calculates checksum for dhcp messages. | |
267 static uint16_t dhcp_checksum(void *addr, int count) | |
268 { | |
269 int32_t sum = 0; | |
270 uint16_t tmp = 0, *source = (uint16_t *)addr; | |
271 | |
272 while (count > 1) { | |
273 sum += *source++; | |
274 count -= 2; | |
275 } | |
276 if (count > 0) { | |
277 *(uint8_t*)&tmp = *(uint8_t*)source; | |
278 sum += tmp; | |
279 } | |
280 while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16); | |
281 return ~sum; | |
282 } | |
283 | |
284 // gets information of INTERFACE and updates IFINDEX, MAC and IP | |
285 static int get_interface( char *interface, int *ifindex, uint32_t *oip, uint8_t *mac) | |
286 { | |
287 struct ifreq req; | |
288 struct sockaddr_in *ip; | |
289 int fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW); | |
290 | |
291 req.ifr_addr.sa_family = AF_INET; | |
292 strncpy(req.ifr_name, interface, IFNAMSIZ); | |
293 req.ifr_name[IFNAMSIZ-1] = '\0'; | |
294 | |
295 xioctl(fd, SIOCGIFFLAGS, &req); | |
296 if (!(req.ifr_flags & IFF_UP)) return -1; | |
297 | |
298 if (oip) { | |
299 xioctl(fd, SIOCGIFADDR, &req); | |
300 ip = (struct sockaddr_in*) &req.ifr_addr; | |
301 dbg("IP %s\n", inet_ntoa(ip->sin_addr)); | |
302 *oip = ntohl(ip->sin_addr.s_addr); | |
303 } | |
304 if (ifindex) { | |
305 xioctl(fd, SIOCGIFINDEX, &req); | |
306 dbg("Adapter index %d\n", req.ifr_ifindex); | |
307 *ifindex = req.ifr_ifindex; | |
308 } | |
309 if (mac) { | |
310 xioctl(fd, SIOCGIFHWADDR, &req); | |
311 memcpy(mac, req.ifr_hwaddr.sa_data, 6); | |
312 dbg("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); | |
313 } | |
314 close(fd); | |
315 return 0; | |
316 } | |
317 | |
318 static int dhcp_daemon(void) | |
319 { | |
320 int fd = open("/dev/null", O_RDWR); | |
321 if (fd < 0) fd = xcreate("/", O_RDONLY, 0666); | |
322 pid_t pid = fork(); | |
323 | |
324 if (pid < 0) perror_exit("DAEMON: failed to fork"); | |
325 if (pid) exit(EXIT_SUCCESS); | |
326 | |
327 setsid(); | |
328 dup2(fd, 0); | |
329 dup2(fd, 1); | |
330 dup2(fd, 2); | |
331 if (fd > 2) xclose(fd); | |
332 | |
333 return 0; | |
334 } | |
335 | |
336 /* | |
337 *logs messeges to syslog or console | |
338 *opening the log is still left with applet. | |
339 *FIXME: move to more relevent lib. probably libc.c | |
340 */ | |
341 static void infomsg(uint8_t infomode, char *s, ...) | |
342 { | |
343 int used; | |
344 char *msg; | |
345 va_list p, t; | |
346 | |
347 if (infomode == LOG_SILENT) return; | |
348 va_start(p, s); | |
349 va_copy(t, p); | |
350 used = vsnprintf(NULL, 0, s, t); | |
351 used++; | |
352 va_end(t); | |
353 | |
354 msg = xmalloc(used); | |
355 vsnprintf(msg, used, s, p); | |
356 va_end(p); | |
357 | |
358 if (infomode & LOG_SYSTEM) syslog(LOG_INFO, "%s", msg); | |
359 if (infomode & LOG_CONSOLE) printf("%s\n", msg); | |
360 free(msg); | |
361 } | |
362 | |
363 /* | |
364 * Writes self PID in file PATH | |
365 * FIXME: libc implementation only writes in /var/run | |
366 * this is more generic as some implemenation may provide | |
367 * arguments to write in specific file. as dhcpd does. | |
368 */ | |
369 static void write_pid(char *path) | |
370 { | |
371 int pidfile = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0666); | |
372 if (pidfile > 0) { | |
373 char *pidbuf = utoa(getpid()); | |
374 write(pidfile, pidbuf, strlen(pidbuf)); | |
375 close(pidfile); | |
376 } | |
377 } | |
378 | |
379 // String STR to UINT32 conversion strored in VAR | |
380 static long strtou32( char *str) | |
381 { | |
382 char *endptr = NULL; | |
383 int base = 10; | |
384 errno=0; | |
385 if (str[0]=='0' && (str[1]=='x' || str[1]=='X')) { | |
386 base = 16; | |
387 str+=2; | |
388 } | |
389 long ret_val = strtol(str, &endptr, base); | |
390 if (errno) return -1; | |
391 else if (endptr && (*endptr!='\0'||endptr == str)) return -1; | |
392 return ret_val; | |
393 } | |
394 | |
395 // IP String STR to binary data. | |
396 static int striptovar( char *str, void *var) | |
397 { | |
398 in_addr_t addr; | |
399 if(!str) error_exit("NULL address string."); | |
400 addr = inet_addr(str); | |
401 if(addr == -1) error_exit("Wrong address %s.",str ); | |
402 *((uint32_t*)(var)) = (uint32_t)addr; | |
403 return 0; | |
404 } | |
405 | |
406 // String to dhcp option conversion | |
407 static int strtoopt( char *str, uint8_t optonly) | |
408 { | |
409 char *option, *valstr, *grp, *tp; | |
410 long optcode = 0, convtmp; | |
411 uint16_t flag = 0; | |
412 uint32_t mask, nip, router; | |
413 int count, size = ARRAY_LEN(options_list); | |
414 | |
415 if (!*str) return 0; | |
416 option = strtok((char*)str, ":"); | |
417 if (!option) return -1; | |
418 | |
419 dbg("-x option : %s ", option); | |
420 optcode = strtou32(option); | |
421 | |
422 if (optcode > 0 && optcode < 256) { // raw option | |
423 for (count = 0; count < size; count++) { | |
424 if ((options_list[count].code & 0X00FF) == optcode) { | |
425 flag = (options_list[count].code & 0XFF00); | |
426 break; | |
427 } | |
428 } | |
429 if (count == size) error_exit("Obsolete OR Unknown Option : %s", option); | |
430 } else { // string option | |
431 for (count = 0; count < size; count++) { | |
432 if (!strcmp(options_list[count].key, option)) { | |
433 flag = (options_list[count].code & 0XFF00); | |
434 optcode = (options_list[count].code & 0X00FF); | |
435 break; | |
436 } | |
437 } | |
438 if (count == size) error_exit("Obsolete OR Unknown Option : %s", option); | |
439 } | |
440 if (!flag || !optcode) return -1; | |
441 if (optonly) return optcode; | |
442 | |
443 valstr = strtok(NULL, "\n"); | |
444 if (!valstr) error_exit("option %s has no value defined.\n", option); | |
445 dbg(" value : %-20s \n ", valstr); | |
446 switch (flag) { | |
447 case DHCP_NUM32: | |
448 options_list[count].len = sizeof(uint32_t); | |
449 options_list[count].val = xmalloc(sizeof(uint32_t)); | |
450 convtmp = strtou32(valstr); | |
451 if (convtmp < 0) error_exit("Invalid/wrong formated number %s", valstr); | |
452 convtmp = htonl(convtmp); | |
453 memcpy(options_list[count].val, &convtmp, sizeof(uint32_t)); | |
454 break; | |
455 case DHCP_NUM16: | |
456 options_list[count].len = sizeof(uint16_t); | |
457 options_list[count].val = xmalloc(sizeof(uint16_t)); | |
458 convtmp = strtou32(valstr); | |
459 if (convtmp < 0) error_exit("Invalid/malformed number %s", valstr); | |
460 convtmp = htons(convtmp); | |
461 memcpy(options_list[count].val, &convtmp, sizeof(uint16_t)); | |
462 break; | |
463 case DHCP_NUM8: | |
464 options_list[count].len = sizeof(uint8_t); | |
465 options_list[count].val = xmalloc(sizeof(uint8_t)); | |
466 convtmp = strtou32(valstr); | |
467 if (convtmp < 0) error_exit("Invalid/malformed number %s", valstr); | |
468 memcpy(options_list[count].val, &convtmp, sizeof(uint8_t)); | |
469 break; | |
470 case DHCP_IP: | |
471 options_list[count].len = sizeof(uint32_t); | |
472 options_list[count].val = xmalloc(sizeof(uint32_t)); | |
473 striptovar(valstr, options_list[count].val); | |
474 break; | |
475 case DHCP_STRING: | |
476 options_list[count].len = strlen(valstr); | |
477 options_list[count].val = strdup(valstr); | |
478 break; | |
479 case DHCP_IPLIST: | |
480 while(valstr){ | |
481 options_list[count].val = xrealloc(options_list[count].val, options_list[count].len + sizeof(uint32_t)); | |
482 striptovar(valstr, ((uint8_t*)options_list[count].val)+options_list[count].len); | |
483 options_list[count].len += sizeof(uint32_t); | |
484 valstr = strtok(NULL," \t"); | |
485 } | |
486 break; | |
487 case DHCP_STRLST: | |
488 case DHCP_IPPLST: | |
489 break; | |
490 case DHCP_STCRTS: | |
491 /* Option binary format: | |
492 * mask [one byte, 0..32] | |
493 * ip [0..4 bytes depending on mask] | |
494 * router [4 bytes] | |
495 * may be repeated | |
496 * staticroutes 10.0.0.0/8 10.127.0.1, 10.11.12.0/24 10.11.12.1 | |
497 */ | |
498 grp = strtok(valstr, ",");; | |
499 while(grp){ | |
500 while(*grp == ' ' || *grp == '\t') grp++; | |
501 tp = strchr(grp, '/'); | |
502 if (!tp) error_exit("malformed static route option"); | |
503 *tp = '\0'; | |
504 mask = strtol(++tp, &tp, 10); | |
505 if (striptovar(grp, (uint8_t*)&nip) < 0) error_exit("malformed static route option"); | |
506 while(*tp == ' ' || *tp == '\t' || *tp == '-') tp++; | |
507 if (striptovar(tp, (uint8_t*)&router) < 0) error_exit("malformed static route option"); | |
508 options_list[count].val = xrealloc(options_list[count].val, options_list[count].len + 1 + mask/8 + 4); | |
509 memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &mask, 1); | |
510 options_list[count].len += 1; | |
511 memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &nip, mask/8); | |
512 options_list[count].len += mask/8; | |
513 memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &router, 4); | |
514 options_list[count].len += 4; | |
515 tp = NULL; | |
516 grp = strtok(NULL, ","); | |
517 } | |
518 break; | |
519 } | |
520 return 0; | |
521 } | |
522 | |
523 // Creates environment pointers from RES to use in script | |
524 static int fill_envp(dhcpc_result_t *res) | |
525 { | |
526 struct in_addr temp; | |
527 int size = ARRAY_LEN(options_list), count, ret = -1; | |
528 | |
529 ret = setenv("interface", state->iface, 1); | |
530 if (!res) return ret; | |
531 if (res->ipaddr.s_addr) { | |
532 temp.s_addr = htonl(res->ipaddr.s_addr); | |
533 ret = setenv("ip", inet_ntoa(temp), 1); | |
534 if (ret) return ret; | |
535 } | |
536 if (msgopt_list) { | |
537 for (count = 0; count < size; count++) { | |
538 if ((msgopt_list[count].len == 0) || (msgopt_list[count].val == NULL)) continue; | |
539 ret = setenv(msgopt_list[count].key, (char*)msgopt_list[count].val, 1); | |
540 if (ret) return ret; | |
541 } | |
542 } | |
543 return ret; | |
544 } | |
545 | |
546 // Executes Script NAME. | |
547 static void run_script(dhcpc_result_t *res, char *name) | |
548 { | |
549 volatile int error = 0; | |
550 pid_t pid; | |
551 char *argv[3]; | |
552 struct stat sts; | |
553 char *script = flag_get(FLAG_s, TT.script, "/usr/share/dhcp/default.script"); | |
554 | |
555 if (stat(script, &sts) == -1 && errno == ENOENT) return; | |
556 if (fill_envp(res)) { | |
557 dbg("Failed to create environment variables."); | |
558 return; | |
559 } | |
560 dbg("Executing %s %s\n", script, name); | |
561 argv[0] = (char*) script; | |
562 argv[1] = (char*) name; | |
563 argv[2] = NULL; | |
564 fflush(NULL); | |
565 | |
566 pid = vfork(); | |
567 if (pid < 0) { | |
568 dbg("Fork failed.\n"); | |
569 return; | |
570 } | |
571 if (!pid) { | |
572 execvp(argv[0], argv); | |
573 error = errno; | |
574 _exit(111); | |
575 } | |
576 if (error) { | |
577 waitpid(pid, NULL,0); | |
578 errno = error; | |
579 perror_msg("script exec failed"); | |
580 } | |
581 dbg("script complete.\n"); | |
582 } | |
583 | |
584 // returns a randome ID | |
585 static uint32_t getxid(void) | |
586 { | |
587 uint32_t randnum; | |
588 int fd = xopen("/dev/urandom", O_RDONLY); | |
589 xreadall(fd, &randnum, sizeof(randnum)); | |
590 xclose(fd); | |
591 return randnum; | |
592 } | |
593 | |
594 // opens socket in raw mode. | |
595 static int mode_raw(void) | |
596 { | |
597 state->mode = MODE_OFF; | |
598 struct sockaddr_ll sock; | |
599 | |
600 if (state->sockfd > 0) close(state->sockfd); | |
601 dbg("Opening raw socket on ifindex %d\n", state->ifindex); | |
602 | |
603 state->sockfd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); | |
604 if (state->sockfd < 0) { | |
605 dbg("MODE RAW : socket fail ERROR : %d\n", state->sockfd); | |
606 return -1; | |
607 } | |
608 dbg("Got raw socket fd %d\n", state->sockfd); | |
609 memset(&sock, 0, sizeof(sock)); | |
610 sock.sll_family = AF_PACKET; | |
611 sock.sll_protocol = htons(ETH_P_IP); | |
612 sock.sll_ifindex = state->ifindex; | |
613 | |
614 if (bind(state->sockfd, (struct sockaddr *) &sock, sizeof(sock))) { | |
615 dbg("MODE RAW : bind fail.\n"); | |
616 close(state->sockfd); | |
617 return -1; | |
618 } | |
619 state->mode = MODE_RAW; | |
620 if (setsockopt(state->sockfd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, sizeof(filter_prog)) < 0) | |
621 dbg("MODE RAW : filter attach fail.\n"); | |
622 | |
623 dbg("MODE RAW : success\n"); | |
624 return 0; | |
625 } | |
626 | |
627 // opens UDP socket | |
628 static int mode_app(void) | |
629 { | |
630 struct sockaddr_in addr; | |
631 struct ifreq ifr; | |
632 | |
633 state->mode = MODE_OFF; | |
634 if (state->sockfd > 0) close(state->sockfd); | |
635 | |
636 dbg("Opening listen socket on *:%d %s\n", DHCPC_CLIENT_PORT, state->iface); | |
637 state->sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
638 if (state->sockfd < 0) { | |
639 dbg("MODE APP : socket fail ERROR: %d\n", state->sockfd); | |
640 return -1; | |
641 } | |
642 setsockopt(state->sockfd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set)); | |
643 if (setsockopt(state->sockfd, SOL_SOCKET, SO_BROADCAST, &set, sizeof(set)) == -1) { | |
644 dbg("MODE APP : brodcast failed.\n"); | |
645 close(state->sockfd); | |
646 return -1; | |
647 } | |
648 strncpy(ifr.ifr_name, state->iface, IFNAMSIZ); | |
649 ifr.ifr_name[IFNAMSIZ -1] = '\0'; | |
650 setsockopt(state->sockfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)); | |
651 | |
652 memset(&addr, 0, sizeof(addr)); | |
653 addr.sin_family = AF_INET; | |
654 addr.sin_port = htons(DHCPC_CLIENT_PORT); | |
655 addr.sin_addr.s_addr = INADDR_ANY ; | |
656 | |
657 if (bind(state->sockfd, (struct sockaddr *) &addr, sizeof(addr))) { | |
658 close(state->sockfd); | |
659 dbg("MODE APP : bind failed.\n"); | |
660 return -1; | |
661 } | |
662 state->mode = MODE_APP; | |
663 dbg("MODE APP : success\n"); | |
664 return 0; | |
665 } | |
666 | |
667 static int read_raw(void) | |
668 { | |
669 dhcp_raw_t packet; | |
670 uint16_t check; | |
671 int bytes = 0; | |
672 | |
673 memset(&packet, 0, sizeof(packet)); | |
674 if ((bytes = read(state->sockfd, &packet, sizeof(packet))) < 0) { | |
675 dbg("\tPacket read error, ignoring\n"); | |
676 return bytes; | |
677 } | |
678 if (bytes < (int) (sizeof(packet.iph) + sizeof(packet.udph))) { | |
679 dbg("\tPacket is too short, ignoring\n"); | |
680 return -2; | |
681 } | |
682 if (bytes < ntohs(packet.iph.tot_len)) { | |
683 dbg("\tOversized packet, ignoring\n"); | |
684 return -2; | |
685 } | |
686 // ignore any extra garbage bytes | |
687 bytes = ntohs(packet.iph.tot_len); | |
688 // make sure its the right packet for us, and that it passes sanity checks | |
689 if (packet.iph.protocol != IPPROTO_UDP || packet.iph.version != IPVERSION | |
690 || packet.iph.ihl != (sizeof(packet.iph) >> 2) | |
691 || packet.udph.dest != htons(DHCPC_CLIENT_PORT) | |
692 || ntohs(packet.udph.len) != (uint16_t)(bytes - sizeof(packet.iph))) { | |
693 dbg("\tUnrelated/bogus packet, ignoring\n"); | |
694 return -2; | |
695 } | |
696 // verify IP checksum | |
697 check = packet.iph.check; | |
698 packet.iph.check = 0; | |
699 if (check != dhcp_checksum(&packet.iph, sizeof(packet.iph))) { | |
700 dbg("\tBad IP header checksum, ignoring\n"); | |
701 return -2; | |
702 } | |
703 memset(&packet.iph, 0, ((size_t) &((struct iphdr *)0)->protocol)); | |
704 packet.iph.tot_len = packet.udph.len; | |
705 check = packet.udph.check; | |
706 packet.udph.check = 0; | |
707 if (check && check != dhcp_checksum(&packet, bytes)) { | |
708 dbg("\tPacket with bad UDP checksum received, ignoring\n"); | |
709 return -2; | |
710 } | |
711 memcpy(&state->pdhcp, &packet.dhcp, bytes - (sizeof(packet.iph) + sizeof(packet.udph))); | |
712 if (state->pdhcp.cookie != htonl(DHCP_MAGIC)) { | |
713 dbg("\tPacket with bad magic, ignoring\n"); | |
714 return -2; | |
715 } | |
716 return bytes - sizeof(packet.iph) - sizeof(packet.udph); | |
717 } | |
718 | |
719 static int read_app(void) | |
720 { | |
721 int ret; | |
722 | |
723 memset(&state->pdhcp, 0, sizeof(dhcp_msg_t)); | |
724 if ((ret = read(state->sockfd, &state->pdhcp, sizeof(dhcp_msg_t))) < 0) { | |
725 dbg("Packet read error, ignoring\n"); | |
726 return ret; /* returns -1 */ | |
727 } | |
728 if (state->pdhcp.cookie != htonl(DHCP_MAGIC)) { | |
729 dbg("Packet with bad magic, ignoring\n"); | |
730 return -2; | |
731 } | |
732 return ret; | |
733 } | |
734 | |
735 // Sends data through raw socket. | |
736 static int send_raw(void) | |
737 { | |
738 struct sockaddr_ll dest_sll; | |
739 dhcp_raw_t packet; | |
740 unsigned padding; | |
741 int fd, result = -1; | |
742 | |
743 memset(&packet, 0, sizeof(dhcp_raw_t)); | |
744 memcpy(&packet.dhcp, &state->pdhcp, sizeof(dhcp_msg_t)); | |
745 | |
746 if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) { | |
747 dbg("SEND RAW: socket failed\n"); | |
748 return result; | |
749 } | |
750 memset(&dest_sll, 0, sizeof(dest_sll)); | |
751 dest_sll.sll_family = AF_PACKET; | |
752 dest_sll.sll_protocol = htons(ETH_P_IP); | |
753 dest_sll.sll_ifindex = state->ifindex; | |
754 dest_sll.sll_halen = 6; | |
755 memcpy(dest_sll.sll_addr, bmacaddr , 6); | |
756 | |
757 if (bind(fd, (struct sockaddr *) &dest_sll, sizeof(dest_sll)) < 0) { | |
758 dbg("SEND RAW: bind failed\n"); | |
759 close(fd); | |
760 return result; | |
761 } | |
762 padding = 308 - 1 - dhcp_opt_size(state->pdhcp.options); | |
763 packet.iph.protocol = IPPROTO_UDP; | |
764 packet.iph.saddr = INADDR_ANY; | |
765 packet.iph.daddr = INADDR_BROADCAST; | |
766 packet.udph.source = htons(DHCPC_CLIENT_PORT); | |
767 packet.udph.dest = htons(DHCPC_SERVER_PORT); | |
768 packet.udph.len = htons(sizeof(dhcp_raw_t) - sizeof(struct iphdr) - padding); | |
769 packet.iph.tot_len = packet.udph.len; | |
770 packet.udph.check = dhcp_checksum(&packet, sizeof(dhcp_raw_t) - padding); | |
771 packet.iph.tot_len = htons(sizeof(dhcp_raw_t) - padding); | |
772 packet.iph.ihl = sizeof(packet.iph) >> 2; | |
773 packet.iph.version = IPVERSION; | |
774 packet.iph.ttl = IPDEFTTL; | |
775 packet.iph.check = dhcp_checksum(&packet.iph, sizeof(packet.iph)); | |
776 | |
777 result = sendto(fd, &packet, sizeof(dhcp_raw_t) - padding, 0, | |
778 (struct sockaddr *) &dest_sll, sizeof(dest_sll)); | |
779 | |
780 close(fd); | |
781 if (result < 0) dbg("SEND RAW: PACKET send error\n"); | |
782 return result; | |
783 } | |
784 | |
785 // Sends data through UDP socket. | |
786 static int send_app(void) | |
787 { | |
788 struct sockaddr_in cli; | |
789 int fd, ret = -1; | |
790 | |
791 if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { | |
792 dbg("SEND APP: sock failed.\n"); | |
793 return ret; | |
794 } | |
795 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set)); | |
796 | |
797 memset(&cli, 0, sizeof(cli)); | |
798 cli.sin_family = AF_INET; | |
799 cli.sin_port = htons(DHCPC_CLIENT_PORT); | |
800 cli.sin_addr.s_addr = state->pdhcp.ciaddr; | |
801 if (bind(fd, (struct sockaddr *)&cli, sizeof(cli)) == -1) { | |
802 dbg("SEND APP: bind failed.\n"); | |
803 goto error_fd; | |
804 } | |
805 memset(&cli, 0, sizeof(cli)); | |
806 cli.sin_family = AF_INET; | |
807 cli.sin_port = htons(DHCPC_SERVER_PORT); | |
808 cli.sin_addr.s_addr = state->serverid.s_addr; | |
809 if (connect(fd, (struct sockaddr *)&cli, sizeof(cli)) == -1) { | |
810 dbg("SEND APP: connect failed.\n"); | |
811 goto error_fd; | |
812 } | |
813 int padding = 308 - 1 - dhcp_opt_size(state->pdhcp.options); | |
814 if((ret = write(fd, &state->pdhcp, sizeof(dhcp_msg_t) - padding)) < 0) { | |
815 dbg("SEND APP: write failed error %d\n", ret); | |
816 goto error_fd; | |
817 } | |
818 dbg("SEND APP: write success wrote %d\n", ret); | |
819 error_fd: | |
820 close(fd); | |
821 return ret; | |
822 } | |
823 | |
824 // Generic signal handler real handling is done in main funcrion. | |
825 static void signal_handler(int sig) | |
826 { | |
827 unsigned char ch = sig; | |
828 if (write(sigfd.wr, &ch, 1) != 1) dbg("can't send signal\n"); | |
829 } | |
830 | |
831 // signal setup for SIGUSR1 SIGUSR2 SIGTERM | |
832 static int setup_signal() | |
833 { | |
834 if (pipe((int *)&sigfd) < 0) { | |
835 dbg("signal pipe failed\n"); | |
836 return -1; | |
837 } | |
838 fcntl(sigfd.wr , F_SETFD, FD_CLOEXEC); | |
839 fcntl(sigfd.rd , F_SETFD, FD_CLOEXEC); | |
840 int flags = fcntl(sigfd.wr, F_GETFL); | |
841 fcntl(sigfd.wr, F_SETFL, flags | O_NONBLOCK); | |
842 signal(SIGUSR1, signal_handler); | |
843 signal(SIGUSR2, signal_handler); | |
844 signal(SIGTERM, signal_handler); | |
845 | |
846 return 0; | |
847 } | |
848 | |
849 // adds client id to dhcp packet | |
850 static uint8_t *dhcpc_addclientid(uint8_t *optptr) | |
851 { | |
852 *optptr++ = DHCP_OPTION_CLIENTID; | |
853 *optptr++ = 7; | |
854 *optptr++ = 1; | |
855 memcpy(optptr, &state->macaddr, 6); | |
856 return optptr + 6; | |
857 } | |
858 | |
859 // adds messege type to dhcp packet | |
860 static uint8_t *dhcpc_addmsgtype(uint8_t *optptr, uint8_t type) | |
861 { | |
862 *optptr++ = DHCP_OPTION_MSG_TYPE; | |
863 *optptr++ = 1; | |
864 *optptr++ = type; | |
865 return optptr; | |
866 } | |
867 | |
868 // adds max size to dhcp packet | |
869 static uint8_t *dhcpc_addmaxsize(uint8_t *optptr, uint16_t size) | |
870 { | |
871 *optptr++ = DHCP_OPTION_MAX_SIZE; | |
872 *optptr++ = 2; | |
873 memcpy(optptr, &size, 2); | |
874 return optptr + 2; | |
875 } | |
876 | |
877 static uint8_t *dhcpc_addstropt(uint8_t *optptr, uint8_t opcode, char* str, int len) | |
878 { | |
879 *optptr++ = opcode; | |
880 *optptr++ = len; | |
881 memcpy(optptr, str, len); | |
882 return optptr + len; | |
883 } | |
884 | |
885 // adds server id to dhcp packet. | |
886 static uint8_t *dhcpc_addserverid(struct in_addr *serverid, uint8_t *optptr) | |
887 { | |
888 *optptr++ = DHCP_OPTION_SERVER_ID; | |
889 *optptr++ = 4; | |
890 memcpy(optptr, &serverid->s_addr, 4); | |
891 return optptr + 4; | |
892 } | |
893 | |
894 // adds requested ip address to dhcp packet. | |
895 static uint8_t *dhcpc_addreqipaddr(struct in_addr *ipaddr, uint8_t *optptr) | |
896 { | |
897 *optptr++ = DHCP_OPTION_REQ_IPADDR; | |
898 *optptr++ = 4; | |
899 memcpy(optptr, &ipaddr->s_addr, 4); | |
900 return optptr + 4; | |
901 } | |
902 | |
903 // adds hostname to dhcp packet. | |
904 static uint8_t *dhcpc_addfdnname(uint8_t *optptr, char *hname) | |
905 { | |
906 int size = strlen(hname); | |
907 *optptr++ = DHCP_OPTION_FQDN; | |
908 *optptr++ = size + 3; | |
909 *optptr++ = 0x1; //flags | |
910 optptr += 2; // two blank bytes | |
911 strncpy((char*)optptr, hname, size); // name | |
912 return optptr + size; | |
913 } | |
914 | |
915 // adds request options using -o,-O flag to dhcp packet | |
916 static uint8_t *dhcpc_addreqoptions(uint8_t *optptr) | |
917 { | |
918 uint8_t *len; | |
919 | |
920 *optptr++ = DHCP_OPTION_REQ_LIST; | |
921 len = optptr; | |
922 *len = 0; | |
923 optptr++; | |
924 | |
925 if (!flag_chk(FLAG_o)) { | |
926 *len = 4; | |
927 *optptr++ = DHCP_OPTION_SUBNET_MASK; | |
928 *optptr++ = DHCP_OPTION_ROUTER; | |
929 *optptr++ = DHCP_OPTION_DNS_SERVER; | |
930 *optptr++ = DHCP_OPTION_BROADCAST; | |
931 } | |
932 if (flag_chk(FLAG_O)) { | |
933 memcpy(optptr++, raw_opt, raw_optcount); | |
934 *len += raw_optcount; | |
935 } | |
936 return optptr; | |
937 } | |
938 | |
939 static uint8_t *dhcpc_addend(uint8_t *optptr) | |
940 { | |
941 *optptr++ = DHCP_OPTION_END; | |
942 return optptr; | |
943 } | |
944 | |
945 // Sets values of -x options in dhcp discover and request packet. | |
946 static uint8_t* set_xopt(uint8_t *optptr) | |
947 { | |
948 int count; | |
949 int size = ARRAY_LEN(options_list); | |
950 for (count = 0; count < size; count++) { | |
951 if ((options_list[count].len == 0) || (options_list[count].val == NULL)) continue; | |
952 *optptr++ = (uint8_t) (options_list[count].code & 0x00FF); | |
953 *optptr++ = (uint8_t) options_list[count].len; | |
954 memcpy(optptr, options_list[count].val, options_list[count].len); | |
955 optptr += options_list[count].len; | |
956 } | |
957 return optptr; | |
958 } | |
959 | |
960 static uint32_t get_option_serverid (uint8_t *opt, dhcpc_result_t *presult) | |
961 { | |
962 uint32_t var = 0; | |
963 while (*opt != DHCP_OPTION_SERVER_ID) { | |
964 if (*opt == DHCP_OPTION_END) return var; | |
965 opt += opt[1] + 2; | |
966 } | |
967 memcpy(&var, opt+2, sizeof(uint32_t)); | |
968 state->serverid.s_addr = var; | |
969 presult->serverid.s_addr = state->serverid.s_addr; | |
970 presult->serverid.s_addr = ntohl(presult->serverid.s_addr); | |
971 return var; | |
972 } | |
973 | |
974 static uint8_t get_option_msgtype(uint8_t *opt) | |
975 { | |
976 uint32_t var = 0; | |
977 while (*opt != DHCP_OPTION_MSG_TYPE) { | |
978 if (*opt == DHCP_OPTION_END) return var; | |
979 opt += opt[1] + 2; | |
980 } | |
981 memcpy(&var, opt+2, sizeof(uint8_t)); | |
982 return var; | |
983 } | |
984 | |
985 static uint8_t get_option_lease(uint8_t *opt, dhcpc_result_t *presult) | |
986 { | |
987 uint32_t var = 0; | |
988 while (*opt != DHCP_OPTION_LEASE_TIME) { | |
989 if (*opt == DHCP_OPTION_END) return var; | |
990 opt += opt[1] + 2; | |
991 } | |
992 memcpy(&var, opt+2, sizeof(uint32_t)); | |
993 var = htonl(var); | |
994 presult->lease_time = var; | |
995 return var; | |
996 } | |
997 | |
998 | |
999 // sends dhcp msg of MSGTYPE | |
1000 static int dhcpc_sendmsg(int msgtype) | |
1001 { | |
1002 uint8_t *pend; | |
1003 struct in_addr rqsd; | |
1004 char *vendor; | |
1005 | |
1006 // Create the common message header settings | |
1007 memset(&state->pdhcp, 0, sizeof(dhcp_msg_t)); | |
1008 state->pdhcp.op = DHCP_REQUEST; | |
1009 state->pdhcp.htype = DHCP_HTYPE_ETHERNET; | |
1010 state->pdhcp.hlen = 6; | |
1011 state->pdhcp.xid = xid; | |
1012 memcpy(state->pdhcp.chaddr, state->macaddr, 6); | |
1013 memset(&state->pdhcp.chaddr[6], 0, 10); | |
1014 state->pdhcp.cookie = htonl(DHCP_MAGIC);; | |
1015 | |
1016 // Add the common header options | |
1017 pend = state->pdhcp.options; | |
1018 pend = dhcpc_addmsgtype(pend, msgtype); | |
1019 | |
1020 if (!flag_chk(FLAG_C)) pend = dhcpc_addclientid(pend); | |
1021 // Handle the message specific settings | |
1022 switch (msgtype) { | |
1023 case DHCPDISCOVER: // Broadcast DISCOVER message to all servers | |
1024 state->pdhcp.flags = htons(BOOTP_BROADCAST); // Broadcast bit. | |
1025 if (flag_chk(FLAG_r)) { | |
1026 inet_aton(TT.req_ip, &rqsd); | |
1027 pend = dhcpc_addreqipaddr(&rqsd, pend); | |
1028 } | |
1029 pend = dhcpc_addmaxsize(pend, htons(sizeof(dhcp_raw_t))); | |
1030 vendor = flag_get(FLAG_V, TT.vendor_cls, "toybox\0"); | |
1031 pend = dhcpc_addstropt(pend, DHCP_OPTION_VENDOR, vendor, strlen(vendor)); | |
1032 if (flag_chk(FLAG_H)) pend = dhcpc_addstropt(pend, DHCP_OPTION_HOST_NAME, TT.hostname, strlen(TT.hostname)); | |
1033 if (flag_chk(FLAG_F)) pend = dhcpc_addfdnname(pend, TT.fdn_name); | |
1034 if ((!flag_chk(FLAG_o)) || flag_chk(FLAG_O)) pend = dhcpc_addreqoptions(pend); | |
1035 if (flag_chk(FLAG_x)) pend = set_xopt(pend); | |
1036 break; | |
1037 case DHCPREQUEST: // Send REQUEST message to the server that sent the *first* OFFER | |
1038 state->pdhcp.flags = htons(BOOTP_BROADCAST); // Broadcast bit. | |
1039 if (state->status == STATE_RENEWING) memcpy(&state->pdhcp.ciaddr, &state->ipaddr.s_addr, 4); | |
1040 pend = dhcpc_addmaxsize(pend, htons(sizeof(dhcp_raw_t))); | |
1041 rqsd.s_addr = htonl(server); | |
1042 pend = dhcpc_addserverid(&rqsd, pend); | |
1043 pend = dhcpc_addreqipaddr(&state->ipaddr, pend); | |
1044 vendor = flag_get(FLAG_V, TT.vendor_cls, "toybox\0"); | |
1045 pend = dhcpc_addstropt(pend, DHCP_OPTION_VENDOR, vendor, strlen(vendor)); | |
1046 if (flag_chk(FLAG_H)) pend = dhcpc_addstropt(pend, DHCP_OPTION_HOST_NAME, TT.hostname, strlen(TT.hostname)); | |
1047 if (flag_chk(FLAG_F)) pend = dhcpc_addfdnname(pend, TT.fdn_name); | |
1048 if ((!flag_chk(FLAG_o)) || flag_chk(FLAG_O)) pend = dhcpc_addreqoptions(pend); | |
1049 if (flag_chk(FLAG_x)) pend = set_xopt(pend); | |
1050 break; | |
1051 case DHCPRELEASE: // Send RELEASE message to the server. | |
1052 memcpy(&state->pdhcp.ciaddr, &state->ipaddr.s_addr, 4); | |
1053 rqsd.s_addr = htonl(server); | |
1054 pend = dhcpc_addserverid(&rqsd, pend); | |
1055 break; | |
1056 default: | |
1057 return -1; | |
1058 } | |
1059 pend = dhcpc_addend(pend); | |
1060 | |
1061 if (state->mode == MODE_APP) return send_app(); | |
1062 return send_raw(); | |
1063 } | |
1064 | |
1065 /* | |
1066 * parses options from received dhcp packet at OPTPTR and | |
1067 * stores result in PRESULT or MSGOPT_LIST | |
1068 */ | |
1069 static uint8_t dhcpc_parseoptions(dhcpc_result_t *presult, uint8_t *optptr) | |
1070 { | |
1071 uint8_t type = 0, *options, overloaded = 0;; | |
1072 uint16_t flag = 0; | |
1073 uint32_t convtmp = 0; | |
1074 char *dest, *pfx; | |
1075 struct in_addr addr; | |
1076 int count, optlen, size = ARRAY_LEN(options_list); | |
1077 | |
1078 if (flag_chk(FLAG_x)) { | |
1079 if(msgopt_list){ | |
1080 for (count = 0; count < size; count++){ | |
1081 if(msgopt_list[count].val) free(msgopt_list[count].val); | |
1082 msgopt_list[count].val = NULL; | |
1083 msgopt_list[count].len = 0; | |
1084 } | |
1085 } else { | |
1086 msgopt_list = xmalloc(sizeof(options_list)); | |
1087 memcpy(msgopt_list, options_list, sizeof(options_list)); | |
1088 for (count = 0; count < size; count++) { | |
1089 msgopt_list[count].len = 0; | |
1090 msgopt_list[count].val = NULL; | |
1091 } | |
1092 } | |
1093 } else { | |
1094 msgopt_list = options_list; | |
1095 for (count = 0; count < size; count++) { | |
1096 msgopt_list[count].len = 0; | |
1097 if(msgopt_list[count].val) free(msgopt_list[count].val); | |
1098 msgopt_list[count].val = NULL; | |
1099 } | |
1100 } | |
1101 | |
1102 while (*optptr != DHCP_OPTION_END) { | |
1103 while (*optptr == DHCP_OPTION_PADDING) optptr++; | |
1104 if (*optptr == DHCP_OPTION_OVERLOAD) { | |
1105 overloaded = optptr[2]; | |
1106 optptr += optptr[1] + 2; | |
1107 continue; | |
1108 } | |
1109 for (count = 0, flag = 0; count < size; count++) { | |
1110 if ((msgopt_list[count].code & 0X00FF) == *optptr) { | |
1111 flag = (msgopt_list[count].code & 0XFF00); | |
1112 break; | |
1113 } | |
1114 } | |
1115 switch (flag) { | |
1116 case DHCP_NUM32: | |
1117 memcpy(&convtmp, &optptr[2], sizeof(uint32_t)); | |
1118 convtmp = htonl(convtmp); | |
1119 sprintf(toybuf, "%u", convtmp); | |
1120 msgopt_list[count].val = strdup(toybuf); | |
1121 msgopt_list[count].len = strlen(toybuf); | |
1122 break; | |
1123 case DHCP_NUM16: | |
1124 memcpy(&convtmp, &optptr[2], sizeof(uint16_t)); | |
1125 convtmp = htons(convtmp); | |
1126 sprintf(toybuf, "%u", convtmp); | |
1127 msgopt_list[count].val = strdup(toybuf); | |
1128 msgopt_list[count].len = strlen(toybuf); | |
1129 break; | |
1130 case DHCP_NUM8: | |
1131 memcpy(&convtmp, &optptr[2], sizeof(uint8_t)); | |
1132 sprintf(toybuf, "%u", convtmp); | |
1133 msgopt_list[count].val = strdup(toybuf); | |
1134 msgopt_list[count].len = strlen(toybuf); | |
1135 break; | |
1136 case DHCP_IP: | |
1137 memcpy(&convtmp, &optptr[2], sizeof(uint32_t)); | |
1138 addr.s_addr = convtmp; | |
1139 sprintf(toybuf, "%s", inet_ntoa(addr)); | |
1140 msgopt_list[count].val = strdup(toybuf); | |
1141 msgopt_list[count].len = strlen(toybuf); | |
1142 break; | |
1143 case DHCP_STRING: | |
1144 sprintf(toybuf, "%.*s", optptr[1], &optptr[2]); | |
1145 msgopt_list[count].val = strdup(toybuf); | |
1146 msgopt_list[count].len = strlen(toybuf); | |
1147 break; | |
1148 case DHCP_IPLIST: | |
1149 optlen = optptr[1]; | |
1150 dest = toybuf; | |
1151 while (optlen) { | |
1152 memcpy(&convtmp, &optptr[2], sizeof(uint32_t)); | |
1153 addr.s_addr = convtmp; | |
1154 dest += sprintf(dest, "%s ", inet_ntoa(addr)); | |
1155 optlen -= 4; | |
1156 } | |
1157 *(dest - 1) = '\0'; | |
1158 msgopt_list[count].val = strdup(toybuf); | |
1159 msgopt_list[count].len = strlen(toybuf); | |
1160 break; | |
1161 case DHCP_STRLST: //FIXME: do smthing. | |
1162 case DHCP_IPPLST: | |
1163 break; | |
1164 case DHCP_STCRTS: | |
1165 pfx = ""; | |
1166 dest = toybuf; | |
1167 options = &optptr[2]; | |
1168 optlen = optptr[1]; | |
1169 | |
1170 while (optlen >= 1 + 4) { | |
1171 uint32_t nip = 0; | |
1172 int bytes; | |
1173 uint8_t *p_tmp; | |
1174 unsigned mask = *options; | |
1175 | |
1176 if (mask > 32) break; | |
1177 optlen--; | |
1178 p_tmp = (void*) &nip; | |
1179 bytes = (mask + 7) / 8; | |
1180 while (--bytes >= 0) { | |
1181 *p_tmp++ = *options++; | |
1182 optlen--; | |
1183 } | |
1184 if (optlen < 4) break; | |
1185 dest += sprintf(dest, "%s%u.%u.%u.%u", pfx, ((uint8_t*) &nip)[0], | |
1186 ((uint8_t*) &nip)[1], ((uint8_t*) &nip)[2], ((uint8_t*) &nip)[3]); | |
1187 pfx = " "; | |
1188 dest += sprintf(dest, "/%u ", mask); | |
1189 dest += sprintf(dest, "%u.%u.%u.%u", options[0], options[1], options[2], options[3]); | |
1190 options += 4; | |
1191 optlen -= 4; | |
1192 } | |
1193 msgopt_list[count].val = strdup(toybuf); | |
1194 msgopt_list[count].len = strlen(toybuf); | |
1195 break; | |
1196 default: break; | |
1197 } | |
1198 optptr += optptr[1] + 2; | |
1199 } | |
1200 if ((overloaded == 1) || (overloaded == 3)) dhcpc_parseoptions(presult, optptr); | |
1201 if ((overloaded == 2) || (overloaded == 3)) dhcpc_parseoptions(presult, optptr); | |
1202 return type; | |
1203 } | |
1204 | |
1205 // parses recvd messege to check that it was for us. | |
1206 static uint8_t dhcpc_parsemsg(dhcpc_result_t *presult) | |
1207 { | |
1208 if (state->pdhcp.op == DHCP_REPLY | |
1209 && !memcmp(state->pdhcp.chaddr, state->macaddr, 6) | |
1210 && !memcmp(&state->pdhcp.xid, &xid, sizeof(xid))) { | |
1211 memcpy(&presult->ipaddr.s_addr, &state->pdhcp.yiaddr, 4); | |
1212 presult->ipaddr.s_addr = ntohl(presult->ipaddr.s_addr); | |
1213 return get_option_msgtype(state->pdhcp.options); | |
1214 } | |
1215 return 0; | |
1216 } | |
1217 | |
1218 // Sends a IP renew request. | |
1219 static void renew(void) | |
1220 { | |
1221 infomsg(infomode, "Performing a DHCP renew"); | |
1222 switch (state->status) { | |
1223 case STATE_INIT: | |
1224 break; | |
1225 case STATE_BOUND: | |
1226 mode_raw(); | |
1227 case STATE_RENEWING: // FALLTHROUGH | |
1228 case STATE_REBINDING: // FALLTHROUGH | |
1229 state->status = STATE_RENEW_REQUESTED; | |
1230 break; | |
1231 case STATE_RENEW_REQUESTED: | |
1232 run_script(NULL, "deconfig"); | |
1233 case STATE_REQUESTING: // FALLTHROUGH | |
1234 case STATE_RELEASED: // FALLTHROUGH | |
1235 mode_raw(); | |
1236 state->status = STATE_INIT; | |
1237 break; | |
1238 default: break; | |
1239 } | |
1240 } | |
1241 | |
1242 // Sends a IP release request. | |
1243 static void release(void) | |
1244 { | |
1245 int len = sizeof("255.255.255.255\0"); | |
1246 char buffer[len]; | |
1247 struct in_addr temp_addr; | |
1248 | |
1249 mode_app(); | |
1250 // send release packet | |
1251 if (state->status == STATE_BOUND || state->status == STATE_RENEWING || state->status == STATE_REBINDING) { | |
1252 temp_addr.s_addr = htonl(server); | |
1253 strncpy(buffer, inet_ntoa(temp_addr), sizeof(buffer)); | |
1254 buffer[len - 1] = '\0'; | |
1255 temp_addr.s_addr = state->ipaddr.s_addr; | |
1256 infomsg( infomode, "Unicasting a release of %s to %s", inet_ntoa(temp_addr), buffer); | |
1257 dhcpc_sendmsg(DHCPRELEASE); | |
1258 run_script(NULL, "deconfig"); | |
1259 } | |
1260 infomsg(infomode, "Entering released state"); | |
1261 close(state->sockfd); | |
1262 state->sockfd = -1; | |
1263 state->mode = MODE_OFF; | |
1264 state->status = STATE_RELEASED; | |
1265 } | |
1266 | |
1267 static void free_option_stores(void) | |
1268 { | |
1269 int count, size = ARRAY_LEN(options_list); | |
1270 for (count = 0; count < size; count++) | |
1271 if (options_list[count].val) free(options_list[count].val); | |
1272 if(flag_chk(FLAG_x)){ | |
1273 for (count = 0; count < size; count++) | |
1274 if (msgopt_list[count].val) free(msgopt_list[count].val); | |
1275 free(msgopt_list); | |
1276 } | |
1277 } | |
1278 | |
1279 void dhcp_main(void) | |
1280 { | |
1281 struct timeval tv; | |
1282 int retval, bufflen = 0; | |
1283 dhcpc_result_t result; | |
1284 uint8_t packets = 0, retries = 0; | |
1285 uint32_t timeout = 0, waited = 0; | |
1286 fd_set rfds; | |
1287 | |
1288 xid = 0; | |
1289 setlinebuf(stdout); | |
1290 dbg = dummy; | |
1291 if (flag_chk(FLAG_v)) dbg = xprintf; | |
1292 if (flag_chk(FLAG_p)) write_pid(TT.pidfile); | |
1293 retries = flag_get(FLAG_t, TT.retries, 3); | |
1294 if (flag_chk(FLAG_S)) { | |
1295 openlog("UDHCPC :", LOG_PID, LOG_DAEMON); | |
1296 infomode |= LOG_SYSTEM; | |
1297 } | |
1298 infomsg(infomode, "dhcp started"); | |
1299 if (flag_chk(FLAG_O)) { | |
1300 while (TT.req_opt) { | |
1301 raw_opt[raw_optcount] = (uint8_t) strtoopt(TT.req_opt->arg, 1); | |
1302 raw_optcount++; | |
1303 TT.req_opt = TT.req_opt->next; | |
1304 } | |
1305 } | |
1306 if (flag_chk(FLAG_x)) { | |
1307 while (TT.pkt_opt) { | |
1308 (void) strtoopt(TT.pkt_opt->arg, 0); | |
1309 TT.pkt_opt = TT.pkt_opt->next; | |
1310 } | |
1311 } | |
1312 memset(&result, 0, sizeof(dhcpc_result_t)); | |
1313 state = (dhcpc_state_t*) xmalloc(sizeof(dhcpc_state_t)); | |
1314 memset(state, 0, sizeof(dhcpc_state_t)); | |
1315 state->iface = flag_get(FLAG_i, TT.iface, "eth0"); | |
1316 | |
1317 if (get_interface(state->iface, &state->ifindex, NULL, state->macaddr)) | |
1318 perror_exit("Failed to get interface %s", state->iface); | |
1319 | |
1320 run_script(NULL, "deconfig"); | |
1321 setup_signal(); | |
1322 state->status = STATE_INIT; | |
1323 mode_raw(); | |
1324 fcntl(state->sockfd, F_SETFD, FD_CLOEXEC); | |
1325 | |
1326 for (;;) { | |
1327 FD_ZERO(&rfds); | |
1328 if (state->sockfd >= 0) FD_SET(state->sockfd, &rfds); | |
1329 FD_SET(sigfd.rd, &rfds); | |
1330 tv.tv_sec = timeout - waited; | |
1331 tv.tv_usec = 0; | |
1332 retval = 0; | |
1333 | |
1334 int maxfd = (sigfd.rd > state->sockfd)? sigfd.rd : state->sockfd; | |
1335 dbg("select wait ....\n"); | |
1336 uint32_t timestmp = time(NULL); | |
1337 if((retval = select(maxfd + 1, &rfds, NULL, NULL, &tv)) < 0) { | |
1338 if (errno == EINTR) { | |
1339 waited += (unsigned) time(NULL) - timestmp; | |
1340 continue; | |
1341 } | |
1342 perror_exit("Error in select"); | |
1343 } | |
1344 if (!retval) { // Timed out | |
1345 if (get_interface(state->iface, &state->ifindex, NULL, state->macaddr)) | |
1346 error_exit("Interface lost %s\n", state->iface); | |
1347 | |
1348 switch (state->status) { | |
1349 case STATE_INIT: | |
1350 if (packets < retries) { | |
1351 if (!packets) xid = getxid(); | |
1352 run_script(NULL, "deconfig"); | |
1353 infomsg(infomode, "Sending discover..."); | |
1354 dhcpc_sendmsg(DHCPDISCOVER); | |
1355 server = 0; | |
1356 timeout = flag_get(FLAG_T, TT.timeout, 3); | |
1357 waited = 0; | |
1358 packets++; | |
1359 continue; | |
1360 } | |
1361 lease_fail: | |
1362 run_script(NULL,"leasefail"); | |
1363 if (flag_chk(FLAG_n)) { | |
1364 infomsg(infomode, "Lease failed. Exiting"); | |
1365 goto ret_with_sockfd; | |
1366 } | |
1367 if (flag_chk(FLAG_b)) { | |
1368 infomsg(infomode, "Lease failed. Going Daemon mode"); | |
1369 dhcp_daemon(); | |
1370 if (flag_chk(FLAG_p)) write_pid(TT.pidfile); | |
1371 toys.optflags &= ~FLAG_b; | |
1372 toys.optflags |= FLAG_f; | |
1373 } | |
1374 timeout = flag_get(FLAG_A, TT.tryagain, 20); | |
1375 waited = 0; | |
1376 packets = 0; | |
1377 continue; | |
1378 case STATE_REQUESTING: | |
1379 if (packets < retries) { | |
1380 memcpy(&state->ipaddr.s_addr,&state->pdhcp.yiaddr, 4); | |
1381 dhcpc_sendmsg(DHCPREQUEST); | |
1382 infomsg(infomode, "Sending select for %d.%d.%d.%d...", | |
1383 (result.ipaddr.s_addr >> 24) & 0xff, (result.ipaddr.s_addr >> 16) & 0xff, (result.ipaddr.s_addr >> 8) & 0xff, (result.ipaddr.s_addr) & 0xff); | |
1384 timeout = flag_get(FLAG_T, TT.timeout, 3); | |
1385 waited = 0; | |
1386 packets++; | |
1387 continue; | |
1388 } | |
1389 mode_raw(); | |
1390 state->status = STATE_INIT; | |
1391 goto lease_fail; | |
1392 case STATE_BOUND: | |
1393 state->status = STATE_RENEWING; | |
1394 dbg("Entering renew state\n"); | |
1395 // FALLTHROUGH | |
1396 case STATE_RENEW_REQUESTED: // FALLTHROUGH | |
1397 case STATE_RENEWING: | |
1398 renew_requested: | |
1399 if (timeout > 60) { | |
1400 dhcpc_sendmsg(DHCPREQUEST); | |
1401 timeout >>= 1; | |
1402 waited = 0; | |
1403 continue; | |
1404 } | |
1405 dbg("Entering rebinding state\n"); | |
1406 state->status = STATE_REBINDING; | |
1407 // FALLTHROUGH | |
1408 case STATE_REBINDING: | |
1409 mode_raw(); | |
1410 if (timeout > 0) { | |
1411 dhcpc_sendmsg(DHCPREQUEST); | |
1412 timeout >>= 1; | |
1413 waited = 0; | |
1414 continue; | |
1415 } | |
1416 infomsg(infomode, "Lease lost, entering INIT state"); | |
1417 run_script(NULL, "deconfig"); | |
1418 state->status = STATE_INIT; | |
1419 timeout = 0; | |
1420 waited = 0; | |
1421 packets = 0; | |
1422 continue; | |
1423 default: break; | |
1424 } | |
1425 timeout = INT_MAX; | |
1426 waited = 0; | |
1427 continue; | |
1428 } | |
1429 if (FD_ISSET(sigfd.rd, &rfds)) { // Some Activity on RDFDs : is signal | |
1430 unsigned char sig; | |
1431 if (read(sigfd.rd, &sig, 1) != 1) { | |
1432 dbg("signal read failed.\n"); | |
1433 continue; | |
1434 } | |
1435 switch (sig) { | |
1436 case SIGUSR1: | |
1437 infomsg(infomode, "Received SIGUSR1"); | |
1438 renew(); | |
1439 packets = 0; | |
1440 waited = 0; | |
1441 if (state->status == STATE_RENEW_REQUESTED) goto renew_requested; | |
1442 if (state->status == STATE_INIT) timeout = 0; | |
1443 continue; | |
1444 case SIGUSR2: | |
1445 infomsg(infomode, "Received SIGUSR2"); | |
1446 release(); | |
1447 timeout = INT_MAX; | |
1448 waited = 0; | |
1449 packets = 0; | |
1450 continue; | |
1451 case SIGTERM: | |
1452 infomsg(infomode, "Received SIGTERM"); | |
1453 if (flag_chk(FLAG_R)) release(); | |
1454 goto ret_with_sockfd; | |
1455 default: break; | |
1456 } | |
1457 } | |
1458 if (FD_ISSET(state->sockfd, &rfds)) { // Some Activity on RDFDs : is socket | |
1459 dbg("main sock read\n"); | |
1460 uint8_t msgType; | |
1461 if (state->mode == MODE_RAW) bufflen = read_raw(); | |
1462 if (state->mode == MODE_APP) bufflen = read_app(); | |
1463 if (bufflen < 0) { | |
1464 if (state->mode == MODE_RAW) mode_raw(); | |
1465 if (state->mode == MODE_APP) mode_app(); | |
1466 continue; | |
1467 } | |
1468 waited += time(NULL) - timestmp; | |
1469 memset(&result, 0, sizeof(dhcpc_result_t)); | |
1470 msgType = dhcpc_parsemsg(&result); | |
1471 if (msgType != DHCPNAK && result.ipaddr.s_addr == 0 ) continue; // no ip for me ignore | |
1472 if (!msgType || !get_option_serverid(state->pdhcp.options, &result)) continue; //no server id ignore | |
1473 if (msgType == DHCPOFFER && server == 0) server = result.serverid.s_addr; // select the server | |
1474 if (result.serverid.s_addr != server) continue; // not from the server we requested ignore | |
1475 dhcpc_parseoptions(&result, state->pdhcp.options); | |
1476 get_option_lease(state->pdhcp.options, &result); | |
1477 | |
1478 switch (state->status) { | |
1479 case STATE_INIT: | |
1480 if (msgType == DHCPOFFER) { | |
1481 state->status = STATE_REQUESTING; | |
1482 mode_raw(); | |
1483 timeout = 0; | |
1484 waited = 0; | |
1485 packets = 0; | |
1486 } | |
1487 continue; | |
1488 case STATE_REQUESTING: // FALLTHROUGH | |
1489 case STATE_RENEWING: // FALLTHROUGH | |
1490 case STATE_RENEW_REQUESTED: // FALLTHROUGH | |
1491 case STATE_REBINDING: | |
1492 if (msgType == DHCPACK) { | |
1493 timeout = result.lease_time / 2; | |
1494 run_script(&result, state->status == STATE_REQUESTING ? "bound" : "renew"); | |
1495 state->status = STATE_BOUND; | |
1496 infomsg(infomode, "Lease of %d.%d.%d.%d obtained, lease time %d from server %d.%d.%d.%d", | |
1497 (result.ipaddr.s_addr >> 24) & 0xff, (result.ipaddr.s_addr >> 16) & 0xff, (result.ipaddr.s_addr >> 8) & 0xff, (result.ipaddr.s_addr) & 0xff, | |
1498 result.lease_time, | |
1499 (result.serverid.s_addr >> 24) & 0xff, (result.serverid.s_addr >> 16) & 0xff, (result.serverid.s_addr >> 8) & 0xff, (result.serverid.s_addr) & 0xff); | |
1500 if (flag_chk(FLAG_q)) { | |
1501 if (flag_chk(FLAG_R)) release(); | |
1502 goto ret_with_sockfd; | |
1503 } | |
1504 toys.optflags &= ~FLAG_n; | |
1505 if (!flag_chk(FLAG_f)) { | |
1506 dhcp_daemon(); | |
1507 toys.optflags |= FLAG_f; | |
1508 if (flag_chk(FLAG_p)) write_pid(TT.pidfile); | |
1509 } | |
1510 waited = 0; | |
1511 continue; | |
1512 } else if (msgType == DHCPNAK) { | |
1513 dbg("NACK received.\n"); | |
1514 run_script(&result, "nak"); | |
1515 if (state->status != STATE_REQUESTING) run_script(NULL, "deconfig"); | |
1516 mode_raw(); | |
1517 sleep(3); | |
1518 state->status = STATE_INIT; | |
1519 state->ipaddr.s_addr = 0; | |
1520 server = 0; | |
1521 timeout = 0; | |
1522 packets = 0; | |
1523 waited = 0; | |
1524 } | |
1525 continue; | |
1526 default: break; | |
1527 } | |
1528 } | |
1529 } | |
1530 ret_with_sockfd: | |
1531 if (CFG_TOYBOX_FREE) { | |
1532 free_option_stores(); | |
1533 if (state->sockfd > 0) close(state->sockfd); | |
1534 free(state); | |
1535 } | |
1536 } |