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