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