forked from opnsense/src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootp_subr.c
1830 lines (1599 loc) · 44.6 KB
/
bootp_subr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* SPDX-License-Identifier: BSD-4-Clause
*
* Copyright (c) 1995 Gordon Ross, Adam Glass
* Copyright (c) 1992 Regents of the University of California.
* All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratory and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* based on:
* nfs/krpc_subr.c
* $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
*/
#define IN_HISTORICAL_NETS /* include class masks */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_bootp.h"
#include "opt_nfs.h"
#include "opt_rootdevname.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/endian.h>
#include <sys/jail.h>
#include <sys/kernel.h>
#include <sys/sockio.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/reboot.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysctl.h>
#include <sys/uio.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/route.h>
#include <net/route/route_ctl.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <net/if_types.h>
#include <net/if_dl.h>
#include <net/vnet.h>
#include <nfs/nfsproto.h>
#include <nfsclient/nfs.h>
#include <nfs/nfsdiskless.h>
#include <nfs/krpc.h>
#include <nfs/xdr_subs.h>
#define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */
#ifndef BOOTP_SETTLE_DELAY
#define BOOTP_SETTLE_DELAY 3
#endif
/*
* Wait 10 seconds for interface appearance
* USB ethernet adapters might require some time to pop up
*/
#ifndef BOOTP_IFACE_WAIT_TIMEOUT
#define BOOTP_IFACE_WAIT_TIMEOUT 10
#endif
/*
* What is the longest we will wait before re-sending a request?
* Note this is also the frequency of "RPC timeout" messages.
* The re-send loop count sup linearly to this maximum, so the
* first complaint will happen after (1+2+3+4+5)=15 seconds.
*/
#define MAX_RESEND_DELAY 5 /* seconds */
/* Definitions from RFC951 */
struct bootp_packet {
u_int8_t op;
u_int8_t htype;
u_int8_t hlen;
u_int8_t hops;
u_int32_t xid;
u_int16_t secs;
u_int16_t flags;
struct in_addr ciaddr;
struct in_addr yiaddr;
struct in_addr siaddr;
struct in_addr giaddr;
unsigned char chaddr[16];
char sname[64];
char file[128];
unsigned char vend[1222];
};
struct bootpc_ifcontext {
STAILQ_ENTRY(bootpc_ifcontext) next;
struct bootp_packet call;
struct bootp_packet reply;
int replylen;
int overload;
union {
struct ifreq _ifreq;
struct in_aliasreq _in_alias_req;
} _req;
#define ireq _req._ifreq
#define iareq _req._in_alias_req
struct ifnet *ifp;
struct sockaddr_dl *sdl;
struct sockaddr_in myaddr;
struct sockaddr_in netmask;
struct sockaddr_in gw;
int gotgw;
int gotnetmask;
int gotrootpath;
int outstanding;
int sentmsg;
u_int32_t xid;
enum {
IF_BOOTP_UNRESOLVED,
IF_BOOTP_RESOLVED,
IF_BOOTP_FAILED,
IF_DHCP_UNRESOLVED,
IF_DHCP_OFFERED,
IF_DHCP_RESOLVED,
IF_DHCP_FAILED,
} state;
int dhcpquerytype; /* dhcp type sent */
struct in_addr dhcpserver;
int gotdhcpserver;
uint16_t mtu;
};
#define TAG_MAXLEN 1024
struct bootpc_tagcontext {
char buf[TAG_MAXLEN + 1];
int overload;
int badopt;
int badtag;
int foundopt;
int taglen;
};
struct bootpc_globalcontext {
STAILQ_HEAD(, bootpc_ifcontext) interfaces;
u_int32_t xid;
int any_root_overrides;
int gotrootpath;
int gotgw;
int ifnum;
int secs;
int starttime;
struct bootp_packet reply;
int replylen;
struct bootpc_ifcontext *setrootfs;
struct bootpc_ifcontext *sethostname;
struct bootpc_tagcontext tmptag;
struct bootpc_tagcontext tag;
};
#define IPPORT_BOOTPC 68
#define IPPORT_BOOTPS 67
#define BOOTP_REQUEST 1
#define BOOTP_REPLY 2
/* Common tags */
#define TAG_PAD 0 /* Pad option, implicit length 1 */
#define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */
#define TAG_ROUTERS 3 /* Routers (in order of preference) */
#define TAG_HOSTNAME 12 /* Client host name */
#define TAG_ROOT 17 /* Root path */
#define TAG_INTF_MTU 26 /* Interface MTU Size (RFC2132) */
/* DHCP specific tags */
#define TAG_OVERLOAD 52 /* Option Overload */
#define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */
#define TAG_END 255 /* End Option (i.e. no more options) */
/* Overload values */
#define OVERLOAD_FILE 1
#define OVERLOAD_SNAME 2
/* Site specific tags: */
#define TAG_ROOTOPTS 130
#define TAG_COOKIE 134 /* ascii info for userland, via sysctl */
#define TAG_DHCP_MSGTYPE 53
#define TAG_DHCP_REQ_ADDR 50
#define TAG_DHCP_SERVERID 54
#define TAG_DHCP_LEASETIME 51
#define TAG_VENDOR_INDENTIFIER 60
#define DHCP_NOMSG 0
#define DHCP_DISCOVER 1
#define DHCP_OFFER 2
#define DHCP_REQUEST 3
#define DHCP_ACK 5
/* NFS read/write block size */
#ifndef BOOTP_BLOCKSIZE
#define BOOTP_BLOCKSIZE 8192
#endif
static char bootp_cookie[128];
static struct socket *bootp_so;
SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD,
bootp_cookie, 0, "Cookie (T134) supplied by bootp server");
/* mountd RPC */
static int md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp,
int *fhsizep, struct nfs_args *args, struct thread *td);
static int setfs(struct sockaddr_in *addr, char *path, char *p,
const struct in_addr *siaddr);
static int getdec(char **ptr);
static int getip(char **ptr, struct in_addr *ip);
static void mountopts(struct nfs_args *args, char *p);
static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
static int xdr_int_decode(struct mbuf **ptr, int *iptr);
static void print_in_addr(struct in_addr addr);
static void print_sin_addr(struct sockaddr_in *addr);
static void clear_sinaddr(struct sockaddr_in *sin);
static void allocifctx(struct bootpc_globalcontext *gctx);
static void bootpc_compose_query(struct bootpc_ifcontext *ifctx,
struct thread *td);
static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx,
struct bootp_packet *bp, int len, int tag);
static void bootpc_tag_helper(struct bootpc_tagcontext *tctx,
unsigned char *start, int len, int tag);
#ifdef BOOTP_DEBUG
void bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa);
void bootpboot_p_iflist(void);
#endif
static int bootpc_call(struct bootpc_globalcontext *gctx,
struct thread *td);
static void bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx,
struct thread *td);
static void bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
struct bootpc_globalcontext *gctx, struct thread *td);
static void bootpc_decode_reply(struct nfsv3_diskless *nd,
struct bootpc_ifcontext *ifctx,
struct bootpc_globalcontext *gctx);
static int bootpc_received(struct bootpc_globalcontext *gctx,
struct bootpc_ifcontext *ifctx);
static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx);
static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx);
static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx);
/*
* In order to have multiple active interfaces with address 0.0.0.0
* and be able to send data to a selected interface, we first set
* mask to /8 on all interfaces, and temporarily set it to /0 when
* doing sosend().
*/
#ifdef BOOTP_DEBUG
void
bootpboot_p_if(struct ifnet *ifp, struct ifaddr *ifa)
{
printf("%s flags %x, addr ",
ifp->if_xname, ifp->if_flags);
print_sin_addr((struct sockaddr_in *) ifa->ifa_addr);
printf(", broadcast ");
print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr);
printf(", netmask ");
print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask);
printf("\n");
}
void
bootpboot_p_iflist(void)
{
struct ifnet *ifp;
struct ifaddr *ifa;
printf("Interface list:\n");
IFNET_RLOCK();
for (ifp = CK_STAILQ_FIRST(&V_ifnet);
ifp != NULL;
ifp = CK_STAILQ_NEXT(ifp, if_link)) {
for (ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
ifa != NULL;
ifa = CK_STAILQ_NEXT(ifa, ifa_link))
if (ifa->ifa_addr->sa_family == AF_INET)
bootpboot_p_if(ifp, ifa);
}
IFNET_RUNLOCK();
}
#endif /* defined(BOOTP_DEBUG) */
static void
clear_sinaddr(struct sockaddr_in *sin)
{
bzero(sin, sizeof(*sin));
sin->sin_len = sizeof(*sin);
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */
sin->sin_port = 0;
}
static void
allocifctx(struct bootpc_globalcontext *gctx)
{
struct bootpc_ifcontext *ifctx;
ifctx = malloc(sizeof(*ifctx), M_TEMP, M_WAITOK | M_ZERO);
ifctx->xid = gctx->xid;
#ifdef BOOTP_NO_DHCP
ifctx->state = IF_BOOTP_UNRESOLVED;
#else
ifctx->state = IF_DHCP_UNRESOLVED;
#endif
gctx->xid += 0x100;
STAILQ_INSERT_TAIL(&gctx->interfaces, ifctx, next);
}
static __inline int
bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx)
{
if (ifctx->state == IF_BOOTP_RESOLVED ||
ifctx->state == IF_DHCP_RESOLVED)
return 1;
return 0;
}
static __inline int
bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx)
{
if (ifctx->state == IF_BOOTP_UNRESOLVED ||
ifctx->state == IF_DHCP_UNRESOLVED)
return 1;
return 0;
}
static __inline int
bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx)
{
if (ifctx->state == IF_BOOTP_FAILED ||
ifctx->state == IF_DHCP_FAILED)
return 1;
return 0;
}
static int
bootpc_received(struct bootpc_globalcontext *gctx,
struct bootpc_ifcontext *ifctx)
{
unsigned char dhcpreplytype;
char *p;
/*
* Need timeout for fallback to less
* desirable alternative.
*/
/* This call used for the side effect (badopt flag) */
(void) bootpc_tag(&gctx->tmptag, &gctx->reply,
gctx->replylen,
TAG_END);
/* If packet is invalid, ignore it */
if (gctx->tmptag.badopt != 0)
return 0;
p = bootpc_tag(&gctx->tmptag, &gctx->reply,
gctx->replylen, TAG_DHCP_MSGTYPE);
if (p != NULL)
dhcpreplytype = *p;
else
dhcpreplytype = DHCP_NOMSG;
switch (ifctx->dhcpquerytype) {
case DHCP_DISCOVER:
if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */
#ifndef BOOTP_FORCE_DHCP
&& dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */
#endif
)
return 0;
break;
case DHCP_REQUEST:
if (dhcpreplytype != DHCP_ACK)
return 0;
case DHCP_NOMSG:
break;
}
/* Ignore packet unless it gives us a root tag we didn't have */
if ((ifctx->state == IF_BOOTP_RESOLVED ||
(ifctx->dhcpquerytype == DHCP_DISCOVER &&
(ifctx->state == IF_DHCP_OFFERED ||
ifctx->state == IF_DHCP_RESOLVED))) &&
(bootpc_tag(&gctx->tmptag, &ifctx->reply,
ifctx->replylen,
TAG_ROOT) != NULL ||
bootpc_tag(&gctx->tmptag, &gctx->reply,
gctx->replylen,
TAG_ROOT) == NULL))
return 0;
bcopy(&gctx->reply, &ifctx->reply, gctx->replylen);
ifctx->replylen = gctx->replylen;
/* XXX: Only reset if 'perfect' response */
if (ifctx->state == IF_BOOTP_UNRESOLVED)
ifctx->state = IF_BOOTP_RESOLVED;
else if (ifctx->state == IF_DHCP_UNRESOLVED &&
ifctx->dhcpquerytype == DHCP_DISCOVER) {
if (dhcpreplytype == DHCP_OFFER)
ifctx->state = IF_DHCP_OFFERED;
else
ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */
} else if (ifctx->state == IF_DHCP_OFFERED &&
ifctx->dhcpquerytype == DHCP_REQUEST)
ifctx->state = IF_DHCP_RESOLVED;
if (ifctx->dhcpquerytype == DHCP_DISCOVER &&
ifctx->state != IF_BOOTP_RESOLVED) {
p = bootpc_tag(&gctx->tmptag, &ifctx->reply,
ifctx->replylen, TAG_DHCP_SERVERID);
if (p != NULL && gctx->tmptag.taglen == 4) {
memcpy(&ifctx->dhcpserver, p, 4);
ifctx->gotdhcpserver = 1;
} else
ifctx->gotdhcpserver = 0;
return 1;
}
ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
ifctx->replylen,
TAG_ROOT) != NULL);
ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
ifctx->replylen,
TAG_ROUTERS) != NULL);
ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply,
ifctx->replylen,
TAG_SUBNETMASK) != NULL);
return 1;
}
static int
bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td)
{
struct sockaddr_in *sin, dst;
struct uio auio;
struct sockopt sopt;
struct iovec aio;
int error, on, rcvflg, timo, len;
time_t atimo;
time_t rtimo;
struct timeval tv;
struct bootpc_ifcontext *ifctx;
int outstanding;
int gotrootpath;
int retry;
const char *s;
tv.tv_sec = 1;
tv.tv_usec = 0;
bzero(&sopt, sizeof(sopt));
sopt.sopt_dir = SOPT_SET;
sopt.sopt_level = SOL_SOCKET;
sopt.sopt_name = SO_RCVTIMEO;
sopt.sopt_val = &tv;
sopt.sopt_valsize = sizeof tv;
error = sosetopt(bootp_so, &sopt);
if (error != 0)
goto out;
/*
* Enable broadcast.
*/
on = 1;
sopt.sopt_name = SO_BROADCAST;
sopt.sopt_val = &on;
sopt.sopt_valsize = sizeof on;
error = sosetopt(bootp_so, &sopt);
if (error != 0)
goto out;
/*
* Disable routing.
*/
on = 1;
sopt.sopt_name = SO_DONTROUTE;
sopt.sopt_val = &on;
sopt.sopt_valsize = sizeof on;
error = sosetopt(bootp_so, &sopt);
if (error != 0)
goto out;
/*
* Bind the local endpoint to a bootp client port.
*/
sin = &dst;
clear_sinaddr(sin);
sin->sin_port = htons(IPPORT_BOOTPC);
error = sobind(bootp_so, (struct sockaddr *)sin, td);
if (error != 0) {
printf("bind failed\n");
goto out;
}
/*
* Setup socket address for the server.
*/
sin = &dst;
clear_sinaddr(sin);
sin->sin_addr.s_addr = INADDR_BROADCAST;
sin->sin_port = htons(IPPORT_BOOTPS);
/*
* Send it, repeatedly, until a reply is received,
* but delay each re-send by an increasing amount.
* If the delay hits the maximum, start complaining.
*/
timo = 0;
rtimo = 0;
for (;;) {
outstanding = 0;
gotrootpath = 0;
STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
if (bootpc_ifctx_isresolved(ifctx) != 0 &&
bootpc_tag(&gctx->tmptag, &ifctx->reply,
ifctx->replylen,
TAG_ROOT) != NULL)
gotrootpath = 1;
}
STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
struct in_aliasreq *ifra = &ifctx->iareq;
sin = (struct sockaddr_in *)&ifra->ifra_mask;
ifctx->outstanding = 0;
if (bootpc_ifctx_isresolved(ifctx) != 0 &&
gotrootpath != 0) {
continue;
}
if (bootpc_ifctx_isfailed(ifctx) != 0)
continue;
outstanding++;
ifctx->outstanding = 1;
/* Proceed to next step in DHCP negotiation */
if ((ifctx->state == IF_DHCP_OFFERED &&
ifctx->dhcpquerytype != DHCP_REQUEST) ||
(ifctx->state == IF_DHCP_UNRESOLVED &&
ifctx->dhcpquerytype != DHCP_DISCOVER) ||
(ifctx->state == IF_BOOTP_UNRESOLVED &&
ifctx->dhcpquerytype != DHCP_NOMSG)) {
ifctx->sentmsg = 0;
bootpc_compose_query(ifctx, td);
}
/* Send BOOTP request (or re-send). */
if (ifctx->sentmsg == 0) {
switch(ifctx->dhcpquerytype) {
case DHCP_DISCOVER:
s = "DHCP Discover";
break;
case DHCP_REQUEST:
s = "DHCP Request";
break;
case DHCP_NOMSG:
default:
s = "BOOTP Query";
break;
}
printf("Sending %s packet from "
"interface %s (%*D)\n",
s,
ifctx->ireq.ifr_name,
ifctx->sdl->sdl_alen,
(unsigned char *) LLADDR(ifctx->sdl),
":");
ifctx->sentmsg = 1;
}
aio.iov_base = (caddr_t) &ifctx->call;
aio.iov_len = sizeof(ifctx->call);
auio.uio_iov = &aio;
auio.uio_iovcnt = 1;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_rw = UIO_WRITE;
auio.uio_offset = 0;
auio.uio_resid = sizeof(ifctx->call);
auio.uio_td = td;
/* Set netmask to 0.0.0.0 */
clear_sinaddr(sin);
error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
td);
if (error != 0)
panic("%s: SIOCAIFADDR, error=%d", __func__,
error);
error = sosend(bootp_so, (struct sockaddr *) &dst,
&auio, NULL, NULL, 0, td);
if (error != 0)
printf("%s: sosend: %d state %08x\n", __func__,
error, (int )bootp_so->so_state);
/* Set netmask to 255.0.0.0 */
sin->sin_addr.s_addr = htonl(0xff000000);
error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra,
td);
if (error != 0)
panic("%s: SIOCAIFADDR, error=%d", __func__,
error);
}
if (outstanding == 0 &&
(rtimo == 0 || time_second >= rtimo)) {
error = 0;
goto out;
}
/* Determine new timeout. */
if (timo < MAX_RESEND_DELAY)
timo++;
else {
printf("DHCP/BOOTP timeout for server ");
print_sin_addr(&dst);
printf("\n");
}
/*
* Wait for up to timo seconds for a reply.
* The socket receive timeout was set to 1 second.
*/
atimo = timo + time_second;
while (time_second < atimo) {
aio.iov_base = (caddr_t) &gctx->reply;
aio.iov_len = sizeof(gctx->reply);
auio.uio_iov = &aio;
auio.uio_iovcnt = 1;
auio.uio_segflg = UIO_SYSSPACE;
auio.uio_rw = UIO_READ;
auio.uio_offset = 0;
auio.uio_resid = sizeof(gctx->reply);
auio.uio_td = td;
rcvflg = 0;
error = soreceive(bootp_so, NULL, &auio,
NULL, NULL, &rcvflg);
gctx->secs = time_second - gctx->starttime;
STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
if (bootpc_ifctx_isresolved(ifctx) != 0 ||
bootpc_ifctx_isfailed(ifctx) != 0)
continue;
ifctx->call.secs = htons(gctx->secs);
}
if (error == EWOULDBLOCK)
continue;
if (error != 0)
goto out;
len = sizeof(gctx->reply) - auio.uio_resid;
/* Do we have the required number of bytes ? */
if (len < BOOTP_MIN_LEN)
continue;
gctx->replylen = len;
/* Is it a reply? */
if (gctx->reply.op != BOOTP_REPLY)
continue;
/* Is this an answer to our query */
STAILQ_FOREACH(ifctx, &gctx->interfaces, next) {
if (gctx->reply.xid != ifctx->call.xid)
continue;
/* Same HW address size ? */
if (gctx->reply.hlen != ifctx->call.hlen)
continue;
/* Correct HW address ? */
if (bcmp(gctx->reply.chaddr,
ifctx->call.chaddr,
ifctx->call.hlen) != 0)
continue;
break;
}
if (ifctx != NULL) {
s = bootpc_tag(&gctx->tmptag,
&gctx->reply,
gctx->replylen,
TAG_DHCP_MSGTYPE);
if (s != NULL) {
switch (*s) {
case DHCP_OFFER:
s = "DHCP Offer";
break;
case DHCP_ACK:
s = "DHCP Ack";
break;
default:
s = "DHCP (unexpected)";
break;
}
} else
s = "BOOTP Reply";
printf("Received %s packet"
" on %s from ",
s,
ifctx->ireq.ifr_name);
print_in_addr(gctx->reply.siaddr);
if (gctx->reply.giaddr.s_addr !=
htonl(INADDR_ANY)) {
printf(" via ");
print_in_addr(gctx->reply.giaddr);
}
if (bootpc_received(gctx, ifctx) != 0) {
printf(" (accepted)");
if (ifctx->outstanding) {
ifctx->outstanding = 0;
outstanding--;
}
/* Network settle delay */
if (outstanding == 0)
atimo = time_second +
BOOTP_SETTLE_DELAY;
} else
printf(" (ignored)");
if (ifctx->gotrootpath ||
gctx->any_root_overrides) {
gotrootpath = 1;
rtimo = time_second +
BOOTP_SETTLE_DELAY;
if (ifctx->gotrootpath)
printf(" (got root path)");
}
printf("\n");
}
} /* while secs */
#ifdef BOOTP_TIMEOUT
if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0)
break;
#endif
/* Force a retry if halfway in DHCP negotiation */
retry = 0;
STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
if (ifctx->state == IF_DHCP_OFFERED) {
if (ifctx->dhcpquerytype == DHCP_DISCOVER)
retry = 1;
else
ifctx->state = IF_DHCP_UNRESOLVED;
}
if (retry != 0)
continue;
if (gotrootpath != 0) {
gctx->gotrootpath = gotrootpath;
if (rtimo != 0 && time_second >= rtimo)
break;
}
} /* forever send/receive */
/*
* XXX: These are errors of varying seriousness being silently
* ignored
*/
STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
if (bootpc_ifctx_isresolved(ifctx) == 0) {
printf("%s timeout for interface %s\n",
ifctx->dhcpquerytype != DHCP_NOMSG ?
"DHCP" : "BOOTP",
ifctx->ireq.ifr_name);
}
if (gctx->gotrootpath != 0) {
#if 0
printf("Got a root path, ignoring remaining timeout\n");
#endif
error = 0;
goto out;
}
#ifndef BOOTP_NFSROOT
STAILQ_FOREACH(ifctx, &gctx->interfaces, next)
if (bootpc_ifctx_isresolved(ifctx) != 0) {
error = 0;
goto out;
}
#endif
error = ETIMEDOUT;
out:
return (error);
}
static void
bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
{
struct ifreq *ifr;
struct in_aliasreq *ifra;
struct sockaddr_in *sin;
int error;
ifr = &ifctx->ireq;
ifra = &ifctx->iareq;
/*
* Bring up the interface.
*
* Get the old interface flags and or IFF_UP into them; if
* IFF_UP set blindly, interface selection can be clobbered.
*/
error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
if (error != 0)
panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
ifr->ifr_flags |= IFF_UP;
error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
if (error != 0)
panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
/*
* Do enough of ifconfig(8) so that the chosen interface
* can talk to the servers. Set address to 0.0.0.0/8 and
* broadcast address to local broadcast.
*/
sin = (struct sockaddr_in *)&ifra->ifra_addr;
clear_sinaddr(sin);
sin = (struct sockaddr_in *)&ifra->ifra_mask;
clear_sinaddr(sin);
sin->sin_addr.s_addr = htonl(0xff000000);
sin = (struct sockaddr_in *)&ifra->ifra_broadaddr;
clear_sinaddr(sin);
sin->sin_addr.s_addr = htonl(INADDR_BROADCAST);
error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
if (error != 0)
panic("%s: SIOCAIFADDR, error=%d", __func__, error);
}
static void
bootpc_shutdown_interface(struct bootpc_ifcontext *ifctx, struct thread *td)
{
struct ifreq *ifr;
struct sockaddr_in *sin;
int error;
ifr = &ifctx->ireq;
printf("Shutdown interface %s\n", ifctx->ireq.ifr_name);
error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td);
if (error != 0)
panic("%s: SIOCGIFFLAGS, error=%d", __func__, error);
ifr->ifr_flags &= ~IFF_UP;
error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td);
if (error != 0)
panic("%s: SIOCSIFFLAGS, error=%d", __func__, error);
sin = (struct sockaddr_in *) &ifr->ifr_addr;
clear_sinaddr(sin);
error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
if (error != 0)
panic("%s: SIOCDIFADDR, error=%d", __func__, error);
}
static void
bootpc_adjust_interface(struct bootpc_ifcontext *ifctx,
struct bootpc_globalcontext *gctx, struct thread *td)
{
int error;
struct sockaddr_in *sin;
struct ifreq *ifr;
struct in_aliasreq *ifra;
struct sockaddr_in *myaddr;
struct sockaddr_in *netmask;
ifr = &ifctx->ireq;
ifra = &ifctx->iareq;
myaddr = &ifctx->myaddr;
netmask = &ifctx->netmask;
if (bootpc_ifctx_isresolved(ifctx) == 0) {
/* Shutdown interfaces where BOOTP failed */
bootpc_shutdown_interface(ifctx, td);
return;
}
printf("Adjusted interface %s", ifctx->ireq.ifr_name);
/* Do BOOTP interface options */
if (ifctx->mtu != 0) {
printf(" (MTU=%d%s)", ifctx->mtu,
(ifctx->mtu > 1514) ? "/JUMBO" : "");
ifr->ifr_mtu = ifctx->mtu;
error = ifioctl(bootp_so, SIOCSIFMTU, (caddr_t) ifr, td);
if (error != 0)
panic("%s: SIOCSIFMTU, error=%d", __func__, error);
}
printf("\n");
/*
* Do enough of ifconfig(8) so that the chosen interface
* can talk to the servers. (just set the address)
*/
sin = (struct sockaddr_in *) &ifr->ifr_addr;
clear_sinaddr(sin);
error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td);
if (error != 0)
panic("%s: SIOCDIFADDR, error=%d", __func__, error);
bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr));
bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask));
clear_sinaddr(&ifra->ifra_broadaddr);
ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr |
~netmask->sin_addr.s_addr;
error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td);
if (error != 0)
panic("%s: SIOCAIFADDR, error=%d", __func__, error);
}
static void
bootpc_add_default_route(struct bootpc_ifcontext *ifctx)
{
int error;
struct sockaddr_in defdst;
struct sockaddr_in defmask;
struct rt_addrinfo info;
struct rib_cmd_info rc;
if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY))
return;
clear_sinaddr(&defdst);
clear_sinaddr(&defmask);
bzero((caddr_t)&info, sizeof(info));
info.rti_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
info.rti_info[RTAX_DST] = (struct sockaddr *)&defdst;
info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&defmask;
info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ifctx->gw;
error = rib_action(RT_DEFAULT_FIB, RTM_ADD, &info, &rc);
if (error != 0) {