1 /* $NetBSD: if_ecosubr.c,v 1.48 2016/06/20 08:30:58 knakahara Exp $ */
2
3 /*-
4 * Copyright (c) 2001 Ben Harris
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 /*
30 * Copyright (c) 1982, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 * @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96
58 */
59
60 #include <sys/cdefs.h>
61 __KERNEL_RCSID(0, "$NetBSD: if_ecosubr.c,v 1.48 2016/06/20 08:30:58 knakahara Exp $");
62
63 #ifdef _KERNEL_OPT
64 #include "opt_inet.h"
65 #endif
66
67 #include <sys/param.h>
68 #include <sys/errno.h>
69 #include <sys/kernel.h>
70 #include <sys/socket.h>
71 #include <sys/sockio.h>
72 #include <sys/syslog.h>
73 #include <sys/systm.h>
74
75 #include <net/if.h>
76 #include <net/if_dl.h>
77 #include <net/if_eco.h>
78 #include <net/if_types.h>
79 #include <net/netisr.h>
80 #include <net/route.h>
81
82 #include <net/bpf.h>
83
84 #ifdef INET
85 #include <net/ethertypes.h>
86 #include <net/if_arp.h>
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #endif
90 #include <netinet/if_inarp.h>
91
92 struct eco_retryparms {
93 int erp_delay;
94 int erp_count;
95 };
96
97 /* Default broadcast address */
98 static const uint8_t eco_broadcastaddr[] = { 0xff, 0xff };
99
100 static int eco_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
101 struct rtentry *);
102 static void eco_input(struct ifnet *, struct mbuf *);
103 static void eco_start(struct ifnet *);
104 static int eco_ioctl(struct ifnet *, u_long, void *);
105
106 static int eco_interestingp(struct ifnet *ifp, struct mbuf *m);
107 static struct mbuf *eco_immediate(struct ifnet *ifp, struct mbuf *m);
108 static struct mbuf *eco_ack(struct ifnet *ifp, struct mbuf *m);
109
110 static void eco_defer(struct ifnet *, struct mbuf *, int);
111 static void eco_retry_free(struct eco_retry *er);
112 static void eco_retry(void *);
113
114 void
eco_ifattach(struct ifnet * ifp,const uint8_t * lla)115 eco_ifattach(struct ifnet *ifp, const uint8_t *lla)
116 {
117 struct ecocom *ec = (void *)ifp;
118
119 ifp->if_type = IFT_ECONET;
120 ifp->if_addrlen = ECO_ADDR_LEN;
121 ifp->if_hdrlen = ECO_HDR_LEN;
122 ifp->if_dlt = DLT_ECONET;
123 ifp->if_mtu = ECO_MTU;
124
125 ifp->if_output = eco_output;
126 ifp->if_input = eco_input;
127 ifp->if_start = eco_start;
128 ifp->if_ioctl = eco_ioctl;
129
130 /* ifp->if_baudrate...; */
131 if_set_sadl(ifp, lla, ECO_ADDR_LEN, FALSE);
132
133 ifp->if_broadcastaddr = eco_broadcastaddr;
134
135 LIST_INIT(&ec->ec_retries);
136
137 bpf_attach(ifp, ifp->if_dlt, ECO_HDR_LEN);
138 }
139
140 #define senderr(e) do { \
141 error = (e); \
142 goto bad; \
143 } while (/*CONSTCOND*/0)
144
145 int
eco_init(struct ifnet * ifp)146 eco_init(struct ifnet *ifp)
147 {
148 struct ecocom *ec = (struct ecocom *)ifp;
149
150 if ((ifp->if_flags & IFF_RUNNING) == 0)
151 ec->ec_state = ECO_UNKNOWN;
152 return 0;
153 }
154
155 void
eco_stop(struct ifnet * ifp,int disable)156 eco_stop(struct ifnet *ifp, int disable)
157 {
158 struct ecocom *ec = (struct ecocom *)ifp;
159
160 while (!LIST_EMPTY(&ec->ec_retries))
161 eco_retry_free(LIST_FIRST(&ec->ec_retries));
162 }
163
164 static int
eco_output(struct ifnet * ifp,struct mbuf * m0,const struct sockaddr * dst,struct rtentry * rt)165 eco_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
166 struct rtentry *rt)
167 {
168 struct eco_header ehdr, *eh;
169 int error;
170 struct mbuf *m = m0, *mcopy = NULL;
171 int hdrcmplt;
172 int retry_delay, retry_count;
173 struct m_tag *mtag;
174 struct eco_retryparms *erp;
175 #ifdef INET
176 struct mbuf *m1;
177 struct arphdr *ah;
178 void *tha;
179 struct eco_arp *ecah;
180 #endif
181
182 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
183 senderr(ENETDOWN);
184 /*
185 * If the queueing discipline needs packet classification,
186 * do it before prepending link headers.
187 */
188 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
189
190 hdrcmplt = 0;
191 retry_delay = hz / 16;
192 retry_count = 16;
193 switch (dst->sa_family) {
194 #ifdef INET
195 case AF_INET:
196 if (m->m_flags & M_BCAST)
197 memcpy(ehdr.eco_dhost, eco_broadcastaddr,
198 ECO_ADDR_LEN);
199
200 else if (!arpresolve(ifp, rt, m, dst, ehdr.eco_dhost,
201 sizeof(ehdr.eco_dhost)))
202 return (0); /* if not yet resolved */
203 /* If broadcasting on a simplex interface, loopback a copy */
204 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
205 mcopy = m_copy(m, 0, (int)M_COPYALL);
206 ehdr.eco_port = ECO_PORT_IP;
207 ehdr.eco_control = ECO_CTL_IP;
208 break;
209
210 case AF_ARP:
211 ah = mtod(m, struct arphdr *);
212
213 if (ntohs(ah->ar_pro) != ETHERTYPE_IP)
214 return EAFNOSUPPORT;
215 ehdr.eco_port = ECO_PORT_IP;
216 switch (ntohs(ah->ar_op)) {
217 case ARPOP_REQUEST:
218 ehdr.eco_control = ECO_CTL_ARP_REQUEST;
219 break;
220 case ARPOP_REPLY:
221 ehdr.eco_control = ECO_CTL_ARP_REPLY;
222 break;
223 default:
224 return EOPNOTSUPP;
225 }
226
227 if (m->m_flags & M_BCAST)
228 memcpy(ehdr.eco_dhost, eco_broadcastaddr,
229 ECO_ADDR_LEN);
230 else {
231 tha = ar_tha(ah);
232 if (tha == NULL)
233 return 0;
234 memcpy(ehdr.eco_dhost, tha, ECO_ADDR_LEN);
235 }
236
237 MGETHDR(m1, M_DONTWAIT, MT_DATA);
238 if (m1 == NULL)
239 senderr(ENOBUFS);
240 M_MOVE_PKTHDR(m1, m);
241 m1->m_len = sizeof(*ecah);
242 m1->m_pkthdr.len = m1->m_len;
243 MH_ALIGN(m1, m1->m_len);
244 ecah = mtod(m1, struct eco_arp *);
245 memset(ecah, 0, m1->m_len);
246 memcpy(ecah->ecar_spa, ar_spa(ah), ah->ar_pln);
247 memcpy(ecah->ecar_tpa, ar_tpa(ah), ah->ar_pln);
248 m_freem(m);
249 m = m1;
250 break;
251 #endif
252 case pseudo_AF_HDRCMPLT:
253 hdrcmplt = 1;
254 /* FALLTHROUGH */
255 case AF_UNSPEC:
256 ehdr = *(struct eco_header const *)dst->sa_data;
257 break;
258 default:
259 log(LOG_ERR, "%s: can't handle af%d\n", ifp->if_xname,
260 dst->sa_family);
261 senderr(EAFNOSUPPORT);
262 }
263
264 if (mcopy)
265 (void) looutput(ifp, mcopy, dst, rt);
266
267 /*
268 * Add local net header. If no space in first mbuf,
269 * allocate another.
270 */
271 M_PREPEND(m, sizeof (struct eco_header), M_DONTWAIT);
272 if (m == 0)
273 senderr(ENOBUFS);
274 eh = mtod(m, struct eco_header *);
275 *eh = ehdr;
276 if (!hdrcmplt)
277 memcpy(eh->eco_shost, CLLADDR(ifp->if_sadl),
278 ECO_ADDR_LEN);
279
280 if ((m->m_flags & M_BCAST) == 0) {
281 /* Attach retry info to packet. */
282 mtag = m_tag_get(PACKET_TAG_ECO_RETRYPARMS,
283 sizeof(struct eco_retryparms), M_NOWAIT);
284 if (mtag == NULL)
285 senderr(ENOBUFS);
286 erp = (struct eco_retryparms *)(mtag + 1);
287 erp->erp_delay = retry_delay;
288 erp->erp_count = retry_count;
289 }
290
291 if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
292 return (error);
293 if (m == NULL)
294 return (0);
295
296 return ifq_enqueue(ifp, m);
297
298 bad:
299 if (m)
300 m_freem(m);
301 return error;
302 }
303
304 /*
305 * Given a scout, decide if we want the rest of the packet.
306 */
307 static int
eco_interestingp(struct ifnet * ifp,struct mbuf * m)308 eco_interestingp(struct ifnet *ifp, struct mbuf *m)
309 {
310 struct eco_header *eh;
311
312 eh = mtod(m, struct eco_header *);
313 switch (eh->eco_port) {
314 #ifdef INET
315 case ECO_PORT_IP:
316 return 1;
317 #endif
318 }
319 return 0;
320 }
321
322 static void
eco_input(struct ifnet * ifp,struct mbuf * m)323 eco_input(struct ifnet *ifp, struct mbuf *m)
324 {
325 pktqueue_t *pktq = NULL;
326 struct ifqueue *inq;
327 struct eco_header ehdr, *eh;
328 int isr = 0;
329 int s;
330 #ifdef INET
331 int i;
332 struct arphdr *ah;
333 struct eco_arp *ecah;
334 struct mbuf *m1;
335 void *tha;
336 #endif
337
338 if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
339 return;
340 if (m == NULL)
341 return;
342
343 /* Copy the mbuf header and trim it off. */
344 /* XXX use m_split? */
345 eh = &ehdr;
346 m_copydata(m, 0, ECO_HDR_LEN, (void *)eh);
347 m_adj(m, ECO_HDR_LEN);
348
349 switch (eh->eco_port) {
350 #ifdef INET
351 case ECO_PORT_IP:
352 switch (eh->eco_control) {
353 case ECO_CTL_IP:
354 pktq = ip_pktq;
355 break;
356 case ECO_CTL_ARP_REQUEST:
357 case ECO_CTL_ARP_REPLY:
358 /*
359 * ARP over Econet is strange, because Econet only
360 * supports 8 bytes of data in a broadcast packet.
361 * To cope with this, only the source and destination
362 * IP addresses are actually contained in the packet
363 * and we have to infer the rest and build a fake ARP
364 * packet to pass upwards.
365 */
366 if (m->m_pkthdr.len != sizeof(struct eco_arp))
367 goto drop;
368 if (m->m_len < sizeof(struct eco_arp)) {
369 m = m_pullup(m, sizeof(struct eco_arp));
370 if (m == NULL) goto drop;
371 }
372 ecah = mtod(m, struct eco_arp *);
373 /* This code derived from arprequest() */
374 MGETHDR(m1, M_DONTWAIT, MT_DATA);
375 if (m1 == NULL)
376 goto drop;
377 M_MOVE_PKTHDR(m1, m);
378 m1->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
379 2*ifp->if_data.ifi_addrlen;
380 m1->m_pkthdr.len = m1->m_len;
381 MH_ALIGN(m1, m1->m_len);
382 ah = mtod(m1, struct arphdr *);
383 memset((void *)ah, 0, m1->m_len);
384 ah->ar_pro = htons(ETHERTYPE_IP);
385 ah->ar_hln = ifp->if_data.ifi_addrlen;
386 ah->ar_pln = sizeof(struct in_addr);
387 if (eh->eco_control == ECO_CTL_ARP_REQUEST)
388 ah->ar_op = htons(ARPOP_REQUEST);
389 else
390 ah->ar_op = htons(ARPOP_REPLY);
391 tha = ar_tha(ah);
392 KASSERT(tha != NULL);
393 memcpy(ar_sha(ah), eh->eco_shost, ah->ar_hln);
394 memcpy(tha, eh->eco_dhost, ah->ar_hln);
395 memcpy(ar_spa(ah), ecah->ecar_spa, ah->ar_pln);
396 memcpy(ar_tpa(ah), ecah->ecar_tpa, ah->ar_pln);
397 m_freem(m);
398 m = m1;
399 isr = NETISR_ARP;
400 inq = &arpintrq;
401 break;
402 case ECO_CTL_IPBCAST_REQUEST:
403 {
404 struct sockaddr_storage dst_store;
405 struct sockaddr *dst = (struct sockaddr *)&dst_store;
406
407 /* Queue? */
408 memcpy(eh->eco_dhost, eh->eco_shost, ECO_ADDR_LEN);
409 eh->eco_control = ECO_CTL_IPBCAST_REPLY;
410 /* dst->sa_len??? */
411 dst->sa_family = AF_UNSPEC;
412 memcpy(dst->sa_data, eh, ECO_HDR_LEN);
413 if_output_lock(ifp, ifp, m, dst, NULL);
414 return;
415 }
416 default:
417 printf("%s: unknown IP stn %s ctl 0x%02x len %d:",
418 ifp->if_xname, eco_sprintf(eh->eco_shost),
419 eh->eco_control, m->m_pkthdr.len);
420 if (m->m_len == 0) {
421 m = m_pullup(m, 1);
422 if (m == 0) {
423 printf("\n");
424 goto drop;
425 }
426 }
427 for (i = 0; i < m->m_len; i++)
428 printf(" %02x", mtod(m, uint8_t *)[i]);
429 printf("\n");
430 goto drop;
431 }
432 break;
433 #endif
434 default:
435 printf("%s: unknown port stn %s port 0x%02x ctl 0x%02x\n",
436 ifp->if_xname, eco_sprintf(eh->eco_shost),
437 eh->eco_port, eh->eco_control);
438 #ifdef INET
439 drop:
440 #endif
441 m_freem(m);
442 return;
443 }
444
445 if (__predict_true(pktq)) {
446 if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
447 m_freem(m);
448 }
449 return;
450 }
451
452 s = splnet();
453 if (IF_QFULL(inq)) {
454 IF_DROP(inq);
455 m_freem(m);
456 } else {
457 IF_ENQUEUE(inq, m);
458 schednetisr(isr);
459 }
460 splx(s);
461 }
462
463 static void
eco_start(struct ifnet * ifp)464 eco_start(struct ifnet *ifp)
465 {
466 struct ecocom *ec = (void *)ifp;
467 struct mbuf *m;
468 struct eco_header *eh;
469
470 if (ec->ec_state != ECO_IDLE) return;
471 IFQ_DEQUEUE(&ifp->if_snd, m);
472 if (m == NULL) return;
473 if (ec->ec_claimwire(ifp) == 0) {
474 eh = mtod(m, struct eco_header *);
475 if (eh->eco_port == ECO_PORT_IMMEDIATE) {
476 ec->ec_txframe(ifp, m);
477 ec->ec_state = ECO_IMMED_SENT;
478 } else if (eh->eco_dhost[0] == 255) {
479 ec->ec_txframe(ifp, m);
480 ec->ec_state = ECO_DONE;
481 } else {
482 ec->ec_packet = m;
483 m = m_copym(m, 0, ECO_HDR_LEN, M_DONTWAIT);
484 if (m == NULL) {
485 m_freem(ec->ec_packet);
486 ec->ec_packet = NULL;
487 return;
488 }
489 ec->ec_txframe(ifp, m);
490 ec->ec_state = ECO_SCOUT_SENT;
491 }
492 ifp->if_flags |= IFF_OACTIVE;
493 } else {
494 log(LOG_ERR, "%s: line jammed\n", ifp->if_xname);
495 m_freem(m);
496 }
497 }
498
499 static int
eco_ioctl(struct ifnet * ifp,u_long cmd,void * data)500 eco_ioctl(struct ifnet *ifp, u_long cmd, void *data)
501 {
502 struct ifaddr *ifa = (struct ifaddr *)data;
503 int error;
504
505 switch (cmd) {
506 case SIOCINITIFADDR:
507 ifp->if_flags |= IFF_UP;
508 if ((ifp->if_flags & IFF_RUNNING) == 0 &&
509 (error = (*ifp->if_init)(ifp)) != 0)
510 return error;
511 switch (ifa->ifa_addr->sa_family) {
512 #ifdef INET
513 case AF_INET:
514 arp_ifinit(ifp, ifa);
515 break;
516 #endif
517 default:
518 break;
519 }
520 return 0;
521 case SIOCSIFMTU:
522 if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
523 return error;
524 else if (ifp->if_flags & IFF_UP)
525 return (*ifp->if_init)(ifp);
526 else
527 return 0;
528 break;
529 case SIOCSIFFLAGS:
530 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
531 return error;
532 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
533 case IFF_RUNNING:
534 /*
535 * If interface is marked down and it is running,
536 * then stop and disable it.
537 */
538 (*ifp->if_stop)(ifp, 1);
539 return 0;
540 case IFF_UP:
541 /*
542 * If interface is marked up and it is stopped, then
543 * start it.
544 */
545 return (*ifp->if_init)(ifp);
546 case IFF_UP|IFF_RUNNING:
547 /*
548 * Reset the interface to pick up changes in any other
549 * flags that affect the hardware state.
550 */
551 return (*ifp->if_init)(ifp);
552 case 0:
553 return 0;
554 }
555 break;
556 default:
557 return ifioctl_common(ifp, cmd, data);
558 }
559
560 return 0;
561 }
562
563 /*
564 * Handle a raw Econet frame off the interface. The interface may be
565 * flag-filling for a response.
566 *
567 * May be called from IPL_NET or IPL_SOFTNET.
568 */
569
570 struct mbuf *
eco_inputframe(struct ifnet * ifp,struct mbuf * m)571 eco_inputframe(struct ifnet *ifp, struct mbuf *m)
572 {
573 struct ecocom *ec = (void *)ifp;
574 struct eco_header *eh, *eh0;
575 struct mbuf *m0;
576 struct mbuf *reply;
577 int len;
578
579 eh = mtod(m, struct eco_header *);
580 switch (ec->ec_state) {
581 case ECO_IDLE: /* Start of a packet (bcast, immed, scout) */
582 if (m->m_pkthdr.len < ECO_HDR_LEN) {
583 log(LOG_NOTICE, "%s: undersize scout\n",
584 ifp->if_xname);
585 goto drop;
586 }
587 if (memcmp(eh->eco_dhost, eco_broadcastaddr,
588 ECO_ADDR_LEN) == 0) {
589 /* Broadcast */
590 eco_input(ifp, m);
591 } else if (memcmp(eh->eco_dhost, CLLADDR(ifp->if_sadl),
592 ECO_ADDR_LEN) == 0) {
593 /* Unicast for us */
594 if (eh->eco_port == ECO_PORT_IMMEDIATE)
595 return eco_immediate(ifp, m);
596 else {
597 if (eco_interestingp(ifp, m)) {
598 reply = eco_ack(ifp, m);
599 if (reply == NULL) {
600 m_freem(m);
601 return NULL;
602 }
603 ec->ec_state = ECO_SCOUT_RCVD;
604 ec->ec_scout = m;
605 return reply;
606 } else {
607 m_freem(m);
608 return NULL;
609 }
610 }
611 } else
612 /* Not for us. Throw it away. */
613 m_freem(m);
614 break;
615 case ECO_SCOUT_RCVD: /* Packet data */
616 KASSERT(ec->ec_scout != NULL);
617 m0 = ec->ec_scout;
618 eh0 = mtod(m0, struct eco_header *);
619 if (m->m_pkthdr.len < ECO_SHDR_LEN ||
620 memcmp(eh->eco_shost, eh0->eco_shost, ECO_ADDR_LEN) != 0 ||
621 memcmp(eh->eco_dhost, eh0->eco_dhost, ECO_ADDR_LEN) != 0) {
622 log(LOG_NOTICE, "%s: garbled data packet header\n",
623 ifp->if_xname);
624 goto drop;
625 }
626 reply = eco_ack(ifp, m);
627 /*
628 * Chop off the small header from this frame, and put
629 * the scout (which holds the control byte and port)
630 * in its place.
631 */
632 ec->ec_scout = NULL;
633 m_adj(m, ECO_SHDR_LEN);
634 len = m0->m_pkthdr.len + m->m_pkthdr.len;
635 m_cat(m0, m);
636 m0->m_pkthdr.len = len;
637 ec->ec_state = ECO_DONE;
638 eco_input(ifp, m0);
639 return reply;
640 case ECO_SCOUT_SENT: /* Scout ack */
641 KASSERT(ec->ec_packet != NULL);
642 m0 = ec->ec_packet;
643 eh0 = mtod(m0, struct eco_header *);
644 if (m->m_pkthdr.len != ECO_SHDR_LEN ||
645 memcmp(eh->eco_shost, eh0->eco_dhost, ECO_ADDR_LEN) != 0 ||
646 memcmp(eh->eco_dhost, eh0->eco_shost, ECO_ADDR_LEN) != 0) {
647 log(LOG_NOTICE, "%s: garbled scout ack\n",
648 ifp->if_xname);
649 goto drop;
650 }
651 m_freem(m);
652 /* Chop out the control and port bytes. */
653 m0 = m_copym(ec->ec_packet, 0, ECO_SHDR_LEN, M_DONTWAIT);
654 if (m0 == NULL) {
655 m_freem(ec->ec_packet);
656 return NULL;
657 }
658 m = ec->ec_packet;
659 ec->ec_packet = m_copypacket(m, M_DONTWAIT);
660 if (ec->ec_packet == NULL) {
661 m_freem(m0);
662 m_freem(m);
663 return NULL;
664 }
665 m_adj(m, ECO_HDR_LEN);
666 len = m0->m_pkthdr.len + m->m_pkthdr.len;
667 m_cat(m0, m); /* Doesn't update packet header */
668 m0->m_pkthdr.len = len;
669 ec->ec_state = ECO_DATA_SENT;
670 return m0;
671 case ECO_DATA_SENT: /* Data ack */
672 KASSERT(ec->ec_packet != NULL);
673 m0 = ec->ec_packet;
674 eh0 = mtod(m0, struct eco_header *);
675 if (m->m_pkthdr.len != ECO_SHDR_LEN ||
676 memcmp(eh->eco_shost, eh0->eco_dhost, ECO_ADDR_LEN) != 0 ||
677 memcmp(eh->eco_dhost, eh0->eco_shost, ECO_ADDR_LEN) != 0) {
678 log(LOG_NOTICE, "%s: garbled data ack\n",
679 ifp->if_xname);
680 goto drop;
681 }
682 m_freem(m);
683 m_freem(ec->ec_packet);
684 ec->ec_packet = NULL;
685 ec->ec_state = ECO_DONE;
686 return NULL;
687 default:
688 drop:
689 m_freem(m);
690 break;
691 }
692 return NULL;
693 }
694
695 /*
696 * Handle an immediate operation, and return the reply, or NULL not to reply.
697 * Frees the incoming mbuf.
698 */
699
700 static struct mbuf *
eco_immediate(struct ifnet * ifp,struct mbuf * m)701 eco_immediate(struct ifnet *ifp, struct mbuf *m)
702 {
703 struct eco_header *eh, *reh;
704 struct mbuf *n;
705 static const uint8_t machinepeek_data[] = { 42, 0, 0, 1 };
706
707 eh = mtod(m, struct eco_header *);
708 switch (eh->eco_control) {
709 case ECO_CTL_MACHINEPEEK:
710 MGETHDR(n, M_DONTWAIT, MT_DATA);
711 if (n == NULL)
712 goto bad;
713 n->m_len = n->m_pkthdr.len = ECO_SHDR_LEN + 4;
714 reh = mtod(n, struct eco_header *);
715 memcpy(reh->eco_dhost, eh->eco_shost,
716 ECO_ADDR_LEN);
717 memcpy(reh->eco_shost, CLLADDR(ifp->if_sadl),
718 ECO_ADDR_LEN);
719 memcpy(mtod(n, char *) + ECO_SHDR_LEN, machinepeek_data,
720 sizeof(machinepeek_data));
721 m_freem(m);
722 return n;
723 default:
724 bad:
725 m_freem(m);
726 return NULL;
727 }
728 }
729
730 /*
731 * Generate (and return) an acknowledgement for a frame. Doesn't free the
732 * original frame, since it's probably needed elsewhere.
733 */
734 static struct mbuf *
eco_ack(struct ifnet * ifp,struct mbuf * m)735 eco_ack(struct ifnet *ifp, struct mbuf *m)
736 {
737 struct eco_header *eh, *reh;
738 struct mbuf *n;
739
740 eh = mtod(m, struct eco_header *);
741 MGETHDR(n, M_DONTWAIT, MT_DATA);
742 if (n == NULL)
743 return NULL;
744 n->m_len = n->m_pkthdr.len = ECO_SHDR_LEN;
745 reh = mtod(n, struct eco_header *);
746 memcpy(reh->eco_dhost, eh->eco_shost, ECO_ADDR_LEN);
747 memcpy(reh->eco_shost, CLLADDR(ifp->if_sadl), ECO_ADDR_LEN);
748 return n;
749 }
750
751 void
eco_inputidle(struct ifnet * ifp)752 eco_inputidle(struct ifnet *ifp)
753 {
754 struct ecocom *ec = (void *)ifp;
755 struct mbuf *m;
756 struct m_tag *mtag;
757 struct eco_retryparms *erp;
758
759 switch (ec->ec_state) {
760 case ECO_SCOUT_SENT:
761 case ECO_DATA_SENT:
762 case ECO_IMMED_SENT:
763 /* Outgoing packet failed. Check if we should retry. */
764 m = ec->ec_packet;
765 ec->ec_packet = NULL;
766 mtag = m_tag_find(m, PACKET_TAG_ECO_RETRYPARMS, NULL);
767 if (mtag == NULL)
768 m_freem(m);
769 else {
770 erp = (struct eco_retryparms *)(mtag + 1);
771 if (--erp->erp_count > 0)
772 eco_defer(ifp, m, erp->erp_delay);
773 else
774 printf("%s: pkt failed\n", ifp->if_xname);
775 }
776 break;
777 case ECO_SCOUT_RCVD:
778 m_freem(ec->ec_scout);
779 ec->ec_scout = NULL;
780 break;
781 default:
782 break;
783 }
784 ec->ec_state = ECO_IDLE;
785 if_start_lock(ifp);
786 }
787
788 /*
789 * Convert Econet address to printable (loggable) representation.
790 */
791 char *
eco_sprintf(const uint8_t * ea)792 eco_sprintf(const uint8_t *ea)
793 {
794 static char buf[8];
795
796 if (ea[1] == 0)
797 snprintf(buf, sizeof(buf), "%d", ea[0]);
798 else
799 snprintf(buf, sizeof(buf), "%d.%d", ea[1], ea[0]);
800 return buf;
801 }
802
803 /*
804 * Econet retry handling.
805 */
806 static void
eco_defer(struct ifnet * ifp,struct mbuf * m,int retry_delay)807 eco_defer(struct ifnet *ifp, struct mbuf *m, int retry_delay)
808 {
809 struct ecocom *ec = (struct ecocom *)ifp;
810 struct eco_retry *er;
811 int s;
812
813 er = malloc(sizeof(*er), M_TEMP, M_NOWAIT);
814 if (er == NULL) {
815 m_freem(m);
816 return;
817 }
818 callout_init(&er->er_callout, 0);
819 er->er_packet = m;
820 er->er_ifp = ifp;
821 s = splnet();
822 LIST_INSERT_HEAD(&ec->ec_retries, er, er_link);
823 splx(s);
824 callout_reset(&er->er_callout, retry_delay, eco_retry, er);
825 }
826
827 static void
eco_retry_free(struct eco_retry * er)828 eco_retry_free(struct eco_retry *er)
829 {
830 int s;
831
832 callout_halt(&er->er_callout, NULL);
833 m_freem(er->er_packet);
834 s = splnet();
835 LIST_REMOVE(er, er_link);
836 splx(s);
837 callout_destroy(&er->er_callout);
838 free(er, M_TEMP);
839 }
840
841 static void
eco_retry(void * arg)842 eco_retry(void *arg)
843 {
844 struct eco_retry *er = arg;
845 struct mbuf *m;
846 struct ifnet *ifp;
847
848 ifp = er->er_ifp;
849 m = er->er_packet;
850 LIST_REMOVE(er, er_link);
851 (void)ifq_enqueue(ifp, m);
852 free(er, M_TEMP);
853 }
854