if_gre.c revision 1.57.2.4 1 1.57.2.4 yamt /* $NetBSD: if_gre.c,v 1.57.2.4 2007/09/03 14:42:06 yamt Exp $ */
2 1.4 thorpej
3 1.1 hwr /*
4 1.4 thorpej * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 hwr * All rights reserved.
6 1.1 hwr *
7 1.1 hwr * This code is derived from software contributed to The NetBSD Foundation
8 1.1 hwr * by Heiko W.Rupp <hwr (at) pilhuhn.de>
9 1.1 hwr *
10 1.56 is * IPv6-over-GRE contributed by Gert Doering <gert (at) greenie.muc.de>
11 1.56 is *
12 1.1 hwr * Redistribution and use in source and binary forms, with or without
13 1.1 hwr * modification, are permitted provided that the following conditions
14 1.1 hwr * are met:
15 1.1 hwr * 1. Redistributions of source code must retain the above copyright
16 1.1 hwr * notice, this list of conditions and the following disclaimer.
17 1.1 hwr * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 hwr * notice, this list of conditions and the following disclaimer in the
19 1.1 hwr * documentation and/or other materials provided with the distribution.
20 1.1 hwr * 3. All advertising materials mentioning features or use of this software
21 1.1 hwr * must display the following acknowledgement:
22 1.1 hwr * This product includes software developed by the NetBSD
23 1.1 hwr * Foundation, Inc. and its contributors.
24 1.1 hwr * 4. Neither the name of The NetBSD Foundation nor the names of its
25 1.1 hwr * contributors may be used to endorse or promote products derived
26 1.1 hwr * from this software without specific prior written permission.
27 1.1 hwr *
28 1.1 hwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 1.1 hwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 1.1 hwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 1.1 hwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 1.1 hwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 1.1 hwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 1.1 hwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 1.1 hwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 1.1 hwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 1.1 hwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 1.1 hwr * POSSIBILITY OF SUCH DAMAGE.
39 1.1 hwr */
40 1.1 hwr
41 1.1 hwr /*
42 1.1 hwr * Encapsulate L3 protocols into IP
43 1.1 hwr * See RFC 1701 and 1702 for more details.
44 1.1 hwr * If_gre is compatible with Cisco GRE tunnels, so you can
45 1.1 hwr * have a NetBSD box as the other end of a tunnel interface of a Cisco
46 1.1 hwr * router. See gre(4) for more details.
47 1.6 hwr * Also supported: IP in IP encaps (proto 55) as of RFC 2004
48 1.1 hwr */
49 1.22 lukem
50 1.22 lukem #include <sys/cdefs.h>
51 1.57.2.4 yamt __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.57.2.4 2007/09/03 14:42:06 yamt Exp $");
52 1.1 hwr
53 1.57.2.2 yamt #include "opt_gre.h"
54 1.1 hwr #include "opt_inet.h"
55 1.1 hwr #include "bpfilter.h"
56 1.1 hwr
57 1.54 christos #ifdef INET
58 1.1 hwr #include <sys/param.h>
59 1.57.2.2 yamt #include <sys/file.h>
60 1.57.2.2 yamt #include <sys/filedesc.h>
61 1.1 hwr #include <sys/malloc.h>
62 1.1 hwr #include <sys/mbuf.h>
63 1.13 martin #include <sys/proc.h>
64 1.1 hwr #include <sys/protosw.h>
65 1.1 hwr #include <sys/socket.h>
66 1.57.2.2 yamt #include <sys/socketvar.h>
67 1.1 hwr #include <sys/ioctl.h>
68 1.10 thorpej #include <sys/queue.h>
69 1.1 hwr #if __NetBSD__
70 1.1 hwr #include <sys/systm.h>
71 1.57.2.2 yamt #include <sys/sysctl.h>
72 1.57.2.1 yamt #include <sys/kauth.h>
73 1.1 hwr #endif
74 1.1 hwr
75 1.57.2.4 yamt #include <sys/kernel.h>
76 1.57.2.4 yamt #include <sys/mutex.h>
77 1.57.2.4 yamt #include <sys/condvar.h>
78 1.57.2.2 yamt #include <sys/kthread.h>
79 1.57.2.2 yamt
80 1.1 hwr #include <machine/cpu.h>
81 1.1 hwr
82 1.1 hwr #include <net/ethertypes.h>
83 1.1 hwr #include <net/if.h>
84 1.1 hwr #include <net/if_types.h>
85 1.1 hwr #include <net/netisr.h>
86 1.1 hwr #include <net/route.h>
87 1.1 hwr
88 1.1 hwr #ifdef INET
89 1.1 hwr #include <netinet/in.h>
90 1.1 hwr #include <netinet/in_systm.h>
91 1.1 hwr #include <netinet/in_var.h>
92 1.1 hwr #include <netinet/ip.h>
93 1.1 hwr #include <netinet/ip_var.h>
94 1.1 hwr #else
95 1.4 thorpej #error "Huh? if_gre without inet?"
96 1.1 hwr #endif
97 1.1 hwr
98 1.1 hwr
99 1.1 hwr #ifdef NETATALK
100 1.1 hwr #include <netatalk/at.h>
101 1.1 hwr #include <netatalk/at_var.h>
102 1.1 hwr #include <netatalk/at_extern.h>
103 1.1 hwr #endif
104 1.1 hwr
105 1.1 hwr #if NBPFILTER > 0
106 1.1 hwr #include <sys/time.h>
107 1.1 hwr #include <net/bpf.h>
108 1.1 hwr #endif
109 1.1 hwr
110 1.1 hwr #include <net/if_gre.h>
111 1.1 hwr
112 1.57.2.4 yamt #include <compat/sys/socket.h>
113 1.57.2.4 yamt #include <compat/sys/sockio.h>
114 1.20 itojun /*
115 1.27 martin * It is not easy to calculate the right value for a GRE MTU.
116 1.27 martin * We leave this task to the admin and use the same default that
117 1.27 martin * other vendors use.
118 1.20 itojun */
119 1.27 martin #define GREMTU 1476
120 1.1 hwr
121 1.57.2.2 yamt #ifdef GRE_DEBUG
122 1.57.2.4 yamt int gre_debug = 0;
123 1.57.2.2 yamt #define GRE_DPRINTF(__sc, __fmt, ...) \
124 1.57.2.2 yamt do { \
125 1.57.2.4 yamt if (gre_debug || ((__sc)->sc_if.if_flags & IFF_DEBUG) != 0)\
126 1.57.2.2 yamt printf(__fmt, __VA_ARGS__); \
127 1.57.2.2 yamt } while (/*CONSTCOND*/0)
128 1.57.2.2 yamt #else
129 1.57.2.2 yamt #define GRE_DPRINTF(__sc, __fmt, ...) do { } while (/*CONSTCOND*/0)
130 1.57.2.2 yamt #endif /* GRE_DEBUG */
131 1.57.2.2 yamt
132 1.11 thorpej struct gre_softc_head gre_softc_list;
133 1.26 martin int ip_gre_ttl = GRE_TTL;
134 1.1 hwr
135 1.57.2.1 yamt static int gre_clone_create(struct if_clone *, int);
136 1.57.2.1 yamt static int gre_clone_destroy(struct ifnet *);
137 1.10 thorpej
138 1.57.2.1 yamt static struct if_clone gre_cloner =
139 1.10 thorpej IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
140 1.1 hwr
141 1.57.2.3 yamt static int gre_output(struct ifnet *, struct mbuf *,
142 1.57.2.3 yamt const struct sockaddr *, struct rtentry *);
143 1.57.2.4 yamt static int gre_ioctl(struct ifnet *, u_long, void *);
144 1.1 hwr
145 1.57.2.4 yamt static void gre_thread(void *);
146 1.57.2.1 yamt static int gre_compute_route(struct gre_softc *sc);
147 1.57.2.1 yamt
148 1.57.2.4 yamt static void gre_closef(struct file **, struct lwp *);
149 1.57.2.2 yamt static int gre_getsockname(struct socket *, struct mbuf *, struct lwp *);
150 1.57.2.2 yamt static int gre_getpeername(struct socket *, struct mbuf *, struct lwp *);
151 1.57.2.2 yamt static int gre_getnames(struct socket *, struct lwp *, struct sockaddr_in *,
152 1.57.2.2 yamt struct sockaddr_in *);
153 1.57.2.2 yamt
154 1.57.2.4 yamt /* Calling thread must hold sc->sc_mtx. */
155 1.57.2.2 yamt static void
156 1.57.2.4 yamt gre_join(struct gre_softc *sc)
157 1.57.2.2 yamt {
158 1.57.2.4 yamt while (sc->sc_running != 0)
159 1.57.2.4 yamt cv_wait(&sc->sc_join_cv, &sc->sc_mtx);
160 1.57.2.2 yamt }
161 1.57.2.2 yamt
162 1.57.2.4 yamt /* Calling thread must hold sc->sc_mtx. */
163 1.57.2.2 yamt static void
164 1.57.2.2 yamt gre_wakeup(struct gre_softc *sc)
165 1.57.2.2 yamt {
166 1.57.2.2 yamt GRE_DPRINTF(sc, "%s: enter\n", __func__);
167 1.57.2.4 yamt sc->sc_haswork = 1;
168 1.57.2.4 yamt cv_signal(&sc->sc_work_cv);
169 1.57.2.2 yamt }
170 1.57.2.2 yamt
171 1.57.2.1 yamt static int
172 1.57.2.1 yamt gre_clone_create(struct if_clone *ifc, int unit)
173 1.1 hwr {
174 1.8 explorer struct gre_softc *sc;
175 1.1 hwr
176 1.10 thorpej sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK);
177 1.10 thorpej memset(sc, 0, sizeof(struct gre_softc));
178 1.57.2.4 yamt mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_NET);
179 1.57.2.4 yamt cv_init(&sc->sc_work_cv, "gre work");
180 1.57.2.4 yamt cv_init(&sc->sc_join_cv, "gre join");
181 1.57.2.4 yamt cv_init(&sc->sc_soparm_cv, "gre soparm");
182 1.10 thorpej
183 1.50 itojun snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
184 1.50 itojun ifc->ifc_name, unit);
185 1.10 thorpej sc->sc_if.if_softc = sc;
186 1.51 tron sc->sc_if.if_type = IFT_TUNNEL;
187 1.34 itojun sc->sc_if.if_addrlen = 0;
188 1.10 thorpej sc->sc_if.if_hdrlen = 24; /* IP + GRE */
189 1.15 thorpej sc->sc_if.if_dlt = DLT_NULL;
190 1.20 itojun sc->sc_if.if_mtu = GREMTU;
191 1.10 thorpej sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
192 1.10 thorpej sc->sc_if.if_output = gre_output;
193 1.10 thorpej sc->sc_if.if_ioctl = gre_ioctl;
194 1.57.2.4 yamt sc->sc_dst.s_addr = sc->sc_src.s_addr = INADDR_ANY;
195 1.57.2.4 yamt sc->sc_dstport = sc->sc_srcport = 0;
196 1.57.2.2 yamt sc->sc_proto = IPPROTO_GRE;
197 1.57.2.2 yamt sc->sc_snd.ifq_maxlen = 256;
198 1.35 itojun sc->sc_if.if_flags |= IFF_LINK0;
199 1.10 thorpej if_attach(&sc->sc_if);
200 1.16 thorpej if_alloc_sadl(&sc->sc_if);
201 1.1 hwr #if NBPFILTER > 0
202 1.14 thorpej bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
203 1.1 hwr #endif
204 1.57.2.4 yamt sc->sc_running = 1;
205 1.57.2.4 yamt if (kthread_create(PRI_NONE, 0, NULL, gre_thread, sc,
206 1.57.2.4 yamt NULL, sc->sc_if.if_xname) != 0)
207 1.57.2.4 yamt sc->sc_running = 0;
208 1.10 thorpej LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
209 1.57.2.3 yamt return 0;
210 1.10 thorpej }
211 1.1 hwr
212 1.57.2.1 yamt static int
213 1.57.2.1 yamt gre_clone_destroy(struct ifnet *ifp)
214 1.10 thorpej {
215 1.10 thorpej struct gre_softc *sc = ifp->if_softc;
216 1.10 thorpej
217 1.10 thorpej LIST_REMOVE(sc, sc_list);
218 1.10 thorpej #if NBPFILTER > 0
219 1.10 thorpej bpfdetach(ifp);
220 1.10 thorpej #endif
221 1.57.2.4 yamt if_detach(ifp);
222 1.57.2.4 yamt mutex_enter(&sc->sc_mtx);
223 1.57.2.4 yamt sc->sc_dying = 1;
224 1.57.2.2 yamt gre_wakeup(sc);
225 1.57.2.4 yamt gre_join(sc);
226 1.57.2.4 yamt mutex_exit(&sc->sc_mtx);
227 1.57.2.3 yamt rtcache_free(&sc->route);
228 1.57.2.4 yamt
229 1.57.2.4 yamt cv_destroy(&sc->sc_soparm_cv);
230 1.57.2.4 yamt cv_destroy(&sc->sc_join_cv);
231 1.57.2.4 yamt cv_destroy(&sc->sc_work_cv);
232 1.57.2.4 yamt mutex_destroy(&sc->sc_mtx);
233 1.10 thorpej free(sc, M_DEVBUF);
234 1.53 peter
235 1.57.2.3 yamt return 0;
236 1.1 hwr }
237 1.1 hwr
238 1.57.2.2 yamt static void
239 1.57.2.4 yamt gre_receive(struct socket *so, void *arg, int waitflag)
240 1.57.2.2 yamt {
241 1.57.2.2 yamt struct gre_softc *sc = (struct gre_softc *)arg;
242 1.57.2.2 yamt
243 1.57.2.2 yamt GRE_DPRINTF(sc, "%s: enter\n", __func__);
244 1.57.2.2 yamt
245 1.57.2.2 yamt gre_wakeup(sc);
246 1.57.2.2 yamt }
247 1.57.2.2 yamt
248 1.57.2.2 yamt static void
249 1.57.2.4 yamt gre_upcall_add(struct socket *so, void *arg)
250 1.57.2.2 yamt {
251 1.57.2.2 yamt /* XXX What if the kernel already set an upcall? */
252 1.57.2.4 yamt KASSERT((so->so_rcv.sb_flags & SB_UPCALL) == 0);
253 1.57.2.2 yamt so->so_upcallarg = arg;
254 1.57.2.2 yamt so->so_upcall = gre_receive;
255 1.57.2.2 yamt so->so_rcv.sb_flags |= SB_UPCALL;
256 1.57.2.2 yamt }
257 1.57.2.2 yamt
258 1.57.2.2 yamt static void
259 1.57.2.2 yamt gre_upcall_remove(struct socket *so)
260 1.57.2.2 yamt {
261 1.57.2.2 yamt /* XXX What if the kernel already set an upcall? */
262 1.57.2.2 yamt so->so_rcv.sb_flags &= ~SB_UPCALL;
263 1.57.2.2 yamt so->so_upcallarg = NULL;
264 1.57.2.2 yamt so->so_upcall = NULL;
265 1.57.2.2 yamt }
266 1.57.2.2 yamt
267 1.57.2.2 yamt static void
268 1.57.2.2 yamt gre_sodestroy(struct socket **sop)
269 1.57.2.2 yamt {
270 1.57.2.2 yamt gre_upcall_remove(*sop);
271 1.57.2.2 yamt soshutdown(*sop, SHUT_RDWR);
272 1.57.2.2 yamt soclose(*sop);
273 1.57.2.2 yamt *sop = NULL;
274 1.57.2.2 yamt }
275 1.57.2.2 yamt
276 1.57.2.2 yamt static struct mbuf *
277 1.57.2.2 yamt gre_getsockmbuf(struct socket *so)
278 1.57.2.2 yamt {
279 1.57.2.2 yamt struct mbuf *m;
280 1.57.2.2 yamt
281 1.57.2.2 yamt m = m_get(M_WAIT, MT_SONAME);
282 1.57.2.2 yamt if (m != NULL)
283 1.57.2.2 yamt MCLAIM(m, so->so_mowner);
284 1.57.2.2 yamt return m;
285 1.57.2.2 yamt }
286 1.57.2.2 yamt
287 1.57.2.2 yamt static int
288 1.57.2.4 yamt gre_socreate1(struct gre_softc *sc, struct lwp *l, struct socket **sop)
289 1.57.2.2 yamt {
290 1.57.2.2 yamt int rc;
291 1.57.2.2 yamt struct mbuf *m;
292 1.57.2.2 yamt struct sockaddr_in *sin;
293 1.57.2.2 yamt struct socket *so;
294 1.57.2.2 yamt
295 1.57.2.2 yamt GRE_DPRINTF(sc, "%s: enter\n", __func__);
296 1.57.2.2 yamt rc = socreate(AF_INET, sop, SOCK_DGRAM, IPPROTO_UDP, l);
297 1.57.2.2 yamt if (rc != 0) {
298 1.57.2.2 yamt GRE_DPRINTF(sc, "%s: socreate failed\n", __func__);
299 1.57.2.2 yamt return rc;
300 1.57.2.2 yamt }
301 1.57.2.2 yamt
302 1.57.2.2 yamt so = *sop;
303 1.57.2.2 yamt
304 1.57.2.4 yamt gre_upcall_add(so, sc);
305 1.57.2.2 yamt if ((m = gre_getsockmbuf(so)) == NULL) {
306 1.57.2.2 yamt rc = ENOBUFS;
307 1.57.2.2 yamt goto out;
308 1.57.2.2 yamt }
309 1.57.2.2 yamt sin = mtod(m, struct sockaddr_in *);
310 1.57.2.4 yamt sockaddr_in_init(sin, &sc->sc_src, sc->sc_srcport);
311 1.57.2.4 yamt m->m_len = sin->sin_len;
312 1.57.2.2 yamt
313 1.57.2.2 yamt GRE_DPRINTF(sc, "%s: bind 0x%08" PRIx32 " port %d\n", __func__,
314 1.57.2.2 yamt sin->sin_addr.s_addr, ntohs(sin->sin_port));
315 1.57.2.2 yamt if ((rc = sobind(so, m, l)) != 0) {
316 1.57.2.2 yamt GRE_DPRINTF(sc, "%s: sobind failed\n", __func__);
317 1.57.2.2 yamt goto out;
318 1.57.2.2 yamt }
319 1.57.2.2 yamt
320 1.57.2.4 yamt if (sc->sc_srcport == 0) {
321 1.57.2.2 yamt if ((rc = gre_getsockname(so, m, l)) != 0) {
322 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: gre_getsockname\n", __func__);
323 1.57.2.2 yamt goto out;
324 1.57.2.2 yamt }
325 1.57.2.4 yamt sc->sc_srcport = sin->sin_port;
326 1.57.2.2 yamt }
327 1.57.2.2 yamt
328 1.57.2.4 yamt sockaddr_in_init(sin, &sc->sc_dst, sc->sc_dstport);
329 1.57.2.4 yamt m->m_len = sin->sin_len;
330 1.57.2.2 yamt
331 1.57.2.2 yamt if ((rc = soconnect(so, m, l)) != 0) {
332 1.57.2.2 yamt GRE_DPRINTF(sc, "%s: soconnect failed\n", __func__);
333 1.57.2.2 yamt goto out;
334 1.57.2.2 yamt }
335 1.57.2.2 yamt
336 1.57.2.2 yamt *mtod(m, int *) = ip_gre_ttl;
337 1.57.2.2 yamt m->m_len = sizeof(int);
338 1.57.2.4 yamt KASSERT(so->so_proto && so->so_proto->pr_ctloutput);
339 1.57.2.2 yamt rc = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, IPPROTO_IP, IP_TTL,
340 1.57.2.2 yamt &m);
341 1.57.2.2 yamt m = NULL;
342 1.57.2.2 yamt if (rc != 0) {
343 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: setopt ttl failed\n", __func__);
344 1.57.2.2 yamt rc = 0;
345 1.57.2.2 yamt }
346 1.57.2.2 yamt out:
347 1.57.2.2 yamt m_freem(m);
348 1.57.2.2 yamt
349 1.57.2.2 yamt if (rc != 0)
350 1.57.2.2 yamt gre_sodestroy(sop);
351 1.57.2.4 yamt else {
352 1.57.2.4 yamt sc->sc_if.if_flags |= IFF_RUNNING;
353 1.57.2.4 yamt sc->sc_soparm = sc->sc_newsoparm;
354 1.57.2.4 yamt }
355 1.57.2.2 yamt
356 1.57.2.2 yamt return rc;
357 1.57.2.2 yamt }
358 1.57.2.2 yamt
359 1.57.2.2 yamt static void
360 1.57.2.4 yamt gre_do_recv(struct gre_softc *sc, struct socket *so, lwp_t *l)
361 1.57.2.2 yamt {
362 1.57.2.2 yamt for (;;) {
363 1.57.2.4 yamt int flags, rc;
364 1.57.2.4 yamt const struct gre_h *gh;
365 1.57.2.4 yamt struct mbuf *m;
366 1.57.2.4 yamt
367 1.57.2.4 yamt flags = MSG_DONTWAIT;
368 1.57.2.4 yamt sc->sc_uio.uio_resid = 1000000;
369 1.57.2.4 yamt rc = (*so->so_receive)(so, NULL, &sc->sc_uio, &m, NULL, &flags);
370 1.57.2.4 yamt /* TBD Back off if ECONNREFUSED (indicates
371 1.57.2.4 yamt * ICMP Port Unreachable)?
372 1.57.2.4 yamt */
373 1.57.2.4 yamt if (rc == EWOULDBLOCK) {
374 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: so_receive EWOULDBLOCK\n",
375 1.57.2.2 yamt __func__);
376 1.57.2.2 yamt break;
377 1.57.2.4 yamt } else if (rc != 0 || m == NULL) {
378 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: rc %d m %p\n",
379 1.57.2.4 yamt sc->sc_if.if_xname, rc, (void *)m);
380 1.57.2.4 yamt continue;
381 1.57.2.4 yamt } else
382 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: so_receive ok\n", __func__);
383 1.57.2.4 yamt if (m->m_len < sizeof(*gh) &&
384 1.57.2.4 yamt (m = m_pullup(m, sizeof(*gh))) == NULL) {
385 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: m_pullup failed\n", __func__);
386 1.57.2.4 yamt continue;
387 1.57.2.2 yamt }
388 1.57.2.4 yamt gh = mtod(m, const struct gre_h *);
389 1.57.2.4 yamt
390 1.57.2.4 yamt if (gre_input3(sc, m, 0, gh, 0) == 0) {
391 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: dropping unsupported\n", __func__);
392 1.57.2.4 yamt m_freem(m);
393 1.57.2.2 yamt }
394 1.57.2.4 yamt }
395 1.57.2.4 yamt }
396 1.57.2.2 yamt
397 1.57.2.4 yamt static void
398 1.57.2.4 yamt gre_do_send(struct gre_softc *sc, struct socket *so, lwp_t *l)
399 1.57.2.4 yamt {
400 1.57.2.4 yamt for (;;) {
401 1.57.2.4 yamt int rc;
402 1.57.2.4 yamt struct mbuf *m;
403 1.57.2.2 yamt
404 1.57.2.4 yamt mutex_enter(&sc->sc_mtx);
405 1.57.2.4 yamt IF_DEQUEUE(&sc->sc_snd, m);
406 1.57.2.4 yamt mutex_exit(&sc->sc_mtx);
407 1.57.2.4 yamt if (m == NULL)
408 1.57.2.4 yamt break;
409 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: dequeue\n", __func__);
410 1.57.2.4 yamt if ((so->so_state & SS_ISCONNECTED) == 0) {
411 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: not connected\n", __func__);
412 1.57.2.4 yamt m_freem(m);
413 1.57.2.4 yamt continue;
414 1.57.2.2 yamt }
415 1.57.2.4 yamt rc = (*so->so_send)(so, NULL, NULL, m, NULL, 0, l);
416 1.57.2.4 yamt /* XXX handle ENOBUFS? */
417 1.57.2.4 yamt if (rc != 0)
418 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: so_send failed\n",
419 1.57.2.4 yamt __func__);
420 1.57.2.2 yamt }
421 1.57.2.4 yamt }
422 1.57.2.4 yamt
423 1.57.2.4 yamt static struct socket *
424 1.57.2.4 yamt gre_reconf(struct gre_softc *sc, struct socket *so, lwp_t *l)
425 1.57.2.4 yamt {
426 1.57.2.4 yamt struct ifnet *ifp = &sc->sc_if;
427 1.57.2.4 yamt
428 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: enter\n", __func__);
429 1.57.2.4 yamt
430 1.57.2.4 yamt shutdown:
431 1.57.2.4 yamt if (sc->sc_soparm.sp_fp != NULL) {
432 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
433 1.57.2.2 yamt gre_upcall_remove(so);
434 1.57.2.4 yamt gre_closef(&sc->sc_soparm.sp_fp, curlwp);
435 1.57.2.4 yamt so = NULL;
436 1.57.2.2 yamt } else if (so != NULL)
437 1.57.2.2 yamt gre_sodestroy(&so);
438 1.57.2.4 yamt
439 1.57.2.4 yamt if (sc->sc_dying)
440 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: dying\n", __func__);
441 1.57.2.4 yamt else if ((ifp->if_flags & IFF_UP) != IFF_UP)
442 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: down\n", __func__);
443 1.57.2.4 yamt else if (sc->sc_proto != IPPROTO_UDP)
444 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: not UDP\n", __func__);
445 1.57.2.4 yamt else if (sc->sc_newsoparm.sp_fp != NULL) {
446 1.57.2.4 yamt sc->sc_soparm = sc->sc_newsoparm;
447 1.57.2.4 yamt sc->sc_newsoparm.sp_fp = NULL;
448 1.57.2.4 yamt so = (struct socket *)sc->sc_soparm.sp_fp->f_data;
449 1.57.2.4 yamt gre_upcall_add(so, sc);
450 1.57.2.4 yamt } else if (gre_socreate1(sc, l, &so) != 0) {
451 1.57.2.4 yamt sc->sc_dying = 1;
452 1.57.2.4 yamt goto shutdown;
453 1.57.2.4 yamt }
454 1.57.2.4 yamt cv_signal(&sc->sc_soparm_cv);
455 1.57.2.4 yamt if (so != NULL)
456 1.57.2.4 yamt sc->sc_if.if_flags |= IFF_RUNNING;
457 1.57.2.4 yamt else if (sc->sc_proto == IPPROTO_UDP)
458 1.57.2.4 yamt sc->sc_if.if_flags &= ~IFF_RUNNING;
459 1.57.2.4 yamt return so;
460 1.57.2.4 yamt }
461 1.57.2.4 yamt
462 1.57.2.4 yamt static void
463 1.57.2.4 yamt gre_thread1(struct gre_softc *sc, struct lwp *l)
464 1.57.2.4 yamt {
465 1.57.2.4 yamt struct ifnet *ifp = &sc->sc_if;
466 1.57.2.4 yamt struct socket *so = NULL;
467 1.57.2.4 yamt
468 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: enter\n", __func__);
469 1.57.2.4 yamt
470 1.57.2.4 yamt while (!sc->sc_dying) {
471 1.57.2.4 yamt while (sc->sc_haswork == 0) {
472 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: sleeping\n", __func__);
473 1.57.2.4 yamt cv_wait(&sc->sc_work_cv, &sc->sc_mtx);
474 1.57.2.4 yamt }
475 1.57.2.4 yamt sc->sc_haswork = 0;
476 1.57.2.4 yamt
477 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: awake\n", __func__);
478 1.57.2.4 yamt
479 1.57.2.4 yamt /* XXX optimize */
480 1.57.2.4 yamt if ((ifp->if_flags & IFF_UP) != IFF_UP ||
481 1.57.2.4 yamt sc->sc_proto != IPPROTO_UDP || so == NULL ||
482 1.57.2.4 yamt sc->sc_newsoparm.sp_fp != NULL ||
483 1.57.2.4 yamt memcmp(&sc->sc_soparm, &sc->sc_newsoparm,
484 1.57.2.4 yamt offsetof(struct gre_soparm, sp_fp)) != 0)
485 1.57.2.4 yamt so = gre_reconf(sc, so, l);
486 1.57.2.4 yamt mutex_exit(&sc->sc_mtx);
487 1.57.2.4 yamt if (so != NULL) {
488 1.57.2.4 yamt gre_do_recv(sc, so, l);
489 1.57.2.4 yamt gre_do_send(sc, so, l);
490 1.57.2.4 yamt }
491 1.57.2.4 yamt mutex_enter(&sc->sc_mtx);
492 1.57.2.2 yamt }
493 1.57.2.4 yamt sc->sc_running = 0;
494 1.57.2.4 yamt cv_signal(&sc->sc_join_cv);
495 1.57.2.2 yamt }
496 1.57.2.2 yamt
497 1.57.2.2 yamt static void
498 1.57.2.2 yamt gre_thread(void *arg)
499 1.57.2.2 yamt {
500 1.57.2.2 yamt struct gre_softc *sc = (struct gre_softc *)arg;
501 1.57.2.2 yamt
502 1.57.2.4 yamt mutex_enter(&sc->sc_mtx);
503 1.57.2.2 yamt gre_thread1(sc, curlwp);
504 1.57.2.4 yamt mutex_exit(&sc->sc_mtx);
505 1.57.2.4 yamt
506 1.57.2.2 yamt /* must not touch sc after this! */
507 1.57.2.2 yamt kthread_exit(0);
508 1.57.2.2 yamt }
509 1.57.2.2 yamt
510 1.57.2.4 yamt /* Calling thread must hold sc->sc_mtx. */
511 1.57.2.2 yamt int
512 1.57.2.4 yamt gre_input3(struct gre_softc *sc, struct mbuf *m, int hlen,
513 1.57.2.4 yamt const struct gre_h *gh, int mtx_held)
514 1.57.2.2 yamt {
515 1.57.2.2 yamt u_int16_t flags;
516 1.57.2.2 yamt #if NBPFILTER > 0
517 1.57.2.2 yamt u_int32_t af = AF_INET; /* af passed to BPF tap */
518 1.57.2.2 yamt #endif
519 1.57.2.4 yamt int isr;
520 1.57.2.2 yamt struct ifqueue *ifq;
521 1.57.2.2 yamt
522 1.57.2.2 yamt sc->sc_if.if_ipackets++;
523 1.57.2.2 yamt sc->sc_if.if_ibytes += m->m_pkthdr.len;
524 1.57.2.2 yamt
525 1.57.2.4 yamt hlen += sizeof(struct gre_h);
526 1.57.2.2 yamt
527 1.57.2.4 yamt /* process GRE flags as packet can be of variable len */
528 1.57.2.4 yamt flags = ntohs(gh->flags);
529 1.57.2.2 yamt
530 1.57.2.4 yamt /* Checksum & Offset are present */
531 1.57.2.4 yamt if ((flags & GRE_CP) | (flags & GRE_RP))
532 1.57.2.4 yamt hlen += 4;
533 1.57.2.4 yamt /* We don't support routing fields (variable length) */
534 1.57.2.4 yamt if (flags & GRE_RP) {
535 1.57.2.4 yamt sc->sc_if.if_ierrors++;
536 1.57.2.4 yamt return 0;
537 1.57.2.4 yamt }
538 1.57.2.4 yamt if (flags & GRE_KP)
539 1.57.2.4 yamt hlen += 4;
540 1.57.2.4 yamt if (flags & GRE_SP)
541 1.57.2.4 yamt hlen += 4;
542 1.57.2.4 yamt
543 1.57.2.4 yamt switch (ntohs(gh->ptype)) { /* ethertypes */
544 1.57.2.4 yamt case ETHERTYPE_IP: /* shouldn't need a schednetisr(), as */
545 1.57.2.4 yamt ifq = &ipintrq; /* we are in ip_input */
546 1.57.2.4 yamt isr = NETISR_IP;
547 1.57.2.4 yamt break;
548 1.57.2.2 yamt #ifdef NETATALK
549 1.57.2.4 yamt case ETHERTYPE_ATALK:
550 1.57.2.4 yamt ifq = &atintrq1;
551 1.57.2.4 yamt isr = NETISR_ATALK;
552 1.57.2.2 yamt #if NBPFILTER > 0
553 1.57.2.4 yamt af = AF_APPLETALK;
554 1.57.2.2 yamt #endif
555 1.57.2.4 yamt break;
556 1.57.2.2 yamt #endif
557 1.57.2.2 yamt #ifdef INET6
558 1.57.2.4 yamt case ETHERTYPE_IPV6:
559 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: IPv6 packet\n", __func__);
560 1.57.2.4 yamt ifq = &ip6intrq;
561 1.57.2.4 yamt isr = NETISR_IPV6;
562 1.57.2.2 yamt #if NBPFILTER > 0
563 1.57.2.4 yamt af = AF_INET6;
564 1.57.2.2 yamt #endif
565 1.57.2.2 yamt break;
566 1.57.2.4 yamt #endif
567 1.57.2.4 yamt default: /* others not yet supported */
568 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: unhandled ethertype 0x%04x\n", __func__,
569 1.57.2.4 yamt ntohs(gh->ptype));
570 1.57.2.4 yamt sc->sc_if.if_noproto++;
571 1.57.2.3 yamt return 0;
572 1.57.2.2 yamt }
573 1.57.2.2 yamt
574 1.57.2.2 yamt if (hlen > m->m_pkthdr.len) {
575 1.57.2.2 yamt m_freem(m);
576 1.57.2.2 yamt sc->sc_if.if_ierrors++;
577 1.57.2.3 yamt return EINVAL;
578 1.57.2.2 yamt }
579 1.57.2.2 yamt m_adj(m, hlen);
580 1.57.2.2 yamt
581 1.57.2.2 yamt #if NBPFILTER > 0
582 1.57.2.2 yamt if (sc->sc_if.if_bpf != NULL)
583 1.57.2.2 yamt bpf_mtap_af(sc->sc_if.if_bpf, af, m);
584 1.57.2.2 yamt #endif /*NBPFILTER > 0*/
585 1.57.2.2 yamt
586 1.57.2.2 yamt m->m_pkthdr.rcvif = &sc->sc_if;
587 1.57.2.2 yamt
588 1.57.2.4 yamt if (!mtx_held)
589 1.57.2.4 yamt mutex_enter(&sc->sc_mtx);
590 1.57.2.2 yamt if (IF_QFULL(ifq)) {
591 1.57.2.2 yamt IF_DROP(ifq);
592 1.57.2.2 yamt m_freem(m);
593 1.57.2.2 yamt } else {
594 1.57.2.2 yamt IF_ENQUEUE(ifq, m);
595 1.57.2.2 yamt }
596 1.57.2.2 yamt /* we need schednetisr since the address family may change */
597 1.57.2.2 yamt schednetisr(isr);
598 1.57.2.4 yamt if (!mtx_held)
599 1.57.2.4 yamt mutex_exit(&sc->sc_mtx);
600 1.57.2.2 yamt
601 1.57.2.3 yamt return 1; /* packet is done, no further processing needed */
602 1.57.2.2 yamt }
603 1.57.2.2 yamt
604 1.20 itojun /*
605 1.1 hwr * The output routine. Takes a packet and encapsulates it in the protocol
606 1.57.2.2 yamt * given by sc->sc_proto. See also RFC 1701 and RFC 2004
607 1.1 hwr */
608 1.57.2.1 yamt static int
609 1.57.2.3 yamt gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
610 1.8 explorer struct rtentry *rt)
611 1.1 hwr {
612 1.57.2.4 yamt int error = 0, hlen, msiz;
613 1.10 thorpej struct gre_softc *sc = ifp->if_softc;
614 1.57.2.2 yamt struct greip *gi;
615 1.57.2.2 yamt struct gre_h *gh;
616 1.57.2.2 yamt struct ip *eip, *ip;
617 1.56 is u_int8_t ip_tos = 0;
618 1.48 itojun u_int16_t etype = 0;
619 1.20 itojun struct mobile_h mob_h;
620 1.24 martin
621 1.57.2.4 yamt if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
622 1.57.2.4 yamt (IFF_UP | IFF_RUNNING) ||
623 1.57.2.4 yamt sc->sc_src.s_addr == INADDR_ANY ||
624 1.57.2.4 yamt sc->sc_dst.s_addr == INADDR_ANY) {
625 1.37 itojun m_freem(m);
626 1.38 itojun error = ENETDOWN;
627 1.38 itojun goto end;
628 1.37 itojun }
629 1.1 hwr
630 1.57.2.2 yamt gi = NULL;
631 1.41 itojun ip = NULL;
632 1.1 hwr
633 1.1 hwr #if NBPFILTER >0
634 1.52 christos if (ifp->if_bpf)
635 1.52 christos bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
636 1.1 hwr #endif
637 1.1 hwr
638 1.26 martin m->m_flags &= ~(M_BCAST|M_MCAST);
639 1.1 hwr
640 1.57.2.2 yamt switch (sc->sc_proto) {
641 1.57.2.2 yamt case IPPROTO_MOBILE:
642 1.57.2.4 yamt if (dst->sa_family != AF_INET) {
643 1.3 hwr IF_DROP(&ifp->if_snd);
644 1.3 hwr m_freem(m);
645 1.38 itojun error = EINVAL;
646 1.38 itojun goto end;
647 1.3 hwr }
648 1.57.2.4 yamt
649 1.57.2.4 yamt if (M_UNWRITABLE(m, sizeof(*ip)) &&
650 1.57.2.4 yamt (m = m_pullup(m, sizeof(*ip))) == NULL) {
651 1.57.2.4 yamt error = ENOBUFS;
652 1.57.2.4 yamt goto end;
653 1.57.2.4 yamt }
654 1.57.2.4 yamt ip = mtod(m, struct ip *);
655 1.57.2.4 yamt
656 1.57.2.4 yamt memset(&mob_h, 0, MOB_H_SIZ_L);
657 1.57.2.4 yamt mob_h.proto = (ip->ip_p) << 8;
658 1.57.2.4 yamt mob_h.odst = ip->ip_dst.s_addr;
659 1.57.2.4 yamt ip->ip_dst.s_addr = sc->sc_dst.s_addr;
660 1.57.2.4 yamt
661 1.57.2.4 yamt /*
662 1.57.2.4 yamt * If the packet comes from our host, we only change
663 1.57.2.4 yamt * the destination address in the IP header.
664 1.57.2.4 yamt * Else we also need to save and change the source
665 1.57.2.4 yamt */
666 1.57.2.4 yamt if (in_hosteq(ip->ip_src, sc->sc_src))
667 1.57.2.4 yamt msiz = MOB_H_SIZ_S;
668 1.57.2.4 yamt else {
669 1.57.2.4 yamt mob_h.proto |= MOB_H_SBIT;
670 1.57.2.4 yamt mob_h.osrc = ip->ip_src.s_addr;
671 1.57.2.4 yamt ip->ip_src.s_addr = sc->sc_src.s_addr;
672 1.57.2.4 yamt msiz = MOB_H_SIZ_L;
673 1.57.2.4 yamt }
674 1.57.2.4 yamt HTONS(mob_h.proto);
675 1.57.2.4 yamt mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
676 1.57.2.4 yamt
677 1.57.2.4 yamt M_PREPEND(m, msiz, M_DONTWAIT);
678 1.57.2.4 yamt if (m == NULL) {
679 1.57.2.4 yamt error = ENOBUFS;
680 1.57.2.4 yamt goto end;
681 1.57.2.4 yamt }
682 1.57.2.4 yamt /* XXX Assuming that ip does not dangle after
683 1.57.2.4 yamt * M_PREPEND. In practice, that's true, but
684 1.57.2.4 yamt * that's not in M_PREPEND's contract.
685 1.57.2.4 yamt */
686 1.57.2.4 yamt memmove(mtod(m, void *), ip, sizeof(*ip));
687 1.57.2.4 yamt ip = mtod(m, struct ip *);
688 1.57.2.4 yamt memcpy(ip + 1, &mob_h, (size_t)msiz);
689 1.57.2.4 yamt ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
690 1.57.2.2 yamt break;
691 1.57.2.2 yamt case IPPROTO_UDP:
692 1.57.2.2 yamt case IPPROTO_GRE:
693 1.57.2.2 yamt GRE_DPRINTF(sc, "%s: dst->sa_family=%d\n", __func__,
694 1.57.2.2 yamt dst->sa_family);
695 1.20 itojun switch (dst->sa_family) {
696 1.1 hwr case AF_INET:
697 1.41 itojun ip = mtod(m, struct ip *);
698 1.56 is ip_tos = ip->ip_tos;
699 1.8 explorer etype = ETHERTYPE_IP;
700 1.1 hwr break;
701 1.1 hwr #ifdef NETATALK
702 1.1 hwr case AF_APPLETALK:
703 1.8 explorer etype = ETHERTYPE_ATALK;
704 1.1 hwr break;
705 1.1 hwr #endif
706 1.56 is #ifdef INET6
707 1.56 is case AF_INET6:
708 1.56 is etype = ETHERTYPE_IPV6;
709 1.56 is break;
710 1.56 is #endif
711 1.1 hwr default:
712 1.1 hwr IF_DROP(&ifp->if_snd);
713 1.1 hwr m_freem(m);
714 1.38 itojun error = EAFNOSUPPORT;
715 1.38 itojun goto end;
716 1.1 hwr }
717 1.57.2.2 yamt break;
718 1.57.2.2 yamt default:
719 1.1 hwr IF_DROP(&ifp->if_snd);
720 1.1 hwr m_freem(m);
721 1.38 itojun error = EINVAL;
722 1.38 itojun goto end;
723 1.1 hwr }
724 1.1 hwr
725 1.57.2.2 yamt switch (sc->sc_proto) {
726 1.57.2.2 yamt case IPPROTO_GRE:
727 1.57.2.2 yamt hlen = sizeof(struct greip);
728 1.57.2.2 yamt break;
729 1.57.2.2 yamt case IPPROTO_UDP:
730 1.57.2.2 yamt hlen = sizeof(struct gre_h);
731 1.57.2.2 yamt break;
732 1.57.2.2 yamt default:
733 1.57.2.2 yamt hlen = 0;
734 1.57.2.2 yamt break;
735 1.57.2.2 yamt }
736 1.57.2.2 yamt
737 1.57.2.2 yamt M_PREPEND(m, hlen, M_DONTWAIT);
738 1.57.2.2 yamt
739 1.57.2.2 yamt if (m == NULL) {
740 1.1 hwr IF_DROP(&ifp->if_snd);
741 1.38 itojun error = ENOBUFS;
742 1.38 itojun goto end;
743 1.1 hwr }
744 1.1 hwr
745 1.57.2.2 yamt switch (sc->sc_proto) {
746 1.57.2.2 yamt case IPPROTO_UDP:
747 1.57.2.2 yamt gh = mtod(m, struct gre_h *);
748 1.57.2.2 yamt memset(gh, 0, sizeof(*gh));
749 1.57.2.2 yamt gh->ptype = htons(etype);
750 1.57.2.2 yamt /* XXX Need to handle IP ToS. Look at how I handle IP TTL. */
751 1.57.2.2 yamt break;
752 1.57.2.2 yamt case IPPROTO_GRE:
753 1.57.2.2 yamt gi = mtod(m, struct greip *);
754 1.57.2.2 yamt gh = &gi->gi_g;
755 1.57.2.2 yamt eip = &gi->gi_i;
756 1.1 hwr /* we don't have any GRE flags for now */
757 1.57.2.2 yamt memset(gh, 0, sizeof(*gh));
758 1.57.2.2 yamt gh->ptype = htons(etype);
759 1.57.2.4 yamt eip->ip_src = sc->sc_src;
760 1.57.2.4 yamt eip->ip_dst = sc->sc_dst;
761 1.57.2.2 yamt eip->ip_hl = (sizeof(struct ip)) >> 2;
762 1.57.2.2 yamt eip->ip_ttl = ip_gre_ttl;
763 1.57.2.2 yamt eip->ip_tos = ip_tos;
764 1.57.2.2 yamt eip->ip_len = htons(m->m_pkthdr.len);
765 1.57.2.2 yamt eip->ip_p = sc->sc_proto;
766 1.57.2.2 yamt break;
767 1.57.2.2 yamt case IPPROTO_MOBILE:
768 1.57.2.2 yamt eip = mtod(m, struct ip *);
769 1.57.2.2 yamt eip->ip_p = sc->sc_proto;
770 1.57.2.2 yamt break;
771 1.57.2.2 yamt default:
772 1.57.2.2 yamt error = EPROTONOSUPPORT;
773 1.57.2.2 yamt m_freem(m);
774 1.57.2.2 yamt goto end;
775 1.3 hwr }
776 1.1 hwr
777 1.1 hwr ifp->if_opackets++;
778 1.8 explorer ifp->if_obytes += m->m_pkthdr.len;
779 1.57.2.2 yamt
780 1.1 hwr /* send it off */
781 1.57.2.2 yamt if (sc->sc_proto == IPPROTO_UDP) {
782 1.57.2.2 yamt if (IF_QFULL(&sc->sc_snd)) {
783 1.57.2.2 yamt IF_DROP(&sc->sc_snd);
784 1.57.2.2 yamt error = ENOBUFS;
785 1.57.2.2 yamt m_freem(m);
786 1.57.2.2 yamt } else {
787 1.57.2.2 yamt IF_ENQUEUE(&sc->sc_snd, m);
788 1.57.2.2 yamt gre_wakeup(sc);
789 1.57.2.2 yamt error = 0;
790 1.57.2.2 yamt }
791 1.57.2.2 yamt goto end;
792 1.57.2.2 yamt }
793 1.57.2.3 yamt if (sc->route.ro_rt == NULL)
794 1.57.2.3 yamt rtcache_init(&sc->route);
795 1.57.2.3 yamt else
796 1.57.2.3 yamt rtcache_check(&sc->route);
797 1.57.2.4 yamt if (sc->route.ro_rt == NULL) {
798 1.57.2.4 yamt m_freem(m);
799 1.57.2.2 yamt goto end;
800 1.57.2.4 yamt }
801 1.57.2.4 yamt if (sc->route.ro_rt->rt_ifp->if_softc == sc) {
802 1.57.2.4 yamt rtcache_clear(&sc->route);
803 1.57.2.4 yamt m_freem(m);
804 1.57.2.4 yamt } else
805 1.57.2.4 yamt error = ip_output(m, NULL, &sc->route, 0, NULL, NULL);
806 1.38 itojun end:
807 1.8 explorer if (error)
808 1.1 hwr ifp->if_oerrors++;
809 1.57.2.3 yamt return error;
810 1.1 hwr }
811 1.1 hwr
812 1.57.2.4 yamt /* Calling thread must hold sc->sc_mtx. */
813 1.57.2.2 yamt static int
814 1.57.2.2 yamt gre_kick(struct gre_softc *sc)
815 1.57.2.2 yamt {
816 1.57.2.4 yamt if (!sc->sc_running)
817 1.57.2.4 yamt return EBUSY;
818 1.57.2.4 yamt gre_wakeup(sc);
819 1.57.2.4 yamt return 0;
820 1.57.2.2 yamt }
821 1.57.2.2 yamt
822 1.57.2.4 yamt /* Calling thread must hold sc->sc_mtx. */
823 1.57.2.2 yamt static int
824 1.57.2.2 yamt gre_getname(struct socket *so, int req, struct mbuf *nam, struct lwp *l)
825 1.57.2.2 yamt {
826 1.57.2.4 yamt return (*so->so_proto->pr_usrreq)(so, req, NULL, nam, NULL, l);
827 1.57.2.2 yamt }
828 1.57.2.2 yamt
829 1.57.2.4 yamt /* Calling thread must hold sc->sc_mtx. */
830 1.57.2.2 yamt static int
831 1.57.2.2 yamt gre_getsockname(struct socket *so, struct mbuf *nam, struct lwp *l)
832 1.57.2.2 yamt {
833 1.57.2.2 yamt return gre_getname(so, PRU_SOCKADDR, nam, l);
834 1.57.2.2 yamt }
835 1.57.2.2 yamt
836 1.57.2.4 yamt /* Calling thread must hold sc->sc_mtx. */
837 1.57.2.2 yamt static int
838 1.57.2.2 yamt gre_getpeername(struct socket *so, struct mbuf *nam, struct lwp *l)
839 1.57.2.2 yamt {
840 1.57.2.2 yamt return gre_getname(so, PRU_PEERADDR, nam, l);
841 1.57.2.2 yamt }
842 1.57.2.2 yamt
843 1.57.2.4 yamt /* Calling thread must hold sc->sc_mtx. */
844 1.57.2.2 yamt static int
845 1.57.2.2 yamt gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_in *src,
846 1.57.2.2 yamt struct sockaddr_in *dst)
847 1.57.2.2 yamt {
848 1.57.2.2 yamt struct mbuf *m;
849 1.57.2.2 yamt struct sockaddr_in *sin;
850 1.57.2.2 yamt int rc;
851 1.57.2.2 yamt
852 1.57.2.2 yamt if ((m = gre_getsockmbuf(so)) == NULL)
853 1.57.2.2 yamt return ENOBUFS;
854 1.57.2.2 yamt
855 1.57.2.2 yamt sin = mtod(m, struct sockaddr_in *);
856 1.57.2.2 yamt
857 1.57.2.2 yamt if ((rc = gre_getsockname(so, m, l)) != 0)
858 1.57.2.2 yamt goto out;
859 1.57.2.2 yamt if (sin->sin_family != AF_INET) {
860 1.57.2.2 yamt rc = EAFNOSUPPORT;
861 1.57.2.2 yamt goto out;
862 1.57.2.2 yamt }
863 1.57.2.2 yamt *src = *sin;
864 1.57.2.2 yamt
865 1.57.2.2 yamt if ((rc = gre_getpeername(so, m, l)) != 0)
866 1.57.2.2 yamt goto out;
867 1.57.2.2 yamt if (sin->sin_family != AF_INET) {
868 1.57.2.2 yamt rc = EAFNOSUPPORT;
869 1.57.2.2 yamt goto out;
870 1.57.2.2 yamt }
871 1.57.2.2 yamt *dst = *sin;
872 1.57.2.2 yamt
873 1.57.2.2 yamt out:
874 1.57.2.2 yamt m_freem(m);
875 1.57.2.2 yamt return rc;
876 1.57.2.2 yamt }
877 1.57.2.2 yamt
878 1.57.2.4 yamt static void
879 1.57.2.4 yamt gre_closef(struct file **fpp, struct lwp *l)
880 1.57.2.4 yamt {
881 1.57.2.4 yamt struct file *fp = *fpp;
882 1.57.2.4 yamt
883 1.57.2.4 yamt simple_lock(&fp->f_slock);
884 1.57.2.4 yamt FILE_USE(fp);
885 1.57.2.4 yamt closef(fp, l);
886 1.57.2.4 yamt *fpp = NULL;
887 1.57.2.4 yamt }
888 1.57.2.4 yamt
889 1.57.2.1 yamt static int
890 1.57.2.4 yamt gre_ioctl(struct ifnet *ifp, const u_long cmd, void *data)
891 1.1 hwr {
892 1.57.2.2 yamt u_char oproto;
893 1.57.2.4 yamt struct file *fp;
894 1.57.2.2 yamt struct socket *so;
895 1.57.2.2 yamt struct sockaddr_in dst, src;
896 1.13 martin struct proc *p = curproc; /* XXX */
897 1.57.2.2 yamt struct lwp *l = curlwp; /* XXX */
898 1.57.2.4 yamt struct ifreq *ifr;
899 1.28 itojun struct if_laddrreq *lifr = (struct if_laddrreq *)data;
900 1.8 explorer struct gre_softc *sc = ifp->if_softc;
901 1.1 hwr struct sockaddr_in si;
902 1.57.2.4 yamt const struct sockaddr *sa;
903 1.57.2.2 yamt int error = 0;
904 1.1 hwr
905 1.57.2.4 yamt ifr = data;
906 1.57.2.4 yamt
907 1.57.2.2 yamt switch (cmd) {
908 1.57.2.2 yamt case SIOCSIFFLAGS:
909 1.57.2.2 yamt case SIOCSIFMTU:
910 1.57.2.2 yamt case GRESPROTO:
911 1.57.2.2 yamt case GRESADDRD:
912 1.57.2.2 yamt case GRESADDRS:
913 1.57.2.2 yamt case GRESSOCK:
914 1.57.2.2 yamt case GREDSOCK:
915 1.57.2.2 yamt case SIOCSLIFPHYADDR:
916 1.57.2.2 yamt case SIOCDIFPHYADDR:
917 1.57.2.2 yamt if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
918 1.57.2.2 yamt KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
919 1.57.2.2 yamt NULL) != 0)
920 1.57.2.3 yamt return EPERM;
921 1.57.2.2 yamt break;
922 1.57.2.2 yamt default:
923 1.57.2.2 yamt break;
924 1.57.2.2 yamt }
925 1.1 hwr
926 1.57.2.4 yamt mutex_enter(&sc->sc_mtx);
927 1.20 itojun switch (cmd) {
928 1.20 itojun case SIOCSIFADDR:
929 1.36 itojun ifp->if_flags |= IFF_UP;
930 1.57.2.2 yamt if ((error = gre_kick(sc)) != 0)
931 1.57.2.2 yamt ifp->if_flags &= ~IFF_UP;
932 1.36 itojun break;
933 1.55 perry case SIOCSIFDSTADDR:
934 1.1 hwr break;
935 1.1 hwr case SIOCSIFFLAGS:
936 1.57.2.2 yamt oproto = sc->sc_proto;
937 1.57.2.2 yamt switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
938 1.57.2.2 yamt case IFF_LINK0|IFF_LINK2:
939 1.57.2.2 yamt sc->sc_proto = IPPROTO_UDP;
940 1.57.2.2 yamt if (oproto != IPPROTO_UDP)
941 1.57.2.2 yamt ifp->if_flags &= ~IFF_RUNNING;
942 1.57.2.2 yamt error = gre_kick(sc);
943 1.13 martin break;
944 1.57.2.2 yamt case IFF_LINK0:
945 1.57.2.2 yamt sc->sc_proto = IPPROTO_GRE;
946 1.57.2.2 yamt gre_wakeup(sc);
947 1.57.2.2 yamt goto recompute;
948 1.57.2.2 yamt case 0:
949 1.57.2.2 yamt sc->sc_proto = IPPROTO_MOBILE;
950 1.57.2.2 yamt gre_wakeup(sc);
951 1.57.2.2 yamt goto recompute;
952 1.57.2.2 yamt }
953 1.1 hwr break;
954 1.20 itojun case SIOCSIFMTU:
955 1.27 martin if (ifr->ifr_mtu < 576) {
956 1.1 hwr error = EINVAL;
957 1.1 hwr break;
958 1.1 hwr }
959 1.1 hwr ifp->if_mtu = ifr->ifr_mtu;
960 1.1 hwr break;
961 1.1 hwr case SIOCGIFMTU:
962 1.1 hwr ifr->ifr_mtu = sc->sc_if.if_mtu;
963 1.1 hwr break;
964 1.1 hwr case SIOCADDMULTI:
965 1.1 hwr case SIOCDELMULTI:
966 1.57.2.4 yamt if (ifr == NULL) {
967 1.1 hwr error = EAFNOSUPPORT;
968 1.1 hwr break;
969 1.1 hwr }
970 1.57.2.4 yamt switch (ifreq_getaddr(cmd, ifr)->sa_family) {
971 1.1 hwr #ifdef INET
972 1.1 hwr case AF_INET:
973 1.1 hwr break;
974 1.1 hwr #endif
975 1.56 is #ifdef INET6
976 1.56 is case AF_INET6:
977 1.56 is break;
978 1.56 is #endif
979 1.1 hwr default:
980 1.1 hwr error = EAFNOSUPPORT;
981 1.1 hwr break;
982 1.1 hwr }
983 1.1 hwr break;
984 1.1 hwr case GRESPROTO:
985 1.57.2.2 yamt oproto = sc->sc_proto;
986 1.57.2.2 yamt sc->sc_proto = ifr->ifr_flags;
987 1.57.2.2 yamt switch (sc->sc_proto) {
988 1.57.2.2 yamt case IPPROTO_UDP:
989 1.57.2.2 yamt ifp->if_flags |= IFF_LINK0|IFF_LINK2;
990 1.57.2.2 yamt if (oproto != IPPROTO_UDP)
991 1.57.2.2 yamt ifp->if_flags &= ~IFF_RUNNING;
992 1.57.2.2 yamt error = gre_kick(sc);
993 1.13 martin break;
994 1.40 itojun case IPPROTO_GRE:
995 1.3 hwr ifp->if_flags |= IFF_LINK0;
996 1.57.2.2 yamt ifp->if_flags &= ~IFF_LINK2;
997 1.57.2.2 yamt goto recompute;
998 1.40 itojun case IPPROTO_MOBILE:
999 1.57.2.2 yamt ifp->if_flags &= ~(IFF_LINK0|IFF_LINK2);
1000 1.57.2.2 yamt goto recompute;
1001 1.1 hwr default:
1002 1.40 itojun error = EPROTONOSUPPORT;
1003 1.35 itojun break;
1004 1.1 hwr }
1005 1.1 hwr break;
1006 1.1 hwr case GREGPROTO:
1007 1.57.2.2 yamt ifr->ifr_flags = sc->sc_proto;
1008 1.1 hwr break;
1009 1.1 hwr case GRESADDRS:
1010 1.1 hwr case GRESADDRD:
1011 1.1 hwr /*
1012 1.20 itojun * set tunnel endpoints, compute a less specific route
1013 1.20 itojun * to the remote end and mark if as up
1014 1.20 itojun */
1015 1.1 hwr sa = &ifr->ifr_addr;
1016 1.57.2.2 yamt if (cmd == GRESADDRS) {
1017 1.57.2.4 yamt sc->sc_src = satocsin(sa)->sin_addr;
1018 1.57.2.4 yamt sc->sc_srcport = satocsin(sa)->sin_port;
1019 1.57.2.2 yamt }
1020 1.57.2.2 yamt if (cmd == GRESADDRD) {
1021 1.57.2.2 yamt if (sc->sc_proto == IPPROTO_UDP &&
1022 1.57.2.4 yamt satocsin(sa)->sin_port == 0) {
1023 1.57.2.2 yamt error = EINVAL;
1024 1.57.2.2 yamt break;
1025 1.57.2.2 yamt }
1026 1.57.2.4 yamt sc->sc_dst = satocsin(sa)->sin_addr;
1027 1.57.2.4 yamt sc->sc_dstport = satocsin(sa)->sin_port;
1028 1.57.2.2 yamt }
1029 1.30 itojun recompute:
1030 1.57.2.2 yamt if (sc->sc_proto == IPPROTO_UDP ||
1031 1.57.2.4 yamt (sc->sc_src.s_addr != INADDR_ANY &&
1032 1.57.2.4 yamt sc->sc_dst.s_addr != INADDR_ANY)) {
1033 1.57.2.2 yamt rtcache_free(&sc->route);
1034 1.57.2.4 yamt if (sc->sc_proto == IPPROTO_UDP) {
1035 1.57.2.4 yamt if ((error = gre_kick(sc)) == 0)
1036 1.57.2.4 yamt ifp->if_flags |= IFF_RUNNING;
1037 1.57.2.4 yamt else
1038 1.57.2.4 yamt ifp->if_flags &= ~IFF_RUNNING;
1039 1.57.2.4 yamt }
1040 1.57.2.2 yamt else if (gre_compute_route(sc) == 0)
1041 1.39 itojun ifp->if_flags |= IFF_RUNNING;
1042 1.39 itojun else
1043 1.39 itojun ifp->if_flags &= ~IFF_RUNNING;
1044 1.1 hwr }
1045 1.1 hwr break;
1046 1.1 hwr case GREGADDRS:
1047 1.57.2.4 yamt sockaddr_in_init(&si, &sc->sc_src,
1048 1.57.2.4 yamt (sc->sc_proto == IPPROTO_UDP) ? sc->sc_srcport : 0);
1049 1.57.2.4 yamt ifr->ifr_addr = *sintosa(&si);
1050 1.1 hwr break;
1051 1.1 hwr case GREGADDRD:
1052 1.57.2.4 yamt sockaddr_in_init(&si, &sc->sc_dst,
1053 1.57.2.4 yamt (sc->sc_proto == IPPROTO_UDP) ? sc->sc_dstport : 0);
1054 1.57.2.4 yamt ifr->ifr_addr = *sintosa(&si);
1055 1.28 itojun break;
1056 1.57.2.2 yamt case GREDSOCK:
1057 1.57.2.4 yamt if (sc->sc_proto != IPPROTO_UDP) {
1058 1.57.2.4 yamt error = EINVAL;
1059 1.57.2.4 yamt break;
1060 1.57.2.2 yamt }
1061 1.57.2.4 yamt ifp->if_flags &= ~IFF_UP;
1062 1.57.2.4 yamt gre_wakeup(sc);
1063 1.57.2.2 yamt break;
1064 1.57.2.2 yamt case GRESSOCK:
1065 1.57.2.4 yamt if (sc->sc_proto != IPPROTO_UDP) {
1066 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1067 1.57.2.4 yamt error = EINVAL;
1068 1.57.2.2 yamt break;
1069 1.57.2.4 yamt }
1070 1.57.2.4 yamt /* getsock() will FILE_USE() and unlock the descriptor for us */
1071 1.57.2.4 yamt if ((error = getsock(p->p_fd, (int)ifr->ifr_value, &fp)) != 0) {
1072 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1073 1.57.2.4 yamt error = EINVAL;
1074 1.57.2.4 yamt break;
1075 1.57.2.4 yamt }
1076 1.57.2.2 yamt so = (struct socket *)fp->f_data;
1077 1.57.2.2 yamt if (so->so_type != SOCK_DGRAM) {
1078 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1079 1.57.2.2 yamt FILE_UNUSE(fp, NULL);
1080 1.57.2.2 yamt error = EINVAL;
1081 1.28 itojun break;
1082 1.57.2.2 yamt }
1083 1.57.2.2 yamt /* check address */
1084 1.57.2.2 yamt if ((error = gre_getnames(so, curlwp, &src, &dst)) != 0) {
1085 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1086 1.57.2.2 yamt FILE_UNUSE(fp, NULL);
1087 1.57.2.2 yamt break;
1088 1.57.2.2 yamt }
1089 1.57.2.2 yamt
1090 1.57.2.4 yamt /* Increase reference count. Now that our reference
1091 1.57.2.4 yamt * to the file descriptor is counted, this thread
1092 1.57.2.4 yamt * can release our "use" of the descriptor, but it
1093 1.57.2.4 yamt * will not be destroyed by some other thread's
1094 1.57.2.4 yamt * action. This thread needs to release its use,
1095 1.57.2.4 yamt * too, because one and only one thread can have
1096 1.57.2.4 yamt * use of the descriptor at once. The kernel thread
1097 1.57.2.4 yamt * will pick up the use if it needs it.
1098 1.57.2.4 yamt */
1099 1.57.2.2 yamt
1100 1.57.2.4 yamt fp->f_count++;
1101 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: l.%d f_count %d\n", __func__, __LINE__,
1102 1.57.2.4 yamt fp->f_count);
1103 1.57.2.4 yamt FILE_UNUSE(fp, NULL);
1104 1.57.2.4 yamt
1105 1.57.2.4 yamt while (sc->sc_newsoparm.sp_fp != NULL && error == 0) {
1106 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1107 1.57.2.4 yamt error = cv_timedwait_sig(&sc->sc_soparm_cv, &sc->sc_mtx,
1108 1.57.2.4 yamt MAX(1, hz / 2));
1109 1.57.2.4 yamt }
1110 1.57.2.4 yamt if (error == 0) {
1111 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1112 1.57.2.4 yamt sc->sc_newsoparm.sp_fp = fp;
1113 1.57.2.4 yamt ifp->if_flags |= IFF_UP;
1114 1.57.2.4 yamt }
1115 1.57.2.4 yamt
1116 1.57.2.4 yamt if (error != 0 || (error = gre_kick(sc)) != 0) {
1117 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1118 1.57.2.4 yamt gre_closef(&fp, l);
1119 1.57.2.4 yamt break;
1120 1.57.2.4 yamt }
1121 1.57.2.4 yamt /* fp does not any longer belong to this thread. */
1122 1.57.2.4 yamt sc->sc_src = src.sin_addr;
1123 1.57.2.4 yamt sc->sc_srcport = src.sin_port;
1124 1.57.2.4 yamt sc->sc_dst = dst.sin_addr;
1125 1.57.2.4 yamt sc->sc_dstport = dst.sin_port;
1126 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: sock 0x%08" PRIx32 " port %d -> "
1127 1.57.2.4 yamt "0x%08" PRIx32 " port %d\n", __func__,
1128 1.57.2.4 yamt src.sin_addr.s_addr, ntohs(src.sin_port),
1129 1.57.2.4 yamt dst.sin_addr.s_addr, ntohs(dst.sin_port));
1130 1.57.2.2 yamt break;
1131 1.57.2.2 yamt case SIOCSLIFPHYADDR:
1132 1.28 itojun if (lifr->addr.ss_family != AF_INET ||
1133 1.31 itojun lifr->dstaddr.ss_family != AF_INET) {
1134 1.31 itojun error = EAFNOSUPPORT;
1135 1.31 itojun break;
1136 1.31 itojun }
1137 1.28 itojun if (lifr->addr.ss_len != sizeof(si) ||
1138 1.31 itojun lifr->dstaddr.ss_len != sizeof(si)) {
1139 1.31 itojun error = EINVAL;
1140 1.31 itojun break;
1141 1.31 itojun }
1142 1.57.2.4 yamt sc->sc_src = satocsin(&lifr->addr)->sin_addr;
1143 1.57.2.4 yamt sc->sc_dst = satocsin(&lifr->dstaddr)->sin_addr;
1144 1.57.2.4 yamt sc->sc_srcport = satocsin(&lifr->addr)->sin_port;
1145 1.57.2.4 yamt sc->sc_dstport = satocsin(&lifr->dstaddr)->sin_port;
1146 1.30 itojun goto recompute;
1147 1.28 itojun case SIOCDIFPHYADDR:
1148 1.57.2.4 yamt sc->sc_src.s_addr = INADDR_ANY;
1149 1.57.2.4 yamt sc->sc_dst.s_addr = INADDR_ANY;
1150 1.57.2.4 yamt sc->sc_srcport = 0;
1151 1.57.2.4 yamt sc->sc_dstport = 0;
1152 1.57.2.2 yamt goto recompute;
1153 1.28 itojun case SIOCGLIFPHYADDR:
1154 1.57.2.4 yamt if (sc->sc_src.s_addr == INADDR_ANY ||
1155 1.57.2.4 yamt sc->sc_dst.s_addr == INADDR_ANY) {
1156 1.28 itojun error = EADDRNOTAVAIL;
1157 1.28 itojun break;
1158 1.28 itojun }
1159 1.57.2.4 yamt sockaddr_in_init(satosin(&lifr->addr), &sc->sc_src,
1160 1.57.2.4 yamt (sc->sc_proto == IPPROTO_UDP) ? sc->sc_srcport : 0);
1161 1.57.2.4 yamt sockaddr_in_init(satosin(&lifr->dstaddr), &sc->sc_dst,
1162 1.57.2.4 yamt (sc->sc_proto == IPPROTO_UDP) ? sc->sc_dstport : 0);
1163 1.1 hwr break;
1164 1.1 hwr default:
1165 1.1 hwr error = EINVAL;
1166 1.31 itojun break;
1167 1.1 hwr }
1168 1.57.2.4 yamt mutex_exit(&sc->sc_mtx);
1169 1.57.2.3 yamt return error;
1170 1.1 hwr }
1171 1.1 hwr
1172 1.20 itojun /*
1173 1.57.2.2 yamt * Compute a route to our destination.
1174 1.1 hwr */
1175 1.57.2.1 yamt static int
1176 1.8 explorer gre_compute_route(struct gre_softc *sc)
1177 1.1 hwr {
1178 1.1 hwr struct route *ro;
1179 1.57.2.4 yamt union {
1180 1.57.2.4 yamt struct sockaddr dst;
1181 1.57.2.4 yamt struct sockaddr_in dst4;
1182 1.57.2.4 yamt } u;
1183 1.1 hwr
1184 1.8 explorer ro = &sc->route;
1185 1.20 itojun
1186 1.57.2.4 yamt memset(ro, 0, sizeof(*ro));
1187 1.57.2.4 yamt sockaddr_in_init(&u.dst4, &sc->sc_dst, 0);
1188 1.57.2.4 yamt rtcache_setdst(ro, &u.dst);
1189 1.1 hwr
1190 1.57.2.2 yamt rtcache_init(ro);
1191 1.1 hwr
1192 1.25 martin if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
1193 1.57.2.4 yamt GRE_DPRINTF(sc, "%s: route to %s %s\n", sc->sc_if.if_xname,
1194 1.57.2.4 yamt inet_ntoa(u.dst4.sin_addr),
1195 1.57.2.4 yamt (ro->ro_rt == NULL)
1196 1.57.2.4 yamt ? "does not exist"
1197 1.57.2.4 yamt : "loops back to ourself");
1198 1.57.2.2 yamt rtcache_free(ro);
1199 1.25 martin return EADDRNOTAVAIL;
1200 1.25 martin }
1201 1.25 martin
1202 1.25 martin return 0;
1203 1.1 hwr }
1204 1.1 hwr
1205 1.3 hwr /*
1206 1.20 itojun * do a checksum of a buffer - much like in_cksum, which operates on
1207 1.20 itojun * mbufs.
1208 1.3 hwr */
1209 1.48 itojun u_int16_t
1210 1.48 itojun gre_in_cksum(u_int16_t *p, u_int len)
1211 1.3 hwr {
1212 1.48 itojun u_int32_t sum = 0;
1213 1.3 hwr int nwords = len >> 1;
1214 1.20 itojun
1215 1.3 hwr while (nwords-- != 0)
1216 1.3 hwr sum += *p++;
1217 1.20 itojun
1218 1.20 itojun if (len & 1) {
1219 1.20 itojun union {
1220 1.20 itojun u_short w;
1221 1.20 itojun u_char c[2];
1222 1.20 itojun } u;
1223 1.20 itojun u.c[0] = *(u_char *)p;
1224 1.20 itojun u.c[1] = 0;
1225 1.20 itojun sum += u.w;
1226 1.20 itojun }
1227 1.20 itojun
1228 1.20 itojun /* end-around-carry */
1229 1.20 itojun sum = (sum >> 16) + (sum & 0xffff);
1230 1.20 itojun sum += (sum >> 16);
1231 1.57.2.3 yamt return ~sum;
1232 1.3 hwr }
1233 1.54 christos #endif
1234 1.54 christos
1235 1.57.2.1 yamt void greattach(int);
1236 1.54 christos
1237 1.54 christos /* ARGSUSED */
1238 1.54 christos void
1239 1.57.2.1 yamt greattach(int count)
1240 1.54 christos {
1241 1.54 christos #ifdef INET
1242 1.54 christos LIST_INIT(&gre_softc_list);
1243 1.54 christos if_clone_attach(&gre_cloner);
1244 1.54 christos #endif
1245 1.54 christos }
1246