xref: /netbsd-src/sys/net/if_ieee1394subr.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /*	$NetBSD: if_ieee1394subr.c,v 1.60 2018/04/26 19:56:55 maxv 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.60 2018/04/26 19:56:55 maxv 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 *, const 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     const struct rtentry *rt)
88 {
89 	uint16_t etype = 0;
90 	struct mbuf *m;
91 	int hdrlen, error = 0;
92 	struct mbuf *mcopy = NULL;
93 	struct ieee1394_hwaddr *hwdst, baddr;
94 	const struct ieee1394_hwaddr *myaddr;
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 
104 	/*
105 	 * If the queueing discipline needs packet classification,
106 	 * do it before prepending link headers.
107 	 */
108 	IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family);
109 
110 	/*
111 	 * For unicast, we make a tag to store the lladdr of the
112 	 * destination. This might not be the first time we have seen
113 	 * the packet (for instance, the arp code might be trying to
114 	 * re-send it after receiving an arp reply) so we only
115 	 * allocate a tag if there isn't one there already. For
116 	 * multicast, we will eventually use a different tag to store
117 	 * the channel number.
118 	 */
119 	unicast = !(m0->m_flags & (M_BCAST | M_MCAST));
120 	if (unicast) {
121 		mtag =
122 		    m_tag_find(m0, MTAG_FIREWIRE_HWADDR, NULL);
123 		if (!mtag) {
124 			mtag = m_tag_get(MTAG_FIREWIRE_HWADDR,
125 			    sizeof (struct ieee1394_hwaddr), M_NOWAIT);
126 			if (!mtag) {
127 				error = ENOMEM;
128 				goto bad;
129 			}
130 			m_tag_prepend(m0, mtag);
131 		}
132 		hwdst = (struct ieee1394_hwaddr *)(mtag + 1);
133 	} else {
134 		hwdst = &baddr;
135 	}
136 
137 	switch (dst->sa_family) {
138 #ifdef INET
139 	case AF_INET:
140 		if (unicast &&
141 		    (error = arpresolve(ifp, rt, m0, dst, hwdst,
142 			sizeof(*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_copym(m0, 0, M_COPYALL, M_DONTWAIT);
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 0
158 		/*
159 		 * XXX This code was in nd6_storelladdr, which was replaced with
160 		 * nd6_resolve, but it never be used because nd6_storelladdr was
161 		 * called only if unicast. Should it be enabled?
162 		 */
163 		if (m0->m_flags & M_BCAST)
164 			memcpy(hwdst->iha_uid, ifp->if_broadcastaddr,
165 			    MIN(IEEE1394_ADDR_LEN, ifp->if_addrlen));
166 #endif
167 		if (unicast) {
168 			error = nd6_resolve(ifp, rt, m0, dst, hwdst->iha_uid,
169 			    IEEE1394_ADDR_LEN);
170 			if (error != 0)
171 				return error == EWOULDBLOCK ? 0 : error;
172 		}
173 		etype = htons(ETHERTYPE_IPV6);
174 		break;
175 #endif /* INET6 */
176 
177 	case pseudo_AF_HDRCMPLT:
178 	case AF_UNSPEC:
179 		/* TODO? */
180 	default:
181 		printf("%s: can't handle af%d\n", ifp->if_xname,
182 		    dst->sa_family);
183 		senderr(EAFNOSUPPORT);
184 		break;
185 	}
186 
187 	if (mcopy)
188 		looutput(ifp, mcopy, dst, rt);
189 	myaddr = (const struct ieee1394_hwaddr *)CLLADDR(ifp->if_sadl);
190 	if (ifp->if_bpf) {
191 		struct ieee1394_bpfhdr h;
192 		if (unicast)
193 			memcpy(h.ibh_dhost, hwdst->iha_uid, 8);
194 		else
195 			memcpy(h.ibh_dhost,
196 			    ((const struct ieee1394_hwaddr *)
197 			    ifp->if_broadcastaddr)->iha_uid, 8);
198 		memcpy(h.ibh_shost, myaddr->iha_uid, 8);
199 		h.ibh_type = etype;
200 		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m0);
201 	}
202 	if ((ifp->if_flags & IFF_SIMPLEX) &&
203 	    unicast &&
204 	    memcmp(hwdst, myaddr, IEEE1394_ADDR_LEN) == 0)
205 		return looutput(ifp, m0, dst, rt);
206 
207 	/*
208 	 * XXX:
209 	 * The maximum possible rate depends on the topology.
210 	 * So the determination of maxrec and fragmentation should be
211 	 * called from the driver after probing the topology map.
212 	 */
213 	if (unicast) {
214 		hdrlen = IEEE1394_GASP_LEN;
215 		hwdst->iha_speed = 0;	/* XXX */
216 	} else
217 		hdrlen = 0;
218 
219 	if (hwdst->iha_speed > myaddr->iha_speed)
220 		hwdst->iha_speed = myaddr->iha_speed;
221 	if (hwdst->iha_maxrec > myaddr->iha_maxrec)
222 		hwdst->iha_maxrec = myaddr->iha_maxrec;
223 	if (hwdst->iha_maxrec > (8 + hwdst->iha_speed))
224 		hwdst->iha_maxrec = 8 + hwdst->iha_speed;
225 	if (hwdst->iha_maxrec < 8)
226 			hwdst->iha_maxrec = 8;
227 
228 	m0 = ieee1394_fragment(ifp, m0, (2<<hwdst->iha_maxrec) - hdrlen, etype);
229 	if (m0 == NULL)
230 		senderr(ENOBUFS);
231 
232 	while ((m = m0) != NULL) {
233 		m0 = m->m_nextpkt;
234 
235 		error = if_transmit_lock(ifp, m);
236 		if (error) {
237 			/* mbuf is already freed */
238 			goto bad;
239 		}
240 	}
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_copym(m0, sizeof(*ifh) + off, fraglen, M_DONTWAIT);
302 		if (m->m_next == NULL) {
303 			m_freem(m);
304 			goto bad;
305 		}
306 		m->m_pkthdr.len = sizeof(*ifh) + fraglen;
307 		off += fraglen;
308 		*mp = m;
309 		mp = &m->m_nextpkt;
310 	}
311 	ifh->ifh_ft_size &= ~htons(IEEE1394_FT_MORE);	/* last fragment */
312 	m_adj(m0, -(m0->m_pkthdr.len - maxsize));
313 
314 	ic->ic_dgl++;
315 	return m0;
316 
317   bad:
318 	while ((m = m0) != NULL) {
319 		m0 = m->m_nextpkt;
320 		m->m_nextpkt = NULL;
321 		m_freem(m);
322 	}
323 	return NULL;
324 }
325 
326 void
327 ieee1394_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
328 {
329 	pktqueue_t *pktq = NULL;
330 	struct ifqueue *inq;
331 	uint16_t etype;
332 	struct ieee1394_unfraghdr *iuh;
333 	int isr = 0;
334 
335 	if ((ifp->if_flags & IFF_UP) == 0) {
336 		m_freem(m);
337 		return;
338 	}
339 	if (m->m_len < sizeof(*iuh)) {
340 		if ((m = m_pullup(m, sizeof(*iuh))) == NULL)
341 			return;
342 	}
343 
344 	iuh = mtod(m, struct ieee1394_unfraghdr *);
345 
346 	if (ntohs(iuh->iuh_ft) & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE)) {
347 		if ((m = ieee1394_reass(ifp, m, src)) == NULL)
348 			return;
349 		iuh = mtod(m, struct ieee1394_unfraghdr *);
350 	}
351 	etype = ntohs(iuh->iuh_etype);
352 
353 	/* strip off the ieee1394 header */
354 	m_adj(m, sizeof(*iuh));
355 	if (ifp->if_bpf) {
356 		struct ieee1394_bpfhdr h;
357 		struct m_tag *mtag;
358 		const struct ieee1394_hwaddr *myaddr;
359 
360 		mtag = m_tag_find(m, MTAG_FIREWIRE_SENDER_EUID, 0);
361 		if (mtag)
362 			memcpy(h.ibh_shost, mtag + 1, 8);
363 		else
364 			memset(h.ibh_shost, 0, 8);
365 		if (m->m_flags & M_BCAST)
366 			memcpy(h.ibh_dhost,
367 			    ((const struct ieee1394_hwaddr *)
368 			    ifp->if_broadcastaddr)->iha_uid, 8);
369 		else {
370 			myaddr =
371 			  (const struct ieee1394_hwaddr *)CLLADDR(ifp->if_sadl);
372 			memcpy(h.ibh_dhost, myaddr->iha_uid, 8);
373 		}
374 		h.ibh_type = htons(etype);
375 		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
376 	}
377 
378 	switch (etype) {
379 #ifdef INET
380 	case ETHERTYPE_IP:
381 		pktq = ip_pktq;
382 		break;
383 
384 	case ETHERTYPE_ARP:
385 		isr = NETISR_ARP;
386 		inq = &arpintrq;
387 		break;
388 #endif /* INET */
389 
390 #ifdef INET6
391 	case ETHERTYPE_IPV6:
392 		pktq = ip6_pktq;
393 		break;
394 #endif /* INET6 */
395 
396 	default:
397 		m_freem(m);
398 		return;
399 	}
400 
401 	if (__predict_true(pktq)) {
402 		if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
403 			m_freem(m);
404 		}
405 		return;
406 	}
407 
408 	IFQ_LOCK(inq);
409 	if (IF_QFULL(inq)) {
410 		IF_DROP(inq);
411 		IFQ_UNLOCK(inq);
412 		m_freem(m);
413 	} else {
414 		IF_ENQUEUE(inq, m);
415 		IFQ_UNLOCK(inq);
416 		schednetisr(isr);
417 	}
418 }
419 
420 static struct mbuf *
421 ieee1394_reass(struct ifnet *ifp, struct mbuf *m0, uint16_t src)
422 {
423 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
424 	struct ieee1394_fraghdr *ifh;
425 	struct ieee1394_unfraghdr *iuh;
426 	struct ieee1394_reassq *rq;
427 	struct ieee1394_reass_pkt *rp, *trp, *nrp = NULL;
428 	int len;
429 	uint16_t etype, off, ftype, size, dgl;
430 	uint32_t id;
431 
432 	if (m0->m_len < sizeof(*ifh)) {
433 		if ((m0 = m_pullup(m0, sizeof(*ifh))) == NULL)
434 			return NULL;
435 	}
436 	ifh = mtod(m0, struct ieee1394_fraghdr *);
437 	m_adj(m0, sizeof(*ifh));
438 	size = ntohs(ifh->ifh_ft_size);
439 	ftype = size & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE);
440 	size = (size & ~ftype) + 1;
441 	dgl = ntohs(ifh->ifh_dgl);
442 	len = m0->m_pkthdr.len;
443 	id = dgl | (src << 16);
444 	if (ftype & IEEE1394_FT_SUBSEQ) {
445 		m_tag_delete_chain(m0, NULL);
446 		m0->m_flags &= ~M_PKTHDR;
447 		etype = 0;
448 		off = ntohs(ifh->ifh_etype_off);
449 	} else {
450 		etype = ifh->ifh_etype_off;
451 		off = 0;
452 	}
453 
454 	for (rq = LIST_FIRST(&ic->ic_reassq); ; rq = LIST_NEXT(rq, rq_node)) {
455 		if (rq == NULL) {
456 			/*
457 			 * Create a new reassemble queue head for the node.
458 			 */
459 			rq = malloc(sizeof(*rq), M_FTABLE, M_NOWAIT);
460 			if (rq == NULL) {
461 				m_freem(m0);
462 				return NULL;
463 			}
464 			rq->fr_id = id;
465 			LIST_INIT(&rq->rq_pkt);
466 			LIST_INSERT_HEAD(&ic->ic_reassq, rq, rq_node);
467 			break;
468 		}
469 		if (rq->fr_id == id)
470 			break;
471 	}
472 	for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
473 		nrp = LIST_NEXT(rp, rp_next);
474 		if (rp->rp_dgl != dgl)
475 			continue;
476 		/*
477 		 * sanity check:
478 		 * datagram size must be same for all fragments, and
479 		 * no overlap is allowed.
480 		 */
481 		if (rp->rp_size != size ||
482 		    (off < rp->rp_off + rp->rp_len && off + len > rp->rp_off)) {
483 			/*
484 			 * This happens probably due to wrapping dgl value.
485 			 * Destroy all previously received fragment and
486 			 * enqueue current fragment.
487 			 */
488 			for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL;
489 			    rp = nrp) {
490 				nrp = LIST_NEXT(rp, rp_next);
491 				if (rp->rp_dgl == dgl) {
492 					LIST_REMOVE(rp, rp_next);
493 					m_freem(rp->rp_m);
494 					free(rp, M_FTABLE);
495 				}
496 			}
497 			break;
498 		}
499 		if (rp->rp_off + rp->rp_len == off) {
500 			/*
501 			 * All the subsequent fragments received in sequence
502 			 * come here.
503 			 * Concatinate mbuf to previous one instead of
504 			 * allocating new reassemble queue structure,
505 			 * and try to merge more with the subsequent fragment
506 			 * in the queue.
507 			 */
508 			m_cat(rp->rp_m, m0);
509 			rp->rp_len += len;
510 			while (rp->rp_off + rp->rp_len < size &&
511 			    nrp != NULL && nrp->rp_dgl == dgl &&
512 			    nrp->rp_off == rp->rp_off + rp->rp_len) {
513 				LIST_REMOVE(nrp, rp_next);
514 				m_cat(rp->rp_m, nrp->rp_m);
515 				rp->rp_len += nrp->rp_len;
516 				free(nrp, M_FTABLE);
517 				nrp = LIST_NEXT(rp, rp_next);
518 			}
519 			m0 = NULL;	/* mark merged */
520 			break;
521 		}
522 		if (off + m0->m_pkthdr.len == rp->rp_off) {
523 			m_cat(m0, rp->rp_m);
524 			rp->rp_m = m0;
525 			rp->rp_off = off;
526 			rp->rp_etype = etype;	 /* over writing trust etype */
527 			rp->rp_len += len;
528 			m0 = NULL;	/* mark merged */
529 			break;
530 		}
531 		if (rp->rp_off > off) {
532 			/* insert before rp */
533 			nrp = rp;
534 			break;
535 		}
536 		if (nrp == NULL || nrp->rp_dgl != dgl) {
537 			/* insert after rp */
538 			nrp = NULL;
539 			break;
540 		}
541 	}
542 	if (m0 == NULL) {
543 		if (rp->rp_off != 0 || rp->rp_len != size)
544 			return NULL;
545 		/* fragment done */
546 		LIST_REMOVE(rp, rp_next);
547 		m0 = rp->rp_m;
548 		m0->m_pkthdr.len = rp->rp_len;
549 		M_PREPEND(m0, sizeof(*iuh), M_DONTWAIT);
550 		if (m0 != NULL) {
551 			iuh = mtod(m0, struct ieee1394_unfraghdr *);
552 			iuh->iuh_ft = 0;
553 			iuh->iuh_etype = rp->rp_etype;
554 		}
555 		free(rp, M_FTABLE);
556 		return m0;
557 	}
558 
559 	/*
560 	 * New fragment received.  Allocate reassemble queue structure.
561 	 */
562 	trp = malloc(sizeof(*trp), M_FTABLE, M_NOWAIT);
563 	if (trp == NULL) {
564 		m_freem(m0);
565 		return NULL;
566 	}
567 	trp->rp_m = m0;
568 	trp->rp_size = size;
569 	trp->rp_etype = etype;		 /* valid only if off==0 */
570 	trp->rp_off = off;
571 	trp->rp_dgl = dgl;
572 	trp->rp_len = len;
573 	trp->rp_ttl = IEEE1394_REASS_TIMEOUT;
574 	if (trp->rp_ttl <= ifp->if_timer)
575 		trp->rp_ttl = ifp->if_timer + 1;
576 
577 	if (rp == NULL) {
578 		/* first fragment for the dgl */
579 		LIST_INSERT_HEAD(&rq->rq_pkt, trp, rp_next);
580 	} else if (nrp == NULL) {
581 		/* no next fragment for the dgl */
582 		LIST_INSERT_AFTER(rp, trp, rp_next);
583 	} else {
584 		/* there is a hole */
585 		LIST_INSERT_BEFORE(nrp, trp, rp_next);
586 	}
587 	return NULL;
588 }
589 
590 void
591 ieee1394_drain(struct ifnet *ifp)
592 {
593 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
594 	struct ieee1394_reassq *rq;
595 	struct ieee1394_reass_pkt *rp;
596 
597 	while ((rq = LIST_FIRST(&ic->ic_reassq)) != NULL) {
598 		LIST_REMOVE(rq, rq_node);
599 		while ((rp = LIST_FIRST(&rq->rq_pkt)) != NULL) {
600 			LIST_REMOVE(rp, rp_next);
601 			m_freem(rp->rp_m);
602 			free(rp, M_FTABLE);
603 		}
604 		free(rq, M_FTABLE);
605 	}
606 }
607 
608 void
609 ieee1394_watchdog(struct ifnet *ifp)
610 {
611 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
612 	struct ieee1394_reassq *rq;
613 	struct ieee1394_reass_pkt *rp, *nrp;
614 	int dec;
615 
616 	dec = (ifp->if_timer > 0) ? ifp->if_timer : 1;
617 	for (rq = LIST_FIRST(&ic->ic_reassq); rq != NULL;
618 	    rq = LIST_NEXT(rq, rq_node)) {
619 		for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
620 			nrp = LIST_NEXT(rp, rp_next);
621 			if (rp->rp_ttl >= dec)
622 				rp->rp_ttl -= dec;
623 			else {
624 				LIST_REMOVE(rp, rp_next);
625 				m_freem(rp->rp_m);
626 				free(rp, M_FTABLE);
627 			}
628 		}
629 	}
630 }
631 
632 const char *
633 ieee1394_sprintf(const uint8_t *laddr)
634 {
635 	static char buf[3*8];
636 
637 	snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
638 	    laddr[0], laddr[1], laddr[2], laddr[3],
639 	    laddr[4], laddr[5], laddr[6], laddr[7]);
640 	return buf;
641 }
642 
643 void
644 ieee1394_ifattach(struct ifnet *ifp, const struct ieee1394_hwaddr *hwaddr)
645 {
646 	struct ieee1394_hwaddr *baddr;
647 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
648 
649 	ifp->if_type = IFT_IEEE1394;
650 	ifp->if_hdrlen = sizeof(struct ieee1394_header);
651 	ifp->if_dlt = DLT_EN10MB;	/* XXX */
652 	ifp->if_mtu = IEEE1394MTU;
653 	ifp->if_output = ieee1394_output;
654 	ifp->if_drain = ieee1394_drain;
655 	ifp->if_watchdog = ieee1394_watchdog;
656 	ifp->if_timer = 1;
657 	if (ifp->if_baudrate == 0)
658 		ifp->if_baudrate = IF_Mbps(100);
659 
660 	if_set_sadl(ifp, hwaddr, sizeof(struct ieee1394_hwaddr), true);
661 
662 	baddr = malloc(ifp->if_addrlen, M_DEVBUF, M_WAITOK);
663 	memset(baddr->iha_uid, 0xff, IEEE1394_ADDR_LEN);
664 	baddr->iha_speed = 0;	/*XXX: how to determine the speed for bcast? */
665 	baddr->iha_maxrec = 512 << baddr->iha_speed;
666 	memset(baddr->iha_offset, 0, sizeof(baddr->iha_offset));
667 	ifp->if_broadcastaddr = (uint8_t *)baddr;
668 	LIST_INIT(&ic->ic_reassq);
669 	bpf_attach(ifp, DLT_APPLE_IP_OVER_IEEE1394,
670 	    sizeof(struct ieee1394_hwaddr));
671 }
672 
673 void
674 ieee1394_ifdetach(struct ifnet *ifp)
675 {
676 	ieee1394_drain(ifp);
677 	bpf_detach(ifp);
678 	free(__UNCONST(ifp->if_broadcastaddr), M_DEVBUF);
679 	ifp->if_broadcastaddr = NULL;
680 }
681 
682 int
683 ieee1394_ioctl(struct ifnet *ifp, u_long cmd, void *data)
684 {
685 	struct ifreq *ifr = (struct ifreq *)data;
686 	struct ifaddr *ifa = (struct ifaddr *)data;
687 	int error = 0;
688 
689 	switch (cmd) {
690 	case SIOCINITIFADDR:
691 		ifp->if_flags |= IFF_UP;
692 		switch (ifa->ifa_addr->sa_family) {
693 #ifdef INET
694 		case AF_INET:
695 			if ((error = (*ifp->if_init)(ifp)) != 0)
696 				break;
697 			arp_ifinit(ifp, ifa);
698 			break;
699 #endif /* INET */
700 		default:
701 			error = (*ifp->if_init)(ifp);
702 			break;
703 		}
704 		break;
705 
706 	case SIOCSIFMTU:
707 		if (ifr->ifr_mtu > IEEE1394MTU)
708 			error = EINVAL;
709 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
710 			error = 0;
711 		break;
712 
713 	default:
714 		error = ifioctl_common(ifp, cmd, data);
715 		break;
716 	}
717 
718 	return error;
719 }
720