1 /* dhcpd.c - DHCP server for dynamic network configuration.
2  *
3  * Copyright 2013 Madhur Verma <mad.flexi@gmail.com>
4  * Copyright 2013 Kyungwan Han <asura321@gamil.com>
5  *
6  * No Standard
7 USE_DHCPD(NEWTOY(dhcpd, ">1P#<0>65535=67fS", TOYFLAG_SBIN|TOYFLAG_ROOTONLY))
8 
9 config DHCPD
10   bool "dhcpd"
11   default n
12   help
13    usage: dhcpd [-fS] [-P N] [CONFFILE]
14 
15     -f    Run in foreground
16     -S    Log to syslog too
17     -P N  Use port N (default 67)
18 
19 config DEBUG_DHCP
20   bool "debugging messeges ON/OFF"
21   default n
22   depends on DHCPD
23 */
24 
25 #define FOR_dhcpd
26 
27 #include "toys.h"
28 #include <linux/sockios.h>
29 #include <linux/if_ether.h>
30 
31 // Todo: headers not in posix
32 #include <netinet/ip.h>
33 #include <netinet/udp.h>
34 #include <netpacket/packet.h>
35 
36 #if CFG_DEBUG_DHCP==1
37 # define dbg(fmt, arg...)   printf(fmt, ##arg)
38 #else
39 # define dbg(fmt, arg...)
40 #endif
41 
42 #define flag_get(f,v,d)     ((toys.optflags & (f)) ? (v) : (d))
43 #define flag_chk(f)         ((toys.optflags & (f)) ? 1 : 0)
44 
45 #define LOG_SILENT          0x0
46 #define LOG_CONSOLE         0x1
47 #define LOG_SYSTEM          0x2
48 
49 #define DHCP_MAGIC          0x63825363
50 
51 #define DHCPDISCOVER        1
52 #define DHCPOFFER           2
53 #define DHCPREQUEST         3
54 #define DHCPDECLINE         4
55 #define DHCPACK             5
56 #define DHCPNAK             6
57 #define DHCPRELEASE         7
58 #define DHCPINFORM          8
59 
60 #define DHCP_NUM8           (1<<8)
61 #define DHCP_NUM16          (1<<9)
62 #define DHCP_NUM32          DHCP_NUM16 | DHCP_NUM8
63 #define DHCP_STRING         (1<<10)
64 #define DHCP_STRLST         (1<<11)
65 #define DHCP_IP             (1<<12)
66 #define DHCP_IPLIST         (1<<13)
67 #define DHCP_IPPLST         (1<<14)
68 #define DHCP_STCRTS         (1<<15)
69 
70 // DHCP option codes (partial list). See RFC 2132 and
71 #define DHCP_OPT_PADDING                          0x00
72 #define DHCP_OPT_HOST_NAME          DHCP_STRING | 0x0c // either client informs server or server gives name to client
73 #define DHCP_OPT_REQUESTED_IP       DHCP_IP     | 0x32 // sent by client if specific IP is wanted
74 #define DHCP_OPT_LEASE_TIME         DHCP_NUM32  | 0x33
75 #define DHCP_OPT_OPTION_OVERLOAD                  0x34
76 #define DHCP_OPT_MESSAGE_TYPE       DHCP_NUM8   | 0x35
77 #define DHCP_OPT_SERVER_ID          DHCP_IP     | 0x36 // by default server's IP
78 #define DHCP_OPT_PARAM_REQ          DHCP_STRING | 0x37 // list of options client wants
79 #define DHCP_OPT_END                              0xff
80 
81 GLOBALS(
82     long port;
83 );
84 
85 typedef struct __attribute__((packed)) dhcp_msg_s {
86   uint8_t op;
87   uint8_t htype;
88   uint8_t hlen;
89   uint8_t hops;
90   uint32_t xid;
91   uint16_t secs;
92   uint16_t flags;
93   uint32_t ciaddr;
94   uint32_t yiaddr;
95   uint32_t nsiaddr;
96   uint32_t ngiaddr;
97   uint8_t chaddr[16];
98   uint8_t sname[64];
99   uint8_t file[128];
100   uint32_t cookie;
101   uint8_t options[308];
102 } dhcp_msg_t;
103 
104 typedef struct __attribute__((packed)) dhcp_raw_s {
105   struct iphdr iph;
106   struct udphdr udph;
107   dhcp_msg_t dhcp;
108 } dhcp_raw_t;
109 
110 typedef struct static_lease_s {
111   struct static_lease_s *next;
112   uint32_t nip;
113   int mac[6];
114 } static_lease;
115 
116 typedef struct {
117   uint32_t expires;
118   uint32_t lease_nip;
119   uint8_t lease_mac[6];
120   char hostname[20];
121   uint8_t pad[2];
122 } dyn_lease;
123 
124 typedef struct option_val_s {
125   char *key;
126   uint16_t code;
127   void *val;
128   size_t len;
129 } option_val_t;
130 
131 typedef struct __attribute__((__may_alias__)) server_config_s {
132   char *interface;                // interface to use
133   int ifindex;
134   uint32_t server_nip;
135   uint32_t port;
136   uint8_t server_mac[6];          // our MAC address (used only for ARP probing)
137   void *options[256];             // list of DHCP options loaded from the config file
138   /* start,end are in host order: we need to compare start <= ip <= end*/
139   uint32_t start_ip;              // start address of leases, in host order
140   uint32_t end_ip;                // end of leases, in host order
141   uint32_t max_lease_sec;         // maximum lease time (host order)
142   uint32_t min_lease_sec;         // minimum lease time a client can request
143   uint32_t max_leases;            // maximum number of leases (including reserved addresses)
144   uint32_t auto_time;             // how long should dhcpd wait before writing a config file.
145                                   // if this is zero, it will only write one on SIGUSR1
146   uint32_t decline_time;          // how long an address is reserved if a client returns a
147                                   // decline message
148   uint32_t conflict_time;         // how long an arp conflict offender is leased for
149   uint32_t offer_time;            // how long an offered address is reserved
150   uint32_t siaddr_nip;            // "next server" bootp option
151   char *lease_file;
152   char *pidfile;
153   char *notify_file;              // what to run whenever leases are written
154   char *sname;                    // bootp server name
155   char *boot_file;                // bootp boot file option
156   struct static_lease *static_leases; // List of ip/mac pairs to assign static leases
157 } server_config_t;
158 
159 typedef struct __attribute__((__may_alias__)) server_state_s {
160   uint8_t rqcode;
161   int listensock;
162   dhcp_msg_t rcvd_pkt;
163   uint8_t* rqopt;
164   dhcp_msg_t send_pkt;
165   static_lease *sleases;
166   struct arg_list *dleases;
167 } server_state_t;
168 
169 struct config_keyword {
170   char *keyword;
171   int (*handler)(const char *str, void *var);
172   void *var;
173   char *def;
174 };
175 
176 static option_val_t options_list[] = {
177     {"lease"          , DHCP_NUM32  | 0x33, NULL, 0},
178     {"subnet"         , DHCP_IP     | 0x01, NULL, 0},
179     {"broadcast"      , DHCP_IP     | 0x1c, NULL, 0},
180     {"router"         , DHCP_IP     | 0x03, NULL, 0},
181     {"ipttl"          , DHCP_NUM8   | 0x17, NULL, 0},
182     {"mtu"            , DHCP_NUM16  | 0x1a, NULL, 0},
183     {"hostname"       , DHCP_STRING | 0x0c, NULL, 0},
184     {"domain"         , DHCP_STRING | 0x0f, NULL, 0},
185     {"search"         , DHCP_STRLST | 0x77, NULL, 0},
186     {"nisdomain"      , DHCP_STRING | 0x28, NULL, 0},
187     {"timezone"       , DHCP_NUM32  | 0x02, NULL, 0},
188     {"tftp"           , DHCP_STRING | 0x42, NULL, 0},
189     {"bootfile"       , DHCP_STRING | 0x43, NULL, 0},
190     {"bootsize"       , DHCP_NUM16  | 0x0d, NULL, 0},
191     {"rootpath"       , DHCP_STRING | 0x11, NULL, 0},
192     {"wpad"           , DHCP_STRING | 0xfc, NULL, 0},
193     {"serverid"       , DHCP_IP     | 0x36, NULL, 0},
194     {"message"        , DHCP_STRING | 0x38, NULL, 0},
195     {"vlanid"         , DHCP_NUM32  | 0x84, NULL, 0},
196     {"vlanpriority"   , DHCP_NUM32  | 0x85, NULL, 0},
197     {"dns"            , DHCP_IPLIST | 0x06, NULL, 0},
198     {"wins"           , DHCP_IPLIST | 0x2c, NULL, 0},
199     {"nissrv"         , DHCP_IPLIST | 0x29, NULL, 0},
200     {"ntpsrv"         , DHCP_IPLIST | 0x2a, NULL, 0},
201     {"lprsrv"         , DHCP_IPLIST | 0x09, NULL, 0},
202     {"swapsrv"        , DHCP_IP     | 0x10, NULL, 0},
203     {"routes"         , DHCP_STCRTS | 0x21, NULL, 0},
204     {"staticroutes"   , DHCP_STCRTS | 0x79, NULL, 0},
205     {"msstaticroutes" , DHCP_STCRTS | 0xf9, NULL, 0},
206 };
207 
208 struct fd_pair { int rd; int wr; };
209 static server_config_t gconfig;
210 static server_state_t gstate;
211 static uint8_t infomode;
212 static struct fd_pair sigfd;
213 static int constone = 1;
214 
215 // calculate options size.
dhcp_opt_size(uint8_t * optionptr)216 static int dhcp_opt_size(uint8_t *optionptr)
217 {
218   int i = 0;
219   for(;optionptr[i] != 0xff; i++) if(optionptr[i] != 0x00) i += optionptr[i + 1] + 2 -1;
220   return i;
221 }
222 
223 // calculates checksum for dhcp messeges.
dhcp_checksum(void * addr,int count)224 static uint16_t dhcp_checksum(void *addr, int count)
225 {
226   int32_t sum = 0;
227   uint16_t tmp = 0, *source = (uint16_t *)addr;
228 
229   while (count > 1)  {
230     sum += *source++;
231     count -= 2;
232   }
233   if (count > 0) {
234     *(uint8_t*)&tmp = *(uint8_t*)source;
235     sum += tmp;
236   }
237   while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16);
238   return ~sum;
239 }
240 
241 // gets information of INTERFACE and updates IFINDEX, MAC and IP
get_interface(const char * interface,int * ifindex,uint32_t * oip,uint8_t * mac)242 static int get_interface(const char *interface, int *ifindex, uint32_t *oip, uint8_t *mac)
243 {
244   struct ifreq req;
245   struct sockaddr_in *ip;
246   int fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
247 
248   req.ifr_addr.sa_family = AF_INET;
249   xstrncpy(req.ifr_name, interface, IFNAMSIZ);
250 
251   xioctl(fd, SIOCGIFFLAGS, &req);
252 
253   if (!(req.ifr_flags & IFF_UP)) return -1;
254   if (oip) {
255     xioctl(fd, SIOCGIFADDR, &req);
256     ip = (struct sockaddr_in*) &req.ifr_addr;
257     dbg("IP %s\n", inet_ntoa(ip->sin_addr));
258     *oip = ntohl(ip->sin_addr.s_addr);
259   }
260   if (ifindex) {
261     xioctl(fd, SIOCGIFINDEX, &req);
262     dbg("Adapter index %d\n", req.ifr_ifindex);
263     *ifindex = req.ifr_ifindex;
264   }
265   if (mac) {
266     xioctl(fd, SIOCGIFHWADDR, &req);
267     memcpy(mac, req.ifr_hwaddr.sa_data, 6);
268     dbg("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
269   }
270   close(fd);
271   return 0;
272 }
273 
274 /*
275  *logs messeges to syslog or console
276  *opening the log is still left with applet.
277  *FIXME: move to more relevent lib. probably libc.c
278  */
infomsg(uint8_t infomode,char * s,...)279 static void infomsg(uint8_t infomode, char *s, ...)
280 {
281   int used;
282   char *msg;
283   va_list p, t;
284 
285   if (infomode == LOG_SILENT) return;
286   va_start(p, s);
287   va_copy(t, p);
288   used = vsnprintf(NULL, 0, s, t);
289   used++;
290   va_end(t);
291 
292   msg = xmalloc(used);
293   vsnprintf(msg, used, s, p);
294   va_end(p);
295 
296   if (infomode & LOG_SYSTEM) syslog(LOG_INFO, "%s", msg);
297   if (infomode & LOG_CONSOLE) printf("%s\n", msg);
298   free(msg);
299 }
300 
301 /*
302  * Writes self PID in file PATH
303  * FIXME: libc implementation only writes in /var/run
304  * this is more generic as some implemenation may provide
305  * arguments to write in specific file. as dhcpd does.
306  */
write_pid(char * path)307 static void write_pid(char *path)
308 {
309   int pidfile = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0666);
310   if (pidfile > 0) {
311     char pidbuf[12];
312 
313     sprintf(pidbuf, "%u", (unsigned)getpid());
314     write(pidfile, pidbuf, strlen(pidbuf));
315     close(pidfile);
316   }
317 }
318 
319 // Generic signal handler real handling is done in main funcrion.
signal_handler(int sig)320 static void signal_handler(int sig)
321 {
322   unsigned char ch = sig;
323   if (write(sigfd.wr, &ch, 1) != 1) dbg("can't send signal\n");
324 }
325 
326 // signal setup for SIGUSR1 SIGTERM
setup_signal()327 static int setup_signal()
328 {
329   if (pipe((int *)&sigfd) < 0) {
330     dbg("signal pipe failed\n");
331     return -1;
332   }
333   fcntl(sigfd.wr , F_SETFD, FD_CLOEXEC);
334   fcntl(sigfd.rd , F_SETFD, FD_CLOEXEC);
335   int flags = fcntl(sigfd.wr, F_GETFL);
336   fcntl(sigfd.wr, F_SETFL, flags | O_NONBLOCK);
337   signal(SIGUSR1, signal_handler);
338   signal(SIGTERM, signal_handler);
339   return 0;
340 }
341 
342 // String STR to UINT32 conversion strored in VAR
strtou32(const char * str,void * var)343 static int strtou32(const char *str, void *var)
344 {
345   char *endptr = NULL;
346   int base = 10;
347   errno=0;
348   *((uint32_t*)(var)) = 0;
349   if (str[0]=='0' && (str[1]=='x' || str[1]=='X')) {
350     base = 16;
351     str+=2;
352   }
353   long ret_val = strtol(str, &endptr, base);
354   if (errno) infomsg(infomode, "config : Invalid num %s",str);
355   else if (endptr && (*endptr!='\0'||endptr == str))
356       infomsg(infomode, "config : Not a valid num %s",str);
357   else *((uint32_t*)(var)) = (uint32_t)ret_val;
358   return 0;
359 }
360 
361 // copy string STR in variable VAR
strinvar(const char * str,void * var)362 static int strinvar(const char *str, void *var)
363 {
364   char **dest = var;
365   if (*dest) free(*dest);
366   *dest = strdup(str);
367   return 0;
368 }
369 
370 // IP String STR to binary data.
striptovar(const char * str,void * var)371 static int striptovar(const char *str, void *var)
372 {
373   in_addr_t addr;
374   *((uint32_t*)(var)) = 0;
375   if(!str) {
376     error_msg("config : NULL address string \n");
377     return -1;
378   }
379   if((addr = inet_addr(str)) == -1) {
380     error_msg("config : wrong address %s \n",str );
381     return -1;
382   }
383   *((uint32_t*)(var)) = (uint32_t)addr;
384   return 0;
385 }
386 
387 // String to dhcp option conversion
strtoopt(const char * str,void * var)388 static int strtoopt(const char *str, void *var)
389 {
390   char *option, *valstr, *grp, *tp;
391   uint32_t optcode = 0, inf = infomode, convtmp, mask, nip, router;
392   uint16_t flag = 0;
393   int count, size = ARRAY_LEN(options_list);
394 
395   if (!*str) return 0;
396   if (!(option = strtok((char*)str, " \t="))) return -1;
397 
398   infomode = LOG_SILENT;
399   strtou32(option, (uint32_t*)&optcode);
400   infomode = inf;
401 
402   if (optcode > 0 && optcode < 256) { // raw option
403     for (count = 0; count < size; count++) {
404       if ((options_list[count].code & 0X00FF) == optcode) {
405         flag = (options_list[count].code & 0XFF00);
406         break;
407       }
408     }
409   } else { //string option
410     for (count = 0; count < size; count++) {
411       if (!strncmp(options_list[count].key, option, strlen(options_list[count].key))) {
412         flag = (options_list[count].code & 0XFF00);
413         optcode = (options_list[count].code & 0X00FF);
414         break;
415       }
416     }
417   }
418   if (count == size) {
419     infomsg(inf, "config : Obsolete OR Unknown Option : %s", option);
420     return -1;
421   }
422 
423   if (!flag || !optcode) return -1;
424 
425   if (!(valstr = strtok(NULL, " \t"))) {
426     dbg("config : option %s has no value defined.\n", option);
427     return -1;
428   }
429   dbg(" value : %-20s : ", valstr);
430   switch (flag) {
431   case DHCP_NUM32:
432     options_list[count].len = sizeof(uint32_t);
433     options_list[count].val = xmalloc(sizeof(uint32_t));
434     strtou32(valstr, &convtmp);
435     memcpy(options_list[count].val, &convtmp, sizeof(uint32_t));
436     break;
437   case DHCP_NUM16:
438     options_list[count].len = sizeof(uint16_t);
439     options_list[count].val = xmalloc(sizeof(uint16_t));
440     strtou32(valstr, &convtmp);
441     memcpy(options_list[count].val, &convtmp, sizeof(uint16_t));
442     break;
443   case DHCP_NUM8:
444     options_list[count].len = sizeof(uint8_t);
445     options_list[count].val = xmalloc(sizeof(uint8_t));
446     strtou32(valstr, &convtmp);
447     memcpy(options_list[count].val, &convtmp, sizeof(uint8_t));
448     break;
449   case DHCP_IP:
450     options_list[count].len = sizeof(uint32_t);
451     options_list[count].val = xmalloc(sizeof(uint32_t));
452     striptovar(valstr, options_list[count].val);
453     break;
454   case DHCP_STRING:
455     options_list[count].len = strlen(valstr);
456     options_list[count].val = strdup(valstr);
457     break;
458   case DHCP_IPLIST:
459     while(valstr){
460       options_list[count].val = xrealloc(options_list[count].val, options_list[count].len + sizeof(uint32_t));
461       striptovar(valstr, ((uint8_t*)options_list[count].val)+options_list[count].len);
462       options_list[count].len += sizeof(uint32_t);
463       valstr = strtok(NULL," \t");
464     }
465     break;
466   case DHCP_IPPLST:
467     break;
468   case DHCP_STCRTS:
469     /* Option binary format:
470      * mask [one byte, 0..32]
471      * ip [0..4 bytes depending on mask]
472      * router [4 bytes]
473      * may be repeated
474      * staticroutes 10.0.0.0/8 10.127.0.1, 10.11.12.0/24 10.11.12.1
475      */
476     grp = strtok(valstr, ",");;
477     while(grp){
478       while(*grp == ' ' || *grp == '\t') grp++;
479       tp = strchr(grp, '/');
480       if (!tp) error_exit("wrong formated static route option");
481       *tp = '\0';
482       mask = strtol(++tp, &tp, 10);
483       if (striptovar(grp, (uint8_t*)&nip)<0) error_exit("wrong formated static route option");
484       while(*tp == ' ' || *tp == '\t' || *tp == '-') tp++;
485       if (striptovar(tp, (uint8_t*)&router)<0) error_exit("wrong formated static route option");
486       options_list[count].val = xrealloc(options_list[count].val, options_list[count].len + 1 + mask/8 + 4);
487       memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &mask, 1);
488       options_list[count].len += 1;
489       memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &nip, mask/8);
490       options_list[count].len += mask/8;
491       memcpy(((uint8_t*)options_list[count].val)+options_list[count].len, &router, 4);
492       options_list[count].len += 4;
493       tp = NULL;
494       grp = strtok(NULL, ",");
495     }
496     break;
497   }
498   return 0;
499 }
500 
501 // Reads Static leases from STR and updates inner structures.
get_staticlease(const char * str,void * var)502 static int get_staticlease(const char *str, void *var)
503 {
504   struct static_lease_s *sltmp;
505   char *tkmac, *tkip;
506   int count;
507 
508   if (!*str) return 0;
509 
510   if (!(tkmac = strtok((char*)str, " \t"))) {
511     infomsg(infomode, "config : static lease : mac not found");
512     return 0;
513   }
514   if (!(tkip = strtok(NULL, " \t"))) {
515     infomsg(infomode, "config : static lease : no ip bind to mac %s", tkmac);
516     return 0;
517   }
518   sltmp = xzalloc(sizeof(struct static_lease_s));
519   for (count = 0; count < 6; count++, tkmac++) {
520     errno = 0;
521     sltmp->mac[count] = strtol(tkmac, &tkmac, 16);
522     if (sltmp->mac[count]>255 || sltmp->mac[count]<0 || (*tkmac && *tkmac!=':') || errno) {
523       infomsg(infomode, "config : static lease : mac address wrong format");
524       free(sltmp);
525       return 0;
526     }
527   }
528   striptovar(tkip, &sltmp->nip);
529   sltmp->next = gstate.sleases;
530   gstate.sleases = sltmp;
531 
532   return 0;
533 }
534 
535 static struct config_keyword keywords[] = {
536 // keyword          handler           variable address                default
537   {"start"        , striptovar      , (void*)&gconfig.start_ip     , "192.168.0.20"},
538   {"end"          , striptovar      , (void*)&gconfig.end_ip       , "192.168.0.254"},
539   {"interface"    , strinvar        , (void*)&gconfig.interface    , "eth0"},
540   {"port"         , strtou32        , (void*)&gconfig.port         , "67"},
541   {"min_lease"    , strtou32        , (void*)&gconfig.min_lease_sec, "60"},
542   {"max_leases"   , strtou32        , (void*)&gconfig.max_leases   , "235"},
543   {"auto_time"    , strtou32        , (void*)&gconfig.auto_time    , "7200"},
544   {"decline_time" , strtou32        , (void*)&gconfig.decline_time , "3600"},
545   {"conflict_time", strtou32        , (void*)&gconfig.conflict_time, "3600"},
546   {"offer_time"   , strtou32        , (void*)&gconfig.offer_time   , "60"},
547   {"lease_file"   , strinvar        , (void*)&gconfig.lease_file   , "/var/lib/misc/dhcpd.leases"}, //LEASES_FILE
548   {"pidfile"      , strinvar        , (void*)&gconfig.pidfile      , "/var/run/dhcpd.pid"}, //DPID_FILE
549   {"siaddr"       , striptovar      , (void*)&gconfig.siaddr_nip   , "0.0.0.0"},
550   {"option"       , strtoopt        , (void*)&gconfig.options      , ""},
551   {"opt"          , strtoopt        , (void*)&gconfig.options      , ""},
552   {"notify_file"  , strinvar        , (void*)&gconfig.notify_file  , ""},
553   {"sname"        , strinvar        , (void*)&gconfig.sname        , ""},
554   {"boot_file"    , strinvar        , (void*)&gconfig.boot_file    , ""},
555   {"static_lease" , get_staticlease , (void*)&gconfig.static_leases, ""},
556 };
557 
558 // Parses the server config file and updates the global server config accordingly.
parse_server_config(char * config_file,struct config_keyword * confkey)559 static int parse_server_config(char *config_file, struct config_keyword *confkey)
560 {
561   FILE *fs = NULL;
562   char *confline_temp = NULL,*confline = NULL, *tk = NULL, *tokens[2] = {NULL, NULL};
563   int len, linelen, tcount, count, size = ARRAY_LEN(keywords);
564 
565   for (count = 0; count < size; count++)
566     if (confkey[count].handler) confkey[count].handler(confkey[count].def, confkey[count].var);
567 
568   if (!(fs = fopen(config_file, "r"))) perror_msg("%s", config_file);
569   for (len = 0, linelen = 0; fs;) {
570     len = getline(&confline_temp, (size_t*) &linelen, fs);
571     confline = confline_temp;
572     if (len <= 0) break;
573     for (; *confline == ' '; confline++, len--);
574     if ((confline[0] == '#') || (confline[0] == '\n')) goto free_conf_continue;
575     tk = strchr(confline, '#');
576     if (tk) {
577       for (; *(tk-1)==' ' || *(tk-1)=='\t'; tk--);
578       *tk = '\0';
579     }
580     tk = strchr(confline, '\n');
581     if (tk) {
582       for (; *(tk-1)==' ' || *(tk-1)=='\t'; tk--);
583       *tk = '\0';
584     }
585     for (tcount=0, tk=strtok(confline, " \t"); tk && (tcount < 2);
586         tcount++, tk=strtok(NULL,(tcount==1)?"":" \t")) {
587       while ((*tk == '\t') || (*tk == ' ')) tk++;
588       tokens[tcount] = xstrdup(tk);
589     }
590     if (tcount<=1) goto free_tk0_continue;
591     for (count = 0; count < size; count++) {
592       if (!strcmp(confkey[count].keyword,tokens[0])) {
593         dbg("got config : %15s : ", confkey[count].keyword);
594         if (confkey[count].handler(tokens[1], confkey[count].var) == 0)
595           dbg("%s \n", tokens[1]);
596         break;
597       }
598     }
599     if (tokens[1]) { free(tokens[1]); tokens[1] = NULL; }
600 free_tk0_continue:
601     if (tokens[0]) { free(tokens[0]); tokens[0] = NULL; }
602 free_conf_continue:
603     free(confline_temp);
604     confline_temp = NULL;
605   }
606   if (fs) fclose(fs);
607   return 0;
608 }
609 
610 // opens UDP socket for listen
open_listensock(void)611 static int open_listensock(void)
612 {
613   struct sockaddr_in addr;
614   struct ifreq ifr;
615 
616   if (gstate.listensock > 0) close(gstate.listensock);
617 
618   dbg("Opening listen socket on *:%d %s\n", gconfig.port, gconfig.interface);
619   gstate.listensock = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
620   setsockopt(gstate.listensock, SOL_SOCKET, SO_REUSEADDR, &constone, sizeof(constone));
621   if (setsockopt(gstate.listensock, SOL_SOCKET, SO_BROADCAST, &constone, sizeof(constone)) == -1) {
622       dbg("OPEN : brodcast ioctl failed.\n");
623       close(gstate.listensock);
624       return -1;
625   }
626   memset(&ifr, 0, sizeof(ifr));
627   xstrncpy(ifr.ifr_name, gconfig.interface, IFNAMSIZ);
628   setsockopt(gstate.listensock, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr));
629 
630   memset(&addr, 0, sizeof(addr));
631   addr.sin_family = AF_INET;
632   addr.sin_port = (flag_chk(FLAG_P))?htons(TT.port):htons(67); //SERVER_PORT
633   addr.sin_addr.s_addr = INADDR_ANY ;
634 
635   if (bind(gstate.listensock, (struct sockaddr *) &addr, sizeof(addr))) {
636     close(gstate.listensock);
637     perror_exit("bind failed");
638   }
639   dbg("OPEN : success\n");
640   return 0;
641 }
642 
643 // Sends data through raw socket.
send_packet(uint8_t broadcast)644 static int send_packet(uint8_t broadcast)
645 {
646   struct sockaddr_ll dest_sll;
647   dhcp_raw_t packet;
648   unsigned padding;
649   int fd, result = -1;
650   uint8_t bmacaddr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
651 
652   memset(&packet, 0, sizeof(dhcp_raw_t));
653   memcpy(&packet.dhcp, &gstate.send_pkt, sizeof(dhcp_msg_t));
654 
655   if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
656     dbg("SEND : socket failed\n");
657     return -1;
658   }
659   memset(&dest_sll, 0, sizeof(dest_sll));
660   dest_sll.sll_family = AF_PACKET;
661   dest_sll.sll_protocol = htons(ETH_P_IP);
662   dest_sll.sll_ifindex = gconfig.ifindex;
663   dest_sll.sll_halen = 6;
664   memcpy(dest_sll.sll_addr, (broadcast)?bmacaddr:gstate.rcvd_pkt.chaddr , 6);
665 
666   if (bind(fd, (struct sockaddr *) &dest_sll, sizeof(dest_sll)) < 0) {
667     dbg("SEND : bind failed\n");
668     close(fd);
669     return -1;
670   }
671   padding = 308 - 1 - dhcp_opt_size(gstate.send_pkt.options);
672   packet.iph.protocol = IPPROTO_UDP;
673   packet.iph.saddr = gconfig.server_nip;
674   packet.iph.daddr = (broadcast || (gstate.rcvd_pkt.ciaddr == 0))?INADDR_BROADCAST:gstate.rcvd_pkt.ciaddr;
675   packet.udph.source = htons(67);//SERVER_PORT
676   packet.udph.dest = htons(68); //CLIENT_PORT
677   packet.udph.len = htons(sizeof(dhcp_raw_t) - sizeof(struct iphdr) - padding);
678   packet.iph.tot_len = packet.udph.len;
679   packet.udph.check = dhcp_checksum(&packet, sizeof(dhcp_raw_t) - padding);
680   packet.iph.tot_len = htons(sizeof(dhcp_raw_t) - padding);
681   packet.iph.ihl = sizeof(packet.iph) >> 2;
682   packet.iph.version = IPVERSION;
683   packet.iph.ttl = IPDEFTTL;
684   packet.iph.check = dhcp_checksum(&packet.iph, sizeof(packet.iph));
685 
686   result = sendto(fd, &packet, sizeof(dhcp_raw_t) - padding, 0,
687       (struct sockaddr *) &dest_sll, sizeof(dest_sll));
688 
689   dbg("sendto %d\n", result);
690   close(fd);
691   if (result < 0) dbg("PACKET send error\n");
692   return result;
693 }
694 
695 // Reads from UDP socket
read_packet(void)696 static int read_packet(void)
697 {
698   int ret;
699 
700   memset(&gstate.rcvd_pkt, 0, sizeof(dhcp_msg_t));
701   ret = read(gstate.listensock, &gstate.rcvd_pkt, sizeof(dhcp_msg_t));
702   if (ret < 0) {
703     dbg("Packet read error, ignoring. \n");
704     return ret; // returns -1
705   }
706   if (gstate.rcvd_pkt.cookie != htonl(DHCP_MAGIC)) {
707     dbg("Packet with bad magic, ignoring. \n");
708     return -2;
709   }
710   if (gstate.rcvd_pkt.op != 1) { //BOOTPREQUEST
711     dbg("Not a BOOT REQUEST ignoring. \n");
712     return -2;
713   }
714   if (gstate.rcvd_pkt.hlen != 6) {
715     dbg("hlen != 6 ignoring. \n");
716     return -2;
717   }
718   dbg("Received a packet. Size : %d \n", ret);
719   return ret;
720 }
721 
722 // Preapres a dhcp packet with defaults and configs
prepare_send_pkt(void)723 static uint8_t* prepare_send_pkt(void)
724 {
725   memset((void*)&gstate.send_pkt, 0, sizeof(gstate.send_pkt));
726   gstate.send_pkt.op = 2; //BOOTPREPLY
727   gstate.send_pkt.htype = 1;
728   gstate.send_pkt.hlen = 6;
729   gstate.send_pkt.xid = gstate.rcvd_pkt.xid;
730   gstate.send_pkt.cookie = htonl(DHCP_MAGIC);
731   gstate.send_pkt.nsiaddr = gconfig.server_nip;
732   memcpy(gstate.send_pkt.chaddr, gstate.rcvd_pkt.chaddr, 16);
733   gstate.send_pkt.options[0] = DHCP_OPT_END;
734   return gstate.send_pkt.options;
735 }
736 
737 // Sets a option value in dhcp packet's option field
set_optval(uint8_t * optptr,uint16_t opt,void * var,size_t len)738 static uint8_t* set_optval(uint8_t *optptr, uint16_t opt, void *var, size_t len)
739 {
740   while (*optptr != DHCP_OPT_END) optptr++;
741   *optptr++ = (uint8_t)(opt & 0x00FF);
742   *optptr++ = (uint8_t) len;
743   memcpy(optptr, var, len);
744   optptr += len;
745   *optptr = DHCP_OPT_END;
746   return optptr;
747 }
748 
749 // Gets a option value from dhcp packet's option field
get_optval(uint8_t * optptr,uint16_t opt,void * var)750 static uint8_t* get_optval(uint8_t *optptr, uint16_t opt, void *var)
751 {
752   size_t len;
753   uint8_t overloaded = 0;
754 
755   while (1) {
756     while (*optptr == DHCP_OPT_PADDING) optptr++;
757     if ((*optptr & 0x00FF) == DHCP_OPT_END) break;
758     if ((*optptr & 0x00FF) == DHCP_OPT_OPTION_OVERLOAD) {
759       overloaded = optptr[2];
760       optptr += optptr[1] + 2;
761     }
762     len = optptr[1];
763     if (*optptr == (opt & 0x00FF))
764       switch (opt & 0xFF00) {
765         case DHCP_NUM32: // FALLTHROUGH
766         case DHCP_IP:
767           memcpy(var, optptr+2, sizeof(uint32_t));
768           optptr += len + 2;
769           return optptr;
770           break;
771         case DHCP_NUM16:
772           memcpy(var, optptr+2, sizeof(uint16_t));
773           optptr += len + 2;
774           return optptr;
775           break;
776         case DHCP_NUM8:
777           memcpy(var, optptr+2, sizeof(uint8_t));
778           optptr += len + 2;
779           return optptr;
780           break;
781         case DHCP_STRING:
782           var = xstrndup((char*) optptr, len);
783           optptr += len + 2;
784           return optptr;
785           break;
786       }
787     optptr += len + 2;
788   }
789   if ((overloaded == 1) | (overloaded == 3)) get_optval((uint8_t*)&gstate.rcvd_pkt.file, opt, var);
790   if ((overloaded == 2) | (overloaded == 3)) get_optval((uint8_t*)&gstate.rcvd_pkt.sname, opt, var);
791   return optptr;
792 }
793 
794 // Retrives Requested Parameter list from dhcp req packet.
get_reqparam(uint8_t ** list)795 static uint8_t get_reqparam(uint8_t **list)
796 {
797   uint8_t len, *optptr;
798   if(*list) free(*list);
799   for (optptr = gstate.rcvd_pkt.options;
800       *optptr && *optptr!=((DHCP_OPT_PARAM_REQ) & 0x00FF); optptr+=optptr[1]+2);
801   len = *++optptr;
802   *list = xzalloc(len+1);
803   memcpy(*list, ++optptr, len);
804   return len;
805 }
806 
807 // Sets values of req param in dhcp offer packet.
set_reqparam(uint8_t * optptr,uint8_t * list)808 static uint8_t* set_reqparam(uint8_t *optptr, uint8_t *list)
809 {
810   uint8_t reqcode;
811   int count, size = ARRAY_LEN(options_list);
812 
813   while (*list) {
814     reqcode = *list++;
815     for (count = 0; count < size; count++) {
816       if ((options_list[count].code & 0X00FF)==reqcode) {
817         if (!(options_list[count].len) || !(options_list[count].val)) break;
818         for (; *optptr && *optptr!=DHCP_OPT_END; optptr+=optptr[1]+2);
819         *optptr++ = (uint8_t) (options_list[count].code & 0x00FF);
820         *optptr++ = (uint8_t) options_list[count].len;
821         memcpy(optptr, options_list[count].val, options_list[count].len);
822         optptr += options_list[count].len;
823         *optptr = DHCP_OPT_END;
824         break;
825       }
826     }
827   }
828   return optptr;
829 }
830 
run_notify(char ** argv)831 static void run_notify(char **argv)
832 {
833   struct stat sts;
834   volatile int error = 0;
835   pid_t pid;
836 
837   if (stat(argv[0], &sts) == -1 && errno == ENOENT) {
838     infomsg(infomode, "notify file: %s : not exist.", argv[0]);
839     return;
840   }
841   fflush(NULL);
842 
843   pid = vfork();
844   if (pid < 0) {
845     dbg("Fork failed.\n");
846     return;
847   }
848   if (!pid) {
849     execvp(argv[0], argv);
850     error = errno;
851     _exit(111);
852   }
853   if (error) {
854     waitpid(pid, NULL, 0);
855     errno = error;
856   }
857   dbg("script complete.\n");
858 }
859 
write_leasefile(void)860 static int write_leasefile(void)
861 {
862   int fd;
863   uint32_t curr, tmp_time;
864   int64_t timestamp;
865   struct arg_list *listdls = gstate.dleases;
866   dyn_lease *dls;
867 
868   if ((fd = open(gconfig.lease_file, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
869     perror_msg("can't open %s ", gconfig.lease_file);
870     return fd;
871   }
872 
873   curr = timestamp = time(NULL);
874   timestamp = SWAP_BE64(timestamp);
875   writeall(fd, &timestamp, sizeof(timestamp));
876 
877   while (listdls) {
878     dls = (dyn_lease*)listdls->arg;
879     tmp_time = dls->expires;
880     dls->expires -= curr;
881     if ((int32_t) dls->expires < 0) goto skip;
882     dls->expires = htonl(dls->expires);
883     writeall(fd, dls, sizeof(dyn_lease));
884 skip:
885     dls->expires = tmp_time;
886     listdls = listdls->next;
887   }
888   close(fd);
889   if (gconfig.notify_file) {
890     char *argv[3];
891     argv[0] = gconfig.notify_file;
892     argv[1] = gconfig.lease_file;
893     argv[2] = NULL;
894     run_notify(argv);
895   }
896   return 0;
897 }
898 
899 // Update max lease time from options.
set_maxlease(void)900 static void set_maxlease(void)
901 {
902   int count, size = ARRAY_LEN(options_list);
903   for (count = 0; count < size; count++)
904     if (options_list[count].val && options_list[count].code == (DHCP_OPT_LEASE_TIME)) {
905       gconfig.max_lease_sec = *((uint32_t*)options_list[count].val);
906       break;
907     }
908   if (!gconfig.max_lease_sec) gconfig.max_lease_sec = (60*60*24*10);// DEFAULT_LEASE_TIME;
909 }
910 
911 // Returns lease time for client.
get_lease(uint32_t req_exp)912 static uint32_t get_lease(uint32_t req_exp)
913 {
914   uint32_t now = time(NULL);
915   req_exp = req_exp - now;
916   if ((req_exp <= 0) || (req_exp > gconfig.max_lease_sec))
917     return gconfig.max_lease_sec;
918 
919   if (req_exp < gconfig.min_lease_sec)
920     return gconfig.min_lease_sec;
921 
922   return req_exp;
923 }
924 
925 // Verify ip NIP in current leases ( assigned or not)
verifyip_in_lease(uint32_t nip,uint8_t mac[6])926 static int verifyip_in_lease(uint32_t nip, uint8_t mac[6])
927 {
928   static_lease *sls;
929   struct arg_list *listdls;
930 
931   for (listdls = gstate.dleases; listdls; listdls = listdls->next) {
932     if (((dyn_lease*) listdls->arg)->lease_nip == nip) {
933       if (((int32_t)(((dyn_lease*) listdls->arg)->expires) - time(NULL)) < 0)
934         return 0;
935       return -1;
936     }
937     if (!memcmp(((dyn_lease*) listdls->arg)->lease_mac, mac, 6)) return -1;
938   }
939   for (sls = gstate.sleases; sls; sls = sls->next)
940     if (sls->nip == nip) return -2;
941 
942   if ((ntohl(nip) < gconfig.start_ip) || (ntohl(nip) > gconfig.end_ip))
943     return -3;
944 
945   return 0;
946 }
947 
948 // add ip assigned_nip to dynamic lease.
addip_to_lease(uint32_t assigned_nip,uint8_t mac[6],uint32_t * req_exp,char * hostname,uint8_t update)949 static int addip_to_lease(uint32_t assigned_nip, uint8_t mac[6], uint32_t *req_exp, char *hostname, uint8_t update)
950 {
951   dyn_lease *dls;
952   struct arg_list *listdls = gstate.dleases;
953   uint32_t now = time(NULL);
954 
955   while (listdls) {
956     if (!memcmp(((dyn_lease*) listdls->arg)->lease_mac, mac, 6)) {
957       if (update) *req_exp = get_lease(*req_exp + ((dyn_lease*) listdls->arg)->expires);
958       ((dyn_lease*) listdls->arg)->expires = *req_exp + now;
959       return 0;
960     }
961     listdls = listdls->next;
962   }
963 
964   dls = xzalloc(sizeof(dyn_lease));
965   memcpy(dls->lease_mac, mac, 6);
966   dls->lease_nip = assigned_nip;
967   if (hostname) memcpy(dls->hostname, hostname, 20);
968 
969   if (update) *req_exp = get_lease(*req_exp + now);
970   dls->expires = *req_exp + now;
971 
972   listdls = xzalloc(sizeof(struct arg_list));
973   listdls->next = gstate.dleases;
974   listdls->arg = (char*)dls;
975   gstate.dleases = listdls;
976 
977   return 0;
978 }
979 
980 // delete ip assigned_nip from dynamic lease.
delip_from_lease(uint32_t assigned_nip,uint8_t mac[6],uint32_t del_time)981 static int delip_from_lease(uint32_t assigned_nip, uint8_t mac[6], uint32_t del_time)
982 {
983   struct arg_list *listdls = gstate.dleases;
984 
985   while (listdls) {
986     if (!memcmp(((dyn_lease*) listdls->arg)->lease_mac, mac, 6)) {
987       ((dyn_lease*) listdls->arg)->expires = del_time + time(NULL);
988       return 0;
989     }
990     listdls = listdls->next;
991   }
992   return -1;
993 }
994 
995 // returns a IP from static, dynamic leases or free ip pool, 0 otherwise.
getip_from_pool(uint32_t req_nip,uint8_t mac[6],uint32_t * req_exp,char * hostname)996 static uint32_t getip_from_pool(uint32_t req_nip, uint8_t mac[6], uint32_t *req_exp, char *hostname)
997 {
998   uint32_t nip = 0;
999   static_lease *sls = gstate.sleases;
1000   struct arg_list *listdls = gstate.dleases, *tmp = NULL;
1001 
1002   if (req_nip && (!verifyip_in_lease(req_nip, mac))) nip = req_nip;
1003 
1004   if (!nip) {
1005     while (listdls) {
1006       if (!memcmp(((dyn_lease*)listdls->arg)->lease_mac, mac, 6)) {
1007         nip = ((dyn_lease*)listdls->arg)->lease_nip;
1008         if (tmp) tmp->next = listdls->next;
1009         else gstate.dleases = listdls->next;
1010         free(listdls->arg);
1011         free(listdls);
1012         if (verifyip_in_lease(nip, mac) < 0) nip = 0;
1013         break;
1014       }
1015       tmp = listdls;
1016       listdls = listdls->next;
1017     }
1018   }
1019   if (!nip) {
1020     while (sls) {
1021       if (memcmp(sls->mac, mac, 6) == 0) {
1022         nip = sls->nip;
1023         break;
1024       }
1025       sls = sls->next;
1026     }
1027   }
1028   if (!nip) {
1029     for (nip = htonl(gconfig.start_ip); ntohl(nip) <= gconfig.end_ip; ) {
1030       if (!verifyip_in_lease(nip, mac)) break;
1031       nip = ntohl(nip);
1032       nip = htonl(++nip);
1033     }
1034     if (ntohl(nip) > gconfig.end_ip) {
1035       nip = 0;
1036       infomsg(infomode, "can't find free IP in IP Pool.");
1037     }
1038   }
1039   if (nip) addip_to_lease(nip, mac, req_exp, hostname, 1);
1040   return nip;
1041 }
1042 
read_leasefile(void)1043 static int read_leasefile(void)
1044 {
1045   uint32_t passed, ip;
1046   int32_t tmp_time;
1047   int64_t timestamp;
1048   dyn_lease *dls;
1049   int ret = -1, fd = open(gconfig.lease_file, O_RDONLY);
1050 
1051   if (fd < 0) return fd;
1052   dls = xzalloc(sizeof(dyn_lease));
1053 
1054   if (read(fd, &timestamp, sizeof(timestamp)) != sizeof(timestamp)) goto error_exit;
1055 
1056   timestamp = SWAP_BE64(timestamp);
1057   passed = time(NULL) - timestamp;
1058   if ((uint64_t)passed > 12 * 60 * 60) goto error_exit;
1059 
1060   while (read(fd, dls, sizeof(dyn_lease)) == sizeof(dyn_lease)) {
1061     ip = ntohl(dls->lease_nip);
1062     if (ip >= gconfig.start_ip && ip <= gconfig.end_ip) {
1063       tmp_time = ntohl(dls->expires) - passed;
1064       if (tmp_time < 0) continue;
1065       addip_to_lease(dls->lease_nip, dls->lease_mac, (uint32_t*)&tmp_time, dls->hostname, 0);
1066     }
1067   }
1068   ret = 0;
1069 error_exit:
1070   free(dls);
1071   close(fd);
1072   return ret;
1073 }
1074 
dhcpd_main(void)1075 void dhcpd_main(void)
1076 {
1077   struct timeval tv;
1078   int retval;
1079   uint8_t *optptr, msgtype = 0;
1080   uint32_t waited = 0, serverid = 0, requested_nip = 0;
1081   uint32_t reqested_lease = 0, ip_pool_size = 0;
1082   char *hstname = NULL;
1083   fd_set rfds;
1084 
1085   infomode = LOG_CONSOLE;
1086   if (!(flag_chk(FLAG_f))) {
1087     daemon(0,0);
1088     infomode = LOG_SILENT;
1089   }
1090   if (flag_chk(FLAG_S)) {
1091         openlog("UDHCPD :", LOG_PID, LOG_DAEMON);
1092         infomode |= LOG_SYSTEM;
1093   }
1094   setlinebuf(stdout);
1095   parse_server_config((toys.optc==1)?toys.optargs[0]:"/etc/dhcpd.conf", keywords); //DHCPD_CONF_FILE
1096   infomsg(infomode, "toybox dhcpd started");
1097   gconfig.start_ip = ntohl(gconfig.start_ip);
1098   gconfig.end_ip = ntohl(gconfig.end_ip);
1099   ip_pool_size = gconfig.end_ip - gconfig.start_ip + 1;
1100   if (gconfig.max_leases > ip_pool_size) {
1101     error_msg("max_leases=%u is too big, setting to %u", (unsigned) gconfig.max_leases, ip_pool_size);
1102     gconfig.max_leases = ip_pool_size;
1103   }
1104   write_pid(gconfig.pidfile);
1105   set_maxlease();
1106   read_leasefile();
1107 
1108   if (get_interface(gconfig.interface, &gconfig.ifindex, &gconfig.server_nip,
1109         gconfig.server_mac)<0)
1110     perror_exit("Failed to get interface %s", gconfig.interface);
1111   gconfig.server_nip = htonl(gconfig.server_nip);
1112 
1113   setup_signal();
1114   open_listensock();
1115   fcntl(gstate.listensock, F_SETFD, FD_CLOEXEC);
1116 
1117   for (;;) {
1118     uint32_t timestmp = time(NULL);
1119     FD_ZERO(&rfds);
1120     FD_SET(gstate.listensock, &rfds);
1121     FD_SET(sigfd.rd, &rfds);
1122     tv.tv_sec = gconfig.auto_time - waited;
1123     tv.tv_usec = 0;
1124     retval = 0;
1125     serverid = 0;
1126     msgtype = 0;
1127 
1128     int maxfd = (sigfd.rd > gstate.listensock)? sigfd.rd : gstate.listensock;
1129     dbg("select waiting ....\n");
1130     retval = select(maxfd + 1, &rfds, NULL, NULL, (gconfig.auto_time?&tv:NULL));
1131     if (retval < 0) {
1132       if (errno == EINTR) {
1133         waited += (unsigned) time(NULL) - timestmp;
1134         continue;
1135       }
1136       dbg("Error in select wait again...\n");
1137       continue;
1138     }
1139     if (!retval) { // Timed out
1140       dbg("select wait Timed Out...\n");
1141       waited = 0;
1142       write_leasefile();
1143       if (get_interface(gconfig.interface, &gconfig.ifindex, &gconfig.server_nip, gconfig.server_mac)<0)
1144         perror_exit("Interface lost %s\n", gconfig.interface);
1145       gconfig.server_nip = htonl(gconfig.server_nip);
1146       continue;
1147     }
1148     if (FD_ISSET(sigfd.rd, &rfds)) { // Some Activity on RDFDs : is signal
1149       unsigned char sig;
1150       if (read(sigfd.rd, &sig, 1) != 1) {
1151         dbg("signal read failed.\n");
1152         continue;
1153       }
1154       switch (sig) {
1155       case SIGUSR1:
1156         infomsg(infomode, "Received SIGUSR1");
1157         write_leasefile();
1158         continue;
1159       case SIGTERM:
1160         infomsg(infomode, "Received SIGTERM");
1161         write_leasefile();
1162         unlink(gconfig.pidfile);
1163         exit(0);
1164         break;
1165       default: break;
1166       }
1167     }
1168     if (FD_ISSET(gstate.listensock, &rfds)) { // Some Activity on RDFDs : is socket
1169       dbg("select listen sock read\n");
1170       if (read_packet() < 0) {
1171         open_listensock();
1172         continue;
1173       }
1174       waited += time(NULL) - timestmp;
1175       get_optval((uint8_t*)&gstate.rcvd_pkt.options, DHCP_OPT_MESSAGE_TYPE, &gstate.rqcode);
1176       if (gstate.rqcode == 0 || gstate.rqcode < DHCPDISCOVER
1177           || gstate.rqcode > DHCPINFORM) {
1178         dbg("no or bad message type option, ignoring packet.\n");
1179         continue;
1180       }
1181       get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_SERVER_ID, &serverid);
1182       if (serverid && (serverid != gconfig.server_nip)) {
1183         dbg("server ID doesn't match, ignoring packet.\n");
1184         continue;
1185       }
1186       switch (gstate.rqcode) {
1187         case DHCPDISCOVER:
1188           msgtype = DHCPOFFER;
1189           dbg("Message Type : DHCPDISCOVER\n");
1190           get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_REQUESTED_IP, &requested_nip);
1191           get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_HOST_NAME, &hstname);
1192           reqested_lease = gconfig.offer_time;
1193           get_reqparam(&gstate.rqopt);
1194           optptr = prepare_send_pkt();
1195           gstate.send_pkt.yiaddr = getip_from_pool(requested_nip, gstate.rcvd_pkt.chaddr, &reqested_lease, hstname);
1196           if(!gstate.send_pkt.yiaddr){
1197             msgtype = DHCPNAK;
1198             optptr = set_optval(optptr, DHCP_OPT_MESSAGE_TYPE, &msgtype, 1);
1199             send_packet(1);
1200             break;
1201           }
1202           get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_LEASE_TIME, &reqested_lease);
1203           reqested_lease = htonl(get_lease(reqested_lease + time(NULL)));
1204           optptr = set_optval(optptr, DHCP_OPT_MESSAGE_TYPE, &msgtype, 1);
1205           optptr = set_optval(optptr, DHCP_OPT_SERVER_ID, &gconfig.server_nip, 4);
1206           optptr = set_optval(optptr, DHCP_OPT_LEASE_TIME, &reqested_lease, 4);
1207           optptr = set_reqparam(optptr, gstate.rqopt);
1208           send_packet(1);
1209           break;
1210         case DHCPREQUEST:
1211           msgtype = DHCPACK;
1212           dbg("Message Type : DHCPREQUEST\n");
1213           optptr = prepare_send_pkt();
1214           get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_REQUESTED_IP, &requested_nip);
1215           get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_LEASE_TIME, &reqested_lease);
1216           get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_HOST_NAME, &hstname);
1217           gstate.send_pkt.yiaddr = getip_from_pool(requested_nip, gstate.rcvd_pkt.chaddr, &reqested_lease, hstname);
1218           if (!serverid) reqested_lease = gconfig.max_lease_sec;
1219           if (!gstate.send_pkt.yiaddr) {
1220             msgtype = DHCPNAK;
1221             optptr = set_optval(optptr, DHCP_OPT_MESSAGE_TYPE, &msgtype, 1);
1222             send_packet(1);
1223             break;
1224           }
1225           optptr = set_optval(optptr, DHCP_OPT_MESSAGE_TYPE, &msgtype, 1);
1226           optptr = set_optval(optptr, DHCP_OPT_SERVER_ID, &gconfig.server_nip, 4);
1227           reqested_lease = htonl(reqested_lease);
1228           optptr = set_optval(optptr, DHCP_OPT_LEASE_TIME, &reqested_lease, 4);
1229           send_packet(1);
1230           write_leasefile();
1231           break;
1232         case DHCPDECLINE:// FALL THROUGH
1233         case DHCPRELEASE:
1234           dbg("Message Type : DHCPDECLINE or DHCPRELEASE \n");
1235           get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_SERVER_ID, &serverid);
1236           if (serverid != gconfig.server_nip) break;
1237           get_optval((uint8_t*) &gstate.rcvd_pkt.options, DHCP_OPT_REQUESTED_IP, &requested_nip);
1238           delip_from_lease(requested_nip, gstate.rcvd_pkt.chaddr, (gstate.rqcode==DHCPRELEASE)?0:gconfig.decline_time);
1239           break;
1240         default:
1241           dbg("Message Type : %u\n", gstate.rqcode);
1242           break;
1243       }
1244     }
1245   }
1246 }
1247