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