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