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