2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2004 Doug Rabson
5 * Copyright (c) 1982, 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #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/if_var.h>
49 #include <net/if_private.h>
50 #include <net/netisr.h>
51 #include <net/route.h>
52 #include <net/if_llc.h>
53 #include <net/if_dl.h>
54 #include <net/if_types.h>
56 #include <net/firewire.h>
57 #include <net/if_llatbl.h>
59 #if defined(INET) || defined(INET6)
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <netinet/if_ether.h>
65 #include <netinet6/nd6.h>
68 #include <security/mac/mac_framework.h>
70 static MALLOC_DEFINE(M_FWCOM, "fw_com", "firewire interface internals");
72 struct fw_hwaddr firewire_broadcastaddr = {
82 firewire_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
85 struct fw_com *fc = IFP2FWC(ifp);
89 struct fw_hwaddr *destfw;
91 uint16_t psize, fsize, dsize;
93 int unicast, dgl, foff;
95 #if defined(INET) || defined(INET6)
98 int af = RO_GET_FAMILY(ro, dst);
101 error = mac_ifnet_check_transmit(ifp, m);
106 if (!((ifp->if_flags & IFF_UP) &&
107 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
112 #if defined(INET) || defined(INET6)
114 is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
117 * For unicast, we make a tag to store the lladdr of the
118 * destination. This might not be the first time we have seen
119 * the packet (for instance, the arp code might be trying to
120 * re-send it after receiving an arp reply) so we only
121 * allocate a tag if there isn't one there already. For
122 * multicast, we will eventually use a different tag to store
123 * the channel number.
125 unicast = !(m->m_flags & (M_BCAST | M_MCAST));
127 mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, NULL);
129 mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR,
130 sizeof (struct fw_hwaddr), M_NOWAIT);
135 m_tag_prepend(m, mtag);
137 destfw = (struct fw_hwaddr *)(mtag + 1);
148 type = ETHERTYPE_ARP;
153 type = ETHERTYPE_IPV6;
157 if_printf(ifp, "can't handle af%d\n", af);
158 error = EAFNOSUPPORT;
162 switch (dst->sa_family) {
166 * Only bother with arp for unicast. Allocation of
167 * channels etc. for firewire is quite different and
168 * doesn't fit into the arp model.
171 error = arpresolve(ifp, is_gw, m, dst,
172 (u_char *) destfw, NULL, NULL);
174 return (error == EWOULDBLOCK ? 0 : error);
181 ah = mtod(m, struct arphdr *);
182 ah->ar_hrd = htons(ARPHRD_IEEE1394);
184 *destfw = *(struct fw_hwaddr *) ar_tha(ah);
187 * The standard arp code leaves a hole for the target
188 * hardware address which we need to close up.
190 bcopy(ar_tpa(ah), ar_tha(ah), ah->ar_pln);
191 m_adj(m, -ah->ar_hln);
199 error = nd6_resolve(fc->fc_ifp, LLE_SF(af, is_gw), m,
200 dst, (u_char *) destfw, NULL, NULL);
202 return (error == EWOULDBLOCK ? 0 : error);
208 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
209 error = EAFNOSUPPORT;
214 * Let BPF tap off a copy before we encapsulate.
216 if (bpf_peers_present(ifp->if_bpf)) {
219 bcopy(destfw, h.firewire_dhost, 8);
221 bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
222 bcopy(&fc->fc_hwaddr, h.firewire_shost, 8);
223 h.firewire_type = htons(type);
224 bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
228 * Punt on MCAP for now and send all multicast packets on the
231 if (m->m_flags & M_MCAST)
232 m->m_flags |= M_BCAST;
235 * Figure out what speed to use and what the largest supported
236 * packet size is. For unicast, this is the minimum of what we
237 * can speak and what they can hear. For broadcast, lets be
238 * conservative and use S100. We could possibly improve that
239 * by examining the bus manager's speed map or similar. We
240 * also reduce the packet size for broadcast to account for
244 speed = min(fc->fc_speed, destfw->sspd);
245 psize = min(512 << speed, 2 << destfw->sender_max_rec);
248 psize = 512 - 2*sizeof(uint32_t);
252 * Next, we encapsulate, possibly fragmenting the original
253 * datagram if it won't fit into a single packet.
255 if (m->m_pkthdr.len <= psize - sizeof(uint32_t)) {
257 * No fragmentation is necessary.
259 M_PREPEND(m, sizeof(uint32_t), M_NOWAIT);
264 enc = mtod(m, union fw_encap *);
265 enc->unfrag.ether_type = type;
266 enc->unfrag.lf = FW_ENCAP_UNFRAG;
267 enc->unfrag.reserved = 0;
270 * Byte swap the encapsulation header manually.
272 enc->ul[0] = htonl(enc->ul[0]);
274 error = (ifp->if_transmit)(ifp, m);
278 * Fragment the datagram, making sure to leave enough
279 * space for the encapsulation header in each packet.
281 fsize = psize - 2*sizeof(uint32_t);
283 dsize = m->m_pkthdr.len;
286 if (m->m_pkthdr.len > fsize) {
288 * Split off the tail segment from the
289 * datagram, copying our tags over.
291 mtail = m_split(m, fsize, M_NOWAIT);
292 m_tag_copy_chain(mtail, m, M_NOWAIT);
298 * Add our encapsulation header to this
299 * fragment and hand it off to the link.
301 M_PREPEND(m, 2*sizeof(uint32_t), M_NOWAIT);
306 enc = mtod(m, union fw_encap *);
308 enc->firstfrag.lf = FW_ENCAP_FIRST;
309 enc->firstfrag.reserved1 = 0;
310 enc->firstfrag.reserved2 = 0;
311 enc->firstfrag.datagram_size = dsize - 1;
312 enc->firstfrag.ether_type = type;
313 enc->firstfrag.dgl = dgl;
316 enc->nextfrag.lf = FW_ENCAP_NEXT;
318 enc->nextfrag.lf = FW_ENCAP_LAST;
319 enc->nextfrag.reserved1 = 0;
320 enc->nextfrag.reserved2 = 0;
321 enc->nextfrag.reserved3 = 0;
322 enc->nextfrag.datagram_size = dsize - 1;
323 enc->nextfrag.fragment_offset = foff;
324 enc->nextfrag.dgl = dgl;
326 foff += m->m_pkthdr.len - 2*sizeof(uint32_t);
329 * Byte swap the encapsulation header manually.
331 enc->ul[0] = htonl(enc->ul[0]);
332 enc->ul[1] = htonl(enc->ul[1]);
334 error = (ifp->if_transmit)(ifp, m);
354 firewire_input_fragment(struct fw_com *fc, struct mbuf *m, int src)
358 struct mbuf *mf, *mprev;
360 int fstart, fend, start, end, islast;
364 * Find an existing reassembly buffer or create a new one.
366 enc = mtod(m, union fw_encap *);
367 id = enc->firstfrag.dgl | (src << 16);
368 STAILQ_FOREACH(r, &fc->fc_frags, fr_link)
372 r = malloc(sizeof(struct fw_reass), M_TEMP, M_NOWAIT);
379 STAILQ_INSERT_HEAD(&fc->fc_frags, r, fr_link);
383 * If this fragment overlaps any other fragment, we must discard
384 * the partial reassembly and start again.
386 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
389 fstart = enc->nextfrag.fragment_offset;
390 fend = fstart + m->m_pkthdr.len - 2*sizeof(uint32_t);
391 dsize = enc->nextfrag.datagram_size;
392 islast = (enc->nextfrag.lf == FW_ENCAP_LAST);
394 for (mf = r->fr_frags; mf; mf = mf->m_nextpkt) {
395 enc = mtod(mf, union fw_encap *);
396 if (enc->nextfrag.datagram_size != dsize) {
398 * This fragment must be from a different
403 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
406 start = enc->nextfrag.fragment_offset;
407 end = start + mf->m_pkthdr.len - 2*sizeof(uint32_t);
408 if ((fstart < end && fend > start) ||
409 (islast && enc->nextfrag.lf == FW_ENCAP_LAST)) {
411 * Overlap - discard reassembly buffer and start
412 * again with this fragment.
419 * Find where to put this fragment in the list.
421 for (mf = r->fr_frags, mprev = NULL; mf;
422 mprev = mf, mf = mf->m_nextpkt) {
423 enc = mtod(mf, union fw_encap *);
424 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
427 start = enc->nextfrag.fragment_offset;
433 * If this is a last fragment and we are not adding at the end
434 * of the list, discard the buffer.
436 if (islast && mprev && mprev->m_nextpkt)
440 m->m_nextpkt = mprev->m_nextpkt;
441 mprev->m_nextpkt = m;
444 * Coalesce forwards and see if we can make a whole
447 enc = mtod(mprev, union fw_encap *);
448 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
451 start = enc->nextfrag.fragment_offset;
452 end = start + mprev->m_pkthdr.len - 2*sizeof(uint32_t);
453 while (end == fstart) {
455 * Strip off the encap header from m and
456 * append it to mprev, freeing m.
458 m_adj(m, 2*sizeof(uint32_t));
459 mprev->m_nextpkt = m->m_nextpkt;
460 mprev->m_pkthdr.len += m->m_pkthdr.len;
463 if (mprev->m_pkthdr.len == dsize + 1 + 2*sizeof(uint32_t)) {
465 * We have assembled a complete packet
466 * we must be finished. Make sure we have
467 * merged the whole chain.
469 STAILQ_REMOVE(&fc->fc_frags, r, fw_reass, fr_link);
471 m = mprev->m_nextpkt;
477 mprev->m_nextpkt = NULL;
483 * See if we can continue merging forwards.
486 m = mprev->m_nextpkt;
488 enc = mtod(m, union fw_encap *);
489 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
492 fstart = enc->nextfrag.fragment_offset;
493 fend = fstart + m->m_pkthdr.len
494 - 2*sizeof(uint32_t);
507 while (r->fr_frags) {
509 r->fr_frags = mf->m_nextpkt;
519 firewire_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
521 struct fw_com *fc = IFP2FWC(ifp);
526 * The caller has already stripped off the packet header
527 * (stream or wreqb) and marked the mbuf's M_BCAST flag
528 * appropriately. We de-encapsulate the IP packet and pass it
529 * up the line after handling link-level fragmentation.
531 if (m->m_pkthdr.len < sizeof(uint32_t)) {
532 if_printf(ifp, "discarding frame without "
533 "encapsulation header (len %u pkt len %u)\n",
534 m->m_len, m->m_pkthdr.len);
537 m = m_pullup(m, sizeof(uint32_t));
540 enc = mtod(m, union fw_encap *);
543 * Byte swap the encapsulation header manually.
545 enc->ul[0] = ntohl(enc->ul[0]);
547 if (enc->unfrag.lf != 0) {
548 m = m_pullup(m, 2*sizeof(uint32_t));
551 enc = mtod(m, union fw_encap *);
552 enc->ul[1] = ntohl(enc->ul[1]);
553 m = firewire_input_fragment(fc, m, src);
556 enc = mtod(m, union fw_encap *);
557 type = enc->firstfrag.ether_type;
558 m_adj(m, 2*sizeof(uint32_t));
560 type = enc->unfrag.ether_type;
561 m_adj(m, sizeof(uint32_t));
564 if (m->m_pkthdr.rcvif == NULL) {
565 if_printf(ifp, "discard frame w/o interface pointer\n");
566 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
571 if (m->m_pkthdr.rcvif != ifp) {
572 if_printf(ifp, "Warning, frame marked as received on %s\n",
573 m->m_pkthdr.rcvif->if_xname);
579 * Tag the mbuf with an appropriate MAC label before any other
580 * consumers can get to it.
582 mac_ifnet_create_mbuf(ifp, m);
586 * Give bpf a chance at the packet. The link-level driver
587 * should have left us a tag with the EUID of the sender.
589 if (bpf_peers_present(ifp->if_bpf)) {
593 mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID, 0);
595 bcopy(mtag + 1, h.firewire_shost, 8);
597 bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
598 bcopy(&fc->fc_hwaddr, h.firewire_dhost, 8);
599 h.firewire_type = htons(type);
600 bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
603 if (ifp->if_flags & IFF_MONITOR) {
605 * Interface marked for monitoring; discard packet.
611 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
613 /* Discard packet if interface is not up */
614 if ((ifp->if_flags & IFF_UP) == 0) {
619 if (m->m_flags & (M_BCAST|M_MCAST))
620 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
631 ah = mtod(m, struct arphdr *);
634 * Adjust the arp packet to insert an empty tha slot.
636 m->m_len += ah->ar_hln;
637 m->m_pkthdr.len += ah->ar_hln;
638 bcopy(ar_tha(ah), ar_tpa(ah), ah->ar_pln);
655 M_SETFIB(m, ifp->if_fib);
656 CURVNET_SET_QUIET(ifp->if_vnet);
657 netisr_dispatch(isr, m);
662 firewire_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
664 struct ifaddr *ifa = (struct ifaddr *) data;
665 struct ifreq *ifr = (struct ifreq *) data;
670 ifp->if_flags |= IFF_UP;
672 switch (ifa->ifa_addr->sa_family) {
675 ifp->if_init(ifp->if_softc); /* before arpwhohas */
676 arp_ifinit(ifp, ifa);
680 ifp->if_init(ifp->if_softc);
686 bcopy(&IFP2FWC(ifp)->fc_hwaddr, &ifr->ifr_addr.sa_data[0],
687 sizeof(struct fw_hwaddr));
692 * Set the interface MTU.
694 if (ifr->ifr_mtu > 1500) {
697 ifp->if_mtu = ifr->ifr_mtu;
701 error = EINVAL; /* XXX netbsd has ENOTTY??? */
708 firewire_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
712 struct sockaddr_in *sin;
715 struct sockaddr_in6 *sin6;
718 switch(sa->sa_family) {
728 sin = (struct sockaddr_in *)sa;
729 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
730 return EADDRNOTAVAIL;
736 sin6 = (struct sockaddr_in6 *)sa;
737 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
739 * An IP6 address of 0 means listen to all
740 * of the Ethernet multicast address used for IP6.
741 * (This is used for multicast routers.)
743 ifp->if_flags |= IFF_ALLMULTI;
747 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
748 return EADDRNOTAVAIL;
755 * Well, the text isn't quite right, but it's the name
763 firewire_ifattach(struct ifnet *ifp, struct fw_hwaddr *llc)
765 struct fw_com *fc = IFP2FWC(ifp);
767 struct sockaddr_dl *sdl;
768 static const char* speeds[] = {
769 "S100", "S200", "S400", "S800",
773 fc->fc_speed = llc->sspd;
774 STAILQ_INIT(&fc->fc_frags);
776 ifp->if_addrlen = sizeof(struct fw_hwaddr);
779 ifp->if_mtu = 1500; /* XXX */
780 ifp->if_output = firewire_output;
781 ifp->if_resolvemulti = firewire_resolvemulti;
782 ifp->if_broadcastaddr = (u_char *) &firewire_broadcastaddr;
785 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
786 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
787 sdl->sdl_type = IFT_IEEE1394;
788 sdl->sdl_alen = ifp->if_addrlen;
789 bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
791 bpfattach(ifp, DLT_APPLE_IP_OVER_IEEE1394,
792 sizeof(struct fw_hwaddr));
794 if_printf(ifp, "Firewire address: %8D @ 0x%04x%08x, %s, maxrec %d\n",
795 (uint8_t *) &llc->sender_unique_ID_hi, ":",
796 ntohs(llc->sender_unicast_FIFO_hi),
797 ntohl(llc->sender_unicast_FIFO_lo),
799 (2 << llc->sender_max_rec));
803 firewire_ifdetach(struct ifnet *ifp)
810 firewire_busreset(struct ifnet *ifp)
812 struct fw_com *fc = IFP2FWC(ifp);
817 * Discard any partial datagrams since the host ids may have changed.
819 while ((r = STAILQ_FIRST(&fc->fc_frags))) {
820 STAILQ_REMOVE_HEAD(&fc->fc_frags, fr_link);
821 while (r->fr_frags) {
823 r->fr_frags = m->m_nextpkt;
831 firewire_alloc(u_char type, struct ifnet *ifp)
835 fc = malloc(sizeof(struct fw_com), M_FWCOM, M_WAITOK | M_ZERO);
842 firewire_free(void *com, u_char type)
849 firewire_modevent(module_t mod, int type, void *data)
854 if_register_com_alloc(IFT_IEEE1394,
855 firewire_alloc, firewire_free);
858 if_deregister_com_alloc(IFT_IEEE1394);
867 static moduledata_t firewire_mod = {
873 DECLARE_MODULE(if_firewire, firewire_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
874 MODULE_VERSION(if_firewire, 1);