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
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
46 #include <net/if_var.h>
47 #include <net/if_private.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>
55 #include <net/if_llatbl.h>
57 #if defined(INET) || defined(INET6)
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet/if_ether.h>
63 #include <netinet6/nd6.h>
66 #include <security/mac/mac_framework.h>
68 static MALLOC_DEFINE(M_FWCOM, "fw_com", "firewire interface internals");
70 struct fw_hwaddr firewire_broadcastaddr = {
80 firewire_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
83 struct fw_com *fc = IFP2FWC(ifp);
87 struct fw_hwaddr *destfw;
89 uint16_t psize, fsize, dsize;
91 int unicast, dgl, foff;
93 #if defined(INET) || defined(INET6)
96 int af = RO_GET_FAMILY(ro, dst);
99 error = mac_ifnet_check_transmit(ifp, m);
104 if (!((ifp->if_flags & IFF_UP) &&
105 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
110 #if defined(INET) || defined(INET6)
112 is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
115 * For unicast, we make a tag to store the lladdr of the
116 * destination. This might not be the first time we have seen
117 * the packet (for instance, the arp code might be trying to
118 * re-send it after receiving an arp reply) so we only
119 * allocate a tag if there isn't one there already. For
120 * multicast, we will eventually use a different tag to store
121 * the channel number.
123 unicast = !(m->m_flags & (M_BCAST | M_MCAST));
125 mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, NULL);
127 mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR,
128 sizeof (struct fw_hwaddr), M_NOWAIT);
133 m_tag_prepend(m, mtag);
135 destfw = (struct fw_hwaddr *)(mtag + 1);
146 type = ETHERTYPE_ARP;
151 type = ETHERTYPE_IPV6;
155 if_printf(ifp, "can't handle af%d\n", af);
156 error = EAFNOSUPPORT;
160 switch (dst->sa_family) {
164 * Only bother with arp for unicast. Allocation of
165 * channels etc. for firewire is quite different and
166 * doesn't fit into the arp model.
169 error = arpresolve(ifp, is_gw, m, dst,
170 (u_char *) destfw, NULL, NULL);
172 return (error == EWOULDBLOCK ? 0 : error);
179 ah = mtod(m, struct arphdr *);
180 ah->ar_hrd = htons(ARPHRD_IEEE1394);
182 *destfw = *(struct fw_hwaddr *) ar_tha(ah);
185 * The standard arp code leaves a hole for the target
186 * hardware address which we need to close up.
188 bcopy(ar_tpa(ah), ar_tha(ah), ah->ar_pln);
189 m_adj(m, -ah->ar_hln);
197 error = nd6_resolve(fc->fc_ifp, LLE_SF(af, is_gw), m,
198 dst, (u_char *) destfw, NULL, NULL);
200 return (error == EWOULDBLOCK ? 0 : error);
206 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
207 error = EAFNOSUPPORT;
212 * Let BPF tap off a copy before we encapsulate.
214 if (bpf_peers_present(ifp->if_bpf)) {
217 bcopy(destfw, h.firewire_dhost, 8);
219 bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
220 bcopy(&fc->fc_hwaddr, h.firewire_shost, 8);
221 h.firewire_type = htons(type);
222 bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
226 * Punt on MCAP for now and send all multicast packets on the
229 if (m->m_flags & M_MCAST)
230 m->m_flags |= M_BCAST;
233 * Figure out what speed to use and what the largest supported
234 * packet size is. For unicast, this is the minimum of what we
235 * can speak and what they can hear. For broadcast, lets be
236 * conservative and use S100. We could possibly improve that
237 * by examining the bus manager's speed map or similar. We
238 * also reduce the packet size for broadcast to account for
242 speed = min(fc->fc_speed, destfw->sspd);
243 psize = min(512 << speed, 2 << destfw->sender_max_rec);
246 psize = 512 - 2*sizeof(uint32_t);
250 * Next, we encapsulate, possibly fragmenting the original
251 * datagram if it won't fit into a single packet.
253 if (m->m_pkthdr.len <= psize - sizeof(uint32_t)) {
255 * No fragmentation is necessary.
257 M_PREPEND(m, sizeof(uint32_t), M_NOWAIT);
262 enc = mtod(m, union fw_encap *);
263 enc->unfrag.ether_type = type;
264 enc->unfrag.lf = FW_ENCAP_UNFRAG;
265 enc->unfrag.reserved = 0;
268 * Byte swap the encapsulation header manually.
270 enc->ul[0] = htonl(enc->ul[0]);
272 error = (ifp->if_transmit)(ifp, m);
276 * Fragment the datagram, making sure to leave enough
277 * space for the encapsulation header in each packet.
279 fsize = psize - 2*sizeof(uint32_t);
281 dsize = m->m_pkthdr.len;
284 if (m->m_pkthdr.len > fsize) {
286 * Split off the tail segment from the
287 * datagram, copying our tags over.
289 mtail = m_split(m, fsize, M_NOWAIT);
290 m_tag_copy_chain(mtail, m, M_NOWAIT);
296 * Add our encapsulation header to this
297 * fragment and hand it off to the link.
299 M_PREPEND(m, 2*sizeof(uint32_t), M_NOWAIT);
304 enc = mtod(m, union fw_encap *);
306 enc->firstfrag.lf = FW_ENCAP_FIRST;
307 enc->firstfrag.reserved1 = 0;
308 enc->firstfrag.reserved2 = 0;
309 enc->firstfrag.datagram_size = dsize - 1;
310 enc->firstfrag.ether_type = type;
311 enc->firstfrag.dgl = dgl;
314 enc->nextfrag.lf = FW_ENCAP_NEXT;
316 enc->nextfrag.lf = FW_ENCAP_LAST;
317 enc->nextfrag.reserved1 = 0;
318 enc->nextfrag.reserved2 = 0;
319 enc->nextfrag.reserved3 = 0;
320 enc->nextfrag.datagram_size = dsize - 1;
321 enc->nextfrag.fragment_offset = foff;
322 enc->nextfrag.dgl = dgl;
324 foff += m->m_pkthdr.len - 2*sizeof(uint32_t);
327 * Byte swap the encapsulation header manually.
329 enc->ul[0] = htonl(enc->ul[0]);
330 enc->ul[1] = htonl(enc->ul[1]);
332 error = (ifp->if_transmit)(ifp, m);
352 firewire_input_fragment(struct fw_com *fc, struct mbuf *m, int src)
356 struct mbuf *mf, *mprev;
358 int fstart, fend, start, end, islast;
362 * Find an existing reassembly buffer or create a new one.
364 enc = mtod(m, union fw_encap *);
365 id = enc->firstfrag.dgl | (src << 16);
366 STAILQ_FOREACH(r, &fc->fc_frags, fr_link)
370 r = malloc(sizeof(struct fw_reass), M_TEMP, M_NOWAIT);
377 STAILQ_INSERT_HEAD(&fc->fc_frags, r, fr_link);
381 * If this fragment overlaps any other fragment, we must discard
382 * the partial reassembly and start again.
384 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
387 fstart = enc->nextfrag.fragment_offset;
388 fend = fstart + m->m_pkthdr.len - 2*sizeof(uint32_t);
389 dsize = enc->nextfrag.datagram_size;
390 islast = (enc->nextfrag.lf == FW_ENCAP_LAST);
392 for (mf = r->fr_frags; mf; mf = mf->m_nextpkt) {
393 enc = mtod(mf, union fw_encap *);
394 if (enc->nextfrag.datagram_size != dsize) {
396 * This fragment must be from a different
401 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
404 start = enc->nextfrag.fragment_offset;
405 end = start + mf->m_pkthdr.len - 2*sizeof(uint32_t);
406 if ((fstart < end && fend > start) ||
407 (islast && enc->nextfrag.lf == FW_ENCAP_LAST)) {
409 * Overlap - discard reassembly buffer and start
410 * again with this fragment.
417 * Find where to put this fragment in the list.
419 for (mf = r->fr_frags, mprev = NULL; mf;
420 mprev = mf, mf = mf->m_nextpkt) {
421 enc = mtod(mf, union fw_encap *);
422 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
425 start = enc->nextfrag.fragment_offset;
431 * If this is a last fragment and we are not adding at the end
432 * of the list, discard the buffer.
434 if (islast && mprev && mprev->m_nextpkt)
438 m->m_nextpkt = mprev->m_nextpkt;
439 mprev->m_nextpkt = m;
442 * Coalesce forwards and see if we can make a whole
445 enc = mtod(mprev, union fw_encap *);
446 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
449 start = enc->nextfrag.fragment_offset;
450 end = start + mprev->m_pkthdr.len - 2*sizeof(uint32_t);
451 while (end == fstart) {
453 * Strip off the encap header from m and
454 * append it to mprev, freeing m.
456 m_adj(m, 2*sizeof(uint32_t));
457 mprev->m_nextpkt = m->m_nextpkt;
458 mprev->m_pkthdr.len += m->m_pkthdr.len;
461 if (mprev->m_pkthdr.len == dsize + 1 + 2*sizeof(uint32_t)) {
463 * We have assembled a complete packet
464 * we must be finished. Make sure we have
465 * merged the whole chain.
467 STAILQ_REMOVE(&fc->fc_frags, r, fw_reass, fr_link);
469 m = mprev->m_nextpkt;
475 mprev->m_nextpkt = NULL;
481 * See if we can continue merging forwards.
484 m = mprev->m_nextpkt;
486 enc = mtod(m, union fw_encap *);
487 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
490 fstart = enc->nextfrag.fragment_offset;
491 fend = fstart + m->m_pkthdr.len
492 - 2*sizeof(uint32_t);
505 while (r->fr_frags) {
507 r->fr_frags = mf->m_nextpkt;
517 firewire_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
519 struct fw_com *fc = IFP2FWC(ifp);
524 * The caller has already stripped off the packet header
525 * (stream or wreqb) and marked the mbuf's M_BCAST flag
526 * appropriately. We de-encapsulate the IP packet and pass it
527 * up the line after handling link-level fragmentation.
529 if (m->m_pkthdr.len < sizeof(uint32_t)) {
530 if_printf(ifp, "discarding frame without "
531 "encapsulation header (len %u pkt len %u)\n",
532 m->m_len, m->m_pkthdr.len);
535 m = m_pullup(m, sizeof(uint32_t));
538 enc = mtod(m, union fw_encap *);
541 * Byte swap the encapsulation header manually.
543 enc->ul[0] = ntohl(enc->ul[0]);
545 if (enc->unfrag.lf != 0) {
546 m = m_pullup(m, 2*sizeof(uint32_t));
549 enc = mtod(m, union fw_encap *);
550 enc->ul[1] = ntohl(enc->ul[1]);
551 m = firewire_input_fragment(fc, m, src);
554 enc = mtod(m, union fw_encap *);
555 type = enc->firstfrag.ether_type;
556 m_adj(m, 2*sizeof(uint32_t));
558 type = enc->unfrag.ether_type;
559 m_adj(m, sizeof(uint32_t));
562 if (m->m_pkthdr.rcvif == NULL) {
563 if_printf(ifp, "discard frame w/o interface pointer\n");
564 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
569 if (m->m_pkthdr.rcvif != ifp) {
570 if_printf(ifp, "Warning, frame marked as received on %s\n",
571 m->m_pkthdr.rcvif->if_xname);
577 * Tag the mbuf with an appropriate MAC label before any other
578 * consumers can get to it.
580 mac_ifnet_create_mbuf(ifp, m);
584 * Give bpf a chance at the packet. The link-level driver
585 * should have left us a tag with the EUID of the sender.
587 if (bpf_peers_present(ifp->if_bpf)) {
591 mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID, 0);
593 bcopy(mtag + 1, h.firewire_shost, 8);
595 bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
596 bcopy(&fc->fc_hwaddr, h.firewire_dhost, 8);
597 h.firewire_type = htons(type);
598 bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
601 if (ifp->if_flags & IFF_MONITOR) {
603 * Interface marked for monitoring; discard packet.
609 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
611 /* Discard packet if interface is not up */
612 if ((ifp->if_flags & IFF_UP) == 0) {
617 if (m->m_flags & (M_BCAST|M_MCAST))
618 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
629 ah = mtod(m, struct arphdr *);
632 * Adjust the arp packet to insert an empty tha slot.
634 m->m_len += ah->ar_hln;
635 m->m_pkthdr.len += ah->ar_hln;
636 bcopy(ar_tha(ah), ar_tpa(ah), ah->ar_pln);
653 M_SETFIB(m, ifp->if_fib);
654 CURVNET_SET_QUIET(ifp->if_vnet);
655 netisr_dispatch(isr, m);
660 firewire_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
662 struct ifaddr *ifa = (struct ifaddr *) data;
663 struct ifreq *ifr = (struct ifreq *) data;
668 ifp->if_flags |= IFF_UP;
670 switch (ifa->ifa_addr->sa_family) {
673 ifp->if_init(ifp->if_softc); /* before arpwhohas */
674 arp_ifinit(ifp, ifa);
678 ifp->if_init(ifp->if_softc);
684 bcopy(&IFP2FWC(ifp)->fc_hwaddr, &ifr->ifr_addr.sa_data[0],
685 sizeof(struct fw_hwaddr));
690 * Set the interface MTU.
692 if (ifr->ifr_mtu > 1500) {
695 ifp->if_mtu = ifr->ifr_mtu;
699 error = EINVAL; /* XXX netbsd has ENOTTY??? */
706 firewire_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
710 struct sockaddr_in *sin;
713 struct sockaddr_in6 *sin6;
716 switch(sa->sa_family) {
726 sin = (struct sockaddr_in *)sa;
727 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
728 return EADDRNOTAVAIL;
734 sin6 = (struct sockaddr_in6 *)sa;
735 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
737 * An IP6 address of 0 means listen to all
738 * of the Ethernet multicast address used for IP6.
739 * (This is used for multicast routers.)
741 ifp->if_flags |= IFF_ALLMULTI;
745 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
746 return EADDRNOTAVAIL;
753 * Well, the text isn't quite right, but it's the name
761 firewire_ifattach(struct ifnet *ifp, struct fw_hwaddr *llc)
763 struct fw_com *fc = IFP2FWC(ifp);
765 struct sockaddr_dl *sdl;
766 static const char* speeds[] = {
767 "S100", "S200", "S400", "S800",
771 fc->fc_speed = llc->sspd;
772 STAILQ_INIT(&fc->fc_frags);
774 ifp->if_addrlen = sizeof(struct fw_hwaddr);
777 ifp->if_mtu = 1500; /* XXX */
778 ifp->if_output = firewire_output;
779 ifp->if_resolvemulti = firewire_resolvemulti;
780 ifp->if_broadcastaddr = (u_char *) &firewire_broadcastaddr;
783 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
784 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
785 sdl->sdl_type = IFT_IEEE1394;
786 sdl->sdl_alen = ifp->if_addrlen;
787 bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
789 bpfattach(ifp, DLT_APPLE_IP_OVER_IEEE1394,
790 sizeof(struct fw_hwaddr));
792 if_printf(ifp, "Firewire address: %8D @ 0x%04x%08x, %s, maxrec %d\n",
793 (uint8_t *) &llc->sender_unique_ID_hi, ":",
794 ntohs(llc->sender_unicast_FIFO_hi),
795 ntohl(llc->sender_unicast_FIFO_lo),
797 (2 << llc->sender_max_rec));
801 firewire_ifdetach(struct ifnet *ifp)
808 firewire_busreset(struct ifnet *ifp)
810 struct fw_com *fc = IFP2FWC(ifp);
815 * Discard any partial datagrams since the host ids may have changed.
817 while ((r = STAILQ_FIRST(&fc->fc_frags))) {
818 STAILQ_REMOVE_HEAD(&fc->fc_frags, fr_link);
819 while (r->fr_frags) {
821 r->fr_frags = m->m_nextpkt;
829 firewire_alloc(u_char type, struct ifnet *ifp)
833 fc = malloc(sizeof(struct fw_com), M_FWCOM, M_WAITOK | M_ZERO);
840 firewire_free(void *com, u_char type)
847 firewire_modevent(module_t mod, int type, void *data)
852 if_register_com_alloc(IFT_IEEE1394,
853 firewire_alloc, firewire_free);
856 if_deregister_com_alloc(IFT_IEEE1394);
865 static moduledata_t firewire_mod = {
871 DECLARE_MODULE(if_firewire, firewire_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
872 MODULE_VERSION(if_firewire, 1);