Initial import from FreeBSD RELENG_4:
[games.git] / sys / netinet / if_atm.c
1 /*      $NetBSD: if_atm.c,v 1.6 1996/10/13 02:03:01 christos Exp $       */
2
3 /*
4  *
5  * Copyright (c) 1996 Charles D. Cranor and Washington University.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Charles D. Cranor and 
19  *      Washington University.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $FreeBSD: src/sys/netinet/if_atm.c,v 1.8.2.1 2001/12/20 10:30:18 ru Exp $
35  */
36
37 /*
38  * IP <=> ATM address resolution.
39  */
40
41 #include "opt_inet.h"
42 #include "opt_inet6.h"
43 #include "opt_natm.h"
44
45 #if defined(INET) || defined(INET6)
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/queue.h>
50 #include <sys/mbuf.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/syslog.h>
54
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/route.h>
58 #include <net/if_atm.h>
59
60 #include <netinet/in.h>
61 #include <netinet/if_atm.h>
62
63 #ifdef NATM
64 #include <netnatm/natm.h>
65 #endif
66
67
68 #define SDL(s) ((struct sockaddr_dl *)s)
69
70 /*
71  * atm_rtrequest: handle ATM rt request (in support of generic code)
72  *   inputs: "req" = request code
73  *           "rt" = route entry
74  *           "info" = rt_addrinfo
75  */
76
77 void
78 atm_rtrequest(req, rt, info)
79         int req;
80         register struct rtentry *rt;
81         struct rt_addrinfo *info;
82 {
83         register struct sockaddr *gate = rt->rt_gateway;
84         struct atm_pseudoioctl api;
85 #ifdef NATM
86         struct sockaddr_in *sin;
87         struct natmpcb *npcb = NULL;
88         struct atm_pseudohdr *aph;
89 #endif
90         static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
91
92         if (rt->rt_flags & RTF_GATEWAY)   /* link level requests only */
93                 return;
94
95         switch (req) {
96
97         case RTM_RESOLVE: /* resolve: only happens when cloning */
98                 printf("atm_rtrequest: RTM_RESOLVE request detected?\n");
99                 break;
100
101         case RTM_ADD:
102
103                 /*
104                  * route added by a command (e.g. ifconfig, route, arp...).
105                  *
106                  * first check to see if this is not a host route, in which
107                  * case we are being called via "ifconfig" to set the address.
108                  */
109
110                 if ((rt->rt_flags & RTF_HOST) == 0) { 
111                         rt_setgate(rt,rt_key(rt),(struct sockaddr *)&null_sdl);
112                         gate = rt->rt_gateway;
113                         SDL(gate)->sdl_type = rt->rt_ifp->if_type;
114                         SDL(gate)->sdl_index = rt->rt_ifp->if_index;
115                         break;
116                 }
117
118                 if ((rt->rt_flags & RTF_CLONING) != 0) {
119                         printf("atm_rtrequest: cloning route detected?\n");
120                         break;
121                 }
122                 if (gate->sa_family != AF_LINK ||
123                     gate->sa_len < sizeof(null_sdl)) {
124                         log(LOG_DEBUG, "atm_rtrequest: bad gateway value");
125                         break;
126                 }
127
128 #ifdef DIAGNOSTIC
129                 if (rt->rt_ifp->if_ioctl == NULL) panic("atm null ioctl");
130 #endif
131
132 #ifdef NATM
133                 /*
134                  * let native ATM know we are using this VCI/VPI
135                  * (i.e. reserve it)
136                  */
137                 sin = (struct sockaddr_in *) rt_key(rt);
138                 if (sin->sin_family != AF_INET)
139                         goto failed;
140                 aph = (struct atm_pseudohdr *) LLADDR(SDL(gate));
141                 npcb = npcb_add(NULL, rt->rt_ifp, ATM_PH_VCI(aph), 
142                                                 ATM_PH_VPI(aph));
143                 if (npcb == NULL) 
144                         goto failed;
145                 npcb->npcb_flags |= NPCB_IP;
146                 npcb->ipaddr.s_addr = sin->sin_addr.s_addr;
147                 /* XXX: move npcb to llinfo when ATM ARP is ready */
148                 rt->rt_llinfo = (caddr_t) npcb;
149                 rt->rt_flags |= RTF_LLINFO;
150 #endif
151                 /*
152                  * let the lower level know this circuit is active
153                  */
154                 bcopy(LLADDR(SDL(gate)), &api.aph, sizeof(api.aph));
155                 api.rxhand = NULL;
156                 if (rt->rt_ifp->if_ioctl(rt->rt_ifp, SIOCATMENA, 
157                                                         (caddr_t)&api) != 0) {
158                         printf("atm: couldn't add VC\n");
159                         goto failed;
160                 }
161
162                 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
163                 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
164
165                 break;
166
167 failed:
168 #ifdef NATM
169                 if (npcb) {
170                         npcb_free(npcb, NPCB_DESTROY);
171                         rt->rt_llinfo = NULL;
172                         rt->rt_flags &= ~RTF_LLINFO;
173                 }
174 #endif
175                 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
176                         rt_mask(rt), 0, (struct rtentry **) 0);
177                 break;
178
179         case RTM_DELETE:
180
181 #ifdef NATM
182                 /*
183                  * tell native ATM we are done with this VC
184                  */
185
186                 if (rt->rt_flags & RTF_LLINFO) {
187                         npcb_free((struct natmpcb *)rt->rt_llinfo, 
188                                                                 NPCB_DESTROY);
189                         rt->rt_llinfo = NULL;
190                         rt->rt_flags &= ~RTF_LLINFO;
191                 }
192 #endif
193                 /*
194                  * tell the lower layer to disable this circuit
195                  */
196
197                 bcopy(LLADDR(SDL(gate)), &api.aph, sizeof(api.aph));
198                 api.rxhand = NULL;
199                 (void)rt->rt_ifp->if_ioctl(rt->rt_ifp, SIOCATMDIS, 
200                                                         (caddr_t)&api);
201
202                 break;
203         }
204 }
205
206 /*
207  * atmresolve:
208  *   inputs:
209  *     [1] "rt" = the link level route to use (or null if need to look one up)
210  *     [2] "m" = mbuf containing the data to be sent
211  *     [3] "dst" = sockaddr_in (IP) address of dest.
212  *   output:
213  *     [4] "desten" = ATM pseudo header which we will fill in VPI/VCI info
214  *   return: 
215  *     0 == resolve FAILED; note that "m" gets m_freem'd in this case
216  *     1 == resolve OK; desten contains result
217  *
218  *   XXX: will need more work if we wish to support ATMARP in the kernel,
219  *   but this is enough for PVCs entered via the "route" command.
220  */
221
222 int
223 atmresolve(rt, m, dst, desten)
224
225 register struct rtentry *rt;
226 struct mbuf *m;
227 register struct sockaddr *dst;
228 register struct atm_pseudohdr *desten;  /* OUT */
229
230 {
231         struct sockaddr_dl *sdl;
232
233         if (m->m_flags & (M_BCAST|M_MCAST)) {
234                 log(LOG_INFO, "atmresolve: BCAST/MCAST packet detected/dumped");
235                 goto bad;
236         }
237
238         if (rt == NULL) {
239                 rt = RTALLOC1(dst, 0);
240                 if (rt == NULL) goto bad; /* failed */
241                 rt->rt_refcnt--;        /* don't keep LL references */
242                 if ((rt->rt_flags & RTF_GATEWAY) != 0 || 
243                         (rt->rt_flags & RTF_LLINFO) == 0 ||
244                         /* XXX: are we using LLINFO? */
245                         rt->rt_gateway->sa_family != AF_LINK) {
246                                 goto bad;
247                 }
248         }
249
250         /*
251          * note that rt_gateway is a sockaddr_dl which contains the 
252          * atm_pseudohdr data structure for this route.   we currently
253          * don't need any rt_llinfo info (but will if we want to support
254          * ATM ARP [c.f. if_ether.c]).
255          */
256
257         sdl = SDL(rt->rt_gateway);
258
259         /*
260          * Check the address family and length is valid, the address
261          * is resolved; otherwise, try to resolve.
262          */
263
264
265         if (sdl->sdl_family == AF_LINK && sdl->sdl_alen == sizeof(*desten)) {
266                 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
267                 return(1);      /* ok, go for it! */
268         }
269
270         /*
271          * we got an entry, but it doesn't have valid link address
272          * info in it (it is prob. the interface route, which has
273          * sdl_alen == 0).    dump packet.  (fall through to "bad").
274          */
275
276 bad:
277         m_freem(m);
278         return(0);
279 }
280 #endif /* INET */