1 /*- 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /*- 31 * Copyright (c) 1982, 1986, 1988, 1993 32 * The Regents of the University of California. All rights reserved. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions 36 * are met: 37 * 1. Redistributions of source code must retain the above copyright 38 * notice, this list of conditions and the following disclaimer. 39 * 2. Redistributions in binary form must reproduce the above copyright 40 * notice, this list of conditions and the following disclaimer in the 41 * documentation and/or other materials provided with the distribution. 42 * 4. Neither the name of the University nor the names of its contributors 43 * may be used to endorse or promote products derived from this software 44 * without specific prior written permission. 45 * 46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 56 * SUCH DAMAGE. 57 * 58 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94 59 */ 60 61 #include <sys/cdefs.h> 62 __FBSDID("$FreeBSD$"); 63 64 #include "opt_ipsec.h" 65 #include "opt_inet6.h" 66 67 #include <sys/param.h> 68 #include <sys/errno.h> 69 #include <sys/lock.h> 70 #include <sys/malloc.h> 71 #include <sys/mbuf.h> 72 #include <sys/priv.h> 73 #include <sys/proc.h> 74 #include <sys/protosw.h> 75 #include <sys/signalvar.h> 76 #include <sys/socket.h> 77 #include <sys/socketvar.h> 78 #include <sys/sx.h> 79 #include <sys/syslog.h> 80 81 #include <net/if.h> 82 #include <net/if_types.h> 83 #include <net/route.h> 84 85 #include <netinet/in.h> 86 #include <netinet/in_var.h> 87 #include <netinet/in_systm.h> 88 #include <netinet/icmp6.h> 89 #include <netinet/in_pcb.h> 90 #include <netinet/ip6.h> 91 #include <netinet6/ip6protosw.h> 92 #include <netinet6/ip6_mroute.h> 93 #include <netinet6/in6_pcb.h> 94 #include <netinet6/ip6_var.h> 95 #include <netinet6/nd6.h> 96 #include <netinet6/raw_ip6.h> 97 #include <netinet6/scope6_var.h> 98 99 #ifdef IPSEC 100 #include <netipsec/ipsec.h> 101 #include <netipsec/ipsec6.h> 102 #endif /* IPSEC */ 103 104 #include <machine/stdarg.h> 105 106 #define satosin6(sa) ((struct sockaddr_in6 *)(sa)) 107 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 108 109 /* 110 * Raw interface to IP6 protocol. 111 */ 112 113 extern struct inpcbhead ripcb; 114 extern struct inpcbinfo ripcbinfo; 115 extern u_long rip_sendspace; 116 extern u_long rip_recvspace; 117 118 struct rip6stat rip6stat; 119 120 /* 121 * Hooks for multicast forwarding. 122 */ 123 struct socket *ip6_mrouter = NULL; 124 int (*ip6_mrouter_set)(struct socket *, struct sockopt *); 125 int (*ip6_mrouter_get)(struct socket *, struct sockopt *); 126 int (*ip6_mrouter_done)(void); 127 int (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *); 128 int (*mrt6_ioctl)(int, caddr_t); 129 130 /* 131 * Setup generic address and protocol structures 132 * for raw_input routine, then pass them along with 133 * mbuf chain. 134 */ 135 int 136 rip6_input(struct mbuf **mp, int *offp, int proto) 137 { 138 struct mbuf *m = *mp; 139 register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 140 register struct inpcb *in6p; 141 struct inpcb *last = 0; 142 struct mbuf *opts = NULL; 143 struct sockaddr_in6 fromsa; 144 145 rip6stat.rip6s_ipackets++; 146 147 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 148 /* XXX send icmp6 host/port unreach? */ 149 m_freem(m); 150 return IPPROTO_DONE; 151 } 152 153 init_sin6(&fromsa, m); /* general init */ 154 155 INP_INFO_RLOCK(&ripcbinfo); 156 LIST_FOREACH(in6p, &ripcb, inp_list) { 157 INP_LOCK(in6p); 158 if ((in6p->in6p_vflag & INP_IPV6) == 0) { 159 docontinue: 160 INP_UNLOCK(in6p); 161 continue; 162 } 163 if (in6p->in6p_ip6_nxt && 164 in6p->in6p_ip6_nxt != proto) 165 goto docontinue; 166 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 167 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 168 goto docontinue; 169 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 170 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 171 goto docontinue; 172 if (in6p->in6p_cksum != -1) { 173 rip6stat.rip6s_isum++; 174 if (in6_cksum(m, proto, *offp, 175 m->m_pkthdr.len - *offp)) { 176 rip6stat.rip6s_badsum++; 177 goto docontinue; 178 } 179 } 180 if (last) { 181 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 182 183 #ifdef IPSEC 184 /* 185 * Check AH/ESP integrity. 186 */ 187 if (n && ipsec6_in_reject(n, last)) { 188 m_freem(n); 189 ipsec6stat.in_polvio++; 190 /* do not inject data into pcb */ 191 } else 192 #endif /* IPSEC */ 193 if (n) { 194 if (last->in6p_flags & IN6P_CONTROLOPTS || 195 last->in6p_socket->so_options & SO_TIMESTAMP) 196 ip6_savecontrol(last, n, &opts); 197 /* strip intermediate headers */ 198 m_adj(n, *offp); 199 if (sbappendaddr(&last->in6p_socket->so_rcv, 200 (struct sockaddr *)&fromsa, 201 n, opts) == 0) { 202 m_freem(n); 203 if (opts) 204 m_freem(opts); 205 rip6stat.rip6s_fullsock++; 206 } else 207 sorwakeup(last->in6p_socket); 208 opts = NULL; 209 } 210 INP_UNLOCK(last); 211 } 212 last = in6p; 213 } 214 #ifdef IPSEC 215 /* 216 * Check AH/ESP integrity. 217 */ 218 if (last && ipsec6_in_reject(m, last)) { 219 m_freem(m); 220 ipsec6stat.in_polvio++; 221 ip6stat.ip6s_delivered--; 222 /* do not inject data into pcb */ 223 INP_UNLOCK(last); 224 } else 225 #endif /* IPSEC */ 226 if (last) { 227 if (last->in6p_flags & IN6P_CONTROLOPTS || 228 last->in6p_socket->so_options & SO_TIMESTAMP) 229 ip6_savecontrol(last, m, &opts); 230 /* strip intermediate headers */ 231 m_adj(m, *offp); 232 if (sbappendaddr(&last->in6p_socket->so_rcv, 233 (struct sockaddr *)&fromsa, m, opts) == 0) { 234 m_freem(m); 235 if (opts) 236 m_freem(opts); 237 rip6stat.rip6s_fullsock++; 238 } else 239 sorwakeup(last->in6p_socket); 240 INP_UNLOCK(last); 241 } else { 242 rip6stat.rip6s_nosock++; 243 if (m->m_flags & M_MCAST) 244 rip6stat.rip6s_nosockmcast++; 245 if (proto == IPPROTO_NONE) 246 m_freem(m); 247 else { 248 char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */ 249 icmp6_error(m, ICMP6_PARAM_PROB, 250 ICMP6_PARAMPROB_NEXTHEADER, 251 prvnxtp - mtod(m, char *)); 252 } 253 ip6stat.ip6s_delivered--; 254 } 255 INP_INFO_RUNLOCK(&ripcbinfo); 256 return IPPROTO_DONE; 257 } 258 259 void 260 rip6_ctlinput(int cmd, struct sockaddr *sa, void *d) 261 { 262 struct ip6_hdr *ip6; 263 struct mbuf *m; 264 int off = 0; 265 struct ip6ctlparam *ip6cp = NULL; 266 const struct sockaddr_in6 *sa6_src = NULL; 267 void *cmdarg; 268 struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange; 269 270 if (sa->sa_family != AF_INET6 || 271 sa->sa_len != sizeof(struct sockaddr_in6)) 272 return; 273 274 if ((unsigned)cmd >= PRC_NCMDS) 275 return; 276 if (PRC_IS_REDIRECT(cmd)) 277 notify = in6_rtchange, d = NULL; 278 else if (cmd == PRC_HOSTDEAD) 279 d = NULL; 280 else if (inet6ctlerrmap[cmd] == 0) 281 return; 282 283 /* if the parameter is from icmp6, decode it. */ 284 if (d != NULL) { 285 ip6cp = (struct ip6ctlparam *)d; 286 m = ip6cp->ip6c_m; 287 ip6 = ip6cp->ip6c_ip6; 288 off = ip6cp->ip6c_off; 289 cmdarg = ip6cp->ip6c_cmdarg; 290 sa6_src = ip6cp->ip6c_src; 291 } else { 292 m = NULL; 293 ip6 = NULL; 294 cmdarg = NULL; 295 sa6_src = &sa6_any; 296 } 297 298 (void) in6_pcbnotify(&ripcbinfo, sa, 0, 299 (const struct sockaddr *)sa6_src, 300 0, cmd, cmdarg, notify); 301 } 302 303 /* 304 * Generate IPv6 header and pass packet to ip6_output. 305 * Tack on options user may have setup with control call. 306 */ 307 int 308 #if __STDC__ 309 rip6_output(struct mbuf *m, ...) 310 #else 311 rip6_output(m, va_alist) 312 struct mbuf *m; 313 va_dcl 314 #endif 315 { 316 struct mbuf *control; 317 struct socket *so; 318 struct sockaddr_in6 *dstsock; 319 struct in6_addr *dst; 320 struct ip6_hdr *ip6; 321 struct inpcb *in6p; 322 u_int plen = m->m_pkthdr.len; 323 int error = 0; 324 struct ip6_pktopts opt, *optp; 325 struct ifnet *oifp = NULL; 326 int type = 0, code = 0; /* for ICMPv6 output statistics only */ 327 int priv = 0; 328 int scope_ambiguous = 0; 329 struct in6_addr *in6a; 330 va_list ap; 331 332 va_start(ap, m); 333 so = va_arg(ap, struct socket *); 334 dstsock = va_arg(ap, struct sockaddr_in6 *); 335 control = va_arg(ap, struct mbuf *); 336 va_end(ap); 337 338 in6p = sotoin6pcb(so); 339 INP_LOCK(in6p); 340 341 priv = 0; 342 if (suser_cred(so->so_cred, 0) == 0) 343 priv = 1; 344 dst = &dstsock->sin6_addr; 345 if (control) { 346 if ((error = ip6_setpktopts(control, &opt, 347 in6p->in6p_outputopts, priv, so->so_proto->pr_protocol)) 348 != 0) { 349 goto bad; 350 } 351 optp = &opt; 352 } else 353 optp = in6p->in6p_outputopts; 354 355 /* 356 * Check and convert scope zone ID into internal form. 357 * XXX: we may still need to determine the zone later. 358 */ 359 if (!(so->so_state & SS_ISCONNECTED)) { 360 if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone) 361 scope_ambiguous = 1; 362 if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0) 363 goto bad; 364 } 365 366 /* 367 * For an ICMPv6 packet, we should know its type and code 368 * to update statistics. 369 */ 370 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 371 struct icmp6_hdr *icmp6; 372 if (m->m_len < sizeof(struct icmp6_hdr) && 373 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 374 error = ENOBUFS; 375 goto bad; 376 } 377 icmp6 = mtod(m, struct icmp6_hdr *); 378 type = icmp6->icmp6_type; 379 code = icmp6->icmp6_code; 380 } 381 382 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT); 383 if (m == NULL) { 384 error = ENOBUFS; 385 goto bad; 386 } 387 ip6 = mtod(m, struct ip6_hdr *); 388 389 /* 390 * Source address selection. 391 */ 392 if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions, NULL, 393 &in6p->in6p_laddr, &oifp, &error)) == NULL) { 394 if (error == 0) 395 error = EADDRNOTAVAIL; 396 goto bad; 397 } 398 ip6->ip6_src = *in6a; 399 400 if (oifp && scope_ambiguous) { 401 /* 402 * Application should provide a proper zone ID or the use of 403 * default zone IDs should be enabled. Unfortunately, some 404 * applications do not behave as it should, so we need a 405 * workaround. Even if an appropriate ID is not determined 406 * (when it's required), if we can determine the outgoing 407 * interface. determine the zone ID based on the interface. 408 */ 409 error = in6_setscope(&dstsock->sin6_addr, oifp, NULL); 410 if (error != 0) 411 goto bad; 412 } 413 ip6->ip6_dst = dstsock->sin6_addr; 414 415 /* fill in the rest of the IPv6 header fields */ 416 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 417 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK); 418 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 419 (IPV6_VERSION & IPV6_VERSION_MASK); 420 /* ip6_plen will be filled in ip6_output, so not fill it here. */ 421 ip6->ip6_nxt = in6p->in6p_ip6_nxt; 422 ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 423 424 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 425 in6p->in6p_cksum != -1) { 426 struct mbuf *n; 427 int off; 428 u_int16_t *p; 429 430 /* compute checksum */ 431 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 432 off = offsetof(struct icmp6_hdr, icmp6_cksum); 433 else 434 off = in6p->in6p_cksum; 435 if (plen < off + 1) { 436 error = EINVAL; 437 goto bad; 438 } 439 off += sizeof(struct ip6_hdr); 440 441 n = m; 442 while (n && n->m_len <= off) { 443 off -= n->m_len; 444 n = n->m_next; 445 } 446 if (!n) 447 goto bad; 448 p = (u_int16_t *)(mtod(n, caddr_t) + off); 449 *p = 0; 450 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 451 } 452 453 error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p); 454 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 455 if (oifp) 456 icmp6_ifoutstat_inc(oifp, type, code); 457 icmp6stat.icp6s_outhist[type]++; 458 } else 459 rip6stat.rip6s_opackets++; 460 461 goto freectl; 462 463 bad: 464 if (m) 465 m_freem(m); 466 467 freectl: 468 if (control) { 469 ip6_clearpktopts(&opt, -1); 470 m_freem(control); 471 } 472 INP_UNLOCK(in6p); 473 return (error); 474 } 475 476 /* 477 * Raw IPv6 socket option processing. 478 */ 479 int 480 rip6_ctloutput(struct socket *so, struct sockopt *sopt) 481 { 482 int error; 483 484 if (sopt->sopt_level == IPPROTO_ICMPV6) 485 /* 486 * XXX: is it better to call icmp6_ctloutput() directly 487 * from protosw? 488 */ 489 return (icmp6_ctloutput(so, sopt)); 490 else if (sopt->sopt_level != IPPROTO_IPV6) 491 return (EINVAL); 492 493 error = 0; 494 495 switch (sopt->sopt_dir) { 496 case SOPT_GET: 497 switch (sopt->sopt_name) { 498 case MRT6_INIT: 499 case MRT6_DONE: 500 case MRT6_ADD_MIF: 501 case MRT6_DEL_MIF: 502 case MRT6_ADD_MFC: 503 case MRT6_DEL_MFC: 504 case MRT6_PIM: 505 error = ip6_mrouter_get ? ip6_mrouter_get(so, sopt) : 506 EOPNOTSUPP; 507 break; 508 case IPV6_CHECKSUM: 509 error = ip6_raw_ctloutput(so, sopt); 510 break; 511 default: 512 error = ip6_ctloutput(so, sopt); 513 break; 514 } 515 break; 516 517 case SOPT_SET: 518 switch (sopt->sopt_name) { 519 case MRT6_INIT: 520 case MRT6_DONE: 521 case MRT6_ADD_MIF: 522 case MRT6_DEL_MIF: 523 case MRT6_ADD_MFC: 524 case MRT6_DEL_MFC: 525 case MRT6_PIM: 526 error = ip6_mrouter_set ? ip6_mrouter_set(so, sopt) : 527 EOPNOTSUPP; 528 break; 529 case IPV6_CHECKSUM: 530 error = ip6_raw_ctloutput(so, sopt); 531 break; 532 default: 533 error = ip6_ctloutput(so, sopt); 534 break; 535 } 536 break; 537 } 538 539 return (error); 540 } 541 542 static int 543 rip6_attach(struct socket *so, int proto, struct thread *td) 544 { 545 struct inpcb *inp; 546 struct icmp6_filter *filter; 547 int error; 548 549 inp = sotoinpcb(so); 550 KASSERT(inp == NULL, ("rip6_attach: inp != NULL")); 551 if (td && (error = suser(td)) != 0) 552 return error; 553 error = soreserve(so, rip_sendspace, rip_recvspace); 554 if (error) 555 return error; 556 MALLOC(filter, struct icmp6_filter *, 557 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 558 if (filter == NULL) 559 return ENOMEM; 560 INP_INFO_WLOCK(&ripcbinfo); 561 error = in_pcballoc(so, &ripcbinfo); 562 if (error) { 563 INP_INFO_WUNLOCK(&ripcbinfo); 564 FREE(filter, M_PCB); 565 return error; 566 } 567 inp = (struct inpcb *)so->so_pcb; 568 INP_INFO_WUNLOCK(&ripcbinfo); 569 inp->inp_vflag |= INP_IPV6; 570 inp->in6p_ip6_nxt = (long)proto; 571 inp->in6p_hops = -1; /* use kernel default */ 572 inp->in6p_cksum = -1; 573 inp->in6p_icmp6filt = filter; 574 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 575 INP_UNLOCK(inp); 576 return 0; 577 } 578 579 static void 580 rip6_detach(struct socket *so) 581 { 582 struct inpcb *inp; 583 584 inp = sotoinpcb(so); 585 KASSERT(inp != NULL, ("rip6_detach: inp == NULL")); 586 587 if (so == ip6_mrouter && ip6_mrouter_done) 588 ip6_mrouter_done(); 589 /* xxx: RSVP */ 590 INP_INFO_WLOCK(&ripcbinfo); 591 INP_LOCK(inp); 592 if (inp->in6p_icmp6filt) { 593 FREE(inp->in6p_icmp6filt, M_PCB); 594 inp->in6p_icmp6filt = NULL; 595 } 596 in6_pcbdetach(inp); 597 in6_pcbfree(inp); 598 INP_INFO_WUNLOCK(&ripcbinfo); 599 } 600 601 /* XXXRW: This can't ever be called. */ 602 static void 603 rip6_abort(struct socket *so) 604 { 605 struct inpcb *inp; 606 607 inp = sotoinpcb(so); 608 KASSERT(inp != NULL, ("rip6_abort: inp == NULL")); 609 610 soisdisconnected(so); 611 } 612 613 static void 614 rip6_close(struct socket *so) 615 { 616 struct inpcb *inp; 617 618 inp = sotoinpcb(so); 619 KASSERT(inp != NULL, ("rip6_close: inp == NULL")); 620 621 soisdisconnected(so); 622 } 623 624 static int 625 rip6_disconnect(struct socket *so) 626 { 627 struct inpcb *inp = sotoinpcb(so); 628 629 if ((so->so_state & SS_ISCONNECTED) == 0) 630 return ENOTCONN; 631 inp->in6p_faddr = in6addr_any; 632 rip6_abort(so); 633 return (0); 634 } 635 636 static int 637 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 638 { 639 struct inpcb *inp = sotoinpcb(so); 640 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 641 struct ifaddr *ia = NULL; 642 int error = 0; 643 644 KASSERT(inp != NULL, ("rip6_bind: inp == NULL")); 645 if (nam->sa_len != sizeof(*addr)) 646 return EINVAL; 647 if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6) 648 return EADDRNOTAVAIL; 649 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0) 650 return(error); 651 652 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 653 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) 654 return EADDRNOTAVAIL; 655 if (ia && 656 ((struct in6_ifaddr *)ia)->ia6_flags & 657 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 658 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 659 return (EADDRNOTAVAIL); 660 } 661 INP_INFO_WLOCK(&ripcbinfo); 662 INP_LOCK(inp); 663 inp->in6p_laddr = addr->sin6_addr; 664 INP_UNLOCK(inp); 665 INP_INFO_WUNLOCK(&ripcbinfo); 666 return 0; 667 } 668 669 static int 670 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 671 { 672 struct inpcb *inp = sotoinpcb(so); 673 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 674 struct in6_addr *in6a = NULL; 675 struct ifnet *ifp = NULL; 676 int error = 0, scope_ambiguous = 0; 677 678 KASSERT(inp != NULL, ("rip6_connect: inp == NULL")); 679 if (nam->sa_len != sizeof(*addr)) 680 return EINVAL; 681 if (TAILQ_EMPTY(&ifnet)) 682 return EADDRNOTAVAIL; 683 if (addr->sin6_family != AF_INET6) 684 return EAFNOSUPPORT; 685 686 /* 687 * Application should provide a proper zone ID or the use of 688 * default zone IDs should be enabled. Unfortunately, some 689 * applications do not behave as it should, so we need a 690 * workaround. Even if an appropriate ID is not determined, 691 * we'll see if we can determine the outgoing interface. If we 692 * can, determine the zone ID based on the interface below. 693 */ 694 if (addr->sin6_scope_id == 0 && !ip6_use_defzone) 695 scope_ambiguous = 1; 696 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0) 697 return(error); 698 699 INP_INFO_WLOCK(&ripcbinfo); 700 INP_LOCK(inp); 701 /* Source address selection. XXX: need pcblookup? */ 702 in6a = in6_selectsrc(addr, inp->in6p_outputopts, 703 inp->in6p_moptions, NULL, 704 &inp->in6p_laddr, &ifp, &error); 705 if (in6a == NULL) { 706 INP_UNLOCK(inp); 707 INP_INFO_WUNLOCK(&ripcbinfo); 708 return (error ? error : EADDRNOTAVAIL); 709 } 710 711 /* XXX: see above */ 712 if (ifp && scope_ambiguous && 713 (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) { 714 INP_UNLOCK(inp); 715 INP_INFO_WUNLOCK(&ripcbinfo); 716 return(error); 717 } 718 inp->in6p_faddr = addr->sin6_addr; 719 inp->in6p_laddr = *in6a; 720 soisconnected(so); 721 INP_UNLOCK(inp); 722 INP_INFO_WUNLOCK(&ripcbinfo); 723 return 0; 724 } 725 726 static int 727 rip6_shutdown(struct socket *so) 728 { 729 struct inpcb *inp; 730 731 inp = sotoinpcb(so); 732 KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL")); 733 INP_LOCK(inp); 734 socantsendmore(so); 735 INP_UNLOCK(inp); 736 return 0; 737 } 738 739 static int 740 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 741 struct mbuf *control, struct thread *td) 742 { 743 struct inpcb *inp = sotoinpcb(so); 744 struct sockaddr_in6 tmp; 745 struct sockaddr_in6 *dst; 746 int ret; 747 748 KASSERT(inp != NULL, ("rip6_send: inp == NULL")); 749 INP_INFO_WLOCK(&ripcbinfo); 750 /* always copy sockaddr to avoid overwrites */ 751 /* Unlocked read. */ 752 if (so->so_state & SS_ISCONNECTED) { 753 if (nam) { 754 INP_INFO_WUNLOCK(&ripcbinfo); 755 m_freem(m); 756 return EISCONN; 757 } 758 /* XXX */ 759 bzero(&tmp, sizeof(tmp)); 760 tmp.sin6_family = AF_INET6; 761 tmp.sin6_len = sizeof(struct sockaddr_in6); 762 bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 763 sizeof(struct in6_addr)); 764 dst = &tmp; 765 } else { 766 if (nam == NULL) { 767 INP_INFO_WUNLOCK(&ripcbinfo); 768 m_freem(m); 769 return ENOTCONN; 770 } 771 if (nam->sa_len != sizeof(struct sockaddr_in6)) { 772 INP_INFO_WUNLOCK(&ripcbinfo); 773 m_freem(m); 774 return(EINVAL); 775 } 776 tmp = *(struct sockaddr_in6 *)nam; 777 dst = &tmp; 778 779 if (dst->sin6_family == AF_UNSPEC) { 780 /* 781 * XXX: we allow this case for backward 782 * compatibility to buggy applications that 783 * rely on old (and wrong) kernel behavior. 784 */ 785 log(LOG_INFO, "rip6 SEND: address family is " 786 "unspec. Assume AF_INET6\n"); 787 dst->sin6_family = AF_INET6; 788 } else if (dst->sin6_family != AF_INET6) { 789 INP_INFO_WUNLOCK(&ripcbinfo); 790 m_freem(m); 791 return(EAFNOSUPPORT); 792 } 793 } 794 ret = rip6_output(m, so, dst, control); 795 INP_INFO_WUNLOCK(&ripcbinfo); 796 return (ret); 797 } 798 799 struct pr_usrreqs rip6_usrreqs = { 800 .pru_abort = rip6_abort, 801 .pru_attach = rip6_attach, 802 .pru_bind = rip6_bind, 803 .pru_connect = rip6_connect, 804 .pru_control = in6_control, 805 .pru_detach = rip6_detach, 806 .pru_disconnect = rip6_disconnect, 807 .pru_peeraddr = in6_getpeeraddr, 808 .pru_send = rip6_send, 809 .pru_shutdown = rip6_shutdown, 810 .pru_sockaddr = in6_getsockaddr, 811 .pru_close = rip6_close, 812 }; 813