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