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