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