source: rtems/cpukit/libnetworking/net/if_ethersubr.c @ c3814c5

4.104.114.84.95
Last change on this file since c3814c5 was c3814c5, checked in by Ralf Corsepius <ralf.corsepius@…>, on 10/22/04 at 10:12:02

2004-10-22 Ralf Corsepius <ralf_corsepius@…>

  • libnetworking/net/if_ethersubr.c: Update from FreeBSD.
  • Property mode set to 100644
File size: 21.3 KB
Line 
1/*
2 * Copyright (c) 1982, 1989, 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_ethersubr.c      8.1 (Berkeley) 6/10/93
30 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.179 2004/10/12 10:33:41 glebius Exp $
31 */
32 
33/*
34 * $Id$
35 */
36
37#include "opt_atalk.h"
38#include "opt_inet.h"
39#include "opt_inet6.h"
40#include "opt_ipx.h"
41#include "opt_bdg.h"
42#include "opt_mac.h"
43#include "opt_netgraph.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/malloc.h>
49#include <sys/mbuf.h>
50#include <sys/protosw.h>
51#include <sys/socket.h>
52#include <sys/ioctl.h>
53#include <sys/errno.h>
54#include <sys/syslog.h>
55#include <sys/sysctl.h>
56
57#include <net/if.h>
58#include <net/if_arp.h>
59#include <net/netisr.h>
60#include <net/route.h>
61#include <net/if_llc.h>
62#include <net/if_dl.h>
63#include <net/if_types.h>
64#include <net/ethernet.h>
65
66#if defined(INET) || defined(INET6)
67#include <netinet/in.h>
68#include <netinet/in_var.h>
69#include <netinet/if_ether.h>
70#include <netinet/ip_fw.h>
71#ifndef __rtems__
72#include <netinet/ip_dummynet.h>
73#endif
74#endif
75#ifdef INET6
76#include <netinet6/nd6.h>
77#endif
78
79#ifdef IPX
80#include <netipx/ipx.h>
81#include <netipx/ipx_if.h>
82#endif
83
84#ifdef NETATALK
85#include <netatalk/at.h>
86#include <netatalk/at_var.h>
87#include <netatalk/at_extern.h>
88
89#define llc_snap_org_code llc_un.type_snap.org_code
90#define llc_snap_ether_type llc_un.type_snap.ether_type
91
92extern u_char   at_org_code[3];
93extern u_char   aarp_org_code[3];
94#endif /* NETATALK */
95
96u_char  etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
97#define senderr(e) { error = (e); goto bad;}
98
99/*
100 * Ethernet output routine.
101 * Encapsulate a packet of type family for the local net.
102 * Use trailer local net encapsulation if enough data in first
103 * packet leaves a multiple of 512 bytes of data in remainder.
104 * Assumes that ifp is actually pointer to arpcom structure.
105 */
106int
107ether_output(struct ifnet *ifp, struct mbuf *m,
108        struct sockaddr *dst, struct rtentry *rt0)
109{
110        short type;
111        int s, error = 0;
112        u_char  edst[6];
113        register struct rtentry *rt;
114        struct mbuf *mcopy = (struct mbuf *)0;
115        register struct ether_header *eh;
116        int off, len = m->m_pkthdr.len;
117        struct arpcom *ac = (struct arpcom *)ifp;
118#ifdef NETATALK
119        struct at_ifaddr *aa;
120#endif /* NETATALK */
121        int hlen;       /* link layer header length */
122
123        if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
124                senderr(ENETDOWN);
125        rt = rt0;
126        if (rt) {
127                if ((rt->rt_flags & RTF_UP) == 0) {
128                        rt0 = rt = rtalloc1(dst, 1, 0UL);
129                        if (rt0)
130                                rt->rt_refcnt--;
131                        else
132                                senderr(EHOSTUNREACH);
133                }
134                if (rt->rt_flags & RTF_GATEWAY) {
135                        if (rt->rt_gwroute == 0)
136                                goto lookup;
137                        if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
138                                rtfree(rt); rt = rt0;
139                        lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
140                                                          0UL);
141                                if ((rt = rt->rt_gwroute) == 0)
142                                        senderr(EHOSTUNREACH);
143                        }
144                }
145                if (rt->rt_flags & RTF_REJECT)
146                        if (rt->rt_rmx.rmx_expire == 0 ||
147                            rtems_bsdnet_seconds_since_boot() < rt->rt_rmx.rmx_expire)
148                                senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
149        }
150        hlen = ETHER_HDR_LEN;
151        switch (dst->sa_family) {
152
153#ifdef INET
154        case AF_INET:
155                if (!arpresolve(ac, rt, m, dst, edst, rt0))
156                        return (0);     /* if not yet resolved */
157                /* If broadcasting on a simplex interface, loopback a copy */
158                if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
159                        mcopy = m_copy(m, 0, (int)M_COPYALL);
160                off = m->m_pkthdr.len - m->m_len;
161                type = htons(ETHERTYPE_IP);
162                break;
163#endif
164#ifdef IPX
165        case AF_IPX:
166                {
167                struct ifaddr *ia;
168
169                type = htons(ETHERTYPE_IPX);
170                bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
171                    (caddr_t)edst, sizeof (edst));
172                for (ia = ifp->if_addrlist; ia != NULL; ia = ia->ifa_next)
173                        if(ia->ifa_addr->sa_family == AF_IPX &&
174                           !bcmp((caddr_t)edst,
175                                 (caddr_t)&((struct ipx_ifaddr *)ia)->ia_addr.sipx_addr.x_host,
176                                 sizeof(edst)))
177                                return (looutput(ifp, m, dst, rt));
178                /* If broadcasting on a simplex interface, loopback a copy */
179                if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
180                        mcopy = m_copy(m, 0, (int)M_COPYALL);
181                break;
182                }
183#endif
184#ifdef NETATALK
185        case AF_APPLETALK:
186            {
187                struct sockaddr_at *sat = (struct sockaddr_at *)dst;
188
189                /*
190                 * super hack..
191                 * Most of this loopback code should move into the appletalk
192                 * code, but it's here for now.. remember to move it! [JRE]
193                 * This may not get the same interface we started with
194                 * fix asap. XXX
195                 */
196                aa = at_ifawithnet( sat );
197                if (aa == NULL) {
198                        goto bad;
199                }
200                if( aa->aa_ifa.ifa_ifp != ifp ) {
201                        (*aa->aa_ifa.ifa_ifp->if_output)(aa->aa_ifa.ifa_ifp,
202                                                        m,dst,rt);
203                }
204                if (((sat->sat_addr.s_net == ATADDR_ANYNET)
205                  && (sat->sat_addr.s_node == ATADDR_ANYNODE))
206                || ((sat->sat_addr.s_net == aa->aa_addr.sat_addr.s_net )
207                  && (sat->sat_addr.s_node == aa->aa_addr.sat_addr.s_node))) {
208                        (void) looutput(ifp, m, dst, rt);
209                        return(0);
210                }
211
212                if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
213#ifdef NETATALKDEBUG
214                        extern char *prsockaddr(struct sockaddr *);
215                        printf("aarpresolv: failed for %s\n", prsockaddr(dst));
216#endif /* NETATALKDEBUG */
217                        return (0);
218                }
219
220                /*
221                 * If broadcasting on a simplex interface, loopback a copy
222                 */
223                if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
224                        mcopy = m_copy(m, 0, (int)M_COPYALL);
225            }
226            /*
227             * In the phase 2 case, we need to prepend an mbuf for the llc header.
228             * Since we must preserve the value of m, which is passed to us by
229             * value, we m_copy() the first mbuf, and use it for our llc header.
230             */
231            if ( aa->aa_flags & AFA_PHASE2 ) {
232                struct llc llc;
233
234                M_PREPEND(m, sizeof(struct llc), M_WAIT);
235                len += sizeof(struct llc);
236                llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
237                llc.llc_control = LLC_UI;
238                bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
239                llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
240                bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
241                type = htons(m->m_pkthdr.len);
242            } else {
243                type = htons(ETHERTYPE_AT);
244            }
245            break;
246#endif /* NETATALK */
247
248        case AF_UNSPEC:
249                eh = (struct ether_header *)dst->sa_data;
250                (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
251                type = eh->ether_type;
252                break;
253
254        default:
255                printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
256                        dst->sa_family);
257                senderr(EAFNOSUPPORT);
258        }
259
260
261        if (mcopy)
262                (void) looutput(ifp, mcopy, dst, rt);
263        /*
264         * Add local net header.  If no space in first mbuf,
265         * allocate another.
266         */
267        M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
268        if (m == NULL)
269                senderr(ENOBUFS);
270        eh = mtod(m, struct ether_header *);
271        (void)memcpy(&eh->ether_type, &type,
272                sizeof(eh->ether_type));
273        (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
274        (void)memcpy(eh->ether_shost, ac->ac_enaddr,
275            sizeof(eh->ether_shost));
276        s = splimp();
277        /*
278         * Queue message on interface, and start output if interface
279         * not yet active.
280         */
281        if (IF_QFULL(&ifp->if_snd)) {
282                IF_DROP(&ifp->if_snd);
283                splx(s);
284                senderr(ENOBUFS);
285        }
286        IF_ENQUEUE(&ifp->if_snd, m);
287        if ((ifp->if_flags & IFF_OACTIVE) == 0)
288                (*ifp->if_start)(ifp);
289        splx(s);
290        ifp->if_obytes += len + sizeof (struct ether_header);
291        if (m->m_flags & M_MCAST)
292                ifp->if_omcasts++;
293        return (error);
294
295bad:
296        if (m)
297                m_freem(m);
298        return (error);
299}
300
301/*
302 * Process a received Ethernet packet;
303 * the packet is in the mbuf chain m without
304 * the ether header, which is provided separately.
305 */
306void
307ether_input(ifp, eh, m)
308        struct ifnet *ifp;
309        register struct ether_header *eh;
310        struct mbuf *m;
311{
312        register struct ifqueue *inq;
313        u_short ether_type;
314        int s;
315#if defined(NETATALK)
316        struct llc *l;
317#endif
318
319        if ((ifp->if_flags & IFF_UP) == 0) {
320                m_freem(m);
321                return;
322        }
323        ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
324        if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
325            sizeof(etherbroadcastaddr)) == 0)
326                m->m_flags |= M_BCAST;
327        else if (eh->ether_dhost[0] & 1)
328                m->m_flags |= M_MCAST;
329        if (m->m_flags & (M_BCAST|M_MCAST))
330                ifp->if_imcasts++;
331
332        /*
333         * RTEMS addition -- allow application to `tap into'
334         * the incoming packet stream.
335         */
336        if (ifp->if_tap && (*ifp->if_tap)(ifp, eh, m)) {
337                m_freem(m);
338                return;
339        }
340
341        ether_type = ntohs(eh->ether_type);
342
343        switch (ether_type) {
344#ifdef INET
345        case ETHERTYPE_IP:
346                schednetisr(NETISR_IP);
347                inq = &ipintrq;
348                break;
349
350        case ETHERTYPE_ARP:
351                schednetisr(NETISR_ARP);
352                inq = &arpintrq;
353                break;
354#endif
355#ifdef IPX
356        case ETHERTYPE_IPX:
357                schednetisr(NETISR_IPX);
358                inq = &ipxintrq;
359                break;
360#endif
361#ifdef NETATALK
362        case ETHERTYPE_AT:
363                schednetisr(NETISR_ATALK);
364                inq = &atintrq1;
365                break;
366        case ETHERTYPE_AARP:
367                /* probably this should be done with a NETISR as well */
368                aarpinput((struct arpcom *)ifp, m); /* XXX */
369                return;
370#endif /* NETATALK */
371        default:
372#if defined (ISO) || defined (LLC) || defined(NETATALK)
373                if (ether_type > ETHERMTU)
374                        goto dropanyway;
375                l = mtod(m, struct llc *);
376                switch (l->llc_dsap) {
377#ifdef NETATALK
378                case LLC_SNAP_LSAP:
379                    switch (l->llc_control) {
380                    case LLC_UI:
381                        if (l->llc_ssap != LLC_SNAP_LSAP)
382                            goto dropanyway;
383       
384                        if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
385                                   sizeof(at_org_code)) == 0 &&
386                             ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
387                            inq = &atintrq2;
388                            m_adj( m, sizeof( struct llc ));
389                            schednetisr(NETISR_ATALK);
390                            break;
391                        }
392
393                        if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
394                                   sizeof(aarp_org_code)) == 0 &&
395                             ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
396                            m_adj( m, sizeof( struct llc ));
397                            aarpinput((struct arpcom *)ifp, m); /* XXX */
398                            return;
399                        }
400               
401                    default:
402                        goto dropanyway;
403                    }
404                    break;
405#endif /* NETATALK */
406#ifdef  ISO
407                case LLC_ISO_LSAP:
408                        switch (l->llc_control) {
409                        case LLC_UI:
410                                /* LLC_UI_P forbidden in class 1 service */
411                                if ((l->llc_dsap == LLC_ISO_LSAP) &&
412                                    (l->llc_ssap == LLC_ISO_LSAP)) {
413                                        /* LSAP for ISO */
414                                        if (m->m_pkthdr.len > ether_type)
415                                                m_adj(m, ether_type - m->m_pkthdr.len);
416                                        m->m_data += 3;         /* XXX */
417                                        m->m_len -= 3;          /* XXX */
418                                        m->m_pkthdr.len -= 3;   /* XXX */
419                                        M_PREPEND(m, sizeof *eh, M_DONTWAIT);
420                                        if (m == 0)
421                                                return;
422                                        *mtod(m, struct ether_header *) = *eh;
423                                        IFDEBUG(D_ETHER)
424                                                printf("clnp packet");
425                                        ENDDEBUG
426                                        schednetisr(NETISR_ISO);
427                                        inq = &clnlintrq;
428                                        break;
429                                }
430                                goto dropanyway;
431
432                        case LLC_XID:
433                        case LLC_XID_P:
434                                if(m->m_len < 6)
435                                        goto dropanyway;
436                                l->llc_window = 0;
437                                l->llc_fid = 9;
438                                l->llc_class = 1;
439                                l->llc_dsap = l->llc_ssap = 0;
440                                /* Fall through to */
441                        case LLC_TEST:
442                        case LLC_TEST_P:
443                        {
444                                struct sockaddr sa;
445                                register struct ether_header *eh2;
446                                int i;
447                                u_char c = l->llc_dsap;
448
449                                l->llc_dsap = l->llc_ssap;
450                                l->llc_ssap = c;
451                                if (m->m_flags & (M_BCAST | M_MCAST))
452                                        bcopy((caddr_t)ac->ac_enaddr,
453                                              (caddr_t)eh->ether_dhost, 6);
454                                sa.sa_family = AF_UNSPEC;
455                                sa.sa_len = sizeof(sa);
456                                eh2 = (struct ether_header *)sa.sa_data;
457                                for (i = 0; i < 6; i++) {
458                                        eh2->ether_shost[i] = c = eh->ether_dhost[i];
459                                        eh2->ether_dhost[i] =
460                                                eh->ether_dhost[i] = eh->ether_shost[i];
461                                        eh->ether_shost[i] = c;
462                                }
463                                ifp->if_output(ifp, m, &sa, NULL);
464                                return;
465                        }
466                        default:
467                                m_freem(m);
468                                return;
469                        }
470                        break;
471#endif /* ISO */
472#ifdef LLC
473                case LLC_X25_LSAP:
474                {
475                        if (m->m_pkthdr.len > ether_type)
476                                m_adj(m, ether_type - m->m_pkthdr.len);
477                        M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
478                        if (m == 0)
479                                return;
480                        if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
481                                            eh->ether_dhost, LLC_X25_LSAP, 6,
482                                            mtod(m, struct sdl_hdr *)))
483                                panic("ETHER cons addr failure");
484                        mtod(m, struct sdl_hdr *)->sdlhdr_len = ether_type;
485#ifdef LLC_DEBUG
486                                printf("llc packet\n");
487#endif /* LLC_DEBUG */
488                        schednetisr(NETISR_CCITT);
489                        inq = &llcintrq;
490                        break;
491                }
492#endif /* LLC */
493                dropanyway:
494                default:
495                        m_freem(m);
496                        return;
497                }
498#else /* ISO || LLC || NETATALK */
499            m_freem(m);
500            return;
501#endif /* ISO || LLC || NETATALK */
502        }
503
504        s = splimp();
505        if (IF_QFULL(inq)) {
506                IF_DROP(inq);
507                m_freem(m);
508        } else
509                IF_ENQUEUE(inq, m);
510        splx(s);
511}
512
513/*
514 * Convert Ethernet address to printable (loggable) representation.
515 * This routine is for compatibility; it's better to just use
516 *
517 *      printf("%6D", <pointer to address>, ":");
518 *
519 * since there's no static buffer involved.
520 */
521char *
522ether_sprintf(const u_char *ap)
523{
524        static char etherbuf[18];
525        snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
526        return (etherbuf);
527}
528
529/*
530 * Perform common duties while attaching to interface list
531 */
532void
533ether_ifattach(struct ifnet *ifp)
534{
535        struct ifaddr *ifa;
536        struct sockaddr_dl *sdl;
537
538        ifp->if_type = IFT_ETHER;
539        ifp->if_addrlen = ETHER_ADDR_LEN;
540        ifp->if_hdrlen = ETHER_HDR_LEN;
541        ifp->if_mtu = ETHERMTU;
542        if (ifp->if_baudrate == 0)
543            ifp->if_baudrate = 10000000;
544        for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
545                if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
546                    sdl->sdl_family == AF_LINK) {
547                        sdl->sdl_type = IFT_ETHER;
548                        sdl->sdl_alen = ifp->if_addrlen;
549                        bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
550                              LLADDR(sdl), ifp->if_addrlen);
551                        break;
552                }
553}
554
555static u_char ether_ipmulticast_min[6] =
556        { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
557static u_char ether_ipmulticast_max[6] =
558        { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
559/*
560 * Add an Ethernet multicast address or range of addresses to the list for a
561 * given interface.
562 */
563int
564ether_addmulti(ifr, ac)
565        struct ifreq *ifr;
566        register struct arpcom *ac;
567{
568        register struct ether_multi *enm;
569        struct sockaddr_in *sin;
570        u_char addrlo[6];
571        u_char addrhi[6];
572        int set_allmulti = 0;
573        int s = splimp();
574
575        switch (ifr->ifr_addr.sa_family) {
576
577        case AF_UNSPEC:
578                bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
579                bcopy(addrlo, addrhi, 6);
580                break;
581
582#ifdef INET
583        case AF_INET:
584                sin = (struct sockaddr_in *)&(ifr->ifr_addr);
585                if (sin->sin_addr.s_addr == INADDR_ANY) {
586                        /*
587                         * An IP address of INADDR_ANY means listen to all
588                         * of the Ethernet multicast addresses used for IP.
589                         * (This is for the sake of IP multicast routers.)
590                         */
591                        bcopy(ether_ipmulticast_min, addrlo, 6);
592                        bcopy(ether_ipmulticast_max, addrhi, 6);
593                      set_allmulti = 1;
594                }
595                else {
596                        ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
597                        bcopy(addrlo, addrhi, 6);
598                }
599                break;
600#endif
601
602        default:
603                splx(s);
604                return (EAFNOSUPPORT);
605        }
606
607        /*
608         * Verify that we have valid Ethernet multicast addresses.
609         */
610        if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
611                splx(s);
612                return (EINVAL);
613        }
614        /*
615         * See if the address range is already in the list.
616         */
617        ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
618        if (enm != NULL) {
619                /*
620                 * Found it; just increment the reference count.
621                 */
622                ++enm->enm_refcount;
623                splx(s);
624                return (0);
625        }
626        /*
627         * New address or range; malloc a new multicast record
628         * and link it into the interface's multicast list.
629         */
630        enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
631        if (enm == NULL) {
632                splx(s);
633                return (ENOBUFS);
634        }
635        bcopy(addrlo, enm->enm_addrlo, 6);
636        bcopy(addrhi, enm->enm_addrhi, 6);
637        enm->enm_ac = ac;
638        enm->enm_refcount = 1;
639        enm->enm_next = ac->ac_multiaddrs;
640        ac->ac_multiaddrs = enm;
641        ac->ac_multicnt++;
642        splx(s);
643        if (set_allmulti)
644                ac->ac_if.if_flags |= IFF_ALLMULTI;
645
646        /*
647         * Return ENETRESET to inform the driver that the list has changed
648         * and its reception filter should be adjusted accordingly.
649         */
650        return (ENETRESET);
651}
652
653/*
654 * Delete a multicast address record.
655 */
656int
657ether_delmulti(ifr, ac)
658        struct ifreq *ifr;
659        register struct arpcom *ac;
660{
661        register struct ether_multi *enm;
662        register struct ether_multi **p;
663        struct sockaddr_in *sin;
664        u_char addrlo[6];
665        u_char addrhi[6];
666      int unset_allmulti = 0;
667        int s = splimp();
668
669        switch (ifr->ifr_addr.sa_family) {
670
671        case AF_UNSPEC:
672                bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
673                bcopy(addrlo, addrhi, 6);
674                break;
675
676#ifdef INET
677        case AF_INET:
678                sin = (struct sockaddr_in *)&(ifr->ifr_addr);
679                if (sin->sin_addr.s_addr == INADDR_ANY) {
680                        /*
681                         * An IP address of INADDR_ANY means stop listening
682                         * to the range of Ethernet multicast addresses used
683                         * for IP.
684                         */
685                        bcopy(ether_ipmulticast_min, addrlo, 6);
686                        bcopy(ether_ipmulticast_max, addrhi, 6);
687                      unset_allmulti = 1;
688                }
689                else {
690                        ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
691                        bcopy(addrlo, addrhi, 6);
692                }
693                break;
694#endif
695
696        default:
697                splx(s);
698                return (EAFNOSUPPORT);
699        }
700
701        /*
702         * Look up the address in our list.
703         */
704        ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
705        if (enm == NULL) {
706                splx(s);
707                return (ENXIO);
708        }
709        if (--enm->enm_refcount != 0) {
710                /*
711                 * Still some claims to this record.
712                 */
713                splx(s);
714                return (0);
715        }
716        /*
717         * No remaining claims to this record; unlink and free it.
718         */
719        for (p = &enm->enm_ac->ac_multiaddrs;
720             *p != enm;
721             p = &(*p)->enm_next)
722                continue;
723        *p = (*p)->enm_next;
724        free(enm, M_IFMADDR);
725        ac->ac_multicnt--;
726        splx(s);
727      if (unset_allmulti)
728              ac->ac_if.if_flags &= ~IFF_ALLMULTI;
729
730        /*
731         * Return ENETRESET to inform the driver that the list has changed
732         * and its reception filter should be adjusted accordingly.
733         */
734        return (ENETRESET);
735}
736
737SYSCTL_DECL(_net_link);
738SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
739
740#if 0
741/*
742 * This is for reference.  We have a table-driven version
743 * of the little-endian crc32 generator, which is faster
744 * than the double-loop.
745 */
746uint32_t
747ether_crc32_le(const uint8_t *buf, size_t len)
748{
749        size_t i;
750        uint32_t crc;
751        int bit;
752        uint8_t data;
753
754        crc = 0xffffffff;       /* initial value */
755
756        for (i = 0; i < len; i++) {
757                for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
758                        carry = (crc ^ data) & 1;
759                        crc >>= 1;
760                        if (carry)
761                                crc = (crc ^ ETHER_CRC_POLY_LE);
762        }
763
764        return (crc);
765}
766#else
767uint32_t
768ether_crc32_le(const uint8_t *buf, size_t len)
769{
770        static const uint32_t crctab[] = {
771                0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
772                0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
773                0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
774                0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
775        };
776        size_t i;
777        uint32_t crc;
778
779        crc = 0xffffffff;       /* initial value */
780
781        for (i = 0; i < len; i++) {
782                crc ^= buf[i];
783                crc = (crc >> 4) ^ crctab[crc & 0xf];
784                crc = (crc >> 4) ^ crctab[crc & 0xf];
785        }
786
787        return (crc);
788}
789#endif
790
791uint32_t
792ether_crc32_be(const uint8_t *buf, size_t len)
793{
794        size_t i;
795        uint32_t crc, carry;
796        int bit;
797        uint8_t data;
798
799        crc = 0xffffffff;       /* initial value */
800
801        for (i = 0; i < len; i++) {
802                for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
803                        carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
804                        crc <<= 1;
805                        if (carry)
806                                crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
807                }
808        }
809
810        return (crc);
811}
812
813int
814ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
815{
816        struct ifaddr *ifa = (struct ifaddr *) data;
817        struct ifreq *ifr = (struct ifreq *) data;
818        int error = 0;
819
820        switch (command) {
821        case SIOCSIFADDR:
822                ifp->if_flags |= IFF_UP;
823
824                switch (ifa->ifa_addr->sa_family) {
825#ifdef INET
826                case AF_INET:
827                        ifp->if_init(ifp->if_softc);    /* before arpwhohas */
828                        arp_ifinit((struct arpcom *)ifp, ifa);
829                        break;
830#endif
831#ifdef IPX
832                /*
833                 * XXX - This code is probably wrong
834                 */
835                case AF_IPX:
836                        {
837                        register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
838                        struct arpcom *ac = (struct arpcom *) (ifp->if_softc);
839
840                        if (ipx_nullhost(*ina))
841                                ina->x_host =
842                                    *(union ipx_host *)
843                                    ac->ac_enaddr;
844                        else {
845                                bcopy((caddr_t) ina->x_host.c_host,
846                                      (caddr_t) ac->ac_enaddr,
847                                      sizeof(ac->ac_enaddr));
848                        }
849
850                        /*
851                         * Set new address
852                         */
853                        ifp->if_init(ifp->if_softc);
854                        break;
855                        }
856#endif
857                default:
858                        ifp->if_init(ifp->if_softc);
859                        break;
860                }
861                break;
862
863        case SIOCGIFADDR:
864                {
865                        struct sockaddr *sa;
866
867                        sa = (struct sockaddr *) & ifr->ifr_data;
868                        bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
869                              (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
870                }
871                break;
872
873        case SIOCSIFMTU:
874                /*
875                 * Set the interface MTU.
876                 */
877                if (ifr->ifr_mtu > ETHERMTU) {
878                        error = EINVAL;
879                } else {
880                        ifp->if_mtu = ifr->ifr_mtu;
881                }
882                break;
883        }
884        return (error);
885}
Note: See TracBrowser for help on using the repository browser.