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