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