if_arcsubr.c revision 1.80.8.1 1 /* $NetBSD: if_arcsubr.c,v 1.80.8.1 2020/02/29 20:21:06 ad Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Ignatios Souvatzis
5 * Copyright (c) 1982, 1989, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp
33 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
34 *
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: if_arcsubr.c,v 1.80.8.1 2020/02/29 20:21:06 ad Exp $");
39
40 #ifdef _KERNEL_OPT
41 #include "opt_inet.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/ioctl.h>
50 #include <sys/errno.h>
51 #include <sys/syslog.h>
52
53 #include <sys/cpu.h>
54
55 #include <net/if.h>
56 #include <net/netisr.h>
57 #include <net/route.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/if_arc.h>
61 #include <net/if_arp.h>
62 #include <net/if_ether.h>
63
64 #include <net/bpf.h>
65
66 #ifdef INET
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/if_inarp.h>
70 #endif
71
72 #ifdef INET6
73 #ifndef INET
74 #include <netinet/in.h>
75 #endif
76 #include <netinet6/in6_var.h>
77 #include <netinet6/nd6.h>
78 #endif
79
80 #define ARCNET_ALLOW_BROKEN_ARP
81
82 #ifndef ARC_IPMTU
83 #define ARC_IPMTU 1500
84 #endif
85
86 static struct mbuf *arc_defrag(struct ifnet *, struct mbuf *);
87
88 /*
89 * RC1201 requires us to have this configurable. We have it only per
90 * machine at the moment... there is no generic "set mtu" ioctl, AFAICS.
91 * Anyway, it is possible to binpatch this or set it per kernel config
92 * option.
93 */
94 #if ARC_IPMTU > 60480
95 ERROR: The arc_ipmtu is ARC_IPMTU, but must not exceed 60480.
96 #endif
97 int arc_ipmtu = ARC_IPMTU;
98 uint8_t arcbroadcastaddr = 0;
99
100 #define senderr(e) { error = (e); goto bad;}
101
102 static int arc_output(struct ifnet *, struct mbuf *,
103 const struct sockaddr *, const struct rtentry *);
104 static void arc_input(struct ifnet *, struct mbuf *);
105
106 /*
107 * ARCnet output routine.
108 * Encapsulate a packet of type family for the local net.
109 * Assumes that ifp is actually pointer to arccom structure.
110 */
111 static int
112 arc_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
113 const struct rtentry *rt)
114 {
115 struct mbuf *m, *m1, *mcopy;
116 struct arccom *ac;
117 const struct arc_header *cah;
118 struct arc_header *ah;
119 struct arphdr *arph;
120 int error, newencoding;
121 uint8_t atype, adst, myself;
122 int tfrags, sflag, fsflag, rsflag;
123
124 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
125 return (ENETDOWN); /* m, m1 aren't initialized yet */
126
127 error = newencoding = 0;
128 ac = (struct arccom *)ifp;
129 m = m0;
130 mcopy = m1 = NULL;
131
132 myself = *CLLADDR(ifp->if_sadl);
133
134 /*
135 * if the queueing discipline needs packet classification,
136 * do it before prepending link headers.
137 */
138 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
139
140 switch (dst->sa_family) {
141 #ifdef INET
142 case AF_INET:
143
144 /*
145 * For now, use the simple IP addr -> ARCnet addr mapping
146 */
147 if (m->m_flags & (M_BCAST|M_MCAST))
148 adst = arcbroadcastaddr; /* ARCnet broadcast address */
149 else if (ifp->if_flags & IFF_NOARP)
150 adst = ntohl(satocsin(dst)->sin_addr.s_addr) & 0xFF;
151 else if ((error = arpresolve(ifp, rt, m, dst, &adst,
152 sizeof(adst))) != 0)
153 return error == EWOULDBLOCK ? 0 : error;
154
155 /* If broadcasting on a simplex interface, loopback a copy */
156 if ((m->m_flags & (M_BCAST|M_MCAST)) &&
157 (ifp->if_flags & IFF_SIMPLEX))
158 mcopy = m_copypacket(m, M_DONTWAIT);
159 if (ifp->if_flags & IFF_LINK0) {
160 atype = ARCTYPE_IP;
161 newencoding = 1;
162 } else {
163 atype = ARCTYPE_IP_OLD;
164 newencoding = 0;
165 }
166 break;
167
168 case AF_ARP:
169 arph = mtod(m, struct arphdr *);
170 if (m->m_flags & M_BCAST)
171 adst = arcbroadcastaddr;
172 else {
173 uint8_t *tha = ar_tha(arph);
174 if (tha == NULL) {
175 m_freem(m);
176 return 0;
177 }
178 adst = *tha;
179 }
180
181 arph->ar_hrd = htons(ARPHRD_ARCNET);
182
183 switch (ntohs(arph->ar_op)) {
184 case ARPOP_REVREQUEST:
185 case ARPOP_REVREPLY:
186 if (!(ifp->if_flags & IFF_LINK0)) {
187 printf("%s: can't handle af%d\n",
188 ifp->if_xname, dst->sa_family);
189 senderr(EAFNOSUPPORT);
190 }
191
192 atype = htons(ARCTYPE_REVARP);
193 newencoding = 1;
194 break;
195
196 case ARPOP_REQUEST:
197 case ARPOP_REPLY:
198 default:
199 if (ifp->if_flags & IFF_LINK0) {
200 atype = htons(ARCTYPE_ARP);
201 newencoding = 1;
202 } else {
203 atype = htons(ARCTYPE_ARP_OLD);
204 newencoding = 0;
205 }
206 }
207 #ifdef ARCNET_ALLOW_BROKEN_ARP
208 /*
209 * XXX It's not clear per RFC826 if this is needed, but
210 * "assigned numbers" say this is wrong.
211 * However, e.g., AmiTCP 3.0Beta used it... we make this
212 * switchable for emergency cases. Not perfect, but...
213 */
214 if (ifp->if_flags & IFF_LINK2)
215 arph->ar_pro = atype - 1;
216 #endif
217 break;
218 #endif
219 #ifdef INET6
220 case AF_INET6:
221 if (m->m_flags & M_MCAST) {
222 adst = 0;
223 } else {
224 error = nd6_resolve(ifp, rt, m, dst, &adst,
225 sizeof(adst));
226 if (error != 0)
227 return error == EWOULDBLOCK ? 0 : error;
228 }
229 atype = htons(ARCTYPE_INET6);
230 newencoding = 1;
231 break;
232 #endif
233
234 case AF_UNSPEC:
235 cah = (const struct arc_header *)dst->sa_data;
236 adst = cah->arc_dhost;
237 atype = cah->arc_type;
238 break;
239
240 default:
241 printf("%s: can't handle af%d\n", ifp->if_xname,
242 dst->sa_family);
243 senderr(EAFNOSUPPORT);
244 }
245
246 if (mcopy)
247 (void) looutput(ifp, mcopy, dst, rt);
248
249 /*
250 * Add local net header. If no space in first mbuf,
251 * allocate another.
252 *
253 * For ARCnet, this is just symbolic. The header changes
254 * form and position on its way into the hardware and out of
255 * the wire. At this point, it contains source, destination and
256 * packet type.
257 */
258 if (newencoding) {
259 ++ac->ac_seqid; /* make the seqid unique */
260
261 tfrags = (m->m_pkthdr.len + 503) / 504;
262 fsflag = 2 * tfrags - 3;
263 sflag = 0;
264 rsflag = fsflag;
265
266 while (sflag < fsflag) {
267 /* we CAN'T have short packets here */
268 m1 = m_split(m, 504, M_DONTWAIT);
269 if (m1 == 0)
270 senderr(ENOBUFS);
271
272 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
273 if (m == 0)
274 senderr(ENOBUFS);
275 ah = mtod(m, struct arc_header *);
276 ah->arc_type = atype;
277 ah->arc_dhost = adst;
278 ah->arc_shost = myself;
279 ah->arc_flag = rsflag;
280 ah->arc_seqid = ac->ac_seqid;
281
282 if ((error = ifq_enqueue(ifp, m)) != 0)
283 return (error);
284
285 m = m1;
286 sflag += 2;
287 rsflag = sflag;
288 }
289 m1 = NULL;
290
291
292 /* here we can have small, especially forbidden packets */
293
294 if ((m->m_pkthdr.len >=
295 ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
296 (m->m_pkthdr.len <=
297 ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
298
299 M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
300 if (m == 0)
301 senderr(ENOBUFS);
302 ah = mtod(m, struct arc_header *);
303 ah->arc_flag = 0xFF;
304 ah->arc_seqid = 0xFFFF;
305 ah->arc_type2 = atype;
306 ah->arc_flag2 = sflag;
307 ah->arc_seqid2 = ac->ac_seqid;
308 } else {
309 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
310 if (m == 0)
311 senderr(ENOBUFS);
312 ah = mtod(m, struct arc_header *);
313 ah->arc_flag = sflag;
314 ah->arc_seqid = ac->ac_seqid;
315 }
316
317 ah->arc_dhost = adst;
318 ah->arc_shost = myself;
319 ah->arc_type = atype;
320 } else {
321 M_PREPEND(m, ARC_HDRLEN, M_DONTWAIT);
322 if (m == 0)
323 senderr(ENOBUFS);
324 ah = mtod(m, struct arc_header *);
325 ah->arc_type = atype;
326 ah->arc_dhost = adst;
327 ah->arc_shost = myself;
328 }
329
330 return ifq_enqueue(ifp, m);
331
332 bad:
333 if (m1)
334 m_freem(m1);
335 if (m)
336 m_freem(m);
337 return (error);
338 }
339
340 /*
341 * Defragmenter. Returns mbuf if last packet found, else
342 * NULL. frees imcoming mbuf as necessary.
343 */
344
345 static struct mbuf *
346 arc_defrag(struct ifnet *ifp, struct mbuf *m)
347 {
348 struct arc_header *ah, *ah1;
349 struct arccom *ac;
350 struct ac_frag *af;
351 struct mbuf *m1;
352 const char *s;
353 int newflen;
354 u_char src, dst, typ;
355
356 ac = (struct arccom *)ifp;
357
358 if (m->m_len < ARC_HDRNEWLEN) {
359 m = m_pullup(m, ARC_HDRNEWLEN);
360 if (m == NULL) {
361 if_statinc(ifp, if_ierrors);
362 return NULL;
363 }
364 }
365
366 ah = mtod(m, struct arc_header *);
367 typ = ah->arc_type;
368
369 if (!arc_isphds(typ))
370 return m;
371
372 src = ah->arc_shost;
373 dst = ah->arc_dhost;
374
375 if (ah->arc_flag == 0xff) {
376 m_adj(m, 4);
377
378 if (m->m_len < ARC_HDRNEWLEN) {
379 m = m_pullup(m, ARC_HDRNEWLEN);
380 if (m == NULL) {
381 if_statinc(ifp, if_ierrors);
382 return NULL;
383 }
384 }
385
386 ah = mtod(m, struct arc_header *);
387 }
388
389 af = &ac->ac_fragtab[src];
390 m1 = af->af_packet;
391 s = "debug code error";
392
393 if (ah->arc_flag & 1) {
394 /*
395 * first fragment. We always initialize, which is
396 * about the right thing to do, as we only want to
397 * accept one fragmented packet per src at a time.
398 */
399 if (m1 != NULL)
400 m_freem(m1);
401
402 af->af_packet = m;
403 m1 = m;
404 af->af_maxflag = ah->arc_flag;
405 af->af_lastseen = 0;
406 af->af_seqid = ah->arc_seqid;
407
408 return NULL;
409 /* notreached */
410 } else {
411 /* check for unfragmented packet */
412 if (ah->arc_flag == 0)
413 return m;
414
415 /* do we have a first packet from that src? */
416 if (m1 == NULL) {
417 s = "no first frag";
418 goto outofseq;
419 }
420
421 ah1 = mtod(m1, struct arc_header *);
422
423 if (ah->arc_seqid != ah1->arc_seqid) {
424 s = "seqid differs";
425 goto outofseq;
426 }
427
428 if (typ != ah1->arc_type) {
429 s = "type differs";
430 goto outofseq;
431 }
432
433 if (dst != ah1->arc_dhost) {
434 s = "dest host differs";
435 goto outofseq;
436 }
437
438 /* typ, seqid and dst are ok here. */
439
440 if (ah->arc_flag == af->af_lastseen) {
441 m_freem(m);
442 return NULL;
443 }
444
445 if (ah->arc_flag == af->af_lastseen + 2) {
446 /* ok, this is next fragment */
447 af->af_lastseen = ah->arc_flag;
448 m_adj(m, ARC_HDRNEWLEN);
449
450 /*
451 * m_cat might free the first mbuf (with pkthdr)
452 * in 2nd chain; therefore:
453 */
454
455 newflen = m->m_pkthdr.len;
456
457 m_cat(m1, m);
458
459 m1->m_pkthdr.len += newflen;
460
461 /* is it the last one? */
462 if (af->af_lastseen > af->af_maxflag) {
463 af->af_packet = NULL;
464 return (m1);
465 } else
466 return NULL;
467 }
468 s = "other reason";
469 /* if all else fails, it is out of sequence, too */
470 }
471 outofseq:
472 if (m1) {
473 m_freem(m1);
474 af->af_packet = NULL;
475 }
476
477 if (m)
478 m_freem(m);
479
480 log(LOG_INFO,"%s: got out of seq. packet: %s\n",
481 ifp->if_xname, s);
482
483 return NULL;
484 }
485
486 /*
487 * return 1 if Packet Header Definition Standard, else 0.
488 * For now: old IP, old ARP aren't obviously. Lacking correct information,
489 * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
490 * (Apple and Novell corporations were involved, among others, in PHDS work).
491 * Easiest is to assume that everybody else uses that, too.
492 */
493 int
494 arc_isphds(uint8_t type)
495 {
496 return (type != ARCTYPE_IP_OLD &&
497 type != ARCTYPE_ARP_OLD &&
498 type != ARCTYPE_DIAGNOSE);
499 }
500
501 /*
502 * Process a received Arcnet packet;
503 * the packet is in the mbuf chain m with
504 * the ARCnet header.
505 */
506 static void
507 arc_input(struct ifnet *ifp, struct mbuf *m)
508 {
509 pktqueue_t *pktq = NULL;
510 struct arc_header *ah;
511 struct ifqueue *inq;
512 uint8_t atype;
513 int isr = 0;
514
515 if ((ifp->if_flags & IFF_UP) == 0) {
516 m_freem(m);
517 return;
518 }
519
520 /* possibly defragment: */
521 m = arc_defrag(ifp, m);
522 if (m == NULL)
523 return;
524
525 ah = mtod(m, struct arc_header *);
526
527 if_statadd(ifp, if_ibytes, m->m_pkthdr.len);
528
529 if (arcbroadcastaddr == ah->arc_dhost) {
530 m->m_flags |= M_BCAST|M_MCAST;
531 if_statinc(ifp, if_imcasts);
532 }
533
534 atype = ah->arc_type;
535 switch (atype) {
536 #ifdef INET
537 case ARCTYPE_IP:
538 m_adj(m, ARC_HDRNEWLEN);
539 pktq = ip_pktq;
540 break;
541
542 case ARCTYPE_IP_OLD:
543 m_adj(m, ARC_HDRLEN);
544 pktq = ip_pktq;
545 break;
546
547 case ARCTYPE_ARP:
548 m_adj(m, ARC_HDRNEWLEN);
549 isr = NETISR_ARP;
550 inq = &arpintrq;
551 #ifdef ARCNET_ALLOW_BROKEN_ARP
552 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
553 #endif
554 break;
555
556 case ARCTYPE_ARP_OLD:
557 m_adj(m, ARC_HDRLEN);
558 isr = NETISR_ARP;
559 inq = &arpintrq;
560 #ifdef ARCNET_ALLOW_BROKEN_ARP
561 mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
562 #endif
563 break;
564 #endif
565 #ifdef INET6
566 case ARCTYPE_INET6:
567 m_adj(m, ARC_HDRNEWLEN);
568 pktq = ip6_pktq;
569 break;
570 #endif
571 default:
572 m_freem(m);
573 return;
574 }
575
576 if (__predict_true(pktq)) {
577 if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
578 m_freem(m);
579 }
580 return;
581 }
582
583 IFQ_LOCK(inq);
584 if (IF_QFULL(inq)) {
585 IF_DROP(inq);
586 IFQ_UNLOCK(inq);
587 m_freem(m);
588 } else {
589 IF_ENQUEUE(inq, m);
590 IFQ_UNLOCK(inq);
591 schednetisr(isr);
592 }
593 }
594
595 /*
596 * Convert Arcnet address to printable (loggable) representation.
597 */
598 char *
599 arc_sprintf(uint8_t *ap)
600 {
601 static char arcbuf[3];
602 char *cp = arcbuf;
603
604 *cp++ = hexdigits[*ap >> 4];
605 *cp++ = hexdigits[*ap++ & 0xf];
606 *cp = 0;
607 return (arcbuf);
608 }
609
610 /*
611 * Perform common duties while attaching to interface list
612 */
613 int
614 arc_ifattach(struct ifnet *ifp, uint8_t lla)
615 {
616 struct arccom *ac;
617 int rv;
618
619 ifp->if_type = IFT_ARCNET;
620 ifp->if_addrlen = 1;
621 ifp->if_hdrlen = ARC_HDRLEN;
622 ifp->if_dlt = DLT_ARCNET;
623 if (ifp->if_flags & IFF_BROADCAST)
624 ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
625 if (ifp->if_flags & IFF_LINK0 && arc_ipmtu > ARC_PHDS_MAXMTU)
626 log(LOG_ERR,
627 "%s: arc_ipmtu is %d, but must not exceed %d\n",
628 ifp->if_xname, arc_ipmtu, ARC_PHDS_MAXMTU);
629
630 ifp->if_output = arc_output;
631 ifp->_if_input = arc_input;
632 ac = (struct arccom *)ifp;
633 ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
634 if (lla == 0) {
635 /* XXX this message isn't entirely clear, to me -- cgd */
636 log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n",
637 ifp->if_xname, ifp->if_xname);
638 }
639 rv = if_attach(ifp);
640 if (rv != 0)
641 return rv;
642
643 if_set_sadl(ifp, &lla, sizeof(lla), true);
644
645 ifp->if_broadcastaddr = &arcbroadcastaddr;
646
647 bpf_attach(ifp, DLT_ARCNET, ARC_HDRLEN);
648
649 return 0;
650 }
651