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