Home | History | Annotate | Line # | Download | only in net
if_ieee1394subr.c revision 1.41
      1  1.41    dyoung /*	$NetBSD: if_ieee1394subr.c,v 1.41 2008/11/07 00:20:13 dyoung 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  *
     19   1.1      onoe  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1      onoe  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1      onoe  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1      onoe  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1      onoe  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1      onoe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1      onoe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1      onoe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1      onoe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1      onoe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1      onoe  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1      onoe  */
     31  1.12     lukem 
     32  1.12     lukem #include <sys/cdefs.h>
     33  1.41    dyoung __KERNEL_RCSID(0, "$NetBSD: if_ieee1394subr.c,v 1.41 2008/11/07 00:20:13 dyoung Exp $");
     34   1.1      onoe 
     35   1.1      onoe #include "opt_inet.h"
     36   1.1      onoe #include "bpfilter.h"
     37   1.1      onoe 
     38   1.1      onoe #include <sys/param.h>
     39   1.1      onoe #include <sys/systm.h>
     40   1.1      onoe #include <sys/socket.h>
     41   1.1      onoe #include <sys/sockio.h>
     42   1.1      onoe #include <sys/kernel.h>
     43   1.1      onoe #include <sys/mbuf.h>
     44   1.1      onoe #include <sys/device.h>
     45   1.1      onoe 
     46   1.1      onoe #include <net/if.h>
     47   1.1      onoe #include <net/if_dl.h>
     48   1.1      onoe #include <net/if_ieee1394.h>
     49   1.1      onoe #include <net/if_types.h>
     50   1.1      onoe #include <net/if_media.h>
     51   1.1      onoe #include <net/ethertypes.h>
     52   1.1      onoe #include <net/netisr.h>
     53   1.1      onoe #include <net/route.h>
     54   1.1      onoe 
     55   1.1      onoe #if NBPFILTER > 0
     56   1.1      onoe #include <net/bpf.h>
     57   1.1      onoe #endif
     58   1.1      onoe 
     59   1.1      onoe #ifdef INET
     60   1.1      onoe #include <netinet/in.h>
     61   1.1      onoe #include <netinet/in_var.h>
     62  1.16    itojun #include <netinet/if_inarp.h>
     63   1.1      onoe #endif /* INET */
     64   1.1      onoe #ifdef INET6
     65   1.1      onoe #include <netinet/in.h>
     66   1.1      onoe #include <netinet6/in6_var.h>
     67   1.1      onoe #include <netinet6/nd6.h>
     68   1.1      onoe #endif /* INET6 */
     69   1.1      onoe 
     70  1.29  kiyohara #include <dev/ieee1394/fw_port.h>
     71  1.29  kiyohara #include <dev/ieee1394/firewire.h>
     72  1.29  kiyohara 
     73  1.29  kiyohara #include <dev/ieee1394/firewirereg.h>
     74  1.29  kiyohara #include <dev/ieee1394/iec13213.h>
     75  1.29  kiyohara #include <dev/ieee1394/if_fwipvar.h>
     76  1.29  kiyohara 
     77   1.3      onoe #define	IEEE1394_REASS_TIMEOUT	3	/* 3 sec */
     78   1.3      onoe 
     79   1.1      onoe #define	senderr(e)	do { error = (e); goto bad; } while(0/*CONSTCOND*/)
     80   1.1      onoe 
     81  1.33    dyoung static int  ieee1394_output(struct ifnet *, struct mbuf *,
     82  1.33    dyoung 		const struct sockaddr *, struct rtentry *);
     83  1.39      matt static struct mbuf *ieee1394_reass(struct ifnet *, struct mbuf *, uint16_t);
     84   1.1      onoe 
     85   1.1      onoe static int
     86  1.33    dyoung ieee1394_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
     87   1.1      onoe     struct rtentry *rt0)
     88   1.1      onoe {
     89  1.39      matt 	uint16_t etype = 0;
     90   1.2      onoe 	struct mbuf *m;
     91   1.2      onoe 	int s, hdrlen, error = 0;
     92   1.1      onoe 	struct rtentry *rt;
     93   1.1      onoe 	struct mbuf *mcopy = NULL;
     94  1.36    dyoung 	struct ieee1394_hwaddr *hwdst, baddr;
     95  1.36    dyoung 	const struct ieee1394_hwaddr *myaddr;
     96  1.14    itojun 	ALTQ_DECL(struct altq_pktattr pktattr;)
     97  1.18      onoe #ifdef INET
     98  1.18      onoe 	struct arphdr *ah;
     99  1.18      onoe #endif /* INET */
    100  1.29  kiyohara 	struct m_tag *mtag;
    101  1.29  kiyohara 	int unicast;
    102   1.1      onoe 
    103   1.1      onoe 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
    104   1.1      onoe 		senderr(ENETDOWN);
    105   1.1      onoe 	if ((rt = rt0) != NULL) {
    106   1.1      onoe 		if ((rt->rt_flags & RTF_UP) == 0) {
    107   1.1      onoe 			if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
    108   1.1      onoe 				rt->rt_refcnt--;
    109   1.1      onoe 				if (rt->rt_ifp != ifp)
    110   1.1      onoe 					return (*rt->rt_ifp->if_output)
    111   1.1      onoe 							(ifp, m0, dst, rt);
    112   1.1      onoe 			} else
    113   1.1      onoe 				senderr(EHOSTUNREACH);
    114   1.1      onoe 		}
    115   1.1      onoe 		if (rt->rt_flags & RTF_GATEWAY) {
    116   1.3      onoe 			if (rt->rt_gwroute == NULL)
    117   1.1      onoe 				goto lookup;
    118   1.1      onoe 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
    119   1.1      onoe 				rtfree(rt);
    120   1.1      onoe 				rt = rt0;
    121   1.1      onoe   lookup:
    122   1.1      onoe 				rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
    123   1.3      onoe 				if ((rt = rt->rt_gwroute) == NULL)
    124   1.1      onoe 					senderr(EHOSTUNREACH);
    125   1.1      onoe 				/* the "G" test below also prevents rt == rt0 */
    126   1.1      onoe 				if ((rt->rt_flags & RTF_GATEWAY) ||
    127   1.1      onoe 				    (rt->rt_ifp != ifp)) {
    128   1.1      onoe 					rt->rt_refcnt--;
    129   1.3      onoe 					rt0->rt_gwroute = NULL;
    130   1.1      onoe 					senderr(EHOSTUNREACH);
    131   1.1      onoe 				}
    132   1.1      onoe 			}
    133   1.1      onoe 		}
    134   1.1      onoe 		if (rt->rt_flags & RTF_REJECT)
    135   1.1      onoe 			if (rt->rt_rmx.rmx_expire == 0 ||
    136  1.32    kardel 			    time_second < rt->rt_rmx.rmx_expire)
    137   1.1      onoe 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
    138   1.1      onoe 	}
    139  1.14    itojun 
    140  1.14    itojun 	/*
    141  1.14    itojun 	 * If the queueing discipline needs packet classification,
    142  1.14    itojun 	 * do it before prepending link headers.
    143  1.14    itojun 	 */
    144  1.18      onoe 	IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
    145  1.14    itojun 
    146  1.29  kiyohara 	/*
    147  1.29  kiyohara 	 * For unicast, we make a tag to store the lladdr of the
    148  1.29  kiyohara 	 * destination. This might not be the first time we have seen
    149  1.29  kiyohara 	 * the packet (for instance, the arp code might be trying to
    150  1.29  kiyohara 	 * re-send it after receiving an arp reply) so we only
    151  1.29  kiyohara 	 * allocate a tag if there isn't one there already. For
    152  1.29  kiyohara 	 * multicast, we will eventually use a different tag to store
    153  1.29  kiyohara 	 * the channel number.
    154  1.29  kiyohara 	 */
    155  1.29  kiyohara 	unicast = !(m0->m_flags & (M_BCAST | M_MCAST));
    156  1.29  kiyohara 	if (unicast) {
    157  1.29  kiyohara 		mtag =
    158  1.29  kiyohara 		    m_tag_locate(m0, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, NULL);
    159  1.29  kiyohara 		if (!mtag) {
    160  1.29  kiyohara 			mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR,
    161  1.29  kiyohara 			    sizeof (struct ieee1394_hwaddr), M_NOWAIT);
    162  1.29  kiyohara 			if (!mtag) {
    163  1.29  kiyohara 				error = ENOMEM;
    164  1.29  kiyohara 				goto bad;
    165  1.29  kiyohara 			}
    166  1.29  kiyohara 			m_tag_prepend(m0, mtag);
    167  1.29  kiyohara 		}
    168  1.29  kiyohara 		hwdst = (struct ieee1394_hwaddr *)(mtag + 1);
    169  1.29  kiyohara 	} else {
    170  1.29  kiyohara 		hwdst = &baddr;
    171  1.29  kiyohara 	}
    172  1.29  kiyohara 
    173   1.1      onoe 	switch (dst->sa_family) {
    174   1.1      onoe #ifdef INET
    175   1.1      onoe 	case AF_INET:
    176  1.29  kiyohara 		if (unicast && (!arpresolve(ifp, rt, m0, dst, (u_char *)hwdst)))
    177   1.1      onoe 			return 0;	/* if not yet resolved */
    178   1.1      onoe 		/* if broadcasting on a simplex interface, loopback a copy */
    179   1.2      onoe 		if ((m0->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    180   1.2      onoe 			mcopy = m_copy(m0, 0, M_COPYALL);
    181   1.1      onoe 		etype = htons(ETHERTYPE_IP);
    182   1.1      onoe 		break;
    183   1.1      onoe 	case AF_ARP:
    184  1.18      onoe 		ah = mtod(m0, struct arphdr *);
    185  1.18      onoe 		ah->ar_hrd = htons(ARPHRD_IEEE1394);
    186   1.1      onoe 		etype = htons(ETHERTYPE_ARP);
    187   1.1      onoe 		break;
    188   1.1      onoe #endif /* INET */
    189   1.1      onoe #ifdef INET6
    190   1.1      onoe 	case AF_INET6:
    191  1.33    dyoung 		if (unicast && (!nd6_storelladdr(ifp, rt, m0, dst,
    192  1.33    dyoung 		    hwdst->iha_uid, IEEE1394_ADDR_LEN))) {
    193  1.20    itojun 			/* something bad happened */
    194   1.1      onoe 			return 0;
    195   1.1      onoe 		}
    196   1.1      onoe 		etype = htons(ETHERTYPE_IPV6);
    197   1.1      onoe 		break;
    198   1.1      onoe #endif /* INET6 */
    199   1.1      onoe 
    200   1.1      onoe 	case pseudo_AF_HDRCMPLT:
    201   1.1      onoe 	case AF_UNSPEC:
    202   1.1      onoe 		/* TODO? */
    203   1.1      onoe 	default:
    204   1.1      onoe 		printf("%s: can't handle af%d\n", ifp->if_xname,
    205   1.1      onoe 		    dst->sa_family);
    206   1.1      onoe 		senderr(EAFNOSUPPORT);
    207   1.1      onoe 		break;
    208   1.1      onoe 	}
    209   1.1      onoe 
    210   1.1      onoe 	if (mcopy)
    211   1.1      onoe 		looutput(ifp, mcopy, dst, rt);
    212  1.36    dyoung 	myaddr = (const struct ieee1394_hwaddr *)CLLADDR(ifp->if_sadl);
    213   1.1      onoe #if NBPFILTER > 0
    214  1.30  kiyohara 	if (ifp->if_bpf) {
    215  1.30  kiyohara 		struct ieee1394_bpfhdr h;
    216  1.30  kiyohara 		if (unicast)
    217  1.30  kiyohara 			memcpy(h.ibh_dhost, hwdst->iha_uid, 8);
    218  1.30  kiyohara 		else
    219  1.30  kiyohara 			memcpy(h.ibh_dhost,
    220  1.30  kiyohara 			    ((const struct ieee1394_hwaddr *)
    221  1.30  kiyohara 			    ifp->if_broadcastaddr)->iha_uid, 8);
    222  1.30  kiyohara 		memcpy(h.ibh_shost, myaddr->iha_uid, 8);
    223  1.30  kiyohara 		h.ibh_type = etype;
    224  1.30  kiyohara 		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m0);
    225  1.30  kiyohara 	}
    226   1.1      onoe #endif
    227   1.2      onoe 	if ((ifp->if_flags & IFF_SIMPLEX) &&
    228  1.29  kiyohara 	    unicast &&
    229  1.29  kiyohara 	    memcmp(hwdst, myaddr, IEEE1394_ADDR_LEN) == 0)
    230   1.2      onoe 		return looutput(ifp, m0, dst, rt);
    231   1.2      onoe 
    232   1.3      onoe 	/*
    233   1.3      onoe 	 * XXX:
    234   1.3      onoe 	 * The maximum possible rate depends on the topology.
    235   1.3      onoe 	 * So the determination of maxrec and fragmentation should be
    236   1.3      onoe 	 * called from the driver after probing the topology map.
    237   1.3      onoe 	 */
    238  1.29  kiyohara 	if (unicast) {
    239   1.2      onoe 		hdrlen = IEEE1394_GASP_LEN;
    240  1.29  kiyohara 		hwdst->iha_speed = 0;	/* XXX */
    241   1.2      onoe 	} else
    242   1.2      onoe 		hdrlen = 0;
    243   1.1      onoe 
    244  1.29  kiyohara 	if (hwdst->iha_speed > myaddr->iha_speed)
    245  1.29  kiyohara 		hwdst->iha_speed = myaddr->iha_speed;
    246  1.29  kiyohara 	if (hwdst->iha_maxrec > myaddr->iha_maxrec)
    247  1.29  kiyohara 		hwdst->iha_maxrec = myaddr->iha_maxrec;
    248  1.29  kiyohara 	if (hwdst->iha_maxrec > (8 + hwdst->iha_speed))
    249  1.29  kiyohara 		hwdst->iha_maxrec = 8 + hwdst->iha_speed;
    250  1.29  kiyohara 	if (hwdst->iha_maxrec < 8)
    251  1.29  kiyohara 			hwdst->iha_maxrec = 8;
    252  1.29  kiyohara 
    253  1.29  kiyohara 	m0 = ieee1394_fragment(ifp, m0, (2<<hwdst->iha_maxrec) - hdrlen, etype);
    254   1.2      onoe 	if (m0 == NULL)
    255   1.2      onoe 		senderr(ENOBUFS);
    256   1.3      onoe 
    257  1.10   thorpej 	s = splnet();
    258   1.2      onoe 	ifp->if_obytes += m0->m_pkthdr.len;
    259   1.1      onoe 	if (m0->m_flags & M_MCAST)
    260   1.1      onoe 		ifp->if_omcasts++;
    261   1.2      onoe 	while ((m = m0) != NULL) {
    262   1.2      onoe 		m0 = m->m_nextpkt;
    263   1.2      onoe 		if (m == NULL) {
    264   1.2      onoe 			splx(s);
    265   1.2      onoe 			senderr(ENOBUFS);
    266   1.2      onoe 		}
    267  1.14    itojun 		IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    268  1.14    itojun 		if (error) {
    269  1.14    itojun 			/* mbuf is already freed */
    270  1.14    itojun 			splx(s);
    271  1.14    itojun 			goto bad;
    272  1.14    itojun 		}
    273   1.2      onoe 	}
    274   1.1      onoe 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    275   1.1      onoe 		(*ifp->if_start)(ifp);
    276   1.1      onoe 	splx(s);
    277   1.2      onoe 	return 0;
    278   1.1      onoe 
    279   1.1      onoe   bad:
    280   1.1      onoe 	while (m0 != NULL) {
    281   1.1      onoe 		m = m0->m_nextpkt;
    282   1.1      onoe 		m_freem(m0);
    283   1.1      onoe 		m0 = m;
    284   1.1      onoe 	}
    285   1.1      onoe 
    286   1.1      onoe 	return error;
    287   1.1      onoe }
    288   1.1      onoe 
    289   1.2      onoe struct mbuf *
    290   1.2      onoe ieee1394_fragment(struct ifnet *ifp, struct mbuf *m0, int maxsize,
    291  1.39      matt     uint16_t etype)
    292   1.2      onoe {
    293   1.2      onoe 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    294   1.2      onoe 	int totlen, fraglen, off;
    295   1.2      onoe 	struct mbuf *m, **mp;
    296   1.2      onoe 	struct ieee1394_fraghdr *ifh;
    297   1.2      onoe 	struct ieee1394_unfraghdr *iuh;
    298   1.2      onoe 
    299   1.2      onoe 	totlen = m0->m_pkthdr.len;
    300   1.2      onoe 	if (totlen + sizeof(struct ieee1394_unfraghdr) <= maxsize) {
    301   1.2      onoe 		M_PREPEND(m0, sizeof(struct ieee1394_unfraghdr), M_DONTWAIT);
    302   1.2      onoe 		if (m0 == NULL)
    303   1.2      onoe 			goto bad;
    304   1.2      onoe 		iuh = mtod(m0, struct ieee1394_unfraghdr *);
    305   1.2      onoe 		iuh->iuh_ft = 0;
    306   1.2      onoe 		iuh->iuh_etype = etype;
    307   1.2      onoe 		return m0;
    308   1.2      onoe 	}
    309   1.2      onoe 
    310   1.2      onoe 	fraglen = maxsize - sizeof(struct ieee1394_fraghdr);
    311   1.2      onoe 
    312   1.2      onoe 	M_PREPEND(m0, sizeof(struct ieee1394_fraghdr), M_DONTWAIT);
    313   1.2      onoe 	if (m0 == NULL)
    314   1.2      onoe 		goto bad;
    315   1.2      onoe 	ifh = mtod(m0, struct ieee1394_fraghdr *);
    316   1.2      onoe 	ifh->ifh_ft_size = htons(IEEE1394_FT_MORE | (totlen - 1));
    317   1.2      onoe 	ifh->ifh_etype_off = etype;
    318   1.2      onoe 	ifh->ifh_dgl = htons(ic->ic_dgl);
    319   1.2      onoe 	ifh->ifh_reserved = 0;
    320   1.2      onoe 	off = fraglen;
    321   1.2      onoe 	mp = &m0->m_nextpkt;
    322   1.2      onoe 	while (off < totlen) {
    323   1.2      onoe 		if (off + fraglen > totlen)
    324   1.2      onoe 			fraglen = totlen - off;
    325   1.2      onoe 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    326   1.2      onoe 		if (m == NULL)
    327   1.2      onoe 			goto bad;
    328   1.2      onoe 		m->m_flags |= m0->m_flags & (M_BCAST|M_MCAST);	/* copy bcast */
    329   1.2      onoe 		MH_ALIGN(m, sizeof(struct ieee1394_fraghdr));
    330   1.2      onoe 		m->m_len = sizeof(struct ieee1394_fraghdr);
    331   1.2      onoe 		ifh = mtod(m, struct ieee1394_fraghdr *);
    332   1.2      onoe 		ifh->ifh_ft_size =
    333   1.2      onoe 		    htons(IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE | (totlen - 1));
    334   1.2      onoe 		ifh->ifh_etype_off = htons(off);
    335   1.2      onoe 		ifh->ifh_dgl = htons(ic->ic_dgl);
    336   1.2      onoe 		ifh->ifh_reserved = 0;
    337   1.3      onoe 		m->m_next = m_copy(m0, sizeof(*ifh) + off, fraglen);
    338   1.2      onoe 		if (m->m_next == NULL)
    339   1.2      onoe 			goto bad;
    340   1.3      onoe 		m->m_pkthdr.len = sizeof(*ifh) + fraglen;
    341   1.2      onoe 		off += fraglen;
    342   1.2      onoe 		*mp = m;
    343   1.2      onoe 		mp = &m->m_nextpkt;
    344   1.2      onoe 	}
    345   1.2      onoe 	ifh->ifh_ft_size &= ~htons(IEEE1394_FT_MORE);	/* last fragment */
    346   1.2      onoe 	m_adj(m0, -(m0->m_pkthdr.len - maxsize));
    347   1.2      onoe 
    348   1.2      onoe 	ic->ic_dgl++;
    349   1.2      onoe 	return m0;
    350   1.2      onoe 
    351   1.2      onoe   bad:
    352   1.2      onoe 	while ((m = m0) != NULL) {
    353   1.2      onoe 		m0 = m->m_nextpkt;
    354   1.2      onoe 		m->m_nextpkt = NULL;
    355   1.2      onoe 		m_freem(m);
    356   1.2      onoe 	}
    357   1.2      onoe 	return NULL;
    358   1.2      onoe }
    359   1.2      onoe 
    360  1.29  kiyohara void
    361  1.39      matt ieee1394_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
    362   1.1      onoe {
    363   1.1      onoe 	struct ifqueue *inq;
    364  1.39      matt 	uint16_t etype;
    365   1.1      onoe 	int s;
    366   1.1      onoe 	struct ieee1394_unfraghdr *iuh;
    367   1.1      onoe 
    368   1.1      onoe 	if ((ifp->if_flags & IFF_UP) == 0) {
    369   1.1      onoe 		m_freem(m);
    370   1.1      onoe 		return;
    371   1.1      onoe 	}
    372  1.29  kiyohara 	if (m->m_len < sizeof(*iuh)) {
    373  1.29  kiyohara 		if ((m = m_pullup(m, sizeof(*iuh))) == NULL)
    374   1.2      onoe 			return;
    375   1.2      onoe 	}
    376   1.1      onoe 
    377  1.29  kiyohara 	iuh = mtod(m, struct ieee1394_unfraghdr *);
    378   1.2      onoe 
    379   1.2      onoe 	if (ntohs(iuh->iuh_ft) & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE)) {
    380  1.29  kiyohara 		if ((m = ieee1394_reass(ifp, m, src)) == NULL)
    381   1.2      onoe 			return;
    382  1.29  kiyohara 		iuh = mtod(m, struct ieee1394_unfraghdr *);
    383   1.1      onoe 	}
    384   1.1      onoe 	etype = ntohs(iuh->iuh_etype);
    385   1.1      onoe 
    386   1.1      onoe 	/* strip off the ieee1394 header */
    387  1.29  kiyohara 	m_adj(m, sizeof(*iuh));
    388   1.1      onoe #if NBPFILTER > 0
    389  1.30  kiyohara 	if (ifp->if_bpf) {
    390  1.30  kiyohara 		struct ieee1394_bpfhdr h;
    391  1.30  kiyohara 		struct m_tag *mtag;
    392  1.36    dyoung 		const struct ieee1394_hwaddr *myaddr;
    393  1.30  kiyohara 
    394  1.30  kiyohara 		mtag = m_tag_locate(m,
    395  1.30  kiyohara 		    MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID, 0);
    396  1.30  kiyohara 		if (mtag)
    397  1.30  kiyohara 			memcpy(h.ibh_shost, mtag + 1, 8);
    398  1.30  kiyohara 		else
    399  1.30  kiyohara 			memset(h.ibh_shost, 0, 8);
    400  1.30  kiyohara 		if (m->m_flags & M_BCAST)
    401  1.30  kiyohara 			memcpy(h.ibh_dhost,
    402  1.30  kiyohara 			    ((const struct ieee1394_hwaddr *)
    403  1.30  kiyohara 			    ifp->if_broadcastaddr)->iha_uid, 8);
    404  1.30  kiyohara 		else {
    405  1.36    dyoung 			myaddr =
    406  1.36    dyoung 			  (const struct ieee1394_hwaddr *)CLLADDR(ifp->if_sadl);
    407  1.30  kiyohara 			memcpy(h.ibh_dhost, myaddr->iha_uid, 8);
    408  1.30  kiyohara 		}
    409  1.30  kiyohara 		h.ibh_type = htons(etype);
    410  1.30  kiyohara 		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
    411  1.30  kiyohara 	}
    412   1.1      onoe #endif
    413   1.1      onoe 
    414   1.1      onoe 	switch (etype) {
    415   1.1      onoe #ifdef INET
    416   1.1      onoe 	case ETHERTYPE_IP:
    417   1.1      onoe 		schednetisr(NETISR_IP);
    418   1.1      onoe 		inq = &ipintrq;
    419   1.1      onoe 		break;
    420   1.1      onoe 
    421   1.1      onoe 	case ETHERTYPE_ARP:
    422  1.16    itojun 		schednetisr(NETISR_ARP);
    423  1.16    itojun 		inq = &arpintrq;
    424  1.17     enami 		break;
    425   1.1      onoe #endif /* INET */
    426   1.1      onoe 
    427   1.1      onoe #ifdef INET6
    428   1.1      onoe 	case ETHERTYPE_IPV6:
    429   1.1      onoe 		schednetisr(NETISR_IPV6);
    430   1.1      onoe 		inq = &ip6intrq;
    431   1.1      onoe 		break;
    432   1.1      onoe #endif /* INET6 */
    433   1.1      onoe 
    434   1.1      onoe 	default:
    435   1.1      onoe 		m_freem(m);
    436   1.1      onoe 		return;
    437   1.1      onoe 	}
    438   1.1      onoe 
    439  1.10   thorpej 	s = splnet();
    440   1.1      onoe 	if (IF_QFULL(inq)) {
    441   1.1      onoe 		IF_DROP(inq);
    442   1.1      onoe 		m_freem(m);
    443   1.1      onoe 	} else
    444   1.1      onoe 		IF_ENQUEUE(inq, m);
    445   1.1      onoe 	splx(s);
    446   1.1      onoe }
    447   1.1      onoe 
    448   1.2      onoe static struct mbuf *
    449  1.39      matt ieee1394_reass(struct ifnet *ifp, struct mbuf *m0, uint16_t src)
    450   1.2      onoe {
    451   1.2      onoe 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    452   1.2      onoe 	struct ieee1394_fraghdr *ifh;
    453   1.2      onoe 	struct ieee1394_unfraghdr *iuh;
    454   1.2      onoe 	struct ieee1394_reassq *rq;
    455  1.25  christos 	struct ieee1394_reass_pkt *rp, *trp, *nrp = NULL;
    456   1.2      onoe 	int len;
    457  1.39      matt 	uint16_t etype, off, ftype, size, dgl;
    458  1.39      matt 	uint32_t id;
    459   1.2      onoe 
    460  1.29  kiyohara 	if (m0->m_len < sizeof(*ifh)) {
    461  1.29  kiyohara 		if ((m0 = m_pullup(m0, sizeof(*ifh))) == NULL)
    462   1.2      onoe 			return NULL;
    463   1.2      onoe 	}
    464  1.29  kiyohara 	ifh = mtod(m0, struct ieee1394_fraghdr *);
    465  1.29  kiyohara 	m_adj(m0, sizeof(*ifh));
    466   1.2      onoe 	size = ntohs(ifh->ifh_ft_size);
    467   1.2      onoe 	ftype = size & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE);
    468   1.2      onoe 	size = (size & ~ftype) + 1;
    469  1.29  kiyohara 	dgl = ntohs(ifh->ifh_dgl);
    470   1.2      onoe 	len = m0->m_pkthdr.len;
    471  1.29  kiyohara 	id = dgl | (src << 16);
    472   1.2      onoe 	if (ftype & IEEE1394_FT_SUBSEQ) {
    473  1.24    itojun 		m_tag_delete_chain(m0, NULL);
    474   1.2      onoe 		m0->m_flags &= ~M_PKTHDR;
    475  1.29  kiyohara 		etype = 0;
    476   1.2      onoe 		off = ntohs(ifh->ifh_etype_off);
    477  1.29  kiyohara 	} else {
    478  1.29  kiyohara 		etype = ifh->ifh_etype_off;
    479   1.2      onoe 		off = 0;
    480  1.29  kiyohara 	}
    481   1.2      onoe 
    482   1.2      onoe 	for (rq = LIST_FIRST(&ic->ic_reassq); ; rq = LIST_NEXT(rq, rq_node)) {
    483   1.2      onoe 		if (rq == NULL) {
    484   1.2      onoe 			/*
    485   1.2      onoe 			 * Create a new reassemble queue head for the node.
    486   1.2      onoe 			 */
    487   1.2      onoe 			rq = malloc(sizeof(*rq), M_FTABLE, M_NOWAIT);
    488   1.2      onoe 			if (rq == NULL) {
    489   1.2      onoe 				m_freem(m0);
    490   1.2      onoe 				return NULL;
    491   1.2      onoe 			}
    492  1.29  kiyohara 			rq->fr_id = id;
    493   1.2      onoe 			LIST_INIT(&rq->rq_pkt);
    494   1.2      onoe 			LIST_INSERT_HEAD(&ic->ic_reassq, rq, rq_node);
    495   1.2      onoe 			break;
    496   1.2      onoe 		}
    497  1.29  kiyohara 		if (rq->fr_id == id)
    498   1.2      onoe 			break;
    499   1.2      onoe 	}
    500   1.2      onoe 	for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
    501   1.2      onoe 		nrp = LIST_NEXT(rp, rp_next);
    502   1.2      onoe 		if (rp->rp_dgl != dgl)
    503   1.2      onoe 			continue;
    504   1.3      onoe 		/*
    505   1.3      onoe 		 * sanity check:
    506   1.3      onoe 		 * datagram size must be same for all fragments, and
    507   1.3      onoe 		 * no overlap is allowed.
    508   1.3      onoe 		 */
    509   1.3      onoe 		if (rp->rp_size != size ||
    510   1.3      onoe 		    (off < rp->rp_off + rp->rp_len && off + len > rp->rp_off)) {
    511   1.2      onoe 			/*
    512   1.3      onoe 			 * This happens probably due to wrapping dgl value.
    513   1.3      onoe 			 * Destroy all previously received fragment and
    514   1.3      onoe 			 * enqueue current fragment.
    515   1.2      onoe 			 */
    516   1.3      onoe 			for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL;
    517   1.3      onoe 			    rp = nrp) {
    518   1.3      onoe 				nrp = LIST_NEXT(rp, rp_next);
    519   1.3      onoe 				if (rp->rp_dgl == dgl) {
    520   1.3      onoe 					LIST_REMOVE(rp, rp_next);
    521   1.3      onoe 					m_freem(rp->rp_m);
    522   1.3      onoe 					free(rp, M_FTABLE);
    523   1.3      onoe 				}
    524   1.3      onoe 			}
    525   1.3      onoe 			break;
    526   1.2      onoe 		}
    527   1.2      onoe 		if (rp->rp_off + rp->rp_len == off) {
    528   1.2      onoe 			/*
    529   1.2      onoe 			 * All the subsequent fragments received in sequence
    530   1.2      onoe 			 * come here.
    531   1.2      onoe 			 * Concatinate mbuf to previous one instead of
    532   1.2      onoe 			 * allocating new reassemble queue structure,
    533   1.2      onoe 			 * and try to merge more with the subsequent fragment
    534   1.2      onoe 			 * in the queue.
    535   1.2      onoe 			 */
    536   1.2      onoe 			m_cat(rp->rp_m, m0);
    537   1.2      onoe 			rp->rp_len += len;
    538   1.2      onoe 			while (rp->rp_off + rp->rp_len < size &&
    539   1.2      onoe 			    nrp != NULL && nrp->rp_dgl == dgl &&
    540   1.2      onoe 			    nrp->rp_off == rp->rp_off + rp->rp_len) {
    541   1.2      onoe 				LIST_REMOVE(nrp, rp_next);
    542   1.2      onoe 				m_cat(rp->rp_m, nrp->rp_m);
    543   1.2      onoe 				rp->rp_len += nrp->rp_len;
    544   1.3      onoe 				free(nrp, M_FTABLE);
    545   1.2      onoe 				nrp = LIST_NEXT(rp, rp_next);
    546   1.2      onoe 			}
    547   1.2      onoe 			m0 = NULL;	/* mark merged */
    548   1.2      onoe 			break;
    549   1.2      onoe 		}
    550   1.2      onoe 		if (off + m0->m_pkthdr.len == rp->rp_off) {
    551   1.2      onoe 			m_cat(m0, rp->rp_m);
    552   1.2      onoe 			rp->rp_m = m0;
    553   1.2      onoe 			rp->rp_off = off;
    554  1.29  kiyohara 			rp->rp_etype = etype;	 /* over writing trust etype */
    555   1.2      onoe 			rp->rp_len += len;
    556   1.2      onoe 			m0 = NULL;	/* mark merged */
    557   1.2      onoe 			break;
    558   1.2      onoe 		}
    559   1.2      onoe 		if (rp->rp_off > off) {
    560   1.2      onoe 			/* insert before rp */
    561   1.2      onoe 			nrp = rp;
    562   1.2      onoe 			break;
    563   1.2      onoe 		}
    564   1.2      onoe 		if (nrp == NULL || nrp->rp_dgl != dgl) {
    565   1.2      onoe 			/* insert after rp */
    566   1.2      onoe 			nrp = NULL;
    567   1.2      onoe 			break;
    568   1.2      onoe 		}
    569   1.2      onoe 	}
    570   1.2      onoe 	if (m0 == NULL) {
    571   1.2      onoe 		if (rp->rp_off != 0 || rp->rp_len != size)
    572   1.2      onoe 			return NULL;
    573   1.2      onoe 		/* fragment done */
    574   1.2      onoe 		LIST_REMOVE(rp, rp_next);
    575   1.2      onoe 		m0 = rp->rp_m;
    576   1.2      onoe 		m0->m_pkthdr.len = rp->rp_len;
    577  1.29  kiyohara 		M_PREPEND(m0, sizeof(*iuh), M_DONTWAIT);
    578   1.2      onoe 		if (m0 != NULL) {
    579  1.29  kiyohara 			iuh = mtod(m0, struct ieee1394_unfraghdr *);
    580   1.2      onoe 			iuh->iuh_ft = 0;
    581   1.2      onoe 			iuh->iuh_etype = rp->rp_etype;
    582   1.2      onoe 		}
    583   1.2      onoe 		free(rp, M_FTABLE);
    584   1.2      onoe 		return m0;
    585   1.2      onoe 	}
    586   1.2      onoe 
    587   1.2      onoe 	/*
    588   1.2      onoe 	 * New fragment received.  Allocate reassemble queue structure.
    589   1.2      onoe 	 */
    590   1.2      onoe 	trp = malloc(sizeof(*trp), M_FTABLE, M_NOWAIT);
    591   1.2      onoe 	if (trp == NULL) {
    592   1.2      onoe 		m_freem(m0);
    593   1.2      onoe 		return NULL;
    594   1.2      onoe 	}
    595   1.2      onoe 	trp->rp_m = m0;
    596   1.2      onoe 	trp->rp_size = size;
    597  1.29  kiyohara 	trp->rp_etype = etype;		 /* valid only if off==0 */
    598   1.2      onoe 	trp->rp_off = off;
    599   1.2      onoe 	trp->rp_dgl = dgl;
    600   1.2      onoe 	trp->rp_len = len;
    601   1.3      onoe 	trp->rp_ttl = IEEE1394_REASS_TIMEOUT;
    602   1.3      onoe 	if (trp->rp_ttl <= ifp->if_timer)
    603   1.3      onoe 		trp->rp_ttl = ifp->if_timer + 1;
    604   1.2      onoe 
    605   1.2      onoe 	if (rp == NULL) {
    606   1.2      onoe 		/* first fragment for the dgl */
    607   1.2      onoe 		LIST_INSERT_HEAD(&rq->rq_pkt, trp, rp_next);
    608   1.2      onoe 	} else if (nrp == NULL) {
    609   1.2      onoe 		/* no next fragment for the dgl */
    610   1.2      onoe 		LIST_INSERT_AFTER(rp, trp, rp_next);
    611   1.2      onoe 	} else {
    612   1.2      onoe 		/* there is a hole */
    613   1.2      onoe 		LIST_INSERT_BEFORE(nrp, trp, rp_next);
    614   1.2      onoe 	}
    615   1.2      onoe 	return NULL;
    616   1.2      onoe }
    617   1.2      onoe 
    618   1.3      onoe void
    619   1.3      onoe ieee1394_drain(struct ifnet *ifp)
    620   1.3      onoe {
    621   1.3      onoe 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    622   1.3      onoe 	struct ieee1394_reassq *rq;
    623   1.3      onoe 	struct ieee1394_reass_pkt *rp;
    624   1.3      onoe 
    625   1.3      onoe 	while ((rq = LIST_FIRST(&ic->ic_reassq)) != NULL) {
    626   1.3      onoe 		LIST_REMOVE(rq, rq_node);
    627   1.3      onoe 		while ((rp = LIST_FIRST(&rq->rq_pkt)) != NULL) {
    628   1.3      onoe 			LIST_REMOVE(rp, rp_next);
    629   1.3      onoe 			m_freem(rp->rp_m);
    630   1.3      onoe 			free(rp, M_FTABLE);
    631   1.3      onoe 		}
    632   1.3      onoe 		free(rq, M_FTABLE);
    633   1.3      onoe 	}
    634   1.3      onoe }
    635   1.3      onoe 
    636   1.3      onoe void
    637   1.3      onoe ieee1394_watchdog(struct ifnet *ifp)
    638   1.3      onoe {
    639   1.3      onoe 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    640   1.3      onoe 	struct ieee1394_reassq *rq;
    641   1.3      onoe 	struct ieee1394_reass_pkt *rp, *nrp;
    642   1.3      onoe 	int dec;
    643   1.3      onoe 
    644   1.3      onoe 	dec = (ifp->if_timer > 0) ? ifp->if_timer : 1;
    645   1.3      onoe 	for (rq = LIST_FIRST(&ic->ic_reassq); rq != NULL;
    646   1.3      onoe 	    rq = LIST_NEXT(rq, rq_node)) {
    647   1.3      onoe 		for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
    648   1.3      onoe 			nrp = LIST_NEXT(rp, rp_next);
    649   1.3      onoe 			if (rp->rp_ttl >= dec)
    650   1.3      onoe 				rp->rp_ttl -= dec;
    651   1.3      onoe 			else {
    652   1.3      onoe 				LIST_REMOVE(rp, rp_next);
    653   1.3      onoe 				m_freem(rp->rp_m);
    654   1.3      onoe 				free(rp, M_FTABLE);
    655   1.3      onoe 			}
    656   1.3      onoe 		}
    657   1.3      onoe 	}
    658   1.3      onoe }
    659   1.3      onoe 
    660   1.1      onoe const char *
    661  1.39      matt ieee1394_sprintf(const uint8_t *laddr)
    662   1.1      onoe {
    663   1.1      onoe 	static char buf[3*8];
    664   1.1      onoe 
    665   1.1      onoe 	snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
    666   1.1      onoe 	    laddr[0], laddr[1], laddr[2], laddr[3],
    667   1.1      onoe 	    laddr[4], laddr[5], laddr[6], laddr[7]);
    668   1.1      onoe 	return buf;
    669   1.1      onoe }
    670   1.1      onoe 
    671   1.1      onoe void
    672   1.1      onoe ieee1394_ifattach(struct ifnet *ifp, const struct ieee1394_hwaddr *hwaddr)
    673   1.1      onoe {
    674   1.2      onoe 	struct ieee1394_hwaddr *baddr;
    675   1.2      onoe 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
    676   1.1      onoe 
    677   1.1      onoe 	ifp->if_type = IFT_IEEE1394;
    678   1.1      onoe 	ifp->if_hdrlen = sizeof(struct ieee1394_header);
    679   1.6   thorpej 	ifp->if_dlt = DLT_EN10MB;	/* XXX */
    680   1.1      onoe 	ifp->if_mtu = IEEE1394MTU;
    681   1.1      onoe 	ifp->if_output = ieee1394_output;
    682   1.3      onoe 	ifp->if_drain = ieee1394_drain;
    683   1.3      onoe 	ifp->if_watchdog = ieee1394_watchdog;
    684   1.3      onoe 	ifp->if_timer = 1;
    685   1.1      onoe 	if (ifp->if_baudrate == 0)
    686   1.1      onoe 		ifp->if_baudrate = IF_Mbps(100);
    687   1.7   thorpej 
    688  1.41    dyoung 	if_set_sadl(ifp, hwaddr, sizeof(struct ieee1394_hwaddr), true);
    689   1.7   thorpej 
    690  1.28      yamt 	baddr = malloc(ifp->if_addrlen, M_DEVBUF, M_WAITOK);
    691   1.2      onoe 	memset(baddr->iha_uid, 0xff, IEEE1394_ADDR_LEN);
    692   1.3      onoe 	baddr->iha_speed = 0;	/*XXX: how to determine the speed for bcast? */
    693   1.2      onoe 	baddr->iha_maxrec = 512 << baddr->iha_speed;
    694   1.2      onoe 	memset(baddr->iha_offset, 0, sizeof(baddr->iha_offset));
    695  1.28      yamt 	ifp->if_broadcastaddr = (uint8_t *)baddr;
    696   1.2      onoe 	LIST_INIT(&ic->ic_reassq);
    697   1.4   thorpej #if NBPFILTER > 0
    698  1.30  kiyohara 	bpfattach(ifp,
    699  1.30  kiyohara 	    DLT_APPLE_IP_OVER_IEEE1394, sizeof(struct ieee1394_hwaddr));
    700   1.4   thorpej #endif
    701   1.1      onoe }
    702   1.1      onoe 
    703   1.1      onoe void
    704   1.1      onoe ieee1394_ifdetach(struct ifnet *ifp)
    705   1.1      onoe {
    706   1.3      onoe 	ieee1394_drain(ifp);
    707   1.4   thorpej #if NBPFILTER > 0
    708   1.4   thorpej 	bpfdetach(ifp);
    709   1.4   thorpej #endif
    710  1.28      yamt 	free(__UNCONST(ifp->if_broadcastaddr), M_DEVBUF);
    711   1.7   thorpej 	ifp->if_broadcastaddr = NULL;
    712  1.23    itojun #if 0	/* done in if_detach() */
    713   1.7   thorpej 	if_free_sadl(ifp);
    714  1.23    itojun #endif
    715   1.1      onoe }
    716   1.1      onoe 
    717   1.1      onoe int
    718  1.34  christos ieee1394_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    719   1.1      onoe {
    720   1.1      onoe 	struct ifreq *ifr = (struct ifreq *)data;
    721   1.1      onoe 	struct ifaddr *ifa = (struct ifaddr *)data;
    722   1.1      onoe 	int error = 0;
    723   1.1      onoe 
    724   1.1      onoe 	switch (cmd) {
    725  1.41    dyoung 	case SIOCINITIFADDR:
    726   1.1      onoe 		ifp->if_flags |= IFF_UP;
    727   1.1      onoe 		switch (ifa->ifa_addr->sa_family) {
    728   1.1      onoe #ifdef INET
    729   1.1      onoe 		case AF_INET:
    730   1.1      onoe 			if ((error = (*ifp->if_init)(ifp)) != 0)
    731   1.1      onoe 				break;
    732  1.16    itojun 			arp_ifinit(ifp, ifa);
    733   1.1      onoe 			break;
    734   1.1      onoe #endif /* INET */
    735   1.1      onoe 		default:
    736   1.1      onoe 			error = (*ifp->if_init)(ifp);
    737   1.1      onoe 			break;
    738   1.1      onoe 		}
    739   1.1      onoe 		break;
    740   1.1      onoe 
    741   1.1      onoe 	case SIOCSIFMTU:
    742   1.1      onoe 		if (ifr->ifr_mtu > IEEE1394MTU)
    743   1.1      onoe 			error = EINVAL;
    744  1.38    dyoung 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    745  1.38    dyoung 			error = 0;
    746   1.1      onoe 		break;
    747   1.1      onoe 
    748   1.1      onoe 	default:
    749  1.41    dyoung 		error = ifioctl_common(ifp, cmd, data);
    750   1.1      onoe 		break;
    751   1.1      onoe 	}
    752   1.1      onoe 
    753   1.1      onoe 	return error;
    754   1.1      onoe }
    755