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