xref: /netbsd-src/sys/net/if_gre.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: if_gre.c,v 1.142 2010/01/19 22:08:01 pooka 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.142 2010/01/19 22:08:01 pooka Exp $");
49 
50 #include "opt_atalk.h"
51 #include "opt_gre.h"
52 #include "opt_inet.h"
53 
54 #include <sys/param.h>
55 #include <sys/file.h>
56 #include <sys/filedesc.h>
57 #include <sys/malloc.h>
58 #include <sys/mallocvar.h>
59 #include <sys/mbuf.h>
60 #include <sys/proc.h>
61 #include <sys/domain.h>
62 #include <sys/protosw.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/ioctl.h>
66 #include <sys/queue.h>
67 #include <sys/intr.h>
68 #include <sys/systm.h>
69 #include <sys/sysctl.h>
70 #include <sys/kauth.h>
71 
72 #include <sys/kernel.h>
73 #include <sys/mutex.h>
74 #include <sys/condvar.h>
75 #include <sys/kthread.h>
76 
77 #include <sys/cpu.h>
78 
79 #include <net/ethertypes.h>
80 #include <net/if.h>
81 #include <net/if_types.h>
82 #include <net/netisr.h>
83 #include <net/route.h>
84 
85 #include <netinet/in_systm.h>
86 #include <netinet/in.h>
87 #include <netinet/ip.h> /* we always need this for sizeof(struct ip) */
88 
89 #ifdef INET
90 #include <netinet/in_var.h>
91 #include <netinet/ip_var.h>
92 #endif
93 
94 #ifdef INET6
95 #include <netinet6/in6_var.h>
96 #endif
97 
98 #ifdef NETATALK
99 #include <netatalk/at.h>
100 #include <netatalk/at_var.h>
101 #include <netatalk/at_extern.h>
102 #endif
103 
104 #include <sys/time.h>
105 #include <net/bpf.h>
106 
107 #include <net/if_gre.h>
108 
109 #include <compat/sys/socket.h>
110 #include <compat/sys/sockio.h>
111 /*
112  * It is not easy to calculate the right value for a GRE MTU.
113  * We leave this task to the admin and use the same default that
114  * other vendors use.
115  */
116 #define GREMTU 1476
117 
118 #ifdef GRE_DEBUG
119 int gre_debug = 0;
120 #define	GRE_DPRINTF(__sc, ...)						\
121 	do {								\
122 		if (__predict_false(gre_debug ||			\
123 		    ((__sc)->sc_if.if_flags & IFF_DEBUG) != 0)) {	\
124 			printf("%s.%d: ", __func__, __LINE__);		\
125 			printf(__VA_ARGS__);				\
126 		}							\
127 	} while (/*CONSTCOND*/0)
128 #else
129 #define	GRE_DPRINTF(__sc, __fmt, ...)	do { } while (/*CONSTCOND*/0)
130 #endif /* GRE_DEBUG */
131 
132 int ip_gre_ttl = GRE_TTL;
133 MALLOC_DEFINE(M_GRE_BUFQ, "gre_bufq", "gre mbuf queue");
134 
135 static int gre_clone_create(struct if_clone *, int);
136 static int gre_clone_destroy(struct ifnet *);
137 
138 static struct if_clone gre_cloner =
139     IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
140 
141 static int gre_input(struct gre_softc *, struct mbuf *, int,
142     const struct gre_h *);
143 static bool gre_is_nullconf(const struct gre_soparm *);
144 static int gre_output(struct ifnet *, struct mbuf *,
145 			   const struct sockaddr *, struct rtentry *);
146 static int gre_ioctl(struct ifnet *, u_long, void *);
147 static int gre_getsockname(struct socket *, struct mbuf *, struct lwp *);
148 static int gre_getpeername(struct socket *, struct mbuf *, struct lwp *);
149 static int gre_getnames(struct socket *, struct lwp *,
150     struct sockaddr_storage *, struct sockaddr_storage *);
151 static void gre_clearconf(struct gre_soparm *, bool);
152 static int gre_soreceive(struct socket *, struct mbuf **);
153 static int gre_sosend(struct socket *, struct mbuf *);
154 static struct socket *gre_reconf(struct gre_softc *, const struct gre_soparm *);
155 
156 static bool gre_fp_send(struct gre_softc *, enum gre_msg, file_t *);
157 static bool gre_fp_recv(struct gre_softc *);
158 static void gre_fp_recvloop(void *);
159 
160 static int
161 nearest_pow2(size_t len0)
162 {
163 	size_t len, mid;
164 
165 	if (len0 == 0)
166 		return 1;
167 
168 	for (len = len0; (len & (len - 1)) != 0; len &= len - 1)
169 		;
170 
171 	mid = len | (len >> 1);
172 
173 	/* avoid overflow */
174 	if ((len << 1) < len)
175 		return len;
176 	if (len0 >= mid)
177 		return len << 1;
178 	return len;
179 }
180 
181 static struct gre_bufq *
182 gre_bufq_init(struct gre_bufq *bq, size_t len0)
183 {
184 	size_t len;
185 
186 	len = nearest_pow2(len0);
187 
188 	memset(bq, 0, sizeof(*bq));
189 	bq->bq_buf = malloc(len * sizeof(struct mbuf *), M_GRE_BUFQ, M_WAITOK);
190 	bq->bq_len = len;
191 	bq->bq_lenmask = len - 1;
192 
193 	return bq;
194 }
195 
196 static bool
197 gre_bufq_empty(struct gre_bufq *bq)
198 {
199 	return bq->bq_prodidx == bq->bq_considx;
200 }
201 
202 static struct mbuf *
203 gre_bufq_dequeue(struct gre_bufq *bq)
204 {
205 	struct mbuf *m;
206 
207 	if (gre_bufq_empty(bq))
208 		return NULL;
209 
210 	m = bq->bq_buf[bq->bq_considx];
211 	bq->bq_considx = (bq->bq_considx + 1) & bq->bq_lenmask;
212 
213 	return m;
214 }
215 
216 static void
217 gre_bufq_purge(struct gre_bufq *bq)
218 {
219 	struct mbuf *m;
220 
221 	while ((m = gre_bufq_dequeue(bq)) != NULL)
222 		m_freem(m);
223 }
224 
225 static int
226 gre_bufq_enqueue(struct gre_bufq *bq, struct mbuf *m)
227 {
228 	int next;
229 
230 	next = (bq->bq_prodidx + 1) & bq->bq_lenmask;
231 
232 	if (next == bq->bq_considx) {
233 		bq->bq_drops++;
234 		return ENOBUFS;
235 	}
236 
237 	bq->bq_buf[bq->bq_prodidx] = m;
238 	bq->bq_prodidx = next;
239 	return 0;
240 }
241 
242 static void
243 greintr(void *arg)
244 {
245 	struct gre_softc *sc = (struct gre_softc *)arg;
246 	struct socket *so = sc->sc_soparm.sp_so;
247 	int rc;
248 	struct mbuf *m;
249 
250 	KASSERT(so != NULL);
251 
252 	sc->sc_send_ev.ev_count++;
253 	GRE_DPRINTF(sc, "enter\n");
254 	while ((m = gre_bufq_dequeue(&sc->sc_snd)) != NULL) {
255 		/* XXX handle ENOBUFS? */
256 		if ((rc = gre_sosend(so, m)) != 0)
257 			GRE_DPRINTF(sc, "gre_sosend failed %d\n", rc);
258 	}
259 }
260 
261 /* Caller must hold sc->sc_mtx. */
262 static void
263 gre_wait(struct gre_softc *sc)
264 {
265 	sc->sc_waiters++;
266 	cv_wait(&sc->sc_condvar, &sc->sc_mtx);
267 	sc->sc_waiters--;
268 }
269 
270 static void
271 gre_fp_wait(struct gre_softc *sc)
272 {
273 	sc->sc_fp_waiters++;
274 	cv_wait(&sc->sc_fp_condvar, &sc->sc_mtx);
275 	sc->sc_fp_waiters--;
276 }
277 
278 static void
279 gre_evcnt_detach(struct gre_softc *sc)
280 {
281 	evcnt_detach(&sc->sc_unsupp_ev);
282 	evcnt_detach(&sc->sc_pullup_ev);
283 	evcnt_detach(&sc->sc_error_ev);
284 	evcnt_detach(&sc->sc_block_ev);
285 	evcnt_detach(&sc->sc_recv_ev);
286 
287 	evcnt_detach(&sc->sc_oflow_ev);
288 	evcnt_detach(&sc->sc_send_ev);
289 }
290 
291 static void
292 gre_evcnt_attach(struct gre_softc *sc)
293 {
294 	evcnt_attach_dynamic(&sc->sc_recv_ev, EVCNT_TYPE_MISC,
295 	    NULL, sc->sc_if.if_xname, "recv");
296 	evcnt_attach_dynamic(&sc->sc_block_ev, EVCNT_TYPE_MISC,
297 	    &sc->sc_recv_ev, sc->sc_if.if_xname, "would block");
298 	evcnt_attach_dynamic(&sc->sc_error_ev, EVCNT_TYPE_MISC,
299 	    &sc->sc_recv_ev, sc->sc_if.if_xname, "error");
300 	evcnt_attach_dynamic(&sc->sc_pullup_ev, EVCNT_TYPE_MISC,
301 	    &sc->sc_recv_ev, sc->sc_if.if_xname, "pullup failed");
302 	evcnt_attach_dynamic(&sc->sc_unsupp_ev, EVCNT_TYPE_MISC,
303 	    &sc->sc_recv_ev, sc->sc_if.if_xname, "unsupported");
304 
305 	evcnt_attach_dynamic(&sc->sc_send_ev, EVCNT_TYPE_MISC,
306 	    NULL, sc->sc_if.if_xname, "send");
307 	evcnt_attach_dynamic(&sc->sc_oflow_ev, EVCNT_TYPE_MISC,
308 	    &sc->sc_send_ev, sc->sc_if.if_xname, "overflow");
309 }
310 
311 static int
312 gre_clone_create(struct if_clone *ifc, int unit)
313 {
314 	int rc;
315 	struct gre_softc *sc;
316 	struct gre_soparm *sp;
317 	const struct sockaddr *any;
318 
319 	if ((any = sockaddr_any_by_family(AF_INET)) == NULL &&
320 	    (any = sockaddr_any_by_family(AF_INET6)) == NULL)
321 		return -1;
322 
323 	sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK|M_ZERO);
324 	mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_SOFTNET);
325 	cv_init(&sc->sc_condvar, "gre wait");
326 	cv_init(&sc->sc_fp_condvar, "gre fp");
327 
328 	if_initname(&sc->sc_if, ifc->ifc_name, unit);
329 	sc->sc_if.if_softc = sc;
330 	sc->sc_if.if_type = IFT_TUNNEL;
331 	sc->sc_if.if_addrlen = 0;
332 	sc->sc_if.if_hdrlen = sizeof(struct ip) + sizeof(struct gre_h);
333 	sc->sc_if.if_dlt = DLT_NULL;
334 	sc->sc_if.if_mtu = GREMTU;
335 	sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
336 	sc->sc_if.if_output = gre_output;
337 	sc->sc_if.if_ioctl = gre_ioctl;
338 	sp = &sc->sc_soparm;
339 	sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst), any);
340 	sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src), 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 	bpf_ops->bpf_attach(&sc->sc_if, DLT_NULL,
359 	    sizeof(uint32_t), &sc->sc_if.if_bpf);
360 	sc->sc_state = GRE_S_IDLE;
361 	return 0;
362 }
363 
364 static int
365 gre_clone_destroy(struct ifnet *ifp)
366 {
367 	int s;
368 	struct gre_softc *sc = ifp->if_softc;
369 
370 	GRE_DPRINTF(sc, "\n");
371 
372 	bpf_ops->bpf_detach(ifp);
373 	s = splnet();
374 	if_detach(ifp);
375 
376 	/* Some LWPs may still wait in gre_ioctl_lock(), however,
377 	 * no new LWP will enter gre_ioctl_lock(), because ifunit()
378 	 * cannot locate the interface any longer.
379 	 */
380 	mutex_enter(&sc->sc_mtx);
381 	GRE_DPRINTF(sc, "\n");
382 	while (sc->sc_state != GRE_S_IDLE)
383 		gre_wait(sc);
384 	GRE_DPRINTF(sc, "\n");
385 	sc->sc_state = GRE_S_DIE;
386 	cv_broadcast(&sc->sc_condvar);
387 	while (sc->sc_waiters > 0)
388 		cv_wait(&sc->sc_condvar, &sc->sc_mtx);
389 	/* At this point, no other LWP will access the gre_softc, so
390 	 * we can release the mutex.
391 	 */
392 	mutex_exit(&sc->sc_mtx);
393 	GRE_DPRINTF(sc, "\n");
394 	/* Note that we must not hold the mutex while we call gre_reconf(). */
395 	gre_reconf(sc, NULL);
396 
397 	mutex_enter(&sc->sc_mtx);
398 	sc->sc_msg = GRE_M_STOP;
399 	cv_signal(&sc->sc_fp_condvar);
400 	while (sc->sc_fp_waiters > 0)
401 		cv_wait(&sc->sc_fp_condvar, &sc->sc_mtx);
402 	mutex_exit(&sc->sc_mtx);
403 
404 	splx(s);
405 
406 	cv_destroy(&sc->sc_condvar);
407 	cv_destroy(&sc->sc_fp_condvar);
408 	mutex_destroy(&sc->sc_mtx);
409 	gre_evcnt_detach(sc);
410 	free(sc, M_DEVBUF);
411 
412 	return 0;
413 }
414 
415 static void
416 gre_receive(struct socket *so, void *arg, int events, int waitflag)
417 {
418 	struct gre_softc *sc = (struct gre_softc *)arg;
419 	int rc;
420 	const struct gre_h *gh;
421 	struct mbuf *m;
422 
423 	GRE_DPRINTF(sc, "enter\n");
424 
425 	sc->sc_recv_ev.ev_count++;
426 
427 	rc = gre_soreceive(so, &m);
428 	/* TBD Back off if ECONNREFUSED (indicates
429 	 * ICMP Port Unreachable)?
430 	 */
431 	if (rc == EWOULDBLOCK) {
432 		GRE_DPRINTF(sc, "EWOULDBLOCK\n");
433 		sc->sc_block_ev.ev_count++;
434 		return;
435 	} else if (rc != 0 || m == NULL) {
436 		GRE_DPRINTF(sc, "%s: rc %d m %p\n",
437 		    sc->sc_if.if_xname, rc, (void *)m);
438 		sc->sc_error_ev.ev_count++;
439 		return;
440 	}
441 	if (m->m_len < sizeof(*gh) && (m = m_pullup(m, sizeof(*gh))) == NULL) {
442 		GRE_DPRINTF(sc, "m_pullup failed\n");
443 		sc->sc_pullup_ev.ev_count++;
444 		return;
445 	}
446 	gh = mtod(m, const struct gre_h *);
447 
448 	if (gre_input(sc, m, 0, gh) == 0) {
449 		sc->sc_unsupp_ev.ev_count++;
450 		GRE_DPRINTF(sc, "dropping unsupported\n");
451 		m_freem(m);
452 	}
453 }
454 
455 static void
456 gre_upcall_add(struct socket *so, void *arg)
457 {
458 	/* XXX What if the kernel already set an upcall? */
459 	KASSERT((so->so_rcv.sb_flags & SB_UPCALL) == 0);
460 	so->so_upcallarg = arg;
461 	so->so_upcall = gre_receive;
462 	so->so_rcv.sb_flags |= SB_UPCALL;
463 }
464 
465 static void
466 gre_upcall_remove(struct socket *so)
467 {
468 	so->so_rcv.sb_flags &= ~SB_UPCALL;
469 	so->so_upcallarg = NULL;
470 	so->so_upcall = NULL;
471 }
472 
473 static int
474 gre_socreate(struct gre_softc *sc, const struct gre_soparm *sp, int *fdout)
475 {
476 	const struct protosw *pr;
477 	int fd, rc;
478 	struct mbuf *m;
479 	struct sockaddr *sa;
480 	struct socket *so;
481 	sa_family_t af;
482 	int val;
483 
484 	GRE_DPRINTF(sc, "enter\n");
485 
486 	af = sp->sp_src.ss_family;
487 	rc = fsocreate(af, NULL, sp->sp_type, sp->sp_proto, curlwp, &fd);
488 	if (rc != 0) {
489 		GRE_DPRINTF(sc, "fsocreate failed\n");
490 		return rc;
491 	}
492 
493 	if ((rc = fd_getsock(fd, &so)) != 0)
494 		return rc;
495 
496 	if ((m = getsombuf(so, MT_SONAME)) == NULL) {
497 		rc = ENOBUFS;
498 		goto out;
499 	}
500 	sa = mtod(m, struct sockaddr *);
501 	sockaddr_copy(sa, MIN(MLEN, sizeof(sp->sp_src)), sstocsa(&sp->sp_src));
502 	m->m_len = sp->sp_src.ss_len;
503 
504 	if ((rc = sobind(so, m, curlwp)) != 0) {
505 		GRE_DPRINTF(sc, "sobind failed\n");
506 		goto out;
507 	}
508 
509 	sockaddr_copy(sa, MIN(MLEN, sizeof(sp->sp_dst)), sstocsa(&sp->sp_dst));
510 	m->m_len = sp->sp_dst.ss_len;
511 
512 	solock(so);
513 	if ((rc = soconnect(so, m, curlwp)) != 0) {
514 		GRE_DPRINTF(sc, "soconnect failed\n");
515 		sounlock(so);
516 		goto out;
517 	}
518 	sounlock(so);
519 
520 	m = NULL;
521 
522 	/* XXX convert to a (new) SOL_SOCKET call */
523   	pr = so->so_proto;
524   	KASSERT(pr != NULL);
525  	rc = so_setsockopt(curlwp, so, IPPROTO_IP, IP_TTL,
526 	    &ip_gre_ttl, sizeof(ip_gre_ttl));
527   	if (rc != 0) {
528  		GRE_DPRINTF(sc, "so_setsockopt ttl failed\n");
529   		rc = 0;
530   	}
531 
532  	val = 1;
533  	rc = so_setsockopt(curlwp, so, SOL_SOCKET, SO_NOHEADER,
534 	    &val, sizeof(val));
535   	if (rc != 0) {
536  		GRE_DPRINTF(sc, "so_setsockopt SO_NOHEADER failed\n");
537 		rc = 0;
538 	}
539 out:
540 	m_freem(m);
541 
542 	if (rc != 0)
543 		fd_close(fd);
544 	else  {
545 		fd_putfile(fd);
546 		*fdout = fd;
547 	}
548 
549 	return rc;
550 }
551 
552 static int
553 gre_sosend(struct socket *so, struct mbuf *top)
554 {
555 	struct mbuf	**mp;
556 	struct proc	*p;
557 	long		space, resid;
558 	int		error;
559 	struct lwp * const l = curlwp;
560 
561 	p = l->l_proc;
562 
563 	resid = top->m_pkthdr.len;
564 	if (p)
565 		l->l_ru.ru_msgsnd++;
566 #define	snderr(errno)	{ error = errno; goto release; }
567 
568 	solock(so);
569 	if ((error = sblock(&so->so_snd, M_NOWAIT)) != 0)
570 		goto out;
571 	if (so->so_state & SS_CANTSENDMORE)
572 		snderr(EPIPE);
573 	if (so->so_error) {
574 		error = so->so_error;
575 		so->so_error = 0;
576 		goto release;
577 	}
578 	if ((so->so_state & SS_ISCONNECTED) == 0) {
579 		if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
580 			if ((so->so_state & SS_ISCONFIRMING) == 0)
581 				snderr(ENOTCONN);
582 		} else
583 			snderr(EDESTADDRREQ);
584 	}
585 	space = sbspace(&so->so_snd);
586 	if (resid > so->so_snd.sb_hiwat)
587 		snderr(EMSGSIZE);
588 	if (space < resid)
589 		snderr(EWOULDBLOCK);
590 	mp = &top;
591 	/*
592 	 * Data is prepackaged in "top".
593 	 */
594 	if (so->so_state & SS_CANTSENDMORE)
595 		snderr(EPIPE);
596 	error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, top, NULL, NULL, l);
597 	top = NULL;
598 	mp = &top;
599  release:
600 	sbunlock(&so->so_snd);
601  out:
602  	sounlock(so);
603 	if (top != NULL)
604 		m_freem(top);
605 	return error;
606 }
607 
608 /* This is a stripped-down version of soreceive() that will never
609  * block.  It will support SOCK_DGRAM sockets.  It may also support
610  * SOCK_SEQPACKET sockets.
611  */
612 static int
613 gre_soreceive(struct socket *so, struct mbuf **mp0)
614 {
615 	struct mbuf *m, **mp;
616 	int flags, len, error, type;
617 	const struct protosw	*pr;
618 	struct mbuf *nextrecord;
619 
620 	KASSERT(mp0 != NULL);
621 
622 	flags = MSG_DONTWAIT;
623 	pr = so->so_proto;
624 	mp = mp0;
625 	type = 0;
626 
627 	*mp = NULL;
628 
629 	KASSERT(pr->pr_flags & PR_ATOMIC);
630 
631 	if (so->so_state & SS_ISCONFIRMING)
632 		(*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, curlwp);
633  restart:
634 	if ((error = sblock(&so->so_rcv, M_NOWAIT)) != 0) {
635 		return error;
636 	}
637 	m = so->so_rcv.sb_mb;
638 	/*
639 	 * If we have less data than requested, do not block awaiting more.
640 	 */
641 	if (m == NULL) {
642 #ifdef DIAGNOSTIC
643 		if (so->so_rcv.sb_cc)
644 			panic("receive 1");
645 #endif
646 		if (so->so_error) {
647 			error = so->so_error;
648 			so->so_error = 0;
649 		} else if (so->so_state & SS_CANTRCVMORE)
650 			;
651 		else if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0
652 		      && (so->so_proto->pr_flags & PR_CONNREQUIRED))
653 			error = ENOTCONN;
654 		else
655 			error = EWOULDBLOCK;
656 		goto release;
657 	}
658 	/*
659 	 * On entry here, m points to the first record of the socket buffer.
660 	 * While we process the initial mbufs containing address and control
661 	 * info, we save a copy of m->m_nextpkt into nextrecord.
662 	 */
663 	if (curlwp != NULL)
664 		curlwp->l_ru.ru_msgrcv++;
665 	KASSERT(m == so->so_rcv.sb_mb);
666 	SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
667 	SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
668 	nextrecord = m->m_nextpkt;
669 	if (pr->pr_flags & PR_ADDR) {
670 #ifdef DIAGNOSTIC
671 		if (m->m_type != MT_SONAME)
672 			panic("receive 1a");
673 #endif
674 		sbfree(&so->so_rcv, m);
675 		MFREE(m, so->so_rcv.sb_mb);
676 		m = so->so_rcv.sb_mb;
677 	}
678 	while (m != NULL && m->m_type == MT_CONTROL && error == 0) {
679 		sbfree(&so->so_rcv, m);
680 		/*
681 		 * Dispose of any SCM_RIGHTS message that went
682 		 * through the read path rather than recv.
683 		 */
684 		if (pr->pr_domain->dom_dispose &&
685 		    mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
686 			(*pr->pr_domain->dom_dispose)(m);
687 		MFREE(m, so->so_rcv.sb_mb);
688 		m = so->so_rcv.sb_mb;
689 	}
690 
691 	/*
692 	 * If m is non-NULL, we have some data to read.  From now on,
693 	 * make sure to keep sb_lastrecord consistent when working on
694 	 * the last packet on the chain (nextrecord == NULL) and we
695 	 * change m->m_nextpkt.
696 	 */
697 	if (m != NULL) {
698 		m->m_nextpkt = nextrecord;
699 		/*
700 		 * If nextrecord == NULL (this is a single chain),
701 		 * then sb_lastrecord may not be valid here if m
702 		 * was changed earlier.
703 		 */
704 		if (nextrecord == NULL) {
705 			KASSERT(so->so_rcv.sb_mb == m);
706 			so->so_rcv.sb_lastrecord = m;
707 		}
708 		type = m->m_type;
709 		if (type == MT_OOBDATA)
710 			flags |= MSG_OOB;
711 	} else {
712 		KASSERT(so->so_rcv.sb_mb == m);
713 		so->so_rcv.sb_mb = nextrecord;
714 		SB_EMPTY_FIXUP(&so->so_rcv);
715 	}
716 	SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
717 	SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
718 
719 	while (m != NULL) {
720 		if (m->m_type == MT_OOBDATA) {
721 			if (type != MT_OOBDATA)
722 				break;
723 		} else if (type == MT_OOBDATA)
724 			break;
725 #ifdef DIAGNOSTIC
726 		else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
727 			panic("receive 3");
728 #endif
729 		so->so_state &= ~SS_RCVATMARK;
730 		if (so->so_oobmark != 0 && so->so_oobmark < m->m_len)
731 			break;
732 		len = m->m_len;
733 		/*
734 		 * mp is set, just pass back the mbufs.
735 		 * Sockbuf must be consistent here (points to current mbuf,
736 		 * it points to next record) when we drop priority;
737 		 * we must note any additions to the sockbuf when we
738 		 * block interrupts again.
739 		 */
740 		if (m->m_flags & M_EOR)
741 			flags |= MSG_EOR;
742 		nextrecord = m->m_nextpkt;
743 		sbfree(&so->so_rcv, m);
744 		*mp = m;
745 		mp = &m->m_next;
746 		so->so_rcv.sb_mb = m = m->m_next;
747 		*mp = NULL;
748 		/*
749 		 * If m != NULL, we also know that
750 		 * so->so_rcv.sb_mb != NULL.
751 		 */
752 		KASSERT(so->so_rcv.sb_mb == m);
753 		if (m) {
754 			m->m_nextpkt = nextrecord;
755 			if (nextrecord == NULL)
756 				so->so_rcv.sb_lastrecord = m;
757 		} else {
758 			so->so_rcv.sb_mb = nextrecord;
759 			SB_EMPTY_FIXUP(&so->so_rcv);
760 		}
761 		SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
762 		SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
763 		if (so->so_oobmark) {
764 			so->so_oobmark -= len;
765 			if (so->so_oobmark == 0) {
766 				so->so_state |= SS_RCVATMARK;
767 				break;
768 			}
769 		}
770 		if (flags & MSG_EOR)
771 			break;
772 	}
773 
774 	if (m != NULL) {
775 		m_freem(*mp);
776 		*mp = NULL;
777 		error = ENOMEM;
778 		(void) sbdroprecord(&so->so_rcv);
779 	} else {
780 		/*
781 		 * First part is an inline SB_EMPTY_FIXUP().  Second
782 		 * part makes sure sb_lastrecord is up-to-date if
783 		 * there is still data in the socket buffer.
784 		 */
785 		so->so_rcv.sb_mb = nextrecord;
786 		if (so->so_rcv.sb_mb == NULL) {
787 			so->so_rcv.sb_mbtail = NULL;
788 			so->so_rcv.sb_lastrecord = NULL;
789 		} else if (nextrecord->m_nextpkt == NULL)
790 			so->so_rcv.sb_lastrecord = nextrecord;
791 	}
792 	SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
793 	SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
794 	if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
795 		(*pr->pr_usrreq)(so, PRU_RCVD, NULL,
796 		    (struct mbuf *)(long)flags, NULL, curlwp);
797 	if (*mp0 == NULL && (flags & MSG_EOR) == 0 &&
798 	    (so->so_state & SS_CANTRCVMORE) == 0) {
799 		sbunlock(&so->so_rcv);
800 		goto restart;
801 	}
802 
803  release:
804 	sbunlock(&so->so_rcv);
805 	return error;
806 }
807 
808 static struct socket *
809 gre_reconf(struct gre_softc *sc, const struct gre_soparm *newsoparm)
810 {
811 	struct ifnet *ifp = &sc->sc_if;
812 
813 	GRE_DPRINTF(sc, "enter\n");
814 
815 shutdown:
816 	if (sc->sc_soparm.sp_so != NULL) {
817 		GRE_DPRINTF(sc, "\n");
818 		gre_upcall_remove(sc->sc_soparm.sp_so);
819 		softint_disestablish(sc->sc_si);
820 		sc->sc_si = NULL;
821 		gre_fp_send(sc, GRE_M_DELFP, NULL);
822 		gre_clearconf(&sc->sc_soparm, false);
823 	}
824 
825 	if (newsoparm != NULL) {
826 		GRE_DPRINTF(sc, "\n");
827 		sc->sc_soparm = *newsoparm;
828 		newsoparm = NULL;
829 	}
830 
831 	if (sc->sc_soparm.sp_so != NULL) {
832 		GRE_DPRINTF(sc, "\n");
833 		sc->sc_si = softint_establish(SOFTINT_NET, greintr, sc);
834 		gre_upcall_add(sc->sc_soparm.sp_so, sc);
835 		if ((ifp->if_flags & IFF_UP) == 0) {
836 			GRE_DPRINTF(sc, "down\n");
837 			goto shutdown;
838 		}
839 	}
840 
841 	GRE_DPRINTF(sc, "\n");
842 	if (sc->sc_soparm.sp_so != NULL)
843 		sc->sc_if.if_flags |= IFF_RUNNING;
844 	else {
845 		gre_bufq_purge(&sc->sc_snd);
846 		sc->sc_if.if_flags &= ~IFF_RUNNING;
847 	}
848 	return sc->sc_soparm.sp_so;
849 }
850 
851 static int
852 gre_input(struct gre_softc *sc, struct mbuf *m, int hlen,
853     const struct gre_h *gh)
854 {
855 	uint16_t flags;
856 	uint32_t af;		/* af passed to BPF tap */
857 	int isr, s;
858 	struct ifqueue *ifq;
859 
860 	sc->sc_if.if_ipackets++;
861 	sc->sc_if.if_ibytes += m->m_pkthdr.len;
862 
863 	hlen += sizeof(struct gre_h);
864 
865 	/* process GRE flags as packet can be of variable len */
866 	flags = ntohs(gh->flags);
867 
868 	/* Checksum & Offset are present */
869 	if ((flags & GRE_CP) | (flags & GRE_RP))
870 		hlen += 4;
871 	/* We don't support routing fields (variable length) */
872 	if (flags & GRE_RP) {
873 		sc->sc_if.if_ierrors++;
874 		return 0;
875 	}
876 	if (flags & GRE_KP)
877 		hlen += 4;
878 	if (flags & GRE_SP)
879 		hlen += 4;
880 
881 	switch (ntohs(gh->ptype)) { /* ethertypes */
882 #ifdef INET
883 	case ETHERTYPE_IP:
884 		ifq = &ipintrq;
885 		isr = NETISR_IP;
886 		af = AF_INET;
887 		break;
888 #endif
889 #ifdef NETATALK
890 	case ETHERTYPE_ATALK:
891 		ifq = &atintrq1;
892 		isr = NETISR_ATALK;
893 		af = AF_APPLETALK;
894 		break;
895 #endif
896 #ifdef INET6
897 	case ETHERTYPE_IPV6:
898 		ifq = &ip6intrq;
899 		isr = NETISR_IPV6;
900 		af = AF_INET6;
901 		break;
902 #endif
903 	default:	   /* others not yet supported */
904 		GRE_DPRINTF(sc, "unhandled ethertype 0x%04x\n",
905 		    ntohs(gh->ptype));
906 		sc->sc_if.if_noproto++;
907 		return 0;
908 	}
909 
910 	if (hlen > m->m_pkthdr.len) {
911 		m_freem(m);
912 		sc->sc_if.if_ierrors++;
913 		return EINVAL;
914 	}
915 	m_adj(m, hlen);
916 
917 	if (sc->sc_if.if_bpf != NULL)
918 		bpf_ops->bpf_mtap_af(sc->sc_if.if_bpf, af, m);
919 
920 	m->m_pkthdr.rcvif = &sc->sc_if;
921 
922 	s = splnet();
923 	if (IF_QFULL(ifq)) {
924 		IF_DROP(ifq);
925 		m_freem(m);
926 	} else {
927 		IF_ENQUEUE(ifq, m);
928 	}
929 	/* we need schednetisr since the address family may change */
930 	schednetisr(isr);
931 	splx(s);
932 
933 	return 1;	/* packet is done, no further processing needed */
934 }
935 
936 /*
937  * The output routine. Takes a packet and encapsulates it in the protocol
938  * given by sc->sc_soparm.sp_proto. See also RFC 1701 and RFC 2004
939  */
940 static int
941 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
942 	   struct rtentry *rt)
943 {
944 	int error = 0;
945 	struct gre_softc *sc = ifp->if_softc;
946 	struct gre_h *gh;
947 	uint16_t etype = 0;
948 
949 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
950 		m_freem(m);
951 		error = ENETDOWN;
952 		goto end;
953 	}
954 
955 	if (ifp->if_bpf != NULL)
956 		bpf_ops->bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
957 
958 	m->m_flags &= ~(M_BCAST|M_MCAST);
959 
960 	GRE_DPRINTF(sc, "dst->sa_family=%d\n", dst->sa_family);
961 	switch (dst->sa_family) {
962 #ifdef INET
963 	case AF_INET:
964 		/* TBD Extract the IP ToS field and set the
965 		 * encapsulating protocol's ToS to suit.
966 		 */
967 		etype = htons(ETHERTYPE_IP);
968 		break;
969 #endif
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 SIOCINITIFADDR:
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 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1316 			break;
1317 		oproto = sp->sp_proto;
1318 		otype = sp->sp_type;
1319 		switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
1320 		case IFF_LINK0|IFF_LINK2:
1321 			sp->sp_proto = IPPROTO_UDP;
1322 			sp->sp_type = SOCK_DGRAM;
1323 			break;
1324 		case IFF_LINK2:
1325 			sp->sp_proto = 0;
1326 			sp->sp_type = 0;
1327 			break;
1328 		case IFF_LINK0:
1329 			sp->sp_proto = IPPROTO_GRE;
1330 			sp->sp_type = SOCK_RAW;
1331 			break;
1332 		default:
1333 			GRE_DPRINTF(sc, "\n");
1334 			error = EINVAL;
1335 			goto out;
1336 		}
1337 		GRE_DPRINTF(sc, "\n");
1338 		gre_clearconf(sp, false);
1339 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
1340 		    (IFF_UP|IFF_RUNNING) &&
1341 		    (oproto == sp->sp_proto || sp->sp_proto == 0) &&
1342 		    (otype == sp->sp_type || sp->sp_type == 0))
1343 			break;
1344 		switch (sp->sp_proto) {
1345 		case IPPROTO_UDP:
1346 		case IPPROTO_GRE:
1347 			goto mksocket;
1348 		default:
1349 			break;
1350 		}
1351 		break;
1352 	case SIOCSIFMTU:
1353 		/* XXX determine MTU automatically by probing w/
1354 		 * XXX do-not-fragment packets?
1355 		 */
1356 		if (ifr->ifr_mtu < 576) {
1357 			error = EINVAL;
1358 			break;
1359 		}
1360 		/*FALLTHROUGH*/
1361 	case SIOCGIFMTU:
1362 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
1363 			error = 0;
1364 		break;
1365 	case SIOCADDMULTI:
1366 	case SIOCDELMULTI:
1367 		if (ifr == NULL) {
1368 			error = EAFNOSUPPORT;
1369 			break;
1370 		}
1371 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
1372 #ifdef INET
1373 		case AF_INET:
1374 			break;
1375 #endif
1376 #ifdef INET6
1377 		case AF_INET6:
1378 			break;
1379 #endif
1380 		default:
1381 			error = EAFNOSUPPORT;
1382 			break;
1383 		}
1384 		break;
1385 	case GRESPROTO:
1386 		gre_clearconf(sp, false);
1387 		oproto = sp->sp_proto;
1388 		otype = sp->sp_type;
1389 		sp->sp_proto = ifr->ifr_flags;
1390 		switch (sp->sp_proto) {
1391 		case IPPROTO_UDP:
1392 			ifp->if_flags |= IFF_LINK0|IFF_LINK2;
1393 			sp->sp_type = SOCK_DGRAM;
1394 			break;
1395 		case IPPROTO_GRE:
1396 			ifp->if_flags |= IFF_LINK0;
1397 			ifp->if_flags &= ~IFF_LINK2;
1398 			sp->sp_type = SOCK_RAW;
1399 			break;
1400 		case 0:
1401 			ifp->if_flags &= ~IFF_LINK0;
1402 			ifp->if_flags |= IFF_LINK2;
1403 			sp->sp_type = 0;
1404 			break;
1405 		default:
1406 			error = EPROTONOSUPPORT;
1407 			break;
1408 		}
1409 		if ((oproto == sp->sp_proto || sp->sp_proto == 0) &&
1410 		    (otype == sp->sp_type || sp->sp_type == 0))
1411 			break;
1412 		switch (sp->sp_proto) {
1413 		case IPPROTO_UDP:
1414 		case IPPROTO_GRE:
1415 			goto mksocket;
1416 		default:
1417 			break;
1418 		}
1419 		break;
1420 	case GREGPROTO:
1421 		ifr->ifr_flags = sp->sp_proto;
1422 		break;
1423 	case GRESADDRS:
1424 	case GRESADDRD:
1425 		gre_clearconf(sp, false);
1426 		/* set tunnel endpoints and mark interface as up */
1427 		switch (cmd) {
1428 		case GRESADDRS:
1429 			sockaddr_copy(sstosa(&sp->sp_src),
1430 			    sizeof(sp->sp_src), ifreq_getaddr(cmd, ifr));
1431 			break;
1432 		case GRESADDRD:
1433 			sockaddr_copy(sstosa(&sp->sp_dst),
1434 			    sizeof(sp->sp_dst), ifreq_getaddr(cmd, ifr));
1435 			break;
1436 		}
1437 	checkaddr:
1438 		if (sockaddr_any(sstosa(&sp->sp_src)) == NULL ||
1439 		    sockaddr_any(sstosa(&sp->sp_dst)) == NULL) {
1440 			error = EINVAL;
1441 			break;
1442 		}
1443 		/* let gre_socreate() check the rest */
1444 	mksocket:
1445 		GRE_DPRINTF(sc, "\n");
1446 		/* If we're administratively down, or the configuration
1447 		 * is empty, there's no use creating a socket.
1448 		 */
1449 		if ((ifp->if_flags & IFF_UP) == 0 || gre_is_nullconf(sp))
1450 			goto sendconf;
1451 
1452 		GRE_DPRINTF(sc, "\n");
1453 		fd = 0;
1454 		error = gre_socreate(sc, sp, &fd);
1455 		if (error != 0)
1456 			break;
1457 
1458 	setsock:
1459 		GRE_DPRINTF(sc, "\n");
1460 
1461 		error = gre_ssock(ifp, sp, fd);
1462 
1463 		if (cmd != GRESSOCK) {
1464 			GRE_DPRINTF(sc, "\n");
1465 			/* XXX v. dodgy */
1466 			if (fd_getfile(fd) != NULL)
1467 				fd_close(fd);
1468 		}
1469 
1470 		if (error == 0) {
1471 	sendconf:
1472 			GRE_DPRINTF(sc, "\n");
1473 			ifp->if_flags &= ~IFF_RUNNING;
1474 			gre_reconf(sc, sp);
1475 		}
1476 
1477 		break;
1478 	case GREGADDRS:
1479 		ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_src));
1480 		break;
1481 	case GREGADDRD:
1482 		ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_dst));
1483 		break;
1484 	case GREDSOCK:
1485 		GRE_DPRINTF(sc, "\n");
1486 		if (sp->sp_bysock)
1487 			ifp->if_flags &= ~IFF_UP;
1488 		gre_clearconf(sp, false);
1489 		goto mksocket;
1490 	case GRESSOCK:
1491 		GRE_DPRINTF(sc, "\n");
1492 		gre_clearconf(sp, true);
1493 		fd = (int)ifr->ifr_value;
1494 		sp->sp_bysock = true;
1495 		ifp->if_flags |= IFF_UP;
1496 		goto setsock;
1497 	case SIOCSLIFPHYADDR:
1498 		GRE_DPRINTF(sc, "\n");
1499 		if (lifr->addr.ss_family != lifr->dstaddr.ss_family) {
1500 			error = EAFNOSUPPORT;
1501 			break;
1502 		}
1503 		sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
1504 		    sstosa(&lifr->addr));
1505 		sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
1506 		    sstosa(&lifr->dstaddr));
1507 		GRE_DPRINTF(sc, "\n");
1508 		goto checkaddr;
1509 	case SIOCDIFPHYADDR:
1510 		GRE_DPRINTF(sc, "\n");
1511 		gre_clearconf(sp, true);
1512 		ifp->if_flags &= ~IFF_UP;
1513 		goto mksocket;
1514 	case SIOCGLIFPHYADDR:
1515 		GRE_DPRINTF(sc, "\n");
1516 		if (gre_is_nullconf(sp)) {
1517 			error = EADDRNOTAVAIL;
1518 			break;
1519 		}
1520 		sockaddr_copy(sstosa(&lifr->addr), sizeof(lifr->addr),
1521 		    sstosa(&sp->sp_src));
1522 		sockaddr_copy(sstosa(&lifr->dstaddr), sizeof(lifr->dstaddr),
1523 		    sstosa(&sp->sp_dst));
1524 		GRE_DPRINTF(sc, "\n");
1525 		break;
1526 	default:
1527 		error = ifioctl_common(ifp, cmd, data);
1528 		break;
1529 	}
1530 out:
1531 	GRE_DPRINTF(sc, "\n");
1532 	splx(s);
1533 	gre_ioctl_unlock(sc);
1534 	return error;
1535 }
1536 
1537 void	greattach(int);
1538 
1539 /* ARGSUSED */
1540 void
1541 greattach(int count)
1542 {
1543 	if_clone_attach(&gre_cloner);
1544 }
1545