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

4.104.115
Last change on this file since b3f8c9e1 was b3f8c9e1, checked in by Ralf Corsepius <ralf.corsepius@…>, on 12/22/08 at 07:47:28

Include <errno.h> (POSIX,C99) instead of <sys/errno.h> (BSD'ism).

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