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