if_fddisubr.c revision 60889
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 60889 2000-05-24 21:16:56Z archie $
37 */
38
39#include "opt_atalk.h"
40#include "opt_inet.h"
41#include "opt_inet6.h"
42#include "opt_ipx.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/mbuf.h>
47#include <sys/socket.h>
48
49#include <net/if.h>
50#include <net/netisr.h>
51#include <net/route.h>
52#include <net/if_llc.h>
53#include <net/if_dl.h>
54#include <net/if_types.h>
55
56#if defined(INET) || defined(INET6)
57#include <netinet/in.h>
58#include <netinet/in_var.h>
59#include <netinet/if_ether.h>
60#endif
61#ifdef INET6
62#include <netinet6/nd6.h>
63#endif
64#if defined(__FreeBSD__)
65#include <netinet/if_fddi.h>
66#else
67#include <net/if_fddi.h>
68#endif
69
70#ifdef IPX
71#include <netipx/ipx.h>
72#include <netipx/ipx_if.h>
73#endif
74
75#ifdef NS
76#include <netns/ns.h>
77#include <netns/ns_if.h>
78#endif
79
80#ifdef DECNET
81#include <netdnet/dn.h>
82#endif
83
84#ifdef NETATALK
85#include <netatalk/at.h>
86#include <netatalk/at_var.h>
87#include <netatalk/at_extern.h>
88
89#define llc_snap_org_code llc_un.type_snap.org_code
90#define llc_snap_ether_type llc_un.type_snap.ether_type
91
92extern u_char	at_org_code[ 3 ];
93extern u_char	aarp_org_code[ 3 ];
94#endif /* NETATALK */
95
96#define senderr(e) { error = (e); goto bad;}
97
98/*
99 * This really should be defined in if_llc.h but in case it isn't.
100 */
101#ifndef llc_snap
102#define	llc_snap	llc_un.type_snap
103#endif
104
105#if defined(__bsdi__) || defined(__NetBSD__)
106#define	RTALLOC1(a, b)			rtalloc1(a, b)
107#define	ARPRESOLVE(a, b, c, d, e, f)	arpresolve(a, b, c, d, e)
108#elif defined(__FreeBSD__)
109#define	RTALLOC1(a, b)			rtalloc1(a, b, 0UL)
110#define	ARPRESOLVE(a, b, c, d, e, f)	arpresolve(a, b, c, d, e, f)
111#endif
112/*
113 * FDDI output routine.
114 * Encapsulate a packet of type family for the local net.
115 * Use trailer local net encapsulation if enough data in first
116 * packet leaves a multiple of 512 bytes of data in remainder.
117 * Assumes that ifp is actually pointer to arpcom structure.
118 */
119int
120fddi_output(ifp, m, dst, rt0)
121	register struct ifnet *ifp;
122	struct mbuf *m;
123	struct sockaddr *dst;
124	struct rtentry *rt0;
125{
126	u_int16_t type;
127	int s, loop_copy = 0, error = 0, hdrcmplt = 0;
128 	u_char esrc[6], edst[6];
129	register struct rtentry *rt;
130	register struct fddi_header *fh;
131	struct arpcom *ac = (struct arpcom *)ifp;
132
133	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
134		senderr(ENETDOWN);
135	getmicrotime(&ifp->if_lastchange);
136#if !defined(__bsdi__) || _BSDI_VERSION >= 199401
137	if ((rt = rt0) != NULL) {
138		if ((rt->rt_flags & RTF_UP) == 0) {
139			if ((rt0 = rt = RTALLOC1(dst, 1)) != NULL)
140				rt->rt_refcnt--;
141			else
142				senderr(EHOSTUNREACH);
143		}
144		if (rt->rt_flags & RTF_GATEWAY) {
145			if (rt->rt_gwroute == 0)
146				goto lookup;
147			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
148				rtfree(rt); rt = rt0;
149			lookup: rt->rt_gwroute = RTALLOC1(rt->rt_gateway, 1);
150				if ((rt = rt->rt_gwroute) == 0)
151					senderr(EHOSTUNREACH);
152			}
153		}
154		if (rt->rt_flags & RTF_REJECT)
155			if (rt->rt_rmx.rmx_expire == 0 ||
156			    time_second < rt->rt_rmx.rmx_expire)
157				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
158	}
159#endif
160	switch (dst->sa_family) {
161
162#ifdef INET
163	case AF_INET: {
164#if !defined(__bsdi__) || _BSDI_VERSION >= 199401
165		if (!ARPRESOLVE(ac, rt, m, dst, edst, rt0))
166			return (0);	/* if not yet resolved */
167#else
168		int usetrailers;
169		if (!arpresolve(ac, m, &((struct sockaddr_in *)dst)->sin_addr, edst, &usetrailers))
170			return (0);	/* if not yet resolved */
171#endif
172		type = htons(ETHERTYPE_IP);
173		break;
174	}
175#endif
176#ifdef INET6
177	case AF_INET6:
178		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
179			/* this must be impossible, so we bark */
180			printf("nd6_storelladdr failed\n");
181			return(0);
182		}
183		type = htons(ETHERTYPE_IPV6);
184		break;
185#endif
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, sizeof (edst));
191		break;
192#endif
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, sizeof(struct llc), M_WAIT);
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), sizeof(struct llc));
220		type = 0;
221	    } else {
222		type = htons(ETHERTYPE_AT);
223	    }
224	    break;
225	}
226#endif /* NETATALK */
227#ifdef NS
228	case AF_NS:
229		type = htons(ETHERTYPE_NS);
230 		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
231		    (caddr_t)edst, sizeof (edst));
232		break;
233#endif
234
235	case pseudo_AF_HDRCMPLT:
236	{
237		struct ether_header *eh;
238		hdrcmplt = 1;
239		eh = (struct ether_header *)dst->sa_data;
240 		(void)memcpy((caddr_t)esrc, (caddr_t)eh->ether_shost, sizeof (esrc));
241		/* FALLTHROUGH */
242	}
243
244	case AF_UNSPEC:
245	{
246		struct ether_header *eh;
247		loop_copy = -1;
248		eh = (struct ether_header *)dst->sa_data;
249 		(void)memcpy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
250		if (*edst & 1)
251			m->m_flags |= (M_BCAST|M_MCAST);
252		type = eh->ether_type;
253		break;
254	}
255
256	case AF_IMPLINK:
257	{
258		fh = mtod(m, struct fddi_header *);
259		error = EPROTONOSUPPORT;
260		switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
261			case FDDIFC_LLC_ASYNC: {
262				/* legal priorities are 0 through 7 */
263				if ((fh->fddi_fc & FDDIFC_Z) > 7)
264			        	goto bad;
265				break;
266			}
267			case FDDIFC_LLC_SYNC: {
268				/* FDDIFC_Z bits reserved, must be zero */
269				if (fh->fddi_fc & FDDIFC_Z)
270					goto bad;
271				break;
272			}
273			case FDDIFC_SMT: {
274				/* FDDIFC_Z bits must be non zero */
275				if ((fh->fddi_fc & FDDIFC_Z) == 0)
276					goto bad;
277				break;
278			}
279			default: {
280				/* anything else is too dangerous */
281               	 		goto bad;
282			}
283		}
284		error = 0;
285		if (fh->fddi_dhost[0] & 1)
286			m->m_flags |= (M_BCAST|M_MCAST);
287		goto queue_it;
288	}
289	default:
290		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
291			dst->sa_family);
292		senderr(EAFNOSUPPORT);
293	}
294
295	if (type != 0) {
296		register struct llc *l;
297		M_PREPEND(m, sizeof (struct llc), M_DONTWAIT);
298		if (m == 0)
299			senderr(ENOBUFS);
300		l = mtod(m, struct llc *);
301		l->llc_control = LLC_UI;
302		l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
303		l->llc_snap.org_code[0] = l->llc_snap.org_code[1] = l->llc_snap.org_code[2] = 0;
304		(void)memcpy((caddr_t) &l->llc_snap.ether_type, (caddr_t) &type,
305			sizeof(u_int16_t));
306	}
307
308	/*
309	 * Add local net header.  If no space in first mbuf,
310	 * allocate another.
311	 */
312	M_PREPEND(m, sizeof (struct fddi_header), M_DONTWAIT);
313	if (m == 0)
314		senderr(ENOBUFS);
315	fh = mtod(m, struct fddi_header *);
316	fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
317 	(void)memcpy((caddr_t)fh->fddi_dhost, (caddr_t)edst, sizeof (edst));
318  queue_it:
319	if (hdrcmplt)
320		(void)memcpy((caddr_t)fh->fddi_shost, (caddr_t)esrc,
321			sizeof(fh->fddi_shost));
322	else
323		(void)memcpy((caddr_t)fh->fddi_shost, (caddr_t)ac->ac_enaddr,
324			sizeof(fh->fddi_shost));
325	/*
326	 * If a simplex interface, and the packet is being sent to our
327	 * Ethernet address or a broadcast address, loopback a copy.
328	 * XXX To make a simplex device behave exactly like a duplex
329	 * device, we should copy in the case of sending to our own
330	 * ethernet address (thus letting the original actually appear
331	 * on the wire). However, we don't do that here for security
332	 * reasons and compatibility with the original behavior.
333	 */
334	if ((ifp->if_flags & IFF_SIMPLEX) &&
335	   (loop_copy != -1)) {
336		if ((m->m_flags & M_BCAST) || loop_copy) {
337			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
338
339			(void) if_simloop(ifp,
340				n, dst->sa_family, sizeof(struct fddi_header));
341	     	} else if (bcmp(fh->fddi_dhost,
342		    fh->fddi_shost, sizeof(fh->fddi_shost)) == 0) {
343			(void) if_simloop(ifp,
344				m, dst->sa_family, sizeof(struct fddi_header));
345			return(0);	/* XXX */
346		}
347	}
348
349	s = splimp();
350	/*
351	 * Queue message on interface, and start output if interface
352	 * not yet active.
353	 */
354	if (IF_QFULL(&ifp->if_snd)) {
355		IF_DROP(&ifp->if_snd);
356		splx(s);
357		senderr(ENOBUFS);
358	}
359	ifp->if_obytes += m->m_pkthdr.len;
360	IF_ENQUEUE(&ifp->if_snd, m);
361	if ((ifp->if_flags & IFF_OACTIVE) == 0)
362		(*ifp->if_start)(ifp);
363	splx(s);
364	if (m->m_flags & M_MCAST)
365		ifp->if_omcasts++;
366	return (error);
367
368bad:
369	if (m)
370		m_freem(m);
371	return (error);
372}
373
374/*
375 * Process a received FDDI packet;
376 * the packet is in the mbuf chain m without
377 * the fddi header, which is provided separately.
378 */
379void
380fddi_input(ifp, fh, m)
381	struct ifnet *ifp;
382	register struct fddi_header *fh;
383	struct mbuf *m;
384{
385	register struct ifqueue *inq;
386	register struct llc *l;
387	int s;
388
389	if ((ifp->if_flags & IFF_UP) == 0) {
390		m_freem(m);
391		return;
392	}
393	getmicrotime(&ifp->if_lastchange);
394	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*fh);
395	if (fh->fddi_dhost[0] & 1) {
396		if (bcmp((caddr_t)fddibroadcastaddr, (caddr_t)fh->fddi_dhost,
397		    sizeof(fddibroadcastaddr)) == 0)
398			m->m_flags |= M_BCAST;
399		else
400			m->m_flags |= M_MCAST;
401		ifp->if_imcasts++;
402	} else if ((ifp->if_flags & IFF_PROMISC)
403	    && bcmp(((struct arpcom *)ifp)->ac_enaddr, (caddr_t)fh->fddi_dhost,
404		    sizeof(fh->fddi_dhost)) != 0) {
405		m_freem(m);
406		return;
407	}
408
409#ifdef M_LINK0
410	/*
411	 * If this has a LLC priority of 0, then mark it so upper
412	 * layers have a hint that it really came via a FDDI/Ethernet
413	 * bridge.
414	 */
415	if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
416		m->m_flags |= M_LINK0;
417#endif
418
419	l = mtod(m, struct llc *);
420	switch (l->llc_dsap) {
421#if defined(INET) || defined(INET6) || defined(NS) || defined(DECNET) || defined(IPX) || defined(NETATALK)
422	case LLC_SNAP_LSAP:
423	{
424		u_int16_t type;
425		if (l->llc_control != LLC_UI || l->llc_ssap != LLC_SNAP_LSAP)
426			goto dropanyway;
427#ifdef NETATALK
428		if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
429			 sizeof(at_org_code)) == 0 &&
430		 	ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
431		    inq = &atintrq2;
432		    m_adj( m, sizeof( struct llc ));
433		    schednetisr(NETISR_ATALK);
434		    break;
435		}
436
437		if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
438			 sizeof(aarp_org_code)) == 0 &&
439			ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
440		    m_adj( m, sizeof( struct llc ));
441		    aarpinput((struct arpcom *)ifp, m); /* XXX */
442		    return;
443		}
444#endif /* NETATALK */
445		if (l->llc_snap.org_code[0] != 0 || l->llc_snap.org_code[1] != 0|| l->llc_snap.org_code[2] != 0)
446			goto dropanyway;
447		type = ntohs(l->llc_snap.ether_type);
448		m_adj(m, 8);
449		switch (type) {
450#ifdef INET
451		case ETHERTYPE_IP:
452			if (ipflow_fastforward(m))
453				return;
454			schednetisr(NETISR_IP);
455			inq = &ipintrq;
456			break;
457
458		case ETHERTYPE_ARP:
459#if !defined(__bsdi__) || _BSDI_VERSION >= 199401
460			schednetisr(NETISR_ARP);
461			inq = &arpintrq;
462			break;
463#else
464			arpinput((struct arpcom *)ifp, m);
465			return;
466#endif
467#endif
468#ifdef INET6
469		case ETHERTYPE_IPV6:
470			schednetisr(NETISR_IPV6);
471			inq = &ip6intrq;
472			break;
473#endif
474#ifdef IPX
475		case ETHERTYPE_IPX:
476			schednetisr(NETISR_IPX);
477			inq = &ipxintrq;
478			break;
479#endif
480#ifdef NS
481		case ETHERTYPE_NS:
482			schednetisr(NETISR_NS);
483			inq = &nsintrq;
484			break;
485#endif
486#ifdef DECNET
487		case ETHERTYPE_DECNET:
488			schednetisr(NETISR_DECNET);
489			inq = &decnetintrq;
490			break;
491#endif
492#ifdef NETATALK
493		case ETHERTYPE_AT:
494	                schednetisr(NETISR_ATALK);
495			inq = &atintrq1;
496			break;
497	        case ETHERTYPE_AARP:
498			/* probably this should be done with a NETISR as well */
499			aarpinput((struct arpcom *)ifp, m); /* XXX */
500			return;
501#endif /* NETATALK */
502		default:
503			/* printf("fddi_input: unknown protocol 0x%x\n", type); */
504			ifp->if_noproto++;
505			goto dropanyway;
506		}
507		break;
508	}
509#endif /* INET || NS */
510
511	default:
512		/* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
513		ifp->if_noproto++;
514	dropanyway:
515		m_freem(m);
516		return;
517	}
518
519	s = splimp();
520	if (IF_QFULL(inq)) {
521		IF_DROP(inq);
522		m_freem(m);
523	} else
524		IF_ENQUEUE(inq, m);
525	splx(s);
526}
527/*
528 * Perform common duties while attaching to interface list
529 */
530#ifdef __NetBSD__
531#define	ifa_next	ifa_list.tqe_next
532#endif
533
534void
535fddi_ifattach(ifp)
536	register struct ifnet *ifp;
537{
538	register struct ifaddr *ifa;
539	register struct sockaddr_dl *sdl;
540
541	ifp->if_type = IFT_FDDI;
542	ifp->if_addrlen = 6;
543	ifp->if_hdrlen = 21;
544	ifp->if_mtu = FDDIMTU;
545	ifp->if_baudrate = 100000000;
546#ifdef IFF_NOTRAILERS
547	ifp->if_flags |= IFF_NOTRAILERS;
548#endif
549#if defined(__FreeBSD__)
550	ifa = ifnet_addrs[ifp->if_index - 1];
551	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
552	sdl->sdl_type = IFT_FDDI;
553	sdl->sdl_alen = ifp->if_addrlen;
554	bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
555#elif defined(__NetBSD__)
556	LIST_INIT(&((struct arpcom *)ifp)->ac_multiaddrs);
557	for (ifa = ifp->if_addrlist.tqh_first; ifa != NULL; ifa = ifa->ifa_list.tqe_next)
558#else
559	for (ifa = ifp->if_addrlist; ifa != NULL; ifa = ifa->ifa_next)
560#endif
561#if !defined(__FreeBSD__)
562		if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
563		    sdl->sdl_family == AF_LINK) {
564			sdl->sdl_type = IFT_FDDI;
565			sdl->sdl_alen = ifp->if_addrlen;
566			bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
567			      LLADDR(sdl), ifp->if_addrlen);
568			break;
569		}
570#endif
571}
572