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