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