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

4.104.115
Last change on this file since b25b88e7 was b25b88e7, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/28/10 at 05:50:29

Add HAVE_CONFIG_H support to let files receive configure defines.

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