[9] | 1 | /* dhcpd 1.15 - Dynamic Host Configuration Protocol daemon.
|
---|
| 2 | * Author: Kees J. Bot
|
---|
| 3 | * 11 Jun 1999
|
---|
| 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/udp_io.h>
|
---|
| 31 | #include <net/gen/dhcp.h>
|
---|
| 32 | #include "arp.h"
|
---|
| 33 | #define EXTERN
|
---|
| 34 | #include "dhcpd.h"
|
---|
| 35 |
|
---|
| 36 | char *configfile= PATH_DHCPCONF;
|
---|
| 37 | char *poolfile= PATH_DHCPPOOL;
|
---|
| 38 | static char *cachefile= PATH_DHCPCACHE;
|
---|
| 39 | static int qflag; /* True if printing cached DHCP data. */
|
---|
| 40 | static int aflag, rflag; /* True if adding or deleting pool addresses. */
|
---|
| 41 |
|
---|
| 42 | #define BCAST_IP HTONL(0xFFFFFFFFUL)
|
---|
| 43 |
|
---|
| 44 | /* We try to play with up to this many networks. */
|
---|
| 45 | #define N_NETS 32
|
---|
| 46 | static unsigned n_nets; /* Actual number of networks. */
|
---|
| 47 |
|
---|
| 48 | void report(const char *label)
|
---|
| 49 | {
|
---|
| 50 | static FILE *logfp;
|
---|
| 51 | if(!logfp)
|
---|
| 52 | logfp = fopen("/usr/log/dhcp.log", "w");
|
---|
| 53 | if(logfp)
|
---|
| 54 | fprintf(logfp, "%s: %s: %s\n", program, label, strerror(errno));
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | void fatal(const char *label)
|
---|
| 58 | {
|
---|
| 59 | report(label);
|
---|
| 60 | exit(1);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | void *allocate(size_t size)
|
---|
| 64 | {
|
---|
| 65 | void *mem;
|
---|
| 66 |
|
---|
| 67 | if ((mem= malloc(size)) == nil) fatal("Can't allocate memory");
|
---|
| 68 | return mem;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /* Choose a DHCP xid based on the start time and network number. Not really
|
---|
| 72 | * random, but we don't have anything more random than the clock anyway.
|
---|
| 73 | */
|
---|
| 74 | #define XID(np) htonl(((u32_t) (np)->start << 8) | (np)->n)
|
---|
| 75 |
|
---|
| 76 | static network_t *network[N_NETS];
|
---|
| 77 |
|
---|
| 78 | int ifname2if(const char *name)
|
---|
| 79 | {
|
---|
| 80 | /* Translate an interface name to a number, -1 if bad. */
|
---|
| 81 | char *end;
|
---|
| 82 | unsigned long n;
|
---|
| 83 |
|
---|
| 84 | if (*name++ != 'i' || *name++ != 'p') return -1;
|
---|
| 85 | n= strtoul(name, &end, 10);
|
---|
| 86 | if (end == name || *end != 0) return -1;
|
---|
| 87 | if (n >= N_NETS) return -1;
|
---|
| 88 | return n;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | network_t *if2net(int n)
|
---|
| 92 | {
|
---|
| 93 | /* Translate an interface number to a network struct. */
|
---|
| 94 | int i;
|
---|
| 95 |
|
---|
| 96 | for (i= 0; i < n_nets; i++) {
|
---|
| 97 | if (network[i]->n == n) return network[i];
|
---|
| 98 | }
|
---|
| 99 | return nil;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | static ipaddr_t defaultmask(ipaddr_t ip)
|
---|
| 103 | {
|
---|
| 104 | /* Compute netmask by the oldfashioned Class rules. */
|
---|
| 105 | if (B(&ip)[0] < 0x80) return HTONL(0xFF000000UL); /* Class A. */
|
---|
| 106 | if (B(&ip)[0] < 0xC0) return HTONL(0xFFFF0000UL); /* Class B. */
|
---|
| 107 | if (B(&ip)[0] < 0xE0) return HTONL(0xFFFFFF00UL); /* Class C. */
|
---|
| 108 | return HTONL(0xFFFFFFFFUL); /* Multicast? Shouldn't happen... */
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | #define POOL_MAGIC HTONL(0x81F85D00UL)
|
---|
| 112 |
|
---|
| 113 | typedef struct pool { /* Dynamic pool entry. */
|
---|
| 114 | u32_t magic; /* Pool file magic number. */
|
---|
| 115 | ipaddr_t ip; /* IP address. */
|
---|
| 116 | u32_t expire; /* When does/did the lease expire? */
|
---|
| 117 | u8_t len; /* Client ID length. */
|
---|
| 118 | u8_t unused[19]; /* Space for extensions. */
|
---|
| 119 | u8_t clid[CLID_MAX]; /* Client ID of current/last user. */
|
---|
| 120 | } pool_t;
|
---|
| 121 |
|
---|
| 122 | static int openpool(int mode)
|
---|
| 123 | {
|
---|
| 124 | /* Open the dynamic pool and lock it, return fd on success or -1. */
|
---|
| 125 | int fd;
|
---|
| 126 | struct flock lck;
|
---|
| 127 |
|
---|
| 128 | if ((fd= open(poolfile, mode, 0644)) < 0) {
|
---|
| 129 | if (errno != ENOENT) fatal(poolfile);
|
---|
| 130 | return -1;
|
---|
| 131 | }
|
---|
| 132 | if (mode != O_RDONLY) {
|
---|
| 133 | lck.l_type= F_WRLCK;
|
---|
| 134 | lck.l_whence= SEEK_SET;
|
---|
| 135 | lck.l_start= 0;
|
---|
| 136 | lck.l_len= 0;
|
---|
| 137 | if (fcntl(fd, F_SETLKW, &lck) < 0) fatal(poolfile);
|
---|
| 138 | }
|
---|
| 139 | return fd;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | static int readpool(int fd, pool_t *entry)
|
---|
| 143 | {
|
---|
| 144 | /* Read one pool table entry, return true unless EOF. */
|
---|
| 145 | ssize_t r;
|
---|
| 146 |
|
---|
| 147 | if ((r= read(fd, entry, sizeof(*entry))) < 0) fatal(poolfile);
|
---|
| 148 | if (r == 0) return 0;
|
---|
| 149 |
|
---|
| 150 | if (r != sizeof(*entry) || entry->magic != POOL_MAGIC) {
|
---|
| 151 | fprintf(stderr, "%s: %s: Pool table is corrupt\n",
|
---|
| 152 | program, poolfile);
|
---|
| 153 | close(fd);
|
---|
| 154 | return 0;
|
---|
| 155 | }
|
---|
| 156 | return 1;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | #if !__minix_vmd /* No fsync() for Minix. */
|
---|
| 160 | #define fsync(fd) sync()
|
---|
| 161 | #endif
|
---|
| 162 |
|
---|
| 163 | static void writepool(int fd, pool_t *entry)
|
---|
| 164 | {
|
---|
| 165 | /* (Over)write a pool table entry. */
|
---|
| 166 | if (write(fd, entry, sizeof(*entry)) < 0
|
---|
| 167 | || (entry->expire > now && fsync(fd) < 0)
|
---|
| 168 | ) {
|
---|
| 169 | fatal(poolfile);
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | static ipaddr_t findpool(u8_t *client, size_t len, ipaddr_t ifip)
|
---|
| 174 | {
|
---|
| 175 | /* Look for a client ID in the dynamic address pool within the same network
|
---|
| 176 | * as 'ifip'. Select an unused one for a new client if necessary. Return
|
---|
| 177 | * 0 if nothing is available, otherwise the IP address we can offer.
|
---|
| 178 | */
|
---|
| 179 | int fd, found;
|
---|
| 180 | pool_t entry, oldest;
|
---|
| 181 | dhcp_t dhcp;
|
---|
| 182 | u8_t *pmask;
|
---|
| 183 | ipaddr_t mask;
|
---|
| 184 |
|
---|
| 185 | /* Any information available on the network the client is at? */
|
---|
| 186 | if (!makedhcp(&dhcp, nil, 0, nil, 0, ifip, ifip, nil)) return 0;
|
---|
| 187 |
|
---|
| 188 | if ((fd= openpool(O_RDWR)) < 0) return 0;
|
---|
| 189 |
|
---|
| 190 | (void) gettag(&dhcp, DHCP_TAG_NETMASK, &pmask, nil);
|
---|
| 191 | memcpy(&mask, pmask, sizeof(mask));
|
---|
| 192 |
|
---|
| 193 | oldest.expire= NEVER;
|
---|
| 194 | while ((found= readpool(fd, &entry))) {
|
---|
| 195 | /* Deleted entry? */
|
---|
| 196 | if (entry.ip == 0) continue;
|
---|
| 197 |
|
---|
| 198 | /* Correct network? */
|
---|
| 199 | if (((entry.ip ^ ifip) & mask) != 0) continue;
|
---|
| 200 |
|
---|
| 201 | /* Client present? */
|
---|
| 202 | if (entry.len == len && memcmp(entry.clid, client, len) == 0) break;
|
---|
| 203 |
|
---|
| 204 | /* Oldest candidate for a new lease? */
|
---|
| 205 | entry.expire= ntohl(entry.expire);
|
---|
| 206 | if (entry.expire < oldest.expire) oldest= entry;
|
---|
| 207 | }
|
---|
| 208 | close(fd);
|
---|
| 209 |
|
---|
| 210 | if (found) return entry.ip;
|
---|
| 211 | if (oldest.expire <= now) return oldest.ip;
|
---|
| 212 | return 0;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | static int commitpool(ipaddr_t ip, u8_t *client, size_t len, time_t expire)
|
---|
| 216 | {
|
---|
| 217 | /* Commit a new binding to stable storage, return true on success. */
|
---|
| 218 | int fd;
|
---|
| 219 | pool_t entry;
|
---|
| 220 |
|
---|
| 221 | if ((fd= openpool(O_RDWR)) < 0) return 0;
|
---|
| 222 |
|
---|
| 223 | do {
|
---|
| 224 | if (!readpool(fd, &entry)) {
|
---|
| 225 | close(fd);
|
---|
| 226 | return 0;
|
---|
| 227 | }
|
---|
| 228 | } while (entry.ip != ip);
|
---|
| 229 |
|
---|
| 230 | entry.expire= htonl(expire);
|
---|
| 231 | entry.len= len;
|
---|
| 232 | memcpy(entry.clid, client, len);
|
---|
| 233 | if (lseek(fd, -(off_t)sizeof(entry), SEEK_CUR) == -1) fatal(poolfile);
|
---|
| 234 | writepool(fd, &entry);
|
---|
| 235 | close(fd);
|
---|
| 236 | return 1;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | static void updatepool(int add, const char *name)
|
---|
| 240 | {
|
---|
| 241 | /* Add a new IP address to the dynamic pool. */
|
---|
| 242 | ipaddr_t ip;
|
---|
| 243 | int fd, i;
|
---|
| 244 | pool_t entry;
|
---|
| 245 | struct hostent *he;
|
---|
| 246 | off_t off, off0;
|
---|
| 247 |
|
---|
| 248 | if ((he= gethostbyname(name)) == nil || he->h_addrtype != AF_INET) {
|
---|
| 249 | fprintf(stderr, "%s: %s: Unknown host\n", program, name);
|
---|
| 250 | exit(1);
|
---|
| 251 | }
|
---|
| 252 | for (i= 0; he->h_addr_list[i] != nil; i++) {}
|
---|
| 253 | if (i != 1) {
|
---|
| 254 | fprintf(stderr, "%s: %s has %d addresses\n", program, name, i);
|
---|
| 255 | exit(1);
|
---|
| 256 | }
|
---|
| 257 | memcpy(&ip, he->h_addr_list[0], sizeof(ip));
|
---|
| 258 |
|
---|
| 259 | if ((fd= openpool(O_RDWR|O_CREAT)) < 0) fatal(poolfile);
|
---|
| 260 |
|
---|
| 261 | off= 0;
|
---|
| 262 | off0= -1;
|
---|
| 263 | while (readpool(fd, &entry)) {
|
---|
| 264 | if (add) {
|
---|
| 265 | if (entry.ip == ip) {
|
---|
| 266 | fprintf(stderr, "%s: %s: %s is already present\n",
|
---|
| 267 | program, poolfile, name);
|
---|
| 268 | exit(1);
|
---|
| 269 | }
|
---|
| 270 | if (entry.ip == 0 && off0 == -1) off0= off;
|
---|
| 271 | } else {
|
---|
| 272 | if (entry.ip == ip) {
|
---|
| 273 | memset(&entry, 0, sizeof(entry));
|
---|
| 274 | entry.magic= POOL_MAGIC;
|
---|
| 275 | entry.ip= 0;
|
---|
| 276 | if (lseek(fd, off, SEEK_SET) == -1) fatal(poolfile);
|
---|
| 277 | writepool(fd, &entry);
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 | off+= sizeof(entry);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | if (add) {
|
---|
| 284 | if (off0 != -1 && lseek(fd, off0, SEEK_SET) == -1) fatal(poolfile);
|
---|
| 285 | memset(&entry, 0, sizeof(entry));
|
---|
| 286 | entry.magic= POOL_MAGIC;
|
---|
| 287 | entry.ip= ip;
|
---|
| 288 | writepool(fd, &entry);
|
---|
| 289 | }
|
---|
| 290 | close(fd);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | static void cachedhcp(int n, dhcp_t *dp)
|
---|
| 294 | {
|
---|
| 295 | /* Store a DHCP packet in a cache where those who care can find it. */
|
---|
| 296 | static int inited;
|
---|
| 297 | FILE *fp;
|
---|
| 298 | int fd;
|
---|
| 299 | int mode;
|
---|
| 300 |
|
---|
| 301 | if (test > 0) return;
|
---|
| 302 |
|
---|
| 303 | if (!inited) {
|
---|
| 304 | /* First time, clear store and also save my pid. */
|
---|
| 305 | if ((fp= fopen(PATH_DHCPPID, "w")) != nil) {
|
---|
| 306 | if (fprintf(fp, "%d\n", getpid()) == EOF || fclose(fp) == EOF) {
|
---|
| 307 | fatal(PATH_DHCPPID);
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 | inited= 1;
|
---|
| 311 | mode= O_WRONLY | O_CREAT | O_TRUNC;
|
---|
| 312 | } else {
|
---|
| 313 | mode= O_WRONLY;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | dp->xid= htonl(now); /* To tell how old this data is. */
|
---|
| 317 |
|
---|
| 318 | if ((fd= open(cachefile, mode, 0666)) < 0
|
---|
| 319 | || lseek(fd, (off_t) n * sizeof(*dp), SEEK_SET) == -1
|
---|
| 320 | || write(fd, dp, sizeof(*dp)) < 0
|
---|
| 321 | || close(fd) < 0
|
---|
| 322 | ) {
|
---|
| 323 | if (errno != ENOENT) fatal(cachefile);
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | static void printdata(void)
|
---|
| 328 | {
|
---|
| 329 | /* Show the contents of the cache and the dynamic pool. */
|
---|
| 330 | int fd;
|
---|
| 331 | dhcp_t d;
|
---|
| 332 | ssize_t r;
|
---|
| 333 | int i;
|
---|
| 334 | pool_t entry;
|
---|
| 335 | unsigned long expire;
|
---|
| 336 | char delta[3*sizeof(u32_t)];
|
---|
| 337 |
|
---|
| 338 | initdhcpconf();
|
---|
| 339 |
|
---|
| 340 | if ((fd= open(cachefile, O_RDONLY)) < 0) fatal(cachefile);
|
---|
| 341 | i= 0;
|
---|
| 342 | while ((r= read(fd, &d, sizeof(d))) == sizeof(d)) {
|
---|
| 343 | if (d.yiaddr != 0) {
|
---|
| 344 | printf("DHCP data for network %d:\n", i);
|
---|
| 345 | printdhcp(&d);
|
---|
| 346 | }
|
---|
| 347 | i++;
|
---|
| 348 | }
|
---|
| 349 | if (r < 0) fatal(cachefile);
|
---|
| 350 | close(fd);
|
---|
| 351 |
|
---|
| 352 | if ((fd= openpool(O_RDONLY)) >= 0) {
|
---|
| 353 | printf("Dynamic address pool since %ld:\n", (long) now);
|
---|
| 354 | while (readpool(fd, &entry)) {
|
---|
| 355 | if (entry.ip == 0) continue;
|
---|
| 356 | expire= ntohl(entry.expire);
|
---|
| 357 | if (expire == 0) {
|
---|
| 358 | strcpy(delta, "unused");
|
---|
| 359 | } else
|
---|
| 360 | if (expire == 0xFFFFFFFFUL) {
|
---|
| 361 | strcpy(delta, "infinite");
|
---|
| 362 | } else
|
---|
| 363 | if (expire < now) {
|
---|
| 364 | sprintf(delta, "-%lu", now - expire);
|
---|
| 365 | } else {
|
---|
| 366 | sprintf(delta, "+%lu", expire - now);
|
---|
| 367 | }
|
---|
| 368 | printf("\t%-15s %8s ", inet_ntoa(entry.ip), delta);
|
---|
| 369 | for (i= 0; i < entry.len; i++) {
|
---|
| 370 | printf("%02X", entry.clid[i]);
|
---|
| 371 | }
|
---|
| 372 | fputc('\n', stdout);
|
---|
| 373 | }
|
---|
| 374 | close(fd);
|
---|
| 375 | }
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | static udpport_t portbyname(const char *name)
|
---|
| 379 | {
|
---|
| 380 | struct servent *se;
|
---|
| 381 |
|
---|
| 382 | if ((se= getservbyname(name, "udp")) == nil) {
|
---|
| 383 | fprintf(stderr, "%s: Unknown port \"%s\"\n", program, name);
|
---|
| 384 | exit(1);
|
---|
| 385 | }
|
---|
| 386 | return se->s_port;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | static int send(network_t *np, void *data, size_t len)
|
---|
| 390 | {
|
---|
| 391 | /* Send out a packet using a filedescriptor that is probably in async mode,
|
---|
| 392 | * so first dup() a sync version, then write. Return true on success.
|
---|
| 393 | */
|
---|
| 394 | int fd;
|
---|
| 395 | ssize_t r;
|
---|
| 396 |
|
---|
| 397 | if ((fd= dup(np->fdp->fd)) < 0) fatal("Can't dup()");
|
---|
| 398 | if ((r= write(fd, data, len)) < 0) {
|
---|
| 399 | report(np->fdp->device);
|
---|
| 400 | sleep(10);
|
---|
| 401 | }
|
---|
| 402 | close(fd);
|
---|
| 403 | return r >= 0;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | static size_t servdhcp(network_t *np, buf_t *bp, size_t dlen)
|
---|
| 407 | {
|
---|
| 408 | buf_t *abp= nil;
|
---|
| 409 | ipaddr_t cip, ifip;
|
---|
| 410 | u8_t defclid[1+sizeof(bp->dhcp->chaddr)];
|
---|
| 411 | u8_t *pdata, *client, *class, *server, *reqip, *lease;
|
---|
| 412 | u32_t expire;
|
---|
| 413 | size_t len, cilen, calen;
|
---|
| 414 | int type, dyn;
|
---|
| 415 | u8_t atype;
|
---|
| 416 | static char NAKMESS[] = "IP address requested isn't yours";
|
---|
| 417 |
|
---|
| 418 | if (test > 0) return 0;
|
---|
| 419 |
|
---|
| 420 | /* The IP address of the interface close to the client. */
|
---|
| 421 | ifip= bp->dhcp->giaddr != 0 ? bp->dhcp->giaddr : np->ip;
|
---|
| 422 |
|
---|
| 423 | /* All kinds of DHCP tags. */
|
---|
| 424 | if (gettag(bp->dhcp, DHCP_TAG_TYPE, &pdata, nil)) {
|
---|
| 425 | type= *pdata;
|
---|
| 426 | } else {
|
---|
| 427 | type= -1; /* BOOTP? */
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | if (!gettag(bp->dhcp, DHCP_TAG_CLIENTID, &client, &cilen)) {
|
---|
| 431 | defclid[0]= bp->dhcp->htype;
|
---|
| 432 | memcpy(defclid+1, bp->dhcp->chaddr, bp->dhcp->hlen);
|
---|
| 433 | client= defclid;
|
---|
| 434 | cilen= 1+bp->dhcp->hlen;
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | if (!gettag(bp->dhcp, DHCP_TAG_CLASSID, &class, &calen)) {
|
---|
| 438 | calen= 0;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | if (!gettag(bp->dhcp, DHCP_TAG_SERVERID, &server, nil)) {
|
---|
| 442 | server= B(&np->ip);
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | if (!gettag(bp->dhcp, DHCP_TAG_REQIP, &reqip, nil)) {
|
---|
| 446 | reqip= nil;
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | /* I'm a server? Then see if I know this client. */
|
---|
| 450 | if ((np->flags & NF_SERVING)
|
---|
| 451 | && bp->dhcp->op == DHCP_BOOTREQUEST
|
---|
| 452 | && between(1, bp->dhcp->hlen, sizeof(bp->dhcp->chaddr))
|
---|
| 453 | && (server == nil || memcmp(server, &np->ip, sizeof(np->ip)) == 0)
|
---|
| 454 | ) {
|
---|
| 455 | get_buf(&abp);
|
---|
| 456 |
|
---|
| 457 | /* Is the client in my tables? */
|
---|
| 458 | (void) makedhcp(abp->dhcp, class, calen, client, cilen, 0, ifip, nil);
|
---|
| 459 | cip= abp->dhcp->yiaddr;
|
---|
| 460 |
|
---|
| 461 | dyn= 0;
|
---|
| 462 | /* If not, do we have a dynamic address? */
|
---|
| 463 | if (cip == 0 && (cip= findpool(client, cilen, ifip)) != 0) dyn= 1;
|
---|
| 464 |
|
---|
| 465 | if (type == DHCP_INFORM) {
|
---|
| 466 | /* The client already has an address, it just wants information.
|
---|
| 467 | * We only answer if we could answer a normal request (cip != 0),
|
---|
| 468 | * unless configured to answer anyone.
|
---|
| 469 | */
|
---|
| 470 | if (cip != 0 || (np->flags & NF_INFORM)) cip= bp->dhcp->ciaddr;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | if (cip == 0 || !makedhcp(abp->dhcp, class, calen,
|
---|
| 474 | client, cilen, cip, ifip, nil)) {
|
---|
| 475 | put_buf(&abp);
|
---|
| 476 | }
|
---|
| 477 |
|
---|
| 478 | if (abp != nil) {
|
---|
| 479 | if (gettag(abp->dhcp, DHCP_TAG_LEASE, &lease, nil)) {
|
---|
| 480 | memcpy(&expire, lease, sizeof(expire));
|
---|
| 481 | expire= now + ntohl(expire);
|
---|
| 482 | if (expire < now) expire= 0xFFFFFFFFUL;
|
---|
| 483 | } else {
|
---|
| 484 | if (dyn) {
|
---|
| 485 | /* A dynamic address must have a lease. */
|
---|
| 486 | fprintf(stderr, "%s: No lease set for address %s\n",
|
---|
| 487 | program, inet_ntoa(cip));
|
---|
| 488 | exit(1);
|
---|
| 489 | }
|
---|
| 490 | lease= nil;
|
---|
| 491 | expire= 0xFFFFFFFFUL;
|
---|
| 492 | }
|
---|
| 493 |
|
---|
| 494 | /* What does our client want, and what do we say? */
|
---|
| 495 | switch (type) {
|
---|
| 496 | case DHCP_DISCOVER:
|
---|
| 497 | atype= DHCP_OFFER;
|
---|
| 498 |
|
---|
| 499 | /* Assign this address for a short moment. */
|
---|
| 500 | if (dyn && !commitpool(cip, client, cilen, now + DELTA_FAST)) {
|
---|
| 501 | put_buf(&abp);
|
---|
| 502 | }
|
---|
| 503 | break;
|
---|
| 504 |
|
---|
| 505 | case -1:/* BOOTP */
|
---|
| 506 | case DHCP_REQUEST:
|
---|
| 507 | case DHCP_INFORM:
|
---|
| 508 | atype= DHCP_ACK;
|
---|
| 509 | /* The address wanted must be the address we offer. */
|
---|
| 510 | if ((reqip != nil && memcmp(reqip, &cip, sizeof(cip)) != 0)
|
---|
| 511 | || (bp->dhcp->ciaddr != 0 && bp->dhcp->ciaddr != cip)
|
---|
| 512 | ) {
|
---|
| 513 | atype= DHCP_NAK;
|
---|
| 514 | } else
|
---|
| 515 | if (dyn && type == DHCP_REQUEST) {
|
---|
| 516 | /* Assign this address for the duration of the lease. */
|
---|
| 517 | if (!commitpool(cip, client, cilen, expire)) put_buf(&abp);
|
---|
| 518 | }
|
---|
| 519 | break;
|
---|
| 520 |
|
---|
| 521 | case DHCP_DECLINE:
|
---|
| 522 | /* Our client doesn't want the offered address! */
|
---|
| 523 | if (dyn
|
---|
| 524 | && reqip != nil
|
---|
| 525 | && memcmp(reqip, &cip, sizeof(cip)) == 0
|
---|
| 526 | ) {
|
---|
| 527 | int i;
|
---|
| 528 |
|
---|
| 529 | fprintf(stderr, "%s: ", program);
|
---|
| 530 | for (i= 0; i < cilen; i++) {
|
---|
| 531 | fprintf(stderr, "%02X", client[i]);
|
---|
| 532 | }
|
---|
| 533 | fprintf(stderr, " declines %s", inet_ntoa(cip));
|
---|
| 534 | if (gettag(bp->dhcp, DHCP_TAG_MESSAGE, &pdata, &len)) {
|
---|
| 535 | fprintf(stderr, " saying: \"%.*s\"", (int)len, pdata);
|
---|
| 536 | }
|
---|
| 537 | fputc('\n', stderr);
|
---|
| 538 |
|
---|
| 539 | /* Disable address for the duration of the lease. */
|
---|
| 540 | (void) commitpool(cip, nil, 0, expire);
|
---|
| 541 | }
|
---|
| 542 | put_buf(&abp);
|
---|
| 543 | break;
|
---|
| 544 |
|
---|
| 545 | case DHCP_RELEASE:
|
---|
| 546 | /* Our client is nice enough to return its address. */
|
---|
| 547 | if (dyn) (void) commitpool(cip, client, cilen, now);
|
---|
| 548 | put_buf(&abp);
|
---|
| 549 | break;
|
---|
| 550 |
|
---|
| 551 | default: /* Anything else is ignored. */
|
---|
| 552 | put_buf(&abp);
|
---|
| 553 | }
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | if (abp != nil) {
|
---|
| 557 | /* Finish the return packet. */
|
---|
| 558 | abp->dhcp->htype= bp->dhcp->htype;
|
---|
| 559 | abp->dhcp->hlen= bp->dhcp->hlen;
|
---|
| 560 | abp->dhcp->hops= 0;
|
---|
| 561 | abp->dhcp->xid= bp->dhcp->xid;
|
---|
| 562 | abp->dhcp->secs= 0;
|
---|
| 563 | abp->dhcp->flags= bp->dhcp->flags;
|
---|
| 564 | abp->dhcp->ciaddr= 0;
|
---|
| 565 | abp->dhcp->yiaddr= atype == DHCP_NAK ? 0 : cip;
|
---|
| 566 | if (atype == DHCP_NAK) abp->dhcp->siaddr= 0;
|
---|
| 567 | abp->dhcp->giaddr= bp->dhcp->giaddr;
|
---|
| 568 | memcpy(abp->dhcp->chaddr,bp->dhcp->chaddr,sizeof(bp->dhcp->chaddr));
|
---|
| 569 |
|
---|
| 570 | settag(abp->dhcp, DHCP_TAG_SERVERID, &np->ip, sizeof(np->ip));
|
---|
| 571 |
|
---|
| 572 | if (lease == nil) {
|
---|
| 573 | /* No lease specified? Then give an infinite lease. */
|
---|
| 574 | settag(abp->dhcp, DHCP_TAG_LEASE, &expire, sizeof(expire));
|
---|
| 575 | }
|
---|
| 576 |
|
---|
| 577 | if (type == DHCP_INFORM) {
|
---|
| 578 | /* Oops, this one has a fixed address, so no lease business. */
|
---|
| 579 | abp->dhcp->yiaddr= 0;
|
---|
| 580 | settag(abp->dhcp, DHCP_TAG_LEASE, nil, 0);
|
---|
| 581 | settag(abp->dhcp, DHCP_TAG_RENEWAL, nil, 0);
|
---|
| 582 | settag(abp->dhcp, DHCP_TAG_REBINDING, nil, 0);
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | if (atype == DHCP_NAK) {
|
---|
| 586 | /* A NAK doesn't need much. */
|
---|
| 587 | memset(abp->dhcp->sname, 0, sizeof(abp->dhcp->sname));
|
---|
| 588 | memset(abp->dhcp->file, 0, sizeof(abp->dhcp->file));
|
---|
| 589 | memset(abp->dhcp->options, 255, sizeof(abp->dhcp->options));
|
---|
| 590 | settag(abp->dhcp, DHCP_TAG_MESSAGE, NAKMESS, sizeof(NAKMESS));
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | settag(abp->dhcp, DHCP_TAG_TYPE, &atype, sizeof(atype));
|
---|
| 594 |
|
---|
| 595 | /* Figure out where to send this to. */
|
---|
| 596 | abp->udpio->uih_src_addr= np->ip;
|
---|
| 597 | abp->udpio->uih_src_port= port_server;
|
---|
| 598 | if (bp->dhcp->giaddr != 0) {
|
---|
| 599 | abp->udpio->uih_dst_addr= bp->dhcp->giaddr;
|
---|
| 600 | abp->udpio->uih_dst_port= port_server;
|
---|
| 601 | } else
|
---|
| 602 | if (bp->dhcp->flags & DHCP_FLAGS_BCAST) {
|
---|
| 603 | abp->udpio->uih_dst_addr= BCAST_IP;
|
---|
| 604 | abp->udpio->uih_dst_port= port_client;
|
---|
| 605 | } else
|
---|
| 606 | if (bp->udpio->uih_src_addr != 0
|
---|
| 607 | && bp->udpio->uih_dst_addr == np->ip
|
---|
| 608 | ) {
|
---|
| 609 | abp->udpio->uih_dst_addr= bp->udpio->uih_src_addr;
|
---|
| 610 | abp->udpio->uih_dst_port= port_client;
|
---|
| 611 | } else {
|
---|
| 612 | abp->udpio->uih_dst_addr= BCAST_IP;
|
---|
| 613 | abp->udpio->uih_dst_port= port_client;
|
---|
| 614 | }
|
---|
| 615 | abp->udpio->uih_ip_opt_len= 0;
|
---|
| 616 | abp->udpio->uih_data_len= sizeof(dhcp_t);
|
---|
| 617 |
|
---|
| 618 | /* Copy the packet to the input buffer, and return the new size. */
|
---|
| 619 | memcpy(bp->buf, abp->buf, sizeof(bp->buf));
|
---|
| 620 | put_buf(&abp);
|
---|
| 621 | return sizeof(udp_io_hdr_t) + sizeof(dhcp_t);
|
---|
| 622 | }
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | /* I'm a relay? If it is a not already a relayed request then relay. */
|
---|
| 626 | if ((np->flags & NF_RELAYING)
|
---|
| 627 | && bp->dhcp->op == DHCP_BOOTREQUEST
|
---|
| 628 | && bp->dhcp->giaddr == 0
|
---|
| 629 | ) {
|
---|
| 630 | bp->dhcp->giaddr= np->ip;
|
---|
| 631 | bp->udpio->uih_src_addr= np->ip;
|
---|
| 632 | bp->udpio->uih_src_port= port_server;
|
---|
| 633 | bp->udpio->uih_dst_addr= np->server;
|
---|
| 634 | bp->udpio->uih_dst_port= port_server;
|
---|
| 635 | return dlen;
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | /* I'm a relay? If the server sends a reply to me then relay back. */
|
---|
| 639 | if ((np->flags & NF_RELAYING)
|
---|
| 640 | && bp->dhcp->op == DHCP_BOOTREPLY
|
---|
| 641 | && bp->dhcp->giaddr == np->ip
|
---|
| 642 | ) {
|
---|
| 643 | bp->dhcp->giaddr= 0;
|
---|
| 644 | bp->udpio->uih_src_addr= np->ip;
|
---|
| 645 | bp->udpio->uih_src_port= port_server;
|
---|
| 646 | bp->udpio->uih_dst_addr= BCAST_IP;
|
---|
| 647 | bp->udpio->uih_dst_port= port_client;
|
---|
| 648 | return dlen;
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | /* Don't know what to do otherwise, so doing nothing seems wise. */
|
---|
| 652 | return 0;
|
---|
| 653 | }
|
---|
| 654 |
|
---|
| 655 | static void onsig(int sig)
|
---|
| 656 | {
|
---|
| 657 | switch (sig) {
|
---|
| 658 | case SIGUSR1: debug++; break;
|
---|
| 659 | case SIGUSR2: debug= 0; break;
|
---|
| 660 | }
|
---|
| 661 | }
|
---|
| 662 |
|
---|
| 663 | static void usage(void)
|
---|
| 664 | {
|
---|
| 665 | fprintf(stderr,
|
---|
| 666 | "Usage: %s [-qar] [-t[L]] [-d[L]] [-f config] [-c cache] [-p pool] [host ...]\n",
|
---|
| 667 | program);
|
---|
| 668 | exit(1);
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | int main(int argc, char **argv)
|
---|
| 672 | {
|
---|
| 673 | int i;
|
---|
| 674 | network_t *np;
|
---|
| 675 | struct sigaction sa;
|
---|
| 676 | ssize_t r= -1;
|
---|
| 677 | buf_t *bp= nil;
|
---|
| 678 | static struct timeval eventtv;
|
---|
| 679 |
|
---|
| 680 | program= argv[0];
|
---|
| 681 | start= now= time(nil);
|
---|
| 682 |
|
---|
| 683 | debug= 0;
|
---|
| 684 | i= 1;
|
---|
| 685 | while (i < argc && argv[i][0] == '-') {
|
---|
| 686 | char *opt= argv[i++]+1;
|
---|
| 687 |
|
---|
| 688 | if (opt[0] == '-' && opt[1] == 0) break; /* -- */
|
---|
| 689 |
|
---|
| 690 | while (*opt != 0) switch (*opt++) {
|
---|
| 691 | case 'f':
|
---|
| 692 | if (*opt == 0) {
|
---|
| 693 | if (i == argc) usage();
|
---|
| 694 | opt= argv[i++];
|
---|
| 695 | }
|
---|
| 696 | configfile= opt;
|
---|
| 697 | opt= "";
|
---|
| 698 | break;
|
---|
| 699 | case 'c':
|
---|
| 700 | if (*opt == 0) {
|
---|
| 701 | if (i == argc) usage();
|
---|
| 702 | opt= argv[i++];
|
---|
| 703 | }
|
---|
| 704 | cachefile= opt;
|
---|
| 705 | opt= "";
|
---|
| 706 | break;
|
---|
| 707 | case 'p':
|
---|
| 708 | if (*opt == 0) {
|
---|
| 709 | if (i == argc) usage();
|
---|
| 710 | opt= argv[i++];
|
---|
| 711 | }
|
---|
| 712 | poolfile= opt;
|
---|
| 713 | opt= "";
|
---|
| 714 | break;
|
---|
| 715 | case 't':
|
---|
| 716 | test= 1;
|
---|
| 717 | if (between('0', *opt, '9')) test= strtoul(opt, &opt, 10);
|
---|
| 718 | break;
|
---|
| 719 | case 'd':
|
---|
| 720 | debug= 1;
|
---|
| 721 | if (between('0', *opt, '9')) debug= strtoul(opt, &opt, 10);
|
---|
| 722 | break;
|
---|
| 723 | case 'q':
|
---|
| 724 | qflag= 1;
|
---|
| 725 | break;
|
---|
| 726 | case 'a':
|
---|
| 727 | aflag= 1;
|
---|
| 728 | break;
|
---|
| 729 | case 'r':
|
---|
| 730 | rflag= 1;
|
---|
| 731 | break;
|
---|
| 732 | default:
|
---|
| 733 | usage();
|
---|
| 734 | }
|
---|
| 735 | }
|
---|
| 736 | if (aflag + rflag + qflag > 1) usage();
|
---|
| 737 |
|
---|
| 738 | if (aflag || rflag) {
|
---|
| 739 | /* Add or remove addresses from the dynamic pool. */
|
---|
| 740 | while (i < argc) updatepool(aflag, argv[i++]);
|
---|
| 741 | exit(0);
|
---|
| 742 | }
|
---|
| 743 |
|
---|
| 744 | if (i != argc) usage();
|
---|
| 745 |
|
---|
| 746 | if (qflag) {
|
---|
| 747 | /* Only show the contents of the cache and dynamic pool to the user. */
|
---|
| 748 | printdata();
|
---|
| 749 | exit(0);
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | /* BOOTP ports. */
|
---|
| 753 | port_server= portbyname("bootps");
|
---|
| 754 | port_client= portbyname("bootpc");
|
---|
| 755 |
|
---|
| 756 | sa.sa_handler= onsig;
|
---|
| 757 | sigemptyset(&sa.sa_mask);
|
---|
| 758 | sa.sa_flags= 0;
|
---|
| 759 | sigaction(SIGUSR1, &sa, nil);
|
---|
| 760 | sigaction(SIGUSR2, &sa, nil);
|
---|
| 761 |
|
---|
| 762 | /* Initial configuration. */
|
---|
| 763 | for (i= 0; i < N_NETS; i++) {
|
---|
| 764 | int fd;
|
---|
| 765 | ipaddr_t ip, mask;
|
---|
| 766 |
|
---|
| 767 | /* Is there something there? */
|
---|
| 768 | if ((fd= open(ipdev(i), O_RDWR|O_NONBLOCK)) < 0) {
|
---|
| 769 | if (errno != ENOENT && errno != ENODEV && errno != ENXIO) {
|
---|
| 770 | fatal(ipdev(i));
|
---|
| 771 | }
|
---|
| 772 | continue;
|
---|
| 773 | }
|
---|
| 774 | close(fd);
|
---|
| 775 |
|
---|
| 776 | network[n_nets++]= np= newnetwork();
|
---|
| 777 | np->n= i;
|
---|
| 778 |
|
---|
| 779 | /* Ethernet? */
|
---|
| 780 | if (opendev(np, FT_ETHERNET, 1)) {
|
---|
| 781 | np->type= B(&np->eth)[0] != 'Z' ? NT_ETHERNET : NT_SINK;
|
---|
| 782 | if (debug >= 1) {
|
---|
| 783 | printf("%s: Ethernet address is %s%s\n",
|
---|
| 784 | np->fdp->device, ether_ntoa(&np->eth),
|
---|
| 785 | np->type == NT_SINK ? " (sink)" : "");
|
---|
| 786 | }
|
---|
| 787 | closedev(np, FT_ETHERNET);
|
---|
| 788 | }
|
---|
| 789 |
|
---|
| 790 | /* Only true Ethernets worry about DHCP. */
|
---|
| 791 | if (np->type != NT_ETHERNET) np->renew= np->rebind= np->lease= NEVER;
|
---|
| 792 | }
|
---|
| 793 |
|
---|
| 794 | /* Try to find my interfaces in the DHCP table. */
|
---|
| 795 | for (i= 0; i < n_nets; i++) {
|
---|
| 796 | ipaddr_t cip;
|
---|
| 797 | u8_t clid[1+DHCP_HLEN_ETH];
|
---|
| 798 | size_t cilen;
|
---|
| 799 |
|
---|
| 800 | np= network[i];
|
---|
| 801 | if (np->flags & NF_BOUND) continue;
|
---|
| 802 |
|
---|
| 803 | if (np->type == NT_IP) {
|
---|
| 804 | cilen= 0;
|
---|
| 805 | } else {
|
---|
| 806 | ether2clid(clid, &np->eth);
|
---|
| 807 | cilen= 1+DHCP_HLEN_ETH;
|
---|
| 808 | }
|
---|
| 809 |
|
---|
| 810 | /* Try to find an Ethernet address, or the IP address of an already
|
---|
| 811 | * configured network. If we have data we get an IP address back.
|
---|
| 812 | */
|
---|
| 813 | get_buf(&bp);
|
---|
| 814 | (void) makedhcp(bp->dhcp, (u8_t *) "Minix", 5,
|
---|
| 815 | clid, cilen, np->ip, 0, np);
|
---|
| 816 | cip= bp->dhcp->yiaddr;
|
---|
| 817 |
|
---|
| 818 | /* Gather information on the interface. */
|
---|
| 819 | if (cip != 0
|
---|
| 820 | && makedhcp(bp->dhcp, (u8_t *) "Minix", 5,
|
---|
| 821 | clid, cilen, cip, cip, np)
|
---|
| 822 | && test < 2
|
---|
| 823 | ) {
|
---|
| 824 | u8_t *pdata;
|
---|
| 825 | u16_t mtu;
|
---|
| 826 |
|
---|
| 827 | cachedhcp(np->n, bp->dhcp);
|
---|
| 828 | np->ip= cip;
|
---|
| 829 | (void) gettag(bp->dhcp, DHCP_TAG_NETMASK, &pdata, nil);
|
---|
| 830 | memcpy(&np->mask, pdata, sizeof(np->mask));
|
---|
| 831 | if (gettag(bp->dhcp, DHCP_TAG_GATEWAY, &pdata, nil)) {
|
---|
| 832 | memcpy(&np->gateway, pdata, sizeof(np->gateway));
|
---|
| 833 | } else {
|
---|
| 834 | np->gateway= 0;
|
---|
| 835 | }
|
---|
| 836 | if (gettag(bp->dhcp, DHCP_TAG_IPMTU, &pdata, nil)) {
|
---|
| 837 | memcpy(&mtu, pdata, sizeof(mtu));
|
---|
| 838 | mtu= ntohs(mtu);
|
---|
| 839 | } else {
|
---|
| 840 | mtu= 0;
|
---|
| 841 | }
|
---|
| 842 | set_ipconf(ipdev(np->n), np->ip, np->mask, mtu);
|
---|
| 843 | if (debug >= 1) {
|
---|
| 844 | printf("%s: IP address is %s\n",
|
---|
| 845 | ipdev(np->n), cidr_ntoa(np->ip, np->mask));
|
---|
| 846 | }
|
---|
| 847 | np->flags |= NF_BOUND;
|
---|
| 848 | np->renew= np->rebind= np->lease= NEVER;
|
---|
| 849 | np->sol_ct= N_SOLICITS;
|
---|
| 850 | np->solicit= 0;
|
---|
| 851 |
|
---|
| 852 | /* Other (previous) interfaces may have been defined. */
|
---|
| 853 | i= 0;
|
---|
| 854 | }
|
---|
| 855 | put_buf(&bp);
|
---|
| 856 | }
|
---|
| 857 |
|
---|
| 858 | for (;;) {
|
---|
| 859 | now= time(nil);
|
---|
| 860 | event= NEVER;
|
---|
| 861 |
|
---|
| 862 | /* Is it time to request/renew a lease? */
|
---|
| 863 | for (i= 0; i < n_nets; i++) {
|
---|
| 864 | np= network[i];
|
---|
| 865 |
|
---|
| 866 | if (np->renew <= now) {
|
---|
| 867 | u8_t type;
|
---|
| 868 | static u8_t taglist[] = {
|
---|
| 869 | DHCP_TAG_NETMASK, DHCP_TAG_GATEWAY, DHCP_TAG_DNS,
|
---|
| 870 | DHCP_TAG_HOSTNAME
|
---|
| 871 | };
|
---|
| 872 | u8_t ethclid[1+DHCP_HLEN_ETH];
|
---|
| 873 |
|
---|
| 874 | /* We may have lost our binding or even our lease. */
|
---|
| 875 | if (np->rebind <= now) np->server= BCAST_IP;
|
---|
| 876 |
|
---|
| 877 | if (np->lease <= now) {
|
---|
| 878 | if (np->flags & NF_BOUND) closedev(np, FT_ALL);
|
---|
| 879 |
|
---|
| 880 | if ((np->flags & (NF_BOUND | NF_POSSESSIVE)) == NF_BOUND) {
|
---|
| 881 | set_ipconf(ipdev(np->n), np->ip= 0, np->mask= 0, 0);
|
---|
| 882 | if (debug >= 1) {
|
---|
| 883 | printf("%s: Interface disabled (lease expired)\n",
|
---|
| 884 | ipdev(np->n));
|
---|
| 885 | }
|
---|
| 886 | }
|
---|
| 887 | np->flags &= ~NF_BOUND;
|
---|
| 888 | }
|
---|
| 889 |
|
---|
| 890 | /* See if we can open the network we need to send on. */
|
---|
| 891 | if (!(np->flags & NF_BOUND)) {
|
---|
| 892 | if (!opendev(np, FT_ETHERNET, 1)) continue;
|
---|
| 893 | } else {
|
---|
| 894 | if (!opendev(np, FT_BOOTPC, 1)) continue;
|
---|
| 895 | }
|
---|
| 896 |
|
---|
| 897 | if (!(np->flags & NF_NEGOTIATING)) {
|
---|
| 898 | /* We need to start querying a DHCP server. */
|
---|
| 899 | np->start= now;
|
---|
| 900 | np->delta= DELTA_FIRST;
|
---|
| 901 | np->flags |= NF_NEGOTIATING;
|
---|
| 902 | }
|
---|
| 903 |
|
---|
| 904 | /* Fill in a DHCP query packet. */
|
---|
| 905 | get_buf(&bp);
|
---|
| 906 | dhcp_init(bp->dhcp);
|
---|
| 907 | bp->dhcp->op= DHCP_BOOTREQUEST;
|
---|
| 908 | bp->dhcp->htype= DHCP_HTYPE_ETH;
|
---|
| 909 | bp->dhcp->hlen= DHCP_HLEN_ETH;
|
---|
| 910 | bp->dhcp->xid= XID(np);
|
---|
| 911 | bp->dhcp->secs= htons(now - np->start > 0xFFFF
|
---|
| 912 | ? 0xFFFF : now - np->start);
|
---|
| 913 | memcpy(bp->dhcp->chaddr, &np->eth, sizeof(np->eth));
|
---|
| 914 |
|
---|
| 915 | if (np->lease <= now) {
|
---|
| 916 | /* First time, or my old server is unresponsive. */
|
---|
| 917 | type= DHCP_DISCOVER;
|
---|
| 918 | } else {
|
---|
| 919 | /* Request an offered address or renew an address. */
|
---|
| 920 | type= DHCP_REQUEST;
|
---|
| 921 | if (np->flags & NF_BOUND) {
|
---|
| 922 | /* A renewal, I claim my current address. */
|
---|
| 923 | bp->dhcp->ciaddr= np->ip;
|
---|
| 924 | } else {
|
---|
| 925 | /* Nicely ask for the address just offered. */
|
---|
| 926 | settag(bp->dhcp, DHCP_TAG_REQIP, &np->ip,
|
---|
| 927 | sizeof(np->ip));
|
---|
| 928 | settag(bp->dhcp, DHCP_TAG_SERVERID, &np->server,
|
---|
| 929 | sizeof(np->server));
|
---|
| 930 | }
|
---|
| 931 | }
|
---|
| 932 | settag(bp->dhcp, DHCP_TAG_TYPE, &type, 1);
|
---|
| 933 |
|
---|
| 934 | /* My client ID. Simply use the default. */
|
---|
| 935 | ether2clid(ethclid, &np->eth);
|
---|
| 936 | settag(bp->dhcp, DHCP_TAG_CLIENTID, ethclid, sizeof(ethclid));
|
---|
| 937 |
|
---|
| 938 | /* The Class ID may serve to recognize Minix hosts. */
|
---|
| 939 | settag(bp->dhcp, DHCP_TAG_CLASSID, "Minix", 5);
|
---|
| 940 |
|
---|
| 941 | /* The few tags that Minix can make good use of. */
|
---|
| 942 | settag(bp->dhcp, DHCP_TAG_REQPAR, taglist, sizeof(taglist));
|
---|
| 943 |
|
---|
| 944 | /* Some weird sites use a hostname, not a client ID. */
|
---|
| 945 | if (np->hostname != nil) {
|
---|
| 946 | settag(bp->dhcp, DHCP_TAG_HOSTNAME,
|
---|
| 947 | np->hostname, strlen(np->hostname));
|
---|
| 948 | }
|
---|
| 949 |
|
---|
| 950 | bp->udpio->uih_src_addr= np->ip;
|
---|
| 951 | bp->udpio->uih_dst_addr= np->server;
|
---|
| 952 | bp->udpio->uih_src_port= port_client;
|
---|
| 953 | bp->udpio->uih_dst_port= port_server;
|
---|
| 954 | bp->udpio->uih_ip_opt_len= 0;
|
---|
| 955 | bp->udpio->uih_data_len= sizeof(dhcp_t);
|
---|
| 956 |
|
---|
| 957 | if (!(np->flags & NF_BOUND)) {
|
---|
| 958 | /* Rebind over Ethernet. */
|
---|
| 959 | udp2ether(bp, np);
|
---|
| 960 | if (send(np, bp->eth, sizeof(eth_hdr_t) + sizeof(ip_hdr_t)
|
---|
| 961 | + sizeof(udp_hdr_t) + sizeof(dhcp_t))) {
|
---|
| 962 | if (debug >= 1) {
|
---|
| 963 | printf("%s: Broadcast DHCP %s\n",
|
---|
| 964 | np->fdp->device, dhcptypename(type));
|
---|
| 965 | if (debug >= 2) printdhcp(bp->dhcp);
|
---|
| 966 | }
|
---|
| 967 | }
|
---|
| 968 | } else {
|
---|
| 969 | /* Renew over UDP. */
|
---|
| 970 | if (send(np, bp->udpio, sizeof(udp_io_hdr_t)
|
---|
| 971 | + sizeof(dhcp_t))) {
|
---|
| 972 | if (debug >= 1) {
|
---|
| 973 | printf("%s: Sent DHCP %s to %s\n",
|
---|
| 974 | np->fdp->device,
|
---|
| 975 | dhcptypename(type),
|
---|
| 976 | inet_ntoa(np->server));
|
---|
| 977 | if (debug >= 2) printdhcp(bp->dhcp);
|
---|
| 978 | }
|
---|
| 979 | }
|
---|
| 980 | }
|
---|
| 981 | put_buf(&bp);
|
---|
| 982 |
|
---|
| 983 | /* When to continue querying a DHCP server? */
|
---|
| 984 | if (np->flags & NF_BOUND) {
|
---|
| 985 | /* Still bound, keep halving time till next event. */
|
---|
| 986 | time_t e, d;
|
---|
| 987 |
|
---|
| 988 | e= now < np->rebind ? np->rebind : np->lease;
|
---|
| 989 | d= (e - now) / 2;
|
---|
| 990 | if (d < DELTA_SLOW) d= DELTA_SLOW;
|
---|
| 991 | np->renew= now + d;
|
---|
| 992 | if (np->renew > e) np->renew= e;
|
---|
| 993 | } else {
|
---|
| 994 | /* Not bound, be desparate. */
|
---|
| 995 | np->renew= now + np->delta;
|
---|
| 996 | if ((np->delta *= 2) > DELTA_FAST) np->delta= DELTA_FAST;
|
---|
| 997 | }
|
---|
| 998 | }
|
---|
| 999 | if (np->renew < event) event= np->renew;
|
---|
| 1000 | }
|
---|
| 1001 |
|
---|
| 1002 | /* Read DHCP responses. */
|
---|
| 1003 | for (i= 0; i < n_nets; i++) {
|
---|
| 1004 | np= network[i];
|
---|
| 1005 | if (!(np->flags & NF_NEGOTIATING)) continue;
|
---|
| 1006 |
|
---|
| 1007 | if (!(np->flags & NF_BOUND)) {
|
---|
| 1008 | if (!opendev(np, FT_ETHERNET, 0)) continue;
|
---|
| 1009 | get_buf(&np->fdp->bp);
|
---|
| 1010 | r= asyn_read(&asyn, np->fdp->fd, np->fdp->bp->eth,
|
---|
| 1011 | BUF_ETH_SIZE);
|
---|
| 1012 | } else {
|
---|
| 1013 | if (!opendev(np, FT_BOOTPC, 0)) continue;
|
---|
| 1014 | get_buf(&np->fdp->bp);
|
---|
| 1015 | r= asyn_read(&asyn, np->fdp->fd, np->fdp->bp->udpio,
|
---|
| 1016 | BUF_UDP_SIZE);
|
---|
| 1017 | }
|
---|
| 1018 | if (r != -1) break;
|
---|
| 1019 | if (errno != ASYN_INPROGRESS && errno != EPACKSIZE) {
|
---|
| 1020 | report(np->fdp->device);
|
---|
| 1021 | sleep(10);
|
---|
| 1022 | }
|
---|
| 1023 | }
|
---|
| 1024 |
|
---|
| 1025 | /* Is there a response? */
|
---|
| 1026 | if (i < n_nets) {
|
---|
| 1027 | give_buf(&bp, &np->fdp->bp);
|
---|
| 1028 | if (((!(np->flags & NF_BOUND)
|
---|
| 1029 | && r >= (sizeof(eth_hdr_t) + sizeof(ip_hdr_t)
|
---|
| 1030 | + sizeof(udp_hdr_t) + offsetof(dhcp_t, options))
|
---|
| 1031 | && ether2udp(bp)
|
---|
| 1032 | && bp->udpio->uih_dst_port == port_client)
|
---|
| 1033 | ||
|
---|
| 1034 | ((np->flags & NF_BOUND)
|
---|
| 1035 | && r >= sizeof(udp_io_hdr_t) + offsetof(dhcp_t, options)))
|
---|
| 1036 | && bp->dhcp->op == DHCP_BOOTREPLY
|
---|
| 1037 | && bp->dhcp->htype == DHCP_HTYPE_ETH
|
---|
| 1038 | && bp->dhcp->hlen == DHCP_HLEN_ETH
|
---|
| 1039 | && bp->dhcp->xid == XID(np)
|
---|
| 1040 | && memcmp(bp->dhcp->chaddr, &np->eth, sizeof(np->eth)) == 0
|
---|
| 1041 | ) {
|
---|
| 1042 | /* Pfew! We got a DHCP reply! */
|
---|
| 1043 | u8_t *pdata;
|
---|
| 1044 | size_t len;
|
---|
| 1045 | int type;
|
---|
| 1046 | ipaddr_t mask, gateway, relay, server;
|
---|
| 1047 | u16_t mtu;
|
---|
| 1048 | u32_t lease, renew, rebind, t;
|
---|
| 1049 |
|
---|
| 1050 | relay= bp->udpio->uih_src_addr;
|
---|
| 1051 | if (gettag(bp->dhcp, DHCP_TAG_SERVERID, &pdata, nil)) {
|
---|
| 1052 | memcpy(&server, pdata, sizeof(server));
|
---|
| 1053 | } else {
|
---|
| 1054 | server= relay;
|
---|
| 1055 | }
|
---|
| 1056 |
|
---|
| 1057 | if (gettag(bp->dhcp, DHCP_TAG_TYPE, &pdata, nil)) {
|
---|
| 1058 | type= pdata[0];
|
---|
| 1059 | } else {
|
---|
| 1060 | type= DHCP_ACK; /* BOOTP? */
|
---|
| 1061 | }
|
---|
| 1062 |
|
---|
| 1063 | if (debug >= 1) {
|
---|
| 1064 | printf("%s: Got a DHCP %s from %s",
|
---|
| 1065 | np->fdp->device, dhcptypename(type), inet_ntoa(server));
|
---|
| 1066 | printf(relay != server ? " through %s\n" : "\n",
|
---|
| 1067 | inet_ntoa(relay));
|
---|
| 1068 | if (debug >= 2) printdhcp(bp->dhcp);
|
---|
| 1069 | }
|
---|
| 1070 |
|
---|
| 1071 | if (gettag(bp->dhcp, DHCP_TAG_NETMASK, &pdata, nil)) {
|
---|
| 1072 | memcpy(&mask, pdata, sizeof(mask));
|
---|
| 1073 | } else {
|
---|
| 1074 | mask= defaultmask(bp->dhcp->ciaddr);
|
---|
| 1075 | }
|
---|
| 1076 |
|
---|
| 1077 | if (gettag(bp->dhcp, DHCP_TAG_IPMTU, &pdata, nil)) {
|
---|
| 1078 | memcpy(&mtu, pdata, sizeof(mtu));
|
---|
| 1079 | mtu= ntohs(mtu);
|
---|
| 1080 | } else {
|
---|
| 1081 | mtu= 0;
|
---|
| 1082 | }
|
---|
| 1083 |
|
---|
| 1084 | if (gettag(bp->dhcp, DHCP_TAG_GATEWAY, &pdata, nil)) {
|
---|
| 1085 | memcpy(&gateway, pdata, sizeof(gateway));
|
---|
| 1086 | } else {
|
---|
| 1087 | gateway= 0;
|
---|
| 1088 | }
|
---|
| 1089 |
|
---|
| 1090 | lease= NEVER;
|
---|
| 1091 | if (gettag(bp->dhcp, DHCP_TAG_LEASE, &pdata, nil)) {
|
---|
| 1092 | memcpy(&lease, pdata, sizeof(lease));
|
---|
| 1093 | lease= ntohl(lease);
|
---|
| 1094 | }
|
---|
| 1095 |
|
---|
| 1096 | rebind= lease - lease / 8;
|
---|
| 1097 | if (gettag(bp->dhcp, DHCP_TAG_REBINDING, &pdata, nil)) {
|
---|
| 1098 | memcpy(&t, pdata, sizeof(t));
|
---|
| 1099 | t= ntohl(t);
|
---|
| 1100 | if (t < rebind) rebind= t;
|
---|
| 1101 | }
|
---|
| 1102 |
|
---|
| 1103 | renew= lease / 2;
|
---|
| 1104 | if (gettag(bp->dhcp, DHCP_TAG_RENEWAL, &pdata, nil)) {
|
---|
| 1105 | memcpy(&t, pdata, sizeof(t));
|
---|
| 1106 | t= ntohl(t);
|
---|
| 1107 | if (t < renew) renew= t;
|
---|
| 1108 | }
|
---|
| 1109 |
|
---|
| 1110 | if (type == DHCP_OFFER && np->rebind <= np->renew) {
|
---|
| 1111 | /* It's an offer for an address and we haven't taken one
|
---|
| 1112 | * yet. It's all the same to us, so take this one.
|
---|
| 1113 | */
|
---|
| 1114 | np->ip= bp->dhcp->yiaddr;
|
---|
| 1115 | np->mask= mask;
|
---|
| 1116 | np->server= server;
|
---|
| 1117 | np->gateway= gateway;
|
---|
| 1118 | np->delta= DELTA_FIRST;
|
---|
| 1119 | np->renew= now;
|
---|
| 1120 | np->rebind= np->lease= now + DELTA_FAST;
|
---|
| 1121 |
|
---|
| 1122 | /* Send out an ARP request to see if the offered address
|
---|
| 1123 | * is in use already.
|
---|
| 1124 | */
|
---|
| 1125 | make_arp(bp, np);
|
---|
| 1126 | if (send(np, bp->eth, sizeof(arp46_t))) {
|
---|
| 1127 | if (debug >= 2) {
|
---|
| 1128 | printf("Sent ARP for %s\n", inet_ntoa(np->ip));
|
---|
| 1129 | }
|
---|
| 1130 | }
|
---|
| 1131 | np->flags &= ~NF_CONFLICT;
|
---|
| 1132 | }
|
---|
| 1133 |
|
---|
| 1134 | if (type == DHCP_ACK && !(np->flags & NF_CONFLICT)) {
|
---|
| 1135 | /* An acknowledgment. The address is all mine. */
|
---|
| 1136 | cachedhcp(np->n, bp->dhcp);
|
---|
| 1137 | np->ip= bp->dhcp->yiaddr;
|
---|
| 1138 | np->mask= mask;
|
---|
| 1139 | np->server= server;
|
---|
| 1140 | set_ipconf(ipdev(np->n), np->ip, np->mask, mtu);
|
---|
| 1141 | if (debug >= 1) {
|
---|
| 1142 | printf("%s: Address set to %s\n",
|
---|
| 1143 | ipdev(np->n), cidr_ntoa(np->ip, np->mask));
|
---|
| 1144 | }
|
---|
| 1145 | if (lease >= NEVER - now) {
|
---|
| 1146 | /* The lease is infinite! */
|
---|
| 1147 | np->renew= np->rebind= np->lease= NEVER;
|
---|
| 1148 | } else {
|
---|
| 1149 | np->lease= now + lease;
|
---|
| 1150 | np->renew= now + renew;
|
---|
| 1151 | np->rebind= now + rebind;
|
---|
| 1152 | }
|
---|
| 1153 | if (test >= 3) {
|
---|
| 1154 | np->renew= now + 60;
|
---|
| 1155 | np->rebind= test >= 4 ? np->renew : np->renew + 60;
|
---|
| 1156 | np->lease= test >= 5 ? np->rebind : np->rebind + 60;
|
---|
| 1157 | }
|
---|
| 1158 | if (!(np->flags & NF_IRDP)) {
|
---|
| 1159 | np->sol_ct= (np->flags & NF_BOUND) ? 1 : N_SOLICITS;
|
---|
| 1160 | np->solicit= 0;
|
---|
| 1161 | }
|
---|
| 1162 | np->flags &= ~NF_NEGOTIATING;
|
---|
| 1163 | np->flags |= NF_BOUND;
|
---|
| 1164 | closedev(np, FT_ETHERNET);
|
---|
| 1165 | closedev(np, FT_BOOTPC);
|
---|
| 1166 | }
|
---|
| 1167 |
|
---|
| 1168 | if (type == DHCP_ACK && (np->flags & NF_CONFLICT)) {
|
---|
| 1169 | /* Alas there is a conflict. Decline to use the address. */
|
---|
| 1170 | u8_t ethclid[1+DHCP_HLEN_ETH];
|
---|
| 1171 | static char USED[]= "Address in use by 00:00:00:00:00:00";
|
---|
| 1172 |
|
---|
| 1173 | type= DHCP_DECLINE;
|
---|
| 1174 | dhcp_init(bp->dhcp);
|
---|
| 1175 | bp->dhcp->op= DHCP_BOOTREQUEST;
|
---|
| 1176 | bp->dhcp->htype= DHCP_HTYPE_ETH;
|
---|
| 1177 | bp->dhcp->hlen= DHCP_HLEN_ETH;
|
---|
| 1178 | bp->dhcp->xid= XID(np);
|
---|
| 1179 | bp->dhcp->secs= 0;
|
---|
| 1180 | memcpy(bp->dhcp->chaddr, &np->eth, sizeof(np->eth));
|
---|
| 1181 | settag(bp->dhcp, DHCP_TAG_REQIP, &np->ip, sizeof(np->ip));
|
---|
| 1182 | settag(bp->dhcp, DHCP_TAG_TYPE, &type, 1);
|
---|
| 1183 | ether2clid(ethclid, &np->eth);
|
---|
| 1184 | settag(bp->dhcp, DHCP_TAG_CLIENTID,ethclid,sizeof(ethclid));
|
---|
| 1185 | strcpy(USED+18, ether_ntoa(&np->conflict));
|
---|
| 1186 | settag(bp->dhcp, DHCP_TAG_MESSAGE, USED, strlen(USED));
|
---|
| 1187 |
|
---|
| 1188 | bp->udpio->uih_src_port= port_client;
|
---|
| 1189 | bp->udpio->uih_dst_port= port_server;
|
---|
| 1190 | bp->udpio->uih_ip_opt_len= 0;
|
---|
| 1191 | bp->udpio->uih_data_len= sizeof(dhcp_t);
|
---|
| 1192 | udp2ether(bp, np);
|
---|
| 1193 |
|
---|
| 1194 | if (send(np, bp->eth, sizeof(eth_hdr_t) + sizeof(ip_hdr_t)
|
---|
| 1195 | + sizeof(udp_hdr_t) + sizeof(dhcp_t))) {
|
---|
| 1196 | if (debug >= 1) {
|
---|
| 1197 | printf("%s: Broadcast DHCP %s\n",
|
---|
| 1198 | np->fdp->device, dhcptypename(type));
|
---|
| 1199 | if (debug >= 2) printdhcp(bp->dhcp);
|
---|
| 1200 | }
|
---|
| 1201 | }
|
---|
| 1202 |
|
---|
| 1203 | np->renew= np->rebind= np->lease= now + DELTA_FAST;
|
---|
| 1204 | np->delta= DELTA_FIRST;
|
---|
| 1205 | }
|
---|
| 1206 |
|
---|
| 1207 | if (type == DHCP_NAK) {
|
---|
| 1208 | /* Oops, a DHCP server doesn't like me, start over! */
|
---|
| 1209 | np->renew= np->rebind= np->lease= now + DELTA_FAST;
|
---|
| 1210 | np->delta= DELTA_FIRST;
|
---|
| 1211 |
|
---|
| 1212 | fprintf(stderr, "%s: Got a NAK from %s",
|
---|
| 1213 | program, inet_ntoa(server));
|
---|
| 1214 | if (relay != server) {
|
---|
| 1215 | fprintf(stderr, " through %s", inet_ntoa(relay));
|
---|
| 1216 | }
|
---|
| 1217 | if (gettag(bp->dhcp, DHCP_TAG_MESSAGE, &pdata, &len)) {
|
---|
| 1218 | fprintf(stderr, " saying: \"%.*s\"", (int)len, pdata);
|
---|
| 1219 | }
|
---|
| 1220 | fputc('\n', stderr);
|
---|
| 1221 | }
|
---|
| 1222 | } else
|
---|
| 1223 | if (!(np->flags & NF_BOUND)
|
---|
| 1224 | && np->rebind > now
|
---|
| 1225 | && r >= sizeof(arp46_t)
|
---|
| 1226 | && is_arp_me(bp, np)
|
---|
| 1227 | ) {
|
---|
| 1228 | /* Oh no, someone else is using the address offered to me! */
|
---|
| 1229 | np->flags |= NF_CONFLICT;
|
---|
| 1230 |
|
---|
| 1231 | fprintf(stderr, "%s: %s: %s offered by ",
|
---|
| 1232 | program,
|
---|
| 1233 | np->fdp->device,
|
---|
| 1234 | inet_ntoa(np->ip));
|
---|
| 1235 | fprintf(stderr, "%s is already in use by %s\n",
|
---|
| 1236 | inet_ntoa(np->server),
|
---|
| 1237 | ether_ntoa(&np->conflict));
|
---|
| 1238 | }
|
---|
| 1239 | put_buf(&bp);
|
---|
| 1240 | if (np->renew < event) event= np->renew;
|
---|
| 1241 | }
|
---|
| 1242 |
|
---|
| 1243 | /* Perform router solicitations. */
|
---|
| 1244 | for (i= 0; i < n_nets; i++) {
|
---|
| 1245 | np= network[i];
|
---|
| 1246 | if (!(np->flags & NF_BOUND)) continue;
|
---|
| 1247 |
|
---|
| 1248 | if (np->solicit <= now) {
|
---|
| 1249 | if (!opendev(np, FT_ICMP, 1)) continue;
|
---|
| 1250 | np->solicit= NEVER;
|
---|
| 1251 |
|
---|
| 1252 | get_buf(&bp);
|
---|
| 1253 | if (np->gateway != 0) {
|
---|
| 1254 | /* No IRDP response seen yet, advertise the router given
|
---|
| 1255 | * by DHCP to my own interface.
|
---|
| 1256 | */
|
---|
| 1257 | icmp_advert(bp, np);
|
---|
| 1258 | if (send(np, bp->ip, sizeof(ip_hdr_t) + 16)) {
|
---|
| 1259 | if (debug >= 2) {
|
---|
| 1260 | printf("%s: Sent advert for %s to self\n",
|
---|
| 1261 | np->fdp->device, inet_ntoa(np->gateway));
|
---|
| 1262 | }
|
---|
| 1263 | }
|
---|
| 1264 | np->solicit= now + DELTA_ADV/2;
|
---|
| 1265 | }
|
---|
| 1266 |
|
---|
| 1267 | if (np->sol_ct >= 0 && --np->sol_ct >= 0) {
|
---|
| 1268 | /* Send a router solicitation. */
|
---|
| 1269 | icmp_solicit(bp);
|
---|
| 1270 | if (send(np, bp->ip, sizeof(*bp->ip) + 8)) {
|
---|
| 1271 | if (debug >= 2) {
|
---|
| 1272 | printf("%s: Broadcast router solicitation\n",
|
---|
| 1273 | np->fdp->device);
|
---|
| 1274 | }
|
---|
| 1275 | }
|
---|
| 1276 | np->solicit= now + DELTA_SOL;
|
---|
| 1277 | } else {
|
---|
| 1278 | /* No response, or not soliciting right now. */
|
---|
| 1279 | closedev(np, FT_ICMP);
|
---|
| 1280 | }
|
---|
| 1281 |
|
---|
| 1282 | put_buf(&bp);
|
---|
| 1283 | }
|
---|
| 1284 | if (np->solicit < event) event= np->solicit;
|
---|
| 1285 | }
|
---|
| 1286 |
|
---|
| 1287 | /* Read router adverts. */
|
---|
| 1288 | for (i= 0; i < n_nets; i++) {
|
---|
| 1289 | np= network[i];
|
---|
| 1290 | if (!(np->flags & NF_BOUND)) continue;
|
---|
| 1291 | if (np->sol_ct < 0) continue;
|
---|
| 1292 |
|
---|
| 1293 | if (!opendev(np, FT_ICMP, 0)) continue;
|
---|
| 1294 | get_buf(&np->fdp->bp);
|
---|
| 1295 | r= asyn_read(&asyn, np->fdp->fd, np->fdp->bp->ip, BUF_IP_SIZE);
|
---|
| 1296 | if (r != -1) break;
|
---|
| 1297 | if (errno != ASYN_INPROGRESS && errno != EPACKSIZE) {
|
---|
| 1298 | report(np->fdp->device);
|
---|
| 1299 | sleep(10);
|
---|
| 1300 | }
|
---|
| 1301 | }
|
---|
| 1302 |
|
---|
| 1303 | /* Is there an advert? */
|
---|
| 1304 | if (i < n_nets && r >= sizeof(ip_hdr_t) + 8) {
|
---|
| 1305 | ipaddr_t router;
|
---|
| 1306 |
|
---|
| 1307 | give_buf(&bp, &np->fdp->bp);
|
---|
| 1308 | if ((router= icmp_is_advert(bp)) != 0) {
|
---|
| 1309 | if (debug >= 2) {
|
---|
| 1310 | printf("%s: Router advert received from %s\n",
|
---|
| 1311 | np->fdp->device, inet_ntoa(router));
|
---|
| 1312 | }
|
---|
| 1313 | np->solicit= NEVER;
|
---|
| 1314 | np->sol_ct= -1;
|
---|
| 1315 | np->flags |= NF_IRDP;
|
---|
| 1316 | closedev(np, FT_ICMP);
|
---|
| 1317 | }
|
---|
| 1318 | put_buf(&bp);
|
---|
| 1319 | }
|
---|
| 1320 |
|
---|
| 1321 | /* We start serving if all the interfaces so marked are configured. */
|
---|
| 1322 | for (i= 0; i < n_nets; i++) {
|
---|
| 1323 | np= network[i];
|
---|
| 1324 | if ((np->flags & NF_RELAYING) && (np->flags & NF_BOUND)) {
|
---|
| 1325 | if (((np->ip ^ np->server) & np->mask) == 0) {
|
---|
| 1326 | /* Don't relay to a server that is on this same net. */
|
---|
| 1327 | np->flags &= ~NF_RELAYING;
|
---|
| 1328 | }
|
---|
| 1329 | }
|
---|
| 1330 | if (!(np->flags & (NF_SERVING|NF_RELAYING))) continue;
|
---|
| 1331 | if (!(np->flags & NF_BOUND)) { serving= 0; break; }
|
---|
| 1332 | serving= 1;
|
---|
| 1333 | }
|
---|
| 1334 |
|
---|
| 1335 | /* Read DHCP requests. */
|
---|
| 1336 | for (i= 0; i < n_nets; i++) {
|
---|
| 1337 | np= network[i];
|
---|
| 1338 | if (!(np->flags & NF_BOUND)) continue;
|
---|
| 1339 | if (!(np->flags & (NF_SERVING|NF_RELAYING)) || !serving) continue;
|
---|
| 1340 |
|
---|
| 1341 | if (!opendev(np, FT_BOOTPS, 0)) continue;
|
---|
| 1342 | get_buf(&np->fdp->bp);
|
---|
| 1343 | r= asyn_read(&asyn, np->fdp->fd, np->fdp->bp->udpio, BUF_UDP_SIZE);
|
---|
| 1344 |
|
---|
| 1345 | if (r != -1) break;
|
---|
| 1346 | if (errno != ASYN_INPROGRESS && errno != EPACKSIZE) {
|
---|
| 1347 | report(np->fdp->device);
|
---|
| 1348 | sleep(10);
|
---|
| 1349 | }
|
---|
| 1350 | }
|
---|
| 1351 |
|
---|
| 1352 | /* Is there a request? */
|
---|
| 1353 | if (i < n_nets
|
---|
| 1354 | && r >= sizeof(udp_io_hdr_t) + offsetof(dhcp_t, options)
|
---|
| 1355 | ) {
|
---|
| 1356 | give_buf(&bp, &np->fdp->bp);
|
---|
| 1357 |
|
---|
| 1358 | if (debug >= 1) {
|
---|
| 1359 | printf("%s: Got DHCP packet from %s to ",
|
---|
| 1360 | np->fdp->device, inet_ntoa(bp->udpio->uih_src_addr));
|
---|
| 1361 | printf("%s\n", inet_ntoa(bp->udpio->uih_dst_addr));
|
---|
| 1362 | if (debug >= 2) printdhcp(bp->dhcp);
|
---|
| 1363 | }
|
---|
| 1364 |
|
---|
| 1365 | /* Can we do something with this DHCP packet? */
|
---|
| 1366 | if ((r= servdhcp(np, bp, r)) > 0) {
|
---|
| 1367 | /* Yes, we have something to send somewhere. */
|
---|
| 1368 | if (send(np, bp->udpio, r)) {
|
---|
| 1369 | if (debug >= 1) {
|
---|
| 1370 | printf("%s: Sent DHCP packet to %s\n",
|
---|
| 1371 | np->fdp->device,
|
---|
| 1372 | inet_ntoa(bp->udpio->uih_dst_addr));
|
---|
| 1373 | if (debug >= 2) printdhcp(bp->dhcp);
|
---|
| 1374 | }
|
---|
| 1375 | }
|
---|
| 1376 | }
|
---|
| 1377 | put_buf(&bp);
|
---|
| 1378 | }
|
---|
| 1379 |
|
---|
| 1380 | if (debug >= 1) {
|
---|
| 1381 | static char *lastbrk;
|
---|
| 1382 | extern char _end;
|
---|
| 1383 |
|
---|
| 1384 | if (sbrk(0) != lastbrk) {
|
---|
| 1385 | lastbrk= sbrk(0);
|
---|
| 1386 | printf("Memory use = %lu\n",
|
---|
| 1387 | (unsigned long) (lastbrk - &_end));
|
---|
| 1388 | }
|
---|
| 1389 | fflush(stdout);
|
---|
| 1390 | }
|
---|
| 1391 |
|
---|
| 1392 | /* Bail out if not a server, and there is nothing else to do ever. */
|
---|
| 1393 | if (!serving && event == NEVER) break;
|
---|
| 1394 |
|
---|
| 1395 | /* Wait for something to do. */
|
---|
| 1396 | eventtv.tv_sec= event;
|
---|
| 1397 | if (asyn_wait(&asyn, 0, event == NEVER ? nil : &eventtv) < 0) {
|
---|
| 1398 | if (errno != EINTR) {
|
---|
| 1399 | report("asyn_wait()");
|
---|
| 1400 | sleep(10);
|
---|
| 1401 | }
|
---|
| 1402 | }
|
---|
| 1403 | }
|
---|
| 1404 | if (debug >= 1) printf("Nothing more to do! Bailing out...\n");
|
---|
| 1405 | return 0;
|
---|
| 1406 | }
|
---|