xref: /freebsd-13-stable/sys/net/if_fwsubr.c (revision f8167e0404dab9ffeaca95853dd237ab7c587f82)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2004 Doug Rabson
5  * Copyright (c) 1982, 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32 
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/module.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/netisr.h>
48 #include <net/route.h>
49 #include <net/if_llc.h>
50 #include <net/if_dl.h>
51 #include <net/if_types.h>
52 #include <net/bpf.h>
53 #include <net/firewire.h>
54 #include <net/if_llatbl.h>
55 
56 #if defined(INET) || defined(INET6)
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
59 #include <netinet/if_ether.h>
60 #endif
61 #ifdef INET6
62 #include <netinet6/nd6.h>
63 #endif
64 
65 #include <security/mac/mac_framework.h>
66 
67 static MALLOC_DEFINE(M_FWCOM, "fw_com", "firewire interface internals");
68 
69 struct fw_hwaddr firewire_broadcastaddr = {
70 	0xffffffff,
71 	0xffffffff,
72 	0xff,
73 	0xff,
74 	0xffff,
75 	0xffffffff
76 };
77 
78 static int
firewire_output(struct ifnet * ifp,struct mbuf * m,const struct sockaddr * dst,struct route * ro)79 firewire_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
80     struct route *ro)
81 {
82 	struct fw_com *fc = IFP2FWC(ifp);
83 	int error, type;
84 	struct m_tag *mtag;
85 	union fw_encap *enc;
86 	struct fw_hwaddr *destfw;
87 	uint8_t speed;
88 	uint16_t psize, fsize, dsize;
89 	struct mbuf *mtail;
90 	int unicast, dgl, foff;
91 	static int next_dgl;
92 #if defined(INET) || defined(INET6)
93 	int is_gw = 0;
94 #endif
95 	int af = RO_GET_FAMILY(ro, dst);
96 
97 #ifdef MAC
98 	error = mac_ifnet_check_transmit(ifp, m);
99 	if (error)
100 		goto bad;
101 #endif
102 
103 	if (!((ifp->if_flags & IFF_UP) &&
104 	   (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
105 		error = ENETDOWN;
106 		goto bad;
107 	}
108 
109 #if defined(INET) || defined(INET6)
110 	if (ro != NULL)
111 		is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
112 #endif
113 	/*
114 	 * For unicast, we make a tag to store the lladdr of the
115 	 * destination. This might not be the first time we have seen
116 	 * the packet (for instance, the arp code might be trying to
117 	 * re-send it after receiving an arp reply) so we only
118 	 * allocate a tag if there isn't one there already. For
119 	 * multicast, we will eventually use a different tag to store
120 	 * the channel number.
121 	 */
122 	unicast = !(m->m_flags & (M_BCAST | M_MCAST));
123 	if (unicast) {
124 		mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, NULL);
125 		if (!mtag) {
126 			mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR,
127 			    sizeof (struct fw_hwaddr), M_NOWAIT);
128 			if (!mtag) {
129 				error = ENOMEM;
130 				goto bad;
131 			}
132 			m_tag_prepend(m, mtag);
133 		}
134 		destfw = (struct fw_hwaddr *)(mtag + 1);
135 	} else {
136 		destfw = NULL;
137 	}
138 
139 	switch (af) {
140 #ifdef INET
141 	case AF_INET:
142 		type = ETHERTYPE_IP;
143 		break;
144 	case AF_ARP:
145 		type = ETHERTYPE_ARP;
146 		break;
147 #endif
148 #ifdef INET6
149 	case AF_INET6:
150 		type = ETHERTYPE_IPV6;
151 		break;
152 #endif
153 	default:
154 		if_printf(ifp, "can't handle af%d\n", af);
155 		error = EAFNOSUPPORT;
156 		goto bad;
157 	}
158 
159 	switch (dst->sa_family) {
160 #ifdef INET
161 	case AF_INET:
162 		/*
163 		 * Only bother with arp for unicast. Allocation of
164 		 * channels etc. for firewire is quite different and
165 		 * doesn't fit into the arp model.
166 		 */
167 		if (unicast) {
168 			error = arpresolve(ifp, is_gw, m, dst,
169 			    (u_char *) destfw, NULL, NULL);
170 			if (error)
171 				return (error == EWOULDBLOCK ? 0 : error);
172 		}
173 		break;
174 
175 	case AF_ARP:
176 	{
177 		struct arphdr *ah;
178 		ah = mtod(m, struct arphdr *);
179 		ah->ar_hrd = htons(ARPHRD_IEEE1394);
180 		if (unicast)
181 			*destfw = *(struct fw_hwaddr *) ar_tha(ah);
182 
183 		/*
184 		 * The standard arp code leaves a hole for the target
185 		 * hardware address which we need to close up.
186 		 */
187 		bcopy(ar_tpa(ah), ar_tha(ah), ah->ar_pln);
188 		m_adj(m, -ah->ar_hln);
189 		break;
190 	}
191 #endif
192 
193 #ifdef INET6
194 	case AF_INET6:
195 		if (unicast) {
196 			error = nd6_resolve(fc->fc_ifp, LLE_SF(af, is_gw), m,
197 			    dst, (u_char *) destfw, NULL, NULL);
198 			if (error)
199 				return (error == EWOULDBLOCK ? 0 : error);
200 		}
201 		break;
202 #endif
203 
204 	default:
205 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
206 		error = EAFNOSUPPORT;
207 		goto bad;
208 	}
209 
210 	/*
211 	 * Let BPF tap off a copy before we encapsulate.
212 	 */
213 	if (bpf_peers_present(ifp->if_bpf)) {
214 		struct fw_bpfhdr h;
215 		if (unicast)
216 			bcopy(destfw, h.firewire_dhost, 8);
217 		else
218 			bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
219 		bcopy(&fc->fc_hwaddr, h.firewire_shost, 8);
220 		h.firewire_type = htons(type);
221 		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
222 	}
223 
224 	/*
225 	 * Punt on MCAP for now and send all multicast packets on the
226 	 * broadcast channel.
227 	 */
228 	if (m->m_flags & M_MCAST)
229 		m->m_flags |= M_BCAST;
230 
231 	/*
232 	 * Figure out what speed to use and what the largest supported
233 	 * packet size is. For unicast, this is the minimum of what we
234 	 * can speak and what they can hear. For broadcast, lets be
235 	 * conservative and use S100. We could possibly improve that
236 	 * by examining the bus manager's speed map or similar. We
237 	 * also reduce the packet size for broadcast to account for
238 	 * the GASP header.
239 	 */
240 	if (unicast) {
241 		speed = min(fc->fc_speed, destfw->sspd);
242 		psize = min(512 << speed, 2 << destfw->sender_max_rec);
243 	} else {
244 		speed = 0;
245 		psize = 512 - 2*sizeof(uint32_t);
246 	}
247 
248 	/*
249 	 * Next, we encapsulate, possibly fragmenting the original
250 	 * datagram if it won't fit into a single packet.
251 	 */
252 	if (m->m_pkthdr.len <= psize - sizeof(uint32_t)) {
253 		/*
254 		 * No fragmentation is necessary.
255 		 */
256 		M_PREPEND(m, sizeof(uint32_t), M_NOWAIT);
257 		if (!m) {
258 			error = ENOBUFS;
259 			goto bad;
260 		}
261 		enc = mtod(m, union fw_encap *);
262 		enc->unfrag.ether_type = type;
263 		enc->unfrag.lf = FW_ENCAP_UNFRAG;
264 		enc->unfrag.reserved = 0;
265 
266 		/*
267 		 * Byte swap the encapsulation header manually.
268 		 */
269 		enc->ul[0] = htonl(enc->ul[0]);
270 
271 		error = (ifp->if_transmit)(ifp, m);
272 		return (error);
273 	} else {
274 		/*
275 		 * Fragment the datagram, making sure to leave enough
276 		 * space for the encapsulation header in each packet.
277 		 */
278 		fsize = psize - 2*sizeof(uint32_t);
279 		dgl = next_dgl++;
280 		dsize = m->m_pkthdr.len;
281 		foff = 0;
282 		while (m) {
283 			if (m->m_pkthdr.len > fsize) {
284 				/*
285 				 * Split off the tail segment from the
286 				 * datagram, copying our tags over.
287 				 */
288 				mtail = m_split(m, fsize, M_NOWAIT);
289 				m_tag_copy_chain(mtail, m, M_NOWAIT);
290 			} else {
291 				mtail = NULL;
292 			}
293 
294 			/*
295 			 * Add our encapsulation header to this
296 			 * fragment and hand it off to the link.
297 			 */
298 			M_PREPEND(m, 2*sizeof(uint32_t), M_NOWAIT);
299 			if (!m) {
300 				error = ENOBUFS;
301 				goto bad;
302 			}
303 			enc = mtod(m, union fw_encap *);
304 			if (foff == 0) {
305 				enc->firstfrag.lf = FW_ENCAP_FIRST;
306 				enc->firstfrag.reserved1 = 0;
307 				enc->firstfrag.reserved2 = 0;
308 				enc->firstfrag.datagram_size = dsize - 1;
309 				enc->firstfrag.ether_type = type;
310 				enc->firstfrag.dgl = dgl;
311 			} else {
312 				if (mtail)
313 					enc->nextfrag.lf = FW_ENCAP_NEXT;
314 				else
315 					enc->nextfrag.lf = FW_ENCAP_LAST;
316 				enc->nextfrag.reserved1 = 0;
317 				enc->nextfrag.reserved2 = 0;
318 				enc->nextfrag.reserved3 = 0;
319 				enc->nextfrag.datagram_size = dsize - 1;
320 				enc->nextfrag.fragment_offset = foff;
321 				enc->nextfrag.dgl = dgl;
322 			}
323 			foff += m->m_pkthdr.len - 2*sizeof(uint32_t);
324 
325 			/*
326 			 * Byte swap the encapsulation header manually.
327 			 */
328 			enc->ul[0] = htonl(enc->ul[0]);
329 			enc->ul[1] = htonl(enc->ul[1]);
330 
331 			error = (ifp->if_transmit)(ifp, m);
332 			if (error) {
333 				if (mtail)
334 					m_freem(mtail);
335 				return (ENOBUFS);
336 			}
337 
338 			m = mtail;
339 		}
340 
341 		return (0);
342 	}
343 
344 bad:
345 	if (m)
346 		m_freem(m);
347 	return (error);
348 }
349 
350 static struct mbuf *
firewire_input_fragment(struct fw_com * fc,struct mbuf * m,int src)351 firewire_input_fragment(struct fw_com *fc, struct mbuf *m, int src)
352 {
353 	union fw_encap *enc;
354 	struct fw_reass *r;
355 	struct mbuf *mf, *mprev;
356 	int dsize;
357 	int fstart, fend, start, end, islast;
358 	uint32_t id;
359 
360 	/*
361 	 * Find an existing reassembly buffer or create a new one.
362 	 */
363 	enc = mtod(m, union fw_encap *);
364 	id = enc->firstfrag.dgl | (src << 16);
365 	STAILQ_FOREACH(r, &fc->fc_frags, fr_link)
366 		if (r->fr_id == id)
367 			break;
368 	if (!r) {
369 		r = malloc(sizeof(struct fw_reass), M_TEMP, M_NOWAIT);
370 		if (!r) {
371 			m_freem(m);
372 			return 0;
373 		}
374 		r->fr_id = id;
375 		r->fr_frags = 0;
376 		STAILQ_INSERT_HEAD(&fc->fc_frags, r, fr_link);
377 	}
378 
379 	/*
380 	 * If this fragment overlaps any other fragment, we must discard
381 	 * the partial reassembly and start again.
382 	 */
383 	if (enc->firstfrag.lf == FW_ENCAP_FIRST)
384 		fstart = 0;
385 	else
386 		fstart = enc->nextfrag.fragment_offset;
387 	fend = fstart + m->m_pkthdr.len - 2*sizeof(uint32_t);
388 	dsize = enc->nextfrag.datagram_size;
389 	islast = (enc->nextfrag.lf == FW_ENCAP_LAST);
390 
391 	for (mf = r->fr_frags; mf; mf = mf->m_nextpkt) {
392 		enc = mtod(mf, union fw_encap *);
393 		if (enc->nextfrag.datagram_size != dsize) {
394 			/*
395 			 * This fragment must be from a different
396 			 * packet.
397 			 */
398 			goto bad;
399 		}
400 		if (enc->firstfrag.lf == FW_ENCAP_FIRST)
401 			start = 0;
402 		else
403 			start = enc->nextfrag.fragment_offset;
404 		end = start + mf->m_pkthdr.len - 2*sizeof(uint32_t);
405 		if ((fstart < end && fend > start) ||
406 		    (islast && enc->nextfrag.lf == FW_ENCAP_LAST)) {
407 			/*
408 			 * Overlap - discard reassembly buffer and start
409 			 * again with this fragment.
410 			 */
411 			goto bad;
412 		}
413 	}
414 
415 	/*
416 	 * Find where to put this fragment in the list.
417 	 */
418 	for (mf = r->fr_frags, mprev = NULL; mf;
419 	    mprev = mf, mf = mf->m_nextpkt) {
420 		enc = mtod(mf, union fw_encap *);
421 		if (enc->firstfrag.lf == FW_ENCAP_FIRST)
422 			start = 0;
423 		else
424 			start = enc->nextfrag.fragment_offset;
425 		if (start >= fend)
426 			break;
427 	}
428 
429 	/*
430 	 * If this is a last fragment and we are not adding at the end
431 	 * of the list, discard the buffer.
432 	 */
433 	if (islast && mprev && mprev->m_nextpkt)
434 		goto bad;
435 
436 	if (mprev) {
437 		m->m_nextpkt = mprev->m_nextpkt;
438 		mprev->m_nextpkt = m;
439 
440 		/*
441 		 * Coalesce forwards and see if we can make a whole
442 		 * datagram.
443 		 */
444 		enc = mtod(mprev, union fw_encap *);
445 		if (enc->firstfrag.lf == FW_ENCAP_FIRST)
446 			start = 0;
447 		else
448 			start = enc->nextfrag.fragment_offset;
449 		end = start + mprev->m_pkthdr.len - 2*sizeof(uint32_t);
450 		while (end == fstart) {
451 			/*
452 			 * Strip off the encap header from m and
453 			 * append it to mprev, freeing m.
454 			 */
455 			m_adj(m, 2*sizeof(uint32_t));
456 			mprev->m_nextpkt = m->m_nextpkt;
457 			mprev->m_pkthdr.len += m->m_pkthdr.len;
458 			m_cat(mprev, m);
459 
460 			if (mprev->m_pkthdr.len == dsize + 1 + 2*sizeof(uint32_t)) {
461 				/*
462 				 * We have assembled a complete packet
463 				 * we must be finished. Make sure we have
464 				 * merged the whole chain.
465 				 */
466 				STAILQ_REMOVE(&fc->fc_frags, r, fw_reass, fr_link);
467 				free(r, M_TEMP);
468 				m = mprev->m_nextpkt;
469 				while (m) {
470 					mf = m->m_nextpkt;
471 					m_freem(m);
472 					m = mf;
473 				}
474 				mprev->m_nextpkt = NULL;
475 
476 				return (mprev);
477 			}
478 
479 			/*
480 			 * See if we can continue merging forwards.
481 			 */
482 			end = fend;
483 			m = mprev->m_nextpkt;
484 			if (m) {
485 				enc = mtod(m, union fw_encap *);
486 				if (enc->firstfrag.lf == FW_ENCAP_FIRST)
487 					fstart = 0;
488 				else
489 					fstart = enc->nextfrag.fragment_offset;
490 				fend = fstart + m->m_pkthdr.len
491 				    - 2*sizeof(uint32_t);
492 			} else {
493 				break;
494 			}
495 		}
496 	} else {
497 		m->m_nextpkt = 0;
498 		r->fr_frags = m;
499 	}
500 
501 	return (0);
502 
503 bad:
504 	while (r->fr_frags) {
505 		mf = r->fr_frags;
506 		r->fr_frags = mf->m_nextpkt;
507 		m_freem(mf);
508 	}
509 	m->m_nextpkt = 0;
510 	r->fr_frags = m;
511 
512 	return (0);
513 }
514 
515 void
firewire_input(struct ifnet * ifp,struct mbuf * m,uint16_t src)516 firewire_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
517 {
518 	struct fw_com *fc = IFP2FWC(ifp);
519 	union fw_encap *enc;
520 	int type, isr;
521 
522 	/*
523 	 * The caller has already stripped off the packet header
524 	 * (stream or wreqb) and marked the mbuf's M_BCAST flag
525 	 * appropriately. We de-encapsulate the IP packet and pass it
526 	 * up the line after handling link-level fragmentation.
527 	 */
528 	if (m->m_pkthdr.len < sizeof(uint32_t)) {
529 		if_printf(ifp, "discarding frame without "
530 		    "encapsulation header (len %u pkt len %u)\n",
531 		    m->m_len, m->m_pkthdr.len);
532 	}
533 
534 	m = m_pullup(m, sizeof(uint32_t));
535 	if (m == NULL)
536 		return;
537 	enc = mtod(m, union fw_encap *);
538 
539 	/*
540 	 * Byte swap the encapsulation header manually.
541 	 */
542 	enc->ul[0] = ntohl(enc->ul[0]);
543 
544 	if (enc->unfrag.lf != 0) {
545 		m = m_pullup(m, 2*sizeof(uint32_t));
546 		if (!m)
547 			return;
548 		enc = mtod(m, union fw_encap *);
549 		enc->ul[1] = ntohl(enc->ul[1]);
550 		m = firewire_input_fragment(fc, m, src);
551 		if (!m)
552 			return;
553 		enc = mtod(m, union fw_encap *);
554 		type = enc->firstfrag.ether_type;
555 		m_adj(m, 2*sizeof(uint32_t));
556 	} else {
557 		type = enc->unfrag.ether_type;
558 		m_adj(m, sizeof(uint32_t));
559 	}
560 
561 	if (m->m_pkthdr.rcvif == NULL) {
562 		if_printf(ifp, "discard frame w/o interface pointer\n");
563 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
564 		m_freem(m);
565 		return;
566 	}
567 #ifdef DIAGNOSTIC
568 	if (m->m_pkthdr.rcvif != ifp) {
569 		if_printf(ifp, "Warning, frame marked as received on %s\n",
570 			m->m_pkthdr.rcvif->if_xname);
571 	}
572 #endif
573 
574 #ifdef MAC
575 	/*
576 	 * Tag the mbuf with an appropriate MAC label before any other
577 	 * consumers can get to it.
578 	 */
579 	mac_ifnet_create_mbuf(ifp, m);
580 #endif
581 
582 	/*
583 	 * Give bpf a chance at the packet. The link-level driver
584 	 * should have left us a tag with the EUID of the sender.
585 	 */
586 	if (bpf_peers_present(ifp->if_bpf)) {
587 		struct fw_bpfhdr h;
588 		struct m_tag *mtag;
589 
590 		mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID, 0);
591 		if (mtag)
592 			bcopy(mtag + 1, h.firewire_shost, 8);
593 		else
594 			bcopy(&firewire_broadcastaddr, h.firewire_dhost, 8);
595 		bcopy(&fc->fc_hwaddr, h.firewire_dhost, 8);
596 		h.firewire_type = htons(type);
597 		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
598 	}
599 
600 	if (ifp->if_flags & IFF_MONITOR) {
601 		/*
602 		 * Interface marked for monitoring; discard packet.
603 		 */
604 		m_freem(m);
605 		return;
606 	}
607 
608 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
609 
610 	/* Discard packet if interface is not up */
611 	if ((ifp->if_flags & IFF_UP) == 0) {
612 		m_freem(m);
613 		return;
614 	}
615 
616 	if (m->m_flags & (M_BCAST|M_MCAST))
617 		if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
618 
619 	switch (type) {
620 #ifdef INET
621 	case ETHERTYPE_IP:
622 		isr = NETISR_IP;
623 		break;
624 
625 	case ETHERTYPE_ARP:
626 	{
627 		struct arphdr *ah;
628 		ah = mtod(m, struct arphdr *);
629 
630 		/*
631 		 * Adjust the arp packet to insert an empty tha slot.
632 		 */
633 		m->m_len += ah->ar_hln;
634 		m->m_pkthdr.len += ah->ar_hln;
635 		bcopy(ar_tha(ah), ar_tpa(ah), ah->ar_pln);
636 		isr = NETISR_ARP;
637 		break;
638 	}
639 #endif
640 
641 #ifdef INET6
642 	case ETHERTYPE_IPV6:
643 		isr = NETISR_IPV6;
644 		break;
645 #endif
646 
647 	default:
648 		m_freem(m);
649 		return;
650 	}
651 
652 	M_SETFIB(m, ifp->if_fib);
653 	CURVNET_SET_QUIET(ifp->if_vnet);
654 	netisr_dispatch(isr, m);
655 	CURVNET_RESTORE();
656 }
657 
658 int
firewire_ioctl(struct ifnet * ifp,u_long command,caddr_t data)659 firewire_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
660 {
661 	struct ifaddr *ifa = (struct ifaddr *) data;
662 	struct ifreq *ifr = (struct ifreq *) data;
663 	int error = 0;
664 
665 	switch (command) {
666 	case SIOCSIFADDR:
667 		ifp->if_flags |= IFF_UP;
668 
669 		switch (ifa->ifa_addr->sa_family) {
670 #ifdef INET
671 		case AF_INET:
672 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
673 			arp_ifinit(ifp, ifa);
674 			break;
675 #endif
676 		default:
677 			ifp->if_init(ifp->if_softc);
678 			break;
679 		}
680 		break;
681 
682 	case SIOCGIFADDR:
683 		bcopy(&IFP2FWC(ifp)->fc_hwaddr, &ifr->ifr_addr.sa_data[0],
684 		    sizeof(struct fw_hwaddr));
685 		break;
686 
687 	case SIOCSIFMTU:
688 		/*
689 		 * Set the interface MTU.
690 		 */
691 		if (ifr->ifr_mtu > 1500) {
692 			error = EINVAL;
693 		} else {
694 			ifp->if_mtu = ifr->ifr_mtu;
695 		}
696 		break;
697 	default:
698 		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
699 		break;
700 	}
701 	return (error);
702 }
703 
704 static int
firewire_resolvemulti(struct ifnet * ifp,struct sockaddr ** llsa,struct sockaddr * sa)705 firewire_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
706     struct sockaddr *sa)
707 {
708 #ifdef INET
709 	struct sockaddr_in *sin;
710 #endif
711 #ifdef INET6
712 	struct sockaddr_in6 *sin6;
713 #endif
714 
715 	switch(sa->sa_family) {
716 	case AF_LINK:
717 		/*
718 		 * No mapping needed.
719 		 */
720 		*llsa = NULL;
721 		return 0;
722 
723 #ifdef INET
724 	case AF_INET:
725 		sin = (struct sockaddr_in *)sa;
726 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
727 			return EADDRNOTAVAIL;
728 		*llsa = NULL;
729 		return 0;
730 #endif
731 #ifdef INET6
732 	case AF_INET6:
733 		sin6 = (struct sockaddr_in6 *)sa;
734 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
735 			/*
736 			 * An IP6 address of 0 means listen to all
737 			 * of the Ethernet multicast address used for IP6.
738 			 * (This is used for multicast routers.)
739 			 */
740 			ifp->if_flags |= IFF_ALLMULTI;
741 			*llsa = NULL;
742 			return 0;
743 		}
744 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
745 			return EADDRNOTAVAIL;
746 		*llsa = NULL;
747 		return 0;
748 #endif
749 
750 	default:
751 		/*
752 		 * Well, the text isn't quite right, but it's the name
753 		 * that counts...
754 		 */
755 		return EAFNOSUPPORT;
756 	}
757 }
758 
759 void
firewire_ifattach(struct ifnet * ifp,struct fw_hwaddr * llc)760 firewire_ifattach(struct ifnet *ifp, struct fw_hwaddr *llc)
761 {
762 	struct fw_com *fc = IFP2FWC(ifp);
763 	struct ifaddr *ifa;
764 	struct sockaddr_dl *sdl;
765 	static const char* speeds[] = {
766 		"S100", "S200", "S400", "S800",
767 		"S1600", "S3200"
768 	};
769 
770 	fc->fc_speed = llc->sspd;
771 	STAILQ_INIT(&fc->fc_frags);
772 
773 	ifp->if_addrlen = sizeof(struct fw_hwaddr);
774 	ifp->if_hdrlen = 0;
775 	if_attach(ifp);
776 	ifp->if_mtu = 1500;	/* XXX */
777 	ifp->if_output = firewire_output;
778 	ifp->if_resolvemulti = firewire_resolvemulti;
779 	ifp->if_broadcastaddr = (u_char *) &firewire_broadcastaddr;
780 
781 	ifa = ifp->if_addr;
782 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
783 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
784 	sdl->sdl_type = IFT_IEEE1394;
785 	sdl->sdl_alen = ifp->if_addrlen;
786 	bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
787 
788 	bpfattach(ifp, DLT_APPLE_IP_OVER_IEEE1394,
789 	    sizeof(struct fw_hwaddr));
790 
791 	if_printf(ifp, "Firewire address: %8D @ 0x%04x%08x, %s, maxrec %d\n",
792 	    (uint8_t *) &llc->sender_unique_ID_hi, ":",
793 	    ntohs(llc->sender_unicast_FIFO_hi),
794 	    ntohl(llc->sender_unicast_FIFO_lo),
795 	    speeds[llc->sspd],
796 	    (2 << llc->sender_max_rec));
797 }
798 
799 void
firewire_ifdetach(struct ifnet * ifp)800 firewire_ifdetach(struct ifnet *ifp)
801 {
802 	bpfdetach(ifp);
803 	if_detach(ifp);
804 }
805 
806 void
firewire_busreset(struct ifnet * ifp)807 firewire_busreset(struct ifnet *ifp)
808 {
809 	struct fw_com *fc = IFP2FWC(ifp);
810 	struct fw_reass *r;
811 	struct mbuf *m;
812 
813 	/*
814 	 * Discard any partial datagrams since the host ids may have changed.
815 	 */
816 	while ((r = STAILQ_FIRST(&fc->fc_frags))) {
817 		STAILQ_REMOVE_HEAD(&fc->fc_frags, fr_link);
818 		while (r->fr_frags) {
819 			m = r->fr_frags;
820 			r->fr_frags = m->m_nextpkt;
821 			m_freem(m);
822 		}
823 		free(r, M_TEMP);
824 	}
825 }
826 
827 static void *
firewire_alloc(u_char type,struct ifnet * ifp)828 firewire_alloc(u_char type, struct ifnet *ifp)
829 {
830 	struct fw_com	*fc;
831 
832 	fc = malloc(sizeof(struct fw_com), M_FWCOM, M_WAITOK | M_ZERO);
833 	fc->fc_ifp = ifp;
834 
835 	return (fc);
836 }
837 
838 static void
firewire_free(void * com,u_char type)839 firewire_free(void *com, u_char type)
840 {
841 
842 	free(com, M_FWCOM);
843 }
844 
845 static int
firewire_modevent(module_t mod,int type,void * data)846 firewire_modevent(module_t mod, int type, void *data)
847 {
848 
849 	switch (type) {
850 	case MOD_LOAD:
851 		if_register_com_alloc(IFT_IEEE1394,
852 		    firewire_alloc, firewire_free);
853 		break;
854 	case MOD_UNLOAD:
855 		if_deregister_com_alloc(IFT_IEEE1394);
856 		break;
857 	default:
858 		return (EOPNOTSUPP);
859 	}
860 
861 	return (0);
862 }
863 
864 static moduledata_t firewire_mod = {
865 	"if_firewire",
866 	firewire_modevent,
867 	0
868 };
869 
870 DECLARE_MODULE(if_firewire, firewire_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
871 MODULE_VERSION(if_firewire, 1);
872