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