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