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