Merge from vendor branch ZLIB:
[dragonfly.git] / sys / net / if_ethersubr.c
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)if_ethersubr.c      8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
35  * $DragonFly: src/sys/net/if_ethersubr.c,v 1.32 2005/06/15 19:29:30 joerg Exp $
36  */
37
38 #include "opt_atalk.h"
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41 #include "opt_ipx.h"
42 #include "opt_bdg.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/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
53
54 #include <net/if.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/if_llc.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/ifq_var.h>
61 #include <net/bpf.h>
62 #include <net/ethernet.h>
63 #include <net/bridge/bridge.h>
64
65 #if defined(INET) || defined(INET6)
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/if_ether.h>
69 #include <net/ipfw/ip_fw.h>
70 #include <net/dummynet/ip_dummynet.h>
71 #endif
72 #ifdef INET6
73 #include <netinet6/nd6.h>
74 #endif
75
76 #ifdef IPX
77 #include <netproto/ipx/ipx.h>
78 #include <netproto/ipx/ipx_if.h>
79 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
80 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
81                   short *tp, int *hlen);
82 #endif
83
84 #ifdef NS
85 #include <netns/ns.h>
86 #include <netns/ns_if.h>
87 ushort ns_nettype;
88 int ether_outputdebug = 0;
89 int ether_inputdebug = 0;
90 #endif
91
92 #ifdef NETATALK
93 #include <netproto/atalk/at.h>
94 #include <netproto/atalk/at_var.h>
95 #include <netproto/atalk/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
100 extern u_char   at_org_code[3];
101 extern u_char   aarp_org_code[3];
102 #endif /* NETATALK */
103
104 /* netgraph node hooks for ng_ether(4) */
105 void    (*ng_ether_input_p)(struct ifnet *ifp,
106                 struct mbuf **mp, struct ether_header *eh);
107 void    (*ng_ether_input_orphan_p)(struct ifnet *ifp,
108                 struct mbuf *m, struct ether_header *eh);
109 int     (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
110 void    (*ng_ether_attach_p)(struct ifnet *ifp);
111 void    (*ng_ether_detach_p)(struct ifnet *ifp);
112
113 int     (*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
114 int     (*vlan_input_tag_p)(struct mbuf *m, uint16_t t);
115
116 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
117                         struct rtentry *);
118
119 /*
120  * bridge support
121  */
122 int do_bridge;
123 bridge_in_t *bridge_in_ptr;
124 bdg_forward_t *bdg_forward_ptr;
125 bdgtakeifaces_t *bdgtakeifaces_ptr;
126 struct bdg_softc *ifp2sc;
127
128 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
129                               struct sockaddr *);
130
131 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
132         0xff, 0xff, 0xff, 0xff, 0xff, 0xff
133 };
134
135 #define gotoerr(e) do { error = (e); goto bad; } while (0)
136 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
137
138 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
139                                 struct ip_fw **rule, struct ether_header *eh,
140                                 boolean_t shared);
141
142 static int ether_ipfw;
143 SYSCTL_DECL(_net_link);
144 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
145 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
146            &ether_ipfw, 0, "Pass ether pkts through firewall");
147
148 /*
149  * Ethernet output routine.
150  * Encapsulate a packet of type family for the local net.
151  * Use trailer local net encapsulation if enough data in first
152  * packet leaves a multiple of 512 bytes of data in remainder.
153  * Assumes that ifp is actually pointer to arpcom structure.
154  */
155 static int
156 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
157              struct rtentry *rt)
158 {
159         struct ether_header *eh, *deh;
160         u_char *edst;
161         int loop_copy = 0;
162         int hlen = ETHER_HDR_LEN;       /* link layer header length */
163         struct arpcom *ac = IFP2AC(ifp);
164         int error;
165
166         if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
167                 gotoerr(ENETDOWN);
168
169         M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
170         if (m == NULL)
171                 return (ENOBUFS);
172         eh = mtod(m, struct ether_header *);
173         edst = eh->ether_dhost;
174
175         /*
176          * Fill in the destination ethernet address and frame type.
177          */
178         switch (dst->sa_family) {
179 #ifdef INET
180         case AF_INET:
181                 if (!arpresolve(ifp, rt, m, dst, edst))
182                         return (0);     /* if not yet resolved */
183                 eh->ether_type = htons(ETHERTYPE_IP);
184                 break;
185 #endif
186 #ifdef INET6
187         case AF_INET6:
188                 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
189                         return (0);             /* Something bad happenned. */
190                 eh->ether_type = htons(ETHERTYPE_IPV6);
191                 break;
192 #endif
193 #ifdef IPX
194         case AF_IPX:
195                 if (ef_outputp != NULL) {
196                         error = ef_outputp(ifp, &m, dst, &eh->ether_type,
197                                            &hlen);
198                         if (error)
199                                 goto bad;
200                 } else {
201                         eh->ether_type = htons(ETHERTYPE_IPX);
202                         bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
203                               edst, ETHER_ADDR_LEN);
204                 }
205                 break;
206 #endif
207 #ifdef NETATALK
208         case AF_APPLETALK: {
209                 struct at_ifaddr *aa;
210
211                 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
212                         error = 0;      /* XXX */
213                         goto bad;
214                 }
215                 /*
216                  * In the phase 2 case, need to prepend an mbuf for
217                  * the llc header.  Since we must preserve the value
218                  * of m, which is passed to us by value, we m_copy()
219                  * the first mbuf, and use it for our llc header.
220                  */
221                 if (aa->aa_flags & AFA_PHASE2) {
222                         struct llc llc;
223
224                         M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
225                         eh = mtod(m, struct ether_header *);
226                         edst = eh->ether_dhost;
227                         llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
228                         llc.llc_control = LLC_UI;
229                         bcopy(at_org_code, llc.llc_snap_org_code,
230                               sizeof at_org_code);
231                         llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
232                         bcopy(&llc,
233                               mtod(m, caddr_t) + sizeof(struct ether_header),
234                               sizeof(struct llc));
235                         eh->ether_type = htons(m->m_pkthdr.len);
236                         hlen = sizeof(struct llc) + ETHER_HDR_LEN;
237                 } else {
238                         eh->ether_type = htons(ETHERTYPE_AT);
239                 }
240                 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
241                         return (0);
242                 break;
243           }
244 #endif
245 #ifdef NS
246         case AF_NS:
247                 switch(ns_nettype) {
248                 default:
249                 case 0x8137:    /* Novell Ethernet_II Ethernet TYPE II */
250                         eh->ether_type = 0x8137;
251                         break;
252                 case 0x0:       /* Novell 802.3 */
253                         eh->ether_type = htons(m->m_pkthdr.len);
254                         break;
255                 case 0xe0e0:    /* Novell 802.2 and Token-Ring */
256                         M_PREPEND(m, 3, MB_DONTWAIT);
257                         eh = mtod(m, struct ether_header *);
258                         edst = eh->ether_dhost;
259                         eh->ether_type = htons(m->m_pkthdr.len);
260                         cp = mtod(m, u_char *) + sizeof(struct ether_header);
261                         *cp++ = 0xE0;
262                         *cp++ = 0xE0;
263                         *cp++ = 0x03;
264                         break;
265                 }
266                 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
267                       ETHER_ADDR_LEN);
268                 /*
269                  * XXX if ns_thishost is the same as the node's ethernet
270                  * address then just the default code will catch this anyhow.
271                  * So I'm not sure if this next clause should be here at all?
272                  * [JRE]
273                  */
274                 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
275                         m->m_pkthdr.rcvif = ifp;
276                         netisr_dispatch(NETISR_NS, m);
277                         return (error);
278                 }
279                 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
280                         m->m_flags |= M_BCAST;
281                 break;
282 #endif
283         case pseudo_AF_HDRCMPLT:
284         case AF_UNSPEC:
285                 loop_copy = -1; /* if this is for us, don't do it */
286                 deh = (struct ether_header *)dst->sa_data;
287                 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
288                 eh->ether_type = deh->ether_type;
289                 break;
290
291         default:
292                 printf("%s: can't handle af%d\n", ifp->if_xname,
293                         dst->sa_family);
294                 gotoerr(EAFNOSUPPORT);
295         }
296
297         if (dst->sa_family == pseudo_AF_HDRCMPLT)       /* unlikely */
298                 memcpy(eh->ether_shost,
299                        ((struct ether_header *)dst->sa_data)->ether_shost,
300                        ETHER_ADDR_LEN);
301         else
302                 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
303
304         /*
305          * If a simplex interface, and the packet is being sent to our
306          * Ethernet address or a broadcast address, loopback a copy.
307          * XXX To make a simplex device behave exactly like a duplex
308          * device, we should copy in the case of sending to our own
309          * ethernet address (thus letting the original actually appear
310          * on the wire). However, we don't do that here for security
311          * reasons and compatibility with the original behavior.
312          */
313         if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
314                 int csum_flags = 0;
315
316                 if (m->m_pkthdr.csum_flags & CSUM_IP)
317                         csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
318                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
319                         csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
320                 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
321                         struct mbuf *n;
322
323                         if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
324                                 n->m_pkthdr.csum_flags |= csum_flags;
325                                 if (csum_flags & CSUM_DATA_VALID)
326                                         n->m_pkthdr.csum_data = 0xffff;
327                                 if_simloop(ifp, n, dst->sa_family, hlen);
328                         } else
329                                 ifp->if_iqdrops++;
330                 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
331                                 ETHER_ADDR_LEN) == 0) {
332                         m->m_pkthdr.csum_flags |= csum_flags;
333                         if (csum_flags & CSUM_DATA_VALID)
334                                 m->m_pkthdr.csum_data = 0xffff;
335                         if_simloop(ifp, m, dst->sa_family, hlen);
336                         return (0);     /* XXX */
337                 }
338         }
339
340         /* Handle ng_ether(4) processing, if any */
341         if (ng_ether_output_p != NULL) {
342                 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
343                         goto bad;
344                 if (m == NULL)
345                         return (0);
346         }
347
348         /* Continue with link-layer output */
349         return ether_output_frame(ifp, m);
350
351 bad:
352         m_freem(m);
353         return (error);
354 }
355
356 /*
357  * Ethernet link layer output routine to send a raw frame to the device.
358  *
359  * This assumes that the 14 byte Ethernet header is present and contiguous
360  * in the first mbuf (if BRIDGE'ing).
361  */
362 int
363 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
364 {
365         struct ip_fw *rule = NULL;
366         int error = 0;
367         struct altq_pktattr pktattr;
368
369         /* Extract info from dummynet tag, ignore others */
370         while (m->m_type == MT_TAG) {
371                 if (m->m_flags == PACKET_TAG_DUMMYNET) {
372                         rule = ((struct dn_pkt *)m)->rule;
373                         break;
374                 }
375                 m = m->m_next;
376         }
377         if (rule != NULL)               /* packet was already bridged */
378                 goto no_bridge;
379
380         if (BDG_ACTIVE(ifp)) {
381                 struct ether_header *eh;        /* a pointer suffices */
382
383                 m->m_pkthdr.rcvif = NULL;
384                 eh = mtod(m, struct ether_header *);
385                 m_adj(m, ETHER_HDR_LEN);
386                 m = bdg_forward_ptr(m, eh, ifp);
387                 m_freem(m);
388                 return (0);
389         }
390
391 no_bridge:
392         if (ifq_is_enabled(&ifp->if_snd))
393                 altq_etherclassify(&ifp->if_snd, m, &pktattr);
394         crit_enter();
395         if (IPFW_LOADED && ether_ipfw != 0) {
396                 struct ether_header save_eh, *eh;
397
398                 eh = mtod(m, struct ether_header *);
399                 save_eh = *eh;
400                 m_adj(m, ETHER_HDR_LEN);
401                 if (!ether_ipfw_chk(&m, ifp, &rule, eh, FALSE)) {
402                         crit_exit();
403                         if (m != NULL) {
404                                 m_freem(m);
405                                 return ENOBUFS; /* pkt dropped */
406                         } else
407                                 return 0;       /* consumed e.g. in a pipe */
408                 }
409                 eh = mtod(m, struct ether_header *);
410                 /* packet was ok, restore the ethernet header */
411                 if ((void *)(eh + 1) == (void *)m->m_data) {
412                         m->m_data -= ETHER_HDR_LEN ;
413                         m->m_len += ETHER_HDR_LEN ;
414                         m->m_pkthdr.len += ETHER_HDR_LEN ;
415                 } else {
416                         M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
417                         if (m == NULL) /* nope... */ {
418                                 crit_exit();
419                                 return ENOBUFS;
420                         }
421                         bcopy(&save_eh, mtod(m, struct ether_header *),
422                               ETHER_HDR_LEN);
423                 }
424         }
425
426         /*
427          * Queue message on interface, update output statistics if
428          * successful, and start output if interface not yet active.
429          */
430         error = ifq_handoff(ifp, m, &pktattr);
431         crit_exit();
432         return (error);
433 }
434
435 /*
436  * ipfw processing for ethernet packets (in and out).
437  * The second parameter is NULL from ether_demux(), and ifp from
438  * ether_output_frame(). This section of code could be used from
439  * bridge.c as well as long as we use some extra info
440  * to distinguish that case from ether_output_frame().
441  */
442 static boolean_t
443 ether_ipfw_chk(
444         struct mbuf **m0,
445         struct ifnet *dst,
446         struct ip_fw **rule,
447         struct ether_header *eh,
448         boolean_t shared)
449 {
450         struct ether_header save_eh = *eh;      /* might be a ptr in m */
451         struct ip_fw_args args;
452         struct m_tag *mtag;
453         int i;
454
455         if (*rule != NULL && fw_one_pass)
456                 return TRUE; /* dummynet packet, already partially processed */
457
458         /*
459          * I need some amount of data to be contiguous, and in case others
460          * need the packet (shared==TRUE), it also better be in the first mbuf.
461          */
462         i = min((*m0)->m_pkthdr.len, max_protohdr);
463         if (shared || (*m0)->m_len < i) {
464                 *m0 = m_pullup(*m0, i);
465                 if (*m0 == NULL)
466                         return FALSE;
467         }
468
469         args.m = *m0;           /* the packet we are looking at         */
470         args.oif = dst;         /* destination, if any                  */
471         if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
472                 m_tag_delete(*m0, mtag);
473         args.rule = *rule;      /* matching rule to restart             */
474         args.next_hop = NULL;   /* we do not support forward yet        */
475         args.eh = &save_eh;     /* MAC header for bridged/MAC packets   */
476         i = ip_fw_chk_ptr(&args);
477         *m0 = args.m;
478         *rule = args.rule;
479
480         if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL)  /* drop */
481                 return FALSE;
482
483         if (i == 0)                                     /* a PASS rule.  */
484                 return TRUE;
485
486         if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
487                 /*
488                  * Pass the pkt to dummynet, which consumes it.
489                  * If shared, make a copy and keep the original.
490                  */
491                 struct mbuf *m ;
492
493                 if (shared) {
494                         m = m_copypacket(*m0, MB_DONTWAIT);
495                         if (m == NULL)
496                                 return FALSE;
497                 } else {
498                         m = *m0 ;       /* pass the original to dummynet */
499                         *m0 = NULL ;    /* and nothing back to the caller */
500                 }
501                 /*
502                  * Prepend the header, optimize for the common case of
503                  * eh pointing into the mbuf.
504                  */
505                 if ((void *)(eh + 1) == (void *)m->m_data) {
506                         m->m_data -= ETHER_HDR_LEN ;
507                         m->m_len += ETHER_HDR_LEN ;
508                         m->m_pkthdr.len += ETHER_HDR_LEN ;
509                 } else {
510                         M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
511                         if (m == NULL)
512                                 return FALSE;
513                         bcopy(&save_eh, mtod(m, struct ether_header *),
514                               ETHER_HDR_LEN);
515                 }
516                 ip_dn_io_ptr(m, (i & 0xffff),
517                              dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
518                 return FALSE;
519         }
520         /*
521          * XXX at some point add support for divert/forward actions.
522          * If none of the above matches, we have to drop the pkt.
523          */
524         return FALSE;
525 }
526
527 /*
528  * XXX merge this function with ether_input.
529  */
530 static void
531 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
532 {
533         ether_input(ifp, NULL, m);
534 }
535
536 /*
537  * Process a received Ethernet packet. We have two different interfaces:
538  * one (conventional) assumes the packet in the mbuf, with the ethernet
539  * header provided separately in *eh. The second one (new) has everything
540  * in the mbuf, and we can tell it because eh == NULL.
541  * The caller MUST MAKE SURE that there are at least
542  * sizeof(struct ether_header) bytes in the first mbuf.
543  *
544  * This allows us to concentrate in one place a bunch of code which
545  * is replicated in all device drivers. Also, many functions called
546  * from ether_input() try to put the eh back into the mbuf, so we
547  * can later propagate the 'contiguous packet' interface to them,
548  * and handle the old interface just here.
549  *
550  * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
551  * cluster, right before m_data. So be very careful when working on m,
552  * as you could destroy *eh !!
553  *
554  * First we perform any link layer operations, then continue
555  * to the upper layers with ether_demux().
556  */
557 void
558 ether_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
559 {
560         struct ether_header save_eh;
561
562         if (eh == NULL) {
563                 if (m->m_len < sizeof(struct ether_header)) {
564                         /* XXX error in the caller. */
565                         m_freem(m);
566                         return;
567                 }
568                 m->m_pkthdr.rcvif = ifp;
569                 eh = mtod(m, struct ether_header *);
570                 m_adj(m, sizeof(struct ether_header));
571                 /* XXX */
572                 /* m->m_pkthdr.len = m->m_len; */
573         }
574
575         if (ifp->if_bpf)
576                 bpf_ptap(ifp->if_bpf, m, eh, ETHER_HDR_LEN);
577
578         ifp->if_ibytes += m->m_pkthdr.len + (sizeof *eh);
579
580         /* Handle ng_ether(4) processing, if any */
581         if (ng_ether_input_p != NULL) {
582                 (*ng_ether_input_p)(ifp, &m, eh);
583                 if (m == NULL)
584                         return;
585         }
586
587         /* Check for bridging mode */
588         if (BDG_ACTIVE(ifp)) {
589                 struct ifnet *bif;
590
591                 /* Check with bridging code */
592                 if ((bif = bridge_in_ptr(ifp, eh)) == BDG_DROP) {
593                         m_freem(m);
594                         return;
595                 }
596                 if (bif != BDG_LOCAL) {
597                         save_eh = *eh ; /* because it might change */
598                         m = bdg_forward_ptr(m, eh, bif); /* needs forwarding */
599                         /*
600                          * Do not continue if bdg_forward_ptr() processed our
601                          * packet (and cleared the mbuf pointer m) or if
602                          * it dropped (m_free'd) the packet itself.
603                          */
604                         if (m == NULL) {
605                             if (bif == BDG_BCAST || bif == BDG_MCAST)
606                                 printf("bdg_forward drop MULTICAST PKT\n");
607                             return;
608                         }
609                         eh = &save_eh ;
610                 }
611                 if (bif == BDG_LOCAL || bif == BDG_BCAST || bif == BDG_MCAST)
612                         goto recvLocal;         /* receive locally */
613
614                 /* If not local and not multicast, just drop it */
615                 m_freem(m);
616                 return;
617         }
618
619 recvLocal:
620         /* Continue with upper layer processing */
621         ether_demux(ifp, eh, m);
622 }
623
624 /*
625  * Upper layer processing for a received Ethernet packet.
626  */
627 void
628 ether_demux(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
629 {
630         int isr;
631         u_short ether_type;
632         struct ip_fw *rule = NULL;
633 #ifdef NETATALK
634         struct llc *l;
635 #endif
636
637         /* Extract info from dummynet tag, ignore others */
638         while (m->m_type == MT_TAG) {
639                 if (m->m_flags == PACKET_TAG_DUMMYNET) {
640                         rule = ((struct dn_pkt *)m)->rule;
641                         ifp = m->m_next->m_pkthdr.rcvif;
642                         break;
643                 }
644                 m = m->m_next;
645         }
646         if (rule)       /* packet was already bridged */
647                 goto post_stats;
648
649         /*
650          * Discard packet if upper layers shouldn't see it because
651          * it was unicast to a different Ethernet address.  If the
652          * driver is working properly, then this situation can only
653          * happen when the interface is in promiscuous mode.
654          */
655         if (!BDG_ACTIVE(ifp) &&
656             ((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
657             (eh->ether_dhost[0] & 1) == 0 &&
658             bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
659                 m_freem(m);
660                 return;
661         }
662         /* Discard packet if interface is not up */
663         if (!(ifp->if_flags & IFF_UP)) {
664                 m_freem(m);
665                 return;
666         }
667         if (eh->ether_dhost[0] & 1) {
668                 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
669                          ifp->if_addrlen) == 0)
670                         m->m_flags |= M_BCAST;
671                 else
672                         m->m_flags |= M_MCAST;
673                 ifp->if_imcasts++;
674         }
675
676 post_stats:
677         if (IPFW_LOADED && ether_ipfw != 0) {
678                 if (!ether_ipfw_chk(&m, NULL, &rule, eh, FALSE)) {
679                         m_freem(m);
680                         return;
681                 }
682                 eh = mtod(m, struct ether_header *);
683         }
684
685         ether_type = ntohs(eh->ether_type);
686
687         switch (ether_type) {
688 #ifdef INET
689         case ETHERTYPE_IP:
690                 if (ipflow_fastforward(m))
691                         return;
692                 isr = NETISR_IP;
693                 break;
694
695         case ETHERTYPE_ARP:
696                 if (ifp->if_flags & IFF_NOARP) {
697                         /* Discard packet if ARP is disabled on interface */
698                         m_freem(m);
699                         return;
700                 }
701                 isr = NETISR_ARP;
702                 break;
703 #endif
704
705 #ifdef INET6
706         case ETHERTYPE_IPV6:
707                 isr = NETISR_IPV6;
708                 break;
709 #endif
710
711 #ifdef IPX
712         case ETHERTYPE_IPX:
713                 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
714                         return;
715                 isr = NETISR_IPX;
716                 break;
717 #endif
718
719 #ifdef NS
720         case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
721                 isr = NETISR_NS;
722                 break;
723
724 #endif
725
726 #ifdef NETATALK
727         case ETHERTYPE_AT:
728                 isr = NETISR_ATALK1;
729                 break;
730         case ETHERTYPE_AARP:
731                 isr = NETISR_AARP;
732                 break;
733 #endif
734
735         case ETHERTYPE_VLAN:
736                 if (vlan_input_p != NULL)
737                         (*vlan_input_p)(eh, m);
738                 else {
739                         m->m_pkthdr.rcvif->if_noproto++;
740                         m_freem(m);
741                 }
742                 return;
743
744         default:
745 #ifdef IPX
746                 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
747                         return;
748 #endif
749 #ifdef NS
750                 checksum = mtod(m, ushort *);
751                 /* Novell 802.3 */
752                 if ((ether_type <= ETHERMTU) &&
753                     ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
754                         if (*checksum == 0xE0E0) {
755                                 m->m_pkthdr.len -= 3;
756                                 m->m_len -= 3;
757                                 m->m_data += 3;
758                         }
759                         isr = NETISR_NS;
760                         break;
761                 }
762 #endif
763 #ifdef NETATALK
764                 if (ether_type > ETHERMTU)
765                         goto dropanyway;
766                 l = mtod(m, struct llc *);
767                 if (l->llc_dsap == LLC_SNAP_LSAP &&
768                     l->llc_ssap == LLC_SNAP_LSAP &&
769                     l->llc_control == LLC_UI) {
770                         if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
771                                  sizeof at_org_code) == 0 &&
772                             ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
773                                 m_adj(m, sizeof(struct llc));
774                                 isr = NETISR_ATALK2;
775                                 break;
776                         }
777                         if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
778                                  sizeof aarp_org_code) == 0 &&
779                             ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
780                                 m_adj(m, sizeof(struct llc));
781                                 isr = NETISR_AARP;
782                                 break;
783                         }
784                 }
785 dropanyway:
786 #endif
787                 if (ng_ether_input_orphan_p != NULL)
788                         (*ng_ether_input_orphan_p)(ifp, m, eh);
789                 else
790                         m_freem(m);
791                 return;
792         }
793         netisr_dispatch(isr, m);
794 }
795
796 /*
797  * Perform common duties while attaching to interface list
798  */
799
800 void
801 ether_ifattach(struct ifnet *ifp, uint8_t *lla)
802 {
803         ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header));
804 }
805
806 void
807 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen)
808 {
809         struct sockaddr_dl *sdl;
810
811         ifp->if_type = IFT_ETHER;
812         ifp->if_addrlen = ETHER_ADDR_LEN;
813         ifp->if_hdrlen = ETHER_HDR_LEN;
814         if_attach(ifp);
815         ifp->if_mtu = ETHERMTU;
816         if (ifp->if_baudrate == 0)
817                 ifp->if_baudrate = 10000000;
818         ifp->if_output = ether_output;
819         ifp->if_input = ether_input_internal;
820         ifp->if_resolvemulti = ether_resolvemulti;
821         ifp->if_broadcastaddr = etherbroadcastaddr;
822         sdl = IF_LLSOCKADDR(ifp);
823         sdl->sdl_type = IFT_ETHER;
824         sdl->sdl_alen = ifp->if_addrlen;
825         bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
826         /*
827          * XXX Keep the current drivers happy.
828          * XXX Remove once all drivers have been cleaned up
829          */
830         if (lla != IFP2AC(ifp)->ac_enaddr)
831                 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
832         bpfattach(ifp, dlt, hdrlen);
833         if (ng_ether_attach_p != NULL)
834                 (*ng_ether_attach_p)(ifp);
835         if (BDG_LOADED)
836                 bdgtakeifaces_ptr();
837
838         if_printf(ifp, "MAC address: %6D\n", lla, ":");
839 }
840
841 /*
842  * Perform common duties while detaching an Ethernet interface
843  */
844 void
845 ether_ifdetach(struct ifnet *ifp)
846 {
847         crit_enter();
848         if_down(ifp);
849         crit_exit();
850
851         if (ng_ether_detach_p != NULL)
852                 (*ng_ether_detach_p)(ifp);
853         bpfdetach(ifp);
854         if_detach(ifp);
855         if (BDG_LOADED)
856                 bdgtakeifaces_ptr();
857 }
858
859 int
860 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
861 {
862         struct ifaddr *ifa = (struct ifaddr *) data;
863         struct ifreq *ifr = (struct ifreq *) data;
864         int error = 0;
865
866         switch (command) {
867         case SIOCSIFADDR:
868                 ifp->if_flags |= IFF_UP;
869
870                 switch (ifa->ifa_addr->sa_family) {
871 #ifdef INET
872                 case AF_INET:
873                         ifp->if_init(ifp->if_softc);    /* before arpwhohas */
874                         arp_ifinit(ifp, ifa);
875                         break;
876 #endif
877 #ifdef IPX
878                 /*
879                  * XXX - This code is probably wrong
880                  */
881                 case AF_IPX:
882                         {
883                         struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
884                         struct arpcom *ac = IFP2AC(ifp);
885
886                         if (ipx_nullhost(*ina))
887                                 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
888                         else
889                                 bcopy(ina->x_host.c_host, ac->ac_enaddr,
890                                       sizeof ac->ac_enaddr);
891
892                         ifp->if_init(ifp->if_softc);    /* Set new address. */
893                         break;
894                         }
895 #endif
896 #ifdef NS
897                 /*
898                  * XXX - This code is probably wrong
899                  */
900                 case AF_NS:
901                 {
902                         struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
903                         struct arpcom *ac = IFP2AC(ifp);
904
905                         if (ns_nullhost(*ina))
906                                 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
907                         else
908                                 bcopy(ina->x_host.c_host, ac->ac_enaddr,
909                                       sizeof ac->ac_enaddr);
910
911                         /*
912                          * Set new address
913                          */
914                         ifp->if_init(ifp->if_softc);
915                         break;
916                 }
917 #endif
918                 default:
919                         ifp->if_init(ifp->if_softc);
920                         break;
921                 }
922                 break;
923
924         case SIOCGIFADDR:
925                 bcopy(IFP2AC(ifp)->ac_enaddr,
926                       ((struct sockaddr *)ifr->ifr_data)->sa_data,
927                       ETHER_ADDR_LEN);
928                 break;
929
930         case SIOCSIFMTU:
931                 /*
932                  * Set the interface MTU.
933                  */
934                 if (ifr->ifr_mtu > ETHERMTU) {
935                         error = EINVAL;
936                 } else {
937                         ifp->if_mtu = ifr->ifr_mtu;
938                 }
939                 break;
940         default:
941                 error = EINVAL;
942                 break;
943         }
944         return (error);
945 }
946
947 int
948 ether_resolvemulti(
949         struct ifnet *ifp,
950         struct sockaddr **llsa,
951         struct sockaddr *sa)
952 {
953         struct sockaddr_dl *sdl;
954         struct sockaddr_in *sin;
955 #ifdef INET6
956         struct sockaddr_in6 *sin6;
957 #endif
958         u_char *e_addr;
959
960         switch(sa->sa_family) {
961         case AF_LINK:
962                 /*
963                  * No mapping needed. Just check that it's a valid MC address.
964                  */
965                 sdl = (struct sockaddr_dl *)sa;
966                 e_addr = LLADDR(sdl);
967                 if ((e_addr[0] & 1) != 1)
968                         return EADDRNOTAVAIL;
969                 *llsa = 0;
970                 return 0;
971
972 #ifdef INET
973         case AF_INET:
974                 sin = (struct sockaddr_in *)sa;
975                 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
976                         return EADDRNOTAVAIL;
977                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
978                        M_WAITOK | M_ZERO);
979                 sdl->sdl_len = sizeof *sdl;
980                 sdl->sdl_family = AF_LINK;
981                 sdl->sdl_index = ifp->if_index;
982                 sdl->sdl_type = IFT_ETHER;
983                 sdl->sdl_alen = ETHER_ADDR_LEN;
984                 e_addr = LLADDR(sdl);
985                 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
986                 *llsa = (struct sockaddr *)sdl;
987                 return 0;
988 #endif
989 #ifdef INET6
990         case AF_INET6:
991                 sin6 = (struct sockaddr_in6 *)sa;
992                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
993                         /*
994                          * An IP6 address of 0 means listen to all
995                          * of the Ethernet multicast address used for IP6.
996                          * (This is used for multicast routers.)
997                          */
998                         ifp->if_flags |= IFF_ALLMULTI;
999                         *llsa = 0;
1000                         return 0;
1001                 }
1002                 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1003                         return EADDRNOTAVAIL;
1004                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1005                        M_WAITOK | M_ZERO);
1006                 sdl->sdl_len = sizeof *sdl;
1007                 sdl->sdl_family = AF_LINK;
1008                 sdl->sdl_index = ifp->if_index;
1009                 sdl->sdl_type = IFT_ETHER;
1010                 sdl->sdl_alen = ETHER_ADDR_LEN;
1011                 e_addr = LLADDR(sdl);
1012                 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1013                 *llsa = (struct sockaddr *)sdl;
1014                 return 0;
1015 #endif
1016
1017         default:
1018                 /*
1019                  * Well, the text isn't quite right, but it's the name
1020                  * that counts...
1021                  */
1022                 return EAFNOSUPPORT;
1023         }
1024 }
1025
1026 #if 0
1027 /*
1028  * This is for reference.  We have a table-driven version
1029  * of the little-endian crc32 generator, which is faster
1030  * than the double-loop.
1031  */
1032 uint32_t
1033 ether_crc32_le(const uint8_t *buf, size_t len)
1034 {
1035         uint32_t c, crc, carry;
1036         size_t i, j;
1037
1038         crc = 0xffffffffU;      /* initial value */
1039
1040         for (i = 0; i < len; i++) {
1041                 c = buf[i];
1042                 for (j = 0; j < 8; j++) {
1043                         carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1044                         crc >>= 1;
1045                         c >>= 1;
1046                         if (carry)
1047                                 crc = (crc ^ ETHER_CRC_POLY_LE);
1048                 }
1049         }
1050
1051         return (crc);
1052 }
1053 #else
1054 uint32_t
1055 ether_crc32_le(const uint8_t *buf, size_t len)
1056 {
1057         static const uint32_t crctab[] = {
1058                 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1059                 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1060                 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1061                 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1062         };
1063         uint32_t crc;
1064         size_t i;
1065
1066         crc = 0xffffffffU;      /* initial value */
1067
1068         for (i = 0; i < len; i++) {
1069                 crc ^= buf[i];
1070                 crc = (crc >> 4) ^ crctab[crc & 0xf];
1071                 crc = (crc >> 4) ^ crctab[crc & 0xf];
1072         }
1073
1074         return (crc);
1075 }
1076 #endif
1077
1078 uint32_t
1079 ether_crc32_be(const uint8_t *buf, size_t len)
1080 {
1081         uint32_t c, crc, carry;
1082         size_t i, j;
1083
1084         crc = 0xffffffffU;      /* initial value */
1085
1086         for (i = 0; i < len; i++) {
1087                 c = buf[i];
1088                 for (j = 0; j < 8; j++) {
1089                         carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1090                         crc <<= 1;
1091                         c >>= 1;
1092                         if (carry)
1093                                 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1094                 }
1095         }
1096
1097         return (crc);
1098 }
1099
1100 /*
1101  * find the size of ethernet header, and call classifier
1102  */
1103 void
1104 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
1105                    struct altq_pktattr *pktattr)
1106 {
1107         struct ether_header *eh;
1108         uint16_t ether_type;
1109         int hlen, af, hdrsize;
1110         caddr_t hdr;
1111
1112         hlen = sizeof(struct ether_header);
1113         eh = mtod(m, struct ether_header *);
1114
1115         ether_type = ntohs(eh->ether_type);
1116         if (ether_type < ETHERMTU) {
1117                 /* ick! LLC/SNAP */
1118                 struct llc *llc = (struct llc *)(eh + 1);
1119                 hlen += 8;
1120
1121                 if (m->m_len < hlen ||
1122                     llc->llc_dsap != LLC_SNAP_LSAP ||
1123                     llc->llc_ssap != LLC_SNAP_LSAP ||
1124                     llc->llc_control != LLC_UI)
1125                         goto bad;  /* not snap! */
1126
1127                 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
1128         }
1129
1130         if (ether_type == ETHERTYPE_IP) {
1131                 af = AF_INET;
1132                 hdrsize = 20;  /* sizeof(struct ip) */
1133 #ifdef INET6
1134         } else if (ether_type == ETHERTYPE_IPV6) {
1135                 af = AF_INET6;
1136                 hdrsize = 40;  /* sizeof(struct ip6_hdr) */
1137 #endif
1138         } else
1139                 goto bad;
1140
1141         while (m->m_len <= hlen) {
1142                 hlen -= m->m_len;
1143                 m = m->m_next;
1144         }
1145         hdr = m->m_data + hlen;
1146         if (m->m_len < hlen + hdrsize) {
1147                 /*
1148                  * ip header is not in a single mbuf.  this should not
1149                  * happen in the current code.
1150                  * (todo: use m_pulldown in the future)
1151                  */
1152                 goto bad;
1153         }
1154         m->m_data += hlen;
1155         m->m_len -= hlen;
1156         ifq_classify(ifq, m, af, pktattr);
1157         m->m_data -= hlen;
1158         m->m_len += hlen;
1159
1160         return;
1161
1162 bad:
1163         pktattr->pattr_class = NULL;
1164         pktattr->pattr_hdr = NULL;
1165         pktattr->pattr_af = AF_UNSPEC;
1166 }