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