xref: /netbsd/sys/net/if_ieee1394subr.c (revision 6550d01e)
1 /*	$NetBSD: if_ieee1394subr.c,v 1.45 2010/04/05 07:22:23 joerg Exp $	*/
2 
3 /*
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Atsushi Onoe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_ieee1394subr.c,v 1.45 2010/04/05 07:22:23 joerg Exp $");
34 
35 #include "opt_inet.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/device.h>
41 #include <sys/kernel.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/select.h>
46 
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_ieee1394.h>
50 #include <net/if_types.h>
51 #include <net/if_media.h>
52 #include <net/ethertypes.h>
53 #include <net/netisr.h>
54 #include <net/route.h>
55 
56 #include <net/bpf.h>
57 
58 #ifdef INET
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/if_inarp.h>
62 #endif /* INET */
63 #ifdef INET6
64 #include <netinet/in.h>
65 #include <netinet6/in6_var.h>
66 #include <netinet6/nd6.h>
67 #endif /* INET6 */
68 
69 #include <dev/ieee1394/firewire.h>
70 
71 #include <dev/ieee1394/firewirereg.h>
72 #include <dev/ieee1394/iec13213.h>
73 #include <dev/ieee1394/if_fwipvar.h>
74 
75 #define	IEEE1394_REASS_TIMEOUT	3	/* 3 sec */
76 
77 #define	senderr(e)	do { error = (e); goto bad; } while(0/*CONSTCOND*/)
78 
79 static int  ieee1394_output(struct ifnet *, struct mbuf *,
80 		const struct sockaddr *, struct rtentry *);
81 static struct mbuf *ieee1394_reass(struct ifnet *, struct mbuf *, uint16_t);
82 
83 static int
84 ieee1394_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
85     struct rtentry *rt0)
86 {
87 	uint16_t etype = 0;
88 	struct mbuf *m;
89 	int s, hdrlen, error = 0;
90 	struct rtentry *rt;
91 	struct mbuf *mcopy = NULL;
92 	struct ieee1394_hwaddr *hwdst, baddr;
93 	const struct ieee1394_hwaddr *myaddr;
94 	ALTQ_DECL(struct altq_pktattr pktattr;)
95 #ifdef INET
96 	struct arphdr *ah;
97 #endif /* INET */
98 	struct m_tag *mtag;
99 	int unicast;
100 
101 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
102 		senderr(ENETDOWN);
103 	if ((rt = rt0) != NULL) {
104 		if ((rt->rt_flags & RTF_UP) == 0) {
105 			if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
106 				rt->rt_refcnt--;
107 				if (rt->rt_ifp != ifp)
108 					return (*rt->rt_ifp->if_output)
109 							(ifp, m0, dst, rt);
110 			} else
111 				senderr(EHOSTUNREACH);
112 		}
113 		if (rt->rt_flags & RTF_GATEWAY) {
114 			if (rt->rt_gwroute == NULL)
115 				goto lookup;
116 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
117 				rtfree(rt);
118 				rt = rt0;
119   lookup:
120 				rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
121 				if ((rt = rt->rt_gwroute) == NULL)
122 					senderr(EHOSTUNREACH);
123 				/* the "G" test below also prevents rt == rt0 */
124 				if ((rt->rt_flags & RTF_GATEWAY) ||
125 				    (rt->rt_ifp != ifp)) {
126 					rt->rt_refcnt--;
127 					rt0->rt_gwroute = NULL;
128 					senderr(EHOSTUNREACH);
129 				}
130 			}
131 		}
132 		if (rt->rt_flags & RTF_REJECT)
133 			if (rt->rt_rmx.rmx_expire == 0 ||
134 			    time_second < rt->rt_rmx.rmx_expire)
135 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
136 	}
137 
138 	/*
139 	 * If the queueing discipline needs packet classification,
140 	 * do it before prepending link headers.
141 	 */
142 	IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
143 
144 	/*
145 	 * For unicast, we make a tag to store the lladdr of the
146 	 * destination. This might not be the first time we have seen
147 	 * the packet (for instance, the arp code might be trying to
148 	 * re-send it after receiving an arp reply) so we only
149 	 * allocate a tag if there isn't one there already. For
150 	 * multicast, we will eventually use a different tag to store
151 	 * the channel number.
152 	 */
153 	unicast = !(m0->m_flags & (M_BCAST | M_MCAST));
154 	if (unicast) {
155 		mtag =
156 		    m_tag_find(m0, MTAG_FIREWIRE_HWADDR, NULL);
157 		if (!mtag) {
158 			mtag = m_tag_get(MTAG_FIREWIRE_HWADDR,
159 			    sizeof (struct ieee1394_hwaddr), M_NOWAIT);
160 			if (!mtag) {
161 				error = ENOMEM;
162 				goto bad;
163 			}
164 			m_tag_prepend(m0, mtag);
165 		}
166 		hwdst = (struct ieee1394_hwaddr *)(mtag + 1);
167 	} else {
168 		hwdst = &baddr;
169 	}
170 
171 	switch (dst->sa_family) {
172 #ifdef INET
173 	case AF_INET:
174 		if (unicast && (!arpresolve(ifp, rt, m0, dst, (u_char *)hwdst)))
175 			return 0;	/* if not yet resolved */
176 		/* if broadcasting on a simplex interface, loopback a copy */
177 		if ((m0->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
178 			mcopy = m_copy(m0, 0, M_COPYALL);
179 		etype = htons(ETHERTYPE_IP);
180 		break;
181 	case AF_ARP:
182 		ah = mtod(m0, struct arphdr *);
183 		ah->ar_hrd = htons(ARPHRD_IEEE1394);
184 		etype = htons(ETHERTYPE_ARP);
185 		break;
186 #endif /* INET */
187 #ifdef INET6
188 	case AF_INET6:
189 		if (unicast && (!nd6_storelladdr(ifp, rt, m0, dst,
190 		    hwdst->iha_uid, IEEE1394_ADDR_LEN))) {
191 			/* something bad happened */
192 			return 0;
193 		}
194 		etype = htons(ETHERTYPE_IPV6);
195 		break;
196 #endif /* INET6 */
197 
198 	case pseudo_AF_HDRCMPLT:
199 	case AF_UNSPEC:
200 		/* TODO? */
201 	default:
202 		printf("%s: can't handle af%d\n", ifp->if_xname,
203 		    dst->sa_family);
204 		senderr(EAFNOSUPPORT);
205 		break;
206 	}
207 
208 	if (mcopy)
209 		looutput(ifp, mcopy, dst, rt);
210 	myaddr = (const struct ieee1394_hwaddr *)CLLADDR(ifp->if_sadl);
211 	if (ifp->if_bpf) {
212 		struct ieee1394_bpfhdr h;
213 		if (unicast)
214 			memcpy(h.ibh_dhost, hwdst->iha_uid, 8);
215 		else
216 			memcpy(h.ibh_dhost,
217 			    ((const struct ieee1394_hwaddr *)
218 			    ifp->if_broadcastaddr)->iha_uid, 8);
219 		memcpy(h.ibh_shost, myaddr->iha_uid, 8);
220 		h.ibh_type = etype;
221 		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m0);
222 	}
223 	if ((ifp->if_flags & IFF_SIMPLEX) &&
224 	    unicast &&
225 	    memcmp(hwdst, myaddr, IEEE1394_ADDR_LEN) == 0)
226 		return looutput(ifp, m0, dst, rt);
227 
228 	/*
229 	 * XXX:
230 	 * The maximum possible rate depends on the topology.
231 	 * So the determination of maxrec and fragmentation should be
232 	 * called from the driver after probing the topology map.
233 	 */
234 	if (unicast) {
235 		hdrlen = IEEE1394_GASP_LEN;
236 		hwdst->iha_speed = 0;	/* XXX */
237 	} else
238 		hdrlen = 0;
239 
240 	if (hwdst->iha_speed > myaddr->iha_speed)
241 		hwdst->iha_speed = myaddr->iha_speed;
242 	if (hwdst->iha_maxrec > myaddr->iha_maxrec)
243 		hwdst->iha_maxrec = myaddr->iha_maxrec;
244 	if (hwdst->iha_maxrec > (8 + hwdst->iha_speed))
245 		hwdst->iha_maxrec = 8 + hwdst->iha_speed;
246 	if (hwdst->iha_maxrec < 8)
247 			hwdst->iha_maxrec = 8;
248 
249 	m0 = ieee1394_fragment(ifp, m0, (2<<hwdst->iha_maxrec) - hdrlen, etype);
250 	if (m0 == NULL)
251 		senderr(ENOBUFS);
252 
253 	s = splnet();
254 	ifp->if_obytes += m0->m_pkthdr.len;
255 	if (m0->m_flags & M_MCAST)
256 		ifp->if_omcasts++;
257 	while ((m = m0) != NULL) {
258 		m0 = m->m_nextpkt;
259 		if (m == NULL) {
260 			splx(s);
261 			senderr(ENOBUFS);
262 		}
263 		IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
264 		if (error) {
265 			/* mbuf is already freed */
266 			splx(s);
267 			goto bad;
268 		}
269 	}
270 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
271 		(*ifp->if_start)(ifp);
272 	splx(s);
273 	return 0;
274 
275   bad:
276 	while (m0 != NULL) {
277 		m = m0->m_nextpkt;
278 		m_freem(m0);
279 		m0 = m;
280 	}
281 
282 	return error;
283 }
284 
285 struct mbuf *
286 ieee1394_fragment(struct ifnet *ifp, struct mbuf *m0, int maxsize,
287     uint16_t etype)
288 {
289 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
290 	int totlen, fraglen, off;
291 	struct mbuf *m, **mp;
292 	struct ieee1394_fraghdr *ifh;
293 	struct ieee1394_unfraghdr *iuh;
294 
295 	totlen = m0->m_pkthdr.len;
296 	if (totlen + sizeof(struct ieee1394_unfraghdr) <= maxsize) {
297 		M_PREPEND(m0, sizeof(struct ieee1394_unfraghdr), M_DONTWAIT);
298 		if (m0 == NULL)
299 			goto bad;
300 		iuh = mtod(m0, struct ieee1394_unfraghdr *);
301 		iuh->iuh_ft = 0;
302 		iuh->iuh_etype = etype;
303 		return m0;
304 	}
305 
306 	fraglen = maxsize - sizeof(struct ieee1394_fraghdr);
307 
308 	M_PREPEND(m0, sizeof(struct ieee1394_fraghdr), M_DONTWAIT);
309 	if (m0 == NULL)
310 		goto bad;
311 	ifh = mtod(m0, struct ieee1394_fraghdr *);
312 	ifh->ifh_ft_size = htons(IEEE1394_FT_MORE | (totlen - 1));
313 	ifh->ifh_etype_off = etype;
314 	ifh->ifh_dgl = htons(ic->ic_dgl);
315 	ifh->ifh_reserved = 0;
316 	off = fraglen;
317 	mp = &m0->m_nextpkt;
318 	while (off < totlen) {
319 		if (off + fraglen > totlen)
320 			fraglen = totlen - off;
321 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
322 		if (m == NULL)
323 			goto bad;
324 		m->m_flags |= m0->m_flags & (M_BCAST|M_MCAST);	/* copy bcast */
325 		MH_ALIGN(m, sizeof(struct ieee1394_fraghdr));
326 		m->m_len = sizeof(struct ieee1394_fraghdr);
327 		ifh = mtod(m, struct ieee1394_fraghdr *);
328 		ifh->ifh_ft_size =
329 		    htons(IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE | (totlen - 1));
330 		ifh->ifh_etype_off = htons(off);
331 		ifh->ifh_dgl = htons(ic->ic_dgl);
332 		ifh->ifh_reserved = 0;
333 		m->m_next = m_copy(m0, sizeof(*ifh) + off, fraglen);
334 		if (m->m_next == NULL)
335 			goto bad;
336 		m->m_pkthdr.len = sizeof(*ifh) + fraglen;
337 		off += fraglen;
338 		*mp = m;
339 		mp = &m->m_nextpkt;
340 	}
341 	ifh->ifh_ft_size &= ~htons(IEEE1394_FT_MORE);	/* last fragment */
342 	m_adj(m0, -(m0->m_pkthdr.len - maxsize));
343 
344 	ic->ic_dgl++;
345 	return m0;
346 
347   bad:
348 	while ((m = m0) != NULL) {
349 		m0 = m->m_nextpkt;
350 		m->m_nextpkt = NULL;
351 		m_freem(m);
352 	}
353 	return NULL;
354 }
355 
356 void
357 ieee1394_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
358 {
359 	struct ifqueue *inq;
360 	uint16_t etype;
361 	int s;
362 	struct ieee1394_unfraghdr *iuh;
363 
364 	if ((ifp->if_flags & IFF_UP) == 0) {
365 		m_freem(m);
366 		return;
367 	}
368 	if (m->m_len < sizeof(*iuh)) {
369 		if ((m = m_pullup(m, sizeof(*iuh))) == NULL)
370 			return;
371 	}
372 
373 	iuh = mtod(m, struct ieee1394_unfraghdr *);
374 
375 	if (ntohs(iuh->iuh_ft) & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE)) {
376 		if ((m = ieee1394_reass(ifp, m, src)) == NULL)
377 			return;
378 		iuh = mtod(m, struct ieee1394_unfraghdr *);
379 	}
380 	etype = ntohs(iuh->iuh_etype);
381 
382 	/* strip off the ieee1394 header */
383 	m_adj(m, sizeof(*iuh));
384 	if (ifp->if_bpf) {
385 		struct ieee1394_bpfhdr h;
386 		struct m_tag *mtag;
387 		const struct ieee1394_hwaddr *myaddr;
388 
389 		mtag = m_tag_find(m, MTAG_FIREWIRE_SENDER_EUID, 0);
390 		if (mtag)
391 			memcpy(h.ibh_shost, mtag + 1, 8);
392 		else
393 			memset(h.ibh_shost, 0, 8);
394 		if (m->m_flags & M_BCAST)
395 			memcpy(h.ibh_dhost,
396 			    ((const struct ieee1394_hwaddr *)
397 			    ifp->if_broadcastaddr)->iha_uid, 8);
398 		else {
399 			myaddr =
400 			  (const struct ieee1394_hwaddr *)CLLADDR(ifp->if_sadl);
401 			memcpy(h.ibh_dhost, myaddr->iha_uid, 8);
402 		}
403 		h.ibh_type = htons(etype);
404 		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
405 	}
406 
407 	switch (etype) {
408 #ifdef INET
409 	case ETHERTYPE_IP:
410 		schednetisr(NETISR_IP);
411 		inq = &ipintrq;
412 		break;
413 
414 	case ETHERTYPE_ARP:
415 		schednetisr(NETISR_ARP);
416 		inq = &arpintrq;
417 		break;
418 #endif /* INET */
419 
420 #ifdef INET6
421 	case ETHERTYPE_IPV6:
422 		schednetisr(NETISR_IPV6);
423 		inq = &ip6intrq;
424 		break;
425 #endif /* INET6 */
426 
427 	default:
428 		m_freem(m);
429 		return;
430 	}
431 
432 	s = splnet();
433 	if (IF_QFULL(inq)) {
434 		IF_DROP(inq);
435 		m_freem(m);
436 	} else
437 		IF_ENQUEUE(inq, m);
438 	splx(s);
439 }
440 
441 static struct mbuf *
442 ieee1394_reass(struct ifnet *ifp, struct mbuf *m0, uint16_t src)
443 {
444 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
445 	struct ieee1394_fraghdr *ifh;
446 	struct ieee1394_unfraghdr *iuh;
447 	struct ieee1394_reassq *rq;
448 	struct ieee1394_reass_pkt *rp, *trp, *nrp = NULL;
449 	int len;
450 	uint16_t etype, off, ftype, size, dgl;
451 	uint32_t id;
452 
453 	if (m0->m_len < sizeof(*ifh)) {
454 		if ((m0 = m_pullup(m0, sizeof(*ifh))) == NULL)
455 			return NULL;
456 	}
457 	ifh = mtod(m0, struct ieee1394_fraghdr *);
458 	m_adj(m0, sizeof(*ifh));
459 	size = ntohs(ifh->ifh_ft_size);
460 	ftype = size & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE);
461 	size = (size & ~ftype) + 1;
462 	dgl = ntohs(ifh->ifh_dgl);
463 	len = m0->m_pkthdr.len;
464 	id = dgl | (src << 16);
465 	if (ftype & IEEE1394_FT_SUBSEQ) {
466 		m_tag_delete_chain(m0, NULL);
467 		m0->m_flags &= ~M_PKTHDR;
468 		etype = 0;
469 		off = ntohs(ifh->ifh_etype_off);
470 	} else {
471 		etype = ifh->ifh_etype_off;
472 		off = 0;
473 	}
474 
475 	for (rq = LIST_FIRST(&ic->ic_reassq); ; rq = LIST_NEXT(rq, rq_node)) {
476 		if (rq == NULL) {
477 			/*
478 			 * Create a new reassemble queue head for the node.
479 			 */
480 			rq = malloc(sizeof(*rq), M_FTABLE, M_NOWAIT);
481 			if (rq == NULL) {
482 				m_freem(m0);
483 				return NULL;
484 			}
485 			rq->fr_id = id;
486 			LIST_INIT(&rq->rq_pkt);
487 			LIST_INSERT_HEAD(&ic->ic_reassq, rq, rq_node);
488 			break;
489 		}
490 		if (rq->fr_id == id)
491 			break;
492 	}
493 	for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
494 		nrp = LIST_NEXT(rp, rp_next);
495 		if (rp->rp_dgl != dgl)
496 			continue;
497 		/*
498 		 * sanity check:
499 		 * datagram size must be same for all fragments, and
500 		 * no overlap is allowed.
501 		 */
502 		if (rp->rp_size != size ||
503 		    (off < rp->rp_off + rp->rp_len && off + len > rp->rp_off)) {
504 			/*
505 			 * This happens probably due to wrapping dgl value.
506 			 * Destroy all previously received fragment and
507 			 * enqueue current fragment.
508 			 */
509 			for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL;
510 			    rp = nrp) {
511 				nrp = LIST_NEXT(rp, rp_next);
512 				if (rp->rp_dgl == dgl) {
513 					LIST_REMOVE(rp, rp_next);
514 					m_freem(rp->rp_m);
515 					free(rp, M_FTABLE);
516 				}
517 			}
518 			break;
519 		}
520 		if (rp->rp_off + rp->rp_len == off) {
521 			/*
522 			 * All the subsequent fragments received in sequence
523 			 * come here.
524 			 * Concatinate mbuf to previous one instead of
525 			 * allocating new reassemble queue structure,
526 			 * and try to merge more with the subsequent fragment
527 			 * in the queue.
528 			 */
529 			m_cat(rp->rp_m, m0);
530 			rp->rp_len += len;
531 			while (rp->rp_off + rp->rp_len < size &&
532 			    nrp != NULL && nrp->rp_dgl == dgl &&
533 			    nrp->rp_off == rp->rp_off + rp->rp_len) {
534 				LIST_REMOVE(nrp, rp_next);
535 				m_cat(rp->rp_m, nrp->rp_m);
536 				rp->rp_len += nrp->rp_len;
537 				free(nrp, M_FTABLE);
538 				nrp = LIST_NEXT(rp, rp_next);
539 			}
540 			m0 = NULL;	/* mark merged */
541 			break;
542 		}
543 		if (off + m0->m_pkthdr.len == rp->rp_off) {
544 			m_cat(m0, rp->rp_m);
545 			rp->rp_m = m0;
546 			rp->rp_off = off;
547 			rp->rp_etype = etype;	 /* over writing trust etype */
548 			rp->rp_len += len;
549 			m0 = NULL;	/* mark merged */
550 			break;
551 		}
552 		if (rp->rp_off > off) {
553 			/* insert before rp */
554 			nrp = rp;
555 			break;
556 		}
557 		if (nrp == NULL || nrp->rp_dgl != dgl) {
558 			/* insert after rp */
559 			nrp = NULL;
560 			break;
561 		}
562 	}
563 	if (m0 == NULL) {
564 		if (rp->rp_off != 0 || rp->rp_len != size)
565 			return NULL;
566 		/* fragment done */
567 		LIST_REMOVE(rp, rp_next);
568 		m0 = rp->rp_m;
569 		m0->m_pkthdr.len = rp->rp_len;
570 		M_PREPEND(m0, sizeof(*iuh), M_DONTWAIT);
571 		if (m0 != NULL) {
572 			iuh = mtod(m0, struct ieee1394_unfraghdr *);
573 			iuh->iuh_ft = 0;
574 			iuh->iuh_etype = rp->rp_etype;
575 		}
576 		free(rp, M_FTABLE);
577 		return m0;
578 	}
579 
580 	/*
581 	 * New fragment received.  Allocate reassemble queue structure.
582 	 */
583 	trp = malloc(sizeof(*trp), M_FTABLE, M_NOWAIT);
584 	if (trp == NULL) {
585 		m_freem(m0);
586 		return NULL;
587 	}
588 	trp->rp_m = m0;
589 	trp->rp_size = size;
590 	trp->rp_etype = etype;		 /* valid only if off==0 */
591 	trp->rp_off = off;
592 	trp->rp_dgl = dgl;
593 	trp->rp_len = len;
594 	trp->rp_ttl = IEEE1394_REASS_TIMEOUT;
595 	if (trp->rp_ttl <= ifp->if_timer)
596 		trp->rp_ttl = ifp->if_timer + 1;
597 
598 	if (rp == NULL) {
599 		/* first fragment for the dgl */
600 		LIST_INSERT_HEAD(&rq->rq_pkt, trp, rp_next);
601 	} else if (nrp == NULL) {
602 		/* no next fragment for the dgl */
603 		LIST_INSERT_AFTER(rp, trp, rp_next);
604 	} else {
605 		/* there is a hole */
606 		LIST_INSERT_BEFORE(nrp, trp, rp_next);
607 	}
608 	return NULL;
609 }
610 
611 void
612 ieee1394_drain(struct ifnet *ifp)
613 {
614 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
615 	struct ieee1394_reassq *rq;
616 	struct ieee1394_reass_pkt *rp;
617 
618 	while ((rq = LIST_FIRST(&ic->ic_reassq)) != NULL) {
619 		LIST_REMOVE(rq, rq_node);
620 		while ((rp = LIST_FIRST(&rq->rq_pkt)) != NULL) {
621 			LIST_REMOVE(rp, rp_next);
622 			m_freem(rp->rp_m);
623 			free(rp, M_FTABLE);
624 		}
625 		free(rq, M_FTABLE);
626 	}
627 }
628 
629 void
630 ieee1394_watchdog(struct ifnet *ifp)
631 {
632 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
633 	struct ieee1394_reassq *rq;
634 	struct ieee1394_reass_pkt *rp, *nrp;
635 	int dec;
636 
637 	dec = (ifp->if_timer > 0) ? ifp->if_timer : 1;
638 	for (rq = LIST_FIRST(&ic->ic_reassq); rq != NULL;
639 	    rq = LIST_NEXT(rq, rq_node)) {
640 		for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
641 			nrp = LIST_NEXT(rp, rp_next);
642 			if (rp->rp_ttl >= dec)
643 				rp->rp_ttl -= dec;
644 			else {
645 				LIST_REMOVE(rp, rp_next);
646 				m_freem(rp->rp_m);
647 				free(rp, M_FTABLE);
648 			}
649 		}
650 	}
651 }
652 
653 const char *
654 ieee1394_sprintf(const uint8_t *laddr)
655 {
656 	static char buf[3*8];
657 
658 	snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
659 	    laddr[0], laddr[1], laddr[2], laddr[3],
660 	    laddr[4], laddr[5], laddr[6], laddr[7]);
661 	return buf;
662 }
663 
664 void
665 ieee1394_ifattach(struct ifnet *ifp, const struct ieee1394_hwaddr *hwaddr)
666 {
667 	struct ieee1394_hwaddr *baddr;
668 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
669 
670 	ifp->if_type = IFT_IEEE1394;
671 	ifp->if_hdrlen = sizeof(struct ieee1394_header);
672 	ifp->if_dlt = DLT_EN10MB;	/* XXX */
673 	ifp->if_mtu = IEEE1394MTU;
674 	ifp->if_output = ieee1394_output;
675 	ifp->if_drain = ieee1394_drain;
676 	ifp->if_watchdog = ieee1394_watchdog;
677 	ifp->if_timer = 1;
678 	if (ifp->if_baudrate == 0)
679 		ifp->if_baudrate = IF_Mbps(100);
680 
681 	if_set_sadl(ifp, hwaddr, sizeof(struct ieee1394_hwaddr), true);
682 
683 	baddr = malloc(ifp->if_addrlen, M_DEVBUF, M_WAITOK);
684 	memset(baddr->iha_uid, 0xff, IEEE1394_ADDR_LEN);
685 	baddr->iha_speed = 0;	/*XXX: how to determine the speed for bcast? */
686 	baddr->iha_maxrec = 512 << baddr->iha_speed;
687 	memset(baddr->iha_offset, 0, sizeof(baddr->iha_offset));
688 	ifp->if_broadcastaddr = (uint8_t *)baddr;
689 	LIST_INIT(&ic->ic_reassq);
690 	bpf_attach(ifp, DLT_APPLE_IP_OVER_IEEE1394,
691 	    sizeof(struct ieee1394_hwaddr));
692 }
693 
694 void
695 ieee1394_ifdetach(struct ifnet *ifp)
696 {
697 	ieee1394_drain(ifp);
698 	bpf_detach(ifp);
699 	free(__UNCONST(ifp->if_broadcastaddr), M_DEVBUF);
700 	ifp->if_broadcastaddr = NULL;
701 #if 0	/* done in if_detach() */
702 	if_free_sadl(ifp);
703 #endif
704 }
705 
706 int
707 ieee1394_ioctl(struct ifnet *ifp, u_long cmd, void *data)
708 {
709 	struct ifreq *ifr = (struct ifreq *)data;
710 	struct ifaddr *ifa = (struct ifaddr *)data;
711 	int error = 0;
712 
713 	switch (cmd) {
714 	case SIOCINITIFADDR:
715 		ifp->if_flags |= IFF_UP;
716 		switch (ifa->ifa_addr->sa_family) {
717 #ifdef INET
718 		case AF_INET:
719 			if ((error = (*ifp->if_init)(ifp)) != 0)
720 				break;
721 			arp_ifinit(ifp, ifa);
722 			break;
723 #endif /* INET */
724 		default:
725 			error = (*ifp->if_init)(ifp);
726 			break;
727 		}
728 		break;
729 
730 	case SIOCSIFMTU:
731 		if (ifr->ifr_mtu > IEEE1394MTU)
732 			error = EINVAL;
733 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
734 			error = 0;
735 		break;
736 
737 	default:
738 		error = ifioctl_common(ifp, cmd, data);
739 		break;
740 	}
741 
742 	return error;
743 }
744