Home | History | Annotate | Line # | Download | only in net
if_ieee1394subr.c revision 1.20
      1 /*	$NetBSD: if_ieee1394subr.c,v 1.20 2003/05/01 07:41:59 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.20 2003/05/01 07:41:59 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_inarp.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 	ALTQ_DECL(struct altq_pktattr pktattr;)
     97 #ifdef INET
     98 	struct arphdr *ah;
     99 #endif /* INET */
    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, m0, 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 (!arpresolve(ifp, rt, m0, dst, (u_char *)&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 arphdr *);
    158 		memcpy(&hwdst, ifp->if_broadcastaddr, sizeof(hwdst));
    159 		ah->ar_hrd = htons(ARPHRD_IEEE1394);
    160 		etype = htons(ETHERTYPE_ARP);
    161 		break;
    162 #endif /* INET */
    163 #ifdef INET6
    164 	case AF_INET6:
    165 		if (m0->m_flags & M_MCAST)
    166 			memcpy(&hwdst, ifp->if_broadcastaddr, sizeof(hwdst));
    167 		else if (!nd6_storelladdr(ifp, rt, m0, dst, (u_char *)&hwdst)) {
    168 			/* something bad happened */
    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 		M_COPY_PKTHDR(&mb, m0);
    193 		mb.m_next = m0;
    194 		mb.m_len = 14;
    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 		mb.m_pkthdr.len += mb.m_len;
    200 		bpf_mtap(ifp->if_bpf, &mb);
    201 	}
    202 #endif
    203 	myaddr = (struct ieee1394_hwaddr *)LLADDR(ifp->if_sadl);
    204 	if ((ifp->if_flags & IFF_SIMPLEX) &&
    205 	    memcmp(&hwdst, myaddr, IEEE1394_ADDR_LEN) == 0)
    206 		return looutput(ifp, m0, dst, rt);
    207 
    208 	/*
    209 	 * XXX:
    210 	 * The maximum possible rate depends on the topology.
    211 	 * So the determination of maxrec and fragmentation should be
    212 	 * called from the driver after probing the topology map.
    213 	 */
    214 	if (m0->m_flags & (M_BCAST | M_MCAST)) {
    215 		hdrlen = IEEE1394_GASP_LEN;
    216 		hwdst.iha_speed = 0;	/* XXX */
    217 	} else
    218 		hdrlen = 0;
    219 	if (hwdst.iha_speed > myaddr->iha_speed)
    220 		hwdst.iha_speed = myaddr->iha_speed;
    221 	if (hwdst.iha_maxrec > myaddr->iha_maxrec)
    222 		hwdst.iha_maxrec = myaddr->iha_maxrec;
    223 	if (hwdst.iha_maxrec > (8 + hwdst.iha_speed))
    224 		hwdst.iha_maxrec = 8 + hwdst.iha_speed;
    225 	if (hwdst.iha_maxrec < 8)
    226 		hwdst.iha_maxrec = 8;
    227 
    228 	m0 = ieee1394_fragment(ifp, m0, (2<<hwdst.iha_maxrec) - hdrlen, etype);
    229 	if (m0 == NULL)
    230 		senderr(ENOBUFS);
    231 
    232 	s = splnet();
    233 	ifp->if_obytes += m0->m_pkthdr.len;
    234 	if (m0->m_flags & M_MCAST)
    235 		ifp->if_omcasts++;
    236 	while ((m = m0) != NULL) {
    237 		m0 = m->m_nextpkt;
    238 		M_PREPEND(m, sizeof(struct ieee1394_header), M_DONTWAIT);
    239 		if (m == NULL) {
    240 			splx(s);
    241 			senderr(ENOBUFS);
    242 		}
    243 		memcpy(mtod(m, caddr_t), &hwdst, sizeof(hwdst));
    244 		IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    245 		if (error) {
    246 			/* mbuf is already freed */
    247 			splx(s);
    248 			goto bad;
    249 		}
    250 	}
    251 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    252 		(*ifp->if_start)(ifp);
    253 	splx(s);
    254 	return 0;
    255 
    256   bad:
    257 	while (m0 != NULL) {
    258 		m = m0->m_nextpkt;
    259 		m_freem(m0);
    260 		m0 = m;
    261 	}
    262 
    263 	return error;
    264 }
    265 
    266 struct mbuf *
    267 ieee1394_fragment(struct ifnet *ifp, struct mbuf *m0, int maxsize,
    268     u_int16_t etype)
    269 {
    270 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    271 	int totlen, fraglen, off;
    272 	struct mbuf *m, **mp;
    273 	struct ieee1394_fraghdr *ifh;
    274 	struct ieee1394_unfraghdr *iuh;
    275 
    276 	totlen = m0->m_pkthdr.len;
    277 	if (totlen + sizeof(struct ieee1394_unfraghdr) <= maxsize) {
    278 		M_PREPEND(m0, sizeof(struct ieee1394_unfraghdr), M_DONTWAIT);
    279 		if (m0 == NULL)
    280 			goto bad;
    281 		iuh = mtod(m0, struct ieee1394_unfraghdr *);
    282 		iuh->iuh_ft = 0;
    283 		iuh->iuh_etype = etype;
    284 		return m0;
    285 	}
    286 
    287 	fraglen = maxsize - sizeof(struct ieee1394_fraghdr);
    288 
    289 	M_PREPEND(m0, sizeof(struct ieee1394_fraghdr), M_DONTWAIT);
    290 	if (m0 == NULL)
    291 		goto bad;
    292 	ifh = mtod(m0, struct ieee1394_fraghdr *);
    293 	ifh->ifh_ft_size = htons(IEEE1394_FT_MORE | (totlen - 1));
    294 	ifh->ifh_etype_off = etype;
    295 	ifh->ifh_dgl = htons(ic->ic_dgl);
    296 	ifh->ifh_reserved = 0;
    297 	off = fraglen;
    298 	mp = &m0->m_nextpkt;
    299 	while (off < totlen) {
    300 		if (off + fraglen > totlen)
    301 			fraglen = totlen - off;
    302 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    303 		if (m == NULL)
    304 			goto bad;
    305 		m->m_flags |= m0->m_flags & (M_BCAST|M_MCAST);	/* copy bcast */
    306 		MH_ALIGN(m, sizeof(struct ieee1394_fraghdr));
    307 		m->m_len = sizeof(struct ieee1394_fraghdr);
    308 		ifh = mtod(m, struct ieee1394_fraghdr *);
    309 		ifh->ifh_ft_size =
    310 		    htons(IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE | (totlen - 1));
    311 		ifh->ifh_etype_off = htons(off);
    312 		ifh->ifh_dgl = htons(ic->ic_dgl);
    313 		ifh->ifh_reserved = 0;
    314 		m->m_next = m_copy(m0, sizeof(*ifh) + off, fraglen);
    315 		if (m->m_next == NULL)
    316 			goto bad;
    317 		m->m_pkthdr.len = sizeof(*ifh) + fraglen;
    318 		off += fraglen;
    319 		*mp = m;
    320 		mp = &m->m_nextpkt;
    321 	}
    322 	ifh->ifh_ft_size &= ~htons(IEEE1394_FT_MORE);	/* last fragment */
    323 	m_adj(m0, -(m0->m_pkthdr.len - maxsize));
    324 
    325 	ic->ic_dgl++;
    326 	return m0;
    327 
    328   bad:
    329 	while ((m = m0) != NULL) {
    330 		m0 = m->m_nextpkt;
    331 		m->m_nextpkt = NULL;
    332 		m_freem(m);
    333 	}
    334 	return NULL;
    335 }
    336 
    337 static void
    338 ieee1394_input(struct ifnet *ifp, struct mbuf *m)
    339 {
    340 	struct ifqueue *inq;
    341 	u_int16_t etype;
    342 	int s;
    343 	struct ieee1394_header *ih;
    344 	struct ieee1394_unfraghdr *iuh;
    345 
    346 	if ((ifp->if_flags & IFF_UP) == 0) {
    347 		m_freem(m);
    348 		return;
    349 	}
    350 	if (m->m_len < sizeof(*ih) + sizeof(*iuh)) {
    351 		if ((m = m_pullup(m, sizeof(*ih) + sizeof(*iuh))) == NULL)
    352 			return;
    353 	}
    354 
    355 	ih = mtod(m, struct ieee1394_header *);
    356 	iuh = (struct ieee1394_unfraghdr *)&ih[1];
    357 
    358 	if (ntohs(iuh->iuh_ft) & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE)) {
    359 		if ((m = ieee1394_reass(ifp, m)) == NULL)
    360 			return;
    361 		ih = mtod(m, struct ieee1394_header *);
    362 		iuh = (struct ieee1394_unfraghdr *)&ih[1];
    363 	}
    364 	etype = ntohs(iuh->iuh_etype);
    365 
    366 	/* strip off the ieee1394 header */
    367 	m_adj(m, sizeof(*ih) + sizeof(*iuh));
    368 #if NBPFILTER > 0
    369 	/* XXX: emulate DLT_EN10MB */
    370 	if (ifp->if_bpf) {
    371 		struct mbuf mb;
    372 
    373 		M_COPY_PKTHDR(&mb, m);
    374 		mb.m_next = m;
    375 		mb.m_len = 14;
    376 		((u_int32_t *)mb.m_data)[0] = 0;
    377 		((u_int32_t *)mb.m_data)[1] = 0;
    378 		((u_int32_t *)mb.m_data)[2] = 0;
    379 		((u_int16_t *)mb.m_data)[6] = iuh->iuh_etype;
    380 		mb.m_pkthdr.len += mb.m_len;
    381 		bpf_mtap(ifp->if_bpf, &mb);
    382 	}
    383 #endif
    384 
    385 	switch (etype) {
    386 #ifdef INET
    387 	case ETHERTYPE_IP:
    388 		schednetisr(NETISR_IP);
    389 		inq = &ipintrq;
    390 		break;
    391 
    392 	case ETHERTYPE_ARP:
    393 		schednetisr(NETISR_ARP);
    394 		inq = &arpintrq;
    395 		break;
    396 #endif /* INET */
    397 
    398 #ifdef INET6
    399 	case ETHERTYPE_IPV6:
    400 		schednetisr(NETISR_IPV6);
    401 		inq = &ip6intrq;
    402 		break;
    403 #endif /* INET6 */
    404 
    405 	default:
    406 		m_freem(m);
    407 		return;
    408 	}
    409 
    410 	s = splnet();
    411 	if (IF_QFULL(inq)) {
    412 		IF_DROP(inq);
    413 		m_freem(m);
    414 	} else
    415 		IF_ENQUEUE(inq, m);
    416 	splx(s);
    417 }
    418 
    419 static struct mbuf *
    420 ieee1394_reass(struct ifnet *ifp, struct mbuf *m0)
    421 {
    422 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    423 	struct ieee1394_header *ih;
    424 	struct ieee1394_fraghdr *ifh;
    425 	struct ieee1394_unfraghdr *iuh;
    426 	struct ieee1394_reassq *rq;
    427 	struct ieee1394_reass_pkt *rp, *trp, *nrp;
    428 	int len;
    429 	u_int16_t off, ftype, size, dgl;
    430 
    431 	if (m0->m_len < sizeof(*ih) + sizeof(*ifh)) {
    432 		if ((m0 = m_pullup(m0, sizeof(*ih) + sizeof(*ifh))) == NULL)
    433 			return NULL;
    434 	}
    435 	ih = mtod(m0, struct ieee1394_header *);
    436 	ifh = (struct ieee1394_fraghdr *)&ih[1];
    437 	m_adj(m0, sizeof(*ih) + sizeof(*ifh));
    438 	size = ntohs(ifh->ifh_ft_size);
    439 	ftype = size & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE);
    440 	size = (size & ~ftype) + 1;
    441 	dgl = ifh->ifh_dgl;
    442 	len = m0->m_pkthdr.len;
    443 	if (ftype & IEEE1394_FT_SUBSEQ) {
    444 		m0->m_flags &= ~M_PKTHDR;
    445 		off = ntohs(ifh->ifh_etype_off);
    446 	} else
    447 		off = 0;
    448 
    449 	for (rq = LIST_FIRST(&ic->ic_reassq); ; rq = LIST_NEXT(rq, rq_node)) {
    450 		if (rq == NULL) {
    451 			/*
    452 			 * Create a new reassemble queue head for the node.
    453 			 */
    454 			rq = malloc(sizeof(*rq), M_FTABLE, M_NOWAIT);
    455 			if (rq == NULL) {
    456 				m_freem(m0);
    457 				return NULL;
    458 			}
    459 			memcpy(rq->rq_uid, ih->ih_uid, IEEE1394_ADDR_LEN);
    460 			LIST_INIT(&rq->rq_pkt);
    461 			LIST_INSERT_HEAD(&ic->ic_reassq, rq, rq_node);
    462 			break;
    463 		}
    464 		if (memcmp(rq->rq_uid, ih->ih_uid, IEEE1394_ADDR_LEN) == 0)
    465 			break;
    466 	}
    467 	for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
    468 		nrp = LIST_NEXT(rp, rp_next);
    469 		if (rp->rp_dgl != dgl)
    470 			continue;
    471 		/*
    472 		 * sanity check:
    473 		 * datagram size must be same for all fragments, and
    474 		 * no overlap is allowed.
    475 		 */
    476 		if (rp->rp_size != size ||
    477 		    (off < rp->rp_off + rp->rp_len && off + len > rp->rp_off)) {
    478 			/*
    479 			 * This happens probably due to wrapping dgl value.
    480 			 * Destroy all previously received fragment and
    481 			 * enqueue current fragment.
    482 			 */
    483 			for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL;
    484 			    rp = nrp) {
    485 				nrp = LIST_NEXT(rp, rp_next);
    486 				if (rp->rp_dgl == dgl) {
    487 					LIST_REMOVE(rp, rp_next);
    488 					m_freem(rp->rp_m);
    489 					free(rp, M_FTABLE);
    490 				}
    491 			}
    492 			break;
    493 		}
    494 		if (rp->rp_off + rp->rp_len == off) {
    495 			/*
    496 			 * All the subsequent fragments received in sequence
    497 			 * come here.
    498 			 * Concatinate mbuf to previous one instead of
    499 			 * allocating new reassemble queue structure,
    500 			 * and try to merge more with the subsequent fragment
    501 			 * in the queue.
    502 			 */
    503 			m_cat(rp->rp_m, m0);
    504 			rp->rp_len += len;
    505 			while (rp->rp_off + rp->rp_len < size &&
    506 			    nrp != NULL && nrp->rp_dgl == dgl &&
    507 			    nrp->rp_off == rp->rp_off + rp->rp_len) {
    508 				LIST_REMOVE(nrp, rp_next);
    509 				m_cat(rp->rp_m, nrp->rp_m);
    510 				rp->rp_len += nrp->rp_len;
    511 				free(nrp, M_FTABLE);
    512 				nrp = LIST_NEXT(rp, rp_next);
    513 			}
    514 			m0 = NULL;	/* mark merged */
    515 			break;
    516 		}
    517 		if (off + m0->m_pkthdr.len == rp->rp_off) {
    518 			m_cat(m0, rp->rp_m);
    519 			rp->rp_m = m0;
    520 			rp->rp_off = off;
    521 			rp->rp_len += len;
    522 			m0 = NULL;	/* mark merged */
    523 			break;
    524 		}
    525 		if (rp->rp_off > off) {
    526 			/* insert before rp */
    527 			nrp = rp;
    528 			break;
    529 		}
    530 		if (nrp == NULL || nrp->rp_dgl != dgl) {
    531 			/* insert after rp */
    532 			nrp = NULL;
    533 			break;
    534 		}
    535 	}
    536 	if (m0 == NULL) {
    537 		if (rp->rp_off != 0 || rp->rp_len != size)
    538 			return NULL;
    539 		/* fragment done */
    540 		LIST_REMOVE(rp, rp_next);
    541 		m0 = rp->rp_m;
    542 		m0->m_pkthdr.len = rp->rp_len;
    543 		M_PREPEND(m0, sizeof(*ih) + sizeof(*iuh), M_DONTWAIT);
    544 		if (m0 != NULL) {
    545 			ih = mtod(m0, struct ieee1394_header *);
    546 			iuh = (struct ieee1394_unfraghdr *)&ih[1];
    547 			memcpy(ih, &rp->rp_hdr, sizeof(*ih));
    548 			iuh->iuh_ft = 0;
    549 			iuh->iuh_etype = rp->rp_etype;
    550 		}
    551 		free(rp, M_FTABLE);
    552 		return m0;
    553 	}
    554 
    555 	/*
    556 	 * New fragment received.  Allocate reassemble queue structure.
    557 	 */
    558 	trp = malloc(sizeof(*trp), M_FTABLE, M_NOWAIT);
    559 	if (trp == NULL) {
    560 		m_freem(m0);
    561 		return NULL;
    562 	}
    563 	trp->rp_m = m0;
    564 	memcpy(&trp->rp_hdr, ih, sizeof(*ih));
    565 	trp->rp_size = size;
    566 	trp->rp_etype = ifh->ifh_etype_off;	 /* valid only if off==0 */
    567 	trp->rp_off = off;
    568 	trp->rp_dgl = dgl;
    569 	trp->rp_len = len;
    570 	trp->rp_ttl = IEEE1394_REASS_TIMEOUT;
    571 	if (trp->rp_ttl <= ifp->if_timer)
    572 		trp->rp_ttl = ifp->if_timer + 1;
    573 
    574 	if (rp == NULL) {
    575 		/* first fragment for the dgl */
    576 		LIST_INSERT_HEAD(&rq->rq_pkt, trp, rp_next);
    577 	} else if (nrp == NULL) {
    578 		/* no next fragment for the dgl */
    579 		LIST_INSERT_AFTER(rp, trp, rp_next);
    580 	} else {
    581 		/* there is a hole */
    582 		LIST_INSERT_BEFORE(nrp, trp, rp_next);
    583 	}
    584 	return NULL;
    585 }
    586 
    587 void
    588 ieee1394_drain(struct ifnet *ifp)
    589 {
    590 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    591 	struct ieee1394_reassq *rq;
    592 	struct ieee1394_reass_pkt *rp;
    593 
    594 	while ((rq = LIST_FIRST(&ic->ic_reassq)) != NULL) {
    595 		LIST_REMOVE(rq, rq_node);
    596 		while ((rp = LIST_FIRST(&rq->rq_pkt)) != NULL) {
    597 			LIST_REMOVE(rp, rp_next);
    598 			m_freem(rp->rp_m);
    599 			free(rp, M_FTABLE);
    600 		}
    601 		free(rq, M_FTABLE);
    602 	}
    603 }
    604 
    605 void
    606 ieee1394_watchdog(struct ifnet *ifp)
    607 {
    608 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    609 	struct ieee1394_reassq *rq;
    610 	struct ieee1394_reass_pkt *rp, *nrp;
    611 	int dec;
    612 
    613 	dec = (ifp->if_timer > 0) ? ifp->if_timer : 1;
    614 	for (rq = LIST_FIRST(&ic->ic_reassq); rq != NULL;
    615 	    rq = LIST_NEXT(rq, rq_node)) {
    616 		for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
    617 			nrp = LIST_NEXT(rp, rp_next);
    618 			if (rp->rp_ttl >= dec)
    619 				rp->rp_ttl -= dec;
    620 			else {
    621 				LIST_REMOVE(rp, rp_next);
    622 				m_freem(rp->rp_m);
    623 				free(rp, M_FTABLE);
    624 			}
    625 		}
    626 	}
    627 }
    628 
    629 const char *
    630 ieee1394_sprintf(const u_int8_t *laddr)
    631 {
    632 	static char buf[3*8];
    633 
    634 	snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
    635 	    laddr[0], laddr[1], laddr[2], laddr[3],
    636 	    laddr[4], laddr[5], laddr[6], laddr[7]);
    637 	return buf;
    638 }
    639 
    640 void
    641 ieee1394_ifattach(struct ifnet *ifp, const struct ieee1394_hwaddr *hwaddr)
    642 {
    643 	struct ieee1394_hwaddr *baddr;
    644 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    645 
    646 	ifp->if_type = IFT_IEEE1394;
    647 	ifp->if_addrlen = sizeof(struct ieee1394_hwaddr);
    648 	ifp->if_hdrlen = sizeof(struct ieee1394_header);
    649 	ifp->if_dlt = DLT_EN10MB;	/* XXX */
    650 	ifp->if_mtu = IEEE1394MTU;
    651 	ifp->if_output = ieee1394_output;
    652 	ifp->if_input = ieee1394_input;
    653 	ifp->if_drain = ieee1394_drain;
    654 	ifp->if_watchdog = ieee1394_watchdog;
    655 	ifp->if_timer = 1;
    656 	if (ifp->if_baudrate == 0)
    657 		ifp->if_baudrate = IF_Mbps(100);
    658 
    659 	if_alloc_sadl(ifp);
    660 	memcpy(LLADDR(ifp->if_sadl), hwaddr, ifp->if_addrlen);
    661 
    662 	ifp->if_broadcastaddr = malloc(ifp->if_addrlen, M_DEVBUF, M_WAITOK);
    663 	baddr = (struct ieee1394_hwaddr *)ifp->if_broadcastaddr;
    664 	memset(baddr->iha_uid, 0xff, IEEE1394_ADDR_LEN);
    665 	baddr->iha_speed = 0;	/*XXX: how to determine the speed for bcast? */
    666 	baddr->iha_maxrec = 512 << baddr->iha_speed;
    667 	memset(baddr->iha_offset, 0, sizeof(baddr->iha_offset));
    668 	LIST_INIT(&ic->ic_reassq);
    669 #if NBPFILTER > 0
    670 	bpfattach(ifp, DLT_EN10MB, 14);	/* XXX */
    671 #endif
    672 }
    673 
    674 void
    675 ieee1394_ifdetach(struct ifnet *ifp)
    676 {
    677 	ieee1394_drain(ifp);
    678 #if NBPFILTER > 0
    679 	bpfdetach(ifp);
    680 #endif
    681 	free(ifp->if_broadcastaddr, M_DEVBUF);
    682 	ifp->if_broadcastaddr = NULL;
    683 	if_free_sadl(ifp);
    684 }
    685 
    686 int
    687 ieee1394_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    688 {
    689 	struct ifreq *ifr = (struct ifreq *)data;
    690 	struct ifaddr *ifa = (struct ifaddr *)data;
    691 	int error = 0;
    692 #if __NetBSD_Version__ < 105080000
    693 	int fw_init(struct ifnet *);
    694 	void fw_stop(struct ifnet *, int);
    695 #endif
    696 
    697 	switch (cmd) {
    698 	case SIOCSIFADDR:
    699 		ifp->if_flags |= IFF_UP;
    700 		switch (ifa->ifa_addr->sa_family) {
    701 #ifdef INET
    702 		case AF_INET:
    703 #if __NetBSD_Version__ >= 105080000
    704 			if ((error = (*ifp->if_init)(ifp)) != 0)
    705 #else
    706 			if ((error = fw_init(ifp)) != 0)
    707 #endif
    708 				break;
    709 			arp_ifinit(ifp, ifa);
    710 			break;
    711 #endif /* INET */
    712 		default:
    713 #if __NetBSD_Version__ >= 105080000
    714 			error = (*ifp->if_init)(ifp);
    715 #else
    716 			error = fw_init(ifp);
    717 #endif
    718 			break;
    719 		}
    720 		break;
    721 
    722 	case SIOCGIFADDR:
    723 		memcpy(((struct sockaddr *)&ifr->ifr_data)->sa_data,
    724 		    LLADDR(ifp->if_sadl), IEEE1394_ADDR_LEN);
    725 		    break;
    726 
    727 	case SIOCSIFMTU:
    728 		if (ifr->ifr_mtu > IEEE1394MTU)
    729 			error = EINVAL;
    730 		else
    731 			ifp->if_mtu = ifr->ifr_mtu;
    732 		break;
    733 
    734 	case SIOCSIFFLAGS:
    735 #if __NetBSD_Version__ >= 105080000
    736 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING)
    737 			(*ifp->if_stop)(ifp, 1);
    738 		else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP)
    739 			error = (*ifp->if_init)(ifp);
    740 		else if ((ifp->if_flags & IFF_UP) != 0)
    741 			error = (*ifp->if_init)(ifp);
    742 #else
    743 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING)
    744 			fw_stop(ifp, 1);
    745 		else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP)
    746 			error = fw_init(ifp);
    747 		else if ((ifp->if_flags & IFF_UP) != 0)
    748 			error = fw_init(ifp);
    749 #endif
    750 		break;
    751 
    752 	case SIOCADDMULTI:
    753 	case SIOCDELMULTI:
    754 		/* nothing to do */
    755 		break;
    756 
    757 	default:
    758 		error = ENOTTY;
    759 		break;
    760 	}
    761 
    762 	return error;
    763 }
    764