if_gre.c revision 1.98.6.7 1 /* $NetBSD: if_gre.c,v 1.98.6.7 2007/11/27 19:38:56 joerg 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 (at) pilhuhn.de>
9 *
10 * IPv6-over-GRE contributed by Gert Doering <gert (at) 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.98.6.7 2007/11/27 19:38:56 joerg 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) &&
428 (m = m_pullup(m, sizeof(*gh))) == NULL) {
429 GRE_DPRINTF(sc, "%s: m_pullup failed\n", __func__);
430 sc->sc_pullup_ev.ev_count++;
431 return;
432 }
433 gh = mtod(m, const struct gre_h *);
434
435 if (gre_input(sc, m, 0, gh) == 0) {
436 sc->sc_unsupp_ev.ev_count++;
437 GRE_DPRINTF(sc, "%s: dropping unsupported\n", __func__);
438 m_freem(m);
439 }
440 }
441
442 static void
443 gre_upcall_add(struct socket *so, void *arg)
444 {
445 /* XXX What if the kernel already set an upcall? */
446 KASSERT((so->so_rcv.sb_flags & SB_UPCALL) == 0);
447 so->so_upcallarg = arg;
448 so->so_upcall = gre_receive;
449 so->so_rcv.sb_flags |= SB_UPCALL;
450 }
451
452 static void
453 gre_upcall_remove(struct socket *so)
454 {
455 so->so_rcv.sb_flags &= ~SB_UPCALL;
456 so->so_upcallarg = NULL;
457 so->so_upcall = NULL;
458 }
459
460 static int
461 gre_socreate(struct gre_softc *sc, struct lwp *l,
462 struct gre_soparm *sp, int *fdout)
463 {
464 const struct protosw *pr;
465 int fd, rc;
466 struct mbuf *m;
467 struct sockaddr *sa;
468 sa_family_t af;
469 struct socket *so;
470
471 GRE_DPRINTF(sc, "%s: enter\n", __func__);
472
473 af = sp->sp_src.ss_family;
474 rc = fsocreate(af, &so, sp->sp_type, sp->sp_proto, l, &fd);
475 if (rc != 0) {
476 GRE_DPRINTF(sc, "%s: fsocreate failed\n", __func__);
477 return rc;
478 }
479
480 if ((m = getsombuf(so, MT_SONAME)) == NULL) {
481 rc = ENOBUFS;
482 goto out;
483 }
484 sa = mtod(m, struct sockaddr *);
485 sockaddr_copy(sa, MIN(MLEN, sizeof(sp->sp_src)), sstosa(&sp->sp_src));
486 m->m_len = sp->sp_src.ss_len;
487
488 #if 0
489 /* XXX */
490 GRE_DPRINTF(sc, "%s: bind 0x%08" PRIx32 " port %d\n", __func__,
491 sin->sin_addr.s_addr, ntohs(sin->sin_port));
492 #endif
493 if ((rc = sobind(so, m, l)) != 0) {
494 GRE_DPRINTF(sc, "%s: sobind failed\n", __func__);
495 goto out;
496 }
497
498 if ((rc = gre_getsockname(so, m, l)) != 0) {
499 GRE_DPRINTF(sc, "%s: gre_getsockname\n", __func__);
500 goto out;
501 }
502 sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src), sa);
503
504 sockaddr_copy(sa, MIN(MLEN, sizeof(sp->sp_dst)), sstosa(&sp->sp_dst));
505 m->m_len = sp->sp_dst.ss_len;
506
507 if ((rc = soconnect(so, m, l)) != 0) {
508 GRE_DPRINTF(sc, "%s: soconnect failed\n", __func__);
509 goto out;
510 }
511
512 /* XXX convert to a (new) SOL_SOCKET call */
513 *mtod(m, int *) = ip_gre_ttl;
514 m->m_len = sizeof(int);
515 pr = so->so_proto;
516 KASSERT(pr != NULL);
517 rc = sosetopt(so, IPPROTO_IP, IP_TTL, m);
518 m = NULL;
519 if (rc != 0) {
520 GRE_DPRINTF(sc, "%s: sosetopt ttl failed\n", __func__);
521 rc = 0;
522 }
523 rc = sosetopt(so, SOL_SOCKET, SO_NOHEADER, m_intopt(so, 1));
524 if (rc != 0) {
525 GRE_DPRINTF(sc, "%s: sosetopt SO_NOHEADER failed\n", __func__);
526 rc = 0;
527 }
528 out:
529 m_freem(m);
530
531 if (rc != 0)
532 fdrelease(l, fd);
533 else
534 *fdout = fd;
535
536 return rc;
537 }
538
539 static int
540 gre_sosend(struct socket *so, struct mbuf *top, struct lwp *l)
541 {
542 struct mbuf **mp;
543 struct proc *p;
544 long space, resid;
545 int error, s;
546
547 p = l->l_proc;
548
549 resid = top->m_pkthdr.len;
550 if (p)
551 p->p_stats->p_ru.ru_msgsnd++;
552 #define snderr(errno) { error = errno; splx(s); goto release; }
553
554 if ((error = sblock(&so->so_snd, M_NOWAIT)) != 0)
555 goto out;
556 s = splsoftnet();
557 if (so->so_state & SS_CANTSENDMORE)
558 snderr(EPIPE);
559 if (so->so_error) {
560 error = so->so_error;
561 so->so_error = 0;
562 splx(s);
563 goto release;
564 }
565 if ((so->so_state & SS_ISCONNECTED) == 0) {
566 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
567 if ((so->so_state & SS_ISCONFIRMING) == 0)
568 snderr(ENOTCONN);
569 } else
570 snderr(EDESTADDRREQ);
571 }
572 space = sbspace(&so->so_snd);
573 if (resid > so->so_snd.sb_hiwat)
574 snderr(EMSGSIZE);
575 if (space < resid)
576 snderr(EWOULDBLOCK);
577 splx(s);
578 mp = ⊤
579 /*
580 * Data is prepackaged in "top".
581 */
582 s = splsoftnet();
583
584 if (so->so_state & SS_CANTSENDMORE)
585 snderr(EPIPE);
586
587 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, top, NULL, NULL, l);
588 splx(s);
589
590 top = NULL;
591 mp = ⊤
592 if (error != 0)
593 goto release;
594
595 release:
596 sbunlock(&so->so_snd);
597 out:
598 if (top != NULL)
599 m_freem(top);
600 return error;
601 }
602
603 /* This is a stripped-down version of soreceive() that will never
604 * block. It will support SOCK_DGRAM sockets. It may also support
605 * SOCK_SEQPACKET sockets.
606 */
607 static int
608 gre_soreceive(struct socket *so, struct mbuf **mp0)
609 {
610 struct lwp *l = curlwp;
611 struct mbuf *m, **mp;
612 int flags, len, error, s, type;
613 const struct protosw *pr;
614 struct mbuf *nextrecord;
615
616 KASSERT(mp0 != NULL);
617
618 flags = MSG_DONTWAIT;
619 pr = so->so_proto;
620 mp = mp0;
621 type = 0;
622
623 *mp = NULL;
624
625 KASSERT(pr->pr_flags & PR_ATOMIC);
626
627 if (so->so_state & SS_ISCONFIRMING)
628 (*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, l);
629
630 restart:
631 if ((error = sblock(&so->so_rcv, M_NOWAIT)) != 0)
632 return error;
633 s = splsoftnet();
634
635 m = so->so_rcv.sb_mb;
636 /*
637 * If we have less data than requested, do not block awaiting more.
638 */
639 if (m == NULL) {
640 #ifdef DIAGNOSTIC
641 if (so->so_rcv.sb_cc)
642 panic("receive 1");
643 #endif
644 if (so->so_error) {
645 error = so->so_error;
646 so->so_error = 0;
647 } else if (so->so_state & SS_CANTRCVMORE)
648 ;
649 else if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0
650 && (so->so_proto->pr_flags & PR_CONNREQUIRED))
651 error = ENOTCONN;
652 else
653 error = EWOULDBLOCK;
654 goto release;
655 }
656 /*
657 * On entry here, m points to the first record of the socket buffer.
658 * While we process the initial mbufs containing address and control
659 * info, we save a copy of m->m_nextpkt into nextrecord.
660 */
661 if (l != NULL)
662 l->l_proc->p_stats->p_ru.ru_msgrcv++;
663 KASSERT(m == so->so_rcv.sb_mb);
664 SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
665 SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
666 nextrecord = m->m_nextpkt;
667 if (pr->pr_flags & PR_ADDR) {
668 #ifdef DIAGNOSTIC
669 if (m->m_type != MT_SONAME)
670 panic("receive 1a");
671 #endif
672 sbfree(&so->so_rcv, m);
673 MFREE(m, so->so_rcv.sb_mb);
674 m = so->so_rcv.sb_mb;
675 }
676 while (m != NULL && m->m_type == MT_CONTROL && error == 0) {
677 sbfree(&so->so_rcv, m);
678 /*
679 * Dispose of any SCM_RIGHTS message that went
680 * through the read path rather than recv.
681 */
682 if (pr->pr_domain->dom_dispose &&
683 mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
684 (*pr->pr_domain->dom_dispose)(m);
685 MFREE(m, so->so_rcv.sb_mb);
686 m = so->so_rcv.sb_mb;
687 }
688
689 /*
690 * If m is non-NULL, we have some data to read. From now on,
691 * make sure to keep sb_lastrecord consistent when working on
692 * the last packet on the chain (nextrecord == NULL) and we
693 * change m->m_nextpkt.
694 */
695 if (m != NULL) {
696 m->m_nextpkt = nextrecord;
697 /*
698 * If nextrecord == NULL (this is a single chain),
699 * then sb_lastrecord may not be valid here if m
700 * was changed earlier.
701 */
702 if (nextrecord == NULL) {
703 KASSERT(so->so_rcv.sb_mb == m);
704 so->so_rcv.sb_lastrecord = m;
705 }
706 type = m->m_type;
707 if (type == MT_OOBDATA)
708 flags |= MSG_OOB;
709 } else {
710 KASSERT(so->so_rcv.sb_mb == m);
711 so->so_rcv.sb_mb = nextrecord;
712 SB_EMPTY_FIXUP(&so->so_rcv);
713 }
714 SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
715 SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
716
717 while (m != NULL) {
718 if (m->m_type == MT_OOBDATA) {
719 if (type != MT_OOBDATA)
720 break;
721 } else if (type == MT_OOBDATA)
722 break;
723 #ifdef DIAGNOSTIC
724 else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
725 panic("receive 3");
726 #endif
727 so->so_state &= ~SS_RCVATMARK;
728 if (so->so_oobmark != 0 && so->so_oobmark < m->m_len)
729 break;
730 len = m->m_len;
731 /*
732 * mp is set, just pass back the mbufs.
733 * Sockbuf must be consistent here (points to current mbuf,
734 * it points to next record) when we drop priority;
735 * we must note any additions to the sockbuf when we
736 * block interrupts again.
737 */
738 if (m->m_flags & M_EOR)
739 flags |= MSG_EOR;
740 nextrecord = m->m_nextpkt;
741 sbfree(&so->so_rcv, m);
742 *mp = m;
743 mp = &m->m_next;
744 so->so_rcv.sb_mb = m = m->m_next;
745 *mp = NULL;
746 /*
747 * If m != NULL, we also know that
748 * so->so_rcv.sb_mb != NULL.
749 */
750 KASSERT(so->so_rcv.sb_mb == m);
751 if (m) {
752 m->m_nextpkt = nextrecord;
753 if (nextrecord == NULL)
754 so->so_rcv.sb_lastrecord = m;
755 } else {
756 so->so_rcv.sb_mb = nextrecord;
757 SB_EMPTY_FIXUP(&so->so_rcv);
758 }
759 SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
760 SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
761 if (so->so_oobmark) {
762 so->so_oobmark -= len;
763 if (so->so_oobmark == 0) {
764 so->so_state |= SS_RCVATMARK;
765 break;
766 }
767 }
768 if (flags & MSG_EOR)
769 break;
770 }
771
772 if (m != NULL) {
773 m_freem(*mp);
774 *mp = NULL;
775 error = ENOMEM;
776 (void) sbdroprecord(&so->so_rcv);
777 } else {
778 /*
779 * First part is an inline SB_EMPTY_FIXUP(). Second
780 * part makes sure sb_lastrecord is up-to-date if
781 * there is still data in the socket buffer.
782 */
783 so->so_rcv.sb_mb = nextrecord;
784 if (so->so_rcv.sb_mb == NULL) {
785 so->so_rcv.sb_mbtail = NULL;
786 so->so_rcv.sb_lastrecord = NULL;
787 } else if (nextrecord->m_nextpkt == NULL)
788 so->so_rcv.sb_lastrecord = nextrecord;
789 }
790 SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
791 SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
792 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
793 (*pr->pr_usrreq)(so, PRU_RCVD, NULL,
794 (struct mbuf *)(long)flags, NULL, l);
795 if (*mp0 == NULL && (flags & MSG_EOR) == 0 &&
796 (so->so_state & SS_CANTRCVMORE) == 0) {
797 sbunlock(&so->so_rcv);
798 splx(s);
799 goto restart;
800 }
801
802 release:
803 sbunlock(&so->so_rcv);
804 splx(s);
805 return error;
806 }
807
808 static struct socket *
809 gre_reconf(struct gre_softc *sc, struct socket *so, lwp_t *l,
810 const struct gre_soparm *newsoparm)
811 {
812 int rc;
813 struct file *fp;
814 struct ifnet *ifp = &sc->sc_if;
815
816 GRE_DPRINTF(sc, "%s: enter\n", __func__);
817
818 shutdown:
819 if (sc->sc_soparm.sp_fd != -1) {
820 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
821 gre_upcall_remove(so);
822 softint_disestablish(sc->sc_si);
823 sc->sc_si = NULL;
824 fdrelease(l, sc->sc_soparm.sp_fd);
825 gre_clearconf(&sc->sc_soparm, false);
826 sc->sc_soparm.sp_fd = -1;
827 so = NULL;
828 }
829
830 if (newsoparm != NULL) {
831 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
832 sc->sc_soparm = *newsoparm;
833 }
834
835 if (sc->sc_soparm.sp_fd != -1) {
836 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
837 rc = getsock(l->l_proc->p_fd, sc->sc_soparm.sp_fd, &fp);
838 if (rc != 0)
839 goto shutdown;
840 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
841 FILE_UNUSE(fp, NULL);
842 so = (struct socket *)fp->f_data;
843 sc->sc_si = softint_establish(SOFTINT_NET, greintr, sc);
844 gre_upcall_add(so, sc);
845 if ((ifp->if_flags & IFF_UP) == 0) {
846 GRE_DPRINTF(sc, "%s: down\n", __func__);
847 goto shutdown;
848 }
849 }
850
851 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
852 if (so != NULL)
853 sc->sc_if.if_flags |= IFF_RUNNING;
854 else {
855 gre_bufq_purge(&sc->sc_snd);
856 sc->sc_if.if_flags &= ~IFF_RUNNING;
857 }
858 return so;
859 }
860
861 static int
862 gre_input(struct gre_softc *sc, struct mbuf *m, int hlen,
863 const struct gre_h *gh)
864 {
865 u_int16_t flags;
866 u_int32_t af; /* af passed to BPF tap */
867 int isr, s;
868 struct ifqueue *ifq;
869
870 sc->sc_if.if_ipackets++;
871 sc->sc_if.if_ibytes += m->m_pkthdr.len;
872
873 hlen += sizeof(struct gre_h);
874
875 /* process GRE flags as packet can be of variable len */
876 flags = ntohs(gh->flags);
877
878 /* Checksum & Offset are present */
879 if ((flags & GRE_CP) | (flags & GRE_RP))
880 hlen += 4;
881 /* We don't support routing fields (variable length) */
882 if (flags & GRE_RP) {
883 sc->sc_if.if_ierrors++;
884 return 0;
885 }
886 if (flags & GRE_KP)
887 hlen += 4;
888 if (flags & GRE_SP)
889 hlen += 4;
890
891 switch (ntohs(gh->ptype)) { /* ethertypes */
892 case ETHERTYPE_IP:
893 ifq = &ipintrq;
894 isr = NETISR_IP;
895 af = AF_INET;
896 break;
897 #ifdef NETATALK
898 case ETHERTYPE_ATALK:
899 ifq = &atintrq1;
900 isr = NETISR_ATALK;
901 af = AF_APPLETALK;
902 break;
903 #endif
904 #ifdef INET6
905 case ETHERTYPE_IPV6:
906 ifq = &ip6intrq;
907 isr = NETISR_IPV6;
908 af = AF_INET6;
909 break;
910 #endif
911 default: /* others not yet supported */
912 GRE_DPRINTF(sc, "%s: unhandled ethertype 0x%04x\n", __func__,
913 ntohs(gh->ptype));
914 sc->sc_if.if_noproto++;
915 return 0;
916 }
917
918 if (hlen > m->m_pkthdr.len) {
919 m_freem(m);
920 sc->sc_if.if_ierrors++;
921 return EINVAL;
922 }
923 m_adj(m, hlen);
924
925 #if NBPFILTER > 0
926 if (sc->sc_if.if_bpf != NULL)
927 bpf_mtap_af(sc->sc_if.if_bpf, af, m);
928 #endif /*NBPFILTER > 0*/
929
930 m->m_pkthdr.rcvif = &sc->sc_if;
931
932 s = splnet();
933 if (IF_QFULL(ifq)) {
934 IF_DROP(ifq);
935 m_freem(m);
936 } else {
937 IF_ENQUEUE(ifq, m);
938 }
939 /* we need schednetisr since the address family may change */
940 schednetisr(isr);
941 splx(s);
942
943 return 1; /* packet is done, no further processing needed */
944 }
945
946 /*
947 * The output routine. Takes a packet and encapsulates it in the protocol
948 * given by sc->sc_soparm.sp_proto. See also RFC 1701 and RFC 2004
949 */
950 static int
951 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
952 struct rtentry *rt)
953 {
954 int error = 0;
955 struct gre_softc *sc = ifp->if_softc;
956 struct gre_h *gh;
957 struct ip *ip;
958 u_int8_t ip_tos = 0;
959 u_int16_t etype = 0;
960
961 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
962 m_freem(m);
963 error = ENETDOWN;
964 goto end;
965 }
966
967 #if NBPFILTER > 0
968 if (ifp->if_bpf != NULL)
969 bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
970 #endif
971
972 m->m_flags &= ~(M_BCAST|M_MCAST);
973
974 GRE_DPRINTF(sc, "%s: dst->sa_family=%d\n", __func__, dst->sa_family);
975 switch (dst->sa_family) {
976 case AF_INET:
977 ip = mtod(m, struct ip *);
978 ip_tos = ip->ip_tos;
979 etype = htons(ETHERTYPE_IP);
980 break;
981 #ifdef NETATALK
982 case AF_APPLETALK:
983 etype = htons(ETHERTYPE_ATALK);
984 break;
985 #endif
986 #ifdef INET6
987 case AF_INET6:
988 etype = htons(ETHERTYPE_IPV6);
989 break;
990 #endif
991 default:
992 IF_DROP(&ifp->if_snd);
993 m_freem(m);
994 error = EAFNOSUPPORT;
995 goto end;
996 }
997
998 M_PREPEND(m, sizeof(*gh), M_DONTWAIT);
999
1000 if (m == NULL) {
1001 IF_DROP(&ifp->if_snd);
1002 error = ENOBUFS;
1003 goto end;
1004 }
1005
1006 gh = mtod(m, struct gre_h *);
1007 gh->flags = 0;
1008 gh->ptype = etype;
1009 /* XXX Need to handle IP ToS. Look at how I handle IP TTL. */
1010
1011 ifp->if_opackets++;
1012 ifp->if_obytes += m->m_pkthdr.len;
1013
1014 /* send it off */
1015 if ((error = gre_bufq_enqueue(&sc->sc_snd, m)) != 0) {
1016 sc->sc_oflow_ev.ev_count++;
1017 m_freem(m);
1018 } else
1019 softint_schedule(sc->sc_si);
1020 end:
1021 if (error)
1022 ifp->if_oerrors++;
1023 return error;
1024 }
1025
1026 static int
1027 gre_getname(struct socket *so, int req, struct mbuf *nam, struct lwp *l)
1028 {
1029 return (*so->so_proto->pr_usrreq)(so, req, NULL, nam, NULL, l);
1030 }
1031
1032 static int
1033 gre_getsockname(struct socket *so, struct mbuf *nam, struct lwp *l)
1034 {
1035 return gre_getname(so, PRU_SOCKADDR, nam, l);
1036 }
1037
1038 static int
1039 gre_getpeername(struct socket *so, struct mbuf *nam, struct lwp *l)
1040 {
1041 return gre_getname(so, PRU_PEERADDR, nam, l);
1042 }
1043
1044 static int
1045 gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_storage *src,
1046 struct sockaddr_storage *dst)
1047 {
1048 struct mbuf *m;
1049 struct sockaddr_storage *ss;
1050 int rc;
1051
1052 if ((m = getsombuf(so, MT_SONAME)) == NULL)
1053 return ENOBUFS;
1054
1055 ss = mtod(m, struct sockaddr_storage *);
1056
1057 if ((rc = gre_getsockname(so, m, l)) != 0)
1058 goto out;
1059 *src = *ss;
1060
1061 if ((rc = gre_getpeername(so, m, l)) != 0)
1062 goto out;
1063 *dst = *ss;
1064
1065 out:
1066 m_freem(m);
1067 return rc;
1068 }
1069
1070 static void
1071 gre_closef(struct file **fpp, struct lwp *l)
1072 {
1073 struct file *fp = *fpp;
1074
1075 mutex_enter(&fp->f_lock);
1076 FILE_USE(fp);
1077 closef(fp, l);
1078 *fpp = NULL;
1079 }
1080
1081 static int
1082 gre_ssock(struct ifnet *ifp, struct gre_soparm *sp, int fd)
1083 {
1084 int error, kfd;
1085 const struct protosw *pr;
1086 struct file *fp;
1087 struct filedesc *fdp;
1088 struct gre_softc *sc = ifp->if_softc;
1089 struct lwp *l = curlwp;
1090 struct proc *kp, *p = curproc;
1091 struct socket *so;
1092 struct sockaddr_storage dst, src;
1093
1094 /* getsock() will FILE_USE() and unlock the descriptor for us */
1095 if ((error = getsock(p->p_fd, fd, &fp)) != 0) {
1096 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1097 return EINVAL;
1098 }
1099
1100 /* Increase reference count. Now that our reference to
1101 * the file descriptor is counted, this thread can release
1102 * our "use" of the descriptor, but it will not be destroyed
1103 * by some other thread's action. This thread needs to
1104 * release its use, too, because one and only one thread
1105 * can have use of the descriptor at once. The kernel
1106 * thread will pick up the use if it needs it.
1107 */
1108 fp->f_count++;
1109 GRE_DPRINTF(sc, "%s: l.%d f_count %d\n", __func__, __LINE__,
1110 fp->f_count);
1111 FILE_UNUSE(fp, NULL);
1112
1113 kp = sc->sc_lwp->l_proc;
1114 while ((error = fdalloc(kp, 0, &kfd)) != 0 && error == ENOSPC)
1115 fdexpand(kp);
1116 if (error != 0)
1117 goto closef;
1118 fdp = kp->p_fd;
1119 rw_enter(&fdp->fd_lock, RW_WRITER);
1120 fdp->fd_ofiles[kfd] = fp;
1121 rw_exit(&fdp->fd_lock);
1122
1123 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1124
1125 so = (struct socket *)fp->f_data;
1126 pr = so->so_proto;
1127 if ((pr->pr_flags & PR_ATOMIC) == 0 ||
1128 (sp->sp_type != 0 && pr->pr_type != sp->sp_type) ||
1129 (sp->sp_proto != 0 && pr->pr_protocol != 0 &&
1130 pr->pr_protocol != sp->sp_proto)) {
1131 GRE_DPRINTF(sc, "%s: l.%d, type %d, proto %d\n", __func__,
1132 __LINE__, pr->pr_type, pr->pr_protocol);
1133 error = EINVAL;
1134 goto release;
1135 }
1136
1137 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1138
1139 /* check address */
1140 if ((error = gre_getnames(so, l, &src, &dst)) != 0)
1141 goto release;
1142
1143 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1144
1145 if (error != 0)
1146 goto release;
1147
1148 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1149
1150 sp->sp_src = src;
1151 sp->sp_dst = dst;
1152 /* fp does not any longer belong to this thread. */
1153 sp->sp_fd = kfd;
1154
1155 /* XXX print src & dst */
1156
1157 return 0;
1158 release:
1159 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1160 fdrelease(sc->sc_lwp, kfd);
1161 return error;
1162 closef:
1163 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1164 gre_closef(&fp, l);
1165 return error;
1166 }
1167
1168 static bool
1169 sockaddr_is_anyaddr(const struct sockaddr *sa)
1170 {
1171 socklen_t anylen, salen;
1172 const void *anyaddr, *addr;
1173
1174 if ((anyaddr = sockaddr_anyaddr(sa, &anylen)) == NULL ||
1175 (addr = sockaddr_const_addr(sa, &salen)) == NULL)
1176 return false;
1177
1178 if (salen > anylen)
1179 return false;
1180
1181 return memcmp(anyaddr, addr, MIN(anylen, salen)) == 0;
1182 }
1183
1184 static bool
1185 gre_is_nullconf(const struct gre_soparm *sp)
1186 {
1187 return sockaddr_is_anyaddr(sstocsa(&sp->sp_src)) ||
1188 sockaddr_is_anyaddr(sstocsa(&sp->sp_dst));
1189 }
1190
1191 static void
1192 gre_clearconf(struct gre_soparm *sp, bool force)
1193 {
1194 if (sp->sp_bysock || force) {
1195 sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
1196 sockaddr_any(sstosa(&sp->sp_src)));
1197 sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
1198 sockaddr_any(sstosa(&sp->sp_dst)));
1199 sp->sp_bysock = 0;
1200 }
1201 sp->sp_fd = -1;
1202 }
1203
1204 static int
1205 gre_ioctl_lock(struct gre_softc *sc)
1206 {
1207 mutex_enter(&sc->sc_mtx);
1208
1209 while (sc->sc_state == GRE_S_IOCTL)
1210 gre_wait(sc);
1211
1212 if (sc->sc_state != GRE_S_IDLE) {
1213 cv_signal(&sc->sc_condvar);
1214 mutex_exit(&sc->sc_mtx);
1215 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1216 return ENXIO;
1217 }
1218
1219 sc->sc_state = GRE_S_IOCTL;
1220
1221 mutex_exit(&sc->sc_mtx);
1222 return 0;
1223 }
1224
1225 static void
1226 gre_ioctl_unlock(struct gre_softc *sc)
1227 {
1228 mutex_enter(&sc->sc_mtx);
1229
1230 KASSERT(sc->sc_state == GRE_S_IOCTL);
1231 sc->sc_state = GRE_S_IDLE;
1232 cv_signal(&sc->sc_condvar);
1233
1234 mutex_exit(&sc->sc_mtx);
1235 }
1236
1237 static int
1238 gre_ioctl(struct ifnet *ifp, const u_long cmd, void *data)
1239 {
1240 struct lwp *l = curlwp;
1241 struct ifreq *ifr;
1242 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
1243 struct gre_softc *sc = ifp->if_softc;
1244 struct gre_soparm *sp;
1245 int fd, error = 0, oproto, otype, s;
1246 struct gre_soparm sp0;
1247
1248 ifr = data;
1249
1250 GRE_DPRINTF(sc, "%s: l.%d, cmd %lu\n", __func__, __LINE__, cmd);
1251
1252 switch (cmd) {
1253 case SIOCSIFFLAGS:
1254 case SIOCSIFMTU:
1255 case GRESPROTO:
1256 case GRESADDRD:
1257 case GRESADDRS:
1258 case GRESSOCK:
1259 case GREDSOCK:
1260 case SIOCSLIFPHYADDR:
1261 case SIOCDIFPHYADDR:
1262 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
1263 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1264 NULL) != 0)
1265 return EPERM;
1266 break;
1267 default:
1268 break;
1269 }
1270
1271 if ((error = gre_ioctl_lock(sc)) != 0) {
1272 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1273 return error;
1274 }
1275 s = splnet();
1276
1277 sp0 = sc->sc_soparm;
1278 sp0.sp_fd = -1;
1279 sp = &sp0;
1280
1281 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1282
1283 switch (cmd) {
1284 case SIOCSIFADDR:
1285 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1286 if ((ifp->if_flags & IFF_UP) != 0)
1287 break;
1288 gre_clearconf(sp, false);
1289 ifp->if_flags |= IFF_UP;
1290 goto mksocket;
1291 case SIOCSIFDSTADDR:
1292 break;
1293 case SIOCSIFFLAGS:
1294 oproto = sp->sp_proto;
1295 otype = sp->sp_type;
1296 switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
1297 case IFF_LINK0|IFF_LINK2:
1298 sp->sp_proto = IPPROTO_UDP;
1299 sp->sp_type = SOCK_DGRAM;
1300 break;
1301 case IFF_LINK2:
1302 sp->sp_proto = 0;
1303 sp->sp_type = 0;
1304 break;
1305 case IFF_LINK0:
1306 sp->sp_proto = IPPROTO_GRE;
1307 sp->sp_type = SOCK_RAW;
1308 break;
1309 default:
1310 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1311 error = EINVAL;
1312 goto out;
1313 }
1314 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1315 gre_clearconf(sp, false);
1316 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
1317 (IFF_UP|IFF_RUNNING) &&
1318 (oproto == sp->sp_proto || sp->sp_proto == 0) &&
1319 (otype == sp->sp_type || sp->sp_type == 0))
1320 break;
1321 switch (sp->sp_proto) {
1322 case IPPROTO_UDP:
1323 case IPPROTO_GRE:
1324 goto mksocket;
1325 default:
1326 break;
1327 }
1328 break;
1329 case SIOCSIFMTU:
1330 /* XXX determine MTU automatically by probing w/
1331 * XXX do-not-fragment packets?
1332 */
1333 if (ifr->ifr_mtu < 576) {
1334 error = EINVAL;
1335 break;
1336 }
1337 ifp->if_mtu = ifr->ifr_mtu;
1338 break;
1339 case SIOCGIFMTU:
1340 ifr->ifr_mtu = sc->sc_if.if_mtu;
1341 break;
1342 case SIOCADDMULTI:
1343 case SIOCDELMULTI:
1344 if (ifr == NULL) {
1345 error = EAFNOSUPPORT;
1346 break;
1347 }
1348 switch (ifreq_getaddr(cmd, ifr)->sa_family) {
1349 #ifdef INET
1350 case AF_INET:
1351 break;
1352 #endif
1353 #ifdef INET6
1354 case AF_INET6:
1355 break;
1356 #endif
1357 default:
1358 error = EAFNOSUPPORT;
1359 break;
1360 }
1361 break;
1362 case GRESPROTO:
1363 gre_clearconf(sp, false);
1364 oproto = sp->sp_proto;
1365 otype = sp->sp_type;
1366 sp->sp_proto = ifr->ifr_flags;
1367 switch (sp->sp_proto) {
1368 case IPPROTO_UDP:
1369 ifp->if_flags |= IFF_LINK0|IFF_LINK2;
1370 sp->sp_type = SOCK_DGRAM;
1371 break;
1372 case IPPROTO_GRE:
1373 ifp->if_flags |= IFF_LINK0;
1374 ifp->if_flags &= ~IFF_LINK2;
1375 sp->sp_type = SOCK_RAW;
1376 break;
1377 case 0:
1378 ifp->if_flags &= ~IFF_LINK0;
1379 ifp->if_flags |= IFF_LINK2;
1380 sp->sp_type = 0;
1381 break;
1382 default:
1383 error = EPROTONOSUPPORT;
1384 break;
1385 }
1386 if ((oproto == sp->sp_proto || sp->sp_proto == 0) &&
1387 (otype == sp->sp_type || sp->sp_type == 0))
1388 break;
1389 switch (sp->sp_proto) {
1390 case IPPROTO_UDP:
1391 case IPPROTO_GRE:
1392 goto mksocket;
1393 default:
1394 break;
1395 }
1396 break;
1397 case GREGPROTO:
1398 ifr->ifr_flags = sp->sp_proto;
1399 break;
1400 case GRESADDRS:
1401 case GRESADDRD:
1402 gre_clearconf(sp, false);
1403 /*
1404 * set tunnel endpoints, compute a less specific route
1405 * to the remote end and mark if as up
1406 */
1407 switch (cmd) {
1408 case GRESADDRS:
1409 sockaddr_copy(sstosa(&sp->sp_src),
1410 sizeof(sp->sp_src), ifreq_getaddr(cmd, ifr));
1411 break;
1412 case GRESADDRD:
1413 sockaddr_copy(sstosa(&sp->sp_dst),
1414 sizeof(sp->sp_dst), ifreq_getaddr(cmd, ifr));
1415 break;
1416 }
1417 checkaddr:
1418 if (sockaddr_any(sstosa(&sp->sp_src)) == NULL ||
1419 sockaddr_any(sstosa(&sp->sp_dst)) == NULL) {
1420 error = EINVAL;
1421 break;
1422 }
1423 /* let gre_socreate() check the rest */
1424 mksocket:
1425 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1426 /* If we're administratively down, or the configuration
1427 * is empty, there's no use creating a socket.
1428 */
1429 if ((ifp->if_flags & IFF_UP) == 0 || gre_is_nullconf(sp))
1430 goto sendconf;
1431
1432 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1433 error = gre_socreate(sc, l, sp, &fd);
1434
1435 if (error != 0)
1436 break;
1437
1438 setsock:
1439 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1440
1441 error = gre_ssock(ifp, sp, fd);
1442
1443 if (cmd != GRESSOCK) {
1444 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1445 fdrelease(l, fd);
1446 }
1447
1448 if (error == 0) {
1449 sendconf:
1450 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1451 ifp->if_flags &= ~IFF_RUNNING;
1452 sc->sc_so = gre_reconf(sc, sc->sc_so, sc->sc_lwp, sp);
1453 }
1454
1455 break;
1456 case GREGADDRS:
1457 ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_src));
1458 break;
1459 case GREGADDRD:
1460 ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_dst));
1461 break;
1462 case GREDSOCK:
1463 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1464 if (sp->sp_bysock)
1465 ifp->if_flags &= ~IFF_UP;
1466 gre_clearconf(sp, false);
1467 goto mksocket;
1468 case GRESSOCK:
1469 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1470 gre_clearconf(sp, true);
1471 fd = (int)ifr->ifr_value;
1472 sp->sp_bysock = 1;
1473 ifp->if_flags |= IFF_UP;
1474 goto setsock;
1475 case SIOCSLIFPHYADDR:
1476 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1477 if (lifr->addr.ss_family != lifr->dstaddr.ss_family) {
1478 error = EAFNOSUPPORT;
1479 break;
1480 }
1481 sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
1482 sstosa(&lifr->addr));
1483 sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
1484 sstosa(&lifr->dstaddr));
1485 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1486 goto checkaddr;
1487 case SIOCDIFPHYADDR:
1488 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1489 gre_clearconf(sp, true);
1490 ifp->if_flags &= ~IFF_UP;
1491 goto mksocket;
1492 case SIOCGLIFPHYADDR:
1493 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1494 if (gre_is_nullconf(sp)) {
1495 error = EADDRNOTAVAIL;
1496 break;
1497 }
1498 sockaddr_copy(sstosa(&lifr->addr), sizeof(lifr->addr),
1499 sstosa(&sp->sp_src));
1500 sockaddr_copy(sstosa(&lifr->dstaddr), sizeof(lifr->dstaddr),
1501 sstosa(&sp->sp_dst));
1502 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1503 break;
1504 default:
1505 error = EINVAL;
1506 break;
1507 }
1508 out:
1509 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1510 splx(s);
1511 gre_ioctl_unlock(sc);
1512 return error;
1513 }
1514
1515 #endif
1516
1517 void greattach(int);
1518
1519 /* ARGSUSED */
1520 void
1521 greattach(int count)
1522 {
1523 #ifdef INET
1524 if_clone_attach(&gre_cloner);
1525 #endif
1526 }
1527