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