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