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"
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/netisr.h>
47 #include <net/route.h>
48 #include <net/if_llc.h>
49 #include <net/if_dl.h>
50 #include <net/if_types.h>
52 #include <net/firewire.h>
53 #include <net/if_llatbl.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);
85 struct fw_hwaddr *destfw;
87 uint16_t psize, fsize, dsize;
89 int unicast, dgl, foff;
91 #if defined(INET) || defined(INET6)
96 error = mac_ifnet_check_transmit(ifp, m);
101 if (!((ifp->if_flags & IFF_UP) &&
102 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
108 * For unicast, we make a tag to store the lladdr of the
109 * destination. This might not be the first time we have seen
110 * the packet (for instance, the arp code might be trying to
111 * re-send it after receiving an arp reply) so we only
112 * allocate a tag if there isn't one there already. For
113 * multicast, we will eventually use a different tag to store
114 * the channel number.
116 unicast = !(m->m_flags & (M_BCAST | M_MCAST));
118 mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, NULL);
120 mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR,
121 sizeof (struct fw_hwaddr), M_NOWAIT);
126 m_tag_prepend(m, mtag);
128 destfw = (struct fw_hwaddr *)(mtag + 1);
133 switch (dst->sa_family) {
137 * Only bother with arp for unicast. Allocation of
138 * channels etc. for firewire is quite different and
139 * doesn't fit into the arp model.
142 error = arpresolve(ifp, ro ? ro->ro_rt : NULL, m, dst, (u_char *) destfw, &lle);
144 return (error == EWOULDBLOCK ? 0 : error);
152 ah = mtod(m, struct arphdr *);
153 ah->ar_hrd = htons(ARPHRD_IEEE1394);
154 type = ETHERTYPE_ARP;
156 *destfw = *(struct fw_hwaddr *) ar_tha(ah);
159 * The standard arp code leaves a hole for the target
160 * hardware address which we need to close up.
162 bcopy(ar_tpa(ah), ar_tha(ah), ah->ar_pln);
163 m_adj(m, -ah->ar_hln);
171 error = nd6_storelladdr(fc->fc_ifp, m, dst,
172 (u_char *) destfw, &lle);
176 type = ETHERTYPE_IPV6;
181 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
182 error = EAFNOSUPPORT;
187 * Let BPF tap off a copy before we encapsulate.
189 if (bpf_peers_present(ifp->if_bpf)) {
192 bcopy(destfw, h.firewire_dhost, 8);
194 bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
195 bcopy(&fc->fc_hwaddr, h.firewire_shost, 8);
196 h.firewire_type = htons(type);
197 bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
201 * Punt on MCAP for now and send all multicast packets on the
204 if (m->m_flags & M_MCAST)
205 m->m_flags |= M_BCAST;
208 * Figure out what speed to use and what the largest supported
209 * packet size is. For unicast, this is the minimum of what we
210 * can speak and what they can hear. For broadcast, lets be
211 * conservative and use S100. We could possibly improve that
212 * by examining the bus manager's speed map or similar. We
213 * also reduce the packet size for broadcast to account for
217 speed = min(fc->fc_speed, destfw->sspd);
218 psize = min(512 << speed, 2 << destfw->sender_max_rec);
221 psize = 512 - 2*sizeof(uint32_t);
225 * Next, we encapsulate, possibly fragmenting the original
226 * datagram if it won't fit into a single packet.
228 if (m->m_pkthdr.len <= psize - sizeof(uint32_t)) {
230 * No fragmentation is necessary.
232 M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
237 enc = mtod(m, union fw_encap *);
238 enc->unfrag.ether_type = type;
239 enc->unfrag.lf = FW_ENCAP_UNFRAG;
240 enc->unfrag.reserved = 0;
243 * Byte swap the encapsulation header manually.
245 enc->ul[0] = htonl(enc->ul[0]);
247 error = (ifp->if_transmit)(ifp, m);
251 * Fragment the datagram, making sure to leave enough
252 * space for the encapsulation header in each packet.
254 fsize = psize - 2*sizeof(uint32_t);
256 dsize = m->m_pkthdr.len;
259 if (m->m_pkthdr.len > fsize) {
261 * Split off the tail segment from the
262 * datagram, copying our tags over.
264 mtail = m_split(m, fsize, M_DONTWAIT);
265 m_tag_copy_chain(mtail, m, M_NOWAIT);
271 * Add our encapsulation header to this
272 * fragment and hand it off to the link.
274 M_PREPEND(m, 2*sizeof(uint32_t), M_DONTWAIT);
279 enc = mtod(m, union fw_encap *);
281 enc->firstfrag.lf = FW_ENCAP_FIRST;
282 enc->firstfrag.reserved1 = 0;
283 enc->firstfrag.reserved2 = 0;
284 enc->firstfrag.datagram_size = dsize - 1;
285 enc->firstfrag.ether_type = type;
286 enc->firstfrag.dgl = dgl;
289 enc->nextfrag.lf = FW_ENCAP_NEXT;
291 enc->nextfrag.lf = FW_ENCAP_LAST;
292 enc->nextfrag.reserved1 = 0;
293 enc->nextfrag.reserved2 = 0;
294 enc->nextfrag.reserved3 = 0;
295 enc->nextfrag.datagram_size = dsize - 1;
296 enc->nextfrag.fragment_offset = foff;
297 enc->nextfrag.dgl = dgl;
299 foff += m->m_pkthdr.len - 2*sizeof(uint32_t);
302 * Byte swap the encapsulation header manually.
304 enc->ul[0] = htonl(enc->ul[0]);
305 enc->ul[1] = htonl(enc->ul[1]);
307 error = (ifp->if_transmit)(ifp, m);
327 firewire_input_fragment(struct fw_com *fc, struct mbuf *m, int src)
331 struct mbuf *mf, *mprev;
333 int fstart, fend, start, end, islast;
337 * Find an existing reassembly buffer or create a new one.
339 enc = mtod(m, union fw_encap *);
340 id = enc->firstfrag.dgl | (src << 16);
341 STAILQ_FOREACH(r, &fc->fc_frags, fr_link)
345 r = malloc(sizeof(struct fw_reass), M_TEMP, M_NOWAIT);
352 STAILQ_INSERT_HEAD(&fc->fc_frags, r, fr_link);
356 * If this fragment overlaps any other fragment, we must discard
357 * the partial reassembly and start again.
359 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
362 fstart = enc->nextfrag.fragment_offset;
363 fend = fstart + m->m_pkthdr.len - 2*sizeof(uint32_t);
364 dsize = enc->nextfrag.datagram_size;
365 islast = (enc->nextfrag.lf == FW_ENCAP_LAST);
367 for (mf = r->fr_frags; mf; mf = mf->m_nextpkt) {
368 enc = mtod(mf, union fw_encap *);
369 if (enc->nextfrag.datagram_size != dsize) {
371 * This fragment must be from a different
376 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
379 start = enc->nextfrag.fragment_offset;
380 end = start + mf->m_pkthdr.len - 2*sizeof(uint32_t);
381 if ((fstart < end && fend > start) ||
382 (islast && enc->nextfrag.lf == FW_ENCAP_LAST)) {
384 * Overlap - discard reassembly buffer and start
385 * again with this fragment.
392 * Find where to put this fragment in the list.
394 for (mf = r->fr_frags, mprev = NULL; mf;
395 mprev = mf, mf = mf->m_nextpkt) {
396 enc = mtod(mf, union fw_encap *);
397 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
400 start = enc->nextfrag.fragment_offset;
406 * If this is a last fragment and we are not adding at the end
407 * of the list, discard the buffer.
409 if (islast && mprev && mprev->m_nextpkt)
413 m->m_nextpkt = mprev->m_nextpkt;
414 mprev->m_nextpkt = m;
417 * Coalesce forwards and see if we can make a whole
420 enc = mtod(mprev, union fw_encap *);
421 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
424 start = enc->nextfrag.fragment_offset;
425 end = start + mprev->m_pkthdr.len - 2*sizeof(uint32_t);
426 while (end == fstart) {
428 * Strip off the encap header from m and
429 * append it to mprev, freeing m.
431 m_adj(m, 2*sizeof(uint32_t));
432 mprev->m_nextpkt = m->m_nextpkt;
433 mprev->m_pkthdr.len += m->m_pkthdr.len;
436 if (mprev->m_pkthdr.len == dsize + 1 + 2*sizeof(uint32_t)) {
438 * We have assembled a complete packet
439 * we must be finished. Make sure we have
440 * merged the whole chain.
442 STAILQ_REMOVE(&fc->fc_frags, r, fw_reass, fr_link);
444 m = mprev->m_nextpkt;
450 mprev->m_nextpkt = NULL;
456 * See if we can continue merging forwards.
459 m = mprev->m_nextpkt;
461 enc = mtod(m, union fw_encap *);
462 if (enc->firstfrag.lf == FW_ENCAP_FIRST)
465 fstart = enc->nextfrag.fragment_offset;
466 fend = fstart + m->m_pkthdr.len
467 - 2*sizeof(uint32_t);
480 while (r->fr_frags) {
482 r->fr_frags = mf->m_nextpkt;
492 firewire_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
494 struct fw_com *fc = IFP2FWC(ifp);
499 * The caller has already stripped off the packet header
500 * (stream or wreqb) and marked the mbuf's M_BCAST flag
501 * appropriately. We de-encapsulate the IP packet and pass it
502 * up the line after handling link-level fragmentation.
504 if (m->m_pkthdr.len < sizeof(uint32_t)) {
505 if_printf(ifp, "discarding frame without "
506 "encapsulation header (len %u pkt len %u)\n",
507 m->m_len, m->m_pkthdr.len);
510 m = m_pullup(m, sizeof(uint32_t));
513 enc = mtod(m, union fw_encap *);
516 * Byte swap the encapsulation header manually.
518 enc->ul[0] = ntohl(enc->ul[0]);
520 if (enc->unfrag.lf != 0) {
521 m = m_pullup(m, 2*sizeof(uint32_t));
524 enc = mtod(m, union fw_encap *);
525 enc->ul[1] = ntohl(enc->ul[1]);
526 m = firewire_input_fragment(fc, m, src);
529 enc = mtod(m, union fw_encap *);
530 type = enc->firstfrag.ether_type;
531 m_adj(m, 2*sizeof(uint32_t));
533 type = enc->unfrag.ether_type;
534 m_adj(m, sizeof(uint32_t));
537 if (m->m_pkthdr.rcvif == NULL) {
538 if_printf(ifp, "discard frame w/o interface pointer\n");
544 if (m->m_pkthdr.rcvif != ifp) {
545 if_printf(ifp, "Warning, frame marked as received on %s\n",
546 m->m_pkthdr.rcvif->if_xname);
552 * Tag the mbuf with an appropriate MAC label before any other
553 * consumers can get to it.
555 mac_ifnet_create_mbuf(ifp, m);
559 * Give bpf a chance at the packet. The link-level driver
560 * should have left us a tag with the EUID of the sender.
562 if (bpf_peers_present(ifp->if_bpf)) {
566 mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID, 0);
568 bcopy(mtag + 1, h.firewire_shost, 8);
570 bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
571 bcopy(&fc->fc_hwaddr, h.firewire_dhost, 8);
572 h.firewire_type = htons(type);
573 bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
576 if (ifp->if_flags & IFF_MONITOR) {
578 * Interface marked for monitoring; discard packet.
584 ifp->if_ibytes += m->m_pkthdr.len;
586 /* Discard packet if interface is not up */
587 if ((ifp->if_flags & IFF_UP) == 0) {
592 if (m->m_flags & (M_BCAST|M_MCAST))
598 if ((m = ip_fastforward(m)) == NULL)
606 ah = mtod(m, struct arphdr *);
609 * Adjust the arp packet to insert an empty tha slot.
611 m->m_len += ah->ar_hln;
612 m->m_pkthdr.len += ah->ar_hln;
613 bcopy(ar_tha(ah), ar_tpa(ah), ah->ar_pln);
630 M_SETFIB(m, ifp->if_fib);
631 netisr_dispatch(isr, m);
635 firewire_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
637 struct ifaddr *ifa = (struct ifaddr *) data;
638 struct ifreq *ifr = (struct ifreq *) data;
643 ifp->if_flags |= IFF_UP;
645 switch (ifa->ifa_addr->sa_family) {
648 ifp->if_init(ifp->if_softc); /* before arpwhohas */
649 arp_ifinit(ifp, ifa);
653 ifp->if_init(ifp->if_softc);
662 sa = (struct sockaddr *) & ifr->ifr_data;
663 bcopy(&IFP2FWC(ifp)->fc_hwaddr,
664 (caddr_t) sa->sa_data, sizeof(struct fw_hwaddr));
670 * Set the interface MTU.
672 if (ifr->ifr_mtu > 1500) {
675 ifp->if_mtu = ifr->ifr_mtu;
679 error = EINVAL; /* XXX netbsd has ENOTTY??? */
686 firewire_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
690 struct sockaddr_in *sin;
693 struct sockaddr_in6 *sin6;
696 switch(sa->sa_family) {
706 sin = (struct sockaddr_in *)sa;
707 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
708 return EADDRNOTAVAIL;
714 sin6 = (struct sockaddr_in6 *)sa;
715 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
717 * An IP6 address of 0 means listen to all
718 * of the Ethernet multicast address used for IP6.
719 * (This is used for multicast routers.)
721 ifp->if_flags |= IFF_ALLMULTI;
725 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
726 return EADDRNOTAVAIL;
733 * Well, the text isn't quite right, but it's the name
741 firewire_ifattach(struct ifnet *ifp, struct fw_hwaddr *llc)
743 struct fw_com *fc = IFP2FWC(ifp);
745 struct sockaddr_dl *sdl;
746 static const char* speeds[] = {
747 "S100", "S200", "S400", "S800",
751 fc->fc_speed = llc->sspd;
752 STAILQ_INIT(&fc->fc_frags);
754 ifp->if_addrlen = sizeof(struct fw_hwaddr);
757 ifp->if_mtu = 1500; /* XXX */
758 ifp->if_output = firewire_output;
759 ifp->if_resolvemulti = firewire_resolvemulti;
760 ifp->if_broadcastaddr = (u_char *) &firewire_broadcastaddr;
763 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
764 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
765 sdl->sdl_type = IFT_IEEE1394;
766 sdl->sdl_alen = ifp->if_addrlen;
767 bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
769 bpfattach(ifp, DLT_APPLE_IP_OVER_IEEE1394,
770 sizeof(struct fw_hwaddr));
772 if_printf(ifp, "Firewire address: %8D @ 0x%04x%08x, %s, maxrec %d\n",
773 (uint8_t *) &llc->sender_unique_ID_hi, ":",
774 ntohs(llc->sender_unicast_FIFO_hi),
775 ntohl(llc->sender_unicast_FIFO_lo),
777 (2 << llc->sender_max_rec));
781 firewire_ifdetach(struct ifnet *ifp)
788 firewire_busreset(struct ifnet *ifp)
790 struct fw_com *fc = IFP2FWC(ifp);
795 * Discard any partial datagrams since the host ids may have changed.
797 while ((r = STAILQ_FIRST(&fc->fc_frags))) {
798 STAILQ_REMOVE_HEAD(&fc->fc_frags, fr_link);
799 while (r->fr_frags) {
801 r->fr_frags = m->m_nextpkt;
809 firewire_alloc(u_char type, struct ifnet *ifp)
813 fc = malloc(sizeof(struct fw_com), M_FWCOM, M_WAITOK | M_ZERO);
820 firewire_free(void *com, u_char type)
827 firewire_modevent(module_t mod, int type, void *data)
832 if_register_com_alloc(IFT_IEEE1394,
833 firewire_alloc, firewire_free);
836 if_deregister_com_alloc(IFT_IEEE1394);
845 static moduledata_t firewire_mod = {
851 DECLARE_MODULE(if_firewire, firewire_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
852 MODULE_VERSION(if_firewire, 1);