Home | History | Annotate | Line # | Download | only in net
      1 /*	$NetBSD: if_ieee1394subr.c,v 1.70 2025/09/21 15:11:52 christos 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: if_ieee1394subr.c,v 1.70 2025/09/21 15:11:52 christos Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "opt_inet.h"
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/bus.h>
     42 #include <sys/device.h>
     43 #include <sys/kernel.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/socket.h>
     46 #include <sys/sockio.h>
     47 #include <sys/select.h>
     48 
     49 #include <net/if.h>
     50 #include <net/if_dl.h>
     51 #include <net/if_ieee1394.h>
     52 #include <net/if_types.h>
     53 #include <net/if_media.h>
     54 #include <net/ethertypes.h>
     55 #include <net/route.h>
     56 
     57 #include <net/bpf.h>
     58 
     59 #ifdef INET
     60 #include <netinet/in.h>
     61 #include <netinet/in_var.h>
     62 #include <netinet/if_inarp.h>
     63 #endif /* INET */
     64 #ifdef INET6
     65 #include <netinet/in.h>
     66 #include <netinet6/in6_var.h>
     67 #include <netinet6/nd6.h>
     68 #endif /* INET6 */
     69 
     70 #include <dev/ieee1394/firewire.h>
     71 
     72 #include <dev/ieee1394/firewirereg.h>
     73 #include <dev/ieee1394/iec13213.h>
     74 #include <dev/ieee1394/if_fwipvar.h>
     75 
     76 #define	IEEE1394_REASS_TIMEOUT	3	/* 3 sec */
     77 
     78 #define	senderr(e)	do { error = (e); goto bad; } while(0/*CONSTCOND*/)
     79 
     80 static int  ieee1394_output(struct ifnet *, struct mbuf *,
     81 		const struct sockaddr *, const struct rtentry *);
     82 static struct mbuf *ieee1394_reass(struct ifnet *, struct mbuf *, uint16_t);
     83 
     84 static int
     85 ieee1394_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
     86     const struct rtentry *rt)
     87 {
     88 	uint16_t etype = 0;
     89 	struct mbuf *m;
     90 	int hdrlen, error = 0;
     91 	struct mbuf *mcopy = NULL;
     92 	struct ieee1394_hwaddr *hwdst, baddr;
     93 	const struct ieee1394_hwaddr *myaddr;
     94 #ifdef INET
     95 	struct arphdr *ah;
     96 #endif /* INET */
     97 	struct m_tag *mtag;
     98 	int unicast;
     99 
    100 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
    101 		senderr(ENETDOWN);
    102 
    103 	/*
    104 	 * If the queueing discipline needs packet classification,
    105 	 * do it before prepending link headers.
    106 	 */
    107 	IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family);
    108 
    109 	/*
    110 	 * For unicast, we make a tag to store the lladdr of the
    111 	 * destination. This might not be the first time we have seen
    112 	 * the packet (for instance, the arp code might be trying to
    113 	 * re-send it after receiving an arp reply) so we only
    114 	 * allocate a tag if there isn't one there already. For
    115 	 * multicast, we will eventually use a different tag to store
    116 	 * the channel number.
    117 	 */
    118 	unicast = !(m0->m_flags & (M_BCAST | M_MCAST));
    119 	if (unicast) {
    120 		mtag = m_tag_find(m0, MTAG_FIREWIRE_HWADDR);
    121 		if (!mtag) {
    122 			mtag = m_tag_get(MTAG_FIREWIRE_HWADDR,
    123 			    sizeof (struct ieee1394_hwaddr), M_NOWAIT);
    124 			if (!mtag) {
    125 				error = ENOMEM;
    126 				goto bad;
    127 			}
    128 			m_tag_prepend(m0, mtag);
    129 		}
    130 		hwdst = (struct ieee1394_hwaddr *)(mtag + 1);
    131 	} else {
    132 		hwdst = &baddr;
    133 	}
    134 
    135 	switch (dst->sa_family) {
    136 #ifdef INET
    137 	case AF_INET:
    138 		if (unicast &&
    139 		    (error = arpresolve(ifp, rt, m0, dst, hwdst,
    140 			sizeof(*hwdst))) != 0)
    141 			return error == EWOULDBLOCK ? 0 : error;
    142 		/* if broadcasting on a simplex interface, loopback a copy */
    143 		if ((m0->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    144 			mcopy = m_copypacket(m0, M_DONTWAIT);
    145 		etype = htons(ETHERTYPE_IP);
    146 		break;
    147 	case AF_ARP:
    148 		ah = mtod(m0, struct arphdr *);
    149 		ah->ar_hrd = htons(ARPHRD_IEEE1394);
    150 		etype = htons(ETHERTYPE_ARP);
    151 		break;
    152 #endif /* INET */
    153 #ifdef INET6
    154 	case AF_INET6:
    155 #if 0
    156 		/*
    157 		 * XXX This code was in nd6_storelladdr, which was replaced with
    158 		 * nd6_resolve, but it never be used because nd6_storelladdr was
    159 		 * called only if unicast. Should it be enabled?
    160 		 */
    161 		if (m0->m_flags & M_BCAST)
    162 			memcpy(hwdst->iha_uid, ifp->if_broadcastaddr,
    163 			    MIN(IEEE1394_ADDR_LEN, ifp->if_addrlen));
    164 #endif
    165 		if (unicast) {
    166 			error = nd6_resolve(ifp, rt, m0, dst, hwdst->iha_uid,
    167 			    IEEE1394_ADDR_LEN);
    168 			if (error != 0)
    169 				return error == EWOULDBLOCK ? 0 : error;
    170 		}
    171 		etype = htons(ETHERTYPE_IPV6);
    172 		break;
    173 #endif /* INET6 */
    174 
    175 	case pseudo_AF_HDRCMPLT:
    176 	case AF_UNSPEC:
    177 		/* TODO? */
    178 	default:
    179 		rt_unhandled(__func__, ifp, dst);
    180 		senderr(EAFNOSUPPORT);
    181 	}
    182 
    183 	if (mcopy)
    184 		looutput(ifp, mcopy, dst, rt);
    185 	myaddr = (const struct ieee1394_hwaddr *)CLLADDR(ifp->if_sadl);
    186 	if (ifp->if_bpf) {
    187 		struct ieee1394_bpfhdr h;
    188 		if (unicast)
    189 			memcpy(h.ibh_dhost, hwdst->iha_uid, 8);
    190 		else
    191 			memcpy(h.ibh_dhost,
    192 			    ((const struct ieee1394_hwaddr *)
    193 			    ifp->if_broadcastaddr)->iha_uid, 8);
    194 		memcpy(h.ibh_shost, myaddr->iha_uid, 8);
    195 		h.ibh_type = etype;
    196 		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m0, BPF_D_OUT);
    197 	}
    198 	if ((ifp->if_flags & IFF_SIMPLEX) &&
    199 	    unicast &&
    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 (unicast) {
    210 		hdrlen = IEEE1394_GASP_LEN;
    211 		hwdst->iha_speed = 0;	/* XXX */
    212 	} else
    213 		hdrlen = 0;
    214 
    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 	while ((m = m0) != NULL) {
    229 		m0 = m->m_nextpkt;
    230 
    231 		error = if_transmit_lock(ifp, m);
    232 		if (error) {
    233 			/* mbuf is already freed */
    234 			goto bad;
    235 		}
    236 	}
    237 	return 0;
    238 
    239   bad:
    240 	while (m0 != NULL) {
    241 		m = m0->m_nextpkt;
    242 		m_freem(m0);
    243 		m0 = m;
    244 	}
    245 
    246 	return error;
    247 }
    248 
    249 struct mbuf *
    250 ieee1394_fragment(struct ifnet *ifp, struct mbuf *m0, int maxsize,
    251     uint16_t etype)
    252 {
    253 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    254 	int totlen, fraglen, off;
    255 	struct mbuf *m, **mp;
    256 	struct ieee1394_fraghdr *ifh;
    257 	struct ieee1394_unfraghdr *iuh;
    258 
    259 	totlen = m0->m_pkthdr.len;
    260 	if (totlen + sizeof(struct ieee1394_unfraghdr) <= maxsize) {
    261 		M_PREPEND(m0, sizeof(struct ieee1394_unfraghdr), M_DONTWAIT);
    262 		if (m0 == NULL)
    263 			goto bad;
    264 		iuh = mtod(m0, struct ieee1394_unfraghdr *);
    265 		iuh->iuh_ft = 0;
    266 		iuh->iuh_etype = etype;
    267 		return m0;
    268 	}
    269 
    270 	fraglen = maxsize - sizeof(struct ieee1394_fraghdr);
    271 
    272 	M_PREPEND(m0, sizeof(struct ieee1394_fraghdr), M_DONTWAIT);
    273 	if (m0 == NULL)
    274 		goto bad;
    275 	ifh = mtod(m0, struct ieee1394_fraghdr *);
    276 	ifh->ifh_ft_size = htons(IEEE1394_FT_MORE | (totlen - 1));
    277 	ifh->ifh_etype_off = etype;
    278 	ifh->ifh_dgl = htons(ic->ic_dgl);
    279 	ifh->ifh_reserved = 0;
    280 	off = fraglen;
    281 	mp = &m0->m_nextpkt;
    282 	while (off < totlen) {
    283 		if (off + fraglen > totlen)
    284 			fraglen = totlen - off;
    285 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    286 		if (m == NULL)
    287 			goto bad;
    288 		m->m_flags |= m0->m_flags & (M_BCAST|M_MCAST);	/* copy bcast */
    289 		m_align(m, sizeof(struct ieee1394_fraghdr));
    290 		m->m_len = sizeof(struct ieee1394_fraghdr);
    291 		ifh = mtod(m, struct ieee1394_fraghdr *);
    292 		ifh->ifh_ft_size =
    293 		    htons(IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE | (totlen - 1));
    294 		ifh->ifh_etype_off = htons(off);
    295 		ifh->ifh_dgl = htons(ic->ic_dgl);
    296 		ifh->ifh_reserved = 0;
    297 		m->m_next = m_copym(m0, sizeof(*ifh) + off, fraglen, M_DONTWAIT);
    298 		if (m->m_next == NULL) {
    299 			m_freem(m);
    300 			goto bad;
    301 		}
    302 		m->m_pkthdr.len = sizeof(*ifh) + fraglen;
    303 		off += fraglen;
    304 		*mp = m;
    305 		mp = &m->m_nextpkt;
    306 	}
    307 	ifh->ifh_ft_size &= ~htons(IEEE1394_FT_MORE);	/* last fragment */
    308 	m_adj(m0, -(m0->m_pkthdr.len - maxsize));
    309 
    310 	ic->ic_dgl++;
    311 	return m0;
    312 
    313   bad:
    314 	while ((m = m0) != NULL) {
    315 		m0 = m->m_nextpkt;
    316 		m->m_nextpkt = NULL;
    317 		m_freem(m);
    318 	}
    319 	return NULL;
    320 }
    321 
    322 void
    323 ieee1394_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
    324 {
    325 	pktqueue_t *pktq = NULL;
    326 	uint16_t etype;
    327 	struct ieee1394_unfraghdr *iuh;
    328 
    329 	if ((ifp->if_flags & IFF_UP) == 0) {
    330 		m_freem(m);
    331 		return;
    332 	}
    333 	if (m->m_len < sizeof(*iuh)) {
    334 		if ((m = m_pullup(m, sizeof(*iuh))) == NULL)
    335 			return;
    336 	}
    337 
    338 	iuh = mtod(m, struct ieee1394_unfraghdr *);
    339 
    340 	if (ntohs(iuh->iuh_ft) & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE)) {
    341 		if ((m = ieee1394_reass(ifp, m, src)) == NULL)
    342 			return;
    343 		iuh = mtod(m, struct ieee1394_unfraghdr *);
    344 	}
    345 	etype = ntohs(iuh->iuh_etype);
    346 
    347 	/* strip off the ieee1394 header */
    348 	m_adj(m, sizeof(*iuh));
    349 	if (ifp->if_bpf) {
    350 		struct ieee1394_bpfhdr h;
    351 		struct m_tag *mtag;
    352 		const struct ieee1394_hwaddr *myaddr;
    353 
    354 		mtag = m_tag_find(m, MTAG_FIREWIRE_SENDER_EUID);
    355 		if (mtag)
    356 			memcpy(h.ibh_shost, mtag + 1, 8);
    357 		else
    358 			memset(h.ibh_shost, 0, 8);
    359 		if (m->m_flags & M_BCAST)
    360 			memcpy(h.ibh_dhost,
    361 			    ((const struct ieee1394_hwaddr *)
    362 			    ifp->if_broadcastaddr)->iha_uid, 8);
    363 		else {
    364 			myaddr =
    365 			  (const struct ieee1394_hwaddr *)CLLADDR(ifp->if_sadl);
    366 			memcpy(h.ibh_dhost, myaddr->iha_uid, 8);
    367 		}
    368 		h.ibh_type = htons(etype);
    369 		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m, BPF_D_IN);
    370 	}
    371 
    372 	switch (etype) {
    373 #ifdef INET
    374 	case ETHERTYPE_IP:
    375 		pktq = ip_pktq;
    376 		break;
    377 
    378 	case ETHERTYPE_ARP:
    379 		pktq = arp_pktq;
    380 		break;
    381 #endif /* INET */
    382 
    383 #ifdef INET6
    384 	case ETHERTYPE_IPV6:
    385 		pktq = ip6_pktq;
    386 		break;
    387 #endif /* INET6 */
    388 
    389 	default:
    390 		m_freem(m);
    391 		return;
    392 	}
    393 
    394 	KASSERT(pktq != NULL);
    395 	if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
    396 		m_freem(m);
    397 	}
    398 }
    399 
    400 static struct mbuf *
    401 ieee1394_reass(struct ifnet *ifp, struct mbuf *m0, uint16_t src)
    402 {
    403 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    404 	struct ieee1394_fraghdr *ifh;
    405 	struct ieee1394_unfraghdr *iuh;
    406 	struct ieee1394_reassq *rq;
    407 	struct ieee1394_reass_pkt *rp, *trp, *nrp = NULL;
    408 	int len;
    409 	uint16_t etype, off, ftype, size, dgl;
    410 	uint32_t id;
    411 
    412 	if (m0->m_len < sizeof(*ifh)) {
    413 		if ((m0 = m_pullup(m0, sizeof(*ifh))) == NULL)
    414 			return NULL;
    415 	}
    416 	ifh = mtod(m0, struct ieee1394_fraghdr *);
    417 	m_adj(m0, sizeof(*ifh));
    418 	size = ntohs(ifh->ifh_ft_size);
    419 	ftype = size & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE);
    420 	size = (size & ~ftype) + 1;
    421 	dgl = ntohs(ifh->ifh_dgl);
    422 	len = m0->m_pkthdr.len;
    423 	id = dgl | (src << 16);
    424 	if (ftype & IEEE1394_FT_SUBSEQ) {
    425 		m_remove_pkthdr(m0);
    426 		etype = 0;
    427 		off = ntohs(ifh->ifh_etype_off);
    428 	} else {
    429 		etype = ifh->ifh_etype_off;
    430 		off = 0;
    431 	}
    432 
    433 	for (rq = LIST_FIRST(&ic->ic_reassq); ; rq = LIST_NEXT(rq, rq_node)) {
    434 		if (rq == NULL) {
    435 			/*
    436 			 * Create a new reassemble queue head for the node.
    437 			 */
    438 			rq = malloc(sizeof(*rq), M_FTABLE, M_NOWAIT);
    439 			if (rq == NULL) {
    440 				m_freem(m0);
    441 				return NULL;
    442 			}
    443 			rq->fr_id = id;
    444 			LIST_INIT(&rq->rq_pkt);
    445 			LIST_INSERT_HEAD(&ic->ic_reassq, rq, rq_node);
    446 			break;
    447 		}
    448 		if (rq->fr_id == id)
    449 			break;
    450 	}
    451 	for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
    452 		nrp = LIST_NEXT(rp, rp_next);
    453 		if (rp->rp_dgl != dgl)
    454 			continue;
    455 		/*
    456 		 * sanity check:
    457 		 * datagram size must be same for all fragments, and
    458 		 * no overlap is allowed.
    459 		 */
    460 		if (rp->rp_size != size ||
    461 		    (off < rp->rp_off + rp->rp_len && off + len > rp->rp_off)) {
    462 			/*
    463 			 * This happens probably due to wrapping dgl value.
    464 			 * Destroy all previously received fragment and
    465 			 * enqueue current fragment.
    466 			 */
    467 			for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL;
    468 			    rp = nrp) {
    469 				nrp = LIST_NEXT(rp, rp_next);
    470 				if (rp->rp_dgl == dgl) {
    471 					LIST_REMOVE(rp, rp_next);
    472 					m_freem(rp->rp_m);
    473 					free(rp, M_FTABLE);
    474 				}
    475 			}
    476 			break;
    477 		}
    478 		if (rp->rp_off + rp->rp_len == off) {
    479 			/*
    480 			 * All the subsequent fragments received in sequence
    481 			 * come here.
    482 			 * Concatinate mbuf to previous one instead of
    483 			 * allocating new reassemble queue structure,
    484 			 * and try to merge more with the subsequent fragment
    485 			 * in the queue.
    486 			 */
    487 			m_cat(rp->rp_m, m0);
    488 			rp->rp_len += len;
    489 			while (rp->rp_off + rp->rp_len < size &&
    490 			    nrp != NULL && nrp->rp_dgl == dgl &&
    491 			    nrp->rp_off == rp->rp_off + rp->rp_len) {
    492 				LIST_REMOVE(nrp, rp_next);
    493 				m_cat(rp->rp_m, nrp->rp_m);
    494 				rp->rp_len += nrp->rp_len;
    495 				free(nrp, M_FTABLE);
    496 				nrp = LIST_NEXT(rp, rp_next);
    497 			}
    498 			m0 = NULL;	/* mark merged */
    499 			break;
    500 		}
    501 		if (off + m0->m_pkthdr.len == rp->rp_off) {
    502 			m_cat(m0, rp->rp_m);
    503 			rp->rp_m = m0;
    504 			rp->rp_off = off;
    505 			rp->rp_etype = etype;	 /* over writing trust etype */
    506 			rp->rp_len += len;
    507 			m0 = NULL;	/* mark merged */
    508 			break;
    509 		}
    510 		if (rp->rp_off > off) {
    511 			/* insert before rp */
    512 			nrp = rp;
    513 			break;
    514 		}
    515 		if (nrp == NULL || nrp->rp_dgl != dgl) {
    516 			/* insert after rp */
    517 			nrp = NULL;
    518 			break;
    519 		}
    520 	}
    521 	if (m0 == NULL) {
    522 		if (rp->rp_off != 0 || rp->rp_len != size)
    523 			return NULL;
    524 		/* fragment done */
    525 		LIST_REMOVE(rp, rp_next);
    526 		m0 = rp->rp_m;
    527 		m0->m_pkthdr.len = rp->rp_len;
    528 		M_PREPEND(m0, sizeof(*iuh), M_DONTWAIT);
    529 		if (m0 != NULL) {
    530 			iuh = mtod(m0, struct ieee1394_unfraghdr *);
    531 			iuh->iuh_ft = 0;
    532 			iuh->iuh_etype = rp->rp_etype;
    533 		}
    534 		free(rp, M_FTABLE);
    535 		return m0;
    536 	}
    537 
    538 	/*
    539 	 * New fragment received.  Allocate reassemble queue structure.
    540 	 */
    541 	trp = malloc(sizeof(*trp), M_FTABLE, M_NOWAIT);
    542 	if (trp == NULL) {
    543 		m_freem(m0);
    544 		return NULL;
    545 	}
    546 	trp->rp_m = m0;
    547 	trp->rp_size = size;
    548 	trp->rp_etype = etype;		 /* valid only if off==0 */
    549 	trp->rp_off = off;
    550 	trp->rp_dgl = dgl;
    551 	trp->rp_len = len;
    552 	trp->rp_ttl = IEEE1394_REASS_TIMEOUT;
    553 	if (trp->rp_ttl <= ifp->if_timer)
    554 		trp->rp_ttl = ifp->if_timer + 1;
    555 
    556 	if (rp == NULL) {
    557 		/* first fragment for the dgl */
    558 		LIST_INSERT_HEAD(&rq->rq_pkt, trp, rp_next);
    559 	} else if (nrp == NULL) {
    560 		/* no next fragment for the dgl */
    561 		LIST_INSERT_AFTER(rp, trp, rp_next);
    562 	} else {
    563 		/* there is a hole */
    564 		LIST_INSERT_BEFORE(nrp, trp, rp_next);
    565 	}
    566 	return NULL;
    567 }
    568 
    569 void
    570 ieee1394_drain(struct ifnet *ifp)
    571 {
    572 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    573 	struct ieee1394_reassq *rq;
    574 	struct ieee1394_reass_pkt *rp;
    575 
    576 	while ((rq = LIST_FIRST(&ic->ic_reassq)) != NULL) {
    577 		LIST_REMOVE(rq, rq_node);
    578 		while ((rp = LIST_FIRST(&rq->rq_pkt)) != NULL) {
    579 			LIST_REMOVE(rp, rp_next);
    580 			m_freem(rp->rp_m);
    581 			free(rp, M_FTABLE);
    582 		}
    583 		free(rq, M_FTABLE);
    584 	}
    585 }
    586 
    587 void
    588 ieee1394_watchdog(struct ifnet *ifp)
    589 {
    590 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    591 	struct ieee1394_reassq *rq;
    592 	struct ieee1394_reass_pkt *rp, *nrp;
    593 	int dec;
    594 
    595 	dec = (ifp->if_timer > 0) ? ifp->if_timer : 1;
    596 	for (rq = LIST_FIRST(&ic->ic_reassq); rq != NULL;
    597 	    rq = LIST_NEXT(rq, rq_node)) {
    598 		for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
    599 			nrp = LIST_NEXT(rp, rp_next);
    600 			if (rp->rp_ttl >= dec)
    601 				rp->rp_ttl -= dec;
    602 			else {
    603 				LIST_REMOVE(rp, rp_next);
    604 				m_freem(rp->rp_m);
    605 				free(rp, M_FTABLE);
    606 			}
    607 		}
    608 	}
    609 }
    610 
    611 const char *
    612 ieee1394_sprintf(const uint8_t *laddr)
    613 {
    614 	static char buf[3*8];
    615 
    616 	snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
    617 	    laddr[0], laddr[1], laddr[2], laddr[3],
    618 	    laddr[4], laddr[5], laddr[6], laddr[7]);
    619 	return buf;
    620 }
    621 
    622 void
    623 ieee1394_ifattach(struct ifnet *ifp, const struct ieee1394_hwaddr *hwaddr)
    624 {
    625 	struct ieee1394_hwaddr *baddr;
    626 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    627 
    628 	ifp->if_type = IFT_IEEE1394;
    629 	ifp->if_hdrlen = sizeof(struct ieee1394_header);
    630 	ifp->if_dlt = DLT_EN10MB;	/* XXX */
    631 	ifp->if_mtu = IEEE1394MTU;
    632 	ifp->if_output = ieee1394_output;
    633 	ifp->if_drain = ieee1394_drain;
    634 	ifp->if_watchdog = ieee1394_watchdog;
    635 	ifp->if_timer = 1;
    636 	if (ifp->if_baudrate == 0)
    637 		ifp->if_baudrate = IF_Mbps(100);
    638 
    639 	if_set_sadl(ifp, hwaddr, sizeof(struct ieee1394_hwaddr), true);
    640 
    641 	baddr = malloc(ifp->if_addrlen, M_DEVBUF, M_WAITOK);
    642 	memset(baddr->iha_uid, 0xff, IEEE1394_ADDR_LEN);
    643 	baddr->iha_speed = 0;	/*XXX: how to determine the speed for bcast? */
    644 	baddr->iha_maxrec = 512 << baddr->iha_speed;
    645 	memset(baddr->iha_offset, 0, sizeof(baddr->iha_offset));
    646 	ifp->if_broadcastaddr = (uint8_t *)baddr;
    647 	LIST_INIT(&ic->ic_reassq);
    648 	bpf_attach(ifp, DLT_APPLE_IP_OVER_IEEE1394,
    649 	    sizeof(struct ieee1394_hwaddr));
    650 }
    651 
    652 void
    653 ieee1394_ifdetach(struct ifnet *ifp)
    654 {
    655 	ieee1394_drain(ifp);
    656 	bpf_detach(ifp);
    657 	free(__UNCONST(ifp->if_broadcastaddr), M_DEVBUF);
    658 	ifp->if_broadcastaddr = NULL;
    659 }
    660 
    661 int
    662 ieee1394_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    663 {
    664 	struct ifreq *ifr = (struct ifreq *)data;
    665 	struct ifaddr *ifa = (struct ifaddr *)data;
    666 	int error = 0;
    667 
    668 	switch (cmd) {
    669 	case SIOCINITIFADDR:
    670 		ifp->if_flags |= IFF_UP;
    671 		switch (ifa->ifa_addr->sa_family) {
    672 #ifdef INET
    673 		case AF_INET:
    674 			if ((error = if_init(ifp)) != 0)
    675 				break;
    676 			arp_ifinit(ifp, ifa);
    677 			break;
    678 #endif /* INET */
    679 		default:
    680 			error = if_init(ifp);
    681 			break;
    682 		}
    683 		break;
    684 
    685 	case SIOCSIFMTU:
    686 		if (ifr->ifr_mtu > IEEE1394MTU)
    687 			error = EINVAL;
    688 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    689 			error = 0;
    690 		break;
    691 
    692 	default:
    693 		error = ifioctl_common(ifp, cmd, data);
    694 		break;
    695 	}
    696 
    697 	return error;
    698 }
    699