2 * Copyright (c) 2004 Doug Rabson
3 * Copyright (c) 1982, 1989, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
48 #include <net/netisr.h>
49 #include <net/route.h>
50 #include <net/if_llc.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
54 #include <net/firewire.h>
56 #if defined(INET) || defined(INET6)
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
59 #include <netinet/if_ether.h>
62 #include <netinet6/nd6.h>
65 MALLOC_DEFINE(M_FWCOM, "fw_com", "firewire interface internals");
67 struct fw_hwaddr firewire_broadcastaddr = {
77 firewire_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
80 struct fw_com *fc = IFP2FWC(ifp);
82 struct rtentry *rt = NULL;
85 struct fw_hwaddr *destfw;
87 uint16_t psize, fsize, dsize;
89 int unicast, dgl, foff;
93 error = mac_check_ifnet_transmit(ifp, m);
98 if (!((ifp->if_flags & IFF_UP) &&
99 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
105 error = rt_check(&rt, &rt0, dst);
112 * For unicast, we make a tag to store the lladdr of the
113 * destination. This might not be the first time we have seen
114 * the packet (for instance, the arp code might be trying to
115 * re-send it after receiving an arp reply) so we only
116 * allocate a tag if there isn't one there already. For
117 * multicast, we will eventually use a different tag to store
118 * the channel number.
120 unicast = !(m->m_flags & (M_BCAST | M_MCAST));
122 mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, NULL);
124 mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR,
125 sizeof (struct fw_hwaddr), M_NOWAIT);
130 m_tag_prepend(m, mtag);
132 destfw = (struct fw_hwaddr *)(mtag + 1);
137 switch (dst->sa_family) {
141 * Only bother with arp for unicast. Allocation of
142 * channels etc. for firewire is quite different and
143 * doesn't fit into the arp model.
146 error = arpresolve(ifp, rt, m, dst, (u_char *) destfw);
148 return (error == EWOULDBLOCK ? 0 : error);
156 ah = mtod(m, struct arphdr *);
157 ah->ar_hrd = htons(ARPHRD_IEEE1394);
158 type = ETHERTYPE_ARP;
160 *destfw = *(struct fw_hwaddr *) ar_tha(ah);
163 * The standard arp code leaves a hole for the target
164 * hardware address which we need to close up.
166 bcopy(ar_tpa(ah), ar_tha(ah), ah->ar_pln);
167 m_adj(m, -ah->ar_hln);
175 error = nd6_storelladdr(fc->fc_ifp, rt, m, dst,
180 type = ETHERTYPE_IPV6;
185 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
186 error = EAFNOSUPPORT;
191 * Let BPF tap off a copy before we encapsulate.
196 bcopy(destfw, h.firewire_dhost, 8);
198 bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
199 bcopy(&fc->fc_hwaddr, h.firewire_shost, 8);
200 h.firewire_type = htons(type);
201 bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
205 * Punt on MCAP for now and send all multicast packets on the
208 if (m->m_flags & M_MCAST)
209 m->m_flags |= M_BCAST;
212 * Figure out what speed to use and what the largest supported
213 * packet size is. For unicast, this is the minimum of what we
214 * can speak and what they can hear. For broadcast, lets be
215 * conservative and use S100. We could possibly improve that
216 * by examining the bus manager's speed map or similar. We
217 * also reduce the packet size for broadcast to account for
221 speed = min(fc->fc_speed, destfw->sspd);
222 psize = min(512 << speed, 2 << destfw->sender_max_rec);
225 psize = 512 - 2*sizeof(uint32_t);
229 * Next, we encapsulate, possibly fragmenting the original
230 * datagram if it won't fit into a single packet.
232 if (m->m_pkthdr.len <= psize - sizeof(uint32_t)) {
234 * No fragmentation is necessary.
236 M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
241 enc = mtod(m, union fw_encap *);
242 enc->unfrag.ether_type = type;
243 enc->unfrag.lf = FW_ENCAP_UNFRAG;
244 enc->unfrag.reserved = 0;
247 * Byte swap the encapsulation header manually.
249 enc->ul[0] = htonl(enc->ul[0]);
251 IFQ_HANDOFF(ifp, m, error);
255 * Fragment the datagram, making sure to leave enough
256 * space for the encapsulation header in each packet.
258 fsize = psize - 2*sizeof(uint32_t);
260 dsize = m->m_pkthdr.len;
263 if (m->m_pkthdr.len > fsize) {
265 * Split off the tail segment from the
266 * datagram, copying our tags over.
268 mtail = m_split(m, fsize, M_DONTWAIT);
269 m_tag_copy_chain(mtail, m, M_NOWAIT);
275 * Add our encapsulation header to this
276 * fragment and hand it off to the link.
278 M_PREPEND(m, 2*sizeof(uint32_t), M_DONTWAIT);
283 enc = mtod(m, union fw_encap *);
285 enc->firstfrag.lf = FW_ENCAP_FIRST;
286 enc->firstfrag.reserved1 = 0;
287 enc->firstfrag.reserved2 = 0;
288 enc->firstfrag.datagram_size = dsize - 1;
289 enc->firstfrag.ether_type = type;
290 enc->firstfrag.dgl = dgl;
293 enc->nextfrag.lf = FW_ENCAP_NEXT;
295 enc->nextfrag.lf = FW_ENCAP_LAST;
296 enc->nextfrag.reserved1 = 0;
297 enc->nextfrag.reserved2 = 0;
298 enc->nextfrag.reserved3 = 0;
299 enc->nextfrag.datagram_size = dsize - 1;
300 enc->nextfrag.fragment_offset = foff;
301 enc->nextfrag.dgl = dgl;
303 foff += m->m_pkthdr.len - 2*sizeof(uint32_t);
306 * Byte swap the encapsulation header manually.
308 enc->ul[0] = htonl(enc->ul[0]);
309 enc->ul[1] = htonl(enc->ul[1]);
311 IFQ_HANDOFF(ifp, m, error);
331 firewire_input_fragment(struct fw_com *fc, struct mbuf *m, int src)
335 struct mbuf *mf, *mprev;
337 int fstart, fend, start, end, islast;
343 * Find an existing reassembly buffer or create a new one.
345 enc = mtod(m, union fw_encap *);
346 id = enc->firstfrag.dgl | (src << 16);
347 STAILQ_FOREACH(r, &fc->fc_frags, fr_link)
351 r = malloc(sizeof(struct fw_reass), M_TEMP, M_NOWAIT);
358 STAILQ_INSERT_HEAD(&fc->fc_frags, r, fr_link);
362 * If this fragment overlaps any other fragment, we must discard
363 * the partial reassembly and start again.
365 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
368 fstart = enc->nextfrag.fragment_offset;
369 fend = fstart + m->m_pkthdr.len - 2*sizeof(uint32_t);
370 dsize = enc->nextfrag.datagram_size;
371 islast = (enc->nextfrag.lf == FW_ENCAP_LAST);
373 for (mf = r->fr_frags; mf; mf = mf->m_nextpkt) {
374 enc = mtod(mf, union fw_encap *);
375 if (enc->nextfrag.datagram_size != dsize) {
377 * This fragment must be from a different
382 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
385 start = enc->nextfrag.fragment_offset;
386 end = start + mf->m_pkthdr.len - 2*sizeof(uint32_t);
387 if ((fstart < end && fend > start) ||
388 (islast && enc->nextfrag.lf == FW_ENCAP_LAST)) {
390 * Overlap - discard reassembly buffer and start
391 * again with this fragment.
398 * Find where to put this fragment in the list.
400 for (mf = r->fr_frags, mprev = NULL; mf;
401 mprev = mf, mf = mf->m_nextpkt) {
402 enc = mtod(mf, union fw_encap *);
403 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
406 start = enc->nextfrag.fragment_offset;
412 * If this is a last fragment and we are not adding at the end
413 * of the list, discard the buffer.
415 if (islast && mprev && mprev->m_nextpkt)
419 m->m_nextpkt = mprev->m_nextpkt;
420 mprev->m_nextpkt = m;
423 * Coalesce forwards and see if we can make a whole
426 enc = mtod(mprev, union fw_encap *);
427 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
430 start = enc->nextfrag.fragment_offset;
431 end = start + mprev->m_pkthdr.len - 2*sizeof(uint32_t);
432 while (end == fstart) {
434 * Strip off the encap header from m and
435 * append it to mprev, freeing m.
437 m_adj(m, 2*sizeof(uint32_t));
438 mprev->m_nextpkt = m->m_nextpkt;
439 mprev->m_pkthdr.len += m->m_pkthdr.len;
442 if (mprev->m_pkthdr.len == dsize + 1 + 2*sizeof(uint32_t)) {
444 * We have assembled a complete packet
445 * we must be finished. Make sure we have
446 * merged the whole chain.
448 STAILQ_REMOVE(&fc->fc_frags, r, fw_reass, fr_link);
450 m = mprev->m_nextpkt;
456 mprev->m_nextpkt = NULL;
462 * See if we can continue merging forwards.
465 m = mprev->m_nextpkt;
467 enc = mtod(m, union fw_encap *);
468 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
471 fstart = enc->nextfrag.fragment_offset;
472 fend = fstart + m->m_pkthdr.len
473 - 2*sizeof(uint32_t);
486 while (r->fr_frags) {
488 r->fr_frags = mf->m_nextpkt;
498 firewire_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
500 struct fw_com *fc = IFP2FWC(ifp);
507 * The caller has already stripped off the packet header
508 * (stream or wreqb) and marked the mbuf's M_BCAST flag
509 * appropriately. We de-encapsulate the IP packet and pass it
510 * up the line after handling link-level fragmentation.
512 if (m->m_pkthdr.len < sizeof(uint32_t)) {
513 if_printf(ifp, "discarding frame without "
514 "encapsulation header (len %u pkt len %u)\n",
515 m->m_len, m->m_pkthdr.len);
518 m = m_pullup(m, sizeof(uint32_t));
519 enc = mtod(m, union fw_encap *);
522 * Byte swap the encapsulation header manually.
524 enc->ul[0] = ntohl(enc->ul[0]);
526 if (enc->unfrag.lf != 0) {
527 m = m_pullup(m, 2*sizeof(uint32_t));
530 enc = mtod(m, union fw_encap *);
531 enc->ul[1] = ntohl(enc->ul[1]);
532 m = firewire_input_fragment(fc, m, src);
535 enc = mtod(m, union fw_encap *);
536 type = enc->firstfrag.ether_type;
537 m_adj(m, 2*sizeof(uint32_t));
539 type = enc->unfrag.ether_type;
540 m_adj(m, sizeof(uint32_t));
543 if (m->m_pkthdr.rcvif == NULL) {
544 if_printf(ifp, "discard frame w/o interface pointer\n");
550 if (m->m_pkthdr.rcvif != ifp) {
551 if_printf(ifp, "Warning, frame marked as received on %s\n",
552 m->m_pkthdr.rcvif->if_xname);
558 * Tag the mbuf with an appropriate MAC label before any other
559 * consumers can get to it.
561 mac_create_mbuf_from_ifnet(ifp, m);
565 * Give bpf a chance at the packet. The link-level driver
566 * should have left us a tag with the EUID of the sender.
572 mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID, 0);
574 bcopy(mtag + 1, h.firewire_shost, 8);
576 bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
577 bcopy(&fc->fc_hwaddr, h.firewire_dhost, 8);
578 h.firewire_type = htons(type);
579 bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
582 if (ifp->if_flags & IFF_MONITOR) {
584 * Interface marked for monitoring; discard packet.
590 ifp->if_ibytes += m->m_pkthdr.len;
592 /* Discard packet if interface is not up */
593 if ((ifp->if_flags & IFF_UP) == 0) {
598 if (m->m_flags & (M_BCAST|M_MCAST))
604 if ((m = ip_fastforward(m)) == NULL)
612 ah = mtod(m, struct arphdr *);
615 * Adjust the arp packet to insert an empty tha slot.
617 m->m_len += ah->ar_hln;
618 m->m_pkthdr.len += ah->ar_hln;
619 bcopy(ar_tha(ah), ar_tpa(ah), ah->ar_pln);
636 netisr_dispatch(isr, m);
640 firewire_ioctl(struct ifnet *ifp, int command, caddr_t data)
642 struct ifaddr *ifa = (struct ifaddr *) data;
643 struct ifreq *ifr = (struct ifreq *) data;
648 ifp->if_flags |= IFF_UP;
650 switch (ifa->ifa_addr->sa_family) {
653 ifp->if_init(ifp->if_softc); /* before arpwhohas */
654 arp_ifinit(ifp, ifa);
658 ifp->if_init(ifp->if_softc);
667 sa = (struct sockaddr *) & ifr->ifr_data;
668 bcopy(&IFP2FWC(ifp)->fc_hwaddr,
669 (caddr_t) sa->sa_data, sizeof(struct fw_hwaddr));
675 * Set the interface MTU.
677 if (ifr->ifr_mtu > 1500) {
680 ifp->if_mtu = ifr->ifr_mtu;
684 error = EINVAL; /* XXX netbsd has ENOTTY??? */
691 firewire_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
695 struct sockaddr_in *sin;
698 struct sockaddr_in6 *sin6;
701 switch(sa->sa_family) {
711 sin = (struct sockaddr_in *)sa;
712 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
713 return EADDRNOTAVAIL;
719 sin6 = (struct sockaddr_in6 *)sa;
720 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
722 * An IP6 address of 0 means listen to all
723 * of the Ethernet multicast address used for IP6.
724 * (This is used for multicast routers.)
726 ifp->if_flags |= IFF_ALLMULTI;
730 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
731 return EADDRNOTAVAIL;
738 * Well, the text isn't quite right, but it's the name
746 firewire_ifattach(struct ifnet *ifp, struct fw_hwaddr *llc)
748 struct fw_com *fc = IFP2FWC(ifp);
750 struct sockaddr_dl *sdl;
751 static const char* speeds[] = {
752 "S100", "S200", "S400", "S800",
756 fc->fc_speed = llc->sspd;
757 STAILQ_INIT(&fc->fc_frags);
759 ifp->if_addrlen = sizeof(struct fw_hwaddr);
762 ifp->if_mtu = 1500; /* XXX */
763 ifp->if_output = firewire_output;
764 ifp->if_resolvemulti = firewire_resolvemulti;
765 ifp->if_broadcastaddr = (u_char *) &firewire_broadcastaddr;
768 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
769 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
770 sdl->sdl_type = IFT_IEEE1394;
771 sdl->sdl_alen = ifp->if_addrlen;
772 bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
774 bpfattach(ifp, DLT_APPLE_IP_OVER_IEEE1394,
775 sizeof(struct fw_hwaddr));
777 if_printf(ifp, "Firewire address: %8D @ 0x%04x%08x, %s, maxrec %d\n",
778 (uint8_t *) &llc->sender_unique_ID_hi, ":",
779 ntohs(llc->sender_unicast_FIFO_hi),
780 ntohl(llc->sender_unicast_FIFO_lo),
782 (2 << llc->sender_max_rec));
786 firewire_ifdetach(struct ifnet *ifp)
793 firewire_busreset(struct ifnet *ifp)
795 struct fw_com *fc = IFP2FWC(ifp);
800 * Discard any partial datagrams since the host ids may have changed.
802 while ((r = STAILQ_FIRST(&fc->fc_frags))) {
803 STAILQ_REMOVE_HEAD(&fc->fc_frags, fr_link);
804 while (r->fr_frags) {
806 r->fr_frags = m->m_nextpkt;
814 firewire_alloc(u_char type, struct ifnet *ifp)
818 fc = malloc(sizeof(struct fw_com), M_FWCOM, M_WAITOK | M_ZERO);
825 firewire_free(void *com, u_char type)
832 firewire_modevent(module_t mod, int type, void *data)
837 if_register_com_alloc(IFT_IEEE1394,
838 firewire_alloc, firewire_free);
841 if_deregister_com_alloc(IFT_IEEE1394);
850 static moduledata_t firewire_mod = {
856 DECLARE_MODULE(if_firewire, firewire_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
857 MODULE_VERSION(if_firewire, 1);