if_fddisubr.c revision 109623
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 109623 2003-01-21 08:56:16Z alfred $
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_llc.h>
57#include <net/if_dl.h>
58#include <net/if_types.h>
59#include <net/netisr.h>
60#include <net/route.h>
61#include <net/bpf.h>
62#include <net/fddi.h>
63
64#if defined(INET) || defined(INET6)
65#include <netinet/in.h>
66#include <netinet/in_var.h>
67#include <netinet/if_ether.h>
68#endif
69#ifdef INET6
70#include <netinet6/nd6.h>
71#endif
72
73#ifdef IPX
74#include <netipx/ipx.h>
75#include <netipx/ipx_if.h>
76#endif
77
78#ifdef NS
79#include <netns/ns.h>
80#include <netns/ns_if.h>
81#endif
82
83#ifdef DECNET
84#include <netdnet/dn.h>
85#endif
86
87#ifdef NETATALK
88#include <netatalk/at.h>
89#include <netatalk/at_var.h>
90#include <netatalk/at_extern.h>
91
92extern u_char	at_org_code[ 3 ];
93extern u_char	aarp_org_code[ 3 ];
94#endif /* NETATALK */
95
96static u_char fddibroadcastaddr[FDDI_ADDR_LEN] =
97			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
98
99static int fddi_resolvemulti(struct ifnet *, struct sockaddr **,
100			      struct sockaddr *);
101static int fddi_output(struct ifnet *, struct mbuf *, struct sockaddr *,
102		       struct rtentry *);
103static void fddi_input(struct ifnet *ifp, struct mbuf *m);
104
105
106#define	IFP2AC(IFP)	((struct arpcom *)IFP)
107#define	senderr(e)	{ error = (e); goto bad; }
108
109/*
110 * FDDI output routine.
111 * Encapsulate a packet of type family for the local net.
112 * Use trailer local net encapsulation if enough data in first
113 * packet leaves a multiple of 512 bytes of data in remainder.
114 * Assumes that ifp is actually pointer to arpcom structure.
115 */
116static int
117fddi_output(ifp, m, dst, rt0)
118	struct ifnet *ifp;
119	struct mbuf *m;
120	struct sockaddr *dst;
121	struct rtentry *rt0;
122{
123	u_int16_t type;
124	int loop_copy = 0, error = 0, hdrcmplt = 0;
125 	u_char esrc[FDDI_ADDR_LEN], edst[FDDI_ADDR_LEN];
126	struct rtentry *rt;
127	struct fddi_header *fh;
128	struct arpcom *ac = IFP2AC(ifp);
129
130#ifdef MAC
131	error = mac_check_ifnet_transmit(ifp, m);
132	if (error)
133		senderr(error);
134#endif
135
136	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
137		senderr(ENETDOWN);
138	getmicrotime(&ifp->if_lastchange);
139	if ((rt = rt0) != NULL) {
140		if ((rt->rt_flags & RTF_UP) == 0) {
141			if ((rt0 = rt = rtalloc1(dst, 1, 0UL)) != NULL)
142				rt->rt_refcnt--;
143			else
144				senderr(EHOSTUNREACH);
145		}
146		if (rt->rt_flags & RTF_GATEWAY) {
147			if (rt->rt_gwroute == 0)
148				goto lookup;
149			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
150				rtfree(rt); rt = rt0;
151			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
152				if ((rt = rt->rt_gwroute) == 0)
153					senderr(EHOSTUNREACH);
154			}
155		}
156		if (rt->rt_flags & RTF_REJECT)
157			if (rt->rt_rmx.rmx_expire == 0 ||
158			    time_second < rt->rt_rmx.rmx_expire)
159				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
160	}
161	switch (dst->sa_family) {
162
163#ifdef INET
164	case AF_INET: {
165		if (!arpresolve(ifp, rt, m, dst, edst, rt0))
166			return (0);	/* if not yet resolved */
167		type = htons(ETHERTYPE_IP);
168		break;
169	}
170#endif
171#ifdef INET6
172	case AF_INET6:
173		if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst)) {
174			/* Something bad happened */
175			return (0);
176		}
177		type = htons(ETHERTYPE_IPV6);
178		break;
179#endif
180#ifdef IPX
181	case AF_IPX:
182		type = htons(ETHERTYPE_IPX);
183 		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
184		    (caddr_t)edst, FDDI_ADDR_LEN);
185		break;
186#endif
187#ifdef NETATALK
188	case AF_APPLETALK: {
189	    struct at_ifaddr *aa;
190            if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
191                return (0);
192	    /*
193	     * ifaddr is the first thing in at_ifaddr
194	     */
195	    if ((aa = at_ifawithnet( (struct sockaddr_at *)dst)) == 0)
196		goto bad;
197
198	    /*
199	     * In the phase 2 case, we 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, LLC_SNAPFRAMELEN, 0);
207		if (m == 0)
208			senderr(ENOBUFS);
209		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
210		llc.llc_control = LLC_UI;
211		bcopy(at_org_code, llc.llc_snap.org_code, sizeof(at_org_code));
212		llc.llc_snap.ether_type = htons(ETHERTYPE_AT);
213		bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
214		type = 0;
215	    } else {
216		type = htons(ETHERTYPE_AT);
217	    }
218	    break;
219	}
220#endif /* NETATALK */
221#ifdef NS
222	case AF_NS:
223		type = htons(ETHERTYPE_NS);
224 		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
225		    (caddr_t)edst, FDDI_ADDR_LEN);
226		break;
227#endif
228
229	case pseudo_AF_HDRCMPLT:
230	{
231		struct ether_header *eh;
232		hdrcmplt = 1;
233		eh = (struct ether_header *)dst->sa_data;
234		bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, FDDI_ADDR_LEN);
235		/* FALLTHROUGH */
236	}
237
238	case AF_UNSPEC:
239	{
240		struct ether_header *eh;
241		loop_copy = -1;
242		eh = (struct ether_header *)dst->sa_data;
243		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, FDDI_ADDR_LEN);
244		if (*edst & 1)
245			m->m_flags |= (M_BCAST|M_MCAST);
246		type = eh->ether_type;
247		break;
248	}
249
250	case AF_IMPLINK:
251	{
252		fh = mtod(m, struct fddi_header *);
253		error = EPROTONOSUPPORT;
254		switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
255			case FDDIFC_LLC_ASYNC: {
256				/* legal priorities are 0 through 7 */
257				if ((fh->fddi_fc & FDDIFC_Z) > 7)
258			        	goto bad;
259				break;
260			}
261			case FDDIFC_LLC_SYNC: {
262				/* FDDIFC_Z bits reserved, must be zero */
263				if (fh->fddi_fc & FDDIFC_Z)
264					goto bad;
265				break;
266			}
267			case FDDIFC_SMT: {
268				/* FDDIFC_Z bits must be non zero */
269				if ((fh->fddi_fc & FDDIFC_Z) == 0)
270					goto bad;
271				break;
272			}
273			default: {
274				/* anything else is too dangerous */
275               	 		goto bad;
276			}
277		}
278		error = 0;
279		if (fh->fddi_dhost[0] & 1)
280			m->m_flags |= (M_BCAST|M_MCAST);
281		goto queue_it;
282	}
283	default:
284		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
285		senderr(EAFNOSUPPORT);
286	}
287
288	/*
289	 * Add LLC header.
290	 */
291	if (type != 0) {
292		struct llc *l;
293		M_PREPEND(m, LLC_SNAPFRAMELEN, M_NOWAIT);
294		if (m == 0)
295			senderr(ENOBUFS);
296		l = mtod(m, struct llc *);
297		l->llc_control = LLC_UI;
298		l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
299		l->llc_snap.org_code[0] = l->llc_snap.org_code[1] = l->llc_snap.org_code[2] = 0;
300		bcopy((caddr_t)&type, (caddr_t)&l->llc_snap.ether_type,
301			sizeof(u_int16_t));
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_NOWAIT);
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) {
332			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
333
334			(void) if_simloop(ifp,
335				n, dst->sa_family, FDDI_HDR_LEN);
336	     	} else if (bcmp(fh->fddi_dhost,
337		    fh->fddi_shost, FDDI_ADDR_LEN) == 0) {
338			(void) if_simloop(ifp,
339				m, dst->sa_family, 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 * the packet is in the mbuf chain m without
358 * the fddi header, which is provided separately.
359 */
360static void
361fddi_input(ifp, m)
362	struct ifnet *ifp;
363	struct mbuf *m;
364{
365	struct ifqueue *inq;
366	struct llc *l;
367	struct fddi_header *fh;
368
369	fh = mtod(m, struct fddi_header *);
370
371	/*
372	 * Update interface statistics.
373	 */
374	ifp->if_ibytes += m->m_pkthdr.len;
375	getmicrotime(&ifp->if_lastchange);
376
377	/*
378	 * Discard packet if interface is not up.
379	 */
380	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
381		goto dropanyway;
382
383#ifdef MAC
384	mac_create_mbuf_from_ifnet(ifp, m);
385#endif
386
387	/*
388	 * Discard non local unicast packets when interface
389	 * is in promiscuous mode.
390	 */
391	if ((ifp->if_flags & IFF_PROMISC) && ((fh->fddi_dhost[0] & 1) == 0) &&
392	    (bcmp(IFP2AC(ifp)->ac_enaddr, (caddr_t)fh->fddi_dhost,
393	     FDDI_ADDR_LEN) != 0))
394		goto dropanyway;
395
396	/*
397	 * Set mbuf flags for bcast/mcast.
398	 */
399	if (fh->fddi_dhost[0] & 1) {
400		if (bcmp((caddr_t)ifp->if_broadcastaddr,
401			 (caddr_t)fh->fddi_dhost, FDDI_ADDR_LEN) == 0)
402			m->m_flags |= M_BCAST;
403		else
404			m->m_flags |= M_MCAST;
405		ifp->if_imcasts++;
406	}
407
408#ifdef M_LINK0
409	/*
410	 * If this has a LLC priority of 0, then mark it so upper
411	 * layers have a hint that it really came via a FDDI/Ethernet
412	 * bridge.
413	 */
414	if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
415		m->m_flags |= M_LINK0;
416#endif
417
418	/* Strip off FDDI header. */
419	m_adj(m, sizeof(struct fddi_header));
420
421	m = m_pullup(m, sizeof(struct llc));
422	if (m == 0) {
423		ifp->if_ierrors++;
424		goto dropanyway;
425	}
426	l = mtod(m, struct llc *);
427
428	switch (l->llc_dsap) {
429	case LLC_SNAP_LSAP:
430	{
431		u_int16_t type;
432		if (l->llc_control != LLC_UI || l->llc_ssap != LLC_SNAP_LSAP) {
433			ifp->if_noproto++;
434			goto dropanyway;
435		}
436#ifdef NETATALK
437		if (Bcmp(&(l->llc_snap.org_code)[0], at_org_code,
438			 sizeof(at_org_code)) == 0 &&
439		 	ntohs(l->llc_snap.ether_type) == ETHERTYPE_AT) {
440		    inq = &atintrq2;
441		    m_adj(m, LLC_SNAPFRAMELEN);
442		    schednetisr(NETISR_ATALK);
443		    break;
444		}
445
446		if (Bcmp(&(l->llc_snap.org_code)[0], aarp_org_code,
447			 sizeof(aarp_org_code)) == 0 &&
448			ntohs(l->llc_snap.ether_type) == ETHERTYPE_AARP) {
449		    m_adj(m, LLC_SNAPFRAMELEN);
450		    aarpinput(IFP2AC(ifp), m); /* XXX */
451		    return;
452		}
453#endif /* NETATALK */
454		if (l->llc_snap.org_code[0] != 0 ||
455		    l->llc_snap.org_code[1] != 0 ||
456		    l->llc_snap.org_code[2] != 0) {
457			ifp->if_noproto++;
458			goto dropanyway;
459		}
460
461		type = ntohs(l->llc_snap.ether_type);
462		m_adj(m, LLC_SNAPFRAMELEN);
463
464		switch (type) {
465#ifdef INET
466		case ETHERTYPE_IP:
467			if (ipflow_fastforward(m))
468				return;
469			schednetisr(NETISR_IP);
470			inq = &ipintrq;
471			break;
472
473		case ETHERTYPE_ARP:
474			if (ifp->if_flags & IFF_NOARP)
475				goto dropanyway;
476			schednetisr(NETISR_ARP);
477			inq = &arpintrq;
478			break;
479#endif
480#ifdef INET6
481		case ETHERTYPE_IPV6:
482			schednetisr(NETISR_IPV6);
483			inq = &ip6intrq;
484			break;
485#endif
486#ifdef IPX
487		case ETHERTYPE_IPX:
488			schednetisr(NETISR_IPX);
489			inq = &ipxintrq;
490			break;
491#endif
492#ifdef NS
493		case ETHERTYPE_NS:
494			schednetisr(NETISR_NS);
495			inq = &nsintrq;
496			break;
497#endif
498#ifdef DECNET
499		case ETHERTYPE_DECNET:
500			schednetisr(NETISR_DECNET);
501			inq = &decnetintrq;
502			break;
503#endif
504#ifdef NETATALK
505		case ETHERTYPE_AT:
506	                schednetisr(NETISR_ATALK);
507			inq = &atintrq1;
508			break;
509	        case ETHERTYPE_AARP:
510			/* probably this should be done with a NETISR as well */
511			aarpinput(IFP2AC(ifp), m); /* XXX */
512			return;
513#endif /* NETATALK */
514		default:
515			/* printf("fddi_input: unknown protocol 0x%x\n", type); */
516			ifp->if_noproto++;
517			goto dropanyway;
518		}
519		break;
520	}
521
522	default:
523		/* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
524		ifp->if_noproto++;
525		goto dropanyway;
526	}
527
528	(void) IF_HANDOFF(inq, m, NULL);
529	return;
530
531dropanyway:
532	ifp->if_iqdrops++;
533	if (m)
534		m_freem(m);
535	return;
536}
537
538/*
539 * Perform common duties while attaching to interface list
540 */
541void
542fddi_ifattach(ifp, bpf)
543	struct ifnet *ifp;
544	int bpf;
545{
546	struct ifaddr *ifa;
547	struct sockaddr_dl *sdl;
548
549	ifp->if_type = IFT_FDDI;
550	ifp->if_addrlen = FDDI_ADDR_LEN;
551	ifp->if_hdrlen = 21;
552
553	if_attach(ifp);         /* Must be called before additional assignments */
554
555	ifp->if_mtu = FDDIMTU;
556	ifp->if_output = fddi_output;
557	ifp->if_input = fddi_input;
558	ifp->if_resolvemulti = fddi_resolvemulti;
559	ifp->if_broadcastaddr = fddibroadcastaddr;
560	ifp->if_baudrate = 100000000;
561#ifdef IFF_NOTRAILERS
562	ifp->if_flags |= IFF_NOTRAILERS;
563#endif
564	ifa = ifaddr_byindex(ifp->if_index);
565	if (ifa == NULL) {
566		printf("%s(): no lladdr for %s%d!\n", __FUNCTION__,
567		       ifp->if_name, ifp->if_unit);
568		return;
569	}
570
571	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
572	sdl->sdl_type = IFT_FDDI;
573	sdl->sdl_alen = ifp->if_addrlen;
574	bcopy(IFP2AC(ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
575
576	if (bpf)
577		bpfattach(ifp, DLT_FDDI, FDDI_HDR_LEN);
578
579	return;
580}
581
582void
583fddi_ifdetach(ifp, bpf)
584	struct ifnet *ifp;
585	int bpf;
586{
587
588	if (bpf)
589		bpfdetach(ifp);
590
591	if_detach(ifp);
592
593	return;
594}
595
596int
597fddi_ioctl (ifp, command, data)
598	struct ifnet *ifp;
599	int command;
600	caddr_t data;
601{
602	struct ifaddr *ifa;
603	struct ifreq *ifr;
604	int error;
605
606	ifa = (struct ifaddr *) data;
607	ifr = (struct ifreq *) data;
608	error = 0;
609
610	switch (command) {
611	case SIOCSIFADDR:
612		ifp->if_flags |= IFF_UP;
613
614		switch (ifa->ifa_addr->sa_family) {
615#ifdef INET
616		case AF_INET:	/* before arpwhohas */
617			ifp->if_init(ifp->if_softc);
618			arp_ifinit(ifp, ifa);
619			break;
620#endif
621#ifdef IPX
622		/*
623		 * XXX - This code is probably wrong
624		 */
625		case AF_IPX: {
626				struct ipx_addr *ina;
627				struct arpcom *ac;
628
629				ina = &(IA_SIPX(ifa)->sipx_addr);
630				ac = IFP2AC(ifp);
631
632				if (ipx_nullhost(*ina)) {
633					ina->x_host = *(union ipx_host *)
634							ac->ac_enaddr;
635				} else {
636					bcopy((caddr_t) ina->x_host.c_host,
637					      (caddr_t) ac->ac_enaddr,
638					      sizeof(ac->ac_enaddr));
639				}
640
641				/*
642				 * Set new address
643				 */
644				ifp->if_init(ifp->if_softc);
645			}
646			break;
647#endif
648		default:
649			ifp->if_init(ifp->if_softc);
650			break;
651		}
652	case SIOCGIFADDR: {
653			struct sockaddr *sa;
654
655			sa = (struct sockaddr *) & ifr->ifr_data;
656			bcopy(IFP2AC(ifp)->ac_enaddr,
657			      (caddr_t) sa->sa_data, FDDI_ADDR_LEN);
658
659		}
660		break;
661	case SIOCSIFMTU:
662		/*
663		 * Set the interface MTU.
664		 */
665		if (ifr->ifr_mtu > FDDIMTU) {
666			error = EINVAL;
667		} else {
668			ifp->if_mtu = ifr->ifr_mtu;
669		}
670		break;
671	default:
672		break;
673	}
674
675	return (error);
676}
677
678static int
679fddi_resolvemulti(ifp, llsa, sa)
680	struct ifnet *ifp;
681	struct sockaddr **llsa;
682	struct sockaddr *sa;
683{
684	struct sockaddr_dl *sdl;
685	struct sockaddr_in *sin;
686#ifdef INET6
687	struct sockaddr_in6 *sin6;
688#endif
689	u_char *e_addr;
690
691	switch(sa->sa_family) {
692	case AF_LINK:
693		/*
694		 * No mapping needed. Just check that it's a valid MC address.
695		 */
696		sdl = (struct sockaddr_dl *)sa;
697		e_addr = LLADDR(sdl);
698		if ((e_addr[0] & 1) != 1)
699			return (EADDRNOTAVAIL);
700		*llsa = 0;
701		return (0);
702
703#ifdef INET
704	case AF_INET:
705		sin = (struct sockaddr_in *)sa;
706		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
707			return (EADDRNOTAVAIL);
708		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
709		       0);
710		sdl->sdl_len = sizeof *sdl;
711		sdl->sdl_family = AF_LINK;
712		sdl->sdl_index = ifp->if_index;
713		sdl->sdl_type = IFT_FDDI;
714		sdl->sdl_nlen = 0;
715		sdl->sdl_alen = FDDI_ADDR_LEN;
716		sdl->sdl_slen = 0;
717		e_addr = LLADDR(sdl);
718		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
719		*llsa = (struct sockaddr *)sdl;
720		return (0);
721#endif
722#ifdef INET6
723	case AF_INET6:
724		sin6 = (struct sockaddr_in6 *)sa;
725		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
726			/*
727			 * An IP6 address of 0 means listen to all
728			 * of the Ethernet multicast address used for IP6.
729			 * (This is used for multicast routers.)
730			 */
731			ifp->if_flags |= IFF_ALLMULTI;
732			*llsa = 0;
733			return (0);
734		}
735		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
736			return (EADDRNOTAVAIL);
737		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
738		       0);
739		sdl->sdl_len = sizeof *sdl;
740		sdl->sdl_family = AF_LINK;
741		sdl->sdl_index = ifp->if_index;
742		sdl->sdl_type = IFT_FDDI;
743		sdl->sdl_nlen = 0;
744		sdl->sdl_alen = FDDI_ADDR_LEN;
745		sdl->sdl_slen = 0;
746		e_addr = LLADDR(sdl);
747		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
748		*llsa = (struct sockaddr *)sdl;
749		return (0);
750#endif
751
752	default:
753		/*
754		 * Well, the text isn't quite right, but it's the name
755		 * that counts...
756		 */
757		return (EAFNOSUPPORT);
758	}
759
760	return (0);
761}
762
763static moduledata_t fddi_mod = {
764	"fddi",	/* module name */
765	NULL,	/* event handler */
766	0	/* extra data */
767};
768
769DECLARE_MODULE(fddi, fddi_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
770MODULE_VERSION(fddi, 1);
771