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