if_gif.c revision 1.68 1 1.68 dyoung /* $NetBSD: if_gif.c,v 1.68 2007/03/17 06:36:05 dyoung Exp $ */
2 1.34 itojun /* $KAME: if_gif.c,v 1.76 2001/08/20 02:01:02 kjc Exp $ */
3 1.3 itojun
4 1.2 itojun /*
5 1.2 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.2 itojun * All rights reserved.
7 1.9 itojun *
8 1.2 itojun * Redistribution and use in source and binary forms, with or without
9 1.2 itojun * modification, are permitted provided that the following conditions
10 1.2 itojun * are met:
11 1.2 itojun * 1. Redistributions of source code must retain the above copyright
12 1.2 itojun * notice, this list of conditions and the following disclaimer.
13 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.2 itojun * notice, this list of conditions and the following disclaimer in the
15 1.2 itojun * documentation and/or other materials provided with the distribution.
16 1.2 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.2 itojun * may be used to endorse or promote products derived from this software
18 1.2 itojun * without specific prior written permission.
19 1.9 itojun *
20 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.2 itojun * SUCH DAMAGE.
31 1.2 itojun */
32 1.36 lukem
33 1.36 lukem #include <sys/cdefs.h>
34 1.68 dyoung __KERNEL_RCSID(0, "$NetBSD: if_gif.c,v 1.68 2007/03/17 06:36:05 dyoung Exp $");
35 1.2 itojun
36 1.2 itojun #include "opt_inet.h"
37 1.24 itojun #include "opt_iso.h"
38 1.2 itojun
39 1.2 itojun #include <sys/param.h>
40 1.2 itojun #include <sys/systm.h>
41 1.2 itojun #include <sys/kernel.h>
42 1.2 itojun #include <sys/mbuf.h>
43 1.2 itojun #include <sys/socket.h>
44 1.2 itojun #include <sys/sockio.h>
45 1.2 itojun #include <sys/errno.h>
46 1.2 itojun #include <sys/ioctl.h>
47 1.2 itojun #include <sys/time.h>
48 1.2 itojun #include <sys/syslog.h>
49 1.17 martin #include <sys/proc.h>
50 1.9 itojun #include <sys/protosw.h>
51 1.59 elad #include <sys/kauth.h>
52 1.59 elad
53 1.2 itojun #include <machine/cpu.h>
54 1.33 itojun #include <machine/intr.h>
55 1.2 itojun
56 1.2 itojun #include <net/if.h>
57 1.2 itojun #include <net/if_types.h>
58 1.2 itojun #include <net/netisr.h>
59 1.2 itojun #include <net/route.h>
60 1.2 itojun #include <net/bpf.h>
61 1.2 itojun
62 1.2 itojun #include <netinet/in.h>
63 1.2 itojun #include <netinet/in_systm.h>
64 1.15 itojun #include <netinet/ip.h>
65 1.15 itojun #ifdef INET
66 1.2 itojun #include <netinet/in_var.h>
67 1.57 christos #endif /* INET */
68 1.2 itojun #include <netinet/in_gif.h>
69 1.2 itojun
70 1.2 itojun #ifdef INET6
71 1.2 itojun #ifndef INET
72 1.2 itojun #include <netinet/in.h>
73 1.2 itojun #endif
74 1.2 itojun #include <netinet6/in6_var.h>
75 1.2 itojun #include <netinet/ip6.h>
76 1.2 itojun #include <netinet6/ip6_var.h>
77 1.2 itojun #include <netinet6/in6_gif.h>
78 1.9 itojun #include <netinet6/ip6protosw.h>
79 1.2 itojun #endif /* INET6 */
80 1.2 itojun
81 1.24 itojun #ifdef ISO
82 1.24 itojun #include <netiso/iso.h>
83 1.24 itojun #include <netiso/iso_var.h>
84 1.24 itojun #endif
85 1.24 itojun
86 1.9 itojun #include <netinet/ip_encap.h>
87 1.2 itojun #include <net/if_gif.h>
88 1.2 itojun
89 1.2 itojun #include "bpfilter.h"
90 1.2 itojun
91 1.4 itojun #include <net/net_osdep.h>
92 1.4 itojun
93 1.56 thorpej void gifattach(int);
94 1.33 itojun #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
95 1.56 thorpej static void gifnetisr(void);
96 1.33 itojun #endif
97 1.56 thorpej static void gifintr(void *);
98 1.24 itojun #ifdef ISO
99 1.56 thorpej static struct mbuf *gif_eon_encap(struct mbuf *);
100 1.56 thorpej static struct mbuf *gif_eon_decap(struct ifnet *, struct mbuf *);
101 1.24 itojun #endif
102 1.2 itojun
103 1.2 itojun /*
104 1.2 itojun * gif global variable definitions
105 1.2 itojun */
106 1.56 thorpej LIST_HEAD(, gif_softc) gif_softc_list; /* XXX should be static */
107 1.12 thorpej
108 1.56 thorpej static int gif_clone_create(struct if_clone *, int);
109 1.56 thorpej static int gif_clone_destroy(struct ifnet *);
110 1.12 thorpej
111 1.56 thorpej static struct if_clone gif_cloner =
112 1.12 thorpej IF_CLONE_INITIALIZER("gif", gif_clone_create, gif_clone_destroy);
113 1.12 thorpej
114 1.9 itojun #ifndef MAX_GIF_NEST
115 1.9 itojun /*
116 1.9 itojun * This macro controls the upper limitation on nesting of gif tunnels.
117 1.9 itojun * Since, setting a large value to this macro with a careless configuration
118 1.9 itojun * may introduce system crash, we don't allow any nestings by default.
119 1.9 itojun * If you need to configure nested gif tunnels, you can define this macro
120 1.31 itojun * in your kernel configuration file. However, if you do so, please be
121 1.9 itojun * careful to configure the tunnels so that it won't make a loop.
122 1.9 itojun */
123 1.9 itojun #define MAX_GIF_NEST 1
124 1.9 itojun #endif
125 1.9 itojun static int max_gif_nesting = MAX_GIF_NEST;
126 1.2 itojun
127 1.12 thorpej /* ARGSUSED */
128 1.2 itojun void
129 1.63 christos gifattach(int count)
130 1.12 thorpej {
131 1.12 thorpej
132 1.12 thorpej LIST_INIT(&gif_softc_list);
133 1.12 thorpej if_clone_attach(&gif_cloner);
134 1.12 thorpej }
135 1.12 thorpej
136 1.56 thorpej static int
137 1.56 thorpej gif_clone_create(struct if_clone *ifc, int unit)
138 1.2 itojun {
139 1.12 thorpej struct gif_softc *sc;
140 1.12 thorpej
141 1.12 thorpej sc = malloc(sizeof(struct gif_softc), M_DEVBUF, M_WAIT);
142 1.30 thorpej memset(sc, 0, sizeof(struct gif_softc));
143 1.2 itojun
144 1.45 itojun snprintf(sc->gif_if.if_xname, sizeof(sc->gif_if.if_xname), "%s%d",
145 1.45 itojun ifc->ifc_name, unit);
146 1.9 itojun
147 1.31 itojun gifattach0(sc);
148 1.31 itojun
149 1.31 itojun LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
150 1.31 itojun return (0);
151 1.31 itojun }
152 1.31 itojun
153 1.31 itojun void
154 1.56 thorpej gifattach0(struct gif_softc *sc)
155 1.31 itojun {
156 1.31 itojun
157 1.12 thorpej sc->encap_cookie4 = sc->encap_cookie6 = NULL;
158 1.9 itojun
159 1.31 itojun sc->gif_if.if_addrlen = 0;
160 1.12 thorpej sc->gif_if.if_mtu = GIF_MTU;
161 1.12 thorpej sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
162 1.12 thorpej sc->gif_if.if_ioctl = gif_ioctl;
163 1.12 thorpej sc->gif_if.if_output = gif_output;
164 1.12 thorpej sc->gif_if.if_type = IFT_GIF;
165 1.19 thorpej sc->gif_if.if_dlt = DLT_NULL;
166 1.34 itojun IFQ_SET_READY(&sc->gif_if.if_snd);
167 1.12 thorpej if_attach(&sc->gif_if);
168 1.20 thorpej if_alloc_sadl(&sc->gif_if);
169 1.2 itojun #if NBPFILTER > 0
170 1.12 thorpej bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
171 1.9 itojun #endif
172 1.12 thorpej }
173 1.12 thorpej
174 1.56 thorpej static int
175 1.56 thorpej gif_clone_destroy(struct ifnet *ifp)
176 1.12 thorpej {
177 1.12 thorpej struct gif_softc *sc = (void *) ifp;
178 1.12 thorpej
179 1.31 itojun gif_delete_tunnel(&sc->gif_if);
180 1.12 thorpej LIST_REMOVE(sc, gif_list);
181 1.12 thorpej #ifdef INET6
182 1.12 thorpej encap_detach(sc->encap_cookie6);
183 1.12 thorpej #endif
184 1.12 thorpej #ifdef INET
185 1.12 thorpej encap_detach(sc->encap_cookie4);
186 1.12 thorpej #endif
187 1.12 thorpej
188 1.12 thorpej #if NBPFILTER > 0
189 1.12 thorpej bpfdetach(ifp);
190 1.12 thorpej #endif
191 1.66 dyoung rtcache_free(&sc->gif_ro);
192 1.12 thorpej if_detach(ifp);
193 1.12 thorpej
194 1.12 thorpej free(sc, M_DEVBUF);
195 1.47 peter
196 1.47 peter return (0);
197 1.9 itojun }
198 1.9 itojun
199 1.42 itojun #ifdef GIF_ENCAPCHECK
200 1.31 itojun int
201 1.56 thorpej gif_encapcheck(struct mbuf *m, int off, int proto, void *arg)
202 1.9 itojun {
203 1.9 itojun struct ip ip;
204 1.9 itojun struct gif_softc *sc;
205 1.9 itojun
206 1.9 itojun sc = (struct gif_softc *)arg;
207 1.9 itojun if (sc == NULL)
208 1.9 itojun return 0;
209 1.9 itojun
210 1.9 itojun if ((sc->gif_if.if_flags & IFF_UP) == 0)
211 1.9 itojun return 0;
212 1.9 itojun
213 1.9 itojun /* no physical address */
214 1.9 itojun if (!sc->gif_psrc || !sc->gif_pdst)
215 1.9 itojun return 0;
216 1.9 itojun
217 1.9 itojun switch (proto) {
218 1.9 itojun #ifdef INET
219 1.9 itojun case IPPROTO_IPV4:
220 1.9 itojun break;
221 1.9 itojun #endif
222 1.9 itojun #ifdef INET6
223 1.9 itojun case IPPROTO_IPV6:
224 1.9 itojun break;
225 1.9 itojun #endif
226 1.24 itojun #ifdef ISO
227 1.24 itojun case IPPROTO_EON:
228 1.24 itojun break;
229 1.24 itojun #endif
230 1.9 itojun default:
231 1.9 itojun return 0;
232 1.9 itojun }
233 1.40 christos
234 1.40 christos /* Bail on short packets */
235 1.40 christos KASSERT(m->m_flags & M_PKTHDR);
236 1.40 christos if (m->m_pkthdr.len < sizeof(ip))
237 1.40 christos return 0;
238 1.9 itojun
239 1.67 christos m_copydata(m, 0, sizeof(ip), (void *)&ip);
240 1.9 itojun
241 1.9 itojun switch (ip.ip_v) {
242 1.9 itojun #ifdef INET
243 1.9 itojun case 4:
244 1.9 itojun if (sc->gif_psrc->sa_family != AF_INET ||
245 1.9 itojun sc->gif_pdst->sa_family != AF_INET)
246 1.9 itojun return 0;
247 1.9 itojun return gif_encapcheck4(m, off, proto, arg);
248 1.9 itojun #endif
249 1.9 itojun #ifdef INET6
250 1.9 itojun case 6:
251 1.41 itojun if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
252 1.41 itojun return 0;
253 1.9 itojun if (sc->gif_psrc->sa_family != AF_INET6 ||
254 1.9 itojun sc->gif_pdst->sa_family != AF_INET6)
255 1.9 itojun return 0;
256 1.9 itojun return gif_encapcheck6(m, off, proto, arg);
257 1.9 itojun #endif
258 1.9 itojun default:
259 1.9 itojun return 0;
260 1.2 itojun }
261 1.2 itojun }
262 1.42 itojun #endif
263 1.2 itojun
264 1.2 itojun int
265 1.65 dyoung gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
266 1.63 christos struct rtentry *rt)
267 1.2 itojun {
268 1.21 itojun struct gif_softc *sc = (struct gif_softc*)ifp;
269 1.2 itojun int error = 0;
270 1.2 itojun static int called = 0; /* XXX: MUTEX */
271 1.39 itojun ALTQ_DECL(struct altq_pktattr pktattr;)
272 1.33 itojun int s;
273 1.33 itojun
274 1.33 itojun IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
275 1.2 itojun
276 1.2 itojun /*
277 1.2 itojun * gif may cause infinite recursion calls when misconfigured.
278 1.2 itojun * We'll prevent this by introducing upper limit.
279 1.2 itojun * XXX: this mechanism may introduce another problem about
280 1.2 itojun * mutual exclusion of the variable CALLED, especially if we
281 1.2 itojun * use kernel thread.
282 1.2 itojun */
283 1.9 itojun if (++called > max_gif_nesting) {
284 1.2 itojun log(LOG_NOTICE,
285 1.2 itojun "gif_output: recursively called too many times(%d)\n",
286 1.2 itojun called);
287 1.2 itojun m_freem(m);
288 1.2 itojun error = EIO; /* is there better errno? */
289 1.2 itojun goto end;
290 1.2 itojun }
291 1.2 itojun
292 1.2 itojun m->m_flags &= ~(M_BCAST|M_MCAST);
293 1.2 itojun if (!(ifp->if_flags & IFF_UP) ||
294 1.2 itojun sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
295 1.2 itojun m_freem(m);
296 1.2 itojun error = ENETDOWN;
297 1.2 itojun goto end;
298 1.2 itojun }
299 1.2 itojun
300 1.24 itojun /* inner AF-specific encapsulation */
301 1.24 itojun switch (dst->sa_family) {
302 1.24 itojun #ifdef ISO
303 1.24 itojun case AF_ISO:
304 1.31 itojun m = gif_eon_encap(m);
305 1.31 itojun if (!m) {
306 1.31 itojun error = ENOBUFS;
307 1.24 itojun goto end;
308 1.31 itojun }
309 1.31 itojun break;
310 1.24 itojun #endif
311 1.24 itojun default:
312 1.24 itojun break;
313 1.24 itojun }
314 1.24 itojun
315 1.9 itojun /* XXX should we check if our outer source is legal? */
316 1.2 itojun
317 1.33 itojun /* use DLT_NULL encapsulation here to pass inner af type */
318 1.33 itojun M_PREPEND(m, sizeof(int), M_DONTWAIT);
319 1.33 itojun if (!m) {
320 1.33 itojun error = ENOBUFS;
321 1.33 itojun goto end;
322 1.33 itojun }
323 1.33 itojun *mtod(m, int *) = dst->sa_family;
324 1.33 itojun
325 1.33 itojun s = splnet();
326 1.33 itojun IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
327 1.33 itojun if (error) {
328 1.33 itojun splx(s);
329 1.31 itojun goto end;
330 1.2 itojun }
331 1.33 itojun splx(s);
332 1.33 itojun
333 1.33 itojun #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
334 1.33 itojun softintr_schedule(sc->gif_si);
335 1.33 itojun #else
336 1.33 itojun /* XXX bad spl level? */
337 1.33 itojun gifnetisr();
338 1.33 itojun #endif
339 1.33 itojun error = 0;
340 1.2 itojun
341 1.2 itojun end:
342 1.2 itojun called = 0; /* reset recursion counter */
343 1.31 itojun if (error)
344 1.31 itojun ifp->if_oerrors++;
345 1.2 itojun return error;
346 1.2 itojun }
347 1.2 itojun
348 1.33 itojun #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
349 1.56 thorpej static void
350 1.56 thorpej gifnetisr(void)
351 1.33 itojun {
352 1.33 itojun struct gif_softc *sc;
353 1.33 itojun
354 1.68 dyoung LIST_FOREACH(sc, &gif_softc_list, gif_list)
355 1.33 itojun gifintr(sc);
356 1.33 itojun }
357 1.33 itojun #endif
358 1.33 itojun
359 1.56 thorpej static void
360 1.56 thorpej gifintr(void *arg)
361 1.33 itojun {
362 1.33 itojun struct gif_softc *sc;
363 1.33 itojun struct ifnet *ifp;
364 1.33 itojun struct mbuf *m;
365 1.33 itojun int family;
366 1.33 itojun int len;
367 1.33 itojun int s;
368 1.33 itojun int error;
369 1.33 itojun
370 1.33 itojun sc = (struct gif_softc *)arg;
371 1.33 itojun ifp = &sc->gif_if;
372 1.33 itojun
373 1.33 itojun /* output processing */
374 1.33 itojun while (1) {
375 1.33 itojun s = splnet();
376 1.34 itojun IFQ_DEQUEUE(&sc->gif_if.if_snd, m);
377 1.33 itojun splx(s);
378 1.33 itojun if (m == NULL)
379 1.33 itojun break;
380 1.33 itojun
381 1.33 itojun /* grab and chop off inner af type */
382 1.33 itojun if (sizeof(int) > m->m_len) {
383 1.33 itojun m = m_pullup(m, sizeof(int));
384 1.33 itojun if (!m) {
385 1.33 itojun ifp->if_oerrors++;
386 1.33 itojun continue;
387 1.33 itojun }
388 1.33 itojun }
389 1.33 itojun family = *mtod(m, int *);
390 1.33 itojun #if NBPFILTER > 0
391 1.46 christos if (ifp->if_bpf)
392 1.33 itojun bpf_mtap(ifp->if_bpf, m);
393 1.33 itojun #endif
394 1.33 itojun m_adj(m, sizeof(int));
395 1.33 itojun
396 1.33 itojun len = m->m_pkthdr.len;
397 1.33 itojun
398 1.33 itojun /* dispatch to output logic based on outer AF */
399 1.33 itojun switch (sc->gif_psrc->sa_family) {
400 1.33 itojun #ifdef INET
401 1.33 itojun case AF_INET:
402 1.33 itojun error = in_gif_output(ifp, family, m);
403 1.33 itojun break;
404 1.33 itojun #endif
405 1.33 itojun #ifdef INET6
406 1.33 itojun case AF_INET6:
407 1.33 itojun error = in6_gif_output(ifp, family, m);
408 1.33 itojun break;
409 1.33 itojun #endif
410 1.33 itojun default:
411 1.50 perry m_freem(m);
412 1.33 itojun error = ENETDOWN;
413 1.33 itojun break;
414 1.33 itojun }
415 1.33 itojun
416 1.33 itojun if (error)
417 1.33 itojun ifp->if_oerrors++;
418 1.33 itojun else {
419 1.50 perry ifp->if_opackets++;
420 1.33 itojun ifp->if_obytes += len;
421 1.33 itojun }
422 1.33 itojun }
423 1.33 itojun }
424 1.33 itojun
425 1.33 itojun void
426 1.56 thorpej gif_input(struct mbuf *m, int af, struct ifnet *ifp)
427 1.2 itojun {
428 1.2 itojun int s, isr;
429 1.33 itojun struct ifqueue *ifq = NULL;
430 1.2 itojun
431 1.33 itojun if (ifp == NULL) {
432 1.2 itojun /* just in case */
433 1.2 itojun m_freem(m);
434 1.2 itojun return;
435 1.2 itojun }
436 1.2 itojun
437 1.33 itojun m->m_pkthdr.rcvif = ifp;
438 1.50 perry
439 1.2 itojun #if NBPFILTER > 0
440 1.46 christos if (ifp->if_bpf)
441 1.46 christos bpf_mtap_af(ifp->if_bpf, af, m);
442 1.2 itojun #endif /*NBPFILTER > 0*/
443 1.2 itojun
444 1.2 itojun /*
445 1.2 itojun * Put the packet to the network layer input queue according to the
446 1.2 itojun * specified address family.
447 1.2 itojun * Note: older versions of gif_input directly called network layer
448 1.31 itojun * input functions, e.g. ip6_input, here. We changed the policy to
449 1.2 itojun * prevent too many recursive calls of such input functions, which
450 1.31 itojun * might cause kernel panic. But the change may introduce another
451 1.2 itojun * problem; if the input queue is full, packets are discarded.
452 1.31 itojun * The kernel stack overflow really happened, and we believed
453 1.31 itojun * queue-full rarely occurs, so we changed the policy.
454 1.2 itojun */
455 1.2 itojun switch (af) {
456 1.2 itojun #ifdef INET
457 1.2 itojun case AF_INET:
458 1.2 itojun ifq = &ipintrq;
459 1.2 itojun isr = NETISR_IP;
460 1.2 itojun break;
461 1.2 itojun #endif
462 1.2 itojun #ifdef INET6
463 1.2 itojun case AF_INET6:
464 1.2 itojun ifq = &ip6intrq;
465 1.2 itojun isr = NETISR_IPV6;
466 1.2 itojun break;
467 1.2 itojun #endif
468 1.24 itojun #ifdef ISO
469 1.24 itojun case AF_ISO:
470 1.33 itojun m = gif_eon_decap(ifp, m);
471 1.31 itojun if (!m)
472 1.24 itojun return;
473 1.24 itojun ifq = &clnlintrq;
474 1.24 itojun isr = NETISR_ISO;
475 1.24 itojun break;
476 1.24 itojun #endif
477 1.2 itojun default:
478 1.2 itojun m_freem(m);
479 1.2 itojun return;
480 1.2 itojun }
481 1.2 itojun
482 1.27 thorpej s = splnet();
483 1.2 itojun if (IF_QFULL(ifq)) {
484 1.2 itojun IF_DROP(ifq); /* update statistics */
485 1.2 itojun m_freem(m);
486 1.2 itojun splx(s);
487 1.2 itojun return;
488 1.2 itojun }
489 1.33 itojun ifp->if_ipackets++;
490 1.33 itojun ifp->if_ibytes += m->m_pkthdr.len;
491 1.2 itojun IF_ENQUEUE(ifq, m);
492 1.2 itojun /* we need schednetisr since the address family may change */
493 1.2 itojun schednetisr(isr);
494 1.2 itojun splx(s);
495 1.2 itojun }
496 1.2 itojun
497 1.9 itojun /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
498 1.2 itojun int
499 1.67 christos gif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
500 1.2 itojun {
501 1.60 ad struct lwp *l = curlwp; /* XXX */
502 1.2 itojun struct gif_softc *sc = (struct gif_softc*)ifp;
503 1.2 itojun struct ifreq *ifr = (struct ifreq*)data;
504 1.2 itojun int error = 0, size;
505 1.9 itojun struct sockaddr *dst, *src;
506 1.41 itojun #ifdef SIOCSIFMTU
507 1.41 itojun u_long mtu;
508 1.41 itojun #endif
509 1.31 itojun
510 1.2 itojun switch (cmd) {
511 1.60 ad case SIOCSIFMTU:
512 1.60 ad case SIOCSLIFPHYADDR:
513 1.60 ad #ifdef SIOCDIFPHYADDR
514 1.60 ad case SIOCDIFPHYADDR:
515 1.60 ad #endif
516 1.62 elad if ((error = kauth_authorize_network(l->l_cred,
517 1.62 elad KAUTH_NETWORK_INTERFACE,
518 1.62 elad KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
519 1.62 elad NULL)) != 0)
520 1.60 ad return (error);
521 1.60 ad /* FALLTHROUGH */
522 1.60 ad default:
523 1.60 ad break;
524 1.60 ad }
525 1.60 ad
526 1.60 ad switch (cmd) {
527 1.2 itojun case SIOCSIFADDR:
528 1.32 itojun ifp->if_flags |= IFF_UP;
529 1.2 itojun break;
530 1.50 perry
531 1.2 itojun case SIOCSIFDSTADDR:
532 1.2 itojun break;
533 1.2 itojun
534 1.2 itojun case SIOCADDMULTI:
535 1.2 itojun case SIOCDELMULTI:
536 1.2 itojun switch (ifr->ifr_addr.sa_family) {
537 1.2 itojun #ifdef INET
538 1.2 itojun case AF_INET: /* IP supports Multicast */
539 1.2 itojun break;
540 1.2 itojun #endif /* INET */
541 1.2 itojun #ifdef INET6
542 1.2 itojun case AF_INET6: /* IP6 supports Multicast */
543 1.2 itojun break;
544 1.2 itojun #endif /* INET6 */
545 1.2 itojun default: /* Other protocols doesn't support Multicast */
546 1.2 itojun error = EAFNOSUPPORT;
547 1.2 itojun break;
548 1.2 itojun }
549 1.2 itojun break;
550 1.2 itojun
551 1.2 itojun #ifdef SIOCSIFMTU /* xxx */
552 1.2 itojun case SIOCGIFMTU:
553 1.2 itojun break;
554 1.11 itojun
555 1.2 itojun case SIOCSIFMTU:
556 1.41 itojun mtu = ifr->ifr_mtu;
557 1.41 itojun if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
558 1.41 itojun return (EINVAL);
559 1.41 itojun ifp->if_mtu = mtu;
560 1.2 itojun break;
561 1.2 itojun #endif /* SIOCSIFMTU */
562 1.2 itojun
563 1.31 itojun #ifdef INET
564 1.2 itojun case SIOCSIFPHYADDR:
565 1.31 itojun #endif
566 1.2 itojun #ifdef INET6
567 1.2 itojun case SIOCSIFPHYADDR_IN6:
568 1.2 itojun #endif /* INET6 */
569 1.25 itojun case SIOCSLIFPHYADDR:
570 1.11 itojun switch (cmd) {
571 1.15 itojun #ifdef INET
572 1.11 itojun case SIOCSIFPHYADDR:
573 1.11 itojun src = (struct sockaddr *)
574 1.11 itojun &(((struct in_aliasreq *)data)->ifra_addr);
575 1.11 itojun dst = (struct sockaddr *)
576 1.11 itojun &(((struct in_aliasreq *)data)->ifra_dstaddr);
577 1.11 itojun break;
578 1.15 itojun #endif
579 1.11 itojun #ifdef INET6
580 1.11 itojun case SIOCSIFPHYADDR_IN6:
581 1.11 itojun src = (struct sockaddr *)
582 1.11 itojun &(((struct in6_aliasreq *)data)->ifra_addr);
583 1.11 itojun dst = (struct sockaddr *)
584 1.11 itojun &(((struct in6_aliasreq *)data)->ifra_dstaddr);
585 1.25 itojun break;
586 1.25 itojun #endif
587 1.25 itojun case SIOCSLIFPHYADDR:
588 1.25 itojun src = (struct sockaddr *)
589 1.25 itojun &(((struct if_laddrreq *)data)->addr);
590 1.25 itojun dst = (struct sockaddr *)
591 1.25 itojun &(((struct if_laddrreq *)data)->dstaddr);
592 1.31 itojun break;
593 1.31 itojun default:
594 1.31 itojun return EINVAL;
595 1.25 itojun }
596 1.25 itojun
597 1.25 itojun /* sa_family must be equal */
598 1.25 itojun if (src->sa_family != dst->sa_family)
599 1.25 itojun return EINVAL;
600 1.25 itojun
601 1.25 itojun /* validate sa_len */
602 1.25 itojun switch (src->sa_family) {
603 1.25 itojun #ifdef INET
604 1.25 itojun case AF_INET:
605 1.25 itojun if (src->sa_len != sizeof(struct sockaddr_in))
606 1.16 itojun return EINVAL;
607 1.11 itojun break;
608 1.11 itojun #endif
609 1.25 itojun #ifdef INET6
610 1.25 itojun case AF_INET6:
611 1.25 itojun if (src->sa_len != sizeof(struct sockaddr_in6))
612 1.25 itojun return EINVAL;
613 1.25 itojun break;
614 1.25 itojun #endif
615 1.25 itojun default:
616 1.25 itojun return EAFNOSUPPORT;
617 1.25 itojun }
618 1.25 itojun switch (dst->sa_family) {
619 1.25 itojun #ifdef INET
620 1.25 itojun case AF_INET:
621 1.25 itojun if (dst->sa_len != sizeof(struct sockaddr_in))
622 1.25 itojun return EINVAL;
623 1.25 itojun break;
624 1.25 itojun #endif
625 1.25 itojun #ifdef INET6
626 1.25 itojun case AF_INET6:
627 1.25 itojun if (dst->sa_len != sizeof(struct sockaddr_in6))
628 1.25 itojun return EINVAL;
629 1.25 itojun break;
630 1.25 itojun #endif
631 1.25 itojun default:
632 1.25 itojun return EAFNOSUPPORT;
633 1.25 itojun }
634 1.25 itojun
635 1.25 itojun /* check sa_family looks sane for the cmd */
636 1.25 itojun switch (cmd) {
637 1.25 itojun case SIOCSIFPHYADDR:
638 1.25 itojun if (src->sa_family == AF_INET)
639 1.25 itojun break;
640 1.25 itojun return EAFNOSUPPORT;
641 1.25 itojun #ifdef INET6
642 1.25 itojun case SIOCSIFPHYADDR_IN6:
643 1.25 itojun if (src->sa_family == AF_INET6)
644 1.25 itojun break;
645 1.25 itojun return EAFNOSUPPORT;
646 1.25 itojun #endif /* INET6 */
647 1.25 itojun case SIOCSLIFPHYADDR:
648 1.25 itojun /* checks done in the above */
649 1.25 itojun break;
650 1.11 itojun }
651 1.11 itojun
652 1.31 itojun error = gif_set_tunnel(&sc->gif_if, src, dst);
653 1.9 itojun break;
654 1.9 itojun
655 1.9 itojun #ifdef SIOCDIFPHYADDR
656 1.9 itojun case SIOCDIFPHYADDR:
657 1.31 itojun gif_delete_tunnel(&sc->gif_if);
658 1.2 itojun break;
659 1.9 itojun #endif
660 1.50 perry
661 1.2 itojun case SIOCGIFPSRCADDR:
662 1.2 itojun #ifdef INET6
663 1.2 itojun case SIOCGIFPSRCADDR_IN6:
664 1.2 itojun #endif /* INET6 */
665 1.2 itojun if (sc->gif_psrc == NULL) {
666 1.2 itojun error = EADDRNOTAVAIL;
667 1.2 itojun goto bad;
668 1.2 itojun }
669 1.2 itojun src = sc->gif_psrc;
670 1.16 itojun switch (cmd) {
671 1.2 itojun #ifdef INET
672 1.16 itojun case SIOCGIFPSRCADDR:
673 1.2 itojun dst = &ifr->ifr_addr;
674 1.16 itojun size = sizeof(ifr->ifr_addr);
675 1.2 itojun break;
676 1.2 itojun #endif /* INET */
677 1.2 itojun #ifdef INET6
678 1.16 itojun case SIOCGIFPSRCADDR_IN6:
679 1.2 itojun dst = (struct sockaddr *)
680 1.2 itojun &(((struct in6_ifreq *)data)->ifr_addr);
681 1.16 itojun size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
682 1.2 itojun break;
683 1.2 itojun #endif /* INET6 */
684 1.2 itojun default:
685 1.2 itojun error = EADDRNOTAVAIL;
686 1.2 itojun goto bad;
687 1.2 itojun }
688 1.16 itojun if (src->sa_len > size)
689 1.16 itojun return EINVAL;
690 1.68 dyoung memcpy(dst, src, src->sa_len);
691 1.2 itojun break;
692 1.50 perry
693 1.2 itojun case SIOCGIFPDSTADDR:
694 1.2 itojun #ifdef INET6
695 1.2 itojun case SIOCGIFPDSTADDR_IN6:
696 1.2 itojun #endif /* INET6 */
697 1.2 itojun if (sc->gif_pdst == NULL) {
698 1.2 itojun error = EADDRNOTAVAIL;
699 1.2 itojun goto bad;
700 1.2 itojun }
701 1.2 itojun src = sc->gif_pdst;
702 1.16 itojun switch (cmd) {
703 1.2 itojun #ifdef INET
704 1.16 itojun case SIOCGIFPDSTADDR:
705 1.2 itojun dst = &ifr->ifr_addr;
706 1.16 itojun size = sizeof(ifr->ifr_addr);
707 1.2 itojun break;
708 1.2 itojun #endif /* INET */
709 1.2 itojun #ifdef INET6
710 1.16 itojun case SIOCGIFPDSTADDR_IN6:
711 1.2 itojun dst = (struct sockaddr *)
712 1.2 itojun &(((struct in6_ifreq *)data)->ifr_addr);
713 1.16 itojun size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
714 1.2 itojun break;
715 1.2 itojun #endif /* INET6 */
716 1.2 itojun default:
717 1.2 itojun error = EADDRNOTAVAIL;
718 1.2 itojun goto bad;
719 1.2 itojun }
720 1.25 itojun if (src->sa_len > size)
721 1.25 itojun return EINVAL;
722 1.68 dyoung memcpy(dst, src, src->sa_len);
723 1.25 itojun break;
724 1.25 itojun
725 1.25 itojun case SIOCGLIFPHYADDR:
726 1.25 itojun if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
727 1.25 itojun error = EADDRNOTAVAIL;
728 1.25 itojun goto bad;
729 1.25 itojun }
730 1.25 itojun
731 1.25 itojun /* copy src */
732 1.25 itojun src = sc->gif_psrc;
733 1.25 itojun dst = (struct sockaddr *)
734 1.25 itojun &(((struct if_laddrreq *)data)->addr);
735 1.25 itojun size = sizeof(((struct if_laddrreq *)data)->addr);
736 1.25 itojun if (src->sa_len > size)
737 1.25 itojun return EINVAL;
738 1.68 dyoung memcpy(dst, src, src->sa_len);
739 1.25 itojun
740 1.25 itojun /* copy dst */
741 1.25 itojun src = sc->gif_pdst;
742 1.25 itojun dst = (struct sockaddr *)
743 1.25 itojun &(((struct if_laddrreq *)data)->dstaddr);
744 1.25 itojun size = sizeof(((struct if_laddrreq *)data)->dstaddr);
745 1.16 itojun if (src->sa_len > size)
746 1.16 itojun return EINVAL;
747 1.68 dyoung memcpy(dst, src, src->sa_len);
748 1.2 itojun break;
749 1.2 itojun
750 1.2 itojun case SIOCSIFFLAGS:
751 1.9 itojun /* if_ioctl() takes care of it */
752 1.2 itojun break;
753 1.2 itojun
754 1.2 itojun default:
755 1.2 itojun error = EINVAL;
756 1.2 itojun break;
757 1.2 itojun }
758 1.2 itojun bad:
759 1.2 itojun return error;
760 1.12 thorpej }
761 1.12 thorpej
762 1.31 itojun int
763 1.56 thorpej gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
764 1.31 itojun {
765 1.31 itojun struct gif_softc *sc = (struct gif_softc *)ifp;
766 1.31 itojun struct gif_softc *sc2;
767 1.31 itojun struct sockaddr *osrc, *odst, *sa;
768 1.31 itojun int s;
769 1.31 itojun int error;
770 1.31 itojun
771 1.31 itojun s = splsoftnet();
772 1.31 itojun
773 1.68 dyoung LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
774 1.31 itojun if (sc2 == sc)
775 1.31 itojun continue;
776 1.31 itojun if (!sc2->gif_pdst || !sc2->gif_psrc)
777 1.31 itojun continue;
778 1.31 itojun if (sc2->gif_pdst->sa_family != dst->sa_family ||
779 1.31 itojun sc2->gif_pdst->sa_len != dst->sa_len ||
780 1.31 itojun sc2->gif_psrc->sa_family != src->sa_family ||
781 1.31 itojun sc2->gif_psrc->sa_len != src->sa_len)
782 1.31 itojun continue;
783 1.31 itojun /* can't configure same pair of address onto two gifs */
784 1.68 dyoung if (memcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
785 1.68 dyoung memcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
786 1.31 itojun error = EADDRNOTAVAIL;
787 1.31 itojun goto bad;
788 1.31 itojun }
789 1.31 itojun
790 1.31 itojun /* XXX both end must be valid? (I mean, not 0.0.0.0) */
791 1.31 itojun }
792 1.31 itojun
793 1.58 msaitoh #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
794 1.58 msaitoh if (sc->gif_si) {
795 1.58 msaitoh softintr_disestablish(sc->gif_si);
796 1.58 msaitoh sc->gif_si = NULL;
797 1.58 msaitoh }
798 1.58 msaitoh #endif
799 1.58 msaitoh
800 1.31 itojun /* XXX we can detach from both, but be polite just in case */
801 1.31 itojun if (sc->gif_psrc)
802 1.31 itojun switch (sc->gif_psrc->sa_family) {
803 1.31 itojun #ifdef INET
804 1.31 itojun case AF_INET:
805 1.31 itojun (void)in_gif_detach(sc);
806 1.31 itojun break;
807 1.31 itojun #endif
808 1.31 itojun #ifdef INET6
809 1.31 itojun case AF_INET6:
810 1.31 itojun (void)in6_gif_detach(sc);
811 1.31 itojun break;
812 1.31 itojun #endif
813 1.31 itojun }
814 1.31 itojun
815 1.33 itojun #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
816 1.33 itojun sc->gif_si = softintr_establish(IPL_SOFTNET, gifintr, sc);
817 1.33 itojun if (sc->gif_si == NULL) {
818 1.33 itojun error = ENOMEM;
819 1.33 itojun goto bad;
820 1.33 itojun }
821 1.33 itojun #endif
822 1.33 itojun
823 1.31 itojun osrc = sc->gif_psrc;
824 1.31 itojun sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
825 1.68 dyoung memcpy(sa, src, src->sa_len);
826 1.31 itojun sc->gif_psrc = sa;
827 1.31 itojun
828 1.31 itojun odst = sc->gif_pdst;
829 1.31 itojun sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
830 1.68 dyoung memcpy(sa, dst, dst->sa_len);
831 1.31 itojun sc->gif_pdst = sa;
832 1.31 itojun
833 1.31 itojun switch (sc->gif_psrc->sa_family) {
834 1.31 itojun #ifdef INET
835 1.31 itojun case AF_INET:
836 1.31 itojun error = in_gif_attach(sc);
837 1.31 itojun break;
838 1.31 itojun #endif
839 1.31 itojun #ifdef INET6
840 1.31 itojun case AF_INET6:
841 1.31 itojun error = in6_gif_attach(sc);
842 1.31 itojun break;
843 1.31 itojun #endif
844 1.43 christos default:
845 1.44 mycroft error = EINVAL;
846 1.43 christos break;
847 1.31 itojun }
848 1.31 itojun if (error) {
849 1.31 itojun /* rollback */
850 1.67 christos free((void *)sc->gif_psrc, M_IFADDR);
851 1.67 christos free((void *)sc->gif_pdst, M_IFADDR);
852 1.31 itojun sc->gif_psrc = osrc;
853 1.31 itojun sc->gif_pdst = odst;
854 1.31 itojun goto bad;
855 1.31 itojun }
856 1.31 itojun
857 1.31 itojun if (osrc)
858 1.67 christos free((void *)osrc, M_IFADDR);
859 1.31 itojun if (odst)
860 1.67 christos free((void *)odst, M_IFADDR);
861 1.31 itojun
862 1.33 itojun if (sc->gif_psrc && sc->gif_pdst)
863 1.33 itojun ifp->if_flags |= IFF_RUNNING;
864 1.33 itojun else
865 1.33 itojun ifp->if_flags &= ~IFF_RUNNING;
866 1.33 itojun splx(s);
867 1.33 itojun
868 1.33 itojun return 0;
869 1.31 itojun
870 1.31 itojun bad:
871 1.33 itojun #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
872 1.35 itojun if (sc->gif_si) {
873 1.33 itojun softintr_disestablish(sc->gif_si);
874 1.35 itojun sc->gif_si = NULL;
875 1.35 itojun }
876 1.33 itojun #endif
877 1.31 itojun if (sc->gif_psrc && sc->gif_pdst)
878 1.31 itojun ifp->if_flags |= IFF_RUNNING;
879 1.31 itojun else
880 1.31 itojun ifp->if_flags &= ~IFF_RUNNING;
881 1.31 itojun splx(s);
882 1.31 itojun
883 1.31 itojun return error;
884 1.31 itojun }
885 1.31 itojun
886 1.12 thorpej void
887 1.56 thorpej gif_delete_tunnel(struct ifnet *ifp)
888 1.12 thorpej {
889 1.31 itojun struct gif_softc *sc = (struct gif_softc *)ifp;
890 1.12 thorpej int s;
891 1.12 thorpej
892 1.12 thorpej s = splsoftnet();
893 1.12 thorpej
894 1.33 itojun #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
895 1.35 itojun if (sc->gif_si) {
896 1.33 itojun softintr_disestablish(sc->gif_si);
897 1.35 itojun sc->gif_si = NULL;
898 1.35 itojun }
899 1.33 itojun #endif
900 1.12 thorpej if (sc->gif_psrc) {
901 1.67 christos free((void *)sc->gif_psrc, M_IFADDR);
902 1.12 thorpej sc->gif_psrc = NULL;
903 1.12 thorpej }
904 1.12 thorpej if (sc->gif_pdst) {
905 1.67 christos free((void *)sc->gif_pdst, M_IFADDR);
906 1.12 thorpej sc->gif_pdst = NULL;
907 1.12 thorpej }
908 1.31 itojun /* it is safe to detach from both */
909 1.31 itojun #ifdef INET
910 1.31 itojun (void)in_gif_detach(sc);
911 1.31 itojun #endif
912 1.31 itojun #ifdef INET6
913 1.31 itojun (void)in6_gif_detach(sc);
914 1.31 itojun #endif
915 1.12 thorpej
916 1.31 itojun if (sc->gif_psrc && sc->gif_pdst)
917 1.31 itojun ifp->if_flags |= IFF_RUNNING;
918 1.31 itojun else
919 1.31 itojun ifp->if_flags &= ~IFF_RUNNING;
920 1.12 thorpej splx(s);
921 1.2 itojun }
922 1.24 itojun
923 1.24 itojun #ifdef ISO
924 1.24 itojun struct eonhdr {
925 1.24 itojun u_int8_t version;
926 1.24 itojun u_int8_t class;
927 1.24 itojun u_int16_t cksum;
928 1.26 itojun };
929 1.24 itojun
930 1.24 itojun /*
931 1.24 itojun * prepend EON header to ISO PDU
932 1.24 itojun */
933 1.31 itojun static struct mbuf *
934 1.31 itojun gif_eon_encap(struct mbuf *m)
935 1.24 itojun {
936 1.24 itojun struct eonhdr *ehdr;
937 1.24 itojun
938 1.31 itojun M_PREPEND(m, sizeof(*ehdr), M_DONTWAIT);
939 1.31 itojun if (m && m->m_len < sizeof(*ehdr))
940 1.31 itojun m = m_pullup(m, sizeof(*ehdr));
941 1.31 itojun if (m == NULL)
942 1.31 itojun return NULL;
943 1.31 itojun ehdr = mtod(m, struct eonhdr *);
944 1.24 itojun ehdr->version = 1;
945 1.24 itojun ehdr->class = 0; /* always unicast */
946 1.24 itojun #if 0
947 1.24 itojun /* calculate the checksum of the eonhdr */
948 1.24 itojun {
949 1.24 itojun struct mbuf mhead;
950 1.24 itojun memset(&mhead, 0, sizeof(mhead));
951 1.24 itojun ehdr->cksum = 0;
952 1.67 christos mhead.m_data = (void *)ehdr;
953 1.24 itojun mhead.m_len = sizeof(*ehdr);
954 1.24 itojun mhead.m_next = 0;
955 1.24 itojun iso_gen_csum(&mhead, offsetof(struct eonhdr, cksum),
956 1.24 itojun mhead.m_len);
957 1.24 itojun }
958 1.24 itojun #else
959 1.24 itojun /* since the data is always constant we'll just plug the value in */
960 1.24 itojun ehdr->cksum = htons(0xfc02);
961 1.24 itojun #endif
962 1.31 itojun return m;
963 1.24 itojun }
964 1.24 itojun
965 1.24 itojun /*
966 1.24 itojun * remove EON header and check checksum
967 1.24 itojun */
968 1.31 itojun static struct mbuf *
969 1.33 itojun gif_eon_decap(struct ifnet *ifp, struct mbuf *m)
970 1.24 itojun {
971 1.24 itojun struct eonhdr *ehdr;
972 1.24 itojun
973 1.31 itojun if (m->m_len < sizeof(*ehdr) &&
974 1.31 itojun (m = m_pullup(m, sizeof(*ehdr))) == NULL) {
975 1.33 itojun ifp->if_ierrors++;
976 1.31 itojun return NULL;
977 1.24 itojun }
978 1.31 itojun if (iso_check_csum(m, sizeof(struct eonhdr))) {
979 1.31 itojun m_freem(m);
980 1.31 itojun return NULL;
981 1.31 itojun }
982 1.31 itojun m_adj(m, sizeof(*ehdr));
983 1.31 itojun return m;
984 1.24 itojun }
985 1.24 itojun #endif /*ISO*/
986