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