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