Home | History | Annotate | Line # | Download | only in net
if_ieee1394subr.c revision 1.49
      1 /*	$NetBSD: if_ieee1394subr.c,v 1.48 2014/11/28 08:29:00 ozaki-r 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.48 2014/11/28 08:29:00 ozaki-r Exp $");
     34 
     35 #include "opt_inet.h"
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/bus.h>
     40 #include <sys/device.h>
     41 #include <sys/kernel.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/socket.h>
     44 #include <sys/sockio.h>
     45 #include <sys/select.h>
     46 
     47 #include <net/if.h>
     48 #include <net/if_dl.h>
     49 #include <net/if_ieee1394.h>
     50 #include <net/if_types.h>
     51 #include <net/if_media.h>
     52 #include <net/ethertypes.h>
     53 #include <net/netisr.h>
     54 #include <net/route.h>
     55 
     56 #include <net/bpf.h>
     57 
     58 #ifdef INET
     59 #include <netinet/in.h>
     60 #include <netinet/in_var.h>
     61 #include <netinet/if_inarp.h>
     62 #endif /* INET */
     63 #ifdef INET6
     64 #include <netinet/in.h>
     65 #include <netinet6/in6_var.h>
     66 #include <netinet6/nd6.h>
     67 #endif /* INET6 */
     68 
     69 #include <dev/ieee1394/firewire.h>
     70 
     71 #include <dev/ieee1394/firewirereg.h>
     72 #include <dev/ieee1394/iec13213.h>
     73 #include <dev/ieee1394/if_fwipvar.h>
     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 *,
     80 		const struct sockaddr *, struct rtentry *);
     81 static struct mbuf *ieee1394_reass(struct ifnet *, struct mbuf *, uint16_t);
     82 
     83 static int
     84 ieee1394_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
     85     struct rtentry *rt)
     86 {
     87 	uint16_t etype = 0;
     88 	struct mbuf *m;
     89 	int s, hdrlen, error = 0;
     90 	struct mbuf *mcopy = NULL;
     91 	struct ieee1394_hwaddr *hwdst, baddr;
     92 	const struct ieee1394_hwaddr *myaddr;
     93 	ALTQ_DECL(struct altq_pktattr pktattr;)
     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, &pktattr);
    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 =
    121 		    m_tag_find(m0, MTAG_FIREWIRE_HWADDR, NULL);
    122 		if (!mtag) {
    123 			mtag = m_tag_get(MTAG_FIREWIRE_HWADDR,
    124 			    sizeof (struct ieee1394_hwaddr), M_NOWAIT);
    125 			if (!mtag) {
    126 				error = ENOMEM;
    127 				goto bad;
    128 			}
    129 			m_tag_prepend(m0, mtag);
    130 		}
    131 		hwdst = (struct ieee1394_hwaddr *)(mtag + 1);
    132 	} else {
    133 		hwdst = &baddr;
    134 	}
    135 
    136 	switch (dst->sa_family) {
    137 #ifdef INET
    138 	case AF_INET:
    139 		if (unicast && (!arpresolve(ifp, rt, m0, dst, (u_char *)hwdst)))
    140 			return 0;	/* if not yet resolved */
    141 		/* if broadcasting on a simplex interface, loopback a copy */
    142 		if ((m0->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    143 			mcopy = m_copy(m0, 0, M_COPYALL);
    144 		etype = htons(ETHERTYPE_IP);
    145 		break;
    146 	case AF_ARP:
    147 		ah = mtod(m0, struct arphdr *);
    148 		ah->ar_hrd = htons(ARPHRD_IEEE1394);
    149 		etype = htons(ETHERTYPE_ARP);
    150 		break;
    151 #endif /* INET */
    152 #ifdef INET6
    153 	case AF_INET6:
    154 		if (unicast && (!nd6_storelladdr(ifp, rt, m0, dst,
    155 		    hwdst->iha_uid, IEEE1394_ADDR_LEN))) {
    156 			/* something bad happened */
    157 			return 0;
    158 		}
    159 		etype = htons(ETHERTYPE_IPV6);
    160 		break;
    161 #endif /* INET6 */
    162 
    163 	case pseudo_AF_HDRCMPLT:
    164 	case AF_UNSPEC:
    165 		/* TODO? */
    166 	default:
    167 		printf("%s: can't handle af%d\n", ifp->if_xname,
    168 		    dst->sa_family);
    169 		senderr(EAFNOSUPPORT);
    170 		break;
    171 	}
    172 
    173 	if (mcopy)
    174 		looutput(ifp, mcopy, dst, rt);
    175 	myaddr = (const struct ieee1394_hwaddr *)CLLADDR(ifp->if_sadl);
    176 	if (ifp->if_bpf) {
    177 		struct ieee1394_bpfhdr h;
    178 		if (unicast)
    179 			memcpy(h.ibh_dhost, hwdst->iha_uid, 8);
    180 		else
    181 			memcpy(h.ibh_dhost,
    182 			    ((const struct ieee1394_hwaddr *)
    183 			    ifp->if_broadcastaddr)->iha_uid, 8);
    184 		memcpy(h.ibh_shost, myaddr->iha_uid, 8);
    185 		h.ibh_type = etype;
    186 		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m0);
    187 	}
    188 	if ((ifp->if_flags & IFF_SIMPLEX) &&
    189 	    unicast &&
    190 	    memcmp(hwdst, myaddr, IEEE1394_ADDR_LEN) == 0)
    191 		return looutput(ifp, m0, dst, rt);
    192 
    193 	/*
    194 	 * XXX:
    195 	 * The maximum possible rate depends on the topology.
    196 	 * So the determination of maxrec and fragmentation should be
    197 	 * called from the driver after probing the topology map.
    198 	 */
    199 	if (unicast) {
    200 		hdrlen = IEEE1394_GASP_LEN;
    201 		hwdst->iha_speed = 0;	/* XXX */
    202 	} else
    203 		hdrlen = 0;
    204 
    205 	if (hwdst->iha_speed > myaddr->iha_speed)
    206 		hwdst->iha_speed = myaddr->iha_speed;
    207 	if (hwdst->iha_maxrec > myaddr->iha_maxrec)
    208 		hwdst->iha_maxrec = myaddr->iha_maxrec;
    209 	if (hwdst->iha_maxrec > (8 + hwdst->iha_speed))
    210 		hwdst->iha_maxrec = 8 + hwdst->iha_speed;
    211 	if (hwdst->iha_maxrec < 8)
    212 			hwdst->iha_maxrec = 8;
    213 
    214 	m0 = ieee1394_fragment(ifp, m0, (2<<hwdst->iha_maxrec) - hdrlen, etype);
    215 	if (m0 == NULL)
    216 		senderr(ENOBUFS);
    217 
    218 	s = splnet();
    219 	ifp->if_obytes += m0->m_pkthdr.len;
    220 	if (m0->m_flags & M_MCAST)
    221 		ifp->if_omcasts++;
    222 	while ((m = m0) != NULL) {
    223 		m0 = m->m_nextpkt;
    224 		if (m == NULL) {
    225 			splx(s);
    226 			senderr(ENOBUFS);
    227 		}
    228 		IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    229 		if (error) {
    230 			/* mbuf is already freed */
    231 			splx(s);
    232 			goto bad;
    233 		}
    234 	}
    235 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    236 		(*ifp->if_start)(ifp);
    237 	splx(s);
    238 	return 0;
    239 
    240   bad:
    241 	while (m0 != NULL) {
    242 		m = m0->m_nextpkt;
    243 		m_freem(m0);
    244 		m0 = m;
    245 	}
    246 
    247 	return error;
    248 }
    249 
    250 struct mbuf *
    251 ieee1394_fragment(struct ifnet *ifp, struct mbuf *m0, int maxsize,
    252     uint16_t etype)
    253 {
    254 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    255 	int totlen, fraglen, off;
    256 	struct mbuf *m, **mp;
    257 	struct ieee1394_fraghdr *ifh;
    258 	struct ieee1394_unfraghdr *iuh;
    259 
    260 	totlen = m0->m_pkthdr.len;
    261 	if (totlen + sizeof(struct ieee1394_unfraghdr) <= maxsize) {
    262 		M_PREPEND(m0, sizeof(struct ieee1394_unfraghdr), M_DONTWAIT);
    263 		if (m0 == NULL)
    264 			goto bad;
    265 		iuh = mtod(m0, struct ieee1394_unfraghdr *);
    266 		iuh->iuh_ft = 0;
    267 		iuh->iuh_etype = etype;
    268 		return m0;
    269 	}
    270 
    271 	fraglen = maxsize - sizeof(struct ieee1394_fraghdr);
    272 
    273 	M_PREPEND(m0, sizeof(struct ieee1394_fraghdr), M_DONTWAIT);
    274 	if (m0 == NULL)
    275 		goto bad;
    276 	ifh = mtod(m0, struct ieee1394_fraghdr *);
    277 	ifh->ifh_ft_size = htons(IEEE1394_FT_MORE | (totlen - 1));
    278 	ifh->ifh_etype_off = etype;
    279 	ifh->ifh_dgl = htons(ic->ic_dgl);
    280 	ifh->ifh_reserved = 0;
    281 	off = fraglen;
    282 	mp = &m0->m_nextpkt;
    283 	while (off < totlen) {
    284 		if (off + fraglen > totlen)
    285 			fraglen = totlen - off;
    286 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    287 		if (m == NULL)
    288 			goto bad;
    289 		m->m_flags |= m0->m_flags & (M_BCAST|M_MCAST);	/* copy bcast */
    290 		MH_ALIGN(m, sizeof(struct ieee1394_fraghdr));
    291 		m->m_len = sizeof(struct ieee1394_fraghdr);
    292 		ifh = mtod(m, struct ieee1394_fraghdr *);
    293 		ifh->ifh_ft_size =
    294 		    htons(IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE | (totlen - 1));
    295 		ifh->ifh_etype_off = htons(off);
    296 		ifh->ifh_dgl = htons(ic->ic_dgl);
    297 		ifh->ifh_reserved = 0;
    298 		m->m_next = m_copy(m0, sizeof(*ifh) + off, fraglen);
    299 		if (m->m_next == NULL)
    300 			goto bad;
    301 		m->m_pkthdr.len = sizeof(*ifh) + fraglen;
    302 		off += fraglen;
    303 		*mp = m;
    304 		mp = &m->m_nextpkt;
    305 	}
    306 	ifh->ifh_ft_size &= ~htons(IEEE1394_FT_MORE);	/* last fragment */
    307 	m_adj(m0, -(m0->m_pkthdr.len - maxsize));
    308 
    309 	ic->ic_dgl++;
    310 	return m0;
    311 
    312   bad:
    313 	while ((m = m0) != NULL) {
    314 		m0 = m->m_nextpkt;
    315 		m->m_nextpkt = NULL;
    316 		m_freem(m);
    317 	}
    318 	return NULL;
    319 }
    320 
    321 void
    322 ieee1394_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
    323 {
    324 	pktqueue_t *pktq = NULL;
    325 	struct ifqueue *inq;
    326 	uint16_t etype;
    327 	int s;
    328 	struct ieee1394_unfraghdr *iuh;
    329 	int isr = 0;
    330 
    331 	if ((ifp->if_flags & IFF_UP) == 0) {
    332 		m_freem(m);
    333 		return;
    334 	}
    335 	if (m->m_len < sizeof(*iuh)) {
    336 		if ((m = m_pullup(m, sizeof(*iuh))) == NULL)
    337 			return;
    338 	}
    339 
    340 	iuh = mtod(m, struct ieee1394_unfraghdr *);
    341 
    342 	if (ntohs(iuh->iuh_ft) & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE)) {
    343 		if ((m = ieee1394_reass(ifp, m, src)) == NULL)
    344 			return;
    345 		iuh = mtod(m, struct ieee1394_unfraghdr *);
    346 	}
    347 	etype = ntohs(iuh->iuh_etype);
    348 
    349 	/* strip off the ieee1394 header */
    350 	m_adj(m, sizeof(*iuh));
    351 	if (ifp->if_bpf) {
    352 		struct ieee1394_bpfhdr h;
    353 		struct m_tag *mtag;
    354 		const struct ieee1394_hwaddr *myaddr;
    355 
    356 		mtag = m_tag_find(m, MTAG_FIREWIRE_SENDER_EUID, 0);
    357 		if (mtag)
    358 			memcpy(h.ibh_shost, mtag + 1, 8);
    359 		else
    360 			memset(h.ibh_shost, 0, 8);
    361 		if (m->m_flags & M_BCAST)
    362 			memcpy(h.ibh_dhost,
    363 			    ((const struct ieee1394_hwaddr *)
    364 			    ifp->if_broadcastaddr)->iha_uid, 8);
    365 		else {
    366 			myaddr =
    367 			  (const struct ieee1394_hwaddr *)CLLADDR(ifp->if_sadl);
    368 			memcpy(h.ibh_dhost, myaddr->iha_uid, 8);
    369 		}
    370 		h.ibh_type = htons(etype);
    371 		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
    372 	}
    373 
    374 	switch (etype) {
    375 #ifdef INET
    376 	case ETHERTYPE_IP:
    377 		pktq = ip_pktq;
    378 		break;
    379 
    380 	case ETHERTYPE_ARP:
    381 		isr = NETISR_ARP;
    382 		inq = &arpintrq;
    383 		break;
    384 #endif /* INET */
    385 
    386 #ifdef INET6
    387 	case ETHERTYPE_IPV6:
    388 		pktq = ip6_pktq;
    389 		break;
    390 #endif /* INET6 */
    391 
    392 	default:
    393 		m_freem(m);
    394 		return;
    395 	}
    396 
    397 	if (__predict_true(pktq)) {
    398 		if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
    399 			m_freem(m);
    400 		}
    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 		schednetisr(isr);
    411 	}
    412 	splx(s);
    413 }
    414 
    415 static struct mbuf *
    416 ieee1394_reass(struct ifnet *ifp, struct mbuf *m0, uint16_t src)
    417 {
    418 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    419 	struct ieee1394_fraghdr *ifh;
    420 	struct ieee1394_unfraghdr *iuh;
    421 	struct ieee1394_reassq *rq;
    422 	struct ieee1394_reass_pkt *rp, *trp, *nrp = NULL;
    423 	int len;
    424 	uint16_t etype, off, ftype, size, dgl;
    425 	uint32_t id;
    426 
    427 	if (m0->m_len < sizeof(*ifh)) {
    428 		if ((m0 = m_pullup(m0, sizeof(*ifh))) == NULL)
    429 			return NULL;
    430 	}
    431 	ifh = mtod(m0, struct ieee1394_fraghdr *);
    432 	m_adj(m0, sizeof(*ifh));
    433 	size = ntohs(ifh->ifh_ft_size);
    434 	ftype = size & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE);
    435 	size = (size & ~ftype) + 1;
    436 	dgl = ntohs(ifh->ifh_dgl);
    437 	len = m0->m_pkthdr.len;
    438 	id = dgl | (src << 16);
    439 	if (ftype & IEEE1394_FT_SUBSEQ) {
    440 		m_tag_delete_chain(m0, NULL);
    441 		m0->m_flags &= ~M_PKTHDR;
    442 		etype = 0;
    443 		off = ntohs(ifh->ifh_etype_off);
    444 	} else {
    445 		etype = ifh->ifh_etype_off;
    446 		off = 0;
    447 	}
    448 
    449 	for (rq = LIST_FIRST(&ic->ic_reassq); ; rq = LIST_NEXT(rq, rq_node)) {
    450 		if (rq == NULL) {
    451 			/*
    452 			 * Create a new reassemble queue head for the node.
    453 			 */
    454 			rq = malloc(sizeof(*rq), M_FTABLE, M_NOWAIT);
    455 			if (rq == NULL) {
    456 				m_freem(m0);
    457 				return NULL;
    458 			}
    459 			rq->fr_id = id;
    460 			LIST_INIT(&rq->rq_pkt);
    461 			LIST_INSERT_HEAD(&ic->ic_reassq, rq, rq_node);
    462 			break;
    463 		}
    464 		if (rq->fr_id == id)
    465 			break;
    466 	}
    467 	for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
    468 		nrp = LIST_NEXT(rp, rp_next);
    469 		if (rp->rp_dgl != dgl)
    470 			continue;
    471 		/*
    472 		 * sanity check:
    473 		 * datagram size must be same for all fragments, and
    474 		 * no overlap is allowed.
    475 		 */
    476 		if (rp->rp_size != size ||
    477 		    (off < rp->rp_off + rp->rp_len && off + len > rp->rp_off)) {
    478 			/*
    479 			 * This happens probably due to wrapping dgl value.
    480 			 * Destroy all previously received fragment and
    481 			 * enqueue current fragment.
    482 			 */
    483 			for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL;
    484 			    rp = nrp) {
    485 				nrp = LIST_NEXT(rp, rp_next);
    486 				if (rp->rp_dgl == dgl) {
    487 					LIST_REMOVE(rp, rp_next);
    488 					m_freem(rp->rp_m);
    489 					free(rp, M_FTABLE);
    490 				}
    491 			}
    492 			break;
    493 		}
    494 		if (rp->rp_off + rp->rp_len == off) {
    495 			/*
    496 			 * All the subsequent fragments received in sequence
    497 			 * come here.
    498 			 * Concatinate mbuf to previous one instead of
    499 			 * allocating new reassemble queue structure,
    500 			 * and try to merge more with the subsequent fragment
    501 			 * in the queue.
    502 			 */
    503 			m_cat(rp->rp_m, m0);
    504 			rp->rp_len += len;
    505 			while (rp->rp_off + rp->rp_len < size &&
    506 			    nrp != NULL && nrp->rp_dgl == dgl &&
    507 			    nrp->rp_off == rp->rp_off + rp->rp_len) {
    508 				LIST_REMOVE(nrp, rp_next);
    509 				m_cat(rp->rp_m, nrp->rp_m);
    510 				rp->rp_len += nrp->rp_len;
    511 				free(nrp, M_FTABLE);
    512 				nrp = LIST_NEXT(rp, rp_next);
    513 			}
    514 			m0 = NULL;	/* mark merged */
    515 			break;
    516 		}
    517 		if (off + m0->m_pkthdr.len == rp->rp_off) {
    518 			m_cat(m0, rp->rp_m);
    519 			rp->rp_m = m0;
    520 			rp->rp_off = off;
    521 			rp->rp_etype = etype;	 /* over writing trust etype */
    522 			rp->rp_len += len;
    523 			m0 = NULL;	/* mark merged */
    524 			break;
    525 		}
    526 		if (rp->rp_off > off) {
    527 			/* insert before rp */
    528 			nrp = rp;
    529 			break;
    530 		}
    531 		if (nrp == NULL || nrp->rp_dgl != dgl) {
    532 			/* insert after rp */
    533 			nrp = NULL;
    534 			break;
    535 		}
    536 	}
    537 	if (m0 == NULL) {
    538 		if (rp->rp_off != 0 || rp->rp_len != size)
    539 			return NULL;
    540 		/* fragment done */
    541 		LIST_REMOVE(rp, rp_next);
    542 		m0 = rp->rp_m;
    543 		m0->m_pkthdr.len = rp->rp_len;
    544 		M_PREPEND(m0, sizeof(*iuh), M_DONTWAIT);
    545 		if (m0 != NULL) {
    546 			iuh = mtod(m0, struct ieee1394_unfraghdr *);
    547 			iuh->iuh_ft = 0;
    548 			iuh->iuh_etype = rp->rp_etype;
    549 		}
    550 		free(rp, M_FTABLE);
    551 		return m0;
    552 	}
    553 
    554 	/*
    555 	 * New fragment received.  Allocate reassemble queue structure.
    556 	 */
    557 	trp = malloc(sizeof(*trp), M_FTABLE, M_NOWAIT);
    558 	if (trp == NULL) {
    559 		m_freem(m0);
    560 		return NULL;
    561 	}
    562 	trp->rp_m = m0;
    563 	trp->rp_size = size;
    564 	trp->rp_etype = etype;		 /* valid only if off==0 */
    565 	trp->rp_off = off;
    566 	trp->rp_dgl = dgl;
    567 	trp->rp_len = len;
    568 	trp->rp_ttl = IEEE1394_REASS_TIMEOUT;
    569 	if (trp->rp_ttl <= ifp->if_timer)
    570 		trp->rp_ttl = ifp->if_timer + 1;
    571 
    572 	if (rp == NULL) {
    573 		/* first fragment for the dgl */
    574 		LIST_INSERT_HEAD(&rq->rq_pkt, trp, rp_next);
    575 	} else if (nrp == NULL) {
    576 		/* no next fragment for the dgl */
    577 		LIST_INSERT_AFTER(rp, trp, rp_next);
    578 	} else {
    579 		/* there is a hole */
    580 		LIST_INSERT_BEFORE(nrp, trp, rp_next);
    581 	}
    582 	return NULL;
    583 }
    584 
    585 void
    586 ieee1394_drain(struct ifnet *ifp)
    587 {
    588 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    589 	struct ieee1394_reassq *rq;
    590 	struct ieee1394_reass_pkt *rp;
    591 
    592 	while ((rq = LIST_FIRST(&ic->ic_reassq)) != NULL) {
    593 		LIST_REMOVE(rq, rq_node);
    594 		while ((rp = LIST_FIRST(&rq->rq_pkt)) != NULL) {
    595 			LIST_REMOVE(rp, rp_next);
    596 			m_freem(rp->rp_m);
    597 			free(rp, M_FTABLE);
    598 		}
    599 		free(rq, M_FTABLE);
    600 	}
    601 }
    602 
    603 void
    604 ieee1394_watchdog(struct ifnet *ifp)
    605 {
    606 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    607 	struct ieee1394_reassq *rq;
    608 	struct ieee1394_reass_pkt *rp, *nrp;
    609 	int dec;
    610 
    611 	dec = (ifp->if_timer > 0) ? ifp->if_timer : 1;
    612 	for (rq = LIST_FIRST(&ic->ic_reassq); rq != NULL;
    613 	    rq = LIST_NEXT(rq, rq_node)) {
    614 		for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
    615 			nrp = LIST_NEXT(rp, rp_next);
    616 			if (rp->rp_ttl >= dec)
    617 				rp->rp_ttl -= dec;
    618 			else {
    619 				LIST_REMOVE(rp, rp_next);
    620 				m_freem(rp->rp_m);
    621 				free(rp, M_FTABLE);
    622 			}
    623 		}
    624 	}
    625 }
    626 
    627 const char *
    628 ieee1394_sprintf(const uint8_t *laddr)
    629 {
    630 	static char buf[3*8];
    631 
    632 	snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
    633 	    laddr[0], laddr[1], laddr[2], laddr[3],
    634 	    laddr[4], laddr[5], laddr[6], laddr[7]);
    635 	return buf;
    636 }
    637 
    638 void
    639 ieee1394_ifattach(struct ifnet *ifp, const struct ieee1394_hwaddr *hwaddr)
    640 {
    641 	struct ieee1394_hwaddr *baddr;
    642 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    643 
    644 	ifp->if_type = IFT_IEEE1394;
    645 	ifp->if_hdrlen = sizeof(struct ieee1394_header);
    646 	ifp->if_dlt = DLT_EN10MB;	/* XXX */
    647 	ifp->if_mtu = IEEE1394MTU;
    648 	ifp->if_output = ieee1394_output;
    649 	ifp->if_drain = ieee1394_drain;
    650 	ifp->if_watchdog = ieee1394_watchdog;
    651 	ifp->if_timer = 1;
    652 	if (ifp->if_baudrate == 0)
    653 		ifp->if_baudrate = IF_Mbps(100);
    654 
    655 	if_set_sadl(ifp, hwaddr, sizeof(struct ieee1394_hwaddr), true);
    656 
    657 	baddr = malloc(ifp->if_addrlen, M_DEVBUF, M_WAITOK);
    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 	ifp->if_broadcastaddr = (uint8_t *)baddr;
    663 	LIST_INIT(&ic->ic_reassq);
    664 	bpf_attach(ifp, DLT_APPLE_IP_OVER_IEEE1394,
    665 	    sizeof(struct ieee1394_hwaddr));
    666 }
    667 
    668 void
    669 ieee1394_ifdetach(struct ifnet *ifp)
    670 {
    671 	ieee1394_drain(ifp);
    672 	bpf_detach(ifp);
    673 	free(__UNCONST(ifp->if_broadcastaddr), M_DEVBUF);
    674 	ifp->if_broadcastaddr = NULL;
    675 }
    676 
    677 int
    678 ieee1394_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    679 {
    680 	struct ifreq *ifr = (struct ifreq *)data;
    681 	struct ifaddr *ifa = (struct ifaddr *)data;
    682 	int error = 0;
    683 
    684 	switch (cmd) {
    685 	case SIOCINITIFADDR:
    686 		ifp->if_flags |= IFF_UP;
    687 		switch (ifa->ifa_addr->sa_family) {
    688 #ifdef INET
    689 		case AF_INET:
    690 			if ((error = (*ifp->if_init)(ifp)) != 0)
    691 				break;
    692 			arp_ifinit(ifp, ifa);
    693 			break;
    694 #endif /* INET */
    695 		default:
    696 			error = (*ifp->if_init)(ifp);
    697 			break;
    698 		}
    699 		break;
    700 
    701 	case SIOCSIFMTU:
    702 		if (ifr->ifr_mtu > IEEE1394MTU)
    703 			error = EINVAL;
    704 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    705 			error = 0;
    706 		break;
    707 
    708 	default:
    709 		error = ifioctl_common(ifp, cmd, data);
    710 		break;
    711 	}
    712 
    713 	return error;
    714 }
    715