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