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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <sys/dlpi.h>
#include <stdlib.h>
#include <sys/sockio.h>
#include <netinet/in.h>
#include <netinet/dhcp.h>
#include <string.h>
#include <unistd.h>
#include <netinet/if_ether.h>
#include <signal.h>
#include <dhcpmsg.h>
#include <libdevinfo.h>
#include "interface.h"
#include "util.h"
#include "dlpi_io.h"
#include "packet.h"
#include "defaults.h"
#include "states.h"
#include "script_handler.h"
/*
* note to the reader:
*
* the terminology in here is slightly confusing. in particular, the
* term `ifslist' is used to refer both to the `struct ifslist' entry
* that makes up a specific interface entry, and the `internal
* ifslist' which is a linked list of struct ifslists. to reduce
* confusion, in the comments, a `struct ifslist' is referred to as
* an `ifs', and `ifslist' refers to the internal ifslist.
*
*/
static struct ifslist *ifsheadp;
static unsigned int ifscount;
static void init_ifs(struct ifslist *);
static void free_ifs(struct ifslist *);
static void cancel_ifs_timer(struct ifslist *, int);
static boolean_t get_prom_prop(const char *, const char *, uchar_t **,
unsigned int *);
/*
* insert_ifs(): creates a new ifs and chains it on the ifslist. initializes
* state which remains consistent across all use of the ifs entry
*
* input: const char *: the name of the ifs entry (interface name)
* boolean_t: if B_TRUE, we're adopting the interface
* int *: ignored on input; if insert_ifs fails, set to a DHCP_IPC_E_*
* error code with the reason why
* output: struct ifslist *: a pointer to the new ifs entry, or NULL on failure
*/
struct ifslist *
insert_ifs(const char *if_name, boolean_t is_adopting, int *error)
{
uint32_t buf[DLPI_BUF_MAX / sizeof (uint32_t)];
dl_info_ack_t *dlia = (dl_info_ack_t *)buf;
caddr_t dl_addr;
struct ifreq ifr;
unsigned int i, client_id_len = 0;
uchar_t *client_id = NULL;
const char *prl;
struct ifslist *ifsp;
long seed;
ifsp = lookup_ifs(if_name);
if (ifsp != NULL) {
*error = DHCP_IPC_E_INT; /* should never happen */
return (NULL);
}
/*
* okay, we've got a request to put a new interface under our
* control. it's our job to set everything that doesn't
* change for the life of the interface. (state that changes
* should be initialized in init_ifs() and reset by reset_ifs())
*
* 1. verify the interface can support DHCP
* 2. get the interface mtu
* 3. get the interface hardware type and hardware length
* 4. get the interface hardware address
* 5. get the interface broadcast address
* 6. get the interface flags
*/
ifsp = calloc(1, sizeof (struct ifslist));
if (ifsp == NULL) {
dhcpmsg(MSG_ERR, "insert_ifs: cannot allocate ifs entry for "
"%s", if_name);
*error = DHCP_IPC_E_MEMORY;
return (NULL);
}
(void) strlcpy(ifsp->if_name, if_name, IFNAMSIZ);
/* step 1 */
ifsp->if_dlpi_fd = dlpi_open(if_name, dlia, sizeof (buf), ETHERTYPE_IP);
if (ifsp->if_dlpi_fd == -1) {
*error = DHCP_IPC_E_INVIF;
goto failure;
}
init_ifs(ifsp); /* ifsp->if_dlpi_fd must be valid */
ipc_action_init(ifsp);
/* step 2 */
ifsp->if_max = dlia->dl_max_sdu;
ifsp->if_opt = ifsp->if_max - BASE_PKT_SIZE;
ifsp->if_min = dlia->dl_min_sdu;
if (ifsp->if_max < DHCP_DEF_MAX_SIZE) {
dhcpmsg(MSG_ERROR, "insert_ifs: %s does not have a large "
"enough maximum SDU to support DHCP", if_name);
*error = DHCP_IPC_E_INVIF;
goto failure;
}
/* step 3 */
ifsp->if_hwtype = dlpi_to_arp(dlia->dl_mac_type);
ifsp->if_hwlen = dlia->dl_addr_length - abs(dlia->dl_sap_length);
dhcpmsg(MSG_DEBUG, "insert_ifs: %s: sdumax %d, optmax %d, hwtype %d, "
"hwlen %d", if_name, ifsp->if_max, ifsp->if_opt, ifsp->if_hwtype,
ifsp->if_hwlen);
/* step 4 */
ifsp->if_hwaddr = malloc(ifsp->if_hwlen);
if (ifsp->if_hwaddr == NULL) {
dhcpmsg(MSG_ERR, "insert_ifs: cannot allocate if_hwaddr "
"for %s", if_name);
*error = DHCP_IPC_E_MEMORY;
goto failure;
}
/*
* depending on the DLPI device, the sap and hardware addresses
* can be in either order within the dlsap address; find the
* location of the hardware address using dl_sap_length. see the
* DLPI specification for more on this braindamage.
*/
dl_addr = (caddr_t)dlia + dlia->dl_addr_offset;
if (dlia->dl_sap_length > 0) {
ifsp->if_sap_before++;
dl_addr += dlia->dl_sap_length;
}
(void) memcpy(ifsp->if_hwaddr, dl_addr, ifsp->if_hwlen);
/* step 5 */
ifsp->if_saplen = abs(dlia->dl_sap_length);
ifsp->if_daddr = build_broadcast_dest(dlia, &ifsp->if_dlen);
if (ifsp->if_daddr == NULL) {
dhcpmsg(MSG_ERR, "insert_ifs: cannot allocate if_daddr "
"for %s", if_name);
*error = DHCP_IPC_E_MEMORY;
goto failure;
}
/* step 6 */
(void) strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
if (ioctl(ifsp->if_sock_fd, SIOCGIFFLAGS, &ifr) == -1) {
if (errno == ENXIO)
*error = DHCP_IPC_E_INVIF;
else
*error = DHCP_IPC_E_INT;
dhcpmsg(MSG_ERR, "insert_ifs: SIOCGIFFLAGS for %s", if_name);
goto failure;
}
/*
* if DHCPRUNNING is already set on the interface and we're
* not adopting it, the agent probably crashed and burned.
* note it, but don't let it stop the proceedings. we're
* pretty sure we're not already running, since we wouldn't
* have been able to bind to our IPC port.
*/
if ((is_adopting == B_FALSE) && (ifr.ifr_flags & IFF_DHCPRUNNING))
dhcpmsg(MSG_WARNING, "insert_ifs: DHCP flag already set on %s",
if_name);
ifr.ifr_flags |= IFF_DHCPRUNNING;
(void) ioctl(ifsp->if_sock_fd, SIOCSIFFLAGS, &ifr);
ifsp->if_send_pkt.pkt = calloc(ifsp->if_max, 1);
if (ifsp->if_send_pkt.pkt == NULL) {
dhcpmsg(MSG_ERR, "insert_ifs: cannot allocate if_send_pkt "
"for %s", if_name);
*error = DHCP_IPC_E_MEMORY;
goto failure;
}
if (is_adopting) {
/*
* if the agent is adopting a lease OBP is initially
* searched for a client-id
*/
dhcpmsg(MSG_DEBUG, "insert_ifs: getting /chosen:clientid "
"property");
if (!get_prom_prop("chosen", "client-id", &ifsp->if_cid,
&client_id_len)) {
/*
* a failure occurred trying to acquire the client-id
*/
dhcpmsg(MSG_DEBUG, "insert_ifs: cannot allocate client "
"id for %s", if_name);
*error = DHCP_IPC_E_INT;
goto failure;
} else if (dlia->dl_mac_type == DL_IB && ifsp->if_cid == NULL) {
/*
* when the interface is infiniband and the agent
* is adopting the lease there must be an OBP
* client-id.
*/
dhcpmsg(MSG_DEBUG, "insert_ifs: no /chosen:clientid"
"id for %s", if_name);
*error = DHCP_IPC_E_INT;
goto failure;
}
ifsp->if_cidlen = client_id_len;
} else {
/*
* look in defaults file for the client-id
*/
dhcpmsg(MSG_DEBUG, "insert_ifs: getting defaults client-id "
"property");
client_id = df_get_octet(if_name, DF_CLIENT_ID, &client_id_len);
/*
* at this point, all logical interfaces must be explicitly
* configured with a client id by the administrator.
*/
if (client_id == NULL && strchr(if_name, ':') != NULL) {
dhcpmsg(MSG_ERROR, "no client id configured for "
"logical interface %s; cannot manage", if_name);
*error = DHCP_IPC_E_NOIFCID;
goto failure;
}
if (client_id != NULL) {
/*
* the defaults client-id value must be copied out to
* another buffer
*/
ifsp->if_cid = calloc(client_id_len, sizeof (uchar_t));
if (ifsp->if_cid == NULL) {
dhcpmsg(MSG_ERR, "insert_ifs: cannot "
"allocate client id for %s", if_name);
*error = DHCP_IPC_E_MEMORY;
goto failure;
}
(void) memcpy(ifsp->if_cid, client_id, client_id_len);
ifsp->if_cidlen = client_id_len;
} else if (dlia->dl_mac_type == DL_IB) {
/*
* This comes from DHCP over IPoIB spec. In the absence
* of an user specified client id, IPoIB automatically
* uses the required format, with the unique 4 octet
* value set to 0 (since IPoIB driver allows only a
* single interface on a port with a specific GID to
* belong to an IP subnet (PSARC 2001/289,
* FWARC 2002/702).
*
* Type Client-Identifier
* +-----+-----+-----+-----+-----+----....----+
* | 0 | 0 (4 octets) | GID (16 octets)|
* +-----+-----+-----+-----+-----+----....----+
*/
ifsp->if_cidlen = 1 + 4 + 16;
ifsp->if_cid = client_id = malloc(ifsp->if_cidlen);
if (ifsp->if_cid == NULL) {
dhcpmsg(MSG_ERR, "insert_ifs: cannot "
"allocate client id for %s", if_name);
*error = DHCP_IPC_E_MEMORY;
goto failure;
}
/*
* Pick the GID from the mac address. The format
* of the hardware address is:
* +-----+-----+-----+-----+----....----+
* | QPN (4 octets) | GID (16 octets)|
* +-----+-----+-----+-----+----....----+
*/
(void) memcpy(client_id + 5, ifsp->if_hwaddr + 4,
ifsp->if_hwlen - 4);
(void) memset(client_id, 0, 5);
}
}
/*
* initialize the parameter request list, if there is one.
*/
prl = df_get_string(if_name, DF_PARAM_REQUEST_LIST);
if (prl == NULL)
ifsp->if_prl = NULL;
else {
for (ifsp->if_prllen = 1, i = 0; prl[i] != '\0'; i++)
if (prl[i] == ',')
ifsp->if_prllen++;
ifsp->if_prl = malloc(ifsp->if_prllen);
if (ifsp->if_prl == NULL) {
dhcpmsg(MSG_WARNING, "insert_ifs: cannot allocate "
"parameter request list for %s (continuing)",
if_name);
} else {
for (i = 0; i < ifsp->if_prllen; prl++, i++) {
ifsp->if_prl[i] = strtoul(prl, NULL, 0);
while (*prl != ',' && *prl != '\0')
prl++;
if (*prl == '\0')
break;
}
}
}
ifsp->if_offer_wait = df_get_int(if_name, DF_OFFER_WAIT);
/*
* we're past the point of failure; chain it on.
*/
ifsp->next = ifsheadp;
ifsp->prev = NULL;
ifsheadp = ifsp;
if (ifsheadp->next != NULL)
ifsheadp->next->prev = ifsheadp;
hold_ifs(ifsp);
ifscount++;
if (inactivity_id != -1) {
if (iu_cancel_timer(tq, inactivity_id, NULL) == 1)
inactivity_id = -1;
}
/*
* seed the random number generator, since we're going to need it
* to set transaction id's and for exponential backoff. if an
* interface is already initialized, then we just end up harmlessly
* reseeding it. note that we try to spread the hardware address
* over as many bits of the seed as possible.
*/
seed = gethrtime();
for (i = 0; i < ifsp->if_hwlen; i++)
seed += ifsp->if_hwaddr[i] << ((i % 7) * 4);
seed ^= getpid();
srand48(seed);
dhcpmsg(MSG_DEBUG, "insert_ifs: inserted interface %s", if_name);
return (ifsp);
failure:
free_ifs(ifsp);
return (NULL);
}
/*
* init_ifs(): puts an ifs in its initial state
*
* input: struct ifslist *: the ifs to initialize
* output: void
* note: if the interface isn't fresh, use reset_ifs()
*/
static void
init_ifs(struct ifslist *ifsp)
{
/*
* if_sock_ip_fd is created and bound in configure_if().
* if_sock_fd is bound in configure_if(); see comments in
* bound.c for more details on why. if creation of if_sock_fd
* fails, we'll need more context anyway, so don't check.
*/
ifsp->if_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
ifsp->if_sock_ip_fd = -1;
ifsp->if_state = INIT;
ifsp->if_routers = NULL;
ifsp->if_nrouters = 0;
ifsp->if_ack = NULL;
ifsp->if_orig_ack = NULL;
ifsp->if_server.s_addr = htonl(INADDR_BROADCAST);
ifsp->if_neg_monosec = monosec();
ifsp->if_lease = 0;
ifsp->if_t1 = 0;
ifsp->if_t2 = 0;
ifsp->if_reqhost = NULL;
ifsp->if_script_helper_pid = -1;
ifsp->if_script_callback = NULL;
ifsp->if_script_event = NULL;
ifsp->if_callback_msg = NULL;
ifsp->if_script_event_id = -1;
ifsp->if_script_pid = -1;
ifsp->if_script_fd = -1;
ifsp->if_offer_id = -1;
ifsp->if_acknak_id = -1;
ifsp->if_acknak_bcast_id = -1;
ifsp->if_timer[DHCP_T1_TIMER] = -1;
ifsp->if_timer[DHCP_T2_TIMER] = -1;
ifsp->if_timer[DHCP_LEASE_TIMER] = -1;
set_packet_filter(ifsp->if_dlpi_fd, dhcp_filter, NULL, "DHCP");
dhcpmsg(MSG_DEBUG, "init_ifs: initted interface %s", ifsp->if_name);
}
/*
* remove_ifs_default_routes(): removes an ifs's default routes
*
* input: struct ifslist *: the ifs whose default routes need to be removed
* output: void
*/
static void
remove_ifs_default_routes(struct ifslist *ifsp)
{
if (ifsp->if_routers != NULL) {
while (ifsp->if_nrouters > 0) {
(void) del_default_route(ifsp->if_name,
&ifsp->if_routers[--ifsp->if_nrouters]);
}
free(ifsp->if_routers);
ifsp->if_routers = NULL;
}
}
/*
* reset_ifs(): resets an ifs to its initial state
*
* input: struct ifslist *: the ifs to reset
* output: void
*/
void
reset_ifs(struct ifslist *ifsp)
{
ifsp->if_dflags &= ~DHCP_IF_FAILED;
remove_ifs_default_routes(ifsp);
if (ifsp->if_sock_fd != -1)
(void) close(ifsp->if_sock_fd);
if (ifsp->if_orig_ack != ifsp->if_ack)
free_pkt_list(&ifsp->if_orig_ack);
free_pkt_list(&ifsp->if_ack);
if (ifsp->if_sock_ip_fd != -1)
(void) close(ifsp->if_sock_ip_fd);
if (ifsp->if_offer_id != -1) {
if (iu_unregister_event(eh, ifsp->if_offer_id, NULL) != 0)
(void) release_ifs(ifsp);
}
(void) unregister_acknak(ifsp); /* just in case */
cancel_ifs_timers(ifsp);
init_ifs(ifsp);
}
/*
* lookup_ifs(): looks up an ifs, given its name
*
* input: const char *: the name of the ifs entry (the interface name)
* the name "" searches for the primary interface
* output: struct ifslist *: the corresponding ifs, or NULL if not found
*/
struct ifslist *
lookup_ifs(const char *if_name)
{
struct ifslist *ifs;
for (ifs = ifsheadp; ifs != NULL; ifs = ifs->next)
if (*if_name != '\0') {
if (strcmp(ifs->if_name, if_name) == 0)
break;
} else if (ifs->if_dflags & DHCP_IF_PRIMARY)
break;
return (ifs);
}
/*
* lookup_ifs_by_xid(): looks up an ifs, given its last used transaction id
*
* input: int: the transaction id to look up
* output: struct ifslist *: the corresponding ifs, or NULL if not found
*/
struct ifslist *
lookup_ifs_by_xid(uint32_t xid)
{
struct ifslist *ifs;
for (ifs = ifsheadp; ifs != NULL; ifs = ifs->next) {
if (ifs->if_send_pkt.pkt->xid == xid)
break;
}
return (ifs);
}
/*
* remove_ifs(): removes a given ifs from the ifslist. marks the ifs
* for being freed (but may not actually free it).
*
* input: struct ifslist *: the ifs to remove
* output: void
* note: see interface.h for a discussion of ifs memory management
*/
void
remove_ifs(struct ifslist *ifsp)
{
struct ifreq ifr;
if (ifsp->if_dflags & DHCP_IF_REMOVED)
return;
(void) memset(&ifr, 0, sizeof (struct ifreq));
(void) strlcpy(ifr.ifr_name, ifsp->if_name, IFNAMSIZ);
if (ioctl(ifsp->if_sock_fd, SIOCGIFFLAGS, &ifr) == 0) {
ifr.ifr_flags &= ~IFF_DHCPRUNNING;
(void) ioctl(ifsp->if_sock_fd, SIOCSIFFLAGS, &ifr);
}
ifsp->if_dflags |= DHCP_IF_REMOVED;
/*
* if we have long term timers, cancel them so that interface
* resources can be reclaimed in a reasonable amount of time.
*/
cancel_ifs_timers(ifsp);
if (ifsp->prev != NULL)
ifsp->prev->next = ifsp->next;
else
ifsheadp = ifsp->next;
if (ifsp->next != NULL)
ifsp->next->prev = ifsp->prev;
ifscount--;
(void) release_ifs(ifsp);
/* no big deal if this fails */
if (ifscount == 0) {
inactivity_id = iu_schedule_timer(tq, DHCP_INACTIVITY_WAIT,
inactivity_shutdown, NULL);
}
}
/*
* hold_ifs(): acquires a hold on an ifs
*
* input: struct ifslist *: the ifs entry to acquire a hold on
* output: void
*/
void
hold_ifs(struct ifslist *ifsp)
{
ifsp->if_hold_count++;
dhcpmsg(MSG_DEBUG2, "hold_ifs: hold count on %s: %d",
ifsp->if_name, ifsp->if_hold_count);
}
/*
* release_ifs(): releases a hold previously acquired on an ifs. if the
* hold count reaches 0, the ifs is freed
*
* input: struct ifslist *: the ifs entry to release the hold on
* output: int: the number of holds outstanding on the ifs
*/
int
release_ifs(struct ifslist *ifsp)
{
if (ifsp->if_hold_count == 0) {
dhcpmsg(MSG_CRIT, "release_ifs: extraneous release");
return (0);
}
if (--ifsp->if_hold_count == 0) {
free_ifs(ifsp);
return (0);
}
dhcpmsg(MSG_DEBUG2, "release_ifs: hold count on %s: %d",
ifsp->if_name, ifsp->if_hold_count);
return (ifsp->if_hold_count);
}
/*
* free_ifs(): frees the memory occupied by an ifs entry
*
* input: struct ifslist *: the ifs entry to free
* output: void
*/
static void
free_ifs(struct ifslist *ifsp)
{
dhcpmsg(MSG_DEBUG, "free_ifs: freeing interface %s", ifsp->if_name);
free_pkt_list(&ifsp->if_recv_pkt_list);
if (ifsp->if_ack != ifsp->if_orig_ack)
free_pkt_list(&ifsp->if_orig_ack);
free_pkt_list(&ifsp->if_ack);
free(ifsp->if_send_pkt.pkt);
free(ifsp->if_cid);
free(ifsp->if_daddr);
free(ifsp->if_hwaddr);
free(ifsp->if_prl);
free(ifsp->if_reqhost);
free(ifsp->if_routers);
if (ifsp->if_sock_fd != -1)
(void) close(ifsp->if_sock_fd);
if (ifsp->if_sock_ip_fd != -1)
(void) close(ifsp->if_sock_ip_fd);
if (ifsp->if_dlpi_fd != -1)
(void) dlpi_close(ifsp->if_dlpi_fd);
free(ifsp);
}
/*
* checkaddr(): checks if the given address is still set on the given ifs
*
* input: struct ifslist *: the ifs to check
* int: the address to lookup on the interface
* struct in_addr *: the address to compare to
* output: boolean_t: B_TRUE if the address is still set; B_FALSE if not
*/
static boolean_t
checkaddr(struct ifslist *ifsp, int ioccmd, struct in_addr *addr)
{
struct ifreq ifr;
struct sockaddr_in *sin;
/* LINTED [ifr_addr is a sockaddr which will be aligned] */
sin = (struct sockaddr_in *)&ifr.ifr_addr;
(void) memset(&ifr, 0, sizeof (struct ifreq));
(void) strlcpy(ifr.ifr_name, ifsp->if_name, IFNAMSIZ);
ifr.ifr_addr.sa_family = AF_INET;
switch (ioctl(ifsp->if_sock_fd, ioccmd, &ifr)) {
case 0:
if (sin->sin_addr.s_addr != addr->s_addr)
return (B_FALSE);
break;
case -1:
if (errno == ENXIO)
return (B_FALSE);
break;
}
return (B_TRUE);
}
/*
* verify_ifs(): verifies than an ifs is still valid (i.e., has not been
* explicitly or implicitly dropped or released)
*
* input: struct ifslist *: the ifs to verify
* output: int: 1 if the ifs is still valid, 0 if the interface is invalid
*/
int
verify_ifs(struct ifslist *ifsp)
{
struct ifreq ifr;
if (ifsp->if_dflags & DHCP_IF_REMOVED)
return (0);
(void) memset(&ifr, 0, sizeof (struct ifreq));
(void) strlcpy(ifr.ifr_name, ifsp->if_name, IFNAMSIZ);
ifr.ifr_addr.sa_family = AF_INET;
switch (ifsp->if_state) {
case BOUND:
case RENEWING:
case REBINDING:
/*
* if the interface has gone down or been unplumbed, then we
* act like there has been an implicit drop.
*/
switch (ioctl(ifsp->if_sock_fd, SIOCGIFFLAGS, &ifr)) {
case 0:
if ((ifr.ifr_flags & (IFF_UP|IFF_DHCPRUNNING)) !=
(IFF_UP|IFF_DHCPRUNNING))
goto abandon;
break;
case -1:
if (errno == ENXIO)
goto abandon;
break;
}
/* FALLTHRU */
case INIT_REBOOT:
case SELECTING:
case REQUESTING:
/*
* if the IP address, netmask, or broadcast address have
* changed, or the interface has been unplumbed, then we act
* like there has been an implicit drop.
*/
if (!checkaddr(ifsp, SIOCGIFADDR, &ifsp->if_addr) ||
!checkaddr(ifsp, SIOCGIFNETMASK, &ifsp->if_netmask) ||
!checkaddr(ifsp, SIOCGIFBRDADDR, &ifsp->if_broadcast))
goto abandon;
}
return (1);
abandon:
dhcpmsg(MSG_WARNING, "verify_ifs: %s has changed properties, "
"abandoning", ifsp->if_name);
remove_ifs(ifsp);
return (0);
}
/*
* canonize_ifs(): puts the interface in a canonical (zeroed) form
*
* input: struct ifslist *: the interface to canonize
* output: int: 1 on success, 0 on failure
*/
int
canonize_ifs(struct ifslist *ifsp)
{
struct sockaddr_in *sin;
struct ifreq ifr;
dhcpmsg(MSG_VERBOSE, "canonizing interface %s", ifsp->if_name);
/*
* note that due to infelicities in the routing code, any default
* routes must be removed prior to clearing the UP flag.
*/
remove_ifs_default_routes(ifsp);
/* LINTED [ifr_addr is a sockaddr which will be aligned] */
sin = (struct sockaddr_in *)&ifr.ifr_addr;
(void) memset(&ifr, 0, sizeof (struct ifreq));
(void) strlcpy(ifr.ifr_name, ifsp->if_name, IFNAMSIZ);
if (ioctl(ifsp->if_sock_fd, SIOCGIFFLAGS, &ifr) == -1)
return (0);
/*
* clear the UP flag, but don't clear DHCPRUNNING since
* that should only be done when the interface is removed
* (see remove_ifs())
*/
ifr.ifr_flags &= ~IFF_UP;
if (ioctl(ifsp->if_sock_fd, SIOCSIFFLAGS, &ifr) == -1)
return (0);
/*
* since ifr is actually a union, we need to explicitly zero
* the flags field before we reuse the structure, or otherwise
* cruft may leak over into other members of the union.
*/
ifr.ifr_flags = 0;
ifr.ifr_addr.sa_family = AF_INET;
sin->sin_addr.s_addr = htonl(INADDR_ANY);
if (ioctl(ifsp->if_sock_fd, SIOCSIFADDR, &ifr) == -1)
return (0);
if (ioctl(ifsp->if_sock_fd, SIOCSIFNETMASK, &ifr) == -1)
return (0);
if (ioctl(ifsp->if_sock_fd, SIOCSIFBRDADDR, &ifr) == -1)
return (0);
/*
* any time we change the IP address, netmask, or broadcast we
* must be careful to also reset bookkeeping of what these are
* set to. this is so we can detect if these characteristics
* are changed by another process.
*/
ifsp->if_addr.s_addr = htonl(INADDR_ANY);
ifsp->if_netmask.s_addr = htonl(INADDR_ANY);
ifsp->if_broadcast.s_addr = htonl(INADDR_ANY);
return (1);
}
/*
* check_ifs(): makes sure an ifs is still valid, and if it is, releases the
* ifs. otherwise, it informs the caller the ifs is going away
* and expects the caller to perform the release
*
* input: struct ifslist *: the ifs to check
* output: int: 1 if the interface is valid, 0 otherwise
*/
int
check_ifs(struct ifslist *ifsp)
{
hold_ifs(ifsp);
if (release_ifs(ifsp) == 1 || verify_ifs(ifsp) == 0) {
/*
* this interface is going away. if there's an
* uncancelled IPC event roaming around, cancel it
* now. we leave the hold on in case anyone else has
* any cleanup work that needs to be done before the
* interface goes away.
*/
ipc_action_finish(ifsp, DHCP_IPC_E_UNKIF);
async_finish(ifsp);
return (0);
}
(void) release_ifs(ifsp);
return (1);
}
/*
* nuke_ifslist(): delete the ifslist (for use when the dhcpagent is exiting)
*
* input: boolean_t: B_TRUE if the agent is exiting due to SIGTERM
* output: void
*/
void
nuke_ifslist(boolean_t onterm)
{
int status;
struct ifslist *ifsp, *ifsp_next;
for (ifsp = ifsheadp; ifsp != NULL; ifsp = ifsp_next) {
ifsp_next = ifsp->next;
cancel_ifs_timers(ifsp);
if (ifsp->if_script_pid != -1) {
/* stop a script if it is not for DROP or RELEASE */
if (strcmp(ifsp->if_script_event, EVENT_DROP) == 0 ||
strcmp(ifsp->if_script_event, EVENT_RELEASE) == 0) {
continue;
}
script_stop(ifsp);
}
/*
* if the script is started by script_start, dhcp_drop and
* dhcp_release should and will only be called after the
* script exits.
*/
if (onterm &&
df_get_bool(ifsp->if_name, DF_RELEASE_ON_SIGTERM)) {
if (script_start(ifsp, EVENT_RELEASE, dhcp_release,
"DHCP agent is exiting", &status) == 1) {
continue;
}
if (status == 1)
continue;
}
(void) script_start(ifsp, EVENT_DROP, dhcp_drop, NULL, NULL);
}
}
/*
* refresh_ifslist(): refreshes all finite leases under DHCP control
*
* input: iu_eh_t *: unused
* int: unused
* void *: unused
* output: void
*/
/* ARGSUSED */
void
refresh_ifslist(iu_eh_t *eh, int sig, void *arg)
{
struct ifslist *ifsp;
for (ifsp = ifsheadp; ifsp != NULL; ifsp = ifsp->next) {
if (ifsp->if_state != BOUND && ifsp->if_state != RENEWING &&
ifsp->if_state != REBINDING)
continue;
if (ifsp->if_lease == DHCP_PERM)
continue;
/*
* this interface has a finite lease and we do not know
* how long the machine's been off for. refresh it.
*/
dhcpmsg(MSG_WARNING, "refreshing lease on %s", ifsp->if_name);
cancel_ifs_timer(ifsp, DHCP_T1_TIMER);
cancel_ifs_timer(ifsp, DHCP_T2_TIMER);
(void) iu_adjust_timer(tq, ifsp->if_timer[DHCP_LEASE_TIMER], 0);
}
}
/*
* ifs_count(): returns the number of interfaces currently managed
*
* input: void
* output: unsigned int: the number of interfaces currently managed
*/
unsigned int
ifs_count(void)
{
return (ifscount);
}
/*
* cancel_ifs_timer(): cancels a lease-related timer on an interface
*
* input: struct ifslist *: the interface to operate on
* int: the timer id of the timer to cancel
* output: void
*/
static void
cancel_ifs_timer(struct ifslist *ifsp, int timer_id)
{
if (ifsp->if_timer[timer_id] != -1) {
if (iu_cancel_timer(tq, ifsp->if_timer[timer_id], NULL) == 1) {
(void) release_ifs(ifsp);
ifsp->if_timer[timer_id] = -1;
} else
dhcpmsg(MSG_WARNING, "cancel_ifs_timer: cannot cancel "
"if_timer[%d]", timer_id);
}
}
/*
* cancel_ifs_timers(): cancels an interface's pending lease-related timers
*
* input: struct ifslist *: the interface to operate on
* output: void
*/
void
cancel_ifs_timers(struct ifslist *ifsp)
{
cancel_ifs_timer(ifsp, DHCP_T1_TIMER);
cancel_ifs_timer(ifsp, DHCP_T2_TIMER);
cancel_ifs_timer(ifsp, DHCP_LEASE_TIMER);
}
/*
* schedule_ifs_timer(): schedules a lease-related timer on an interface
*
* input: struct ifslist *: the interface to operate on
* int: the timer to schedule
* uint32_t: the number of seconds in the future it should fire
* iu_tq_callback_t *: the callback to call upon firing
* output: int: 1 if the timer was scheduled successfully, 0 on failure
*/
int
schedule_ifs_timer(struct ifslist *ifsp, int timer_id, uint32_t sec,
iu_tq_callback_t *expire)
{
cancel_ifs_timer(ifsp, timer_id); /* just in case */
ifsp->if_timer[timer_id] = iu_schedule_timer(tq, sec, expire, ifsp);
if (ifsp->if_timer[timer_id] == -1) {
dhcpmsg(MSG_WARNING, "schedule_ifs_timer: cannot schedule "
"if_timer[%d]", timer_id);
return (0);
}
hold_ifs(ifsp);
return (1);
}
/*
* Get the value of the named property on the named node in devinfo root.
*
* input: const char *: The name of the node containing the property.
* const char *: The name of the property.
* uchar_t **: The property value, modified iff B_TRUE is returned.
* If no value is found the value is set to NULL.
* unsigned int *: The length of the property value
* output: boolean_t: Returns B_TRUE if successful (no problems),
* otherwise B_FALSE.
* note: The memory allocated by this function must be freed by
* the caller. This code is derived from
* usr/src/lib/libwanboot/common/bootinfo_aux.c.
*/
static boolean_t
get_prom_prop(const char *nodename, const char *propname, uchar_t **propvaluep,
unsigned int *lenp)
{
di_node_t root_node = DI_NODE_NIL;
di_node_t node;
di_prom_handle_t phdl = DI_PROM_HANDLE_NIL;
di_prom_prop_t pp;
uchar_t *value = NULL;
unsigned int len = 0;
boolean_t success = B_TRUE;
/*
* locate root node
*/
if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL ||
(phdl = di_prom_init()) == DI_PROM_HANDLE_NIL) {
dhcpmsg(MSG_DEBUG, "get_prom_prop: property root node "
"not found");
goto get_prom_prop_cleanup;
}
/*
* locate nodename within '/'
*/
for (node = di_child_node(root_node);
node != DI_NODE_NIL;
node = di_sibling_node(node)) {
if (strcmp(di_node_name(node), nodename) == 0) {
break;
}
}
if (node == DI_NODE_NIL) {
dhcpmsg(MSG_DEBUG, "get_prom_prop: node not found");
goto get_prom_prop_cleanup;
}
/*
* scan all properties of /nodename for the 'propname' property
*/
for (pp = di_prom_prop_next(phdl, node, DI_PROM_PROP_NIL);
pp != DI_PROM_PROP_NIL;
pp = di_prom_prop_next(phdl, node, pp)) {
dhcpmsg(MSG_DEBUG, "get_prom_prop: property = %s",
di_prom_prop_name(pp));
if (strcmp(propname, di_prom_prop_name(pp)) == 0) {
break;
}
}
if (pp == DI_PROM_PROP_NIL) {
dhcpmsg(MSG_DEBUG, "get_prom_prop: property not found");
goto get_prom_prop_cleanup;
}
/*
* get the property; allocate some memory copy it out
*/
len = di_prom_prop_data(pp, (uchar_t **)&value);
if (value == NULL) {
/*
* property data read problems
*/
success = B_FALSE;
dhcpmsg(MSG_ERR, "get_prom_prop: cannot read property data");
goto get_prom_prop_cleanup;
}
if (propvaluep != NULL) {
/*
* allocate somewhere to copy the property value to
*/
*propvaluep = calloc(len, sizeof (uchar_t));
if (*propvaluep == NULL) {
/*
* allocation problems
*/
success = B_FALSE;
dhcpmsg(MSG_ERR, "get_prom_prop: cannot allocate "
"memory for property value");
goto get_prom_prop_cleanup;
}
/*
* copy data out
*/
(void) memcpy(*propvaluep, value, len);
/*
* copy out the length if a suitable pointer has
* been supplied
*/
if (lenp != NULL) {
*lenp = len;
}
dhcpmsg(MSG_DEBUG, "get_prom_prop: property value "
"length = %d", len);
}
get_prom_prop_cleanup:
if (phdl != DI_PROM_HANDLE_NIL) {
di_prom_fini(phdl);
}
if (root_node != DI_NODE_NIL) {
di_fini(root_node);
}
return (success);
}
|