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