source: rtems/cpukit/libnetworking/net/if.c @ 9b4422a2

4.115
Last change on this file since 9b4422a2 was 9b4422a2, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 15:09:24

Remove All CVS Id Strings Possible Using a Script

Script does what is expected and tries to do it as
smartly as possible.

+ remove occurrences of two blank comment lines

next to each other after Id string line removed.

+ remove entire comment blocks which only exited to

contain CVS Ids

+ If the processing left a blank line at the top of

a file, it was removed.

  • Property mode set to 100644
File size: 18.7 KB
Line 
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *      @(#)if.c        8.5 (Berkeley) 1/9/95
30 * $FreeBSD: src/sys/net/if.c,v 1.226 2005/04/15 01:51:26 cperciva Exp $
31 */
32
33
34#ifdef HAVE_CONFIG_H
35#include "config.h"
36#endif
37
38#include <sys/param.h>
39#include <sys/queue.h>
40#include <sys/mbuf.h>
41#include <sys/systm.h>
42#include <sys/proc.h>
43#include <sys/socket.h>
44#include <sys/socketvar.h>
45#include <sys/protosw.h>
46#include <sys/kernel.h>
47#include <sys/ioctl.h>
48#include <errno.h>
49#include <sys/syslog.h>
50#include <sys/sysctl.h>
51
52#include <net/if.h>
53#include <net/if_dl.h>
54#include <net/if_types.h>
55#include <net/if_var.h>
56#include <net/radix.h>
57
58/*
59 * System initialization
60 */
61
62static int ifconf(u_long, caddr_t);
63       void ifinit(void *);
64static void if_qflush(struct ifqueue *);
65static void if_slowtimo(void *);
66static void link_rtrequest(int, struct rtentry *, struct sockaddr *);
67
68SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
69
70
71int     ifqmaxlen = IFQ_MAXLEN;
72struct  ifnet *ifnet;
73
74/*
75 * Network interface utility routines.
76 *
77 * Routines with ifa_ifwith* names take sockaddr *'s as
78 * parameters.
79 *
80 * This routine assumes that it will be called at splimp() or higher.
81 */
82/* ARGSUSED*/
83void
84ifinit(void *dummy)
85{
86        struct ifnet *ifp;
87
88        for (ifp = ifnet; ifp; ifp = ifp->if_next)
89                if (ifp->if_snd.ifq_maxlen == 0)
90                        ifp->if_snd.ifq_maxlen = ifqmaxlen;
91        if_slowtimo(0);
92}
93
94int if_index = 0;
95struct ifaddr **ifnet_addrs;
96
97
98/*
99 * Attach an interface to the
100 * list of "active" interfaces.
101 */
102void
103if_attach(struct ifnet *ifp)
104{
105        unsigned socksize, ifasize;
106        int namelen, masklen;
107        char workbuf[64];
108        struct ifnet **p = &ifnet;
109        struct sockaddr_dl *sdl;
110        struct ifaddr *ifa;
111        static int if_indexlim = 8;
112
113
114        while (*p)
115                p = &((*p)->if_next);
116        *p = ifp;
117        ifp->if_index = ++if_index;
118        microtime(&ifp->if_lastchange);
119        if (ifnet_addrs == 0 || if_index >= if_indexlim) {
120                unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
121                struct ifaddr **q = (struct ifaddr **)
122                                        malloc(n, M_IFADDR, M_WAITOK);
123                bzero((caddr_t)q, n);
124                if (ifnet_addrs) {
125                        bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
126                        free((caddr_t)ifnet_addrs, M_IFADDR);
127                }
128                ifnet_addrs = q;
129        }
130        /*
131         * create a Link Level name for this device
132         */
133        namelen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
134#define _offsetof(t, m) ((uintptr_t)((void*)&((t *)0)->m))
135        masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
136        socksize = masklen + ifp->if_addrlen;
137#define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
138        socksize = ROUNDUP(socksize);
139        if (socksize < sizeof(*sdl))
140                socksize = sizeof(*sdl);
141        ifasize = sizeof(*ifa) + 2 * socksize;
142        ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
143        if (ifa) {
144                bzero((caddr_t)ifa, ifasize);
145                sdl = (struct sockaddr_dl *)(ifa + 1);
146                sdl->sdl_len = socksize;
147                sdl->sdl_family = AF_LINK;
148                bcopy(workbuf, sdl->sdl_data, namelen);
149                sdl->sdl_nlen = namelen;
150                sdl->sdl_index = ifp->if_index;
151                sdl->sdl_type = ifp->if_type;
152                ifnet_addrs[if_index - 1] = ifa;
153                ifa->ifa_ifp = ifp;
154                ifa->ifa_next = ifp->if_addrlist;
155                ifa->ifa_rtrequest = link_rtrequest;
156                ifp->if_addrlist = ifa;
157                ifa->ifa_addr = (struct sockaddr *)sdl;
158
159                sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
160                ifa->ifa_netmask = (struct sockaddr *)sdl;
161                sdl->sdl_len = masklen;
162                while (namelen != 0)
163                        sdl->sdl_data[--namelen] = 0xff;
164        }
165}
166/*
167 * Locate an interface based on a complete address.
168 */
169/*ARGSUSED*/
170struct ifaddr *
171ifa_ifwithaddr(struct sockaddr *addr)
172{
173        struct ifnet *ifp;
174        struct ifaddr *ifa;
175
176#define equal(a1, a2) \
177  (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
178        for (ifp = ifnet; ifp; ifp = ifp->if_next)
179            for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
180                if (ifa->ifa_addr->sa_family != addr->sa_family)
181                        continue;
182                if (equal(addr, ifa->ifa_addr))
183                        return (ifa);
184                if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
185                    equal(ifa->ifa_broadaddr, addr))
186                        return (ifa);
187        }
188        return ((struct ifaddr *)0);
189}
190/*
191 * Locate the point to point interface with a given destination address.
192 */
193/*ARGSUSED*/
194struct ifaddr *
195ifa_ifwithdstaddr(struct sockaddr *addr)
196{
197        struct ifnet *ifp;
198        struct ifaddr *ifa;
199
200        for (ifp = ifnet; ifp; ifp = ifp->if_next)
201            if (ifp->if_flags & IFF_POINTOPOINT)
202                for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
203                        if (ifa->ifa_addr->sa_family != addr->sa_family)
204                                continue;
205                        if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
206                                return (ifa);
207        }
208        return ((struct ifaddr *)0);
209}
210
211/*
212 * Find an interface on a specific network.  If many, choice
213 * is most specific found.
214 */
215struct ifaddr *
216ifa_ifwithnet(struct sockaddr *addr)
217{
218        struct ifnet *ifp;
219        struct ifaddr *ifa;
220        struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
221        u_int af = addr->sa_family;
222        char *addr_data = addr->sa_data, *cplim;
223
224        if (af == AF_LINK) {
225            struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
226            if (sdl->sdl_index && sdl->sdl_index <= if_index)
227                return (ifnet_addrs[sdl->sdl_index - 1]);
228        }
229        for (ifp = ifnet; ifp; ifp = ifp->if_next) {
230                for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
231                        char *cp, *cp2, *cp3;
232
233                        if (ifa->ifa_addr->sa_family != af)
234                                next: continue;
235                        if (ifp->if_flags & IFF_POINTOPOINT) {
236                                if (ifa->ifa_dstaddr != 0
237                                    && equal(addr, ifa->ifa_dstaddr))
238                                        return (ifa);
239                        } else {
240                                /*
241                                 * if we have a special address handler,
242                                 * then use it instead of the generic one.
243                                 */
244                                if (ifa->ifa_claim_addr) {
245                                        if ((*ifa->ifa_claim_addr)(ifa, addr)) {
246                                                return (ifa);
247                                        } else {
248                                                continue;
249                                        }
250                                }
251
252                                /*
253                                 * Scan all the bits in the ifa's address.
254                                 * If a bit dissagrees with what we are
255                                 * looking for, mask it with the netmask
256                                 * to see if it really matters.
257                                 * (A byte at a time)
258                                 */
259                                if (ifa->ifa_netmask == 0)
260                                        continue;
261                                cp = addr_data;
262                                cp2 = ifa->ifa_addr->sa_data;
263                                cp3 = ifa->ifa_netmask->sa_data;
264                                cplim = ifa->ifa_netmask->sa_len
265                                        + (char *)ifa->ifa_netmask;
266                                while (cp3 < cplim)
267                                        if ((*cp++ ^ *cp2++) & *cp3++)
268                                                goto next; /* next address! */
269                                /*
270                                 * If the netmask of what we just found
271                                 * is more specific than what we had before
272                                 * (if we had one) then remember the new one
273                                 * before continuing to search
274                                 * for an even better one.
275                                 */
276                                if (ifa_maybe == 0 ||
277                                    rn_refines((caddr_t)ifa->ifa_netmask,
278                                    (caddr_t)ifa_maybe->ifa_netmask))
279                                        ifa_maybe = ifa;
280                        }
281                }
282        }
283        return (ifa_maybe);
284}
285
286/*
287 * Find an interface address specific to an interface best matching
288 * a given address.
289 */
290struct ifaddr *
291ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
292{
293        struct ifaddr *ifa;
294        char *cp, *cp2, *cp3;
295        char *cplim;
296        struct ifaddr *ifa_maybe = 0;
297        u_int af = addr->sa_family;
298
299        if (af >= AF_MAX)
300                return (0);
301        for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
302                if (ifa->ifa_addr->sa_family != af)
303                        continue;
304                if (ifa_maybe == 0)
305                        ifa_maybe = ifa;
306                if (ifa->ifa_netmask == 0) {
307                        if (equal(addr, ifa->ifa_addr) ||
308                            (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
309                                return (ifa);
310                        continue;
311                }
312                if (ifp->if_flags & IFF_POINTOPOINT) {
313                        if (equal(addr, ifa->ifa_dstaddr))
314                                return (ifa);
315                } else {
316                        cp = addr->sa_data;
317                        cp2 = ifa->ifa_addr->sa_data;
318                        cp3 = ifa->ifa_netmask->sa_data;
319                        cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
320                        for (; cp3 < cplim; cp3++)
321                                if ((*cp++ ^ *cp2++) & *cp3)
322                                        break;
323                        if (cp3 == cplim)
324                                return (ifa);
325                }
326        }
327        return (ifa_maybe);
328}
329
330#include <net/route.h>
331
332/*
333 * Default action when installing a route with a Link Level gateway.
334 * Lookup an appropriate real ifa to point to.
335 * This should be moved to /sys/net/link.c eventually.
336 */
337static void
338link_rtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
339{
340        struct ifaddr *ifa;
341        struct sockaddr *dst;
342        struct ifnet *ifp;
343
344        if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
345            ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
346                return;
347        ifa = ifaof_ifpforaddr(dst, ifp);
348        if (ifa) {
349                IFAFREE(rt->rt_ifa);
350                rt->rt_ifa = ifa;
351                ifa->ifa_refcnt++;
352                if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
353                        ifa->ifa_rtrequest(cmd, rt, sa);
354        }
355}
356
357/*
358 * Mark an interface down and notify protocols of
359 * the transition.
360 * NOTE: must be called at splnet or eqivalent.
361 */
362void
363if_down(struct ifnet *ifp)
364{
365        struct ifaddr *ifa;
366
367        ifp->if_flags &= ~IFF_UP;
368        microtime(&ifp->if_lastchange);
369        for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
370                pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
371        if_qflush(&ifp->if_snd);
372        rt_ifmsg(ifp);
373}
374
375/*
376 * Mark an interface up and notify protocols of
377 * the transition.
378 * NOTE: must be called at splnet or eqivalent.
379 */
380void
381if_up(struct ifnet *ifp)
382{
383
384        ifp->if_flags |= IFF_UP;
385        microtime(&ifp->if_lastchange);
386#ifdef notyet
387        struct ifaddr *ifa;
388        /* this has no effect on IP, and will kill all iso connections XXX */
389        for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
390                pfctlinput(PRC_IFUP, ifa->ifa_addr);
391#endif
392        rt_ifmsg(ifp);
393}
394
395/*
396 * Flush an interface queue.
397 */
398static void
399if_qflush(struct ifqueue *ifq)
400{
401        struct mbuf *m, *n;
402
403        n = ifq->ifq_head;
404        while ((m = n) != 0) {
405                n = m->m_act;
406                m_freem(m);
407        }
408        ifq->ifq_head = 0;
409        ifq->ifq_tail = 0;
410        ifq->ifq_len = 0;
411}
412
413/*
414 * Handle interface watchdog timer routines.  Called
415 * from softclock, we decrement timers (if set) and
416 * call the appropriate interface routine on expiration.
417 */
418static void
419if_slowtimo(void *arg)
420{
421        struct ifnet *ifp;
422        int s = splimp();
423
424        for (ifp = ifnet; ifp; ifp = ifp->if_next) {
425                if (ifp->if_timer == 0 || --ifp->if_timer)
426                        continue;
427                if (ifp->if_watchdog)
428                        (*ifp->if_watchdog)(ifp);
429        }
430        splx(s);
431        timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
432}
433
434/*
435 * Map interface name to
436 * interface structure pointer.
437 */
438struct ifnet *
439ifunit(char *name)
440{
441        char *cp;
442        struct ifnet *ifp;
443        int unit;
444        unsigned len;
445        char *ep, c;
446
447        for (cp = name; cp < name + IFNAMSIZ && *cp; cp++)
448                if (*cp >= '0' && *cp <= '9')
449                        break;
450        if (*cp == '\0' || cp == name + IFNAMSIZ)
451                return ((struct ifnet *)0);
452        /*
453         * Save first char of unit, and pointer to it,
454         * so we can put a null there to avoid matching
455         * initial substrings of interface names.
456         */
457        len = cp - name + 1;
458        c = *cp;
459        ep = cp;
460        for (unit = 0; *cp >= '0' && *cp <= '9'; )
461                unit = unit * 10 + *cp++ - '0';
462        if (*cp != '\0')
463                return 0;       /* no trailing garbage allowed */
464        *ep = 0;
465        for (ifp = ifnet; ifp; ifp = ifp->if_next) {
466                if (bcmp(ifp->if_name, name, len))
467                        continue;
468                if (unit == ifp->if_unit)
469                        break;
470        }
471        *ep = c;
472        return (ifp);
473}
474
475/*
476 * Interface ioctls.
477 */
478int
479ifioctl(struct socket *so, u_long cmd, caddr_t data, struct proc *p)
480{
481        struct ifnet *ifp;
482        struct ifreq *ifr;
483        int error;
484
485        switch (cmd) {
486
487        case SIOCGIFCONF:
488        case OSIOCGIFCONF:
489                return (ifconf(cmd, data));
490        }
491        ifr = (struct ifreq *)data;
492        ifp = ifunit(ifr->ifr_name);
493        if (ifp == 0)
494                return (ENXIO);
495        switch (cmd) {
496
497        case SIOCGIFFLAGS:
498                ifr->ifr_flags = ifp->if_flags;
499                break;
500
501        case SIOCGIFMETRIC:
502                ifr->ifr_metric = ifp->if_metric;
503                break;
504
505        case SIOCGIFMTU:
506                ifr->ifr_mtu = ifp->if_mtu;
507                break;
508
509        case SIOCGIFPHYS:
510                ifr->ifr_phys = ifp->if_physical;
511                break;
512
513        case SIOCSIFFLAGS:
514                error = suser(p->p_ucred, &p->p_acflag);
515                if (error)
516                        return (error);
517                if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
518                        int s = splimp();
519                        if_down(ifp);
520                        splx(s);
521                }
522                if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
523                        int s = splimp();
524                        if_up(ifp);
525                        splx(s);
526                }
527                ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
528                        (ifr->ifr_flags &~ IFF_CANTCHANGE);
529                if (ifp->if_ioctl)
530                        (void) (*ifp->if_ioctl)(ifp, cmd, data);
531                microtime(&ifp->if_lastchange);
532                break;
533
534        case SIOCSIFMETRIC:
535                error = suser(p->p_ucred, &p->p_acflag);
536                if (error)
537                        return (error);
538                ifp->if_metric = ifr->ifr_metric;
539                microtime(&ifp->if_lastchange);
540                break;
541
542        case SIOCSIFPHYS:
543                error = suser(p->p_ucred, &p->p_acflag);
544                if (error)
545                        return error;
546                if (!ifp->if_ioctl)
547                        return EOPNOTSUPP;
548                error = (*ifp->if_ioctl)(ifp, cmd, data);
549                if (error == 0)
550                        microtime(&ifp->if_lastchange);
551                return(error);
552
553        case SIOCSIFMTU:
554                error = suser(p->p_ucred, &p->p_acflag);
555                if (error)
556                        return (error);
557                if (ifp->if_ioctl == NULL)
558                        return (EOPNOTSUPP);
559                /*
560                 * 72 was chosen below because it is the size of a TCP/IP
561                 * header (40) + the minimum mss (32).
562                 */
563                if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535L)
564                        return (EINVAL);
565                error = (*ifp->if_ioctl)(ifp, cmd, data);
566                if (error == 0)
567                        microtime(&ifp->if_lastchange);
568                return(error);
569
570        case SIOCADDMULTI:
571        case SIOCDELMULTI:
572                error = suser(p->p_ucred, &p->p_acflag);
573                if (error)
574                        return (error);
575                if (ifp->if_ioctl == NULL)
576                        return (EOPNOTSUPP);
577                error = (*ifp->if_ioctl)(ifp, cmd, data);
578                if (error == 0 )
579                        microtime(&ifp->if_lastchange);
580                return(error);
581
582        case SIOCSIFMEDIA:
583                error = suser(p->p_ucred, &p->p_acflag);
584                if (error)
585                        return (error);
586                if (ifp->if_ioctl == NULL)
587                        return (EOPNOTSUPP);
588                error = (*ifp->if_ioctl)(ifp, cmd, data);
589                if (error == 0)
590                        microtime(&ifp->if_lastchange);
591                return error;
592
593        case SIOCGIFMEDIA:
594                if (ifp->if_ioctl == NULL)
595                        return (EOPNOTSUPP);
596                return ((*ifp->if_ioctl)(ifp, cmd, data));
597
598        default:
599                if (so->so_proto == 0)
600                        return (EOPNOTSUPP);
601#ifndef COMPAT_43
602                return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
603                                                                 data,
604                                                                 ifp));
605#else
606            {
607                int ocmd = cmd;
608
609                switch (cmd) {
610
611                case SIOCSIFDSTADDR:
612                case SIOCSIFADDR:
613                case SIOCSIFBRDADDR:
614                case SIOCSIFNETMASK:
615#if BYTE_ORDER != BIG_ENDIAN
616                        if (ifr->ifr_addr.sa_family == 0 &&
617                            ifr->ifr_addr.sa_len < 16) {
618                                ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
619                                ifr->ifr_addr.sa_len = 16;
620                        }
621#else
622                        if (ifr->ifr_addr.sa_len == 0)
623                                ifr->ifr_addr.sa_len = 16;
624#endif
625                        break;
626
627                case OSIOCGIFADDR:
628                        cmd = SIOCGIFADDR;
629                        break;
630
631                case OSIOCGIFDSTADDR:
632                        cmd = SIOCGIFDSTADDR;
633                        break;
634
635                case OSIOCGIFBRDADDR:
636                        cmd = SIOCGIFBRDADDR;
637                        break;
638
639                case OSIOCGIFNETMASK:
640                        cmd = SIOCGIFNETMASK;
641                }
642                error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
643                                                                   cmd,
644                                                                   data,
645                                                                   ifp));
646                switch (ocmd) {
647
648                case OSIOCGIFADDR:
649                case OSIOCGIFDSTADDR:
650                case OSIOCGIFBRDADDR:
651                case OSIOCGIFNETMASK:
652                        *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
653                }
654                return (error);
655
656            }
657#endif
658
659        /*
660         * RTEMS additions for setting/getting `tap' function
661         */
662        case SIOCSIFTAP:
663                ifp->if_tap = ifr->ifr_tap;
664                return 0;
665
666        case SIOCGIFTAP:
667                ifr->ifr_tap = ifp->if_tap;
668                return 0;
669        }
670        return (0);
671}
672
673/*
674 * Set/clear promiscuous mode on interface ifp based on the truth value
675 * of pswitch.  The calls are reference counted so that only the first
676 * "on" request actually has an effect, as does the final "off" request.
677 * Results are undefined if the "off" and "on" requests are not matched.
678 */
679int
680ifpromisc(struct ifnet *ifp, int pswitch)
681{
682        struct ifreq ifr;
683
684        if (pswitch) {
685                /*
686                 * If the device is not configured up, we cannot put it in
687                 * promiscuous mode.
688                 */
689                if ((ifp->if_flags & IFF_UP) == 0)
690                        return (ENETDOWN);
691                if (ifp->if_pcount++ != 0)
692                        return (0);
693                ifp->if_flags |= IFF_PROMISC;
694                log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
695                    ifp->if_name, ifp->if_unit);
696        } else {
697                if (--ifp->if_pcount > 0)
698                        return (0);
699                ifp->if_flags &= ~IFF_PROMISC;
700        }
701        ifr.ifr_flags = ifp->if_flags;
702        return ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr));
703}
704
705/*
706 * Return interface configuration
707 * of system.  List may be used
708 * in later ioctl's (above) to get
709 * other information.
710 */
711/*ARGSUSED*/
712static int
713ifconf(u_long cmd, caddr_t data)
714{
715        struct ifconf *ifc = (struct ifconf *)data;
716        struct ifnet *ifp = ifnet;
717        struct ifaddr *ifa;
718        struct ifreq ifr, *ifrp;
719        int space = ifc->ifc_len, error = 0;
720
721        ifrp = ifc->ifc_req;
722        for (; space > sizeof (ifr) && ifp; ifp = ifp->if_next) {
723                char workbuf[64];
724                int ifnlen;
725
726                ifnlen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
727                if(ifnlen + 1 > sizeof ifr.ifr_name) {
728                        error = ENAMETOOLONG;
729                } else {
730                        strcpy(ifr.ifr_name, workbuf);
731                }
732
733                if ((ifa = ifp->if_addrlist) == 0) {
734                        bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
735                        error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
736                            sizeof (ifr));
737                        if (error)
738                                break;
739                        space -= sizeof (ifr), ifrp++;
740                } else
741                    for ( ; space > sizeof (ifr) && ifa; ifa = ifa->ifa_next) {
742                        struct sockaddr *sa = ifa->ifa_addr;
743#ifdef COMPAT_43
744                        if (cmd == OSIOCGIFCONF) {
745                                struct osockaddr *osa =
746                                         (struct osockaddr *)&ifr.ifr_addr;
747                                ifr.ifr_addr = *sa;
748                                osa->sa_family = sa->sa_family;
749                                error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
750                                                sizeof (ifr));
751                                ifrp++;
752                        } else
753#endif
754                        if (sa->sa_len <= sizeof(*sa)) {
755                                ifr.ifr_addr = *sa;
756                                error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
757                                                sizeof (ifr));
758                                ifrp++;
759                        } else {
760                                space -= sa->sa_len - sizeof(*sa);
761                                if (space < sizeof (ifr))
762                                        break;
763                                error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
764                                                sizeof (ifr.ifr_name));
765                                if (error == 0)
766                                    error = copyout((caddr_t)sa,
767                                      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
768                                ifrp = (struct ifreq *)
769                                        (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
770                        }
771                        if (error)
772                                break;
773                        space -= sizeof (ifr);
774                }
775        }
776        ifc->ifc_len -= space;
777        return (error);
778}
779
780SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
781SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
Note: See TracBrowser for help on using the repository browser.