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