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