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>
40 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
47 #include <net/netisr.h>
48 #include <net/route.h>
49 #include <net/if_llc.h>
50 #include <net/if_dl.h>
51 #include <net/if_types.h>
53 #include <net/firewire.h>
55 #if defined(INET) || defined(INET6)
56 #include <netinet/in.h>
57 #include <netinet/in_var.h>
58 #include <netinet/if_ether.h>
61 #include <netinet6/nd6.h>
64 #include <security/mac/mac_framework.h>
66 MALLOC_DEFINE(M_FWCOM, "fw_com", "firewire interface internals");
68 struct fw_hwaddr firewire_broadcastaddr = {
78 firewire_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
81 struct fw_com *fc = IFP2FWC(ifp);
83 struct rtentry *rt = NULL;
86 struct fw_hwaddr *destfw;
88 uint16_t psize, fsize, dsize;
90 int unicast, dgl, foff;
94 error = mac_check_ifnet_transmit(ifp, m);
99 if (!((ifp->if_flags & IFF_UP) &&
100 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
106 error = rt_check(&rt, &rt0, dst);
113 * For unicast, we make a tag to store the lladdr of the
114 * destination. This might not be the first time we have seen
115 * the packet (for instance, the arp code might be trying to
116 * re-send it after receiving an arp reply) so we only
117 * allocate a tag if there isn't one there already. For
118 * multicast, we will eventually use a different tag to store
119 * the channel number.
121 unicast = !(m->m_flags & (M_BCAST | M_MCAST));
123 mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, NULL);
125 mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR,
126 sizeof (struct fw_hwaddr), M_NOWAIT);
131 m_tag_prepend(m, mtag);
133 destfw = (struct fw_hwaddr *)(mtag + 1);
138 switch (dst->sa_family) {
142 * Only bother with arp for unicast. Allocation of
143 * channels etc. for firewire is quite different and
144 * doesn't fit into the arp model.
147 error = arpresolve(ifp, rt, m, dst, (u_char *) destfw);
149 return (error == EWOULDBLOCK ? 0 : error);
157 ah = mtod(m, struct arphdr *);
158 ah->ar_hrd = htons(ARPHRD_IEEE1394);
159 type = ETHERTYPE_ARP;
161 *destfw = *(struct fw_hwaddr *) ar_tha(ah);
164 * The standard arp code leaves a hole for the target
165 * hardware address which we need to close up.
167 bcopy(ar_tpa(ah), ar_tha(ah), ah->ar_pln);
168 m_adj(m, -ah->ar_hln);
176 error = nd6_storelladdr(fc->fc_ifp, rt, m, dst,
181 type = ETHERTYPE_IPV6;
186 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
187 error = EAFNOSUPPORT;
192 * Let BPF tap off a copy before we encapsulate.
194 if (bpf_peers_present(ifp->if_bpf)) {
197 bcopy(destfw, h.firewire_dhost, 8);
199 bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
200 bcopy(&fc->fc_hwaddr, h.firewire_shost, 8);
201 h.firewire_type = htons(type);
202 bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
206 * Punt on MCAP for now and send all multicast packets on the
209 if (m->m_flags & M_MCAST)
210 m->m_flags |= M_BCAST;
213 * Figure out what speed to use and what the largest supported
214 * packet size is. For unicast, this is the minimum of what we
215 * can speak and what they can hear. For broadcast, lets be
216 * conservative and use S100. We could possibly improve that
217 * by examining the bus manager's speed map or similar. We
218 * also reduce the packet size for broadcast to account for
222 speed = min(fc->fc_speed, destfw->sspd);
223 psize = min(512 << speed, 2 << destfw->sender_max_rec);
226 psize = 512 - 2*sizeof(uint32_t);
230 * Next, we encapsulate, possibly fragmenting the original
231 * datagram if it won't fit into a single packet.
233 if (m->m_pkthdr.len <= psize - sizeof(uint32_t)) {
235 * No fragmentation is necessary.
237 M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
242 enc = mtod(m, union fw_encap *);
243 enc->unfrag.ether_type = type;
244 enc->unfrag.lf = FW_ENCAP_UNFRAG;
245 enc->unfrag.reserved = 0;
248 * Byte swap the encapsulation header manually.
250 enc->ul[0] = htonl(enc->ul[0]);
252 IFQ_HANDOFF(ifp, m, error);
256 * Fragment the datagram, making sure to leave enough
257 * space for the encapsulation header in each packet.
259 fsize = psize - 2*sizeof(uint32_t);
261 dsize = m->m_pkthdr.len;
264 if (m->m_pkthdr.len > fsize) {
266 * Split off the tail segment from the
267 * datagram, copying our tags over.
269 mtail = m_split(m, fsize, M_DONTWAIT);
270 m_tag_copy_chain(mtail, m, M_NOWAIT);
276 * Add our encapsulation header to this
277 * fragment and hand it off to the link.
279 M_PREPEND(m, 2*sizeof(uint32_t), M_DONTWAIT);
284 enc = mtod(m, union fw_encap *);
286 enc->firstfrag.lf = FW_ENCAP_FIRST;
287 enc->firstfrag.reserved1 = 0;
288 enc->firstfrag.reserved2 = 0;
289 enc->firstfrag.datagram_size = dsize - 1;
290 enc->firstfrag.ether_type = type;
291 enc->firstfrag.dgl = dgl;
294 enc->nextfrag.lf = FW_ENCAP_NEXT;
296 enc->nextfrag.lf = FW_ENCAP_LAST;
297 enc->nextfrag.reserved1 = 0;
298 enc->nextfrag.reserved2 = 0;
299 enc->nextfrag.reserved3 = 0;
300 enc->nextfrag.datagram_size = dsize - 1;
301 enc->nextfrag.fragment_offset = foff;
302 enc->nextfrag.dgl = dgl;
304 foff += m->m_pkthdr.len - 2*sizeof(uint32_t);
307 * Byte swap the encapsulation header manually.
309 enc->ul[0] = htonl(enc->ul[0]);
310 enc->ul[1] = htonl(enc->ul[1]);
312 IFQ_HANDOFF(ifp, m, error);
332 firewire_input_fragment(struct fw_com *fc, struct mbuf *m, int src)
336 struct mbuf *mf, *mprev;
338 int fstart, fend, start, end, islast;
344 * Find an existing reassembly buffer or create a new one.
346 enc = mtod(m, union fw_encap *);
347 id = enc->firstfrag.dgl | (src << 16);
348 STAILQ_FOREACH(r, &fc->fc_frags, fr_link)
352 r = malloc(sizeof(struct fw_reass), M_TEMP, M_NOWAIT);
359 STAILQ_INSERT_HEAD(&fc->fc_frags, r, fr_link);
363 * If this fragment overlaps any other fragment, we must discard
364 * the partial reassembly and start again.
366 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
369 fstart = enc->nextfrag.fragment_offset;
370 fend = fstart + m->m_pkthdr.len - 2*sizeof(uint32_t);
371 dsize = enc->nextfrag.datagram_size;
372 islast = (enc->nextfrag.lf == FW_ENCAP_LAST);
374 for (mf = r->fr_frags; mf; mf = mf->m_nextpkt) {
375 enc = mtod(mf, union fw_encap *);
376 if (enc->nextfrag.datagram_size != dsize) {
378 * This fragment must be from a different
383 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
386 start = enc->nextfrag.fragment_offset;
387 end = start + mf->m_pkthdr.len - 2*sizeof(uint32_t);
388 if ((fstart < end && fend > start) ||
389 (islast && enc->nextfrag.lf == FW_ENCAP_LAST)) {
391 * Overlap - discard reassembly buffer and start
392 * again with this fragment.
399 * Find where to put this fragment in the list.
401 for (mf = r->fr_frags, mprev = NULL; mf;
402 mprev = mf, mf = mf->m_nextpkt) {
403 enc = mtod(mf, union fw_encap *);
404 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
407 start = enc->nextfrag.fragment_offset;
413 * If this is a last fragment and we are not adding at the end
414 * of the list, discard the buffer.
416 if (islast && mprev && mprev->m_nextpkt)
420 m->m_nextpkt = mprev->m_nextpkt;
421 mprev->m_nextpkt = m;
424 * Coalesce forwards and see if we can make a whole
427 enc = mtod(mprev, union fw_encap *);
428 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
431 start = enc->nextfrag.fragment_offset;
432 end = start + mprev->m_pkthdr.len - 2*sizeof(uint32_t);
433 while (end == fstart) {
435 * Strip off the encap header from m and
436 * append it to mprev, freeing m.
438 m_adj(m, 2*sizeof(uint32_t));
439 mprev->m_nextpkt = m->m_nextpkt;
440 mprev->m_pkthdr.len += m->m_pkthdr.len;
443 if (mprev->m_pkthdr.len == dsize + 1 + 2*sizeof(uint32_t)) {
445 * We have assembled a complete packet
446 * we must be finished. Make sure we have
447 * merged the whole chain.
449 STAILQ_REMOVE(&fc->fc_frags, r, fw_reass, fr_link);
451 m = mprev->m_nextpkt;
457 mprev->m_nextpkt = NULL;
463 * See if we can continue merging forwards.
466 m = mprev->m_nextpkt;
468 enc = mtod(m, union fw_encap *);
469 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
472 fstart = enc->nextfrag.fragment_offset;
473 fend = fstart + m->m_pkthdr.len
474 - 2*sizeof(uint32_t);
487 while (r->fr_frags) {
489 r->fr_frags = mf->m_nextpkt;
499 firewire_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
501 struct fw_com *fc = IFP2FWC(ifp);
508 * The caller has already stripped off the packet header
509 * (stream or wreqb) and marked the mbuf's M_BCAST flag
510 * appropriately. We de-encapsulate the IP packet and pass it
511 * up the line after handling link-level fragmentation.
513 if (m->m_pkthdr.len < sizeof(uint32_t)) {
514 if_printf(ifp, "discarding frame without "
515 "encapsulation header (len %u pkt len %u)\n",
516 m->m_len, m->m_pkthdr.len);
519 m = m_pullup(m, sizeof(uint32_t));
522 enc = mtod(m, union fw_encap *);
525 * Byte swap the encapsulation header manually.
527 enc->ul[0] = ntohl(enc->ul[0]);
529 if (enc->unfrag.lf != 0) {
530 m = m_pullup(m, 2*sizeof(uint32_t));
533 enc = mtod(m, union fw_encap *);
534 enc->ul[1] = ntohl(enc->ul[1]);
535 m = firewire_input_fragment(fc, m, src);
538 enc = mtod(m, union fw_encap *);
539 type = enc->firstfrag.ether_type;
540 m_adj(m, 2*sizeof(uint32_t));
542 type = enc->unfrag.ether_type;
543 m_adj(m, sizeof(uint32_t));
546 if (m->m_pkthdr.rcvif == NULL) {
547 if_printf(ifp, "discard frame w/o interface pointer\n");
553 if (m->m_pkthdr.rcvif != ifp) {
554 if_printf(ifp, "Warning, frame marked as received on %s\n",
555 m->m_pkthdr.rcvif->if_xname);
561 * Tag the mbuf with an appropriate MAC label before any other
562 * consumers can get to it.
564 mac_create_mbuf_from_ifnet(ifp, m);
568 * Give bpf a chance at the packet. The link-level driver
569 * should have left us a tag with the EUID of the sender.
571 if (bpf_peers_present(ifp->if_bpf)) {
575 mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID, 0);
577 bcopy(mtag + 1, h.firewire_shost, 8);
579 bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
580 bcopy(&fc->fc_hwaddr, h.firewire_dhost, 8);
581 h.firewire_type = htons(type);
582 bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
585 if (ifp->if_flags & IFF_MONITOR) {
587 * Interface marked for monitoring; discard packet.
593 ifp->if_ibytes += m->m_pkthdr.len;
595 /* Discard packet if interface is not up */
596 if ((ifp->if_flags & IFF_UP) == 0) {
601 if (m->m_flags & (M_BCAST|M_MCAST))
607 if ((m = ip_fastforward(m)) == NULL)
615 ah = mtod(m, struct arphdr *);
618 * Adjust the arp packet to insert an empty tha slot.
620 m->m_len += ah->ar_hln;
621 m->m_pkthdr.len += ah->ar_hln;
622 bcopy(ar_tha(ah), ar_tpa(ah), ah->ar_pln);
639 netisr_dispatch(isr, m);
643 firewire_ioctl(struct ifnet *ifp, int command, caddr_t data)
645 struct ifaddr *ifa = (struct ifaddr *) data;
646 struct ifreq *ifr = (struct ifreq *) data;
651 ifp->if_flags |= IFF_UP;
653 switch (ifa->ifa_addr->sa_family) {
656 ifp->if_init(ifp->if_softc); /* before arpwhohas */
657 arp_ifinit(ifp, ifa);
661 ifp->if_init(ifp->if_softc);
670 sa = (struct sockaddr *) & ifr->ifr_data;
671 bcopy(&IFP2FWC(ifp)->fc_hwaddr,
672 (caddr_t) sa->sa_data, sizeof(struct fw_hwaddr));
678 * Set the interface MTU.
680 if (ifr->ifr_mtu > 1500) {
683 ifp->if_mtu = ifr->ifr_mtu;
687 error = EINVAL; /* XXX netbsd has ENOTTY??? */
694 firewire_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
698 struct sockaddr_in *sin;
701 struct sockaddr_in6 *sin6;
704 switch(sa->sa_family) {
714 sin = (struct sockaddr_in *)sa;
715 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
716 return EADDRNOTAVAIL;
722 sin6 = (struct sockaddr_in6 *)sa;
723 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
725 * An IP6 address of 0 means listen to all
726 * of the Ethernet multicast address used for IP6.
727 * (This is used for multicast routers.)
729 ifp->if_flags |= IFF_ALLMULTI;
733 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
734 return EADDRNOTAVAIL;
741 * Well, the text isn't quite right, but it's the name
749 firewire_ifattach(struct ifnet *ifp, struct fw_hwaddr *llc)
751 struct fw_com *fc = IFP2FWC(ifp);
753 struct sockaddr_dl *sdl;
754 static const char* speeds[] = {
755 "S100", "S200", "S400", "S800",
759 fc->fc_speed = llc->sspd;
760 STAILQ_INIT(&fc->fc_frags);
762 ifp->if_addrlen = sizeof(struct fw_hwaddr);
765 ifp->if_mtu = 1500; /* XXX */
766 ifp->if_output = firewire_output;
767 ifp->if_resolvemulti = firewire_resolvemulti;
768 ifp->if_broadcastaddr = (u_char *) &firewire_broadcastaddr;
771 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
772 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
773 sdl->sdl_type = IFT_IEEE1394;
774 sdl->sdl_alen = ifp->if_addrlen;
775 bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
777 bpfattach(ifp, DLT_APPLE_IP_OVER_IEEE1394,
778 sizeof(struct fw_hwaddr));
780 if_printf(ifp, "Firewire address: %8D @ 0x%04x%08x, %s, maxrec %d\n",
781 (uint8_t *) &llc->sender_unique_ID_hi, ":",
782 ntohs(llc->sender_unicast_FIFO_hi),
783 ntohl(llc->sender_unicast_FIFO_lo),
785 (2 << llc->sender_max_rec));
789 firewire_ifdetach(struct ifnet *ifp)
796 firewire_busreset(struct ifnet *ifp)
798 struct fw_com *fc = IFP2FWC(ifp);
803 * Discard any partial datagrams since the host ids may have changed.
805 while ((r = STAILQ_FIRST(&fc->fc_frags))) {
806 STAILQ_REMOVE_HEAD(&fc->fc_frags, fr_link);
807 while (r->fr_frags) {
809 r->fr_frags = m->m_nextpkt;
817 firewire_alloc(u_char type, struct ifnet *ifp)
821 fc = malloc(sizeof(struct fw_com), M_FWCOM, M_WAITOK | M_ZERO);
828 firewire_free(void *com, u_char type)
835 firewire_modevent(module_t mod, int type, void *data)
840 if_register_com_alloc(IFT_IEEE1394,
841 firewire_alloc, firewire_free);
844 if_deregister_com_alloc(IFT_IEEE1394);
853 static moduledata_t firewire_mod = {
859 DECLARE_MODULE(if_firewire, firewire_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
860 MODULE_VERSION(if_firewire, 1);