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