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