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