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