-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathin.c
1883 lines (1625 loc) · 48 KB
/
in.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-3-Clause
*
* Copyright (c) 1982, 1986, 1991, 1993
* The Regents of the University of California. All rights reserved.
* Copyright (C) 2001 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 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.
*/
#include <sys/cdefs.h>
#include "opt_inet.h"
#define IN_HISTORICAL_NETS /* include class masks */
#include <sys/param.h>
#include <sys/eventhandler.h>
#include <sys/systm.h>
#include <sys/sockio.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <sys/socket.h>
#include <sys/jail.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>
#include <sys/sx.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_arp.h>
#include <net/if_dl.h>
#include <net/if_llatbl.h>
#include <net/if_private.h>
#include <net/if_types.h>
#include <net/route.h>
#include <net/route/nhop.h>
#include <net/route/route_ctl.h>
#include <net/vnet.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <netinet/in_fib.h>
#include <netinet/in_var.h>
#include <netinet/in_pcb.h>
#include <netinet/ip_var.h>
#include <netinet/ip_carp.h>
#include <netinet/igmp_var.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#ifdef MAC
#include <security/mac/mac_framework.h>
#endif
static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *);
static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *);
static int in_gifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *);
static void in_socktrim(struct sockaddr_in *);
static void in_purgemaddrs(struct ifnet *);
static bool ia_need_loopback_route(const struct in_ifaddr *);
VNET_DEFINE_STATIC(int, nosameprefix);
#define V_nosameprefix VNET(nosameprefix)
SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(nosameprefix), 0,
"Refuse to create same prefixes on different interfaces");
VNET_DEFINE_STATIC(bool, broadcast_lowest);
#define V_broadcast_lowest VNET(broadcast_lowest)
SYSCTL_BOOL(_net_inet_ip, OID_AUTO, broadcast_lowest, CTLFLAG_VNET | CTLFLAG_RW,
&VNET_NAME(broadcast_lowest), 0,
"Treat lowest address on a subnet (host 0) as broadcast");
VNET_DEFINE(bool, ip_allow_net240) = false;
#define V_ip_allow_net240 VNET(ip_allow_net240)
SYSCTL_BOOL(_net_inet_ip, OID_AUTO, allow_net240,
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_allow_net240), 0,
"Allow forwarding of and ICMP response to Experimental addresses, aka Class E (240/4)");
/* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-240 */
VNET_DEFINE(bool, ip_allow_net0) = false;
SYSCTL_BOOL(_net_inet_ip, OID_AUTO, allow_net0,
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_allow_net0), 0,
"Allow forwarding of and ICMP response to addresses in network 0/8");
/* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-0 */
VNET_DEFINE(uint32_t, in_loopback_mask) = IN_LOOPBACK_MASK_DFLT;
#define V_in_loopback_mask VNET(in_loopback_mask)
static int sysctl_loopback_prefixlen(SYSCTL_HANDLER_ARGS);
SYSCTL_PROC(_net_inet_ip, OID_AUTO, loopback_prefixlen,
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
NULL, 0, sysctl_loopback_prefixlen, "I",
"Prefix length of address space reserved for loopback");
/* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-127 */
VNET_DECLARE(struct inpcbinfo, ripcbinfo);
#define V_ripcbinfo VNET(ripcbinfo)
static struct sx in_control_sx;
SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
/*
* Return 1 if an internet address is for a ``local'' host
* (one to which we have a connection).
*/
int
in_localaddr(struct in_addr in)
{
u_long i = ntohl(in.s_addr);
struct in_ifaddr *ia;
NET_EPOCH_ASSERT();
CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
if ((i & ia->ia_subnetmask) == ia->ia_subnet)
return (1);
}
return (0);
}
/*
* Return 1 if an internet address is for the local host and configured
* on one of its interfaces.
*/
bool
in_localip(struct in_addr in)
{
struct in_ifaddr *ia;
NET_EPOCH_ASSERT();
CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash)
if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
return (true);
return (false);
}
/*
* Like in_localip(), but FIB-aware and carp(4)-aware.
*/
bool
in_localip_fib(struct in_addr in, uint16_t fib)
{
struct in_ifaddr *ia;
NET_EPOCH_ASSERT();
CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash)
if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr &&
(ia->ia_ifa.ifa_carp == NULL ||
carp_master_p(&ia->ia_ifa)) &&
ia->ia_ifa.ifa_ifp->if_fib == fib)
return (true);
return (false);
}
/*
* Return 1 if an internet address is configured on an interface.
*/
int
in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
{
struct ifaddr *ifa;
struct in_ifaddr *ia;
NET_EPOCH_ASSERT();
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
ia = (struct in_ifaddr *)ifa;
if (ia->ia_addr.sin_addr.s_addr == in.s_addr)
return (1);
}
return (0);
}
/*
* Return a reference to the interface address which is different to
* the supplied one but with same IP address value.
*/
static struct in_ifaddr *
in_localip_more(struct in_ifaddr *original_ia)
{
struct epoch_tracker et;
in_addr_t original_addr = IA_SIN(original_ia)->sin_addr.s_addr;
uint32_t original_fib = original_ia->ia_ifa.ifa_ifp->if_fib;
struct in_ifaddr *ia;
NET_EPOCH_ENTER(et);
CK_LIST_FOREACH(ia, INADDR_HASH(original_addr), ia_hash) {
in_addr_t addr = IA_SIN(ia)->sin_addr.s_addr;
uint32_t fib = ia->ia_ifa.ifa_ifp->if_fib;
if (!V_rt_add_addr_allfibs && (original_fib != fib))
continue;
if ((original_ia != ia) && (original_addr == addr)) {
ifa_ref(&ia->ia_ifa);
NET_EPOCH_EXIT(et);
return (ia);
}
}
NET_EPOCH_EXIT(et);
return (NULL);
}
/*
* Tries to find first IPv4 address in the provided fib.
* Prefers non-loopback addresses and return loopback IFF
* @loopback_ok is set.
*
* Returns ifa or NULL.
*/
struct in_ifaddr *
in_findlocal(uint32_t fibnum, bool loopback_ok)
{
struct in_ifaddr *ia = NULL, *ia_lo = NULL;
NET_EPOCH_ASSERT();
CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
uint32_t ia_fib = ia->ia_ifa.ifa_ifp->if_fib;
if (!V_rt_add_addr_allfibs && (fibnum != ia_fib))
continue;
if (!IN_LOOPBACK(ntohl(IA_SIN(ia)->sin_addr.s_addr)))
break;
if (loopback_ok)
ia_lo = ia;
}
if (ia == NULL)
ia = ia_lo;
return (ia);
}
/*
* Determine whether an IP address is in a reserved set of addresses
* that may not be forwarded, or whether datagrams to that destination
* may be forwarded.
*/
int
in_canforward(struct in_addr in)
{
u_long i = ntohl(in.s_addr);
if (IN_MULTICAST(i) || IN_LINKLOCAL(i) || IN_LOOPBACK(i))
return (0);
if (IN_EXPERIMENTAL(i) && !V_ip_allow_net240)
return (0);
if (IN_ZERONET(i) && !V_ip_allow_net0)
return (0);
return (1);
}
/*
* Sysctl to manage prefix of reserved loopback network; translate
* to/from mask. The mask is always contiguous high-order 1 bits
* followed by all 0 bits.
*/
static int
sysctl_loopback_prefixlen(SYSCTL_HANDLER_ARGS)
{
int error, preflen;
/* ffs is 1-based; compensate. */
preflen = 33 - ffs(V_in_loopback_mask);
error = sysctl_handle_int(oidp, &preflen, 0, req);
if (error || !req->newptr)
return (error);
if (preflen < 8 || preflen > 31)
return (EINVAL);
V_in_loopback_mask = 0xffffffff << (32 - preflen);
return (0);
}
/*
* Trim a mask in a sockaddr
*/
static void
in_socktrim(struct sockaddr_in *ap)
{
char *cplim = (char *) &ap->sin_addr;
char *cp = (char *) (&ap->sin_addr + 1);
ap->sin_len = 0;
while (--cp >= cplim)
if (*cp) {
(ap)->sin_len = cp - (char *) (ap) + 1;
break;
}
}
/*
* Generic internet control operations (ioctl's).
*/
int
in_control_ioctl(u_long cmd, void *data, struct ifnet *ifp,
struct ucred *cred)
{
struct ifreq *ifr = (struct ifreq *)data;
struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
struct epoch_tracker et;
struct ifaddr *ifa;
struct in_ifaddr *ia;
int error;
if (ifp == NULL)
return (EADDRNOTAVAIL);
/*
* Filter out 4 ioctls we implement directly. Forward the rest
* to specific functions and ifp->if_ioctl().
*/
switch (cmd) {
case SIOCGIFADDR:
case SIOCGIFBRDADDR:
case SIOCGIFDSTADDR:
case SIOCGIFNETMASK:
break;
case SIOCGIFALIAS:
sx_xlock(&in_control_sx);
error = in_gifaddr_ioctl(cmd, data, ifp, cred);
sx_xunlock(&in_control_sx);
return (error);
case SIOCDIFADDR:
sx_xlock(&in_control_sx);
error = in_difaddr_ioctl(cmd, data, ifp, cred);
sx_xunlock(&in_control_sx);
return (error);
case OSIOCAIFADDR: /* 9.x compat */
case SIOCAIFADDR:
sx_xlock(&in_control_sx);
error = in_aifaddr_ioctl(cmd, data, ifp, cred);
sx_xunlock(&in_control_sx);
return (error);
case SIOCSIFADDR:
case SIOCSIFBRDADDR:
case SIOCSIFDSTADDR:
case SIOCSIFNETMASK:
/* We no longer support that old commands. */
return (EINVAL);
default:
if (ifp->if_ioctl == NULL)
return (EOPNOTSUPP);
return ((*ifp->if_ioctl)(ifp, cmd, data));
}
if (addr->sin_addr.s_addr != INADDR_ANY &&
prison_check_ip4(cred, &addr->sin_addr) != 0)
return (EADDRNOTAVAIL);
/*
* Find address for this interface, if it exists. If an
* address was specified, find that one instead of the
* first one on the interface, if possible.
*/
NET_EPOCH_ENTER(et);
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
ia = (struct in_ifaddr *)ifa;
if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
break;
}
if (ifa == NULL)
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
if (ifa->ifa_addr->sa_family == AF_INET) {
ia = (struct in_ifaddr *)ifa;
if (prison_check_ip4(cred,
&ia->ia_addr.sin_addr) == 0)
break;
}
if (ifa == NULL) {
NET_EPOCH_EXIT(et);
return (EADDRNOTAVAIL);
}
error = 0;
switch (cmd) {
case SIOCGIFADDR:
*addr = ia->ia_addr;
break;
case SIOCGIFBRDADDR:
if ((ifp->if_flags & IFF_BROADCAST) == 0) {
error = EINVAL;
break;
}
*addr = ia->ia_broadaddr;
break;
case SIOCGIFDSTADDR:
if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
error = EINVAL;
break;
}
*addr = ia->ia_dstaddr;
break;
case SIOCGIFNETMASK:
*addr = ia->ia_sockmask;
break;
}
NET_EPOCH_EXIT(et);
return (error);
}
int
in_mask2len(struct in_addr *mask)
{
int x, y;
u_char *p;
p = (u_char *)mask;
for (x = 0; x < sizeof(*mask); x++) {
if (p[x] != 0xff)
break;
}
y = 0;
if (x < sizeof(*mask)) {
for (y = 0; y < 8; y++) {
if ((p[x] & (0x80 >> y)) == 0)
break;
}
}
return (x * 8 + y);
}
int
in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
struct thread *td)
{
return (in_control_ioctl(cmd, data, ifp, td ? td->td_ucred : NULL));
}
static int
in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred)
{
const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
const struct sockaddr_in *addr = &ifra->ifra_addr;
const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
const struct sockaddr_in *mask = &ifra->ifra_mask;
const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
struct epoch_tracker et;
struct ifaddr *ifa;
struct in_ifaddr *ia;
bool iaIsFirst;
int error = 0;
error = priv_check_cred(cred, PRIV_NET_ADDIFADDR);
if (error)
return (error);
/*
* ifra_addr must be present and be of INET family.
* ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
*/
if (addr->sin_len != sizeof(struct sockaddr_in) ||
addr->sin_family != AF_INET)
return (EINVAL);
if (broadaddr->sin_len != 0 &&
(broadaddr->sin_len != sizeof(struct sockaddr_in) ||
broadaddr->sin_family != AF_INET))
return (EINVAL);
if (mask->sin_len != 0 &&
(mask->sin_len != sizeof(struct sockaddr_in) ||
mask->sin_family != AF_INET))
return (EINVAL);
if ((ifp->if_flags & IFF_POINTOPOINT) &&
(dstaddr->sin_len != sizeof(struct sockaddr_in) ||
dstaddr->sin_addr.s_addr == INADDR_ANY))
return (EDESTADDRREQ);
if (vhid != 0 && carp_attach_p == NULL)
return (EPROTONOSUPPORT);
#ifdef MAC
/* Check if a MAC policy disallows setting the IPv4 address. */
error = mac_inet_check_add_addr(cred, &addr->sin_addr, ifp);
if (error != 0)
return (error);
#endif
/*
* See whether address already exist.
*/
iaIsFirst = true;
ia = NULL;
NET_EPOCH_ENTER(et);
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
struct in_ifaddr *it;
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
it = (struct in_ifaddr *)ifa;
if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
prison_check_ip4(cred, &addr->sin_addr) == 0)
ia = it;
else
iaIsFirst = false;
}
NET_EPOCH_EXIT(et);
if (ia != NULL)
(void )in_difaddr_ioctl(cmd, data, ifp, cred);
ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
ia = (struct in_ifaddr *)ifa;
ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
CALLOUT_RETURNUNLOCKED);
ia->ia_ifp = ifp;
ia->ia_addr = *addr;
if (mask->sin_len != 0) {
ia->ia_sockmask = *mask;
ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
} else {
in_addr_t i = ntohl(addr->sin_addr.s_addr);
/*
* If netmask isn't supplied, use historical default.
* This is deprecated for interfaces other than loopback
* or point-to-point; warn in other cases. In the future
* we should return an error rather than warning.
*/
if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0)
printf("%s: set address: WARNING: network mask "
"should be specified; using historical default\n",
ifp->if_xname);
if (IN_CLASSA(i))
ia->ia_subnetmask = IN_CLASSA_NET;
else if (IN_CLASSB(i))
ia->ia_subnetmask = IN_CLASSB_NET;
else
ia->ia_subnetmask = IN_CLASSC_NET;
ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
}
ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
in_socktrim(&ia->ia_sockmask);
if (ifp->if_flags & IFF_BROADCAST) {
if (broadaddr->sin_len != 0) {
ia->ia_broadaddr = *broadaddr;
} else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
ia->ia_broadaddr.sin_family = AF_INET;
} else {
ia->ia_broadaddr.sin_addr.s_addr =
htonl(ia->ia_subnet | ~ia->ia_subnetmask);
ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
ia->ia_broadaddr.sin_family = AF_INET;
}
}
if (ifp->if_flags & IFF_POINTOPOINT)
ia->ia_dstaddr = *dstaddr;
if (vhid != 0) {
error = (*carp_attach_p)(&ia->ia_ifa, vhid);
if (error)
return (error);
}
/* if_addrhead is already referenced by ifa_alloc() */
IF_ADDR_WLOCK(ifp);
CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
IF_ADDR_WUNLOCK(ifp);
ifa_ref(ifa); /* in_ifaddrhead */
sx_assert(&in_control_sx, SA_XLOCKED);
CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
CK_LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia,
ia_hash);
/*
* Give the interface a chance to initialize
* if this is its first address,
* and to validate the address if necessary.
*/
if (ifp->if_ioctl != NULL) {
error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
if (error)
goto fail1;
}
/*
* Add route for the network.
*/
if (vhid == 0) {
error = in_addprefix(ia);
if (error)
goto fail1;
}
/*
* Add a loopback route to self.
*/
if (vhid == 0 && ia_need_loopback_route(ia)) {
struct in_ifaddr *eia;
eia = in_localip_more(ia);
if (eia == NULL) {
error = ifa_add_loopback_route((struct ifaddr *)ia,
(struct sockaddr *)&ia->ia_addr);
if (error)
goto fail2;
} else
ifa_free(&eia->ia_ifa);
}
if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
struct in_addr allhosts_addr;
struct in_ifinfo *ii;
ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
error = in_joingroup(ifp, &allhosts_addr, NULL,
&ii->ii_allhosts);
}
/*
* Note: we don't need extra reference for ifa, since we called
* with sx lock held, and ifaddr can not be deleted in concurrent
* thread.
*/
EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
return (error);
fail2:
if (vhid == 0)
(void )in_scrubprefix(ia, LLE_STATIC);
fail1:
if (ia->ia_ifa.ifa_carp)
(*carp_detach_p)(&ia->ia_ifa, false);
IF_ADDR_WLOCK(ifp);
CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
IF_ADDR_WUNLOCK(ifp);
ifa_free(&ia->ia_ifa); /* if_addrhead */
sx_assert(&in_control_sx, SA_XLOCKED);
CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
CK_LIST_REMOVE(ia, ia_hash);
ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
return (error);
}
static int
in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred)
{
const struct ifreq *ifr = (struct ifreq *)data;
const struct sockaddr_in *addr = (const struct sockaddr_in *)
&ifr->ifr_addr;
struct ifaddr *ifa;
struct in_ifaddr *ia;
bool deleteAny, iaIsLast;
int error;
if (cred != NULL) {
error = priv_check_cred(cred, PRIV_NET_DELIFADDR);
if (error)
return (error);
}
if (addr->sin_len != sizeof(struct sockaddr_in) ||
addr->sin_family != AF_INET)
deleteAny = true;
else
deleteAny = false;
iaIsLast = true;
ia = NULL;
IF_ADDR_WLOCK(ifp);
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
struct in_ifaddr *it;
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
it = (struct in_ifaddr *)ifa;
if (deleteAny && ia == NULL && (cred == NULL ||
prison_check_ip4(cred, &it->ia_addr.sin_addr) == 0))
ia = it;
if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
(cred == NULL || prison_check_ip4(cred,
&addr->sin_addr) == 0))
ia = it;
if (it != ia)
iaIsLast = false;
}
if (ia == NULL) {
IF_ADDR_WUNLOCK(ifp);
return (EADDRNOTAVAIL);
}
CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
IF_ADDR_WUNLOCK(ifp);
ifa_free(&ia->ia_ifa); /* if_addrhead */
sx_assert(&in_control_sx, SA_XLOCKED);
CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
CK_LIST_REMOVE(ia, ia_hash);
/*
* in_scrubprefix() kills the interface route.
*/
in_scrubprefix(ia, LLE_STATIC);
/*
* in_ifadown gets rid of all the rest of
* the routes. This is not quite the right
* thing to do, but at least if we are running
* a routing process they will come back.
*/
in_ifadown(&ia->ia_ifa, 1);
if (ia->ia_ifa.ifa_carp)
(*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
/*
* If this is the last IPv4 address configured on this
* interface, leave the all-hosts group.
* No state-change report need be transmitted.
*/
if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
struct in_ifinfo *ii;
ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
if (ii->ii_allhosts) {
(void)in_leavegroup(ii->ii_allhosts, NULL);
ii->ii_allhosts = NULL;
}
}
IF_ADDR_WLOCK(ifp);
if (callout_stop(&ia->ia_garp_timer) == 1) {
ifa_free(&ia->ia_ifa);
}
IF_ADDR_WUNLOCK(ifp);
EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
IFADDR_EVENT_DEL);
ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
return (0);
}
static int
in_gifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred)
{
struct in_aliasreq *ifra = (struct in_aliasreq *)data;
const struct sockaddr_in *addr = &ifra->ifra_addr;
struct epoch_tracker et;
struct ifaddr *ifa;
struct in_ifaddr *ia;
/*
* ifra_addr must be present and be of INET family.
*/
if (addr->sin_len != sizeof(struct sockaddr_in) ||
addr->sin_family != AF_INET)
return (EINVAL);
/*
* See whether address exist.
*/
ia = NULL;
NET_EPOCH_ENTER(et);
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
struct in_ifaddr *it;
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
it = (struct in_ifaddr *)ifa;
if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
prison_check_ip4(cred, &addr->sin_addr) == 0) {
ia = it;
break;
}
}
if (ia == NULL) {
NET_EPOCH_EXIT(et);
return (EADDRNOTAVAIL);
}
ifra->ifra_mask = ia->ia_sockmask;
if ((ifp->if_flags & IFF_POINTOPOINT) &&
ia->ia_dstaddr.sin_family == AF_INET)
ifra->ifra_dstaddr = ia->ia_dstaddr;
else if ((ifp->if_flags & IFF_BROADCAST) &&
ia->ia_broadaddr.sin_family == AF_INET)
ifra->ifra_broadaddr = ia->ia_broadaddr;
else
memset(&ifra->ifra_broadaddr, 0,
sizeof(ifra->ifra_broadaddr));
NET_EPOCH_EXIT(et);
return (0);
}
static int
in_match_ifaddr(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
{
if (nh->nh_ifa == (struct ifaddr *)arg)
return (1);
return (0);
}
static int
in_handle_prefix_route(uint32_t fibnum, int cmd,
struct sockaddr_in *dst, struct sockaddr_in *netmask, struct ifaddr *ifa,
struct ifnet *ifp)
{
NET_EPOCH_ASSERT();
/* Prepare gateway */
struct sockaddr_dl_short sdl = {
.sdl_family = AF_LINK,
.sdl_len = sizeof(struct sockaddr_dl_short),
.sdl_type = ifa->ifa_ifp->if_type,
.sdl_index = ifa->ifa_ifp->if_index,
};
struct rt_addrinfo info = {
.rti_ifa = ifa,
.rti_ifp = ifp,
.rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST),
.rti_info = {
[RTAX_DST] = (struct sockaddr *)dst,
[RTAX_NETMASK] = (struct sockaddr *)netmask,
[RTAX_GATEWAY] = (struct sockaddr *)&sdl,
},
/* Ensure we delete the prefix IFF prefix ifa matches */
.rti_filter = in_match_ifaddr,
.rti_filterdata = ifa,
};
return (rib_handle_ifaddr_info(fibnum, cmd, &info));
}
/*
* Routing table interaction with interface addresses.
*
* In general, two types of routes needs to be installed:
* a) "interface" or "prefix" route, telling user that the addresses
* behind the ifa prefix are reached directly.
* b) "loopback" route installed for the ifa address, telling user that
* the address belongs to local system.
*
* Handling for (a) and (b) differs in multi-fib aspects, hence they
* are implemented in different functions below.
*
* The cases above may intersect - /32 interface aliases results in
* the same prefix produced by (a) and (b). This blurs the definition
* of the "loopback" route and complicate interactions. The interaction
* table is defined below. The case numbers are used in the multiple
* functions below to refer to the particular test case.
*
* There can be multiple options:
* 1) Adding address with prefix on non-p2p/non-loopback interface.
* Example: 192.0.2.1/24. Action:
* * add "prefix" route towards 192.0.2.0/24 via @ia interface,
* using @ia as an address source.
* * add "loopback" route towards 192.0.2.1 via V_loif, saving
* @ia ifp in the gateway and using @ia as an address source.
*
* 2) Adding address with /32 mask to non-p2p/non-loopback interface.
* Example: 192.0.2.2/32. Action:
* * add "prefix" host route via V_loif, using @ia as an address source.
*
* 3) Adding address with or without prefix to p2p interface.
* Example: 10.0.0.1/24->10.0.0.2. Action:
* * add "prefix" host route towards 10.0.0.2 via this interface, using @ia
* as an address source. Note: no sense in installing full /24 as the interface
* is point-to-point.
* * add "loopback" route towards 10.0.9.1 via V_loif, saving
* @ia ifp in the gateway and using @ia as an address source.
*
* 4) Adding address with or without prefix to loopback interface.
* Example: 192.0.2.1/24. Action:
* * add "prefix" host route via @ia interface, using @ia as an address source.
* Note: Skip installing /24 prefix as it would introduce TTL loop
* for the traffic destined to these addresses.
*/
/*
* Checks if @ia needs to install loopback route to @ia address via
* ifa_maintain_loopback_route().
*
* Return true on success.
*/
static bool
ia_need_loopback_route(const struct in_ifaddr *ia)
{
struct ifnet *ifp = ia->ia_ifp;
/* Case 4: Skip loopback interfaces */
if ((ifp->if_flags & IFF_LOOPBACK) ||
(ia->ia_addr.sin_addr.s_addr == INADDR_ANY))
return (false);
/* Clash avoidance: Skip p2p interfaces with both addresses are equal */
if ((ifp->if_flags & IFF_POINTOPOINT) &&
ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
return (false);
/* Case 2: skip /32 prefixes */
if (!(ifp->if_flags & IFF_POINTOPOINT) &&
(ia->ia_sockmask.sin_addr.s_addr == INADDR_BROADCAST))
return (false);
return (true);
}
/*
* Calculate "prefix" route corresponding to @ia.
*/
static void
ia_getrtprefix(const struct in_ifaddr *ia, struct in_addr *prefix, struct in_addr *mask)
{
if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) {
/* Case 3: return host route for dstaddr */
*prefix = ia->ia_dstaddr.sin_addr;
mask->s_addr = INADDR_BROADCAST;
} else if (ia->ia_ifp->if_flags & IFF_LOOPBACK) {
/* Case 4: return host route for ifaddr */
*prefix = ia->ia_addr.sin_addr;
mask->s_addr = INADDR_BROADCAST;
} else {
/* Cases 1,2: return actual ia prefix */
*prefix = ia->ia_addr.sin_addr;
*mask = ia->ia_sockmask.sin_addr;
prefix->s_addr &= mask->s_addr;
}
}
/*
* Adds or delete interface "prefix" route corresponding to @ifa.
* Returns 0 on success or errno.
*/
static int
in_handle_ifaddr_route(int cmd, struct in_ifaddr *ia)
{
struct ifaddr *ifa = &ia->ia_ifa;
struct in_addr daddr, maddr;
struct sockaddr_in *pmask;