Initial import from FreeBSD RELENG_4:
[dragonfly.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: src/sys/net/if_iso88025subr.c,v 1.7.2.7 2002/06/18 00:15:31 kbyanc Exp $
34  *
35  */
36
37 /*
38  *
39  * General ISO 802.5 (Token Ring) support routines
40  * 
41  */
42
43 #include "opt_inet.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.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 #ifdef INET
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/if_ether.h>
69 #endif
70
71 #include <net/bpf.h>
72
73 #include <machine/clock.h>
74 #include <machine/md_var.h>
75
76 #include <i386/isa/isa_device.h>
77
78 #include <vm/vm.h>
79 #include <vm/vm_param.h>
80 #include <vm/pmap.h>
81
82 #include <sys/kernel.h>
83 #include <net/iso88025.h>
84
85 void
86 iso88025_ifattach(struct ifnet *ifp)
87 {
88     register struct ifaddr *ifa = NULL;
89     register struct sockaddr_dl *sdl;
90
91     ifp->if_type = IFT_ISO88025;
92     ifp->if_addrlen = ISO88025_ADDR_LEN;
93     ifp->if_hdrlen = ISO88025_HDR_LEN;
94     if (ifp->if_baudrate == 0)
95         ifp->if_baudrate = TR_16MBPS; /* 16Mbit should be a safe default */
96     if (ifp->if_mtu == 0)
97         ifp->if_mtu = ISO88025_DEFAULT_MTU;
98
99         ifa = ifnet_addrs[ifp->if_index - 1];
100         if (ifa == 0) {
101                 printf("iso88025_ifattach: no lladdr!\n");
102                 return;
103         }
104         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
105         sdl->sdl_type = IFT_ISO88025;
106         sdl->sdl_alen = ifp->if_addrlen;
107         bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
108 }
109
110 int
111 iso88025_ioctl(struct ifnet *ifp, int command, caddr_t data)
112 {
113         struct ifaddr *ifa = (struct ifaddr *) data;
114         struct ifreq *ifr = (struct ifreq *) data;
115         int error = 0;
116
117         switch (command) {
118         case SIOCSIFADDR:
119                 ifp->if_flags |= IFF_UP;
120
121                 switch (ifa->ifa_addr->sa_family) {
122 #ifdef INET
123                 case AF_INET:
124                         ifp->if_init(ifp->if_softc);    /* before arpwhohas */
125                         arp_ifinit(ifp, ifa);
126                         break;
127 #endif
128                 default:
129                         ifp->if_init(ifp->if_softc);
130                         break;
131                 }
132                 break;
133
134         case SIOCGIFADDR:
135                 {
136                         struct sockaddr *sa;
137
138                         sa = (struct sockaddr *) & ifr->ifr_data;
139                         bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
140                               (caddr_t) sa->sa_data, ISO88025_ADDR_LEN);
141                 }
142                 break;
143
144         case SIOCSIFMTU:
145                 /*
146                  * Set the interface MTU.
147                  */
148                 if (ifr->ifr_mtu > ISO88025_MAX_MTU) {
149                         error = EINVAL;
150                 } else {
151                         ifp->if_mtu = ifr->ifr_mtu;
152                 }
153                 break;
154         }
155         return (error);
156 }
157
158 /*
159  * ISO88025 encapsulation
160  */
161 int
162 iso88025_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, struct rtentry *rt0)
163 {
164         register struct iso88025_header *th;
165         struct iso88025_header gen_th;
166         register struct iso88025_sockaddr_data *sd = (struct iso88025_sockaddr_data *)dst->sa_data;
167         register struct llc *l;
168         register struct sockaddr_dl *sdl = NULL;
169         int s, error = 0, rif_len = 0;
170         u_char edst[6];
171         register struct rtentry *rt;
172         int len = m->m_pkthdr.len, loop_copy = 0;
173         struct arpcom *ac = (struct arpcom *)ifp;
174
175         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
176                 senderr(ENETDOWN);
177         rt = rt0;
178         if (rt) {
179                 if ((rt->rt_flags & RTF_UP) == 0) {
180                         rt0 = rt = rtalloc1(dst, 1, 0UL);
181                         if (rt0)
182                                 rt->rt_refcnt--;
183                         else
184                                 senderr(EHOSTUNREACH);
185                 }
186                 if (rt->rt_flags & RTF_GATEWAY) {
187                         if (rt->rt_gwroute == 0)
188                                 goto lookup;
189                         if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
190                                 rtfree(rt); rt = rt0;
191                         lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
192                                                           0UL);
193                                 if ((rt = rt->rt_gwroute) == 0)
194                                         senderr(EHOSTUNREACH);
195                         }
196                 }
197                 if (rt->rt_flags & RTF_REJECT)
198                         if (rt->rt_rmx.rmx_expire == 0 ||
199                             time_second < rt->rt_rmx.rmx_expire)
200                                 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
201         }
202
203         /* Calculate routing info length based on arp table entry */
204         if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway))
205                 if (SDL_ISO88025(sdl)->trld_rcf != NULL)
206                         rif_len = TR_RCF_RIFLEN(SDL_ISO88025(sdl)->trld_rcf);
207
208         /* Generate a generic 802.5 header for the packet */
209         gen_th.ac = TR_AC;
210         gen_th.fc = TR_LLC_FRAME;
211         memcpy(gen_th.iso88025_shost, ac->ac_enaddr, sizeof(ac->ac_enaddr));
212         if (rif_len) {
213                 gen_th.iso88025_shost[0] |= TR_RII;
214                 if (rif_len > 2) {
215                         gen_th.rcf = sdl->sdl_rcf;
216                         memcpy(gen_th.rd, sdl->sdl_route, rif_len - 2);
217                 }
218         }
219         
220
221         switch (dst->sa_family) {
222 #ifdef INET
223         case AF_INET:
224                 if (!arpresolve(ifp, rt, m, dst, edst, rt0))
225                         return (0);     /* if not yet resolved */
226                 /* Add LLC and SNAP headers */
227                 M_PREPEND(m, 8, M_DONTWAIT);
228                 if (m == 0)
229                         senderr(ENOBUFS);
230                 l = mtod(m, struct llc *);
231                 l->llc_un.type_snap.ether_type = htons(ETHERTYPE_IP);
232                 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
233                 l->llc_un.type_snap.control = LLC_UI;
234                 l->llc_un.type_snap.org_code[0] = 0x0;
235                 l->llc_un.type_snap.org_code[1] = 0x0;
236                 l->llc_un.type_snap.org_code[2] = 0x0;
237                 memcpy(gen_th.iso88025_dhost, edst, sizeof(edst));
238                 break;
239 #endif
240
241         case AF_UNSPEC:
242                 /*
243                  * For AF_UNSPEC sockaddr.sa_data must contain all of the
244                  * mac information needed to send the packet.  This allows
245                  * full mac, llc, and source routing function to be controlled.
246                  * llc and source routing information must already be in the
247                  * mbuf provided, ac/fc are set in sa_data.  sockaddr.sa_data
248                  * should be a iso88025_sockaddr_data structure see iso88025.h
249                  */
250                 loop_copy = -1;
251                 sd = (struct iso88025_sockaddr_data *)dst->sa_data;
252                 gen_th.ac = sd->ac;
253                 gen_th.fc = sd->fc;
254                 memcpy(gen_th.iso88025_dhost, sd->ether_dhost, sizeof(sd->ether_dhost));
255                 memcpy(gen_th.iso88025_shost, sd->ether_shost, sizeof(sd->ether_shost));
256                 rif_len = 0;
257                 break;
258
259         default:
260                 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
261                         dst->sa_family);
262                 senderr(EAFNOSUPPORT);
263         }
264
265         /*
266          * Add local net header.  If no space in first mbuf,
267          * allocate another.
268          */
269         
270         M_PREPEND(m, ISO88025_HDR_LEN + rif_len, M_DONTWAIT);
271         if (m == 0)
272                 senderr(ENOBUFS);
273
274         /* Copy as much of the generic header as is needed into the mbuf */
275         th = mtod(m, struct iso88025_header *);
276         memcpy(th, &gen_th, ISO88025_HDR_LEN + rif_len);
277
278         /*
279          * If a simplex interface, and the packet is being sent to our
280          * Ethernet address or a broadcast address, loopback a copy.
281          * XXX To make a simplex device behave exactly like a duplex
282          * device, we should copy in the case of sending to our own
283          * ethernet address (thus letting the original actually appear
284          * on the wire). However, we don't do that here for security
285          * reasons and compatibility with the original behavior.
286          */     
287         if ((ifp->if_flags & IFF_SIMPLEX) &&
288            (loop_copy != -1)) {
289                 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 
290                         struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
291                         (void) if_simloop(ifp,
292                             n, dst->sa_family, ISO88025_HDR_LEN);
293                 } else if (bcmp(th->iso88025_dhost,
294                     th->iso88025_shost, ETHER_ADDR_LEN) == 0) {
295                         (void) if_simloop(ifp,
296                             m, dst->sa_family, ISO88025_HDR_LEN);
297                         return(0);      /* XXX */
298                 }       
299         }      
300
301         s = splimp();
302         /*
303          * Queue message on interface, and start output if interface
304          * not yet active.
305          */
306         if (IF_QFULL(&ifp->if_snd)) {
307             printf("iso88025_output: packet dropped QFULL.\n");
308                 IF_DROP(&ifp->if_snd);
309                 splx(s);
310                 senderr(ENOBUFS);
311         }
312         if (m->m_flags & M_MCAST)
313                 ifp->if_omcasts++;
314         IF_ENQUEUE(&ifp->if_snd, m);
315         if ((ifp->if_flags & IFF_OACTIVE) == 0)
316                 (*ifp->if_start)(ifp);
317         splx(s);
318         ifp->if_obytes += len + ISO88025_HDR_LEN + 8;
319         return (error);
320
321 bad:
322         if (m)
323                 m_freem(m);
324         return (error);
325 }
326
327 /*
328  * ISO 88025 de-encapsulation
329  */
330 void
331 iso88025_input(struct ifnet *ifp, struct iso88025_header *th, struct mbuf *m)
332 {
333         register struct ifqueue *inq;
334         u_short ether_type;
335         int s;
336         register struct llc *l = mtod(m, struct llc *);
337
338         if ((ifp->if_flags & IFF_UP) == 0) {
339                 m_freem(m);
340                 return;
341         }
342
343         switch (l->llc_control) {
344         case LLC_UI:
345                 break;
346         case LLC_TEST:
347         case LLC_TEST_P:
348         {
349                 struct sockaddr sa;
350                 struct arpcom *ac = (struct arpcom *)ifp;
351                 struct iso88025_sockaddr_data *th2;
352                 int i;
353                 u_char c = l->llc_dsap;
354
355                 if (th->iso88025_shost[0] & TR_RII) { /* XXX */
356                         printf("iso88025_input: dropping source routed LLC_TEST\n");
357                         m_free(m);
358                         return;
359                 }
360                 l->llc_dsap = l->llc_ssap;
361                 l->llc_ssap = c;
362                 if (m->m_flags & (M_BCAST | M_MCAST))
363                         bcopy((caddr_t)ac->ac_enaddr, 
364                               (caddr_t)th->iso88025_dhost, ISO88025_ADDR_LEN);
365                 sa.sa_family = AF_UNSPEC;
366                 sa.sa_len = sizeof(sa);
367                 th2 = (struct iso88025_sockaddr_data *)sa.sa_data;
368                 for (i = 0; i < ISO88025_ADDR_LEN; i++) {
369                         th2->ether_shost[i] = c = th->iso88025_dhost[i];
370                         th2->ether_dhost[i] = th->iso88025_dhost[i] = th->iso88025_shost[i];
371                         th->iso88025_shost[i] = c;
372                 }
373                 th2->ac = TR_AC;
374                 th2->fc = TR_LLC_FRAME;
375                 ifp->if_output(ifp, m, &sa, NULL);
376                 return;
377         }
378         default:
379                 printf("iso88025_input: unexpected llc control 0x%02x\n", l->llc_control);
380                 m_freem(m);
381                 return;
382         }
383
384         m->m_pkthdr.len -= 8;
385         m->m_len -= 8;
386         m->m_data += 8; /* Length of LLC header in our case */
387
388         ifp->if_ibytes += m->m_pkthdr.len + sizeof(*th);
389         if (th->iso88025_dhost[0] & 1) {
390                 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)th->iso88025_dhost, sizeof(etherbroadcastaddr)) == 0)
391                         m->m_flags |= M_BCAST;
392                 else
393                         m->m_flags |= M_MCAST;
394         } 
395         if (m->m_flags & (M_BCAST|M_MCAST))
396                 ifp->if_imcasts++;
397
398         ether_type = ntohs(l->llc_un.type_snap.ether_type);
399
400         switch (ether_type) {
401 #ifdef INET
402         case ETHERTYPE_IP:
403                 th->iso88025_shost[0] &= ~(TR_RII); 
404                 if (ipflow_fastforward(m))
405                         return;
406                 schednetisr(NETISR_IP);
407                 inq = &ipintrq;
408                 break;
409
410         case ETHERTYPE_ARP:
411                 if (ifp->if_flags & IFF_NOARP) {
412                         m_freem(m);
413                         return;
414                 }
415                 schednetisr(NETISR_ARP);
416                 inq = &arpintrq;
417                 break;
418 #endif
419         default:
420             m_freem(m);
421             return;
422         }
423
424         s = splimp();
425         if (IF_QFULL(inq)) {
426                 IF_DROP(inq);
427                 m_freem(m);
428                 printf("iso88025_input: Packet dropped (Queue full).\n");
429         } else
430                 IF_ENQUEUE(inq, m);
431         splx(s);
432 }