if_gre.c revision 1.88.2.8 1 1.88.2.8 ad /* $NetBSD: if_gre.c,v 1.88.2.8 2007/10/09 13:44:40 ad 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.1 hwr */
48 1.22 lukem
49 1.22 lukem #include <sys/cdefs.h>
50 1.88.2.8 ad __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.88.2.8 2007/10/09 13:44:40 ad Exp $");
51 1.1 hwr
52 1.62 dyoung #include "opt_gre.h"
53 1.1 hwr #include "opt_inet.h"
54 1.1 hwr #include "bpfilter.h"
55 1.1 hwr
56 1.54 christos #ifdef INET
57 1.1 hwr #include <sys/param.h>
58 1.62 dyoung #include <sys/file.h>
59 1.62 dyoung #include <sys/filedesc.h>
60 1.1 hwr #include <sys/malloc.h>
61 1.88.2.8 ad #include <sys/mallocvar.h>
62 1.1 hwr #include <sys/mbuf.h>
63 1.13 martin #include <sys/proc.h>
64 1.88.2.8 ad #include <sys/domain.h>
65 1.1 hwr #include <sys/protosw.h>
66 1.1 hwr #include <sys/socket.h>
67 1.62 dyoung #include <sys/socketvar.h>
68 1.1 hwr #include <sys/ioctl.h>
69 1.10 thorpej #include <sys/queue.h>
70 1.1 hwr #if __NetBSD__
71 1.1 hwr #include <sys/systm.h>
72 1.62 dyoung #include <sys/sysctl.h>
73 1.60 elad #include <sys/kauth.h>
74 1.1 hwr #endif
75 1.1 hwr
76 1.88.2.5 ad #include <sys/kernel.h>
77 1.88.2.5 ad #include <sys/mutex.h>
78 1.88.2.5 ad #include <sys/condvar.h>
79 1.62 dyoung #include <sys/kthread.h>
80 1.62 dyoung
81 1.1 hwr #include <machine/cpu.h>
82 1.1 hwr
83 1.1 hwr #include <net/ethertypes.h>
84 1.1 hwr #include <net/if.h>
85 1.1 hwr #include <net/if_types.h>
86 1.1 hwr #include <net/netisr.h>
87 1.1 hwr #include <net/route.h>
88 1.1 hwr
89 1.1 hwr #ifdef INET
90 1.1 hwr #include <netinet/in.h>
91 1.1 hwr #include <netinet/in_systm.h>
92 1.1 hwr #include <netinet/in_var.h>
93 1.1 hwr #include <netinet/ip.h>
94 1.1 hwr #include <netinet/ip_var.h>
95 1.1 hwr #else
96 1.4 thorpej #error "Huh? if_gre without inet?"
97 1.1 hwr #endif
98 1.1 hwr
99 1.1 hwr
100 1.1 hwr #ifdef NETATALK
101 1.1 hwr #include <netatalk/at.h>
102 1.1 hwr #include <netatalk/at_var.h>
103 1.1 hwr #include <netatalk/at_extern.h>
104 1.1 hwr #endif
105 1.1 hwr
106 1.1 hwr #if NBPFILTER > 0
107 1.1 hwr #include <sys/time.h>
108 1.1 hwr #include <net/bpf.h>
109 1.1 hwr #endif
110 1.1 hwr
111 1.1 hwr #include <net/if_gre.h>
112 1.1 hwr
113 1.88.2.7 ad #include <compat/sys/socket.h>
114 1.88.2.6 ad #include <compat/sys/sockio.h>
115 1.20 itojun /*
116 1.27 martin * It is not easy to calculate the right value for a GRE MTU.
117 1.27 martin * We leave this task to the admin and use the same default that
118 1.27 martin * other vendors use.
119 1.20 itojun */
120 1.27 martin #define GREMTU 1476
121 1.1 hwr
122 1.62 dyoung #ifdef GRE_DEBUG
123 1.88.2.8 ad int gre_debug = 0;
124 1.62 dyoung #define GRE_DPRINTF(__sc, __fmt, ...) \
125 1.62 dyoung do { \
126 1.88.2.8 ad if (__predict_false(gre_debug || \
127 1.88.2.8 ad ((__sc)->sc_if.if_flags & IFF_DEBUG) != 0)) \
128 1.62 dyoung printf(__fmt, __VA_ARGS__); \
129 1.62 dyoung } while (/*CONSTCOND*/0)
130 1.62 dyoung #else
131 1.62 dyoung #define GRE_DPRINTF(__sc, __fmt, ...) do { } while (/*CONSTCOND*/0)
132 1.62 dyoung #endif /* GRE_DEBUG */
133 1.62 dyoung
134 1.26 martin int ip_gre_ttl = GRE_TTL;
135 1.88.2.8 ad MALLOC_DEFINE(M_GRE_BUFQ, "gre_bufq", "gre mbuf queue");
136 1.1 hwr
137 1.88.2.8 ad static int gre_clone_create(struct if_clone *, int);
138 1.88.2.8 ad static int gre_clone_destroy(struct ifnet *);
139 1.10 thorpej
140 1.59 thorpej static struct if_clone gre_cloner =
141 1.10 thorpej IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
142 1.1 hwr
143 1.88.2.8 ad static int gre_input(struct gre_softc *, struct mbuf *, int,
144 1.88.2.8 ad const struct gre_h *);
145 1.88.2.8 ad static bool gre_is_nullconf(const struct gre_soparm *);
146 1.88.2.8 ad static int gre_output(struct ifnet *, struct mbuf *,
147 1.84 dyoung const struct sockaddr *, struct rtentry *);
148 1.88.2.8 ad static int gre_ioctl(struct ifnet *, u_long, void *);
149 1.88.2.5 ad static void gre_closef(struct file **, struct lwp *);
150 1.62 dyoung static int gre_getsockname(struct socket *, struct mbuf *, struct lwp *);
151 1.62 dyoung static int gre_getpeername(struct socket *, struct mbuf *, struct lwp *);
152 1.88.2.8 ad static int gre_getnames(struct socket *, struct lwp *,
153 1.88.2.8 ad struct sockaddr_storage *, struct sockaddr_storage *);
154 1.88.2.8 ad static void gre_clearconf(struct gre_soparm *, bool);
155 1.88.2.8 ad static int gre_soreceive(struct socket *, struct mbuf **);
156 1.88.2.8 ad static int gre_sosend(struct socket *, struct mbuf *, struct lwp *);
157 1.88.2.8 ad static struct socket *gre_reconf(struct gre_softc *, struct socket *, lwp_t *,
158 1.88.2.8 ad const struct gre_soparm *);
159 1.88.2.8 ad
160 1.88.2.8 ad static int
161 1.88.2.8 ad nearest_pow2(size_t len0)
162 1.88.2.8 ad {
163 1.88.2.8 ad size_t len, mid;
164 1.88.2.8 ad
165 1.88.2.8 ad if (len0 == 0)
166 1.88.2.8 ad return 1;
167 1.88.2.8 ad
168 1.88.2.8 ad for (len = len0; (len & (len - 1)) != 0; len &= len - 1)
169 1.88.2.8 ad ;
170 1.88.2.8 ad
171 1.88.2.8 ad mid = len | (len >> 1);
172 1.88.2.8 ad
173 1.88.2.8 ad /* avoid overflow */
174 1.88.2.8 ad if ((len << 1) < len)
175 1.88.2.8 ad return len;
176 1.88.2.8 ad if (len0 >= mid)
177 1.88.2.8 ad return len << 1;
178 1.88.2.8 ad return len;
179 1.88.2.8 ad }
180 1.88.2.8 ad
181 1.88.2.8 ad static struct gre_bufq *
182 1.88.2.8 ad gre_bufq_init(struct gre_bufq *bq, size_t len0)
183 1.88.2.8 ad {
184 1.88.2.8 ad size_t len;
185 1.88.2.8 ad
186 1.88.2.8 ad len = nearest_pow2(len0);
187 1.88.2.8 ad
188 1.88.2.8 ad memset(bq, 0, sizeof(*bq));
189 1.88.2.8 ad bq->bq_buf = malloc(len * sizeof(struct mbuf *), M_GRE_BUFQ, M_WAITOK);
190 1.88.2.8 ad bq->bq_len = len;
191 1.88.2.8 ad bq->bq_lenmask = len - 1;
192 1.88.2.8 ad
193 1.88.2.8 ad return bq;
194 1.88.2.8 ad }
195 1.88.2.8 ad
196 1.88.2.8 ad static bool
197 1.88.2.8 ad gre_bufq_empty(struct gre_bufq *bq)
198 1.88.2.8 ad {
199 1.88.2.8 ad return bq->bq_prodidx == bq->bq_considx;
200 1.88.2.8 ad }
201 1.88.2.8 ad
202 1.88.2.8 ad static struct mbuf *
203 1.88.2.8 ad gre_bufq_dequeue(struct gre_bufq *bq)
204 1.88.2.8 ad {
205 1.88.2.8 ad struct mbuf *m;
206 1.88.2.8 ad
207 1.88.2.8 ad if (gre_bufq_empty(bq))
208 1.88.2.8 ad return NULL;
209 1.88.2.8 ad
210 1.88.2.8 ad m = bq->bq_buf[bq->bq_considx];
211 1.88.2.8 ad bq->bq_considx = (bq->bq_considx + 1) & bq->bq_lenmask;
212 1.88.2.8 ad
213 1.88.2.8 ad return m;
214 1.88.2.8 ad }
215 1.62 dyoung
216 1.62 dyoung static void
217 1.88.2.8 ad gre_bufq_purge(struct gre_bufq *bq)
218 1.88.2.8 ad {
219 1.88.2.8 ad struct mbuf *m;
220 1.88.2.8 ad
221 1.88.2.8 ad while ((m = gre_bufq_dequeue(bq)) != NULL)
222 1.88.2.8 ad m_freem(m);
223 1.88.2.8 ad }
224 1.88.2.8 ad
225 1.88.2.8 ad static int
226 1.88.2.8 ad gre_bufq_enqueue(struct gre_bufq *bq, struct mbuf *m)
227 1.62 dyoung {
228 1.88.2.8 ad int next;
229 1.88.2.8 ad
230 1.88.2.8 ad next = (bq->bq_prodidx + 1) & bq->bq_lenmask;
231 1.88.2.8 ad
232 1.88.2.8 ad if (next == bq->bq_considx) {
233 1.88.2.8 ad bq->bq_drops++;
234 1.88.2.8 ad return ENOBUFS;
235 1.88.2.8 ad }
236 1.88.2.8 ad
237 1.88.2.8 ad bq->bq_buf[bq->bq_prodidx] = m;
238 1.88.2.8 ad bq->bq_prodidx = next;
239 1.88.2.8 ad return 0;
240 1.62 dyoung }
241 1.62 dyoung
242 1.88.2.8 ad static void
243 1.88.2.8 ad greintr(void *arg)
244 1.88.2.8 ad {
245 1.88.2.8 ad struct gre_softc *sc = (struct gre_softc *)arg;
246 1.88.2.8 ad struct socket *so = sc->sc_so;
247 1.88.2.8 ad lwp_t *l = curlwp;
248 1.88.2.8 ad int rc;
249 1.88.2.8 ad struct mbuf *m;
250 1.88.2.8 ad
251 1.88.2.8 ad KASSERT(sc->sc_so != NULL);
252 1.88.2.8 ad
253 1.88.2.8 ad sc->sc_send_ev.ev_count++;
254 1.88.2.8 ad GRE_DPRINTF(sc, "%s: enter\n", __func__);
255 1.88.2.8 ad while ((m = gre_bufq_dequeue(&sc->sc_snd)) != NULL) {
256 1.88.2.8 ad /* XXX handle ENOBUFS? */
257 1.88.2.8 ad if ((rc = gre_sosend(so, m, l)) != 0) {
258 1.88.2.8 ad GRE_DPRINTF(sc, "%s: gre_sosend failed %d\n", __func__,
259 1.88.2.8 ad rc);
260 1.88.2.8 ad }
261 1.88.2.8 ad }
262 1.88.2.8 ad }
263 1.88.2.8 ad
264 1.88.2.8 ad /* Caller must hold sc->sc_mtx. */
265 1.88.2.8 ad static void
266 1.88.2.8 ad gre_wait(struct gre_softc *sc)
267 1.88.2.8 ad {
268 1.88.2.8 ad sc->sc_waiters++;
269 1.88.2.8 ad cv_wait(&sc->sc_condvar, &sc->sc_mtx);
270 1.88.2.8 ad sc->sc_waiters--;
271 1.88.2.8 ad }
272 1.88.2.8 ad
273 1.88.2.8 ad /* Caller must hold sc->sc_mtx. */
274 1.62 dyoung static void
275 1.88.2.5 ad gre_join(struct gre_softc *sc)
276 1.62 dyoung {
277 1.88.2.8 ad while (sc->sc_waiters > 0)
278 1.88.2.8 ad cv_wait(&sc->sc_condvar, &sc->sc_mtx);
279 1.62 dyoung }
280 1.62 dyoung
281 1.62 dyoung static void
282 1.88.2.8 ad gre_evcnt_detach(struct gre_softc *sc)
283 1.62 dyoung {
284 1.88.2.8 ad evcnt_detach(&sc->sc_unsupp_ev);
285 1.88.2.8 ad evcnt_detach(&sc->sc_pullup_ev);
286 1.88.2.8 ad evcnt_detach(&sc->sc_error_ev);
287 1.88.2.8 ad evcnt_detach(&sc->sc_block_ev);
288 1.88.2.8 ad evcnt_detach(&sc->sc_recv_ev);
289 1.88.2.8 ad
290 1.88.2.8 ad evcnt_detach(&sc->sc_oflow_ev);
291 1.88.2.8 ad evcnt_detach(&sc->sc_send_ev);
292 1.88.2.8 ad }
293 1.88.2.8 ad
294 1.88.2.8 ad static void
295 1.88.2.8 ad gre_evcnt_attach(struct gre_softc *sc)
296 1.88.2.8 ad {
297 1.88.2.8 ad evcnt_attach_dynamic(&sc->sc_recv_ev, EVCNT_TYPE_MISC,
298 1.88.2.8 ad NULL, sc->sc_if.if_xname, "recv");
299 1.88.2.8 ad evcnt_attach_dynamic(&sc->sc_block_ev, EVCNT_TYPE_MISC,
300 1.88.2.8 ad &sc->sc_recv_ev, sc->sc_if.if_xname, "would block");
301 1.88.2.8 ad evcnt_attach_dynamic(&sc->sc_error_ev, EVCNT_TYPE_MISC,
302 1.88.2.8 ad &sc->sc_recv_ev, sc->sc_if.if_xname, "error");
303 1.88.2.8 ad evcnt_attach_dynamic(&sc->sc_pullup_ev, EVCNT_TYPE_MISC,
304 1.88.2.8 ad &sc->sc_recv_ev, sc->sc_if.if_xname, "pullup failed");
305 1.88.2.8 ad evcnt_attach_dynamic(&sc->sc_unsupp_ev, EVCNT_TYPE_MISC,
306 1.88.2.8 ad &sc->sc_recv_ev, sc->sc_if.if_xname, "unsupported");
307 1.88.2.8 ad
308 1.88.2.8 ad evcnt_attach_dynamic(&sc->sc_send_ev, EVCNT_TYPE_MISC,
309 1.88.2.8 ad NULL, sc->sc_if.if_xname, "send");
310 1.88.2.8 ad evcnt_attach_dynamic(&sc->sc_oflow_ev, EVCNT_TYPE_MISC,
311 1.88.2.8 ad &sc->sc_send_ev, sc->sc_if.if_xname, "overflow");
312 1.62 dyoung }
313 1.62 dyoung
314 1.59 thorpej static int
315 1.59 thorpej gre_clone_create(struct if_clone *ifc, int unit)
316 1.1 hwr {
317 1.88.2.8 ad struct gre_soparm sp;
318 1.8 explorer struct gre_softc *sc;
319 1.1 hwr
320 1.88.2.8 ad sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK|M_ZERO);
321 1.88.2.8 ad mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_SOFTNET);
322 1.88.2.8 ad cv_init(&sc->sc_condvar, "gre wait");
323 1.10 thorpej
324 1.50 itojun snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
325 1.50 itojun ifc->ifc_name, unit);
326 1.10 thorpej sc->sc_if.if_softc = sc;
327 1.51 tron sc->sc_if.if_type = IFT_TUNNEL;
328 1.34 itojun sc->sc_if.if_addrlen = 0;
329 1.88.2.8 ad sc->sc_if.if_hdrlen = sizeof(struct ip) + sizeof(struct gre_h);
330 1.15 thorpej sc->sc_if.if_dlt = DLT_NULL;
331 1.20 itojun sc->sc_if.if_mtu = GREMTU;
332 1.10 thorpej sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
333 1.10 thorpej sc->sc_if.if_output = gre_output;
334 1.10 thorpej sc->sc_if.if_ioctl = gre_ioctl;
335 1.88.2.8 ad sockaddr_copy(sstosa(&sp.sp_dst), sizeof(sp.sp_dst), sintocsa(&in_any));
336 1.88.2.8 ad sockaddr_copy(sstosa(&sp.sp_src), sizeof(sp.sp_src), sintocsa(&in_any));
337 1.88.2.8 ad sp.sp_proto = IPPROTO_GRE;
338 1.88.2.8 ad sp.sp_type = SOCK_RAW;
339 1.88.2.8 ad sp.sp_bysock = 0;
340 1.88.2.8 ad sp.sp_fd = -1;
341 1.88.2.8 ad sc->sc_soparm = sp;
342 1.88.2.8 ad
343 1.88.2.8 ad gre_evcnt_attach(sc);
344 1.88.2.8 ad
345 1.88.2.8 ad gre_bufq_init(&sc->sc_snd, 17);
346 1.35 itojun sc->sc_if.if_flags |= IFF_LINK0;
347 1.10 thorpej if_attach(&sc->sc_if);
348 1.16 thorpej if_alloc_sadl(&sc->sc_if);
349 1.1 hwr #if NBPFILTER > 0
350 1.14 thorpej bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
351 1.1 hwr #endif
352 1.88.2.8 ad sc->sc_lwp = &lwp0;
353 1.88.2.8 ad sc->sc_state = GRE_S_IDLE;
354 1.87 dyoung return 0;
355 1.10 thorpej }
356 1.1 hwr
357 1.59 thorpej static int
358 1.59 thorpej gre_clone_destroy(struct ifnet *ifp)
359 1.10 thorpej {
360 1.10 thorpej struct gre_softc *sc = ifp->if_softc;
361 1.10 thorpej
362 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
363 1.88.2.8 ad
364 1.10 thorpej #if NBPFILTER > 0
365 1.10 thorpej bpfdetach(ifp);
366 1.10 thorpej #endif
367 1.88.2.5 ad if_detach(ifp);
368 1.88.2.8 ad
369 1.88.2.5 ad mutex_enter(&sc->sc_mtx);
370 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
371 1.88.2.8 ad while (sc->sc_state != GRE_S_IDLE)
372 1.88.2.8 ad gre_wait(sc);
373 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
374 1.88.2.8 ad sc->sc_state = GRE_S_DIE;
375 1.88.2.8 ad cv_broadcast(&sc->sc_condvar);
376 1.88.2.5 ad gre_join(sc);
377 1.88.2.8 ad sc->sc_so = gre_reconf(sc, sc->sc_so, sc->sc_lwp, NULL);
378 1.88.2.5 ad mutex_exit(&sc->sc_mtx);
379 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
380 1.88.2.5 ad
381 1.88.2.8 ad cv_destroy(&sc->sc_condvar);
382 1.88.2.5 ad mutex_destroy(&sc->sc_mtx);
383 1.88.2.8 ad gre_evcnt_detach(sc);
384 1.10 thorpej free(sc, M_DEVBUF);
385 1.53 peter
386 1.87 dyoung return 0;
387 1.1 hwr }
388 1.1 hwr
389 1.62 dyoung static void
390 1.88 christos gre_receive(struct socket *so, void *arg, int waitflag)
391 1.62 dyoung {
392 1.62 dyoung struct gre_softc *sc = (struct gre_softc *)arg;
393 1.88.2.8 ad int rc;
394 1.88.2.8 ad const struct gre_h *gh;
395 1.88.2.8 ad struct mbuf *m;
396 1.62 dyoung
397 1.62 dyoung GRE_DPRINTF(sc, "%s: enter\n", __func__);
398 1.62 dyoung
399 1.88.2.8 ad sc->sc_recv_ev.ev_count++;
400 1.88.2.8 ad
401 1.88.2.8 ad rc = gre_soreceive(so, &m);
402 1.88.2.8 ad /* TBD Back off if ECONNREFUSED (indicates
403 1.88.2.8 ad * ICMP Port Unreachable)?
404 1.88.2.8 ad */
405 1.88.2.8 ad if (rc == EWOULDBLOCK) {
406 1.88.2.8 ad GRE_DPRINTF(sc, "%s: EWOULDBLOCK\n", __func__);
407 1.88.2.8 ad sc->sc_block_ev.ev_count++;
408 1.88.2.8 ad return;
409 1.88.2.8 ad } else if (rc != 0 || m == NULL) {
410 1.88.2.8 ad GRE_DPRINTF(sc, "%s: rc %d m %p\n",
411 1.88.2.8 ad sc->sc_if.if_xname, rc, (void *)m);
412 1.88.2.8 ad sc->sc_error_ev.ev_count++;
413 1.88.2.8 ad return;
414 1.88.2.8 ad }
415 1.88.2.8 ad if (m->m_len < sizeof(*gh) &&
416 1.88.2.8 ad (m = m_pullup(m, sizeof(*gh))) == NULL) {
417 1.88.2.8 ad GRE_DPRINTF(sc, "%s: m_pullup failed\n", __func__);
418 1.88.2.8 ad sc->sc_pullup_ev.ev_count++;
419 1.88.2.8 ad return;
420 1.88.2.8 ad }
421 1.88.2.8 ad gh = mtod(m, const struct gre_h *);
422 1.88.2.8 ad
423 1.88.2.8 ad if (gre_input(sc, m, 0, gh) == 0) {
424 1.88.2.8 ad sc->sc_unsupp_ev.ev_count++;
425 1.88.2.8 ad GRE_DPRINTF(sc, "%s: dropping unsupported\n", __func__);
426 1.88.2.8 ad m_freem(m);
427 1.88.2.8 ad }
428 1.62 dyoung }
429 1.62 dyoung
430 1.62 dyoung static void
431 1.88 christos gre_upcall_add(struct socket *so, void *arg)
432 1.62 dyoung {
433 1.62 dyoung /* XXX What if the kernel already set an upcall? */
434 1.88.2.8 ad KASSERT((so->so_rcv.sb_flags & SB_UPCALL) == 0);
435 1.62 dyoung so->so_upcallarg = arg;
436 1.62 dyoung so->so_upcall = gre_receive;
437 1.62 dyoung so->so_rcv.sb_flags |= SB_UPCALL;
438 1.62 dyoung }
439 1.62 dyoung
440 1.62 dyoung static void
441 1.62 dyoung gre_upcall_remove(struct socket *so)
442 1.62 dyoung {
443 1.62 dyoung so->so_rcv.sb_flags &= ~SB_UPCALL;
444 1.62 dyoung so->so_upcallarg = NULL;
445 1.62 dyoung so->so_upcall = NULL;
446 1.62 dyoung }
447 1.62 dyoung
448 1.62 dyoung static int
449 1.88.2.8 ad gre_socreate(struct gre_softc *sc, struct lwp *l,
450 1.88.2.8 ad struct gre_soparm *sp, int *fdout)
451 1.62 dyoung {
452 1.88.2.8 ad const struct protosw *pr;
453 1.88.2.8 ad int fd, rc;
454 1.62 dyoung struct mbuf *m;
455 1.88.2.8 ad struct sockaddr *sa;
456 1.88.2.8 ad sa_family_t af;
457 1.62 dyoung struct socket *so;
458 1.62 dyoung
459 1.62 dyoung GRE_DPRINTF(sc, "%s: enter\n", __func__);
460 1.88.2.8 ad
461 1.88.2.8 ad af = sp->sp_src.ss_family;
462 1.88.2.8 ad rc = fsocreate(af, &so, sp->sp_type, sp->sp_proto, l, &fd);
463 1.62 dyoung if (rc != 0) {
464 1.88.2.8 ad GRE_DPRINTF(sc, "%s: fsocreate failed\n", __func__);
465 1.62 dyoung return rc;
466 1.62 dyoung }
467 1.62 dyoung
468 1.88.2.8 ad if ((m = getsombuf(so)) == NULL) {
469 1.62 dyoung rc = ENOBUFS;
470 1.62 dyoung goto out;
471 1.62 dyoung }
472 1.88.2.8 ad sa = mtod(m, struct sockaddr *);
473 1.88.2.8 ad sockaddr_copy(sa, MIN(MLEN, sizeof(sp->sp_src)), sstosa(&sp->sp_src));
474 1.88.2.8 ad m->m_len = sp->sp_src.ss_len;
475 1.62 dyoung
476 1.88.2.8 ad #if 0
477 1.88.2.8 ad /* XXX */
478 1.62 dyoung GRE_DPRINTF(sc, "%s: bind 0x%08" PRIx32 " port %d\n", __func__,
479 1.62 dyoung sin->sin_addr.s_addr, ntohs(sin->sin_port));
480 1.88.2.8 ad #endif
481 1.62 dyoung if ((rc = sobind(so, m, l)) != 0) {
482 1.62 dyoung GRE_DPRINTF(sc, "%s: sobind failed\n", __func__);
483 1.62 dyoung goto out;
484 1.62 dyoung }
485 1.62 dyoung
486 1.88.2.8 ad if ((rc = gre_getsockname(so, m, l)) != 0) {
487 1.88.2.8 ad GRE_DPRINTF(sc, "%s: gre_getsockname\n", __func__);
488 1.88.2.8 ad goto out;
489 1.62 dyoung }
490 1.88.2.8 ad sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src), sa);
491 1.62 dyoung
492 1.88.2.8 ad sockaddr_copy(sa, MIN(MLEN, sizeof(sp->sp_dst)), sstosa(&sp->sp_dst));
493 1.88.2.8 ad m->m_len = sp->sp_dst.ss_len;
494 1.62 dyoung
495 1.68 dyoung if ((rc = soconnect(so, m, l)) != 0) {
496 1.62 dyoung GRE_DPRINTF(sc, "%s: soconnect failed\n", __func__);
497 1.62 dyoung goto out;
498 1.62 dyoung }
499 1.62 dyoung
500 1.88.2.8 ad /* XXX convert to a (new) SOL_SOCKET call */
501 1.62 dyoung *mtod(m, int *) = ip_gre_ttl;
502 1.76 dyoung m->m_len = sizeof(int);
503 1.88.2.8 ad pr = so->so_proto;
504 1.88.2.8 ad KASSERT(pr != NULL);
505 1.88.2.8 ad rc = sosetopt(so, IPPROTO_IP, IP_TTL, m);
506 1.62 dyoung m = NULL;
507 1.62 dyoung if (rc != 0) {
508 1.88.2.8 ad GRE_DPRINTF(sc, "%s: sosetopt ttl failed\n", __func__);
509 1.88.2.8 ad rc = 0;
510 1.88.2.8 ad }
511 1.88.2.8 ad rc = sosetopt(so, SOL_SOCKET, SO_NOHEADER, m_intopt(so, 1));
512 1.88.2.8 ad if (rc != 0) {
513 1.88.2.8 ad GRE_DPRINTF(sc, "%s: sosetopt SO_NOHEADER failed\n", __func__);
514 1.62 dyoung rc = 0;
515 1.62 dyoung }
516 1.62 dyoung out:
517 1.62 dyoung m_freem(m);
518 1.62 dyoung
519 1.62 dyoung if (rc != 0)
520 1.88.2.8 ad fdrelease(l, fd);
521 1.88.2.8 ad else
522 1.88.2.8 ad *fdout = fd;
523 1.62 dyoung
524 1.62 dyoung return rc;
525 1.62 dyoung }
526 1.62 dyoung
527 1.88.2.8 ad static int
528 1.88.2.8 ad gre_sosend(struct socket *so, struct mbuf *top, struct lwp *l)
529 1.62 dyoung {
530 1.88.2.8 ad struct mbuf **mp;
531 1.88.2.8 ad struct proc *p;
532 1.88.2.8 ad long space, resid;
533 1.88.2.8 ad int error, s;
534 1.62 dyoung
535 1.88.2.8 ad p = l->l_proc;
536 1.62 dyoung
537 1.88.2.8 ad resid = top->m_pkthdr.len;
538 1.88.2.8 ad if (p)
539 1.88.2.8 ad p->p_stats->p_ru.ru_msgsnd++;
540 1.88.2.8 ad #define snderr(errno) { error = errno; splx(s); goto release; }
541 1.62 dyoung
542 1.88.2.8 ad if ((error = sblock(&so->so_snd, M_NOWAIT)) != 0)
543 1.88.2.8 ad goto out;
544 1.88.2.8 ad s = splsoftnet();
545 1.88.2.8 ad if (so->so_state & SS_CANTSENDMORE)
546 1.88.2.8 ad snderr(EPIPE);
547 1.88.2.8 ad if (so->so_error) {
548 1.88.2.8 ad error = so->so_error;
549 1.88.2.8 ad so->so_error = 0;
550 1.88.2.8 ad splx(s);
551 1.88.2.8 ad goto release;
552 1.88.2.8 ad }
553 1.88.2.8 ad if ((so->so_state & SS_ISCONNECTED) == 0) {
554 1.88.2.8 ad if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
555 1.88.2.8 ad if ((so->so_state & SS_ISCONFIRMING) == 0)
556 1.88.2.8 ad snderr(ENOTCONN);
557 1.88.2.8 ad } else
558 1.88.2.8 ad snderr(EDESTADDRREQ);
559 1.88.2.8 ad }
560 1.88.2.8 ad space = sbspace(&so->so_snd);
561 1.88.2.8 ad if (resid > so->so_snd.sb_hiwat)
562 1.88.2.8 ad snderr(EMSGSIZE);
563 1.88.2.8 ad if (space < resid)
564 1.88.2.8 ad snderr(EWOULDBLOCK);
565 1.88.2.8 ad splx(s);
566 1.88.2.8 ad mp = ⊤
567 1.88.2.8 ad /*
568 1.88.2.8 ad * Data is prepackaged in "top".
569 1.88.2.8 ad */
570 1.88.2.8 ad s = splsoftnet();
571 1.88.2.8 ad
572 1.88.2.8 ad if (so->so_state & SS_CANTSENDMORE)
573 1.88.2.8 ad snderr(EPIPE);
574 1.88.2.8 ad
575 1.88.2.8 ad error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, top, NULL, NULL, l);
576 1.88.2.8 ad splx(s);
577 1.88.2.8 ad
578 1.88.2.8 ad top = NULL;
579 1.88.2.8 ad mp = ⊤
580 1.88.2.8 ad if (error != 0)
581 1.88.2.8 ad goto release;
582 1.88.2.8 ad
583 1.88.2.8 ad release:
584 1.88.2.8 ad sbunlock(&so->so_snd);
585 1.88.2.8 ad out:
586 1.88.2.8 ad if (top != NULL)
587 1.88.2.8 ad m_freem(top);
588 1.88.2.8 ad return error;
589 1.88.2.8 ad }
590 1.62 dyoung
591 1.88.2.8 ad /* This is a stripped-down version of soreceive() that will never
592 1.88.2.8 ad * block. It will support SOCK_DGRAM sockets. It may also support
593 1.88.2.8 ad * SOCK_SEQPACKET sockets.
594 1.88.2.8 ad */
595 1.88.2.8 ad static int
596 1.88.2.8 ad gre_soreceive(struct socket *so, struct mbuf **mp0)
597 1.88.2.8 ad {
598 1.88.2.8 ad struct lwp *l = curlwp;
599 1.88.2.8 ad struct mbuf *m, **mp;
600 1.88.2.8 ad int flags, len, error, s, type;
601 1.88.2.8 ad const struct protosw *pr;
602 1.88.2.8 ad struct mbuf *nextrecord;
603 1.62 dyoung
604 1.88.2.8 ad KASSERT(mp0 != NULL);
605 1.88.2.8 ad
606 1.88.2.8 ad flags = MSG_DONTWAIT;
607 1.88.2.8 ad pr = so->so_proto;
608 1.88.2.8 ad mp = mp0;
609 1.88.2.8 ad type = 0;
610 1.88.2.8 ad
611 1.88.2.8 ad *mp = NULL;
612 1.88.2.8 ad
613 1.88.2.8 ad KASSERT(pr->pr_flags & PR_ATOMIC);
614 1.88.2.8 ad
615 1.88.2.8 ad if (so->so_state & SS_ISCONFIRMING)
616 1.88.2.8 ad (*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, l);
617 1.88.2.8 ad
618 1.88.2.8 ad restart:
619 1.88.2.8 ad if ((error = sblock(&so->so_rcv, M_NOWAIT)) != 0)
620 1.88.2.8 ad return error;
621 1.88.2.8 ad s = splsoftnet();
622 1.88.2.8 ad
623 1.88.2.8 ad m = so->so_rcv.sb_mb;
624 1.88.2.8 ad /*
625 1.88.2.8 ad * If we have less data than requested, do not block awaiting more.
626 1.88.2.8 ad */
627 1.88.2.8 ad if (m == NULL) {
628 1.88.2.8 ad #ifdef DIAGNOSTIC
629 1.88.2.8 ad if (so->so_rcv.sb_cc)
630 1.88.2.8 ad panic("receive 1");
631 1.88.2.8 ad #endif
632 1.88.2.8 ad if (so->so_error) {
633 1.88.2.8 ad error = so->so_error;
634 1.88.2.8 ad so->so_error = 0;
635 1.88.2.8 ad } else if (so->so_state & SS_CANTRCVMORE)
636 1.88.2.8 ad ;
637 1.88.2.8 ad else if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0
638 1.88.2.8 ad && (so->so_proto->pr_flags & PR_CONNREQUIRED))
639 1.88.2.8 ad error = ENOTCONN;
640 1.88.2.8 ad else
641 1.88.2.8 ad error = EWOULDBLOCK;
642 1.88.2.8 ad goto release;
643 1.88.2.8 ad }
644 1.88.2.8 ad /*
645 1.88.2.8 ad * On entry here, m points to the first record of the socket buffer.
646 1.88.2.8 ad * While we process the initial mbufs containing address and control
647 1.88.2.8 ad * info, we save a copy of m->m_nextpkt into nextrecord.
648 1.88.2.8 ad */
649 1.88.2.8 ad if (l != NULL)
650 1.88.2.8 ad l->l_proc->p_stats->p_ru.ru_msgrcv++;
651 1.88.2.8 ad KASSERT(m == so->so_rcv.sb_mb);
652 1.88.2.8 ad SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
653 1.88.2.8 ad SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
654 1.88.2.8 ad nextrecord = m->m_nextpkt;
655 1.88.2.8 ad if (pr->pr_flags & PR_ADDR) {
656 1.88.2.8 ad #ifdef DIAGNOSTIC
657 1.88.2.8 ad if (m->m_type != MT_SONAME)
658 1.88.2.8 ad panic("receive 1a");
659 1.88.2.8 ad #endif
660 1.88.2.8 ad sbfree(&so->so_rcv, m);
661 1.88.2.8 ad MFREE(m, so->so_rcv.sb_mb);
662 1.88.2.8 ad m = so->so_rcv.sb_mb;
663 1.88.2.8 ad }
664 1.88.2.8 ad while (m != NULL && m->m_type == MT_CONTROL && error == 0) {
665 1.88.2.8 ad sbfree(&so->so_rcv, m);
666 1.88.2.8 ad /*
667 1.88.2.8 ad * Dispose of any SCM_RIGHTS message that went
668 1.88.2.8 ad * through the read path rather than recv.
669 1.88.2.8 ad */
670 1.88.2.8 ad if (pr->pr_domain->dom_dispose &&
671 1.88.2.8 ad mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
672 1.88.2.8 ad (*pr->pr_domain->dom_dispose)(m);
673 1.88.2.8 ad MFREE(m, so->so_rcv.sb_mb);
674 1.88.2.8 ad m = so->so_rcv.sb_mb;
675 1.88.2.8 ad }
676 1.88.2.8 ad
677 1.88.2.8 ad /*
678 1.88.2.8 ad * If m is non-NULL, we have some data to read. From now on,
679 1.88.2.8 ad * make sure to keep sb_lastrecord consistent when working on
680 1.88.2.8 ad * the last packet on the chain (nextrecord == NULL) and we
681 1.88.2.8 ad * change m->m_nextpkt.
682 1.88.2.8 ad */
683 1.88.2.8 ad if (m != NULL) {
684 1.88.2.8 ad m->m_nextpkt = nextrecord;
685 1.88.2.8 ad /*
686 1.88.2.8 ad * If nextrecord == NULL (this is a single chain),
687 1.88.2.8 ad * then sb_lastrecord may not be valid here if m
688 1.88.2.8 ad * was changed earlier.
689 1.88.2.8 ad */
690 1.88.2.8 ad if (nextrecord == NULL) {
691 1.88.2.8 ad KASSERT(so->so_rcv.sb_mb == m);
692 1.88.2.8 ad so->so_rcv.sb_lastrecord = m;
693 1.88.2.8 ad }
694 1.88.2.8 ad type = m->m_type;
695 1.88.2.8 ad if (type == MT_OOBDATA)
696 1.88.2.8 ad flags |= MSG_OOB;
697 1.88.2.8 ad } else {
698 1.88.2.8 ad KASSERT(so->so_rcv.sb_mb == m);
699 1.88.2.8 ad so->so_rcv.sb_mb = nextrecord;
700 1.88.2.8 ad SB_EMPTY_FIXUP(&so->so_rcv);
701 1.88.2.8 ad }
702 1.88.2.8 ad SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
703 1.88.2.8 ad SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
704 1.88.2.8 ad
705 1.88.2.8 ad while (m != NULL) {
706 1.88.2.8 ad if (m->m_type == MT_OOBDATA) {
707 1.88.2.8 ad if (type != MT_OOBDATA)
708 1.88.2.8 ad break;
709 1.88.2.8 ad } else if (type == MT_OOBDATA)
710 1.62 dyoung break;
711 1.88.2.8 ad #ifdef DIAGNOSTIC
712 1.88.2.8 ad else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
713 1.88.2.8 ad panic("receive 3");
714 1.88.2.8 ad #endif
715 1.88.2.8 ad so->so_state &= ~SS_RCVATMARK;
716 1.88.2.8 ad if (so->so_oobmark != 0 && so->so_oobmark < m->m_len)
717 1.62 dyoung break;
718 1.88.2.8 ad len = m->m_len;
719 1.88.2.8 ad /*
720 1.88.2.8 ad * mp is set, just pass back the mbufs.
721 1.88.2.8 ad * Sockbuf must be consistent here (points to current mbuf,
722 1.88.2.8 ad * it points to next record) when we drop priority;
723 1.88.2.8 ad * we must note any additions to the sockbuf when we
724 1.88.2.8 ad * block interrupts again.
725 1.88.2.8 ad */
726 1.88.2.8 ad if (m->m_flags & M_EOR)
727 1.88.2.8 ad flags |= MSG_EOR;
728 1.88.2.8 ad nextrecord = m->m_nextpkt;
729 1.88.2.8 ad sbfree(&so->so_rcv, m);
730 1.88.2.8 ad *mp = m;
731 1.88.2.8 ad mp = &m->m_next;
732 1.88.2.8 ad so->so_rcv.sb_mb = m = m->m_next;
733 1.88.2.8 ad *mp = NULL;
734 1.88.2.8 ad /*
735 1.88.2.8 ad * If m != NULL, we also know that
736 1.88.2.8 ad * so->so_rcv.sb_mb != NULL.
737 1.88.2.8 ad */
738 1.88.2.8 ad KASSERT(so->so_rcv.sb_mb == m);
739 1.88.2.8 ad if (m) {
740 1.88.2.8 ad m->m_nextpkt = nextrecord;
741 1.88.2.8 ad if (nextrecord == NULL)
742 1.88.2.8 ad so->so_rcv.sb_lastrecord = m;
743 1.88.2.8 ad } else {
744 1.88.2.8 ad so->so_rcv.sb_mb = nextrecord;
745 1.88.2.8 ad SB_EMPTY_FIXUP(&so->so_rcv);
746 1.62 dyoung }
747 1.88.2.8 ad SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
748 1.88.2.8 ad SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
749 1.88.2.8 ad if (so->so_oobmark) {
750 1.88.2.8 ad so->so_oobmark -= len;
751 1.88.2.8 ad if (so->so_oobmark == 0) {
752 1.88.2.8 ad so->so_state |= SS_RCVATMARK;
753 1.62 dyoung break;
754 1.62 dyoung }
755 1.62 dyoung }
756 1.88.2.8 ad if (flags & MSG_EOR)
757 1.88.2.8 ad break;
758 1.62 dyoung }
759 1.88.2.8 ad
760 1.88.2.8 ad if (m != NULL) {
761 1.88.2.8 ad m_freem(*mp);
762 1.88.2.8 ad *mp = NULL;
763 1.88.2.8 ad error = ENOMEM;
764 1.88.2.8 ad (void) sbdroprecord(&so->so_rcv);
765 1.88.2.8 ad } else {
766 1.88.2.8 ad /*
767 1.88.2.8 ad * First part is an inline SB_EMPTY_FIXUP(). Second
768 1.88.2.8 ad * part makes sure sb_lastrecord is up-to-date if
769 1.88.2.8 ad * there is still data in the socket buffer.
770 1.88.2.8 ad */
771 1.88.2.8 ad so->so_rcv.sb_mb = nextrecord;
772 1.88.2.8 ad if (so->so_rcv.sb_mb == NULL) {
773 1.88.2.8 ad so->so_rcv.sb_mbtail = NULL;
774 1.88.2.8 ad so->so_rcv.sb_lastrecord = NULL;
775 1.88.2.8 ad } else if (nextrecord->m_nextpkt == NULL)
776 1.88.2.8 ad so->so_rcv.sb_lastrecord = nextrecord;
777 1.88.2.8 ad }
778 1.88.2.8 ad SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
779 1.88.2.8 ad SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
780 1.88.2.8 ad if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
781 1.88.2.8 ad (*pr->pr_usrreq)(so, PRU_RCVD, NULL,
782 1.88.2.8 ad (struct mbuf *)(long)flags, NULL, l);
783 1.88.2.8 ad if (*mp0 == NULL && (flags & MSG_EOR) == 0 &&
784 1.88.2.8 ad (so->so_state & SS_CANTRCVMORE) == 0) {
785 1.88.2.8 ad sbunlock(&so->so_rcv);
786 1.88.2.8 ad splx(s);
787 1.88.2.8 ad goto restart;
788 1.88.2.8 ad }
789 1.88.2.8 ad
790 1.88.2.8 ad release:
791 1.88.2.8 ad sbunlock(&so->so_rcv);
792 1.88.2.8 ad splx(s);
793 1.88.2.8 ad return error;
794 1.62 dyoung }
795 1.62 dyoung
796 1.88.2.8 ad static struct socket *
797 1.88.2.8 ad gre_reconf(struct gre_softc *sc, struct socket *so, lwp_t *l,
798 1.88.2.8 ad const struct gre_soparm *newsoparm)
799 1.62 dyoung {
800 1.88.2.8 ad int rc;
801 1.88.2.8 ad struct file *fp;
802 1.88.2.8 ad struct ifnet *ifp = &sc->sc_if;
803 1.88.2.8 ad
804 1.88.2.8 ad GRE_DPRINTF(sc, "%s: enter\n", __func__);
805 1.88.2.8 ad
806 1.88.2.8 ad shutdown:
807 1.88.2.8 ad if (sc->sc_soparm.sp_fd != -1) {
808 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
809 1.88.2.8 ad gre_upcall_remove(so);
810 1.88.2.8 ad softintr_disestablish(sc->sc_si);
811 1.88.2.8 ad sc->sc_si = NULL;
812 1.88.2.8 ad mutex_exit(&sc->sc_mtx);
813 1.88.2.8 ad fdrelease(l, sc->sc_soparm.sp_fd);
814 1.88.2.8 ad mutex_enter(&sc->sc_mtx);
815 1.88.2.8 ad gre_clearconf(&sc->sc_soparm, false);
816 1.88.2.8 ad sc->sc_soparm.sp_fd = -1;
817 1.88.2.8 ad so = NULL;
818 1.88.2.8 ad }
819 1.88.2.8 ad
820 1.88.2.8 ad if (newsoparm != NULL) {
821 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
822 1.88.2.8 ad sc->sc_soparm = *newsoparm;
823 1.88.2.8 ad }
824 1.88.2.8 ad
825 1.88.2.8 ad if (sc->sc_soparm.sp_fd != -1) {
826 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
827 1.88.2.8 ad rc = getsock(l->l_proc->p_fd, sc->sc_soparm.sp_fd, &fp);
828 1.88.2.8 ad if (rc != 0)
829 1.88.2.8 ad goto shutdown;
830 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
831 1.88.2.8 ad FILE_UNUSE(fp, NULL);
832 1.88.2.8 ad so = (struct socket *)fp->f_data;
833 1.88.2.8 ad sc->sc_si = softintr_establish(IPL_SOFTNET, greintr, sc);
834 1.88.2.8 ad gre_upcall_add(so, sc);
835 1.88.2.8 ad if ((ifp->if_flags & IFF_UP) == 0) {
836 1.88.2.8 ad GRE_DPRINTF(sc, "%s: down\n", __func__);
837 1.88.2.8 ad goto shutdown;
838 1.88.2.8 ad }
839 1.88.2.8 ad }
840 1.62 dyoung
841 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
842 1.88.2.8 ad if (so != NULL)
843 1.88.2.8 ad sc->sc_if.if_flags |= IFF_RUNNING;
844 1.88.2.8 ad else {
845 1.88.2.8 ad gre_bufq_purge(&sc->sc_snd);
846 1.88.2.8 ad sc->sc_if.if_flags &= ~IFF_RUNNING;
847 1.88.2.8 ad }
848 1.88.2.8 ad return so;
849 1.62 dyoung }
850 1.62 dyoung
851 1.88.2.8 ad static int
852 1.88.2.8 ad gre_input(struct gre_softc *sc, struct mbuf *m, int hlen,
853 1.88.2.8 ad const struct gre_h *gh)
854 1.62 dyoung {
855 1.62 dyoung u_int16_t flags;
856 1.88.2.8 ad u_int32_t af; /* af passed to BPF tap */
857 1.88.2.8 ad int isr, s;
858 1.62 dyoung struct ifqueue *ifq;
859 1.62 dyoung
860 1.62 dyoung sc->sc_if.if_ipackets++;
861 1.62 dyoung sc->sc_if.if_ibytes += m->m_pkthdr.len;
862 1.62 dyoung
863 1.88.2.3 ad hlen += sizeof(struct gre_h);
864 1.62 dyoung
865 1.88.2.3 ad /* process GRE flags as packet can be of variable len */
866 1.88.2.3 ad flags = ntohs(gh->flags);
867 1.62 dyoung
868 1.88.2.3 ad /* Checksum & Offset are present */
869 1.88.2.3 ad if ((flags & GRE_CP) | (flags & GRE_RP))
870 1.88.2.3 ad hlen += 4;
871 1.88.2.3 ad /* We don't support routing fields (variable length) */
872 1.88.2.3 ad if (flags & GRE_RP) {
873 1.88.2.3 ad sc->sc_if.if_ierrors++;
874 1.88.2.3 ad return 0;
875 1.88.2.3 ad }
876 1.88.2.3 ad if (flags & GRE_KP)
877 1.88.2.3 ad hlen += 4;
878 1.88.2.3 ad if (flags & GRE_SP)
879 1.88.2.3 ad hlen += 4;
880 1.88.2.3 ad
881 1.88.2.3 ad switch (ntohs(gh->ptype)) { /* ethertypes */
882 1.88.2.8 ad case ETHERTYPE_IP:
883 1.88.2.8 ad ifq = &ipintrq;
884 1.88.2.3 ad isr = NETISR_IP;
885 1.88.2.8 ad af = AF_INET;
886 1.88.2.3 ad break;
887 1.62 dyoung #ifdef NETATALK
888 1.88.2.3 ad case ETHERTYPE_ATALK:
889 1.88.2.3 ad ifq = &atintrq1;
890 1.88.2.3 ad isr = NETISR_ATALK;
891 1.88.2.3 ad af = AF_APPLETALK;
892 1.88.2.3 ad break;
893 1.62 dyoung #endif
894 1.62 dyoung #ifdef INET6
895 1.88.2.3 ad case ETHERTYPE_IPV6:
896 1.88.2.3 ad ifq = &ip6intrq;
897 1.88.2.3 ad isr = NETISR_IPV6;
898 1.88.2.3 ad af = AF_INET6;
899 1.62 dyoung break;
900 1.88.2.3 ad #endif
901 1.88.2.3 ad default: /* others not yet supported */
902 1.88.2.3 ad GRE_DPRINTF(sc, "%s: unhandled ethertype 0x%04x\n", __func__,
903 1.88.2.3 ad ntohs(gh->ptype));
904 1.88.2.3 ad sc->sc_if.if_noproto++;
905 1.87 dyoung return 0;
906 1.62 dyoung }
907 1.62 dyoung
908 1.62 dyoung if (hlen > m->m_pkthdr.len) {
909 1.62 dyoung m_freem(m);
910 1.62 dyoung sc->sc_if.if_ierrors++;
911 1.87 dyoung return EINVAL;
912 1.62 dyoung }
913 1.62 dyoung m_adj(m, hlen);
914 1.62 dyoung
915 1.62 dyoung #if NBPFILTER > 0
916 1.62 dyoung if (sc->sc_if.if_bpf != NULL)
917 1.62 dyoung bpf_mtap_af(sc->sc_if.if_bpf, af, m);
918 1.62 dyoung #endif /*NBPFILTER > 0*/
919 1.62 dyoung
920 1.62 dyoung m->m_pkthdr.rcvif = &sc->sc_if;
921 1.62 dyoung
922 1.88.2.8 ad s = splnet();
923 1.62 dyoung if (IF_QFULL(ifq)) {
924 1.62 dyoung IF_DROP(ifq);
925 1.62 dyoung m_freem(m);
926 1.62 dyoung } else {
927 1.62 dyoung IF_ENQUEUE(ifq, m);
928 1.62 dyoung }
929 1.62 dyoung /* we need schednetisr since the address family may change */
930 1.62 dyoung schednetisr(isr);
931 1.88.2.8 ad splx(s);
932 1.62 dyoung
933 1.87 dyoung return 1; /* packet is done, no further processing needed */
934 1.62 dyoung }
935 1.62 dyoung
936 1.20 itojun /*
937 1.1 hwr * The output routine. Takes a packet and encapsulates it in the protocol
938 1.88.2.8 ad * given by sc->sc_soparm.sp_proto. See also RFC 1701 and RFC 2004
939 1.1 hwr */
940 1.59 thorpej static int
941 1.84 dyoung gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
942 1.74 christos struct rtentry *rt)
943 1.1 hwr {
944 1.88.2.8 ad int error = 0;
945 1.10 thorpej struct gre_softc *sc = ifp->if_softc;
946 1.62 dyoung struct gre_h *gh;
947 1.88.2.8 ad struct ip *ip;
948 1.56 is u_int8_t ip_tos = 0;
949 1.48 itojun u_int16_t etype = 0;
950 1.24 martin
951 1.88.2.8 ad if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
952 1.37 itojun m_freem(m);
953 1.38 itojun error = ENETDOWN;
954 1.38 itojun goto end;
955 1.37 itojun }
956 1.1 hwr
957 1.88.2.8 ad #if NBPFILTER > 0
958 1.88.2.8 ad if (ifp->if_bpf != NULL)
959 1.52 christos bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
960 1.1 hwr #endif
961 1.1 hwr
962 1.26 martin m->m_flags &= ~(M_BCAST|M_MCAST);
963 1.1 hwr
964 1.88.2.8 ad GRE_DPRINTF(sc, "%s: dst->sa_family=%d\n", __func__, dst->sa_family);
965 1.88.2.8 ad switch (dst->sa_family) {
966 1.88.2.8 ad case AF_INET:
967 1.88.2.3 ad ip = mtod(m, struct ip *);
968 1.88.2.8 ad ip_tos = ip->ip_tos;
969 1.88.2.8 ad etype = htons(ETHERTYPE_IP);
970 1.62 dyoung break;
971 1.1 hwr #ifdef NETATALK
972 1.88.2.8 ad case AF_APPLETALK:
973 1.88.2.8 ad etype = htons(ETHERTYPE_ATALK);
974 1.88.2.8 ad break;
975 1.1 hwr #endif
976 1.56 is #ifdef INET6
977 1.88.2.8 ad case AF_INET6:
978 1.88.2.8 ad etype = htons(ETHERTYPE_IPV6);
979 1.62 dyoung break;
980 1.88.2.8 ad #endif
981 1.62 dyoung default:
982 1.1 hwr IF_DROP(&ifp->if_snd);
983 1.1 hwr m_freem(m);
984 1.88.2.8 ad error = EAFNOSUPPORT;
985 1.38 itojun goto end;
986 1.1 hwr }
987 1.1 hwr
988 1.88.2.8 ad M_PREPEND(m, sizeof(*gh), M_DONTWAIT);
989 1.62 dyoung
990 1.62 dyoung if (m == NULL) {
991 1.1 hwr IF_DROP(&ifp->if_snd);
992 1.38 itojun error = ENOBUFS;
993 1.38 itojun goto end;
994 1.1 hwr }
995 1.1 hwr
996 1.88.2.8 ad gh = mtod(m, struct gre_h *);
997 1.88.2.8 ad gh->flags = 0;
998 1.88.2.8 ad gh->ptype = etype;
999 1.88.2.8 ad /* XXX Need to handle IP ToS. Look at how I handle IP TTL. */
1000 1.1 hwr
1001 1.1 hwr ifp->if_opackets++;
1002 1.8 explorer ifp->if_obytes += m->m_pkthdr.len;
1003 1.62 dyoung
1004 1.1 hwr /* send it off */
1005 1.88.2.8 ad if ((error = gre_bufq_enqueue(&sc->sc_snd, m)) != 0) {
1006 1.88.2.8 ad sc->sc_oflow_ev.ev_count++;
1007 1.88.2.5 ad m_freem(m);
1008 1.88.2.5 ad } else
1009 1.88.2.8 ad softintr_schedule(sc->sc_si);
1010 1.38 itojun end:
1011 1.8 explorer if (error)
1012 1.1 hwr ifp->if_oerrors++;
1013 1.87 dyoung return error;
1014 1.1 hwr }
1015 1.1 hwr
1016 1.88.2.8 ad /* Caller must hold sc->sc_mtx. */
1017 1.62 dyoung static int
1018 1.62 dyoung gre_getname(struct socket *so, int req, struct mbuf *nam, struct lwp *l)
1019 1.62 dyoung {
1020 1.88.2.5 ad return (*so->so_proto->pr_usrreq)(so, req, NULL, nam, NULL, l);
1021 1.62 dyoung }
1022 1.62 dyoung
1023 1.88.2.8 ad /* Caller must hold sc->sc_mtx. */
1024 1.62 dyoung static int
1025 1.62 dyoung gre_getsockname(struct socket *so, struct mbuf *nam, struct lwp *l)
1026 1.62 dyoung {
1027 1.62 dyoung return gre_getname(so, PRU_SOCKADDR, nam, l);
1028 1.62 dyoung }
1029 1.62 dyoung
1030 1.88.2.8 ad /* Caller must hold sc->sc_mtx. */
1031 1.62 dyoung static int
1032 1.62 dyoung gre_getpeername(struct socket *so, struct mbuf *nam, struct lwp *l)
1033 1.62 dyoung {
1034 1.62 dyoung return gre_getname(so, PRU_PEERADDR, nam, l);
1035 1.62 dyoung }
1036 1.62 dyoung
1037 1.88.2.8 ad /* Caller must hold sc->sc_mtx. */
1038 1.62 dyoung static int
1039 1.88.2.8 ad gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_storage *src,
1040 1.88.2.8 ad struct sockaddr_storage *dst)
1041 1.62 dyoung {
1042 1.62 dyoung struct mbuf *m;
1043 1.88.2.8 ad struct sockaddr_storage *ss;
1044 1.62 dyoung int rc;
1045 1.62 dyoung
1046 1.88.2.8 ad if ((m = getsombuf(so)) == NULL)
1047 1.62 dyoung return ENOBUFS;
1048 1.62 dyoung
1049 1.88.2.8 ad ss = mtod(m, struct sockaddr_storage *);
1050 1.62 dyoung
1051 1.62 dyoung if ((rc = gre_getsockname(so, m, l)) != 0)
1052 1.62 dyoung goto out;
1053 1.88.2.8 ad *src = *ss;
1054 1.62 dyoung
1055 1.62 dyoung if ((rc = gre_getpeername(so, m, l)) != 0)
1056 1.62 dyoung goto out;
1057 1.88.2.8 ad *dst = *ss;
1058 1.62 dyoung
1059 1.62 dyoung out:
1060 1.62 dyoung m_freem(m);
1061 1.62 dyoung return rc;
1062 1.62 dyoung }
1063 1.62 dyoung
1064 1.88.2.5 ad static void
1065 1.88.2.5 ad gre_closef(struct file **fpp, struct lwp *l)
1066 1.88.2.5 ad {
1067 1.88.2.5 ad struct file *fp = *fpp;
1068 1.88.2.5 ad
1069 1.88.2.6 ad mutex_enter(&fp->f_lock);
1070 1.88.2.5 ad FILE_USE(fp);
1071 1.88.2.5 ad closef(fp, l);
1072 1.88.2.5 ad *fpp = NULL;
1073 1.88.2.5 ad }
1074 1.88.2.5 ad
1075 1.59 thorpej static int
1076 1.88 christos gre_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1077 1.1 hwr {
1078 1.62 dyoung u_char oproto;
1079 1.88.2.5 ad struct file *fp;
1080 1.62 dyoung struct socket *so;
1081 1.62 dyoung struct sockaddr_in dst, src;
1082 1.62 dyoung struct proc *p = curproc; /* XXX */
1083 1.61 ad struct lwp *l = curlwp; /* XXX */
1084 1.88.2.6 ad struct ifreq *ifr;
1085 1.28 itojun struct if_laddrreq *lifr = (struct if_laddrreq *)data;
1086 1.8 explorer struct gre_softc *sc = ifp->if_softc;
1087 1.1 hwr struct sockaddr_in si;
1088 1.8 explorer struct sockaddr *sa = NULL;
1089 1.80 christos int error = 0;
1090 1.88.2.6 ad #ifdef COMPAT_OIFREQ
1091 1.88.2.6 ad u_long ocmd = cmd;
1092 1.88.2.6 ad struct oifreq *oifr = NULL;
1093 1.88.2.6 ad struct ifreq ifrb;
1094 1.88.2.6 ad
1095 1.88.2.7 ad cmd = compat_cvtcmd(cmd);
1096 1.88.2.6 ad if (cmd != ocmd) {
1097 1.88.2.6 ad oifr = data;
1098 1.88.2.6 ad data = ifr = &ifrb;
1099 1.88.2.6 ad ifreqo2n(oifr, ifr);
1100 1.88.2.6 ad } else
1101 1.88.2.6 ad #endif
1102 1.88.2.6 ad ifr = data;
1103 1.88.2.6 ad
1104 1.61 ad switch (cmd) {
1105 1.61 ad case SIOCSIFFLAGS:
1106 1.61 ad case SIOCSIFMTU:
1107 1.61 ad case GRESPROTO:
1108 1.61 ad case GRESADDRD:
1109 1.61 ad case GRESADDRS:
1110 1.62 dyoung case GRESSOCK:
1111 1.62 dyoung case GREDSOCK:
1112 1.61 ad case SIOCSLIFPHYADDR:
1113 1.61 ad case SIOCDIFPHYADDR:
1114 1.70 elad if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
1115 1.70 elad KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1116 1.70 elad NULL) != 0)
1117 1.87 dyoung return EPERM;
1118 1.61 ad break;
1119 1.61 ad default:
1120 1.61 ad break;
1121 1.61 ad }
1122 1.1 hwr
1123 1.88.2.5 ad mutex_enter(&sc->sc_mtx);
1124 1.88.2.8 ad
1125 1.88.2.8 ad while (sc->sc_state == GRE_S_IOCTL)
1126 1.88.2.8 ad gre_wait(sc);
1127 1.88.2.8 ad
1128 1.88.2.8 ad if (sc->sc_state != GRE_S_IDLE) {
1129 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1130 1.88.2.8 ad error = ENXIO;
1131 1.88.2.8 ad goto out;
1132 1.88.2.8 ad }
1133 1.88.2.8 ad
1134 1.88.2.8 ad sc->sc_state = GRE_S_IOCTL;
1135 1.88.2.8 ad sp0 = sc->sc_soparm;
1136 1.88.2.8 ad sp0.sp_fd = -1;
1137 1.88.2.8 ad sp = &sp0;
1138 1.88.2.8 ad
1139 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1140 1.88.2.8 ad
1141 1.20 itojun switch (cmd) {
1142 1.20 itojun case SIOCSIFADDR:
1143 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1144 1.88.2.8 ad if ((ifp->if_flags & IFF_UP) != 0)
1145 1.88.2.8 ad break;
1146 1.88.2.8 ad gre_clearconf(sp, false);
1147 1.36 itojun ifp->if_flags |= IFF_UP;
1148 1.88.2.8 ad goto mksocket;
1149 1.55 perry case SIOCSIFDSTADDR:
1150 1.1 hwr break;
1151 1.1 hwr case SIOCSIFFLAGS:
1152 1.88.2.8 ad oproto = sp->sp_proto;
1153 1.88.2.8 ad otype = sp->sp_type;
1154 1.62 dyoung switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
1155 1.62 dyoung case IFF_LINK0|IFF_LINK2:
1156 1.88.2.8 ad sp->sp_proto = IPPROTO_UDP;
1157 1.88.2.8 ad sp->sp_type = SOCK_DGRAM;
1158 1.88.2.8 ad break;
1159 1.88.2.8 ad case IFF_LINK2:
1160 1.88.2.8 ad sp->sp_proto = 0;
1161 1.88.2.8 ad sp->sp_type = 0;
1162 1.62 dyoung break;
1163 1.62 dyoung case IFF_LINK0:
1164 1.88.2.8 ad sp->sp_proto = IPPROTO_GRE;
1165 1.88.2.8 ad sp->sp_type = SOCK_RAW;
1166 1.88.2.8 ad break;
1167 1.88.2.8 ad default:
1168 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1169 1.88.2.8 ad error = EINVAL;
1170 1.88.2.8 ad goto out;
1171 1.88.2.8 ad }
1172 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1173 1.88.2.8 ad gre_clearconf(sp, false);
1174 1.88.2.8 ad if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
1175 1.88.2.8 ad (IFF_UP|IFF_RUNNING) &&
1176 1.88.2.8 ad (oproto == sp->sp_proto || sp->sp_proto == 0) &&
1177 1.88.2.8 ad (otype == sp->sp_type || sp->sp_type == 0))
1178 1.88.2.8 ad break;
1179 1.88.2.8 ad switch (sp->sp_proto) {
1180 1.88.2.8 ad case IPPROTO_UDP:
1181 1.88.2.8 ad case IPPROTO_GRE:
1182 1.88.2.8 ad goto mksocket;
1183 1.88.2.8 ad default:
1184 1.88.2.8 ad break;
1185 1.62 dyoung }
1186 1.1 hwr break;
1187 1.20 itojun case SIOCSIFMTU:
1188 1.88.2.8 ad /* XXX determine MTU automatically by probing w/
1189 1.88.2.8 ad * XXX do-not-fragment packets?
1190 1.88.2.8 ad */
1191 1.27 martin if (ifr->ifr_mtu < 576) {
1192 1.1 hwr error = EINVAL;
1193 1.1 hwr break;
1194 1.1 hwr }
1195 1.1 hwr ifp->if_mtu = ifr->ifr_mtu;
1196 1.1 hwr break;
1197 1.1 hwr case SIOCGIFMTU:
1198 1.1 hwr ifr->ifr_mtu = sc->sc_if.if_mtu;
1199 1.1 hwr break;
1200 1.1 hwr case SIOCADDMULTI:
1201 1.1 hwr case SIOCDELMULTI:
1202 1.88.2.8 ad if (ifr == NULL) {
1203 1.1 hwr error = EAFNOSUPPORT;
1204 1.1 hwr break;
1205 1.1 hwr }
1206 1.88.2.8 ad switch (ifreq_getaddr(cmd, ifr)->sa_family) {
1207 1.1 hwr #ifdef INET
1208 1.1 hwr case AF_INET:
1209 1.1 hwr break;
1210 1.1 hwr #endif
1211 1.56 is #ifdef INET6
1212 1.56 is case AF_INET6:
1213 1.56 is break;
1214 1.56 is #endif
1215 1.1 hwr default:
1216 1.1 hwr error = EAFNOSUPPORT;
1217 1.1 hwr break;
1218 1.1 hwr }
1219 1.1 hwr break;
1220 1.1 hwr case GRESPROTO:
1221 1.88.2.8 ad gre_clearconf(sp, false);
1222 1.88.2.8 ad oproto = sp->sp_proto;
1223 1.88.2.8 ad otype = sp->sp_type;
1224 1.88.2.8 ad sp->sp_proto = ifr->ifr_flags;
1225 1.88.2.8 ad switch (sp->sp_proto) {
1226 1.62 dyoung case IPPROTO_UDP:
1227 1.62 dyoung ifp->if_flags |= IFF_LINK0|IFF_LINK2;
1228 1.88.2.8 ad sp->sp_type = SOCK_DGRAM;
1229 1.62 dyoung break;
1230 1.40 itojun case IPPROTO_GRE:
1231 1.3 hwr ifp->if_flags |= IFF_LINK0;
1232 1.62 dyoung ifp->if_flags &= ~IFF_LINK2;
1233 1.88.2.8 ad sp->sp_type = SOCK_RAW;
1234 1.88.2.8 ad break;
1235 1.88.2.8 ad case 0:
1236 1.88.2.8 ad ifp->if_flags &= ~IFF_LINK0;
1237 1.88.2.8 ad ifp->if_flags |= IFF_LINK2;
1238 1.88.2.8 ad sp->sp_type = 0;
1239 1.88.2.8 ad break;
1240 1.1 hwr default:
1241 1.40 itojun error = EPROTONOSUPPORT;
1242 1.35 itojun break;
1243 1.1 hwr }
1244 1.88.2.8 ad if ((oproto == sp->sp_proto || sp->sp_proto == 0) &&
1245 1.88.2.8 ad (otype == sp->sp_type || sp->sp_type == 0))
1246 1.88.2.8 ad break;
1247 1.88.2.8 ad switch (sp->sp_proto) {
1248 1.88.2.8 ad case IPPROTO_UDP:
1249 1.88.2.8 ad case IPPROTO_GRE:
1250 1.88.2.8 ad goto mksocket;
1251 1.88.2.8 ad default:
1252 1.88.2.8 ad break;
1253 1.88.2.8 ad }
1254 1.1 hwr break;
1255 1.1 hwr case GREGPROTO:
1256 1.88.2.8 ad ifr->ifr_flags = sp->sp_proto;
1257 1.1 hwr break;
1258 1.1 hwr case GRESADDRS:
1259 1.1 hwr case GRESADDRD:
1260 1.88.2.8 ad gre_clearconf(sp, false);
1261 1.1 hwr /*
1262 1.20 itojun * set tunnel endpoints, compute a less specific route
1263 1.20 itojun * to the remote end and mark if as up
1264 1.20 itojun */
1265 1.88.2.8 ad switch (cmd) {
1266 1.88.2.8 ad case GRESADDRS:
1267 1.88.2.8 ad sockaddr_copy(sstosa(&sp->sp_src),
1268 1.88.2.8 ad sizeof(sp->sp_src), ifreq_getaddr(cmd, ifr));
1269 1.88.2.8 ad break;
1270 1.88.2.8 ad case GRESADDRD:
1271 1.88.2.8 ad sockaddr_copy(sstosa(&sp->sp_dst),
1272 1.88.2.8 ad sizeof(sp->sp_dst), ifreq_getaddr(cmd, ifr));
1273 1.88.2.5 ad break;
1274 1.63 dyoung }
1275 1.88.2.8 ad checkaddr:
1276 1.88.2.8 ad if (sockaddr_any(sstosa(&sp->sp_src)) == NULL ||
1277 1.88.2.8 ad sockaddr_any(sstosa(&sp->sp_dst)) == NULL) {
1278 1.62 dyoung error = EINVAL;
1279 1.62 dyoung break;
1280 1.62 dyoung }
1281 1.88.2.8 ad /* let gre_socreate() check the rest */
1282 1.88.2.8 ad mksocket:
1283 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1284 1.88.2.8 ad /* If we're administratively down, or the configuration
1285 1.88.2.8 ad * is empty, there's no use creating a socket.
1286 1.88.2.8 ad */
1287 1.88.2.8 ad if ((ifp->if_flags & IFF_UP) == 0 || gre_is_nullconf(sp))
1288 1.88.2.8 ad goto sendconf;
1289 1.88.2.8 ad
1290 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1291 1.88.2.8 ad mutex_exit(&sc->sc_mtx);
1292 1.88.2.8 ad error = gre_socreate(sc, l, sp, &fd);
1293 1.88.2.8 ad mutex_enter(&sc->sc_mtx);
1294 1.88.2.8 ad
1295 1.88.2.8 ad if (error != 0)
1296 1.62 dyoung break;
1297 1.62 dyoung
1298 1.88.2.8 ad setsock:
1299 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1300 1.88.2.5 ad
1301 1.88.2.8 ad mutex_exit(&sc->sc_mtx);
1302 1.88.2.8 ad error = gre_ssock(ifp, sp, fd);
1303 1.88.2.5 ad
1304 1.88.2.8 ad if (cmd != GRESSOCK) {
1305 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1306 1.88.2.8 ad fdrelease(l, fd);
1307 1.88.2.5 ad }
1308 1.88.2.8 ad
1309 1.88.2.8 ad mutex_enter(&sc->sc_mtx);
1310 1.88.2.8 ad
1311 1.88.2.5 ad if (error == 0) {
1312 1.88.2.8 ad sendconf:
1313 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1314 1.88.2.8 ad ifp->if_flags &= ~IFF_RUNNING;
1315 1.88.2.8 ad sc->sc_so = gre_reconf(sc, sc->sc_so, sc->sc_lwp, sp);
1316 1.88.2.5 ad }
1317 1.62 dyoung
1318 1.62 dyoung break;
1319 1.88.2.8 ad case GREGADDRS:
1320 1.88.2.8 ad ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_src));
1321 1.88.2.8 ad break;
1322 1.88.2.8 ad case GREGADDRD:
1323 1.88.2.8 ad ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_dst));
1324 1.88.2.8 ad break;
1325 1.88.2.8 ad case GREDSOCK:
1326 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1327 1.88.2.8 ad if (sp->sp_bysock)
1328 1.88.2.8 ad ifp->if_flags &= ~IFF_UP;
1329 1.88.2.8 ad gre_clearconf(sp, false);
1330 1.88.2.8 ad goto mksocket;
1331 1.88.2.8 ad case GRESSOCK:
1332 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1333 1.88.2.8 ad gre_clearconf(sp, true);
1334 1.88.2.8 ad fd = (int)ifr->ifr_value;
1335 1.88.2.8 ad sp->sp_bysock = 1;
1336 1.88.2.8 ad ifp->if_flags |= IFF_UP;
1337 1.88.2.8 ad goto setsock;
1338 1.28 itojun case SIOCSLIFPHYADDR:
1339 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1340 1.88.2.8 ad if (lifr->addr.ss_family != lifr->dstaddr.ss_family) {
1341 1.31 itojun error = EAFNOSUPPORT;
1342 1.31 itojun break;
1343 1.31 itojun }
1344 1.88.2.8 ad sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
1345 1.88.2.8 ad sstosa(&lifr->addr));
1346 1.88.2.8 ad sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
1347 1.88.2.8 ad sstosa(&lifr->dstaddr));
1348 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1349 1.88.2.8 ad goto checkaddr;
1350 1.28 itojun case SIOCDIFPHYADDR:
1351 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1352 1.88.2.8 ad gre_clearconf(sp, true);
1353 1.88.2.8 ad ifp->if_flags &= ~IFF_UP;
1354 1.88.2.8 ad goto mksocket;
1355 1.28 itojun case SIOCGLIFPHYADDR:
1356 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1357 1.88.2.8 ad if (gre_is_nullconf(sp)) {
1358 1.28 itojun error = EADDRNOTAVAIL;
1359 1.28 itojun break;
1360 1.28 itojun }
1361 1.88.2.8 ad sockaddr_copy(sstosa(&lifr->addr), sizeof(lifr->addr),
1362 1.88.2.8 ad sstosa(&sp->sp_src));
1363 1.88.2.8 ad sockaddr_copy(sstosa(&lifr->dstaddr), sizeof(lifr->dstaddr),
1364 1.88.2.8 ad sstosa(&sp->sp_dst));
1365 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1366 1.1 hwr break;
1367 1.1 hwr default:
1368 1.1 hwr error = EINVAL;
1369 1.31 itojun break;
1370 1.1 hwr }
1371 1.88.2.8 ad out:
1372 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1373 1.88.2.8 ad if (sc->sc_state == GRE_S_IOCTL) {
1374 1.88.2.8 ad GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1375 1.88.2.8 ad sc->sc_state = GRE_S_IDLE;
1376 1.88.2.8 ad }
1377 1.88.2.8 ad cv_signal(&sc->sc_condvar);
1378 1.88.2.5 ad mutex_exit(&sc->sc_mtx);
1379 1.87 dyoung return error;
1380 1.1 hwr }
1381 1.1 hwr
1382 1.54 christos #endif
1383 1.54 christos
1384 1.59 thorpej void greattach(int);
1385 1.54 christos
1386 1.54 christos /* ARGSUSED */
1387 1.54 christos void
1388 1.74 christos greattach(int count)
1389 1.54 christos {
1390 1.54 christos #ifdef INET
1391 1.54 christos if_clone_attach(&gre_cloner);
1392 1.54 christos #endif
1393 1.54 christos }
1394