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