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