PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

commands/dhcpd/tags.c

http://www.minix3.org/
C | 924 lines | 776 code | 86 blank | 62 comment | 370 complexity | 94385c0200d8d890287f927d065d1e66 MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  1. /* tags.c - Obtain DHCP tags from the config file
  2. * Author: Kees J. Bot
  3. * 16 Dec 2000
  4. */
  5. #include <sys/types.h>
  6. #include <stdio.h>
  7. #include <stddef.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <errno.h>
  12. #include <signal.h>
  13. #include <string.h>
  14. #include <time.h>
  15. #include <limits.h>
  16. #include <configfile.h>
  17. #include <sys/ioctl.h>
  18. #include <sys/asynchio.h>
  19. #include <net/hton.h>
  20. #include <net/gen/socket.h>
  21. #include <net/gen/netdb.h>
  22. #include <net/gen/in.h>
  23. #include <net/gen/inet.h>
  24. #include <net/gen/ether.h>
  25. #include <net/gen/if_ether.h>
  26. #include <net/gen/eth_hdr.h>
  27. #include <net/gen/ip_hdr.h>
  28. #include <net/gen/udp.h>
  29. #include <net/gen/udp_hdr.h>
  30. #include <net/gen/dhcp.h>
  31. #include "dhcpd.h"
  32. #define doff(field) offsetof(dhcp_t, field)
  33. void settag(dhcp_t *dp, int tag, void *data, size_t len)
  34. {
  35. if (!dhcp_settag(dp, tag, data, len)) {
  36. /* Oops, it didn't fit? Is this really Minix??? */
  37. fprintf(stderr,
  38. "%s: DHCP packet too big, please trim the configuration\n",
  39. program);
  40. exit(1);
  41. }
  42. }
  43. static int name2ip(ipaddr_t *pip, const char *name, ipaddr_t ifip)
  44. {
  45. /* Translate a name to an IP address, preferably from the hosts file,
  46. * but also from the DNS if being a server. Prefer the address closest
  47. * to the interface with IP address 'ifip' if there are choices..
  48. */
  49. extern struct hostent *_gethostent(void); /* File reading versions. */
  50. extern void _endhostent(void);
  51. struct hostent *he;
  52. size_t len= strlen(name);
  53. u32_t d, distance= -1;
  54. ipaddr_t ip;
  55. int i;
  56. char *hn;
  57. /* Already an IP address? */
  58. if (inet_aton(name, pip)) return 1;
  59. /* In the hosts file? */
  60. while ((he= _gethostent()) != nil) {
  61. hn= he->h_name;
  62. i= -1;
  63. do {
  64. if (strncasecmp(name, hn, len) == 0
  65. && (hn[len] == 0 || hn[len] == '.')
  66. ) {
  67. memcpy(&ip, he->h_addr, sizeof(ip));
  68. d= ntohl(ip) ^ ntohl(ifip);
  69. if (d < distance) {
  70. *pip= ip;
  71. distance= d;
  72. }
  73. break;
  74. }
  75. } while ((hn= he->h_aliases[++i]) != nil);
  76. }
  77. _endhostent();
  78. if (distance < -1) return 1;
  79. /* Nothing? Try the real DNS if being a server. */
  80. if (serving) {
  81. if ((he= gethostbyname(name)) != nil && he->h_addrtype == AF_INET) {
  82. /* Select the address closest to 'ifip'. */
  83. for (i= 0; he->h_addr_list[i] != nil; i++) {
  84. memcpy(&ip, he->h_addr_list[i], sizeof(ip));
  85. d= ntohl(ip) ^ ntohl(ifip);
  86. if (d < distance) {
  87. *pip= ip;
  88. distance= d;
  89. }
  90. }
  91. return 1;
  92. }
  93. }
  94. return 0;
  95. }
  96. static char *ip2name(ipaddr_t ip)
  97. {
  98. /* Translate an IP address to a name, etc, etc. */
  99. extern struct hostent *_gethostent(void); /* File reading versions. */
  100. extern void _endhostent(void);
  101. struct hostent *he;
  102. /* In the hosts file? */
  103. while ((he= _gethostent()) != nil) {
  104. if (memcmp(he->h_addr, &ip, sizeof(ip)) == 0) break;
  105. }
  106. _endhostent();
  107. /* Nothing? Try the real DNS if being a server. */
  108. if (he == nil && serving) {
  109. he= gethostbyaddr((char *) &ip, sizeof(ip), AF_INET);
  110. }
  111. return he != nil ? he->h_name : nil;
  112. }
  113. static int cidr_aton(const char *cidr, ipaddr_t *addr, ipaddr_t *mask)
  114. {
  115. char *slash, *check;
  116. ipaddr_t a;
  117. int ok;
  118. unsigned long len;
  119. if ((slash= strchr(cidr, '/')) == nil) return 0;
  120. *slash++= 0;
  121. ok= inet_aton(cidr, &a);
  122. len= strtoul(slash, &check, 10);
  123. if (check == slash || *check != 0 || len > 32) ok= 0;
  124. *--slash= '/';
  125. if (!ok) return 0;
  126. *addr= a;
  127. *mask= htonl(len == 0 ? 0 : (0xFFFFFFFFUL << (32-len)) & 0xFFFFFFFFUL);
  128. return 1;
  129. }
  130. char *cidr_ntoa(ipaddr_t addr, ipaddr_t mask)
  131. {
  132. ipaddr_t testmask= 0xFFFFFFFFUL;
  133. int n;
  134. static char result[sizeof("255.255.255.255/255.255.255.255")];
  135. for (n= 32; n >= 0; n--) {
  136. if (mask == htonl(testmask)) break;
  137. testmask= (testmask << 1) & 0xFFFFFFFFUL;
  138. }
  139. sprintf(result, "%s/%-2d", inet_ntoa(addr), n);
  140. if (n == -1) strcpy(strchr(result, '/')+1, inet_ntoa(mask));
  141. return result;
  142. }
  143. static size_t ascii2octet(u8_t *b, size_t size, const char *a)
  144. {
  145. /* Convert a series of hex digit pairs to an octet (binary) array at
  146. * 'b' with length 'size'. Return the number of octets in 'a' or
  147. * -1 on error.
  148. */
  149. size_t len;
  150. int n, c;
  151. len= 0;
  152. n= 0;
  153. while ((c= *a++) != 0) {
  154. if (between('0', c, '9')) c= (c - '0') + 0x0;
  155. else
  156. if (between('a', c, 'f')) c= (c - 'a') + 0xa;
  157. else
  158. if (between('A', c, 'F')) c= (c - 'A') + 0xA;
  159. else {
  160. return -1;
  161. }
  162. if (n == 0) {
  163. if (len < size) b[len] = c << 4;
  164. } else {
  165. if (len < size) b[len] |= c;
  166. len++;
  167. }
  168. n ^= 1;
  169. }
  170. return n == 0 ? len : -1;
  171. }
  172. void ether2clid(u8_t *clid, ether_addr_t *eth)
  173. {
  174. /* Convert an Ethernet address to the default client ID form. */
  175. clid[0]= DHCP_HTYPE_ETH;
  176. memcpy(clid+1, eth, DHCP_HLEN_ETH);
  177. }
  178. static size_t ascii2clid(u8_t *clid, const char *a)
  179. {
  180. /* Convert an ethernet address, or a series of hex digits to a client ID.
  181. * Return its length if ok, otherwise -1.
  182. */
  183. size_t len;
  184. ether_addr_t *eth;
  185. if ((eth= ether_aton(a)) != nil) {
  186. ether2clid(clid, eth);
  187. len= 1+DHCP_HLEN_ETH;
  188. } else {
  189. len= ascii2octet(clid, CLID_MAX, a);
  190. }
  191. return len;
  192. }
  193. static config_t *dhcpconf; /* In-core DHCP configuration. */
  194. /* DHCP tag types. */
  195. typedef enum { TT_ASCII, TT_BOOLEAN, TT_IP, TT_NUMBER, TT_OCTET } tagtype_t;
  196. /* DHCP/BOOTP tag definitions. */
  197. typedef struct tagdef {
  198. u8_t tag; /* Tag number. */
  199. u8_t type; /* Type and flags. */
  200. u8_t gran; /* Granularity. */
  201. u8_t max; /* Maximum number of arguments. */
  202. const char *name; /* Defined name. */
  203. } tagdef_t;
  204. #define TF_TYPE 0x07 /* To mask out the type. */
  205. #define TF_STATIC 0x08 /* "Static", i.e. a struct field. */
  206. #define TF_RO 0x10 /* Read-only, user can't set. */
  207. /* List of static DHCP fields. The tag field is misused here as an offset
  208. * into the DHCP structure.
  209. */
  210. static tagdef_t statictag[] = {
  211. { doff(op), TT_NUMBER|TF_STATIC|TF_RO, 1, 1, "op" },
  212. { doff(htype), TT_NUMBER|TF_STATIC|TF_RO, 1, 1, "htype" },
  213. { doff(hlen), TT_NUMBER|TF_STATIC|TF_RO, 1, 1, "hlen" },
  214. { doff(hops), TT_NUMBER|TF_STATIC|TF_RO, 1, 1, "hops" },
  215. { doff(xid), TT_NUMBER|TF_STATIC|TF_RO, 4, 1, "xid" },
  216. { doff(secs), TT_NUMBER|TF_STATIC|TF_RO, 2, 1, "secs" },
  217. { doff(flags), TT_NUMBER|TF_STATIC|TF_RO, 2, 1, "flags" },
  218. { doff(ciaddr), TT_IP|TF_STATIC|TF_RO, 1, 1, "ciaddr" },
  219. { doff(yiaddr), TT_IP|TF_STATIC|TF_RO, 1, 1, "yiaddr" },
  220. { doff(siaddr), TT_IP|TF_STATIC, 1, 1, "siaddr" },
  221. { doff(giaddr), TT_IP|TF_STATIC|TF_RO, 1, 1, "giaddr" },
  222. { doff(chaddr), TT_OCTET|TF_STATIC|TF_RO, 1, 16, "chaddr" },
  223. { doff(sname), TT_ASCII|TF_STATIC, 1, 64, "sname" },
  224. { doff(file), TT_ASCII|TF_STATIC, 1, 128, "file" },
  225. };
  226. #define N_STATIC arraysize(statictag)
  227. static tagdef_t alltagdef[N_STATIC + 254]; /* List of tag definitions. */
  228. #define tagdef (alltagdef+N_STATIC-1) /* Just the optional ones. */
  229. #define tagdefined(tp) ((tp)->name != nil)
  230. static void inittagdef(void)
  231. {
  232. /* Initialize the tag definitions from the "tag" commands in the config
  233. * file.
  234. */
  235. int t;
  236. tagdef_t *tp;
  237. static tagdef_t predef[] = {
  238. { DHCP_TAG_NETMASK, TT_IP, 1, 1, "netmask" },
  239. { DHCP_TAG_GATEWAY, TT_IP, 1, 255, "gateway" },
  240. { DHCP_TAG_DNS, TT_IP, 1, 255, "DNSserver" },
  241. };
  242. static char *typenames[] = { "ascii", "boolean", "ip", "number", "octet" };
  243. config_t *cfg;
  244. static u8_t rotags[] = {
  245. DHCP_TAG_REQIP, DHCP_TAG_OVERLOAD, DHCP_TAG_TYPE, DHCP_TAG_SERVERID,
  246. DHCP_TAG_REQPAR, DHCP_TAG_MESSAGE, DHCP_TAG_MAXDHCP
  247. };
  248. for (t= 1; t <= 254; t++) {
  249. tp= &tagdef[t];
  250. tp->tag= t;
  251. tp->type= TT_OCTET;
  252. tp->name= nil;
  253. }
  254. /* Set the static and "all Minix needs" tags. */
  255. memcpy(alltagdef, statictag, sizeof(statictag));
  256. for (tp= predef; tp < arraylimit(predef); tp++) tagdef[tp->tag] = *tp;
  257. /* Search for tag definitions in the config file. */
  258. for (cfg= dhcpconf; cfg != nil; cfg= cfg->next) {
  259. config_t *cmd= cfg->list;
  260. if (strcasecmp(cmd->word, "tag") == 0) {
  261. if (config_length(cmd) == 6
  262. && (cmd->next->flags & CFG_DULONG)
  263. && config_isatom(cmd->next->next)
  264. && config_isatom(cmd->next->next->next)
  265. && (cmd->next->next->next->next->flags & CFG_DULONG)
  266. && (cmd->next->next->next->next->next->flags & CFG_DULONG)
  267. ) {
  268. unsigned long tag, gran, max;
  269. const char *name, *typename;
  270. unsigned type;
  271. tag= strtoul(cmd->next->word, nil, 10);
  272. name= cmd->next->next->word;
  273. typename= cmd->next->next->next->word;
  274. gran= strtoul(cmd->next->next->next->next->word, nil, 10);
  275. max= strtoul(cmd->next->next->next->next->next->word, nil, 10);
  276. for (type= 0; type < arraysize(typenames); type++) {
  277. if (strcasecmp(typename, typenames[type]) == 0) break;
  278. }
  279. if (!(1 <= tag && tag <= 254)
  280. || !(type < arraysize(typenames))
  281. || !((type == TT_NUMBER
  282. && (gran == 1 || gran == 2 || gran == 4))
  283. || (type != TT_NUMBER && 1 <= gran && gran <= 16))
  284. || !(max <= 255)
  285. ) {
  286. fprintf(stderr,
  287. "\"%s\", line %u: Tag definition is incorrect\n",
  288. cmd->file, cmd->line);
  289. exit(1);
  290. }
  291. tp= &tagdef[(int)tag];
  292. tp->type= type;
  293. tp->name= name;
  294. tp->gran= gran;
  295. tp->max= max;
  296. } else {
  297. fprintf(stderr,
  298. "\"%s\", line %u: Usage: tag number name type granularity max\n",
  299. cmd->file, cmd->line);
  300. exit(1);
  301. }
  302. }
  303. }
  304. /* Many DHCP tags are not for the user to play with. */
  305. for (t= 0; t < arraysize(rotags); t++) tagdef[rotags[t]].type |= TF_RO;
  306. }
  307. static tagdef_t *tagdefbyname(const char *name)
  308. {
  309. /* Find a tag definition by the name of the tag. Return null if not
  310. * defined.
  311. */
  312. tagdef_t *tp;
  313. for (tp= alltagdef; tp < arraylimit(alltagdef); tp++) {
  314. if (tagdefined(tp) && strcasecmp(tp->name, name) == 0) return tp;
  315. }
  316. return nil;
  317. }
  318. void initdhcpconf(void)
  319. {
  320. /* Read/refresh configuration from the DHCP configuration file. */
  321. dhcpconf= config_read(configfile, 0, dhcpconf);
  322. if (config_renewed(dhcpconf)) inittagdef();
  323. }
  324. static void configtag(dhcp_t *dp, config_t *cmd, ipaddr_t ifip)
  325. {
  326. /* Add a tag to a DHCP packet from the config file. */
  327. tagdef_t *tp;
  328. u8_t data[260], *d;
  329. size_t i;
  330. int delete= 0;
  331. if (strcasecmp(cmd->word, "no") == 0) {
  332. if (config_length(cmd) != 2 || !config_isatom(cmd->next)) {
  333. fprintf(stderr, "\"%s\", line %u: Usage: no tag-name\n",
  334. cmd->file, cmd->line);
  335. exit(1);
  336. }
  337. cmd= cmd->next;
  338. delete= 1;
  339. }
  340. if ((tp= tagdefbyname(cmd->word)) == nil) {
  341. fprintf(stderr, "\"%s\", line %u: Unknown tag '%s'\n",
  342. cmd->file, cmd->line, cmd->word);
  343. exit(1);
  344. }
  345. if (tp->type & TF_RO) {
  346. fprintf(stderr, "\"%s\", line %u: Tag '%s' can't be configured\n",
  347. cmd->file, cmd->line, cmd->word);
  348. exit(1);
  349. }
  350. i= 0;
  351. d= data;
  352. if (!delete) {
  353. config_t *arg= cmd->next;
  354. do {
  355. switch (tp->type & TF_TYPE) {
  356. case TT_ASCII: {
  357. if (arg == nil || !config_isatom(arg) || arg->next != nil) {
  358. fprintf(stderr, "\"%s\", line %u: Usage: %s string\n",
  359. cmd->file, cmd->line, cmd->word);
  360. exit(1);
  361. }
  362. strncpy((char *) data, arg->word, sizeof(data));
  363. d += i = strnlen((char *) data, sizeof(data));
  364. break;}
  365. case TT_BOOLEAN: {
  366. if (arg == nil || !config_isatom(arg)
  367. || !(strcasecmp(arg->word, "false") == 0
  368. || strcasecmp(arg->word, "true") == 0)
  369. ) {
  370. fprintf(stderr,
  371. "\"%s\", line %u: Usage: %s false|true ...\n",
  372. cmd->file, cmd->line, cmd->word);
  373. exit(1);
  374. }
  375. if (d < arraylimit(data)) {
  376. *d++ = (arg->word[0] != 'f' && arg->word[0] != 'F');
  377. }
  378. i++;
  379. break;}
  380. case TT_IP: {
  381. ipaddr_t ip;
  382. unsigned long len;
  383. char *end;
  384. if (arg == nil || !config_isatom(arg)) {
  385. fprintf(stderr, "\"%s\", line %u: Usage: %s host ...\n",
  386. cmd->file, cmd->line, cmd->word);
  387. exit(1);
  388. }
  389. if (arg->word[0] == '/'
  390. && between(1, len= strtoul(arg->word+1, &end, 10), 31)
  391. && *end == 0
  392. ) {
  393. ip= htonl((0xFFFFFFFFUL << (32-len)) & 0xFFFFFFFFUL);
  394. } else
  395. if (!name2ip(&ip, arg->word, ifip)) {
  396. fprintf(stderr,
  397. "\"%s\", line %u: Can't translate %s to an IP address\n",
  398. arg->file, arg->line, arg->word);
  399. exit(1);
  400. }
  401. if (d <= arraylimit(data) - sizeof(ip)) {
  402. memcpy(d, &ip, sizeof(ip));
  403. d += sizeof(ip);
  404. }
  405. i++;
  406. break;}
  407. case TT_NUMBER: {
  408. unsigned long n;
  409. int g;
  410. if (arg == nil || !(arg->flags & CFG_CLONG)) {
  411. fprintf(stderr, "\"%s\", line %u: Usage: %s number ...\n",
  412. cmd->file, cmd->line, cmd->word);
  413. exit(1);
  414. }
  415. n= strtoul(arg->word, nil, 0);
  416. g= tp->gran;
  417. do {
  418. if (d <= arraylimit(data)) *d++ = (n >> (--g * 8)) & 0xFF;
  419. } while (g != 0);
  420. i++;
  421. break;}
  422. case TT_OCTET: {
  423. if (arg == nil || !config_isatom(arg) || arg->next != nil) {
  424. fprintf(stderr, "\"%s\", line %u: Usage: %s hexdigits\n",
  425. cmd->file, cmd->line, cmd->word);
  426. exit(1);
  427. }
  428. i= ascii2octet(data, sizeof(data), arg->word);
  429. if (i == -1) {
  430. fprintf(stderr,
  431. "\"%s\", line %u: %s: Bad hexdigit string\n",
  432. arg->file, arg->line, arg->word);
  433. exit(1);
  434. }
  435. d= data + i;
  436. break;}
  437. }
  438. } while ((arg= arg->next) != nil);
  439. if (d > data + 255) {
  440. fprintf(stderr, "\"%s\", line %u: Tag value is way too big\n",
  441. cmd->file, cmd->line);
  442. exit(1);
  443. }
  444. if ((tp->type & TF_TYPE) != TT_NUMBER && (i % tp->gran) != 0) {
  445. fprintf(stderr,
  446. "\"%s\", line %u: Expected a multiple of %d initializers\n",
  447. cmd->file, cmd->line, tp->gran);
  448. exit(1);
  449. }
  450. if (tp->max != 0 && i > tp->max) {
  451. fprintf(stderr,
  452. "\"%s\", line %u: Got %d initializers, can have only %d\n",
  453. cmd->file, cmd->line, (int) i, tp->max);
  454. exit(1);
  455. }
  456. }
  457. if (tp->type & TF_STATIC) {
  458. size_t len= tp->gran * tp->max;
  459. if ((tp->type & TF_TYPE) == TT_IP) len *= sizeof(ipaddr_t);
  460. memset(B(dp) + tp->tag, 0, len);
  461. memcpy(B(dp) + tp->tag, data, (d - data));
  462. } else {
  463. settag(dp, tp->tag, data, (d - data));
  464. }
  465. }
  466. int makedhcp(dhcp_t *dp, u8_t *class, size_t calen, u8_t *client, size_t cilen,
  467. ipaddr_t ip, ipaddr_t ifip, network_t *np)
  468. {
  469. /* Fill in a DHCP packet at 'dp' for the host identified by the
  470. * (class, client, ip) combination. Makedhcp is normally called twice,
  471. * once to find the IP address (so ip == 0) and once again to find all
  472. * data that goes with that IP address (ip != 0). On the first call the
  473. * return value of this function should be ignored and only 'yiaddr'
  474. * checked and used as 'ip' on the next pass. True is returned iff there
  475. * is information for the client on the network at interface address
  476. * 'ifip', by checking if the 'ip' and 'ifip' are on the same network.
  477. * If np is nonnull then we are working for one of our own interfaces, so
  478. * options can be set and adjourning interfaces can be programmed.
  479. */
  480. config_t *todo[16];
  481. size_t ntodo= 0;
  482. ipaddr_t hip, mask;
  483. u8_t *pmask;
  484. char *hostname;
  485. u32_t distance= -1;
  486. initdhcpconf();
  487. /* Start creating a packet. */
  488. dhcp_init(dp);
  489. dp->op= DHCP_BOOTREPLY;
  490. /* The initial TODO list is the whole DHCP config. */
  491. todo[ntodo++]= dhcpconf;
  492. while (ntodo > 0) {
  493. config_t *cmd, *follow;
  494. if (todo[ntodo-1] == nil) { ntodo--; continue; }
  495. cmd= todo[ntodo-1]->list;
  496. todo[ntodo-1]= todo[ntodo-1]->next;
  497. follow= nil; /* Macro or list to follow next? */
  498. if (strcasecmp(cmd->word, "client") == 0) {
  499. u8_t cfgid[CLID_MAX];
  500. size_t cfglen;
  501. char *name;
  502. int ifno;
  503. u32_t d;
  504. if (between(3, config_length(cmd), 5)
  505. && config_isatom(cmd->next)
  506. && (cfglen= ascii2clid(cfgid, cmd->next->word)) != -1
  507. && config_isatom(cmd->next->next)
  508. && (((ifno= ifname2if(cmd->next->next->word)) == -1
  509. && config_length(cmd) <= 4)
  510. || ((ifno= ifname2if(cmd->next->next->word)) != -1
  511. && config_length(cmd) >= 4
  512. && config_isatom(cmd->next->next->next)))
  513. ) {
  514. if (cilen == cfglen && memcmp(client, cfgid, cilen) == 0
  515. && (ifno == -1 || np == nil || ifno == np->n)
  516. ) {
  517. config_t *atname= cmd->next->next;
  518. if (ifno != -1) atname= atname->next;
  519. name= atname->word;
  520. if (name2ip(&hip, name, ifip) && (ip == 0 || ip == hip)) {
  521. d= ntohl(hip) ^ ntohl(ifip);
  522. if (d < distance) {
  523. dp->yiaddr= hip;
  524. follow= atname->next;
  525. distance= d;
  526. }
  527. }
  528. }
  529. } else {
  530. fprintf(stderr,
  531. "\"%s\", line %u: Usage: client ID [ip#] host [macro|{params}]\n",
  532. cmd->file, cmd->line);
  533. exit(1);
  534. }
  535. } else
  536. if (strcasecmp(cmd->word, "class") == 0) {
  537. config_t *clist;
  538. int match;
  539. match= 0;
  540. for (clist= cmd->next; clist != nil
  541. && clist->next != nil
  542. && config_isatom(clist); clist= clist->next) {
  543. if (calen > 0
  544. && strncmp(clist->word, (char *) class, calen) == 0
  545. ) {
  546. match= 1;
  547. }
  548. }
  549. if (clist == cmd->next || clist->next != nil) {
  550. fprintf(stderr,
  551. "\"%s\", line %u: Usage: class class-name ... macro|{params}\n",
  552. cmd->file, cmd->line);
  553. }
  554. if (match) follow= clist;
  555. } else
  556. if (strcasecmp(cmd->word, "host") == 0) {
  557. if (config_length(cmd) == 3
  558. && config_isatom(cmd->next)
  559. ) {
  560. if (ip != 0) {
  561. if (cidr_aton(cmd->next->word, &hip, &mask)) {
  562. if (((hip ^ ip) & mask) == 0) {
  563. if (!gettag(dp, DHCP_TAG_NETMASK, nil, nil)) {
  564. settag(dp, DHCP_TAG_NETMASK,
  565. &mask, sizeof(mask));
  566. }
  567. dp->yiaddr= ip;
  568. follow= cmd->next->next;
  569. }
  570. } else
  571. if (name2ip(&hip, cmd->next->word, ifip)) {
  572. if (hip == ip) {
  573. dp->yiaddr= ip;
  574. follow= cmd->next->next;
  575. }
  576. }
  577. }
  578. } else {
  579. fprintf(stderr,
  580. "\"%s\", line %u: Usage: host host-spec macro|{params}\n",
  581. cmd->file, cmd->line);
  582. exit(1);
  583. }
  584. } else
  585. if (strcasecmp(cmd->word, "interface") == 0) {
  586. if (between(3, config_length(cmd), 4)
  587. && config_isatom(cmd->next)
  588. && config_isatom(cmd->next->next)
  589. ) {
  590. network_t *ifnp;
  591. if (np != nil) {
  592. if ((ifnp= if2net(ifname2if(cmd->next->word))) == nil) {
  593. fprintf(stderr,
  594. "\"%s\", line %u: Can't find interface %s\n",
  595. cmd->next->file, cmd->next->line, cmd->next->word);
  596. exit(1);
  597. }
  598. if (!name2ip(&hip, cmd->next->next->word, 0)) {
  599. fprintf(stderr,
  600. "\"%s\", line %u: Can't find IP address of %s\n",
  601. cmd->next->next->file, cmd->next->next->line,
  602. cmd->next->next->word);
  603. exit(1);
  604. }
  605. ifnp->ip= hip;
  606. if (ifnp == np) {
  607. dp->yiaddr= hip;
  608. follow= cmd->next->next->next;
  609. }
  610. }
  611. } else {
  612. fprintf(stderr,
  613. "\"%s\", line %u: Usage: interface ip# host%s\n",
  614. cmd->file, cmd->line, ntodo==1 ? " [macro|{params}]" : "");
  615. exit(1);
  616. }
  617. } else
  618. if (strcasecmp(cmd->word, "macro") == 0) {
  619. if (config_length(cmd) == 2 && config_isatom(cmd->next)) {
  620. follow= cmd->next;
  621. } else
  622. if (ntodo > 1) {
  623. fprintf(stderr, "\"%s\", line %u: Usage: macro macro-name\n",
  624. cmd->file, cmd->line);
  625. exit(1);
  626. }
  627. } else
  628. if (strcasecmp(cmd->word, "tag") == 0) {
  629. if (ntodo > 1) {
  630. fprintf(stderr,
  631. "\"%s\", line %u: A %s can't be defined here\n",
  632. cmd->file, cmd->line, cmd->word);
  633. exit(1);
  634. }
  635. } else
  636. if (strcasecmp(cmd->word, "option") == 0) {
  637. int ifno;
  638. network_t *ifnp;
  639. config_t *opt;
  640. if ((opt= cmd->next) != nil
  641. && config_isatom(opt)
  642. && (ifno= ifname2if(opt->word)) != -1
  643. ) {
  644. if ((ifnp= if2net(ifno)) == nil) {
  645. fprintf(stderr,
  646. "\"%s\", line %u: Interface %s is not enabled\n",
  647. opt->file, opt->line, opt->word);
  648. exit(1);
  649. }
  650. opt= opt->next;
  651. } else {
  652. ifnp= np;
  653. }
  654. if (between(1, config_length(opt), 2)
  655. && config_isatom(opt)
  656. && strcasecmp(opt->word, "server") == 0
  657. && (opt->next == nil
  658. || strcasecmp(opt->next->word, "inform") == 0)
  659. ) {
  660. if (np != nil) {
  661. ifnp->flags |= NF_SERVING;
  662. if (opt->next != nil) ifnp->flags |= NF_INFORM;
  663. }
  664. } else
  665. if (config_length(opt) == 2
  666. && config_isatom(opt)
  667. && strcasecmp(opt->word, "relay") == 0
  668. && config_isatom(opt->next)
  669. ) {
  670. if (np != nil) {
  671. if (!name2ip(&hip, opt->next->word, ifip)) {
  672. fprintf(stderr,
  673. "\"%s\", line %u: Can't find IP address of %s\n",
  674. opt->next->file, opt->next->line,
  675. opt->next->word);
  676. exit(1);
  677. }
  678. ifnp->flags |= NF_RELAYING;
  679. ifnp->server= hip;
  680. }
  681. } else
  682. if (config_length(opt) == 1
  683. && config_isatom(opt)
  684. && strcasecmp(opt->word, "possessive") == 0
  685. ) {
  686. if (np != nil) ifnp->flags |= NF_POSSESSIVE;
  687. } else
  688. if (config_length(opt) == 2
  689. && config_isatom(opt)
  690. && strcasecmp(opt->word, "hostname") == 0
  691. && config_isatom(opt->next)
  692. ) {
  693. if (np != nil) np->hostname= opt->next->word;
  694. } else {
  695. fprintf(stderr, "\"%s\", line %u: Unknown option\n",
  696. cmd->file, cmd->line);
  697. exit(1);
  698. }
  699. } else {
  700. /* Must be an actual data carrying tag. */
  701. configtag(dp, cmd, ifip);
  702. }
  703. if (follow != nil) {
  704. /* A client/class/host entry selects a macro or list that must
  705. * be followed next.
  706. */
  707. config_t *macro;
  708. if (config_isatom(follow)) { /* Macro name */
  709. config_t *cfg;
  710. for (cfg= dhcpconf; cfg != nil; cfg= cfg->next) {
  711. macro= cfg->list;
  712. if (strcasecmp(macro->word, "macro") == 0) {
  713. if (config_length(macro) == 3
  714. && config_isatom(macro->next)
  715. && config_issub(macro->next->next)
  716. ) {
  717. if (strcasecmp(macro->next->word, follow->word) == 0
  718. ) {
  719. break;
  720. }
  721. } else {
  722. fprintf(stderr,
  723. "\"%s\", line %u: Usage: macro macro-name {params}\n",
  724. macro->file, macro->line);
  725. }
  726. }
  727. }
  728. follow= cfg == nil ? nil : macro->next->next->list;
  729. } else {
  730. /* Simply a list of more tags and stuff. */
  731. follow= follow->list;
  732. }
  733. if (ntodo == arraysize(todo)) {
  734. fprintf(stderr, "\"%s\", line %u: Nesting is too deep\n",
  735. follow->file, follow->line);
  736. exit(1);
  737. }
  738. todo[ntodo++]= follow;
  739. }
  740. }
  741. /* Check if the IP and netmask are OK for the interface. */
  742. if (!gettag(dp, DHCP_TAG_NETMASK, &pmask, nil)) return 0;
  743. memcpy(&mask, pmask, sizeof(mask));
  744. if (((ip ^ ifip) & mask) != 0) return 0;
  745. /* Fill in the hostname and/or domain. */
  746. if ((hostname= ip2name(ip)) != nil) {
  747. char *domain;
  748. if ((domain= strchr(hostname, '.')) != nil) *domain++ = 0;
  749. if (!gettag(dp, DHCP_TAG_HOSTNAME, nil, nil)) {
  750. settag(dp, DHCP_TAG_HOSTNAME, hostname, strlen(hostname));
  751. }
  752. if (domain != nil && !gettag(dp, DHCP_TAG_DOMAIN, nil, nil)) {
  753. settag(dp, DHCP_TAG_DOMAIN, domain, strlen(domain));
  754. }
  755. }
  756. return 1;
  757. }
  758. static char *dhcpopname(int op)
  759. {
  760. static char *onames[] = { "??\?", "REQUEST", "REPLY" };
  761. return onames[op < arraysize(onames) ? op : 0];
  762. }
  763. char *dhcptypename(int type)
  764. {
  765. static char *tnames[] = {
  766. "??\?", "DISCOVER", "OFFER", "REQUEST", "DECLINE",
  767. "ACK", "NAK", "RELEASE", "INFORM"
  768. };
  769. return tnames[type < arraysize(tnames) ? type : 0];
  770. }
  771. void printdhcp(dhcp_t *dp)
  772. {
  773. /* Print the contents of a DHCP packet, usually for debug purposes. */
  774. tagdef_t *tp;
  775. u8_t *data, *ovld;
  776. size_t i, len;
  777. for (tp= alltagdef; tp < arraylimit(alltagdef); tp++) {
  778. if (tp->type & TF_STATIC) {
  779. data= B(dp) + tp->tag;
  780. len= tp->gran * tp->max;
  781. if ((tp->type & TF_TYPE) == TT_IP) len *= sizeof(ipaddr_t);
  782. if (tp->tag == doff(chaddr)) len= dp->hlen;
  783. /* Don't show uninteresting stuff. */
  784. if (tp->tag == doff(htype) && dp->htype == DHCP_HTYPE_ETH) continue;
  785. if (tp->tag == doff(hlen) && dp->hlen == DHCP_HLEN_ETH) continue;
  786. if ((tp->tag == doff(file) || tp->tag == doff(sname))
  787. && gettag(dp, DHCP_TAG_OVERLOAD, &ovld, nil)
  788. && (ovld[0] & (tp->tag == doff(file) ? 1 : 2))
  789. ) {
  790. continue;
  791. }
  792. for (i= 0; i < len && data[i] == 0; i++) {}
  793. if (i == len) continue;
  794. } else {
  795. if (!gettag(dp, tp->tag, &data, &len)) continue;
  796. }
  797. if (tagdefined(tp)) {
  798. printf("\t%s =", tp->name);
  799. } else {
  800. printf("\tT%d =", tp->tag);
  801. }
  802. i= 0;
  803. while (i < len) {
  804. switch (tp->type & TF_TYPE) {
  805. case TT_ASCII: {
  806. printf(" \"%.*s\"", (int) len, data);
  807. i= len;
  808. break;}
  809. case TT_BOOLEAN: {
  810. printf(data[i++] == 0 ? " false" : " true");
  811. break;}
  812. case TT_IP: {
  813. ipaddr_t ip;
  814. memcpy(&ip, data+i, sizeof(ip));
  815. printf(" %s", inet_ntoa(ip));
  816. i += sizeof(ip);
  817. break;}
  818. case TT_NUMBER: {
  819. u32_t n= 0;
  820. int g= tp->gran;
  821. do n= (n << 8) | data[i++]; while (--g != 0);
  822. printf(" %lu", (unsigned long) n);
  823. if ((tp->type & TF_STATIC) && tp->tag == doff(op)) {
  824. printf(" (%s)", dhcpopname(n));
  825. }
  826. if (!(tp->type & TF_STATIC) && tp->tag == DHCP_TAG_TYPE) {
  827. printf(" (%s)", dhcptypename(n));
  828. }
  829. break;}
  830. case TT_OCTET: {
  831. if (i == 0) fputc(' ', stdout);
  832. printf("%02X", data[i++]);
  833. break;}
  834. }
  835. }
  836. fputc('\n', stdout);
  837. }
  838. }