Convert to critical sections.
[dragonfly.git] / sys / net / if.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)if.c 8.3 (Berkeley) 1/4/94
f23061d4 34 * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $
141697b6 35 * $DragonFly: src/sys/net/if.c,v 1.39 2005/06/05 12:35:24 joerg Exp $
984263bc
MD
36 */
37
38#include "opt_compat.h"
39#include "opt_inet6.h"
40#include "opt_inet.h"
41
42#include <sys/param.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/systm.h>
46#include <sys/proc.h>
6b6e0885 47#include <sys/protosw.h>
984263bc
MD
48#include <sys/socket.h>
49#include <sys/socketvar.h>
6b6e0885 50#include <sys/socketops.h>
984263bc
MD
51#include <sys/protosw.h>
52#include <sys/kernel.h>
53#include <sys/sockio.h>
54#include <sys/syslog.h>
55#include <sys/sysctl.h>
698ac46c 56#include <sys/domain.h>
e9cb6d99 57#include <sys/thread.h>
984263bc
MD
58
59#include <net/if.h>
60#include <net/if_arp.h>
61#include <net/if_dl.h>
62#include <net/if_types.h>
63#include <net/if_var.h>
4d723e5a 64#include <net/ifq_var.h>
984263bc
MD
65#include <net/radix.h>
66#include <net/route.h>
67#include <machine/stdarg.h>
68
e9cb6d99
MD
69#include <sys/thread2.h>
70
984263bc
MD
71#if defined(INET) || defined(INET6)
72/*XXX*/
73#include <netinet/in.h>
74#include <netinet/in_var.h>
75#include <netinet/if_ether.h>
76#ifdef INET6
77#include <machine/clock.h> /* XXX: temporal workaround for fxp issue */
78#include <netinet6/in6_var.h>
79#include <netinet6/in6_ifattach.h>
80#endif
81#endif
82
9eee10d0
DRJ
83#if defined(COMPAT_43)
84#include <emulation/43bsd/43bsd_socket.h>
85#endif /* COMPAT_43 */
86
e3e4574a
JS
87/*
88 * Support for non-ALTQ interfaces.
89 */
90static int ifq_classic_enqueue(struct ifaltq *, struct mbuf *,
91 struct altq_pktattr *);
92static struct mbuf *
93 ifq_classic_dequeue(struct ifaltq *, int);
94static int ifq_classic_request(struct ifaltq *, int, void *);
95
984263bc
MD
96/*
97 * System initialization
98 */
99
698ac46c
HS
100static void if_attachdomain(void *);
101static void if_attachdomain1(struct ifnet *);
61b8d551 102static int ifconf (u_long, caddr_t, struct thread *);
158abb01 103static void ifinit (void *);
158abb01
RG
104static void if_slowtimo (void *);
105static void link_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
106static int if_rtdel (struct radix_node *, void *);
984263bc
MD
107
108SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
109
110MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
111MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
b7b982e2 112MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
984263bc
MD
113
114int ifqmaxlen = IFQ_MAXLEN;
115struct ifnethead ifnet; /* depend on static init XXX */
116
117#ifdef INET6
118/*
119 * XXX: declare here to avoid to include many inet6 related files..
120 * should be more generalized?
121 */
158abb01 122extern void nd6_setmtu (struct ifnet *);
984263bc
MD
123#endif
124
158abb01
RG
125struct if_clone *if_clone_lookup (const char *, int *);
126int if_clone_list (struct if_clonereq *);
984263bc
MD
127
128LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
129int if_cloners_count;
130
abbb44bb
JS
131struct callout if_slowtimo_timer;
132
984263bc
MD
133/*
134 * Network interface utility routines.
135 *
136 * Routines with ifa_ifwith* names take sockaddr *'s as
137 * parameters.
138 */
139/* ARGSUSED*/
140void
f23061d4 141ifinit(void *dummy)
984263bc
MD
142{
143 struct ifnet *ifp;
144 int s;
145
abbb44bb
JS
146 callout_init(&if_slowtimo_timer);
147
984263bc
MD
148 s = splimp();
149 TAILQ_FOREACH(ifp, &ifnet, if_link) {
150 if (ifp->if_snd.ifq_maxlen == 0) {
3e4a09e7 151 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
984263bc
MD
152 ifp->if_snd.ifq_maxlen = ifqmaxlen;
153 }
154 }
155 splx(s);
abbb44bb 156
984263bc
MD
157 if_slowtimo(0);
158}
159
160int if_index = 0;
984263bc
MD
161struct ifnet **ifindex2ifnet = NULL;
162
984263bc
MD
163/*
164 * Attach an interface to the
165 * list of "active" interfaces.
166 */
167void
f23061d4 168if_attach(struct ifnet *ifp)
984263bc
MD
169{
170 unsigned socksize, ifasize;
171 int namelen, masklen;
82ed7fc2
RG
172 struct sockaddr_dl *sdl;
173 struct ifaddr *ifa;
e3e4574a 174 struct ifaltq *ifq;
590b8cd4 175
984263bc 176 static int if_indexlim = 8;
590b8cd4 177 static boolean_t inited;
984263bc
MD
178
179 if (!inited) {
180 TAILQ_INIT(&ifnet);
590b8cd4 181 inited = TRUE;
984263bc
MD
182 }
183
184 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
185 ifp->if_index = ++if_index;
186 /*
187 * XXX -
188 * The old code would work if the interface passed a pre-existing
189 * chain of ifaddrs to this code. We don't trust our callers to
190 * properly initialize the tailq, however, so we no longer allow
191 * this unlikely case.
192 */
193 TAILQ_INIT(&ifp->if_addrhead);
194 TAILQ_INIT(&ifp->if_prefixhead);
195 LIST_INIT(&ifp->if_multiaddrs);
196 getmicrotime(&ifp->if_lastchange);
141697b6 197 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
590b8cd4 198 unsigned int n;
141697b6 199 struct ifnet **q;
590b8cd4
JH
200
201 if_indexlim <<= 1;
984263bc
MD
202
203 /* grow ifindex2ifnet */
141697b6 204 n = if_indexlim * sizeof(*q);
590b8cd4 205 q = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
984263bc 206 if (ifindex2ifnet) {
f23061d4
JH
207 bcopy(ifindex2ifnet, q, n/2);
208 free(ifindex2ifnet, M_IFADDR);
984263bc 209 }
141697b6 210 ifindex2ifnet = q;
984263bc
MD
211 }
212
213 ifindex2ifnet[if_index] = ifp;
214
215 /*
216 * create a Link Level name for this device
217 */
3e4a09e7 218 namelen = strlen(ifp->if_xname);
984263bc
MD
219#define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
220 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
221 socksize = masklen + ifp->if_addrlen;
222#define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
223 if (socksize < sizeof(*sdl))
224 socksize = sizeof(*sdl);
225 socksize = ROUNDUP(socksize);
590b8cd4
JH
226 ifasize = sizeof(struct ifaddr) + 2 * socksize;
227 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
228 sdl = (struct sockaddr_dl *)(ifa + 1);
229 sdl->sdl_len = socksize;
230 sdl->sdl_family = AF_LINK;
231 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
232 sdl->sdl_nlen = namelen;
233 sdl->sdl_index = ifp->if_index;
234 sdl->sdl_type = ifp->if_type;
141697b6 235 ifp->if_lladdr = ifa;
590b8cd4
JH
236 ifa->ifa_ifp = ifp;
237 ifa->ifa_rtrequest = link_rtrequest;
238 ifa->ifa_addr = (struct sockaddr *)sdl;
239 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
240 ifa->ifa_netmask = (struct sockaddr *)sdl;
241 sdl->sdl_len = masklen;
242 while (namelen != 0)
243 sdl->sdl_data[--namelen] = 0xff;
244 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
984263bc 245
f2bd8b67
JS
246 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
247
e3e4574a
JS
248 ifq = &ifp->if_snd;
249 ifq->altq_type = 0;
250 ifq->altq_disc = NULL;
251 ifq->altq_flags &= ALTQF_CANTCHANGE;
252 ifq->altq_tbr = NULL;
253 ifq->altq_ifp = ifp;
b2f93efe 254 ifq_set_classic(ifq);
4d723e5a 255
9c70fe43 256 if (!SLIST_EMPTY(&domains))
698ac46c
HS
257 if_attachdomain1(ifp);
258
984263bc
MD
259 /* Announce the interface. */
260 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
261}
262
698ac46c
HS
263static void
264if_attachdomain(void *dummy)
265{
266 struct ifnet *ifp;
267 int s;
268
269 s = splnet();
270 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
271 if_attachdomain1(ifp);
272 splx(s);
273}
274SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
275 if_attachdomain, NULL);
276
277static void
278if_attachdomain1(struct ifnet *ifp)
279{
280 struct domain *dp;
281 int s;
282
283 s = splnet();
284
285 /* address family dependent data region */
286 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
9c70fe43 287 SLIST_FOREACH(dp, &domains, dom_next)
698ac46c
HS
288 if (dp->dom_ifattach)
289 ifp->if_afdata[dp->dom_family] =
290 (*dp->dom_ifattach)(ifp);
698ac46c
HS
291 splx(s);
292}
293
984263bc
MD
294/*
295 * Detach an interface, removing it from the
296 * list of "active" interfaces.
297 */
298void
f23061d4 299if_detach(struct ifnet *ifp)
984263bc
MD
300{
301 struct ifaddr *ifa;
302 struct radix_node_head *rnh;
303 int s;
304 int i;
698ac46c 305 struct domain *dp;
984263bc 306
f2bd8b67
JS
307 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
308
984263bc
MD
309 /*
310 * Remove routes and flush queues.
311 */
312 s = splnet();
323f031d 313#ifdef DEVICE_POLLING
9c095379
MD
314 if (ifp->if_flags & IFF_POLLING)
315 ether_poll_deregister(ifp);
323f031d 316#endif
984263bc
MD
317 if_down(ifp);
318
4d723e5a
JS
319 if (ifq_is_enabled(&ifp->if_snd))
320 altq_disable(&ifp->if_snd);
321 if (ifq_is_attached(&ifp->if_snd))
322 altq_detach(&ifp->if_snd);
323
984263bc 324 /*
984263bc
MD
325 * Clean up all addresses.
326 */
141697b6 327 ifp->if_lladdr = NULL;
984263bc
MD
328
329 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
330 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
331#ifdef INET
332 /* XXX: Ugly!! ad hoc just for INET */
333 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
334 struct ifaliasreq ifr;
335
f23061d4 336 bzero(&ifr, sizeof ifr);
984263bc
MD
337 ifr.ifra_addr = *ifa->ifa_addr;
338 if (ifa->ifa_dstaddr)
339 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
340 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
590b8cd4 341 NULL) == 0)
984263bc
MD
342 continue;
343 }
344#endif /* INET */
345#ifdef INET6
346 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
347 in6_purgeaddr(ifa);
348 /* ifp_addrhead is already updated */
349 continue;
350 }
351#endif /* INET6 */
352 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
353 IFAFREE(ifa);
354 }
355
a98eb818
JS
356#ifdef INET
357 /*
358 * Remove all IPv4 kernel structures related to ifp.
359 */
360 in_ifdetach(ifp);
361#endif
362
984263bc
MD
363#ifdef INET6
364 /*
365 * Remove all IPv6 kernel structs related to ifp. This should be done
366 * before removing routing entries below, since IPv6 interface direct
367 * routes are expected to be removed by the IPv6-specific kernel API.
368 * Otherwise, the kernel will detect some inconsistency and bark it.
369 */
370 in6_ifdetach(ifp);
371#endif
372
373 /*
374 * Delete all remaining routes using this interface
375 * Unfortuneatly the only way to do this is to slog through
376 * the entire routing table looking for routes which point
377 * to this interface...oh well...
378 */
379 for (i = 1; i <= AF_MAX; i++) {
380 if ((rnh = rt_tables[i]) == NULL)
381 continue;
f23061d4 382 rnh->rnh_walktree(rnh, if_rtdel, ifp);
984263bc
MD
383 }
384
385 /* Announce that the interface is gone. */
386 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
387
9c70fe43 388 SLIST_FOREACH(dp, &domains, dom_next)
698ac46c
HS
389 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
390 (*dp->dom_ifdetach)(ifp,
391 ifp->if_afdata[dp->dom_family]);
698ac46c 392
141697b6
JS
393 /*
394 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
395 */
75857e7c 396 ifindex2ifnet[ifp->if_index] = NULL;
141697b6
JS
397 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
398 if_index--;
75857e7c 399
984263bc
MD
400 TAILQ_REMOVE(&ifnet, ifp, if_link);
401 splx(s);
402}
403
404/*
405 * Delete Routes for a Network Interface
f23061d4 406 *
984263bc
MD
407 * Called for each routing entry via the rnh->rnh_walktree() call above
408 * to delete all route entries referencing a detaching network interface.
409 *
410 * Arguments:
411 * rn pointer to node in the routing table
412 * arg argument passed to rnh->rnh_walktree() - detaching interface
413 *
414 * Returns:
415 * 0 successful
416 * errno failed - reason indicated
417 *
418 */
419static int
f23061d4 420if_rtdel(struct radix_node *rn, void *arg)
984263bc
MD
421{
422 struct rtentry *rt = (struct rtentry *)rn;
423 struct ifnet *ifp = arg;
424 int err;
425
426 if (rt->rt_ifp == ifp) {
427
428 /*
429 * Protect (sorta) against walktree recursion problems
430 * with cloned routes
431 */
f23061d4 432 if (!(rt->rt_flags & RTF_UP))
984263bc
MD
433 return (0);
434
435 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
436 rt_mask(rt), rt->rt_flags,
437 (struct rtentry **) NULL);
438 if (err) {
439 log(LOG_WARNING, "if_rtdel: error %d\n", err);
440 }
441 }
442
443 return (0);
444}
445
446/*
447 * Create a clone network interface.
448 */
449int
f23061d4 450if_clone_create(char *name, int len)
984263bc
MD
451{
452 struct if_clone *ifc;
453 char *dp;
b7b982e2 454 int wildcard, bytoff, bitoff;
984263bc
MD
455 int unit;
456 int err;
457
458 ifc = if_clone_lookup(name, &unit);
459 if (ifc == NULL)
460 return (EINVAL);
461
462 if (ifunit(name) != NULL)
463 return (EEXIST);
464
b7b982e2 465 bytoff = bitoff = 0;
984263bc 466 wildcard = (unit < 0);
b7b982e2
MD
467 /*
468 * Find a free unit if none was given.
469 */
470 if (wildcard) {
f23061d4
JH
471 while (bytoff < ifc->ifc_bmlen &&
472 ifc->ifc_units[bytoff] == 0xff)
b7b982e2
MD
473 bytoff++;
474 if (bytoff >= ifc->ifc_bmlen)
475 return (ENOSPC);
476 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
477 bitoff++;
478 unit = (bytoff << 3) + bitoff;
479 }
984263bc 480
b7b982e2
MD
481 if (unit > ifc->ifc_maxunit)
482 return (ENXIO);
483
484 err = (*ifc->ifc_create)(ifc, unit);
984263bc
MD
485 if (err != 0)
486 return (err);
487
b7b982e2
MD
488 if (!wildcard) {
489 bytoff = unit >> 3;
490 bitoff = unit - (bytoff << 3);
491 }
492
493 /*
494 * Allocate the unit in the bitmap.
495 */
496 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
497 ("%s: bit is already set", __func__));
498 ifc->ifc_units[bytoff] |= (1 << bitoff);
499
984263bc
MD
500 /* In the wildcard case, we need to update the name. */
501 if (wildcard) {
502 for (dp = name; *dp != '\0'; dp++);
503 if (snprintf(dp, len - (dp-name), "%d", unit) >
504 len - (dp-name) - 1) {
505 /*
506 * This can only be a programmer error and
507 * there's no straightforward way to recover if
508 * it happens.
509 */
510 panic("if_clone_create(): interface name too long");
511 }
b7b982e2 512
984263bc
MD
513 }
514
f2bd8b67
JS
515 EVENTHANDLER_INVOKE(if_clone_event, ifc);
516
984263bc
MD
517 return (0);
518}
519
520/*
521 * Destroy a clone network interface.
522 */
523int
f23061d4 524if_clone_destroy(const char *name)
984263bc
MD
525{
526 struct if_clone *ifc;
527 struct ifnet *ifp;
b7b982e2
MD
528 int bytoff, bitoff;
529 int unit;
984263bc 530
b7b982e2 531 ifc = if_clone_lookup(name, &unit);
984263bc
MD
532 if (ifc == NULL)
533 return (EINVAL);
534
b7b982e2
MD
535 if (unit < ifc->ifc_minifs)
536 return (EINVAL);
537
984263bc
MD
538 ifp = ifunit(name);
539 if (ifp == NULL)
540 return (ENXIO);
541
542 if (ifc->ifc_destroy == NULL)
543 return (EOPNOTSUPP);
544
545 (*ifc->ifc_destroy)(ifp);
b7b982e2
MD
546
547 /*
548 * Compute offset in the bitmap and deallocate the unit.
549 */
550 bytoff = unit >> 3;
551 bitoff = unit - (bytoff << 3);
552 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
553 ("%s: bit is already cleared", __func__));
554 ifc->ifc_units[bytoff] &= ~(1 << bitoff);
984263bc
MD
555 return (0);
556}
557
558/*
559 * Look up a network interface cloner.
560 */
561struct if_clone *
f23061d4 562if_clone_lookup(const char *name, int *unitp)
984263bc
MD
563{
564 struct if_clone *ifc;
565 const char *cp;
566 int i;
567
568 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
569 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
570 if (ifc->ifc_name[i] != *cp)
571 goto next_ifc;
572 }
573 goto found_name;
574 next_ifc:
575 ifc = LIST_NEXT(ifc, ifc_list);
576 }
577
578 /* No match. */
579 return ((struct if_clone *)NULL);
580
581 found_name:
582 if (*cp == '\0') {
583 i = -1;
584 } else {
585 for (i = 0; *cp != '\0'; cp++) {
586 if (*cp < '0' || *cp > '9') {
587 /* Bogus unit number. */
588 return (NULL);
589 }
590 i = (i * 10) + (*cp - '0');
591 }
592 }
593
594 if (unitp != NULL)
595 *unitp = i;
596 return (ifc);
597}
598
599/*
600 * Register a network interface cloner.
601 */
602void
f23061d4 603if_clone_attach(struct if_clone *ifc)
984263bc 604{
b7b982e2
MD
605 int bytoff, bitoff;
606 int err;
607 int len, maxclone;
608 int unit;
609
610 KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
611 ("%s: %s requested more units then allowed (%d > %d)",
612 __func__, ifc->ifc_name, ifc->ifc_minifs,
613 ifc->ifc_maxunit + 1));
614 /*
615 * Compute bitmap size and allocate it.
616 */
617 maxclone = ifc->ifc_maxunit + 1;
618 len = maxclone >> 3;
619 if ((len << 3) < maxclone)
620 len++;
621 ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
622 ifc->ifc_bmlen = len;
984263bc
MD
623
624 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
625 if_cloners_count++;
b7b982e2
MD
626
627 for (unit = 0; unit < ifc->ifc_minifs; unit++) {
628 err = (*ifc->ifc_create)(ifc, unit);
629 KASSERT(err == 0,
630 ("%s: failed to create required interface %s%d",
631 __func__, ifc->ifc_name, unit));
632
633 /* Allocate the unit in the bitmap. */
634 bytoff = unit >> 3;
635 bitoff = unit - (bytoff << 3);
636 ifc->ifc_units[bytoff] |= (1 << bitoff);
637 }
984263bc
MD
638}
639
640/*
641 * Unregister a network interface cloner.
642 */
643void
f23061d4 644if_clone_detach(struct if_clone *ifc)
984263bc
MD
645{
646
647 LIST_REMOVE(ifc, ifc_list);
b7b982e2 648 free(ifc->ifc_units, M_CLONE);
984263bc
MD
649 if_cloners_count--;
650}
651
652/*
653 * Provide list of interface cloners to userspace.
654 */
655int
f23061d4 656if_clone_list(struct if_clonereq *ifcr)
984263bc
MD
657{
658 char outbuf[IFNAMSIZ], *dst;
659 struct if_clone *ifc;
660 int count, error = 0;
661
662 ifcr->ifcr_total = if_cloners_count;
663 if ((dst = ifcr->ifcr_buffer) == NULL) {
664 /* Just asking how many there are. */
665 return (0);
666 }
667
668 if (ifcr->ifcr_count < 0)
669 return (EINVAL);
670
671 count = (if_cloners_count < ifcr->ifcr_count) ?
672 if_cloners_count : ifcr->ifcr_count;
673
674 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
675 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
3e4a09e7 676 strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
984263bc
MD
677 error = copyout(outbuf, dst, IFNAMSIZ);
678 if (error)
679 break;
680 }
681
682 return (error);
683}
684
685/*
686 * Locate an interface based on a complete address.
687 */
984263bc 688struct ifaddr *
f23061d4 689ifa_ifwithaddr(struct sockaddr *addr)
984263bc 690{
82ed7fc2
RG
691 struct ifnet *ifp;
692 struct ifaddr *ifa;
984263bc 693
984263bc
MD
694 TAILQ_FOREACH(ifp, &ifnet, if_link)
695 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
696 if (ifa->ifa_addr->sa_family != addr->sa_family)
697 continue;
0c3c561c 698 if (sa_equal(addr, ifa->ifa_addr))
984263bc
MD
699 return (ifa);
700 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
0c3c561c 701 /* IPv6 doesn't have broadcast */
984263bc 702 ifa->ifa_broadaddr->sa_len != 0 &&
0c3c561c 703 sa_equal(ifa->ifa_broadaddr, addr))
984263bc
MD
704 return (ifa);
705 }
0c3c561c 706 return ((struct ifaddr *)NULL);
984263bc
MD
707}
708/*
709 * Locate the point to point interface with a given destination address.
710 */
984263bc 711struct ifaddr *
f23061d4 712ifa_ifwithdstaddr(struct sockaddr *addr)
984263bc 713{
82ed7fc2
RG
714 struct ifnet *ifp;
715 struct ifaddr *ifa;
984263bc
MD
716
717 TAILQ_FOREACH(ifp, &ifnet, if_link)
718 if (ifp->if_flags & IFF_POINTOPOINT)
719 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
720 if (ifa->ifa_addr->sa_family != addr->sa_family)
721 continue;
0c3c561c
JH
722 if (ifa->ifa_dstaddr &&
723 sa_equal(addr, ifa->ifa_dstaddr))
984263bc
MD
724 return (ifa);
725 }
0c3c561c 726 return ((struct ifaddr *)NULL);
984263bc
MD
727}
728
729/*
730 * Find an interface on a specific network. If many, choice
731 * is most specific found.
732 */
733struct ifaddr *
f23061d4 734ifa_ifwithnet(struct sockaddr *addr)
984263bc 735{
82ed7fc2
RG
736 struct ifnet *ifp;
737 struct ifaddr *ifa;
984263bc
MD
738 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
739 u_int af = addr->sa_family;
740 char *addr_data = addr->sa_data, *cplim;
741
742 /*
743 * AF_LINK addresses can be looked up directly by their index number,
744 * so do that if we can.
745 */
746 if (af == AF_LINK) {
82ed7fc2 747 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
590b8cd4 748
984263bc 749 if (sdl->sdl_index && sdl->sdl_index <= if_index)
141697b6 750 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
984263bc
MD
751 }
752
753 /*
754 * Scan though each interface, looking for ones that have
755 * addresses in this address family.
756 */
757 TAILQ_FOREACH(ifp, &ifnet, if_link) {
758 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
82ed7fc2 759 char *cp, *cp2, *cp3;
984263bc
MD
760
761 if (ifa->ifa_addr->sa_family != af)
762next: continue;
763 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
764 /*
765 * This is a bit broken as it doesn't
766 * take into account that the remote end may
767 * be a single node in the network we are
768 * looking for.
769 * The trouble is that we don't know the
770 * netmask for the remote end.
771 */
0c3c561c
JH
772 if (ifa->ifa_dstaddr != NULL &&
773 sa_equal(addr, ifa->ifa_dstaddr))
f23061d4 774 return (ifa);
984263bc
MD
775 } else {
776 /*
777 * if we have a special address handler,
778 * then use it instead of the generic one.
779 */
f23061d4 780 if (ifa->ifa_claim_addr) {
984263bc
MD
781 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
782 return (ifa);
783 } else {
784 continue;
785 }
786 }
787
788 /*
789 * Scan all the bits in the ifa's address.
790 * If a bit dissagrees with what we are
791 * looking for, mask it with the netmask
792 * to see if it really matters.
793 * (A byte at a time)
794 */
795 if (ifa->ifa_netmask == 0)
796 continue;
797 cp = addr_data;
798 cp2 = ifa->ifa_addr->sa_data;
799 cp3 = ifa->ifa_netmask->sa_data;
590b8cd4
JH
800 cplim = ifa->ifa_netmask->sa_len +
801 (char *)ifa->ifa_netmask;
984263bc
MD
802 while (cp3 < cplim)
803 if ((*cp++ ^ *cp2++) & *cp3++)
804 goto next; /* next address! */
805 /*
806 * If the netmask of what we just found
807 * is more specific than what we had before
808 * (if we had one) then remember the new one
809 * before continuing to search
810 * for an even better one.
811 */
812 if (ifa_maybe == 0 ||
f23061d4
JH
813 rn_refines((char *)ifa->ifa_netmask,
814 (char *)ifa_maybe->ifa_netmask))
984263bc
MD
815 ifa_maybe = ifa;
816 }
817 }
818 }
819 return (ifa_maybe);
820}
821
822/*
823 * Find an interface address specific to an interface best matching
824 * a given address.
825 */
826struct ifaddr *
f23061d4 827ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
984263bc 828{
82ed7fc2
RG
829 struct ifaddr *ifa;
830 char *cp, *cp2, *cp3;
831 char *cplim;
984263bc
MD
832 struct ifaddr *ifa_maybe = 0;
833 u_int af = addr->sa_family;
834
835 if (af >= AF_MAX)
836 return (0);
837 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
838 if (ifa->ifa_addr->sa_family != af)
839 continue;
840 if (ifa_maybe == 0)
841 ifa_maybe = ifa;
0c3c561c
JH
842 if (ifa->ifa_netmask == NULL) {
843 if (sa_equal(addr, ifa->ifa_addr) ||
844 (ifa->ifa_dstaddr != NULL &&
845 sa_equal(addr, ifa->ifa_dstaddr)))
984263bc
MD
846 return (ifa);
847 continue;
848 }
849 if (ifp->if_flags & IFF_POINTOPOINT) {
0c3c561c 850 if (sa_equal(addr, ifa->ifa_dstaddr))
984263bc
MD
851 return (ifa);
852 } else {
853 cp = addr->sa_data;
854 cp2 = ifa->ifa_addr->sa_data;
855 cp3 = ifa->ifa_netmask->sa_data;
856 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
857 for (; cp3 < cplim; cp3++)
858 if ((*cp++ ^ *cp2++) & *cp3)
859 break;
860 if (cp3 == cplim)
861 return (ifa);
862 }
863 }
864 return (ifa_maybe);
865}
866
867#include <net/route.h>
868
869/*
870 * Default action when installing a route with a Link Level gateway.
871 * Lookup an appropriate real ifa to point to.
872 * This should be moved to /sys/net/link.c eventually.
873 */
874static void
f23061d4 875link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
984263bc 876{
82ed7fc2 877 struct ifaddr *ifa;
984263bc
MD
878 struct sockaddr *dst;
879 struct ifnet *ifp;
880
f23061d4
JH
881 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
882 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
984263bc
MD
883 return;
884 ifa = ifaof_ifpforaddr(dst, ifp);
f23061d4 885 if (ifa != NULL) {
984263bc 886 IFAFREE(rt->rt_ifa);
f23061d4 887 IFAREF(ifa);
984263bc 888 rt->rt_ifa = ifa;
984263bc
MD
889 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
890 ifa->ifa_rtrequest(cmd, rt, info);
891 }
892}
893
894/*
895 * Mark an interface down and notify protocols of
896 * the transition.
897 * NOTE: must be called at splnet or eqivalent.
898 */
899void
f23061d4 900if_unroute(struct ifnet *ifp, int flag, int fam)
984263bc 901{
82ed7fc2 902 struct ifaddr *ifa;
984263bc
MD
903
904 ifp->if_flags &= ~flag;
905 getmicrotime(&ifp->if_lastchange);
906 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
907 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
908 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
4d723e5a 909 ifq_purge(&ifp->if_snd);
984263bc
MD
910 rt_ifmsg(ifp);
911}
912
913/*
914 * Mark an interface up and notify protocols of
915 * the transition.
916 * NOTE: must be called at splnet or eqivalent.
917 */
918void
f23061d4 919if_route(struct ifnet *ifp, int flag, int fam)
984263bc 920{
82ed7fc2 921 struct ifaddr *ifa;
984263bc
MD
922
923 ifp->if_flags |= flag;
924 getmicrotime(&ifp->if_lastchange);
925 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
926 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
927 pfctlinput(PRC_IFUP, ifa->ifa_addr);
928 rt_ifmsg(ifp);
929#ifdef INET6
930 in6_if_up(ifp);
931#endif
932}
933
934/*
5c703385
MD
935 * Mark an interface down and notify protocols of the transition. An
936 * interface going down is also considered to be a synchronizing event.
937 * We must ensure that all packet processing related to the interface
938 * has completed before we return so e.g. the caller can free the ifnet
939 * structure that the mbufs may be referencing.
940 *
984263bc
MD
941 * NOTE: must be called at splnet or eqivalent.
942 */
943void
f23061d4 944if_down(struct ifnet *ifp)
984263bc 945{
984263bc 946 if_unroute(ifp, IFF_UP, AF_UNSPEC);
5c703385 947 netmsg_service_sync();
984263bc
MD
948}
949
950/*
951 * Mark an interface up and notify protocols of
952 * the transition.
953 * NOTE: must be called at splnet or eqivalent.
954 */
955void
f23061d4 956if_up(struct ifnet *ifp)
984263bc
MD
957{
958
959 if_route(ifp, IFF_UP, AF_UNSPEC);
960}
961
984263bc
MD
962/*
963 * Handle interface watchdog timer routines. Called
964 * from softclock, we decrement timers (if set) and
965 * call the appropriate interface routine on expiration.
966 */
967static void
f23061d4 968if_slowtimo(void *arg)
984263bc 969{
82ed7fc2 970 struct ifnet *ifp;
984263bc
MD
971 int s = splimp();
972
973 TAILQ_FOREACH(ifp, &ifnet, if_link) {
974 if (ifp->if_timer == 0 || --ifp->if_timer)
975 continue;
976 if (ifp->if_watchdog)
977 (*ifp->if_watchdog)(ifp);
978 }
979 splx(s);
abbb44bb 980 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
984263bc
MD
981}
982
983/*
984 * Map interface name to
985 * interface structure pointer.
986 */
987struct ifnet *
988ifunit(const char *name)
989{
984263bc 990 struct ifnet *ifp;
984263bc 991
984263bc 992 /*
3e4a09e7 993 * Search all the interfaces for this name/number
984263bc 994 */
3e4a09e7 995
984263bc 996 TAILQ_FOREACH(ifp, &ifnet, if_link) {
3e4a09e7 997 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
984263bc
MD
998 break;
999 }
1000 return (ifp);
1001}
1002
1003
1004/*
1005 * Map interface name in a sockaddr_dl to
1006 * interface structure pointer.
1007 */
1008struct ifnet *
f23061d4 1009if_withname(struct sockaddr *sa)
984263bc
MD
1010{
1011 char ifname[IFNAMSIZ+1];
1012 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1013
1014 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1015 (sdl->sdl_nlen > IFNAMSIZ) )
1016 return NULL;
1017
1018 /*
1019 * ifunit wants a null-terminated name. It may not be null-terminated
1020 * in the sockaddr. We don't want to change the caller's sockaddr,
1021 * and there might not be room to put the trailing null anyway, so we
1022 * make a local copy that we know we can null terminate safely.
1023 */
1024
1025 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1026 ifname[sdl->sdl_nlen] = '\0';
1027 return ifunit(ifname);
1028}
1029
1030
1031/*
1032 * Interface ioctls.
1033 */
1034int
dadab5e9 1035ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
984263bc 1036{
41c20dac
MD
1037 struct ifnet *ifp;
1038 struct ifreq *ifr;
984263bc
MD
1039 struct ifstat *ifs;
1040 int error;
1041 short oif_flags;
1042 int new_flags;
1fdf0954
HP
1043 size_t namelen, onamelen;
1044 char new_name[IFNAMSIZ];
1045 struct ifaddr *ifa;
1046 struct sockaddr_dl *sdl;
984263bc
MD
1047
1048 switch (cmd) {
1049
1050 case SIOCGIFCONF:
1051 case OSIOCGIFCONF:
61b8d551 1052 return (ifconf(cmd, data, td));
984263bc
MD
1053 }
1054 ifr = (struct ifreq *)data;
1055
1056 switch (cmd) {
1057 case SIOCIFCREATE:
1058 case SIOCIFDESTROY:
dadab5e9 1059 if ((error = suser(td)) != 0)
984263bc
MD
1060 return (error);
1061 return ((cmd == SIOCIFCREATE) ?
1062 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1063 if_clone_destroy(ifr->ifr_name));
f23061d4 1064
984263bc
MD
1065 case SIOCIFGCLONERS:
1066 return (if_clone_list((struct if_clonereq *)data));
1067 }
1068
1069 ifp = ifunit(ifr->ifr_name);
1070 if (ifp == 0)
1071 return (ENXIO);
1072 switch (cmd) {
1073
1074 case SIOCGIFFLAGS:
1075 ifr->ifr_flags = ifp->if_flags;
46f25451 1076 ifr->ifr_flagshigh = ifp->if_flags >> 16;
984263bc
MD
1077 break;
1078
1079 case SIOCGIFCAP:
1080 ifr->ifr_reqcap = ifp->if_capabilities;
1081 ifr->ifr_curcap = ifp->if_capenable;
1082 break;
1083
1084 case SIOCGIFMETRIC:
1085 ifr->ifr_metric = ifp->if_metric;
1086 break;
1087
1088 case SIOCGIFMTU:
1089 ifr->ifr_mtu = ifp->if_mtu;
1090 break;
1091
1092 case SIOCGIFPHYS:
1093 ifr->ifr_phys = ifp->if_physical;
1094 break;
1095
1096 case SIOCSIFFLAGS:
dadab5e9 1097 error = suser(td);
984263bc
MD
1098 if (error)
1099 return (error);
1100 new_flags = (ifr->ifr_flags & 0xffff) |
1101 (ifr->ifr_flagshigh << 16);
1102 if (ifp->if_flags & IFF_SMART) {
1103 /* Smart drivers twiddle their own routes */
1104 } else if (ifp->if_flags & IFF_UP &&
1105 (new_flags & IFF_UP) == 0) {
1106 int s = splimp();
1107 if_down(ifp);
1108 splx(s);
1109 } else if (new_flags & IFF_UP &&
1110 (ifp->if_flags & IFF_UP) == 0) {
1111 int s = splimp();
1112 if_up(ifp);
1113 splx(s);
1114 }
9c095379
MD
1115
1116#ifdef DEVICE_POLLING
1117 if ((new_flags ^ ifp->if_flags) & IFF_POLLING) {
1118 if (new_flags & IFF_POLLING) {
1119 ether_poll_register(ifp);
1120 } else {
1121 ether_poll_deregister(ifp);
1122 }
1123 }
1124#endif
1125
984263bc
MD
1126 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1127 (new_flags &~ IFF_CANTCHANGE);
984263bc
MD
1128 if (new_flags & IFF_PPROMISC) {
1129 /* Permanently promiscuous mode requested */
1130 ifp->if_flags |= IFF_PROMISC;
1131 } else if (ifp->if_pcount == 0) {
1132 ifp->if_flags &= ~IFF_PROMISC;
1133 }
1134 if (ifp->if_ioctl)
f23061d4 1135 (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
984263bc
MD
1136 getmicrotime(&ifp->if_lastchange);
1137 break;
1138
1139 case SIOCSIFCAP:
dadab5e9 1140 error = suser(td);
984263bc
MD
1141 if (error)
1142 return (error);
1143 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1144 return (EINVAL);
f23061d4 1145 (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
984263bc
MD
1146 break;
1147
f23061d4
JH
1148 case SIOCSIFNAME:
1149 error = suser(td);
1150 if (error != 0)
1151 return (error);
1152 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1153 if (error != 0)
1154 return (error);
1fdf0954
HP
1155 if (new_name[0] == '\0')
1156 return (EINVAL);
f23061d4
JH
1157 if (ifunit(new_name) != NULL)
1158 return (EEXIST);
f2bd8b67
JS
1159
1160 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
f23061d4
JH
1161
1162 /* Announce the departure of the interface. */
1163 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1164
1165 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1166 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1167 /* XXX IFA_LOCK(ifa); */
1168 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1169 namelen = strlen(new_name);
1170 onamelen = sdl->sdl_nlen;
1171 /*
1172 * Move the address if needed. This is safe because we
1173 * allocate space for a name of length IFNAMSIZ when we
1174 * create this in if_attach().
1175 */
1176 if (namelen != onamelen) {
1177 bcopy(sdl->sdl_data + onamelen,
1178 sdl->sdl_data + namelen, sdl->sdl_alen);
1179 }
1180 bcopy(new_name, sdl->sdl_data, namelen);
1181 sdl->sdl_nlen = namelen;
1182 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1183 bzero(sdl->sdl_data, onamelen);
1184 while (namelen != 0)
1185 sdl->sdl_data[--namelen] = 0xff;
1186 /* XXX IFA_UNLOCK(ifa) */
f2bd8b67
JS
1187
1188 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
f23061d4
JH
1189
1190 /* Announce the return of the interface. */
1191 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1192 break;
1fdf0954 1193
984263bc 1194 case SIOCSIFMETRIC:
dadab5e9 1195 error = suser(td);
984263bc
MD
1196 if (error)
1197 return (error);
1198 ifp->if_metric = ifr->ifr_metric;
1199 getmicrotime(&ifp->if_lastchange);
1200 break;
1201
1202 case SIOCSIFPHYS:
dadab5e9 1203 error = suser(td);
984263bc
MD
1204 if (error)
1205 return error;
1206 if (!ifp->if_ioctl)
1207 return EOPNOTSUPP;
bd4539cc 1208 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
984263bc
MD
1209 if (error == 0)
1210 getmicrotime(&ifp->if_lastchange);
f23061d4 1211 return (error);
984263bc
MD
1212
1213 case SIOCSIFMTU:
1214 {
1215 u_long oldmtu = ifp->if_mtu;
1216
dadab5e9 1217 error = suser(td);
984263bc
MD
1218 if (error)
1219 return (error);
1220 if (ifp->if_ioctl == NULL)
1221 return (EOPNOTSUPP);
1222 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1223 return (EINVAL);
bd4539cc 1224 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
984263bc
MD
1225 if (error == 0) {
1226 getmicrotime(&ifp->if_lastchange);
1227 rt_ifmsg(ifp);
1228 }
1229 /*
1230 * If the link MTU changed, do network layer specific procedure.
1231 */
1232 if (ifp->if_mtu != oldmtu) {
1233#ifdef INET6
1234 nd6_setmtu(ifp);
1235#endif
1236 }
1237 return (error);
1238 }
1239
1240 case SIOCADDMULTI:
1241 case SIOCDELMULTI:
dadab5e9 1242 error = suser(td);
984263bc
MD
1243 if (error)
1244 return (error);
1245
1246 /* Don't allow group membership on non-multicast interfaces. */
1247 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1248 return EOPNOTSUPP;
1249
1250 /* Don't let users screw up protocols' entries. */
1251 if (ifr->ifr_addr.sa_family != AF_LINK)
1252 return EINVAL;
1253
1254 if (cmd == SIOCADDMULTI) {
1255 struct ifmultiaddr *ifma;
1256 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1257 } else {
1258 error = if_delmulti(ifp, &ifr->ifr_addr);
1259 }
1260 if (error == 0)
1261 getmicrotime(&ifp->if_lastchange);
1262 return error;
1263
1264 case SIOCSIFPHYADDR:
1265 case SIOCDIFPHYADDR:
1266#ifdef INET6
1267 case SIOCSIFPHYADDR_IN6:
1268#endif
1269 case SIOCSLIFPHYADDR:
1270 case SIOCSIFMEDIA:
1271 case SIOCSIFGENERIC:
dadab5e9 1272 error = suser(td);
984263bc
MD
1273 if (error)
1274 return (error);
1275 if (ifp->if_ioctl == 0)
1276 return (EOPNOTSUPP);
bd4539cc 1277 error = (*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred);
984263bc
MD
1278 if (error == 0)
1279 getmicrotime(&ifp->if_lastchange);
1280 return error;
1281
1282 case SIOCGIFSTATUS:
1283 ifs = (struct ifstat *)data;
1284 ifs->ascii[0] = '\0';
f23061d4 1285
984263bc
MD
1286 case SIOCGIFPSRCADDR:
1287 case SIOCGIFPDSTADDR:
1288 case SIOCGLIFPHYADDR:
1289 case SIOCGIFMEDIA:
1290 case SIOCGIFGENERIC:
1291 if (ifp->if_ioctl == 0)
1292 return (EOPNOTSUPP);
bd4539cc 1293 return ((*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred));
984263bc
MD
1294
1295 case SIOCSIFLLADDR:
dadab5e9 1296 error = suser(td);
984263bc
MD
1297 if (error)
1298 return (error);
1299 return if_setlladdr(ifp,
1300 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1301
1302 default:
1303 oif_flags = ifp->if_flags;
1304 if (so->so_proto == 0)
1305 return (EOPNOTSUPP);
1306#ifndef COMPAT_43
6b6e0885 1307 error = so_pru_control(so, cmd, data, ifp, td);
984263bc
MD
1308#else
1309 {
1310 int ocmd = cmd;
1311
1312 switch (cmd) {
1313
1314 case SIOCSIFDSTADDR:
1315 case SIOCSIFADDR:
1316 case SIOCSIFBRDADDR:
1317 case SIOCSIFNETMASK:
1318#if BYTE_ORDER != BIG_ENDIAN
1319 if (ifr->ifr_addr.sa_family == 0 &&
1320 ifr->ifr_addr.sa_len < 16) {
1321 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1322 ifr->ifr_addr.sa_len = 16;
1323 }
1324#else
1325 if (ifr->ifr_addr.sa_len == 0)
1326 ifr->ifr_addr.sa_len = 16;
1327#endif
1328 break;
1329
1330 case OSIOCGIFADDR:
1331 cmd = SIOCGIFADDR;
1332 break;
1333
1334 case OSIOCGIFDSTADDR:
1335 cmd = SIOCGIFDSTADDR;
1336 break;
1337
1338 case OSIOCGIFBRDADDR:
1339 cmd = SIOCGIFBRDADDR;
1340 break;
1341
1342 case OSIOCGIFNETMASK:
1343 cmd = SIOCGIFNETMASK;
1344 }
6b6e0885 1345 error = so_pru_control(so, cmd, data, ifp, td);
984263bc
MD
1346 switch (ocmd) {
1347
1348 case OSIOCGIFADDR:
1349 case OSIOCGIFDSTADDR:
1350 case OSIOCGIFBRDADDR:
1351 case OSIOCGIFNETMASK:
1352 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1353
1354 }
1355 }
1356#endif /* COMPAT_43 */
1357
1358 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1359#ifdef INET6
1360 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1361 if (ifp->if_flags & IFF_UP) {
1362 int s = splimp();
1363 in6_if_up(ifp);
1364 splx(s);
1365 }
1366#endif
1367 }
1368 return (error);
1369
1370 }
1371 return (0);
1372}
1373
1374/*
1375 * Set/clear promiscuous mode on interface ifp based on the truth value
1376 * of pswitch. The calls are reference counted so that only the first
1377 * "on" request actually has an effect, as does the final "off" request.
1378 * Results are undefined if the "off" and "on" requests are not matched.
1379 */
1380int
f23061d4 1381ifpromisc(struct ifnet *ifp, int pswitch)
984263bc
MD
1382{
1383 struct ifreq ifr;
1384 int error;
1385 int oldflags;
1386
1387 oldflags = ifp->if_flags;
46f25451 1388 if (ifp->if_flags & IFF_PPROMISC) {
984263bc
MD
1389 /* Do nothing if device is in permanently promiscuous mode */
1390 ifp->if_pcount += pswitch ? 1 : -1;
1391 return (0);
1392 }
1393 if (pswitch) {
1394 /*
1395 * If the device is not configured up, we cannot put it in
1396 * promiscuous mode.
1397 */
1398 if ((ifp->if_flags & IFF_UP) == 0)
1399 return (ENETDOWN);
1400 if (ifp->if_pcount++ != 0)
1401 return (0);
1402 ifp->if_flags |= IFF_PROMISC;
3e4a09e7
MD
1403 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1404 ifp->if_xname);
984263bc
MD
1405 } else {
1406 if (--ifp->if_pcount > 0)
1407 return (0);
1408 ifp->if_flags &= ~IFF_PROMISC;
3e4a09e7
MD
1409 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1410 ifp->if_xname);
984263bc
MD
1411 }
1412 ifr.ifr_flags = ifp->if_flags;
46f25451 1413 ifr.ifr_flagshigh = ifp->if_flags >> 16;
bd4539cc
JH
1414 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1415 (struct ucred *)NULL);
984263bc
MD
1416 if (error == 0)
1417 rt_ifmsg(ifp);
1418 else
1419 ifp->if_flags = oldflags;
1420 return error;
1421}
1422
1423/*
1424 * Return interface configuration
1425 * of system. List may be used
1426 * in later ioctl's (above) to get
1427 * other information.
1428 */
984263bc 1429static int
61b8d551 1430ifconf(u_long cmd, caddr_t data, struct thread *td)
984263bc 1431{
41c20dac
MD
1432 struct ifconf *ifc = (struct ifconf *)data;
1433 struct ifnet *ifp;
1434 struct ifaddr *ifa;
984263bc
MD
1435 struct sockaddr *sa;
1436 struct ifreq ifr, *ifrp;
1437 int space = ifc->ifc_len, error = 0;
1438
1439 ifrp = ifc->ifc_req;
1440 TAILQ_FOREACH(ifp, &ifnet, if_link) {
3e4a09e7 1441 int addrs;
984263bc 1442
f23061d4 1443 if (space <= sizeof ifr)
984263bc 1444 break;
623c059e
JS
1445
1446 /*
95f018e8
MD
1447 * Zero the stack declared structure first to prevent
1448 * memory disclosure.
623c059e 1449 */
95f018e8 1450 bzero(&ifr, sizeof(ifr));
3e4a09e7
MD
1451 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1452 >= sizeof(ifr.ifr_name)) {
984263bc
MD
1453 error = ENAMETOOLONG;
1454 break;
984263bc
MD
1455 }
1456
1457 addrs = 0;
984263bc 1458 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
f23061d4 1459 if (space <= sizeof ifr)
984263bc
MD
1460 break;
1461 sa = ifa->ifa_addr;
61b8d551
JH
1462 if (td->td_proc->p_ucred->cr_prison &&
1463 prison_if(td, sa))
984263bc
MD
1464 continue;
1465 addrs++;
1466#ifdef COMPAT_43
1467 if (cmd == OSIOCGIFCONF) {
1468 struct osockaddr *osa =
1469 (struct osockaddr *)&ifr.ifr_addr;
1470 ifr.ifr_addr = *sa;
1471 osa->sa_family = sa->sa_family;
f23061d4 1472 error = copyout(&ifr, ifrp, sizeof ifr);
984263bc
MD
1473 ifrp++;
1474 } else
1475#endif
1476 if (sa->sa_len <= sizeof(*sa)) {
1477 ifr.ifr_addr = *sa;
f23061d4 1478 error = copyout(&ifr, ifrp, sizeof ifr);
984263bc
MD
1479 ifrp++;
1480 } else {
f23061d4 1481 if (space < (sizeof ifr) + sa->sa_len -
984263bc
MD
1482 sizeof(*sa))
1483 break;
1484 space -= sa->sa_len - sizeof(*sa);
f23061d4
JH
1485 error = copyout(&ifr, ifrp,
1486 sizeof ifr.ifr_name);
984263bc 1487 if (error == 0)
f23061d4
JH
1488 error = copyout(sa, &ifrp->ifr_addr,
1489 sa->sa_len);
984263bc
MD
1490 ifrp = (struct ifreq *)
1491 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1492 }
1493 if (error)
1494 break;
f23061d4 1495 space -= sizeof ifr;
984263bc
MD
1496 }
1497 if (error)
1498 break;
1499 if (!addrs) {
f23061d4
JH
1500 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
1501 error = copyout(&ifr, ifrp, sizeof ifr);
984263bc
MD
1502 if (error)
1503 break;
f23061d4 1504 space -= sizeof ifr;
984263bc
MD
1505 ifrp++;
1506 }
1507 }
1508 ifc->ifc_len -= space;
1509 return (error);
1510}
1511
1512/*
1513 * Just like if_promisc(), but for all-multicast-reception mode.
1514 */
1515int
f23061d4 1516if_allmulti(struct ifnet *ifp, int onswitch)
984263bc
MD
1517{
1518 int error = 0;
1519 int s = splimp();
1520 struct ifreq ifr;
1521
1522 if (onswitch) {
1523 if (ifp->if_amcount++ == 0) {
1524 ifp->if_flags |= IFF_ALLMULTI;
1525 ifr.ifr_flags = ifp->if_flags;
46f25451 1526 ifr.ifr_flagshigh = ifp->if_flags >> 16;
bd4539cc
JH
1527 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1528 (struct ucred *)NULL);
984263bc
MD
1529 }
1530 } else {
1531 if (ifp->if_amcount > 1) {
1532 ifp->if_amcount--;
1533 } else {
1534 ifp->if_amcount = 0;
1535 ifp->if_flags &= ~IFF_ALLMULTI;
1536 ifr.ifr_flags = ifp->if_flags;
46f25451 1537 ifr.ifr_flagshigh = ifp->if_flags >> 16;
bd4539cc
JH
1538 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1539 (struct ucred *)NULL);
984263bc
MD
1540 }
1541 }
1542 splx(s);
1543
1544 if (error == 0)
1545 rt_ifmsg(ifp);
1546 return error;
1547}
1548
1549/*
1550 * Add a multicast listenership to the interface in question.
1551 * The link layer provides a routine which converts
1552 */
1553int
f23061d4
JH
1554if_addmulti(
1555 struct ifnet *ifp, /* interface to manipulate */
1556 struct sockaddr *sa, /* address to add */
1557 struct ifmultiaddr **retifma)
984263bc
MD
1558{
1559 struct sockaddr *llsa, *dupsa;
1560 int error, s;
1561 struct ifmultiaddr *ifma;
1562
1563 /*
1564 * If the matching multicast address already exists
1565 * then don't add a new one, just add a reference
1566 */
1567 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
0c3c561c 1568 if (sa_equal(sa, ifma->ifma_addr)) {
984263bc
MD
1569 ifma->ifma_refcount++;
1570 if (retifma)
1571 *retifma = ifma;
1572 return 0;
1573 }
1574 }
1575
1576 /*
1577 * Give the link layer a chance to accept/reject it, and also
1578 * find out which AF_LINK address this maps to, if it isn't one
1579 * already.
1580 */
1581 if (ifp->if_resolvemulti) {
1582 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1583 if (error) return error;
1584 } else {
1585 llsa = 0;
1586 }
1587
1588 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1589 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1590 bcopy(sa, dupsa, sa->sa_len);
1591
1592 ifma->ifma_addr = dupsa;
1593 ifma->ifma_lladdr = llsa;
1594 ifma->ifma_ifp = ifp;
1595 ifma->ifma_refcount = 1;
1596 ifma->ifma_protospec = 0;
1597 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1598
1599 /*
1600 * Some network interfaces can scan the address list at
1601 * interrupt time; lock them out.
1602 */
1603 s = splimp();
1604 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1605 splx(s);
1606 *retifma = ifma;
1607
1608 if (llsa != 0) {
1609 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
0c3c561c 1610 if (sa_equal(ifma->ifma_addr, llsa))
984263bc
MD
1611 break;
1612 }
1613 if (ifma) {
1614 ifma->ifma_refcount++;
1615 } else {
1616 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1617 M_IFMADDR, M_WAITOK);
1618 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1619 M_IFMADDR, M_WAITOK);
1620 bcopy(llsa, dupsa, llsa->sa_len);
1621 ifma->ifma_addr = dupsa;
1622 ifma->ifma_ifp = ifp;
1623 ifma->ifma_refcount = 1;
1624 s = splimp();
1625 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1626 splx(s);
1627 }
1628 }
1629 /*
1630 * We are certain we have added something, so call down to the
1631 * interface to let them know about it.
1632 */
1633 s = splimp();
bd4539cc 1634 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, (struct ucred *)NULL);
984263bc
MD
1635 splx(s);
1636
1637 return 0;
1638}
1639
1640/*
1641 * Remove a reference to a multicast address on this interface. Yell
1642 * if the request does not match an existing membership.
1643 */
1644int
f23061d4 1645if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
984263bc
MD
1646{
1647 struct ifmultiaddr *ifma;
1648 int s;
1649
1650 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
0c3c561c 1651 if (sa_equal(sa, ifma->ifma_addr))
984263bc
MD
1652 break;
1653 if (ifma == 0)
1654 return ENOENT;
1655
1656 if (ifma->ifma_refcount > 1) {
1657 ifma->ifma_refcount--;
1658 return 0;
1659 }
1660
1661 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1662 sa = ifma->ifma_lladdr;
1663 s = splimp();
1664 LIST_REMOVE(ifma, ifma_link);
1665 /*
1666 * Make sure the interface driver is notified
1667 * in the case of a link layer mcast group being left.
1668 */
1669 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
bd4539cc 1670 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
984263bc
MD
1671 splx(s);
1672 free(ifma->ifma_addr, M_IFMADDR);
1673 free(ifma, M_IFMADDR);
1674 if (sa == 0)
1675 return 0;
1676
1677 /*
1678 * Now look for the link-layer address which corresponds to
1679 * this network address. It had been squirreled away in
1680 * ifma->ifma_lladdr for this purpose (so we don't have
1681 * to call ifp->if_resolvemulti() again), and we saved that
1682 * value in sa above. If some nasty deleted the
1683 * link-layer address out from underneath us, we can deal because
1684 * the address we stored was is not the same as the one which was
1685 * in the record for the link-layer address. (So we don't complain
1686 * in that case.)
1687 */
1688 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
0c3c561c 1689 if (sa_equal(sa, ifma->ifma_addr))
984263bc
MD
1690 break;
1691 if (ifma == 0)
1692 return 0;
1693
1694 if (ifma->ifma_refcount > 1) {
1695 ifma->ifma_refcount--;
1696 return 0;
1697 }
1698
1699 s = splimp();
1700 LIST_REMOVE(ifma, ifma_link);
bd4539cc 1701 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, (struct ucred *)NULL);
984263bc
MD
1702 splx(s);
1703 free(ifma->ifma_addr, M_IFMADDR);
1704 free(sa, M_IFMADDR);
1705 free(ifma, M_IFMADDR);
1706
1707 return 0;
1708}
1709
1710/*
1711 * Set the link layer address on an interface.
1712 *
1713 * At this time we only support certain types of interfaces,
1714 * and we don't allow the length of the address to change.
1715 */
1716int
1717if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1718{
1719 struct sockaddr_dl *sdl;
1720 struct ifaddr *ifa;
1721 struct ifreq ifr;
1722
f2682cb9 1723 sdl = IF_LLSOCKADDR(ifp);
984263bc
MD
1724 if (sdl == NULL)
1725 return (EINVAL);
1726 if (len != sdl->sdl_alen) /* don't allow length to change */
1727 return (EINVAL);
1728 switch (ifp->if_type) {
1729 case IFT_ETHER: /* these types use struct arpcom */
1730 case IFT_FDDI:
1731 case IFT_XETHER:
1732 case IFT_ISO88025:
1733 case IFT_L2VLAN:
1734 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1735 /* FALLTHROUGH */
1736 case IFT_ARCNET:
1737 bcopy(lladdr, LLADDR(sdl), len);
1738 break;
1739 default:
1740 return (ENODEV);
1741 }
1742 /*
1743 * If the interface is already up, we need
1744 * to re-init it in order to reprogram its
1745 * address filter.
1746 */
1747 if ((ifp->if_flags & IFF_UP) != 0) {
1748 ifp->if_flags &= ~IFF_UP;
1749 ifr.ifr_flags = ifp->if_flags;
46f25451 1750 ifr.ifr_flagshigh = ifp->if_flags >> 16;
bd4539cc
JH
1751 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1752 (struct ucred *)NULL);
984263bc
MD
1753 ifp->if_flags |= IFF_UP;
1754 ifr.ifr_flags = ifp->if_flags;
46f25451 1755 ifr.ifr_flagshigh = ifp->if_flags >> 16;
bd4539cc
JH
1756 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
1757 (struct ucred *)NULL);
984263bc
MD
1758#ifdef INET
1759 /*
1760 * Also send gratuitous ARPs to notify other nodes about
1761 * the address change.
1762 */
1763 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1764 if (ifa->ifa_addr != NULL &&
1765 ifa->ifa_addr->sa_family == AF_INET)
1766 arp_ifinit(ifp, ifa);
1767 }
1768#endif
1769 }
1770 return (0);
1771}
1772
1773struct ifmultiaddr *
f23061d4 1774ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
984263bc
MD
1775{
1776 struct ifmultiaddr *ifma;
1777
1778 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
0c3c561c 1779 if (sa_equal(ifma->ifma_addr, sa))
984263bc
MD
1780 break;
1781
1782 return ifma;
1783}
1784
1550dfd9
MD
1785/*
1786 * The name argument must be a pointer to storage which will last as
1787 * long as the interface does. For physical devices, the result of
1788 * device_get_name(dev) is a good choice and for pseudo-devices a
1789 * static string works well.
1790 */
1791void
1792if_initname(struct ifnet *ifp, const char *name, int unit)
1793{
3e4a09e7
MD
1794 ifp->if_dname = name;
1795 ifp->if_dunit = unit;
1550dfd9
MD
1796 if (unit != IF_DUNIT_NONE)
1797 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
1798 else
1799 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1800}
1801
984263bc
MD
1802int
1803if_printf(struct ifnet *ifp, const char *fmt, ...)
1804{
e2565a42 1805 __va_list ap;
984263bc
MD
1806 int retval;
1807
3e4a09e7 1808 retval = printf("%s: ", ifp->if_xname);
e2565a42 1809 __va_start(ap, fmt);
984263bc 1810 retval += vprintf(fmt, ap);
e2565a42 1811 __va_end(ap);
984263bc
MD
1812 return (retval);
1813}
1814
1815SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1816SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
e3e4574a 1817
b2f93efe
JS
1818void
1819ifq_set_classic(struct ifaltq *ifq)
1820{
1821 ifq->altq_enqueue = ifq_classic_enqueue;
1822 ifq->altq_dequeue = ifq_classic_dequeue;
1823 ifq->altq_request = ifq_classic_request;
1824}
1825
e3e4574a
JS
1826static int
1827ifq_classic_enqueue(struct ifaltq *ifq, struct mbuf *m,
1828 struct altq_pktattr *pa __unused)
1829{
e9cb6d99 1830 crit_enter();
e3e4574a
JS
1831 if (IF_QFULL(ifq)) {
1832 m_freem(m);
e9cb6d99 1833 crit_exit();
e3e4574a
JS
1834 return(ENOBUFS);
1835 } else {
1836 IF_ENQUEUE(ifq, m);
e9cb6d99 1837 crit_exit();
e3e4574a
JS
1838 return(0);
1839 }
1840}
1841
1842static struct mbuf *
1843ifq_classic_dequeue(struct ifaltq *ifq, int op)
1844{
1845 struct mbuf *m;
1846
e9cb6d99 1847 crit_enter();
e3e4574a
JS
1848 switch (op) {
1849 case ALTDQ_POLL:
1850 IF_POLL(ifq, m);
1851 break;
1852 case ALTDQ_REMOVE:
1853 IF_DEQUEUE(ifq, m);
1854 break;
1855 default:
1856 panic("unsupported ALTQ dequeue op: %d", op);
1857 }
e9cb6d99 1858 crit_exit();
e3e4574a
JS
1859 return(m);
1860}
1861
1862static int
1863ifq_classic_request(struct ifaltq *ifq, int req, void *arg)
1864{
e9cb6d99 1865 crit_enter();
e3e4574a
JS
1866 switch (req) {
1867 case ALTRQ_PURGE:
1868 IF_DRAIN(ifq);
1869 break;
1870 default:
1871 panic("unspported ALTQ request: %d", req);
1872 }
e9cb6d99 1873 crit_exit();
e3e4574a
JS
1874 return(0);
1875}
e9cb6d99 1876