raw_ip6.c revision 194760
159191Skris/*- 259191Skris * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 359191Skris * All rights reserved. 459191Skris * 559191Skris * Redistribution and use in source and binary forms, with or without 659191Skris * modification, are permitted provided that the following conditions 759191Skris * are met: 859191Skris * 1. Redistributions of source code must retain the above copyright 959191Skris * notice, this list of conditions and the following disclaimer. 1059191Skris * 2. Redistributions in binary form must reproduce the above copyright 1159191Skris * notice, this list of conditions and the following disclaimer in the 1259191Skris * documentation and/or other materials provided with the distribution. 1359191Skris * 3. Neither the name of the project nor the names of its contributors 1459191Skris * may be used to endorse or promote products derived from this software 1559191Skris * without specific prior written permission. 1659191Skris * 1759191Skris * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1859191Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1959191Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2059191Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2159191Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2259191Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2359191Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2459191Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2559191Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2659191Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2759191Skris * SUCH DAMAGE. 2859191Skris */ 2959191Skris 3059191Skris/*- 3159191Skris * Copyright (c) 1982, 1986, 1988, 1993 3259191Skris * The Regents of the University of California. 3359191Skris * All rights reserved. 3459191Skris * 3559191Skris * Redistribution and use in source and binary forms, with or without 3659191Skris * modification, are permitted provided that the following conditions 3759191Skris * are met: 3859191Skris * 1. Redistributions of source code must retain the above copyright 3959191Skris * notice, this list of conditions and the following disclaimer. 4059191Skris * 2. Redistributions in binary form must reproduce the above copyright 4159191Skris * notice, this list of conditions and the following disclaimer in the 4259191Skris * documentation and/or other materials provided with the distribution. 4359191Skris * 4. Neither the name of the University nor the names of its contributors 4459191Skris * may be used to endorse or promote products derived from this software 4559191Skris * without specific prior written permission. 4659191Skris * 4759191Skris * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 4859191Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4959191Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5059191Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5159191Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5259191Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5359191Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5459191Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5559191Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5659191Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5759191Skris * SUCH DAMAGE. 5859191Skris * 5959191Skris * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94 6059191Skris */ 6159191Skris 6259191Skris#include <sys/cdefs.h> 6359191Skris__FBSDID("$FreeBSD: head/sys/netinet6/raw_ip6.c 194760 2009-06-23 20:19:09Z rwatson $"); 6459191Skris 6559191Skris#include "opt_ipsec.h" 6659191Skris#include "opt_inet6.h" 67109998Smarkm 6868651Skris#include <sys/param.h> 6968651Skris#include <sys/errno.h> 70109998Smarkm#include <sys/jail.h> 7159191Skris#include <sys/lock.h> 7259191Skris#include <sys/malloc.h> 7359191Skris#include <sys/mbuf.h> 7459191Skris#include <sys/priv.h> 75160814Ssimon#include <sys/proc.h> 7659191Skris#include <sys/protosw.h> 77109998Smarkm#include <sys/signalvar.h> 7859191Skris#include <sys/socket.h> 7959191Skris#include <sys/socketvar.h> 8059191Skris#include <sys/sx.h> 81238405Sjkim#include <sys/syslog.h> 8259191Skris#include <sys/vimage.h> 8359191Skris 8459191Skris#include <net/if.h> 85109998Smarkm#include <net/if_types.h> 8659191Skris#include <net/route.h> 8759191Skris#include <net/vnet.h> 8859191Skris 8959191Skris#include <netinet/in.h> 9059191Skris#include <netinet/in_var.h> 91160814Ssimon#include <netinet/in_systm.h> 92160814Ssimon#include <netinet/in_pcb.h> 9359191Skris#include <netinet/vinet.h> 94160814Ssimon 95109998Smarkm#include <netinet/icmp6.h> 9659191Skris#include <netinet/ip6.h> 9759191Skris#include <netinet6/ip6protosw.h> 98109998Smarkm#include <netinet6/ip6_mroute.h> 9959191Skris#include <netinet6/in6_pcb.h> 10059191Skris#include <netinet6/ip6_var.h> 10159191Skris#include <netinet6/nd6.h> 10259191Skris#include <netinet6/raw_ip6.h> 103160814Ssimon#include <netinet6/scope6_var.h> 104160814Ssimon#include <netinet6/vinet6.h> 105160814Ssimon 106160814Ssimon#ifdef IPSEC 107160814Ssimon#include <netipsec/ipsec.h> 108160814Ssimon#include <netipsec/ipsec6.h> 109160814Ssimon#endif /* IPSEC */ 110160814Ssimon 111238405Sjkim#include <machine/stdarg.h> 112238405Sjkim 113238405Sjkim#define satosin6(sa) ((struct sockaddr_in6 *)(sa)) 11459191Skris#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 115 116/* 117 * Raw interface to IP6 protocol. 118 */ 119 120#ifdef VIMAGE_GLOBALS 121extern struct inpcbhead ripcb; 122extern struct inpcbinfo ripcbinfo; 123struct rip6stat rip6stat; 124#endif 125 126extern u_long rip_sendspace; 127extern u_long rip_recvspace; 128 129/* 130 * Hooks for multicast routing. They all default to NULL, so leave them not 131 * initialized and rely on BSS being set to 0. 132 */ 133 134/* 135 * The socket used to communicate with the multicast routing daemon. 136 */ 137#ifdef VIMAGE_GLOBALS 138struct socket *ip6_mrouter; 139#endif 140 141/* 142 * The various mrouter functions. 143 */ 144int (*ip6_mrouter_set)(struct socket *, struct sockopt *); 145int (*ip6_mrouter_get)(struct socket *, struct sockopt *); 146int (*ip6_mrouter_done)(void); 147int (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *); 148int (*mrt6_ioctl)(u_long, caddr_t); 149 150/* 151 * Setup generic address and protocol structures for raw_input routine, then 152 * pass them along with mbuf chain. 153 */ 154int 155rip6_input(struct mbuf **mp, int *offp, int proto) 156{ 157 INIT_VNET_INET(curvnet); 158 INIT_VNET_INET6(curvnet); 159#ifdef IPSEC 160 INIT_VNET_IPSEC(curvnet); 161#endif 162 struct ifnet *ifp; 163 struct mbuf *m = *mp; 164 register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 165 register struct inpcb *in6p; 166 struct inpcb *last = 0; 167 struct mbuf *opts = NULL; 168 struct sockaddr_in6 fromsa; 169 170 V_rip6stat.rip6s_ipackets++; 171 172 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 173 /* XXX Send icmp6 host/port unreach? */ 174 m_freem(m); 175 return (IPPROTO_DONE); 176 } 177 178 init_sin6(&fromsa, m); /* general init */ 179 180 ifp = m->m_pkthdr.rcvif; 181 182 INP_INFO_RLOCK(&V_ripcbinfo); 183 LIST_FOREACH(in6p, &V_ripcb, inp_list) { 184 /* XXX inp locking */ 185 if ((in6p->inp_vflag & INP_IPV6) == 0) 186 continue; 187 if (in6p->inp_ip_p && 188 in6p->inp_ip_p != proto) 189 continue; 190 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 191 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 192 continue; 193 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 194 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 195 continue; 196 if (jailed(in6p->inp_cred)) { 197 /* 198 * Allow raw socket in jail to receive multicast; 199 * assume process had PRIV_NETINET_RAW at attach, 200 * and fall through into normal filter path if so. 201 */ 202 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 203 prison_check_ip6(in6p->inp_cred, 204 &ip6->ip6_dst) != 0) 205 continue; 206 } 207 if (in6p->in6p_cksum != -1) { 208 V_rip6stat.rip6s_isum++; 209 if (in6_cksum(m, proto, *offp, 210 m->m_pkthdr.len - *offp)) { 211 INP_RUNLOCK(in6p); 212 V_rip6stat.rip6s_badsum++; 213 continue; 214 } 215 } 216 INP_RLOCK(in6p); 217 /* 218 * If this raw socket has multicast state, and we 219 * have received a multicast, check if this socket 220 * should receive it, as multicast filtering is now 221 * the responsibility of the transport layer. 222 */ 223 if (in6p->in6p_moptions && 224 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 225 struct sockaddr_in6 mcaddr; 226 int blocked; 227 228 bzero(&mcaddr, sizeof(struct sockaddr_in6)); 229 mcaddr.sin6_len = sizeof(struct sockaddr_in6); 230 mcaddr.sin6_family = AF_INET6; 231 mcaddr.sin6_addr = ip6->ip6_dst; 232 233 blocked = im6o_mc_filter(in6p->in6p_moptions, ifp, 234 (struct sockaddr *)&mcaddr, 235 (struct sockaddr *)&fromsa); 236 if (blocked != MCAST_PASS) { 237 IP6STAT_INC(ip6s_notmember); 238 continue; 239 } 240 } 241 if (last != NULL) { 242 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 243 244#ifdef IPSEC 245 /* 246 * Check AH/ESP integrity. 247 */ 248 if (n && ipsec6_in_reject(n, last)) { 249 m_freem(n); 250 V_ipsec6stat.in_polvio++; 251 /* Do not inject data into pcb. */ 252 } else 253#endif /* IPSEC */ 254 if (n) { 255 if (last->inp_flags & INP_CONTROLOPTS || 256 last->inp_socket->so_options & SO_TIMESTAMP) 257 ip6_savecontrol(last, n, &opts); 258 /* strip intermediate headers */ 259 m_adj(n, *offp); 260 if (sbappendaddr(&last->inp_socket->so_rcv, 261 (struct sockaddr *)&fromsa, 262 n, opts) == 0) { 263 m_freem(n); 264 if (opts) 265 m_freem(opts); 266 V_rip6stat.rip6s_fullsock++; 267 } else 268 sorwakeup(last->inp_socket); 269 opts = NULL; 270 } 271 INP_RUNLOCK(last); 272 } 273 last = in6p; 274 } 275 INP_INFO_RUNLOCK(&V_ripcbinfo); 276#ifdef IPSEC 277 /* 278 * Check AH/ESP integrity. 279 */ 280 if ((last != NULL) && ipsec6_in_reject(m, last)) { 281 m_freem(m); 282 V_ipsec6stat.in_polvio++; 283 V_ip6stat.ip6s_delivered--; 284 /* Do not inject data into pcb. */ 285 INP_RUNLOCK(last); 286 } else 287#endif /* IPSEC */ 288 if (last != NULL) { 289 if (last->inp_flags & INP_CONTROLOPTS || 290 last->inp_socket->so_options & SO_TIMESTAMP) 291 ip6_savecontrol(last, m, &opts); 292 /* Strip intermediate headers. */ 293 m_adj(m, *offp); 294 if (sbappendaddr(&last->inp_socket->so_rcv, 295 (struct sockaddr *)&fromsa, m, opts) == 0) { 296 m_freem(m); 297 if (opts) 298 m_freem(opts); 299 V_rip6stat.rip6s_fullsock++; 300 } else 301 sorwakeup(last->inp_socket); 302 INP_RUNLOCK(last); 303 } else { 304 V_rip6stat.rip6s_nosock++; 305 if (m->m_flags & M_MCAST) 306 V_rip6stat.rip6s_nosockmcast++; 307 if (proto == IPPROTO_NONE) 308 m_freem(m); 309 else { 310 char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */ 311 icmp6_error(m, ICMP6_PARAM_PROB, 312 ICMP6_PARAMPROB_NEXTHEADER, 313 prvnxtp - mtod(m, char *)); 314 } 315 V_ip6stat.ip6s_delivered--; 316 } 317 return (IPPROTO_DONE); 318} 319 320void 321rip6_ctlinput(int cmd, struct sockaddr *sa, void *d) 322{ 323 INIT_VNET_INET(curvnet); 324 struct ip6_hdr *ip6; 325 struct mbuf *m; 326 int off = 0; 327 struct ip6ctlparam *ip6cp = NULL; 328 const struct sockaddr_in6 *sa6_src = NULL; 329 void *cmdarg; 330 struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange; 331 332 if (sa->sa_family != AF_INET6 || 333 sa->sa_len != sizeof(struct sockaddr_in6)) 334 return; 335 336 if ((unsigned)cmd >= PRC_NCMDS) 337 return; 338 if (PRC_IS_REDIRECT(cmd)) 339 notify = in6_rtchange, d = NULL; 340 else if (cmd == PRC_HOSTDEAD) 341 d = NULL; 342 else if (inet6ctlerrmap[cmd] == 0) 343 return; 344 345 /* 346 * If the parameter is from icmp6, decode it. 347 */ 348 if (d != NULL) { 349 ip6cp = (struct ip6ctlparam *)d; 350 m = ip6cp->ip6c_m; 351 ip6 = ip6cp->ip6c_ip6; 352 off = ip6cp->ip6c_off; 353 cmdarg = ip6cp->ip6c_cmdarg; 354 sa6_src = ip6cp->ip6c_src; 355 } else { 356 m = NULL; 357 ip6 = NULL; 358 cmdarg = NULL; 359 sa6_src = &sa6_any; 360 } 361 362 (void) in6_pcbnotify(&V_ripcbinfo, sa, 0, 363 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify); 364} 365 366/* 367 * Generate IPv6 header and pass packet to ip6_output. Tack on options user 368 * may have setup with control call. 369 */ 370int 371#if __STDC__ 372rip6_output(struct mbuf *m, ...) 373#else 374rip6_output(m, va_alist) 375 struct mbuf *m; 376 va_dcl 377#endif 378{ 379 INIT_VNET_INET6(curvnet); 380 struct mbuf *control; 381 struct socket *so; 382 struct sockaddr_in6 *dstsock; 383 struct in6_addr *dst; 384 struct ip6_hdr *ip6; 385 struct inpcb *in6p; 386 u_int plen = m->m_pkthdr.len; 387 int error = 0; 388 struct ip6_pktopts opt, *optp; 389 struct ifnet *oifp = NULL; 390 int type = 0, code = 0; /* for ICMPv6 output statistics only */ 391 int scope_ambiguous = 0; 392 struct in6_addr *in6a; 393 va_list ap; 394 395 va_start(ap, m); 396 so = va_arg(ap, struct socket *); 397 dstsock = va_arg(ap, struct sockaddr_in6 *); 398 control = va_arg(ap, struct mbuf *); 399 va_end(ap); 400 401 in6p = sotoinpcb(so); 402 INP_WLOCK(in6p); 403 404 dst = &dstsock->sin6_addr; 405 if (control != NULL) { 406 if ((error = ip6_setpktopts(control, &opt, 407 in6p->in6p_outputopts, so->so_cred, 408 so->so_proto->pr_protocol)) != 0) { 409 goto bad; 410 } 411 optp = &opt; 412 } else 413 optp = in6p->in6p_outputopts; 414 415 /* 416 * Check and convert scope zone ID into internal form. 417 * 418 * XXX: we may still need to determine the zone later. 419 */ 420 if (!(so->so_state & SS_ISCONNECTED)) { 421 if (dstsock->sin6_scope_id == 0 && !V_ip6_use_defzone) 422 scope_ambiguous = 1; 423 if ((error = sa6_embedscope(dstsock, V_ip6_use_defzone)) != 0) 424 goto bad; 425 } 426 427 /* 428 * For an ICMPv6 packet, we should know its type and code to update 429 * statistics. 430 */ 431 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 432 struct icmp6_hdr *icmp6; 433 if (m->m_len < sizeof(struct icmp6_hdr) && 434 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 435 error = ENOBUFS; 436 goto bad; 437 } 438 icmp6 = mtod(m, struct icmp6_hdr *); 439 type = icmp6->icmp6_type; 440 code = icmp6->icmp6_code; 441 } 442 443 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT); 444 if (m == NULL) { 445 error = ENOBUFS; 446 goto bad; 447 } 448 ip6 = mtod(m, struct ip6_hdr *); 449 450 /* 451 * Source address selection. 452 */ 453 if ((in6a = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred, 454 &oifp, &error)) == NULL) { 455 if (error == 0) 456 error = EADDRNOTAVAIL; 457 goto bad; 458 } 459 error = prison_get_ip6(in6p->inp_cred, in6a); 460 if (error != 0) 461 goto bad; 462 ip6->ip6_src = *in6a; 463 464 if (oifp && scope_ambiguous) { 465 /* 466 * Application should provide a proper zone ID or the use of 467 * default zone IDs should be enabled. Unfortunately, some 468 * applications do not behave as it should, so we need a 469 * workaround. Even if an appropriate ID is not determined 470 * (when it's required), if we can determine the outgoing 471 * interface. determine the zone ID based on the interface. 472 */ 473 error = in6_setscope(&dstsock->sin6_addr, oifp, NULL); 474 if (error != 0) 475 goto bad; 476 } 477 ip6->ip6_dst = dstsock->sin6_addr; 478 479 /* 480 * Fill in the rest of the IPv6 header fields. 481 */ 482 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 483 (in6p->inp_flow & IPV6_FLOWINFO_MASK); 484 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 485 (IPV6_VERSION & IPV6_VERSION_MASK); 486 487 /* 488 * ip6_plen will be filled in ip6_output, so not fill it here. 489 */ 490 ip6->ip6_nxt = in6p->inp_ip_p; 491 ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 492 493 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 494 in6p->in6p_cksum != -1) { 495 struct mbuf *n; 496 int off; 497 u_int16_t *p; 498 499 /* Compute checksum. */ 500 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 501 off = offsetof(struct icmp6_hdr, icmp6_cksum); 502 else 503 off = in6p->in6p_cksum; 504 if (plen < off + 1) { 505 error = EINVAL; 506 goto bad; 507 } 508 off += sizeof(struct ip6_hdr); 509 510 n = m; 511 while (n && n->m_len <= off) { 512 off -= n->m_len; 513 n = n->m_next; 514 } 515 if (!n) 516 goto bad; 517 p = (u_int16_t *)(mtod(n, caddr_t) + off); 518 *p = 0; 519 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 520 } 521 522 error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p); 523 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 524 if (oifp) 525 icmp6_ifoutstat_inc(oifp, type, code); 526 ICMP6STAT_INC(icp6s_outhist[type]); 527 } else 528 V_rip6stat.rip6s_opackets++; 529 530 goto freectl; 531 532 bad: 533 if (m) 534 m_freem(m); 535 536 freectl: 537 if (control != NULL) { 538 ip6_clearpktopts(&opt, -1); 539 m_freem(control); 540 } 541 INP_WUNLOCK(in6p); 542 return (error); 543} 544 545/* 546 * Raw IPv6 socket option processing. 547 */ 548int 549rip6_ctloutput(struct socket *so, struct sockopt *sopt) 550{ 551 int error; 552 553 if (sopt->sopt_level == IPPROTO_ICMPV6) 554 /* 555 * XXX: is it better to call icmp6_ctloutput() directly 556 * from protosw? 557 */ 558 return (icmp6_ctloutput(so, sopt)); 559 else if (sopt->sopt_level != IPPROTO_IPV6) 560 return (EINVAL); 561 562 error = 0; 563 564 switch (sopt->sopt_dir) { 565 case SOPT_GET: 566 switch (sopt->sopt_name) { 567 case MRT6_INIT: 568 case MRT6_DONE: 569 case MRT6_ADD_MIF: 570 case MRT6_DEL_MIF: 571 case MRT6_ADD_MFC: 572 case MRT6_DEL_MFC: 573 case MRT6_PIM: 574 error = ip6_mrouter_get ? ip6_mrouter_get(so, sopt) : 575 EOPNOTSUPP; 576 break; 577 case IPV6_CHECKSUM: 578 error = ip6_raw_ctloutput(so, sopt); 579 break; 580 default: 581 error = ip6_ctloutput(so, sopt); 582 break; 583 } 584 break; 585 586 case SOPT_SET: 587 switch (sopt->sopt_name) { 588 case MRT6_INIT: 589 case MRT6_DONE: 590 case MRT6_ADD_MIF: 591 case MRT6_DEL_MIF: 592 case MRT6_ADD_MFC: 593 case MRT6_DEL_MFC: 594 case MRT6_PIM: 595 error = ip6_mrouter_set ? ip6_mrouter_set(so, sopt) : 596 EOPNOTSUPP; 597 break; 598 case IPV6_CHECKSUM: 599 error = ip6_raw_ctloutput(so, sopt); 600 break; 601 default: 602 error = ip6_ctloutput(so, sopt); 603 break; 604 } 605 break; 606 } 607 608 return (error); 609} 610 611static int 612rip6_attach(struct socket *so, int proto, struct thread *td) 613{ 614 INIT_VNET_INET(so->so_vnet); 615 struct inpcb *inp; 616 struct icmp6_filter *filter; 617 int error; 618 619 inp = sotoinpcb(so); 620 KASSERT(inp == NULL, ("rip6_attach: inp != NULL")); 621 622 error = priv_check(td, PRIV_NETINET_RAW); 623 if (error) 624 return (error); 625 error = soreserve(so, rip_sendspace, rip_recvspace); 626 if (error) 627 return (error); 628 filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 629 if (filter == NULL) 630 return (ENOMEM); 631 INP_INFO_WLOCK(&V_ripcbinfo); 632 error = in_pcballoc(so, &V_ripcbinfo); 633 if (error) { 634 INP_INFO_WUNLOCK(&V_ripcbinfo); 635 free(filter, M_PCB); 636 return (error); 637 } 638 inp = (struct inpcb *)so->so_pcb; 639 INP_INFO_WUNLOCK(&V_ripcbinfo); 640 inp->inp_vflag |= INP_IPV6; 641 inp->inp_ip_p = (long)proto; 642 inp->in6p_hops = -1; /* use kernel default */ 643 inp->in6p_cksum = -1; 644 inp->in6p_icmp6filt = filter; 645 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 646 INP_WUNLOCK(inp); 647 return (0); 648} 649 650static void 651rip6_detach(struct socket *so) 652{ 653 INIT_VNET_INET(so->so_vnet); 654 INIT_VNET_INET6(so->so_vnet); 655 struct inpcb *inp; 656 657 inp = sotoinpcb(so); 658 KASSERT(inp != NULL, ("rip6_detach: inp == NULL")); 659 660 if (so == V_ip6_mrouter && ip6_mrouter_done) 661 ip6_mrouter_done(); 662 /* xxx: RSVP */ 663 INP_INFO_WLOCK(&V_ripcbinfo); 664 INP_WLOCK(inp); 665 free(inp->in6p_icmp6filt, M_PCB); 666 in_pcbdetach(inp); 667 in_pcbfree(inp); 668 INP_INFO_WUNLOCK(&V_ripcbinfo); 669} 670 671/* XXXRW: This can't ever be called. */ 672static void 673rip6_abort(struct socket *so) 674{ 675 struct inpcb *inp; 676 677 inp = sotoinpcb(so); 678 KASSERT(inp != NULL, ("rip6_abort: inp == NULL")); 679 680 soisdisconnected(so); 681} 682 683static void 684rip6_close(struct socket *so) 685{ 686 struct inpcb *inp; 687 688 inp = sotoinpcb(so); 689 KASSERT(inp != NULL, ("rip6_close: inp == NULL")); 690 691 soisdisconnected(so); 692} 693 694static int 695rip6_disconnect(struct socket *so) 696{ 697 struct inpcb *inp; 698 699 inp = sotoinpcb(so); 700 KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL")); 701 702 if ((so->so_state & SS_ISCONNECTED) == 0) 703 return (ENOTCONN); 704 inp->in6p_faddr = in6addr_any; 705 rip6_abort(so); 706 return (0); 707} 708 709static int 710rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 711{ 712 INIT_VNET_NET(so->so_vnet); 713 INIT_VNET_INET(so->so_vnet); 714 INIT_VNET_INET6(so->so_vnet); 715 struct inpcb *inp; 716 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 717 struct ifaddr *ifa = NULL; 718 int error = 0; 719 720 inp = sotoinpcb(so); 721 KASSERT(inp != NULL, ("rip6_bind: inp == NULL")); 722 723 if (nam->sa_len != sizeof(*addr)) 724 return (EINVAL); 725 if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0) 726 return (error); 727 if (TAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6) 728 return (EADDRNOTAVAIL); 729 if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 730 return (error); 731 732 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 733 (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL) 734 return (EADDRNOTAVAIL); 735 if (ifa != NULL && 736 ((struct in6_ifaddr *)ifa)->ia6_flags & 737 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 738 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 739 ifa_free(ifa); 740 return (EADDRNOTAVAIL); 741 } 742 if (ifa != NULL) 743 ifa_free(ifa); 744 INP_INFO_WLOCK(&V_ripcbinfo); 745 INP_WLOCK(inp); 746 inp->in6p_laddr = addr->sin6_addr; 747 INP_WUNLOCK(inp); 748 INP_INFO_WUNLOCK(&V_ripcbinfo); 749 return (0); 750} 751 752static int 753rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 754{ 755 INIT_VNET_NET(so->so_vnet); 756 INIT_VNET_INET(so->so_vnet); 757 INIT_VNET_INET6(so->so_vnet); 758 struct inpcb *inp; 759 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 760 struct in6_addr *in6a = NULL; 761 struct ifnet *ifp = NULL; 762 int error = 0, scope_ambiguous = 0; 763 764 inp = sotoinpcb(so); 765 KASSERT(inp != NULL, ("rip6_connect: inp == NULL")); 766 767 if (nam->sa_len != sizeof(*addr)) 768 return (EINVAL); 769 if (TAILQ_EMPTY(&V_ifnet)) 770 return (EADDRNOTAVAIL); 771 if (addr->sin6_family != AF_INET6) 772 return (EAFNOSUPPORT); 773 774 /* 775 * Application should provide a proper zone ID or the use of default 776 * zone IDs should be enabled. Unfortunately, some applications do 777 * not behave as it should, so we need a workaround. Even if an 778 * appropriate ID is not determined, we'll see if we can determine 779 * the outgoing interface. If we can, determine the zone ID based on 780 * the interface below. 781 */ 782 if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone) 783 scope_ambiguous = 1; 784 if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 785 return (error); 786 787 INP_INFO_WLOCK(&V_ripcbinfo); 788 INP_WLOCK(inp); 789 /* Source address selection. XXX: need pcblookup? */ 790 in6a = in6_selectsrc(addr, inp->in6p_outputopts, 791 inp, NULL, so->so_cred, 792 &ifp, &error); 793 if (in6a == NULL) { 794 INP_WUNLOCK(inp); 795 INP_INFO_WUNLOCK(&V_ripcbinfo); 796 return (error ? error : EADDRNOTAVAIL); 797 } 798 799 /* XXX: see above */ 800 if (ifp && scope_ambiguous && 801 (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) { 802 INP_WUNLOCK(inp); 803 INP_INFO_WUNLOCK(&V_ripcbinfo); 804 return (error); 805 } 806 inp->in6p_faddr = addr->sin6_addr; 807 inp->in6p_laddr = *in6a; 808 soisconnected(so); 809 INP_WUNLOCK(inp); 810 INP_INFO_WUNLOCK(&V_ripcbinfo); 811 return (0); 812} 813 814static int 815rip6_shutdown(struct socket *so) 816{ 817 struct inpcb *inp; 818 819 inp = sotoinpcb(so); 820 KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL")); 821 822 INP_WLOCK(inp); 823 socantsendmore(so); 824 INP_WUNLOCK(inp); 825 return (0); 826} 827 828static int 829rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 830 struct mbuf *control, struct thread *td) 831{ 832 struct inpcb *inp; 833 struct sockaddr_in6 tmp; 834 struct sockaddr_in6 *dst; 835 int ret; 836 837 inp = sotoinpcb(so); 838 KASSERT(inp != NULL, ("rip6_send: inp == NULL")); 839 840 /* Always copy sockaddr to avoid overwrites. */ 841 /* Unlocked read. */ 842 if (so->so_state & SS_ISCONNECTED) { 843 if (nam) { 844 m_freem(m); 845 return (EISCONN); 846 } 847 /* XXX */ 848 bzero(&tmp, sizeof(tmp)); 849 tmp.sin6_family = AF_INET6; 850 tmp.sin6_len = sizeof(struct sockaddr_in6); 851 INP_RLOCK(inp); 852 bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 853 sizeof(struct in6_addr)); 854 INP_RUNLOCK(inp); 855 dst = &tmp; 856 } else { 857 if (nam == NULL) { 858 m_freem(m); 859 return (ENOTCONN); 860 } 861 if (nam->sa_len != sizeof(struct sockaddr_in6)) { 862 m_freem(m); 863 return (EINVAL); 864 } 865 tmp = *(struct sockaddr_in6 *)nam; 866 dst = &tmp; 867 868 if (dst->sin6_family == AF_UNSPEC) { 869 /* 870 * XXX: we allow this case for backward 871 * compatibility to buggy applications that 872 * rely on old (and wrong) kernel behavior. 873 */ 874 log(LOG_INFO, "rip6 SEND: address family is " 875 "unspec. Assume AF_INET6\n"); 876 dst->sin6_family = AF_INET6; 877 } else if (dst->sin6_family != AF_INET6) { 878 m_freem(m); 879 return(EAFNOSUPPORT); 880 } 881 } 882 ret = rip6_output(m, so, dst, control); 883 return (ret); 884} 885 886struct pr_usrreqs rip6_usrreqs = { 887 .pru_abort = rip6_abort, 888 .pru_attach = rip6_attach, 889 .pru_bind = rip6_bind, 890 .pru_connect = rip6_connect, 891 .pru_control = in6_control, 892 .pru_detach = rip6_detach, 893 .pru_disconnect = rip6_disconnect, 894 .pru_peeraddr = in6_getpeeraddr, 895 .pru_send = rip6_send, 896 .pru_shutdown = rip6_shutdown, 897 .pru_sockaddr = in6_getsockaddr, 898 .pru_close = rip6_close, 899}; 900