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