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