source: rtems/cpukit/libnetworking/net/if.c @ 70148ee

4.10
Last change on this file since 70148ee was 70148ee, checked in by Till Straumann <strauman@…>, on 02/23/15 at 15:46:22

networking: alignment exception in ioctl(SIOCGIFCONF)

Access memory using a byte stream when copying to avoid unaligned
access. update #1401

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