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