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