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