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