xref: /freebsd/sys/net/if_ethersubr.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /*
2  * Copyright (c) 1982, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
34  * $FreeBSD$
35  */
36 
37 #include "opt_atalk.h"
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 #include "opt_ipx.h"
41 #include "opt_bdg.h"
42 #include "opt_netgraph.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
52 
53 #include <net/if.h>
54 #include <net/netisr.h>
55 #include <net/route.h>
56 #include <net/if_llc.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/bpf.h>
60 #include <net/ethernet.h>
61 
62 #if defined(INET) || defined(INET6)
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
65 #include <netinet/if_ether.h>
66 #endif
67 #ifdef INET6
68 #include <netinet6/nd6.h>
69 #endif
70 
71 #ifdef IPX
72 #include <netipx/ipx.h>
73 #include <netipx/ipx_if.h>
74 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
75 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
76 		struct sockaddr *dst, short *tp, int *hlen);
77 #endif
78 
79 #ifdef NS
80 #include <netns/ns.h>
81 #include <netns/ns_if.h>
82 ushort ns_nettype;
83 int ether_outputdebug = 0;
84 int ether_inputdebug = 0;
85 #endif
86 
87 #ifdef NETATALK
88 #include <netatalk/at.h>
89 #include <netatalk/at_var.h>
90 #include <netatalk/at_extern.h>
91 
92 #define llc_snap_org_code llc_un.type_snap.org_code
93 #define llc_snap_ether_type llc_un.type_snap.ether_type
94 
95 extern u_char	at_org_code[3];
96 extern u_char	aarp_org_code[3];
97 #endif /* NETATALK */
98 
99 #ifdef BRIDGE
100 #include <net/bridge.h>
101 #endif
102 
103 #include "vlan.h"
104 #if NVLAN > 0
105 #include <net/if_vlan_var.h>
106 #endif /* NVLAN > 0 */
107 
108 /* netgraph node hooks for ng_ether(4) */
109 void	(*ng_ether_input_p)(struct ifnet *ifp,
110 		struct mbuf **mp, struct ether_header *eh);
111 void	(*ng_ether_input_orphan_p)(struct ifnet *ifp,
112 		struct mbuf *m, struct ether_header *eh);
113 int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
114 void	(*ng_ether_attach_p)(struct ifnet *ifp);
115 void	(*ng_ether_detach_p)(struct ifnet *ifp);
116 
117 static	int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
118 				    struct sockaddr *));
119 u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
120 #define senderr(e) do { error = (e); goto bad;} while (0)
121 #define IFP2AC(IFP) ((struct arpcom *)IFP)
122 
123 /*
124  * Ethernet output routine.
125  * Encapsulate a packet of type family for the local net.
126  * Use trailer local net encapsulation if enough data in first
127  * packet leaves a multiple of 512 bytes of data in remainder.
128  * Assumes that ifp is actually pointer to arpcom structure.
129  */
130 int
131 ether_output(ifp, m, dst, rt0)
132 	register struct ifnet *ifp;
133 	struct mbuf *m;
134 	struct sockaddr *dst;
135 	struct rtentry *rt0;
136 {
137 	short type;
138 	int error = 0, hdrcmplt = 0;
139  	u_char esrc[6], edst[6];
140 	register struct rtentry *rt;
141 	register struct ether_header *eh;
142 	int off, loop_copy = 0;
143 	int hlen;	/* link layer header lenght */
144 	struct arpcom *ac = IFP2AC(ifp);
145 
146 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
147 		senderr(ENETDOWN);
148 	rt = rt0;
149 	if (rt) {
150 		if ((rt->rt_flags & RTF_UP) == 0) {
151 			rt0 = rt = rtalloc1(dst, 1, 0UL);
152 			if (rt0)
153 				rt->rt_refcnt--;
154 			else
155 				senderr(EHOSTUNREACH);
156 		}
157 		if (rt->rt_flags & RTF_GATEWAY) {
158 			if (rt->rt_gwroute == 0)
159 				goto lookup;
160 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
161 				rtfree(rt); rt = rt0;
162 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
163 							  0UL);
164 				if ((rt = rt->rt_gwroute) == 0)
165 					senderr(EHOSTUNREACH);
166 			}
167 		}
168 		if (rt->rt_flags & RTF_REJECT)
169 			if (rt->rt_rmx.rmx_expire == 0 ||
170 			    time_second < rt->rt_rmx.rmx_expire)
171 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
172 	}
173 	hlen = ETHER_HDR_LEN;
174 	switch (dst->sa_family) {
175 #ifdef INET
176 	case AF_INET:
177 		if (!arpresolve(ac, rt, m, dst, edst, rt0))
178 			return (0);	/* if not yet resolved */
179 		off = m->m_pkthdr.len - m->m_len;
180 		type = htons(ETHERTYPE_IP);
181 		break;
182 #endif
183 #ifdef INET6
184 	case AF_INET6:
185 		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
186 			/* this must be impossible, so we bark */
187 			printf("nd6_storelladdr failed\n");
188 			return(0);
189 		}
190 		off = m->m_pkthdr.len - m->m_len;
191 		type = htons(ETHERTYPE_IPV6);
192 		break;
193 #endif
194 #ifdef IPX
195 	case AF_IPX:
196 		if (ef_outputp) {
197 		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
198 		    if (error)
199 			goto bad;
200 		} else
201 		    type = htons(ETHERTYPE_IPX);
202  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
203 		    (caddr_t)edst, sizeof (edst));
204 		break;
205 #endif
206 #ifdef NETATALK
207 	case AF_APPLETALK:
208 	  {
209 	    struct at_ifaddr *aa;
210 
211 	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
212 		    goto bad;
213 	    }
214 	    if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
215 		    return (0);
216 	    /*
217 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
218 	     * Since we must preserve the value of m, which is passed to us by
219 	     * value, we m_copy() the first mbuf, and use it for our llc header.
220 	     */
221 	    if ( aa->aa_flags & AFA_PHASE2 ) {
222 		struct llc llc;
223 
224 		M_PREPEND(m, sizeof(struct llc), M_WAIT);
225 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
226 		llc.llc_control = LLC_UI;
227 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
228 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
229 		bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
230 		type = htons(m->m_pkthdr.len);
231 		hlen = sizeof(struct llc) + ETHER_HDR_LEN;
232 	    } else {
233 		type = htons(ETHERTYPE_AT);
234 	    }
235 	    break;
236 	  }
237 #endif NETATALK
238 #ifdef NS
239 	case AF_NS:
240 		switch(ns_nettype){
241 		default:
242 		case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
243 			type = 0x8137;
244 			break;
245 		case 0x0: /* Novell 802.3 */
246 			type = htons( m->m_pkthdr.len);
247 			break;
248 		case 0xe0e0: /* Novell 802.2 and Token-Ring */
249 			M_PREPEND(m, 3, M_WAIT);
250 			type = htons( m->m_pkthdr.len);
251 			cp = mtod(m, u_char *);
252 			*cp++ = 0xE0;
253 			*cp++ = 0xE0;
254 			*cp++ = 0x03;
255 			break;
256 		}
257  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
258 		    (caddr_t)edst, sizeof (edst));
259 		/*
260 		 * XXX if ns_thishost is the same as the node's ethernet
261 		 * address then just the default code will catch this anyhow.
262 		 * So I'm not sure if this next clause should be here at all?
263 		 * [JRE]
264 		 */
265 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
266 			m->m_pkthdr.rcvif = ifp;
267 			inq = &nsintrq;
268 			if (IF_HANDOFF(inq, m, NULL))
269 				schednetisr(NETISR_NS);
270 			return (error);
271 		}
272 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
273 			m->m_flags |= M_BCAST;
274 		}
275 		break;
276 #endif /* NS */
277 
278 	case pseudo_AF_HDRCMPLT:
279 		hdrcmplt = 1;
280 		eh = (struct ether_header *)dst->sa_data;
281 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
282 		/* FALLTHROUGH */
283 
284 	case AF_UNSPEC:
285 		loop_copy = -1; /* if this is for us, don't do it */
286 		eh = (struct ether_header *)dst->sa_data;
287  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
288 		type = eh->ether_type;
289 		break;
290 
291 	default:
292 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
293 			dst->sa_family);
294 		senderr(EAFNOSUPPORT);
295 	}
296 
297 	/*
298 	 * Add local net header.  If no space in first mbuf,
299 	 * allocate another.
300 	 */
301 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
302 	if (m == 0)
303 		senderr(ENOBUFS);
304 	eh = mtod(m, struct ether_header *);
305 	(void)memcpy(&eh->ether_type, &type,
306 		sizeof(eh->ether_type));
307  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
308 	if (hdrcmplt)
309 		(void)memcpy(eh->ether_shost, esrc,
310 			sizeof(eh->ether_shost));
311 	else
312 		(void)memcpy(eh->ether_shost, ac->ac_enaddr,
313 			sizeof(eh->ether_shost));
314 
315 	/*
316 	 * If a simplex interface, and the packet is being sent to our
317 	 * Ethernet address or a broadcast address, loopback a copy.
318 	 * XXX To make a simplex device behave exactly like a duplex
319 	 * device, we should copy in the case of sending to our own
320 	 * ethernet address (thus letting the original actually appear
321 	 * on the wire). However, we don't do that here for security
322 	 * reasons and compatibility with the original behavior.
323 	 */
324 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
325 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
326 			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
327 
328 			(void) if_simloop(ifp, n, dst->sa_family, hlen);
329 		} else if (bcmp(eh->ether_dhost,
330 		    eh->ether_shost, ETHER_ADDR_LEN) == 0) {
331 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
332 			return (0);	/* XXX */
333 		}
334 	}
335 
336 	/* Handle ng_ether(4) processing, if any */
337 	if (ng_ether_output_p != NULL) {
338 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
339 bad:			if (m != NULL)
340 				m_freem(m);
341 			return (error);
342 		}
343 		if (m == NULL)
344 			return (0);
345 	}
346 
347 	/* Continue with link-layer output */
348 	return ether_output_frame(ifp, m);
349 }
350 
351 /*
352  * Ethernet link layer output routine to send a raw frame to the device.
353  *
354  * This assumes that the 14 byte Ethernet header is present and contiguous
355  * in the first mbuf (if BRIDGE'ing).
356  */
357 int
358 ether_output_frame(ifp, m)
359 	struct ifnet *ifp;
360 	struct mbuf *m;
361 {
362 	int error = 0;
363 
364 #ifdef BRIDGE
365 	if (do_bridge) {
366 		struct ether_header hdr;
367 
368 		m->m_pkthdr.rcvif = NULL;
369 		bcopy(mtod(m, struct ether_header *), &hdr, ETHER_HDR_LEN);
370 		m_adj(m, ETHER_HDR_LEN);
371 		ifp = bridge_dst_lookup(&hdr);
372 		bdg_forward(&m, &hdr, ifp);
373 		if (m != NULL)
374 			m_freem(m);
375 		return (0);
376 	}
377 #endif
378 
379 	/*
380 	 * Queue message on interface, update output statistics if
381 	 * successful, and start output if interface not yet active.
382 	 */
383 	if (! IF_HANDOFF(&ifp->if_snd, m, ifp))
384 		return (ENOBUFS);
385 	return (error);
386 }
387 
388 /*
389  * Process a received Ethernet packet;
390  * the packet is in the mbuf chain m without
391  * the ether header, which is provided separately.
392  *
393  * First we perform any link layer operations, then continue
394  * to the upper layers with ether_demux().
395  */
396 void
397 ether_input(ifp, eh, m)
398 	struct ifnet *ifp;
399 	struct ether_header *eh;
400 	struct mbuf *m;
401 {
402 
403 	/* Check for a BPF tap */
404 	if (ifp->if_bpf != NULL) {
405 		struct m_hdr mh;
406 
407 		/* This kludge is OK; BPF treats the "mbuf" as read-only */
408 		mh.mh_next = m;
409 		mh.mh_data = (char *)eh;
410 		mh.mh_len = ETHER_HDR_LEN;
411 		bpf_mtap(ifp, (struct mbuf *)&mh);
412 	}
413 
414 	/* Handle ng_ether(4) processing, if any */
415 	if (ng_ether_input_p != NULL) {
416 		(*ng_ether_input_p)(ifp, &m, eh);
417 		if (m == NULL)
418 			return;
419 	}
420 
421 #ifdef BRIDGE
422 	/* Check for bridging mode */
423 	if (do_bridge) {
424 		struct ifnet *bif;
425 
426 		/* Check with bridging code */
427 		if ((bif = bridge_in(ifp, eh)) == BDG_DROP) {
428 			m_freem(m);
429 			return;
430 		}
431 		if (bif != BDG_LOCAL) {
432 			bdg_forward(&m, eh, bif);	/* needs forwarding */
433 			/*
434 			 * Do not continue if bdg_forward() processed our
435 			 * packet (and cleared the mbuf pointer m) or if
436 			 * it dropped (m_free'd) the packet itself.
437 			 */
438 			if (m == NULL)
439 				return;
440 		}
441 		if (bif == BDG_LOCAL
442 		    || bif == BDG_BCAST
443 		    || bif == BDG_MCAST)
444 			goto recvLocal;			/* receive locally */
445 
446 		/* If not local and not multicast, just drop it */
447 		if (m != NULL)
448 			m_freem(m);
449 		return;
450        }
451 #endif
452 
453 	/* Discard packet if upper layers shouldn't see it. This should
454 	   only happen when the interface is in promiscuous mode. */
455 	if ((ifp->if_flags & IFF_PROMISC) != 0
456 	    && (eh->ether_dhost[0] & 1) == 0
457 	    && bcmp(eh->ether_dhost,
458 	      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0) {
459 		m_freem(m);
460 		return;
461 	}
462 
463 #ifdef BRIDGE
464 recvLocal:
465 #endif
466 	/* Continue with upper layer processing */
467 	ether_demux(ifp, eh, m);
468 }
469 
470 /*
471  * Upper layer processing for a received Ethernet packet.
472  */
473 void
474 ether_demux(ifp, eh, m)
475 	struct ifnet *ifp;
476 	struct ether_header *eh;
477 	struct mbuf *m;
478 {
479 	struct ifqueue *inq;
480 	u_short ether_type;
481 #if defined(NETATALK)
482 	register struct llc *l;
483 #endif
484 
485 	/* Discard packet if interface is not up */
486 	if ((ifp->if_flags & IFF_UP) == 0) {
487 		m_freem(m);
488 		return;
489 	}
490 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
491 	if (eh->ether_dhost[0] & 1) {
492 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
493 			 sizeof(etherbroadcastaddr)) == 0)
494 			m->m_flags |= M_BCAST;
495 		else
496 			m->m_flags |= M_MCAST;
497 	}
498 	if (m->m_flags & (M_BCAST|M_MCAST))
499 		ifp->if_imcasts++;
500 
501 	ether_type = ntohs(eh->ether_type);
502 
503 #if NVLAN > 0
504 	if (ether_type == vlan_proto) {
505 		if (vlan_input(eh, m) < 0)
506 			ifp->if_data.ifi_noproto++;
507 		return;
508 	}
509 #endif /* NVLAN > 0 */
510 
511 	switch (ether_type) {
512 #ifdef INET
513 	case ETHERTYPE_IP:
514 		if (ipflow_fastforward(m))
515 			return;
516 		schednetisr(NETISR_IP);
517 		inq = &ipintrq;
518 		break;
519 
520 	case ETHERTYPE_ARP:
521 		schednetisr(NETISR_ARP);
522 		inq = &arpintrq;
523 		break;
524 #endif
525 #ifdef IPX
526 	case ETHERTYPE_IPX:
527 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
528 			return;
529 		schednetisr(NETISR_IPX);
530 		inq = &ipxintrq;
531 		break;
532 #endif
533 #ifdef INET6
534 	case ETHERTYPE_IPV6:
535 		schednetisr(NETISR_IPV6);
536 		inq = &ip6intrq;
537 		break;
538 #endif
539 #ifdef NS
540 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
541 		schednetisr(NETISR_NS);
542 		inq = &nsintrq;
543 		break;
544 
545 #endif /* NS */
546 #ifdef NETATALK
547         case ETHERTYPE_AT:
548                 schednetisr(NETISR_ATALK);
549                 inq = &atintrq1;
550                 break;
551         case ETHERTYPE_AARP:
552 		/* probably this should be done with a NETISR as well */
553                 aarpinput(IFP2AC(ifp), m); /* XXX */
554                 return;
555 #endif NETATALK
556 	default:
557 #ifdef IPX
558 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
559 			return;
560 #endif /* IPX */
561 #ifdef NS
562 		checksum = mtod(m, ushort *);
563 		/* Novell 802.3 */
564 		if ((ether_type <= ETHERMTU) &&
565 			((*checksum == 0xffff) || (*checksum == 0xE0E0))){
566 			if(*checksum == 0xE0E0) {
567 				m->m_pkthdr.len -= 3;
568 				m->m_len -= 3;
569 				m->m_data += 3;
570 			}
571 				schednetisr(NETISR_NS);
572 				inq = &nsintrq;
573 				break;
574 		}
575 #endif /* NS */
576 #if defined(NETATALK)
577 		if (ether_type > ETHERMTU)
578 			goto dropanyway;
579 		l = mtod(m, struct llc *);
580 		switch (l->llc_dsap) {
581 		case LLC_SNAP_LSAP:
582 		    switch (l->llc_control) {
583 		    case LLC_UI:
584 			if (l->llc_ssap != LLC_SNAP_LSAP)
585 			    goto dropanyway;
586 
587 			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
588 				   sizeof(at_org_code)) == 0 &&
589 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
590 			    inq = &atintrq2;
591 			    m_adj( m, sizeof( struct llc ));
592 			    schednetisr(NETISR_ATALK);
593 			    break;
594 			}
595 
596 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
597 				   sizeof(aarp_org_code)) == 0 &&
598 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
599 			    m_adj( m, sizeof( struct llc ));
600 			    aarpinput(IFP2AC(ifp), m); /* XXX */
601 			    return;
602 			}
603 
604 		    default:
605 			goto dropanyway;
606 		    }
607 		    break;
608 		dropanyway:
609 		default:
610 			if (ng_ether_input_orphan_p != NULL)
611 				(*ng_ether_input_orphan_p)(ifp, m, eh);
612 			else
613 				m_freem(m);
614 			return;
615 		}
616 #else /* NETATALK */
617 		if (ng_ether_input_orphan_p != NULL)
618 			(*ng_ether_input_orphan_p)(ifp, m, eh);
619 		else
620 			m_freem(m);
621 		return;
622 #endif /* NETATALK */
623 	}
624 
625 	(void) IF_HANDOFF(inq, m, NULL);
626 }
627 
628 /*
629  * Perform common duties while attaching to interface list
630  */
631 void
632 ether_ifattach(ifp, bpf)
633 	register struct ifnet *ifp;
634 	int bpf;
635 {
636 	register struct ifaddr *ifa;
637 	register struct sockaddr_dl *sdl;
638 
639 	if_attach(ifp);
640 	ifp->if_type = IFT_ETHER;
641 	ifp->if_addrlen = 6;
642 	ifp->if_hdrlen = 14;
643 	ifp->if_mtu = ETHERMTU;
644 	ifp->if_resolvemulti = ether_resolvemulti;
645 	if (ifp->if_baudrate == 0)
646 	    ifp->if_baudrate = 10000000;
647 	ifa = ifnet_addrs[ifp->if_index - 1];
648 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
649 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
650 	sdl->sdl_type = IFT_ETHER;
651 	sdl->sdl_alen = ifp->if_addrlen;
652 	bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
653 	if (bpf)
654 		bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
655 	if (ng_ether_attach_p != NULL)
656 		(*ng_ether_attach_p)(ifp);
657 #ifdef BRIDGE
658 	bdgtakeifaces();
659 #endif
660 }
661 
662 /*
663  * Perform common duties while detaching an Ethernet interface
664  */
665 void
666 ether_ifdetach(ifp, bpf)
667 	struct ifnet *ifp;
668 	int bpf;
669 {
670 	if (ng_ether_detach_p != NULL)
671 		(*ng_ether_detach_p)(ifp);
672 	if (bpf)
673 		bpfdetach(ifp);
674 	if_detach(ifp);
675 #ifdef BRIDGE
676 	bdgtakeifaces();
677 #endif
678 }
679 
680 SYSCTL_DECL(_net_link);
681 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
682 
683 int
684 ether_ioctl(ifp, command, data)
685 	struct ifnet *ifp;
686 	int command;
687 	caddr_t data;
688 {
689 	struct ifaddr *ifa = (struct ifaddr *) data;
690 	struct ifreq *ifr = (struct ifreq *) data;
691 	int error = 0;
692 
693 	switch (command) {
694 	case SIOCSIFADDR:
695 		ifp->if_flags |= IFF_UP;
696 
697 		switch (ifa->ifa_addr->sa_family) {
698 #ifdef INET
699 		case AF_INET:
700 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
701 			arp_ifinit(IFP2AC(ifp), ifa);
702 			break;
703 #endif
704 #ifdef IPX
705 		/*
706 		 * XXX - This code is probably wrong
707 		 */
708 		case AF_IPX:
709 			{
710 			register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
711 			struct arpcom *ac = IFP2AC(ifp);
712 
713 			if (ipx_nullhost(*ina))
714 				ina->x_host =
715 				    *(union ipx_host *)
716 			            ac->ac_enaddr;
717 			else {
718 				bcopy((caddr_t) ina->x_host.c_host,
719 				      (caddr_t) ac->ac_enaddr,
720 				      sizeof(ac->ac_enaddr));
721 			}
722 
723 			/*
724 			 * Set new address
725 			 */
726 			ifp->if_init(ifp->if_softc);
727 			break;
728 			}
729 #endif
730 #ifdef NS
731 		/*
732 		 * XXX - This code is probably wrong
733 		 */
734 		case AF_NS:
735 		{
736 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
737 			struct arpcom *ac = IFP2AC(ifp);
738 
739 			if (ns_nullhost(*ina))
740 				ina->x_host =
741 				    *(union ns_host *) (ac->ac_enaddr);
742 			else {
743 				bcopy((caddr_t) ina->x_host.c_host,
744 				      (caddr_t) ac->ac_enaddr,
745 				      sizeof(ac->ac_enaddr));
746 			}
747 
748 			/*
749 			 * Set new address
750 			 */
751 			ifp->if_init(ifp->if_softc);
752 			break;
753 		}
754 #endif
755 		default:
756 			ifp->if_init(ifp->if_softc);
757 			break;
758 		}
759 		break;
760 
761 	case SIOCGIFADDR:
762 		{
763 			struct sockaddr *sa;
764 
765 			sa = (struct sockaddr *) & ifr->ifr_data;
766 			bcopy(IFP2AC(ifp)->ac_enaddr,
767 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
768 		}
769 		break;
770 
771 	case SIOCSIFMTU:
772 		/*
773 		 * Set the interface MTU.
774 		 */
775 		if (ifr->ifr_mtu > ETHERMTU) {
776 			error = EINVAL;
777 		} else {
778 			ifp->if_mtu = ifr->ifr_mtu;
779 		}
780 		break;
781 	}
782 	return (error);
783 }
784 
785 int
786 ether_resolvemulti(ifp, llsa, sa)
787 	struct ifnet *ifp;
788 	struct sockaddr **llsa;
789 	struct sockaddr *sa;
790 {
791 	struct sockaddr_dl *sdl;
792 	struct sockaddr_in *sin;
793 #ifdef INET6
794 	struct sockaddr_in6 *sin6;
795 #endif
796 	u_char *e_addr;
797 
798 	switch(sa->sa_family) {
799 	case AF_LINK:
800 		/*
801 		 * No mapping needed. Just check that it's a valid MC address.
802 		 */
803 		sdl = (struct sockaddr_dl *)sa;
804 		e_addr = LLADDR(sdl);
805 		if ((e_addr[0] & 1) != 1)
806 			return EADDRNOTAVAIL;
807 		*llsa = 0;
808 		return 0;
809 
810 #ifdef INET
811 	case AF_INET:
812 		sin = (struct sockaddr_in *)sa;
813 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
814 			return EADDRNOTAVAIL;
815 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
816 		       M_WAITOK);
817 		sdl->sdl_len = sizeof *sdl;
818 		sdl->sdl_family = AF_LINK;
819 		sdl->sdl_index = ifp->if_index;
820 		sdl->sdl_type = IFT_ETHER;
821 		sdl->sdl_nlen = 0;
822 		sdl->sdl_alen = ETHER_ADDR_LEN;
823 		sdl->sdl_slen = 0;
824 		e_addr = LLADDR(sdl);
825 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
826 		*llsa = (struct sockaddr *)sdl;
827 		return 0;
828 #endif
829 #ifdef INET6
830 	case AF_INET6:
831 		sin6 = (struct sockaddr_in6 *)sa;
832 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
833 			/*
834 			 * An IP6 address of 0 means listen to all
835 			 * of the Ethernet multicast address used for IP6.
836 			 * (This is used for multicast routers.)
837 			 */
838 			ifp->if_flags |= IFF_ALLMULTI;
839 			*llsa = 0;
840 			return 0;
841 		}
842 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
843 			return EADDRNOTAVAIL;
844 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
845 		       M_WAITOK);
846 		sdl->sdl_len = sizeof *sdl;
847 		sdl->sdl_family = AF_LINK;
848 		sdl->sdl_index = ifp->if_index;
849 		sdl->sdl_type = IFT_ETHER;
850 		sdl->sdl_nlen = 0;
851 		sdl->sdl_alen = ETHER_ADDR_LEN;
852 		sdl->sdl_slen = 0;
853 		e_addr = LLADDR(sdl);
854 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
855 		*llsa = (struct sockaddr *)sdl;
856 		return 0;
857 #endif
858 
859 	default:
860 		/*
861 		 * Well, the text isn't quite right, but it's the name
862 		 * that counts...
863 		 */
864 		return EAFNOSUPPORT;
865 	}
866 }
867 
868