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