1/*
2 * Copyright (c) 1995, 1996
3 * Matt Thomas <matt@3am-software.com>. All rights reserved.
4 * Copyright (c) 1982, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * from: if_ethersubr.c,v 1.5 1994/12/13 22:31:45 wollman Exp
36 * $FreeBSD: head/sys/net/if_fddisubr.c 93377 2002-03-29 09:52:01Z mdodd $
37 */
38
39#include "opt_atalk.h"
40#include "opt_inet.h"
41#include "opt_inet6.h"
42#include "opt_ipx.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/module.h>
50#include <sys/socket.h>
51#include <sys/sockio.h>
52
53#include <net/if.h>
54#include <net/if_llc.h>
55#include <net/if_dl.h>
56#include <net/if_types.h>
57#include <net/netisr.h>
58#include <net/route.h>
59#include <net/bpf.h>
60#include <net/fddi.h>
61
62#if defined(INET) || defined(INET6)
63#include <netinet/in.h>
64#include <netinet/in_var.h>
65#include <netinet/if_ether.h>
66#endif
67#ifdef INET6
68#include <netinet6/nd6.h>
69#endif
70
71#ifdef IPX
72#include <netipx/ipx.h>
73#include <netipx/ipx_if.h>
74#endif
75
76#ifdef NS
77#include <netns/ns.h>
78#include <netns/ns_if.h>
79#endif
80
81#ifdef DECNET
82#include <netdnet/dn.h>
83#endif
84
85#ifdef NETATALK
86#include <netatalk/at.h>
87#include <netatalk/at_var.h>
88#include <netatalk/at_extern.h>
89
90extern u_char at_org_code[ 3 ];
91extern u_char aarp_org_code[ 3 ];
92#endif /* NETATALK */
93
94static int fddi_resolvemulti(struct ifnet *, struct sockaddr **,
95 struct sockaddr *);
96
97#define IFP2AC(IFP) ((struct arpcom *)IFP)
98#define senderr(e) { error = (e); goto bad; }
99
100/*
101 * FDDI output routine.
102 * Encapsulate a packet of type family for the local net.
103 * Use trailer local net encapsulation if enough data in first
104 * packet leaves a multiple of 512 bytes of data in remainder.
105 * Assumes that ifp is actually pointer to arpcom structure.
106 */
107int
108fddi_output(ifp, m, dst, rt0)
109 struct ifnet *ifp;
110 struct mbuf *m;
111 struct sockaddr *dst;
112 struct rtentry *rt0;
113{
114 u_int16_t type;
115 int loop_copy = 0, error = 0, hdrcmplt = 0;
116 u_char esrc[FDDI_ADDR_LEN], edst[FDDI_ADDR_LEN];
117 struct rtentry *rt;
118 struct fddi_header *fh;
119 struct arpcom *ac = IFP2AC(ifp);
120
121 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
122 senderr(ENETDOWN);
123 getmicrotime(&ifp->if_lastchange);
124 if ((rt = rt0) != NULL) {
125 if ((rt->rt_flags & RTF_UP) == 0) {
126 if ((rt0 = rt = rtalloc1(dst, 1, 0UL)) != NULL)
127 rt->rt_refcnt--;
128 else
129 senderr(EHOSTUNREACH);
130 }
131 if (rt->rt_flags & RTF_GATEWAY) {
132 if (rt->rt_gwroute == 0)
133 goto lookup;
134 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
135 rtfree(rt); rt = rt0;
136 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
137 if ((rt = rt->rt_gwroute) == 0)
138 senderr(EHOSTUNREACH);
139 }
140 }
141 if (rt->rt_flags & RTF_REJECT)
142 if (rt->rt_rmx.rmx_expire == 0 ||
143 time_second < rt->rt_rmx.rmx_expire)
144 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
145 }
146 switch (dst->sa_family) {
147
148#ifdef INET
149 case AF_INET: {
150 if (!arpresolve(ifp, rt, m, dst, edst, rt0))
151 return (0); /* if not yet resolved */
152 type = htons(ETHERTYPE_IP);
153 break;
154 }
155#endif
156#ifdef INET6
157 case AF_INET6:
158 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst)) {
159 /* Something bad happened */
160 return (0);
161 }
162 type = htons(ETHERTYPE_IPV6);
163 break;
164#endif
165#ifdef IPX
166 case AF_IPX:
167 type = htons(ETHERTYPE_IPX);
168 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
169 (caddr_t)edst, FDDI_ADDR_LEN);
170 break;
171#endif
172#ifdef NETATALK
173 case AF_APPLETALK: {
174 struct at_ifaddr *aa;
175 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
176 return (0);
177 /*
178 * ifaddr is the first thing in at_ifaddr
179 */
180 if ((aa = at_ifawithnet( (struct sockaddr_at *)dst)) == 0)
181 goto bad;
182
183 /*
184 * In the phase 2 case, we need to prepend an mbuf for the llc header.
185 * Since we must preserve the value of m, which is passed to us by
186 * value, we m_copy() the first mbuf, and use it for our llc header.
187 */
188 if (aa->aa_flags & AFA_PHASE2) {
189 struct llc llc;
190
191 M_PREPEND(m, LLC_SNAPFRAMELEN, M_TRYWAIT);
192 if (m == 0)
193 senderr(ENOBUFS);
194 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
195 llc.llc_control = LLC_UI;
196 bcopy(at_org_code, llc.llc_snap.org_code, sizeof(at_org_code));
197 llc.llc_snap.ether_type = htons(ETHERTYPE_AT);
198 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
199 type = 0;
200 } else {
201 type = htons(ETHERTYPE_AT);
202 }
203 break;
204 }
205#endif /* NETATALK */
206#ifdef NS
207 case AF_NS:
208 type = htons(ETHERTYPE_NS);
209 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
210 (caddr_t)edst, FDDI_ADDR_LEN);
211 break;
212#endif
213
214 case pseudo_AF_HDRCMPLT:
215 {
216 struct ether_header *eh;
217 hdrcmplt = 1;
218 eh = (struct ether_header *)dst->sa_data;
219 bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, FDDI_ADDR_LEN);
220 /* FALLTHROUGH */
221 }
222
223 case AF_UNSPEC:
224 {
225 struct ether_header *eh;
226 loop_copy = -1;
227 eh = (struct ether_header *)dst->sa_data;
228 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, FDDI_ADDR_LEN);
229 if (*edst & 1)
230 m->m_flags |= (M_BCAST|M_MCAST);
231 type = eh->ether_type;
232 break;
233 }
234
235 case AF_IMPLINK:
236 {
237 fh = mtod(m, struct fddi_header *);
238 error = EPROTONOSUPPORT;
239 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
240 case FDDIFC_LLC_ASYNC: {
241 /* legal priorities are 0 through 7 */
242 if ((fh->fddi_fc & FDDIFC_Z) > 7)
243 goto bad;
244 break;
245 }
246 case FDDIFC_LLC_SYNC: {
247 /* FDDIFC_Z bits reserved, must be zero */
248 if (fh->fddi_fc & FDDIFC_Z)
249 goto bad;
250 break;
251 }
252 case FDDIFC_SMT: {
253 /* FDDIFC_Z bits must be non zero */
254 if ((fh->fddi_fc & FDDIFC_Z) == 0)
255 goto bad;
256 break;
257 }
258 default: {
259 /* anything else is too dangerous */
260 goto bad;
261 }
262 }
263 error = 0;
264 if (fh->fddi_dhost[0] & 1)
265 m->m_flags |= (M_BCAST|M_MCAST);
266 goto queue_it;
267 }
268 default:
269 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
270 dst->sa_family);
271 senderr(EAFNOSUPPORT);
272 }
273
274 if (type != 0) {
275 struct llc *l;
276 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
277 if (m == 0)
278 senderr(ENOBUFS);
279 l = mtod(m, struct llc *);
280 l->llc_control = LLC_UI;
281 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
282 l->llc_snap.org_code[0] = l->llc_snap.org_code[1] = l->llc_snap.org_code[2] = 0;
283 bcopy((caddr_t)&type, (caddr_t)&l->llc_snap.ether_type,
284 sizeof(u_int16_t));
285 }
286
287 /*
288 * Add local net header. If no space in first mbuf,
289 * allocate another.
290 */
291 M_PREPEND(m, FDDI_HDR_LEN, M_DONTWAIT);
292 if (m == 0)
293 senderr(ENOBUFS);
294 fh = mtod(m, struct fddi_header *);
295 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
296 bcopy((caddr_t)edst, (caddr_t)fh->fddi_dhost, FDDI_ADDR_LEN);
297 queue_it:
298 if (hdrcmplt)
299 bcopy((caddr_t)esrc, (caddr_t)fh->fddi_shost, FDDI_ADDR_LEN);
300 else
301 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)fh->fddi_shost,
302 FDDI_ADDR_LEN);
303
304 /*
305 * If a simplex interface, and the packet is being sent to our
306 * Ethernet address or a broadcast address, loopback a copy.
307 * XXX To make a simplex device behave exactly like a duplex
308 * device, we should copy in the case of sending to our own
309 * ethernet address (thus letting the original actually appear
310 * on the wire). However, we don't do that here for security
311 * reasons and compatibility with the original behavior.
312 */
313 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
314 if ((m->m_flags & M_BCAST) || loop_copy) {
315 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
316
317 (void) if_simloop(ifp,
318 n, dst->sa_family, FDDI_HDR_LEN);
319 } else if (bcmp(fh->fddi_dhost,
320 fh->fddi_shost, FDDI_ADDR_LEN) == 0) {
321 (void) if_simloop(ifp,
322 m, dst->sa_family, FDDI_HDR_LEN);
323 return (0); /* XXX */
324 }
325 }
326
327 if (! IF_HANDOFF(&ifp->if_snd, m, ifp))
328 senderr(ENOBUFS);
329 return (error);
330
331bad:
332 if (m)
333 m_freem(m);
334 return (error);
335}
336
337/*
338 * Process a received FDDI packet;
339 * the packet is in the mbuf chain m without
340 * the fddi header, which is provided separately.
341 */
342void
343fddi_input(ifp, fh, m)
344 struct ifnet *ifp;
345 struct fddi_header *fh;
346 struct mbuf *m;
347{
348 struct ifqueue *inq;
349 struct llc *l;
350
351 if ((ifp->if_flags & IFF_UP) == 0) {
352 m_freem(m);
353 return;
354 }
355 getmicrotime(&ifp->if_lastchange);
356 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*fh);
357 if (fh->fddi_dhost[0] & 1) {
358 if (bcmp((caddr_t)fddibroadcastaddr, (caddr_t)fh->fddi_dhost,
359 FDDI_ADDR_LEN) == 0)
360 m->m_flags |= M_BCAST;
361 else
362 m->m_flags |= M_MCAST;
363 ifp->if_imcasts++;
364 } else if ((ifp->if_flags & IFF_PROMISC)
365 && bcmp(IFP2AC(ifp)->ac_enaddr, (caddr_t)fh->fddi_dhost,
366 FDDI_ADDR_LEN) != 0) {
367 m_freem(m);
368 return;
369 }
370
371#ifdef M_LINK0
372 /*
373 * If this has a LLC priority of 0, then mark it so upper
374 * layers have a hint that it really came via a FDDI/Ethernet
375 * bridge.
376 */
377 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
378 m->m_flags |= M_LINK0;
379#endif
380
381 l = mtod(m, struct llc *);
382
383 switch (l->llc_dsap) {
384#if defined(INET) || defined(INET6) || defined(NS) || defined(DECNET) || defined(IPX) || defined(NETATALK)
385 case LLC_SNAP_LSAP:
386 {
387 u_int16_t type;
388 if (l->llc_control != LLC_UI || l->llc_ssap != LLC_SNAP_LSAP)
389 goto dropanyway;
390#ifdef NETATALK
391 if (Bcmp(&(l->llc_snap.org_code)[0], at_org_code,
392 sizeof(at_org_code)) == 0 &&
393 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AT) {
394 inq = &atintrq2;
395 m_adj(m, LLC_SNAPFRAMELEN);
396 schednetisr(NETISR_ATALK);
397 break;
398 }
399
400 if (Bcmp(&(l->llc_snap.org_code)[0], aarp_org_code,
401 sizeof(aarp_org_code)) == 0 &&
402 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AARP) {
403 m_adj(m, LLC_SNAPFRAMELEN);
404 aarpinput(IFP2AC(ifp), m); /* XXX */
405 return;
406 }
407#endif /* NETATALK */
408 if (l->llc_snap.org_code[0] != 0 ||
409 l->llc_snap.org_code[1] != 0 ||
410 l->llc_snap.org_code[2] != 0)
411 goto dropanyway;
412
413 type = ntohs(l->llc_snap.ether_type);
414 m_adj(m, LLC_SNAPFRAMELEN);
415
416 switch (type) {
417#ifdef INET
418 case ETHERTYPE_IP:
419 if (ipflow_fastforward(m))
420 return;
421 schednetisr(NETISR_IP);
422 inq = &ipintrq;
423 break;
424
425 case ETHERTYPE_ARP:
426 if (ifp->if_flags & IFF_NOARP)
427 goto dropanyway;
428 schednetisr(NETISR_ARP);
429 inq = &arpintrq;
430 break;
431#endif
432#ifdef INET6
433 case ETHERTYPE_IPV6:
434 schednetisr(NETISR_IPV6);
435 inq = &ip6intrq;
436 break;
437#endif
438#ifdef IPX
439 case ETHERTYPE_IPX:
440 schednetisr(NETISR_IPX);
441 inq = &ipxintrq;
442 break;
443#endif
444#ifdef NS
445 case ETHERTYPE_NS:
446 schednetisr(NETISR_NS);
447 inq = &nsintrq;
448 break;
449#endif
450#ifdef DECNET
451 case ETHERTYPE_DECNET:
452 schednetisr(NETISR_DECNET);
453 inq = &decnetintrq;
454 break;
455#endif
456#ifdef NETATALK
457 case ETHERTYPE_AT:
458 schednetisr(NETISR_ATALK);
459 inq = &atintrq1;
460 break;
461 case ETHERTYPE_AARP:
462 /* probably this should be done with a NETISR as well */
463 aarpinput(IFP2AC(ifp), m); /* XXX */
464 return;
465#endif /* NETATALK */
466 default:
467 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
468 ifp->if_noproto++;
469 goto dropanyway;
470 }
471 break;
472 }
473#endif /* INET || NS */
474
475 default:
476 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
477 ifp->if_noproto++;
478 goto dropanyway;
479 }
480
481 (void) IF_HANDOFF(inq, m, NULL);
482 return;
483
484dropanyway:
485 ifp->if_iqdrops++;
486 if (m)
487 m_freem(m);
488 return;
489}
490/*
491 * Perform common duties while attaching to interface list
492 */
493void
494fddi_ifattach(ifp)
495 struct ifnet *ifp;
496{
497 struct ifaddr *ifa;
498 struct sockaddr_dl *sdl;
499
500 ifp->if_type = IFT_FDDI;
501 ifp->if_addrlen = FDDI_ADDR_LEN;
502 ifp->if_hdrlen = 21;
503 ifp->if_mtu = FDDIMTU;
504 ifp->if_resolvemulti = fddi_resolvemulti;
505 ifp->if_baudrate = 100000000;
506#ifdef IFF_NOTRAILERS
507 ifp->if_flags |= IFF_NOTRAILERS;
508#endif
509 ifp->if_broadcastaddr = fddibroadcastaddr;
510 ifa = ifaddr_byindex(ifp->if_index);
511 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
512 sdl->sdl_type = IFT_FDDI;
513 sdl->sdl_alen = ifp->if_addrlen;
514 bcopy(IFP2AC(ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
515}
516
517static int
518fddi_resolvemulti(ifp, llsa, sa)
519 struct ifnet *ifp;
520 struct sockaddr **llsa;
521 struct sockaddr *sa;
522{
523 struct sockaddr_dl *sdl;
524 struct sockaddr_in *sin;
525#ifdef INET6
526 struct sockaddr_in6 *sin6;
527#endif
528 u_char *e_addr;
529
530 switch(sa->sa_family) {
531 case AF_LINK:
532 /*
533 * No mapping needed. Just check that it's a valid MC address.
534 */
535 sdl = (struct sockaddr_dl *)sa;
536 e_addr = LLADDR(sdl);
537 if ((e_addr[0] & 1) != 1)
538 return (EADDRNOTAVAIL);
539 *llsa = 0;
540 return (0);
541
542#ifdef INET
543 case AF_INET:
544 sin = (struct sockaddr_in *)sa;
545 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
546 return (EADDRNOTAVAIL);
547 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
548 M_WAITOK);
549 sdl->sdl_len = sizeof *sdl;
550 sdl->sdl_family = AF_LINK;
551 sdl->sdl_index = ifp->if_index;
552 sdl->sdl_type = IFT_FDDI;
553 sdl->sdl_nlen = 0;
554 sdl->sdl_alen = FDDI_ADDR_LEN;
555 sdl->sdl_slen = 0;
556 e_addr = LLADDR(sdl);
557 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
558 *llsa = (struct sockaddr *)sdl;
559 return (0);
560#endif
561#ifdef INET6
562 case AF_INET6:
563 sin6 = (struct sockaddr_in6 *)sa;
564 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
565 /*
566 * An IP6 address of 0 means listen to all
567 * of the Ethernet multicast address used for IP6.
568 * (This is used for multicast routers.)
569 */
570 ifp->if_flags |= IFF_ALLMULTI;
571 *llsa = 0;
572 return (0);
573 }
574 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
575 return (EADDRNOTAVAIL);
576 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
577 M_WAITOK);
578 sdl->sdl_len = sizeof *sdl;
579 sdl->sdl_family = AF_LINK;
580 sdl->sdl_index = ifp->if_index;
581 sdl->sdl_type = IFT_FDDI;
582 sdl->sdl_nlen = 0;
583 sdl->sdl_alen = FDDI_ADDR_LEN;
584 sdl->sdl_slen = 0;
585 e_addr = LLADDR(sdl);
586 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
587 *llsa = (struct sockaddr *)sdl;
588 return (0);
589#endif
590
591 default:
592 /*
593 * Well, the text isn't quite right, but it's the name
594 * that counts...
595 */
596 return (EAFNOSUPPORT);
597 }
598
599 return (0);
600}
601
602static moduledata_t fddi_mod = {
603 "fddi", /* module name */
604 NULL, /* event handler */
605 0 /* extra data */
606};
607
608DECLARE_MODULE(fddi, fddi_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
609MODULE_VERSION(fddi, 1);
2 * Copyright (c) 1995, 1996
3 * Matt Thomas <matt@3am-software.com>. All rights reserved.
4 * Copyright (c) 1982, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * from: if_ethersubr.c,v 1.5 1994/12/13 22:31:45 wollman Exp
36 * $FreeBSD: head/sys/net/if_fddisubr.c 93377 2002-03-29 09:52:01Z mdodd $
37 */
38
39#include "opt_atalk.h"
40#include "opt_inet.h"
41#include "opt_inet6.h"
42#include "opt_ipx.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/module.h>
50#include <sys/socket.h>
51#include <sys/sockio.h>
52
53#include <net/if.h>
54#include <net/if_llc.h>
55#include <net/if_dl.h>
56#include <net/if_types.h>
57#include <net/netisr.h>
58#include <net/route.h>
59#include <net/bpf.h>
60#include <net/fddi.h>
61
62#if defined(INET) || defined(INET6)
63#include <netinet/in.h>
64#include <netinet/in_var.h>
65#include <netinet/if_ether.h>
66#endif
67#ifdef INET6
68#include <netinet6/nd6.h>
69#endif
70
71#ifdef IPX
72#include <netipx/ipx.h>
73#include <netipx/ipx_if.h>
74#endif
75
76#ifdef NS
77#include <netns/ns.h>
78#include <netns/ns_if.h>
79#endif
80
81#ifdef DECNET
82#include <netdnet/dn.h>
83#endif
84
85#ifdef NETATALK
86#include <netatalk/at.h>
87#include <netatalk/at_var.h>
88#include <netatalk/at_extern.h>
89
90extern u_char at_org_code[ 3 ];
91extern u_char aarp_org_code[ 3 ];
92#endif /* NETATALK */
93
94static int fddi_resolvemulti(struct ifnet *, struct sockaddr **,
95 struct sockaddr *);
96
97#define IFP2AC(IFP) ((struct arpcom *)IFP)
98#define senderr(e) { error = (e); goto bad; }
99
100/*
101 * FDDI output routine.
102 * Encapsulate a packet of type family for the local net.
103 * Use trailer local net encapsulation if enough data in first
104 * packet leaves a multiple of 512 bytes of data in remainder.
105 * Assumes that ifp is actually pointer to arpcom structure.
106 */
107int
108fddi_output(ifp, m, dst, rt0)
109 struct ifnet *ifp;
110 struct mbuf *m;
111 struct sockaddr *dst;
112 struct rtentry *rt0;
113{
114 u_int16_t type;
115 int loop_copy = 0, error = 0, hdrcmplt = 0;
116 u_char esrc[FDDI_ADDR_LEN], edst[FDDI_ADDR_LEN];
117 struct rtentry *rt;
118 struct fddi_header *fh;
119 struct arpcom *ac = IFP2AC(ifp);
120
121 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
122 senderr(ENETDOWN);
123 getmicrotime(&ifp->if_lastchange);
124 if ((rt = rt0) != NULL) {
125 if ((rt->rt_flags & RTF_UP) == 0) {
126 if ((rt0 = rt = rtalloc1(dst, 1, 0UL)) != NULL)
127 rt->rt_refcnt--;
128 else
129 senderr(EHOSTUNREACH);
130 }
131 if (rt->rt_flags & RTF_GATEWAY) {
132 if (rt->rt_gwroute == 0)
133 goto lookup;
134 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
135 rtfree(rt); rt = rt0;
136 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
137 if ((rt = rt->rt_gwroute) == 0)
138 senderr(EHOSTUNREACH);
139 }
140 }
141 if (rt->rt_flags & RTF_REJECT)
142 if (rt->rt_rmx.rmx_expire == 0 ||
143 time_second < rt->rt_rmx.rmx_expire)
144 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
145 }
146 switch (dst->sa_family) {
147
148#ifdef INET
149 case AF_INET: {
150 if (!arpresolve(ifp, rt, m, dst, edst, rt0))
151 return (0); /* if not yet resolved */
152 type = htons(ETHERTYPE_IP);
153 break;
154 }
155#endif
156#ifdef INET6
157 case AF_INET6:
158 if (!nd6_storelladdr(ifp, rt, m, dst, (u_char *)edst)) {
159 /* Something bad happened */
160 return (0);
161 }
162 type = htons(ETHERTYPE_IPV6);
163 break;
164#endif
165#ifdef IPX
166 case AF_IPX:
167 type = htons(ETHERTYPE_IPX);
168 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
169 (caddr_t)edst, FDDI_ADDR_LEN);
170 break;
171#endif
172#ifdef NETATALK
173 case AF_APPLETALK: {
174 struct at_ifaddr *aa;
175 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
176 return (0);
177 /*
178 * ifaddr is the first thing in at_ifaddr
179 */
180 if ((aa = at_ifawithnet( (struct sockaddr_at *)dst)) == 0)
181 goto bad;
182
183 /*
184 * In the phase 2 case, we need to prepend an mbuf for the llc header.
185 * Since we must preserve the value of m, which is passed to us by
186 * value, we m_copy() the first mbuf, and use it for our llc header.
187 */
188 if (aa->aa_flags & AFA_PHASE2) {
189 struct llc llc;
190
191 M_PREPEND(m, LLC_SNAPFRAMELEN, M_TRYWAIT);
192 if (m == 0)
193 senderr(ENOBUFS);
194 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
195 llc.llc_control = LLC_UI;
196 bcopy(at_org_code, llc.llc_snap.org_code, sizeof(at_org_code));
197 llc.llc_snap.ether_type = htons(ETHERTYPE_AT);
198 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
199 type = 0;
200 } else {
201 type = htons(ETHERTYPE_AT);
202 }
203 break;
204 }
205#endif /* NETATALK */
206#ifdef NS
207 case AF_NS:
208 type = htons(ETHERTYPE_NS);
209 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
210 (caddr_t)edst, FDDI_ADDR_LEN);
211 break;
212#endif
213
214 case pseudo_AF_HDRCMPLT:
215 {
216 struct ether_header *eh;
217 hdrcmplt = 1;
218 eh = (struct ether_header *)dst->sa_data;
219 bcopy((caddr_t)eh->ether_shost, (caddr_t)esrc, FDDI_ADDR_LEN);
220 /* FALLTHROUGH */
221 }
222
223 case AF_UNSPEC:
224 {
225 struct ether_header *eh;
226 loop_copy = -1;
227 eh = (struct ether_header *)dst->sa_data;
228 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, FDDI_ADDR_LEN);
229 if (*edst & 1)
230 m->m_flags |= (M_BCAST|M_MCAST);
231 type = eh->ether_type;
232 break;
233 }
234
235 case AF_IMPLINK:
236 {
237 fh = mtod(m, struct fddi_header *);
238 error = EPROTONOSUPPORT;
239 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
240 case FDDIFC_LLC_ASYNC: {
241 /* legal priorities are 0 through 7 */
242 if ((fh->fddi_fc & FDDIFC_Z) > 7)
243 goto bad;
244 break;
245 }
246 case FDDIFC_LLC_SYNC: {
247 /* FDDIFC_Z bits reserved, must be zero */
248 if (fh->fddi_fc & FDDIFC_Z)
249 goto bad;
250 break;
251 }
252 case FDDIFC_SMT: {
253 /* FDDIFC_Z bits must be non zero */
254 if ((fh->fddi_fc & FDDIFC_Z) == 0)
255 goto bad;
256 break;
257 }
258 default: {
259 /* anything else is too dangerous */
260 goto bad;
261 }
262 }
263 error = 0;
264 if (fh->fddi_dhost[0] & 1)
265 m->m_flags |= (M_BCAST|M_MCAST);
266 goto queue_it;
267 }
268 default:
269 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
270 dst->sa_family);
271 senderr(EAFNOSUPPORT);
272 }
273
274 if (type != 0) {
275 struct llc *l;
276 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
277 if (m == 0)
278 senderr(ENOBUFS);
279 l = mtod(m, struct llc *);
280 l->llc_control = LLC_UI;
281 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
282 l->llc_snap.org_code[0] = l->llc_snap.org_code[1] = l->llc_snap.org_code[2] = 0;
283 bcopy((caddr_t)&type, (caddr_t)&l->llc_snap.ether_type,
284 sizeof(u_int16_t));
285 }
286
287 /*
288 * Add local net header. If no space in first mbuf,
289 * allocate another.
290 */
291 M_PREPEND(m, FDDI_HDR_LEN, M_DONTWAIT);
292 if (m == 0)
293 senderr(ENOBUFS);
294 fh = mtod(m, struct fddi_header *);
295 fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
296 bcopy((caddr_t)edst, (caddr_t)fh->fddi_dhost, FDDI_ADDR_LEN);
297 queue_it:
298 if (hdrcmplt)
299 bcopy((caddr_t)esrc, (caddr_t)fh->fddi_shost, FDDI_ADDR_LEN);
300 else
301 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)fh->fddi_shost,
302 FDDI_ADDR_LEN);
303
304 /*
305 * If a simplex interface, and the packet is being sent to our
306 * Ethernet address or a broadcast address, loopback a copy.
307 * XXX To make a simplex device behave exactly like a duplex
308 * device, we should copy in the case of sending to our own
309 * ethernet address (thus letting the original actually appear
310 * on the wire). However, we don't do that here for security
311 * reasons and compatibility with the original behavior.
312 */
313 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
314 if ((m->m_flags & M_BCAST) || loop_copy) {
315 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
316
317 (void) if_simloop(ifp,
318 n, dst->sa_family, FDDI_HDR_LEN);
319 } else if (bcmp(fh->fddi_dhost,
320 fh->fddi_shost, FDDI_ADDR_LEN) == 0) {
321 (void) if_simloop(ifp,
322 m, dst->sa_family, FDDI_HDR_LEN);
323 return (0); /* XXX */
324 }
325 }
326
327 if (! IF_HANDOFF(&ifp->if_snd, m, ifp))
328 senderr(ENOBUFS);
329 return (error);
330
331bad:
332 if (m)
333 m_freem(m);
334 return (error);
335}
336
337/*
338 * Process a received FDDI packet;
339 * the packet is in the mbuf chain m without
340 * the fddi header, which is provided separately.
341 */
342void
343fddi_input(ifp, fh, m)
344 struct ifnet *ifp;
345 struct fddi_header *fh;
346 struct mbuf *m;
347{
348 struct ifqueue *inq;
349 struct llc *l;
350
351 if ((ifp->if_flags & IFF_UP) == 0) {
352 m_freem(m);
353 return;
354 }
355 getmicrotime(&ifp->if_lastchange);
356 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*fh);
357 if (fh->fddi_dhost[0] & 1) {
358 if (bcmp((caddr_t)fddibroadcastaddr, (caddr_t)fh->fddi_dhost,
359 FDDI_ADDR_LEN) == 0)
360 m->m_flags |= M_BCAST;
361 else
362 m->m_flags |= M_MCAST;
363 ifp->if_imcasts++;
364 } else if ((ifp->if_flags & IFF_PROMISC)
365 && bcmp(IFP2AC(ifp)->ac_enaddr, (caddr_t)fh->fddi_dhost,
366 FDDI_ADDR_LEN) != 0) {
367 m_freem(m);
368 return;
369 }
370
371#ifdef M_LINK0
372 /*
373 * If this has a LLC priority of 0, then mark it so upper
374 * layers have a hint that it really came via a FDDI/Ethernet
375 * bridge.
376 */
377 if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
378 m->m_flags |= M_LINK0;
379#endif
380
381 l = mtod(m, struct llc *);
382
383 switch (l->llc_dsap) {
384#if defined(INET) || defined(INET6) || defined(NS) || defined(DECNET) || defined(IPX) || defined(NETATALK)
385 case LLC_SNAP_LSAP:
386 {
387 u_int16_t type;
388 if (l->llc_control != LLC_UI || l->llc_ssap != LLC_SNAP_LSAP)
389 goto dropanyway;
390#ifdef NETATALK
391 if (Bcmp(&(l->llc_snap.org_code)[0], at_org_code,
392 sizeof(at_org_code)) == 0 &&
393 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AT) {
394 inq = &atintrq2;
395 m_adj(m, LLC_SNAPFRAMELEN);
396 schednetisr(NETISR_ATALK);
397 break;
398 }
399
400 if (Bcmp(&(l->llc_snap.org_code)[0], aarp_org_code,
401 sizeof(aarp_org_code)) == 0 &&
402 ntohs(l->llc_snap.ether_type) == ETHERTYPE_AARP) {
403 m_adj(m, LLC_SNAPFRAMELEN);
404 aarpinput(IFP2AC(ifp), m); /* XXX */
405 return;
406 }
407#endif /* NETATALK */
408 if (l->llc_snap.org_code[0] != 0 ||
409 l->llc_snap.org_code[1] != 0 ||
410 l->llc_snap.org_code[2] != 0)
411 goto dropanyway;
412
413 type = ntohs(l->llc_snap.ether_type);
414 m_adj(m, LLC_SNAPFRAMELEN);
415
416 switch (type) {
417#ifdef INET
418 case ETHERTYPE_IP:
419 if (ipflow_fastforward(m))
420 return;
421 schednetisr(NETISR_IP);
422 inq = &ipintrq;
423 break;
424
425 case ETHERTYPE_ARP:
426 if (ifp->if_flags & IFF_NOARP)
427 goto dropanyway;
428 schednetisr(NETISR_ARP);
429 inq = &arpintrq;
430 break;
431#endif
432#ifdef INET6
433 case ETHERTYPE_IPV6:
434 schednetisr(NETISR_IPV6);
435 inq = &ip6intrq;
436 break;
437#endif
438#ifdef IPX
439 case ETHERTYPE_IPX:
440 schednetisr(NETISR_IPX);
441 inq = &ipxintrq;
442 break;
443#endif
444#ifdef NS
445 case ETHERTYPE_NS:
446 schednetisr(NETISR_NS);
447 inq = &nsintrq;
448 break;
449#endif
450#ifdef DECNET
451 case ETHERTYPE_DECNET:
452 schednetisr(NETISR_DECNET);
453 inq = &decnetintrq;
454 break;
455#endif
456#ifdef NETATALK
457 case ETHERTYPE_AT:
458 schednetisr(NETISR_ATALK);
459 inq = &atintrq1;
460 break;
461 case ETHERTYPE_AARP:
462 /* probably this should be done with a NETISR as well */
463 aarpinput(IFP2AC(ifp), m); /* XXX */
464 return;
465#endif /* NETATALK */
466 default:
467 /* printf("fddi_input: unknown protocol 0x%x\n", type); */
468 ifp->if_noproto++;
469 goto dropanyway;
470 }
471 break;
472 }
473#endif /* INET || NS */
474
475 default:
476 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
477 ifp->if_noproto++;
478 goto dropanyway;
479 }
480
481 (void) IF_HANDOFF(inq, m, NULL);
482 return;
483
484dropanyway:
485 ifp->if_iqdrops++;
486 if (m)
487 m_freem(m);
488 return;
489}
490/*
491 * Perform common duties while attaching to interface list
492 */
493void
494fddi_ifattach(ifp)
495 struct ifnet *ifp;
496{
497 struct ifaddr *ifa;
498 struct sockaddr_dl *sdl;
499
500 ifp->if_type = IFT_FDDI;
501 ifp->if_addrlen = FDDI_ADDR_LEN;
502 ifp->if_hdrlen = 21;
503 ifp->if_mtu = FDDIMTU;
504 ifp->if_resolvemulti = fddi_resolvemulti;
505 ifp->if_baudrate = 100000000;
506#ifdef IFF_NOTRAILERS
507 ifp->if_flags |= IFF_NOTRAILERS;
508#endif
509 ifp->if_broadcastaddr = fddibroadcastaddr;
510 ifa = ifaddr_byindex(ifp->if_index);
511 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
512 sdl->sdl_type = IFT_FDDI;
513 sdl->sdl_alen = ifp->if_addrlen;
514 bcopy(IFP2AC(ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
515}
516
517static int
518fddi_resolvemulti(ifp, llsa, sa)
519 struct ifnet *ifp;
520 struct sockaddr **llsa;
521 struct sockaddr *sa;
522{
523 struct sockaddr_dl *sdl;
524 struct sockaddr_in *sin;
525#ifdef INET6
526 struct sockaddr_in6 *sin6;
527#endif
528 u_char *e_addr;
529
530 switch(sa->sa_family) {
531 case AF_LINK:
532 /*
533 * No mapping needed. Just check that it's a valid MC address.
534 */
535 sdl = (struct sockaddr_dl *)sa;
536 e_addr = LLADDR(sdl);
537 if ((e_addr[0] & 1) != 1)
538 return (EADDRNOTAVAIL);
539 *llsa = 0;
540 return (0);
541
542#ifdef INET
543 case AF_INET:
544 sin = (struct sockaddr_in *)sa;
545 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
546 return (EADDRNOTAVAIL);
547 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
548 M_WAITOK);
549 sdl->sdl_len = sizeof *sdl;
550 sdl->sdl_family = AF_LINK;
551 sdl->sdl_index = ifp->if_index;
552 sdl->sdl_type = IFT_FDDI;
553 sdl->sdl_nlen = 0;
554 sdl->sdl_alen = FDDI_ADDR_LEN;
555 sdl->sdl_slen = 0;
556 e_addr = LLADDR(sdl);
557 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
558 *llsa = (struct sockaddr *)sdl;
559 return (0);
560#endif
561#ifdef INET6
562 case AF_INET6:
563 sin6 = (struct sockaddr_in6 *)sa;
564 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
565 /*
566 * An IP6 address of 0 means listen to all
567 * of the Ethernet multicast address used for IP6.
568 * (This is used for multicast routers.)
569 */
570 ifp->if_flags |= IFF_ALLMULTI;
571 *llsa = 0;
572 return (0);
573 }
574 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
575 return (EADDRNOTAVAIL);
576 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
577 M_WAITOK);
578 sdl->sdl_len = sizeof *sdl;
579 sdl->sdl_family = AF_LINK;
580 sdl->sdl_index = ifp->if_index;
581 sdl->sdl_type = IFT_FDDI;
582 sdl->sdl_nlen = 0;
583 sdl->sdl_alen = FDDI_ADDR_LEN;
584 sdl->sdl_slen = 0;
585 e_addr = LLADDR(sdl);
586 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
587 *llsa = (struct sockaddr *)sdl;
588 return (0);
589#endif
590
591 default:
592 /*
593 * Well, the text isn't quite right, but it's the name
594 * that counts...
595 */
596 return (EAFNOSUPPORT);
597 }
598
599 return (0);
600}
601
602static moduledata_t fddi_mod = {
603 "fddi", /* module name */
604 NULL, /* event handler */
605 0 /* extra data */
606};
607
608DECLARE_MODULE(fddi, fddi_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
609MODULE_VERSION(fddi, 1);