Blob


1 #include <u.h>
2 #include <sys/socket.h>
3 #include <net/if_arp.h>
4 #include <netinet/ip.h>
5 #include <sys/ioctl.h>
6 #include <libc.h>
7 #include <ip.h>
8 #include <bio.h>
9 #include <ndb.h>
10 #include "dat.h"
12 int bwfd;
13 int wfd;
15 //
16 // ala rfc2131
17 //
19 typedef struct Req Req;
20 struct Req
21 {
22 int fd; /* for reply */
23 Bootp *bp;
24 Udphdr uh;
25 Udphdr *up;
26 uchar *e; /* end of received message */
27 uchar *p; /* options pointer */
28 uchar *max; /* max end of reply */
30 /* expanded to v6 */
31 uchar ciaddr[IPaddrlen];
32 uchar giaddr[IPaddrlen];
34 /* parsed options */
35 int p9request; /* true if this is a bootp with plan9 options */
36 int genrequest; /* true if this is a bootp with generic options */
37 int dhcptype; /* dhcp message type */
38 int leasetime; /* dhcp lease */
39 uchar ip[IPaddrlen]; /* requested address */
40 uchar server[IPaddrlen]; /* server address */
41 char msg[ERRMAX]; /* error message */
42 char vci[32]; /* vendor class id */
43 char *id; /* client id */
44 uchar requested[32]; /* requested params */
45 uchar vendorclass[32];
46 char cputype[32-3];
48 Info gii; /* about target network */
49 Info ii; /* about target system */
50 int staticbinding;
52 uchar buf[2*1024]; /* message buffer */
53 };
55 #define TFTP "/lib/tftpd"
56 char *blog = "ipboot";
57 char mysysname[64];
58 Ipifc *ipifcs;
59 int debug;
60 int nobootp;
61 long now;
62 int slow;
63 char net[256];
64 uchar xmyipaddr[IPaddrlen];
66 int pptponly; // only answer request that came from the pptp server
67 int mute;
68 int minlease = MinLease;
70 ulong start;
72 /* option magic */
73 char plan9opt[4] = { 'p', '9', ' ', ' ' };
74 char genericopt[4] = { 0x63, 0x82, 0x53, 0x63 };
76 /* well known addresses */
77 uchar zeros[Maxhwlen];
79 /* option debug buffer */
80 char optbuf[1024];
81 char *op;
82 char *oe = optbuf + sizeof(optbuf);
84 char *optname[256] =
85 {
86 [OBend] "end",
87 [OBpad] "pad",
88 [OBmask] "mask",
89 [OBtimeoff] "timeoff",
90 [OBrouter] "router",
91 [OBtimeserver] "time",
92 [OBnameserver] "name",
93 [OBdnserver] "dns",
94 [OBlogserver] "log",
95 [OBcookieserver] "cookie",
96 [OBlprserver] "lpr",
97 [OBimpressserver] "impress",
98 [OBrlserver] "rl",
99 [OBhostname] "host",
100 [OBbflen] "bflen",
101 [OBdumpfile] "dumpfile",
102 [OBdomainname] "dom",
103 [OBswapserver] "swap",
104 [OBrootpath] "rootpath",
105 [OBextpath] "extpath",
106 [OBipforward] "ipforward",
107 [OBnonlocal] "nonlocal",
108 [OBpolicyfilter] "policyfilter",
109 [OBmaxdatagram] "maxdatagram",
110 [OBttl] "ttl",
111 [OBpathtimeout] "pathtimeout",
112 [OBpathplateau] "pathplateau",
113 [OBmtu] "mtu",
114 [OBsubnetslocal] "subnetslocal",
115 [OBbaddr] "baddr",
116 [OBdiscovermask] "discovermask",
117 [OBsupplymask] "supplymask",
118 [OBdiscoverrouter] "discoverrouter",
119 [OBrsserver] "rsserver",
120 [OBstaticroutes] "staticroutes",
121 [OBtrailerencap] "trailerencap",
122 [OBarptimeout] "arptimeout",
123 [OBetherencap] "etherencap",
124 [OBtcpttl] "tcpttl",
125 [OBtcpka] "tcpka",
126 [OBtcpkag] "tcpkag",
127 [OBnisdomain] "nisdomain",
128 [OBniserver] "niserver",
129 [OBntpserver] "ntpserver",
130 [OBvendorinfo] "vendorinfo",
131 [OBnetbiosns] "NBns",
132 [OBnetbiosdds] "NBdds",
133 [OBnetbiostype] "NBtype",
134 [OBnetbiosscope] "NBscope",
135 [OBxfontserver] "xfont",
136 [OBxdispmanager] "xdisp",
137 [OBnisplusdomain] "NPdomain",
138 [OBnisplusserver] "NP",
139 [OBhomeagent] "homeagent",
140 [OBsmtpserver] "smtp",
141 [OBpop3server] "pop3",
142 [OBnntpserver] "nntp",
143 [OBwwwserver] "www",
144 [OBfingerserver] "finger",
145 [OBircserver] "ircserver",
146 [OBstserver] "stserver",
147 [OBstdaserver] "stdaserver",
149 /* dhcp options */
150 [ODipaddr] "ip",
151 [ODlease] "leas",
152 [ODoverload] "overload",
153 [ODtype] "typ",
154 [ODserverid] "sid",
155 [ODparams] "params",
156 [ODmessage] "message",
157 [ODmaxmsg] "maxmsg",
158 [ODrenewaltime] "renewaltime",
159 [ODrebindingtime] "rebindingtime",
160 [ODvendorclass] "vendorclass",
161 [ODclientid] "cid",
162 [ODtftpserver] "tftpserver",
163 [ODbootfile] "bf",
164 };
166 void addropt(Req*, int, uchar*);
167 void addrsopt(Req*, int, uchar**, int);
168 void arpenter(uchar*, uchar*);
169 void bootp(Req*);
170 void byteopt(Req*, int, uchar);
171 void dhcp(Req*);
172 void fatal(int, char*, ...);
173 void hexopt(Req*, int, char*);
174 void longopt(Req*, int, long);
175 void maskopt(Req*, int, uchar*);
176 void miscoptions(Req*, uchar*);
177 int openlisten(char *net);
178 void parseoptions(Req*);
179 void proto(Req*, int);
180 void rcvdecline(Req*);
181 void rcvdiscover(Req*);
182 void rcvinform(Req*);
183 void rcvrelease(Req*);
184 void rcvrequest(Req*);
185 char* readsysname(void);
186 void remrequested(Req*, int);
187 void sendack(Req*, uchar*, int, int);
188 void sendnak(Req*, char*);
189 void sendoffer(Req*, uchar*, int);
190 void stringopt(Req*, int, char*);
191 void termopt(Req*);
192 int validip(uchar*);
193 void vectoropt(Req*, int, uchar*, int);
194 void warning(int, char*, ...);
195 void logdhcp(Req*);
196 void logdhcpout(Req *, char *);
197 int readlast(int, Udphdr*, uchar*, int);
199 void
200 timestamp(char *tag)
202 ulong t;
204 t = nsec()/1000;
205 syslog(0, blog, "%s %lud", tag, t - start);
208 void
209 usage(void)
211 fprint(2, "usage: dhcp [-dmsnp] [-f directory] [-x netmtpt] [-M minlease] addr n [addr n ...]\n");
212 exits("usage");
215 void
216 main(int argc, char **argv)
218 int i, n, fd;
219 char *p;
220 uchar ip[IPaddrlen];
221 Req r;
223 fmtinstall('E', eipfmt);
224 fmtinstall('I', eipfmt);
225 fmtinstall('V', eipfmt);
226 fmtinstall('M', eipfmt);
227 ARGBEGIN {
228 case 'm':
229 mute = 1;
230 break;
231 case 'd':
232 debug = 1;
233 break;
234 case 'f':
235 p = ARGF();
236 if(p == nil)
237 usage();
238 ndbfile = p;
239 break;
240 case 's':
241 slow = 1;
242 break;
243 case 'n':
244 nobootp = 1;
245 break;
246 case 'i':
247 parseip(xmyipaddr,EARGF(usage()));
248 break;
249 case 'p':
250 pptponly = 1;
251 break;
252 case 'M':
253 p = ARGF();
254 if(p == nil)
255 usage();
256 minlease = atoi(p);
257 if(minlease <= 0)
258 minlease = MinLease;
259 break;
260 } ARGEND;
262 while(argc > 1){
263 parseip(ip, argv[0]);
264 if(!validip(ip))
265 usage();
266 n = atoi(argv[1]);
267 if(n <= 0)
268 usage();
269 initbinding(ip, n);
270 argc -= 2;
271 argv += 2;
274 /* for debugging */
275 for(i = 0; i < 256; i++)
276 if(optname[i] == 0)
277 optname[i] = smprint("%d", i);
279 /* what is my name? */
280 p = readsysname();
281 strcpy(mysysname, p);
283 /* put process in background */
284 if(!debug) switch(rfork(RFNOTEG|RFPROC|RFFDG)) {
285 case -1:
286 fatal(1, "fork");
287 case 0:
288 break;
289 default:
290 exits(0);
293 chdir(TFTP);
294 fd = openlisten(net);
295 wfd = fd;
296 bwfd = fd;
297 if(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5) < 0)
298 print("setsockopt: %r\n");
300 for(;;){
301 memset(&r, 0, sizeof(r));
302 r.fd = fd;
303 n = readlast(fd, &r.uh, r.buf, sizeof(r.buf));
304 if(n < 0)
305 fatal(1, "error reading requests");
306 start = nsec()/1000;
307 op = optbuf;
308 *op = 0;
309 proto(&r, n);
310 if(r.id != nil)
311 free(r.id);
315 void
316 proto(Req *rp, int n)
318 uchar relip[IPaddrlen];
319 char buf[64];
321 now = time(0);
323 rp->e = rp->buf + n;
324 rp->bp = (Bootp*)rp->buf;
325 rp->up = &rp->uh;
326 rp->max = rp->buf + MINSUPPORTED - IPUDPHDRSIZE;
327 rp->p = rp->bp->optdata;
328 v4tov6(rp->giaddr, rp->bp->giaddr);
329 v4tov6(rp->ciaddr, rp->bp->ciaddr);
331 if(pptponly && rp->bp->htype != 0)
332 return;
334 ipifcs = readipifc(net, ipifcs, -1);
335 if(validip(rp->giaddr))
336 ipmove(relip, rp->giaddr);
337 else if(validip(rp->up->raddr))
338 ipmove(relip, rp->up->raddr);
339 else
340 ipmove(relip, xmyipaddr);
341 ipmove(rp->up->laddr, xmyipaddr);
342 if(rp->e < (uchar*)rp->bp->sname){
343 warning(0, "packet too short");
344 return;
346 if(rp->bp->op != Bootrequest){
347 warning(0, "not bootrequest");
348 return;
351 if(rp->e >= rp->bp->optdata){
352 if(memcmp(rp->bp->optmagic, plan9opt, sizeof(rp->bp->optmagic)) == 0)
353 rp->p9request = 1;
354 if(memcmp(rp->bp->optmagic, genericopt, sizeof(rp->bp->optmagic)) == 0) {
355 rp->genrequest = 1;
356 parseoptions(rp);
359 rp->p = rp->bp->optdata;
361 /* If no id is specified, make one from the hardware address
362 * of the target. We assume all zeros is not a hardware address
363 * which could be a mistake.
364 */
365 if(rp->id == nil){
366 if(rp->bp->hlen > Maxhwlen){
367 warning(0, "hlen %d", rp->bp->hlen);
368 return;
370 if(memcmp(zeros, rp->bp->chaddr, rp->bp->hlen) == 0){
371 warning(0, "no chaddr");
372 return;
374 sprint(buf, "hwa%2.2ux_", rp->bp->htype);
375 rp->id = tohex(buf, rp->bp->chaddr, rp->bp->hlen);
378 /* info about gateway */
379 if(lookupip(relip, &rp->gii, 1) < 0){
380 warning(0, "lookupip failed");
381 return;
384 /* info about target system */
385 if(lookup(rp->bp, &rp->ii, &rp->gii) == 0)
386 if(rp->ii.indb && rp->ii.dhcpgroup[0] == 0)
387 rp->staticbinding = 1;
389 if(rp->dhcptype)
390 dhcp(rp);
391 else
392 bootp(rp);
393 timestamp("done");
396 void
397 dhcp(Req *rp)
399 logdhcp(rp);
401 switch(rp->dhcptype){
402 case Discover:
403 if(slow)
404 sleep(500);
405 rcvdiscover(rp);
406 break;
407 case Request:
408 rcvrequest(rp);
409 break;
410 case Decline:
411 rcvdecline(rp);
412 break;
413 case Release:
414 rcvrelease(rp);
415 break;
416 case Inform:
417 rcvinform(rp);
418 break;
422 void
423 rcvdiscover(Req *rp)
425 Binding *b, *nb;
427 if(rp->staticbinding){
428 sendoffer(rp, rp->ii.ipaddr, StaticLease);
429 return;
432 /*
433 * first look for an outstanding offer
434 */
435 b = idtooffer(rp->id, &rp->gii);
437 /*
438 * rfc2131 says:
439 * If an address is available, the new address
440 * SHOULD be chosen as follows:
442 * o The client's current address as recorded in the client's current
443 * binding, ELSE
445 * o The client's previous address as recorded in the client's (now
446 * expired or released) binding, if that address is in the server's
447 * pool of available addresses and not already allocated, ELSE
449 * o The address requested in the 'Requested IP Address' option, if that
450 * address is valid and not already allocated, ELSE
452 * o A new address allocated from the server's pool of available
453 * addresses; the address is selected based on the subnet from which
454 * the message was received (if 'giaddr' is 0) or on the address of
455 * the relay agent that forwarded the message ('giaddr' when not 0).
456 */
457 if(b == nil){
458 b = idtobinding(rp->id, &rp->gii, 1);
459 if(b && b->boundto && strcmp(b->boundto, rp->id) != 0)
460 if(validip(rp->ip) && samenet(rp->ip, &rp->gii)){
461 nb = iptobinding(rp->ip, 0);
462 if(nb && nb->lease < now)
463 b = nb;
466 if(b == nil){
467 warning(0, "!Discover(%s via %I): no binding %I",
468 rp->id, rp->gii.ipaddr, rp->ip);
469 return;
471 mkoffer(b, rp->id, rp->leasetime);
472 sendoffer(rp, b->ip, b->offer);
475 void
476 rcvrequest(Req *rp)
478 Binding *b;
480 if(validip(rp->server)){
481 /* this is a reply to an offer - SELECTING */
483 /* check for hard assignment */
484 if(rp->staticbinding){
485 if(forme(rp->server))
486 sendack(rp, rp->ii.ipaddr, StaticLease, 1);
487 else
488 warning(0, "!Request(%s via %I): for server %I not me",
489 rp->id, rp->gii.ipaddr, rp->server);
490 return;
493 b = idtooffer(rp->id, &rp->gii);
495 /* if we don't have an offer, nak */
496 if(b == nil){
497 warning(0, "!Request(%s via %I): no offer",
498 rp->id, rp->gii.ipaddr);
499 if(forme(rp->server))
500 sendnak(rp, "no offer for you");
501 return;
504 /* if not for me, retract offer */
505 if(!forme(rp->server)){
506 b->expoffer = 0;
507 warning(0, "!Request(%s via %I): for server %I not me",
508 rp->id, rp->gii.ipaddr, rp->server);
509 return;
512 /*
513 * if the client is confused about what we offered, nak.
514 * client really shouldn't be specifying this when selecting
515 */
516 if(validip(rp->ip) && ipcmp(rp->ip, b->ip) != 0){
517 warning(0, "!Request(%s via %I): requests %I, not %I",
518 rp->id, rp->gii.ipaddr, rp->ip, b->ip);
519 sendnak(rp, "bad ip address option");
520 return;
522 if(commitbinding(b) < 0){
523 warning(0, "!Request(%s via %I): can't commit %I",
524 rp->id, rp->gii.ipaddr, b->ip);
525 sendnak(rp, "can't commit binding");
526 return;
528 sendack(rp, b->ip, b->offer, 1);
529 } else if(validip(rp->ip)){
530 /*
531 * checking address/net - INIT-REBOOT
533 * This is a rebooting client that remembers its old
534 * address.
535 */
536 /* check for hard assignment */
537 if(rp->staticbinding){
538 if(memcmp(rp->ip, rp->ii.ipaddr, IPaddrlen) != 0){
539 warning(0, "!Request(%s via %I): %I not valid for %E",
540 rp->id, rp->gii.ipaddr, rp->ip, rp->bp->chaddr);
541 sendnak(rp, "not valid");
543 sendack(rp, rp->ii.ipaddr, StaticLease, 1);
544 return;
547 /* make sure the network makes sense */
548 if(!samenet(rp->ip, &rp->gii)){
549 warning(0, "!Request(%s via %I): bad forward of %I",
550 rp->id, rp->gii.ipaddr, rp->ip);
551 sendnak(rp, "wrong network");
552 return;
554 b = iptobinding(rp->ip, 0);
555 if(b == nil){
556 warning(0, "!Request(%s via %I): no binding for %I for",
557 rp->id, rp->gii.ipaddr, rp->ip);
558 return;
560 if(memcmp(rp->ip, b->ip, IPaddrlen) != 0 || now > b->lease){
561 warning(0, "!Request(%s via %I): %I not valid",
562 rp->id, rp->gii.ipaddr, rp->ip);
563 sendnak(rp, "not valid");
564 return;
566 b->offer = b->lease - now;
567 sendack(rp, b->ip, b->offer, 1);
568 } else if(validip(rp->ciaddr)){
569 /*
570 * checking address - RENEWING or REBINDING
572 * these states are indistinguishable in our action. The only
573 * difference is how close to lease expiration the client is.
574 * If it is really close, it broadcasts the request hoping that
575 * some server will answer.
576 */
578 /* check for hard assignment */
579 if(rp->staticbinding){
580 if(ipcmp(rp->ciaddr, rp->ii.ipaddr) != 0){
581 warning(0, "!Request(%s via %I): %I not valid",
582 rp->id, rp->gii.ipaddr, rp->ciaddr);
583 sendnak(rp, "not valid");
585 sendack(rp, rp->ii.ipaddr, StaticLease, 1);
586 return;
589 /* make sure the network makes sense */
590 if(!samenet(rp->ciaddr, &rp->gii)){
591 warning(0, "!Request(%s via %I): bad forward of %I",
592 rp->id, rp->gii.ipaddr, rp->ip);
593 sendnak(rp, "wrong network");
594 return;
596 b = iptobinding(rp->ciaddr, 0);
597 if(b == nil){
598 warning(0, "!Request(%s via %I): no binding for %I",
599 rp->id, rp->gii.ipaddr, rp->ciaddr);
600 return;
602 if(ipcmp(rp->ciaddr, b->ip) != 0){
603 warning(0, "!Request(%I via %s): %I not valid",
604 rp->id, rp->gii.ipaddr, rp->ciaddr);
605 sendnak(rp, "invalid ip address");
606 return;
608 mkoffer(b, rp->id, rp->leasetime);
609 if(commitbinding(b) < 0){
610 warning(0, "!Request(%s via %I): can't commit %I",
611 rp->id, rp->gii.ipaddr, b->ip);
612 sendnak(rp, "can't commit binding");
613 return;
615 sendack(rp, b->ip, b->offer, 1);
619 void
620 rcvdecline(Req *rp)
622 Binding *b;
623 char buf[64];
625 if(rp->staticbinding)
626 return;
628 b = idtooffer(rp->id, &rp->gii);
629 if(b == nil){
630 warning(0, "!Decline(%s via %I): no binding",
631 rp->id, rp->gii.ipaddr);
632 return;
635 /* mark ip address as in use */
636 snprint(buf, sizeof(buf), "declined by %s", rp->id);
637 mkoffer(b, buf, 0x7fffffff);
638 commitbinding(b);
641 void
642 rcvrelease(Req *rp)
644 Binding *b;
646 if(rp->staticbinding)
647 return;
649 b = idtobinding(rp->id, &rp->gii, 0);
650 if(b == nil){
651 warning(0, "!Release(%s via %I): no binding",
652 rp->id, rp->gii.ipaddr);
653 return;
655 if(strcmp(rp->id, b->boundto) != 0){
656 warning(0, "!Release(%s via %I): invalid release of %I",
657 rp->id, rp->gii.ipaddr, rp->ip);
658 return;
660 warning(0, "Release(%s via %I): releasing %I", b->boundto, rp->gii.ipaddr, b->ip);
661 if(releasebinding(b, rp->id) < 0)
662 warning(0, "release: couldn't release");
665 void
666 rcvinform(Req *rp)
668 Binding *b;
670 if(rp->staticbinding){
671 sendack(rp, rp->ii.ipaddr, 0, 0);
672 return;
675 b = iptobinding(rp->ciaddr, 0);
676 if(b == nil){
677 warning(0, "!Inform(%s via %I): no binding for %I",
678 rp->id, rp->gii.ipaddr, rp->ip);
679 return;
681 sendack(rp, b->ip, 0, 0);
684 int
685 setsiaddr(uchar *siaddr, uchar *saddr, uchar *laddr)
687 if(ipcmp(saddr, IPnoaddr) != 0){
688 v6tov4(siaddr, saddr);
689 return 0;
690 } else {
691 v6tov4(siaddr, laddr);
692 return 1;
696 void
697 sendoffer(Req *rp, uchar *ip, int offer)
699 int n;
700 int fd;
701 ushort flags;
702 Bootp *bp;
703 Udphdr *up;
705 bp = rp->bp;
706 up = rp->up;
708 /*
709 * set destination
710 */
711 flags = nhgets(bp->flags);
712 fd = wfd;
713 if(validip(rp->giaddr)){
714 ipmove(up->raddr, rp->giaddr);
715 hnputs(up->rport, 67);
716 } else if(flags & Fbroadcast){
717 fd = bwfd;
718 ipmove(up->raddr, IPv4bcast);
719 hnputs(up->rport, 68);
720 } else {
721 ipmove(up->raddr, ip);
722 if(bp->htype == 1)
723 arpenter(up->raddr, bp->chaddr);
724 hnputs(up->rport, 68);
727 /*
728 * fill in standard bootp part
729 */
730 bp->op = Bootreply;
731 bp->hops = 0;
732 hnputs(bp->secs, 0);
733 memset(bp->ciaddr, 0, sizeof(bp->ciaddr));
734 v6tov4(bp->giaddr, rp->giaddr);
735 v6tov4(bp->yiaddr, ip);
736 setsiaddr(bp->siaddr, rp->ii.tftp, up->laddr);
737 strncpy(bp->sname, mysysname, sizeof(bp->sname));
738 strncpy(bp->file, rp->ii.bootf, sizeof(bp->file));
740 /*
741 * set options
742 */
743 byteopt(rp, ODtype, Offer);
744 longopt(rp, ODlease, offer);
745 addropt(rp, ODserverid, up->laddr);
746 miscoptions(rp, ip);
747 termopt(rp);
749 logdhcpout(rp, "Offer");
751 /*
752 * send
753 */
754 n = rp->p - rp->buf;
755 print("OFFER: %I %I %d %d\n", rp->up->laddr, rp->up->raddr, nhgets(rp->up->lport), nhgets(rp->up->rport));
756 if(!mute && udpwrite(fd, rp->up, rp->buf, n) != n)
757 warning(0, "offer: write failed: %r");
760 void
761 sendack(Req *rp, uchar *ip, int offer, int sendlease)
763 int n, fd;
764 ushort flags;
765 Bootp *bp;
766 Udphdr *up;
768 bp = rp->bp;
769 up = rp->up;
771 /*
772 * set destination
773 */
774 fd = wfd;
775 flags = nhgets(bp->flags);
776 if(validip(rp->giaddr)){
777 ipmove(up->raddr, rp->giaddr);
778 hnputs(up->rport, 67);
779 } else if(flags & Fbroadcast){
780 fd = bwfd;
781 ipmove(up->raddr, IPv4bcast);
782 hnputs(up->rport, 68);
783 } else {
784 ipmove(up->raddr, ip);
785 if(bp->htype == 1)
786 arpenter(up->raddr, bp->chaddr);
787 hnputs(up->rport, 68);
790 /*
791 * fill in standard bootp part
792 */
793 bp->op = Bootreply;
794 bp->hops = 0;
795 hnputs(bp->secs, 0);
796 v6tov4(bp->giaddr, rp->giaddr);
797 v6tov4(bp->yiaddr, ip);
798 setsiaddr(bp->siaddr, rp->ii.tftp, up->laddr);
799 strncpy(bp->sname, mysysname, sizeof(bp->sname));
800 strncpy(bp->file, rp->ii.bootf, sizeof(bp->file));
802 /*
803 * set options
804 */
805 byteopt(rp, ODtype, Ack);
806 if(sendlease){
807 longopt(rp, ODlease, offer);
809 addropt(rp, ODserverid, up->laddr);
810 miscoptions(rp, ip);
811 termopt(rp);
813 logdhcpout(rp, "Ack");
815 /*
816 * send
817 */
818 n = rp->p - rp->buf;
819 if(!mute && udpwrite(fd, rp->up, rp->buf, n) != n)
820 warning(0, "ack: write failed: %r");
823 void
824 sendnak(Req *rp, char *msg)
826 int n, fd;
827 Bootp *bp;
828 Udphdr *up;
830 bp = rp->bp;
831 up = rp->up;
833 /*
834 * set destination (always broadcast)
835 */
836 fd = wfd;
837 if(validip(rp->giaddr)){
838 ipmove(up->raddr, rp->giaddr);
839 hnputs(up->rport, 67);
840 } else {
841 fd = bwfd;
842 ipmove(up->raddr, IPv4bcast);
843 hnputs(up->rport, 68);
846 /*
847 * fill in standard bootp part
848 */
849 bp->op = Bootreply;
850 bp->hops = 0;
851 hnputs(bp->secs, 0);
852 v6tov4(bp->giaddr, rp->giaddr);
853 memset(bp->ciaddr, 0, sizeof(bp->ciaddr));
854 memset(bp->yiaddr, 0, sizeof(bp->yiaddr));
855 memset(bp->siaddr, 0, sizeof(bp->siaddr));
857 /*
858 * set options
859 */
860 byteopt(rp, ODtype, Nak);
861 addropt(rp, ODserverid, up->laddr);
862 if(msg)
863 stringopt(rp, ODmessage, msg);
864 if(strncmp(rp->id, "id", 2) == 0)
865 hexopt(rp, ODclientid, rp->id+2);
866 termopt(rp);
868 logdhcpout(rp, "Nak");
870 /*
871 * send nak
872 */
873 n = rp->p - rp->buf;
874 if(!mute && udpwrite(fd, rp->up, rp->buf, n) != n)
875 warning(0, "nak: write failed: %r");
878 void
879 bootp(Req *rp)
881 int n, fd;
882 Bootp *bp;
883 Udphdr *up;
884 ushort flags;
885 Iplifc *lifc;
886 Info *iip;
888 warning(0, "bootp %s %I->%I from %s via %I, file %s",
889 rp->genrequest ? "generic" : (rp->p9request ? "p9" : ""),
890 rp->up->raddr, rp->up->laddr,
891 rp->id, rp->gii.ipaddr,
892 rp->bp->file);
894 if(nobootp)
895 return;
897 bp = rp->bp;
898 up = rp->up;
899 iip = &rp->ii;
901 if(rp->staticbinding == 0){
902 warning(0, "bootp from unknown %s via %I", rp->id, rp->gii.ipaddr);
903 return;
906 /* ignore if not for us */
907 if(*bp->sname){
908 if(strcmp(bp->sname, mysysname) != 0){
909 bp->sname[20] = 0;
910 warning(0, "bootp for server %s", bp->sname);
911 return;
913 } else if(slow)
914 sleep(500);
916 /* ignore if we don't know what file to load */
917 if(*bp->file == 0){
918 if(rp->genrequest && *iip->bootf2) /* if not plan 9 and we have an alternate file... */
919 strncpy(bp->file, iip->bootf2, sizeof(bp->file));
920 else if(*iip->bootf)
921 strncpy(bp->file, iip->bootf, sizeof(bp->file));
922 else if(*bp->sname) /* if we were asked, respond no matter what */
923 bp->file[0] = '\0';
924 else {
925 warning(0, "no bootfile for %I", iip->ipaddr);
926 return;
930 /* ignore if the file is unreadable */
931 if((!rp->genrequest) && bp->file[0] && access(bp->file, 4) < 0){
932 warning(0, "inaccessible bootfile1 %s", bp->file);
933 return;
936 bp->op = Bootreply;
937 v6tov4(bp->yiaddr, iip->ipaddr);
938 if(rp->p9request){
939 warning(0, "p9bootp: %I", iip->ipaddr);
940 memmove(bp->optmagic, plan9opt, 4);
941 if(iip->gwip == 0)
942 v4tov6(iip->gwip, bp->giaddr);
943 rp->p += sprint((char*)rp->p, "%V %I %I %I", iip->ipmask+IPv4off, iip->fsip,
944 iip->auip, iip->gwip);
945 sprint(optbuf, "%s", (char*)(bp->optmagic));
946 } else if(rp->genrequest){
947 warning(0, "genericbootp: %I", iip->ipaddr);
948 memmove(bp->optmagic, genericopt, 4);
949 miscoptions(rp, iip->ipaddr);
950 termopt(rp);
951 } else if(iip->vendor[0] != 0) {
952 warning(0, "bootp vendor field: %s", iip->vendor);
953 memset(rp->p, 0, 128-4);
954 rp->p += sprint((char*)bp->optmagic, "%s", iip->vendor);
955 } else {
956 memset(rp->p, 0, 128-4);
957 rp->p += 128-4;
960 /*
961 * set destination
962 */
963 fd = wfd;
964 flags = nhgets(bp->flags);
965 if(validip(rp->giaddr)){
966 ipmove(up->raddr, rp->giaddr);
967 hnputs(up->rport, 67);
968 } else if(flags & Fbroadcast){
969 fd = bwfd;
970 ipmove(up->raddr, IPv4bcast);
971 hnputs(up->rport, 68);
972 } else {
973 v4tov6(up->raddr, bp->yiaddr);
974 if(bp->htype == 1)
975 arpenter(up->raddr, bp->chaddr);
976 hnputs(up->rport, 68);
979 /*
980 * select best local address if destination is directly connected
981 */
982 lifc = findlifc(up->raddr);
983 if(lifc)
984 ipmove(up->laddr, lifc->ip);
986 /*
987 * our identity
988 */
989 strncpy(bp->sname, mysysname, sizeof(bp->sname));
991 /*
992 * set tftp server
993 */
994 setsiaddr(bp->siaddr, iip->tftp, up->laddr);
995 if(rp->genrequest && *iip->bootf2)
996 setsiaddr(bp->siaddr, iip->tftp2, up->laddr);
998 /*
999 * RFC 1048 says that we must pad vendor field with
1000 * zeros until we have a 64 byte field.
1002 n = rp->p - rp->bp->optdata;
1003 if(n < 64-4) {
1004 memset(rp->p, 0, (64-4)-n);
1005 rp->p += (64-4)-n;
1009 * send
1011 n = rp->p - rp->buf;
1012 if(!mute && udpwrite(fd, rp->up, rp->buf, n) != n)
1013 warning(0, "bootp: write failed: %r");
1015 warning(0, "bootp via %I: file %s xid(%ux)flag(%ux)ci(%V)gi(%V)yi(%V)si(%V) %s",
1016 up->raddr, bp->file, nhgetl(bp->xid), nhgets(bp->flags),
1017 bp->ciaddr, bp->giaddr, bp->yiaddr, bp->siaddr,
1018 optbuf);
1021 void
1022 parseoptions(Req *rp)
1024 int n, c, code;
1025 uchar *o, *p;
1027 p = rp->p;
1029 while(p < rp->e){
1030 code = *p++;
1031 if(code == 255)
1032 break;
1033 if(code == 0)
1034 continue;
1036 /* ignore anything that's too long */
1037 n = *p++;
1038 o = p;
1039 p += n;
1040 if(p > rp->e)
1041 return;
1043 switch(code){
1044 case ODipaddr: /* requested ip address */
1045 if(n == IPv4addrlen)
1046 v4tov6(rp->ip, o);
1047 break;
1048 case ODlease: /* requested lease time */
1049 rp->leasetime = nhgetl(o);
1050 if(rp->leasetime > MaxLease || rp->leasetime < 0)
1051 rp->leasetime = MaxLease;
1052 break;
1053 case ODtype:
1054 c = *o;
1055 if(c < 10 && c > 0)
1056 rp->dhcptype = c;
1057 break;
1058 case ODserverid:
1059 if(n == IPv4addrlen)
1060 v4tov6(rp->server, o);
1061 break;
1062 case ODmessage:
1063 if(n > sizeof rp->msg-1)
1064 n = sizeof rp->msg-1;
1065 memmove(rp->msg, o, n);
1066 rp->msg[n] = 0;
1067 break;
1068 case ODmaxmsg:
1069 c = nhgets(o);
1070 c -= 28;
1071 if(c > 0)
1072 rp->max = rp->buf + c;
1073 break;
1074 case ODclientid:
1075 if(n <= 1)
1076 break;
1077 rp->id = toid( o, n);
1078 break;
1079 case ODparams:
1080 if(n > sizeof(rp->requested))
1081 n = sizeof(rp->requested);
1082 memmove(rp->requested, o, n);
1083 break;
1084 case ODvendorclass:
1085 if(n >= sizeof(rp->vendorclass))
1086 n = sizeof(rp->vendorclass)-1;
1087 memmove(rp->vendorclass, o, n);
1088 rp->vendorclass[n] = 0;
1089 if(strncmp((char*)rp->vendorclass, "p9-", 3) == 0)
1090 strcpy(rp->cputype, (char*)rp->vendorclass+3);
1091 break;
1092 case OBend:
1093 return;
1098 void
1099 remrequested(Req *rp, int opt)
1101 uchar *p;
1103 p = memchr(rp->requested, opt, sizeof(rp->requested));
1104 if(p != nil)
1105 *p = OBpad;
1108 void
1109 miscoptions(Req *rp, uchar *ip)
1111 char *p;
1112 int i, j;
1113 uchar *addrs[2];
1114 uchar x[2*IPaddrlen];
1115 uchar vopts[64];
1116 uchar *op, *omax;
1117 char *attr[100], **a;
1118 int na;
1119 Ndbtuple *t;
1121 addrs[0] = x;
1122 addrs[1] = x+IPaddrlen;
1124 /* always supply these */
1125 maskopt(rp, OBmask, rp->gii.ipmask);
1126 if(validip(rp->gii.gwip)){
1127 remrequested(rp, OBrouter);
1128 addropt(rp, OBrouter, rp->gii.gwip);
1129 } else if(validip(rp->giaddr)){
1130 remrequested(rp, OBrouter);
1131 addropt(rp, OBrouter, rp->giaddr);
1134 // OBhostname for the HP4000M switches
1135 // (this causes NT to log infinite errors - tough shit )
1136 if(*rp->ii.domain){
1137 remrequested(rp, OBhostname);
1138 stringopt(rp, OBhostname, rp->ii.domain);
1140 if(*rp->ii.rootpath)
1141 stringopt(rp, OBrootpath, rp->ii.rootpath);
1143 /* figure out what we need to lookup */
1144 na = 0;
1145 a = attr;
1146 if(*rp->ii.domain == 0)
1147 a[na++] = "dom";
1148 for(i = 0; i < sizeof(rp->requested); i++)
1149 switch(rp->requested[i]){
1150 case OBrouter:
1151 a[na++] = "@ipgw";
1152 break;
1153 case OBdnserver:
1154 a[na++] = "@dns";
1155 break;
1156 case OBnetbiosns:
1157 a[na++] = "@wins";
1158 break;
1159 case OBsmtpserver:
1160 a[na++] = "@smtp";
1161 break;
1162 case OBpop3server:
1163 a[na++] = "@pop3";
1164 break;
1165 case OBwwwserver:
1166 a[na++] = "@www";
1167 break;
1168 case OBntpserver:
1169 a[na++] = "@ntp";
1170 break;
1171 case OBtimeserver:
1172 a[na++] = "@time";
1173 break;
1175 if(strncmp((char*)rp->vendorclass, "plan9_", 6) == 0
1176 || strncmp((char*)rp->vendorclass, "p9-", 3) == 0){
1177 a[na++] = "@fs";
1178 a[na++] = "@auth";
1180 t = lookupinfo(ip, a, na);
1182 /* lookup anything we might be missing */
1183 if(*rp->ii.domain == 0)
1184 lookupname(rp->ii.domain, t);
1186 /* add any requested ones that we know about */
1187 for(i = 0; i < sizeof(rp->requested); i++)
1188 switch(rp->requested[i]){
1189 case OBrouter:
1190 j = lookupserver("ipgw", addrs, t);
1191 addrsopt(rp, OBrouter, addrs, j);
1192 break;
1193 case OBdnserver:
1194 j = lookupserver("dns", addrs, t);
1195 addrsopt(rp, OBdnserver, addrs, j);
1196 break;
1197 case OBhostname:
1198 if(*rp->ii.domain)
1199 stringopt(rp, OBhostname, rp->ii.domain);
1200 break;
1201 case OBdomainname:
1202 p = strchr(rp->ii.domain, '.');
1203 if(p)
1204 stringopt(rp, OBdomainname, p+1);
1205 break;
1206 case OBnetbiosns:
1207 j = lookupserver("wins", addrs, t);
1208 addrsopt(rp, OBnetbiosns, addrs, j);
1209 break;
1210 case OBnetbiostype:
1211 /* p-node: peer to peer WINS queries */
1212 byteopt(rp, OBnetbiostype, 0x2);
1213 break;
1214 case OBsmtpserver:
1215 j = lookupserver("smtp", addrs, t);
1216 addrsopt(rp, OBsmtpserver, addrs, j);
1217 break;
1218 case OBpop3server:
1219 j = lookupserver("pop3", addrs, t);
1220 addrsopt(rp, OBpop3server, addrs, j);
1221 break;
1222 case OBwwwserver:
1223 j = lookupserver("www", addrs, t);
1224 addrsopt(rp, OBwwwserver, addrs, j);
1225 break;
1226 case OBntpserver:
1227 j = lookupserver("ntp", addrs, t);
1228 addrsopt(rp, OBntpserver, addrs, j);
1229 break;
1230 case OBtimeserver:
1231 j = lookupserver("time", addrs, t);
1232 addrsopt(rp, OBtimeserver, addrs, j);
1233 break;
1234 case OBttl:
1235 byteopt(rp, OBttl, 255);
1236 break;
1239 // add plan9 specific options
1240 if(strncmp((char*)rp->vendorclass, "plan9_", 6) == 0
1241 || strncmp((char*)rp->vendorclass, "p9-", 3) == 0){
1242 // point to temporary area
1243 op = rp->p;
1244 omax = rp->max;
1245 rp->p = vopts;
1246 rp->max = vopts + sizeof(vopts) - 1;
1248 j = lookupserver("fs", addrs, t);
1249 addrsopt(rp, OP9fs, addrs, j);
1250 j = lookupserver("auth", addrs, t);
1251 addrsopt(rp, OP9auth, addrs, j);
1253 // point back
1254 j = rp->p - vopts;
1255 rp->p = op;
1256 rp->max = omax;
1257 vectoropt(rp, OBvendorinfo, vopts, j);
1260 ndbfree(t);
1263 int
1264 openlisten(char *net)
1266 int fd;
1267 char data[128];
1268 char devdir[40];
1269 int yes;
1271 sprint(data, "udp!*!bootps");
1272 fd = announce(data, devdir);
1273 if(fd < 0)
1274 fatal(1, "can't announce");
1275 yes = 1;
1276 if(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &yes, sizeof yes) < 0)
1277 fatal(1, "can't broadcast");
1278 return fd;
1281 void
1282 fatal(int syserr, char *fmt, ...)
1284 char buf[ERRMAX];
1285 va_list arg;
1287 va_start(arg, fmt);
1288 vseprint(buf, buf+sizeof(buf), fmt, arg);
1289 va_end(arg);
1290 if(syserr)
1291 syslog(1, blog, "%s: %r", buf);
1292 else
1293 syslog(1, blog, "%s", buf);
1294 exits(buf);
1297 extern void
1298 warning(int syserr, char *fmt, ...)
1300 char buf[256];
1301 va_list arg;
1303 va_start(arg, fmt);
1304 vseprint(buf, buf+sizeof(buf), fmt, arg);
1305 va_end(arg);
1306 if(syserr){
1307 syslog(0, blog, "%s: %r", buf);
1308 if(debug)
1309 fprint(2, "%s: %r\n", buf);
1310 } else {
1311 syslog(0, blog, "%s", buf);
1312 if(debug)
1313 fprint(2, "%s\n", buf);
1317 char*
1318 readsysname(void)
1320 static char name[128];
1321 char *p;
1322 int n, fd;
1324 fd = open("/dev/sysname", OREAD);
1325 if(fd >= 0){
1326 n = read(fd, name, sizeof(name)-1);
1327 close(fd);
1328 if(n > 0){
1329 name[n] = 0;
1330 return name;
1333 p = getenv("sysname");
1334 if(p == nil || *p == 0)
1335 return "unknown";
1336 return p;
1339 extern int
1340 validip(uchar *ip)
1342 if(ipcmp(ip, IPnoaddr) == 0)
1343 return 0;
1344 if(ipcmp(ip, v4prefix) == 0)
1345 return 0;
1346 return 1;
1349 void
1350 longopt(Req *rp, int t, long v)
1352 if(rp->p + 6 > rp->max)
1353 return;
1354 *rp->p++ = t;
1355 *rp->p++ = 4;
1356 hnputl(rp->p, v);
1357 rp->p += 4;
1359 op = seprint(op, oe, "%s(%ld)", optname[t], v);
1362 void
1363 addropt(Req *rp, int t, uchar *ip)
1365 if(rp->p + 6 > rp->max)
1366 return;
1367 *rp->p++ = t;
1368 *rp->p++ = 4;
1369 memmove(rp->p, ip+IPv4off, 4);
1370 rp->p += 4;
1372 op = seprint(op, oe, "%s(%I)", optname[t], ip);
1375 void
1376 maskopt(Req *rp, int t, uchar *ip)
1378 if(rp->p + 6 > rp->max)
1379 return;
1380 *rp->p++ = t;
1381 *rp->p++ = 4;
1382 memmove(rp->p, ip+IPv4off, 4);
1383 rp->p += 4;
1385 op = seprint(op, oe, "%s(%M)", optname[t], ip);
1388 void
1389 addrsopt(Req *rp, int t, uchar **ip, int i)
1391 if(i <= 0)
1392 return;
1393 if(rp->p + 2 + 4*i > rp->max)
1394 return;
1395 *rp->p++ = t;
1396 *rp->p++ = 4*i;
1397 op = seprint(op, oe, "%s(", optname[t]);
1398 while(i-- > 0){
1399 v6tov4(rp->p, *ip);
1400 rp->p += 4;
1401 op = seprint(op, oe, "%I", *ip);
1402 ip++;
1403 if(i > 0)
1404 op = seprint(op, oe, " ");
1406 op = seprint(op, oe, ")");
1409 void
1410 byteopt(Req *rp, int t, uchar v)
1412 if(rp->p + 3 > rp->max)
1413 return;
1414 *rp->p++ = t;
1415 *rp->p++ = 1;
1416 *rp->p++ = v;
1418 op = seprint(op, oe, "%s(%d)", optname[t], v);
1421 void
1422 termopt(Req *rp)
1424 if(rp->p + 1 > rp->max)
1425 return;
1426 *rp->p++ = OBend;
1429 void
1430 stringopt(Req *rp, int t, char *str)
1432 int n;
1434 n = strlen(str);
1435 if(n > 255)
1436 n = 255;
1437 if(rp->p+n+2 > rp->max)
1438 return;
1439 *rp->p++ = t;
1440 *rp->p++ = n;
1441 memmove(rp->p, str, n);
1442 rp->p += n;
1444 op = seprint(op, oe, "%s(%s)", optname[t], str);
1447 void
1448 vectoropt(Req *rp, int t, uchar *v, int n)
1450 int i;
1452 if(n > 255)
1453 n = 255;
1454 if(rp->p+n+2 > rp->max)
1455 return;
1456 *rp->p++ = t;
1457 *rp->p++ = n;
1458 memmove(rp->p, v, n);
1459 rp->p += n;
1461 op = seprint(op, oe, "%s(", optname[t]);
1462 if(n > 0)
1463 op = seprint(op, oe, "%ud", 0);
1464 for(i = 1; i < n; i++)
1465 op = seprint(op, oe, " %ud", v[i]);
1468 int
1469 fromhex(int x)
1471 if(x >= '0' && x <= '9')
1472 return x - '0';
1473 return x - 'a';
1476 void
1477 hexopt(Req *rp, int t, char *str)
1479 int n;
1481 n = strlen(str);
1482 n /= 2;
1483 if(n > 255)
1484 n = 255;
1485 if(rp->p+n+2 > rp->max)
1486 return;
1487 *rp->p++ = t;
1488 *rp->p++ = n;
1489 while(n-- > 0){
1490 *rp->p++ = (fromhex(str[0])<<4)|fromhex(str[1]);
1491 str += 2;
1494 op = seprint(op, oe, "%s(%s)", optname[t], str);
1498 * What a crock it is to do this for real.
1499 * A giant hairy mess of ioctls that differ from
1500 * system to system. Don't get sucked in.
1501 * This need not be fast.
1503 void
1504 arpenter(uchar *ip, uchar *ether)
1506 int pid;
1507 char xip[100], xether[100];
1509 switch(pid=fork()){
1510 case -1:
1511 break;
1512 default:
1513 waitpid();
1514 break;
1515 case 0:
1516 snprint(xip, sizeof xip, "%I", ip);
1517 snprint(xether, sizeof xether, "%#E", ether);
1518 execl("arp", "arp", "-s", xip, xether, "temp", nil);
1519 _exits("execl");
1522 for comfort - ah, the good old days
1524 int f;
1525 char buf[256];
1527 sprint(buf, "%s/arp", net);
1528 f = open(buf, OWRITE);
1529 if(f < 0){
1530 syslog(debug, blog, "open %s: %r", buf);
1531 return;
1533 fprint(f, "add ether %I %E", ip, ether);
1534 close(f);
1538 char *dhcpmsgname[] =
1540 [Discover] "Discover",
1541 [Offer] "Offer",
1542 [Request] "Request",
1543 [Decline] "Decline",
1544 [Ack] "Ack",
1545 [Nak] "Nak",
1546 [Release] "Release",
1547 [Inform] "Inform",
1550 void
1551 logdhcp(Req *rp)
1553 char buf[4096];
1554 char *p, *e;
1555 int i;
1557 p = buf;
1558 e = buf + sizeof(buf);
1559 if(rp->dhcptype > 0 && rp->dhcptype <= Inform)
1560 p = seprint(p, e, "%s(", dhcpmsgname[rp->dhcptype]);
1561 else
1562 p = seprint(p, e, "%d(", rp->dhcptype);
1563 p = seprint(p, e, "%I->%I) xid(%ux)flag(%ux)", rp->up->raddr, rp->up->laddr,
1564 nhgetl(rp->bp->xid), nhgets(rp->bp->flags));
1565 if(rp->bp->htype == 1)
1566 p = seprint(p, e, "ea(%E)", rp->bp->chaddr);
1567 if(validip(rp->ciaddr))
1568 p = seprint(p, e, "ci(%I)", rp->ciaddr);
1569 if(validip(rp->giaddr))
1570 p = seprint(p, e, "gi(%I)", rp->giaddr);
1571 if(validip(rp->ip))
1572 p = seprint(p, e, "ip(%I)", rp->ip);
1573 if(rp->id != nil)
1574 p = seprint(p, e, "id(%s)", rp->id);
1575 if(rp->leasetime)
1576 p = seprint(p, e, "leas(%d)", rp->leasetime);
1577 if(validip(rp->server))
1578 p = seprint(p, e, "sid(%I)", rp->server);
1579 p = seprint(p, e, "need(");
1580 for(i = 0; i < sizeof(rp->requested); i++)
1581 if(rp->requested[i] != 0)
1582 p = seprint(p, e, "%s ", optname[rp->requested[i]]);
1583 p = seprint(p, e, ")");
1585 USED(p);
1586 syslog(0, blog, "%s", buf);
1589 void
1590 logdhcpout(Req *rp, char *type)
1592 syslog(0, blog, "%s(%I->%I)id(%s)ci(%V)gi(%V)yi(%V)si(%V) %s",
1593 type, rp->up->laddr, rp->up->raddr, rp->id,
1594 rp->bp->ciaddr, rp->bp->giaddr, rp->bp->yiaddr, rp->bp->siaddr, optbuf);
1598 * if we get behind, it's useless to try answering since the sender
1599 * will probably have retransmitted with a differnt sequence number.
1600 * So dump all the last message in the queue.
1602 void ding(void *x, char *msg)
1604 USED(x);
1606 if(strstr(msg, "alarm"))
1607 noted(NCONT);
1608 else
1609 noted(NDFLT);
1612 int
1613 readlast(int fd, Udphdr *hdr, uchar *buf, int len)
1615 int lastn, n;
1617 notify(ding);
1619 lastn = 0;
1620 for(;;){
1621 alarm(20);
1622 n = udpread(fd, hdr, buf, len);
1623 alarm(0);
1624 if(n < 0){
1625 if(lastn > 0)
1626 return lastn;
1627 break;
1629 lastn = n;
1631 return udpread(fd, hdr, buf, len);