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