Deleted Added
full compact
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: head/sys/net/if_ethersubr.c 50477 1999-08-28 01:08:13Z peter $
34 * $FreeBSD: head/sys/net/if_ethersubr.c 52248 1999-10-15 05:07:00Z msmith $
35 */
36
37#include "opt_atalk.h"
38#include "opt_inet.h"
39#include "opt_ipx.h"
40#include "opt_bdg.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/malloc.h>
46#include <sys/mbuf.h>
47#include <sys/socket.h>
48#include <sys/sockio.h>
49#include <sys/sysctl.h>
50
51#include <net/if.h>
52#include <net/netisr.h>
53#include <net/route.h>
54#include <net/if_llc.h>
55#include <net/if_dl.h>
56#include <net/if_types.h>
57
58#ifdef INET
59#include <netinet/in.h>
60#include <netinet/in_var.h>
61#include <netinet/if_ether.h>
62#endif
63
64#ifdef IPX
65#include <netipx/ipx.h>
66#include <netipx/ipx_if.h>
67#endif
68
69#ifdef NS
70#include <netns/ns.h>
71#include <netns/ns_if.h>
72ushort ns_nettype;
73int ether_outputdebug = 0;
74int ether_inputdebug = 0;
75#endif
76
77#ifdef ISO
78#include <netiso/argo_debug.h>
79#include <netiso/iso.h>
80#include <netiso/iso_var.h>
81#include <netiso/iso_snpac.h>
82#endif
83
84/*#ifdef LLC
85#include <netccitt/dll.h>
86#include <netccitt/llc_var.h>
87#endif*/
88
89#if defined(LLC) && defined(CCITT)
90extern struct ifqueue pkintrq;
91#endif
92
93#ifdef NETATALK
94#include <netatalk/at.h>
95#include <netatalk/at_var.h>
96#include <netatalk/at_extern.h>
97
98#define llc_snap_org_code llc_un.type_snap.org_code
99#define llc_snap_ether_type llc_un.type_snap.ether_type
100
101extern u_char at_org_code[3];
102extern u_char aarp_org_code[3];
103#endif /* NETATALK */
104
105#ifdef BRIDGE
106#include <net/bridge.h>
107#endif
108
109#include "vlan.h"
110#if NVLAN > 0
111#include <net/if_vlan_var.h>
112#endif /* NVLAN > 0 */
113
114static int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
115 struct sockaddr *));
116u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
117#define senderr(e) do { error = (e); goto bad;} while (0)
118#define IFP2AC(IFP) ((struct arpcom *)IFP)
119
120/*
121 * Ethernet output routine.
122 * Encapsulate a packet of type family for the local net.
123 * Use trailer local net encapsulation if enough data in first
124 * packet leaves a multiple of 512 bytes of data in remainder.
125 * Assumes that ifp is actually pointer to arpcom structure.
126 */
127int
128ether_output(ifp, m0, dst, rt0)
129 register struct ifnet *ifp;
130 struct mbuf *m0;
131 struct sockaddr *dst;
132 struct rtentry *rt0;
133{
134 short type;
135 int s, error = 0;
136 u_char edst[6];
135 int s, error = 0, hdrcmplt = 0;
136 u_char esrc[6], edst[6];
137 register struct mbuf *m = m0;
138 register struct rtentry *rt;
139 register struct ether_header *eh;
140 int off, len = m->m_pkthdr.len, loop_copy = 0;
141 int hlen; /* link layer header lenght */
142 struct arpcom *ac = IFP2AC(ifp);
143
144 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
145 senderr(ENETDOWN);
146 rt = rt0;
147 if (rt) {
148 if ((rt->rt_flags & RTF_UP) == 0) {
149 rt0 = rt = rtalloc1(dst, 1, 0UL);
150 if (rt0)
151 rt->rt_refcnt--;
152 else
153 senderr(EHOSTUNREACH);
154 }
155 if (rt->rt_flags & RTF_GATEWAY) {
156 if (rt->rt_gwroute == 0)
157 goto lookup;
158 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
159 rtfree(rt); rt = rt0;
160 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
161 0UL);
162 if ((rt = rt->rt_gwroute) == 0)
163 senderr(EHOSTUNREACH);
164 }
165 }
166 if (rt->rt_flags & RTF_REJECT)
167 if (rt->rt_rmx.rmx_expire == 0 ||
168 time_second < rt->rt_rmx.rmx_expire)
169 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
170 }
171 hlen = ETHER_HDR_LEN;
172 switch (dst->sa_family) {
173#ifdef INET
174 case AF_INET:
175 if (!arpresolve(ac, rt, m, dst, edst, rt0))
176 return (0); /* if not yet resolved */
177 off = m->m_pkthdr.len - m->m_len;
178 type = htons(ETHERTYPE_IP);
179 break;
180#endif
181#ifdef IPX
182 case AF_IPX:
183 type = htons(ETHERTYPE_IPX);
184 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
185 (caddr_t)edst, sizeof (edst));
186 break;
187#endif
188#ifdef NETATALK
189 case AF_APPLETALK:
190 {
191 struct at_ifaddr *aa;
192
193 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
194 goto bad;
195 }
196 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
197 return (0);
198 /*
199 * In the phase 2 case, need to prepend an mbuf for the llc header.
200 * Since we must preserve the value of m, which is passed to us by
201 * value, we m_copy() the first mbuf, and use it for our llc header.
202 */
203 if ( aa->aa_flags & AFA_PHASE2 ) {
204 struct llc llc;
205
206 M_PREPEND(m, sizeof(struct llc), M_WAIT);
207 len += sizeof(struct llc);
208 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
209 llc.llc_control = LLC_UI;
210 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
211 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
212 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
213 type = htons(m->m_pkthdr.len);
214 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
215 } else {
216 type = htons(ETHERTYPE_AT);
217 }
218 break;
219 }
220#endif NETATALK
221#ifdef NS
222 case AF_NS:
223 switch(ns_nettype){
224 default:
225 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
226 type = 0x8137;
227 break;
228 case 0x0: /* Novell 802.3 */
229 type = htons( m->m_pkthdr.len);
230 break;
231 case 0xe0e0: /* Novell 802.2 and Token-Ring */
232 M_PREPEND(m, 3, M_WAIT);
233 type = htons( m->m_pkthdr.len);
234 cp = mtod(m, u_char *);
235 *cp++ = 0xE0;
236 *cp++ = 0xE0;
237 *cp++ = 0x03;
238 break;
239 }
240 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
241 (caddr_t)edst, sizeof (edst));
242 /*
243 * XXX if ns_thishost is the same as the node's ethernet
244 * address then just the default code will catch this anyhow.
245 * So I'm not sure if this next clause should be here at all?
246 * [JRE]
247 */
248 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
249 m->m_pkthdr.rcvif = ifp;
250 schednetisr(NETISR_NS);
251 inq = &nsintrq;
252 s = splimp();
253 if (IF_QFULL(inq)) {
254 IF_DROP(inq);
255 m_freem(m);
256 } else
257 IF_ENQUEUE(inq, m);
258 splx(s);
259 return (error);
260 }
261 if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
262 m->m_flags |= M_BCAST;
263 }
264 break;
265#endif /* NS */
266#ifdef ISO
267 case AF_ISO: {
268 int snpalen;
269 struct llc *l;
270 register struct sockaddr_dl *sdl;
271
272 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
273 sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
274 bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
275 } else if (error =
276 iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
277 (char *)edst, &snpalen))
278 goto bad; /* Not Resolved */
279 /* If broadcasting on a simplex interface, loopback a copy */
280 if (*edst & 1)
281 m->m_flags |= (M_BCAST|M_MCAST);
282 M_PREPEND(m, 3, M_DONTWAIT);
283 if (m == NULL)
284 return (0);
285 type = htons(m->m_pkthdr.len);
286 l = mtod(m, struct llc *);
287 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
288 l->llc_control = LLC_UI;
289 len += 3;
290 IFDEBUG(D_ETHER)
291 int i;
292 printf("unoutput: sending pkt to: ");
293 for (i=0; i<6; i++)
294 printf("%x ", edst[i] & 0xff);
295 printf("\n");
296 ENDDEBUG
297 } break;
298#endif /* ISO */
299#ifdef LLC
300/* case AF_NSAP: */
301 case AF_CCITT: {
302 register struct sockaddr_dl *sdl =
303 (struct sockaddr_dl *) rt -> rt_gateway;
304
305 if (sdl && sdl->sdl_family == AF_LINK
306 && sdl->sdl_alen > 0) {
307 bcopy(LLADDR(sdl), (char *)edst, sizeof(edst));
308 } else goto bad; /* Not a link interface ? Funny ... */
309 if (*edst & 1)
310 loop_copy = 1;
311 type = htons(m->m_pkthdr.len);
312#ifdef LLC_DEBUG
313 {
314 int i;
315 register struct llc *l = mtod(m, struct llc *);
316
317 printf("ether_output: sending LLC2 pkt to: ");
318 for (i=0; i<6; i++)
319 printf("%x ", edst[i] & 0xff);
320 printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
321 type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff,
322 l->llc_control & 0xff);
323
324 }
325#endif /* LLC_DEBUG */
326 } break;
327#endif /* LLC */
328
329 case pseudo_AF_HDRCMPLT:
330 hdrcmplt = 1;
331 eh = (struct ether_header *)dst->sa_data;
332 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
333 /* FALLTHROUGH */
334
335 case AF_UNSPEC:
336 loop_copy = -1; /* if this is for us, don't do it */
337 eh = (struct ether_header *)dst->sa_data;
338 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
339 type = eh->ether_type;
340 break;
341
342 default:
343 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
344 dst->sa_family);
345 senderr(EAFNOSUPPORT);
346 }
347
348 /*
349 * Add local net header. If no space in first mbuf,
350 * allocate another.
351 */
352 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
353 if (m == 0)
354 senderr(ENOBUFS);
355 eh = mtod(m, struct ether_header *);
356 (void)memcpy(&eh->ether_type, &type,
357 sizeof(eh->ether_type));
358 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
353 (void)memcpy(eh->ether_shost, ac->ac_enaddr,
354 sizeof(eh->ether_shost));
359 if (hdrcmplt)
360 (void)memcpy(eh->ether_shost, esrc,
361 sizeof(eh->ether_shost));
362 else
363 (void)memcpy(eh->ether_shost, ac->ac_enaddr,
364 sizeof(eh->ether_shost));
365
366 /*
367 * If a simplex interface, and the packet is being sent to our
368 * Ethernet address or a broadcast address, loopback a copy.
369 * XXX To make a simplex device behave exactly like a duplex
370 * device, we should copy in the case of sending to our own
371 * ethernet address (thus letting the original actually appear
372 * on the wire). However, we don't do that here for security
373 * reasons and compatibility with the original behavior.
374 */
375 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
376 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
377 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
378
379 (void) if_simloop(ifp, n, dst, hlen);
380 } else if (bcmp(eh->ether_dhost,
381 eh->ether_shost, ETHER_ADDR_LEN) == 0) {
382 (void) if_simloop(ifp, m, dst, hlen);
383 return (0); /* XXX */
384 }
385 }
386#ifdef BRIDGE
387 if (do_bridge) {
388 struct mbuf *m0 = m ;
389
390 if (m->m_pkthdr.rcvif)
391 m->m_pkthdr.rcvif = NULL ;
392 ifp = bridge_dst_lookup(m);
393 bdg_forward(&m0, ifp);
394 if (m0)
395 m_freem(m0);
396 return (0);
397 }
398#endif
399 s = splimp();
400 /*
401 * Queue message on interface, and start output if interface
402 * not yet active.
403 */
404 if (IF_QFULL(&ifp->if_snd)) {
405 IF_DROP(&ifp->if_snd);
406 splx(s);
407 senderr(ENOBUFS);
408 }
409 IF_ENQUEUE(&ifp->if_snd, m);
410 if ((ifp->if_flags & IFF_OACTIVE) == 0)
411 (*ifp->if_start)(ifp);
412 splx(s);
413 ifp->if_obytes += len + sizeof (struct ether_header);
414 if (m->m_flags & M_MCAST)
415 ifp->if_omcasts++;
416 return (error);
417
418bad:
419 if (m)
420 m_freem(m);
421 return (error);
422}
423
424/*
425 * Process a received Ethernet packet;
426 * the packet is in the mbuf chain m without
427 * the ether header, which is provided separately.
428 */
429void
430ether_input(ifp, eh, m)
431 struct ifnet *ifp;
432 register struct ether_header *eh;
433 struct mbuf *m;
434{
435 register struct ifqueue *inq;
436 u_short ether_type;
437 int s;
438#if defined (ISO) || defined (LLC) || defined(NETATALK)
439 register struct llc *l;
440#endif
441
442 if ((ifp->if_flags & IFF_UP) == 0) {
443 m_freem(m);
444 return;
445 }
446 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
447 if (eh->ether_dhost[0] & 1) {
448 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
449 sizeof(etherbroadcastaddr)) == 0)
450 m->m_flags |= M_BCAST;
451 else
452 m->m_flags |= M_MCAST;
453 }
454 if (m->m_flags & (M_BCAST|M_MCAST))
455 ifp->if_imcasts++;
456
457 ether_type = ntohs(eh->ether_type);
458
459#if NVLAN > 0
460 if (ether_type == vlan_proto) {
461 if (vlan_input(eh, m) < 0)
462 ifp->if_data.ifi_noproto++;
463 return;
464 }
465#endif /* NVLAN > 0 */
466
467 switch (ether_type) {
468#ifdef INET
469 case ETHERTYPE_IP:
470 if (ipflow_fastforward(m))
471 return;
472 schednetisr(NETISR_IP);
473 inq = &ipintrq;
474 break;
475
476 case ETHERTYPE_ARP:
477 schednetisr(NETISR_ARP);
478 inq = &arpintrq;
479 break;
480#endif
481#ifdef IPX
482 case ETHERTYPE_IPX:
483 schednetisr(NETISR_IPX);
484 inq = &ipxintrq;
485 break;
486#endif
487#ifdef NS
488 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
489 schednetisr(NETISR_NS);
490 inq = &nsintrq;
491 break;
492
493#endif /* NS */
494#ifdef NETATALK
495 case ETHERTYPE_AT:
496 schednetisr(NETISR_ATALK);
497 inq = &atintrq1;
498 break;
499 case ETHERTYPE_AARP:
500 /* probably this should be done with a NETISR as well */
501 aarpinput(IFP2AC(ifp), m); /* XXX */
502 return;
503#endif NETATALK
504 default:
505#ifdef NS
506 checksum = mtod(m, ushort *);
507 /* Novell 802.3 */
508 if ((ether_type <= ETHERMTU) &&
509 ((*checksum == 0xffff) || (*checksum == 0xE0E0))){
510 if(*checksum == 0xE0E0) {
511 m->m_pkthdr.len -= 3;
512 m->m_len -= 3;
513 m->m_data += 3;
514 }
515 schednetisr(NETISR_NS);
516 inq = &nsintrq;
517 break;
518 }
519#endif /* NS */
520#if defined (ISO) || defined (LLC) || defined(NETATALK)
521 if (ether_type > ETHERMTU)
522 goto dropanyway;
523 l = mtod(m, struct llc *);
524 switch (l->llc_dsap) {
525#ifdef NETATALK
526 case LLC_SNAP_LSAP:
527 switch (l->llc_control) {
528 case LLC_UI:
529 if (l->llc_ssap != LLC_SNAP_LSAP)
530 goto dropanyway;
531
532 if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
533 sizeof(at_org_code)) == 0 &&
534 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
535 inq = &atintrq2;
536 m_adj( m, sizeof( struct llc ));
537 schednetisr(NETISR_ATALK);
538 break;
539 }
540
541 if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
542 sizeof(aarp_org_code)) == 0 &&
543 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
544 m_adj( m, sizeof( struct llc ));
545 aarpinput(IFP2AC(ifp), m); /* XXX */
546 return;
547 }
548
549 default:
550 goto dropanyway;
551 }
552 break;
553#endif NETATALK
554#ifdef ISO
555 case LLC_ISO_LSAP:
556 switch (l->llc_control) {
557 case LLC_UI:
558 /* LLC_UI_P forbidden in class 1 service */
559 if ((l->llc_dsap == LLC_ISO_LSAP) &&
560 (l->llc_ssap == LLC_ISO_LSAP)) {
561 /* LSAP for ISO */
562 if (m->m_pkthdr.len > ether_type)
563 m_adj(m, ether_type - m->m_pkthdr.len);
564 m->m_data += 3; /* XXX */
565 m->m_len -= 3; /* XXX */
566 m->m_pkthdr.len -= 3; /* XXX */
567 M_PREPEND(m, sizeof *eh, M_DONTWAIT);
568 if (m == 0)
569 return;
570 *mtod(m, struct ether_header *) = *eh;
571 IFDEBUG(D_ETHER)
572 printf("clnp packet");
573 ENDDEBUG
574 schednetisr(NETISR_ISO);
575 inq = &clnlintrq;
576 break;
577 }
578 goto dropanyway;
579
580 case LLC_XID:
581 case LLC_XID_P:
582 if(m->m_len < 6)
583 goto dropanyway;
584 l->llc_window = 0;
585 l->llc_fid = 9;
586 l->llc_class = 1;
587 l->llc_dsap = l->llc_ssap = 0;
588 /* Fall through to */
589 case LLC_TEST:
590 case LLC_TEST_P:
591 {
592 struct sockaddr sa;
593 register struct ether_header *eh2;
594 int i;
595 u_char c = l->llc_dsap;
596
597 l->llc_dsap = l->llc_ssap;
598 l->llc_ssap = c;
599 if (m->m_flags & (M_BCAST | M_MCAST))
600 bcopy((caddr_t)ac->ac_enaddr,
601 (caddr_t)eh->ether_dhost, 6);
602 sa.sa_family = AF_UNSPEC;
603 sa.sa_len = sizeof(sa);
604 eh2 = (struct ether_header *)sa.sa_data;
605 for (i = 0; i < 6; i++) {
606 eh2->ether_shost[i] = c = eh->ether_dhost[i];
607 eh2->ether_dhost[i] =
608 eh->ether_dhost[i] = eh->ether_shost[i];
609 eh->ether_shost[i] = c;
610 }
611 ifp->if_output(ifp, m, &sa, NULL);
612 return;
613 }
614 default:
615 m_freem(m);
616 return;
617 }
618 break;
619#endif /* ISO */
620#ifdef LLC
621 case LLC_X25_LSAP:
622 {
623 if (m->m_pkthdr.len > ether_type)
624 m_adj(m, ether_type - m->m_pkthdr.len);
625 M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
626 if (m == 0)
627 return;
628 if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
629 eh->ether_dhost, LLC_X25_LSAP, 6,
630 mtod(m, struct sdl_hdr *)))
631 panic("ETHER cons addr failure");
632 mtod(m, struct sdl_hdr *)->sdlhdr_len = ether_type;
633#ifdef LLC_DEBUG
634 printf("llc packet\n");
635#endif /* LLC_DEBUG */
636 schednetisr(NETISR_CCITT);
637 inq = &llcintrq;
638 break;
639 }
640#endif /* LLC */
641 dropanyway:
642 default:
643 m_freem(m);
644 return;
645 }
646#else /* ISO || LLC || NETATALK */
647 m_freem(m);
648 return;
649#endif /* ISO || LLC || NETATALK */
650 }
651
652 s = splimp();
653 if (IF_QFULL(inq)) {
654 IF_DROP(inq);
655 m_freem(m);
656 } else
657 IF_ENQUEUE(inq, m);
658 splx(s);
659}
660
661/*
662 * Perform common duties while attaching to interface list
663 */
664void
665ether_ifattach(ifp)
666 register struct ifnet *ifp;
667{
668 register struct ifaddr *ifa;
669 register struct sockaddr_dl *sdl;
670
671 ifp->if_type = IFT_ETHER;
672 ifp->if_addrlen = 6;
673 ifp->if_hdrlen = 14;
674 ifp->if_mtu = ETHERMTU;
675 ifp->if_resolvemulti = ether_resolvemulti;
676 if (ifp->if_baudrate == 0)
677 ifp->if_baudrate = 10000000;
678 ifa = ifnet_addrs[ifp->if_index - 1];
679 if (ifa == 0) {
680 printf("ether_ifattach: no lladdr!\n");
681 return;
682 }
683 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
684 sdl->sdl_type = IFT_ETHER;
685 sdl->sdl_alen = ifp->if_addrlen;
686 bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
687#ifdef NETGRAPH
688 ngether_init(ifp);
689#endif /* NETGRAPH */
690}
691
692SYSCTL_DECL(_net_link);
693SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
694
695int
696ether_ioctl(ifp, command, data)
697 struct ifnet *ifp;
698 int command;
699 caddr_t data;
700{
701 struct ifaddr *ifa = (struct ifaddr *) data;
702 struct ifreq *ifr = (struct ifreq *) data;
703 int error = 0;
704
705 switch (command) {
706 case SIOCSIFADDR:
707 ifp->if_flags |= IFF_UP;
708
709 switch (ifa->ifa_addr->sa_family) {
710#ifdef INET
711 case AF_INET:
712 ifp->if_init(ifp->if_softc); /* before arpwhohas */
713 arp_ifinit(IFP2AC(ifp), ifa);
714 break;
715#endif
716#ifdef IPX
717 /*
718 * XXX - This code is probably wrong
719 */
720 case AF_IPX:
721 {
722 register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
723 struct arpcom *ac = IFP2AC(ifp);
724
725 if (ipx_nullhost(*ina))
726 ina->x_host =
727 *(union ipx_host *)
728 ac->ac_enaddr;
729 else {
730 bcopy((caddr_t) ina->x_host.c_host,
731 (caddr_t) ac->ac_enaddr,
732 sizeof(ac->ac_enaddr));
733 }
734
735 /*
736 * Set new address
737 */
738 ifp->if_init(ifp->if_softc);
739 break;
740 }
741#endif
742#ifdef NS
743 /*
744 * XXX - This code is probably wrong
745 */
746 case AF_NS:
747 {
748 register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
749 struct arpcom *ac = IFP2AC(ifp);
750
751 if (ns_nullhost(*ina))
752 ina->x_host =
753 *(union ns_host *) (ac->ac_enaddr);
754 else {
755 bcopy((caddr_t) ina->x_host.c_host,
756 (caddr_t) ac->ac_enaddr,
757 sizeof(ac->ac_enaddr));
758 }
759
760 /*
761 * Set new address
762 */
763 ifp->if_init(ifp->if_softc);
764 break;
765 }
766#endif
767 default:
768 ifp->if_init(ifp->if_softc);
769 break;
770 }
771 break;
772
773 case SIOCGIFADDR:
774 {
775 struct sockaddr *sa;
776
777 sa = (struct sockaddr *) & ifr->ifr_data;
778 bcopy(IFP2AC(ifp)->ac_enaddr,
779 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
780 }
781 break;
782
783 case SIOCSIFMTU:
784 /*
785 * Set the interface MTU.
786 */
787 if (ifr->ifr_mtu > ETHERMTU) {
788 error = EINVAL;
789 } else {
790 ifp->if_mtu = ifr->ifr_mtu;
791 }
792 break;
793 }
794 return (error);
795}
796
797int
798ether_resolvemulti(ifp, llsa, sa)
799 struct ifnet *ifp;
800 struct sockaddr **llsa;
801 struct sockaddr *sa;
802{
803 struct sockaddr_dl *sdl;
804 struct sockaddr_in *sin;
805 u_char *e_addr;
806
807 switch(sa->sa_family) {
808 case AF_LINK:
809 /*
810 * No mapping needed. Just check that it's a valid MC address.
811 */
812 sdl = (struct sockaddr_dl *)sa;
813 e_addr = LLADDR(sdl);
814 if ((e_addr[0] & 1) != 1)
815 return EADDRNOTAVAIL;
816 *llsa = 0;
817 return 0;
818
819#ifdef INET
820 case AF_INET:
821 sin = (struct sockaddr_in *)sa;
822 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
823 return EADDRNOTAVAIL;
824 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
825 M_WAITOK);
826 sdl->sdl_len = sizeof *sdl;
827 sdl->sdl_family = AF_LINK;
828 sdl->sdl_index = ifp->if_index;
829 sdl->sdl_type = IFT_ETHER;
830 sdl->sdl_nlen = 0;
831 sdl->sdl_alen = ETHER_ADDR_LEN;
832 sdl->sdl_slen = 0;
833 e_addr = LLADDR(sdl);
834 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
835 *llsa = (struct sockaddr *)sdl;
836 return 0;
837#endif
838
839 default:
840 /*
841 * Well, the text isn't quite right, but it's the name
842 * that counts...
843 */
844 return EAFNOSUPPORT;
845 }
846}