raw_ip6.c revision 160549
198514Sluigi/*- 298514Sluigi * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 398514Sluigi * All rights reserved. 498514Sluigi * 598514Sluigi * Redistribution and use in source and binary forms, with or without 698514Sluigi * modification, are permitted provided that the following conditions 798514Sluigi * are met: 898514Sluigi * 1. Redistributions of source code must retain the above copyright 998514Sluigi * notice, this list of conditions and the following disclaimer. 1098514Sluigi * 2. Redistributions in binary form must reproduce the above copyright 1198514Sluigi * notice, this list of conditions and the following disclaimer in the 1298514Sluigi * documentation and/or other materials provided with the distribution. 1398514Sluigi * 3. Neither the name of the project nor the names of its contributors 1498514Sluigi * may be used to endorse or promote products derived from this software 1598514Sluigi * without specific prior written permission. 1698514Sluigi * 1798514Sluigi * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1898514Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1998514Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2098514Sluigi * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2198514Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2298514Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2398514Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2498514Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2598514Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2698514Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2798514Sluigi * SUCH DAMAGE. 2898514Sluigi * 2998514Sluigi * $FreeBSD: head/sys/netinet6/raw_ip6.c 160549 2006-07-21 17:11:15Z rwatson $ 3098514Sluigi */ 3198514Sluigi 3298514Sluigi/*- 3398514Sluigi * Copyright (c) 1982, 1986, 1988, 1993 3498514Sluigi * The Regents of the University of California. All rights reserved. 3598514Sluigi * 3698514Sluigi * Redistribution and use in source and binary forms, with or without 3798514Sluigi * modification, are permitted provided that the following conditions 3898514Sluigi * are met: 3998514Sluigi * 1. Redistributions of source code must retain the above copyright 4098514Sluigi * notice, this list of conditions and the following disclaimer. 4198514Sluigi * 2. Redistributions in binary form must reproduce the above copyright 4298514Sluigi * notice, this list of conditions and the following disclaimer in the 4398514Sluigi * documentation and/or other materials provided with the distribution. 4498514Sluigi * 4. Neither the name of the University nor the names of its contributors 4598514Sluigi * may be used to endorse or promote products derived from this software 4698514Sluigi * without specific prior written permission. 4798514Sluigi * 4898514Sluigi * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 4998514Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5098514Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5198514Sluigi * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5298514Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5398514Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5498514Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5598514Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56104744Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5798514Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5898514Sluigi * SUCH DAMAGE. 5998514Sluigi * 6098514Sluigi * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94 6198514Sluigi */ 6298514Sluigi 6398514Sluigi#include "opt_ipsec.h" 6498514Sluigi#include "opt_inet6.h" 6598514Sluigi 6698514Sluigi#include <sys/param.h> 6798514Sluigi#include <sys/errno.h> 6898514Sluigi#include <sys/lock.h> 6998514Sluigi#include <sys/malloc.h> 7098514Sluigi#include <sys/mbuf.h> 7198514Sluigi#include <sys/proc.h> 7298514Sluigi#include <sys/protosw.h> 7398514Sluigi#include <sys/signalvar.h> 7498514Sluigi#include <sys/socket.h> 7598514Sluigi#include <sys/socketvar.h> 7698514Sluigi#include <sys/sx.h> 7798514Sluigi#include <sys/systm.h> 7898514Sluigi#include <sys/syslog.h> 7998514Sluigi 8098514Sluigi#include <net/if.h> 8198514Sluigi#include <net/if_types.h> 8298514Sluigi#include <net/route.h> 8398514Sluigi 8498514Sluigi#include <netinet/in.h> 8598514Sluigi#include <netinet/in_var.h> 8698514Sluigi#include <netinet/in_systm.h> 8798514Sluigi#include <netinet/icmp6.h> 8898514Sluigi#include <netinet/in_pcb.h> 8998514Sluigi#include <netinet/ip6.h> 9098514Sluigi#include <netinet6/ip6protosw.h> 9198514Sluigi#include <netinet6/ip6_mroute.h> 9298514Sluigi#include <netinet6/in6_pcb.h> 9398514Sluigi#include <netinet6/ip6_var.h> 9498514Sluigi#include <netinet6/nd6.h> 9598514Sluigi#include <netinet6/raw_ip6.h> 9698514Sluigi#include <netinet6/scope6_var.h> 9798514Sluigi 9898514Sluigi#ifdef IPSEC 9998514Sluigi#include <netinet6/ipsec.h> 10098514Sluigi#include <netinet6/ipsec6.h> 10198514Sluigi#endif /*IPSEC*/ 10298514Sluigi 10398514Sluigi#ifdef FAST_IPSEC 10498514Sluigi#include <netipsec/ipsec.h> 10598514Sluigi#include <netipsec/ipsec6.h> 10698514Sluigi#endif /* FAST_IPSEC */ 10798514Sluigi 10898514Sluigi#include <machine/stdarg.h> 10998514Sluigi 11098514Sluigi#define satosin6(sa) ((struct sockaddr_in6 *)(sa)) 11198514Sluigi#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 11298514Sluigi 11398514Sluigi/* 11498514Sluigi * Raw interface to IP6 protocol. 11598514Sluigi */ 11698514Sluigi 11798514Sluigiextern struct inpcbhead ripcb; 11898514Sluigiextern struct inpcbinfo ripcbinfo; 11998514Sluigiextern u_long rip_sendspace; 12098514Sluigiextern u_long rip_recvspace; 12198514Sluigi 12298514Sluigistruct rip6stat rip6stat; 12398514Sluigi 12498514Sluigi/* 12598514Sluigi * Setup generic address and protocol structures 12698514Sluigi * for raw_input routine, then pass them along with 12798514Sluigi * mbuf chain. 12898514Sluigi */ 12998514Sluigiint 13098514Sluigirip6_input(mp, offp, proto) 13198514Sluigi struct mbuf **mp; 13298514Sluigi int *offp, proto; 13398514Sluigi{ 13498514Sluigi struct mbuf *m = *mp; 13598514Sluigi register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 13698514Sluigi register struct inpcb *in6p; 13798514Sluigi struct inpcb *last = 0; 13898514Sluigi struct mbuf *opts = NULL; 13998514Sluigi struct sockaddr_in6 fromsa; 14098514Sluigi 14198514Sluigi rip6stat.rip6s_ipackets++; 14298514Sluigi 14398514Sluigi if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 14498514Sluigi /* XXX send icmp6 host/port unreach? */ 14598514Sluigi m_freem(m); 14698514Sluigi return IPPROTO_DONE; 14798514Sluigi } 14898514Sluigi 14998514Sluigi init_sin6(&fromsa, m); /* general init */ 15098514Sluigi 15198514Sluigi INP_INFO_RLOCK(&ripcbinfo); 15298514Sluigi LIST_FOREACH(in6p, &ripcb, inp_list) { 15398514Sluigi INP_LOCK(in6p); 15498514Sluigi if ((in6p->in6p_vflag & INP_IPV6) == 0) { 15598514Sluigidocontinue: 15698514Sluigi INP_UNLOCK(in6p); 15798514Sluigi continue; 15898514Sluigi } 15998514Sluigi if (in6p->in6p_ip6_nxt && 16098514Sluigi in6p->in6p_ip6_nxt != proto) 16198514Sluigi goto docontinue; 16298514Sluigi if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && 16398514Sluigi !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) 16498514Sluigi goto docontinue; 16598514Sluigi if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && 16698514Sluigi !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) 16798514Sluigi goto docontinue; 16898514Sluigi if (in6p->in6p_cksum != -1) { 16998514Sluigi rip6stat.rip6s_isum++; 17098514Sluigi if (in6_cksum(m, proto, *offp, 17198514Sluigi m->m_pkthdr.len - *offp)) { 17298514Sluigi rip6stat.rip6s_badsum++; 17398514Sluigi goto docontinue; 17498514Sluigi } 17598514Sluigi } 17698514Sluigi if (last) { 17798514Sluigi struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 17898514Sluigi 17998514Sluigi#if defined(IPSEC) || defined(FAST_IPSEC) 18098514Sluigi /* 18198514Sluigi * Check AH/ESP integrity. 18298514Sluigi */ 18398514Sluigi if (n && ipsec6_in_reject(n, last)) { 18498514Sluigi m_freem(n); 18598514Sluigi#ifdef IPSEC 18698514Sluigi ipsec6stat.in_polvio++; 18798514Sluigi#endif /*IPSEC*/ 18898514Sluigi /* do not inject data into pcb */ 18998514Sluigi } else 19098514Sluigi#endif /*IPSEC || FAST_IPSEC*/ 19198514Sluigi if (n) { 19298514Sluigi if (last->in6p_flags & IN6P_CONTROLOPTS || 19398514Sluigi last->in6p_socket->so_options & SO_TIMESTAMP) 19498514Sluigi ip6_savecontrol(last, n, &opts); 19598514Sluigi /* strip intermediate headers */ 196229779Suqs m_adj(n, *offp); 19798514Sluigi if (sbappendaddr(&last->in6p_socket->so_rcv, 19898514Sluigi (struct sockaddr *)&fromsa, 19998514Sluigi n, opts) == 0) { 20098514Sluigi m_freem(n); 20198514Sluigi if (opts) 20298514Sluigi m_freem(opts); 20398514Sluigi rip6stat.rip6s_fullsock++; 20498514Sluigi } else 20598514Sluigi sorwakeup(last->in6p_socket); 20698514Sluigi opts = NULL; 20798514Sluigi } 20898514Sluigi INP_UNLOCK(last); 20998514Sluigi } 21098514Sluigi last = in6p; 21198514Sluigi } 21298514Sluigi#if defined(IPSEC) || defined(FAST_IPSEC) 21398514Sluigi /* 21498514Sluigi * Check AH/ESP integrity. 21598514Sluigi */ 21698514Sluigi if (last && ipsec6_in_reject(m, last)) { 21798514Sluigi m_freem(m); 21898514Sluigi#ifdef IPSEC 21998514Sluigi ipsec6stat.in_polvio++; 22098514Sluigi#endif /*IPSEC*/ 22198514Sluigi ip6stat.ip6s_delivered--; 22298514Sluigi /* do not inject data into pcb */ 22398514Sluigi INP_UNLOCK(last); 22498514Sluigi } else 22598514Sluigi#endif /*IPSEC || FAST_IPSEC*/ 22698514Sluigi if (last) { 22798514Sluigi if (last->in6p_flags & IN6P_CONTROLOPTS || 22898514Sluigi last->in6p_socket->so_options & SO_TIMESTAMP) 22998514Sluigi ip6_savecontrol(last, m, &opts); 23098514Sluigi /* strip intermediate headers */ 23198514Sluigi m_adj(m, *offp); 23298514Sluigi if (sbappendaddr(&last->in6p_socket->so_rcv, 23398514Sluigi (struct sockaddr *)&fromsa, m, opts) == 0) { 23498514Sluigi m_freem(m); 23598514Sluigi if (opts) 23698514Sluigi m_freem(opts); 23798514Sluigi rip6stat.rip6s_fullsock++; 23898514Sluigi } else 23998514Sluigi sorwakeup(last->in6p_socket); 24098514Sluigi INP_UNLOCK(last); 24198514Sluigi } else { 24298514Sluigi rip6stat.rip6s_nosock++; 24398514Sluigi if (m->m_flags & M_MCAST) 24498514Sluigi rip6stat.rip6s_nosockmcast++; 24598514Sluigi if (proto == IPPROTO_NONE) 24698514Sluigi m_freem(m); 24798514Sluigi else { 24898514Sluigi char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */ 24998514Sluigi icmp6_error(m, ICMP6_PARAM_PROB, 25098514Sluigi ICMP6_PARAMPROB_NEXTHEADER, 25198514Sluigi prvnxtp - mtod(m, char *)); 25298514Sluigi } 25398514Sluigi ip6stat.ip6s_delivered--; 25498514Sluigi } 25598514Sluigi INP_INFO_RUNLOCK(&ripcbinfo); 25698514Sluigi return IPPROTO_DONE; 25798514Sluigi} 25898514Sluigi 25998514Sluigivoid 26098514Sluigirip6_ctlinput(cmd, sa, d) 26198514Sluigi int cmd; 26298514Sluigi struct sockaddr *sa; 26398514Sluigi void *d; 26498514Sluigi{ 26598514Sluigi struct ip6_hdr *ip6; 26698514Sluigi struct mbuf *m; 26798514Sluigi int off = 0; 26898514Sluigi struct ip6ctlparam *ip6cp = NULL; 26998514Sluigi const struct sockaddr_in6 *sa6_src = NULL; 27098514Sluigi void *cmdarg; 27198514Sluigi struct inpcb *(*notify) __P((struct inpcb *, int)) = in6_rtchange; 27298514Sluigi 27398514Sluigi if (sa->sa_family != AF_INET6 || 27498514Sluigi sa->sa_len != sizeof(struct sockaddr_in6)) 27598514Sluigi return; 27698514Sluigi 27798514Sluigi if ((unsigned)cmd >= PRC_NCMDS) 27898514Sluigi return; 27998514Sluigi if (PRC_IS_REDIRECT(cmd)) 28098514Sluigi notify = in6_rtchange, d = NULL; 28198514Sluigi else if (cmd == PRC_HOSTDEAD) 28298514Sluigi d = NULL; 28398514Sluigi else if (inet6ctlerrmap[cmd] == 0) 28498514Sluigi return; 28598514Sluigi 28698514Sluigi /* if the parameter is from icmp6, decode it. */ 28798514Sluigi if (d != NULL) { 28898514Sluigi ip6cp = (struct ip6ctlparam *)d; 28998514Sluigi m = ip6cp->ip6c_m; 29098514Sluigi ip6 = ip6cp->ip6c_ip6; 29198514Sluigi off = ip6cp->ip6c_off; 29298514Sluigi cmdarg = ip6cp->ip6c_cmdarg; 29398514Sluigi sa6_src = ip6cp->ip6c_src; 29498514Sluigi } else { 29598514Sluigi m = NULL; 29698514Sluigi ip6 = NULL; 29798514Sluigi cmdarg = NULL; 29898514Sluigi sa6_src = &sa6_any; 29998514Sluigi } 30098514Sluigi 30198514Sluigi (void) in6_pcbnotify(&ripcbinfo, sa, 0, 30298514Sluigi (const struct sockaddr *)sa6_src, 30398514Sluigi 0, cmd, cmdarg, notify); 30498514Sluigi} 305 306/* 307 * Generate IPv6 header and pass packet to ip6_output. 308 * Tack on options user may have setup with control call. 309 */ 310int 311#if __STDC__ 312rip6_output(struct mbuf *m, ...) 313#else 314rip6_output(m, va_alist) 315 struct mbuf *m; 316 va_dcl 317#endif 318{ 319 struct mbuf *control; 320 struct socket *so; 321 struct sockaddr_in6 *dstsock; 322 struct in6_addr *dst; 323 struct ip6_hdr *ip6; 324 struct inpcb *in6p; 325 u_int plen = m->m_pkthdr.len; 326 int error = 0; 327 struct ip6_pktopts opt, *optp; 328 struct ifnet *oifp = NULL; 329 int type = 0, code = 0; /* for ICMPv6 output statistics only */ 330 int priv = 0; 331 int scope_ambiguous = 0; 332 struct in6_addr *in6a; 333 va_list ap; 334 335 va_start(ap, m); 336 so = va_arg(ap, struct socket *); 337 dstsock = va_arg(ap, struct sockaddr_in6 *); 338 control = va_arg(ap, struct mbuf *); 339 va_end(ap); 340 341 in6p = sotoin6pcb(so); 342 INP_LOCK(in6p); 343 344 priv = 0; 345 if (suser_cred(so->so_cred, SUSER_ALLOWJAIL) == 0) 346 priv = 1; 347 dst = &dstsock->sin6_addr; 348 if (control) { 349 if ((error = ip6_setpktopts(control, &opt, 350 in6p->in6p_outputopts, priv, so->so_proto->pr_protocol)) 351 != 0) { 352 goto bad; 353 } 354 optp = &opt; 355 } else 356 optp = in6p->in6p_outputopts; 357 358 /* 359 * Check and convert scope zone ID into internal form. 360 * XXX: we may still need to determine the zone later. 361 */ 362 if (!(so->so_state & SS_ISCONNECTED)) { 363 if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone) 364 scope_ambiguous = 1; 365 if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0) 366 goto bad; 367 } 368 369 /* 370 * For an ICMPv6 packet, we should know its type and code 371 * to update statistics. 372 */ 373 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 374 struct icmp6_hdr *icmp6; 375 if (m->m_len < sizeof(struct icmp6_hdr) && 376 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 377 error = ENOBUFS; 378 goto bad; 379 } 380 icmp6 = mtod(m, struct icmp6_hdr *); 381 type = icmp6->icmp6_type; 382 code = icmp6->icmp6_code; 383 } 384 385 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT); 386 if (m == NULL) { 387 error = ENOBUFS; 388 goto bad; 389 } 390 ip6 = mtod(m, struct ip6_hdr *); 391 392 /* 393 * Source address selection. 394 */ 395 if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions, NULL, 396 &in6p->in6p_laddr, &oifp, &error)) == NULL) { 397 if (error == 0) 398 error = EADDRNOTAVAIL; 399 goto bad; 400 } 401 ip6->ip6_src = *in6a; 402 403 if (oifp && scope_ambiguous) { 404 /* 405 * Application should provide a proper zone ID or the use of 406 * default zone IDs should be enabled. Unfortunately, some 407 * applications do not behave as it should, so we need a 408 * workaround. Even if an appropriate ID is not determined 409 * (when it's required), if we can determine the outgoing 410 * interface. determine the zone ID based on the interface. 411 */ 412 error = in6_setscope(&dstsock->sin6_addr, oifp, NULL); 413 if (error != 0) 414 goto bad; 415 } 416 ip6->ip6_dst = dstsock->sin6_addr; 417 418 /* fill in the rest of the IPv6 header fields */ 419 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 420 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK); 421 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 422 (IPV6_VERSION & IPV6_VERSION_MASK); 423 /* ip6_plen will be filled in ip6_output, so not fill it here. */ 424 ip6->ip6_nxt = in6p->in6p_ip6_nxt; 425 ip6->ip6_hlim = in6_selecthlim(in6p, oifp); 426 427 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || 428 in6p->in6p_cksum != -1) { 429 struct mbuf *n; 430 int off; 431 u_int16_t *p; 432 433 /* compute checksum */ 434 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 435 off = offsetof(struct icmp6_hdr, icmp6_cksum); 436 else 437 off = in6p->in6p_cksum; 438 if (plen < off + 1) { 439 error = EINVAL; 440 goto bad; 441 } 442 off += sizeof(struct ip6_hdr); 443 444 n = m; 445 while (n && n->m_len <= off) { 446 off -= n->m_len; 447 n = n->m_next; 448 } 449 if (!n) 450 goto bad; 451 p = (u_int16_t *)(mtod(n, caddr_t) + off); 452 *p = 0; 453 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 454 } 455 456 error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p); 457 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { 458 if (oifp) 459 icmp6_ifoutstat_inc(oifp, type, code); 460 icmp6stat.icp6s_outhist[type]++; 461 } else 462 rip6stat.rip6s_opackets++; 463 464 goto freectl; 465 466 bad: 467 if (m) 468 m_freem(m); 469 470 freectl: 471 if (control) { 472 ip6_clearpktopts(&opt, -1); 473 m_freem(control); 474 } 475 INP_UNLOCK(in6p); 476 return (error); 477} 478 479/* 480 * Raw IPv6 socket option processing. 481 */ 482int 483rip6_ctloutput(so, sopt) 484 struct socket *so; 485 struct sockopt *sopt; 486{ 487 int error; 488 489 if (sopt->sopt_level == IPPROTO_ICMPV6) 490 /* 491 * XXX: is it better to call icmp6_ctloutput() directly 492 * from protosw? 493 */ 494 return (icmp6_ctloutput(so, sopt)); 495 else if (sopt->sopt_level != IPPROTO_IPV6) 496 return (EINVAL); 497 498 error = 0; 499 500 switch (sopt->sopt_dir) { 501 case SOPT_GET: 502 switch (sopt->sopt_name) { 503 case MRT6_INIT: 504 case MRT6_DONE: 505 case MRT6_ADD_MIF: 506 case MRT6_DEL_MIF: 507 case MRT6_ADD_MFC: 508 case MRT6_DEL_MFC: 509 case MRT6_PIM: 510 error = ip6_mrouter_get(so, sopt); 511 break; 512 case IPV6_CHECKSUM: 513 error = ip6_raw_ctloutput(so, sopt); 514 break; 515 default: 516 error = ip6_ctloutput(so, sopt); 517 break; 518 } 519 break; 520 521 case SOPT_SET: 522 switch (sopt->sopt_name) { 523 case MRT6_INIT: 524 case MRT6_DONE: 525 case MRT6_ADD_MIF: 526 case MRT6_DEL_MIF: 527 case MRT6_ADD_MFC: 528 case MRT6_DEL_MFC: 529 case MRT6_PIM: 530 error = ip6_mrouter_set(so, sopt); 531 break; 532 case IPV6_CHECKSUM: 533 error = ip6_raw_ctloutput(so, sopt); 534 break; 535 default: 536 error = ip6_ctloutput(so, sopt); 537 break; 538 } 539 break; 540 } 541 542 return (error); 543} 544 545static int 546rip6_attach(struct socket *so, int proto, struct thread *td) 547{ 548 struct inpcb *inp; 549 struct icmp6_filter *filter; 550 int error; 551 552 inp = sotoinpcb(so); 553 KASSERT(inp == NULL, ("rip6_attach: inp != NULL")); 554 if (td && (error = suser(td)) != 0) 555 return error; 556 error = soreserve(so, rip_sendspace, rip_recvspace); 557 if (error) 558 return error; 559 MALLOC(filter, struct icmp6_filter *, 560 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 561 if (filter == NULL) 562 return ENOMEM; 563 INP_INFO_WLOCK(&ripcbinfo); 564 error = in_pcballoc(so, &ripcbinfo); 565 if (error) { 566 INP_INFO_WUNLOCK(&ripcbinfo); 567 FREE(filter, M_PCB); 568 return error; 569 } 570 inp = (struct inpcb *)so->so_pcb; 571 INP_INFO_WUNLOCK(&ripcbinfo); 572 inp->inp_vflag |= INP_IPV6; 573 inp->in6p_ip6_nxt = (long)proto; 574 inp->in6p_hops = -1; /* use kernel default */ 575 inp->in6p_cksum = -1; 576 inp->in6p_icmp6filt = filter; 577 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 578 INP_UNLOCK(inp); 579 return 0; 580} 581 582static void 583rip6_detach(struct socket *so) 584{ 585 struct inpcb *inp; 586 587 inp = sotoinpcb(so); 588 KASSERT(inp != NULL, ("rip6_detach: inp == NULL")); 589 590 /* xxx: RSVP */ 591 if (so == ip6_mrouter) 592 ip6_mrouter_done(); 593 INP_INFO_WLOCK(&ripcbinfo); 594 INP_LOCK(inp); 595 if (inp->in6p_icmp6filt) { 596 FREE(inp->in6p_icmp6filt, M_PCB); 597 inp->in6p_icmp6filt = NULL; 598 } 599 in6_pcbdetach(inp); 600 in6_pcbfree(inp); 601 INP_INFO_WUNLOCK(&ripcbinfo); 602} 603 604/* XXXRW: This can't ever be called. */ 605static void 606rip6_abort(struct socket *so) 607{ 608 struct inpcb *inp; 609 610 inp = sotoinpcb(so); 611 KASSERT(inp != NULL, ("rip6_abort: inp == NULL")); 612 613 soisdisconnected(so); 614} 615 616static void 617rip6_close(struct socket *so) 618{ 619 struct inpcb *inp; 620 621 inp = sotoinpcb(so); 622 KASSERT(inp != NULL, ("rip6_close: inp == NULL")); 623 624 soisdisconnected(so); 625} 626 627static int 628rip6_disconnect(struct socket *so) 629{ 630 struct inpcb *inp = sotoinpcb(so); 631 632 if ((so->so_state & SS_ISCONNECTED) == 0) 633 return ENOTCONN; 634 inp->in6p_faddr = in6addr_any; 635 rip6_abort(so); 636 return (0); 637} 638 639static int 640rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 641{ 642 struct inpcb *inp = sotoinpcb(so); 643 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 644 struct ifaddr *ia = NULL; 645 int error = 0; 646 647 KASSERT(inp != NULL, ("rip6_bind: inp == NULL")); 648 if (nam->sa_len != sizeof(*addr)) 649 return EINVAL; 650 if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6) 651 return EADDRNOTAVAIL; 652 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0) 653 return(error); 654 655 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 656 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) 657 return EADDRNOTAVAIL; 658 if (ia && 659 ((struct in6_ifaddr *)ia)->ia6_flags & 660 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 661 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 662 return (EADDRNOTAVAIL); 663 } 664 INP_INFO_WLOCK(&ripcbinfo); 665 INP_LOCK(inp); 666 inp->in6p_laddr = addr->sin6_addr; 667 INP_UNLOCK(inp); 668 INP_INFO_WUNLOCK(&ripcbinfo); 669 return 0; 670} 671 672static int 673rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 674{ 675 struct inpcb *inp = sotoinpcb(so); 676 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 677 struct in6_addr *in6a = NULL; 678 struct ifnet *ifp = NULL; 679 int error = 0, scope_ambiguous = 0; 680 681 KASSERT(inp != NULL, ("rip6_connect: inp == NULL")); 682 if (nam->sa_len != sizeof(*addr)) 683 return EINVAL; 684 if (TAILQ_EMPTY(&ifnet)) 685 return EADDRNOTAVAIL; 686 if (addr->sin6_family != AF_INET6) 687 return EAFNOSUPPORT; 688 689 /* 690 * Application should provide a proper zone ID or the use of 691 * default zone IDs should be enabled. Unfortunately, some 692 * applications do not behave as it should, so we need a 693 * workaround. Even if an appropriate ID is not determined, 694 * we'll see if we can determine the outgoing interface. If we 695 * can, determine the zone ID based on the interface below. 696 */ 697 if (addr->sin6_scope_id == 0 && !ip6_use_defzone) 698 scope_ambiguous = 1; 699 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0) 700 return(error); 701 702 INP_INFO_WLOCK(&ripcbinfo); 703 INP_LOCK(inp); 704 /* Source address selection. XXX: need pcblookup? */ 705 in6a = in6_selectsrc(addr, inp->in6p_outputopts, 706 inp->in6p_moptions, NULL, 707 &inp->in6p_laddr, &ifp, &error); 708 if (in6a == NULL) { 709 INP_UNLOCK(inp); 710 INP_INFO_WUNLOCK(&ripcbinfo); 711 return (error ? error : EADDRNOTAVAIL); 712 } 713 714 /* XXX: see above */ 715 if (ifp && scope_ambiguous && 716 (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) { 717 INP_UNLOCK(inp); 718 INP_INFO_WUNLOCK(&ripcbinfo); 719 return(error); 720 } 721 inp->in6p_faddr = addr->sin6_addr; 722 inp->in6p_laddr = *in6a; 723 soisconnected(so); 724 INP_UNLOCK(inp); 725 INP_INFO_WUNLOCK(&ripcbinfo); 726 return 0; 727} 728 729static int 730rip6_shutdown(struct socket *so) 731{ 732 struct inpcb *inp; 733 734 inp = sotoinpcb(so); 735 KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL")); 736 INP_LOCK(inp); 737 socantsendmore(so); 738 INP_UNLOCK(inp); 739 return 0; 740} 741 742static int 743rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 744 struct mbuf *control, struct thread *td) 745{ 746 struct inpcb *inp = sotoinpcb(so); 747 struct sockaddr_in6 tmp; 748 struct sockaddr_in6 *dst; 749 int ret; 750 751 KASSERT(inp != NULL, ("rip6_send: inp == NULL")); 752 INP_INFO_WLOCK(&ripcbinfo); 753 /* always copy sockaddr to avoid overwrites */ 754 /* Unlocked read. */ 755 if (so->so_state & SS_ISCONNECTED) { 756 if (nam) { 757 INP_INFO_WUNLOCK(&ripcbinfo); 758 m_freem(m); 759 return EISCONN; 760 } 761 /* XXX */ 762 bzero(&tmp, sizeof(tmp)); 763 tmp.sin6_family = AF_INET6; 764 tmp.sin6_len = sizeof(struct sockaddr_in6); 765 bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 766 sizeof(struct in6_addr)); 767 dst = &tmp; 768 } else { 769 if (nam == NULL) { 770 INP_INFO_WUNLOCK(&ripcbinfo); 771 m_freem(m); 772 return ENOTCONN; 773 } 774 if (nam->sa_len != sizeof(struct sockaddr_in6)) { 775 INP_INFO_WUNLOCK(&ripcbinfo); 776 m_freem(m); 777 return(EINVAL); 778 } 779 tmp = *(struct sockaddr_in6 *)nam; 780 dst = &tmp; 781 782 if (dst->sin6_family == AF_UNSPEC) { 783 /* 784 * XXX: we allow this case for backward 785 * compatibility to buggy applications that 786 * rely on old (and wrong) kernel behavior. 787 */ 788 log(LOG_INFO, "rip6 SEND: address family is " 789 "unspec. Assume AF_INET6\n"); 790 dst->sin6_family = AF_INET6; 791 } else if (dst->sin6_family != AF_INET6) { 792 INP_INFO_WUNLOCK(&ripcbinfo); 793 m_freem(m); 794 return(EAFNOSUPPORT); 795 } 796 } 797 ret = rip6_output(m, so, dst, control); 798 INP_INFO_WUNLOCK(&ripcbinfo); 799 return (ret); 800} 801 802struct pr_usrreqs rip6_usrreqs = { 803 .pru_abort = rip6_abort, 804 .pru_attach = rip6_attach, 805 .pru_bind = rip6_bind, 806 .pru_connect = rip6_connect, 807 .pru_control = in6_control, 808 .pru_detach = rip6_detach, 809 .pru_disconnect = rip6_disconnect, 810 .pru_peeraddr = in6_setpeeraddr, 811 .pru_send = rip6_send, 812 .pru_shutdown = rip6_shutdown, 813 .pru_sockaddr = in6_setsockaddr, 814 .pru_close = rip6_close, 815}; 816