]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_fddisubr.c
Update to 20140321
[FreeBSD/FreeBSD.git] / sys / net / if_fddisubr.c
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$
37  */
38
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/module.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/if_dl.h>
54 #include <net/if_llc.h>
55 #include <net/if_types.h>
56 #include <net/if_llatbl.h>
57
58 #include <net/ethernet.h>
59 #include <net/netisr.h>
60 #include <net/route.h>
61 #include <net/bpf.h>
62 #include <net/fddi.h>
63
64 #if defined(INET) || defined(INET6)
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/if_ether.h>
68 #endif
69 #ifdef INET6
70 #include <netinet6/nd6.h>
71 #endif
72
73 #ifdef DECNET
74 #include <netdnet/dn.h>
75 #endif
76
77 #include <security/mac/mac_framework.h>
78
79 static const u_char fddibroadcastaddr[FDDI_ADDR_LEN] =
80                         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
81
82 static int fddi_resolvemulti(struct ifnet *, struct sockaddr **,
83                               struct sockaddr *);
84 static int fddi_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
85                        struct route *); 
86 static void fddi_input(struct ifnet *ifp, struct mbuf *m);
87
88 #define senderr(e)      do { error = (e); goto bad; } while (0)
89
90 /*
91  * FDDI output routine.
92  * Encapsulate a packet of type family for the local net.
93  * Use trailer local net encapsulation if enough data in first
94  * packet leaves a multiple of 512 bytes of data in remainder.
95  * Assumes that ifp is actually pointer to arpcom structure.
96  */
97 static int
98 fddi_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
99         struct route *ro)
100 {
101         u_int16_t type;
102         int loop_copy = 0, error = 0, hdrcmplt = 0;
103         u_char esrc[FDDI_ADDR_LEN], edst[FDDI_ADDR_LEN];
104         struct fddi_header *fh;
105 #if defined(INET) || defined(INET6)
106         struct llentry *lle;
107 #endif
108
109 #ifdef MAC
110         error = mac_ifnet_check_transmit(ifp, m);
111         if (error)
112                 senderr(error);
113 #endif
114
115         if (ifp->if_flags & IFF_MONITOR)
116                 senderr(ENETDOWN);
117         if (!((ifp->if_flags & IFF_UP) &&
118             (ifp->if_drv_flags & IFF_DRV_RUNNING)))
119                 senderr(ENETDOWN);
120         getmicrotime(&ifp->if_lastchange);
121
122         switch (dst->sa_family) {
123 #ifdef INET
124         case AF_INET: {
125                 struct rtentry *rt0 = NULL;
126
127                 if (ro != NULL)
128                         rt0 = ro->ro_rt;
129                 error = arpresolve(ifp, rt0, m, dst, edst, &lle);
130                 if (error)
131                         return (error == EWOULDBLOCK ? 0 : error);
132                 type = htons(ETHERTYPE_IP);
133                 break;
134         }
135         case AF_ARP:
136         {
137                 struct arphdr *ah;
138                 ah = mtod(m, struct arphdr *);
139                 ah->ar_hrd = htons(ARPHRD_ETHER);
140
141                 loop_copy = -1; /* if this is for us, don't do it */
142
143                 switch (ntohs(ah->ar_op)) {
144                 case ARPOP_REVREQUEST:
145                 case ARPOP_REVREPLY:
146                         type = htons(ETHERTYPE_REVARP);
147                         break;
148                 case ARPOP_REQUEST:
149                 case ARPOP_REPLY:
150                 default:
151                         type = htons(ETHERTYPE_ARP);
152                         break;
153                 }
154
155                 if (m->m_flags & M_BCAST)
156                         bcopy(ifp->if_broadcastaddr, edst, FDDI_ADDR_LEN);
157                 else
158                         bcopy(ar_tha(ah), edst, FDDI_ADDR_LEN);
159
160         }
161         break;
162 #endif /* INET */
163 #ifdef INET6
164         case AF_INET6:
165                 error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
166                 if (error)
167                         return (error); /* Something bad happened */
168                 type = htons(ETHERTYPE_IPV6);
169                 break;
170 #endif /* INET6 */
171         case pseudo_AF_HDRCMPLT:
172         {
173                 const struct ether_header *eh;
174
175                 hdrcmplt = 1;
176                 eh = (const struct ether_header *)dst->sa_data;
177                 bcopy(eh->ether_shost, esrc, FDDI_ADDR_LEN);
178                 /* FALLTHROUGH */
179         }
180
181         case AF_UNSPEC:
182         {
183                 const struct ether_header *eh;
184
185                 loop_copy = -1;
186                 eh = (const struct ether_header *)dst->sa_data;
187                 bcopy(eh->ether_dhost, edst, FDDI_ADDR_LEN);
188                 if (*edst & 1)
189                         m->m_flags |= (M_BCAST|M_MCAST);
190                 type = eh->ether_type;
191                 break;
192         }
193
194         case AF_IMPLINK:
195         {
196                 fh = mtod(m, struct fddi_header *);
197                 error = EPROTONOSUPPORT;
198                 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
199                         case FDDIFC_LLC_ASYNC: {
200                                 /* legal priorities are 0 through 7 */
201                                 if ((fh->fddi_fc & FDDIFC_Z) > 7)
202                                         goto bad;
203                                 break;
204                         }
205                         case FDDIFC_LLC_SYNC: {
206                                 /* FDDIFC_Z bits reserved, must be zero */
207                                 if (fh->fddi_fc & FDDIFC_Z)
208                                         goto bad;
209                                 break;
210                         }
211                         case FDDIFC_SMT: {
212                                 /* FDDIFC_Z bits must be non zero */
213                                 if ((fh->fddi_fc & FDDIFC_Z) == 0)
214                                         goto bad;
215                                 break;
216                         }
217                         default: {
218                                 /* anything else is too dangerous */
219                                 goto bad;
220                         }
221                 }
222                 error = 0;
223                 if (fh->fddi_dhost[0] & 1)
224                         m->m_flags |= (M_BCAST|M_MCAST);
225                 goto queue_it;
226         }
227         default:
228                 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
229                 senderr(EAFNOSUPPORT);
230         }
231
232         /*
233          * Add LLC header.
234          */
235         if (type != 0) {
236                 struct llc *l;
237                 M_PREPEND(m, LLC_SNAPFRAMELEN, M_NOWAIT);
238                 if (m == 0)
239                         senderr(ENOBUFS);
240                 l = mtod(m, struct llc *);
241                 l->llc_control = LLC_UI;
242                 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
243                 l->llc_snap.org_code[0] =
244                         l->llc_snap.org_code[1] =
245                         l->llc_snap.org_code[2] = 0;
246                 l->llc_snap.ether_type = htons(type);
247         }
248
249         /*
250          * Add local net header.  If no space in first mbuf,
251          * allocate another.
252          */
253         M_PREPEND(m, FDDI_HDR_LEN, M_NOWAIT);
254         if (m == 0)
255                 senderr(ENOBUFS);
256         fh = mtod(m, struct fddi_header *);
257         fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
258         bcopy((caddr_t)edst, (caddr_t)fh->fddi_dhost, FDDI_ADDR_LEN);
259   queue_it:
260         if (hdrcmplt)
261                 bcopy((caddr_t)esrc, (caddr_t)fh->fddi_shost, FDDI_ADDR_LEN);
262         else
263                 bcopy(IF_LLADDR(ifp), (caddr_t)fh->fddi_shost,
264                         FDDI_ADDR_LEN);
265
266         /*
267          * If a simplex interface, and the packet is being sent to our
268          * Ethernet address or a broadcast address, loopback a copy.
269          * XXX To make a simplex device behave exactly like a duplex
270          * device, we should copy in the case of sending to our own
271          * ethernet address (thus letting the original actually appear
272          * on the wire). However, we don't do that here for security
273          * reasons and compatibility with the original behavior.
274          */
275         if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
276                 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
277                         struct mbuf *n;
278                         n = m_copy(m, 0, (int)M_COPYALL);
279                         (void) if_simloop(ifp, n, dst->sa_family,
280                                           FDDI_HDR_LEN);
281                 } else if (bcmp(fh->fddi_dhost, fh->fddi_shost,
282                                 FDDI_ADDR_LEN) == 0) {
283                         (void) if_simloop(ifp, m, dst->sa_family,
284                                           FDDI_HDR_LEN);
285                         return (0);     /* XXX */
286                 }
287         }
288
289         error = (ifp->if_transmit)(ifp, m);
290         if (error)
291                 ifp->if_oerrors++;
292
293         return (error);
294
295 bad:
296         ifp->if_oerrors++;
297         if (m)
298                 m_freem(m);
299         return (error);
300 }
301
302 /*
303  * Process a received FDDI packet.
304  */
305 static void
306 fddi_input(ifp, m)
307         struct ifnet *ifp;
308         struct mbuf *m;
309 {
310         int isr;
311         struct llc *l;
312         struct fddi_header *fh;
313
314         /*
315          * Do consistency checks to verify assumptions
316          * made by code past this point.
317          */
318         if ((m->m_flags & M_PKTHDR) == 0) {
319                 if_printf(ifp, "discard frame w/o packet header\n");
320                 ifp->if_ierrors++;
321                 m_freem(m);
322                 return;
323         }
324         if (m->m_pkthdr.rcvif == NULL) {
325                 if_printf(ifp, "discard frame w/o interface pointer\n");
326                 ifp->if_ierrors++;
327                 m_freem(m);
328                 return;
329         }
330
331         m = m_pullup(m, FDDI_HDR_LEN);
332         if (m == NULL) {
333                 ifp->if_ierrors++;
334                 goto dropanyway;
335         }
336         fh = mtod(m, struct fddi_header *);
337
338         /*
339          * Discard packet if interface is not up.
340          */
341         if (!((ifp->if_flags & IFF_UP) &&
342             (ifp->if_drv_flags & IFF_DRV_RUNNING)))
343                 goto dropanyway;
344
345         /*
346          * Give bpf a chance at the packet.
347          */
348         BPF_MTAP(ifp, m);
349
350         /*
351          * Interface marked for monitoring; discard packet.
352          */
353         if (ifp->if_flags & IFF_MONITOR) {
354                 m_freem(m);
355                 return;
356         }
357
358 #ifdef MAC
359         mac_ifnet_create_mbuf(ifp, m);
360 #endif
361
362         /*
363          * Update interface statistics.
364          */
365         ifp->if_ibytes += m->m_pkthdr.len;
366         getmicrotime(&ifp->if_lastchange);
367
368         /*
369          * Discard non local unicast packets when interface
370          * is in promiscuous mode.
371          */
372         if ((ifp->if_flags & IFF_PROMISC) && ((fh->fddi_dhost[0] & 1) == 0) &&
373             (bcmp(IF_LLADDR(ifp), (caddr_t)fh->fddi_dhost,
374              FDDI_ADDR_LEN) != 0))
375                 goto dropanyway;
376
377         /*
378          * Set mbuf flags for bcast/mcast.
379          */
380         if (fh->fddi_dhost[0] & 1) {
381                 if (bcmp(ifp->if_broadcastaddr, fh->fddi_dhost,
382                     FDDI_ADDR_LEN) == 0)
383                         m->m_flags |= M_BCAST;
384                 else
385                         m->m_flags |= M_MCAST;
386                 ifp->if_imcasts++;
387         }
388
389 #ifdef M_LINK0
390         /*
391          * If this has a LLC priority of 0, then mark it so upper
392          * layers have a hint that it really came via a FDDI/Ethernet
393          * bridge.
394          */
395         if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
396                 m->m_flags |= M_LINK0;
397 #endif
398
399         /* Strip off FDDI header. */
400         m_adj(m, FDDI_HDR_LEN);
401
402         m = m_pullup(m, LLC_SNAPFRAMELEN);
403         if (m == 0) {
404                 ifp->if_ierrors++;
405                 goto dropanyway;
406         }
407         l = mtod(m, struct llc *);
408
409         switch (l->llc_dsap) {
410         case LLC_SNAP_LSAP:
411         {
412                 u_int16_t type;
413                 if ((l->llc_control != LLC_UI) ||
414                     (l->llc_ssap != LLC_SNAP_LSAP)) {
415                         ifp->if_noproto++;
416                         goto dropanyway;
417                 }
418                 if (l->llc_snap.org_code[0] != 0 ||
419                     l->llc_snap.org_code[1] != 0 ||
420                     l->llc_snap.org_code[2] != 0) {
421                         ifp->if_noproto++;
422                         goto dropanyway;
423                 }
424
425                 type = ntohs(l->llc_snap.ether_type);
426                 m_adj(m, LLC_SNAPFRAMELEN);
427
428                 switch (type) {
429 #ifdef INET
430                 case ETHERTYPE_IP:
431                         if ((m = ip_fastforward(m)) == NULL)
432                                 return;
433                         isr = NETISR_IP;
434                         break;
435
436                 case ETHERTYPE_ARP:
437                         if (ifp->if_flags & IFF_NOARP)
438                                 goto dropanyway;
439                         isr = NETISR_ARP;
440                         break;
441 #endif
442 #ifdef INET6
443                 case ETHERTYPE_IPV6:
444                         isr = NETISR_IPV6;
445                         break;
446 #endif
447 #ifdef DECNET
448                 case ETHERTYPE_DECNET:
449                         isr = NETISR_DECNET;
450                         break;
451 #endif
452                 default:
453                         /* printf("fddi_input: unknown protocol 0x%x\n", type); */
454                         ifp->if_noproto++;
455                         goto dropanyway;
456                 }
457                 break;
458         }
459                 
460         default:
461                 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
462                 ifp->if_noproto++;
463                 goto dropanyway;
464         }
465         M_SETFIB(m, ifp->if_fib);
466         netisr_dispatch(isr, m);
467         return;
468
469 dropanyway:
470         ifp->if_iqdrops++;
471         if (m)
472                 m_freem(m);
473         return;
474 }
475
476 /*
477  * Perform common duties while attaching to interface list
478  */
479 void
480 fddi_ifattach(ifp, lla, bpf)
481         struct ifnet *ifp;
482         const u_int8_t *lla;
483         int bpf;
484 {
485         struct ifaddr *ifa;
486         struct sockaddr_dl *sdl;
487
488         ifp->if_type = IFT_FDDI;
489         ifp->if_addrlen = FDDI_ADDR_LEN;
490         ifp->if_hdrlen = 21;
491
492         if_attach(ifp);         /* Must be called before additional assignments */
493
494         ifp->if_mtu = FDDIMTU;
495         ifp->if_output = fddi_output;
496         ifp->if_input = fddi_input;
497         ifp->if_resolvemulti = fddi_resolvemulti;
498         ifp->if_broadcastaddr = fddibroadcastaddr;
499         ifp->if_baudrate = 100000000;
500 #ifdef IFF_NOTRAILERS
501         ifp->if_flags |= IFF_NOTRAILERS;
502 #endif
503         ifa = ifp->if_addr;
504         KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
505
506         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
507         sdl->sdl_type = IFT_FDDI;
508         sdl->sdl_alen = ifp->if_addrlen;
509         bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
510
511         if (bpf)
512                 bpfattach(ifp, DLT_FDDI, FDDI_HDR_LEN);
513
514         return;
515 }
516
517 void
518 fddi_ifdetach(ifp, bpf)
519         struct ifnet *ifp;
520         int bpf;
521 {
522      
523         if (bpf)
524                 bpfdetach(ifp);
525
526         if_detach(ifp);
527
528         return;
529 }
530
531 int
532 fddi_ioctl (ifp, command, data)
533         struct ifnet *ifp;
534         u_long command;
535         caddr_t data;
536 {
537         struct ifaddr *ifa;
538         struct ifreq *ifr;
539         int error;
540
541         ifa = (struct ifaddr *) data;
542         ifr = (struct ifreq *) data;
543         error = 0;
544
545         switch (command) {
546         case SIOCSIFADDR:
547                 ifp->if_flags |= IFF_UP;
548
549                 switch (ifa->ifa_addr->sa_family) {
550 #ifdef INET
551                 case AF_INET:   /* before arpwhohas */
552                         ifp->if_init(ifp->if_softc);
553                         arp_ifinit(ifp, ifa);
554                         break;
555 #endif
556                 default:
557                         ifp->if_init(ifp->if_softc);
558                         break;
559                 }
560                 break;
561         case SIOCGIFADDR: {
562                         struct sockaddr *sa;
563
564                         sa = (struct sockaddr *) & ifr->ifr_data;
565                         bcopy(IF_LLADDR(ifp),
566                               (caddr_t) sa->sa_data, FDDI_ADDR_LEN);
567
568                 }
569                 break;
570         case SIOCSIFMTU:
571                 /*
572                  * Set the interface MTU.
573                  */
574                 if (ifr->ifr_mtu > FDDIMTU) {
575                         error = EINVAL;
576                 } else {
577                         ifp->if_mtu = ifr->ifr_mtu;
578                 }
579                 break;
580         default:
581                 error = EINVAL;
582                 break;
583         }
584
585         return (error);
586 }
587
588 static int
589 fddi_resolvemulti(ifp, llsa, sa)
590         struct ifnet *ifp;
591         struct sockaddr **llsa;
592         struct sockaddr *sa;
593 {
594         struct sockaddr_dl *sdl;
595 #ifdef INET
596         struct sockaddr_in *sin;
597 #endif
598 #ifdef INET6
599         struct sockaddr_in6 *sin6;
600 #endif
601         u_char *e_addr;
602
603         switch(sa->sa_family) {
604         case AF_LINK:
605                 /*
606                  * No mapping needed. Just check that it's a valid MC address.
607                  */
608                 sdl = (struct sockaddr_dl *)sa;
609                 e_addr = LLADDR(sdl);
610                 if ((e_addr[0] & 1) != 1)
611                         return (EADDRNOTAVAIL);
612                 *llsa = 0;
613                 return (0);
614
615 #ifdef INET
616         case AF_INET:
617                 sin = (struct sockaddr_in *)sa;
618                 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
619                         return (EADDRNOTAVAIL);
620                 sdl = link_init_sdl(ifp, *llsa, IFT_FDDI);
621                 sdl->sdl_nlen = 0;
622                 sdl->sdl_alen = FDDI_ADDR_LEN;
623                 sdl->sdl_slen = 0;
624                 e_addr = LLADDR(sdl);
625                 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
626                 *llsa = (struct sockaddr *)sdl;
627                 return (0);
628 #endif
629 #ifdef INET6
630         case AF_INET6:
631                 sin6 = (struct sockaddr_in6 *)sa;
632                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
633                         /*
634                          * An IP6 address of 0 means listen to all
635                          * of the Ethernet multicast address used for IP6.
636                          * (This is used for multicast routers.)
637                          */
638                         ifp->if_flags |= IFF_ALLMULTI;
639                         *llsa = 0;
640                         return (0);
641                 }
642                 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
643                         return (EADDRNOTAVAIL);
644                 sdl = link_init_sdl(ifp, *llsa, IFT_FDDI);
645                 sdl->sdl_nlen = 0;
646                 sdl->sdl_alen = FDDI_ADDR_LEN;
647                 sdl->sdl_slen = 0;
648                 e_addr = LLADDR(sdl);
649                 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
650                 *llsa = (struct sockaddr *)sdl;
651                 return (0);
652 #endif
653
654         default:
655                 /*
656                  * Well, the text isn't quite right, but it's the name
657                  * that counts...
658                  */
659                 return (EAFNOSUPPORT);
660         }
661
662         return (0);
663 }
664
665 static moduledata_t fddi_mod = {
666         "fddi", /* module name */
667         NULL,   /* event handler */
668         0       /* extra data */
669 };
670
671 DECLARE_MODULE(fddi, fddi_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
672 MODULE_VERSION(fddi, 1);