Annotation of src/sys/net/if_ethersubr.c, Revision 1.126.2.3
1.126.2.3! yamt 1: /* $NetBSD: if_ethersubr.c,v 1.126.2.2 2006/12/30 20:50:20 yamt Exp $ */
1.44 itojun 2:
3: /*
4: * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5: * All rights reserved.
1.120 perry 6: *
1.44 itojun 7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. Neither the name of the project nor the names of its contributors
16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
1.120 perry 18: *
1.44 itojun 19: * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
1.9 cgd 31:
1.1 cgd 32: /*
1.8 mycroft 33: * Copyright (c) 1982, 1989, 1993
34: * The Regents of the University of California. All rights reserved.
1.1 cgd 35: *
36: * Redistribution and use in source and binary forms, with or without
37: * modification, are permitted provided that the following conditions
38: * are met:
39: * 1. Redistributions of source code must retain the above copyright
40: * notice, this list of conditions and the following disclaimer.
41: * 2. Redistributions in binary form must reproduce the above copyright
42: * notice, this list of conditions and the following disclaimer in the
43: * documentation and/or other materials provided with the distribution.
1.113 agc 44: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 45: * may be used to endorse or promote products derived from this software
46: * without specific prior written permission.
47: *
48: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58: * SUCH DAMAGE.
59: *
1.27 fvdl 60: * @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96
1.1 cgd 61: */
1.90 lukem 62:
63: #include <sys/cdefs.h>
1.126.2.3! yamt 64: __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.126.2.2 2006/12/30 20:50:20 yamt Exp $");
1.1 cgd 65:
1.33 jonathan 66: #include "opt_inet.h"
67: #include "opt_atalk.h"
1.36 jonathan 68: #include "opt_iso.h"
1.112 martin 69: #include "opt_ipx.h"
70: #include "opt_mbuftrace.h"
1.30 matt 71: #include "opt_gateway.h"
1.77 thorpej 72: #include "opt_pfil_hooks.h"
1.126.2.2 yamt 73: #include "opt_pppoe.h"
1.59 thorpej 74: #include "vlan.h"
1.81 martin 75: #include "pppoe.h"
1.78 thorpej 76: #include "bridge.h"
1.69 thorpej 77: #include "bpfilter.h"
1.93 martin 78: #include "arp.h"
1.121 yamt 79: #include "agr.h"
1.30 matt 80:
1.4 mycroft 81: #include <sys/param.h>
82: #include <sys/systm.h>
83: #include <sys/kernel.h>
1.81 martin 84: #include <sys/callout.h>
1.4 mycroft 85: #include <sys/malloc.h>
86: #include <sys/mbuf.h>
87: #include <sys/protosw.h>
88: #include <sys/socket.h>
89: #include <sys/ioctl.h>
90: #include <sys/errno.h>
91: #include <sys/syslog.h>
1.126.2.1 yamt 92: #include <sys/kauth.h>
1.4 mycroft 93:
1.8 mycroft 94: #include <machine/cpu.h>
95:
1.4 mycroft 96: #include <net/if.h>
97: #include <net/netisr.h>
98: #include <net/route.h>
99: #include <net/if_llc.h>
100: #include <net/if_dl.h>
1.8 mycroft 101: #include <net/if_types.h>
1.93 martin 102:
103: #if NARP == 0
104: /*
1.102 jmmv 105: * XXX there should really be a way to issue this warning from within config(8)
1.93 martin 106: */
1.111 martin 107: #error You have included NETATALK or a pseudo-device in your configuration that depends on the presence of ethernet interfaces, but have no such interfaces configured. Check if you really need pseudo-device bridge, pppoe, vlan or options NETATALK.
1.93 martin 108: #endif
1.1 cgd 109:
1.120 perry 110: #if NBPFILTER > 0
1.69 thorpej 111: #include <net/bpf.h>
112: #endif
113:
1.22 is 114: #include <net/if_ether.h>
1.59 thorpej 115: #if NVLAN > 0
116: #include <net/if_vlanvar.h>
117: #endif
1.22 is 118:
1.81 martin 119: #if NPPPOE > 0
120: #include <net/if_pppoe.h>
121: #endif
122:
1.121 yamt 123: #if NAGR > 0
124: #include <net/agr/ieee8023_slowprotocols.h> /* XXX */
125: #include <net/agr/ieee8023ad.h>
126: #include <net/agr/if_agrvar.h>
127: #endif
128:
1.78 thorpej 129: #if NBRIDGE > 0
130: #include <net/if_bridgevar.h>
131: #endif
132:
1.15 phil 133: #include <netinet/in.h>
1.1 cgd 134: #ifdef INET
1.4 mycroft 135: #include <netinet/in_var.h>
1.1 cgd 136: #endif
1.22 is 137: #include <netinet/if_inarp.h>
1.1 cgd 138:
1.44 itojun 139: #ifdef INET6
140: #ifndef INET
141: #include <netinet/in.h>
142: #endif
143: #include <netinet6/in6_var.h>
144: #include <netinet6/nd6.h>
145: #endif
146:
1.1 cgd 147:
1.126.2.1 yamt 148: #include "carp.h"
149: #if NCARP > 0
150: #include <netinet/ip_carp.h>
151: #endif
152:
1.32 christos 153: #ifdef IPX
154: #include <netipx/ipx.h>
155: #include <netipx/ipx_if.h>
156: #endif
157:
1.1 cgd 158: #ifdef ISO
1.4 mycroft 159: #include <netiso/argo_debug.h>
160: #include <netiso/iso.h>
161: #include <netiso/iso_var.h>
162: #include <netiso/iso_snpac.h>
1.1 cgd 163: #endif
1.4 mycroft 164:
1.8 mycroft 165:
1.1 cgd 166:
1.23 christos 167: #ifdef NETATALK
168: #include <netatalk/at.h>
169: #include <netatalk/at_var.h>
170: #include <netatalk/at_extern.h>
171:
172: #define llc_snap_org_code llc_un.type_snap.org_code
173: #define llc_snap_ether_type llc_un.type_snap.ether_type
174:
175: extern u_char at_org_code[3];
176: extern u_char aarp_org_code[3];
177: #endif /* NETATALK */
178:
1.123 matt 179: static struct timeval bigpktppslim_last;
180: static int bigpktppslim = 2; /* XXX */
181: static int bigpktpps_count;
182:
183:
1.118 yamt 184: const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
185: { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1.121 yamt 186: const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
187: { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
1.8 mycroft 188: #define senderr(e) { error = (e); goto bad;}
1.1 cgd 189:
1.126.2.1 yamt 190: static int ether_output(struct ifnet *, struct mbuf *,
1.126.2.3! yamt 191: const struct sockaddr *, struct rtentry *);
1.42 thorpej 192:
1.1 cgd 193: /*
194: * Ethernet output routine.
195: * Encapsulate a packet of type family for the local net.
1.22 is 196: * Assumes that ifp is actually pointer to ethercom structure.
1.1 cgd 197: */
1.42 thorpej 198: static int
1.126.2.3! yamt 199: ether_output(struct ifnet *ifp0, struct mbuf *m0, const struct sockaddr *dst,
1.58 matt 200: struct rtentry *rt0)
1.1 cgd 201: {
1.49 matt 202: u_int16_t etype = 0;
1.122 christos 203: int error = 0, hdrcmplt = 0;
1.126.2.3! yamt 204: uint8_t esrc[6], edst[6];
1.29 mrg 205: struct mbuf *m = m0;
206: struct rtentry *rt;
1.1 cgd 207: struct mbuf *mcopy = (struct mbuf *)0;
1.29 mrg 208: struct ether_header *eh;
1.126.2.1 yamt 209: struct ifnet *ifp = ifp0;
1.72 thorpej 210: ALTQ_DECL(struct altq_pktattr pktattr;)
1.24 christos 211: #ifdef INET
1.22 is 212: struct arphdr *ah;
1.24 christos 213: #endif /* INET */
1.23 christos 214: #ifdef NETATALK
215: struct at_ifaddr *aa;
216: #endif /* NETATALK */
1.1 cgd 217:
1.104 matt 218: #ifdef MBUFTRACE
1.116 jonathan 219: m_claimm(m, ifp->if_mowner);
1.104 matt 220: #endif
1.126.2.1 yamt 221:
222: #if NCARP > 0
223: if (ifp->if_type == IFT_CARP) {
224: struct ifaddr *ifa;
225:
226: /* loop back if this is going to the carp interface */
227: if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
228: (ifa = ifa_ifwithaddr(dst)) != NULL &&
229: ifa->ifa_ifp == ifp0)
1.126.2.3! yamt 230: return looutput(ifp0, m, dst, rt0);
1.126.2.1 yamt 231:
232: ifp = ifp->if_carpdev;
233: /* ac = (struct arpcom *)ifp; */
234:
235: if ((ifp0->if_flags & (IFF_UP|IFF_RUNNING)) !=
236: (IFF_UP|IFF_RUNNING))
237: senderr(ENETDOWN);
238: }
239: #endif /* NCARP > 0 */
240:
1.8 mycroft 241: if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
242: senderr(ENETDOWN);
1.18 christos 243: if ((rt = rt0) != NULL) {
1.8 mycroft 244: if ((rt->rt_flags & RTF_UP) == 0) {
1.27 fvdl 245: if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
1.8 mycroft 246: rt->rt_refcnt--;
1.27 fvdl 247: if (rt->rt_ifp != ifp)
248: return (*rt->rt_ifp->if_output)
249: (ifp, m0, dst, rt);
1.120 perry 250: } else
1.8 mycroft 251: senderr(EHOSTUNREACH);
252: }
1.27 fvdl 253: if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) {
1.8 mycroft 254: if (rt->rt_gwroute == 0)
255: goto lookup;
256: if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
257: rtfree(rt); rt = rt0;
258: lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
259: if ((rt = rt->rt_gwroute) == 0)
260: senderr(EHOSTUNREACH);
1.27 fvdl 261: /* the "G" test below also prevents rt == rt0 */
262: if ((rt->rt_flags & RTF_GATEWAY) ||
263: (rt->rt_ifp != ifp)) {
264: rt->rt_refcnt--;
265: rt0->rt_gwroute = 0;
266: senderr(EHOSTUNREACH);
267: }
1.8 mycroft 268: }
269: }
270: if (rt->rt_flags & RTF_REJECT)
271: if (rt->rt_rmx.rmx_expire == 0 ||
1.126.2.1 yamt 272: (u_long) time_second < rt->rt_rmx.rmx_expire)
1.8 mycroft 273: senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1.1 cgd 274: }
1.72 thorpej 275:
1.1 cgd 276: switch (dst->sa_family) {
277:
278: #ifdef INET
279: case AF_INET:
1.22 is 280: if (m->m_flags & M_BCAST)
1.125 christos 281: (void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
1.126.2.3! yamt 282: else if (m->m_flags & M_MCAST)
! 283: ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
! 284: else if (!arpresolve(ifp, rt, m, dst, edst))
1.1 cgd 285: return (0); /* if not yet resolved */
1.3 hpeyerl 286: /* If broadcasting on a simplex interface, loopback a copy */
287: if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
1.1 cgd 288: mcopy = m_copy(m, 0, (int)M_COPYALL);
1.17 mycroft 289: etype = htons(ETHERTYPE_IP);
1.8 mycroft 290: break;
1.22 is 291:
292: case AF_ARP:
293: ah = mtod(m, struct arphdr *);
294: if (m->m_flags & M_BCAST)
1.125 christos 295: (void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
1.126.2.1 yamt 296: else {
297: caddr_t tha = ar_tha(ah);
298:
299: KASSERT(tha);
1.126.2.3! yamt 300: memcpy(edst, tha, sizeof(edst));
1.126.2.1 yamt 301: }
1.120 perry 302:
1.22 is 303: ah->ar_hrd = htons(ARPHRD_ETHER);
304:
1.107 itojun 305: switch (ntohs(ah->ar_op)) {
1.22 is 306: case ARPOP_REVREQUEST:
307: case ARPOP_REVREPLY:
308: etype = htons(ETHERTYPE_REVARP);
309: break;
310:
311: case ARPOP_REQUEST:
312: case ARPOP_REPLY:
313: default:
314: etype = htons(ETHERTYPE_ARP);
315: }
316:
317: break;
1.1 cgd 318: #endif
1.44 itojun 319: #ifdef INET6
320: case AF_INET6:
1.126.2.3! yamt 321: if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst, sizeof(edst))){
1.67 itojun 322: /* something bad happened */
1.99 itojun 323: return (0);
1.51 itojun 324: }
1.44 itojun 325: etype = htons(ETHERTYPE_IPV6);
326: break;
327: #endif
1.23 christos 328: #ifdef NETATALK
329: case AF_APPLETALK:
1.126.2.3! yamt 330: if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
1.23 christos 331: #ifdef NETATALKDEBUG
332: printf("aarpresolv failed\n");
333: #endif /* NETATALKDEBUG */
334: return (0);
335: }
336: /*
337: * ifaddr is the first thing in at_ifaddr
338: */
339: aa = (struct at_ifaddr *) at_ifawithnet(
1.126.2.3! yamt 340: (const struct sockaddr_at *)dst, ifp);
1.23 christos 341: if (aa == NULL)
342: goto bad;
1.120 perry 343:
1.23 christos 344: /*
345: * In the phase 2 case, we need to prepend an mbuf for the
346: * llc header. Since we must preserve the value of m,
347: * which is passed to us by value, we m_copy() the first
348: * mbuf, and use it for our llc header.
349: */
350: if (aa->aa_flags & AFA_PHASE2) {
351: struct llc llc;
352:
1.43 bouyer 353: M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
1.23 christos 354: llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
355: llc.llc_control = LLC_UI;
1.126.2.3! yamt 356: memcpy(llc.llc_snap_org_code, at_org_code,
1.23 christos 357: sizeof(llc.llc_snap_org_code));
1.38 kim 358: llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
1.126.2.3! yamt 359: memcpy(mtod(m, caddr_t), &llc, sizeof(struct llc));
1.23 christos 360: } else {
1.38 kim 361: etype = htons(ETHERTYPE_ATALK);
1.23 christos 362: }
363: break;
364: #endif /* NETATALK */
1.32 christos 365: #ifdef IPX
366: case AF_IPX:
1.39 christos 367: etype = htons(ETHERTYPE_IPX);
1.126.2.3! yamt 368: memcpy(edst,
! 369: &(((const struct sockaddr_ipx *)dst)->sipx_addr.x_host),
! 370: sizeof(edst));
1.32 christos 371: /* If broadcasting on a simplex interface, loopback a copy */
372: if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
373: mcopy = m_copy(m, 0, (int)M_COPYALL);
374: break;
375: #endif
1.1 cgd 376: #ifdef ISO
377: case AF_ISO: {
378: int snpalen;
379: struct llc *l;
1.29 mrg 380: struct sockaddr_dl *sdl;
1.1 cgd 381:
1.8 mycroft 382: if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
383: sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
1.126.2.3! yamt 384: memcpy(edst, LLADDR(sdl), sizeof(edst));
1.18 christos 385: } else {
1.126.2.3! yamt 386: error = iso_snparesolve(ifp,
! 387: (const struct sockaddr_iso *)dst,
1.18 christos 388: (char *)edst, &snpalen);
389: if (error)
390: goto bad; /* Not Resolved */
391: }
1.3 hpeyerl 392: /* If broadcasting on a simplex interface, loopback a copy */
1.8 mycroft 393: if (*edst & 1)
394: m->m_flags |= (M_BCAST|M_MCAST);
1.3 hpeyerl 395: if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) &&
1.1 cgd 396: (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
397: M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
398: if (mcopy) {
399: eh = mtod(mcopy, struct ether_header *);
1.126.2.3! yamt 400: memcpy(eh->ether_dhost, edst, sizeof(edst));
! 401: memcpy(eh->ether_shost, LLADDR(ifp->if_sadl),
! 402: sizeof(edst));
1.1 cgd 403: }
404: }
405: M_PREPEND(m, 3, M_DONTWAIT);
406: if (m == NULL)
407: return (0);
408: l = mtod(m, struct llc *);
409: l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
410: l->llc_control = LLC_UI;
1.18 christos 411: #ifdef ARGO_DEBUG
412: if (argo_debug[D_ETHER]) {
1.1 cgd 413: int i;
1.21 christos 414: printf("unoutput: sending pkt to: ");
1.1 cgd 415: for (i=0; i<6; i++)
1.21 christos 416: printf("%x ", edst[i] & 0xff);
417: printf("\n");
1.18 christos 418: }
419: #endif
1.8 mycroft 420: } break;
421: #endif /* ISO */
1.1 cgd 422:
1.31 thorpej 423: case pseudo_AF_HDRCMPLT:
424: hdrcmplt = 1;
1.126.2.3! yamt 425: memcpy(esrc,
! 426: ((const struct ether_header *)dst->sa_data)->ether_shost,
! 427: sizeof(esrc));
1.31 thorpej 428: /* FALLTHROUGH */
429:
1.1 cgd 430: case AF_UNSPEC:
1.126.2.3! yamt 431: memcpy(edst,
! 432: ((const struct ether_header *)dst->sa_data)->ether_dhost,
! 433: sizeof(edst));
1.8 mycroft 434: /* AF_UNSPEC doesn't swap the byte order of the ether_type. */
1.126.2.3! yamt 435: etype = ((const struct ether_header *)dst->sa_data)->ether_type;
1.8 mycroft 436: break;
1.1 cgd 437:
438: default:
1.21 christos 439: printf("%s: can't handle af%d\n", ifp->if_xname,
1.1 cgd 440: dst->sa_family);
1.8 mycroft 441: senderr(EAFNOSUPPORT);
1.1 cgd 442: }
443:
444: if (mcopy)
1.126.2.3! yamt 445: (void)looutput(ifp, mcopy, dst, rt);
1.16 mycroft 446:
1.50 matt 447: /* If no ether type is set, this must be a 802.2 formatted packet.
448: */
449: if (etype == 0)
450: etype = htons(m->m_pkthdr.len);
1.1 cgd 451: /*
452: * Add local net header. If no space in first mbuf,
453: * allocate another.
454: */
455: M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
1.8 mycroft 456: if (m == 0)
457: senderr(ENOBUFS);
1.1 cgd 458: eh = mtod(m, struct ether_header *);
1.96 thorpej 459: /* Note: etype is already in network byte order. */
1.126.2.3! yamt 460: (void)memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
! 461: memcpy(eh->ether_dhost, edst, sizeof(edst));
1.31 thorpej 462: if (hdrcmplt)
1.126.2.3! yamt 463: memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
1.31 thorpej 464: else
1.126.2.3! yamt 465: memcpy(eh->ether_shost, LLADDR(ifp->if_sadl),
1.31 thorpej 466: sizeof(eh->ether_shost));
1.77 thorpej 467:
1.126.2.1 yamt 468: #if NCARP > 0
469: if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
1.126.2.3! yamt 470: memcpy(eh->ether_shost, LLADDR(ifp0->if_sadl),
1.126.2.1 yamt 471: sizeof(eh->ether_shost));
472: }
473: #endif /* NCARP > 0 */
474:
1.77 thorpej 475: #ifdef PFIL_HOOKS
476: if ((error = pfil_run_hooks(&ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
477: return (error);
478: if (m == NULL)
479: return (0);
480: #endif
481:
1.78 thorpej 482: #if NBRIDGE > 0
483: /*
484: * Bridges require special output handling.
485: */
486: if (ifp->if_bridge)
487: return (bridge_output(ifp, m, NULL, NULL));
488: #endif
489:
1.126.2.1 yamt 490: #if NCARP > 0
491: if (ifp != ifp0)
492: ifp0->if_obytes += m->m_pkthdr.len + ETHER_HDR_LEN;
493: #endif /* NCARP > 0 */
494:
1.77 thorpej 495: #ifdef ALTQ
496: /*
497: * If ALTQ is enabled on the parent interface, do
498: * classification; the queueing discipline might not
499: * require classification, but might require the
500: * address family/header pointer in the pktattr.
501: */
502: if (ALTQ_IS_ENABLED(&ifp->if_snd))
503: altq_etherclassify(&ifp->if_snd, m, &pktattr);
504: #endif
505:
1.122 christos 506: return ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(&pktattr));
1.1 cgd 507:
508: bad:
509: if (m)
510: m_freem(m);
511: return (error);
512: }
1.76 thorpej 513:
514: #ifdef ALTQ
515: /*
516: * This routine is a slight hack to allow a packet to be classified
517: * if the Ethernet headers are present. It will go away when ALTQ's
518: * classification engine understands link headers.
519: */
520: void
521: altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
522: struct altq_pktattr *pktattr)
523: {
524: struct ether_header *eh;
525: u_int16_t ether_type;
526: int hlen, af, hdrsize;
527: caddr_t hdr;
528:
529: hlen = ETHER_HDR_LEN;
530: eh = mtod(m, struct ether_header *);
531:
532: ether_type = htons(eh->ether_type);
533:
534: if (ether_type < ETHERMTU) {
535: /* LLC/SNAP */
536: struct llc *llc = (struct llc *)(eh + 1);
537: hlen += 8;
538:
539: if (m->m_len < hlen ||
540: llc->llc_dsap != LLC_SNAP_LSAP ||
541: llc->llc_ssap != LLC_SNAP_LSAP ||
542: llc->llc_control != LLC_UI) {
543: /* Not SNAP. */
544: goto bad;
545: }
546:
547: ether_type = htons(llc->llc_un.type_snap.ether_type);
548: }
549:
550: switch (ether_type) {
551: case ETHERTYPE_IP:
552: af = AF_INET;
553: hdrsize = 20; /* sizeof(struct ip) */
554: break;
555:
556: case ETHERTYPE_IPV6:
557: af = AF_INET6;
558: hdrsize = 40; /* sizeof(struct ip6_hdr) */
559: break;
560:
561: default:
562: af = AF_UNSPEC;
563: hdrsize = 0;
564: break;
565: }
566:
1.95 itojun 567: while (m->m_len <= hlen) {
568: hlen -= m->m_len;
569: m = m->m_next;
570: }
1.76 thorpej 571: if (m->m_len < (hlen + hdrsize)) {
572: /*
1.95 itojun 573: * protocol header not in a single mbuf.
574: * We can't cope with this situation right
1.76 thorpej 575: * now (but it shouldn't ever happen, really, anyhow).
576: */
1.92 itojun 577: #ifdef DEBUG
1.78 thorpej 578: printf("altq_etherclassify: headers span multiple mbufs: "
579: "%d < %d\n", m->m_len, (hlen + hdrsize));
1.92 itojun 580: #endif
1.76 thorpej 581: goto bad;
582: }
583:
584: m->m_data += hlen;
585: m->m_len -= hlen;
586:
587: hdr = mtod(m, caddr_t);
588:
589: if (ALTQ_NEEDS_CLASSIFY(ifq))
590: pktattr->pattr_class =
591: (*ifq->altq_classify)(ifq->altq_clfier, m, af);
592: pktattr->pattr_af = af;
593: pktattr->pattr_hdr = hdr;
594:
595: m->m_data -= hlen;
596: m->m_len += hlen;
597:
598: return;
599:
600: bad:
601: pktattr->pattr_class = NULL;
602: pktattr->pattr_hdr = NULL;
603: pktattr->pattr_af = AF_UNSPEC;
604: }
605: #endif /* ALTQ */
1.1 cgd 606:
607: /*
608: * Process a received Ethernet packet;
1.42 thorpej 609: * the packet is in the mbuf chain m with
610: * the ether header.
1.1 cgd 611: */
1.126.2.1 yamt 612: void
1.58 matt 613: ether_input(struct ifnet *ifp, struct mbuf *m)
1.1 cgd 614: {
1.91 thorpej 615: struct ethercom *ec = (struct ethercom *) ifp;
1.29 mrg 616: struct ifqueue *inq;
1.18 christos 617: u_int16_t etype;
1.42 thorpej 618: struct ether_header *eh;
1.23 christos 619: #if defined (ISO) || defined (LLC) || defined(NETATALK)
1.29 mrg 620: struct llc *l;
1.18 christos 621: #endif
1.1 cgd 622:
1.8 mycroft 623: if ((ifp->if_flags & IFF_UP) == 0) {
624: m_freem(m);
625: return;
626: }
1.42 thorpej 627:
1.104 matt 628: #ifdef MBUFTRACE
1.116 jonathan 629: m_claimm(m, &ec->ec_rx_mowner);
1.104 matt 630: #endif
1.42 thorpej 631: eh = mtod(m, struct ether_header *);
1.63 thorpej 632: etype = ntohs(eh->ether_type);
633:
634: /*
635: * Determine if the packet is within its size limits.
636: */
1.83 thorpej 637: if (m->m_pkthdr.len >
638: ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
1.123 matt 639: if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
640: bigpktppslim)) {
641: printf("%s: discarding oversize frame (len=%d)\n",
642: ifp->if_xname, m->m_pkthdr.len);
643: }
1.63 thorpej 644: m_freem(m);
645: return;
646: }
1.77 thorpej 647:
1.84 thorpej 648: if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
649: /*
650: * If this is not a simplex interface, drop the packet
651: * if it came from us.
652: */
653: if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
654: memcmp(LLADDR(ifp->if_sadl), eh->ether_shost,
655: ETHER_ADDR_LEN) == 0) {
656: m_freem(m);
657: return;
658: }
659:
660: if (memcmp(etherbroadcastaddr,
661: eh->ether_dhost, ETHER_ADDR_LEN) == 0)
662: m->m_flags |= M_BCAST;
663: else
664: m->m_flags |= M_MCAST;
665: ifp->if_imcasts++;
666: }
667:
1.79 thorpej 668: /* If the CRC is still on the packet, trim it off. */
669: if (m->m_flags & M_HASFCS) {
670: m_adj(m, -ETHER_CRC_LEN);
671: m->m_flags &= ~M_HASFCS;
672: }
673:
1.42 thorpej 674: ifp->if_ibytes += m->m_pkthdr.len;
1.78 thorpej 675:
676: #if NBRIDGE > 0
677: /*
678: * Tap the packet off here for a bridge. bridge_input()
679: * will return NULL if it has consumed the packet, otherwise
680: * it gets processed as normal. Note that bridge_input()
681: * will always return the original packet if we need to
682: * process it locally.
683: */
684: if (ifp->if_bridge) {
1.126.2.2 yamt 685: /* clear M_PROMISC, in case the packets comes from a vlan */
686: m->m_flags &= ~M_PROMISC;
687: m = bridge_input(ifp, m);
688: if (m == NULL)
689: return;
1.78 thorpej 690:
1.126.2.2 yamt 691: /*
692: * Bridge has determined that the packet is for us.
693: * Update our interface pointer -- we may have had
694: * to "bridge" the packet locally.
695: */
696: ifp = m->m_pkthdr.rcvif;
1.120 perry 697: } else
1.78 thorpej 698: #endif /* NBRIDGE > 0 */
1.106 bouyer 699: {
1.126.2.1 yamt 700:
701: #if NCARP > 0
1.126.2.3! yamt 702: if (ifp->if_carp && ifp->if_type != IFT_CARP) {
! 703: /*
! 704: * clear M_PROMISC, in case the packets comes from a
! 705: * vlan
! 706: */
! 707: m->m_flags &= ~M_PROMISC;
! 708: if (carp_input(m, (u_int8_t *)&eh->ether_shost,
! 709: (u_int8_t *)&eh->ether_dhost, eh->ether_type) == 0)
! 710: return;
1.126.2.1 yamt 711: }
712: #endif /* NCARP > 0 */
1.106 bouyer 713: if ((m->m_flags & (M_BCAST|M_MCAST)) == 0 &&
714: (ifp->if_flags & IFF_PROMISC) != 0 &&
715: memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
716: ETHER_ADDR_LEN) != 0) {
717: m->m_flags |= M_PROMISC;
718: }
1.70 bouyer 719: }
1.78 thorpej 720:
721: #ifdef PFIL_HOOKS
1.106 bouyer 722: if ((m->m_flags & M_PROMISC) == 0) {
723: if (pfil_run_hooks(&ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
724: return;
725: if (m == NULL)
726: return;
1.78 thorpej 727:
1.106 bouyer 728: eh = mtod(m, struct ether_header *);
729: etype = ntohs(eh->ether_type);
730: }
1.78 thorpej 731: #endif
1.70 bouyer 732:
1.91 thorpej 733: /*
734: * If VLANs are configured on the interface, check to
735: * see if the device performed the decapsulation and
736: * provided us with the tag.
737: */
1.114 simonb 738: if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) {
1.70 bouyer 739: #if NVLAN > 0
740: /*
741: * vlan_input() will either recursively call ether_input()
742: * or drop the packet.
743: */
1.91 thorpej 744: vlan_input(ifp, m);
745: #else
746: m_freem(m);
1.70 bouyer 747: #endif
1.61 thorpej 748: return;
749: }
1.1 cgd 750:
1.121 yamt 751: #if NAGR > 0
752: if (ifp->if_agrprivate &&
753: __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
754: m->m_flags &= ~M_PROMISC;
755: agr_input(ifp, m);
756: return;
757: }
758: #endif /* NAGR > 0 */
759:
1.59 thorpej 760: /*
761: * Handle protocols that expect to have the Ethernet header
762: * (and possibly FCS) intact.
763: */
764: switch (etype) {
765: #if NVLAN > 0
766: case ETHERTYPE_VLAN:
767: /*
768: * vlan_input() will either recursively call ether_input()
769: * or drop the packet.
770: */
1.63 thorpej 771: if (((struct ethercom *)ifp)->ec_nvlans != 0)
772: vlan_input(ifp, m);
1.65 enami 773: else
774: m_freem(m);
1.59 thorpej 775: return;
776: #endif /* NVLAN > 0 */
1.81 martin 777: #if NPPPOE > 0
778: case ETHERTYPE_PPPOEDISC:
779: case ETHERTYPE_PPPOE:
1.106 bouyer 780: if (m->m_flags & M_PROMISC) {
781: m_freem(m);
782: return;
783: }
1.105 aymeric 784: #ifndef PPPOE_SERVER
785: if (m->m_flags & (M_MCAST | M_BCAST)) {
786: m_freem(m);
787: return;
788: }
789: #endif
790:
1.120 perry 791: if (etype == ETHERTYPE_PPPOEDISC)
1.81 martin 792: inq = &ppoediscinq;
793: else
794: inq = &ppoeinq;
795: if (IF_QFULL(inq)) {
796: IF_DROP(inq);
797: m_freem(m);
798: } else
799: IF_ENQUEUE(inq, m);
800: #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
1.103 thorpej 801: if (!callout_pending(&pppoe_softintr))
1.81 martin 802: callout_reset(&pppoe_softintr, 1, pppoe_softintr_handler, NULL);
803: #else
804: softintr_schedule(pppoe_softintr);
805: #endif
806: return;
807: #endif /* NPPPOE > 0 */
1.121 yamt 808: case ETHERTYPE_SLOWPROTOCOLS: {
809: uint8_t subtype;
810:
811: #if defined(DIAGNOSTIC)
812: if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) {
813: panic("ether_input: too short slow protocol packet");
814: }
815: #endif
816: m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
817: switch (subtype) {
818: #if NAGR > 0
819: case SLOWPROTOCOLS_SUBTYPE_LACP:
820: if (ifp->if_agrprivate) {
821: ieee8023ad_lacp_input(ifp, m);
822: return;
823: }
824: break;
825:
826: case SLOWPROTOCOLS_SUBTYPE_MARKER:
827: if (ifp->if_agrprivate) {
828: ieee8023ad_marker_input(ifp, m);
829: return;
830: }
831: break;
832: #endif /* NAGR > 0 */
833: default:
834: if (subtype == 0 || subtype > 10) {
835: /* illegal value */
836: m_freem(m);
837: return;
838: }
839: /* unknown subtype */
840: break;
841: }
842: /* FALLTHROUGH */
843: }
1.59 thorpej 844: default:
1.106 bouyer 845: if (m->m_flags & M_PROMISC) {
846: m_freem(m);
847: return;
848: }
1.59 thorpej 849: }
1.42 thorpej 850:
1.45 thorpej 851: /* If the CRC is still on the packet, trim it off. */
1.79 thorpej 852: if (m->m_flags & M_HASFCS) {
1.45 thorpej 853: m_adj(m, -ETHER_CRC_LEN);
1.79 thorpej 854: m->m_flags &= ~M_HASFCS;
855: }
1.42 thorpej 856:
1.126.2.2 yamt 857: if (etype > ETHERMTU + sizeof (struct ether_header)) {
858: /* Strip off the Ethernet header. */
859: m_adj(m, sizeof(struct ether_header));
860:
861: switch (etype) {
1.1 cgd 862: #ifdef INET
1.126.2.2 yamt 863: case ETHERTYPE_IP:
1.30 matt 864: #ifdef GATEWAY
1.126.2.2 yamt 865: if (ipflow_fastforward(m))
866: return;
1.30 matt 867: #endif
1.126.2.2 yamt 868: schednetisr(NETISR_IP);
869: inq = &ipintrq;
870: break;
1.1 cgd 871:
1.126.2.2 yamt 872: case ETHERTYPE_ARP:
873: schednetisr(NETISR_ARP);
874: inq = &arpintrq;
875: break;
1.7 glass 876:
1.126.2.2 yamt 877: case ETHERTYPE_REVARP:
878: revarpinput(m); /* XXX queue? */
879: return;
1.1 cgd 880: #endif
1.44 itojun 881: #ifdef INET6
1.126.2.2 yamt 882: case ETHERTYPE_IPV6:
883: schednetisr(NETISR_IPV6);
884: inq = &ip6intrq;
885: break;
1.32 christos 886: #endif
887: #ifdef IPX
1.126.2.2 yamt 888: case ETHERTYPE_IPX:
889: schednetisr(NETISR_IPX);
890: inq = &ipxintrq;
891: break;
1.1 cgd 892: #endif
1.23 christos 893: #ifdef NETATALK
1.126.2.2 yamt 894: case ETHERTYPE_ATALK:
895: schednetisr(NETISR_ATALK);
896: inq = &atintrq1;
897: break;
898: case ETHERTYPE_AARP:
899: /* probably this should be done with a NETISR as well */
900: aarpinput(ifp, m); /* XXX */
901: return;
1.23 christos 902: #endif /* NETATALK */
1.126.2.2 yamt 903: default:
904: m_freem(m);
905: return;
906: }
907: } else {
1.23 christos 908: #if defined (ISO) || defined (LLC) || defined (NETATALK)
1.126.2.2 yamt 909: l = (struct llc *)(eh+1);
1.8 mycroft 910: switch (l->llc_dsap) {
1.23 christos 911: #ifdef NETATALK
912: case LLC_SNAP_LSAP:
913: switch (l->llc_control) {
914: case LLC_UI:
915: if (l->llc_ssap != LLC_SNAP_LSAP) {
916: goto dropanyway;
917: }
1.120 perry 918:
1.23 christos 919: if (Bcmp(&(l->llc_snap_org_code)[0],
920: at_org_code, sizeof(at_org_code)) == 0 &&
921: ntohs(l->llc_snap_ether_type) ==
1.38 kim 922: ETHERTYPE_ATALK) {
1.23 christos 923: inq = &atintrq2;
1.126.2.2 yamt 924: m_adj(m, sizeof(struct ether_header)
925: + sizeof(struct llc));
1.23 christos 926: schednetisr(NETISR_ATALK);
927: break;
928: }
929:
930: if (Bcmp(&(l->llc_snap_org_code)[0],
931: aarp_org_code,
932: sizeof(aarp_org_code)) == 0 &&
933: ntohs(l->llc_snap_ether_type) ==
934: ETHERTYPE_AARP) {
1.126.2.2 yamt 935: m_adj( m, sizeof(struct ether_header)
936: + sizeof(struct llc));
1.23 christos 937: aarpinput(ifp, m); /* XXX */
938: return;
939: }
1.120 perry 940:
1.23 christos 941: default:
942: goto dropanyway;
943: }
944: break;
945: #endif /* NETATALK */
1.8 mycroft 946: #ifdef ISO
1.120 perry 947: case LLC_ISO_LSAP:
1.8 mycroft 948: switch (l->llc_control) {
949: case LLC_UI:
950: /* LLC_UI_P forbidden in class 1 service */
1.126.2.2 yamt 951: if ((l->llc_dsap == LLC_ISO_LSAP) && /* XXX? case tested */
1.8 mycroft 952: (l->llc_ssap == LLC_ISO_LSAP)) {
953: /* LSAP for ISO */
1.126.2.2 yamt 954: /* XXX length computation?? */
955: if (m->m_pkthdr.len > etype + sizeof(struct ether_header))
1.11 mycroft 956: m_adj(m, etype - m->m_pkthdr.len);
1.126.2.2 yamt 957:
1.18 christos 958: #ifdef ARGO_DEBUG
959: if (argo_debug[D_ETHER])
1.21 christos 960: printf("clnp packet");
1.18 christos 961: #endif
1.8 mycroft 962: schednetisr(NETISR_ISO);
963: inq = &clnlintrq;
964: break;
965: }
966: goto dropanyway;
1.120 perry 967:
1.8 mycroft 968: case LLC_XID:
969: case LLC_XID_P:
1.126.2.2 yamt 970: if(m->m_len < LLC_XID_BASIC_MINLEN + sizeof(struct ether_header))
971: /* XXX m_pullup? */
1.8 mycroft 972: goto dropanyway;
973: l->llc_window = 0;
1.126.2.2 yamt 974: l->llc_fid = LLC_XID_FORMAT_BASIC;
975: l->llc_class = LLC_XID_CLASS_I;
1.8 mycroft 976: l->llc_dsap = l->llc_ssap = 0;
977: /* Fall through to */
978: case LLC_TEST:
979: case LLC_TEST_P:
980: {
981: struct sockaddr sa;
1.29 mrg 982: struct ether_header *eh2;
1.8 mycroft 983: int i;
984: u_char c = l->llc_dsap;
985:
986: l->llc_dsap = l->llc_ssap;
987: l->llc_ssap = c;
1.126.2.2 yamt 988: m_adj(m, sizeof(struct ether_header));
989: /* XXX we can optimize here? */
1.8 mycroft 990: if (m->m_flags & (M_BCAST | M_MCAST))
1.126.2.3! yamt 991: memcpy(eh->ether_dhost,
! 992: LLADDR(ifp->if_sadl),
! 993: ETHER_ADDR_LEN);
1.8 mycroft 994: sa.sa_family = AF_UNSPEC;
995: sa.sa_len = sizeof(sa);
996: eh2 = (struct ether_header *)sa.sa_data;
997: for (i = 0; i < 6; i++) {
1.120 perry 998: eh2->ether_shost[i] = c =
1.22 is 999: eh->ether_dhost[i];
1.120 perry 1000: eh2->ether_dhost[i] =
1.22 is 1001: eh->ether_dhost[i] =
1002: eh->ether_shost[i];
1.8 mycroft 1003: eh->ether_shost[i] = c;
1004: }
1005: ifp->if_output(ifp, m, &sa, NULL);
1006: return;
1007: }
1008: default:
1009: m_freem(m);
1010: return;
1011: }
1012: break;
1013: #endif /* ISO */
1.126.2.3! yamt 1014: #if defined (ISO) || defined (NETATALK)
1.1 cgd 1015: dropanyway:
1.126.2.3! yamt 1016: #endif
1.1 cgd 1017: default:
1.8 mycroft 1018: m_freem(m);
1019: return;
1020: }
1.126.2.2 yamt 1021: #else /* ISO || LLC || NETATALK*/
1022: m_freem(m);
1023: return;
1.23 christos 1024: #endif /* ISO || LLC || NETATALK*/
1.1 cgd 1025: }
1026:
1027: if (IF_QFULL(inq)) {
1028: IF_DROP(inq);
1029: m_freem(m);
1030: } else
1031: IF_ENQUEUE(inq, m);
1032: }
1033:
1034: /*
1035: * Convert Ethernet address to printable (loggable) representation.
1036: */
1037: char *
1.58 matt 1038: ether_sprintf(const u_char *ap)
1.1 cgd 1039: {
1.126.2.1 yamt 1040: static char etherbuf[3 * ETHER_ADDR_LEN];
1041: return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
1042: return etherbuf;
1043: }
1044:
1045: char *
1046: ether_snprintf(char *buf, size_t len, const u_char *ap)
1047: {
1048: char *cp = buf;
1049: size_t i;
1.1 cgd 1050:
1.126.2.1 yamt 1051: for (i = 0; i < len / 3; i++) {
1.124 christos 1052: *cp++ = hexdigits[*ap >> 4];
1053: *cp++ = hexdigits[*ap++ & 0xf];
1.1 cgd 1054: *cp++ = ':';
1055: }
1.126.2.1 yamt 1056: *--cp = '\0';
1057: return buf;
1.1 cgd 1058: }
1.8 mycroft 1059:
1060: /*
1061: * Perform common duties while attaching to interface list
1062: */
1063: void
1.58 matt 1064: ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
1.8 mycroft 1065: {
1.104 matt 1066: struct ethercom *ec = (struct ethercom *)ifp;
1.8 mycroft 1067:
1068: ifp->if_type = IFT_ETHER;
1.75 thorpej 1069: ifp->if_addrlen = ETHER_ADDR_LEN;
1.94 enami 1070: ifp->if_hdrlen = ETHER_HDR_LEN;
1.73 thorpej 1071: ifp->if_dlt = DLT_EN10MB;
1.8 mycroft 1072: ifp->if_mtu = ETHERMTU;
1.12 mycroft 1073: ifp->if_output = ether_output;
1.42 thorpej 1074: ifp->if_input = ether_input;
1.54 thorpej 1075: if (ifp->if_baudrate == 0)
1076: ifp->if_baudrate = IF_Mbps(10); /* just a default */
1.75 thorpej 1077:
1078: if_alloc_sadl(ifp);
1079: memcpy(LLADDR(ifp->if_sadl), lla, ifp->if_addrlen);
1080:
1.104 matt 1081: LIST_INIT(&ec->ec_multiaddrs);
1.26 is 1082: ifp->if_broadcastaddr = etherbroadcastaddr;
1.69 thorpej 1083: #if NBPFILTER > 0
1.71 thorpej 1084: bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
1.69 thorpej 1085: #endif
1.104 matt 1086: #ifdef MBUFTRACE
1.108 itojun 1087: strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname,
1088: sizeof(ec->ec_tx_mowner.mo_name));
1089: strlcpy(ec->ec_tx_mowner.mo_descr, "tx",
1090: sizeof(ec->ec_tx_mowner.mo_descr));
1091: strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname,
1092: sizeof(ec->ec_rx_mowner.mo_name));
1093: strlcpy(ec->ec_rx_mowner.mo_descr, "rx",
1094: sizeof(ec->ec_rx_mowner.mo_descr));
1.104 matt 1095: MOWNER_ATTACH(&ec->ec_tx_mowner);
1096: MOWNER_ATTACH(&ec->ec_rx_mowner);
1097: ifp->if_mowner = &ec->ec_tx_mowner;
1098: #endif
1.52 thorpej 1099: }
1100:
1101: void
1.58 matt 1102: ether_ifdetach(struct ifnet *ifp)
1.52 thorpej 1103: {
1.63 thorpej 1104: struct ethercom *ec = (void *) ifp;
1105: struct ether_multi *enm;
1106: int s;
1.69 thorpej 1107:
1.115 dyoung 1108: #if NBRIDGE > 0
1109: if (ifp->if_bridge)
1110: bridge_ifdetach(ifp);
1111: #endif
1112:
1.69 thorpej 1113: #if NBPFILTER > 0
1114: bpfdetach(ifp);
1115: #endif
1.64 thorpej 1116:
1117: #if NVLAN > 0
1118: if (ec->ec_nvlans)
1119: vlan_ifdetach(ifp);
1120: #endif
1.63 thorpej 1121:
1.79 thorpej 1122: s = splnet();
1.63 thorpej 1123: while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
1124: LIST_REMOVE(enm, enm_list);
1.100 jdolecek 1125: free(enm, M_IFMADDR);
1.63 thorpej 1126: ec->ec_multicnt--;
1127: }
1128: splx(s);
1.52 thorpej 1129:
1.109 itojun 1130: #if 0 /* done in if_detach() */
1.75 thorpej 1131: if_free_sadl(ifp);
1.109 itojun 1132: #endif
1.104 matt 1133:
1134: MOWNER_DETACH(&ec->ec_rx_mowner);
1135: MOWNER_DETACH(&ec->ec_tx_mowner);
1.53 thorpej 1136: }
1137:
1.56 thorpej 1138: #if 0
1139: /*
1140: * This is for reference. We have a table-driven version
1141: * of the little-endian crc32 generator, which is faster
1142: * than the double-loop.
1143: */
1.53 thorpej 1144: u_int32_t
1.58 matt 1145: ether_crc32_le(const u_int8_t *buf, size_t len)
1.53 thorpej 1146: {
1147: u_int32_t c, crc, carry;
1148: size_t i, j;
1149:
1150: crc = 0xffffffffU; /* initial value */
1151:
1152: for (i = 0; i < len; i++) {
1153: c = buf[i];
1154: for (j = 0; j < 8; j++) {
1155: carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1156: crc >>= 1;
1157: c >>= 1;
1158: if (carry)
1.56 thorpej 1159: crc = (crc ^ ETHER_CRC_POLY_LE);
1.53 thorpej 1160: }
1161: }
1162:
1163: return (crc);
1164: }
1.56 thorpej 1165: #else
1166: u_int32_t
1.58 matt 1167: ether_crc32_le(const u_int8_t *buf, size_t len)
1.56 thorpej 1168: {
1169: static const u_int32_t crctab[] = {
1170: 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1171: 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1172: 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1173: 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1174: };
1175: u_int32_t crc;
1.98 thorpej 1176: size_t i;
1.56 thorpej 1177:
1178: crc = 0xffffffffU; /* initial value */
1179:
1180: for (i = 0; i < len; i++) {
1181: crc ^= buf[i];
1182: crc = (crc >> 4) ^ crctab[crc & 0xf];
1183: crc = (crc >> 4) ^ crctab[crc & 0xf];
1184: }
1185:
1186: return (crc);
1187: }
1188: #endif
1.53 thorpej 1189:
1190: u_int32_t
1.58 matt 1191: ether_crc32_be(const u_int8_t *buf, size_t len)
1.53 thorpej 1192: {
1193: u_int32_t c, crc, carry;
1194: size_t i, j;
1195:
1196: crc = 0xffffffffU; /* initial value */
1197:
1198: for (i = 0; i < len; i++) {
1199: c = buf[i];
1200: for (j = 0; j < 8; j++) {
1201: carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1202: crc <<= 1;
1203: c >>= 1;
1204: if (carry)
1205: crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1206: }
1207: }
1208:
1209: return (crc);
1.8 mycroft 1210: }
1211:
1.48 is 1212: #ifdef INET
1.118 yamt 1213: const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
1214: { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1215: const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
1216: { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1.48 is 1217: #endif
1.44 itojun 1218: #ifdef INET6
1.118 yamt 1219: const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
1220: { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1221: const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
1222: { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1.44 itojun 1223: #endif
1.60 enami 1224:
1.3 hpeyerl 1225: /*
1.126.2.2 yamt 1226: * ether_aton implementation, not using a static buffer.
1227: */
1228: int
1229: ether_nonstatic_aton(u_char *dest, char *str)
1230: {
1231: int i;
1232: char *cp = str;
1233: u_char val[6];
1234:
1235: #define set_value \
1236: if (*cp > '9' && *cp < 'a') \
1237: *cp -= 'A' - 10; \
1238: else if (*cp > '9') \
1239: *cp -= 'a' - 10; \
1240: else \
1241: *cp -= '0'
1242:
1243: for (i = 0; i < 6; i++, cp++) {
1244: if (!isxdigit(*cp))
1245: return (1);
1246: set_value;
1247: val[i] = *cp++;
1248: if (isxdigit(*cp)) {
1249: set_value;
1250: val[i] *= 16;
1251: val[i] += *cp++;
1252: }
1253: if (*cp == ':' || i == 5)
1254: continue;
1255: else
1256: return 1;
1257: }
1258: memcpy(dest, val, 6);
1259:
1260: return 0;
1261: }
1262:
1263:
1264: /*
1.60 enami 1265: * Convert a sockaddr into an Ethernet address or range of Ethernet
1266: * addresses.
1.3 hpeyerl 1267: */
1268: int
1.60 enami 1269: ether_multiaddr(struct sockaddr *sa, u_int8_t addrlo[ETHER_ADDR_LEN],
1270: u_int8_t addrhi[ETHER_ADDR_LEN])
1.3 hpeyerl 1271: {
1.24 christos 1272: #ifdef INET
1.3 hpeyerl 1273: struct sockaddr_in *sin;
1.24 christos 1274: #endif /* INET */
1.44 itojun 1275: #ifdef INET6
1276: struct sockaddr_in6 *sin6;
1277: #endif /* INET6 */
1.3 hpeyerl 1278:
1.60 enami 1279: switch (sa->sa_family) {
1.3 hpeyerl 1280:
1281: case AF_UNSPEC:
1.126.2.3! yamt 1282: memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
! 1283: memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1.3 hpeyerl 1284: break;
1285:
1286: #ifdef INET
1287: case AF_INET:
1.60 enami 1288: sin = satosin(sa);
1.3 hpeyerl 1289: if (sin->sin_addr.s_addr == INADDR_ANY) {
1290: /*
1.60 enami 1291: * An IP address of INADDR_ANY means listen to
1292: * or stop listening to all of the Ethernet
1293: * multicast addresses used for IP.
1.3 hpeyerl 1294: * (This is for the sake of IP multicast routers.)
1295: */
1.126.2.3! yamt 1296: memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
! 1297: memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
1.3 hpeyerl 1298: }
1299: else {
1300: ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1.126.2.3! yamt 1301: memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1.3 hpeyerl 1302: }
1303: break;
1304: #endif
1.44 itojun 1305: #ifdef INET6
1306: case AF_INET6:
1.60 enami 1307: sin6 = satosin6(sa);
1.47 itojun 1308: if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1.44 itojun 1309: /*
1.60 enami 1310: * An IP6 address of 0 means listen to or stop
1311: * listening to all of the Ethernet multicast
1312: * address used for IP6.
1.44 itojun 1313: * (This is used for multicast routers.)
1314: */
1.126.2.3! yamt 1315: memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
! 1316: memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
1.44 itojun 1317: } else {
1318: ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1.126.2.3! yamt 1319: memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1.44 itojun 1320: }
1321: break;
1322: #endif
1.3 hpeyerl 1323:
1324: default:
1.126.2.3! yamt 1325: return EAFNOSUPPORT;
1.60 enami 1326: }
1.126.2.3! yamt 1327: return 0;
1.60 enami 1328: }
1329:
1330: /*
1331: * Add an Ethernet multicast address or range of addresses to the list for a
1332: * given interface.
1333: */
1334: int
1335: ether_addmulti(struct ifreq *ifr, struct ethercom *ec)
1336: {
1337: struct ether_multi *enm;
1338: u_char addrlo[ETHER_ADDR_LEN];
1339: u_char addrhi[ETHER_ADDR_LEN];
1.79 thorpej 1340: int s = splnet(), error;
1.60 enami 1341:
1342: error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1343: if (error != 0) {
1.3 hpeyerl 1344: splx(s);
1.126.2.3! yamt 1345: return error;
1.3 hpeyerl 1346: }
1347:
1348: /*
1349: * Verify that we have valid Ethernet multicast addresses.
1350: */
1351: if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
1352: splx(s);
1.126.2.3! yamt 1353: return EINVAL;
1.3 hpeyerl 1354: }
1355: /*
1356: * See if the address range is already in the list.
1357: */
1.22 is 1358: ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1.3 hpeyerl 1359: if (enm != NULL) {
1360: /*
1361: * Found it; just increment the reference count.
1362: */
1363: ++enm->enm_refcount;
1364: splx(s);
1.126.2.3! yamt 1365: return 0;
1.3 hpeyerl 1366: }
1367: /*
1368: * New address or range; malloc a new multicast record
1369: * and link it into the interface's multicast list.
1370: */
1371: enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1372: if (enm == NULL) {
1373: splx(s);
1.126.2.3! yamt 1374: return ENOBUFS;
1.3 hpeyerl 1375: }
1.126.2.3! yamt 1376: memcpy(enm->enm_addrlo, addrlo, 6);
! 1377: memcpy(enm->enm_addrhi, addrhi, 6);
1.3 hpeyerl 1378: enm->enm_refcount = 1;
1.22 is 1379: LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1380: ec->ec_multicnt++;
1.3 hpeyerl 1381: splx(s);
1382: /*
1383: * Return ENETRESET to inform the driver that the list has changed
1384: * and its reception filter should be adjusted accordingly.
1385: */
1.126.2.3! yamt 1386: return ENETRESET;
1.3 hpeyerl 1387: }
1388:
1389: /*
1390: * Delete a multicast address record.
1391: */
1392: int
1.58 matt 1393: ether_delmulti(struct ifreq *ifr, struct ethercom *ec)
1.3 hpeyerl 1394: {
1.29 mrg 1395: struct ether_multi *enm;
1.60 enami 1396: u_char addrlo[ETHER_ADDR_LEN];
1397: u_char addrhi[ETHER_ADDR_LEN];
1.79 thorpej 1398: int s = splnet(), error;
1.3 hpeyerl 1399:
1.60 enami 1400: error = ether_multiaddr(&ifr->ifr_addr, addrlo, addrhi);
1401: if (error != 0) {
1.3 hpeyerl 1402: splx(s);
1.60 enami 1403: return (error);
1.3 hpeyerl 1404: }
1405:
1406: /*
1.66 thorpej 1407: * Look ur the address in our list.
1.3 hpeyerl 1408: */
1.22 is 1409: ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1.3 hpeyerl 1410: if (enm == NULL) {
1411: splx(s);
1412: return (ENXIO);
1413: }
1414: if (--enm->enm_refcount != 0) {
1415: /*
1416: * Still some claims to this record.
1417: */
1418: splx(s);
1419: return (0);
1420: }
1421: /*
1422: * No remaining claims to this record; unlink and free it.
1423: */
1.13 mycroft 1424: LIST_REMOVE(enm, enm_list);
1.3 hpeyerl 1425: free(enm, M_IFMADDR);
1.22 is 1426: ec->ec_multicnt--;
1.3 hpeyerl 1427: splx(s);
1428: /*
1429: * Return ENETRESET to inform the driver that the list has changed
1430: * and its reception filter should be adjusted accordingly.
1431: */
1432: return (ENETRESET);
1.66 thorpej 1433: }
1434:
1435: /*
1436: * Common ioctls for Ethernet interfaces. Note, we must be
1437: * called at splnet().
1438: */
1439: int
1440: ether_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1441: {
1442: struct ethercom *ec = (void *) ifp;
1443: struct ifreq *ifr = (struct ifreq *)data;
1444: struct ifaddr *ifa = (struct ifaddr *)data;
1445: int error = 0;
1446:
1447: switch (cmd) {
1448: case SIOCSIFADDR:
1449: ifp->if_flags |= IFF_UP;
1450: switch (ifa->ifa_addr->sa_family) {
1.75 thorpej 1451: case AF_LINK:
1452: {
1453: struct sockaddr_dl *sdl =
1454: (struct sockaddr_dl *) ifa->ifa_addr;
1455:
1456: if (sdl->sdl_type != IFT_ETHER ||
1457: sdl->sdl_alen != ifp->if_addrlen) {
1458: error = EINVAL;
1459: break;
1460: }
1461:
1462: memcpy(LLADDR(ifp->if_sadl), LLADDR(sdl),
1463: ifp->if_addrlen);
1464:
1465: /* Set new address. */
1466: error = (*ifp->if_init)(ifp);
1467: break;
1468: }
1.66 thorpej 1469: #ifdef INET
1470: case AF_INET:
1.86 thorpej 1471: if ((ifp->if_flags & IFF_RUNNING) == 0 &&
1472: (error = (*ifp->if_init)(ifp)) != 0)
1.66 thorpej 1473: break;
1474: arp_ifinit(ifp, ifa);
1475: break;
1476: #endif /* INET */
1477: default:
1.86 thorpej 1478: if ((ifp->if_flags & IFF_RUNNING) == 0)
1479: error = (*ifp->if_init)(ifp);
1.66 thorpej 1480: break;
1481: }
1482: break;
1483:
1484: case SIOCGIFADDR:
1485: memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
1486: LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1487: break;
1488:
1489: case SIOCSIFMTU:
1.82 thorpej 1490: {
1491: int maxmtu;
1492:
1493: if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
1494: maxmtu = ETHERMTU_JUMBO;
1495: else
1496: maxmtu = ETHERMTU;
1497:
1498: if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1.66 thorpej 1499: error = EINVAL;
1.88 thorpej 1500: else {
1.66 thorpej 1501: ifp->if_mtu = ifr->ifr_mtu;
1.87 thorpej 1502:
1.88 thorpej 1503: /* Make sure the device notices the MTU change. */
1504: if (ifp->if_flags & IFF_UP)
1505: error = (*ifp->if_init)(ifp);
1506: }
1.66 thorpej 1507: break;
1.82 thorpej 1508: }
1.66 thorpej 1509:
1510: case SIOCSIFFLAGS:
1511: if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
1512: /*
1513: * If interface is marked down and it is running,
1514: * then stop and disable it.
1515: */
1516: (*ifp->if_stop)(ifp, 1);
1517: } else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
1518: /*
1519: * If interface is marked up and it is stopped, then
1520: * start it.
1521: */
1522: error = (*ifp->if_init)(ifp);
1523: } else if ((ifp->if_flags & IFF_UP) != 0) {
1524: /*
1525: * Reset the interface to pick up changes in any other
1526: * flags that affect the hardware state.
1527: */
1528: error = (*ifp->if_init)(ifp);
1529: }
1530: break;
1531:
1532: case SIOCADDMULTI:
1.74 augustss 1533: error = ether_addmulti(ifr, ec);
1534: break;
1535:
1.66 thorpej 1536: case SIOCDELMULTI:
1.74 augustss 1537: error = ether_delmulti(ifr, ec);
1.66 thorpej 1538: break;
1539:
1540: default:
1541: error = ENOTTY;
1542: }
1543:
1544: return (error);
1.3 hpeyerl 1545: }
CVSweb <webmaster@jp.NetBSD.org>