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