if_fwsubr.c revision 154209
1/*-
2 * Copyright (c) 2004 Doug Rabson
3 * Copyright (c) 1982, 1989, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 *    may be used to endorse or promote products derived from this software
16 *    without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD: head/sys/net/if_fwsubr.c 154209 2006-01-11 05:37:21Z brooks $
31 */
32
33#include "opt_inet.h"
34#include "opt_inet6.h"
35#include "opt_mac.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/mac.h>
41#include <sys/malloc.h>
42#include <sys/mbuf.h>
43#include <sys/module.h>
44#include <sys/socket.h>
45#include <sys/sockio.h>
46
47#include <net/if.h>
48#include <net/netisr.h>
49#include <net/route.h>
50#include <net/if_llc.h>
51#include <net/if_dl.h>
52#include <net/if_types.h>
53#include <net/bpf.h>
54#include <net/firewire.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
65MALLOC_DEFINE(M_FWCOM, "fw_com", "firewire interface internals");
66
67struct fw_hwaddr firewire_broadcastaddr = {
68	0xffffffff,
69	0xffffffff,
70	0xff,
71	0xff,
72	0xffff,
73	0xffffffff
74};
75
76static int
77firewire_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
78    struct rtentry *rt0)
79{
80	struct fw_com *fc = IFP2FWC(ifp);
81	int error, type;
82	struct rtentry *rt = NULL;
83	struct m_tag *mtag;
84	union fw_encap *enc;
85	struct fw_hwaddr *destfw;
86	uint8_t speed;
87	uint16_t psize, fsize, dsize;
88	struct mbuf *mtail;
89	int unicast, dgl, foff;
90	static int next_dgl;
91
92#ifdef MAC
93	error = mac_check_ifnet_transmit(ifp, m);
94	if (error)
95		goto bad;
96#endif
97
98	if (!((ifp->if_flags & IFF_UP) &&
99	   (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
100		error = ENETDOWN;
101		goto bad;
102	}
103
104	if (rt0 != NULL) {
105		error = rt_check(&rt, &rt0, dst);
106		if (error)
107			goto bad;
108		RT_UNLOCK(rt);
109	}
110
111	/*
112	 * For unicast, we make a tag to store the lladdr of the
113	 * destination. This might not be the first time we have seen
114	 * the packet (for instance, the arp code might be trying to
115	 * re-send it after receiving an arp reply) so we only
116	 * allocate a tag if there isn't one there already. For
117	 * multicast, we will eventually use a different tag to store
118	 * the channel number.
119	 */
120	unicast = !(m->m_flags & (M_BCAST | M_MCAST));
121	if (unicast) {
122		mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, NULL);
123		if (!mtag) {
124			mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR,
125			    sizeof (struct fw_hwaddr), M_NOWAIT);
126			if (!mtag) {
127				error = ENOMEM;
128				goto bad;
129			}
130			m_tag_prepend(m, mtag);
131		}
132		destfw = (struct fw_hwaddr *)(mtag + 1);
133	} else {
134		destfw = 0;
135	}
136
137	switch (dst->sa_family) {
138#ifdef AF_INET
139	case AF_INET:
140		/*
141		 * Only bother with arp for unicast. Allocation of
142		 * channels etc. for firewire is quite different and
143		 * doesn't fit into the arp model.
144		 */
145		if (unicast) {
146			error = arpresolve(ifp, rt, m, dst, (u_char *) destfw);
147			if (error)
148				return (error == EWOULDBLOCK ? 0 : error);
149		}
150		type = ETHERTYPE_IP;
151		break;
152
153	case AF_ARP:
154	{
155		struct arphdr *ah;
156		ah = mtod(m, struct arphdr *);
157		ah->ar_hrd = htons(ARPHRD_IEEE1394);
158		type = ETHERTYPE_ARP;
159		if (unicast)
160			*destfw = *(struct fw_hwaddr *) ar_tha(ah);
161
162		/*
163		 * The standard arp code leaves a hole for the target
164		 * hardware address which we need to close up.
165		 */
166		bcopy(ar_tpa(ah), ar_tha(ah), ah->ar_pln);
167		m_adj(m, -ah->ar_hln);
168		break;
169	}
170#endif
171
172#ifdef INET6
173	case AF_INET6:
174		if (unicast) {
175			error = nd6_storelladdr(fc->fc_ifp, rt, m, dst,
176			    (u_char *) destfw);
177			if (error)
178				return (error);
179		}
180		type = ETHERTYPE_IPV6;
181		break;
182#endif
183
184	default:
185		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
186		error = EAFNOSUPPORT;
187		goto bad;
188	}
189
190	/*
191	 * Let BPF tap off a copy before we encapsulate.
192	 */
193	if (ifp->if_bpf) {
194		struct fw_bpfhdr h;
195		if (unicast)
196			bcopy(destfw, h.firewire_dhost, 8);
197		else
198			bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
199		bcopy(&fc->fc_hwaddr, h.firewire_shost, 8);
200		h.firewire_type = htons(type);
201		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
202	}
203
204	/*
205	 * Punt on MCAP for now and send all multicast packets on the
206	 * broadcast channel.
207	 */
208	if (m->m_flags & M_MCAST)
209		m->m_flags |= M_BCAST;
210
211	/*
212	 * Figure out what speed to use and what the largest supported
213	 * packet size is. For unicast, this is the minimum of what we
214	 * can speak and what they can hear. For broadcast, lets be
215	 * conservative and use S100. We could possibly improve that
216	 * by examining the bus manager's speed map or similar. We
217	 * also reduce the packet size for broadcast to account for
218	 * the GASP header.
219	 */
220	if (unicast) {
221		speed = min(fc->fc_speed, destfw->sspd);
222		psize = min(512 << speed, 2 << destfw->sender_max_rec);
223	} else {
224		speed = 0;
225		psize = 512 - 2*sizeof(uint32_t);
226	}
227
228	/*
229	 * Next, we encapsulate, possibly fragmenting the original
230	 * datagram if it won't fit into a single packet.
231	 */
232	if (m->m_pkthdr.len <= psize - sizeof(uint32_t)) {
233		/*
234		 * No fragmentation is necessary.
235		 */
236		M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
237		if (!m) {
238			error = ENOBUFS;
239			goto bad;
240		}
241		enc = mtod(m, union fw_encap *);
242		enc->unfrag.ether_type = type;
243		enc->unfrag.lf = FW_ENCAP_UNFRAG;
244		enc->unfrag.reserved = 0;
245
246		/*
247		 * Byte swap the encapsulation header manually.
248		 */
249		enc->ul[0] = htonl(enc->ul[0]);
250
251		IFQ_HANDOFF(ifp, m, error);
252		return (error);
253	} else {
254		/*
255		 * Fragment the datagram, making sure to leave enough
256		 * space for the encapsulation header in each packet.
257		 */
258		fsize = psize - 2*sizeof(uint32_t);
259		dgl = next_dgl++;
260		dsize = m->m_pkthdr.len;
261		foff = 0;
262		while (m) {
263			if (m->m_pkthdr.len > fsize) {
264				/*
265				 * Split off the tail segment from the
266				 * datagram, copying our tags over.
267				 */
268				mtail = m_split(m, fsize, M_DONTWAIT);
269				m_tag_copy_chain(mtail, m, M_NOWAIT);
270			} else {
271				mtail = 0;
272			}
273
274			/*
275			 * Add our encapsulation header to this
276			 * fragment and hand it off to the link.
277			 */
278			M_PREPEND(m, 2*sizeof(uint32_t), M_DONTWAIT);
279			if (!m) {
280				error = ENOBUFS;
281				goto bad;
282			}
283			enc = mtod(m, union fw_encap *);
284			if (foff == 0) {
285				enc->firstfrag.lf = FW_ENCAP_FIRST;
286				enc->firstfrag.reserved1 = 0;
287				enc->firstfrag.reserved2 = 0;
288				enc->firstfrag.datagram_size = dsize - 1;
289				enc->firstfrag.ether_type = type;
290				enc->firstfrag.dgl = dgl;
291			} else {
292				if (mtail)
293					enc->nextfrag.lf = FW_ENCAP_NEXT;
294				else
295					enc->nextfrag.lf = FW_ENCAP_LAST;
296				enc->nextfrag.reserved1 = 0;
297				enc->nextfrag.reserved2 = 0;
298				enc->nextfrag.reserved3 = 0;
299				enc->nextfrag.datagram_size = dsize - 1;
300				enc->nextfrag.fragment_offset = foff;
301				enc->nextfrag.dgl = dgl;
302			}
303			foff += m->m_pkthdr.len - 2*sizeof(uint32_t);
304
305			/*
306			 * Byte swap the encapsulation header manually.
307			 */
308			enc->ul[0] = htonl(enc->ul[0]);
309			enc->ul[1] = htonl(enc->ul[1]);
310
311			IFQ_HANDOFF(ifp, m, error);
312			if (error) {
313				if (mtail)
314					m_freem(mtail);
315				return (ENOBUFS);
316			}
317
318			m = mtail;
319		}
320
321		return (0);
322	}
323
324bad:
325	if (m)
326		m_freem(m);
327	return (error);
328}
329
330static struct mbuf *
331firewire_input_fragment(struct fw_com *fc, struct mbuf *m, int src)
332{
333	union fw_encap *enc;
334	struct fw_reass *r;
335	struct mbuf *mf, *mprev;
336	int dsize;
337	int fstart, fend, start, end, islast;
338	uint32_t id;
339
340	GIANT_REQUIRED;
341
342	/*
343	 * Find an existing reassembly buffer or create a new one.
344	 */
345	enc = mtod(m, union fw_encap *);
346	id = enc->firstfrag.dgl | (src << 16);
347	STAILQ_FOREACH(r, &fc->fc_frags, fr_link)
348		if (r->fr_id == id)
349			break;
350	if (!r) {
351		r = malloc(sizeof(struct fw_reass), M_TEMP, M_NOWAIT);
352		if (!r) {
353			m_freem(m);
354			return 0;
355		}
356		r->fr_id = id;
357		r->fr_frags = 0;
358		STAILQ_INSERT_HEAD(&fc->fc_frags, r, fr_link);
359	}
360
361	/*
362	 * If this fragment overlaps any other fragment, we must discard
363	 * the partial reassembly and start again.
364	 */
365	if (enc->firstfrag.lf == FW_ENCAP_FIRST)
366		fstart = 0;
367	else
368		fstart = enc->nextfrag.fragment_offset;
369	fend = fstart + m->m_pkthdr.len - 2*sizeof(uint32_t);
370	dsize = enc->nextfrag.datagram_size;
371	islast = (enc->nextfrag.lf == FW_ENCAP_LAST);
372
373	for (mf = r->fr_frags; mf; mf = mf->m_nextpkt) {
374		enc = mtod(mf, union fw_encap *);
375		if (enc->nextfrag.datagram_size != dsize) {
376			/*
377			 * This fragment must be from a different
378			 * packet.
379			 */
380			goto bad;
381		}
382		if (enc->firstfrag.lf == FW_ENCAP_FIRST)
383			start = 0;
384		else
385			start = enc->nextfrag.fragment_offset;
386		end = start + mf->m_pkthdr.len - 2*sizeof(uint32_t);
387		if ((fstart < end && fend > start) ||
388		    (islast && enc->nextfrag.lf == FW_ENCAP_LAST)) {
389			/*
390			 * Overlap - discard reassembly buffer and start
391			 * again with this fragment.
392			 */
393			goto bad;
394		}
395	}
396
397	/*
398	 * Find where to put this fragment in the list.
399	 */
400	for (mf = r->fr_frags, mprev = NULL; mf;
401	    mprev = mf, mf = mf->m_nextpkt) {
402		enc = mtod(mf, union fw_encap *);
403		if (enc->firstfrag.lf == FW_ENCAP_FIRST)
404			start = 0;
405		else
406			start = enc->nextfrag.fragment_offset;
407		if (start >= fend)
408			break;
409	}
410
411	/*
412	 * If this is a last fragment and we are not adding at the end
413	 * of the list, discard the buffer.
414	 */
415	if (islast && mprev && mprev->m_nextpkt)
416		goto bad;
417
418	if (mprev) {
419		m->m_nextpkt = mprev->m_nextpkt;
420		mprev->m_nextpkt = m;
421
422		/*
423		 * Coalesce forwards and see if we can make a whole
424		 * datagram.
425		 */
426		enc = mtod(mprev, union fw_encap *);
427		if (enc->firstfrag.lf == FW_ENCAP_FIRST)
428			start = 0;
429		else
430			start = enc->nextfrag.fragment_offset;
431		end = start + mprev->m_pkthdr.len - 2*sizeof(uint32_t);
432		while (end == fstart) {
433			/*
434			 * Strip off the encap header from m and
435			 * append it to mprev, freeing m.
436			 */
437			m_adj(m, 2*sizeof(uint32_t));
438			mprev->m_nextpkt = m->m_nextpkt;
439			mprev->m_pkthdr.len += m->m_pkthdr.len;
440			m_cat(mprev, m);
441
442			if (mprev->m_pkthdr.len == dsize + 1 + 2*sizeof(uint32_t)) {
443				/*
444				 * We have assembled a complete packet
445				 * we must be finished. Make sure we have
446				 * merged the whole chain.
447				 */
448				STAILQ_REMOVE(&fc->fc_frags, r, fw_reass, fr_link);
449				free(r, M_TEMP);
450				m = mprev->m_nextpkt;
451				while (m) {
452					mf = m->m_nextpkt;
453					m_freem(m);
454					m = mf;
455				}
456				mprev->m_nextpkt = NULL;
457
458				return (mprev);
459			}
460
461			/*
462			 * See if we can continue merging forwards.
463			 */
464			end = fend;
465			m = mprev->m_nextpkt;
466			if (m) {
467				enc = mtod(m, union fw_encap *);
468				if (enc->firstfrag.lf == FW_ENCAP_FIRST)
469					fstart = 0;
470				else
471					fstart = enc->nextfrag.fragment_offset;
472				fend = fstart + m->m_pkthdr.len
473				    - 2*sizeof(uint32_t);
474			} else {
475				break;
476			}
477		}
478	} else {
479		m->m_nextpkt = 0;
480		r->fr_frags = m;
481	}
482
483	return (0);
484
485bad:
486	while (r->fr_frags) {
487		mf = r->fr_frags;
488		r->fr_frags = mf->m_nextpkt;
489		m_freem(mf);
490	}
491	m->m_nextpkt = 0;
492	r->fr_frags = m;
493
494	return (0);
495}
496
497void
498firewire_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
499{
500	struct fw_com *fc = IFP2FWC(ifp);
501	union fw_encap *enc;
502	int type, isr;
503
504	GIANT_REQUIRED;
505
506	/*
507	 * The caller has already stripped off the packet header
508	 * (stream or wreqb) and marked the mbuf's M_BCAST flag
509	 * appropriately. We de-encapsulate the IP packet and pass it
510	 * up the line after handling link-level fragmentation.
511	 */
512	if (m->m_pkthdr.len < sizeof(uint32_t)) {
513		if_printf(ifp, "discarding frame without "
514		    "encapsulation header (len %u pkt len %u)\n",
515		    m->m_len, m->m_pkthdr.len);
516	}
517
518	m = m_pullup(m, sizeof(uint32_t));
519	enc = mtod(m, union fw_encap *);
520
521	/*
522	 * Byte swap the encapsulation header manually.
523	 */
524	enc->ul[0] = ntohl(enc->ul[0]);
525
526	if (enc->unfrag.lf != 0) {
527		m = m_pullup(m, 2*sizeof(uint32_t));
528		if (!m)
529			return;
530		enc = mtod(m, union fw_encap *);
531		enc->ul[1] = ntohl(enc->ul[1]);
532		m = firewire_input_fragment(fc, m, src);
533		if (!m)
534			return;
535		enc = mtod(m, union fw_encap *);
536		type = enc->firstfrag.ether_type;
537		m_adj(m, 2*sizeof(uint32_t));
538	} else {
539		type = enc->unfrag.ether_type;
540		m_adj(m, sizeof(uint32_t));
541	}
542
543	if (m->m_pkthdr.rcvif == NULL) {
544		if_printf(ifp, "discard frame w/o interface pointer\n");
545		ifp->if_ierrors++;
546		m_freem(m);
547		return;
548	}
549#ifdef DIAGNOSTIC
550	if (m->m_pkthdr.rcvif != ifp) {
551		if_printf(ifp, "Warning, frame marked as received on %s\n",
552			m->m_pkthdr.rcvif->if_xname);
553	}
554#endif
555
556#ifdef MAC
557	/*
558	 * Tag the mbuf with an appropriate MAC label before any other
559	 * consumers can get to it.
560	 */
561	mac_create_mbuf_from_ifnet(ifp, m);
562#endif
563
564	/*
565	 * Give bpf a chance at the packet. The link-level driver
566	 * should have left us a tag with the EUID of the sender.
567	 */
568	if (ifp->if_bpf) {
569		struct fw_bpfhdr h;
570		struct m_tag *mtag;
571
572		mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID, 0);
573		if (mtag)
574			bcopy(mtag + 1, h.firewire_shost, 8);
575		else
576			bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
577		bcopy(&fc->fc_hwaddr, h.firewire_dhost, 8);
578		h.firewire_type = htons(type);
579		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
580	}
581
582	if (ifp->if_flags & IFF_MONITOR) {
583		/*
584		 * Interface marked for monitoring; discard packet.
585		 */
586		m_freem(m);
587		return;
588	}
589
590	ifp->if_ibytes += m->m_pkthdr.len;
591
592	/* Discard packet if interface is not up */
593	if ((ifp->if_flags & IFF_UP) == 0) {
594		m_freem(m);
595		return;
596	}
597
598	if (m->m_flags & (M_BCAST|M_MCAST))
599		ifp->if_imcasts++;
600
601	switch (type) {
602#ifdef INET
603	case ETHERTYPE_IP:
604		if (ip_fastforward(m))
605			return;
606		isr = NETISR_IP;
607		break;
608
609	case ETHERTYPE_ARP:
610	{
611		struct arphdr *ah;
612		ah = mtod(m, struct arphdr *);
613
614		/*
615		 * Adjust the arp packet to insert an empty tha slot.
616		 */
617		m->m_len += ah->ar_hln;
618		m->m_pkthdr.len += ah->ar_hln;
619		bcopy(ar_tha(ah), ar_tpa(ah), ah->ar_pln);
620		isr = NETISR_ARP;
621		break;
622	}
623#endif
624
625#ifdef INET6
626	case ETHERTYPE_IPV6:
627		isr = NETISR_IPV6;
628		break;
629#endif
630
631	default:
632		m_freem(m);
633		return;
634	}
635
636	netisr_dispatch(isr, m);
637}
638
639int
640firewire_ioctl(struct ifnet *ifp, int command, caddr_t data)
641{
642	struct ifaddr *ifa = (struct ifaddr *) data;
643	struct ifreq *ifr = (struct ifreq *) data;
644	int error = 0;
645
646	switch (command) {
647	case SIOCSIFADDR:
648		ifp->if_flags |= IFF_UP;
649
650		switch (ifa->ifa_addr->sa_family) {
651#ifdef INET
652		case AF_INET:
653			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
654			arp_ifinit(ifp, ifa);
655			break;
656#endif
657		default:
658			ifp->if_init(ifp->if_softc);
659			break;
660		}
661		break;
662
663	case SIOCGIFADDR:
664		{
665			struct sockaddr *sa;
666
667			sa = (struct sockaddr *) & ifr->ifr_data;
668			bcopy(&IFP2FWC(ifp)->fc_hwaddr,
669			    (caddr_t) sa->sa_data, sizeof(struct fw_hwaddr));
670		}
671		break;
672
673	case SIOCSIFMTU:
674		/*
675		 * Set the interface MTU.
676		 */
677		if (ifr->ifr_mtu > 1500) {
678			error = EINVAL;
679		} else {
680			ifp->if_mtu = ifr->ifr_mtu;
681		}
682		break;
683	default:
684		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
685		break;
686	}
687	return (error);
688}
689
690static int
691firewire_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
692    struct sockaddr *sa)
693{
694#ifdef INET
695	struct sockaddr_in *sin;
696#endif
697#ifdef INET6
698	struct sockaddr_in6 *sin6;
699#endif
700
701	switch(sa->sa_family) {
702	case AF_LINK:
703		/*
704		 * No mapping needed.
705		 */
706		*llsa = 0;
707		return 0;
708
709#ifdef INET
710	case AF_INET:
711		sin = (struct sockaddr_in *)sa;
712		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
713			return EADDRNOTAVAIL;
714		*llsa = 0;
715		return 0;
716#endif
717#ifdef INET6
718	case AF_INET6:
719		sin6 = (struct sockaddr_in6 *)sa;
720		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
721			/*
722			 * An IP6 address of 0 means listen to all
723			 * of the Ethernet multicast address used for IP6.
724			 * (This is used for multicast routers.)
725			 */
726			ifp->if_flags |= IFF_ALLMULTI;
727			*llsa = 0;
728			return 0;
729		}
730		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
731			return EADDRNOTAVAIL;
732		*llsa = 0;
733		return 0;
734#endif
735
736	default:
737		/*
738		 * Well, the text isn't quite right, but it's the name
739		 * that counts...
740		 */
741		return EAFNOSUPPORT;
742	}
743}
744
745void
746firewire_ifattach(struct ifnet *ifp, struct fw_hwaddr *llc)
747{
748	struct fw_com *fc = IFP2FWC(ifp);
749	struct ifaddr *ifa;
750	struct sockaddr_dl *sdl;
751	static const char* speeds[] = {
752		"S100", "S200", "S400", "S800",
753		"S1600", "S3200"
754	};
755
756	fc->fc_speed = llc->sspd;
757	STAILQ_INIT(&fc->fc_frags);
758
759	ifp->if_addrlen = sizeof(struct fw_hwaddr);
760	ifp->if_hdrlen = 0;
761	if_attach(ifp);
762	ifp->if_mtu = 1500;	/* XXX */
763	ifp->if_output = firewire_output;
764	ifp->if_resolvemulti = firewire_resolvemulti;
765	ifp->if_broadcastaddr = (u_char *) &firewire_broadcastaddr;
766
767	ifa = ifp->if_addr;
768	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
769	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
770	sdl->sdl_type = IFT_IEEE1394;
771	sdl->sdl_alen = ifp->if_addrlen;
772	bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
773
774	bpfattach(ifp, DLT_APPLE_IP_OVER_IEEE1394,
775	    sizeof(struct fw_hwaddr));
776
777	if_printf(ifp, "Firewire address: %8D @ 0x%04x%08x, %s, maxrec %d\n",
778	    (uint8_t *) &llc->sender_unique_ID_hi, ":",
779	    ntohs(llc->sender_unicast_FIFO_hi),
780	    ntohl(llc->sender_unicast_FIFO_lo),
781	    speeds[llc->sspd],
782	    (2 << llc->sender_max_rec));
783}
784
785void
786firewire_ifdetach(struct ifnet *ifp)
787{
788	bpfdetach(ifp);
789	if_detach(ifp);
790}
791
792void
793firewire_busreset(struct ifnet *ifp)
794{
795	struct fw_com *fc = IFP2FWC(ifp);
796	struct fw_reass *r;
797	struct mbuf *m;
798
799	/*
800	 * Discard any partial datagrams since the host ids may have changed.
801	 */
802	while ((r = STAILQ_FIRST(&fc->fc_frags))) {
803		STAILQ_REMOVE_HEAD(&fc->fc_frags, fr_link);
804		while (r->fr_frags) {
805			m = r->fr_frags;
806			r->fr_frags = m->m_nextpkt;
807			m_freem(m);
808		}
809		free(r, M_TEMP);
810	}
811}
812
813static void *
814firewire_alloc(u_char type, struct ifnet *ifp)
815{
816	struct fw_com	*fc;
817
818	fc = malloc(sizeof(struct fw_com), M_FWCOM, M_WAITOK | M_ZERO);
819	fc->fc_ifp = ifp;
820
821	return (fc);
822}
823
824static void
825firewire_free(void *com, u_char type)
826{
827
828	free(com, M_FWCOM);
829}
830
831static int
832firewire_modevent(module_t mod, int type, void *data)
833{
834
835	switch (type) {
836	case MOD_LOAD:
837		if_register_com_alloc(IFT_IEEE1394,
838		    firewire_alloc, firewire_free);
839		break;
840	case MOD_UNLOAD:
841		if_deregister_com_alloc(IFT_IEEE1394);
842		break;
843	default:
844		return (EOPNOTSUPP);
845	}
846
847	return (0);
848}
849
850static moduledata_t firewire_mod = {
851	"if_firewire",
852	firewire_modevent,
853	0
854};
855
856DECLARE_MODULE(if_firewire, firewire_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
857MODULE_VERSION(if_firewire, 1);
858