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