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