Home | History | Annotate | Line # | Download | only in net
if_ieee1394subr.c revision 1.4
      1 /*	$NetBSD: if_ieee1394subr.c,v 1.4 2000/12/12 18:00:27 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include "opt_inet.h"
     40 #include "bpfilter.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/types.h>
     45 #include <sys/socket.h>
     46 #include <sys/sockio.h>
     47 #include <sys/kernel.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/device.h>
     50 
     51 #include <net/if.h>
     52 #include <net/if_dl.h>
     53 #include <net/if_ieee1394.h>
     54 #include <net/if_types.h>
     55 #include <net/if_media.h>
     56 #include <net/ethertypes.h>
     57 #include <net/netisr.h>
     58 #include <net/route.h>
     59 
     60 #if NBPFILTER > 0
     61 #include <net/bpf.h>
     62 #endif
     63 
     64 #ifdef INET
     65 #include <netinet/in.h>
     66 #include <netinet/in_var.h>
     67 #include <netinet/if_ieee1394arp.h>
     68 #endif /* INET */
     69 #ifdef INET6
     70 #include <netinet/in.h>
     71 #include <netinet6/in6_var.h>
     72 #include <netinet6/nd6.h>
     73 #endif /* INET6 */
     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 *, struct sockaddr *,
     80 		struct rtentry *);
     81 static void ieee1394_input(struct ifnet *, struct mbuf *);
     82 static struct mbuf *ieee1394_reass(struct ifnet *, struct mbuf *);
     83 
     84 static int
     85 ieee1394_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
     86     struct rtentry *rt0)
     87 {
     88 	u_int16_t etype = 0;
     89 	struct mbuf *m;
     90 	int s, hdrlen, error = 0;
     91 	struct rtentry *rt;
     92 	struct mbuf *mcopy = NULL;
     93 	struct ieee1394_hwaddr hwdst, *myaddr;
     94 #ifdef INET
     95 	struct ieee1394_arphdr *ah;
     96 #endif /* INET */
     97 
     98 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
     99 		senderr(ENETDOWN);
    100 	ifp->if_lastchange = time;
    101 	if ((rt = rt0) != NULL) {
    102 		if ((rt->rt_flags & RTF_UP) == 0) {
    103 			if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
    104 				rt->rt_refcnt--;
    105 				if (rt->rt_ifp != ifp)
    106 					return (*rt->rt_ifp->if_output)
    107 							(ifp, m0, dst, rt);
    108 			} else
    109 				senderr(EHOSTUNREACH);
    110 		}
    111 		if (rt->rt_flags & RTF_GATEWAY) {
    112 			if (rt->rt_gwroute == NULL)
    113 				goto lookup;
    114 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
    115 				rtfree(rt);
    116 				rt = rt0;
    117   lookup:
    118 				rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
    119 				if ((rt = rt->rt_gwroute) == NULL)
    120 					senderr(EHOSTUNREACH);
    121 				/* the "G" test below also prevents rt == rt0 */
    122 				if ((rt->rt_flags & RTF_GATEWAY) ||
    123 				    (rt->rt_ifp != ifp)) {
    124 					rt->rt_refcnt--;
    125 					rt0->rt_gwroute = NULL;
    126 					senderr(EHOSTUNREACH);
    127 				}
    128 			}
    129 		}
    130 		if (rt->rt_flags & RTF_REJECT)
    131 			if (rt->rt_rmx.rmx_expire == 0 ||
    132 			    time.tv_sec < rt->rt_rmx.rmx_expire)
    133 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
    134 	}
    135 	switch (dst->sa_family) {
    136 #ifdef INET
    137 	case AF_INET:
    138 		if (m0->m_flags & (M_BCAST | M_MCAST))
    139 			memcpy(&hwdst, ifp->if_broadcastaddr, sizeof(hwdst));
    140 		else if (!ieee1394arpresolve(ifp, rt, m0, dst, &hwdst))
    141 			return 0;	/* if not yet resolved */
    142 		/* if broadcasting on a simplex interface, loopback a copy */
    143 		if ((m0->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    144 			mcopy = m_copy(m0, 0, M_COPYALL);
    145 		etype = htons(ETHERTYPE_IP);
    146 		break;
    147 	case AF_ARP:
    148 		ah = mtod(m0, struct ieee1394_arphdr *);
    149 		memcpy(&hwdst, ifp->if_broadcastaddr, sizeof(hwdst));
    150 		etype = htons(ETHERTYPE_ARP);
    151 		break;
    152 #endif /* INET */
    153 #ifdef INET6
    154 	case AF_INET6:
    155 		if (m0->m_flags & M_MCAST)
    156 			memcpy(&hwdst, ifp->if_broadcastaddr, sizeof(hwdst));
    157 		else if (!nd6_storelladdr(ifp, rt, m0, dst, (u_char *)&hwdst)) {
    158 			/* this must be impossible, so we bark */
    159 			printf("ieee1394_output: nd6_storelladdr failed\n");
    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 #if NBPFILTER > 0
    179 	/* XXX: emulate DLT_EN10MB */
    180 	if (ifp->if_bpf) {
    181 		struct mbuf mb;
    182 
    183 		mb.m_next = m0;
    184 		mb.m_len = 14;
    185 		mb.m_data = mb.m_dat;
    186 		((u_int32_t *)mb.m_data)[0] = 0;
    187 		((u_int32_t *)mb.m_data)[1] = 0;
    188 		((u_int32_t *)mb.m_data)[2] = 0;
    189 		((u_int16_t *)mb.m_data)[6] = etype;
    190 		bpf_mtap(ifp->if_bpf, &mb);
    191 	}
    192 #endif
    193 	myaddr = (struct ieee1394_hwaddr *)LLADDR(ifp->if_sadl);
    194 	if ((ifp->if_flags & IFF_SIMPLEX) &&
    195 	    memcmp(&hwdst, myaddr, IEEE1394_ADDR_LEN) == 0)
    196 		return looutput(ifp, m0, dst, rt);
    197 
    198 	/*
    199 	 * XXX:
    200 	 * The maximum possible rate depends on the topology.
    201 	 * So the determination of maxrec and fragmentation should be
    202 	 * called from the driver after probing the topology map.
    203 	 */
    204 	if (m0->m_flags & (M_BCAST | M_MCAST)) {
    205 		hdrlen = IEEE1394_GASP_LEN;
    206 		hwdst.iha_speed = 0;	/* XXX */
    207 	} else
    208 		hdrlen = 0;
    209 	if (hwdst.iha_speed > myaddr->iha_speed)
    210 		hwdst.iha_speed = myaddr->iha_speed;
    211 	if (hwdst.iha_maxrec > myaddr->iha_maxrec)
    212 		hwdst.iha_maxrec = myaddr->iha_maxrec;
    213 	if (hwdst.iha_maxrec > (8 + hwdst.iha_speed))
    214 		hwdst.iha_maxrec = 8 + hwdst.iha_speed;
    215 	if (hwdst.iha_maxrec < 8)
    216 		hwdst.iha_maxrec = 8;
    217 
    218 	m0 = ieee1394_fragment(ifp, m0, (2<<hwdst.iha_maxrec) - hdrlen, etype);
    219 	if (m0 == NULL)
    220 		senderr(ENOBUFS);
    221 
    222 	s = splimp();
    223 	if (IF_QFULL(&ifp->if_snd)) {
    224 		IF_DROP(&ifp->if_snd);
    225 		splx(s);
    226 		senderr(ENOBUFS);
    227 	}
    228 	ifp->if_obytes += m0->m_pkthdr.len;
    229 	if (m0->m_flags & M_MCAST)
    230 		ifp->if_omcasts++;
    231 	while ((m = m0) != NULL) {
    232 		m0 = m->m_nextpkt;
    233 		M_PREPEND(m, sizeof(struct ieee1394_header), M_DONTWAIT);
    234 		if (m == NULL) {
    235 			splx(s);
    236 			senderr(ENOBUFS);
    237 		}
    238 		memcpy(mtod(m, caddr_t), &hwdst, sizeof(hwdst));
    239 		IF_ENQUEUE(&ifp->if_snd, m);
    240 	}
    241 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    242 		(*ifp->if_start)(ifp);
    243 	splx(s);
    244 	return 0;
    245 
    246   bad:
    247 	while (m0 != NULL) {
    248 		m = m0->m_nextpkt;
    249 		m_freem(m0);
    250 		m0 = m;
    251 	}
    252 
    253 	return error;
    254 }
    255 
    256 struct mbuf *
    257 ieee1394_fragment(struct ifnet *ifp, struct mbuf *m0, int maxsize,
    258     u_int16_t etype)
    259 {
    260 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    261 	int totlen, fraglen, off;
    262 	struct mbuf *m, **mp;
    263 	struct ieee1394_fraghdr *ifh;
    264 	struct ieee1394_unfraghdr *iuh;
    265 
    266 	totlen = m0->m_pkthdr.len;
    267 	if (totlen + sizeof(struct ieee1394_unfraghdr) <= maxsize) {
    268 		M_PREPEND(m0, sizeof(struct ieee1394_unfraghdr), M_DONTWAIT);
    269 		if (m0 == NULL)
    270 			goto bad;
    271 		iuh = mtod(m0, struct ieee1394_unfraghdr *);
    272 		iuh->iuh_ft = 0;
    273 		iuh->iuh_etype = etype;
    274 		return m0;
    275 	}
    276 
    277 	fraglen = maxsize - sizeof(struct ieee1394_fraghdr);
    278 
    279 	M_PREPEND(m0, sizeof(struct ieee1394_fraghdr), M_DONTWAIT);
    280 	if (m0 == NULL)
    281 		goto bad;
    282 	ifh = mtod(m0, struct ieee1394_fraghdr *);
    283 	ifh->ifh_ft_size = htons(IEEE1394_FT_MORE | (totlen - 1));
    284 	ifh->ifh_etype_off = etype;
    285 	ifh->ifh_dgl = htons(ic->ic_dgl);
    286 	ifh->ifh_reserved = 0;
    287 	off = fraglen;
    288 	mp = &m0->m_nextpkt;
    289 	while (off < totlen) {
    290 		if (off + fraglen > totlen)
    291 			fraglen = totlen - off;
    292 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    293 		if (m == NULL)
    294 			goto bad;
    295 		m->m_flags |= m0->m_flags & (M_BCAST|M_MCAST);	/* copy bcast */
    296 		MH_ALIGN(m, sizeof(struct ieee1394_fraghdr));
    297 		m->m_len = sizeof(struct ieee1394_fraghdr);
    298 		ifh = mtod(m, struct ieee1394_fraghdr *);
    299 		ifh->ifh_ft_size =
    300 		    htons(IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE | (totlen - 1));
    301 		ifh->ifh_etype_off = htons(off);
    302 		ifh->ifh_dgl = htons(ic->ic_dgl);
    303 		ifh->ifh_reserved = 0;
    304 		m->m_next = m_copy(m0, sizeof(*ifh) + off, fraglen);
    305 		if (m->m_next == NULL)
    306 			goto bad;
    307 		m->m_pkthdr.len = sizeof(*ifh) + fraglen;
    308 		off += fraglen;
    309 		*mp = m;
    310 		mp = &m->m_nextpkt;
    311 	}
    312 	ifh->ifh_ft_size &= ~htons(IEEE1394_FT_MORE);	/* last fragment */
    313 	m_adj(m0, -(m0->m_pkthdr.len - maxsize));
    314 
    315 	ic->ic_dgl++;
    316 	return m0;
    317 
    318   bad:
    319 	while ((m = m0) != NULL) {
    320 		m0 = m->m_nextpkt;
    321 		m->m_nextpkt = NULL;
    322 		m_freem(m);
    323 	}
    324 	return NULL;
    325 }
    326 
    327 static void
    328 ieee1394_input(struct ifnet *ifp, struct mbuf *m)
    329 {
    330 	struct ifqueue *inq;
    331 	u_int16_t etype;
    332 	int s;
    333 	struct ieee1394_header *ih;
    334 	struct ieee1394_unfraghdr *iuh;
    335 
    336 	if ((ifp->if_flags & IFF_UP) == 0) {
    337 		m_freem(m);
    338 		return;
    339 	}
    340 	if (m->m_len < sizeof(*ih) + sizeof(*iuh)) {
    341 		if ((m = m_pullup(m, sizeof(*ih) + sizeof(*iuh))) == NULL)
    342 			return;
    343 	}
    344 
    345 	ih = mtod(m, struct ieee1394_header *);
    346 	iuh = (struct ieee1394_unfraghdr *)&ih[1];
    347 
    348 	if (ntohs(iuh->iuh_ft) & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE)) {
    349 		if ((m = ieee1394_reass(ifp, m)) == NULL)
    350 			return;
    351 		ih = mtod(m, struct ieee1394_header *);
    352 		iuh = (struct ieee1394_unfraghdr *)&ih[1];
    353 	}
    354 	etype = ntohs(iuh->iuh_etype);
    355 
    356 	/* strip off the ieee1394 header */
    357 	m_adj(m, sizeof(*ih) + sizeof(*iuh));
    358 #if NBPFILTER > 0
    359 	/* XXX: emulate DLT_EN10MB */
    360 	if (ifp->if_bpf) {
    361 		struct mbuf mb;
    362 
    363 		mb.m_next = m;
    364 		mb.m_len = 14;
    365 		mb.m_data = mb.m_dat;
    366 		((u_int32_t *)mb.m_data)[0] = 0;
    367 		((u_int32_t *)mb.m_data)[1] = 0;
    368 		((u_int32_t *)mb.m_data)[2] = 0;
    369 		((u_int16_t *)mb.m_data)[6] = iuh->iuh_etype;
    370 		bpf_mtap(ifp->if_bpf, &mb);
    371 	}
    372 #endif
    373 
    374 	switch (etype) {
    375 #ifdef INET
    376 	case ETHERTYPE_IP:
    377 		schednetisr(NETISR_IP);
    378 		inq = &ipintrq;
    379 		break;
    380 
    381 	case ETHERTYPE_ARP:
    382 		in_ieee1394arpinput(m);
    383 		return;
    384 #endif /* INET */
    385 
    386 #ifdef INET6
    387 	case ETHERTYPE_IPV6:
    388 		schednetisr(NETISR_IPV6);
    389 		inq = &ip6intrq;
    390 		break;
    391 #endif /* INET6 */
    392 
    393 	default:
    394 		m_freem(m);
    395 		return;
    396 	}
    397 
    398 	s = splimp();
    399 	if (IF_QFULL(inq)) {
    400 		IF_DROP(inq);
    401 		m_freem(m);
    402 	} else
    403 		IF_ENQUEUE(inq, m);
    404 	splx(s);
    405 }
    406 
    407 static struct mbuf *
    408 ieee1394_reass(struct ifnet *ifp, struct mbuf *m0)
    409 {
    410 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    411 	struct ieee1394_header *ih;
    412 	struct ieee1394_fraghdr *ifh;
    413 	struct ieee1394_unfraghdr *iuh;
    414 	struct ieee1394_reassq *rq;
    415 	struct ieee1394_reass_pkt *rp, *trp, *nrp;
    416 	int len;
    417 	u_int16_t off, ftype, size, dgl;
    418 
    419 	if (m0->m_len < sizeof(*ih) + sizeof(*ifh)) {
    420 		if ((m0 = m_pullup(m0, sizeof(*ih) + sizeof(*ifh))) == NULL)
    421 			return NULL;
    422 	}
    423 	ih = mtod(m0, struct ieee1394_header *);
    424 	ifh = (struct ieee1394_fraghdr *)&ih[1];
    425 	m_adj(m0, sizeof(*ih) + sizeof(*ifh));
    426 	size = ntohs(ifh->ifh_ft_size);
    427 	ftype = size & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE);
    428 	size = (size & ~ftype) + 1;
    429 	dgl = ifh->ifh_dgl;
    430 	len = m0->m_pkthdr.len;
    431 	if (ftype & IEEE1394_FT_SUBSEQ) {
    432 		m0->m_flags &= ~M_PKTHDR;
    433 		off = ntohs(ifh->ifh_etype_off);
    434 	} else
    435 		off = 0;
    436 
    437 	for (rq = LIST_FIRST(&ic->ic_reassq); ; rq = LIST_NEXT(rq, rq_node)) {
    438 		if (rq == NULL) {
    439 			/*
    440 			 * Create a new reassemble queue head for the node.
    441 			 */
    442 			rq = malloc(sizeof(*rq), M_FTABLE, M_NOWAIT);
    443 			if (rq == NULL) {
    444 				m_freem(m0);
    445 				return NULL;
    446 			}
    447 			memcpy(rq->rq_uid, ih->ih_uid, IEEE1394_ADDR_LEN);
    448 			LIST_INIT(&rq->rq_pkt);
    449 			LIST_INSERT_HEAD(&ic->ic_reassq, rq, rq_node);
    450 			break;
    451 		}
    452 		if (memcmp(rq->rq_uid, ih->ih_uid, IEEE1394_ADDR_LEN) == 0)
    453 			break;
    454 	}
    455 	for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
    456 		nrp = LIST_NEXT(rp, rp_next);
    457 		if (rp->rp_dgl != dgl)
    458 			continue;
    459 		/*
    460 		 * sanity check:
    461 		 * datagram size must be same for all fragments, and
    462 		 * no overlap is allowed.
    463 		 */
    464 		if (rp->rp_size != size ||
    465 		    (off < rp->rp_off + rp->rp_len && off + len > rp->rp_off)) {
    466 			/*
    467 			 * This happens probably due to wrapping dgl value.
    468 			 * Destroy all previously received fragment and
    469 			 * enqueue current fragment.
    470 			 */
    471 			for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL;
    472 			    rp = nrp) {
    473 				nrp = LIST_NEXT(rp, rp_next);
    474 				if (rp->rp_dgl == dgl) {
    475 					LIST_REMOVE(rp, rp_next);
    476 					m_freem(rp->rp_m);
    477 					free(rp, M_FTABLE);
    478 				}
    479 			}
    480 			break;
    481 		}
    482 		if (rp->rp_off + rp->rp_len == off) {
    483 			/*
    484 			 * All the subsequent fragments received in sequence
    485 			 * come here.
    486 			 * Concatinate mbuf to previous one instead of
    487 			 * allocating new reassemble queue structure,
    488 			 * and try to merge more with the subsequent fragment
    489 			 * in the queue.
    490 			 */
    491 			m_cat(rp->rp_m, m0);
    492 			rp->rp_len += len;
    493 			while (rp->rp_off + rp->rp_len < size &&
    494 			    nrp != NULL && nrp->rp_dgl == dgl &&
    495 			    nrp->rp_off == rp->rp_off + rp->rp_len) {
    496 				LIST_REMOVE(nrp, rp_next);
    497 				m_cat(rp->rp_m, nrp->rp_m);
    498 				rp->rp_len += nrp->rp_len;
    499 				free(nrp, M_FTABLE);
    500 				nrp = LIST_NEXT(rp, rp_next);
    501 			}
    502 			m0 = NULL;	/* mark merged */
    503 			break;
    504 		}
    505 		if (off + m0->m_pkthdr.len == rp->rp_off) {
    506 			m_cat(m0, rp->rp_m);
    507 			rp->rp_m = m0;
    508 			rp->rp_off = off;
    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(*ih) + sizeof(*iuh), M_DONTWAIT);
    532 		if (m0 != NULL) {
    533 			ih = mtod(m0, struct ieee1394_header *);
    534 			iuh = (struct ieee1394_unfraghdr *)&ih[1];
    535 			memcpy(ih, &rp->rp_hdr, sizeof(*ih));
    536 			iuh->iuh_ft = 0;
    537 			iuh->iuh_etype = rp->rp_etype;
    538 		}
    539 		free(rp, M_FTABLE);
    540 		return m0;
    541 	}
    542 
    543 	/*
    544 	 * New fragment received.  Allocate reassemble queue structure.
    545 	 */
    546 	trp = malloc(sizeof(*trp), M_FTABLE, M_NOWAIT);
    547 	if (trp == NULL) {
    548 		m_freem(m0);
    549 		return NULL;
    550 	}
    551 	trp->rp_m = m0;
    552 	memcpy(&trp->rp_hdr, ih, sizeof(*ih));
    553 	trp->rp_size = size;
    554 	trp->rp_etype = ifh->ifh_etype_off;	 /* valid only if off==0 */
    555 	trp->rp_off = off;
    556 	trp->rp_dgl = dgl;
    557 	trp->rp_len = len;
    558 	trp->rp_ttl = IEEE1394_REASS_TIMEOUT;
    559 	if (trp->rp_ttl <= ifp->if_timer)
    560 		trp->rp_ttl = ifp->if_timer + 1;
    561 
    562 	if (rp == NULL) {
    563 		/* first fragment for the dgl */
    564 		LIST_INSERT_HEAD(&rq->rq_pkt, trp, rp_next);
    565 	} else if (nrp == NULL) {
    566 		/* no next fragment for the dgl */
    567 		LIST_INSERT_AFTER(rp, trp, rp_next);
    568 	} else {
    569 		/* there is a hole */
    570 		LIST_INSERT_BEFORE(nrp, trp, rp_next);
    571 	}
    572 	return NULL;
    573 }
    574 
    575 void
    576 ieee1394_drain(struct ifnet *ifp)
    577 {
    578 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    579 	struct ieee1394_reassq *rq;
    580 	struct ieee1394_reass_pkt *rp;
    581 
    582 	while ((rq = LIST_FIRST(&ic->ic_reassq)) != NULL) {
    583 		LIST_REMOVE(rq, rq_node);
    584 		while ((rp = LIST_FIRST(&rq->rq_pkt)) != NULL) {
    585 			LIST_REMOVE(rp, rp_next);
    586 			m_freem(rp->rp_m);
    587 			free(rp, M_FTABLE);
    588 		}
    589 		free(rq, M_FTABLE);
    590 	}
    591 }
    592 
    593 void
    594 ieee1394_watchdog(struct ifnet *ifp)
    595 {
    596 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    597 	struct ieee1394_reassq *rq;
    598 	struct ieee1394_reass_pkt *rp, *nrp;
    599 	int dec;
    600 
    601 	dec = (ifp->if_timer > 0) ? ifp->if_timer : 1;
    602 	for (rq = LIST_FIRST(&ic->ic_reassq); rq != NULL;
    603 	    rq = LIST_NEXT(rq, rq_node)) {
    604 		for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
    605 			nrp = LIST_NEXT(rp, rp_next);
    606 			if (rp->rp_ttl >= dec)
    607 				rp->rp_ttl -= dec;
    608 			else {
    609 				LIST_REMOVE(rp, rp_next);
    610 				m_freem(rp->rp_m);
    611 				free(rp, M_FTABLE);
    612 			}
    613 		}
    614 	}
    615 }
    616 
    617 const char *
    618 ieee1394_sprintf(const u_int8_t *laddr)
    619 {
    620 	static char buf[3*8];
    621 
    622 	snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
    623 	    laddr[0], laddr[1], laddr[2], laddr[3],
    624 	    laddr[4], laddr[5], laddr[6], laddr[7]);
    625 	return buf;
    626 }
    627 
    628 void
    629 ieee1394_ifattach(struct ifnet *ifp, const struct ieee1394_hwaddr *hwaddr)
    630 {
    631 	struct sockaddr_dl *sdl;
    632 	struct ieee1394_hwaddr *baddr;
    633 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    634 
    635 	ifp->if_type = IFT_IEEE1394;
    636 	ifp->if_addrlen = sizeof(struct ieee1394_hwaddr);
    637 	ifp->if_hdrlen = sizeof(struct ieee1394_header);
    638 	ifp->if_mtu = IEEE1394MTU;
    639 	ifp->if_output = ieee1394_output;
    640 	ifp->if_input = ieee1394_input;
    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 	if ((sdl = ifp->if_sadl) && sdl->sdl_family == AF_LINK) {
    647 		sdl->sdl_type = ifp->if_type;
    648 		sdl->sdl_alen = ifp->if_addrlen;
    649 		memcpy(LLADDR(sdl), hwaddr, ifp->if_addrlen);
    650 	}
    651 	ifp->if_broadcastaddr = malloc(ifp->if_addrlen, M_DEVBUF, M_WAITOK);
    652 	baddr = (struct ieee1394_hwaddr *)ifp->if_broadcastaddr;
    653 	memset(baddr->iha_uid, 0xff, IEEE1394_ADDR_LEN);
    654 	baddr->iha_speed = 0;	/*XXX: how to determine the speed for bcast? */
    655 	baddr->iha_maxrec = 512 << baddr->iha_speed;
    656 	memset(baddr->iha_offset, 0, sizeof(baddr->iha_offset));
    657 	LIST_INIT(&ic->ic_reassq);
    658 #if NBPFILTER > 0
    659 	bpfattach(ifp, DLT_EN10MB, 14);	/* XXX */
    660 #endif
    661 }
    662 
    663 void
    664 ieee1394_ifdetach(struct ifnet *ifp)
    665 {
    666 	struct sockaddr_dl *sdl = ifp->if_sadl;
    667 
    668 	ieee1394_drain(ifp);
    669 	free(ifp->if_broadcastaddr, M_DEVBUF);
    670 	ifp->if_broadcastaddr = NULL;
    671 	memset(LLADDR(sdl), 0, sizeof(struct ieee1394_hwaddr));
    672 	sdl->sdl_alen = 0;
    673 	sdl->sdl_type = 0;
    674 #if NBPFILTER > 0
    675 	bpfdetach(ifp);
    676 #endif
    677 }
    678 
    679 int
    680 ieee1394_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    681 {
    682 	struct ifreq *ifr = (struct ifreq *)data;
    683 	struct ifaddr *ifa = (struct ifaddr *)data;
    684 	int error = 0;
    685 #if __NetBSD_Version < 105080000
    686 	int fw_init(struct ifnet *);
    687 	void fw_stop(struct ifnet *, int);
    688 #endif
    689 
    690 	switch (cmd) {
    691 	case SIOCSIFADDR:
    692 		ifp->if_flags |= IFF_UP;
    693 		switch (ifa->ifa_addr->sa_family) {
    694 #ifdef INET
    695 		case AF_INET:
    696 #if __NetBSD_Version >= 105080000
    697 			if ((error = (*ifp->if_init)(ifp)) != 0)
    698 #else
    699 			if ((error = fw_init(ifp)) != 0)
    700 #endif
    701 				break;
    702 			ieee1394arp_ifinit(ifp, ifa);
    703 			break;
    704 #endif /* INET */
    705 		default:
    706 #if __NetBSD_Version >= 105080000
    707 			error = (*ifp->if_init)(ifp);
    708 #else
    709 			error = fw_init(ifp);
    710 #endif
    711 			break;
    712 		}
    713 		break;
    714 
    715 	case SIOCGIFADDR:
    716 		memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
    717 		    LLADDR(ifp->if_sadl), IEEE1394_ADDR_LEN);
    718 		    break;
    719 
    720 	case SIOCSIFMTU:
    721 		if (ifr->ifr_mtu > IEEE1394MTU)
    722 			error = EINVAL;
    723 		else
    724 			ifp->if_mtu = ifr->ifr_mtu;
    725 		break;
    726 
    727 	case SIOCSIFFLAGS:
    728 #if __NetBSD_Version >= 105080000
    729 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING)
    730 			(*ifp->if_stop)(ifp, 1);
    731 		else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP)
    732 			error = (*ifp->if_init)(ifp);
    733 		else if ((ifp->if_flags & IFF_UP) != 0)
    734 			error = (*ifp->if_init)(ifp);
    735 #else
    736 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING)
    737 			fw_stop(ifp, 1);
    738 		else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP)
    739 			error = fw_init(ifp);
    740 		else if ((ifp->if_flags & IFF_UP) != 0)
    741 			error = fw_init(ifp);
    742 #endif
    743 		break;
    744 
    745 	case SIOCADDMULTI:
    746 	case SIOCDELMULTI:
    747 		/* nothing to do */
    748 		break;
    749 
    750 	default:
    751 		error = ENOTTY;
    752 		break;
    753 	}
    754 
    755 	return error;
    756 }
    757