1 /*        $NetBSD: if_arcsubr.c,v 1.86 2024/07/05 04:31:53 rin 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.86 2024/07/05 04:31:53 rin 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
arc_output(struct ifnet * ifp,struct mbuf * m0,const struct sockaddr * dst,const struct rtentry * rt)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                                         printf("%s: can't handle af%d\n",
187                                             ifp->if_xname, dst->sa_family);
188                                         senderr(EAFNOSUPPORT);
189                               }
190 
191                               atype = htons(ARCTYPE_REVARP);
192                               newencoding = 1;
193                               break;
194 
195                     case ARPOP_REQUEST:
196                     case ARPOP_REPLY:
197                     default:
198                               if (ifp->if_flags & IFF_LINK0) {
199                                         atype = htons(ARCTYPE_ARP);
200                                         newencoding = 1;
201                               } else {
202                                         atype = htons(ARCTYPE_ARP_OLD);
203                                         newencoding = 0;
204                               }
205                     }
206 #ifdef ARCNET_ALLOW_BROKEN_ARP
207                     /*
208                      * XXX It's not clear per RFC826 if this is needed, but
209                      * "assigned numbers" say this is wrong.
210                      * However, e.g., AmiTCP 3.0Beta used it... we make this
211                      * switchable for emergency cases. Not perfect, but...
212                      */
213                     if (ifp->if_flags & IFF_LINK2)
214                               arph->ar_pro = atype - 1;
215 #endif
216                     break;
217 #endif
218 #ifdef INET6
219           case AF_INET6:
220                     if (m->m_flags & M_MCAST) {
221                               adst = 0;
222                     } else {
223                               error = nd6_resolve(ifp, rt, m, dst, &adst,
224                                   sizeof(adst));
225                               if (error != 0)
226                                         return error == EWOULDBLOCK ? 0 : error;
227                     }
228                     atype = htons(ARCTYPE_INET6);
229                     newencoding = 1;
230                     break;
231 #endif
232 
233           case AF_UNSPEC:
234                     cah = (const struct arc_header *)dst->sa_data;
235                     adst = cah->arc_dhost;
236                     atype = cah->arc_type;
237                     break;
238 
239           default:
240                     printf("%s: can't handle af%d\n", ifp->if_xname,
241                         dst->sa_family);
242                     senderr(EAFNOSUPPORT);
243           }
244 
245           if (mcopy)
246                     (void) looutput(ifp, mcopy, dst, rt);
247 
248           /*
249            * Add local net header.  If no space in first mbuf,
250            * allocate another.
251            *
252            * For ARCnet, this is just symbolic. The header changes
253            * form and position on its way into the hardware and out of
254            * the wire.  At this point, it contains source, destination and
255            * packet type.
256            */
257           if (newencoding) {
258                     ++ac->ac_seqid; /* make the seqid unique */
259 
260                     tfrags = (m->m_pkthdr.len + 503) / 504;
261                     fsflag = 2 * tfrags - 3;
262                     sflag = 0;
263                     rsflag = fsflag;
264 
265                     while (sflag < fsflag) {
266                               /* we CAN'T have short packets here */
267                               m1 = m_split(m, 504, M_DONTWAIT);
268                               if (m1 == 0)
269                                         senderr(ENOBUFS);
270 
271                               M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
272                               if (m == 0)
273                                         senderr(ENOBUFS);
274                               ah = mtod(m, struct arc_header *);
275                               ah->arc_type = atype;
276                               ah->arc_dhost = adst;
277                               ah->arc_shost = myself;
278                               ah->arc_flag = rsflag;
279                               ah->arc_seqid = ac->ac_seqid;
280 
281                               if ((error = ifq_enqueue(ifp, m)) != 0)
282                                         return (error);
283 
284                               m = m1;
285                               sflag += 2;
286                               rsflag = sflag;
287                     }
288                     m1 = NULL;
289 
290 
291                     /* here we can have small, especially forbidden packets */
292 
293                     if ((m->m_pkthdr.len >=
294                         ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
295                         (m->m_pkthdr.len <=
296                         ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
297 
298                               M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
299                               if (m == 0)
300                                         senderr(ENOBUFS);
301                               ah = mtod(m, struct arc_header *);
302                               ah->arc_flag = 0xFF;
303                               ah->arc_seqid = 0xFFFF;
304                               ah->arc_type2 = atype;
305                               ah->arc_flag2 = sflag;
306                               ah->arc_seqid2 = ac->ac_seqid;
307                     } else {
308                               M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
309                               if (m == 0)
310                                         senderr(ENOBUFS);
311                               ah = mtod(m, struct arc_header *);
312                               ah->arc_flag = sflag;
313                               ah->arc_seqid = ac->ac_seqid;
314                     }
315 
316                     ah->arc_dhost = adst;
317                     ah->arc_shost = myself;
318                     ah->arc_type = atype;
319           } else {
320                     M_PREPEND(m, ARC_HDRLEN, M_DONTWAIT);
321                     if (m == 0)
322                               senderr(ENOBUFS);
323                     ah = mtod(m, struct arc_header *);
324                     ah->arc_type = atype;
325                     ah->arc_dhost = adst;
326                     ah->arc_shost = myself;
327           }
328 
329           return ifq_enqueue(ifp, m);
330 
331 bad:
332           m_freem(m1);
333           m_freem(m);
334           return (error);
335 }
336 
337 /*
338  * Defragmenter. Returns mbuf if last packet found, else
339  * NULL. frees imcoming mbuf as necessary.
340  */
341 
342 static struct mbuf *
arc_defrag(struct ifnet * ifp,struct mbuf * m)343 arc_defrag(struct ifnet *ifp, struct mbuf *m)
344 {
345           struct arc_header *ah, *ah1;
346           struct arccom *ac;
347           struct ac_frag *af;
348           struct mbuf *m1;
349           const char *s;
350           int newflen;
351           u_char src, dst, typ;
352 
353           ac = (struct arccom *)ifp;
354 
355           if (m->m_len < ARC_HDRNEWLEN) {
356                     m = m_pullup(m, ARC_HDRNEWLEN);
357                     if (m == NULL) {
358                               if_statinc(ifp, if_ierrors);
359                               return NULL;
360                     }
361           }
362 
363           ah = mtod(m, struct arc_header *);
364           typ = ah->arc_type;
365 
366           if (!arc_isphds(typ))
367                     return m;
368 
369           src = ah->arc_shost;
370           dst = ah->arc_dhost;
371 
372           if (ah->arc_flag == 0xff) {
373                     m_adj(m, 4);
374 
375                     if (m->m_len < ARC_HDRNEWLEN) {
376                               m = m_pullup(m, ARC_HDRNEWLEN);
377                               if (m == NULL) {
378                                         if_statinc(ifp, if_ierrors);
379                                         return NULL;
380                               }
381                     }
382 
383                     ah = mtod(m, struct arc_header *);
384           }
385 
386           af = &ac->ac_fragtab[src];
387           m1 = af->af_packet;
388           s = "debug code error";
389 
390           if (ah->arc_flag & 1) {
391                     /*
392                      * first fragment. We always initialize, which is
393                      * about the right thing to do, as we only want to
394                      * accept one fragmented packet per src at a time.
395                      */
396                     m_freem(m1);
397 
398                     af->af_packet = m;
399                     m1 = m;
400                     af->af_maxflag = ah->arc_flag;
401                     af->af_lastseen = 0;
402                     af->af_seqid = ah->arc_seqid;
403 
404                     return NULL;
405                     /* notreached */
406           } else {
407                     /* check for unfragmented packet */
408                     if (ah->arc_flag == 0)
409                               return m;
410 
411                     /* do we have a first packet from that src? */
412                     if (m1 == NULL) {
413                               s = "no first frag";
414                               goto outofseq;
415                     }
416 
417                     ah1 = mtod(m1, struct arc_header *);
418 
419                     if (ah->arc_seqid != ah1->arc_seqid) {
420                               s = "seqid differs";
421                               goto outofseq;
422                     }
423 
424                     if (typ != ah1->arc_type) {
425                               s = "type differs";
426                               goto outofseq;
427                     }
428 
429                     if (dst != ah1->arc_dhost) {
430                               s = "dest host differs";
431                               goto outofseq;
432                     }
433 
434                     /* typ, seqid and dst are ok here. */
435 
436                     if (ah->arc_flag == af->af_lastseen) {
437                               m_freem(m);
438                               return NULL;
439                     }
440 
441                     if (ah->arc_flag == af->af_lastseen + 2) {
442                               /* ok, this is next fragment */
443                               af->af_lastseen = ah->arc_flag;
444                               m_adj(m, ARC_HDRNEWLEN);
445 
446                               /*
447                                * m_cat might free the first mbuf (with pkthdr)
448                                * in 2nd chain; therefore:
449                                */
450 
451                               newflen = m->m_pkthdr.len;
452 
453                               m_cat(m1, m);
454 
455                               m1->m_pkthdr.len += newflen;
456 
457                               /* is it the last one? */
458                               if (af->af_lastseen > af->af_maxflag) {
459                                         af->af_packet = NULL;
460                                         return (m1);
461                               } else
462                                         return NULL;
463                     }
464                     s = "other reason";
465                     /* if all else fails, it is out of sequence, too */
466           }
467 outofseq:
468           if (m1) {
469                     m_freem(m1);
470                     af->af_packet = NULL;
471           }
472 
473           m_freem(m);
474 
475           log(LOG_INFO,"%s: got out of seq. packet: %s\n",
476               ifp->if_xname, s);
477 
478           return NULL;
479 }
480 
481 /*
482  * return 1 if Packet Header Definition Standard, else 0.
483  * For now: old IP, old ARP aren't obviously. Lacking correct information,
484  * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
485  * (Apple and Novell corporations were involved, among others, in PHDS work).
486  * Easiest is to assume that everybody else uses that, too.
487  */
488 int
arc_isphds(uint8_t type)489 arc_isphds(uint8_t type)
490 {
491           return (type != ARCTYPE_IP_OLD &&
492                     type != ARCTYPE_ARP_OLD &&
493                     type != ARCTYPE_DIAGNOSE);
494 }
495 
496 /*
497  * Process a received Arcnet packet;
498  * the packet is in the mbuf chain m with
499  * the ARCnet header.
500  */
501 static void
arc_input(struct ifnet * ifp,struct mbuf * m)502 arc_input(struct ifnet *ifp, struct mbuf *m)
503 {
504           pktqueue_t *pktq = NULL;
505           struct arc_header *ah;
506           uint8_t atype;
507 
508           if ((ifp->if_flags & IFF_UP) == 0) {
509                     m_freem(m);
510                     return;
511           }
512 
513           /* possibly defragment: */
514           m = arc_defrag(ifp, m);
515           if (m == NULL)
516                     return;
517 
518           ah = mtod(m, struct arc_header *);
519 
520           if_statadd(ifp, if_ibytes, m->m_pkthdr.len);
521 
522           if (arcbroadcastaddr == ah->arc_dhost) {
523                     m->m_flags |= M_BCAST|M_MCAST;
524                     if_statinc(ifp, if_imcasts);
525           }
526 
527           atype = ah->arc_type;
528           switch (atype) {
529 #ifdef INET
530           case ARCTYPE_IP:
531                     m_adj(m, ARC_HDRNEWLEN);
532                     pktq = ip_pktq;
533                     break;
534 
535           case ARCTYPE_IP_OLD:
536                     m_adj(m, ARC_HDRLEN);
537                     pktq = ip_pktq;
538                     break;
539 
540           case ARCTYPE_ARP:
541                     m_adj(m, ARC_HDRNEWLEN);
542                     pktq = arp_pktq;
543 #ifdef ARCNET_ALLOW_BROKEN_ARP
544                     mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
545 #endif
546                     break;
547 
548           case ARCTYPE_ARP_OLD:
549                     m_adj(m, ARC_HDRLEN);
550                     pktq = arp_pktq;
551 #ifdef ARCNET_ALLOW_BROKEN_ARP
552                     mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
553 #endif
554                     break;
555 #endif
556 #ifdef INET6
557           case ARCTYPE_INET6:
558                     m_adj(m, ARC_HDRNEWLEN);
559                     pktq = ip6_pktq;
560                     break;
561 #endif
562           default:
563                     m_freem(m);
564                     return;
565           }
566 
567           KASSERT(pktq != NULL);
568           if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
569                     m_freem(m);
570           }
571 }
572 
573 /*
574  * Convert Arcnet address to printable (loggable) representation.
575  */
576 char *
arc_sprintf(uint8_t * ap)577 arc_sprintf(uint8_t *ap)
578 {
579           static char arcbuf[3];
580           char *cp = arcbuf;
581 
582           *cp++ = hexdigits[*ap >> 4];
583           *cp++ = hexdigits[*ap++ & 0xf];
584           *cp   = 0;
585           return (arcbuf);
586 }
587 
588 /*
589  * Perform common duties while attaching to interface list
590  */
591 int
arc_ifattach(struct ifnet * ifp,uint8_t lla)592 arc_ifattach(struct ifnet *ifp, uint8_t lla)
593 {
594           struct arccom *ac;
595 
596           ifp->if_type = IFT_ARCNET;
597           ifp->if_addrlen = 1;
598           ifp->if_hdrlen = ARC_HDRLEN;
599           ifp->if_dlt = DLT_ARCNET;
600           if (ifp->if_flags & IFF_BROADCAST)
601                     ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
602           if (ifp->if_flags & IFF_LINK0 && arc_ipmtu > ARC_PHDS_MAXMTU)
603                     log(LOG_ERR,
604                         "%s: arc_ipmtu is %d, but must not exceed %d\n",
605                         ifp->if_xname, arc_ipmtu, ARC_PHDS_MAXMTU);
606 
607           ifp->if_output = arc_output;
608           ifp->_if_input = arc_input;
609           ac = (struct arccom *)ifp;
610           ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
611           if (lla == 0) {
612                     /* XXX this message isn't entirely clear, to me -- cgd */
613                     log(LOG_ERR,"%s: link address 0 reserved for broadcasts.  Please change it and ifconfig %s down up\n",
614                        ifp->if_xname, ifp->if_xname);
615           }
616           if_attach(ifp);
617 
618           if_set_sadl(ifp, &lla, sizeof(lla), true);
619 
620           ifp->if_broadcastaddr = &arcbroadcastaddr;
621 
622           bpf_attach(ifp, DLT_ARCNET, ARC_HDRLEN);
623 
624           return 0;
625 }
626