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

4.104.114.84.95
Last change on this file since c75f2d1 was 751c513, checked in by Ralf Corsepius <ralf.corsepius@…>, on 12/03/04 at 05:07:56

2004-12-03 Ralf Corsepius <ralf.corsepius@…>

  • libnetworking/net/if_ethersubr.c, libnetworking/net/radix.h, libnetworking/netinet/if_ether.h, libnetworking/netinet/igmp_var.h, libnetworking/netinet/in_var.h, libnetworking/netinet/tcp_timer.h, libnetworking/netinet/udp_usrreq.c, libnetworking/rtems/rtems_glue.c: Misc. updates from FreeBSD and bugfixes tripped by GCC-4.0.0.
  • Property mode set to 100644
File size: 21.5 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 * The static buffer isn't a really huge problem since this code
516 * is protected by the RTEMS network mutex.
517 */
518char *
519ether_sprintf(const u_int8_t *addr)
520{
521        static char buf[32];
522        char *b = buf;
523        int i;
524
525        for (i = 0; i < ETHER_ADDR_LEN; i++, b+=3)
526                sprintf(b, "%02x:", *addr++);
527        *--b = '\0';
528        return buf;
529}
530
531/*
532 * Perform common duties while attaching to interface list
533 */
534void
535ether_ifattach(struct ifnet *ifp)
536{
537        struct ifaddr *ifa;
538        struct sockaddr_dl *sdl;
539
540        ifp->if_type = IFT_ETHER;
541        ifp->if_addrlen = ETHER_ADDR_LEN;
542        ifp->if_hdrlen = ETHER_HDR_LEN;
543        ifp->if_mtu = ETHERMTU;
544        if (ifp->if_baudrate == 0)
545            ifp->if_baudrate = 10000000;
546        for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
547                if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
548                    sdl->sdl_family == AF_LINK) {
549                        sdl->sdl_type = IFT_ETHER;
550                        sdl->sdl_alen = ifp->if_addrlen;
551                        bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
552                              LLADDR(sdl), ifp->if_addrlen);
553                        break;
554                }
555}
556
557#if defined(__rtems__)
558u_char ether_ipmulticast_min[6] =
559        { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
560u_char ether_ipmulticast_max[6] =
561        { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
562#else
563static u_char ether_ipmulticast_min[6] =
564        { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
565static u_char ether_ipmulticast_max[6] =
566        { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
567#endif
568
569/*
570 * Add an Ethernet multicast address or range of addresses to the list for a
571 * given interface.
572 */
573int
574ether_addmulti(ifr, ac)
575        struct ifreq *ifr;
576        register struct arpcom *ac;
577{
578        register struct ether_multi *enm;
579        struct sockaddr_in *sin;
580        u_char addrlo[6];
581        u_char addrhi[6];
582        int set_allmulti = 0;
583        int s = splimp();
584
585        switch (ifr->ifr_addr.sa_family) {
586
587        case AF_UNSPEC:
588                bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
589                bcopy(addrlo, addrhi, 6);
590                break;
591
592#ifdef INET
593        case AF_INET:
594                sin = (struct sockaddr_in *)&(ifr->ifr_addr);
595                if (sin->sin_addr.s_addr == INADDR_ANY) {
596                        /*
597                         * An IP address of INADDR_ANY means listen to all
598                         * of the Ethernet multicast addresses used for IP.
599                         * (This is for the sake of IP multicast routers.)
600                         */
601                        bcopy(ether_ipmulticast_min, addrlo, 6);
602                        bcopy(ether_ipmulticast_max, addrhi, 6);
603                      set_allmulti = 1;
604                }
605                else {
606                        ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
607                        bcopy(addrlo, addrhi, 6);
608                }
609                break;
610#endif
611
612        default:
613                splx(s);
614                return (EAFNOSUPPORT);
615        }
616
617        /*
618         * Verify that we have valid Ethernet multicast addresses.
619         */
620        if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
621                splx(s);
622                return (EINVAL);
623        }
624        /*
625         * See if the address range is already in the list.
626         */
627        ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
628        if (enm != NULL) {
629                /*
630                 * Found it; just increment the reference count.
631                 */
632                ++enm->enm_refcount;
633                splx(s);
634                return (0);
635        }
636        /*
637         * New address or range; malloc a new multicast record
638         * and link it into the interface's multicast list.
639         */
640        enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
641        if (enm == NULL) {
642                splx(s);
643                return (ENOBUFS);
644        }
645        bcopy(addrlo, enm->enm_addrlo, 6);
646        bcopy(addrhi, enm->enm_addrhi, 6);
647        enm->enm_ac = ac;
648        enm->enm_refcount = 1;
649        enm->enm_next = ac->ac_multiaddrs;
650        ac->ac_multiaddrs = enm;
651        ac->ac_multicnt++;
652        splx(s);
653        if (set_allmulti)
654                ac->ac_if.if_flags |= IFF_ALLMULTI;
655
656        /*
657         * Return ENETRESET to inform the driver that the list has changed
658         * and its reception filter should be adjusted accordingly.
659         */
660        return (ENETRESET);
661}
662
663/*
664 * Delete a multicast address record.
665 */
666int
667ether_delmulti(ifr, ac)
668        struct ifreq *ifr;
669        register struct arpcom *ac;
670{
671        register struct ether_multi *enm;
672        register struct ether_multi **p;
673        struct sockaddr_in *sin;
674        u_char addrlo[6];
675        u_char addrhi[6];
676      int unset_allmulti = 0;
677        int s = splimp();
678
679        switch (ifr->ifr_addr.sa_family) {
680
681        case AF_UNSPEC:
682                bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
683                bcopy(addrlo, addrhi, 6);
684                break;
685
686#ifdef INET
687        case AF_INET:
688                sin = (struct sockaddr_in *)&(ifr->ifr_addr);
689                if (sin->sin_addr.s_addr == INADDR_ANY) {
690                        /*
691                         * An IP address of INADDR_ANY means stop listening
692                         * to the range of Ethernet multicast addresses used
693                         * for IP.
694                         */
695                        bcopy(ether_ipmulticast_min, addrlo, 6);
696                        bcopy(ether_ipmulticast_max, addrhi, 6);
697                      unset_allmulti = 1;
698                }
699                else {
700                        ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
701                        bcopy(addrlo, addrhi, 6);
702                }
703                break;
704#endif
705
706        default:
707                splx(s);
708                return (EAFNOSUPPORT);
709        }
710
711        /*
712         * Look up the address in our list.
713         */
714        ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
715        if (enm == NULL) {
716                splx(s);
717                return (ENXIO);
718        }
719        if (--enm->enm_refcount != 0) {
720                /*
721                 * Still some claims to this record.
722                 */
723                splx(s);
724                return (0);
725        }
726        /*
727         * No remaining claims to this record; unlink and free it.
728         */
729        for (p = &enm->enm_ac->ac_multiaddrs;
730             *p != enm;
731             p = &(*p)->enm_next)
732                continue;
733        *p = (*p)->enm_next;
734        free(enm, M_IFMADDR);
735        ac->ac_multicnt--;
736        splx(s);
737      if (unset_allmulti)
738              ac->ac_if.if_flags &= ~IFF_ALLMULTI;
739
740        /*
741         * Return ENETRESET to inform the driver that the list has changed
742         * and its reception filter should be adjusted accordingly.
743         */
744        return (ENETRESET);
745}
746
747SYSCTL_DECL(_net_link);
748SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
749
750#if 0
751/*
752 * This is for reference.  We have a table-driven version
753 * of the little-endian crc32 generator, which is faster
754 * than the double-loop.
755 */
756uint32_t
757ether_crc32_le(const uint8_t *buf, size_t len)
758{
759        size_t i;
760        uint32_t crc;
761        int bit;
762        uint8_t data;
763
764        crc = 0xffffffff;       /* initial value */
765
766        for (i = 0; i < len; i++) {
767                for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
768                        carry = (crc ^ data) & 1;
769                        crc >>= 1;
770                        if (carry)
771                                crc = (crc ^ ETHER_CRC_POLY_LE);
772        }
773
774        return (crc);
775}
776#else
777uint32_t
778ether_crc32_le(const uint8_t *buf, size_t len)
779{
780        static const uint32_t crctab[] = {
781                0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
782                0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
783                0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
784                0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
785        };
786        size_t i;
787        uint32_t crc;
788
789        crc = 0xffffffff;       /* initial value */
790
791        for (i = 0; i < len; i++) {
792                crc ^= buf[i];
793                crc = (crc >> 4) ^ crctab[crc & 0xf];
794                crc = (crc >> 4) ^ crctab[crc & 0xf];
795        }
796
797        return (crc);
798}
799#endif
800
801uint32_t
802ether_crc32_be(const uint8_t *buf, size_t len)
803{
804        size_t i;
805        uint32_t crc, carry;
806        int bit;
807        uint8_t data;
808
809        crc = 0xffffffff;       /* initial value */
810
811        for (i = 0; i < len; i++) {
812                for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
813                        carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
814                        crc <<= 1;
815                        if (carry)
816                                crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
817                }
818        }
819
820        return (crc);
821}
822
823int
824ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
825{
826        struct ifaddr *ifa = (struct ifaddr *) data;
827        struct ifreq *ifr = (struct ifreq *) data;
828        int error = 0;
829
830        switch (command) {
831        case SIOCSIFADDR:
832                ifp->if_flags |= IFF_UP;
833
834                switch (ifa->ifa_addr->sa_family) {
835#ifdef INET
836                case AF_INET:
837                        ifp->if_init(ifp->if_softc);    /* before arpwhohas */
838                        arp_ifinit((struct arpcom *)ifp, ifa);
839                        break;
840#endif
841#ifdef IPX
842                /*
843                 * XXX - This code is probably wrong
844                 */
845                case AF_IPX:
846                        {
847                        register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
848                        struct arpcom *ac = (struct arpcom *) (ifp->if_softc);
849
850                        if (ipx_nullhost(*ina))
851                                ina->x_host =
852                                    *(union ipx_host *)
853                                    ac->ac_enaddr;
854                        else {
855                                bcopy((caddr_t) ina->x_host.c_host,
856                                      (caddr_t) ac->ac_enaddr,
857                                      sizeof(ac->ac_enaddr));
858                        }
859
860                        /*
861                         * Set new address
862                         */
863                        ifp->if_init(ifp->if_softc);
864                        break;
865                        }
866#endif
867                default:
868                        ifp->if_init(ifp->if_softc);
869                        break;
870                }
871                break;
872
873        case SIOCGIFADDR:
874                {
875                        struct sockaddr *sa;
876
877                        sa = (struct sockaddr *) & ifr->ifr_data;
878                        bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
879                              (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
880                }
881                break;
882
883        case SIOCSIFMTU:
884                /*
885                 * Set the interface MTU.
886                 */
887                if (ifr->ifr_mtu > ETHERMTU) {
888                        error = EINVAL;
889                } else {
890                        ifp->if_mtu = ifr->ifr_mtu;
891                }
892                break;
893        }
894        return (error);
895}
Note: See TracBrowser for help on using the repository browser.