Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/netinet/in.c
103007 views
1
/*-
2
* SPDX-License-Identifier: BSD-3-Clause
3
*
4
* Copyright (c) 1982, 1986, 1991, 1993
5
* The Regents of the University of California. All rights reserved.
6
* Copyright (C) 2001 WIDE Project. All rights reserved.
7
*
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
10
* are met:
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
13
* 2. Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
16
* 3. Neither the name of the University nor the names of its contributors
17
* may be used to endorse or promote products derived from this software
18
* without specific prior written permission.
19
*
20
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
* SUCH DAMAGE.
31
*/
32
33
#include "opt_inet.h"
34
35
#define IN_HISTORICAL_NETS /* include class masks */
36
37
#include <sys/param.h>
38
#include <sys/eventhandler.h>
39
#include <sys/systm.h>
40
#include <sys/sockio.h>
41
#include <sys/malloc.h>
42
#include <sys/priv.h>
43
#include <sys/socket.h>
44
#include <sys/jail.h>
45
#include <sys/kernel.h>
46
#include <sys/lock.h>
47
#include <sys/proc.h>
48
#include <sys/sysctl.h>
49
#include <sys/syslog.h>
50
#include <sys/sx.h>
51
52
#include <net/if.h>
53
#include <net/if_var.h>
54
#include <net/if_arp.h>
55
#include <net/if_dl.h>
56
#include <net/if_llatbl.h>
57
#include <net/if_private.h>
58
#include <net/if_types.h>
59
#include <net/if_bridgevar.h>
60
#include <net/route.h>
61
#include <net/route/nhop.h>
62
#include <net/route/route_ctl.h>
63
#include <net/vnet.h>
64
65
#include <netinet/if_ether.h>
66
#include <netinet/in.h>
67
#include <netinet/in_fib.h>
68
#include <netinet/in_var.h>
69
#include <netinet/in_pcb.h>
70
#include <netinet/ip_var.h>
71
#include <netinet/ip_carp.h>
72
#include <netinet/igmp_var.h>
73
#include <netinet/udp.h>
74
#include <netinet/udp_var.h>
75
76
#ifdef MAC
77
#include <security/mac/mac_framework.h>
78
#endif
79
80
static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *);
81
static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *);
82
static int in_gifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *);
83
84
static void in_socktrim(struct sockaddr_in *);
85
static void in_purgemaddrs(struct ifnet *);
86
87
static bool ia_need_loopback_route(const struct in_ifaddr *);
88
89
VNET_DEFINE_STATIC(int, nosameprefix);
90
#define V_nosameprefix VNET(nosameprefix)
91
SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW,
92
&VNET_NAME(nosameprefix), 0,
93
"Refuse to create same prefixes on different interfaces");
94
95
VNET_DEFINE_STATIC(bool, broadcast_lowest);
96
#define V_broadcast_lowest VNET(broadcast_lowest)
97
SYSCTL_BOOL(_net_inet_ip, OID_AUTO, broadcast_lowest, CTLFLAG_VNET | CTLFLAG_RW,
98
&VNET_NAME(broadcast_lowest), 0,
99
"Treat lowest address on a subnet (host 0) as broadcast");
100
101
VNET_DEFINE(bool, ip_allow_net240) = false;
102
#define V_ip_allow_net240 VNET(ip_allow_net240)
103
SYSCTL_BOOL(_net_inet_ip, OID_AUTO, allow_net240,
104
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_allow_net240), 0,
105
"Allow forwarding of and ICMP response to Experimental addresses, aka Class E (240/4)");
106
/* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-240 */
107
108
VNET_DEFINE(bool, ip_allow_net0) = false;
109
SYSCTL_BOOL(_net_inet_ip, OID_AUTO, allow_net0,
110
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_allow_net0), 0,
111
"Allow forwarding of and ICMP response to addresses in network 0/8");
112
/* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-0 */
113
114
VNET_DEFINE(uint32_t, in_loopback_mask) = IN_LOOPBACK_MASK_DFLT;
115
#define V_in_loopback_mask VNET(in_loopback_mask)
116
static int sysctl_loopback_prefixlen(SYSCTL_HANDLER_ARGS);
117
SYSCTL_PROC(_net_inet_ip, OID_AUTO, loopback_prefixlen,
118
CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
119
NULL, 0, sysctl_loopback_prefixlen, "I",
120
"Prefix length of address space reserved for loopback");
121
/* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-127 */
122
123
VNET_DECLARE(struct inpcbinfo, ripcbinfo);
124
#define V_ripcbinfo VNET(ripcbinfo)
125
126
static struct sx in_control_sx;
127
SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
128
129
/*
130
* Return true if an internet address is for a ``local'' host
131
* (one to which we have a connection).
132
*/
133
bool
134
in_localaddr(struct in_addr in)
135
{
136
u_long i = ntohl(in.s_addr);
137
struct in_ifaddr *ia;
138
139
NET_EPOCH_ASSERT();
140
141
CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
142
if ((i & ia->ia_subnetmask) == ia->ia_subnet)
143
return (true);
144
}
145
146
return (false);
147
}
148
149
/*
150
* Return true if an internet address is for the local host and configured
151
* on one of its interfaces.
152
*/
153
bool
154
in_localip(struct in_addr in)
155
{
156
struct in_ifaddr *ia;
157
158
NET_EPOCH_ASSERT();
159
160
CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash)
161
if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
162
return (true);
163
164
return (false);
165
}
166
167
/*
168
* Like in_localip(), but FIB-aware and carp(4)-aware.
169
*/
170
bool
171
in_localip_fib(struct in_addr in, uint16_t fib)
172
{
173
struct in_ifaddr *ia;
174
175
NET_EPOCH_ASSERT();
176
177
CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash)
178
if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr &&
179
(ia->ia_ifa.ifa_carp == NULL ||
180
carp_master_p(&ia->ia_ifa)) &&
181
ia->ia_ifa.ifa_ifp->if_fib == fib)
182
return (true);
183
184
return (false);
185
}
186
187
/*
188
* Return true if an internet address is configured on an interface.
189
*/
190
bool
191
in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
192
{
193
struct ifaddr *ifa;
194
struct in_ifaddr *ia;
195
196
NET_EPOCH_ASSERT();
197
198
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
199
if (ifa->ifa_addr->sa_family != AF_INET)
200
continue;
201
ia = (struct in_ifaddr *)ifa;
202
if (ia->ia_addr.sin_addr.s_addr == in.s_addr)
203
return (true);
204
}
205
206
return (false);
207
}
208
209
/*
210
* Return a reference to the interface address which is different to
211
* the supplied one but with same IP address value.
212
*/
213
static struct in_ifaddr *
214
in_localip_more(struct in_ifaddr *original_ia)
215
{
216
struct epoch_tracker et;
217
in_addr_t original_addr = IA_SIN(original_ia)->sin_addr.s_addr;
218
uint32_t original_fib = original_ia->ia_ifa.ifa_ifp->if_fib;
219
struct in_ifaddr *ia;
220
221
NET_EPOCH_ENTER(et);
222
CK_LIST_FOREACH(ia, INADDR_HASH(original_addr), ia_hash) {
223
in_addr_t addr = IA_SIN(ia)->sin_addr.s_addr;
224
uint32_t fib = ia->ia_ifa.ifa_ifp->if_fib;
225
if (!V_rt_add_addr_allfibs && (original_fib != fib))
226
continue;
227
if ((original_ia != ia) && (original_addr == addr)) {
228
ifa_ref(&ia->ia_ifa);
229
NET_EPOCH_EXIT(et);
230
return (ia);
231
}
232
}
233
NET_EPOCH_EXIT(et);
234
235
return (NULL);
236
}
237
238
/*
239
* Tries to find first IPv4 address in the provided fib.
240
* Prefers non-loopback addresses and return loopback IFF
241
* @loopback_ok is set.
242
*
243
* Returns ifa or NULL.
244
*/
245
struct in_ifaddr *
246
in_findlocal(uint32_t fibnum, bool loopback_ok)
247
{
248
struct in_ifaddr *ia = NULL, *ia_lo = NULL;
249
250
NET_EPOCH_ASSERT();
251
252
CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
253
uint32_t ia_fib = ia->ia_ifa.ifa_ifp->if_fib;
254
if (!V_rt_add_addr_allfibs && (fibnum != ia_fib))
255
continue;
256
257
if (!IN_LOOPBACK(ntohl(IA_SIN(ia)->sin_addr.s_addr)))
258
break;
259
if (loopback_ok)
260
ia_lo = ia;
261
}
262
263
if (ia == NULL)
264
ia = ia_lo;
265
266
return (ia);
267
}
268
269
/*
270
* Determine whether an IP address is in a reserved set of addresses
271
* that may not be forwarded, or whether datagrams to that destination
272
* may be forwarded.
273
*/
274
bool
275
in_canforward(struct in_addr in)
276
{
277
u_long i = ntohl(in.s_addr);
278
279
if (IN_MULTICAST(i) || IN_LINKLOCAL(i) || IN_LOOPBACK(i) ||
280
in_nullhost(in))
281
return (false);
282
if (IN_EXPERIMENTAL(i) && !V_ip_allow_net240)
283
return (false);
284
if (IN_ZERONET(i) && !V_ip_allow_net0)
285
return (false);
286
return (true);
287
}
288
289
/*
290
* Sysctl to manage prefix of reserved loopback network; translate
291
* to/from mask. The mask is always contiguous high-order 1 bits
292
* followed by all 0 bits.
293
*/
294
static int
295
sysctl_loopback_prefixlen(SYSCTL_HANDLER_ARGS)
296
{
297
int error, preflen;
298
299
/* ffs is 1-based; compensate. */
300
preflen = 33 - ffs(V_in_loopback_mask);
301
error = sysctl_handle_int(oidp, &preflen, 0, req);
302
if (error || !req->newptr)
303
return (error);
304
if (preflen < 8 || preflen > 31)
305
return (EINVAL);
306
V_in_loopback_mask = 0xffffffff << (32 - preflen);
307
return (0);
308
}
309
310
/*
311
* Trim a mask in a sockaddr
312
*/
313
static void
314
in_socktrim(struct sockaddr_in *ap)
315
{
316
char *cplim = (char *) &ap->sin_addr;
317
char *cp = (char *) (&ap->sin_addr + 1);
318
319
ap->sin_len = 0;
320
while (--cp >= cplim)
321
if (*cp) {
322
(ap)->sin_len = cp - (char *) (ap) + 1;
323
break;
324
}
325
}
326
327
/*
328
* Generic internet control operations (ioctl's).
329
*/
330
int
331
in_control_ioctl(u_long cmd, void *data, struct ifnet *ifp,
332
struct ucred *cred)
333
{
334
struct ifreq *ifr = (struct ifreq *)data;
335
struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
336
struct epoch_tracker et;
337
struct ifaddr *ifa;
338
struct in_ifaddr *ia;
339
int error;
340
341
if (ifp == NULL)
342
return (EADDRNOTAVAIL);
343
344
/*
345
* Filter out 4 ioctls we implement directly. Forward the rest
346
* to specific functions and ifp->if_ioctl().
347
*/
348
switch (cmd) {
349
case SIOCGIFADDR:
350
case SIOCGIFBRDADDR:
351
case SIOCGIFDSTADDR:
352
case SIOCGIFNETMASK:
353
break;
354
case SIOCGIFALIAS:
355
sx_xlock(&in_control_sx);
356
error = in_gifaddr_ioctl(cmd, data, ifp, cred);
357
sx_xunlock(&in_control_sx);
358
return (error);
359
case SIOCDIFADDR:
360
sx_xlock(&in_control_sx);
361
error = in_difaddr_ioctl(cmd, data, ifp, cred);
362
sx_xunlock(&in_control_sx);
363
return (error);
364
case OSIOCAIFADDR: /* 9.x compat */
365
case SIOCAIFADDR:
366
sx_xlock(&in_control_sx);
367
error = in_aifaddr_ioctl(cmd, data, ifp, cred);
368
sx_xunlock(&in_control_sx);
369
return (error);
370
case SIOCSIFADDR:
371
case SIOCSIFBRDADDR:
372
case SIOCSIFDSTADDR:
373
case SIOCSIFNETMASK:
374
/* We no longer support that old commands. */
375
return (EINVAL);
376
default:
377
if (ifp->if_ioctl == NULL)
378
return (EOPNOTSUPP);
379
return ((*ifp->if_ioctl)(ifp, cmd, data));
380
}
381
382
if (addr->sin_addr.s_addr != INADDR_ANY &&
383
prison_check_ip4(cred, &addr->sin_addr) != 0)
384
return (EADDRNOTAVAIL);
385
386
/*
387
* Find address for this interface, if it exists. If an
388
* address was specified, find that one instead of the
389
* first one on the interface, if possible.
390
*/
391
NET_EPOCH_ENTER(et);
392
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
393
if (ifa->ifa_addr->sa_family != AF_INET)
394
continue;
395
ia = (struct in_ifaddr *)ifa;
396
if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
397
break;
398
}
399
if (ifa == NULL)
400
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
401
if (ifa->ifa_addr->sa_family == AF_INET) {
402
ia = (struct in_ifaddr *)ifa;
403
if (prison_check_ip4(cred,
404
&ia->ia_addr.sin_addr) == 0)
405
break;
406
}
407
408
if (ifa == NULL) {
409
NET_EPOCH_EXIT(et);
410
return (EADDRNOTAVAIL);
411
}
412
413
error = 0;
414
switch (cmd) {
415
case SIOCGIFADDR:
416
*addr = ia->ia_addr;
417
break;
418
419
case SIOCGIFBRDADDR:
420
if ((ifp->if_flags & IFF_BROADCAST) == 0) {
421
error = EINVAL;
422
break;
423
}
424
*addr = ia->ia_broadaddr;
425
break;
426
427
case SIOCGIFDSTADDR:
428
if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
429
error = EINVAL;
430
break;
431
}
432
*addr = ia->ia_dstaddr;
433
break;
434
435
case SIOCGIFNETMASK:
436
*addr = ia->ia_sockmask;
437
break;
438
}
439
440
NET_EPOCH_EXIT(et);
441
442
return (error);
443
}
444
445
int
446
in_mask2len(struct in_addr *mask)
447
{
448
int x, y;
449
u_char *p;
450
451
p = (u_char *)mask;
452
for (x = 0; x < sizeof(*mask); x++) {
453
if (p[x] != 0xff)
454
break;
455
}
456
y = 0;
457
if (x < sizeof(*mask)) {
458
for (y = 0; y < 8; y++) {
459
if ((p[x] & (0x80 >> y)) == 0)
460
break;
461
}
462
}
463
return (x * 8 + y);
464
}
465
466
int
467
in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
468
struct thread *td)
469
{
470
return (in_control_ioctl(cmd, data, ifp, td ? td->td_ucred : NULL));
471
}
472
473
static int
474
in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred)
475
{
476
const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
477
const struct sockaddr_in *addr = &ifra->ifra_addr;
478
const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
479
const struct sockaddr_in *mask = &ifra->ifra_mask;
480
const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
481
const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
482
struct epoch_tracker et;
483
struct ifaddr *ifa;
484
struct in_ifaddr *ia;
485
bool iaIsFirst;
486
int error = 0;
487
488
error = priv_check_cred(cred, PRIV_NET_ADDIFADDR);
489
if (error)
490
return (error);
491
492
/*
493
* ifra_addr must be present and be of INET family.
494
* ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
495
*/
496
if (addr->sin_len != sizeof(struct sockaddr_in) ||
497
addr->sin_family != AF_INET)
498
return (EINVAL);
499
if (broadaddr->sin_len != 0 &&
500
(broadaddr->sin_len != sizeof(struct sockaddr_in) ||
501
broadaddr->sin_family != AF_INET))
502
return (EINVAL);
503
if (mask->sin_len != 0 &&
504
(mask->sin_len != sizeof(struct sockaddr_in) ||
505
mask->sin_family != AF_INET))
506
return (EINVAL);
507
if ((ifp->if_flags & IFF_POINTOPOINT) &&
508
(dstaddr->sin_len != sizeof(struct sockaddr_in) ||
509
dstaddr->sin_addr.s_addr == INADDR_ANY))
510
return (EDESTADDRREQ);
511
if (vhid != 0 && carp_attach_p == NULL)
512
return (EPROTONOSUPPORT);
513
514
#ifdef MAC
515
/* Check if a MAC policy disallows setting the IPv4 address. */
516
error = mac_inet_check_add_addr(cred, &addr->sin_addr, ifp);
517
if (error != 0)
518
return (error);
519
#endif
520
521
/*
522
* Check if bridge wants to allow adding addrs to member interfaces.
523
*/
524
if (ifp->if_bridge != NULL && ifp->if_type != IFT_GIF &&
525
bridge_member_ifaddrs_p != NULL) {
526
if (bridge_member_ifaddrs_p())
527
if_printf(ifp, "WARNING: Assigning an IP address to "
528
"an interface which is also a bridge member is "
529
"deprecated and will be unsupported in a future "
530
"release.\n");
531
else
532
return (EINVAL);
533
}
534
535
/*
536
* See whether address already exist.
537
*/
538
iaIsFirst = true;
539
ia = NULL;
540
NET_EPOCH_ENTER(et);
541
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
542
struct in_ifaddr *it;
543
544
if (ifa->ifa_addr->sa_family != AF_INET)
545
continue;
546
547
it = (struct in_ifaddr *)ifa;
548
if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
549
prison_check_ip4(cred, &addr->sin_addr) == 0)
550
ia = it;
551
else
552
iaIsFirst = false;
553
}
554
NET_EPOCH_EXIT(et);
555
556
if (ia != NULL)
557
(void )in_difaddr_ioctl(cmd, data, ifp, cred);
558
559
ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
560
ia = (struct in_ifaddr *)ifa;
561
ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
562
ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
563
ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
564
callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
565
CALLOUT_RETURNUNLOCKED);
566
567
ia->ia_ifp = ifp;
568
ia->ia_addr = *addr;
569
if (mask->sin_len != 0) {
570
ia->ia_sockmask = *mask;
571
ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
572
} else {
573
in_addr_t i = ntohl(addr->sin_addr.s_addr);
574
575
/*
576
* If netmask isn't supplied, use historical default.
577
* This is deprecated for interfaces other than loopback
578
* or point-to-point; warn in other cases. In the future
579
* we should return an error rather than warning.
580
*/
581
if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0)
582
printf("%s: set address: WARNING: network mask "
583
"should be specified; using historical default\n",
584
ifp->if_xname);
585
if (IN_CLASSA(i))
586
ia->ia_subnetmask = IN_CLASSA_NET;
587
else if (IN_CLASSB(i))
588
ia->ia_subnetmask = IN_CLASSB_NET;
589
else
590
ia->ia_subnetmask = IN_CLASSC_NET;
591
ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
592
}
593
ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
594
in_socktrim(&ia->ia_sockmask);
595
596
if (ifp->if_flags & IFF_BROADCAST) {
597
if (broadaddr->sin_len != 0) {
598
ia->ia_broadaddr = *broadaddr;
599
} else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
600
ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
601
ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
602
ia->ia_broadaddr.sin_family = AF_INET;
603
} else {
604
ia->ia_broadaddr.sin_addr.s_addr =
605
htonl(ia->ia_subnet | ~ia->ia_subnetmask);
606
ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
607
ia->ia_broadaddr.sin_family = AF_INET;
608
}
609
}
610
611
if (ifp->if_flags & IFF_POINTOPOINT)
612
ia->ia_dstaddr = *dstaddr;
613
614
if (vhid != 0) {
615
error = (*carp_attach_p)(&ia->ia_ifa, vhid);
616
if (error)
617
return (error);
618
}
619
620
/* if_addrhead is already referenced by ifa_alloc() */
621
IF_ADDR_WLOCK(ifp);
622
CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
623
IF_ADDR_WUNLOCK(ifp);
624
625
ifa_ref(ifa); /* in_ifaddrhead */
626
sx_assert(&in_control_sx, SA_XLOCKED);
627
CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
628
CK_LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia,
629
ia_hash);
630
631
/*
632
* Give the interface a chance to initialize
633
* if this is its first address,
634
* and to validate the address if necessary.
635
*/
636
if (ifp->if_ioctl != NULL) {
637
error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
638
if (error)
639
goto fail1;
640
}
641
642
/*
643
* Add route for the network.
644
*/
645
if (vhid == 0) {
646
error = in_addprefix(ia);
647
if (error)
648
goto fail1;
649
}
650
651
/*
652
* Add a loopback route to self.
653
*/
654
if (vhid == 0 && ia_need_loopback_route(ia)) {
655
struct in_ifaddr *eia;
656
657
eia = in_localip_more(ia);
658
659
if (eia == NULL) {
660
error = ifa_add_loopback_route((struct ifaddr *)ia,
661
(struct sockaddr *)&ia->ia_addr);
662
if (error)
663
goto fail2;
664
} else
665
ifa_free(&eia->ia_ifa);
666
}
667
668
if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
669
struct in_addr allhosts_addr;
670
struct in_ifinfo *ii;
671
672
ii = (struct in_ifinfo *)ifp->if_inet;
673
allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
674
675
error = in_joingroup(ifp, &allhosts_addr, NULL,
676
&ii->ii_allhosts);
677
}
678
679
/*
680
* Note: we don't need extra reference for ifa, since we called
681
* with sx lock held, and ifaddr can not be deleted in concurrent
682
* thread.
683
*/
684
EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
685
686
return (error);
687
688
fail2:
689
if (vhid == 0)
690
(void )in_scrubprefix(ia, LLE_STATIC);
691
692
fail1:
693
if (ia->ia_ifa.ifa_carp)
694
(*carp_detach_p)(&ia->ia_ifa, false);
695
696
IF_ADDR_WLOCK(ifp);
697
CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
698
IF_ADDR_WUNLOCK(ifp);
699
ifa_free(&ia->ia_ifa); /* if_addrhead */
700
701
sx_assert(&in_control_sx, SA_XLOCKED);
702
CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
703
CK_LIST_REMOVE(ia, ia_hash);
704
ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
705
706
return (error);
707
}
708
709
static int
710
in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred)
711
{
712
const struct ifreq *ifr = (struct ifreq *)data;
713
const struct sockaddr_in *addr = (const struct sockaddr_in *)
714
&ifr->ifr_addr;
715
struct ifaddr *ifa;
716
struct in_ifaddr *ia;
717
bool deleteAny, iaIsLast;
718
int error;
719
720
if (cred != NULL) {
721
error = priv_check_cred(cred, PRIV_NET_DELIFADDR);
722
if (error)
723
return (error);
724
}
725
726
if (addr->sin_len != sizeof(struct sockaddr_in) ||
727
addr->sin_family != AF_INET)
728
deleteAny = true;
729
else
730
deleteAny = false;
731
732
iaIsLast = true;
733
ia = NULL;
734
IF_ADDR_WLOCK(ifp);
735
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
736
struct in_ifaddr *it;
737
738
if (ifa->ifa_addr->sa_family != AF_INET)
739
continue;
740
741
it = (struct in_ifaddr *)ifa;
742
if (deleteAny && ia == NULL && (cred == NULL ||
743
prison_check_ip4(cred, &it->ia_addr.sin_addr) == 0))
744
ia = it;
745
746
if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
747
(cred == NULL || prison_check_ip4(cred,
748
&addr->sin_addr) == 0))
749
ia = it;
750
751
if (it != ia)
752
iaIsLast = false;
753
}
754
755
if (ia == NULL) {
756
IF_ADDR_WUNLOCK(ifp);
757
return (EADDRNOTAVAIL);
758
}
759
760
CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
761
IF_ADDR_WUNLOCK(ifp);
762
ifa_free(&ia->ia_ifa); /* if_addrhead */
763
764
sx_assert(&in_control_sx, SA_XLOCKED);
765
CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
766
CK_LIST_REMOVE(ia, ia_hash);
767
768
/*
769
* in_scrubprefix() kills the interface route.
770
*/
771
in_scrubprefix(ia, LLE_STATIC);
772
773
/*
774
* in_ifadown gets rid of all the rest of
775
* the routes. This is not quite the right
776
* thing to do, but at least if we are running
777
* a routing process they will come back.
778
*/
779
in_ifadown(&ia->ia_ifa, 1);
780
781
if (ia->ia_ifa.ifa_carp)
782
(*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
783
784
/*
785
* If this is the last IPv4 address configured on this
786
* interface, leave the all-hosts group.
787
* No state-change report need be transmitted.
788
*/
789
if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
790
struct in_ifinfo *ii;
791
792
ii = (struct in_ifinfo *)ifp->if_inet;
793
if (ii->ii_allhosts) {
794
(void)in_leavegroup(ii->ii_allhosts, NULL);
795
ii->ii_allhosts = NULL;
796
}
797
}
798
799
IF_ADDR_WLOCK(ifp);
800
if (callout_stop(&ia->ia_garp_timer) == 1) {
801
ifa_free(&ia->ia_ifa);
802
}
803
IF_ADDR_WUNLOCK(ifp);
804
805
EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
806
IFADDR_EVENT_DEL);
807
ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
808
809
return (0);
810
}
811
812
static int
813
in_gifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred)
814
{
815
struct in_aliasreq *ifra = (struct in_aliasreq *)data;
816
const struct sockaddr_in *addr = &ifra->ifra_addr;
817
struct epoch_tracker et;
818
struct ifaddr *ifa;
819
struct in_ifaddr *ia;
820
821
/*
822
* ifra_addr must be present and be of INET family.
823
*/
824
if (addr->sin_len != sizeof(struct sockaddr_in) ||
825
addr->sin_family != AF_INET)
826
return (EINVAL);
827
828
/*
829
* See whether address exist.
830
*/
831
ia = NULL;
832
NET_EPOCH_ENTER(et);
833
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
834
struct in_ifaddr *it;
835
836
if (ifa->ifa_addr->sa_family != AF_INET)
837
continue;
838
839
it = (struct in_ifaddr *)ifa;
840
if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
841
prison_check_ip4(cred, &addr->sin_addr) == 0) {
842
ia = it;
843
break;
844
}
845
}
846
if (ia == NULL) {
847
NET_EPOCH_EXIT(et);
848
return (EADDRNOTAVAIL);
849
}
850
851
ifra->ifra_mask = ia->ia_sockmask;
852
if ((ifp->if_flags & IFF_POINTOPOINT) &&
853
ia->ia_dstaddr.sin_family == AF_INET)
854
ifra->ifra_dstaddr = ia->ia_dstaddr;
855
else if ((ifp->if_flags & IFF_BROADCAST) &&
856
ia->ia_broadaddr.sin_family == AF_INET)
857
ifra->ifra_broadaddr = ia->ia_broadaddr;
858
else
859
memset(&ifra->ifra_broadaddr, 0,
860
sizeof(ifra->ifra_broadaddr));
861
862
NET_EPOCH_EXIT(et);
863
return (0);
864
}
865
866
static int
867
in_match_ifaddr(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
868
{
869
870
if (nh->nh_ifa == (struct ifaddr *)arg)
871
return (1);
872
873
return (0);
874
}
875
876
static int
877
in_handle_prefix_route(uint32_t fibnum, int cmd,
878
struct sockaddr_in *dst, struct sockaddr_in *netmask, struct ifaddr *ifa,
879
struct ifnet *ifp)
880
{
881
882
NET_EPOCH_ASSERT();
883
884
/* Prepare gateway */
885
struct sockaddr_dl_short sdl = {
886
.sdl_family = AF_LINK,
887
.sdl_len = sizeof(struct sockaddr_dl_short),
888
.sdl_type = ifa->ifa_ifp->if_type,
889
.sdl_index = ifa->ifa_ifp->if_index,
890
};
891
892
struct rt_addrinfo info = {
893
.rti_ifa = ifa,
894
.rti_ifp = ifp,
895
.rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST),
896
.rti_info = {
897
[RTAX_DST] = (struct sockaddr *)dst,
898
[RTAX_NETMASK] = (struct sockaddr *)netmask,
899
[RTAX_GATEWAY] = (struct sockaddr *)&sdl,
900
},
901
/* Ensure we delete the prefix IFF prefix ifa matches */
902
.rti_filter = in_match_ifaddr,
903
.rti_filterdata = ifa,
904
};
905
906
return (rib_handle_ifaddr_info(fibnum, cmd, &info));
907
}
908
909
/*
910
* Routing table interaction with interface addresses.
911
*
912
* In general, two types of routes needs to be installed:
913
* a) "interface" or "prefix" route, telling user that the addresses
914
* behind the ifa prefix are reached directly.
915
* b) "loopback" route installed for the ifa address, telling user that
916
* the address belongs to local system.
917
*
918
* Handling for (a) and (b) differs in multi-fib aspects, hence they
919
* are implemented in different functions below.
920
*
921
* The cases above may intersect - /32 interface aliases results in
922
* the same prefix produced by (a) and (b). This blurs the definition
923
* of the "loopback" route and complicate interactions. The interaction
924
* table is defined below. The case numbers are used in the multiple
925
* functions below to refer to the particular test case.
926
*
927
* There can be multiple options:
928
* 1) Adding address with prefix on non-p2p/non-loopback interface.
929
* Example: 192.0.2.1/24. Action:
930
* * add "prefix" route towards 192.0.2.0/24 via @ia interface,
931
* using @ia as an address source.
932
* * add "loopback" route towards 192.0.2.1 via V_loif, saving
933
* @ia ifp in the gateway and using @ia as an address source.
934
*
935
* 2) Adding address with /32 mask to non-p2p/non-loopback interface.
936
* Example: 192.0.2.2/32. Action:
937
* * add "prefix" host route via V_loif, using @ia as an address source.
938
*
939
* 3) Adding address with or without prefix to p2p interface.
940
* Example: 10.0.0.1/24->10.0.0.2. Action:
941
* * add "prefix" host route towards 10.0.0.2 via this interface, using @ia
942
* as an address source. Note: no sense in installing full /24 as the interface
943
* is point-to-point.
944
* * add "loopback" route towards 10.0.9.1 via V_loif, saving
945
* @ia ifp in the gateway and using @ia as an address source.
946
*
947
* 4) Adding address with or without prefix to loopback interface.
948
* Example: 192.0.2.1/24. Action:
949
* * add "prefix" host route via @ia interface, using @ia as an address source.
950
* Note: Skip installing /24 prefix as it would introduce TTL loop
951
* for the traffic destined to these addresses.
952
*/
953
954
/*
955
* Checks if @ia needs to install loopback route to @ia address via
956
* ifa_maintain_loopback_route().
957
*
958
* Return true on success.
959
*/
960
static bool
961
ia_need_loopback_route(const struct in_ifaddr *ia)
962
{
963
struct ifnet *ifp = ia->ia_ifp;
964
965
/* Case 4: Skip loopback interfaces */
966
if ((ifp->if_flags & IFF_LOOPBACK) ||
967
(ia->ia_addr.sin_addr.s_addr == INADDR_ANY))
968
return (false);
969
970
/* Clash avoidance: Skip p2p interfaces with both addresses are equal */
971
if ((ifp->if_flags & IFF_POINTOPOINT) &&
972
ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
973
return (false);
974
975
/* Case 2: skip /32 prefixes */
976
if (!(ifp->if_flags & IFF_POINTOPOINT) &&
977
(ia->ia_sockmask.sin_addr.s_addr == INADDR_BROADCAST))
978
return (false);
979
980
return (true);
981
}
982
983
/*
984
* Calculate "prefix" route corresponding to @ia.
985
*/
986
static void
987
ia_getrtprefix(const struct in_ifaddr *ia, struct in_addr *prefix, struct in_addr *mask)
988
{
989
990
if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) {
991
/* Case 3: return host route for dstaddr */
992
*prefix = ia->ia_dstaddr.sin_addr;
993
mask->s_addr = INADDR_BROADCAST;
994
} else if (ia->ia_ifp->if_flags & IFF_LOOPBACK) {
995
/* Case 4: return host route for ifaddr */
996
*prefix = ia->ia_addr.sin_addr;
997
mask->s_addr = INADDR_BROADCAST;
998
} else {
999
/* Cases 1,2: return actual ia prefix */
1000
*prefix = ia->ia_addr.sin_addr;
1001
*mask = ia->ia_sockmask.sin_addr;
1002
prefix->s_addr &= mask->s_addr;
1003
}
1004
}
1005
1006
/*
1007
* Adds or delete interface "prefix" route corresponding to @ifa.
1008
* Returns 0 on success or errno.
1009
*/
1010
static int
1011
in_handle_ifaddr_route(int cmd, struct in_ifaddr *ia)
1012
{
1013
struct ifaddr *ifa = &ia->ia_ifa;
1014
struct in_addr daddr, maddr;
1015
struct sockaddr_in *pmask;
1016
struct epoch_tracker et;
1017
int error;
1018
1019
ia_getrtprefix(ia, &daddr, &maddr);
1020
1021
struct sockaddr_in mask = {
1022
.sin_family = AF_INET,
1023
.sin_len = sizeof(struct sockaddr_in),
1024
.sin_addr = maddr,
1025
};
1026
1027
pmask = (maddr.s_addr != INADDR_BROADCAST) ? &mask : NULL;
1028
1029
struct sockaddr_in dst = {
1030
.sin_family = AF_INET,
1031
.sin_len = sizeof(struct sockaddr_in),
1032
.sin_addr.s_addr = daddr.s_addr & maddr.s_addr,
1033
};
1034
1035
struct ifnet *ifp = ia->ia_ifp;
1036
1037
if ((maddr.s_addr == INADDR_BROADCAST) &&
1038
(!(ia->ia_ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))) {
1039
/* Case 2: host route on broadcast interface */
1040
ifp = V_loif;
1041
}
1042
1043
uint32_t fibnum = ifa->ifa_ifp->if_fib;
1044
NET_EPOCH_ENTER(et);
1045
error = in_handle_prefix_route(fibnum, cmd, &dst, pmask, ifa, ifp);
1046
NET_EPOCH_EXIT(et);
1047
1048
return (error);
1049
}
1050
1051
/*
1052
* Check if we have a route for the given prefix already.
1053
*/
1054
static bool
1055
in_hasrtprefix(struct in_ifaddr *target)
1056
{
1057
struct epoch_tracker et;
1058
struct in_ifaddr *ia;
1059
struct in_addr prefix, mask, p, m;
1060
bool result = false;
1061
1062
ia_getrtprefix(target, &prefix, &mask);
1063
1064
/* Look for an existing address with the same prefix, mask, and fib */
1065
NET_EPOCH_ENTER(et);
1066
CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1067
ia_getrtprefix(ia, &p, &m);
1068
1069
if (prefix.s_addr != p.s_addr ||
1070
mask.s_addr != m.s_addr)
1071
continue;
1072
1073
if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
1074
continue;
1075
1076
/*
1077
* If we got a matching prefix route inserted by other
1078
* interface address, we are done here.
1079
*/
1080
if (ia->ia_flags & IFA_ROUTE) {
1081
result = true;
1082
break;
1083
}
1084
}
1085
NET_EPOCH_EXIT(et);
1086
1087
return (result);
1088
}
1089
1090
int
1091
in_addprefix(struct in_ifaddr *target)
1092
{
1093
int error;
1094
1095
if (in_hasrtprefix(target)) {
1096
if (V_nosameprefix)
1097
return (EEXIST);
1098
else {
1099
rt_addrmsg(RTM_ADD, &target->ia_ifa,
1100
target->ia_ifp->if_fib);
1101
return (0);
1102
}
1103
}
1104
1105
/*
1106
* No-one seem to have this prefix route, so we try to insert it.
1107
*/
1108
rt_addrmsg(RTM_ADD, &target->ia_ifa, target->ia_ifp->if_fib);
1109
error = in_handle_ifaddr_route(RTM_ADD, target);
1110
if (!error)
1111
target->ia_flags |= IFA_ROUTE;
1112
return (error);
1113
}
1114
1115
/*
1116
* Removes either all lle entries for given @ia, or lle
1117
* corresponding to @ia address.
1118
*/
1119
static void
1120
in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
1121
{
1122
struct sockaddr_in addr, mask;
1123
struct sockaddr *saddr, *smask;
1124
struct ifnet *ifp;
1125
1126
saddr = (struct sockaddr *)&addr;
1127
bzero(&addr, sizeof(addr));
1128
addr.sin_len = sizeof(addr);
1129
addr.sin_family = AF_INET;
1130
smask = (struct sockaddr *)&mask;
1131
bzero(&mask, sizeof(mask));
1132
mask.sin_len = sizeof(mask);
1133
mask.sin_family = AF_INET;
1134
mask.sin_addr.s_addr = ia->ia_subnetmask;
1135
ifp = ia->ia_ifp;
1136
1137
if (all) {
1138
/*
1139
* Remove all L2 entries matching given prefix.
1140
* Convert address to host representation to avoid
1141
* doing this on every callback. ia_subnetmask is already
1142
* stored in host representation.
1143
*/
1144
addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
1145
lltable_prefix_free(AF_INET, saddr, smask, flags);
1146
} else {
1147
/* Remove interface address only */
1148
addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
1149
lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
1150
}
1151
}
1152
1153
/*
1154
* If there is no other address in the system that can serve a route to the
1155
* same prefix, remove the route. Hand over the route to the new address
1156
* otherwise.
1157
*/
1158
int
1159
in_scrubprefix(struct in_ifaddr *target, u_int flags)
1160
{
1161
struct epoch_tracker et;
1162
struct in_ifaddr *ia;
1163
struct in_addr prefix, mask, p, m;
1164
int error = 0;
1165
1166
/*
1167
* Remove the loopback route to the interface address.
1168
*/
1169
if (ia_need_loopback_route(target) && (flags & LLE_STATIC)) {
1170
struct in_ifaddr *eia;
1171
1172
eia = in_localip_more(target);
1173
1174
if (eia != NULL) {
1175
error = ifa_switch_loopback_route((struct ifaddr *)eia,
1176
(struct sockaddr *)&target->ia_addr);
1177
ifa_free(&eia->ia_ifa);
1178
} else {
1179
error = ifa_del_loopback_route((struct ifaddr *)target,
1180
(struct sockaddr *)&target->ia_addr);
1181
}
1182
}
1183
1184
ia_getrtprefix(target, &prefix, &mask);
1185
1186
if ((target->ia_flags & IFA_ROUTE) == 0) {
1187
rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1188
1189
/*
1190
* Removing address from !IFF_UP interface or
1191
* prefix which exists on other interface (along with route).
1192
* No entries should exist here except target addr.
1193
* Given that, delete this entry only.
1194
*/
1195
in_scrubprefixlle(target, 0, flags);
1196
return (0);
1197
}
1198
1199
NET_EPOCH_ENTER(et);
1200
CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1201
ia_getrtprefix(ia, &p, &m);
1202
1203
if (prefix.s_addr != p.s_addr ||
1204
mask.s_addr != m.s_addr)
1205
continue;
1206
1207
if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1208
continue;
1209
1210
/*
1211
* If we got a matching prefix address, move IFA_ROUTE and
1212
* the route itself to it. Make sure that routing daemons
1213
* get a heads-up.
1214
*/
1215
if ((ia->ia_flags & IFA_ROUTE) == 0) {
1216
ifa_ref(&ia->ia_ifa);
1217
NET_EPOCH_EXIT(et);
1218
error = in_handle_ifaddr_route(RTM_DELETE, target);
1219
if (error == 0)
1220
target->ia_flags &= ~IFA_ROUTE;
1221
else
1222
log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1223
error);
1224
/* Scrub all entries IFF interface is different */
1225
in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
1226
flags);
1227
error = in_handle_ifaddr_route(RTM_ADD, ia);
1228
if (error == 0)
1229
ia->ia_flags |= IFA_ROUTE;
1230
else
1231
log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1232
error);
1233
ifa_free(&ia->ia_ifa);
1234
return (error);
1235
}
1236
}
1237
NET_EPOCH_EXIT(et);
1238
1239
/*
1240
* remove all L2 entries on the given prefix
1241
*/
1242
in_scrubprefixlle(target, 1, flags);
1243
1244
/*
1245
* As no-one seem to have this prefix, we can remove the route.
1246
*/
1247
rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1248
error = in_handle_ifaddr_route(RTM_DELETE, target);
1249
if (error == 0)
1250
target->ia_flags &= ~IFA_ROUTE;
1251
else
1252
log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1253
return (error);
1254
}
1255
1256
void
1257
in_ifscrub_all(void)
1258
{
1259
struct ifnet *ifp;
1260
struct ifaddr *ifa, *nifa;
1261
struct ifreq ifr;
1262
1263
IFNET_RLOCK();
1264
CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1265
/* Cannot lock here - lock recursion. */
1266
/* NET_EPOCH_ENTER(et); */
1267
CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1268
if (ifa->ifa_addr->sa_family != AF_INET)
1269
continue;
1270
1271
/*
1272
* This is ugly but the only way for legacy IP to
1273
* cleanly remove addresses and everything attached.
1274
*/
1275
bzero(&ifr, sizeof(ifr));
1276
ifr.ifr_addr = *ifa->ifa_addr;
1277
(void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
1278
ifp, NULL);
1279
}
1280
/* NET_EPOCH_EXIT(et); */
1281
in_purgemaddrs(ifp);
1282
igmp_domifdetach(ifp);
1283
}
1284
IFNET_RUNLOCK();
1285
}
1286
1287
bool
1288
in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
1289
{
1290
1291
return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1292
/*
1293
* Optionally check for old-style (host 0) broadcast, but
1294
* taking into account that RFC 3021 obsoletes it.
1295
*/
1296
__predict_false(V_broadcast_lowest &&
1297
ia->ia_subnetmask != IN_RFC3021_MASK &&
1298
ntohl(in.s_addr) == ia->ia_subnet)) &&
1299
/*
1300
* Check for an all one subnetmask. These
1301
* only exist when an interface gets a secondary
1302
* address.
1303
*/
1304
ia->ia_subnetmask != (u_long)0xffffffff);
1305
}
1306
1307
/*
1308
* Return true if the address might be a local broadcast address.
1309
*/
1310
bool
1311
in_ifnet_broadcast(struct in_addr in, struct ifnet *ifp)
1312
{
1313
struct ifaddr *ifa;
1314
1315
NET_EPOCH_ASSERT();
1316
1317
if (in_broadcast(in))
1318
return (true);
1319
if ((ifp->if_flags & IFF_BROADCAST) == 0)
1320
return (false);
1321
/*
1322
* Look through the list of addresses for a match
1323
* with a broadcast address.
1324
*/
1325
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1326
if (ifa->ifa_addr->sa_family == AF_INET &&
1327
in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa))
1328
return (true);
1329
return (false);
1330
}
1331
1332
1333
static struct lltable *in_lltattach(struct ifnet *);
1334
void
1335
in_ifattach(void *arg __unused, struct ifnet *ifp)
1336
{
1337
struct in_ifinfo *ii;
1338
1339
ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1340
ii->ii_llt = in_lltattach(ifp);
1341
ii->ii_igmp = igmp_domifattach(ifp);
1342
ifp->if_inet = ii;
1343
}
1344
EVENTHANDLER_DEFINE(ifnet_arrival_event, in_ifattach, NULL,
1345
EVENTHANDLER_PRI_ANY);
1346
1347
/*
1348
* On interface removal, clean up IPv4 data structures hung off of the ifnet.
1349
*/
1350
static void
1351
in_ifdetach(void *arg __unused, struct ifnet *ifp)
1352
{
1353
struct in_ifinfo *ii = ifp->if_inet;
1354
1355
#ifdef VIMAGE
1356
/*
1357
* On VNET shutdown abort here as the stack teardown will do all
1358
* the work top-down for us. XXXGL: this logic is copied from
1359
* if_detach() before dom_ifattach removal. Ideally we'd like to have
1360
* same logic for VNET shutdown and normal detach. This means that
1361
* interfaces should be detach before protocols destroyed during VNET
1362
* shutdown.
1363
*/
1364
if (!VNET_IS_SHUTTING_DOWN(ifp->if_vnet))
1365
#endif
1366
{
1367
IN_MULTI_LOCK();
1368
in_pcbpurgeif0(&V_ripcbinfo, ifp);
1369
in_pcbpurgeif0(&V_udbinfo, ifp);
1370
in_pcbpurgeif0(&V_ulitecbinfo, ifp);
1371
in_purgemaddrs(ifp);
1372
IN_MULTI_UNLOCK();
1373
1374
/*
1375
* Make sure all multicast deletions invoking if_ioctl() are
1376
* completed before returning. Else we risk accessing a freed
1377
* ifnet structure pointer.
1378
*/
1379
inm_release_wait(NULL);
1380
}
1381
1382
igmp_domifdetach(ifp);
1383
lltable_free(ii->ii_llt);
1384
free(ii, M_IFADDR);
1385
}
1386
EVENTHANDLER_DEFINE(ifnet_departure_event, in_ifdetach, NULL,
1387
EVENTHANDLER_PRI_ANY);
1388
1389
static void
1390
in_ifnet_event(void *arg __unused, struct ifnet *ifp, int event)
1391
{
1392
struct epoch_tracker et;
1393
struct ifaddr *ifa;
1394
struct in_ifaddr *ia;
1395
int error;
1396
1397
NET_EPOCH_ENTER(et);
1398
switch (event) {
1399
case IFNET_EVENT_DOWN:
1400
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1401
if (ifa->ifa_addr->sa_family != AF_INET)
1402
continue;
1403
ia = (struct in_ifaddr *)ifa;
1404
if ((ia->ia_flags & IFA_ROUTE) == 0)
1405
continue;
1406
ifa_ref(ifa);
1407
/*
1408
* in_scrubprefix() kills the interface route.
1409
*/
1410
in_scrubprefix(ia, 0);
1411
/*
1412
* in_ifadown gets rid of all the rest of the
1413
* routes. This is not quite the right thing
1414
* to do, but at least if we are running a
1415
* routing process they will come back.
1416
*/
1417
in_ifadown(ifa, 0);
1418
ifa_free(ifa);
1419
}
1420
break;
1421
1422
case IFNET_EVENT_UP:
1423
CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1424
if (ifa->ifa_addr->sa_family != AF_INET)
1425
continue;
1426
ia = (struct in_ifaddr *)ifa;
1427
if (ia->ia_flags & IFA_ROUTE)
1428
continue;
1429
ifa_ref(ifa);
1430
error = ifa_del_loopback_route(ifa, ifa->ifa_addr);
1431
rt_addrmsg(RTM_ADD, ifa, ifa->ifa_ifp->if_fib);
1432
error = in_handle_ifaddr_route(RTM_ADD, ia);
1433
if (error == 0)
1434
ia->ia_flags |= IFA_ROUTE;
1435
error = ifa_add_loopback_route(ifa, ifa->ifa_addr);
1436
ifa_free(ifa);
1437
}
1438
break;
1439
}
1440
NET_EPOCH_EXIT(et);
1441
}
1442
EVENTHANDLER_DEFINE(ifnet_event, in_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
1443
1444
/*
1445
* Delete all IPv4 multicast address records, and associated link-layer
1446
* multicast address records, associated with ifp.
1447
* XXX It looks like domifdetach runs AFTER the link layer cleanup.
1448
* XXX This should not race with ifma_protospec being set during
1449
* a new allocation, if it does, we have bigger problems.
1450
*/
1451
static void
1452
in_purgemaddrs(struct ifnet *ifp)
1453
{
1454
struct epoch_tracker et;
1455
struct in_multi_head purgeinms;
1456
struct in_multi *inm;
1457
struct ifmultiaddr *ifma;
1458
1459
SLIST_INIT(&purgeinms);
1460
IN_MULTI_LIST_LOCK();
1461
1462
/*
1463
* Extract list of in_multi associated with the detaching ifp
1464
* which the PF_INET layer is about to release.
1465
* We need to do this as IF_ADDR_LOCK() may be re-acquired
1466
* by code further down.
1467
*/
1468
IF_ADDR_WLOCK(ifp);
1469
NET_EPOCH_ENTER(et);
1470
CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1471
inm = inm_ifmultiaddr_get_inm(ifma);
1472
if (inm == NULL)
1473
continue;
1474
inm_rele_locked(&purgeinms, inm);
1475
}
1476
NET_EPOCH_EXIT(et);
1477
IF_ADDR_WUNLOCK(ifp);
1478
1479
inm_release_list_deferred(&purgeinms);
1480
igmp_ifdetach(ifp);
1481
IN_MULTI_LIST_UNLOCK();
1482
}
1483
1484
struct in_llentry {
1485
struct llentry base;
1486
};
1487
1488
#define IN_LLTBL_DEFAULT_HSIZE 32
1489
#define IN_LLTBL_HASH(k, h) \
1490
(((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1491
1492
/*
1493
* Do actual deallocation of @lle.
1494
*/
1495
static void
1496
in_lltable_destroy_lle_unlocked(epoch_context_t ctx)
1497
{
1498
struct llentry *lle;
1499
1500
lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
1501
LLE_LOCK_DESTROY(lle);
1502
LLE_REQ_DESTROY(lle);
1503
free(lle, M_LLTABLE);
1504
}
1505
1506
/*
1507
* Called by LLE_FREE_LOCKED when number of references
1508
* drops to zero.
1509
*/
1510
static void
1511
in_lltable_destroy_lle(struct llentry *lle)
1512
{
1513
1514
LLE_WUNLOCK(lle);
1515
NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1516
}
1517
1518
static struct llentry *
1519
in_lltable_new(struct in_addr addr4, u_int flags)
1520
{
1521
struct in_llentry *lle;
1522
1523
lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1524
if (lle == NULL) /* NB: caller generates msg */
1525
return NULL;
1526
1527
/*
1528
* For IPv4 this will trigger "arpresolve" to generate
1529
* an ARP request.
1530
*/
1531
lle->base.la_expire = time_uptime; /* mark expired */
1532
lle->base.r_l3addr.addr4 = addr4;
1533
lle->base.lle_refcnt = 1;
1534
lle->base.lle_free = in_lltable_destroy_lle;
1535
LLE_LOCK_INIT(&lle->base);
1536
LLE_REQ_INIT(&lle->base);
1537
callout_init(&lle->base.lle_timer, 1);
1538
1539
return (&lle->base);
1540
}
1541
1542
static int
1543
in_lltable_match_prefix(const struct sockaddr *saddr,
1544
const struct sockaddr *smask, u_int flags, struct llentry *lle)
1545
{
1546
struct in_addr addr, mask, lle_addr;
1547
1548
addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1549
mask = ((const struct sockaddr_in *)smask)->sin_addr;
1550
lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1551
1552
if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1553
return (0);
1554
1555
if (lle->la_flags & LLE_IFADDR) {
1556
/*
1557
* Delete LLE_IFADDR records IFF address & flag matches.
1558
* Note that addr is the interface address within prefix
1559
* being matched.
1560
* Note also we should handle 'ifdown' cases without removing
1561
* ifaddr macs.
1562
*/
1563
if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1564
return (1);
1565
return (0);
1566
}
1567
1568
/* flags & LLE_STATIC means deleting both dynamic and static entries */
1569
if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1570
return (1);
1571
1572
return (0);
1573
}
1574
1575
static void
1576
in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1577
{
1578
size_t pkts_dropped;
1579
1580
LLE_WLOCK_ASSERT(lle);
1581
KASSERT(llt != NULL, ("lltable is NULL"));
1582
1583
/* Unlink entry from table if not already */
1584
if ((lle->la_flags & LLE_LINKED) != 0) {
1585
LLTABLE_LOCK_ASSERT(llt);
1586
lltable_unlink_entry(llt, lle);
1587
}
1588
1589
/* Drop hold queue */
1590
pkts_dropped = llentry_free(lle);
1591
ARPSTAT_ADD(dropped, pkts_dropped);
1592
}
1593
1594
static int
1595
in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1596
{
1597
struct nhop_object *nh;
1598
struct in_addr addr;
1599
1600
KASSERT(l3addr->sa_family == AF_INET,
1601
("sin_family %d", l3addr->sa_family));
1602
1603
addr = ((const struct sockaddr_in *)l3addr)->sin_addr;
1604
1605
nh = fib4_lookup(ifp->if_fib, addr, 0, NHR_NONE, 0);
1606
if (nh == NULL)
1607
return (EINVAL);
1608
1609
/*
1610
* If the gateway for an existing host route matches the target L3
1611
* address, which is a special route inserted by some implementation
1612
* such as MANET, and the interface is of the correct type, then
1613
* allow for ARP to proceed.
1614
*/
1615
if (nh->nh_flags & NHF_GATEWAY) {
1616
if (!(nh->nh_flags & NHF_HOST) || nh->nh_ifp->if_type != IFT_ETHER ||
1617
(nh->nh_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1618
memcmp(nh->gw_sa.sa_data, l3addr->sa_data,
1619
sizeof(in_addr_t)) != 0) {
1620
return (EINVAL);
1621
}
1622
}
1623
1624
/*
1625
* Make sure that at least the destination address is covered
1626
* by the route. This is for handling the case where 2 or more
1627
* interfaces have the same prefix. An incoming packet arrives
1628
* on one interface and the corresponding outgoing packet leaves
1629
* another interface.
1630
*/
1631
if ((nh->nh_ifp != ifp) && (nh->nh_flags & NHF_HOST) == 0) {
1632
struct in_ifaddr *ia = (struct in_ifaddr *)ifaof_ifpforaddr(l3addr, ifp);
1633
struct in_addr dst_addr, mask_addr;
1634
1635
if (ia == NULL)
1636
return (EINVAL);
1637
1638
/*
1639
* ifaof_ifpforaddr() returns _best matching_ IFA.
1640
* It is possible that ifa prefix does not cover our address.
1641
* Explicitly verify and fail if that's the case.
1642
*/
1643
dst_addr = IA_SIN(ia)->sin_addr;
1644
mask_addr.s_addr = htonl(ia->ia_subnetmask);
1645
1646
if (!IN_ARE_MASKED_ADDR_EQUAL(dst_addr, addr, mask_addr))
1647
return (EINVAL);
1648
}
1649
1650
return (0);
1651
}
1652
1653
static inline uint32_t
1654
in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1655
{
1656
1657
return (IN_LLTBL_HASH(dst.s_addr, hsize));
1658
}
1659
1660
static uint32_t
1661
in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1662
{
1663
1664
return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1665
}
1666
1667
static void
1668
in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1669
{
1670
struct sockaddr_in *sin;
1671
1672
sin = (struct sockaddr_in *)sa;
1673
bzero(sin, sizeof(*sin));
1674
sin->sin_family = AF_INET;
1675
sin->sin_len = sizeof(*sin);
1676
sin->sin_addr = lle->r_l3addr.addr4;
1677
}
1678
1679
static inline struct llentry *
1680
in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1681
{
1682
struct llentry *lle;
1683
struct llentries *lleh;
1684
u_int hashidx;
1685
1686
hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1687
lleh = &llt->lle_head[hashidx];
1688
CK_LIST_FOREACH(lle, lleh, lle_next) {
1689
if (lle->la_flags & LLE_DELETED)
1690
continue;
1691
if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1692
break;
1693
}
1694
1695
return (lle);
1696
}
1697
1698
static void
1699
in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
1700
{
1701
1702
lle->la_flags |= LLE_DELETED;
1703
EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1704
#ifdef DIAGNOSTIC
1705
log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1706
#endif
1707
llentry_free(lle);
1708
}
1709
1710
static struct llentry *
1711
in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1712
{
1713
const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1714
struct ifnet *ifp = llt->llt_ifp;
1715
struct llentry *lle;
1716
char linkhdr[LLE_MAX_LINKHDR];
1717
size_t linkhdrsize;
1718
int lladdr_off;
1719
1720
KASSERT(l3addr->sa_family == AF_INET,
1721
("sin_family %d", l3addr->sa_family));
1722
1723
/*
1724
* A route that covers the given address must have
1725
* been installed 1st because we are doing a resolution,
1726
* verify this.
1727
*/
1728
if (!(flags & LLE_IFADDR) &&
1729
in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1730
return (NULL);
1731
1732
lle = in_lltable_new(sin->sin_addr, flags);
1733
if (lle == NULL) {
1734
log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1735
return (NULL);
1736
}
1737
lle->la_flags = flags;
1738
if (flags & LLE_STATIC)
1739
lle->r_flags |= RLLE_VALID;
1740
if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1741
linkhdrsize = LLE_MAX_LINKHDR;
1742
if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
1743
linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1744
in_lltable_free_entry(llt, lle);
1745
return (NULL);
1746
}
1747
lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1748
lladdr_off);
1749
lle->la_flags |= LLE_STATIC;
1750
lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
1751
lle->la_expire = 0;
1752
}
1753
1754
return (lle);
1755
}
1756
1757
/*
1758
* Return NULL if not found or marked for deletion.
1759
* If found return lle read locked.
1760
*/
1761
static struct llentry *
1762
in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1763
{
1764
const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1765
struct llentry *lle;
1766
1767
LLTABLE_RLOCK_ASSERT(llt);
1768
KASSERT(l3addr->sa_family == AF_INET,
1769
("sin_family %d", l3addr->sa_family));
1770
KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
1771
(LLE_UNLOCKED | LLE_EXCLUSIVE),
1772
("wrong lle request flags: %#x", flags));
1773
1774
lle = in_lltable_find_dst(llt, sin->sin_addr);
1775
if (lle == NULL)
1776
return (NULL);
1777
if (flags & LLE_UNLOCKED)
1778
return (lle);
1779
1780
if (flags & LLE_EXCLUSIVE)
1781
LLE_WLOCK(lle);
1782
else
1783
LLE_RLOCK(lle);
1784
1785
/*
1786
* If the afdata lock is not held, the LLE may have been unlinked while
1787
* we were blocked on the LLE lock. Check for this case.
1788
*/
1789
if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
1790
if (flags & LLE_EXCLUSIVE)
1791
LLE_WUNLOCK(lle);
1792
else
1793
LLE_RUNLOCK(lle);
1794
return (NULL);
1795
}
1796
return (lle);
1797
}
1798
1799
static int
1800
in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
1801
struct sysctl_req *wr)
1802
{
1803
struct ifnet *ifp = llt->llt_ifp;
1804
/* XXX stack use */
1805
struct {
1806
struct rt_msghdr rtm;
1807
struct sockaddr_in sin;
1808
struct sockaddr_dl sdl;
1809
} arpc;
1810
struct sockaddr_dl *sdl;
1811
int error;
1812
1813
bzero(&arpc, sizeof(arpc));
1814
/* skip deleted entries */
1815
if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1816
return (0);
1817
/* Skip if jailed and not a valid IP of the prison. */
1818
lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
1819
if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0)
1820
return (0);
1821
/*
1822
* produce a msg made of:
1823
* struct rt_msghdr;
1824
* struct sockaddr_in; (IPv4)
1825
* struct sockaddr_dl;
1826
*/
1827
arpc.rtm.rtm_msglen = sizeof(arpc);
1828
arpc.rtm.rtm_version = RTM_VERSION;
1829
arpc.rtm.rtm_type = RTM_GET;
1830
arpc.rtm.rtm_flags = RTF_UP;
1831
arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1832
1833
/* publish */
1834
if (lle->la_flags & LLE_PUB)
1835
arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1836
1837
sdl = &arpc.sdl;
1838
sdl->sdl_family = AF_LINK;
1839
sdl->sdl_len = sizeof(*sdl);
1840
sdl->sdl_index = ifp->if_index;
1841
sdl->sdl_type = ifp->if_type;
1842
if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1843
sdl->sdl_alen = ifp->if_addrlen;
1844
bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1845
} else {
1846
sdl->sdl_alen = 0;
1847
bzero(LLADDR(sdl), ifp->if_addrlen);
1848
}
1849
1850
arpc.rtm.rtm_rmx.rmx_expire =
1851
lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1852
arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1853
if (lle->la_flags & LLE_STATIC)
1854
arpc.rtm.rtm_flags |= RTF_STATIC;
1855
if (lle->la_flags & LLE_IFADDR)
1856
arpc.rtm.rtm_flags |= RTF_PINNED;
1857
arpc.rtm.rtm_index = ifp->if_index;
1858
error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1859
1860
return (error);
1861
}
1862
1863
static void
1864
in_lltable_post_resolved(struct lltable *llt, struct llentry *lle)
1865
{
1866
struct ifnet *ifp = llt->llt_ifp;
1867
1868
/* gratuitous ARP */
1869
if ((lle->la_flags & LLE_PUB) != 0)
1870
arprequest(ifp, &lle->r_l3addr.addr4, &lle->r_l3addr.addr4,
1871
lle->ll_addr);
1872
}
1873
1874
static struct lltable *
1875
in_lltattach(struct ifnet *ifp)
1876
{
1877
struct lltable *llt;
1878
1879
llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1880
llt->llt_af = AF_INET;
1881
llt->llt_ifp = ifp;
1882
1883
llt->llt_lookup = in_lltable_lookup;
1884
llt->llt_alloc_entry = in_lltable_alloc;
1885
llt->llt_delete_entry = in_lltable_delete_entry;
1886
llt->llt_dump_entry = in_lltable_dump_entry;
1887
llt->llt_hash = in_lltable_hash;
1888
llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1889
llt->llt_free_entry = in_lltable_free_entry;
1890
llt->llt_match_prefix = in_lltable_match_prefix;
1891
llt->llt_mark_used = llentry_mark_used;
1892
llt->llt_post_resolved = in_lltable_post_resolved;
1893
lltable_link(llt);
1894
1895
return (llt);
1896
}
1897
1898
struct lltable *
1899
in_lltable_get(struct ifnet *ifp)
1900
{
1901
/* XXXGL: ??? */
1902
if (ifp->if_inet == NULL)
1903
return (NULL);
1904
1905
return (((struct in_ifinfo *)ifp->if_inet)->ii_llt);
1906
}
1907
1908