if_fddisubr.c revision 127836
1/*
2 * Copyright (c) 1995, 1996
3 *	Matt Thomas <matt@3am-software.com>.  All rights reserved.
4 * Copyright (c) 1982, 1989, 1993
5 *	The Regents of the University of California.  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. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by the University of
18 *	California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *	from: if_ethersubr.c,v 1.5 1994/12/13 22:31:45 wollman Exp
36 * $FreeBSD: head/sys/net/if_fddisubr.c 127836 2004-04-04 11:52:09Z luigi $
37 */
38
39#include "opt_atalk.h"
40#include "opt_inet.h"
41#include "opt_inet6.h"
42#include "opt_ipx.h"
43#include "opt_mac.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/mac.h>
49#include <sys/malloc.h>
50#include <sys/mbuf.h>
51#include <sys/module.h>
52#include <sys/socket.h>
53#include <sys/sockio.h>
54
55#include <net/if.h>
56#include <net/if_dl.h>
57#include <net/if_llc.h>
58#include <net/if_types.h>
59
60#include <net/netisr.h>
61#include <net/route.h>
62#include <net/bpf.h>
63#include <net/fddi.h>
64
65#if defined(INET) || defined(INET6)
66#include <netinet/in.h>
67#include <netinet/in_var.h>
68#include <netinet/if_ether.h>
69#endif
70#ifdef INET6
71#include <netinet6/nd6.h>
72#endif
73
74#ifdef IPX
75#include <netipx/ipx.h>
76#include <netipx/ipx_if.h>
77#endif
78
79#ifdef DECNET
80#include <netdnet/dn.h>
81#endif
82
83#ifdef NETATALK
84#include <netatalk/at.h>
85#include <netatalk/at_var.h>
86#include <netatalk/at_extern.h>
87
88extern u_char	at_org_code[ 3 ];
89extern u_char	aarp_org_code[ 3 ];
90#endif /* NETATALK */
91
92static const u_char fddibroadcastaddr[FDDI_ADDR_LEN] =
93			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
94
95static int fddi_resolvemulti(struct ifnet *, struct sockaddr **,
96			      struct sockaddr *);
97static int fddi_output(struct ifnet *, struct mbuf *, struct sockaddr *,
98		       struct rtentry *);
99static void fddi_input(struct ifnet *ifp, struct mbuf *m);
100
101#define	IFP2AC(IFP)	((struct arpcom *)IFP)
102#define	senderr(e)	do { error = (e); goto bad; } while (0)
103
104/*
105 * FDDI output routine.
106 * Encapsulate a packet of type family for the local net.
107 * Use trailer local net encapsulation if enough data in first
108 * packet leaves a multiple of 512 bytes of data in remainder.
109 * Assumes that ifp is actually pointer to arpcom structure.
110 */
111static int
112fddi_output(ifp, m, dst, rt0)
113	struct ifnet *ifp;
114	struct mbuf *m;
115	struct sockaddr *dst;
116	struct rtentry *rt0;
117{
118	u_int16_t type;
119	int loop_copy = 0, error = 0, hdrcmplt = 0;
120 	u_char esrc[FDDI_ADDR_LEN], edst[FDDI_ADDR_LEN];
121	struct rtentry *rt;
122	struct fddi_header *fh;
123	struct arpcom *ac = IFP2AC(ifp);
124
125#ifdef MAC
126	error = mac_check_ifnet_transmit(ifp, m);
127	if (error)
128		senderr(error);
129#endif
130
131	if (ifp->if_flags & IFF_MONITOR)
132		senderr(ENETDOWN);
133	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
134		senderr(ENETDOWN);
135	getmicrotime(&ifp->if_lastchange);
136
137	error = rt_check(&rt, &rt0, dst);
138	if (error)
139		goto bad;
140
141	switch (dst->sa_family) {
142#ifdef INET
143	case AF_INET: {
144		if (!arpresolve(ifp, rt, m, dst, edst))
145			return (0);	/* if not yet resolved */
146		type = htons(ETHERTYPE_IP);
147		break;
148	}
149	case AF_ARP:
150	{
151		struct arphdr *ah;
152		ah = mtod(m, struct arphdr *);
153		ah->ar_hrd = htons(ARPHRD_ETHER);
154
155		loop_copy = -1; /* if this is for us, don't do it */
156
157		switch (ntohs(ah->ar_op)) {
158		case ARPOP_REVREQUEST:
159		case ARPOP_REVREPLY:
160			type = htons(ETHERTYPE_REVARP);
161			break;
162		case ARPOP_REQUEST:
163		case ARPOP_REPLY:
164		default:
165			type = htons(ETHERTYPE_ARP);
166			break;
167		}
168
169		if (m->m_flags & M_BCAST)
170			bcopy(ifp->if_broadcastaddr, edst, FDDI_ADDR_LEN);
171                else
172			bcopy(ar_tha(ah), edst, FDDI_ADDR_LEN);
173
174	}
175	break;
176#endif /* INET */
177#ifdef INET6
178	case AF_INET6:
179		if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst)) {
180			/* Something bad happened */
181			return (0);
182		}
183		type = htons(ETHERTYPE_IPV6);
184		break;
185#endif /* INET6 */
186#ifdef IPX
187	case AF_IPX:
188		type = htons(ETHERTYPE_IPX);
189 		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
190		    (caddr_t)edst, FDDI_ADDR_LEN);
191		break;
192#endif /* IPX */
193#ifdef NETATALK
194	case AF_APPLETALK: {
195	    struct at_ifaddr *aa;
196            if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
197                return (0);
198	    /*
199	     * ifaddr is the first thing in at_ifaddr
200	     */
201	    if ((aa = at_ifawithnet( (struct sockaddr_at *)dst)) == 0)
202		goto bad;
203
204	    /*
205	     * In the phase 2 case, we need to prepend an mbuf for the llc header.
206	     * Since we must preserve the value of m, which is passed to us by
207	     * value, we m_copy() the first mbuf, and use it for our llc header.
208	     */
209	    if (aa->aa_flags & AFA_PHASE2) {
210		struct llc llc;
211
212		M_PREPEND(m, LLC_SNAPFRAMELEN, M_TRYWAIT);
213		if (m == 0)
214			senderr(ENOBUFS);
215		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
216		llc.llc_control = LLC_UI;
217		bcopy(at_org_code, llc.llc_snap.org_code, sizeof(at_org_code));
218		llc.llc_snap.ether_type = htons(ETHERTYPE_AT);
219		bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
220		type = 0;
221	    } else {
222		type = htons(ETHERTYPE_AT);
223	    }
224	    break;
225	}
226#endif /* NETATALK */
227
228	case pseudo_AF_HDRCMPLT:
229	{
230		struct ether_header *eh;
231		hdrcmplt = 1;
232		eh = (struct ether_header *)dst->sa_data;
233		bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, FDDI_ADDR_LEN);
234		/* FALLTHROUGH */
235	}
236
237	case AF_UNSPEC:
238	{
239		struct ether_header *eh;
240		loop_copy = -1;
241		eh = (struct ether_header *)dst->sa_data;
242		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, FDDI_ADDR_LEN);
243		if (*edst & 1)
244			m->m_flags |= (M_BCAST|M_MCAST);
245		type = eh->ether_type;
246		break;
247	}
248
249	case AF_IMPLINK:
250	{
251		fh = mtod(m, struct fddi_header *);
252		error = EPROTONOSUPPORT;
253		switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
254			case FDDIFC_LLC_ASYNC: {
255				/* legal priorities are 0 through 7 */
256				if ((fh->fddi_fc & FDDIFC_Z) > 7)
257			        	goto bad;
258				break;
259			}
260			case FDDIFC_LLC_SYNC: {
261				/* FDDIFC_Z bits reserved, must be zero */
262				if (fh->fddi_fc & FDDIFC_Z)
263					goto bad;
264				break;
265			}
266			case FDDIFC_SMT: {
267				/* FDDIFC_Z bits must be non zero */
268				if ((fh->fddi_fc & FDDIFC_Z) == 0)
269					goto bad;
270				break;
271			}
272			default: {
273				/* anything else is too dangerous */
274               	 		goto bad;
275			}
276		}
277		error = 0;
278		if (fh->fddi_dhost[0] & 1)
279			m->m_flags |= (M_BCAST|M_MCAST);
280		goto queue_it;
281	}
282	default:
283		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
284		senderr(EAFNOSUPPORT);
285	}
286
287	/*
288	 * Add LLC header.
289	 */
290	if (type != 0) {
291		struct llc *l;
292		M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
293		if (m == 0)
294			senderr(ENOBUFS);
295		l = mtod(m, struct llc *);
296		l->llc_control = LLC_UI;
297		l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
298		l->llc_snap.org_code[0] =
299			l->llc_snap.org_code[1] =
300			l->llc_snap.org_code[2] = 0;
301		l->llc_snap.ether_type = htons(type);
302	}
303
304	/*
305	 * Add local net header.  If no space in first mbuf,
306	 * allocate another.
307	 */
308	M_PREPEND(m, FDDI_HDR_LEN, M_DONTWAIT);
309	if (m == 0)
310		senderr(ENOBUFS);
311	fh = mtod(m, struct fddi_header *);
312	fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
313	bcopy((caddr_t)edst, (caddr_t)fh->fddi_dhost, FDDI_ADDR_LEN);
314  queue_it:
315	if (hdrcmplt)
316		bcopy((caddr_t)esrc, (caddr_t)fh->fddi_shost, FDDI_ADDR_LEN);
317	else
318		bcopy((caddr_t)ac->ac_enaddr, (caddr_t)fh->fddi_shost,
319			FDDI_ADDR_LEN);
320
321	/*
322	 * If a simplex interface, and the packet is being sent to our
323	 * Ethernet address or a broadcast address, loopback a copy.
324	 * XXX To make a simplex device behave exactly like a duplex
325	 * device, we should copy in the case of sending to our own
326	 * ethernet address (thus letting the original actually appear
327	 * on the wire). However, we don't do that here for security
328	 * reasons and compatibility with the original behavior.
329	 */
330	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
331		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
332			struct mbuf *n;
333			n = m_copy(m, 0, (int)M_COPYALL);
334			(void) if_simloop(ifp, n, dst->sa_family,
335					  FDDI_HDR_LEN);
336	     	} else if (bcmp(fh->fddi_dhost, fh->fddi_shost,
337				FDDI_ADDR_LEN) == 0) {
338			(void) if_simloop(ifp, m, dst->sa_family,
339					  FDDI_HDR_LEN);
340			return (0);	/* XXX */
341		}
342	}
343
344	if (! IF_HANDOFF(&ifp->if_snd, m, ifp))
345		senderr(ENOBUFS);
346	return (error);
347
348bad:
349	ifp->if_oerrors++;
350	if (m)
351		m_freem(m);
352	return (error);
353}
354
355/*
356 * Process a received FDDI packet.
357 */
358static void
359fddi_input(ifp, m)
360	struct ifnet *ifp;
361	struct mbuf *m;
362{
363	int isr;
364	struct llc *l;
365	struct fddi_header *fh;
366
367	/*
368	 * Do consistency checks to verify assumptions
369	 * made by code past this point.
370	 */
371	if ((m->m_flags & M_PKTHDR) == 0) {
372		if_printf(ifp, "discard frame w/o packet header\n");
373		ifp->if_ierrors++;
374		m_freem(m);
375		return;
376	}
377	if (m->m_pkthdr.rcvif == NULL) {
378		if_printf(ifp, "discard frame w/o interface pointer\n");
379		ifp->if_ierrors++;
380		m_freem(m);
381		return;
382        }
383
384	m = m_pullup(m, FDDI_HDR_LEN);
385	if (m == NULL) {
386		ifp->if_ierrors++;
387		goto dropanyway;
388	}
389	fh = mtod(m, struct fddi_header *);
390	m->m_pkthdr.header = (void *)fh;
391
392	/*
393	 * Discard packet if interface is not up.
394	 */
395	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
396		goto dropanyway;
397
398	/*
399	 * Give bpf a chance at the packet.
400	 */
401	BPF_MTAP(ifp, m);
402
403	/*
404	 * Interface marked for monitoring; discard packet.
405	 */
406	if (ifp->if_flags & IFF_MONITOR) {
407		m_freem(m);
408		return;
409	}
410
411#ifdef MAC
412	mac_create_mbuf_from_ifnet(ifp, m);
413#endif
414
415	/*
416	 * Update interface statistics.
417	 */
418	ifp->if_ibytes += m->m_pkthdr.len;
419	getmicrotime(&ifp->if_lastchange);
420
421	/*
422	 * Discard non local unicast packets when interface
423	 * is in promiscuous mode.
424	 */
425	if ((ifp->if_flags & IFF_PROMISC) && ((fh->fddi_dhost[0] & 1) == 0) &&
426	    (bcmp(IFP2AC(ifp)->ac_enaddr, (caddr_t)fh->fddi_dhost,
427	     FDDI_ADDR_LEN) != 0))
428		goto dropanyway;
429
430	/*
431	 * Set mbuf flags for bcast/mcast.
432	 */
433	if (fh->fddi_dhost[0] & 1) {
434		if (bcmp(ifp->if_broadcastaddr, fh->fddi_dhost,
435		    FDDI_ADDR_LEN) == 0)
436			m->m_flags |= M_BCAST;
437		else
438			m->m_flags |= M_MCAST;
439		ifp->if_imcasts++;
440	}
441
442#ifdef M_LINK0
443	/*
444	 * If this has a LLC priority of 0, then mark it so upper
445	 * layers have a hint that it really came via a FDDI/Ethernet
446	 * bridge.
447	 */
448	if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
449		m->m_flags |= M_LINK0;
450#endif
451
452	/* Strip off FDDI header. */
453	m_adj(m, FDDI_HDR_LEN);
454
455	m = m_pullup(m, LLC_SNAPFRAMELEN);
456	if (m == 0) {
457		ifp->if_ierrors++;
458		goto dropanyway;
459	}
460	l = mtod(m, struct llc *);
461
462	switch (l->llc_dsap) {
463	case LLC_SNAP_LSAP:
464	{
465		u_int16_t type;
466		if ((l->llc_control != LLC_UI) ||
467		    (l->llc_ssap != LLC_SNAP_LSAP)) {
468			ifp->if_noproto++;
469			goto dropanyway;
470		}
471#ifdef NETATALK
472		if (Bcmp(&(l->llc_snap.org_code)[0], at_org_code,
473		    sizeof(at_org_code)) == 0 &&
474		    ntohs(l->llc_snap.ether_type) == ETHERTYPE_AT) {
475			isr = NETISR_ATALK2;
476			m_adj(m, LLC_SNAPFRAMELEN);
477			break;
478		}
479
480		if (Bcmp(&(l->llc_snap.org_code)[0], aarp_org_code,
481		    sizeof(aarp_org_code)) == 0 &&
482		    ntohs(l->llc_snap.ether_type) == ETHERTYPE_AARP) {
483			m_adj(m, LLC_SNAPFRAMELEN);
484			isr = NETISR_AARP;
485			break;
486		}
487#endif /* NETATALK */
488		if (l->llc_snap.org_code[0] != 0 ||
489		    l->llc_snap.org_code[1] != 0 ||
490		    l->llc_snap.org_code[2] != 0) {
491			ifp->if_noproto++;
492			goto dropanyway;
493		}
494
495		type = ntohs(l->llc_snap.ether_type);
496		m_adj(m, LLC_SNAPFRAMELEN);
497
498		switch (type) {
499#ifdef INET
500		case ETHERTYPE_IP:
501			if (ip_fastforward(m))
502				return;
503			isr = NETISR_IP;
504			break;
505
506		case ETHERTYPE_ARP:
507			if (ifp->if_flags & IFF_NOARP)
508				goto dropanyway;
509			isr = NETISR_ARP;
510			break;
511#endif
512#ifdef INET6
513		case ETHERTYPE_IPV6:
514			isr = NETISR_IPV6;
515			break;
516#endif
517#ifdef IPX
518		case ETHERTYPE_IPX:
519			isr = NETISR_IPX;
520			break;
521#endif
522#ifdef DECNET
523		case ETHERTYPE_DECNET:
524			isr = NETISR_DECNET;
525			break;
526#endif
527#ifdef NETATALK
528		case ETHERTYPE_AT:
529	                isr = NETISR_ATALK1;
530			break;
531	        case ETHERTYPE_AARP:
532			isr = NETISR_AARP;
533			break;
534#endif /* NETATALK */
535		default:
536			/* printf("fddi_input: unknown protocol 0x%x\n", type); */
537			ifp->if_noproto++;
538			goto dropanyway;
539		}
540		break;
541	}
542
543	default:
544		/* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
545		ifp->if_noproto++;
546		goto dropanyway;
547	}
548	netisr_dispatch(isr, m);
549	return;
550
551dropanyway:
552	ifp->if_iqdrops++;
553	if (m)
554		m_freem(m);
555	return;
556}
557
558/*
559 * Perform common duties while attaching to interface list
560 */
561void
562fddi_ifattach(ifp, bpf)
563	struct ifnet *ifp;
564	int bpf;
565{
566	struct ifaddr *ifa;
567	struct sockaddr_dl *sdl;
568
569	ifp->if_type = IFT_FDDI;
570	ifp->if_addrlen = FDDI_ADDR_LEN;
571	ifp->if_hdrlen = 21;
572
573	if_attach(ifp);         /* Must be called before additional assignments */
574
575	ifp->if_mtu = FDDIMTU;
576	ifp->if_output = fddi_output;
577	ifp->if_input = fddi_input;
578	ifp->if_resolvemulti = fddi_resolvemulti;
579	ifp->if_broadcastaddr = fddibroadcastaddr;
580	ifp->if_baudrate = 100000000;
581#ifdef IFF_NOTRAILERS
582	ifp->if_flags |= IFF_NOTRAILERS;
583#endif
584	ifa = ifaddr_byindex(ifp->if_index);
585	if (ifa == NULL) {
586		if_printf(ifp, "%s() no lladdr!\n", __func__);
587		return;
588	}
589
590	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
591	sdl->sdl_type = IFT_FDDI;
592	sdl->sdl_alen = ifp->if_addrlen;
593	bcopy(IFP2AC(ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
594
595	if (bpf)
596		bpfattach(ifp, DLT_FDDI, FDDI_HDR_LEN);
597
598	return;
599}
600
601void
602fddi_ifdetach(ifp, bpf)
603	struct ifnet *ifp;
604	int bpf;
605{
606
607	if (bpf)
608		bpfdetach(ifp);
609
610	if_detach(ifp);
611
612	return;
613}
614
615int
616fddi_ioctl (ifp, command, data)
617	struct ifnet *ifp;
618	int command;
619	caddr_t data;
620{
621	struct ifaddr *ifa;
622	struct ifreq *ifr;
623	int error;
624
625	ifa = (struct ifaddr *) data;
626	ifr = (struct ifreq *) data;
627	error = 0;
628
629	switch (command) {
630	case SIOCSIFADDR:
631		ifp->if_flags |= IFF_UP;
632
633		switch (ifa->ifa_addr->sa_family) {
634#ifdef INET
635		case AF_INET:	/* before arpwhohas */
636			ifp->if_init(ifp->if_softc);
637			arp_ifinit(ifp, ifa);
638			break;
639#endif
640#ifdef IPX
641		/*
642		 * XXX - This code is probably wrong
643		 */
644		case AF_IPX: {
645				struct ipx_addr *ina;
646				struct arpcom *ac;
647
648				ina = &(IA_SIPX(ifa)->sipx_addr);
649				ac = IFP2AC(ifp);
650
651				if (ipx_nullhost(*ina)) {
652					ina->x_host = *(union ipx_host *)
653							ac->ac_enaddr;
654				} else {
655					bcopy((caddr_t) ina->x_host.c_host,
656					      (caddr_t) ac->ac_enaddr,
657					      sizeof(ac->ac_enaddr));
658				}
659
660				/*
661				 * Set new address
662				 */
663				ifp->if_init(ifp->if_softc);
664			}
665			break;
666#endif
667		default:
668			ifp->if_init(ifp->if_softc);
669			break;
670		}
671	case SIOCGIFADDR: {
672			struct sockaddr *sa;
673
674			sa = (struct sockaddr *) & ifr->ifr_data;
675			bcopy(IFP2AC(ifp)->ac_enaddr,
676			      (caddr_t) sa->sa_data, FDDI_ADDR_LEN);
677
678		}
679		break;
680	case SIOCSIFMTU:
681		/*
682		 * Set the interface MTU.
683		 */
684		if (ifr->ifr_mtu > FDDIMTU) {
685			error = EINVAL;
686		} else {
687			ifp->if_mtu = ifr->ifr_mtu;
688		}
689		break;
690	default:
691		break;
692	}
693
694	return (error);
695}
696
697static int
698fddi_resolvemulti(ifp, llsa, sa)
699	struct ifnet *ifp;
700	struct sockaddr **llsa;
701	struct sockaddr *sa;
702{
703	struct sockaddr_dl *sdl;
704	struct sockaddr_in *sin;
705#ifdef INET6
706	struct sockaddr_in6 *sin6;
707#endif
708	u_char *e_addr;
709
710	switch(sa->sa_family) {
711	case AF_LINK:
712		/*
713		 * No mapping needed. Just check that it's a valid MC address.
714		 */
715		sdl = (struct sockaddr_dl *)sa;
716		e_addr = LLADDR(sdl);
717		if ((e_addr[0] & 1) != 1)
718			return (EADDRNOTAVAIL);
719		*llsa = 0;
720		return (0);
721
722#ifdef INET
723	case AF_INET:
724		sin = (struct sockaddr_in *)sa;
725		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
726			return (EADDRNOTAVAIL);
727		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
728		       M_WAITOK);
729		sdl->sdl_len = sizeof *sdl;
730		sdl->sdl_family = AF_LINK;
731		sdl->sdl_index = ifp->if_index;
732		sdl->sdl_type = IFT_FDDI;
733		sdl->sdl_nlen = 0;
734		sdl->sdl_alen = FDDI_ADDR_LEN;
735		sdl->sdl_slen = 0;
736		e_addr = LLADDR(sdl);
737		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
738		*llsa = (struct sockaddr *)sdl;
739		return (0);
740#endif
741#ifdef INET6
742	case AF_INET6:
743		sin6 = (struct sockaddr_in6 *)sa;
744		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
745			/*
746			 * An IP6 address of 0 means listen to all
747			 * of the Ethernet multicast address used for IP6.
748			 * (This is used for multicast routers.)
749			 */
750			ifp->if_flags |= IFF_ALLMULTI;
751			*llsa = 0;
752			return (0);
753		}
754		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
755			return (EADDRNOTAVAIL);
756		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
757		       M_WAITOK);
758		sdl->sdl_len = sizeof *sdl;
759		sdl->sdl_family = AF_LINK;
760		sdl->sdl_index = ifp->if_index;
761		sdl->sdl_type = IFT_FDDI;
762		sdl->sdl_nlen = 0;
763		sdl->sdl_alen = FDDI_ADDR_LEN;
764		sdl->sdl_slen = 0;
765		e_addr = LLADDR(sdl);
766		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
767		*llsa = (struct sockaddr *)sdl;
768		return (0);
769#endif
770
771	default:
772		/*
773		 * Well, the text isn't quite right, but it's the name
774		 * that counts...
775		 */
776		return (EAFNOSUPPORT);
777	}
778
779	return (0);
780}
781
782static moduledata_t fddi_mod = {
783	"fddi",	/* module name */
784	NULL,	/* event handler */
785	0	/* extra data */
786};
787
788DECLARE_MODULE(fddi, fddi_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
789MODULE_VERSION(fddi, 1);
790