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)
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);
140 switch (dst->sa_family) {
144 * Only bother with arp for unicast. Allocation of
145 * channels etc. for firewire is quite different and
146 * doesn't fit into the arp model.
149 error = arpresolve(ifp, is_gw, m, dst,
150 (u_char *) destfw, NULL, NULL);
152 return (error == EWOULDBLOCK ? 0 : error);
160 ah = mtod(m, struct arphdr *);
161 ah->ar_hrd = htons(ARPHRD_IEEE1394);
162 type = ETHERTYPE_ARP;
164 *destfw = *(struct fw_hwaddr *) ar_tha(ah);
167 * The standard arp code leaves a hole for the target
168 * hardware address which we need to close up.
170 bcopy(ar_tpa(ah), ar_tha(ah), ah->ar_pln);
171 m_adj(m, -ah->ar_hln);
179 error = nd6_resolve(fc->fc_ifp, is_gw, m, dst,
180 (u_char *) destfw, NULL, NULL);
182 return (error == EWOULDBLOCK ? 0 : error);
184 type = ETHERTYPE_IPV6;
189 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
190 error = EAFNOSUPPORT;
195 * Let BPF tap off a copy before we encapsulate.
197 if (bpf_peers_present(ifp->if_bpf)) {
200 bcopy(destfw, h.firewire_dhost, 8);
202 bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
203 bcopy(&fc->fc_hwaddr, h.firewire_shost, 8);
204 h.firewire_type = htons(type);
205 bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
209 * Punt on MCAP for now and send all multicast packets on the
212 if (m->m_flags & M_MCAST)
213 m->m_flags |= M_BCAST;
216 * Figure out what speed to use and what the largest supported
217 * packet size is. For unicast, this is the minimum of what we
218 * can speak and what they can hear. For broadcast, lets be
219 * conservative and use S100. We could possibly improve that
220 * by examining the bus manager's speed map or similar. We
221 * also reduce the packet size for broadcast to account for
225 speed = min(fc->fc_speed, destfw->sspd);
226 psize = min(512 << speed, 2 << destfw->sender_max_rec);
229 psize = 512 - 2*sizeof(uint32_t);
233 * Next, we encapsulate, possibly fragmenting the original
234 * datagram if it won't fit into a single packet.
236 if (m->m_pkthdr.len <= psize - sizeof(uint32_t)) {
238 * No fragmentation is necessary.
240 M_PREPEND(m, sizeof(uint32_t), M_NOWAIT);
245 enc = mtod(m, union fw_encap *);
246 enc->unfrag.ether_type = type;
247 enc->unfrag.lf = FW_ENCAP_UNFRAG;
248 enc->unfrag.reserved = 0;
251 * Byte swap the encapsulation header manually.
253 enc->ul[0] = htonl(enc->ul[0]);
255 error = (ifp->if_transmit)(ifp, m);
259 * Fragment the datagram, making sure to leave enough
260 * space for the encapsulation header in each packet.
262 fsize = psize - 2*sizeof(uint32_t);
264 dsize = m->m_pkthdr.len;
267 if (m->m_pkthdr.len > fsize) {
269 * Split off the tail segment from the
270 * datagram, copying our tags over.
272 mtail = m_split(m, fsize, M_NOWAIT);
273 m_tag_copy_chain(mtail, m, M_NOWAIT);
279 * Add our encapsulation header to this
280 * fragment and hand it off to the link.
282 M_PREPEND(m, 2*sizeof(uint32_t), M_NOWAIT);
287 enc = mtod(m, union fw_encap *);
289 enc->firstfrag.lf = FW_ENCAP_FIRST;
290 enc->firstfrag.reserved1 = 0;
291 enc->firstfrag.reserved2 = 0;
292 enc->firstfrag.datagram_size = dsize - 1;
293 enc->firstfrag.ether_type = type;
294 enc->firstfrag.dgl = dgl;
297 enc->nextfrag.lf = FW_ENCAP_NEXT;
299 enc->nextfrag.lf = FW_ENCAP_LAST;
300 enc->nextfrag.reserved1 = 0;
301 enc->nextfrag.reserved2 = 0;
302 enc->nextfrag.reserved3 = 0;
303 enc->nextfrag.datagram_size = dsize - 1;
304 enc->nextfrag.fragment_offset = foff;
305 enc->nextfrag.dgl = dgl;
307 foff += m->m_pkthdr.len - 2*sizeof(uint32_t);
310 * Byte swap the encapsulation header manually.
312 enc->ul[0] = htonl(enc->ul[0]);
313 enc->ul[1] = htonl(enc->ul[1]);
315 error = (ifp->if_transmit)(ifp, m);
335 firewire_input_fragment(struct fw_com *fc, struct mbuf *m, int src)
339 struct mbuf *mf, *mprev;
341 int fstart, fend, start, end, islast;
345 * Find an existing reassembly buffer or create a new one.
347 enc = mtod(m, union fw_encap *);
348 id = enc->firstfrag.dgl | (src << 16);
349 STAILQ_FOREACH(r, &fc->fc_frags, fr_link)
353 r = malloc(sizeof(struct fw_reass), M_TEMP, M_NOWAIT);
360 STAILQ_INSERT_HEAD(&fc->fc_frags, r, fr_link);
364 * If this fragment overlaps any other fragment, we must discard
365 * the partial reassembly and start again.
367 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
370 fstart = enc->nextfrag.fragment_offset;
371 fend = fstart + m->m_pkthdr.len - 2*sizeof(uint32_t);
372 dsize = enc->nextfrag.datagram_size;
373 islast = (enc->nextfrag.lf == FW_ENCAP_LAST);
375 for (mf = r->fr_frags; mf; mf = mf->m_nextpkt) {
376 enc = mtod(mf, union fw_encap *);
377 if (enc->nextfrag.datagram_size != dsize) {
379 * This fragment must be from a different
384 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
387 start = enc->nextfrag.fragment_offset;
388 end = start + mf->m_pkthdr.len - 2*sizeof(uint32_t);
389 if ((fstart < end && fend > start) ||
390 (islast && enc->nextfrag.lf == FW_ENCAP_LAST)) {
392 * Overlap - discard reassembly buffer and start
393 * again with this fragment.
400 * Find where to put this fragment in the list.
402 for (mf = r->fr_frags, mprev = NULL; mf;
403 mprev = mf, mf = mf->m_nextpkt) {
404 enc = mtod(mf, union fw_encap *);
405 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
408 start = enc->nextfrag.fragment_offset;
414 * If this is a last fragment and we are not adding at the end
415 * of the list, discard the buffer.
417 if (islast && mprev && mprev->m_nextpkt)
421 m->m_nextpkt = mprev->m_nextpkt;
422 mprev->m_nextpkt = m;
425 * Coalesce forwards and see if we can make a whole
428 enc = mtod(mprev, union fw_encap *);
429 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
432 start = enc->nextfrag.fragment_offset;
433 end = start + mprev->m_pkthdr.len - 2*sizeof(uint32_t);
434 while (end == fstart) {
436 * Strip off the encap header from m and
437 * append it to mprev, freeing m.
439 m_adj(m, 2*sizeof(uint32_t));
440 mprev->m_nextpkt = m->m_nextpkt;
441 mprev->m_pkthdr.len += m->m_pkthdr.len;
444 if (mprev->m_pkthdr.len == dsize + 1 + 2*sizeof(uint32_t)) {
446 * We have assembled a complete packet
447 * we must be finished. Make sure we have
448 * merged the whole chain.
450 STAILQ_REMOVE(&fc->fc_frags, r, fw_reass, fr_link);
452 m = mprev->m_nextpkt;
458 mprev->m_nextpkt = NULL;
464 * See if we can continue merging forwards.
467 m = mprev->m_nextpkt;
469 enc = mtod(m, union fw_encap *);
470 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
473 fstart = enc->nextfrag.fragment_offset;
474 fend = fstart + m->m_pkthdr.len
475 - 2*sizeof(uint32_t);
488 while (r->fr_frags) {
490 r->fr_frags = mf->m_nextpkt;
500 firewire_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
502 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));
521 enc = mtod(m, union fw_encap *);
524 * Byte swap the encapsulation header manually.
526 enc->ul[0] = ntohl(enc->ul[0]);
528 if (enc->unfrag.lf != 0) {
529 m = m_pullup(m, 2*sizeof(uint32_t));
532 enc = mtod(m, union fw_encap *);
533 enc->ul[1] = ntohl(enc->ul[1]);
534 m = firewire_input_fragment(fc, m, src);
537 enc = mtod(m, union fw_encap *);
538 type = enc->firstfrag.ether_type;
539 m_adj(m, 2*sizeof(uint32_t));
541 type = enc->unfrag.ether_type;
542 m_adj(m, sizeof(uint32_t));
545 if (m->m_pkthdr.rcvif == NULL) {
546 if_printf(ifp, "discard frame w/o interface pointer\n");
547 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
552 if (m->m_pkthdr.rcvif != ifp) {
553 if_printf(ifp, "Warning, frame marked as received on %s\n",
554 m->m_pkthdr.rcvif->if_xname);
560 * Tag the mbuf with an appropriate MAC label before any other
561 * consumers can get to it.
563 mac_ifnet_create_mbuf(ifp, m);
567 * Give bpf a chance at the packet. The link-level driver
568 * should have left us a tag with the EUID of the sender.
570 if (bpf_peers_present(ifp->if_bpf)) {
574 mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID, 0);
576 bcopy(mtag + 1, h.firewire_shost, 8);
578 bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
579 bcopy(&fc->fc_hwaddr, h.firewire_dhost, 8);
580 h.firewire_type = htons(type);
581 bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
584 if (ifp->if_flags & IFF_MONITOR) {
586 * Interface marked for monitoring; discard packet.
592 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
594 /* Discard packet if interface is not up */
595 if ((ifp->if_flags & IFF_UP) == 0) {
600 if (m->m_flags & (M_BCAST|M_MCAST))
601 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
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 M_SETFIB(m, ifp->if_fib);
637 netisr_dispatch(isr, m);
641 firewire_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
643 struct ifaddr *ifa = (struct ifaddr *) data;
644 struct ifreq *ifr = (struct ifreq *) data;
649 ifp->if_flags |= IFF_UP;
651 switch (ifa->ifa_addr->sa_family) {
654 ifp->if_init(ifp->if_softc); /* before arpwhohas */
655 arp_ifinit(ifp, ifa);
659 ifp->if_init(ifp->if_softc);
665 bcopy(&IFP2FWC(ifp)->fc_hwaddr, &ifr->ifr_addr.sa_data[0],
666 sizeof(struct fw_hwaddr));
671 * Set the interface MTU.
673 if (ifr->ifr_mtu > 1500) {
676 ifp->if_mtu = ifr->ifr_mtu;
680 error = EINVAL; /* XXX netbsd has ENOTTY??? */
687 firewire_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
691 struct sockaddr_in *sin;
694 struct sockaddr_in6 *sin6;
697 switch(sa->sa_family) {
707 sin = (struct sockaddr_in *)sa;
708 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
709 return EADDRNOTAVAIL;
715 sin6 = (struct sockaddr_in6 *)sa;
716 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
718 * An IP6 address of 0 means listen to all
719 * of the Ethernet multicast address used for IP6.
720 * (This is used for multicast routers.)
722 ifp->if_flags |= IFF_ALLMULTI;
726 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
727 return EADDRNOTAVAIL;
734 * Well, the text isn't quite right, but it's the name
742 firewire_ifattach(struct ifnet *ifp, struct fw_hwaddr *llc)
744 struct fw_com *fc = IFP2FWC(ifp);
746 struct sockaddr_dl *sdl;
747 static const char* speeds[] = {
748 "S100", "S200", "S400", "S800",
752 fc->fc_speed = llc->sspd;
753 STAILQ_INIT(&fc->fc_frags);
755 ifp->if_addrlen = sizeof(struct fw_hwaddr);
758 ifp->if_mtu = 1500; /* XXX */
759 ifp->if_output = firewire_output;
760 ifp->if_resolvemulti = firewire_resolvemulti;
761 ifp->if_broadcastaddr = (u_char *) &firewire_broadcastaddr;
764 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
765 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
766 sdl->sdl_type = IFT_IEEE1394;
767 sdl->sdl_alen = ifp->if_addrlen;
768 bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
770 bpfattach(ifp, DLT_APPLE_IP_OVER_IEEE1394,
771 sizeof(struct fw_hwaddr));
773 if_printf(ifp, "Firewire address: %8D @ 0x%04x%08x, %s, maxrec %d\n",
774 (uint8_t *) &llc->sender_unique_ID_hi, ":",
775 ntohs(llc->sender_unicast_FIFO_hi),
776 ntohl(llc->sender_unicast_FIFO_lo),
778 (2 << llc->sender_max_rec));
782 firewire_ifdetach(struct ifnet *ifp)
789 firewire_busreset(struct ifnet *ifp)
791 struct fw_com *fc = IFP2FWC(ifp);
796 * Discard any partial datagrams since the host ids may have changed.
798 while ((r = STAILQ_FIRST(&fc->fc_frags))) {
799 STAILQ_REMOVE_HEAD(&fc->fc_frags, fr_link);
800 while (r->fr_frags) {
802 r->fr_frags = m->m_nextpkt;
810 firewire_alloc(u_char type, struct ifnet *ifp)
814 fc = malloc(sizeof(struct fw_com), M_FWCOM, M_WAITOK | M_ZERO);
821 firewire_free(void *com, u_char type)
828 firewire_modevent(module_t mod, int type, void *data)
833 if_register_com_alloc(IFT_IEEE1394,
834 firewire_alloc, firewire_free);
837 if_deregister_com_alloc(IFT_IEEE1394);
846 static moduledata_t firewire_mod = {
852 DECLARE_MODULE(if_firewire, firewire_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
853 MODULE_VERSION(if_firewire, 1);