xref: /netbsd-src/sys/net/if_arcsubr.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: if_arcsubr.c,v 1.69 2015/10/13 12:33:07 roy 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.69 2015/10/13 12:33:07 roy 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)) != 0)
155 			return error == EWOULDBLOCK ? 0 : error;
156 
157 		/* If broadcasting on a simplex interface, loopback a copy */
158 		if ((m->m_flags & (M_BCAST|M_MCAST)) &&
159 		    (ifp->if_flags & IFF_SIMPLEX))
160 			mcopy = m_copy(m, 0, (int)M_COPYALL);
161 		if (ifp->if_flags & IFF_LINK0) {
162 			atype = ARCTYPE_IP;
163 			newencoding = 1;
164 		} else {
165 			atype = ARCTYPE_IP_OLD;
166 			newencoding = 0;
167 		}
168 		break;
169 
170 	case AF_ARP:
171 		arph = mtod(m, struct arphdr *);
172 		if (m->m_flags & M_BCAST)
173 			adst = arcbroadcastaddr;
174 		else {
175 			uint8_t *tha = ar_tha(arph);
176 			if (tha == NULL)
177 				return 0;
178 			adst = *tha;
179 		}
180 
181 		arph->ar_hrd = htons(ARPHRD_ARCNET);
182 
183 		switch (ntohs(arph->ar_op)) {
184 		case ARPOP_REVREQUEST:
185 		case ARPOP_REVREPLY:
186 			if (!(ifp->if_flags & IFF_LINK0)) {
187 				printf("%s: can't handle af%d\n",
188 				    ifp->if_xname, dst->sa_family);
189 				senderr(EAFNOSUPPORT);
190 			}
191 
192 			atype = htons(ARCTYPE_REVARP);
193 			newencoding = 1;
194 			break;
195 
196 		case ARPOP_REQUEST:
197 		case ARPOP_REPLY:
198 		default:
199 			if (ifp->if_flags & IFF_LINK0) {
200 				atype = htons(ARCTYPE_ARP);
201 				newencoding = 1;
202 			} else {
203 				atype = htons(ARCTYPE_ARP_OLD);
204 				newencoding = 0;
205 			}
206 		}
207 #ifdef ARCNET_ALLOW_BROKEN_ARP
208 		/*
209 		 * XXX It's not clear per RFC826 if this is needed, but
210 		 * "assigned numbers" say this is wrong.
211 		 * However, e.g., AmiTCP 3.0Beta used it... we make this
212 		 * switchable for emergency cases. Not perfect, but...
213 		 */
214 		if (ifp->if_flags & IFF_LINK2)
215 			arph->ar_pro = atype - 1;
216 #endif
217 		break;
218 #endif
219 #ifdef INET6
220 	case AF_INET6:
221 		if (!nd6_storelladdr(ifp, rt, m, dst, &adst, sizeof(adst)))
222 			return (0); /* it must be impossible, but... */
223 		atype = htons(ARCTYPE_INET6);
224 		newencoding = 1;
225 		break;
226 #endif
227 
228 	case AF_UNSPEC:
229 		cah = (const struct arc_header *)dst->sa_data;
230  		adst = cah->arc_dhost;
231 		atype = cah->arc_type;
232 		break;
233 
234 	default:
235 		printf("%s: can't handle af%d\n", ifp->if_xname,
236 		    dst->sa_family);
237 		senderr(EAFNOSUPPORT);
238 	}
239 
240 	if (mcopy)
241 		(void) looutput(ifp, mcopy, dst, rt);
242 
243 	/*
244 	 * Add local net header.  If no space in first mbuf,
245 	 * allocate another.
246 	 *
247 	 * For ARCnet, this is just symbolic. The header changes
248 	 * form and position on its way into the hardware and out of
249 	 * the wire.  At this point, it contains source, destination and
250 	 * packet type.
251 	 */
252 	if (newencoding) {
253 		++ac->ac_seqid; /* make the seqid unique */
254 
255 		tfrags = (m->m_pkthdr.len + 503) / 504;
256 		fsflag = 2 * tfrags - 3;
257 		sflag = 0;
258 		rsflag = fsflag;
259 
260 		while (sflag < fsflag) {
261 			/* we CAN'T have short packets here */
262 			m1 = m_split(m, 504, M_DONTWAIT);
263 			if (m1 == 0)
264 				senderr(ENOBUFS);
265 
266 			M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
267 			if (m == 0)
268 				senderr(ENOBUFS);
269 			ah = mtod(m, struct arc_header *);
270 			ah->arc_type = atype;
271 			ah->arc_dhost = adst;
272 			ah->arc_shost = myself;
273 			ah->arc_flag = rsflag;
274 			ah->arc_seqid = ac->ac_seqid;
275 
276 			if ((error = ifq_enqueue(ifp, m ALTQ_COMMA
277 			    ALTQ_DECL(&pktattr))) != 0)
278 				return (error);
279 
280 			m = m1;
281 			sflag += 2;
282 			rsflag = sflag;
283 		}
284 		m1 = NULL;
285 
286 
287 		/* here we can have small, especially forbidden packets */
288 
289 		if ((m->m_pkthdr.len >=
290 		    ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
291 		    (m->m_pkthdr.len <=
292 		    ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
293 
294 			M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_DONTWAIT);
295 			if (m == 0)
296 				senderr(ENOBUFS);
297 			ah = mtod(m, struct arc_header *);
298 			ah->arc_flag = 0xFF;
299 			ah->arc_seqid = 0xFFFF;
300 			ah->arc_type2 = atype;
301 			ah->arc_flag2 = sflag;
302 			ah->arc_seqid2 = ac->ac_seqid;
303 		} else {
304 			M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
305 			if (m == 0)
306 				senderr(ENOBUFS);
307 			ah = mtod(m, struct arc_header *);
308 			ah->arc_flag = sflag;
309 			ah->arc_seqid = ac->ac_seqid;
310 		}
311 
312 		ah->arc_dhost = adst;
313 		ah->arc_shost = myself;
314 		ah->arc_type = atype;
315 	} else {
316 		M_PREPEND(m, ARC_HDRLEN, M_DONTWAIT);
317 		if (m == 0)
318 			senderr(ENOBUFS);
319 		ah = mtod(m, struct arc_header *);
320 		ah->arc_type = atype;
321 		ah->arc_dhost = adst;
322 		ah->arc_shost = myself;
323 	}
324 
325 	return ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(&pktattr));
326 
327 bad:
328 	if (m1)
329 		m_freem(m1);
330 	if (m)
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 			++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 				++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 		if (m1 != NULL)
395 			m_freem(m1);
396 
397 		af->af_packet = m;
398 		m1 = m;
399 		af->af_maxflag = ah->arc_flag;
400 		af->af_lastseen = 0;
401 		af->af_seqid = ah->arc_seqid;
402 
403 		return NULL;
404 		/* notreached */
405 	} else {
406 		/* check for unfragmented packet */
407 		if (ah->arc_flag == 0)
408 			return m;
409 
410 		/* do we have a first packet from that src? */
411 		if (m1 == NULL) {
412 			s = "no first frag";
413 			goto outofseq;
414 		}
415 
416 		ah1 = mtod(m1, struct arc_header *);
417 
418 		if (ah->arc_seqid != ah1->arc_seqid) {
419 			s = "seqid differs";
420 			goto outofseq;
421 		}
422 
423 		if (typ != ah1->arc_type) {
424 			s = "type differs";
425 			goto outofseq;
426 		}
427 
428 		if (dst != ah1->arc_dhost) {
429 			s = "dest host differs";
430 			goto outofseq;
431 		}
432 
433 		/* typ, seqid and dst are ok here. */
434 
435 		if (ah->arc_flag == af->af_lastseen) {
436 			m_freem(m);
437 			return NULL;
438 		}
439 
440 		if (ah->arc_flag == af->af_lastseen + 2) {
441 			/* ok, this is next fragment */
442 			af->af_lastseen = ah->arc_flag;
443 			m_adj(m, ARC_HDRNEWLEN);
444 
445 			/*
446 			 * m_cat might free the first mbuf (with pkthdr)
447 			 * in 2nd chain; therefore:
448 			 */
449 
450 			newflen = m->m_pkthdr.len;
451 
452 			m_cat(m1, m);
453 
454 			m1->m_pkthdr.len += newflen;
455 
456 			/* is it the last one? */
457 			if (af->af_lastseen > af->af_maxflag) {
458 				af->af_packet = NULL;
459 				return (m1);
460 			} else
461 				return NULL;
462 		}
463 		s = "other reason";
464 		/* if all else fails, it is out of sequence, too */
465 	}
466 outofseq:
467 	if (m1) {
468 		m_freem(m1);
469 		af->af_packet = NULL;
470 	}
471 
472 	if (m)
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
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
502 arc_input(struct ifnet *ifp, struct mbuf *m)
503 {
504 	pktqueue_t *pktq = NULL;
505 	struct arc_header *ah;
506 	struct ifqueue *inq;
507 	uint8_t atype;
508 	int isr = 0;
509 	int s;
510 
511 	if ((ifp->if_flags & IFF_UP) == 0) {
512 		m_freem(m);
513 		return;
514 	}
515 
516 	/* possibly defragment: */
517 	m = arc_defrag(ifp, m);
518 	if (m == NULL)
519 		return;
520 
521 	ah = mtod(m, struct arc_header *);
522 
523 	ifp->if_ibytes += m->m_pkthdr.len;
524 
525 	if (arcbroadcastaddr == ah->arc_dhost) {
526 		m->m_flags |= M_BCAST|M_MCAST;
527 		ifp->if_imcasts++;
528 	}
529 
530 	atype = ah->arc_type;
531 	switch (atype) {
532 #ifdef INET
533 	case ARCTYPE_IP:
534 		m_adj(m, ARC_HDRNEWLEN);
535 		pktq = ip_pktq;
536 		break;
537 
538 	case ARCTYPE_IP_OLD:
539 		m_adj(m, ARC_HDRLEN);
540 		pktq = ip_pktq;
541 		break;
542 
543 	case ARCTYPE_ARP:
544 		m_adj(m, ARC_HDRNEWLEN);
545 		isr = NETISR_ARP;
546 		inq = &arpintrq;
547 #ifdef ARCNET_ALLOW_BROKEN_ARP
548 		mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
549 #endif
550 		break;
551 
552 	case ARCTYPE_ARP_OLD:
553 		m_adj(m, ARC_HDRLEN);
554 		isr = NETISR_ARP;
555 		inq = &arpintrq;
556 #ifdef ARCNET_ALLOW_BROKEN_ARP
557 		mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP);
558 #endif
559 		break;
560 #endif
561 #ifdef INET6
562 	case ARCTYPE_INET6:
563 		m_adj(m, ARC_HDRNEWLEN);
564 		pktq = ip6_pktq;
565 		break;
566 #endif
567 	default:
568 		m_freem(m);
569 		return;
570 	}
571 
572 	s = splnet();
573 	if (__predict_true(pktq)) {
574 		if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
575 			m_freem(m);
576 		}
577 		splx(s);
578 		return;
579 	}
580 	if (IF_QFULL(inq)) {
581 		IF_DROP(inq);
582 		m_freem(m);
583 	} else {
584 		IF_ENQUEUE(inq, m);
585 		schednetisr(isr);
586 	}
587 	splx(s);
588 }
589 
590 /*
591  * Convert Arcnet address to printable (loggable) representation.
592  */
593 char *
594 arc_sprintf(uint8_t *ap)
595 {
596 	static char arcbuf[3];
597 	char *cp = arcbuf;
598 
599 	*cp++ = hexdigits[*ap >> 4];
600 	*cp++ = hexdigits[*ap++ & 0xf];
601 	*cp   = 0;
602 	return (arcbuf);
603 }
604 
605 /*
606  * Perform common duties while attaching to interface list
607  */
608 void
609 arc_ifattach(struct ifnet *ifp, uint8_t lla)
610 {
611 	struct arccom *ac;
612 
613 	ifp->if_type = IFT_ARCNET;
614 	ifp->if_addrlen = 1;
615 	ifp->if_hdrlen = ARC_HDRLEN;
616 	ifp->if_dlt = DLT_ARCNET;
617 	if (ifp->if_flags & IFF_BROADCAST)
618 		ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
619 	if (ifp->if_flags & IFF_LINK0 && arc_ipmtu > ARC_PHDS_MAXMTU)
620 		log(LOG_ERR,
621 		    "%s: arc_ipmtu is %d, but must not exceed %d\n",
622 		    ifp->if_xname, arc_ipmtu, ARC_PHDS_MAXMTU);
623 
624 	ifp->if_output = arc_output;
625 	ifp->if_input = arc_input;
626 	ac = (struct arccom *)ifp;
627 	ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */
628 	if (lla == 0) {
629 		/* XXX this message isn't entirely clear, to me -- cgd */
630 		log(LOG_ERR,"%s: link address 0 reserved for broadcasts.  Please change it and ifconfig %s down up\n",
631 		   ifp->if_xname, ifp->if_xname);
632 	}
633 	if_attach(ifp);
634 	if_set_sadl(ifp, &lla, sizeof(lla), true);
635 
636 	ifp->if_broadcastaddr = &arcbroadcastaddr;
637 
638 	bpf_attach(ifp, DLT_ARCNET, ARC_HDRLEN);
639 }
640