-
Notifications
You must be signed in to change notification settings - Fork 22
/
in6.c
2662 lines (2415 loc) · 68.8 KB
/
in6.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
/* $KAME: in6.c,v 1.91 2000/07/04 05:50:13 jinmei Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* 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. Neither the name of the project 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 PROJECT 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 PROJECT 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.
*/
/*
* Copyright (c) 1982, 1986, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* 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, Berkeley 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.
*
* @(#)in.c 8.2 (Berkeley) 11/15/93
*/
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
#include "opt_inet.h"
#include "opt_inet6.h"
#endif
#ifdef __NetBSD__
#include "opt_inet.h"
#endif
#include <sys/param.h>
#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
#include <sys/ioctl.h>
#endif
#include <sys/errno.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sockio.h>
#include <sys/systm.h>
#if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3)
#include <sys/proc.h>
#endif
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/syslog.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/route.h>
#include <net/if_dl.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#ifdef __NetBSD__
#include <net/if_ether.h>
#else
#include <netinet/if_ether.h>
#endif
#include <netinet6/nd6.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet6/mld6_var.h>
#include <netinet6/ip6_mroute.h>
#include <netinet6/in6_ifattach.h>
#include <netinet6/scope6_var.h>
#include "gif.h"
#if NGIF > 0
#include <net/if_gif.h>
#endif
#include <net/net_osdep.h>
#ifdef MIP6
#include <netinet6/mip6.h>
#include <netinet6/mip6_common.h>
struct nd_prefix *(*mip6_get_home_prefix_hook) __P((void));
#endif /* MIP6 */
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
MALLOC_DEFINE(M_IPMADDR, "in6_multi", "internet multicast address");
#endif
/*
* Definitions of some costant IP6 addresses.
*/
const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
const struct in6_addr in6addr_nodelocal_allnodes =
IN6ADDR_NODELOCAL_ALLNODES_INIT;
const struct in6_addr in6addr_linklocal_allnodes =
IN6ADDR_LINKLOCAL_ALLNODES_INIT;
const struct in6_addr in6addr_linklocal_allrouters =
IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
const struct in6_addr in6mask0 = IN6MASK0;
const struct in6_addr in6mask32 = IN6MASK32;
const struct in6_addr in6mask64 = IN6MASK64;
const struct in6_addr in6mask96 = IN6MASK96;
const struct in6_addr in6mask128 = IN6MASK128;
#if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3)
static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
struct ifnet *, struct proc *));
#else
static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
struct ifnet *));
#endif
static int in6_ifaddroute __P((struct ifnet *, struct in6_ifaddr *));
static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
struct in6_multihead in6_multihead; /* XXX BSS initialization */
#else
/*
* This structure is used to keep track of in6_multi chains which belong to
* deleted interface addresses.
*/
static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */
struct multi6_kludge {
LIST_ENTRY(multi6_kludge) mk_entry;
struct ifnet *mk_ifp;
struct in6_multihead mk_head;
};
#endif
/*
* Check if the loopback entry will be automatically generated.
* if 0 returned, will not be automatically generated.
* if 1 returned, will be automatically generated.
*/
static int
in6_is_ifloop_auto(struct ifaddr *ifa)
{
#ifdef __OpenBSD__
return 0;
#else
#define SIN6(s) ((struct sockaddr_in6 *)s)
/*
* If RTF_CLONING is unset, or (IFF_LOOPBACK | IFF_POINTOPOINT),
* or netmask is all0 or all1, then cloning will not happen,
* then we can't rely on its loopback entry generation.
*/
if ((ifa->ifa_flags & RTF_CLONING) == 0 ||
(ifa->ifa_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) ||
(SIN6(ifa->ifa_netmask)->sin6_len == sizeof(struct sockaddr_in6)
&&
IN6_ARE_ADDR_EQUAL(&SIN6(ifa->ifa_netmask)->sin6_addr,
&in6mask128)) ||
((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_len == 0)
return 0;
else
return 1;
#undef SIN6
#endif
}
/*
* Subroutine for in6_ifaddloop() and in6_ifremloop().
* This routine does actual work.
*/
static void
in6_ifloop_request(int cmd, struct ifaddr *ifa)
{
struct sockaddr_in6 lo_sa;
struct sockaddr_in6 all1_sa;
struct rtentry *nrt = NULL;
bzero(&lo_sa, sizeof(lo_sa));
bzero(&all1_sa, sizeof(all1_sa));
lo_sa.sin6_family = AF_INET6;
lo_sa.sin6_len = sizeof(struct sockaddr_in6);
all1_sa = lo_sa;
lo_sa.sin6_addr = in6addr_loopback;
all1_sa.sin6_addr = in6mask128;
/*
* So we add or remove static loopback entry, here.
* This request for deletion could fail, e.g. when we remove
* an address right after adding it.
*/
rtrequest(cmd, ifa->ifa_addr,
(struct sockaddr *)&lo_sa,
(struct sockaddr *)&all1_sa,
RTF_UP|RTF_HOST, &nrt);
/*
* Make sure rt_ifa be equal to IFA, the second argument of the
* function.
* We need this because when we refer rt_ifa->ia6_flags in ip6_input,
* we assume that the rt_ifa points to the address instead of the
* loopback address.
*/
if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
IFAFREE(nrt->rt_ifa);
ifa->ifa_refcnt++;
nrt->rt_ifa = ifa;
}
if (nrt)
nrt->rt_refcnt--;
}
/*
* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link).
* Because, KAME needs loopback rtentry for ownaddr check in
* ip6_input().
*/
static void
in6_ifaddloop(struct ifaddr *ifa)
{
if (!in6_is_ifloop_auto(ifa)) {
struct rtentry *rt;
/* If there is no loopback entry, allocate one. */
rt = rtalloc1(ifa->ifa_addr, 0
#ifdef __FreeBSD__
, 0
#endif /* __FreeBSD__ */
);
if (rt == 0 || (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
in6_ifloop_request(RTM_ADD, ifa);
if (rt)
rt->rt_refcnt--;
}
}
/*
* Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
* if it exists.
*/
static void
in6_ifremloop(struct ifaddr *ifa)
{
struct in6_ifaddr *ia;
int ia_count = 0;
/*
* All BSD variants except BSD/OS do not remove cloned routes
* from an interface direct route, when removing the direct route
* (see comments in net/net_osdep.h). Even BSD/OS could fail to remove
* the cloned routes when we handle multple addresses that share a
* common prefix.
* So, we should remove the route corresponding to the deleted address
* regardless of the result of in6_is_ifloop_auto().
*/
/*
* If only one ifa for the loopback entry, delete it.
* (XXX Is this really necessary? jinmei@kame.net 20000613)
*/
for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
ia_count++;
if (ia_count > 1)
break;
}
}
if (ia_count == 1)
in6_ifloop_request(RTM_DELETE, ifa);
}
int
in6_ifindex2scopeid(idx)
int idx;
{
struct ifnet *ifp;
struct ifaddr *ifa;
struct sockaddr_in6 *sin6;
if (idx < 0 || if_index < idx)
return -1;
ifp = ifindex2ifnet[idx];
#if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
#elif defined(__FreeBSD__) && __FreeBSD__ >= 4
TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
#else
for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
#endif
{
if (ifa->ifa_addr->sa_family != AF_INET6)
continue;
sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
return sin6->sin6_scope_id & 0xffff;
}
return -1;
}
int
in6_mask2len(mask)
struct in6_addr *mask;
{
int x, y;
for (x = 0; x < sizeof(*mask); x++) {
if (mask->s6_addr8[x] != 0xff)
break;
}
y = 0;
if (x < sizeof(*mask)) {
for (y = 0; y < 8; y++) {
if ((mask->s6_addr8[x] & (0x80 >> y)) == 0)
break;
}
}
return x * 8 + y;
}
void
in6_len2mask(mask, len)
struct in6_addr *mask;
int len;
{
int i;
bzero(mask, sizeof(*mask));
for (i = 0; i < len / 8; i++)
mask->s6_addr8[i] = 0xff;
if (len % 8)
mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
}
#define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
#define ia62ifa(ia6) (&((ia6)->ia_ifa))
int
#if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3)
in6_control(so, cmd, data, ifp, p)
struct socket *so;
u_long cmd;
caddr_t data;
struct ifnet *ifp;
struct proc *p;
#else
in6_control(so, cmd, data, ifp)
struct socket *so;
u_long cmd;
caddr_t data;
struct ifnet *ifp;
#endif
{
struct in6_ifreq *ifr = (struct in6_ifreq *)data;
struct ifaddr *ifa;
struct in6_ifaddr *ia = NULL, *oia;
struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
#ifdef COMPAT_IN6IFIOCTL
struct sockaddr_in6 oldaddr;
#endif
struct sockaddr_in6 dst6; /* XXX: used in very limited place only */
#ifdef COMPAT_IN6IFIOCTL
struct sockaddr_in6 net;
#endif
int error = 0, hostIsNew, prefixIsNew;
int newifaddr;
#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
time_t time_second = (time_t)time.tv_sec;
#endif
int privileged;
int plen; /* XXX: used in very limited place only */
privileged = 0;
#if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD__ == 3)
if (p && !suser(p->p_ucred, &p->p_acflag))
privileged++;
#elif (defined(__FreeBSD__) && __FreeBSD__ >= 4)
if (p == NULL || !suser(p))
privileged++;
#else
if ((so->so_state & SS_PRIV) != 0)
privileged++;
#endif
/*
* xxx should prevent processes for link-local addresses?
*/
#if NGIF > 0
if (ifp && ifp->if_type == IFT_GIF) {
switch (cmd) {
case SIOCSIFPHYADDR_IN6:
if (!privileged)
return(EPERM);
/*fall through*/
case SIOCGIFPSRCADDR_IN6:
case SIOCGIFPDSTADDR_IN6:
return gif_ioctl(ifp, cmd, data);
}
}
#endif
switch (cmd) {
case SIOCGETSGCNT_IN6:
case SIOCGETMIFCNT_IN6:
return (mrt6_ioctl(cmd, data));
}
#ifdef MIP6
/* These require root privileges */
switch (cmd) {
case SIOCSDEBUG_MIP6:
case SIOCSBCFLUSH_MIP6:
case SIOCSDEFCONFIG_MIP6:
case SIOCSBRUPDATE_MIP6:
case SIOCSENABLEBR_MIP6:
case SIOCSATTACH_MIP6:
case SIOCSRELEASE_MIP6:
case SIOCSHALISTFLUSH_MIP6:
case SIOCSHAPREF_MIP6:
case SIOCSFWDSLUNICAST_MIP6:
case SIOCSFWDSLMULTICAST_MIP6:
case SIOCSFORADDRFLUSH_MIP6:
case SIOCSHADDRFLUSH_MIP6:
case SIOCSBULISTFLUSH_MIP6:
case SIOCACOADDR_MIP6:
case SIOCAHOMEADDR_MIP6:
case SIOCSBULIFETIME_MIP6:
case SIOCSHRLIFETIME_MIP6:
case SIOCDCOADDR_MIP6:
case SIOCSPROMMODE_MIP6:
case SIOCSBU2CN_MIP6:
case SIOCSREVTUNNEL_MIP6:
case SIOCSAUTOCONFIG_MIP6:
case SIOCSEAGERMD_MIP6:
if (!privileged)
return(EPERM);
/* Anyone can use these or the user is root */
/* case SIOCXVERYSAFECOMMAND_MIP6: */
#if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3)
return mip6_ioctl(so, cmd, data, ifp, p);
#else
return mip6_ioctl(so, cmd, data, ifp);
#endif
}
#endif /* MIP6 */
if (ifp == NULL)
return(EOPNOTSUPP);
switch (cmd) {
case SIOCSNDFLUSH_IN6:
case SIOCSPFXFLUSH_IN6:
case SIOCSRTRFLUSH_IN6:
case SIOCSDEFIFACE_IN6:
case SIOCSIFINFO_FLAGS:
if (!privileged)
return(EPERM);
/*fall through*/
case SIOCGIFINFO_IN6:
case SIOCGDRLST_IN6:
case SIOCGPRLST_IN6:
case SIOCGNBRINFO_IN6:
case SIOCGDEFIFACE_IN6:
return(nd6_ioctl(cmd, data, ifp));
}
switch (cmd) {
case SIOCSIFPREFIX_IN6:
case SIOCDIFPREFIX_IN6:
case SIOCAIFPREFIX_IN6:
case SIOCCIFPREFIX_IN6:
case SIOCSGIFPREFIX_IN6:
if (!privileged)
return(EPERM);
/*fall through*/
case SIOCGIFPREFIX_IN6:
if (ip6_forwarding == 0)
return(EPERM);
return(in6_prefix_ioctl(so, cmd, data, ifp));
}
switch(cmd) {
case SIOCSSCOPE6:
if (!privileged)
return(EPERM);
return(scope6_set(ifp, ifr->ifr_ifru.ifru_scope_id));
break;
case SIOCGSCOPE6:
return(scope6_get(ifp, ifr->ifr_ifru.ifru_scope_id));
break;
case SIOCGSCOPE6DEF:
return(scope6_get_default(ifr->ifr_ifru.ifru_scope_id));
break;
}
switch (cmd) {
case SIOCALIFADDR:
case SIOCDLIFADDR:
if (!privileged)
return(EPERM);
/*fall through*/
case SIOCGLIFADDR:
#if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3)
return in6_lifaddr_ioctl(so, cmd, data, ifp, p);
#else
return in6_lifaddr_ioctl(so, cmd, data, ifp);
#endif
}
/*
* Find address for this interface, if it exists.
*/
if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
struct sockaddr_in6 *sa6 =
(struct sockaddr_in6 *)&ifra->ifra_addr;
if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
if (sa6->sin6_addr.s6_addr16[1] == 0) {
/* link ID is not embedded by the user */
sa6->sin6_addr.s6_addr16[1] =
htons(ifp->if_index);
} else if (sa6->sin6_addr.s6_addr16[1] !=
htons(ifp->if_index)) {
return(EINVAL); /* link ID is contradict */
}
if (sa6->sin6_scope_id) {
if (sa6->sin6_scope_id !=
(u_int32_t)ifp->if_index)
return(EINVAL);
sa6->sin6_scope_id = 0; /* XXX: good way? */
}
}
ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
}
switch (cmd) {
case SIOCDIFADDR_IN6:
/*
* for IPv4, we look for existing in_ifaddr here to allow
* "ifconfig if0 delete" to remove first IPv4 address on the
* interface. For IPv6, as the spec allow multiple interface
* address from the day one, we consider "remove the first one"
* semantics to be not preferable.
*/
if (ia == NULL)
return(EADDRNOTAVAIL);
/* FALLTHROUGH */
case SIOCAIFADDR_IN6:
case SIOCSIFADDR_IN6:
#ifdef COMPAT_IN6IFIOCTL
case SIOCSIFDSTADDR_IN6:
case SIOCSIFNETMASK_IN6:
/*
* Since IPv6 allows a node to assign multiple addresses
* on a single interface, SIOCSIFxxx ioctls are not suitable
* and should be unused.
*/
#endif
/*
* We always require users to specify a valid IPv6 address for
* the corresponding operation.
*/
if (ifra->ifra_addr.sin6_family != AF_INET6 ||
ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
return(EAFNOSUPPORT);
if (!privileged)
return(EPERM);
if (ia == NULL) {
ia = (struct in6_ifaddr *)
malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
if (ia == NULL)
return (ENOBUFS);
bzero((caddr_t)ia, sizeof(*ia));
/* Initialize the address and masks */
ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
ia->ia_addr.sin6_family = AF_INET6;
ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
#if 1
if (ifp->if_flags & IFF_POINTOPOINT) {
ia->ia_ifa.ifa_dstaddr
= (struct sockaddr *)&ia->ia_dstaddr;
ia->ia_dstaddr.sin6_family = AF_INET6;
ia->ia_dstaddr.sin6_len = sizeof(ia->ia_dstaddr);
} else {
ia->ia_ifa.ifa_dstaddr = NULL;
bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr));
}
#else /* always initilize by NULL */
ia->ia_ifa.ifa_dstaddr = NULL;
bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr));
#endif
ia->ia_ifa.ifa_netmask
= (struct sockaddr *)&ia->ia_prefixmask;
ia->ia_ifp = ifp;
if ((oia = in6_ifaddr) != NULL) {
for ( ; oia->ia_next; oia = oia->ia_next)
continue;
oia->ia_next = ia;
} else
in6_ifaddr = ia;
/* gain a refcnt for the link from in6_ifaddr */
ia->ia_ifa.ifa_refcnt++;
#if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
if ((ifa = ifp->if_addrlist) != NULL) {
for ( ; ifa->ifa_next; ifa = ifa->ifa_next)
continue;
ifa->ifa_next = ia62ifa(ia);
} else
ifp->if_addrlist = ia62ifa(ia);
#else
TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
ifa_list);
#endif
/* gain another refcnt for the link from if_addrlist */
ia->ia_ifa.ifa_refcnt++;
newifaddr = 1;
} else
newifaddr = 0;
if (cmd == SIOCAIFADDR_IN6) {
/* sanity for overflow - beware unsigned */
struct in6_addrlifetime *lt;
lt = &ifra->ifra_lifetime;
if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
&& lt->ia6t_vltime + time_second < time_second) {
return EINVAL;
}
if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
&& lt->ia6t_pltime + time_second < time_second) {
return EINVAL;
}
}
break;
case SIOCGIFADDR_IN6:
/* This interface is basically deprecated. use SIOCGIFCONF. */
/* fall through */
case SIOCGIFAFLAG_IN6:
case SIOCGIFNETMASK_IN6:
case SIOCGIFDSTADDR_IN6:
case SIOCGIFALIFETIME_IN6:
/* must think again about its semantics */
if (ia == NULL)
return(EADDRNOTAVAIL);
break;
case SIOCSIFALIFETIME_IN6:
{
struct in6_addrlifetime *lt;
if (!privileged)
return(EPERM);
if (ia == NULL)
return(EADDRNOTAVAIL);
/* sanity for overflow - beware unsigned */
lt = &ifr->ifr_ifru.ifru_lifetime;
if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
&& lt->ia6t_vltime + time_second < time_second) {
return EINVAL;
}
if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
&& lt->ia6t_pltime + time_second < time_second) {
return EINVAL;
}
break;
}
}
switch (cmd) {
case SIOCGIFADDR_IN6:
ifr->ifr_addr = ia->ia_addr;
break;
case SIOCGIFDSTADDR_IN6:
if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
return(EINVAL);
/*
* XXX: should we check if ifa_dstaddr is NULL and return
* an error?
*/
ifr->ifr_dstaddr = ia->ia_dstaddr;
break;
case SIOCGIFNETMASK_IN6:
ifr->ifr_addr = ia->ia_prefixmask;
break;
case SIOCGIFAFLAG_IN6:
ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
break;
case SIOCGIFSTAT_IN6:
if (ifp == NULL)
return EINVAL;
if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax
|| in6_ifstat[ifp->if_index] == NULL) {
/* return EAFNOSUPPORT? */
bzero(&ifr->ifr_ifru.ifru_stat,
sizeof(ifr->ifr_ifru.ifru_stat));
} else
ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index];
break;
case SIOCGIFSTAT_ICMP6:
if (ifp == NULL)
return EINVAL;
if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax ||
icmp6_ifstat[ifp->if_index] == NULL) {
/* return EAFNOSUPPORT? */
bzero(&ifr->ifr_ifru.ifru_stat,
sizeof(ifr->ifr_ifru.ifru_icmp6stat));
} else
ifr->ifr_ifru.ifru_icmp6stat =
*icmp6_ifstat[ifp->if_index];
break;
#ifdef COMPAT_IN6IFIOCTL /* should be unused */
case SIOCSIFDSTADDR_IN6:
if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
return(EINVAL);
oldaddr = ia->ia_dstaddr;
ia->ia_dstaddr = ifr->ifr_dstaddr;
/* link-local index check */
if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
/* interface ID is not embedded by the user */
ia->ia_dstaddr.sin6_addr.s6_addr16[1]
= htons(ifp->if_index);
} else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
htons(ifp->if_index)) {
ia->ia_dstaddr = oldaddr;
return(EINVAL); /* ifid is contradict */
}
}
if (ifp->if_ioctl && (error = (ifp->if_ioctl)
(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
ia->ia_dstaddr = oldaddr;
return(error);
}
ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
if (ia->ia_flags & IFA_ROUTE) {
ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
ia->ia_ifa.ifa_dstaddr =
(struct sockaddr *)&ia->ia_dstaddr;
rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
}
break;
#endif
case SIOCGIFALIFETIME_IN6:
ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
break;
case SIOCSIFALIFETIME_IN6:
ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
/* for sanity */
if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
ia->ia6_lifetime.ia6t_expire =
time_second + ia->ia6_lifetime.ia6t_vltime;
} else
ia->ia6_lifetime.ia6t_expire = 0;
if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
ia->ia6_lifetime.ia6t_preferred =
time_second + ia->ia6_lifetime.ia6t_pltime;
} else
ia->ia6_lifetime.ia6t_preferred = 0;
break;
case SIOCSIFADDR_IN6:
/* XXX: should be obsoleted */
error = in6_ifinit(ifp, ia, &ifr->ifr_addr, 1, 1, 1);
#if 0
/*
* the code chokes if we are to assign multiple addresses with
* the same address prefix (rtinit() will return EEXIST, which
* is not fatal actually). we will get memory leak if we
* don't do it.
* -> we may want to hide EEXIST from rtinit().
*/
undo:
if (error && newifaddr) {
#if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
if ((ifa = ifp->if_addrlist) == ia62ifa(ia))
ifp->if_addrlist = ifa->ifa_next;
else {
while (ifa->ifa_next &&
(ifa->ifa_next != ia62ifa(ia)))
ifa = ifa->ifa_next;
if (ifa->ifa_next)
ifa->ifa_next = ia62ifa(ia)->ifa_next;
else {
printf("Couldn't unlink in6_ifaddr "
"from ifp\n");
}
}
#else
TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
#endif
/* release a refcnt for the link from if_addrlist */
IFAFREE(&ia->ia_ifa);
oia = ia;
if (oia == (ia = in6_ifaddr))
in6_ifaddr = ia->ia_next;
else {
while (ia->ia_next && (ia->ia_next != oia))
ia = ia->ia_next;
if (ia->ia_next)
ia->ia_next = oia->ia_next;
else {
printf("Didn't unlink in6_ifaddr "
"from list\n");
}
}
/* release another refcnt for the link from in6_ifaddr */
IFAFREE(&oia->ia_ifa);
}
#endif
return error;
#ifdef COMPAT_IN6IFIOCTL /* XXX should be unused */
case SIOCSIFNETMASK_IN6:
ia->ia_prefixmask = ifr->ifr_addr;
bzero(&net, sizeof(net));
net.sin6_len = sizeof(struct sockaddr_in6);
net.sin6_family = AF_INET6;
net.sin6_port = htons(0);
net.sin6_flowinfo = htonl(0);
net.sin6_addr.s6_addr32[0]
= ia->ia_addr.sin6_addr.s6_addr32[0] &
ia->ia_prefixmask.sin6_addr.s6_addr32[0];
net.sin6_addr.s6_addr32[1]
= ia->ia_addr.sin6_addr.s6_addr32[1] &
ia->ia_prefixmask.sin6_addr.s6_addr32[1];
net.sin6_addr.s6_addr32[2]
= ia->ia_addr.sin6_addr.s6_addr32[2] &
ia->ia_prefixmask.sin6_addr.s6_addr32[2];
net.sin6_addr.s6_addr32[3]
= ia->ia_addr.sin6_addr.s6_addr32[3] &
ia->ia_prefixmask.sin6_addr.s6_addr32[3];
ia->ia_net = net;
break;
#endif
case SIOCAIFADDR_IN6:
prefixIsNew = 0;
hostIsNew = 1;
/* Validate parameters */
/*
* The destination address for a p2p link must have a family
* of AF_UNSPEC or AF_INET6.
*/
if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) {
error = EAFNOSUPPORT;
goto unlink;
}
/*
* The prefixmask must have a family of AF_UNSPEC or AF_INET6.
*/
if (ifra->ifra_prefixmask.sin6_family != AF_INET6 &&
ifra->ifra_prefixmask.sin6_family != AF_UNSPEC) {
error = EAFNOSUPPORT;
goto unlink;
}
/*
* Because the IPv6 address architecture is classless,
* prefix length must be specified for a new address.
*/
if (hostIsNew && ifra->ifra_prefixmask.sin6_len == 0) {
error = EINVAL;
goto unlink;
}
/*
* If the destination address on a p2p interface is specified,
* and the address is a scoped one, validate/set the scope
* zone identifier.
* XXX: currently, only link-local address is supported.
*/
dst6 = ifra->ifra_dstaddr;
if ((ifp->if_flags & IFF_POINTOPOINT) &&
(dst6.sin6_family == AF_INET6)) {
if (IN6_IS_ADDR_LINKLOCAL(&dst6.sin6_addr)) {
if (dst6.sin6_addr.s6_addr16[1] == 0) {
/*
* link ID is not embedded by the user.
*/
dst6.sin6_addr.s6_addr16[1]
= htons(ifp->if_index);
} else if (dst6.sin6_addr.s6_addr16[1] !=
htons(ifp->if_index)) {
/* link id is contradict */
error = EINVAL;
goto unlink;
}
}
}
/* check if a new address is being added */
if (ia->ia_addr.sin6_len &&
IN6_ARE_ADDR_EQUAL(&ifra->ifra_addr.sin6_addr,
&ia->ia_addr.sin6_addr)) {
/*
* The requested address is already in the list.
* This means the user just wants to modify some
* parameters of the existing address.
*/
hostIsNew = 0;
}
/* calculate the prefix length, which will be used later */
if (ifra->ifra_prefixmask.sin6_len)
plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr);
else
plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr);
/* set prefix mask */
if (ifra->ifra_prefixmask.sin6_len) {
/*
* If the prefix length for an existing address
* changes, (which should be rare in the current IPv6
* address architecture, though) scrub the route
* corresponding to the old prefix.
*/
if (ia->ia_prefixmask.sin6_len &&
in6_mask2len(&ia->ia_prefixmask.sin6_addr) != plen)
in6_ifscrub(ifp, ia, 0);
ia->ia_prefixmask = ifra->ifra_prefixmask;
}
/*
* Check if we have another address that shares the same prefix
* of the address being configured.
*/
#if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
#elif defined(__FreeBSD__) && __FreeBSD__ >= 4
TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
#else
for (ifa = ifp->if_addrlist.tqh_first; ifa;
ifa = ifa->ifa_list.tqe_next)
#endif
{
struct in6_ifaddr *ifa6;
if (ifa->ifa_addr->sa_family != AF_INET6)
continue;
if (ifa == (struct ifaddr *)ia)
continue;
ifa6 = (struct in6_ifaddr *)ifa;