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