]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_iso88025subr.c
This commit was generated by cvs2svn to compensate for changes in r91799,
[FreeBSD/FreeBSD.git] / sys / net / if_iso88025subr.c
1 /*
2  * Copyright (c) 1998, Larry Lile
3  * All rights reserved.
4  *
5  * For latest sources and information on this driver, please
6  * go to http://anarchy.stdio.com.
7  *
8  * Questions, comments or suggestions should be directed to
9  * Larry Lile <lile@stdio.com>.
10  * 
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice unmodified, this list of conditions, and the following
16  *    disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  *
35  */
36
37 /*
38  *
39  * General ISO 802.5 (Token Ring) support routines
40  * 
41  */
42
43 #include "opt_inet.h"
44 #include "opt_inet6.h"
45 #include "opt_ipx.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/mbuf.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h> 
52 #include <sys/sysctl.h>
53
54 #include <net/if.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/if_llc.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60
61 #include <net/if_arp.h>
62
63 #include <net/iso88025.h>
64
65 #if defined(INET) || defined(INET6)
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/if_ether.h>
69 #endif
70 #ifdef INET6
71 #include <netinet6/nd6.h>
72 #endif
73
74 #ifdef IPX
75 #include <netipx/ipx.h>
76 #include <netipx/ipx_if.h>
77 #endif
78
79 #include <net/bpf.h>
80
81 #include <machine/md_var.h>
82
83 #include <vm/vm.h>
84 #include <vm/vm_param.h>
85 #include <vm/pmap.h>
86
87 #include <net/iso88025.h>
88
89 #define IFP2AC(IFP) ((struct arpcom *)IFP)
90
91 void
92 iso88025_ifattach(struct ifnet *ifp)
93 {
94     register struct ifaddr *ifa = NULL;
95     register struct sockaddr_dl *sdl;
96
97     ifp->if_type = IFT_ISO88025;
98     ifp->if_addrlen = ISO88025_ADDR_LEN;
99     ifp->if_hdrlen = ISO88025_HDR_LEN;
100     if (ifp->if_baudrate == 0)
101         ifp->if_baudrate = TR_16MBPS; /* 16Mbit should be a safe default */
102     if (ifp->if_mtu == 0)
103         ifp->if_mtu = ISO88025_DEFAULT_MTU;
104     ifp->if_broadcastaddr = etherbroadcastaddr;
105
106         ifa = ifaddr_byindex(ifp->if_index);
107         if (ifa == 0) {
108                 printf("iso88025_ifattach: no lladdr!\n");
109                 return;
110         }
111         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
112         sdl->sdl_type = IFT_ISO88025;
113         sdl->sdl_alen = ifp->if_addrlen;
114         bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
115 }
116
117 /*
118  * Perform common duties while detaching a Token Ring interface
119  */
120 void
121 iso88025_ifdetach(ifp, bpf)
122         struct ifnet *ifp;
123         int bpf;
124 {
125         if (bpf)
126                 bpfdetach(ifp);
127         if_detach(ifp);
128 }
129
130
131 int
132 iso88025_ioctl(struct ifnet *ifp, int command, caddr_t data)
133 {
134         struct ifaddr *ifa = (struct ifaddr *) data;
135         struct ifreq *ifr = (struct ifreq *) data;
136         int error = 0;
137
138         switch (command) {
139         case SIOCSIFADDR:
140                 ifp->if_flags |= IFF_UP;
141
142                 switch (ifa->ifa_addr->sa_family) {
143 #ifdef INET
144                 case AF_INET:
145                         ifp->if_init(ifp->if_softc);    /* before arpwhohas */
146                         arp_ifinit(ifp, ifa);
147                         break;
148 #endif  /* INET */
149 #ifdef IPX
150                 /*
151                  * XXX - This code is probably wrong
152                  */
153                 case AF_IPX:
154                         {
155                         register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
156                         struct arpcom *ac = IFP2AC(ifp);
157
158                         if (ipx_nullhost(*ina))
159                                 ina->x_host =
160                                     *(union ipx_host *)
161                                     ac->ac_enaddr;
162                         else {
163                                 bcopy((caddr_t) ina->x_host.c_host,
164                                       (caddr_t) ac->ac_enaddr,
165                                       sizeof(ac->ac_enaddr));
166                         }
167
168                         /*
169                          * Set new address
170                          */
171                         ifp->if_init(ifp->if_softc);
172                         break;
173                         }
174 #endif  /* IPX */
175                 default:
176                         ifp->if_init(ifp->if_softc);
177                         break;
178                 }
179                 break;
180
181         case SIOCGIFADDR:
182                 {
183                         struct sockaddr *sa;
184
185                         sa = (struct sockaddr *) & ifr->ifr_data;
186                         bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
187                               (caddr_t) sa->sa_data, ISO88025_ADDR_LEN);
188                 }
189                 break;
190
191         case SIOCSIFMTU:
192                 /*
193                  * Set the interface MTU.
194                  */
195                 if (ifr->ifr_mtu > ISO88025_MAX_MTU) {
196                         error = EINVAL;
197                 } else {
198                         ifp->if_mtu = ifr->ifr_mtu;
199                 }
200                 break;
201         }
202         return (error);
203 }
204
205 /*
206  * ISO88025 encapsulation
207  */
208 int
209 iso88025_output(ifp, m, dst, rt0)
210         struct ifnet *ifp;
211         struct mbuf *m;
212         struct sockaddr *dst;
213         struct rtentry *rt0;
214 {
215         u_int16_t snap_type = 0;
216         int loop_copy = 0, error = 0, rif_len = 0;
217         u_char edst[ISO88025_ADDR_LEN];
218         struct iso88025_header *th;
219         struct iso88025_header gen_th;
220         struct sockaddr_dl *sdl = NULL;
221         struct rtentry *rt;
222         struct arpcom *ac = (struct arpcom *)ifp;
223
224         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
225                 senderr(ENETDOWN);
226         getmicrotime(&ifp->if_lastchange);
227
228         rt = rt0;
229         if (rt != NULL) {
230                 if ((rt->rt_flags & RTF_UP) == 0) {
231                         rt0 = rt = rtalloc1(dst, 1, 0UL);
232                         if (rt0)
233                                 rt->rt_refcnt--;
234                         else
235                                 senderr(EHOSTUNREACH);
236                 }
237                 if (rt->rt_flags & RTF_GATEWAY) {
238                         if (rt->rt_gwroute == 0)
239                                 goto lookup;
240                         if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
241                                 rtfree(rt); rt = rt0;
242                         lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
243                                                           0UL);
244                                 if ((rt = rt->rt_gwroute) == 0)
245                                         senderr(EHOSTUNREACH);
246                         }
247                 }
248                 if (rt->rt_flags & RTF_REJECT)
249                         if (rt->rt_rmx.rmx_expire == 0 ||
250                             time_second < rt->rt_rmx.rmx_expire)
251                                 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
252         }
253
254         /* Calculate routing info length based on arp table entry */
255         if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway))
256                 if (sdl->sdl_rcf != NULL)
257                         rif_len = TR_RCF_RIFLEN(sdl->sdl_rcf);
258
259         /* Generate a generic 802.5 header for the packet */
260         gen_th.ac = TR_AC;
261         gen_th.fc = TR_LLC_FRAME;
262         (void)memcpy((caddr_t)gen_th.iso88025_shost, (caddr_t)ac->ac_enaddr,
263                 sizeof(ac->ac_enaddr));
264         if (rif_len) {
265                 gen_th.iso88025_shost[0] |= TR_RII;
266                 if (rif_len > 2) {
267                         gen_th.rcf = sdl->sdl_rcf;
268                         (void)memcpy((caddr_t)gen_th.rd,
269                                 (caddr_t)sdl->sdl_route, rif_len - 2);
270                 }
271         }
272         
273         switch (dst->sa_family) {
274 #ifdef INET
275         case AF_INET:
276                 if (!arpresolve(ifp, rt, m, dst, edst, rt0))
277                         return (0);     /* if not yet resolved */
278                 snap_type = ETHERTYPE_IP;
279                 break;
280 #endif  /* INET */
281 #ifdef NOT_YET
282 #ifdef INET6
283         case AF_INET6:
284                 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
285                         /* Something bad happened */
286                         return(0);
287                 }
288                 snap_type = ETHERTYPE_IPV6;
289                 break;
290 #endif  /* INET6 */
291 #endif  /* NOT_YET */
292 #ifdef IPX
293         case AF_IPX:
294         {
295                 u_int8_t        *cp;
296
297                 snap_type = 0;
298                 bcopy((caddr_t)&(satoipx_addr(dst).x_host), (caddr_t)edst,
299                         sizeof (edst));
300
301                 M_PREPEND(m, 3, M_TRYWAIT);
302                 if (m == 0)
303                         senderr(ENOBUFS);
304                 m = m_pullup(m, 3);
305                 if (m == 0)
306                         senderr(ENOBUFS);
307                 cp = mtod(m, u_int8_t *);
308                 *cp++ = ETHERTYPE_IPX_8022;
309                 *cp++ = ETHERTYPE_IPX_8022;
310                 *cp++ = LLC_UI;
311         }
312         break;
313 #endif  /* IPX */
314         case AF_UNSPEC:
315         {
316                 struct iso88025_sockaddr_data *sd;
317                 /*
318                  * For AF_UNSPEC sockaddr.sa_data must contain all of the
319                  * mac information needed to send the packet.  This allows
320                  * full mac, llc, and source routing function to be controlled.
321                  * llc and source routing information must already be in the
322                  * mbuf provided, ac/fc are set in sa_data.  sockaddr.sa_data
323                  * should be a iso88025_sockaddr_data structure see iso88025.h
324                  */
325                 loop_copy = -1;
326                 sd = (struct iso88025_sockaddr_data *)dst->sa_data;
327                 gen_th.ac = sd->ac;
328                 gen_th.fc = sd->fc;
329                 (void)memcpy((caddr_t)edst, (caddr_t)sd->ether_dhost,
330                         sizeof(sd->ether_dhost));
331                 (void)memcpy((caddr_t)gen_th.iso88025_shost,
332                         (caddr_t)sd->ether_shost, sizeof(sd->ether_shost));
333                 rif_len = 0;
334                 break;
335         }
336         default:
337                 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
338                         dst->sa_family);
339                 senderr(EAFNOSUPPORT);
340                 break;
341         }
342
343         if (snap_type != 0) {
344                 struct llc *l;
345                 M_PREPEND(m, sizeof (struct llc), M_DONTWAIT);
346                 if (m == 0)
347                         senderr(ENOBUFS);
348                 l = mtod(m, struct llc *);
349                 l->llc_un.type_snap.ether_type = htons(snap_type);
350                 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
351                 l->llc_un.type_snap.control = LLC_UI;
352                 l->llc_un.type_snap.org_code[0] =
353                         l->llc_un.type_snap.org_code[1] =
354                         l->llc_un.type_snap.org_code[2] = 0;
355         }
356
357         /*
358          * Add local net header.  If no space in first mbuf,
359          * allocate another.
360          */
361         M_PREPEND(m, ISO88025_HDR_LEN + rif_len, M_DONTWAIT);
362         if (m == 0)
363                 senderr(ENOBUFS);
364
365         (void)memcpy((caddr_t)&gen_th.iso88025_dhost, (caddr_t)edst,
366                      sizeof(edst));
367
368         /* Copy as much of the generic header as is needed into the mbuf */
369         th = mtod(m, struct iso88025_header *);
370         memcpy(th, &gen_th, ISO88025_HDR_LEN + rif_len);
371
372         /*
373          * If a simplex interface, and the packet is being sent to our
374          * Ethernet address or a broadcast address, loopback a copy.
375          * XXX To make a simplex device behave exactly like a duplex
376          * device, we should copy in the case of sending to our own
377          * ethernet address (thus letting the original actually appear
378          * on the wire). However, we don't do that here for security
379          * reasons and compatibility with the original behavior.
380          */     
381         if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
382                 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 
383                         struct mbuf *n;
384                         n = m_copy(m, 0, (int)M_COPYALL);
385                         (void)if_simloop(ifp, n, dst->sa_family,
386                                           ISO88025_HDR_LEN);
387                 } else
388                         if (bcmp(th->iso88025_dhost, th->iso88025_shost,
389                                  ETHER_ADDR_LEN) == 0) {
390                                 (void)if_simloop(ifp, m, dst->sa_family,
391                                                  ISO88025_HDR_LEN);
392                                 return(0);      /* XXX */
393                         }       
394         }      
395
396         if (! IF_HANDOFF_ADJ(&ifp->if_snd, m, ifp, ISO88025_HDR_LEN + (sizeof(struct llc))) ) {
397                 printf("iso88025_output: packet dropped QFULL.\n");
398                 senderr(ENOBUFS);
399         }
400         return (error);
401
402 bad:
403         if (m)
404                 m_freem(m);
405         return (error);
406 }
407
408 /*
409  * ISO 88025 de-encapsulation
410  */
411 void
412 iso88025_input(ifp, th, m)
413         struct ifnet *ifp;
414         struct iso88025_header *th;
415         struct mbuf *m;
416 {
417         register struct ifqueue *inq;
418         register struct llc *l;
419
420         if ((ifp->if_flags & IFF_UP) == 0) {
421                 m_freem(m);
422                 return;
423         }
424
425         getmicrotime(&ifp->if_lastchange);
426         ifp->if_ibytes += m->m_pkthdr.len + sizeof(*th);
427
428         if (th->iso88025_dhost[0] & 1) {
429                 if (bcmp((caddr_t)etherbroadcastaddr,
430                          (caddr_t)th->iso88025_dhost,
431                          sizeof(etherbroadcastaddr)) == 0)
432                         m->m_flags |= M_BCAST;
433                 else
434                         m->m_flags |= M_MCAST;
435                 ifp->if_imcasts++;
436         } 
437
438         l = mtod(m, struct llc *);
439
440         switch (l->llc_dsap) {
441 #ifdef IPX
442         case ETHERTYPE_IPX_8022:        /* Thanks a bunch Novell */
443                 if ((l->llc_control != LLC_UI) ||
444                     (l->llc_ssap != ETHERTYPE_IPX_8022))
445                         goto dropanyway;
446
447                 th->iso88025_shost[0] &= ~(TR_RII); 
448                 m_adj(m, 3);
449                 schednetisr(NETISR_IPX);
450                 inq = &ipxintrq;
451                 break;
452 #endif  /* IPX */
453         case LLC_SNAP_LSAP: {
454                 u_int16_t type;
455                 if ((l->llc_control != LLC_UI) ||
456                     (l->llc_ssap != LLC_SNAP_LSAP))
457                         goto dropanyway;
458
459                 if (l->llc_un.type_snap.org_code[0] != 0 ||
460                     l->llc_un.type_snap.org_code[1] != 0 ||
461                     l->llc_un.type_snap.org_code[2] != 0)
462                         goto dropanyway;
463
464                 type = ntohs(l->llc_un.type_snap.ether_type);
465                 m_adj(m, sizeof(struct llc));
466                 switch (type) {
467 #ifdef INET
468                 case ETHERTYPE_IP:
469                         th->iso88025_shost[0] &= ~(TR_RII); 
470                         if (ipflow_fastforward(m))
471                                 return;
472                         schednetisr(NETISR_IP);
473                         inq = &ipintrq;
474                         break;
475
476                 case ETHERTYPE_ARP:
477                         if (ifp->if_flags & IFF_NOARP)
478                                 goto dropanyway;
479                         schednetisr(NETISR_ARP);
480                         inq = &arpintrq;
481                         break;
482 #endif  /* INET */
483 #ifdef IPX_SNAP /* XXX: Not supported! */
484                 case ETHERTYPE_IPX:
485                         th->iso88025_shost[0] &= ~(TR_RII); 
486                         schednetisr(NETISR_IPX);
487                         inq = &ipxintrq;
488                         break;
489 #endif  /* IPX_SNAP */
490 #ifdef NOT_YET
491 #ifdef INET6
492                 case ETHERTYPE_IPV6:
493                         th->iso88025_shost[0] &= ~(TR_RII); 
494                         schednetisr(NETISR_IPV6);
495                         inq = &ip6intrq;
496                         break;
497 #endif  /* INET6 */
498 #endif  /* NOT_YET */
499                 default:
500                         printf("iso88025_input: unexpected llc_snap ether_type  0x%02x\n", type);
501                         m_freem(m);
502                         return;
503                 }
504                 break;
505         }
506         case LLC_ISO_LSAP:
507                 switch (l->llc_control) {
508                 case LLC_UI:
509                         goto dropanyway;
510                         break;
511                 case LLC_XID:
512                 case LLC_XID_P:
513                         if(m->m_len < ISO88025_ADDR_LEN)
514                                 goto dropanyway;
515                         l->llc_window = 0;
516                         l->llc_fid = 9;  
517                         l->llc_class = 1;
518                         l->llc_dsap = l->llc_ssap = 0;
519                         /* Fall through to */  
520                 case LLC_TEST:
521                 case LLC_TEST_P:
522                 {
523                         struct sockaddr sa;
524                         struct arpcom *ac = (struct arpcom *)ifp;
525                         struct iso88025_sockaddr_data *th2;
526                         int i;
527                         u_char c = l->llc_dsap;
528
529                         if (th->iso88025_shost[0] & TR_RII) { /* XXX */
530                                 printf("iso88025_input: dropping source routed LLC_TEST\n");
531                                 m_free(m);
532                                 return;
533                         }
534                         l->llc_dsap = l->llc_ssap;
535                         l->llc_ssap = c;
536                         if (m->m_flags & (M_BCAST | M_MCAST))
537                                 bcopy((caddr_t)ac->ac_enaddr, 
538                                         (caddr_t)th->iso88025_dhost,
539                                         ISO88025_ADDR_LEN);
540                         sa.sa_family = AF_UNSPEC;
541                         sa.sa_len = sizeof(sa);
542                         th2 = (struct iso88025_sockaddr_data *)sa.sa_data;
543                         for (i = 0; i < ISO88025_ADDR_LEN; i++) {
544                                 th2->ether_shost[i] = c = th->iso88025_dhost[i];
545                                 th2->ether_dhost[i] = th->iso88025_dhost[i] =
546                                         th->iso88025_shost[i];
547                                 th->iso88025_shost[i] = c;
548                         }
549                         th2->ac = TR_AC;
550                         th2->fc = TR_LLC_FRAME;
551                         ifp->if_output(ifp, m, &sa, NULL);
552                         return;
553                 }
554                 default:
555                         printf("iso88025_input: unexpected llc control 0x%02x\n", l->llc_control);
556                         m_freem(m);
557                         return;
558                 }
559                 break;
560         default:
561                 printf("iso88025_input: unknown dsap 0x%x\n", l->llc_dsap);
562                 ifp->if_noproto++;
563         dropanyway:
564                 m_freem(m);
565                 return;
566         }
567
568         if (! IF_HANDOFF(inq, m, NULL))
569                 printf("iso88025_input: Packet dropped (Queue full).\n");
570 }