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