OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [bsd_tcpip/] [current/] [src/] [sys/] [net/] [if.c] - Blame information for rev 786

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.