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