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