if_gif.c revision 1.96 1 1.96 knakahar /* $NetBSD: if_gif.c,v 1.96 2015/12/09 03:31:28 knakahara 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.96 knakahar __KERNEL_RCSID(0, "$NetBSD: if_gif.c,v 1.96 2015/12/09 03:31:28 knakahara Exp $");
35 1.2 itojun
36 1.88 pooka #ifdef _KERNEL_OPT
37 1.2 itojun #include "opt_inet.h"
38 1.88 pooka #endif
39 1.2 itojun
40 1.2 itojun #include <sys/param.h>
41 1.2 itojun #include <sys/systm.h>
42 1.2 itojun #include <sys/kernel.h>
43 1.2 itojun #include <sys/mbuf.h>
44 1.2 itojun #include <sys/socket.h>
45 1.2 itojun #include <sys/sockio.h>
46 1.2 itojun #include <sys/errno.h>
47 1.2 itojun #include <sys/ioctl.h>
48 1.2 itojun #include <sys/time.h>
49 1.86 martin #include <sys/socketvar.h>
50 1.2 itojun #include <sys/syslog.h>
51 1.17 martin #include <sys/proc.h>
52 1.9 itojun #include <sys/protosw.h>
53 1.72 ad #include <sys/cpu.h>
54 1.72 ad #include <sys/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.9 itojun #include <netinet/ip_encap.h>
82 1.2 itojun #include <net/if_gif.h>
83 1.2 itojun
84 1.87 christos #include <net/net_osdep.h>
85 1.2 itojun
86 1.87 christos #include "ioconf.h"
87 1.4 itojun
88 1.56 thorpej static void gifintr(void *);
89 1.2 itojun
90 1.2 itojun /*
91 1.2 itojun * gif global variable definitions
92 1.2 itojun */
93 1.56 thorpej LIST_HEAD(, gif_softc) gif_softc_list; /* XXX should be static */
94 1.12 thorpej
95 1.56 thorpej static int gif_clone_create(struct if_clone *, int);
96 1.56 thorpej static int gif_clone_destroy(struct ifnet *);
97 1.95 knakahar static int gif_check_nesting(struct ifnet *, struct mbuf *);
98 1.12 thorpej
99 1.56 thorpej static struct if_clone gif_cloner =
100 1.12 thorpej IF_CLONE_INITIALIZER("gif", gif_clone_create, gif_clone_destroy);
101 1.12 thorpej
102 1.9 itojun #ifndef MAX_GIF_NEST
103 1.9 itojun /*
104 1.9 itojun * This macro controls the upper limitation on nesting of gif tunnels.
105 1.9 itojun * Since, setting a large value to this macro with a careless configuration
106 1.9 itojun * may introduce system crash, we don't allow any nestings by default.
107 1.9 itojun * If you need to configure nested gif tunnels, you can define this macro
108 1.31 itojun * in your kernel configuration file. However, if you do so, please be
109 1.9 itojun * careful to configure the tunnels so that it won't make a loop.
110 1.9 itojun */
111 1.9 itojun #define MAX_GIF_NEST 1
112 1.9 itojun #endif
113 1.9 itojun static int max_gif_nesting = MAX_GIF_NEST;
114 1.2 itojun
115 1.12 thorpej /* ARGSUSED */
116 1.2 itojun void
117 1.63 christos gifattach(int count)
118 1.12 thorpej {
119 1.12 thorpej
120 1.12 thorpej LIST_INIT(&gif_softc_list);
121 1.12 thorpej if_clone_attach(&gif_cloner);
122 1.12 thorpej }
123 1.12 thorpej
124 1.56 thorpej static int
125 1.56 thorpej gif_clone_create(struct if_clone *ifc, int unit)
126 1.2 itojun {
127 1.12 thorpej struct gif_softc *sc;
128 1.12 thorpej
129 1.75 christos sc = malloc(sizeof(struct gif_softc), M_DEVBUF, M_WAITOK|M_ZERO);
130 1.2 itojun
131 1.75 christos if_initname(&sc->gif_if, ifc->ifc_name, unit);
132 1.9 itojun
133 1.31 itojun gifattach0(sc);
134 1.31 itojun
135 1.31 itojun LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
136 1.31 itojun return (0);
137 1.31 itojun }
138 1.31 itojun
139 1.31 itojun void
140 1.56 thorpej gifattach0(struct gif_softc *sc)
141 1.31 itojun {
142 1.31 itojun
143 1.12 thorpej sc->encap_cookie4 = sc->encap_cookie6 = NULL;
144 1.9 itojun
145 1.31 itojun sc->gif_if.if_addrlen = 0;
146 1.12 thorpej sc->gif_if.if_mtu = GIF_MTU;
147 1.12 thorpej sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
148 1.12 thorpej sc->gif_if.if_ioctl = gif_ioctl;
149 1.12 thorpej sc->gif_if.if_output = gif_output;
150 1.12 thorpej sc->gif_if.if_type = IFT_GIF;
151 1.19 thorpej sc->gif_if.if_dlt = DLT_NULL;
152 1.76 dyoung sc->gif_if.if_softc = sc;
153 1.34 itojun IFQ_SET_READY(&sc->gif_if.if_snd);
154 1.12 thorpej if_attach(&sc->gif_if);
155 1.20 thorpej if_alloc_sadl(&sc->gif_if);
156 1.78 joerg bpf_attach(&sc->gif_if, DLT_NULL, sizeof(u_int));
157 1.12 thorpej }
158 1.12 thorpej
159 1.56 thorpej static int
160 1.56 thorpej gif_clone_destroy(struct ifnet *ifp)
161 1.12 thorpej {
162 1.12 thorpej struct gif_softc *sc = (void *) ifp;
163 1.12 thorpej
164 1.12 thorpej LIST_REMOVE(sc, gif_list);
165 1.12 thorpej
166 1.94 knakahar gif_delete_tunnel(&sc->gif_if);
167 1.78 joerg bpf_detach(ifp);
168 1.69 dyoung if_detach(ifp);
169 1.66 dyoung rtcache_free(&sc->gif_ro);
170 1.12 thorpej
171 1.12 thorpej free(sc, M_DEVBUF);
172 1.47 peter
173 1.47 peter return (0);
174 1.9 itojun }
175 1.9 itojun
176 1.42 itojun #ifdef GIF_ENCAPCHECK
177 1.31 itojun int
178 1.56 thorpej gif_encapcheck(struct mbuf *m, int off, int proto, void *arg)
179 1.9 itojun {
180 1.9 itojun struct ip ip;
181 1.9 itojun struct gif_softc *sc;
182 1.9 itojun
183 1.76 dyoung sc = arg;
184 1.9 itojun if (sc == NULL)
185 1.9 itojun return 0;
186 1.9 itojun
187 1.9 itojun if ((sc->gif_if.if_flags & IFF_UP) == 0)
188 1.9 itojun return 0;
189 1.9 itojun
190 1.9 itojun /* no physical address */
191 1.9 itojun if (!sc->gif_psrc || !sc->gif_pdst)
192 1.9 itojun return 0;
193 1.9 itojun
194 1.9 itojun switch (proto) {
195 1.9 itojun #ifdef INET
196 1.9 itojun case IPPROTO_IPV4:
197 1.9 itojun break;
198 1.9 itojun #endif
199 1.9 itojun #ifdef INET6
200 1.9 itojun case IPPROTO_IPV6:
201 1.9 itojun break;
202 1.9 itojun #endif
203 1.9 itojun default:
204 1.9 itojun return 0;
205 1.9 itojun }
206 1.40 christos
207 1.40 christos /* Bail on short packets */
208 1.40 christos KASSERT(m->m_flags & M_PKTHDR);
209 1.40 christos if (m->m_pkthdr.len < sizeof(ip))
210 1.40 christos return 0;
211 1.9 itojun
212 1.76 dyoung m_copydata(m, 0, sizeof(ip), &ip);
213 1.9 itojun
214 1.9 itojun switch (ip.ip_v) {
215 1.9 itojun #ifdef INET
216 1.9 itojun case 4:
217 1.9 itojun if (sc->gif_psrc->sa_family != AF_INET ||
218 1.9 itojun sc->gif_pdst->sa_family != AF_INET)
219 1.9 itojun return 0;
220 1.9 itojun return gif_encapcheck4(m, off, proto, arg);
221 1.9 itojun #endif
222 1.9 itojun #ifdef INET6
223 1.9 itojun case 6:
224 1.41 itojun if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
225 1.41 itojun return 0;
226 1.9 itojun if (sc->gif_psrc->sa_family != AF_INET6 ||
227 1.9 itojun sc->gif_pdst->sa_family != AF_INET6)
228 1.9 itojun return 0;
229 1.9 itojun return gif_encapcheck6(m, off, proto, arg);
230 1.9 itojun #endif
231 1.9 itojun default:
232 1.9 itojun return 0;
233 1.2 itojun }
234 1.2 itojun }
235 1.42 itojun #endif
236 1.2 itojun
237 1.95 knakahar /*
238 1.95 knakahar * gif may cause infinite recursion calls when misconfigured.
239 1.95 knakahar * We'll prevent this by introducing upper limit.
240 1.95 knakahar */
241 1.95 knakahar static int
242 1.95 knakahar gif_check_nesting(struct ifnet *ifp, struct mbuf *m)
243 1.95 knakahar {
244 1.95 knakahar struct m_tag *mtag;
245 1.95 knakahar int *count;
246 1.95 knakahar
247 1.95 knakahar mtag = m_tag_find(m, PACKET_TAG_TUNNEL_INFO, NULL);
248 1.95 knakahar if (mtag != NULL) {
249 1.95 knakahar count = (int *)(mtag + 1);
250 1.95 knakahar if (++(*count) > max_gif_nesting) {
251 1.95 knakahar log(LOG_NOTICE,
252 1.95 knakahar "%s: recursively called too many times(%d)\n",
253 1.95 knakahar if_name(ifp),
254 1.95 knakahar *count);
255 1.95 knakahar return EIO;
256 1.95 knakahar }
257 1.95 knakahar } else {
258 1.95 knakahar mtag = m_tag_get(PACKET_TAG_TUNNEL_INFO, sizeof(*count),
259 1.95 knakahar M_NOWAIT);
260 1.95 knakahar if (mtag != NULL) {
261 1.95 knakahar m_tag_prepend(m, mtag);
262 1.95 knakahar count = (int *)(mtag + 1);
263 1.95 knakahar *count = 0;
264 1.95 knakahar } else {
265 1.95 knakahar log(LOG_DEBUG,
266 1.95 knakahar "%s: m_tag_get() failed, recursion calls are not prevented.\n",
267 1.95 knakahar if_name(ifp));
268 1.95 knakahar }
269 1.95 knakahar }
270 1.95 knakahar
271 1.95 knakahar return 0;
272 1.95 knakahar }
273 1.95 knakahar
274 1.2 itojun int
275 1.65 dyoung gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
276 1.63 christos struct rtentry *rt)
277 1.2 itojun {
278 1.76 dyoung struct gif_softc *sc = ifp->if_softc;
279 1.2 itojun int error = 0;
280 1.39 itojun ALTQ_DECL(struct altq_pktattr pktattr;)
281 1.33 itojun int s;
282 1.33 itojun
283 1.33 itojun IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
284 1.2 itojun
285 1.95 knakahar if ((error = gif_check_nesting(ifp, m)) != 0) {
286 1.95 knakahar m_free(m);
287 1.2 itojun goto end;
288 1.2 itojun }
289 1.2 itojun
290 1.2 itojun m->m_flags &= ~(M_BCAST|M_MCAST);
291 1.2 itojun if (!(ifp->if_flags & IFF_UP) ||
292 1.2 itojun sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
293 1.2 itojun m_freem(m);
294 1.2 itojun error = ENETDOWN;
295 1.2 itojun goto end;
296 1.2 itojun }
297 1.2 itojun
298 1.9 itojun /* XXX should we check if our outer source is legal? */
299 1.2 itojun
300 1.33 itojun /* use DLT_NULL encapsulation here to pass inner af type */
301 1.33 itojun M_PREPEND(m, sizeof(int), M_DONTWAIT);
302 1.33 itojun if (!m) {
303 1.33 itojun error = ENOBUFS;
304 1.33 itojun goto end;
305 1.33 itojun }
306 1.33 itojun *mtod(m, int *) = dst->sa_family;
307 1.33 itojun
308 1.79 dyoung /* Clear checksum-offload flags. */
309 1.79 dyoung m->m_pkthdr.csum_flags = 0;
310 1.79 dyoung m->m_pkthdr.csum_data = 0;
311 1.79 dyoung
312 1.33 itojun s = splnet();
313 1.33 itojun IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
314 1.33 itojun if (error) {
315 1.33 itojun splx(s);
316 1.31 itojun goto end;
317 1.2 itojun }
318 1.33 itojun splx(s);
319 1.33 itojun
320 1.72 ad softint_schedule(sc->gif_si);
321 1.33 itojun error = 0;
322 1.2 itojun
323 1.2 itojun end:
324 1.31 itojun if (error)
325 1.31 itojun ifp->if_oerrors++;
326 1.2 itojun return error;
327 1.2 itojun }
328 1.2 itojun
329 1.56 thorpej static void
330 1.56 thorpej gifintr(void *arg)
331 1.33 itojun {
332 1.33 itojun struct gif_softc *sc;
333 1.33 itojun struct ifnet *ifp;
334 1.33 itojun struct mbuf *m;
335 1.33 itojun int family;
336 1.33 itojun int len;
337 1.33 itojun int s;
338 1.33 itojun int error;
339 1.33 itojun
340 1.76 dyoung sc = arg;
341 1.33 itojun ifp = &sc->gif_if;
342 1.33 itojun
343 1.33 itojun /* output processing */
344 1.33 itojun while (1) {
345 1.33 itojun s = splnet();
346 1.34 itojun IFQ_DEQUEUE(&sc->gif_if.if_snd, m);
347 1.33 itojun splx(s);
348 1.33 itojun if (m == NULL)
349 1.33 itojun break;
350 1.33 itojun
351 1.33 itojun /* grab and chop off inner af type */
352 1.33 itojun if (sizeof(int) > m->m_len) {
353 1.33 itojun m = m_pullup(m, sizeof(int));
354 1.33 itojun if (!m) {
355 1.33 itojun ifp->if_oerrors++;
356 1.33 itojun continue;
357 1.33 itojun }
358 1.33 itojun }
359 1.33 itojun family = *mtod(m, int *);
360 1.78 joerg bpf_mtap(ifp, m);
361 1.33 itojun m_adj(m, sizeof(int));
362 1.33 itojun
363 1.33 itojun len = m->m_pkthdr.len;
364 1.33 itojun
365 1.33 itojun /* dispatch to output logic based on outer AF */
366 1.33 itojun switch (sc->gif_psrc->sa_family) {
367 1.33 itojun #ifdef INET
368 1.33 itojun case AF_INET:
369 1.85 hsuenaga mutex_enter(softnet_lock);
370 1.33 itojun error = in_gif_output(ifp, family, m);
371 1.85 hsuenaga mutex_exit(softnet_lock);
372 1.33 itojun break;
373 1.33 itojun #endif
374 1.33 itojun #ifdef INET6
375 1.33 itojun case AF_INET6:
376 1.85 hsuenaga mutex_enter(softnet_lock);
377 1.33 itojun error = in6_gif_output(ifp, family, m);
378 1.85 hsuenaga mutex_exit(softnet_lock);
379 1.33 itojun break;
380 1.33 itojun #endif
381 1.33 itojun default:
382 1.50 perry m_freem(m);
383 1.33 itojun error = ENETDOWN;
384 1.33 itojun break;
385 1.33 itojun }
386 1.33 itojun
387 1.33 itojun if (error)
388 1.33 itojun ifp->if_oerrors++;
389 1.33 itojun else {
390 1.50 perry ifp->if_opackets++;
391 1.33 itojun ifp->if_obytes += len;
392 1.33 itojun }
393 1.33 itojun }
394 1.33 itojun }
395 1.33 itojun
396 1.33 itojun void
397 1.56 thorpej gif_input(struct mbuf *m, int af, struct ifnet *ifp)
398 1.2 itojun {
399 1.83 rmind pktqueue_t *pktq;
400 1.83 rmind size_t pktlen;
401 1.83 rmind int s;
402 1.2 itojun
403 1.33 itojun if (ifp == NULL) {
404 1.2 itojun /* just in case */
405 1.2 itojun m_freem(m);
406 1.2 itojun return;
407 1.2 itojun }
408 1.2 itojun
409 1.33 itojun m->m_pkthdr.rcvif = ifp;
410 1.83 rmind pktlen = m->m_pkthdr.len;
411 1.50 perry
412 1.78 joerg bpf_mtap_af(ifp, af, m);
413 1.2 itojun
414 1.2 itojun /*
415 1.2 itojun * Put the packet to the network layer input queue according to the
416 1.83 rmind * specified address family. Note: we avoid direct call to the
417 1.83 rmind * input function of the network layer in order to avoid recursion.
418 1.83 rmind * This may be revisited in the future.
419 1.2 itojun */
420 1.2 itojun switch (af) {
421 1.2 itojun #ifdef INET
422 1.2 itojun case AF_INET:
423 1.83 rmind pktq = ip_pktq;
424 1.2 itojun break;
425 1.2 itojun #endif
426 1.2 itojun #ifdef INET6
427 1.2 itojun case AF_INET6:
428 1.83 rmind pktq = ip6_pktq;
429 1.2 itojun break;
430 1.2 itojun #endif
431 1.2 itojun default:
432 1.2 itojun m_freem(m);
433 1.2 itojun return;
434 1.2 itojun }
435 1.2 itojun
436 1.27 thorpej s = splnet();
437 1.83 rmind if (__predict_true(pktq_enqueue(pktq, m, 0))) {
438 1.83 rmind ifp->if_ibytes += pktlen;
439 1.83 rmind ifp->if_ipackets++;
440 1.83 rmind } else {
441 1.2 itojun m_freem(m);
442 1.2 itojun }
443 1.2 itojun splx(s);
444 1.2 itojun }
445 1.2 itojun
446 1.9 itojun /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
447 1.2 itojun int
448 1.67 christos gif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
449 1.2 itojun {
450 1.76 dyoung struct gif_softc *sc = ifp->if_softc;
451 1.2 itojun struct ifreq *ifr = (struct ifreq*)data;
452 1.84 roy struct ifaddr *ifa = (struct ifaddr*)data;
453 1.2 itojun int error = 0, size;
454 1.9 itojun struct sockaddr *dst, *src;
455 1.31 itojun
456 1.2 itojun switch (cmd) {
457 1.76 dyoung case SIOCINITIFADDR:
458 1.32 itojun ifp->if_flags |= IFF_UP;
459 1.84 roy ifa->ifa_rtrequest = p2p_rtrequest;
460 1.2 itojun break;
461 1.50 perry
462 1.2 itojun case SIOCADDMULTI:
463 1.2 itojun case SIOCDELMULTI:
464 1.2 itojun switch (ifr->ifr_addr.sa_family) {
465 1.2 itojun #ifdef INET
466 1.2 itojun case AF_INET: /* IP supports Multicast */
467 1.2 itojun break;
468 1.2 itojun #endif /* INET */
469 1.2 itojun #ifdef INET6
470 1.2 itojun case AF_INET6: /* IP6 supports Multicast */
471 1.2 itojun break;
472 1.2 itojun #endif /* INET6 */
473 1.2 itojun default: /* Other protocols doesn't support Multicast */
474 1.2 itojun error = EAFNOSUPPORT;
475 1.2 itojun break;
476 1.2 itojun }
477 1.2 itojun break;
478 1.2 itojun
479 1.2 itojun case SIOCSIFMTU:
480 1.73 dyoung if (ifr->ifr_mtu < GIF_MTU_MIN || ifr->ifr_mtu > GIF_MTU_MAX)
481 1.73 dyoung return EINVAL;
482 1.73 dyoung else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
483 1.73 dyoung error = 0;
484 1.2 itojun break;
485 1.2 itojun
486 1.31 itojun #ifdef INET
487 1.2 itojun case SIOCSIFPHYADDR:
488 1.31 itojun #endif
489 1.2 itojun #ifdef INET6
490 1.2 itojun case SIOCSIFPHYADDR_IN6:
491 1.2 itojun #endif /* INET6 */
492 1.25 itojun case SIOCSLIFPHYADDR:
493 1.11 itojun switch (cmd) {
494 1.15 itojun #ifdef INET
495 1.11 itojun case SIOCSIFPHYADDR:
496 1.11 itojun src = (struct sockaddr *)
497 1.11 itojun &(((struct in_aliasreq *)data)->ifra_addr);
498 1.11 itojun dst = (struct sockaddr *)
499 1.11 itojun &(((struct in_aliasreq *)data)->ifra_dstaddr);
500 1.11 itojun break;
501 1.15 itojun #endif
502 1.11 itojun #ifdef INET6
503 1.11 itojun case SIOCSIFPHYADDR_IN6:
504 1.11 itojun src = (struct sockaddr *)
505 1.11 itojun &(((struct in6_aliasreq *)data)->ifra_addr);
506 1.11 itojun dst = (struct sockaddr *)
507 1.11 itojun &(((struct in6_aliasreq *)data)->ifra_dstaddr);
508 1.25 itojun break;
509 1.25 itojun #endif
510 1.25 itojun case SIOCSLIFPHYADDR:
511 1.25 itojun src = (struct sockaddr *)
512 1.25 itojun &(((struct if_laddrreq *)data)->addr);
513 1.25 itojun dst = (struct sockaddr *)
514 1.25 itojun &(((struct if_laddrreq *)data)->dstaddr);
515 1.31 itojun break;
516 1.31 itojun default:
517 1.31 itojun return EINVAL;
518 1.25 itojun }
519 1.25 itojun
520 1.25 itojun /* sa_family must be equal */
521 1.25 itojun if (src->sa_family != dst->sa_family)
522 1.25 itojun return EINVAL;
523 1.25 itojun
524 1.25 itojun /* validate sa_len */
525 1.25 itojun switch (src->sa_family) {
526 1.25 itojun #ifdef INET
527 1.25 itojun case AF_INET:
528 1.25 itojun if (src->sa_len != sizeof(struct sockaddr_in))
529 1.16 itojun return EINVAL;
530 1.11 itojun break;
531 1.11 itojun #endif
532 1.25 itojun #ifdef INET6
533 1.25 itojun case AF_INET6:
534 1.25 itojun if (src->sa_len != sizeof(struct sockaddr_in6))
535 1.25 itojun return EINVAL;
536 1.25 itojun break;
537 1.25 itojun #endif
538 1.25 itojun default:
539 1.25 itojun return EAFNOSUPPORT;
540 1.25 itojun }
541 1.25 itojun switch (dst->sa_family) {
542 1.25 itojun #ifdef INET
543 1.25 itojun case AF_INET:
544 1.25 itojun if (dst->sa_len != sizeof(struct sockaddr_in))
545 1.25 itojun return EINVAL;
546 1.25 itojun break;
547 1.25 itojun #endif
548 1.25 itojun #ifdef INET6
549 1.25 itojun case AF_INET6:
550 1.25 itojun if (dst->sa_len != sizeof(struct sockaddr_in6))
551 1.25 itojun return EINVAL;
552 1.25 itojun break;
553 1.25 itojun #endif
554 1.25 itojun default:
555 1.25 itojun return EAFNOSUPPORT;
556 1.25 itojun }
557 1.25 itojun
558 1.25 itojun /* check sa_family looks sane for the cmd */
559 1.25 itojun switch (cmd) {
560 1.25 itojun case SIOCSIFPHYADDR:
561 1.25 itojun if (src->sa_family == AF_INET)
562 1.25 itojun break;
563 1.25 itojun return EAFNOSUPPORT;
564 1.25 itojun #ifdef INET6
565 1.25 itojun case SIOCSIFPHYADDR_IN6:
566 1.25 itojun if (src->sa_family == AF_INET6)
567 1.25 itojun break;
568 1.25 itojun return EAFNOSUPPORT;
569 1.25 itojun #endif /* INET6 */
570 1.25 itojun case SIOCSLIFPHYADDR:
571 1.25 itojun /* checks done in the above */
572 1.25 itojun break;
573 1.11 itojun }
574 1.11 itojun
575 1.31 itojun error = gif_set_tunnel(&sc->gif_if, src, dst);
576 1.9 itojun break;
577 1.9 itojun
578 1.9 itojun #ifdef SIOCDIFPHYADDR
579 1.9 itojun case SIOCDIFPHYADDR:
580 1.31 itojun gif_delete_tunnel(&sc->gif_if);
581 1.2 itojun break;
582 1.9 itojun #endif
583 1.50 perry
584 1.2 itojun case SIOCGIFPSRCADDR:
585 1.2 itojun #ifdef INET6
586 1.2 itojun case SIOCGIFPSRCADDR_IN6:
587 1.2 itojun #endif /* INET6 */
588 1.2 itojun if (sc->gif_psrc == NULL) {
589 1.2 itojun error = EADDRNOTAVAIL;
590 1.2 itojun goto bad;
591 1.2 itojun }
592 1.2 itojun src = sc->gif_psrc;
593 1.16 itojun switch (cmd) {
594 1.2 itojun #ifdef INET
595 1.16 itojun case SIOCGIFPSRCADDR:
596 1.2 itojun dst = &ifr->ifr_addr;
597 1.16 itojun size = sizeof(ifr->ifr_addr);
598 1.2 itojun break;
599 1.2 itojun #endif /* INET */
600 1.2 itojun #ifdef INET6
601 1.16 itojun case SIOCGIFPSRCADDR_IN6:
602 1.2 itojun dst = (struct sockaddr *)
603 1.2 itojun &(((struct in6_ifreq *)data)->ifr_addr);
604 1.16 itojun size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
605 1.2 itojun break;
606 1.2 itojun #endif /* INET6 */
607 1.2 itojun default:
608 1.2 itojun error = EADDRNOTAVAIL;
609 1.2 itojun goto bad;
610 1.2 itojun }
611 1.16 itojun if (src->sa_len > size)
612 1.16 itojun return EINVAL;
613 1.68 dyoung memcpy(dst, src, src->sa_len);
614 1.2 itojun break;
615 1.50 perry
616 1.2 itojun case SIOCGIFPDSTADDR:
617 1.2 itojun #ifdef INET6
618 1.2 itojun case SIOCGIFPDSTADDR_IN6:
619 1.2 itojun #endif /* INET6 */
620 1.2 itojun if (sc->gif_pdst == NULL) {
621 1.2 itojun error = EADDRNOTAVAIL;
622 1.2 itojun goto bad;
623 1.2 itojun }
624 1.2 itojun src = sc->gif_pdst;
625 1.16 itojun switch (cmd) {
626 1.2 itojun #ifdef INET
627 1.16 itojun case SIOCGIFPDSTADDR:
628 1.2 itojun dst = &ifr->ifr_addr;
629 1.16 itojun size = sizeof(ifr->ifr_addr);
630 1.2 itojun break;
631 1.2 itojun #endif /* INET */
632 1.2 itojun #ifdef INET6
633 1.16 itojun case SIOCGIFPDSTADDR_IN6:
634 1.2 itojun dst = (struct sockaddr *)
635 1.2 itojun &(((struct in6_ifreq *)data)->ifr_addr);
636 1.16 itojun size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
637 1.2 itojun break;
638 1.2 itojun #endif /* INET6 */
639 1.2 itojun default:
640 1.2 itojun error = EADDRNOTAVAIL;
641 1.2 itojun goto bad;
642 1.2 itojun }
643 1.25 itojun if (src->sa_len > size)
644 1.25 itojun return EINVAL;
645 1.68 dyoung memcpy(dst, src, src->sa_len);
646 1.25 itojun break;
647 1.25 itojun
648 1.25 itojun case SIOCGLIFPHYADDR:
649 1.25 itojun if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
650 1.25 itojun error = EADDRNOTAVAIL;
651 1.25 itojun goto bad;
652 1.25 itojun }
653 1.25 itojun
654 1.25 itojun /* copy src */
655 1.25 itojun src = sc->gif_psrc;
656 1.25 itojun dst = (struct sockaddr *)
657 1.25 itojun &(((struct if_laddrreq *)data)->addr);
658 1.25 itojun size = sizeof(((struct if_laddrreq *)data)->addr);
659 1.25 itojun if (src->sa_len > size)
660 1.25 itojun return EINVAL;
661 1.68 dyoung memcpy(dst, src, src->sa_len);
662 1.25 itojun
663 1.25 itojun /* copy dst */
664 1.25 itojun src = sc->gif_pdst;
665 1.25 itojun dst = (struct sockaddr *)
666 1.25 itojun &(((struct if_laddrreq *)data)->dstaddr);
667 1.25 itojun size = sizeof(((struct if_laddrreq *)data)->dstaddr);
668 1.16 itojun if (src->sa_len > size)
669 1.16 itojun return EINVAL;
670 1.68 dyoung memcpy(dst, src, src->sa_len);
671 1.2 itojun break;
672 1.2 itojun
673 1.2 itojun default:
674 1.76 dyoung return ifioctl_common(ifp, cmd, data);
675 1.2 itojun }
676 1.2 itojun bad:
677 1.2 itojun return error;
678 1.12 thorpej }
679 1.12 thorpej
680 1.31 itojun int
681 1.56 thorpej gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
682 1.31 itojun {
683 1.76 dyoung struct gif_softc *sc = ifp->if_softc;
684 1.31 itojun struct gif_softc *sc2;
685 1.71 dyoung struct sockaddr *osrc, *odst;
686 1.96 knakahar struct sockaddr *nsrc, *ndst;
687 1.31 itojun int s;
688 1.31 itojun int error;
689 1.31 itojun
690 1.31 itojun s = splsoftnet();
691 1.31 itojun
692 1.68 dyoung LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
693 1.31 itojun if (sc2 == sc)
694 1.31 itojun continue;
695 1.31 itojun if (!sc2->gif_pdst || !sc2->gif_psrc)
696 1.31 itojun continue;
697 1.31 itojun /* can't configure same pair of address onto two gifs */
698 1.71 dyoung if (sockaddr_cmp(sc2->gif_pdst, dst) == 0 &&
699 1.71 dyoung sockaddr_cmp(sc2->gif_psrc, src) == 0) {
700 1.91 knakahar /* continue to use the old configureation. */
701 1.96 knakahar splx(s);
702 1.96 knakahar return EADDRNOTAVAIL;
703 1.31 itojun }
704 1.31 itojun
705 1.31 itojun /* XXX both end must be valid? (I mean, not 0.0.0.0) */
706 1.31 itojun }
707 1.31 itojun
708 1.96 knakahar if ((nsrc = sockaddr_dup(src, M_WAITOK)) == NULL) {
709 1.96 knakahar splx(s);
710 1.96 knakahar return ENOMEM;
711 1.96 knakahar }
712 1.96 knakahar if ((ndst = sockaddr_dup(dst, M_WAITOK)) == NULL) {
713 1.96 knakahar sockaddr_free(nsrc);
714 1.96 knakahar splx(s);
715 1.96 knakahar return ENOMEM;
716 1.96 knakahar }
717 1.96 knakahar
718 1.96 knakahar /* Firstly, clear old configurations. */
719 1.58 msaitoh if (sc->gif_si) {
720 1.72 ad softint_disestablish(sc->gif_si);
721 1.58 msaitoh sc->gif_si = NULL;
722 1.58 msaitoh }
723 1.31 itojun /* XXX we can detach from both, but be polite just in case */
724 1.31 itojun if (sc->gif_psrc)
725 1.31 itojun switch (sc->gif_psrc->sa_family) {
726 1.31 itojun #ifdef INET
727 1.31 itojun case AF_INET:
728 1.31 itojun (void)in_gif_detach(sc);
729 1.31 itojun break;
730 1.31 itojun #endif
731 1.31 itojun #ifdef INET6
732 1.31 itojun case AF_INET6:
733 1.31 itojun (void)in6_gif_detach(sc);
734 1.31 itojun break;
735 1.31 itojun #endif
736 1.31 itojun }
737 1.31 itojun
738 1.96 knakahar /*
739 1.96 knakahar * Secondly, try to set new configurations.
740 1.96 knakahar * If the setup failed, rollback to old configurations.
741 1.96 knakahar */
742 1.96 knakahar do {
743 1.96 knakahar osrc = sc->gif_psrc;
744 1.96 knakahar odst = sc->gif_pdst;
745 1.96 knakahar sc->gif_psrc = nsrc;
746 1.96 knakahar sc->gif_pdst = ndst;
747 1.33 itojun
748 1.96 knakahar switch (sc->gif_psrc->sa_family) {
749 1.96 knakahar #ifdef INET
750 1.96 knakahar case AF_INET:
751 1.96 knakahar error = in_gif_attach(sc);
752 1.96 knakahar break;
753 1.96 knakahar #endif
754 1.96 knakahar #ifdef INET6
755 1.96 knakahar case AF_INET6:
756 1.96 knakahar error = in6_gif_attach(sc);
757 1.96 knakahar break;
758 1.96 knakahar #endif
759 1.96 knakahar default:
760 1.96 knakahar error = EINVAL;
761 1.96 knakahar break;
762 1.96 knakahar }
763 1.96 knakahar if (error) {
764 1.96 knakahar /* rollback to the last configuration. */
765 1.96 knakahar nsrc = osrc;
766 1.96 knakahar ndst = odst;
767 1.96 knakahar osrc = sc->gif_psrc;
768 1.96 knakahar odst = sc->gif_pdst;
769 1.31 itojun
770 1.96 knakahar continue;
771 1.96 knakahar }
772 1.31 itojun
773 1.96 knakahar sc->gif_si = softint_establish(SOFTINT_NET, gifintr, sc);
774 1.96 knakahar if (sc->gif_si == NULL) {
775 1.96 knakahar switch (sc->gif_psrc->sa_family) {
776 1.31 itojun #ifdef INET
777 1.96 knakahar case AF_INET:
778 1.96 knakahar (void)in_gif_detach(sc);
779 1.96 knakahar break;
780 1.31 itojun #endif
781 1.31 itojun #ifdef INET6
782 1.96 knakahar case AF_INET6:
783 1.96 knakahar (void)in6_gif_detach(sc);
784 1.96 knakahar break;
785 1.31 itojun #endif
786 1.96 knakahar }
787 1.96 knakahar
788 1.96 knakahar /* rollback to the last configuration. */
789 1.96 knakahar nsrc = osrc;
790 1.96 knakahar ndst = odst;
791 1.96 knakahar osrc = sc->gif_psrc;
792 1.96 knakahar odst = sc->gif_pdst;
793 1.96 knakahar
794 1.96 knakahar error = ENOMEM;
795 1.96 knakahar continue;
796 1.96 knakahar }
797 1.96 knakahar } while (error != 0 && (nsrc != NULL && ndst != NULL));
798 1.96 knakahar /* Thirdly, even rollback failed, clear configurations. */
799 1.96 knakahar if (error) {
800 1.96 knakahar osrc = sc->gif_psrc;
801 1.96 knakahar odst = sc->gif_pdst;
802 1.96 knakahar sc->gif_psrc = NULL;
803 1.96 knakahar sc->gif_pdst = NULL;
804 1.31 itojun }
805 1.31 itojun
806 1.31 itojun if (osrc)
807 1.71 dyoung sockaddr_free(osrc);
808 1.31 itojun if (odst)
809 1.71 dyoung sockaddr_free(odst);
810 1.31 itojun
811 1.31 itojun if (sc->gif_psrc && sc->gif_pdst)
812 1.31 itojun ifp->if_flags |= IFF_RUNNING;
813 1.31 itojun else
814 1.31 itojun ifp->if_flags &= ~IFF_RUNNING;
815 1.92 knakahar
816 1.31 itojun splx(s);
817 1.31 itojun return error;
818 1.31 itojun }
819 1.31 itojun
820 1.12 thorpej void
821 1.56 thorpej gif_delete_tunnel(struct ifnet *ifp)
822 1.12 thorpej {
823 1.76 dyoung struct gif_softc *sc = ifp->if_softc;
824 1.12 thorpej int s;
825 1.12 thorpej
826 1.12 thorpej s = splsoftnet();
827 1.12 thorpej
828 1.35 itojun if (sc->gif_si) {
829 1.72 ad softint_disestablish(sc->gif_si);
830 1.35 itojun sc->gif_si = NULL;
831 1.35 itojun }
832 1.12 thorpej if (sc->gif_psrc) {
833 1.71 dyoung sockaddr_free(sc->gif_psrc);
834 1.12 thorpej sc->gif_psrc = NULL;
835 1.12 thorpej }
836 1.12 thorpej if (sc->gif_pdst) {
837 1.71 dyoung sockaddr_free(sc->gif_pdst);
838 1.12 thorpej sc->gif_pdst = NULL;
839 1.12 thorpej }
840 1.31 itojun /* it is safe to detach from both */
841 1.31 itojun #ifdef INET
842 1.31 itojun (void)in_gif_detach(sc);
843 1.31 itojun #endif
844 1.31 itojun #ifdef INET6
845 1.31 itojun (void)in6_gif_detach(sc);
846 1.31 itojun #endif
847 1.12 thorpej
848 1.31 itojun if (sc->gif_psrc && sc->gif_pdst)
849 1.31 itojun ifp->if_flags |= IFF_RUNNING;
850 1.31 itojun else
851 1.31 itojun ifp->if_flags &= ~IFF_RUNNING;
852 1.12 thorpej splx(s);
853 1.2 itojun }
854