if_arcsubr.c revision 1.4 1 /* $NetBSD: if_arcsubr.c,v 1.4 1995/06/07 00:13:52 cgd 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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp
37 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
38 *
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/syslog.h>
48 #include <sys/errno.h>
49
50 #include <machine/cpu.h>
51
52 #include <net/if.h>
53 #include <net/netisr.h>
54 #include <net/route.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
57
58 #ifdef INET
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #endif
62 #include <netinet/if_arc.h>
63
64 #ifndef ARC_PHDSMTU
65 #define ARC_PHDSMTU 1500
66 #endif
67
68 /* why isnt this in /sys/sys/mbuf.h?? */
69 extern struct mbuf *m_split __P((struct mbuf *, int, int));
70 static struct mbuf *arc_defrag __P((struct ifnet *, struct mbuf *));
71
72 /*
73 * RC1201 requires us to have this configurable. We have it only per
74 * machine at the moment... there is no generic "set mtu" ioctl, AFAICS.
75 * Anyway, it is possible to binpatch this or set it per kernel config
76 * option.
77 */
78 #if ARC_PHDSMTU > 60480
79 ERROR: The arc_phdsmtu is ARC_PHDSMTU, but must not exceed 60480.
80 #endif
81 u_int16_t arc_phdsmtu = ARC_PHDSMTU;
82 u_int8_t arcbroadcastaddr = 0;
83
84 #define senderr(e) { error = (e); goto bad;}
85 #define SIN(s) ((struct sockaddr_in *)s)
86
87 /*
88 * ARCnet output routine.
89 * Encapsulate a packet of type family for the local net.
90 * Assumes that ifp is actually pointer to arccom structure.
91 */
92 int
93 arc_output(ifp, m0, dst, rt0)
94 register struct ifnet *ifp;
95 struct mbuf *m0;
96 struct sockaddr *dst;
97 struct rtentry *rt0;
98 {
99 struct mbuf *m, *m1, *mcopy;
100 struct rtentry *rt;
101 struct arccom *ac;
102 register struct arc_header *ah;
103 int off, len;
104 int s, error, newencoding;
105 u_int8_t atype, adst;
106 int tfrags, sflag, fsflag, rsflag;
107
108 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
109 senderr(ENETDOWN);
110
111 error = newencoding = 0;
112 ac = (struct arccom *)ifp;
113 m = m0;
114 len = m->m_pkthdr.len;
115 mcopy = m1 = NULL;
116
117 ifp->if_lastchange = time;
118 if (rt = rt0) {
119 if ((rt->rt_flags & RTF_UP) == 0) {
120 if (rt0 = rt = rtalloc1(dst, 1))
121 rt->rt_refcnt--;
122 else
123 senderr(EHOSTUNREACH);
124 }
125 if (rt->rt_flags & RTF_GATEWAY) {
126 if (rt->rt_gwroute == 0)
127 goto lookup;
128 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
129 rtfree(rt); rt = rt0;
130 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
131 if ((rt = rt->rt_gwroute) == 0)
132 senderr(EHOSTUNREACH);
133 }
134 }
135 if (rt->rt_flags & RTF_REJECT)
136 if (rt->rt_rmx.rmx_expire == 0 ||
137 time.tv_sec < rt->rt_rmx.rmx_expire)
138 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
139 }
140
141 switch (dst->sa_family) {
142 #ifdef INET
143 case AF_INET:
144
145 /*
146 * For now, use the simple IP addr -> ARCnet addr mapping
147 */
148 if (m->m_flags & M_BCAST)
149 adst = arcbroadcastaddr; /* ARCnet broadcast address */
150 else
151 adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
152
153 /* If broadcasting on a simplex interface, loopback a copy */
154 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
155 mcopy = m_copy(m, 0, (int)M_COPYALL);
156 off = m->m_pkthdr.len - m->m_len;
157 if (ifp->if_flags & IFF_LINK0) {
158 atype = ARCTYPE_IP;
159 newencoding = 1;
160 } else {
161 atype = ARCTYPE_IP_OLD;
162 newencoding = 0;
163 }
164 break;
165 #endif
166
167 case AF_UNSPEC:
168 ah = (struct arc_header *)dst->sa_data;
169 adst = ah->arc_dhost;
170 atype = ah->arc_type;
171 break;
172
173 default:
174 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
175 dst->sa_family);
176 senderr(EAFNOSUPPORT);
177 }
178
179 if (mcopy)
180 (void) looutput(ifp, mcopy, dst, rt);
181
182 /*
183 * Add local net header. If no space in first mbuf,
184 * allocate another.
185 *
186 * For ARCnet, this is just symbolic. The header changes
187 * form and position on its way into the hardware and out of
188 * the wire. At this point, it contains source, destination and
189 * packet type.
190 */
191 if (newencoding) {
192 ++ac->ac_seqid; /* make the seqid unique */
193
194 tfrags = (len + 503) / 504;
195 fsflag = 2*tfrags-3;
196 sflag = 0;
197 rsflag = fsflag;
198
199 while (sflag < fsflag) {
200 /* we CAN'T have short packets here */
201 m1 = m_split(m, 504, M_DONTWAIT);
202 if (m1 == 0)
203 senderr(ENOBUFS);
204
205 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
206 if (m == 0)
207 senderr(ENOBUFS);
208 m = m_pullup(m,ARC_HDRNEWLEN);
209 if (m == 0)
210 senderr(ENOBUFS);
211
212 ah = mtod(m, struct arc_header *);
213 ah->arc_type = atype;
214 ah->arc_dhost = adst;
215 ah->arc_shost = ac->ac_anaddr;
216 ah->arc_flag = rsflag;
217 ah->arc_seqid = ac->ac_seqid;
218
219 s = splimp();
220 /*
221 * Queue message on interface, and start output if
222 * interface not yet active.
223 */
224 if (IF_QFULL(&ifp->if_snd)) {
225 IF_DROP(&ifp->if_snd);
226 splx(s);
227 senderr(ENOBUFS);
228 }
229 IF_ENQUEUE(&ifp->if_snd, m);
230 if ((ifp->if_flags & IFF_OACTIVE) == 0)
231 (*ifp->if_start)(ifp);
232 splx(s);
233
234 /* we don't count the hardwares lenght bytes here */
235 ifp->if_obytes += 504 + ARC_HDRNEWLEN;
236
237 m = m1;
238 len -= 504;
239 sflag += 2;
240 rsflag = sflag;
241 }
242 m1 = NULL;
243
244 M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
245 if (m == 0)
246 senderr(ENOBUFS);
247 m = m_pullup(m, ARC_HDRNEWLEN);
248 if (m == 0)
249 senderr(ENOBUFS);
250
251 ah = mtod(m, struct arc_header *);
252 ah->arc_type = atype;
253 ah->arc_flag = sflag;
254 ah->arc_seqid= ac->ac_seqid;
255
256 /* here we can have small, especially forbidden packets */
257
258 if ((len >= ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) &&
259 (len <= ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) {
260
261 M_PREPEND(m, 4, M_DONTWAIT);
262 if (m == 0)
263 senderr(ENOBUFS);
264
265 m = m_pullup(m, ARC_HDRNEWLEN);
266 if (m == 0)
267 senderr(ENOBUFS);
268
269 ah = mtod(m, struct arc_header *);
270 ah->arc_type = atype;
271 ah->arc_flag = 0xFF;
272 ah->arc_seqid= 0xFFFF;
273 len += 4;
274 }
275
276 ah->arc_dhost= adst;
277 ah->arc_shost= ac->ac_anaddr;
278
279 s = splimp();
280 /*
281 * Queue message on interface, and start output if interface
282 * not yet active.
283 */
284 if (IF_QFULL(&ifp->if_snd)) {
285 IF_DROP(&ifp->if_snd);
286 splx(s);
287 senderr(ENOBUFS);
288 }
289 IF_ENQUEUE(&ifp->if_snd, m);
290 if ((ifp->if_flags & IFF_OACTIVE) == 0)
291 (*ifp->if_start)(ifp);
292 splx(s);
293
294 ifp->if_obytes += len + ARC_HDRNEWLEN;
295
296 } else {
297 M_PREPEND(m, ARC_HDRLEN, M_DONTWAIT);
298 if (m == 0)
299 senderr(ENOBUFS);
300 ah = mtod(m, struct arc_header *);
301 ah->arc_type = atype;
302 ah->arc_dhost= adst;
303 ah->arc_shost= ac->ac_anaddr;
304 s = splimp();
305 /*
306 * Queue message on interface, and start output if interface
307 * not yet active.
308 */
309 if (IF_QFULL(&ifp->if_snd)) {
310 IF_DROP(&ifp->if_snd);
311 splx(s);
312 senderr(ENOBUFS);
313 }
314 IF_ENQUEUE(&ifp->if_snd, m);
315 if ((ifp->if_flags & IFF_OACTIVE) == 0)
316 (*ifp->if_start)(ifp);
317 splx(s);
318
319 ifp->if_obytes += len + ARC_HDRLEN;
320 }
321 return (error);
322
323 bad:
324 if (m1)
325 m_freem(m1);
326 if (m)
327 m_freem(m);
328 return (error);
329 }
330
331 /*
332 * Defragmenter. Returns mbuf if last packet found, else
333 * NULL. frees imcoming mbuf as necessary.
334 */
335
336 __inline struct mbuf *
337 arc_defrag(ifp, m)
338 struct ifnet *ifp;
339 struct mbuf *m;
340 {
341 struct arc_header *ah, *ah1;
342 struct arccom *ac;
343 struct ac_frag *af;
344 struct mbuf *m1;
345 char *s;
346 int newflen;
347 u_char src,dst,typ;
348
349 ac = (struct arccom *)ifp;
350
351 m = m_pullup(m, ARC_HDRNEWLEN);
352 if (m == NULL) {
353 ++ifp->if_ierrors;
354 return NULL;
355 }
356
357 ah = mtod(m, struct arc_header *);
358 typ = ah->arc_type;
359
360 if (!arc_isphds(typ))
361 return m;
362
363 src = ah->arc_shost;
364 dst = ah->arc_dhost;
365
366 if (ah->arc_flag == 0xff) {
367 m_adj(m, 4);
368
369 m = m_pullup(m, ARC_HDRNEWLEN);
370 if (m == NULL) {
371 ++ifp->if_ierrors;
372 return NULL;
373 }
374
375 ah = mtod(m, struct arc_header *);
376 ah->arc_shost = src;
377 ah->arc_dhost = dst;
378 ah->arc_type = typ;
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 (ah->arc_type != ah1->arc_type) {
421 s = "type differs";
422 goto outofseq;
423 }
424
425 if (ah->arc_dhost != 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%d: got out of seq. packet: %s\n",
473 ifp->if_name, ifp->if_unit, 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(type)
487 int type;
488 {
489 return ((type != ARCTYPE_IP_OLD &&
490 type != ARCTYPE_ARP_OLD));
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 void
499 arc_input(ifp, m)
500 struct ifnet *ifp;
501 struct mbuf *m;
502 {
503 register struct arc_header *ah;
504 register struct ifqueue *inq;
505 u_int8_t atype;
506 int s;
507
508 if ((ifp->if_flags & IFF_UP) == 0) {
509 m_freem(m);
510 return;
511 }
512
513 /* possibly defragment: */
514 m = arc_defrag(ifp, m);
515 if (m == NULL)
516 return;
517
518 ah = mtod(m, struct arc_header *);
519
520 ifp->if_lastchange = time;
521 ifp->if_ibytes += m->m_pkthdr.len;
522
523 if (arcbroadcastaddr == ah->arc_dhost) {
524 m->m_flags |= M_BCAST;
525 ifp->if_imcasts++;
526 }
527
528 atype = ah->arc_type;
529 switch (atype) {
530 #ifdef INET
531 case ARCTYPE_IP:
532 m_adj(m,ARC_HDRNEWLEN);
533 schednetisr(NETISR_IP);
534 inq = &ipintrq;
535 break;
536
537 case ARCTYPE_IP_OLD:
538 m_adj(m,ARC_HDRLEN);
539 schednetisr(NETISR_IP);
540 inq = &ipintrq;
541 break;
542 #endif
543 default:
544 m_freem(m);
545 return;
546 }
547
548 s = splimp();
549 if (IF_QFULL(inq)) {
550 IF_DROP(inq);
551 m_freem(m);
552 } else
553 IF_ENQUEUE(inq, m);
554 splx(s);
555 }
556
557 /*
558 * Convert Arcnet address to printable (loggable) representation.
559 */
560 static char digits[] = "0123456789abcdef";
561 char *
562 arc_sprintf(ap)
563 register u_int8_t *ap;
564 {
565 static char arcbuf[3];
566 register char *cp = arcbuf;
567
568 *cp++ = digits[*ap >> 4];
569 *cp++ = digits[*ap++ & 0xf];
570 *cp = 0;
571 return (arcbuf);
572 }
573
574 /*
575 * Perform common duties while attaching to interface list
576 */
577 void
578 arc_ifattach(ifp)
579 register struct ifnet *ifp;
580 {
581 register struct ifaddr *ifa;
582 register struct sockaddr_dl *sdl;
583 register struct arccom *ac;
584
585 ifp->if_type = IFT_ARCNET;
586 ifp->if_addrlen = 1;
587 ifp->if_hdrlen = ARC_HDRLEN;
588 if (ifp->if_flags & IFF_LINK0 && arc_phdsmtu > 60480)
589 log(LOG_ERR,
590 "%s%d: arc_phdsmtu is %d, but must not exceed 60480",
591 ifp->if_name, ifp->if_unit, arc_phdsmtu);
592
593 ifp->if_mtu = (ifp->if_flags & IFF_LINK0 ? arc_phdsmtu : ARCMTU);
594 ac = (struct arccom *)ifp;
595 ac->ac_seqid = (time.tv_sec) & 0xFFFF; /* try to make seqid unique */
596 if (ac->ac_anaddr == 0) {
597 /* XXX this message isn't entirely clear, to me -- cgd */
598 log(LOG_ERR,"%s%d: link address 0 reserved for broadcasts. Please change it and ifconfig %s%d down up\n",
599 ifp->if_name,ifp->if_unit,ifp->if_name,ifp->if_unit);
600 }
601 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
602 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
603 sdl->sdl_family == AF_LINK) {
604 sdl->sdl_type = IFT_ARCNET;
605 sdl->sdl_alen = ifp->if_addrlen;
606 bcopy((caddr_t)&((struct arccom *)ifp)->ac_anaddr,
607 LLADDR(sdl), ifp->if_addrlen);
608 break;
609 }
610 }
611