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