]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
ff6e181a A |
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. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
1c79356b | 12 | * |
ff6e181a A |
13 | * The Original Code and all software distributed under the License are |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
ff6e181a A |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
1c79356b A |
20 | * |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
9bccf70c A |
23 | /* $FreeBSD: src/sys/net/if_gif.c,v 1.4.2.6 2001/07/24 19:10:18 brooks Exp $ */ |
24 | /* $KAME: if_gif.c,v 1.47 2001/05/01 05:28:42 itojun Exp $ */ | |
1c79356b A |
25 | |
26 | /* | |
27 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
28 | * All rights reserved. | |
29 | * | |
30 | * Redistribution and use in source and binary forms, with or without | |
31 | * modification, are permitted provided that the following conditions | |
32 | * are met: | |
33 | * 1. Redistributions of source code must retain the above copyright | |
34 | * notice, this list of conditions and the following disclaimer. | |
35 | * 2. Redistributions in binary form must reproduce the above copyright | |
36 | * notice, this list of conditions and the following disclaimer in the | |
37 | * documentation and/or other materials provided with the distribution. | |
38 | * 3. Neither the name of the project nor the names of its contributors | |
39 | * may be used to endorse or promote products derived from this software | |
40 | * without specific prior written permission. | |
41 | * | |
42 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
43 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
44 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
45 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
46 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
48 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
50 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
51 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
52 | * SUCH DAMAGE. | |
53 | */ | |
54 | ||
1c79356b A |
55 | #include <sys/param.h> |
56 | #include <sys/systm.h> | |
57 | #include <sys/kernel.h> | |
58 | #include <sys/malloc.h> | |
59 | #include <sys/mbuf.h> | |
60 | #include <sys/socket.h> | |
61 | #include <sys/sockio.h> | |
62 | #include <sys/errno.h> | |
63 | #include <sys/time.h> | |
64 | #include <sys/syslog.h> | |
9bccf70c | 65 | #include <sys/protosw.h> |
1c79356b A |
66 | #include <kern/cpu_number.h> |
67 | ||
68 | #include <net/if.h> | |
69 | #include <net/if_types.h> | |
1c79356b A |
70 | #include <net/route.h> |
71 | #include <net/bpf.h> | |
72 | ||
1c79356b A |
73 | #include <netinet/in.h> |
74 | #include <netinet/in_systm.h> | |
1c79356b | 75 | #include <netinet/ip.h> |
9bccf70c A |
76 | #if INET |
77 | #include <netinet/in_var.h> | |
1c79356b | 78 | #include <netinet/in_gif.h> |
9bccf70c | 79 | #include <netinet/ip_var.h> |
1c79356b A |
80 | #endif /* INET */ |
81 | ||
82 | #if INET6 | |
1c79356b A |
83 | #include <netinet6/in6_var.h> |
84 | #include <netinet/ip6.h> | |
85 | #include <netinet6/ip6_var.h> | |
86 | #include <netinet6/in6_gif.h> | |
87 | #include <netinet6/ip6protosw.h> | |
88 | #endif /* INET6 */ | |
89 | ||
90 | #include <netinet/ip_encap.h> | |
91 | #include <net/dlil.h> | |
92 | #include <net/if_gif.h> | |
93 | ||
1c79356b A |
94 | #include <net/net_osdep.h> |
95 | ||
9bccf70c A |
96 | #define GIFNAME "gif" |
97 | #define GIFDEV "if_gif" | |
98 | #define GIF_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits */ | |
1c79356b | 99 | |
9bccf70c A |
100 | #ifndef __APPLE__ |
101 | static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); | |
102 | #endif | |
103 | ||
104 | TAILQ_HEAD(gifhead, gif_softc) gifs = TAILQ_HEAD_INITIALIZER(gifs); | |
105 | ||
106 | #ifdef __APPLE__ | |
91447636 A |
107 | void gifattach(void); |
108 | int gif_pre_output(struct ifnet *ifp, u_long protocol_family, struct mbuf **m0, | |
109 | const struct sockaddr *dst, caddr_t rt, char *frame, char *address); | |
9bccf70c A |
110 | static void gif_create_dev(void); |
111 | static int gif_encapcheck(const struct mbuf*, int, int, void*); | |
112 | ||
113 | ||
114 | int ngif = 0; /* number of interfaces */ | |
115 | #endif | |
116 | ||
117 | #if INET | |
118 | struct protosw in_gif_protosw = | |
119 | { SOCK_RAW, 0, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC|PR_ADDR, | |
120 | in_gif_input, 0, 0, 0, | |
121 | 0, | |
91447636 | 122 | 0, 0, 0, 0, |
9bccf70c | 123 | 0, |
91447636 A |
124 | &rip_usrreqs, |
125 | 0, rip_unlock, 0 | |
9bccf70c A |
126 | }; |
127 | #endif | |
128 | #if INET6 | |
129 | struct ip6protosw in6_gif_protosw = | |
130 | { SOCK_RAW, 0, 0/*IPPROTO_IPV[46]*/, PR_ATOMIC|PR_ADDR, | |
91447636 | 131 | in6_gif_input, 0, 0, 0, |
9bccf70c A |
132 | 0, |
133 | 0, 0, 0, 0, | |
91447636 A |
134 | 0, |
135 | &rip6_usrreqs, | |
136 | 0, rip_unlock, 0, | |
137 | ||
9bccf70c A |
138 | }; |
139 | #endif | |
1c79356b | 140 | |
1c79356b A |
141 | #ifndef MAX_GIF_NEST |
142 | /* | |
143 | * This macro controls the upper limitation on nesting of gif tunnels. | |
144 | * Since, setting a large value to this macro with a careless configuration | |
145 | * may introduce system crash, we don't allow any nestings by default. | |
146 | * If you need to configure nested gif tunnels, you can define this macro | |
147 | * in your kernel configuration file. However, if you do so, please be | |
148 | * careful to configure the tunnels so that it won't make a loop. | |
149 | */ | |
150 | #define MAX_GIF_NEST 1 | |
151 | #endif | |
152 | static int max_gif_nesting = MAX_GIF_NEST; | |
153 | ||
154 | ||
155 | ||
9bccf70c A |
156 | #ifdef __APPLE__ |
157 | /* | |
158 | * Theory of operation: initially, one gif interface is created. | |
159 | * Any time a gif interface is configured, if there are no other | |
160 | * unconfigured gif interfaces, a new gif interface is created. | |
161 | * BSD uses the clone mechanism to dynamically create more | |
162 | * gif interfaces. | |
163 | * | |
164 | * We have some extra glue to support DLIL. | |
165 | */ | |
166 | ||
167 | /* GIF interface module support */ | |
91447636 A |
168 | int gif_demux( |
169 | struct ifnet *ifp, | |
170 | struct mbuf *m, | |
171 | char *frame_header, | |
172 | u_long *protocol_family) | |
1c79356b | 173 | { |
9bccf70c A |
174 | struct gif_softc* gif = (struct gif_softc*)ifp->if_softc; |
175 | ||
176 | /* Only one protocol may be attached to a gif interface. */ | |
91447636 | 177 | *protocol_family = gif->gif_proto; |
9bccf70c A |
178 | |
179 | return 0; | |
1c79356b A |
180 | } |
181 | ||
1c79356b | 182 | static |
91447636 | 183 | int gif_add_proto(struct ifnet *ifp, u_long protocol_family, struct ddesc_head_str *desc_head) |
9bccf70c A |
184 | { |
185 | /* Only one protocol may be attached at a time */ | |
91447636 | 186 | struct gif_softc* gif = (struct gif_softc*)ifp->if_softc; |
1c79356b | 187 | |
91447636 | 188 | if (gif->gif_proto != 0) |
9bccf70c | 189 | printf("gif_add_proto: request add_proto for gif%d\n", gif->gif_if.if_unit); |
1c79356b | 190 | |
91447636 | 191 | gif->gif_proto = protocol_family; |
1c79356b | 192 | |
9bccf70c | 193 | return 0; |
1c79356b A |
194 | } |
195 | ||
196 | static | |
91447636 | 197 | int gif_del_proto(struct ifnet *ifp, u_long protocol_family) |
9bccf70c | 198 | { |
91447636 A |
199 | if (((struct gif_softc*)ifp)->gif_proto == protocol_family) |
200 | ((struct gif_softc*)ifp)->gif_proto = 0; | |
9bccf70c A |
201 | else |
202 | return ENOENT; | |
1c79356b | 203 | |
9bccf70c | 204 | return 0; |
1c79356b A |
205 | } |
206 | ||
9bccf70c | 207 | /* Glue code to attach inet to a gif interface through DLIL */ |
91447636 A |
208 | int |
209 | gif_attach_proto_family( | |
210 | struct ifnet *ifp, | |
211 | u_long protocol_family) | |
1c79356b A |
212 | { |
213 | struct dlil_proto_reg_str reg; | |
9bccf70c A |
214 | int stat; |
215 | ||
91447636 | 216 | bzero(®, sizeof(reg)); |
1c79356b | 217 | TAILQ_INIT(®.demux_desc_head); |
1c79356b A |
218 | reg.interface_family = ifp->if_family; |
219 | reg.unit_number = ifp->if_unit; | |
220 | reg.input = gif_input; | |
221 | reg.pre_output = gif_pre_output; | |
91447636 | 222 | reg.protocol_family = protocol_family; |
1c79356b | 223 | |
91447636 A |
224 | stat = dlil_attach_protocol(®); |
225 | if (stat && stat != EEXIST) { | |
226 | panic("gif_attach_proto_family can't attach interface fam=%d\n", protocol_family); | |
9bccf70c | 227 | } |
55e303ae | 228 | |
91447636 | 229 | return stat; |
55e303ae A |
230 | } |
231 | ||
9bccf70c A |
232 | #endif |
233 | ||
234 | /* Function to setup the first gif interface */ | |
1c79356b | 235 | void |
9bccf70c | 236 | gifattach(void) |
1c79356b | 237 | { |
55e303ae A |
238 | int error; |
239 | ||
9bccf70c A |
240 | /* Init the list of interfaces */ |
241 | TAILQ_INIT(&gifs); | |
1c79356b | 242 | |
55e303ae | 243 | /* Register protocol registration functions */ |
91447636 | 244 | if ( error = dlil_reg_proto_module(AF_INET, APPLE_IF_FAM_GIF, gif_attach_proto_family, NULL) != 0) |
55e303ae | 245 | printf("dlil_reg_proto_module failed for AF_INET error=%d\n", error); |
55e303ae | 246 | |
91447636 | 247 | if ( error = dlil_reg_proto_module(AF_INET6, APPLE_IF_FAM_GIF, gif_attach_proto_family, NULL) != 0) |
55e303ae A |
248 | printf("dlil_reg_proto_module failed for AF_INET6 error=%d\n", error); |
249 | ||
9bccf70c A |
250 | /* Create first device */ |
251 | gif_create_dev(); | |
252 | } | |
253 | ||
254 | /* Creates another gif device if there are none free */ | |
255 | static void | |
256 | gif_create_dev(void) | |
257 | { | |
258 | struct gif_softc *sc; | |
259 | ||
260 | ||
261 | /* Can't create more than GIF_MAXUNIT */ | |
262 | if (ngif >= GIF_MAXUNIT) | |
263 | return; | |
264 | ||
265 | /* Check for unused gif interface */ | |
266 | TAILQ_FOREACH(sc, &gifs, gif_link) { | |
267 | /* If unused, return, no need to create a new interface */ | |
268 | if ((sc->gif_if.if_flags & IFF_RUNNING) == 0) | |
269 | return; | |
270 | } | |
271 | ||
272 | sc = _MALLOC(sizeof(struct gif_softc), M_DEVBUF, M_WAITOK); | |
273 | if (sc == NULL) { | |
274 | log(LOG_ERR, "gifattach: failed to allocate gif%d\n", ngif); | |
275 | return; | |
276 | } | |
277 | ||
278 | bzero(sc, sizeof(struct gif_softc)); | |
279 | sc->gif_if.if_softc = sc; | |
280 | sc->gif_if.if_name = GIFNAME; | |
281 | sc->gif_if.if_unit = ngif; | |
282 | ||
283 | sc->encap_cookie4 = sc->encap_cookie6 = NULL; | |
284 | #ifdef INET | |
285 | sc->encap_cookie4 = encap_attach_func(AF_INET, -1, | |
286 | gif_encapcheck, &in_gif_protosw, sc); | |
287 | if (sc->encap_cookie4 == NULL) { | |
288 | printf("%s: unable to attach encap4\n", if_name(&sc->gif_if)); | |
289 | FREE(sc, M_DEVBUF); | |
290 | return; | |
291 | } | |
292 | #endif | |
293 | #ifdef INET6 | |
294 | sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, | |
295 | gif_encapcheck, (struct protosw*)&in6_gif_protosw, sc); | |
296 | if (sc->encap_cookie6 == NULL) { | |
297 | if (sc->encap_cookie4) { | |
298 | encap_detach(sc->encap_cookie4); | |
299 | sc->encap_cookie4 = NULL; | |
300 | } | |
301 | printf("%s: unable to attach encap6\n", if_name(&sc->gif_if)); | |
302 | FREE(sc, M_DEVBUF); | |
303 | return; | |
304 | } | |
305 | #endif | |
306 | ||
91447636 | 307 | sc->gif_called = 0; |
9bccf70c A |
308 | sc->gif_if.if_family= APPLE_IF_FAM_GIF; |
309 | sc->gif_if.if_mtu = GIF_MTU; | |
310 | sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST; | |
1c79356b | 311 | #if 0 |
9bccf70c A |
312 | /* turn off ingress filter */ |
313 | sc->gif_if.if_flags |= IFF_LINK2; | |
1c79356b | 314 | #endif |
91447636 | 315 | sc->gif_if.if_demux = gif_demux; |
9bccf70c A |
316 | sc->gif_if.if_ioctl = gif_ioctl; |
317 | sc->gif_if.if_output = NULL; /* pre_output returns error or EJUSTRETURN */ | |
318 | sc->gif_if.if_type = IFT_GIF; | |
91447636 A |
319 | sc->gif_if.if_add_proto = gif_add_proto; |
320 | sc->gif_if.if_del_proto = gif_del_proto; | |
9bccf70c A |
321 | dlil_if_attach(&sc->gif_if); |
322 | bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int)); | |
323 | TAILQ_INSERT_TAIL(&gifs, sc, gif_link); | |
324 | ngif++; | |
325 | } | |
326 | ||
327 | static int | |
328 | gif_encapcheck(m, off, proto, arg) | |
329 | const struct mbuf *m; | |
330 | int off; | |
331 | int proto; | |
332 | void *arg; | |
333 | { | |
334 | struct ip ip; | |
335 | struct gif_softc *sc; | |
336 | ||
337 | sc = (struct gif_softc *)arg; | |
338 | if (sc == NULL) | |
339 | return 0; | |
340 | ||
341 | if ((sc->gif_if.if_flags & IFF_UP) == 0) | |
342 | return 0; | |
343 | ||
344 | /* no physical address */ | |
345 | if (!sc->gif_psrc || !sc->gif_pdst) | |
346 | return 0; | |
347 | ||
348 | switch (proto) { | |
349 | #if INET | |
350 | case IPPROTO_IPV4: | |
351 | break; | |
1c79356b | 352 | #endif |
9bccf70c A |
353 | #if INET6 |
354 | case IPPROTO_IPV6: | |
355 | break; | |
1c79356b | 356 | #endif |
9bccf70c A |
357 | default: |
358 | return 0; | |
1c79356b | 359 | } |
1c79356b | 360 | |
9bccf70c A |
361 | /* LINTED const cast */ |
362 | m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip); | |
363 | ||
364 | switch (ip.ip_v) { | |
365 | #if INET | |
366 | case 4: | |
367 | if (sc->gif_psrc->sa_family != AF_INET || | |
368 | sc->gif_pdst->sa_family != AF_INET) | |
369 | return 0; | |
370 | return gif_encapcheck4(m, off, proto, arg); | |
371 | #endif | |
372 | #if INET6 | |
373 | case 6: | |
374 | if (sc->gif_psrc->sa_family != AF_INET6 || | |
375 | sc->gif_pdst->sa_family != AF_INET6) | |
376 | return 0; | |
377 | return gif_encapcheck6(m, off, proto, arg); | |
1c79356b | 378 | #endif |
9bccf70c A |
379 | default: |
380 | return 0; | |
381 | } | |
382 | } | |
1c79356b A |
383 | |
384 | int | |
91447636 A |
385 | gif_pre_output( |
386 | struct ifnet *ifp, | |
387 | u_long protocol_family, | |
388 | struct mbuf **m0, | |
389 | const struct sockaddr *dst, | |
390 | caddr_t rt, | |
391 | char *frame, | |
392 | char *address) | |
1c79356b | 393 | { |
9bccf70c | 394 | struct gif_softc *sc = (struct gif_softc*)ifp; |
1c79356b A |
395 | register struct mbuf * m = *m0; |
396 | int error = 0; | |
91447636 | 397 | |
1c79356b A |
398 | /* |
399 | * gif may cause infinite recursion calls when misconfigured. | |
400 | * We'll prevent this by introducing upper limit. | |
401 | * XXX: this mechanism may introduce another problem about | |
402 | * mutual exclusion of the variable CALLED, especially if we | |
403 | * use kernel thread. | |
404 | */ | |
91447636 | 405 | if (++sc->gif_called > max_gif_nesting) { |
1c79356b A |
406 | log(LOG_NOTICE, |
407 | "gif_output: recursively called too many times(%d)\n", | |
91447636 | 408 | sc->gif_called); |
55e303ae | 409 | m_freem(m); /* free it here not in dlil_output*/ |
1c79356b A |
410 | error = EIO; /* is there better errno? */ |
411 | goto end; | |
412 | } | |
413 | ||
91447636 | 414 | ifnet_touch_lastchange(ifp); |
1c79356b A |
415 | m->m_flags &= ~(M_BCAST|M_MCAST); |
416 | if (!(ifp->if_flags & IFF_UP) || | |
1c79356b | 417 | sc->gif_psrc == NULL || sc->gif_pdst == NULL) { |
55e303ae | 418 | m_freem(m); /* free it here not in dlil_output */ |
1c79356b | 419 | error = ENETDOWN; |
1c79356b A |
420 | goto end; |
421 | } | |
422 | ||
1c79356b A |
423 | if (ifp->if_bpf) { |
424 | /* | |
425 | * We need to prepend the address family as | |
426 | * a four byte field. Cons up a dummy header | |
427 | * to pacify bpf. This is safe because bpf | |
428 | * will only read from the mbuf (i.e., it won't | |
429 | * try to free it or keep a pointer a to it). | |
430 | */ | |
431 | struct mbuf m0; | |
91447636 | 432 | u_int32_t protocol_family = dst->sa_family; |
1c79356b A |
433 | |
434 | m0.m_next = m; | |
435 | m0.m_len = 4; | |
91447636 | 436 | m0.m_data = (char *)&protocol_family; |
1c79356b | 437 | |
1c79356b | 438 | bpf_mtap(ifp, &m0); |
1c79356b | 439 | } |
1c79356b A |
440 | ifp->if_opackets++; |
441 | ifp->if_obytes += m->m_pkthdr.len; | |
1c79356b | 442 | |
9bccf70c A |
443 | /* inner AF-specific encapsulation */ |
444 | ||
445 | /* XXX should we check if our outer source is legal? */ | |
446 | ||
447 | /* dispatch to output logic based on outer AF */ | |
1c79356b A |
448 | switch (sc->gif_psrc->sa_family) { |
449 | #if INET | |
450 | case AF_INET: | |
9bccf70c | 451 | error = in_gif_output(ifp, dst->sa_family, m, (struct rtentry*)rt); |
1c79356b A |
452 | break; |
453 | #endif | |
454 | #if INET6 | |
455 | case AF_INET6: | |
9bccf70c | 456 | error = in6_gif_output(ifp, dst->sa_family, m, (struct rtentry*)rt); |
1c79356b A |
457 | break; |
458 | #endif | |
459 | default: | |
1c79356b | 460 | error = ENETDOWN; |
9bccf70c | 461 | goto end; |
1c79356b | 462 | } |
1c79356b A |
463 | |
464 | end: | |
91447636 | 465 | sc->gif_called = 0; /* reset recursion counter */ |
55e303ae A |
466 | if (error) { |
467 | /* the mbuf was freed either by in_gif_output or in here */ | |
468 | *m0 = NULL; /* avoid getting dlil_output freeing it */ | |
9bccf70c | 469 | ifp->if_oerrors++; |
55e303ae | 470 | } |
9bccf70c A |
471 | if (error == 0) |
472 | error = EJUSTRETURN; /* if no error, packet got sent already */ | |
473 | return error; | |
1c79356b A |
474 | } |
475 | ||
9bccf70c | 476 | int |
91447636 A |
477 | gif_input( |
478 | struct mbuf *m, | |
479 | char* frame_header, | |
480 | struct ifnet* gifp, | |
481 | u_long protocol_family, | |
482 | int sync_ok) | |
1c79356b | 483 | { |
1c79356b A |
484 | |
485 | if (gifp == NULL) { | |
486 | /* just in case */ | |
487 | m_freem(m); | |
488 | return; | |
489 | } | |
490 | ||
491 | if (m->m_pkthdr.rcvif) | |
492 | m->m_pkthdr.rcvif = gifp; | |
493 | ||
1c79356b A |
494 | if (gifp->if_bpf) { |
495 | /* | |
496 | * We need to prepend the address family as | |
497 | * a four byte field. Cons up a dummy header | |
498 | * to pacify bpf. This is safe because bpf | |
499 | * will only read from the mbuf (i.e., it won't | |
500 | * try to free it or keep a pointer a to it). | |
501 | */ | |
502 | struct mbuf m0; | |
91447636 | 503 | u_int32_t protocol_family1 = protocol_family; |
1c79356b A |
504 | |
505 | m0.m_next = m; | |
506 | m0.m_len = 4; | |
91447636 | 507 | m0.m_data = (char *)&protocol_family1; |
1c79356b | 508 | |
1c79356b | 509 | bpf_mtap(gifp, &m0); |
1c79356b | 510 | } |
1c79356b A |
511 | |
512 | /* | |
513 | * Put the packet to the network layer input queue according to the | |
514 | * specified address family. | |
515 | * Note: older versions of gif_input directly called network layer | |
516 | * input functions, e.g. ip6_input, here. We changed the policy to | |
517 | * prevent too many recursive calls of such input functions, which | |
518 | * might cause kernel panic. But the change may introduce another | |
519 | * problem; if the input queue is full, packets are discarded. | |
520 | * We believed it rarely occurs and changed the policy. If we find | |
521 | * it occurs more times than we thought, we may change the policy | |
522 | * again. | |
523 | */ | |
91447636 | 524 | proto_input(protocol_family, m); |
1c79356b A |
525 | gifp->if_ipackets++; |
526 | gifp->if_ibytes += m->m_pkthdr.len; | |
1c79356b | 527 | |
9bccf70c | 528 | return (0); |
1c79356b A |
529 | } |
530 | ||
531 | /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ | |
532 | int | |
533 | gif_ioctl(ifp, cmd, data) | |
534 | struct ifnet *ifp; | |
535 | u_long cmd; | |
9bccf70c | 536 | void* data; |
1c79356b A |
537 | { |
538 | struct gif_softc *sc = (struct gif_softc*)ifp; | |
539 | struct ifreq *ifr = (struct ifreq*)data; | |
540 | int error = 0, size; | |
541 | struct sockaddr *dst, *src; | |
9bccf70c A |
542 | struct sockaddr *sa; |
543 | int s; | |
544 | struct ifnet *ifp2; | |
1c79356b A |
545 | struct gif_softc *sc2; |
546 | ||
547 | switch (cmd) { | |
548 | case SIOCSIFADDR: | |
549 | break; | |
550 | ||
551 | case SIOCSIFDSTADDR: | |
552 | break; | |
553 | ||
554 | case SIOCADDMULTI: | |
555 | case SIOCDELMULTI: | |
1c79356b A |
556 | break; |
557 | ||
558 | #ifdef SIOCSIFMTU /* xxx */ | |
1c79356b A |
559 | case SIOCGIFMTU: |
560 | break; | |
9bccf70c | 561 | |
1c79356b A |
562 | case SIOCSIFMTU: |
563 | { | |
1c79356b A |
564 | u_long mtu; |
565 | mtu = ifr->ifr_mtu; | |
1c79356b A |
566 | if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) { |
567 | return (EINVAL); | |
568 | } | |
569 | ifp->if_mtu = mtu; | |
570 | } | |
571 | break; | |
1c79356b A |
572 | #endif /* SIOCSIFMTU */ |
573 | ||
574 | case SIOCSIFPHYADDR: | |
575 | #if INET6 | |
576 | case SIOCSIFPHYADDR_IN6: | |
577 | #endif /* INET6 */ | |
9bccf70c A |
578 | case SIOCSLIFPHYADDR: |
579 | switch (cmd) { | |
580 | #if INET | |
581 | case SIOCSIFPHYADDR: | |
582 | src = (struct sockaddr *) | |
583 | &(((struct in_aliasreq *)data)->ifra_addr); | |
584 | dst = (struct sockaddr *) | |
585 | &(((struct in_aliasreq *)data)->ifra_dstaddr); | |
586 | break; | |
587 | #endif | |
588 | #if INET6 | |
589 | case SIOCSIFPHYADDR_IN6: | |
590 | src = (struct sockaddr *) | |
591 | &(((struct in6_aliasreq *)data)->ifra_addr); | |
592 | dst = (struct sockaddr *) | |
593 | &(((struct in6_aliasreq *)data)->ifra_dstaddr); | |
594 | break; | |
595 | #endif | |
596 | case SIOCSLIFPHYADDR: | |
597 | src = (struct sockaddr *) | |
598 | &(((struct if_laddrreq *)data)->addr); | |
599 | dst = (struct sockaddr *) | |
600 | &(((struct if_laddrreq *)data)->dstaddr); | |
601 | } | |
602 | ||
603 | /* sa_family must be equal */ | |
604 | if (src->sa_family != dst->sa_family) | |
605 | return EINVAL; | |
606 | ||
607 | /* validate sa_len */ | |
608 | switch (src->sa_family) { | |
609 | #if INET | |
610 | case AF_INET: | |
611 | if (src->sa_len != sizeof(struct sockaddr_in)) | |
612 | return EINVAL; | |
613 | break; | |
614 | #endif | |
615 | #if INET6 | |
616 | case AF_INET6: | |
617 | if (src->sa_len != sizeof(struct sockaddr_in6)) | |
618 | return EINVAL; | |
619 | break; | |
620 | #endif | |
621 | default: | |
622 | return EAFNOSUPPORT; | |
623 | } | |
624 | switch (dst->sa_family) { | |
625 | #if INET | |
626 | case AF_INET: | |
627 | if (dst->sa_len != sizeof(struct sockaddr_in)) | |
628 | return EINVAL; | |
629 | break; | |
630 | #endif | |
631 | #if INET6 | |
632 | case AF_INET6: | |
633 | if (dst->sa_len != sizeof(struct sockaddr_in6)) | |
634 | return EINVAL; | |
635 | break; | |
636 | #endif | |
637 | default: | |
638 | return EAFNOSUPPORT; | |
639 | } | |
640 | ||
641 | /* check sa_family looks sane for the cmd */ | |
642 | switch (cmd) { | |
643 | case SIOCSIFPHYADDR: | |
644 | if (src->sa_family == AF_INET) | |
645 | break; | |
646 | return EAFNOSUPPORT; | |
647 | #if INET6 | |
648 | case SIOCSIFPHYADDR_IN6: | |
649 | if (src->sa_family == AF_INET6) | |
650 | break; | |
651 | return EAFNOSUPPORT; | |
652 | #endif /* INET6 */ | |
653 | case SIOCSLIFPHYADDR: | |
654 | /* checks done in the above */ | |
655 | break; | |
656 | } | |
657 | ||
91447636 A |
658 | ifnet_head_lock_shared(); |
659 | TAILQ_FOREACH(ifp2, &ifnet_head, if_link) { | |
9bccf70c A |
660 | if (strcmp(ifp2->if_name, GIFNAME) != 0) |
661 | continue; | |
662 | sc2 = ifp2->if_softc; | |
1c79356b A |
663 | if (sc2 == sc) |
664 | continue; | |
665 | if (!sc2->gif_pdst || !sc2->gif_psrc) | |
666 | continue; | |
9bccf70c A |
667 | if (sc2->gif_pdst->sa_family != dst->sa_family || |
668 | sc2->gif_pdst->sa_len != dst->sa_len || | |
669 | sc2->gif_psrc->sa_family != src->sa_family || | |
670 | sc2->gif_psrc->sa_len != src->sa_len) | |
671 | continue; | |
672 | #ifndef XBONEHACK | |
673 | /* can't configure same pair of address onto two gifs */ | |
674 | if (bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && | |
1c79356b A |
675 | bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { |
676 | error = EADDRNOTAVAIL; | |
91447636 | 677 | ifnet_head_done(); |
1c79356b A |
678 | goto bad; |
679 | } | |
9bccf70c | 680 | #endif |
1c79356b | 681 | |
9bccf70c A |
682 | /* can't configure multiple multi-dest interfaces */ |
683 | #define multidest(x) \ | |
684 | (((struct sockaddr_in *)(x))->sin_addr.s_addr == INADDR_ANY) | |
1c79356b | 685 | #if INET6 |
9bccf70c A |
686 | #define multidest6(x) \ |
687 | (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(x))->sin6_addr)) | |
688 | #endif | |
689 | if (dst->sa_family == AF_INET && | |
690 | multidest(dst) && multidest(sc2->gif_pdst)) { | |
691 | error = EADDRNOTAVAIL; | |
91447636 | 692 | ifnet_head_done(); |
9bccf70c A |
693 | goto bad; |
694 | } | |
695 | #if INET6 | |
696 | if (dst->sa_family == AF_INET6 && | |
697 | multidest6(dst) && multidest6(sc2->gif_pdst)) { | |
698 | error = EADDRNOTAVAIL; | |
91447636 | 699 | ifnet_head_done(); |
9bccf70c A |
700 | goto bad; |
701 | } | |
702 | #endif | |
1c79356b | 703 | } |
91447636 | 704 | ifnet_head_done(); |
9bccf70c A |
705 | |
706 | if (sc->gif_psrc) | |
707 | FREE((caddr_t)sc->gif_psrc, M_IFADDR); | |
708 | sa = (struct sockaddr *)_MALLOC(src->sa_len, M_IFADDR, M_WAITOK); | |
709 | bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); | |
710 | sc->gif_psrc = sa; | |
711 | ||
712 | if (sc->gif_pdst) | |
713 | FREE((caddr_t)sc->gif_pdst, M_IFADDR); | |
714 | sa = (struct sockaddr *)_MALLOC(dst->sa_len, M_IFADDR, M_WAITOK); | |
715 | bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); | |
716 | sc->gif_pdst = sa; | |
717 | ||
718 | ifp->if_flags |= IFF_RUNNING; | |
719 | ||
9bccf70c A |
720 | s = splimp(); |
721 | if_up(ifp); /* mark interface UP and send up RTM_IFINFO */ | |
722 | #ifdef __APPLE__ | |
723 | /* Make sure at least one unused device is still available */ | |
724 | gif_create_dev(); | |
725 | #endif | |
726 | splx(s); | |
727 | ||
728 | error = 0; | |
1c79356b | 729 | break; |
9bccf70c A |
730 | |
731 | #ifdef SIOCDIFPHYADDR | |
732 | case SIOCDIFPHYADDR: | |
733 | if (sc->gif_psrc) { | |
734 | FREE((caddr_t)sc->gif_psrc, M_IFADDR); | |
735 | sc->gif_psrc = NULL; | |
736 | } | |
737 | if (sc->gif_pdst) { | |
738 | FREE((caddr_t)sc->gif_pdst, M_IFADDR); | |
739 | sc->gif_pdst = NULL; | |
740 | } | |
741 | /* change the IFF_{UP, RUNNING} flag as well? */ | |
742 | break; | |
743 | #endif | |
1c79356b A |
744 | |
745 | case SIOCGIFPSRCADDR: | |
746 | #if INET6 | |
747 | case SIOCGIFPSRCADDR_IN6: | |
748 | #endif /* INET6 */ | |
749 | if (sc->gif_psrc == NULL) { | |
750 | error = EADDRNOTAVAIL; | |
751 | goto bad; | |
752 | } | |
753 | src = sc->gif_psrc; | |
9bccf70c | 754 | switch (cmd) { |
1c79356b | 755 | #if INET |
9bccf70c | 756 | case SIOCGIFPSRCADDR: |
1c79356b | 757 | dst = &ifr->ifr_addr; |
9bccf70c | 758 | size = sizeof(ifr->ifr_addr); |
1c79356b A |
759 | break; |
760 | #endif /* INET */ | |
761 | #if INET6 | |
9bccf70c | 762 | case SIOCGIFPSRCADDR_IN6: |
1c79356b A |
763 | dst = (struct sockaddr *) |
764 | &(((struct in6_ifreq *)data)->ifr_addr); | |
9bccf70c | 765 | size = sizeof(((struct in6_ifreq *)data)->ifr_addr); |
1c79356b A |
766 | break; |
767 | #endif /* INET6 */ | |
768 | default: | |
769 | error = EADDRNOTAVAIL; | |
770 | goto bad; | |
771 | } | |
9bccf70c A |
772 | if (src->sa_len > size) |
773 | return EINVAL; | |
774 | bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); | |
1c79356b A |
775 | break; |
776 | ||
777 | case SIOCGIFPDSTADDR: | |
778 | #if INET6 | |
779 | case SIOCGIFPDSTADDR_IN6: | |
780 | #endif /* INET6 */ | |
781 | if (sc->gif_pdst == NULL) { | |
782 | error = EADDRNOTAVAIL; | |
783 | goto bad; | |
784 | } | |
785 | src = sc->gif_pdst; | |
9bccf70c | 786 | switch (cmd) { |
1c79356b | 787 | #if INET |
9bccf70c | 788 | case SIOCGIFPDSTADDR: |
1c79356b | 789 | dst = &ifr->ifr_addr; |
9bccf70c | 790 | size = sizeof(ifr->ifr_addr); |
1c79356b A |
791 | break; |
792 | #endif /* INET */ | |
793 | #if INET6 | |
9bccf70c | 794 | case SIOCGIFPDSTADDR_IN6: |
1c79356b A |
795 | dst = (struct sockaddr *) |
796 | &(((struct in6_ifreq *)data)->ifr_addr); | |
9bccf70c | 797 | size = sizeof(((struct in6_ifreq *)data)->ifr_addr); |
1c79356b A |
798 | break; |
799 | #endif /* INET6 */ | |
800 | default: | |
801 | error = EADDRNOTAVAIL; | |
802 | goto bad; | |
803 | } | |
9bccf70c A |
804 | if (src->sa_len > size) |
805 | return EINVAL; | |
806 | bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); | |
1c79356b A |
807 | break; |
808 | ||
9bccf70c A |
809 | case SIOCGLIFPHYADDR: |
810 | if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { | |
811 | error = EADDRNOTAVAIL; | |
1c79356b | 812 | goto bad; |
1c79356b | 813 | } |
9bccf70c A |
814 | |
815 | /* copy src */ | |
816 | src = sc->gif_psrc; | |
817 | dst = (struct sockaddr *) | |
818 | &(((struct if_laddrreq *)data)->addr); | |
819 | size = sizeof(((struct if_laddrreq *)data)->addr); | |
820 | if (src->sa_len > size) | |
821 | return EINVAL; | |
822 | bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); | |
823 | ||
824 | /* copy dst */ | |
825 | src = sc->gif_pdst; | |
826 | dst = (struct sockaddr *) | |
827 | &(((struct if_laddrreq *)data)->dstaddr); | |
828 | size = sizeof(((struct if_laddrreq *)data)->dstaddr); | |
829 | if (src->sa_len > size) | |
830 | return EINVAL; | |
831 | bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); | |
832 | break; | |
833 | ||
834 | case SIOCSIFFLAGS: | |
835 | /* if_ioctl() takes care of it */ | |
1c79356b A |
836 | break; |
837 | ||
838 | default: | |
9bccf70c | 839 | error = EOPNOTSUPP; |
1c79356b A |
840 | break; |
841 | } | |
842 | bad: | |
843 | return error; | |
844 | } | |
9bccf70c | 845 | |
91447636 A |
846 | #ifndef __APPLE__ |
847 | /* This function is not used in our stack */ | |
9bccf70c A |
848 | void |
849 | gif_delete_tunnel(sc) | |
850 | struct gif_softc *sc; | |
851 | { | |
852 | /* XXX: NetBSD protects this function with splsoftnet() */ | |
853 | ||
854 | if (sc->gif_psrc) { | |
855 | FREE((caddr_t)sc->gif_psrc, M_IFADDR); | |
856 | sc->gif_psrc = NULL; | |
857 | } | |
858 | if (sc->gif_pdst) { | |
859 | FREE((caddr_t)sc->gif_pdst, M_IFADDR); | |
860 | sc->gif_pdst = NULL; | |
861 | } | |
862 | /* change the IFF_UP flag as well? */ | |
863 | } | |
91447636 | 864 | #endif |