Home | History | Annotate | Line # | Download | only in net
if_ieee1394subr.c revision 1.14
      1 /*	$NetBSD: if_ieee1394subr.c,v 1.14 2002/03/05 04:13:01 itojun 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 <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: if_ieee1394subr.c,v 1.14 2002/03/05 04:13:01 itojun Exp $");
     41 
     42 #include "opt_inet.h"
     43 #include "bpfilter.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/socket.h>
     48 #include <sys/sockio.h>
     49 #include <sys/kernel.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/device.h>
     52 
     53 #include <net/if.h>
     54 #include <net/if_dl.h>
     55 #include <net/if_ieee1394.h>
     56 #include <net/if_types.h>
     57 #include <net/if_media.h>
     58 #include <net/ethertypes.h>
     59 #include <net/netisr.h>
     60 #include <net/route.h>
     61 
     62 #if NBPFILTER > 0
     63 #include <net/bpf.h>
     64 #endif
     65 
     66 #ifdef INET
     67 #include <netinet/in.h>
     68 #include <netinet/in_var.h>
     69 #include <netinet/if_ieee1394arp.h>
     70 #endif /* INET */
     71 #ifdef INET6
     72 #include <netinet/in.h>
     73 #include <netinet6/in6_var.h>
     74 #include <netinet6/nd6.h>
     75 #endif /* INET6 */
     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 *, struct sockaddr *,
     82 		struct rtentry *);
     83 static void ieee1394_input(struct ifnet *, struct mbuf *);
     84 static struct mbuf *ieee1394_reass(struct ifnet *, struct mbuf *);
     85 
     86 static int
     87 ieee1394_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
     88     struct rtentry *rt0)
     89 {
     90 	u_int16_t etype = 0;
     91 	struct mbuf *m;
     92 	int s, hdrlen, error = 0;
     93 	struct rtentry *rt;
     94 	struct mbuf *mcopy = NULL;
     95 	struct ieee1394_hwaddr hwdst, *myaddr;
     96 #ifdef INET
     97 	struct ieee1394_arphdr *ah;
     98 #endif /* INET */
     99 	ALTQ_DECL(struct altq_pktattr pktattr;)
    100 
    101 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
    102 		senderr(ENETDOWN);
    103 	if ((rt = rt0) != NULL) {
    104 		if ((rt->rt_flags & RTF_UP) == 0) {
    105 			if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
    106 				rt->rt_refcnt--;
    107 				if (rt->rt_ifp != ifp)
    108 					return (*rt->rt_ifp->if_output)
    109 							(ifp, m0, dst, rt);
    110 			} else
    111 				senderr(EHOSTUNREACH);
    112 		}
    113 		if (rt->rt_flags & RTF_GATEWAY) {
    114 			if (rt->rt_gwroute == NULL)
    115 				goto lookup;
    116 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
    117 				rtfree(rt);
    118 				rt = rt0;
    119   lookup:
    120 				rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
    121 				if ((rt = rt->rt_gwroute) == NULL)
    122 					senderr(EHOSTUNREACH);
    123 				/* the "G" test below also prevents rt == rt0 */
    124 				if ((rt->rt_flags & RTF_GATEWAY) ||
    125 				    (rt->rt_ifp != ifp)) {
    126 					rt->rt_refcnt--;
    127 					rt0->rt_gwroute = NULL;
    128 					senderr(EHOSTUNREACH);
    129 				}
    130 			}
    131 		}
    132 		if (rt->rt_flags & RTF_REJECT)
    133 			if (rt->rt_rmx.rmx_expire == 0 ||
    134 			    time.tv_sec < rt->rt_rmx.rmx_expire)
    135 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
    136 	}
    137 
    138 	/*
    139 	 * If the queueing discipline needs packet classification,
    140 	 * do it before prepending link headers.
    141 	 */
    142 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
    143 
    144 	switch (dst->sa_family) {
    145 #ifdef INET
    146 	case AF_INET:
    147 		if (m0->m_flags & (M_BCAST | M_MCAST))
    148 			memcpy(&hwdst, ifp->if_broadcastaddr, sizeof(hwdst));
    149 		else if (!ieee1394arpresolve(ifp, rt, m0, dst, &hwdst))
    150 			return 0;	/* if not yet resolved */
    151 		/* if broadcasting on a simplex interface, loopback a copy */
    152 		if ((m0->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    153 			mcopy = m_copy(m0, 0, M_COPYALL);
    154 		etype = htons(ETHERTYPE_IP);
    155 		break;
    156 	case AF_ARP:
    157 		ah = mtod(m0, struct ieee1394_arphdr *);
    158 		memcpy(&hwdst, ifp->if_broadcastaddr, sizeof(hwdst));
    159 		etype = htons(ETHERTYPE_ARP);
    160 		break;
    161 #endif /* INET */
    162 #ifdef INET6
    163 	case AF_INET6:
    164 		if (m0->m_flags & M_MCAST)
    165 			memcpy(&hwdst, ifp->if_broadcastaddr, sizeof(hwdst));
    166 		else if (!nd6_storelladdr(ifp, rt, m0, dst, (u_char *)&hwdst)) {
    167 			/* this must be impossible, so we bark */
    168 			printf("ieee1394_output: nd6_storelladdr failed\n");
    169 			return 0;
    170 		}
    171 		etype = htons(ETHERTYPE_IPV6);
    172 		break;
    173 #endif /* INET6 */
    174 
    175 	case pseudo_AF_HDRCMPLT:
    176 	case AF_UNSPEC:
    177 		/* TODO? */
    178 	default:
    179 		printf("%s: can't handle af%d\n", ifp->if_xname,
    180 		    dst->sa_family);
    181 		senderr(EAFNOSUPPORT);
    182 		break;
    183 	}
    184 
    185 	if (mcopy)
    186 		looutput(ifp, mcopy, dst, rt);
    187 #if NBPFILTER > 0
    188 	/* XXX: emulate DLT_EN10MB */
    189 	if (ifp->if_bpf) {
    190 		struct mbuf mb;
    191 
    192 		mb.m_next = m0;
    193 		mb.m_len = 14;
    194 		mb.m_data = mb.m_dat;
    195 		((u_int32_t *)mb.m_data)[0] = 0;
    196 		((u_int32_t *)mb.m_data)[1] = 0;
    197 		((u_int32_t *)mb.m_data)[2] = 0;
    198 		((u_int16_t *)mb.m_data)[6] = etype;
    199 		bpf_mtap(ifp->if_bpf, &mb);
    200 	}
    201 #endif
    202 	myaddr = (struct ieee1394_hwaddr *)LLADDR(ifp->if_sadl);
    203 	if ((ifp->if_flags & IFF_SIMPLEX) &&
    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 (m0->m_flags & (M_BCAST | M_MCAST)) {
    214 		hdrlen = IEEE1394_GASP_LEN;
    215 		hwdst.iha_speed = 0;	/* XXX */
    216 	} else
    217 		hdrlen = 0;
    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 	s = splnet();
    232 	ifp->if_obytes += m0->m_pkthdr.len;
    233 	if (m0->m_flags & M_MCAST)
    234 		ifp->if_omcasts++;
    235 	while ((m = m0) != NULL) {
    236 		m0 = m->m_nextpkt;
    237 		M_PREPEND(m, sizeof(struct ieee1394_header), M_DONTWAIT);
    238 		if (m == NULL) {
    239 			splx(s);
    240 			senderr(ENOBUFS);
    241 		}
    242 		memcpy(mtod(m, caddr_t), &hwdst, sizeof(hwdst));
    243 		IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    244 		if (error) {
    245 			/* mbuf is already freed */
    246 			splx(s);
    247 			goto bad;
    248 		}
    249 	}
    250 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    251 		(*ifp->if_start)(ifp);
    252 	splx(s);
    253 	return 0;
    254 
    255   bad:
    256 	while (m0 != NULL) {
    257 		m = m0->m_nextpkt;
    258 		m_freem(m0);
    259 		m0 = m;
    260 	}
    261 
    262 	return error;
    263 }
    264 
    265 struct mbuf *
    266 ieee1394_fragment(struct ifnet *ifp, struct mbuf *m0, int maxsize,
    267     u_int16_t etype)
    268 {
    269 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    270 	int totlen, fraglen, off;
    271 	struct mbuf *m, **mp;
    272 	struct ieee1394_fraghdr *ifh;
    273 	struct ieee1394_unfraghdr *iuh;
    274 
    275 	totlen = m0->m_pkthdr.len;
    276 	if (totlen + sizeof(struct ieee1394_unfraghdr) <= maxsize) {
    277 		M_PREPEND(m0, sizeof(struct ieee1394_unfraghdr), M_DONTWAIT);
    278 		if (m0 == NULL)
    279 			goto bad;
    280 		iuh = mtod(m0, struct ieee1394_unfraghdr *);
    281 		iuh->iuh_ft = 0;
    282 		iuh->iuh_etype = etype;
    283 		return m0;
    284 	}
    285 
    286 	fraglen = maxsize - sizeof(struct ieee1394_fraghdr);
    287 
    288 	M_PREPEND(m0, sizeof(struct ieee1394_fraghdr), M_DONTWAIT);
    289 	if (m0 == NULL)
    290 		goto bad;
    291 	ifh = mtod(m0, struct ieee1394_fraghdr *);
    292 	ifh->ifh_ft_size = htons(IEEE1394_FT_MORE | (totlen - 1));
    293 	ifh->ifh_etype_off = etype;
    294 	ifh->ifh_dgl = htons(ic->ic_dgl);
    295 	ifh->ifh_reserved = 0;
    296 	off = fraglen;
    297 	mp = &m0->m_nextpkt;
    298 	while (off < totlen) {
    299 		if (off + fraglen > totlen)
    300 			fraglen = totlen - off;
    301 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    302 		if (m == NULL)
    303 			goto bad;
    304 		m->m_flags |= m0->m_flags & (M_BCAST|M_MCAST);	/* copy bcast */
    305 		MH_ALIGN(m, sizeof(struct ieee1394_fraghdr));
    306 		m->m_len = sizeof(struct ieee1394_fraghdr);
    307 		ifh = mtod(m, struct ieee1394_fraghdr *);
    308 		ifh->ifh_ft_size =
    309 		    htons(IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE | (totlen - 1));
    310 		ifh->ifh_etype_off = htons(off);
    311 		ifh->ifh_dgl = htons(ic->ic_dgl);
    312 		ifh->ifh_reserved = 0;
    313 		m->m_next = m_copy(m0, sizeof(*ifh) + off, fraglen);
    314 		if (m->m_next == NULL)
    315 			goto bad;
    316 		m->m_pkthdr.len = sizeof(*ifh) + fraglen;
    317 		off += fraglen;
    318 		*mp = m;
    319 		mp = &m->m_nextpkt;
    320 	}
    321 	ifh->ifh_ft_size &= ~htons(IEEE1394_FT_MORE);	/* last fragment */
    322 	m_adj(m0, -(m0->m_pkthdr.len - maxsize));
    323 
    324 	ic->ic_dgl++;
    325 	return m0;
    326 
    327   bad:
    328 	while ((m = m0) != NULL) {
    329 		m0 = m->m_nextpkt;
    330 		m->m_nextpkt = NULL;
    331 		m_freem(m);
    332 	}
    333 	return NULL;
    334 }
    335 
    336 static void
    337 ieee1394_input(struct ifnet *ifp, struct mbuf *m)
    338 {
    339 	struct ifqueue *inq;
    340 	u_int16_t etype;
    341 	int s;
    342 	struct ieee1394_header *ih;
    343 	struct ieee1394_unfraghdr *iuh;
    344 
    345 	if ((ifp->if_flags & IFF_UP) == 0) {
    346 		m_freem(m);
    347 		return;
    348 	}
    349 	if (m->m_len < sizeof(*ih) + sizeof(*iuh)) {
    350 		if ((m = m_pullup(m, sizeof(*ih) + sizeof(*iuh))) == NULL)
    351 			return;
    352 	}
    353 
    354 	ih = mtod(m, struct ieee1394_header *);
    355 	iuh = (struct ieee1394_unfraghdr *)&ih[1];
    356 
    357 	if (ntohs(iuh->iuh_ft) & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE)) {
    358 		if ((m = ieee1394_reass(ifp, m)) == NULL)
    359 			return;
    360 		ih = mtod(m, struct ieee1394_header *);
    361 		iuh = (struct ieee1394_unfraghdr *)&ih[1];
    362 	}
    363 	etype = ntohs(iuh->iuh_etype);
    364 
    365 	/* strip off the ieee1394 header */
    366 	m_adj(m, sizeof(*ih) + sizeof(*iuh));
    367 #if NBPFILTER > 0
    368 	/* XXX: emulate DLT_EN10MB */
    369 	if (ifp->if_bpf) {
    370 		struct mbuf mb;
    371 
    372 		mb.m_next = m;
    373 		mb.m_len = 14;
    374 		mb.m_data = mb.m_dat;
    375 		((u_int32_t *)mb.m_data)[0] = 0;
    376 		((u_int32_t *)mb.m_data)[1] = 0;
    377 		((u_int32_t *)mb.m_data)[2] = 0;
    378 		((u_int16_t *)mb.m_data)[6] = iuh->iuh_etype;
    379 		bpf_mtap(ifp->if_bpf, &mb);
    380 	}
    381 #endif
    382 
    383 	switch (etype) {
    384 #ifdef INET
    385 	case ETHERTYPE_IP:
    386 		schednetisr(NETISR_IP);
    387 		inq = &ipintrq;
    388 		break;
    389 
    390 	case ETHERTYPE_ARP:
    391 		in_ieee1394arpinput(m);
    392 		return;
    393 #endif /* INET */
    394 
    395 #ifdef INET6
    396 	case ETHERTYPE_IPV6:
    397 		schednetisr(NETISR_IPV6);
    398 		inq = &ip6intrq;
    399 		break;
    400 #endif /* INET6 */
    401 
    402 	default:
    403 		m_freem(m);
    404 		return;
    405 	}
    406 
    407 	s = splnet();
    408 	if (IF_QFULL(inq)) {
    409 		IF_DROP(inq);
    410 		m_freem(m);
    411 	} else
    412 		IF_ENQUEUE(inq, m);
    413 	splx(s);
    414 }
    415 
    416 static struct mbuf *
    417 ieee1394_reass(struct ifnet *ifp, struct mbuf *m0)
    418 {
    419 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    420 	struct ieee1394_header *ih;
    421 	struct ieee1394_fraghdr *ifh;
    422 	struct ieee1394_unfraghdr *iuh;
    423 	struct ieee1394_reassq *rq;
    424 	struct ieee1394_reass_pkt *rp, *trp, *nrp;
    425 	int len;
    426 	u_int16_t off, ftype, size, dgl;
    427 
    428 	if (m0->m_len < sizeof(*ih) + sizeof(*ifh)) {
    429 		if ((m0 = m_pullup(m0, sizeof(*ih) + sizeof(*ifh))) == NULL)
    430 			return NULL;
    431 	}
    432 	ih = mtod(m0, struct ieee1394_header *);
    433 	ifh = (struct ieee1394_fraghdr *)&ih[1];
    434 	m_adj(m0, sizeof(*ih) + sizeof(*ifh));
    435 	size = ntohs(ifh->ifh_ft_size);
    436 	ftype = size & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE);
    437 	size = (size & ~ftype) + 1;
    438 	dgl = ifh->ifh_dgl;
    439 	len = m0->m_pkthdr.len;
    440 	if (ftype & IEEE1394_FT_SUBSEQ) {
    441 		m0->m_flags &= ~M_PKTHDR;
    442 		off = ntohs(ifh->ifh_etype_off);
    443 	} else
    444 		off = 0;
    445 
    446 	for (rq = LIST_FIRST(&ic->ic_reassq); ; rq = LIST_NEXT(rq, rq_node)) {
    447 		if (rq == NULL) {
    448 			/*
    449 			 * Create a new reassemble queue head for the node.
    450 			 */
    451 			rq = malloc(sizeof(*rq), M_FTABLE, M_NOWAIT);
    452 			if (rq == NULL) {
    453 				m_freem(m0);
    454 				return NULL;
    455 			}
    456 			memcpy(rq->rq_uid, ih->ih_uid, IEEE1394_ADDR_LEN);
    457 			LIST_INIT(&rq->rq_pkt);
    458 			LIST_INSERT_HEAD(&ic->ic_reassq, rq, rq_node);
    459 			break;
    460 		}
    461 		if (memcmp(rq->rq_uid, ih->ih_uid, IEEE1394_ADDR_LEN) == 0)
    462 			break;
    463 	}
    464 	for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
    465 		nrp = LIST_NEXT(rp, rp_next);
    466 		if (rp->rp_dgl != dgl)
    467 			continue;
    468 		/*
    469 		 * sanity check:
    470 		 * datagram size must be same for all fragments, and
    471 		 * no overlap is allowed.
    472 		 */
    473 		if (rp->rp_size != size ||
    474 		    (off < rp->rp_off + rp->rp_len && off + len > rp->rp_off)) {
    475 			/*
    476 			 * This happens probably due to wrapping dgl value.
    477 			 * Destroy all previously received fragment and
    478 			 * enqueue current fragment.
    479 			 */
    480 			for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL;
    481 			    rp = nrp) {
    482 				nrp = LIST_NEXT(rp, rp_next);
    483 				if (rp->rp_dgl == dgl) {
    484 					LIST_REMOVE(rp, rp_next);
    485 					m_freem(rp->rp_m);
    486 					free(rp, M_FTABLE);
    487 				}
    488 			}
    489 			break;
    490 		}
    491 		if (rp->rp_off + rp->rp_len == off) {
    492 			/*
    493 			 * All the subsequent fragments received in sequence
    494 			 * come here.
    495 			 * Concatinate mbuf to previous one instead of
    496 			 * allocating new reassemble queue structure,
    497 			 * and try to merge more with the subsequent fragment
    498 			 * in the queue.
    499 			 */
    500 			m_cat(rp->rp_m, m0);
    501 			rp->rp_len += len;
    502 			while (rp->rp_off + rp->rp_len < size &&
    503 			    nrp != NULL && nrp->rp_dgl == dgl &&
    504 			    nrp->rp_off == rp->rp_off + rp->rp_len) {
    505 				LIST_REMOVE(nrp, rp_next);
    506 				m_cat(rp->rp_m, nrp->rp_m);
    507 				rp->rp_len += nrp->rp_len;
    508 				free(nrp, M_FTABLE);
    509 				nrp = LIST_NEXT(rp, rp_next);
    510 			}
    511 			m0 = NULL;	/* mark merged */
    512 			break;
    513 		}
    514 		if (off + m0->m_pkthdr.len == rp->rp_off) {
    515 			m_cat(m0, rp->rp_m);
    516 			rp->rp_m = m0;
    517 			rp->rp_off = off;
    518 			rp->rp_len += len;
    519 			m0 = NULL;	/* mark merged */
    520 			break;
    521 		}
    522 		if (rp->rp_off > off) {
    523 			/* insert before rp */
    524 			nrp = rp;
    525 			break;
    526 		}
    527 		if (nrp == NULL || nrp->rp_dgl != dgl) {
    528 			/* insert after rp */
    529 			nrp = NULL;
    530 			break;
    531 		}
    532 	}
    533 	if (m0 == NULL) {
    534 		if (rp->rp_off != 0 || rp->rp_len != size)
    535 			return NULL;
    536 		/* fragment done */
    537 		LIST_REMOVE(rp, rp_next);
    538 		m0 = rp->rp_m;
    539 		m0->m_pkthdr.len = rp->rp_len;
    540 		M_PREPEND(m0, sizeof(*ih) + sizeof(*iuh), M_DONTWAIT);
    541 		if (m0 != NULL) {
    542 			ih = mtod(m0, struct ieee1394_header *);
    543 			iuh = (struct ieee1394_unfraghdr *)&ih[1];
    544 			memcpy(ih, &rp->rp_hdr, sizeof(*ih));
    545 			iuh->iuh_ft = 0;
    546 			iuh->iuh_etype = rp->rp_etype;
    547 		}
    548 		free(rp, M_FTABLE);
    549 		return m0;
    550 	}
    551 
    552 	/*
    553 	 * New fragment received.  Allocate reassemble queue structure.
    554 	 */
    555 	trp = malloc(sizeof(*trp), M_FTABLE, M_NOWAIT);
    556 	if (trp == NULL) {
    557 		m_freem(m0);
    558 		return NULL;
    559 	}
    560 	trp->rp_m = m0;
    561 	memcpy(&trp->rp_hdr, ih, sizeof(*ih));
    562 	trp->rp_size = size;
    563 	trp->rp_etype = ifh->ifh_etype_off;	 /* valid only if off==0 */
    564 	trp->rp_off = off;
    565 	trp->rp_dgl = dgl;
    566 	trp->rp_len = len;
    567 	trp->rp_ttl = IEEE1394_REASS_TIMEOUT;
    568 	if (trp->rp_ttl <= ifp->if_timer)
    569 		trp->rp_ttl = ifp->if_timer + 1;
    570 
    571 	if (rp == NULL) {
    572 		/* first fragment for the dgl */
    573 		LIST_INSERT_HEAD(&rq->rq_pkt, trp, rp_next);
    574 	} else if (nrp == NULL) {
    575 		/* no next fragment for the dgl */
    576 		LIST_INSERT_AFTER(rp, trp, rp_next);
    577 	} else {
    578 		/* there is a hole */
    579 		LIST_INSERT_BEFORE(nrp, trp, rp_next);
    580 	}
    581 	return NULL;
    582 }
    583 
    584 void
    585 ieee1394_drain(struct ifnet *ifp)
    586 {
    587 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    588 	struct ieee1394_reassq *rq;
    589 	struct ieee1394_reass_pkt *rp;
    590 
    591 	while ((rq = LIST_FIRST(&ic->ic_reassq)) != NULL) {
    592 		LIST_REMOVE(rq, rq_node);
    593 		while ((rp = LIST_FIRST(&rq->rq_pkt)) != NULL) {
    594 			LIST_REMOVE(rp, rp_next);
    595 			m_freem(rp->rp_m);
    596 			free(rp, M_FTABLE);
    597 		}
    598 		free(rq, M_FTABLE);
    599 	}
    600 }
    601 
    602 void
    603 ieee1394_watchdog(struct ifnet *ifp)
    604 {
    605 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    606 	struct ieee1394_reassq *rq;
    607 	struct ieee1394_reass_pkt *rp, *nrp;
    608 	int dec;
    609 
    610 	dec = (ifp->if_timer > 0) ? ifp->if_timer : 1;
    611 	for (rq = LIST_FIRST(&ic->ic_reassq); rq != NULL;
    612 	    rq = LIST_NEXT(rq, rq_node)) {
    613 		for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
    614 			nrp = LIST_NEXT(rp, rp_next);
    615 			if (rp->rp_ttl >= dec)
    616 				rp->rp_ttl -= dec;
    617 			else {
    618 				LIST_REMOVE(rp, rp_next);
    619 				m_freem(rp->rp_m);
    620 				free(rp, M_FTABLE);
    621 			}
    622 		}
    623 	}
    624 }
    625 
    626 const char *
    627 ieee1394_sprintf(const u_int8_t *laddr)
    628 {
    629 	static char buf[3*8];
    630 
    631 	snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
    632 	    laddr[0], laddr[1], laddr[2], laddr[3],
    633 	    laddr[4], laddr[5], laddr[6], laddr[7]);
    634 	return buf;
    635 }
    636 
    637 void
    638 ieee1394_ifattach(struct ifnet *ifp, const struct ieee1394_hwaddr *hwaddr)
    639 {
    640 	struct ieee1394_hwaddr *baddr;
    641 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    642 
    643 	ifp->if_type = IFT_IEEE1394;
    644 	ifp->if_addrlen = sizeof(struct ieee1394_hwaddr);
    645 	ifp->if_hdrlen = sizeof(struct ieee1394_header);
    646 	ifp->if_dlt = DLT_EN10MB;	/* XXX */
    647 	ifp->if_mtu = IEEE1394MTU;
    648 	ifp->if_output = ieee1394_output;
    649 	ifp->if_input = ieee1394_input;
    650 	ifp->if_drain = ieee1394_drain;
    651 	ifp->if_watchdog = ieee1394_watchdog;
    652 	ifp->if_timer = 1;
    653 	if (ifp->if_baudrate == 0)
    654 		ifp->if_baudrate = IF_Mbps(100);
    655 
    656 	if_alloc_sadl(ifp);
    657 	memcpy(LLADDR(ifp->if_sadl), hwaddr, ifp->if_addrlen);
    658 
    659 	ifp->if_broadcastaddr = malloc(ifp->if_addrlen, M_DEVBUF, M_WAITOK);
    660 	baddr = (struct ieee1394_hwaddr *)ifp->if_broadcastaddr;
    661 	memset(baddr->iha_uid, 0xff, IEEE1394_ADDR_LEN);
    662 	baddr->iha_speed = 0;	/*XXX: how to determine the speed for bcast? */
    663 	baddr->iha_maxrec = 512 << baddr->iha_speed;
    664 	memset(baddr->iha_offset, 0, sizeof(baddr->iha_offset));
    665 	LIST_INIT(&ic->ic_reassq);
    666 #if NBPFILTER > 0
    667 	bpfattach(ifp, DLT_EN10MB, 14);	/* XXX */
    668 #endif
    669 }
    670 
    671 void
    672 ieee1394_ifdetach(struct ifnet *ifp)
    673 {
    674 	ieee1394_drain(ifp);
    675 #if NBPFILTER > 0
    676 	bpfdetach(ifp);
    677 #endif
    678 	free(ifp->if_broadcastaddr, M_DEVBUF);
    679 	ifp->if_broadcastaddr = NULL;
    680 	if_free_sadl(ifp);
    681 }
    682 
    683 int
    684 ieee1394_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    685 {
    686 	struct ifreq *ifr = (struct ifreq *)data;
    687 	struct ifaddr *ifa = (struct ifaddr *)data;
    688 	int error = 0;
    689 #if __NetBSD_Version < 105080000
    690 	int fw_init(struct ifnet *);
    691 	void fw_stop(struct ifnet *, int);
    692 #endif
    693 
    694 	switch (cmd) {
    695 	case SIOCSIFADDR:
    696 		ifp->if_flags |= IFF_UP;
    697 		switch (ifa->ifa_addr->sa_family) {
    698 #ifdef INET
    699 		case AF_INET:
    700 #if __NetBSD_Version >= 105080000
    701 			if ((error = (*ifp->if_init)(ifp)) != 0)
    702 #else
    703 			if ((error = fw_init(ifp)) != 0)
    704 #endif
    705 				break;
    706 			ieee1394arp_ifinit(ifp, ifa);
    707 			break;
    708 #endif /* INET */
    709 		default:
    710 #if __NetBSD_Version >= 105080000
    711 			error = (*ifp->if_init)(ifp);
    712 #else
    713 			error = fw_init(ifp);
    714 #endif
    715 			break;
    716 		}
    717 		break;
    718 
    719 	case SIOCGIFADDR:
    720 		memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
    721 		    LLADDR(ifp->if_sadl), IEEE1394_ADDR_LEN);
    722 		    break;
    723 
    724 	case SIOCSIFMTU:
    725 		if (ifr->ifr_mtu > IEEE1394MTU)
    726 			error = EINVAL;
    727 		else
    728 			ifp->if_mtu = ifr->ifr_mtu;
    729 		break;
    730 
    731 	case SIOCSIFFLAGS:
    732 #if __NetBSD_Version >= 105080000
    733 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING)
    734 			(*ifp->if_stop)(ifp, 1);
    735 		else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP)
    736 			error = (*ifp->if_init)(ifp);
    737 		else if ((ifp->if_flags & IFF_UP) != 0)
    738 			error = (*ifp->if_init)(ifp);
    739 #else
    740 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING)
    741 			fw_stop(ifp, 1);
    742 		else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP)
    743 			error = fw_init(ifp);
    744 		else if ((ifp->if_flags & IFF_UP) != 0)
    745 			error = fw_init(ifp);
    746 #endif
    747 		break;
    748 
    749 	case SIOCADDMULTI:
    750 	case SIOCDELMULTI:
    751 		/* nothing to do */
    752 		break;
    753 
    754 	default:
    755 		error = ENOTTY;
    756 		break;
    757 	}
    758 
    759 	return error;
    760 }
    761