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