xref: /netbsd-src/sys/net/if_ieee1394subr.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: if_ieee1394subr.c,v 1.47 2014/06/05 23:48:16 rmind Exp $	*/
2 
3 /*
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Atsushi Onoe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_ieee1394subr.c,v 1.47 2014/06/05 23:48:16 rmind Exp $");
34 
35 #include "opt_inet.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/device.h>
41 #include <sys/kernel.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/select.h>
46 
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_ieee1394.h>
50 #include <net/if_types.h>
51 #include <net/if_media.h>
52 #include <net/ethertypes.h>
53 #include <net/netisr.h>
54 #include <net/route.h>
55 
56 #include <net/bpf.h>
57 
58 #ifdef INET
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/if_inarp.h>
62 #endif /* INET */
63 #ifdef INET6
64 #include <netinet/in.h>
65 #include <netinet6/in6_var.h>
66 #include <netinet6/nd6.h>
67 #endif /* INET6 */
68 
69 #include <dev/ieee1394/firewire.h>
70 
71 #include <dev/ieee1394/firewirereg.h>
72 #include <dev/ieee1394/iec13213.h>
73 #include <dev/ieee1394/if_fwipvar.h>
74 
75 #define	IEEE1394_REASS_TIMEOUT	3	/* 3 sec */
76 
77 #define	senderr(e)	do { error = (e); goto bad; } while(0/*CONSTCOND*/)
78 
79 static int  ieee1394_output(struct ifnet *, struct mbuf *,
80 		const struct sockaddr *, struct rtentry *);
81 static struct mbuf *ieee1394_reass(struct ifnet *, struct mbuf *, uint16_t);
82 
83 static int
84 ieee1394_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
85     struct rtentry *rt0)
86 {
87 	uint16_t etype = 0;
88 	struct mbuf *m;
89 	int s, hdrlen, error = 0;
90 	struct rtentry *rt;
91 	struct mbuf *mcopy = NULL;
92 	struct ieee1394_hwaddr *hwdst, baddr;
93 	const struct ieee1394_hwaddr *myaddr;
94 	ALTQ_DECL(struct altq_pktattr pktattr;)
95 #ifdef INET
96 	struct arphdr *ah;
97 #endif /* INET */
98 	struct m_tag *mtag;
99 	int unicast;
100 
101 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
102 		senderr(ENETDOWN);
103 	if ((rt = rt0) != NULL) {
104 		if ((rt->rt_flags & RTF_UP) == 0) {
105 			if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
106 				rt->rt_refcnt--;
107 				if (rt->rt_ifp != ifp)
108 					return (*rt->rt_ifp->if_output)
109 							(ifp, m0, dst, rt);
110 			} else
111 				senderr(EHOSTUNREACH);
112 		}
113 		if (rt->rt_flags & RTF_GATEWAY) {
114 			if (rt->rt_gwroute == NULL)
115 				goto lookup;
116 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
117 				rtfree(rt);
118 				rt = rt0;
119   lookup:
120 				rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
121 				if ((rt = rt->rt_gwroute) == NULL)
122 					senderr(EHOSTUNREACH);
123 				/* the "G" test below also prevents rt == rt0 */
124 				if ((rt->rt_flags & RTF_GATEWAY) ||
125 				    (rt->rt_ifp != ifp)) {
126 					rt->rt_refcnt--;
127 					rt0->rt_gwroute = NULL;
128 					senderr(EHOSTUNREACH);
129 				}
130 			}
131 		}
132 		if (rt->rt_flags & RTF_REJECT)
133 			if (rt->rt_rmx.rmx_expire == 0 ||
134 			    time_second < rt->rt_rmx.rmx_expire)
135 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
136 	}
137 
138 	/*
139 	 * If the queueing discipline needs packet classification,
140 	 * do it before prepending link headers.
141 	 */
142 	IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
143 
144 	/*
145 	 * For unicast, we make a tag to store the lladdr of the
146 	 * destination. This might not be the first time we have seen
147 	 * the packet (for instance, the arp code might be trying to
148 	 * re-send it after receiving an arp reply) so we only
149 	 * allocate a tag if there isn't one there already. For
150 	 * multicast, we will eventually use a different tag to store
151 	 * the channel number.
152 	 */
153 	unicast = !(m0->m_flags & (M_BCAST | M_MCAST));
154 	if (unicast) {
155 		mtag =
156 		    m_tag_find(m0, MTAG_FIREWIRE_HWADDR, NULL);
157 		if (!mtag) {
158 			mtag = m_tag_get(MTAG_FIREWIRE_HWADDR,
159 			    sizeof (struct ieee1394_hwaddr), M_NOWAIT);
160 			if (!mtag) {
161 				error = ENOMEM;
162 				goto bad;
163 			}
164 			m_tag_prepend(m0, mtag);
165 		}
166 		hwdst = (struct ieee1394_hwaddr *)(mtag + 1);
167 	} else {
168 		hwdst = &baddr;
169 	}
170 
171 	switch (dst->sa_family) {
172 #ifdef INET
173 	case AF_INET:
174 		if (unicast && (!arpresolve(ifp, rt, m0, dst, (u_char *)hwdst)))
175 			return 0;	/* if not yet resolved */
176 		/* if broadcasting on a simplex interface, loopback a copy */
177 		if ((m0->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
178 			mcopy = m_copy(m0, 0, M_COPYALL);
179 		etype = htons(ETHERTYPE_IP);
180 		break;
181 	case AF_ARP:
182 		ah = mtod(m0, struct arphdr *);
183 		ah->ar_hrd = htons(ARPHRD_IEEE1394);
184 		etype = htons(ETHERTYPE_ARP);
185 		break;
186 #endif /* INET */
187 #ifdef INET6
188 	case AF_INET6:
189 		if (unicast && (!nd6_storelladdr(ifp, rt, m0, dst,
190 		    hwdst->iha_uid, IEEE1394_ADDR_LEN))) {
191 			/* something bad happened */
192 			return 0;
193 		}
194 		etype = htons(ETHERTYPE_IPV6);
195 		break;
196 #endif /* INET6 */
197 
198 	case pseudo_AF_HDRCMPLT:
199 	case AF_UNSPEC:
200 		/* TODO? */
201 	default:
202 		printf("%s: can't handle af%d\n", ifp->if_xname,
203 		    dst->sa_family);
204 		senderr(EAFNOSUPPORT);
205 		break;
206 	}
207 
208 	if (mcopy)
209 		looutput(ifp, mcopy, dst, rt);
210 	myaddr = (const struct ieee1394_hwaddr *)CLLADDR(ifp->if_sadl);
211 	if (ifp->if_bpf) {
212 		struct ieee1394_bpfhdr h;
213 		if (unicast)
214 			memcpy(h.ibh_dhost, hwdst->iha_uid, 8);
215 		else
216 			memcpy(h.ibh_dhost,
217 			    ((const struct ieee1394_hwaddr *)
218 			    ifp->if_broadcastaddr)->iha_uid, 8);
219 		memcpy(h.ibh_shost, myaddr->iha_uid, 8);
220 		h.ibh_type = etype;
221 		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m0);
222 	}
223 	if ((ifp->if_flags & IFF_SIMPLEX) &&
224 	    unicast &&
225 	    memcmp(hwdst, myaddr, IEEE1394_ADDR_LEN) == 0)
226 		return looutput(ifp, m0, dst, rt);
227 
228 	/*
229 	 * XXX:
230 	 * The maximum possible rate depends on the topology.
231 	 * So the determination of maxrec and fragmentation should be
232 	 * called from the driver after probing the topology map.
233 	 */
234 	if (unicast) {
235 		hdrlen = IEEE1394_GASP_LEN;
236 		hwdst->iha_speed = 0;	/* XXX */
237 	} else
238 		hdrlen = 0;
239 
240 	if (hwdst->iha_speed > myaddr->iha_speed)
241 		hwdst->iha_speed = myaddr->iha_speed;
242 	if (hwdst->iha_maxrec > myaddr->iha_maxrec)
243 		hwdst->iha_maxrec = myaddr->iha_maxrec;
244 	if (hwdst->iha_maxrec > (8 + hwdst->iha_speed))
245 		hwdst->iha_maxrec = 8 + hwdst->iha_speed;
246 	if (hwdst->iha_maxrec < 8)
247 			hwdst->iha_maxrec = 8;
248 
249 	m0 = ieee1394_fragment(ifp, m0, (2<<hwdst->iha_maxrec) - hdrlen, etype);
250 	if (m0 == NULL)
251 		senderr(ENOBUFS);
252 
253 	s = splnet();
254 	ifp->if_obytes += m0->m_pkthdr.len;
255 	if (m0->m_flags & M_MCAST)
256 		ifp->if_omcasts++;
257 	while ((m = m0) != NULL) {
258 		m0 = m->m_nextpkt;
259 		if (m == NULL) {
260 			splx(s);
261 			senderr(ENOBUFS);
262 		}
263 		IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
264 		if (error) {
265 			/* mbuf is already freed */
266 			splx(s);
267 			goto bad;
268 		}
269 	}
270 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
271 		(*ifp->if_start)(ifp);
272 	splx(s);
273 	return 0;
274 
275   bad:
276 	while (m0 != NULL) {
277 		m = m0->m_nextpkt;
278 		m_freem(m0);
279 		m0 = m;
280 	}
281 
282 	return error;
283 }
284 
285 struct mbuf *
286 ieee1394_fragment(struct ifnet *ifp, struct mbuf *m0, int maxsize,
287     uint16_t etype)
288 {
289 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
290 	int totlen, fraglen, off;
291 	struct mbuf *m, **mp;
292 	struct ieee1394_fraghdr *ifh;
293 	struct ieee1394_unfraghdr *iuh;
294 
295 	totlen = m0->m_pkthdr.len;
296 	if (totlen + sizeof(struct ieee1394_unfraghdr) <= maxsize) {
297 		M_PREPEND(m0, sizeof(struct ieee1394_unfraghdr), M_DONTWAIT);
298 		if (m0 == NULL)
299 			goto bad;
300 		iuh = mtod(m0, struct ieee1394_unfraghdr *);
301 		iuh->iuh_ft = 0;
302 		iuh->iuh_etype = etype;
303 		return m0;
304 	}
305 
306 	fraglen = maxsize - sizeof(struct ieee1394_fraghdr);
307 
308 	M_PREPEND(m0, sizeof(struct ieee1394_fraghdr), M_DONTWAIT);
309 	if (m0 == NULL)
310 		goto bad;
311 	ifh = mtod(m0, struct ieee1394_fraghdr *);
312 	ifh->ifh_ft_size = htons(IEEE1394_FT_MORE | (totlen - 1));
313 	ifh->ifh_etype_off = etype;
314 	ifh->ifh_dgl = htons(ic->ic_dgl);
315 	ifh->ifh_reserved = 0;
316 	off = fraglen;
317 	mp = &m0->m_nextpkt;
318 	while (off < totlen) {
319 		if (off + fraglen > totlen)
320 			fraglen = totlen - off;
321 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
322 		if (m == NULL)
323 			goto bad;
324 		m->m_flags |= m0->m_flags & (M_BCAST|M_MCAST);	/* copy bcast */
325 		MH_ALIGN(m, sizeof(struct ieee1394_fraghdr));
326 		m->m_len = sizeof(struct ieee1394_fraghdr);
327 		ifh = mtod(m, struct ieee1394_fraghdr *);
328 		ifh->ifh_ft_size =
329 		    htons(IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE | (totlen - 1));
330 		ifh->ifh_etype_off = htons(off);
331 		ifh->ifh_dgl = htons(ic->ic_dgl);
332 		ifh->ifh_reserved = 0;
333 		m->m_next = m_copy(m0, sizeof(*ifh) + off, fraglen);
334 		if (m->m_next == NULL)
335 			goto bad;
336 		m->m_pkthdr.len = sizeof(*ifh) + fraglen;
337 		off += fraglen;
338 		*mp = m;
339 		mp = &m->m_nextpkt;
340 	}
341 	ifh->ifh_ft_size &= ~htons(IEEE1394_FT_MORE);	/* last fragment */
342 	m_adj(m0, -(m0->m_pkthdr.len - maxsize));
343 
344 	ic->ic_dgl++;
345 	return m0;
346 
347   bad:
348 	while ((m = m0) != NULL) {
349 		m0 = m->m_nextpkt;
350 		m->m_nextpkt = NULL;
351 		m_freem(m);
352 	}
353 	return NULL;
354 }
355 
356 void
357 ieee1394_input(struct ifnet *ifp, struct mbuf *m, uint16_t src)
358 {
359 	pktqueue_t *pktq = NULL;
360 	struct ifqueue *inq;
361 	uint16_t etype;
362 	int s;
363 	struct ieee1394_unfraghdr *iuh;
364 	int isr = 0;
365 
366 	if ((ifp->if_flags & IFF_UP) == 0) {
367 		m_freem(m);
368 		return;
369 	}
370 	if (m->m_len < sizeof(*iuh)) {
371 		if ((m = m_pullup(m, sizeof(*iuh))) == NULL)
372 			return;
373 	}
374 
375 	iuh = mtod(m, struct ieee1394_unfraghdr *);
376 
377 	if (ntohs(iuh->iuh_ft) & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE)) {
378 		if ((m = ieee1394_reass(ifp, m, src)) == NULL)
379 			return;
380 		iuh = mtod(m, struct ieee1394_unfraghdr *);
381 	}
382 	etype = ntohs(iuh->iuh_etype);
383 
384 	/* strip off the ieee1394 header */
385 	m_adj(m, sizeof(*iuh));
386 	if (ifp->if_bpf) {
387 		struct ieee1394_bpfhdr h;
388 		struct m_tag *mtag;
389 		const struct ieee1394_hwaddr *myaddr;
390 
391 		mtag = m_tag_find(m, MTAG_FIREWIRE_SENDER_EUID, 0);
392 		if (mtag)
393 			memcpy(h.ibh_shost, mtag + 1, 8);
394 		else
395 			memset(h.ibh_shost, 0, 8);
396 		if (m->m_flags & M_BCAST)
397 			memcpy(h.ibh_dhost,
398 			    ((const struct ieee1394_hwaddr *)
399 			    ifp->if_broadcastaddr)->iha_uid, 8);
400 		else {
401 			myaddr =
402 			  (const struct ieee1394_hwaddr *)CLLADDR(ifp->if_sadl);
403 			memcpy(h.ibh_dhost, myaddr->iha_uid, 8);
404 		}
405 		h.ibh_type = htons(etype);
406 		bpf_mtap2(ifp->if_bpf, &h, sizeof(h), m);
407 	}
408 
409 	switch (etype) {
410 #ifdef INET
411 	case ETHERTYPE_IP:
412 		pktq = ip_pktq;
413 		break;
414 
415 	case ETHERTYPE_ARP:
416 		isr = NETISR_ARP;
417 		inq = &arpintrq;
418 		break;
419 #endif /* INET */
420 
421 #ifdef INET6
422 	case ETHERTYPE_IPV6:
423 		pktq = ip6_pktq;
424 		break;
425 #endif /* INET6 */
426 
427 	default:
428 		m_freem(m);
429 		return;
430 	}
431 
432 	if (__predict_true(pktq)) {
433 		if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
434 			m_freem(m);
435 		}
436 		return;
437 	}
438 
439 	s = splnet();
440 	if (IF_QFULL(inq)) {
441 		IF_DROP(inq);
442 		m_freem(m);
443 	} else {
444 		IF_ENQUEUE(inq, m);
445 		schednetisr(isr);
446 	}
447 	splx(s);
448 }
449 
450 static struct mbuf *
451 ieee1394_reass(struct ifnet *ifp, struct mbuf *m0, uint16_t src)
452 {
453 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
454 	struct ieee1394_fraghdr *ifh;
455 	struct ieee1394_unfraghdr *iuh;
456 	struct ieee1394_reassq *rq;
457 	struct ieee1394_reass_pkt *rp, *trp, *nrp = NULL;
458 	int len;
459 	uint16_t etype, off, ftype, size, dgl;
460 	uint32_t id;
461 
462 	if (m0->m_len < sizeof(*ifh)) {
463 		if ((m0 = m_pullup(m0, sizeof(*ifh))) == NULL)
464 			return NULL;
465 	}
466 	ifh = mtod(m0, struct ieee1394_fraghdr *);
467 	m_adj(m0, sizeof(*ifh));
468 	size = ntohs(ifh->ifh_ft_size);
469 	ftype = size & (IEEE1394_FT_SUBSEQ | IEEE1394_FT_MORE);
470 	size = (size & ~ftype) + 1;
471 	dgl = ntohs(ifh->ifh_dgl);
472 	len = m0->m_pkthdr.len;
473 	id = dgl | (src << 16);
474 	if (ftype & IEEE1394_FT_SUBSEQ) {
475 		m_tag_delete_chain(m0, NULL);
476 		m0->m_flags &= ~M_PKTHDR;
477 		etype = 0;
478 		off = ntohs(ifh->ifh_etype_off);
479 	} else {
480 		etype = ifh->ifh_etype_off;
481 		off = 0;
482 	}
483 
484 	for (rq = LIST_FIRST(&ic->ic_reassq); ; rq = LIST_NEXT(rq, rq_node)) {
485 		if (rq == NULL) {
486 			/*
487 			 * Create a new reassemble queue head for the node.
488 			 */
489 			rq = malloc(sizeof(*rq), M_FTABLE, M_NOWAIT);
490 			if (rq == NULL) {
491 				m_freem(m0);
492 				return NULL;
493 			}
494 			rq->fr_id = id;
495 			LIST_INIT(&rq->rq_pkt);
496 			LIST_INSERT_HEAD(&ic->ic_reassq, rq, rq_node);
497 			break;
498 		}
499 		if (rq->fr_id == id)
500 			break;
501 	}
502 	for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
503 		nrp = LIST_NEXT(rp, rp_next);
504 		if (rp->rp_dgl != dgl)
505 			continue;
506 		/*
507 		 * sanity check:
508 		 * datagram size must be same for all fragments, and
509 		 * no overlap is allowed.
510 		 */
511 		if (rp->rp_size != size ||
512 		    (off < rp->rp_off + rp->rp_len && off + len > rp->rp_off)) {
513 			/*
514 			 * This happens probably due to wrapping dgl value.
515 			 * Destroy all previously received fragment and
516 			 * enqueue current fragment.
517 			 */
518 			for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL;
519 			    rp = nrp) {
520 				nrp = LIST_NEXT(rp, rp_next);
521 				if (rp->rp_dgl == dgl) {
522 					LIST_REMOVE(rp, rp_next);
523 					m_freem(rp->rp_m);
524 					free(rp, M_FTABLE);
525 				}
526 			}
527 			break;
528 		}
529 		if (rp->rp_off + rp->rp_len == off) {
530 			/*
531 			 * All the subsequent fragments received in sequence
532 			 * come here.
533 			 * Concatinate mbuf to previous one instead of
534 			 * allocating new reassemble queue structure,
535 			 * and try to merge more with the subsequent fragment
536 			 * in the queue.
537 			 */
538 			m_cat(rp->rp_m, m0);
539 			rp->rp_len += len;
540 			while (rp->rp_off + rp->rp_len < size &&
541 			    nrp != NULL && nrp->rp_dgl == dgl &&
542 			    nrp->rp_off == rp->rp_off + rp->rp_len) {
543 				LIST_REMOVE(nrp, rp_next);
544 				m_cat(rp->rp_m, nrp->rp_m);
545 				rp->rp_len += nrp->rp_len;
546 				free(nrp, M_FTABLE);
547 				nrp = LIST_NEXT(rp, rp_next);
548 			}
549 			m0 = NULL;	/* mark merged */
550 			break;
551 		}
552 		if (off + m0->m_pkthdr.len == rp->rp_off) {
553 			m_cat(m0, rp->rp_m);
554 			rp->rp_m = m0;
555 			rp->rp_off = off;
556 			rp->rp_etype = etype;	 /* over writing trust etype */
557 			rp->rp_len += len;
558 			m0 = NULL;	/* mark merged */
559 			break;
560 		}
561 		if (rp->rp_off > off) {
562 			/* insert before rp */
563 			nrp = rp;
564 			break;
565 		}
566 		if (nrp == NULL || nrp->rp_dgl != dgl) {
567 			/* insert after rp */
568 			nrp = NULL;
569 			break;
570 		}
571 	}
572 	if (m0 == NULL) {
573 		if (rp->rp_off != 0 || rp->rp_len != size)
574 			return NULL;
575 		/* fragment done */
576 		LIST_REMOVE(rp, rp_next);
577 		m0 = rp->rp_m;
578 		m0->m_pkthdr.len = rp->rp_len;
579 		M_PREPEND(m0, sizeof(*iuh), M_DONTWAIT);
580 		if (m0 != NULL) {
581 			iuh = mtod(m0, struct ieee1394_unfraghdr *);
582 			iuh->iuh_ft = 0;
583 			iuh->iuh_etype = rp->rp_etype;
584 		}
585 		free(rp, M_FTABLE);
586 		return m0;
587 	}
588 
589 	/*
590 	 * New fragment received.  Allocate reassemble queue structure.
591 	 */
592 	trp = malloc(sizeof(*trp), M_FTABLE, M_NOWAIT);
593 	if (trp == NULL) {
594 		m_freem(m0);
595 		return NULL;
596 	}
597 	trp->rp_m = m0;
598 	trp->rp_size = size;
599 	trp->rp_etype = etype;		 /* valid only if off==0 */
600 	trp->rp_off = off;
601 	trp->rp_dgl = dgl;
602 	trp->rp_len = len;
603 	trp->rp_ttl = IEEE1394_REASS_TIMEOUT;
604 	if (trp->rp_ttl <= ifp->if_timer)
605 		trp->rp_ttl = ifp->if_timer + 1;
606 
607 	if (rp == NULL) {
608 		/* first fragment for the dgl */
609 		LIST_INSERT_HEAD(&rq->rq_pkt, trp, rp_next);
610 	} else if (nrp == NULL) {
611 		/* no next fragment for the dgl */
612 		LIST_INSERT_AFTER(rp, trp, rp_next);
613 	} else {
614 		/* there is a hole */
615 		LIST_INSERT_BEFORE(nrp, trp, rp_next);
616 	}
617 	return NULL;
618 }
619 
620 void
621 ieee1394_drain(struct ifnet *ifp)
622 {
623 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
624 	struct ieee1394_reassq *rq;
625 	struct ieee1394_reass_pkt *rp;
626 
627 	while ((rq = LIST_FIRST(&ic->ic_reassq)) != NULL) {
628 		LIST_REMOVE(rq, rq_node);
629 		while ((rp = LIST_FIRST(&rq->rq_pkt)) != NULL) {
630 			LIST_REMOVE(rp, rp_next);
631 			m_freem(rp->rp_m);
632 			free(rp, M_FTABLE);
633 		}
634 		free(rq, M_FTABLE);
635 	}
636 }
637 
638 void
639 ieee1394_watchdog(struct ifnet *ifp)
640 {
641 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
642 	struct ieee1394_reassq *rq;
643 	struct ieee1394_reass_pkt *rp, *nrp;
644 	int dec;
645 
646 	dec = (ifp->if_timer > 0) ? ifp->if_timer : 1;
647 	for (rq = LIST_FIRST(&ic->ic_reassq); rq != NULL;
648 	    rq = LIST_NEXT(rq, rq_node)) {
649 		for (rp = LIST_FIRST(&rq->rq_pkt); rp != NULL; rp = nrp) {
650 			nrp = LIST_NEXT(rp, rp_next);
651 			if (rp->rp_ttl >= dec)
652 				rp->rp_ttl -= dec;
653 			else {
654 				LIST_REMOVE(rp, rp_next);
655 				m_freem(rp->rp_m);
656 				free(rp, M_FTABLE);
657 			}
658 		}
659 	}
660 }
661 
662 const char *
663 ieee1394_sprintf(const uint8_t *laddr)
664 {
665 	static char buf[3*8];
666 
667 	snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
668 	    laddr[0], laddr[1], laddr[2], laddr[3],
669 	    laddr[4], laddr[5], laddr[6], laddr[7]);
670 	return buf;
671 }
672 
673 void
674 ieee1394_ifattach(struct ifnet *ifp, const struct ieee1394_hwaddr *hwaddr)
675 {
676 	struct ieee1394_hwaddr *baddr;
677 	struct ieee1394com *ic = (struct ieee1394com *)ifp;
678 
679 	ifp->if_type = IFT_IEEE1394;
680 	ifp->if_hdrlen = sizeof(struct ieee1394_header);
681 	ifp->if_dlt = DLT_EN10MB;	/* XXX */
682 	ifp->if_mtu = IEEE1394MTU;
683 	ifp->if_output = ieee1394_output;
684 	ifp->if_drain = ieee1394_drain;
685 	ifp->if_watchdog = ieee1394_watchdog;
686 	ifp->if_timer = 1;
687 	if (ifp->if_baudrate == 0)
688 		ifp->if_baudrate = IF_Mbps(100);
689 
690 	if_set_sadl(ifp, hwaddr, sizeof(struct ieee1394_hwaddr), true);
691 
692 	baddr = malloc(ifp->if_addrlen, M_DEVBUF, M_WAITOK);
693 	memset(baddr->iha_uid, 0xff, IEEE1394_ADDR_LEN);
694 	baddr->iha_speed = 0;	/*XXX: how to determine the speed for bcast? */
695 	baddr->iha_maxrec = 512 << baddr->iha_speed;
696 	memset(baddr->iha_offset, 0, sizeof(baddr->iha_offset));
697 	ifp->if_broadcastaddr = (uint8_t *)baddr;
698 	LIST_INIT(&ic->ic_reassq);
699 	bpf_attach(ifp, DLT_APPLE_IP_OVER_IEEE1394,
700 	    sizeof(struct ieee1394_hwaddr));
701 }
702 
703 void
704 ieee1394_ifdetach(struct ifnet *ifp)
705 {
706 	ieee1394_drain(ifp);
707 	bpf_detach(ifp);
708 	free(__UNCONST(ifp->if_broadcastaddr), M_DEVBUF);
709 	ifp->if_broadcastaddr = NULL;
710 #if 0	/* done in if_detach() */
711 	if_free_sadl(ifp);
712 #endif
713 }
714 
715 int
716 ieee1394_ioctl(struct ifnet *ifp, u_long cmd, void *data)
717 {
718 	struct ifreq *ifr = (struct ifreq *)data;
719 	struct ifaddr *ifa = (struct ifaddr *)data;
720 	int error = 0;
721 
722 	switch (cmd) {
723 	case SIOCINITIFADDR:
724 		ifp->if_flags |= IFF_UP;
725 		switch (ifa->ifa_addr->sa_family) {
726 #ifdef INET
727 		case AF_INET:
728 			if ((error = (*ifp->if_init)(ifp)) != 0)
729 				break;
730 			arp_ifinit(ifp, ifa);
731 			break;
732 #endif /* INET */
733 		default:
734 			error = (*ifp->if_init)(ifp);
735 			break;
736 		}
737 		break;
738 
739 	case SIOCSIFMTU:
740 		if (ifr->ifr_mtu > IEEE1394MTU)
741 			error = EINVAL;
742 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
743 			error = 0;
744 		break;
745 
746 	default:
747 		error = ifioctl_common(ifp, cmd, data);
748 		break;
749 	}
750 
751 	return error;
752 }
753