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/netisr.h>
50 #include <net/route.h>
51 #include <net/if_llc.h>
52 #include <net/if_dl.h>
53 #include <net/if_types.h>
55 #include <net/firewire.h>
56 #include <net/if_llatbl.h>
58 #if defined(INET) || defined(INET6)
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/if_ether.h>
64 #include <netinet6/nd6.h>
67 #include <security/mac/mac_framework.h>
69 static MALLOC_DEFINE(M_FWCOM, "fw_com", "firewire interface internals");
71 struct fw_hwaddr firewire_broadcastaddr = {
81 firewire_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
84 struct fw_com *fc = IFP2FWC(ifp);
88 struct fw_hwaddr *destfw;
90 uint16_t psize, fsize, dsize;
92 int unicast, dgl, foff;
94 #if defined(INET) || defined(INET6)
97 int af = RO_GET_FAMILY(ro, dst);
100 error = mac_ifnet_check_transmit(ifp, m);
105 if (!((ifp->if_flags & IFF_UP) &&
106 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
111 #if defined(INET) || defined(INET6)
113 is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
116 * For unicast, we make a tag to store the lladdr of the
117 * destination. This might not be the first time we have seen
118 * the packet (for instance, the arp code might be trying to
119 * re-send it after receiving an arp reply) so we only
120 * allocate a tag if there isn't one there already. For
121 * multicast, we will eventually use a different tag to store
122 * the channel number.
124 unicast = !(m->m_flags & (M_BCAST | M_MCAST));
126 mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, NULL);
128 mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR,
129 sizeof (struct fw_hwaddr), M_NOWAIT);
134 m_tag_prepend(m, mtag);
136 destfw = (struct fw_hwaddr *)(mtag + 1);
147 type = ETHERTYPE_ARP;
152 type = ETHERTYPE_IPV6;
156 if_printf(ifp, "can't handle af%d\n", af);
157 error = EAFNOSUPPORT;
161 switch (dst->sa_family) {
165 * Only bother with arp for unicast. Allocation of
166 * channels etc. for firewire is quite different and
167 * doesn't fit into the arp model.
170 error = arpresolve(ifp, is_gw, m, dst,
171 (u_char *) destfw, NULL, NULL);
173 return (error == EWOULDBLOCK ? 0 : error);
180 ah = mtod(m, struct arphdr *);
181 ah->ar_hrd = htons(ARPHRD_IEEE1394);
183 *destfw = *(struct fw_hwaddr *) ar_tha(ah);
186 * The standard arp code leaves a hole for the target
187 * hardware address which we need to close up.
189 bcopy(ar_tpa(ah), ar_tha(ah), ah->ar_pln);
190 m_adj(m, -ah->ar_hln);
198 error = nd6_resolve(fc->fc_ifp, LLE_SF(af, is_gw), m,
199 dst, (u_char *) destfw, NULL, NULL);
201 return (error == EWOULDBLOCK ? 0 : error);
207 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
208 error = EAFNOSUPPORT;
213 * Let BPF tap off a copy before we encapsulate.
215 if (bpf_peers_present(ifp->if_bpf)) {
218 bcopy(destfw, h.firewire_dhost, 8);
220 bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
221 bcopy(&fc->fc_hwaddr, h.firewire_shost, 8);
222 h.firewire_type = htons(type);
223 bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
227 * Punt on MCAP for now and send all multicast packets on the
230 if (m->m_flags & M_MCAST)
231 m->m_flags |= M_BCAST;
234 * Figure out what speed to use and what the largest supported
235 * packet size is. For unicast, this is the minimum of what we
236 * can speak and what they can hear. For broadcast, lets be
237 * conservative and use S100. We could possibly improve that
238 * by examining the bus manager's speed map or similar. We
239 * also reduce the packet size for broadcast to account for
243 speed = min(fc->fc_speed, destfw->sspd);
244 psize = min(512 << speed, 2 << destfw->sender_max_rec);
247 psize = 512 - 2*sizeof(uint32_t);
251 * Next, we encapsulate, possibly fragmenting the original
252 * datagram if it won't fit into a single packet.
254 if (m->m_pkthdr.len <= psize - sizeof(uint32_t)) {
256 * No fragmentation is necessary.
258 M_PREPEND(m, sizeof(uint32_t), M_NOWAIT);
263 enc = mtod(m, union fw_encap *);
264 enc->unfrag.ether_type = type;
265 enc->unfrag.lf = FW_ENCAP_UNFRAG;
266 enc->unfrag.reserved = 0;
269 * Byte swap the encapsulation header manually.
271 enc->ul[0] = htonl(enc->ul[0]);
273 error = (ifp->if_transmit)(ifp, m);
277 * Fragment the datagram, making sure to leave enough
278 * space for the encapsulation header in each packet.
280 fsize = psize - 2*sizeof(uint32_t);
282 dsize = m->m_pkthdr.len;
285 if (m->m_pkthdr.len > fsize) {
287 * Split off the tail segment from the
288 * datagram, copying our tags over.
290 mtail = m_split(m, fsize, M_NOWAIT);
291 m_tag_copy_chain(mtail, m, M_NOWAIT);
297 * Add our encapsulation header to this
298 * fragment and hand it off to the link.
300 M_PREPEND(m, 2*sizeof(uint32_t), M_NOWAIT);
305 enc = mtod(m, union fw_encap *);
307 enc->firstfrag.lf = FW_ENCAP_FIRST;
308 enc->firstfrag.reserved1 = 0;
309 enc->firstfrag.reserved2 = 0;
310 enc->firstfrag.datagram_size = dsize - 1;
311 enc->firstfrag.ether_type = type;
312 enc->firstfrag.dgl = dgl;
315 enc->nextfrag.lf = FW_ENCAP_NEXT;
317 enc->nextfrag.lf = FW_ENCAP_LAST;
318 enc->nextfrag.reserved1 = 0;
319 enc->nextfrag.reserved2 = 0;
320 enc->nextfrag.reserved3 = 0;
321 enc->nextfrag.datagram_size = dsize - 1;
322 enc->nextfrag.fragment_offset = foff;
323 enc->nextfrag.dgl = dgl;
325 foff += m->m_pkthdr.len - 2*sizeof(uint32_t);
328 * Byte swap the encapsulation header manually.
330 enc->ul[0] = htonl(enc->ul[0]);
331 enc->ul[1] = htonl(enc->ul[1]);
333 error = (ifp->if_transmit)(ifp, m);
353 firewire_input_fragment(struct fw_com *fc, struct mbuf *m, int src)
357 struct mbuf *mf, *mprev;
359 int fstart, fend, start, end, islast;
363 * Find an existing reassembly buffer or create a new one.
365 enc = mtod(m, union fw_encap *);
366 id = enc->firstfrag.dgl | (src << 16);
367 STAILQ_FOREACH(r, &fc->fc_frags, fr_link)
371 r = malloc(sizeof(struct fw_reass), M_TEMP, M_NOWAIT);
378 STAILQ_INSERT_HEAD(&fc->fc_frags, r, fr_link);
382 * If this fragment overlaps any other fragment, we must discard
383 * the partial reassembly and start again.
385 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
388 fstart = enc->nextfrag.fragment_offset;
389 fend = fstart + m->m_pkthdr.len - 2*sizeof(uint32_t);
390 dsize = enc->nextfrag.datagram_size;
391 islast = (enc->nextfrag.lf == FW_ENCAP_LAST);
393 for (mf = r->fr_frags; mf; mf = mf->m_nextpkt) {
394 enc = mtod(mf, union fw_encap *);
395 if (enc->nextfrag.datagram_size != dsize) {
397 * This fragment must be from a different
402 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
405 start = enc->nextfrag.fragment_offset;
406 end = start + mf->m_pkthdr.len - 2*sizeof(uint32_t);
407 if ((fstart < end && fend > start) ||
408 (islast && enc->nextfrag.lf == FW_ENCAP_LAST)) {
410 * Overlap - discard reassembly buffer and start
411 * again with this fragment.
418 * Find where to put this fragment in the list.
420 for (mf = r->fr_frags, mprev = NULL; mf;
421 mprev = mf, mf = mf->m_nextpkt) {
422 enc = mtod(mf, union fw_encap *);
423 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
426 start = enc->nextfrag.fragment_offset;
432 * If this is a last fragment and we are not adding at the end
433 * of the list, discard the buffer.
435 if (islast && mprev && mprev->m_nextpkt)
439 m->m_nextpkt = mprev->m_nextpkt;
440 mprev->m_nextpkt = m;
443 * Coalesce forwards and see if we can make a whole
446 enc = mtod(mprev, union fw_encap *);
447 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
450 start = enc->nextfrag.fragment_offset;
451 end = start + mprev->m_pkthdr.len - 2*sizeof(uint32_t);
452 while (end == fstart) {
454 * Strip off the encap header from m and
455 * append it to mprev, freeing m.
457 m_adj(m, 2*sizeof(uint32_t));
458 mprev->m_nextpkt = m->m_nextpkt;
459 mprev->m_pkthdr.len += m->m_pkthdr.len;
462 if (mprev->m_pkthdr.len == dsize + 1 + 2*sizeof(uint32_t)) {
464 * We have assembled a complete packet
465 * we must be finished. Make sure we have
466 * merged the whole chain.
468 STAILQ_REMOVE(&fc->fc_frags, r, fw_reass, fr_link);
470 m = mprev->m_nextpkt;
476 mprev->m_nextpkt = NULL;
482 * See if we can continue merging forwards.
485 m = mprev->m_nextpkt;
487 enc = mtod(m, union fw_encap *);
488 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
491 fstart = enc->nextfrag.fragment_offset;
492 fend = fstart + m->m_pkthdr.len
493 - 2*sizeof(uint32_t);
506 while (r->fr_frags) {
508 r->fr_frags = mf->m_nextpkt;
518 firewire_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
520 struct fw_com *fc = IFP2FWC(ifp);
525 * The caller has already stripped off the packet header
526 * (stream or wreqb) and marked the mbuf's M_BCAST flag
527 * appropriately. We de-encapsulate the IP packet and pass it
528 * up the line after handling link-level fragmentation.
530 if (m->m_pkthdr.len < sizeof(uint32_t)) {
531 if_printf(ifp, "discarding frame without "
532 "encapsulation header (len %u pkt len %u)\n",
533 m->m_len, m->m_pkthdr.len);
536 m = m_pullup(m, sizeof(uint32_t));
539 enc = mtod(m, union fw_encap *);
542 * Byte swap the encapsulation header manually.
544 enc->ul[0] = ntohl(enc->ul[0]);
546 if (enc->unfrag.lf != 0) {
547 m = m_pullup(m, 2*sizeof(uint32_t));
550 enc = mtod(m, union fw_encap *);
551 enc->ul[1] = ntohl(enc->ul[1]);
552 m = firewire_input_fragment(fc, m, src);
555 enc = mtod(m, union fw_encap *);
556 type = enc->firstfrag.ether_type;
557 m_adj(m, 2*sizeof(uint32_t));
559 type = enc->unfrag.ether_type;
560 m_adj(m, sizeof(uint32_t));
563 if (m->m_pkthdr.rcvif == NULL) {
564 if_printf(ifp, "discard frame w/o interface pointer\n");
565 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
570 if (m->m_pkthdr.rcvif != ifp) {
571 if_printf(ifp, "Warning, frame marked as received on %s\n",
572 m->m_pkthdr.rcvif->if_xname);
578 * Tag the mbuf with an appropriate MAC label before any other
579 * consumers can get to it.
581 mac_ifnet_create_mbuf(ifp, m);
585 * Give bpf a chance at the packet. The link-level driver
586 * should have left us a tag with the EUID of the sender.
588 if (bpf_peers_present(ifp->if_bpf)) {
592 mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID, 0);
594 bcopy(mtag + 1, h.firewire_shost, 8);
596 bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
597 bcopy(&fc->fc_hwaddr, h.firewire_dhost, 8);
598 h.firewire_type = htons(type);
599 bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
602 if (ifp->if_flags & IFF_MONITOR) {
604 * Interface marked for monitoring; discard packet.
610 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
612 /* Discard packet if interface is not up */
613 if ((ifp->if_flags & IFF_UP) == 0) {
618 if (m->m_flags & (M_BCAST|M_MCAST))
619 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
630 ah = mtod(m, struct arphdr *);
633 * Adjust the arp packet to insert an empty tha slot.
635 m->m_len += ah->ar_hln;
636 m->m_pkthdr.len += ah->ar_hln;
637 bcopy(ar_tha(ah), ar_tpa(ah), ah->ar_pln);
654 M_SETFIB(m, ifp->if_fib);
655 CURVNET_SET_QUIET(ifp->if_vnet);
656 netisr_dispatch(isr, m);
661 firewire_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
663 struct ifaddr *ifa = (struct ifaddr *) data;
664 struct ifreq *ifr = (struct ifreq *) data;
669 ifp->if_flags |= IFF_UP;
671 switch (ifa->ifa_addr->sa_family) {
674 ifp->if_init(ifp->if_softc); /* before arpwhohas */
675 arp_ifinit(ifp, ifa);
679 ifp->if_init(ifp->if_softc);
685 bcopy(&IFP2FWC(ifp)->fc_hwaddr, &ifr->ifr_addr.sa_data[0],
686 sizeof(struct fw_hwaddr));
691 * Set the interface MTU.
693 if (ifr->ifr_mtu > 1500) {
696 ifp->if_mtu = ifr->ifr_mtu;
700 error = EINVAL; /* XXX netbsd has ENOTTY??? */
707 firewire_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
711 struct sockaddr_in *sin;
714 struct sockaddr_in6 *sin6;
717 switch(sa->sa_family) {
727 sin = (struct sockaddr_in *)sa;
728 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
729 return EADDRNOTAVAIL;
735 sin6 = (struct sockaddr_in6 *)sa;
736 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
738 * An IP6 address of 0 means listen to all
739 * of the Ethernet multicast address used for IP6.
740 * (This is used for multicast routers.)
742 ifp->if_flags |= IFF_ALLMULTI;
746 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
747 return EADDRNOTAVAIL;
754 * Well, the text isn't quite right, but it's the name
762 firewire_ifattach(struct ifnet *ifp, struct fw_hwaddr *llc)
764 struct fw_com *fc = IFP2FWC(ifp);
766 struct sockaddr_dl *sdl;
767 static const char* speeds[] = {
768 "S100", "S200", "S400", "S800",
772 fc->fc_speed = llc->sspd;
773 STAILQ_INIT(&fc->fc_frags);
775 ifp->if_addrlen = sizeof(struct fw_hwaddr);
778 ifp->if_mtu = 1500; /* XXX */
779 ifp->if_output = firewire_output;
780 ifp->if_resolvemulti = firewire_resolvemulti;
781 ifp->if_broadcastaddr = (u_char *) &firewire_broadcastaddr;
784 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
785 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
786 sdl->sdl_type = IFT_IEEE1394;
787 sdl->sdl_alen = ifp->if_addrlen;
788 bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
790 bpfattach(ifp, DLT_APPLE_IP_OVER_IEEE1394,
791 sizeof(struct fw_hwaddr));
793 if_printf(ifp, "Firewire address: %8D @ 0x%04x%08x, %s, maxrec %d\n",
794 (uint8_t *) &llc->sender_unique_ID_hi, ":",
795 ntohs(llc->sender_unicast_FIFO_hi),
796 ntohl(llc->sender_unicast_FIFO_lo),
798 (2 << llc->sender_max_rec));
802 firewire_ifdetach(struct ifnet *ifp)
809 firewire_busreset(struct ifnet *ifp)
811 struct fw_com *fc = IFP2FWC(ifp);
816 * Discard any partial datagrams since the host ids may have changed.
818 while ((r = STAILQ_FIRST(&fc->fc_frags))) {
819 STAILQ_REMOVE_HEAD(&fc->fc_frags, fr_link);
820 while (r->fr_frags) {
822 r->fr_frags = m->m_nextpkt;
830 firewire_alloc(u_char type, struct ifnet *ifp)
834 fc = malloc(sizeof(struct fw_com), M_FWCOM, M_WAITOK | M_ZERO);
841 firewire_free(void *com, u_char type)
848 firewire_modevent(module_t mod, int type, void *data)
853 if_register_com_alloc(IFT_IEEE1394,
854 firewire_alloc, firewire_free);
857 if_deregister_com_alloc(IFT_IEEE1394);
866 static moduledata_t firewire_mod = {
872 DECLARE_MODULE(if_firewire, firewire_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
873 MODULE_VERSION(if_firewire, 1);