xref: /netbsd/sys/net/if_ieee1394subr.c (revision c4a72b64)
1 /*	$NetBSD: if_ieee1394subr.c,v 1.19 2002/09/26 13:06:56 onoe 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: if_ieee1394subr.c,v 1.19 2002/09/26 13:06:56 onoe Exp $");
41 
42 #include "opt_inet.h"
43 #include "bpfilter.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/kernel.h>
50 #include <sys/mbuf.h>
51 #include <sys/device.h>
52 
53 #include <net/if.h>
54 #include <net/if_dl.h>
55 #include <net/if_ieee1394.h>
56 #include <net/if_types.h>
57 #include <net/if_media.h>
58 #include <net/ethertypes.h>
59 #include <net/netisr.h>
60 #include <net/route.h>
61 
62 #if NBPFILTER > 0
63 #include <net/bpf.h>
64 #endif
65 
66 #ifdef INET
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/if_inarp.h>
70 #endif /* INET */
71 #ifdef INET6
72 #include <netinet/in.h>
73 #include <netinet6/in6_var.h>
74 #include <netinet6/nd6.h>
75 #endif /* INET6 */
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 *, struct sockaddr *,
82 		struct rtentry *);
83 static void ieee1394_input(struct ifnet *, struct mbuf *);
84 static struct mbuf *ieee1394_reass(struct ifnet *, struct mbuf *);
85 
86 static int
87 ieee1394_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
88     struct rtentry *rt0)
89 {
90 	u_int16_t etype = 0;
91 	struct mbuf *m;
92 	int s, hdrlen, error = 0;
93 	struct rtentry *rt;
94 	struct mbuf *mcopy = NULL;
95 	struct ieee1394_hwaddr hwdst, *myaddr;
96 	ALTQ_DECL(struct altq_pktattr pktattr;)
97 #ifdef INET
98 	struct arphdr *ah;
99 #endif /* INET */
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.tv_sec < 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 	switch (dst->sa_family) {
145 #ifdef INET
146 	case AF_INET:
147 		if (m0->m_flags & (M_BCAST | M_MCAST))
148 			memcpy(&hwdst, ifp->if_broadcastaddr, sizeof(hwdst));
149 		else if (!arpresolve(ifp, rt, m0, dst, (u_char *)&hwdst))
150 			return 0;	/* if not yet resolved */
151 		/* if broadcasting on a simplex interface, loopback a copy */
152 		if ((m0->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
153 			mcopy = m_copy(m0, 0, M_COPYALL);
154 		etype = htons(ETHERTYPE_IP);
155 		break;
156 	case AF_ARP:
157 		ah = mtod(m0, struct arphdr *);
158 		memcpy(&hwdst, ifp->if_broadcastaddr, sizeof(hwdst));
159 		ah->ar_hrd = htons(ARPHRD_IEEE1394);
160 		etype = htons(ETHERTYPE_ARP);
161 		break;
162 #endif /* INET */
163 #ifdef INET6
164 	case AF_INET6:
165 		if (m0->m_flags & M_MCAST)
166 			memcpy(&hwdst, ifp->if_broadcastaddr, sizeof(hwdst));
167 		else if (!nd6_storelladdr(ifp, rt, m0, dst, (u_char *)&hwdst)) {
168 			/* this must be impossible, so we bark */
169 			printf("ieee1394_output: nd6_storelladdr failed\n");
170 			return 0;
171 		}
172 		etype = htons(ETHERTYPE_IPV6);
173 		break;
174 #endif /* INET6 */
175 
176 	case pseudo_AF_HDRCMPLT:
177 	case AF_UNSPEC:
178 		/* TODO? */
179 	default:
180 		printf("%s: can't handle af%d\n", ifp->if_xname,
181 		    dst->sa_family);
182 		senderr(EAFNOSUPPORT);
183 		break;
184 	}
185 
186 	if (mcopy)
187 		looutput(ifp, mcopy, dst, rt);
188 #if NBPFILTER > 0
189 	/* XXX: emulate DLT_EN10MB */
190 	if (ifp->if_bpf) {
191 		struct mbuf mb;
192 
193 		M_COPY_PKTHDR(&mb, m0);
194 		mb.m_next = m0;
195 		mb.m_len = 14;
196 		((u_int32_t *)mb.m_data)[0] = 0;
197 		((u_int32_t *)mb.m_data)[1] = 0;
198 		((u_int32_t *)mb.m_data)[2] = 0;
199 		((u_int16_t *)mb.m_data)[6] = etype;
200 		mb.m_pkthdr.len += mb.m_len;
201 		bpf_mtap(ifp->if_bpf, &mb);
202 	}
203 #endif
204 	myaddr = (struct ieee1394_hwaddr *)LLADDR(ifp->if_sadl);
205 	if ((ifp->if_flags & IFF_SIMPLEX) &&
206 	    memcmp(&hwdst, myaddr, IEEE1394_ADDR_LEN) == 0)
207 		return looutput(ifp, m0, dst, rt);
208 
209 	/*
210 	 * XXX:
211 	 * The maximum possible rate depends on the topology.
212 	 * So the determination of maxrec and fragmentation should be
213 	 * called from the driver after probing the topology map.
214 	 */
215 	if (m0->m_flags & (M_BCAST | M_MCAST)) {
216 		hdrlen = IEEE1394_GASP_LEN;
217 		hwdst.iha_speed = 0;	/* XXX */
218 	} else
219 		hdrlen = 0;
220 	if (hwdst.iha_speed > myaddr->iha_speed)
221 		hwdst.iha_speed = myaddr->iha_speed;
222 	if (hwdst.iha_maxrec > myaddr->iha_maxrec)
223 		hwdst.iha_maxrec = myaddr->iha_maxrec;
224 	if (hwdst.iha_maxrec > (8 + hwdst.iha_speed))
225 		hwdst.iha_maxrec = 8 + hwdst.iha_speed;
226 	if (hwdst.iha_maxrec < 8)
227 		hwdst.iha_maxrec = 8;
228 
229 	m0 = ieee1394_fragment(ifp, m0, (2<<hwdst.iha_maxrec) - hdrlen, etype);
230 	if (m0 == NULL)
231 		senderr(ENOBUFS);
232 
233 	s = splnet();
234 	ifp->if_obytes += m0->m_pkthdr.len;
235 	if (m0->m_flags & M_MCAST)
236 		ifp->if_omcasts++;
237 	while ((m = m0) != NULL) {
238 		m0 = m->m_nextpkt;
239 		M_PREPEND(m, sizeof(struct ieee1394_header), M_DONTWAIT);
240 		if (m == NULL) {
241 			splx(s);
242 			senderr(ENOBUFS);
243 		}
244 		memcpy(mtod(m, caddr_t), &hwdst, sizeof(hwdst));
245 		IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
246 		if (error) {
247 			/* mbuf is already freed */
248 			splx(s);
249 			goto bad;
250 		}
251 	}
252 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
253 		(*ifp->if_start)(ifp);
254 	splx(s);
255 	return 0;
256 
257   bad:
258 	while (m0 != NULL) {
259 		m = m0->m_nextpkt;
260 		m_freem(m0);
261 		m0 = m;
262 	}
263 
264 	return error;
265 }
266 
267 struct mbuf *
268 ieee1394_fragment(struct ifnet *ifp, struct mbuf *m0, int maxsize,
269     u_int16_t etype)
270 {
271 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
272 	int totlen, fraglen, off;
273 	struct mbuf *m, **mp;
274 	struct ieee1394_fraghdr *ifh;
275 	struct ieee1394_unfraghdr *iuh;
276 
277 	totlen = m0->m_pkthdr.len;
278 	if (totlen + sizeof(struct ieee1394_unfraghdr) <= maxsize) {
279 		M_PREPEND(m0, sizeof(struct ieee1394_unfraghdr), M_DONTWAIT);
280 		if (m0 == NULL)
281 			goto bad;
282 		iuh = mtod(m0, struct ieee1394_unfraghdr *);
283 		iuh->iuh_ft = 0;
284 		iuh->iuh_etype = etype;
285 		return m0;
286 	}
287 
288 	fraglen = maxsize - sizeof(struct ieee1394_fraghdr);
289 
290 	M_PREPEND(m0, sizeof(struct ieee1394_fraghdr), M_DONTWAIT);
291 	if (m0 == NULL)
292 		goto bad;
293 	ifh = mtod(m0, struct ieee1394_fraghdr *);
294 	ifh->ifh_ft_size = htons(IEEE1394_FT_MORE | (totlen - 1));
295 	ifh->ifh_etype_off = etype;
296 	ifh->ifh_dgl = htons(ic->ic_dgl);
297 	ifh->ifh_reserved = 0;
298 	off = fraglen;
299 	mp = &m0->m_nextpkt;
300 	while (off < totlen) {
301 		if (off + fraglen > totlen)
302 			fraglen = totlen - off;
303 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
304 		if (m == NULL)
305 			goto bad;
306 		m->m_flags |= m0->m_flags & (M_BCAST|M_MCAST);	/* copy bcast */
307 		MH_ALIGN(m, sizeof(struct ieee1394_fraghdr));
308 		m->m_len = sizeof(struct ieee1394_fraghdr);
309 		ifh = mtod(m, struct ieee1394_fraghdr *);
310 		ifh->ifh_ft_size =
311 		    htons(IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE | (totlen - 1));
312 		ifh->ifh_etype_off = htons(off);
313 		ifh->ifh_dgl = htons(ic->ic_dgl);
314 		ifh->ifh_reserved = 0;
315 		m->m_next = m_copy(m0, sizeof(*ifh) + off, fraglen);
316 		if (m->m_next == NULL)
317 			goto bad;
318 		m->m_pkthdr.len = sizeof(*ifh) + fraglen;
319 		off += fraglen;
320 		*mp = m;
321 		mp = &m->m_nextpkt;
322 	}
323 	ifh->ifh_ft_size &= ~htons(IEEE1394_FT_MORE);	/* last fragment */
324 	m_adj(m0, -(m0->m_pkthdr.len - maxsize));
325 
326 	ic->ic_dgl++;
327 	return m0;
328 
329   bad:
330 	while ((m = m0) != NULL) {
331 		m0 = m->m_nextpkt;
332 		m->m_nextpkt = NULL;
333 		m_freem(m);
334 	}
335 	return NULL;
336 }
337 
338 static void
339 ieee1394_input(struct ifnet *ifp, struct mbuf *m)
340 {
341 	struct ifqueue *inq;
342 	u_int16_t etype;
343 	int s;
344 	struct ieee1394_header *ih;
345 	struct ieee1394_unfraghdr *iuh;
346 
347 	if ((ifp->if_flags & IFF_UP) == 0) {
348 		m_freem(m);
349 		return;
350 	}
351 	if (m->m_len < sizeof(*ih) + sizeof(*iuh)) {
352 		if ((m = m_pullup(m, sizeof(*ih) + sizeof(*iuh))) == NULL)
353 			return;
354 	}
355 
356 	ih = mtod(m, struct ieee1394_header *);
357 	iuh = (struct ieee1394_unfraghdr *)&ih[1];
358 
359 	if (ntohs(iuh->iuh_ft) & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE)) {
360 		if ((m = ieee1394_reass(ifp, m)) == NULL)
361 			return;
362 		ih = mtod(m, struct ieee1394_header *);
363 		iuh = (struct ieee1394_unfraghdr *)&ih[1];
364 	}
365 	etype = ntohs(iuh->iuh_etype);
366 
367 	/* strip off the ieee1394 header */
368 	m_adj(m, sizeof(*ih) + sizeof(*iuh));
369 #if NBPFILTER > 0
370 	/* XXX: emulate DLT_EN10MB */
371 	if (ifp->if_bpf) {
372 		struct mbuf mb;
373 
374 		M_COPY_PKTHDR(&mb, m);
375 		mb.m_next = m;
376 		mb.m_len = 14;
377 		((u_int32_t *)mb.m_data)[0] = 0;
378 		((u_int32_t *)mb.m_data)[1] = 0;
379 		((u_int32_t *)mb.m_data)[2] = 0;
380 		((u_int16_t *)mb.m_data)[6] = iuh->iuh_etype;
381 		mb.m_pkthdr.len += mb.m_len;
382 		bpf_mtap(ifp->if_bpf, &mb);
383 	}
384 #endif
385 
386 	switch (etype) {
387 #ifdef INET
388 	case ETHERTYPE_IP:
389 		schednetisr(NETISR_IP);
390 		inq = &ipintrq;
391 		break;
392 
393 	case ETHERTYPE_ARP:
394 		schednetisr(NETISR_ARP);
395 		inq = &arpintrq;
396 		break;
397 #endif /* INET */
398 
399 #ifdef INET6
400 	case ETHERTYPE_IPV6:
401 		schednetisr(NETISR_IPV6);
402 		inq = &ip6intrq;
403 		break;
404 #endif /* INET6 */
405 
406 	default:
407 		m_freem(m);
408 		return;
409 	}
410 
411 	s = splnet();
412 	if (IF_QFULL(inq)) {
413 		IF_DROP(inq);
414 		m_freem(m);
415 	} else
416 		IF_ENQUEUE(inq, m);
417 	splx(s);
418 }
419 
420 static struct mbuf *
421 ieee1394_reass(struct ifnet *ifp, struct mbuf *m0)
422 {
423 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
424 	struct ieee1394_header *ih;
425 	struct ieee1394_fraghdr *ifh;
426 	struct ieee1394_unfraghdr *iuh;
427 	struct ieee1394_reassq *rq;
428 	struct ieee1394_reass_pkt *rp, *trp, *nrp;
429 	int len;
430 	u_int16_t off, ftype, size, dgl;
431 
432 	if (m0->m_len < sizeof(*ih) + sizeof(*ifh)) {
433 		if ((m0 = m_pullup(m0, sizeof(*ih) + sizeof(*ifh))) == NULL)
434 			return NULL;
435 	}
436 	ih = mtod(m0, struct ieee1394_header *);
437 	ifh = (struct ieee1394_fraghdr *)&ih[1];
438 	m_adj(m0, sizeof(*ih) + sizeof(*ifh));
439 	size = ntohs(ifh->ifh_ft_size);
440 	ftype = size & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE);
441 	size = (size & ~ftype) + 1;
442 	dgl = ifh->ifh_dgl;
443 	len = m0->m_pkthdr.len;
444 	if (ftype & IEEE1394_FT_SUBSEQ) {
445 		m0->m_flags &= ~M_PKTHDR;
446 		off = ntohs(ifh->ifh_etype_off);
447 	} else
448 		off = 0;
449 
450 	for (rq = LIST_FIRST(&ic->ic_reassq); ; rq = LIST_NEXT(rq, rq_node)) {
451 		if (rq == NULL) {
452 			/*
453 			 * Create a new reassemble queue head for the node.
454 			 */
455 			rq = malloc(sizeof(*rq), M_FTABLE, M_NOWAIT);
456 			if (rq == NULL) {
457 				m_freem(m0);
458 				return NULL;
459 			}
460 			memcpy(rq->rq_uid, ih->ih_uid, IEEE1394_ADDR_LEN);
461 			LIST_INIT(&rq->rq_pkt);
462 			LIST_INSERT_HEAD(&ic->ic_reassq, rq, rq_node);
463 			break;
464 		}
465 		if (memcmp(rq->rq_uid, ih->ih_uid, IEEE1394_ADDR_LEN) == 0)
466 			break;
467 	}
468 	for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
469 		nrp = LIST_NEXT(rp, rp_next);
470 		if (rp->rp_dgl != dgl)
471 			continue;
472 		/*
473 		 * sanity check:
474 		 * datagram size must be same for all fragments, and
475 		 * no overlap is allowed.
476 		 */
477 		if (rp->rp_size != size ||
478 		    (off < rp->rp_off + rp->rp_len && off + len > rp->rp_off)) {
479 			/*
480 			 * This happens probably due to wrapping dgl value.
481 			 * Destroy all previously received fragment and
482 			 * enqueue current fragment.
483 			 */
484 			for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL;
485 			    rp = nrp) {
486 				nrp = LIST_NEXT(rp, rp_next);
487 				if (rp->rp_dgl == dgl) {
488 					LIST_REMOVE(rp, rp_next);
489 					m_freem(rp->rp_m);
490 					free(rp, M_FTABLE);
491 				}
492 			}
493 			break;
494 		}
495 		if (rp->rp_off + rp->rp_len == off) {
496 			/*
497 			 * All the subsequent fragments received in sequence
498 			 * come here.
499 			 * Concatinate mbuf to previous one instead of
500 			 * allocating new reassemble queue structure,
501 			 * and try to merge more with the subsequent fragment
502 			 * in the queue.
503 			 */
504 			m_cat(rp->rp_m, m0);
505 			rp->rp_len += len;
506 			while (rp->rp_off + rp->rp_len < size &&
507 			    nrp != NULL && nrp->rp_dgl == dgl &&
508 			    nrp->rp_off == rp->rp_off + rp->rp_len) {
509 				LIST_REMOVE(nrp, rp_next);
510 				m_cat(rp->rp_m, nrp->rp_m);
511 				rp->rp_len += nrp->rp_len;
512 				free(nrp, M_FTABLE);
513 				nrp = LIST_NEXT(rp, rp_next);
514 			}
515 			m0 = NULL;	/* mark merged */
516 			break;
517 		}
518 		if (off + m0->m_pkthdr.len == rp->rp_off) {
519 			m_cat(m0, rp->rp_m);
520 			rp->rp_m = m0;
521 			rp->rp_off = off;
522 			rp->rp_len += len;
523 			m0 = NULL;	/* mark merged */
524 			break;
525 		}
526 		if (rp->rp_off > off) {
527 			/* insert before rp */
528 			nrp = rp;
529 			break;
530 		}
531 		if (nrp == NULL || nrp->rp_dgl != dgl) {
532 			/* insert after rp */
533 			nrp = NULL;
534 			break;
535 		}
536 	}
537 	if (m0 == NULL) {
538 		if (rp->rp_off != 0 || rp->rp_len != size)
539 			return NULL;
540 		/* fragment done */
541 		LIST_REMOVE(rp, rp_next);
542 		m0 = rp->rp_m;
543 		m0->m_pkthdr.len = rp->rp_len;
544 		M_PREPEND(m0, sizeof(*ih) + sizeof(*iuh), M_DONTWAIT);
545 		if (m0 != NULL) {
546 			ih = mtod(m0, struct ieee1394_header *);
547 			iuh = (struct ieee1394_unfraghdr *)&ih[1];
548 			memcpy(ih, &rp->rp_hdr, sizeof(*ih));
549 			iuh->iuh_ft = 0;
550 			iuh->iuh_etype = rp->rp_etype;
551 		}
552 		free(rp, M_FTABLE);
553 		return m0;
554 	}
555 
556 	/*
557 	 * New fragment received.  Allocate reassemble queue structure.
558 	 */
559 	trp = malloc(sizeof(*trp), M_FTABLE, M_NOWAIT);
560 	if (trp == NULL) {
561 		m_freem(m0);
562 		return NULL;
563 	}
564 	trp->rp_m = m0;
565 	memcpy(&trp->rp_hdr, ih, sizeof(*ih));
566 	trp->rp_size = size;
567 	trp->rp_etype = ifh->ifh_etype_off;	 /* 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 u_int8_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_addrlen = sizeof(struct ieee1394_hwaddr);
649 	ifp->if_hdrlen = sizeof(struct ieee1394_header);
650 	ifp->if_dlt = DLT_EN10MB;	/* XXX */
651 	ifp->if_mtu = IEEE1394MTU;
652 	ifp->if_output = ieee1394_output;
653 	ifp->if_input = ieee1394_input;
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_alloc_sadl(ifp);
661 	memcpy(LLADDR(ifp->if_sadl), hwaddr, ifp->if_addrlen);
662 
663 	ifp->if_broadcastaddr = malloc(ifp->if_addrlen, M_DEVBUF, M_WAITOK);
664 	baddr = (struct ieee1394_hwaddr *)ifp->if_broadcastaddr;
665 	memset(baddr->iha_uid, 0xff, IEEE1394_ADDR_LEN);
666 	baddr->iha_speed = 0;	/*XXX: how to determine the speed for bcast? */
667 	baddr->iha_maxrec = 512 << baddr->iha_speed;
668 	memset(baddr->iha_offset, 0, sizeof(baddr->iha_offset));
669 	LIST_INIT(&ic->ic_reassq);
670 #if NBPFILTER > 0
671 	bpfattach(ifp, DLT_EN10MB, 14);	/* XXX */
672 #endif
673 }
674 
675 void
676 ieee1394_ifdetach(struct ifnet *ifp)
677 {
678 	ieee1394_drain(ifp);
679 #if NBPFILTER > 0
680 	bpfdetach(ifp);
681 #endif
682 	free(ifp->if_broadcastaddr, M_DEVBUF);
683 	ifp->if_broadcastaddr = NULL;
684 	if_free_sadl(ifp);
685 }
686 
687 int
688 ieee1394_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
689 {
690 	struct ifreq *ifr = (struct ifreq *)data;
691 	struct ifaddr *ifa = (struct ifaddr *)data;
692 	int error = 0;
693 #if __NetBSD_Version__ < 105080000
694 	int fw_init(struct ifnet *);
695 	void fw_stop(struct ifnet *, int);
696 #endif
697 
698 	switch (cmd) {
699 	case SIOCSIFADDR:
700 		ifp->if_flags |= IFF_UP;
701 		switch (ifa->ifa_addr->sa_family) {
702 #ifdef INET
703 		case AF_INET:
704 #if __NetBSD_Version__ >= 105080000
705 			if ((error = (*ifp->if_init)(ifp)) != 0)
706 #else
707 			if ((error = fw_init(ifp)) != 0)
708 #endif
709 				break;
710 			arp_ifinit(ifp, ifa);
711 			break;
712 #endif /* INET */
713 		default:
714 #if __NetBSD_Version__ >= 105080000
715 			error = (*ifp->if_init)(ifp);
716 #else
717 			error = fw_init(ifp);
718 #endif
719 			break;
720 		}
721 		break;
722 
723 	case SIOCGIFADDR:
724 		memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
725 		    LLADDR(ifp->if_sadl), IEEE1394_ADDR_LEN);
726 		    break;
727 
728 	case SIOCSIFMTU:
729 		if (ifr->ifr_mtu > IEEE1394MTU)
730 			error = EINVAL;
731 		else
732 			ifp->if_mtu = ifr->ifr_mtu;
733 		break;
734 
735 	case SIOCSIFFLAGS:
736 #if __NetBSD_Version__ >= 105080000
737 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING)
738 			(*ifp->if_stop)(ifp, 1);
739 		else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP)
740 			error = (*ifp->if_init)(ifp);
741 		else if ((ifp->if_flags & IFF_UP) != 0)
742 			error = (*ifp->if_init)(ifp);
743 #else
744 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING)
745 			fw_stop(ifp, 1);
746 		else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP)
747 			error = fw_init(ifp);
748 		else if ((ifp->if_flags & IFF_UP) != 0)
749 			error = fw_init(ifp);
750 #endif
751 		break;
752 
753 	case SIOCADDMULTI:
754 	case SIOCDELMULTI:
755 		/* nothing to do */
756 		break;
757 
758 	default:
759 		error = ENOTTY;
760 		break;
761 	}
762 
763 	return error;
764 }
765