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