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