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