ip6_output.c revision 122922
190618Stmm/* $FreeBSD: head/sys/netinet6/ip6_output.c 122922 2003-11-20 20:07:39Z andre $ */ 2219567Smarius/* $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $ */ 390618Stmm 490618Stmm/* 590618Stmm * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 690618Stmm * All rights reserved. 790618Stmm * 890618Stmm * Redistribution and use in source and binary forms, with or without 990618Stmm * modification, are permitted provided that the following conditions 1090618Stmm * are met: 1190618Stmm * 1. Redistributions of source code must retain the above copyright 1290618Stmm * notice, this list of conditions and the following disclaimer. 13219567Smarius * 2. Redistributions in binary form must reproduce the above copyright 14219567Smarius * notice, this list of conditions and the following disclaimer in the 1590618Stmm * documentation and/or other materials provided with the distribution. 16219567Smarius * 3. Neither the name of the project nor the names of its contributors 17219567Smarius * may be used to endorse or promote products derived from this software 18219567Smarius * without specific prior written permission. 19219567Smarius * 20219567Smarius * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21219567Smarius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22219567Smarius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23219567Smarius * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24219567Smarius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2590618Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2690618Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27219567Smarius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28219567Smarius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2990618Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30139825Simp * SUCH DAMAGE. 3190618Stmm */ 32167308Smarius 3390618Stmm/* 3490618Stmm * Copyright (c) 1982, 1986, 1988, 1990, 1993 3590618Stmm * The Regents of the University of California. All rights reserved. 3690618Stmm * 3790618Stmm * Redistribution and use in source and binary forms, with or without 3890618Stmm * modification, are permitted provided that the following conditions 3990618Stmm * are met: 4090618Stmm * 1. Redistributions of source code must retain the above copyright 4190618Stmm * notice, this list of conditions and the following disclaimer. 4290618Stmm * 2. Redistributions in binary form must reproduce the above copyright 4390618Stmm * notice, this list of conditions and the following disclaimer in the 4490618Stmm * documentation and/or other materials provided with the distribution. 4590618Stmm * 3. All advertising materials mentioning features or use of this software 4690618Stmm * must display the following acknowledgement: 4790618Stmm * This product includes software developed by the University of 4890618Stmm * California, Berkeley and its contributors. 4990618Stmm * 4. Neither the name of the University nor the names of its contributors 5090618Stmm * may be used to endorse or promote products derived from this software 5190618Stmm * without specific prior written permission. 5290618Stmm * 5390618Stmm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54145185Smarius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55145185Smarius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56145185Smarius * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5790618Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58145185Smarius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5990618Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60131376Smarius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 6190618Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 6290618Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 6390618Stmm * SUCH DAMAGE. 6490618Stmm * 6590618Stmm * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 66130068Sphk */ 67107477Stmm 68171730Smarius#include "opt_ip6fw.h" 6990618Stmm#include "opt_inet.h" 70171730Smarius#include "opt_inet6.h" 7190618Stmm#include "opt_ipsec.h" 72133589Smarius#include "opt_pfil_hooks.h" 73152684Smarius#include "opt_random_ip_id.h" 74119338Simp 7590618Stmm#include <sys/param.h> 7690618Stmm#include <sys/malloc.h> 77171730Smarius#include <sys/mbuf.h> 78116541Stmm#include <sys/proc.h> 7990618Stmm#include <sys/errno.h> 80171730Smarius#include <sys/protosw.h> 8190618Stmm#include <sys/socket.h> 8290618Stmm#include <sys/socketvar.h> 8390618Stmm#include <sys/systm.h> 8490618Stmm#include <sys/kernel.h> 8590618Stmm 8690618Stmm#include <net/if.h> 8790618Stmm#include <net/route.h> 8890618Stmm#ifdef PFIL_HOOKS 89145186Smarius#include <net/pfil.h> 9090618Stmm#endif 9190618Stmm 92152684Smarius#include <netinet/in.h> 9390618Stmm#include <netinet/in_var.h> 9490618Stmm#include <netinet6/in6_var.h> 9590618Stmm#include <netinet/ip6.h> 9690618Stmm#include <netinet/icmp6.h> 9790618Stmm#include <netinet6/ip6_var.h> 9890618Stmm#include <netinet/in_pcb.h> 9990618Stmm#include <netinet/tcp_var.h> 10090618Stmm#include <netinet6/nd6.h> 10190618Stmm 10290618Stmm#ifdef IPSEC 10390618Stmm#include <netinet6/ipsec.h> 10490618Stmm#ifdef INET6 10590618Stmm#include <netinet6/ipsec6.h> 10690618Stmm#endif 10790618Stmm#include <netkey/key.h> 10890618Stmm#endif /* IPSEC */ 109172066Smarius 11090618Stmm#ifdef FAST_IPSEC 11190618Stmm#include <netipsec/ipsec.h> 11290618Stmm#include <netipsec/ipsec6.h> 11390618Stmm#include <netipsec/key.h> 114145185Smarius#endif /* FAST_IPSEC */ 11590618Stmm 11690618Stmm#include <netinet6/ip6_fw.h> 117145185Smarius 118145185Smarius#include <net/net_osdep.h> 11990618Stmm 12090618Stmm#include <netinet6/ip6protosw.h> 12190618Stmm 12290618Stmmstatic MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options"); 12390618Stmm 12490618Stmmstruct ip6_exthdrs { 12590618Stmm struct mbuf *ip6e_ip6; 126206018Smarius struct mbuf *ip6e_hbh; 127170852Smarius struct mbuf *ip6e_dest1; 128206018Smarius struct mbuf *ip6e_rthdr; 129170852Smarius struct mbuf *ip6e_dest2; 13090618Stmm}; 131145185Smarius 132145186Smariusstatic int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **, 133145185Smarius int, int)); 134145185Smariusstatic int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *, 135145185Smarius struct socket *, struct sockopt *)); 136145185Smariusstatic int ip6_getpcbopt __P((struct ip6_pktopts *, int, struct sockopt *)); 137145185Smariusstatic int ip6_setpktoption __P((int, u_char *, int, struct ip6_pktopts *, int, 138145185Smarius int, int, int)); 139225931Smarius 140225931Smariusstatic int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *)); 141145185Smariusstatic int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **)); 142167308Smariusstatic int ip6_copyexthdr __P((struct mbuf **, caddr_t, int)); 143152684Smariusstatic int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int, 14490618Stmm struct ip6_frag **)); 145190112Smariusstatic int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t)); 146152684Smariusstatic int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *)); 147152684Smariusstatic int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *, 148152684Smarius struct ifnet *, struct in6_addr *, u_long *, int *)); 149172066Smarius 150172066Smarius 151178443Smarius/* 152178443Smarius * IP6 output. The packet in mbuf chain m contains a skeletal IP6 153172066Smarius * header (with pri, len, nxt, hlim, src, dst). 154172066Smarius * This function may modify ver and hlim only. 155247621Smarius * The mbuf chain containing the packet will be freed. 156247621Smarius * The mbuf opt, if present, will not be freed. 157152684Smarius * 15890618Stmm * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and 15990618Stmm * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one, 16090618Stmm * which is rt_rmx.rmx_mtu. 16190618Stmm */ 162145186Smariusint 163154870Smariusip6_output(m0, opt, ro, flags, im6o, ifpp, inp) 164154870Smarius struct mbuf *m0; 165154870Smarius struct ip6_pktopts *opt; 16690618Stmm struct route_in6 *ro; 16790618Stmm int flags; 16890618Stmm struct ip6_moptions *im6o; 16990618Stmm struct ifnet **ifpp; /* XXX: just for statistics */ 17090618Stmm struct inpcb *inp; 17190618Stmm{ 172225931Smarius struct ip6_hdr *ip6, *mhip6; 173225931Smarius struct ifnet *ifp, *origifp; 174225931Smarius struct mbuf *m = m0; 17590618Stmm int hlen, tlen, len, off; 176225931Smarius struct route_in6 ip6route; 177190112Smarius struct sockaddr_in6 *dst; 178190112Smarius int error = 0; 17990618Stmm struct in6_ifaddr *ia = NULL; 180190114Smarius u_long mtu; 181167308Smarius int alwaysfrag, dontfrag; 18290618Stmm u_int32_t optlen = 0, plen = 0, unfragpartlen = 0; 183133589Smarius struct ip6_exthdrs exthdrs; 184152684Smarius struct in6_addr finaldst; 185152684Smarius struct route_in6 *ro_pmtu = NULL; 186152684Smarius int hdrsplit = 0; 187152684Smarius int needipsec = 0; 188152684Smarius#ifdef FAST_IPSEC 189152684Smarius int needipsectun = 0; 190133589Smarius struct secpolicy *sp = NULL; 191227848Smarius#endif /* FAST_IPSEC */ 19290618Stmm#ifdef IPSEC 19390618Stmm int needipsectun = 0; 19490618Stmm struct socket *so; 19590618Stmm struct secpolicy *sp = NULL; 19690618Stmm 19790618Stmm /* for AH processing. stupid to have "socket" variable in IP layer... */ 19890618Stmm so = ipsec_getsocket(m); 19990618Stmm (void)ipsec_setsocket(m, NULL); 20090618Stmm#endif /* IPSEC */ 20190618Stmm 202247601Smarius ip6 = mtod(m, struct ip6_hdr *); 203200874Smarius 204200816Smarius#define MAKE_EXTHDR(hp, mp) \ 205182062Smarius do { \ 20690618Stmm if (hp) { \ 20790618Stmm struct ip6_ext *eh = (struct ip6_ext *)(hp); \ 20890618Stmm error = ip6_copyexthdr((mp), (caddr_t)(hp), \ 20990618Stmm ((eh)->ip6e_len + 1) << 3); \ 210172066Smarius if (error) \ 211172066Smarius goto freehdrs; \ 212172066Smarius } \ 213178443Smarius } while (/*CONSTCOND*/ 0) 214178443Smarius 215172066Smarius bzero(&exthdrs, sizeof(exthdrs)); 216172066Smarius 217172066Smarius if (opt) { 218172066Smarius /* Hop-by-Hop options header */ 219172066Smarius MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh); 220172066Smarius /* Destination options header(1st part) */ 221172066Smarius if (opt->ip6po_rthdr) { 222172066Smarius /* 223190112Smarius * Destination options header(1st part) 224146391Smarius * This only makes sence with a routing header. 225146391Smarius * See Section 9.2 of RFC 3542. 226146391Smarius * Disabling this part just for MIP6 convenience is 227146391Smarius * a bad idea. We need to think carefully about a 228146391Smarius * way to make the advanced API coexist with MIP6 22990618Stmm * options, which might automatically be inserted in 230190112Smarius * the kernel. 231146391Smarius */ 232146391Smarius MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1); 233146391Smarius } 234146391Smarius /* Routing header */ 235146391Smarius MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr); 236146391Smarius /* Destination options header(2nd part) */ 237146391Smarius MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2); 238146391Smarius } 239146391Smarius 240146391Smarius#ifdef IPSEC 241146391Smarius /* get a security policy for this packet */ 242146391Smarius if (so == NULL) 243146391Smarius sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error); 24490618Stmm else 24590618Stmm sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error); 246167308Smarius 247145186Smarius if (sp == NULL) { 248167308Smarius ipsec6stat.out_inval++; 249145186Smarius goto freehdrs; 250167308Smarius } 251145186Smarius 252145186Smarius error = 0; 253145186Smarius 254145186Smarius /* check policy */ 255145186Smarius switch (sp->policy) { 256145186Smarius case IPSEC_POLICY_DISCARD: 257145186Smarius /* 258145186Smarius * This packet is just discarded. 259145185Smarius */ 26090618Stmm ipsec6stat.out_polvio++; 261172066Smarius goto freehdrs; 26290618Stmm 26390618Stmm case IPSEC_POLICY_BYPASS: 264167308Smarius case IPSEC_POLICY_NONE: 26590618Stmm /* no need to do IPsec. */ 266172066Smarius needipsec = 0; 26790618Stmm break; 268172066Smarius 269145185Smarius case IPSEC_POLICY_IPSEC: 270190112Smarius if (sp->req == NULL) { 271190112Smarius /* acquire a policy */ 27290618Stmm error = key_spdacquire(sp); 273145185Smarius goto freehdrs; 274172066Smarius } 275167308Smarius needipsec = 1; 276145185Smarius break; 277190112Smarius 278190112Smarius case IPSEC_POLICY_ENTRUST: 279167308Smarius default: 280167308Smarius printf("ip6_output: Invalid policy found. %d\n", sp->policy); 281145185Smarius } 28290618Stmm#endif /* IPSEC */ 283190112Smarius#ifdef FAST_IPSEC 284145185Smarius /* get a security policy for this packet */ 285190112Smarius if (inp == NULL) 28690618Stmm sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error); 28790618Stmm else 28890618Stmm sp = ipsec_getpolicybysock(m, IPSEC_DIR_OUTBOUND, inp, &error); 28990618Stmm 29090618Stmm if (sp == NULL) { 291190112Smarius newipsecstat.ips_out_inval++; 292190112Smarius goto freehdrs; 293190112Smarius } 294190112Smarius 295190112Smarius error = 0; 29690618Stmm 29790618Stmm /* check policy */ 29890618Stmm switch (sp->policy) { 29990618Stmm case IPSEC_POLICY_DISCARD: 30090618Stmm /* 30190618Stmm * This packet is just discarded. 302145185Smarius */ 30390618Stmm newipsecstat.ips_out_polvio++; 304225229Smarius goto freehdrs; 305225229Smarius 30690618Stmm case IPSEC_POLICY_BYPASS: 307145185Smarius case IPSEC_POLICY_NONE: 30890618Stmm /* no need to do IPsec. */ 30990618Stmm needipsec = 0; 31090618Stmm break; 31190618Stmm 312167308Smarius case IPSEC_POLICY_IPSEC: 31390618Stmm if (sp->req == NULL) { 31490618Stmm /* acquire a policy */ 31590618Stmm error = key_spdacquire(sp); 31690618Stmm goto freehdrs; 31790618Stmm } 31890618Stmm needipsec = 1; 319190112Smarius break; 320167308Smarius 321190112Smarius case IPSEC_POLICY_ENTRUST: 322167308Smarius default: 323145185Smarius printf("ip6_output: Invalid policy found. %d\n", sp->policy); 32490618Stmm } 32590618Stmm#endif /* FAST_IPSEC */ 32690618Stmm 32790618Stmm /* 32890618Stmm * Calculate the total length of the extension header chain. 329145185Smarius * Keep the length of the unfragmentable part for fragmentation. 33090618Stmm */ 33190618Stmm optlen = 0; 33290618Stmm if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len; 33390618Stmm if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len; 33490618Stmm if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len; 33590618Stmm unfragpartlen = optlen + sizeof(struct ip6_hdr); 33690618Stmm /* NOTE: we don't add AH/ESP length here. do that later. */ 33790618Stmm if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len; 33890618Stmm 339190112Smarius /* 34090618Stmm * If we need IPsec, or there is at least one extension header, 341190112Smarius * separate IP6 header from the payload. 342190112Smarius */ 34390618Stmm if ((needipsec || optlen) && !hdrsplit) { 34490618Stmm if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 34590618Stmm m = NULL; 34690618Stmm goto freehdrs; 347171730Smarius } 348170852Smarius m = exthdrs.ip6e_ip6; 349170852Smarius hdrsplit++; 35090618Stmm } 35190618Stmm 35290618Stmm /* adjust pointer */ 35390618Stmm ip6 = mtod(m, struct ip6_hdr *); 35490618Stmm 35590618Stmm /* adjust mbuf packet header length */ 35690618Stmm m->m_pkthdr.len += optlen; 357123865Sobrien plen = m->m_pkthdr.len - sizeof(*ip6); 35890618Stmm 359100188Stmm /* If this is a jumbo payload, insert a jumbo payload option. */ 360100188Stmm if (plen > IPV6_MAXPACKET) { 361100188Stmm if (!hdrsplit) { 362100188Stmm if ((error = ip6_splithdr(m, &exthdrs)) != 0) { 363100188Stmm m = NULL; 364178840Smarius goto freehdrs; 36590618Stmm } 366116213Stmm m = exthdrs.ip6e_ip6; 367171730Smarius hdrsplit++; 368171730Smarius } 369171730Smarius /* adjust pointer */ 370145185Smarius ip6 = mtod(m, struct ip6_hdr *); 371116213Stmm if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0) 372116213Stmm goto freehdrs; 373116541Stmm ip6->ip6_plen = 0; 374116213Stmm } else 375225931Smarius ip6->ip6_plen = htons(plen); 376172066Smarius 377172066Smarius /* 378190112Smarius * Concatenate headers and fill in next header fields. 379172066Smarius * Here we have, on "m" 380172066Smarius * IPv6 payload 381172066Smarius * and we insert headers accordingly. Finally, we should be getting: 382172066Smarius * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload] 383172066Smarius * 384172066Smarius * during the header composing process, "m" points to IPv6 header. 385172066Smarius * "mprev" points to an extension header prior to esp. 386172066Smarius */ 387172066Smarius { 388172066Smarius u_char *nexthdrp = &ip6->ip6_nxt; 389172066Smarius struct mbuf *mprev = m; 390172066Smarius 391172066Smarius /* 392172066Smarius * we treat dest2 specially. this makes IPsec processing 393172066Smarius * much easier. the goal here is to make mprev point the 394172066Smarius * mbuf prior to dest2. 395172066Smarius * 396172066Smarius * result: IPv6 dest2 payload 397190112Smarius * m and mprev will point to IPv6 header. 398190112Smarius */ 399190112Smarius if (exthdrs.ip6e_dest2) { 400190112Smarius if (!hdrsplit) 401190112Smarius panic("assumption failed: hdr not split"); 402172066Smarius exthdrs.ip6e_dest2->m_next = m->m_next; 403172066Smarius m->m_next = exthdrs.ip6e_dest2; 404145185Smarius *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt; 405190112Smarius ip6->ip6_nxt = IPPROTO_DSTOPTS; 406190112Smarius } 407167308Smarius 408166034Smarius#define MAKE_CHAIN(m, mp, p, i)\ 409172066Smarius do {\ 410172066Smarius if (m) {\ 411172066Smarius if (!hdrsplit) \ 412216961Smarius panic("assumption failed: hdr not split"); \ 413247621Smarius *mtod((m), u_char *) = *(p);\ 414166034Smarius *(p) = (i);\ 415190112Smarius p = mtod((m), u_char *);\ 416190112Smarius (m)->m_next = (mp)->m_next;\ 417167308Smarius (mp)->m_next = (m);\ 418166034Smarius (mp) = (m);\ 419172066Smarius }\ 420172066Smarius } while (/*CONSTCOND*/ 0) 421172066Smarius /* 422216961Smarius * result: IPv6 hbh dest1 rthdr dest2 payload 423247621Smarius * m will point to IPv6 header. mprev will point to the 424166034Smarius * extension header prior to dest2 (rthdr in the above case). 42590618Stmm */ 42690618Stmm MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS); 427178840Smarius MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, 428178840Smarius IPPROTO_DSTOPTS); 429170852Smarius MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, 43090618Stmm IPPROTO_ROUTING); 43190618Stmm 43290618Stmm#if defined(IPSEC) || defined(FAST_IPSEC) 43390618Stmm if (!needipsec) 43490618Stmm goto skip_ipsec2; 43590618Stmm 436152684Smarius /* 43790618Stmm * pointers after IPsec headers are not valid any more. 438146391Smarius * other pointers need a great care too. 439146391Smarius * (IPsec routines should not mangle mbufs prior to AH/ESP) 440146391Smarius */ 441146391Smarius exthdrs.ip6e_dest2 = NULL; 442146391Smarius 443146391Smarius { 444146391Smarius struct ip6_rthdr *rh = NULL; 445146391Smarius int segleft_org = 0; 446146391Smarius struct ipsec_output_state state; 447146391Smarius 448146391Smarius if (exthdrs.ip6e_rthdr) { 449146391Smarius rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *); 450152684Smarius segleft_org = rh->ip6r_segleft; 451152684Smarius rh->ip6r_segleft = 0; 452152684Smarius } 453152684Smarius 454152684Smarius bzero(&state, sizeof(state)); 455152684Smarius state.m = m; 456152684Smarius error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags, 457152684Smarius &needipsectun); 458152684Smarius m = state.m; 45990618Stmm if (error) { 46090618Stmm /* mbuf is already reclaimed in ipsec6_output_trans. */ 461145186Smarius m = NULL; 46290618Stmm switch (error) { 46390618Stmm case EHOSTUNREACH: 46490618Stmm case ENETUNREACH: 465152684Smarius case EMSGSIZE: 46690618Stmm case ENOBUFS: 46790618Stmm case ENOMEM: 46890618Stmm break; 46990618Stmm default: 47090618Stmm printf("ip6_output (ipsec): error code %d\n", error); 47190618Stmm /* FALLTHROUGH */ 472111119Simp case ENOENT: 473152684Smarius /* don't show these error codes to the user */ 474152684Smarius error = 0; 47590618Stmm break; 476152684Smarius } 47790618Stmm goto bad; 47890618Stmm } 47990618Stmm if (exthdrs.ip6e_rthdr) { 48090618Stmm /* ah6_output doesn't modify mbuf chain */ 481152684Smarius rh->ip6r_segleft = segleft_org; 482152684Smarius } 483152684Smarius } 484152684Smariusskip_ipsec2:; 485152684Smarius#endif 48690618Stmm } 48790618Stmm 48890618Stmm /* 48990618Stmm * If there is a routing header, replace the destination address field 49090618Stmm * with the first hop of the routing header. 49190618Stmm */ 49290618Stmm if (exthdrs.ip6e_rthdr) { 49390618Stmm struct ip6_rthdr *rh = 49490618Stmm (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr, 495152684Smarius struct ip6_rthdr *)); 496152684Smarius struct ip6_rthdr0 *rh0; 497152684Smarius struct in6_addr *addrs; 498152684Smarius 499152684Smarius finaldst = ip6->ip6_dst; 500152684Smarius switch (rh->ip6r_type) { 50190618Stmm case IPV6_RTHDR_TYPE_0: 50290618Stmm rh0 = (struct ip6_rthdr0 *)rh; 50390618Stmm addrs = (struct in6_addr *)(rh + 1); 50490618Stmm 50590618Stmm ip6->ip6_dst = *addrs; 50690618Stmm bcopy((caddr_t)(addrs + 1), (caddr_t)addrs, 50790618Stmm sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1) 50890618Stmm ); 50990618Stmm *(addrs + rh0->ip6r0_segleft - 1) = finaldst; 51090618Stmm break; 511145185Smarius default: /* is it possible? */ 51290618Stmm error = EINVAL; 513145185Smarius goto bad; 514145185Smarius } 51590618Stmm } 51690618Stmm 51790618Stmm /* Source address validation */ 51890618Stmm if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && 519145185Smarius (flags & IPV6_DADOUTPUT) == 0) { 52090618Stmm error = EOPNOTSUPP; 52190618Stmm ip6stat.ip6s_badscope++; 522107477Stmm goto bad; 52390618Stmm } 524172066Smarius if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 52590618Stmm error = EOPNOTSUPP; 52690618Stmm ip6stat.ip6s_badscope++; 52790618Stmm goto bad; 52890618Stmm } 52990618Stmm 53090618Stmm ip6stat.ip6s_localout++; 53190618Stmm 53290618Stmm /* 53390618Stmm * Route packet. 53490618Stmm */ 535145186Smarius if (ro == 0) { 536145186Smarius ro = &ip6route; 537145186Smarius bzero((caddr_t)ro, sizeof(*ro)); 53890618Stmm } 53990618Stmm ro_pmtu = ro; 540152684Smarius if (opt && opt->ip6po_rthdr) 541152684Smarius ro = &opt->ip6po_route; 542152684Smarius dst = (struct sockaddr_in6 *)&ro->ro_dst; 543152684Smarius 54490618Stmm /* 54590618Stmm * If there is a cached route, 54690618Stmm * check that it is to the same destination 54790618Stmm * and is still up. If not, free it and try again. 54890618Stmm */ 54990618Stmm if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 55090618Stmm dst->sin6_family != AF_INET6 || 551152684Smarius !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) { 55290618Stmm RTFREE(ro->ro_rt); 55390618Stmm ro->ro_rt = (struct rtentry *)0; 55490618Stmm } 55590618Stmm if (ro->ro_rt == 0) { 55690618Stmm bzero(dst, sizeof(*dst)); 55790618Stmm dst->sin6_family = AF_INET6; 55890618Stmm dst->sin6_len = sizeof(struct sockaddr_in6); 55990618Stmm dst->sin6_addr = ip6->ip6_dst; 56090618Stmm } 561152684Smarius 56290618Stmm /* 56390618Stmm * if specified, try to fill in the traffic class field. 56490618Stmm * do not override if a non-zero value is already set. 56590618Stmm * we check the diffserv field and the ecn field separately. 56690618Stmm */ 56790618Stmm if (opt && opt->ip6po_tclass >= 0) { 56890618Stmm int mask = 0; 569152684Smarius 57090618Stmm if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0) 571152684Smarius mask |= 0xfc; 572152684Smarius if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0) 573152684Smarius mask |= 0x03; 574145186Smarius if (mask != 0) 575152684Smarius ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20); 57690618Stmm } 57790618Stmm 57890618Stmm /* fill in or override the hop limit field, if necessary. */ 57990618Stmm if (opt && opt->ip6po_hlim != -1) 58090618Stmm ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 581146391Smarius else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 58290618Stmm if (im6o != NULL) 58390618Stmm ip6->ip6_hlim = im6o->im6o_multicast_hlim; 584146391Smarius else 58590618Stmm ip6->ip6_hlim = ip6_defmcasthlim; 58690618Stmm } 58790618Stmm 58890618Stmm#if defined(IPSEC) || defined(FAST_IPSEC) 58990618Stmm if (needipsec && needipsectun) { 59090618Stmm struct ipsec_output_state state; 59190618Stmm 592145186Smarius /* 59390618Stmm * All the extension headers will become inaccessible 594146391Smarius * (since they can be encrypted). 595146391Smarius * Don't panic, we need no more updates to extension headers 596146391Smarius * on inner IPv6 packet (since they are now encapsulated). 59790618Stmm * 59890618Stmm * IPv6 [ESP|AH] IPv6 [extension headers] payload 59990618Stmm */ 60090618Stmm bzero(&exthdrs, sizeof(exthdrs)); 60190618Stmm exthdrs.ip6e_ip6 = m; 60290618Stmm 603146391Smarius bzero(&state, sizeof(state)); 60490618Stmm state.m = m; 60590618Stmm state.ro = (struct route *)ro; 60690618Stmm state.dst = (struct sockaddr *)dst; 60790618Stmm 60890618Stmm error = ipsec6_output_tunnel(&state, sp, flags); 60990618Stmm 61090618Stmm m = state.m; 61190618Stmm ro = (struct route_in6 *)state.ro; 61290618Stmm dst = (struct sockaddr_in6 *)state.dst; 61390618Stmm if (error) { 61490618Stmm /* mbuf is already reclaimed in ipsec6_output_tunnel. */ 615172066Smarius m0 = m = NULL; 616172066Smarius m = NULL; 617170387Spiso switch (error) { 618172066Smarius case EHOSTUNREACH: 619172066Smarius case ENETUNREACH: 620170387Spiso case EMSGSIZE: 621172066Smarius case ENOBUFS: 622172066Smarius case ENOMEM: 623170387Spiso break; 624178443Smarius default: 625172066Smarius printf("ip6_output (ipsec): error code %d\n", error); 626172066Smarius /* FALLTHROUGH */ 627172066Smarius case ENOENT: 628172066Smarius /* don't show these error codes to the user */ 629172066Smarius error = 0; 630170387Spiso break; 631172066Smarius } 632172066Smarius goto bad; 633172066Smarius } 634170387Spiso 635178443Smarius exthdrs.ip6e_ip6 = m; 63690618Stmm } 637172066Smarius#endif /* IPSEC */ 638172066Smarius 63990618Stmm if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 640178443Smarius /* Unicast */ 641178443Smarius 642178443Smarius#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 643178443Smarius#define sin6tosa(sin6) ((struct sockaddr *)(sin6)) 644178443Smarius /* xxx 645178443Smarius * interface selection comes here 646178443Smarius * if an interface is specified from an upper layer, 647178443Smarius * ifp must point it. 648178443Smarius */ 649178443Smarius if (ro->ro_rt == 0) { 650206018Smarius /* 65190618Stmm * non-bsdi always clone routes, if parent is 65290618Stmm * PRF_CLONING. 65390618Stmm */ 654172066Smarius rtalloc((struct route *)ro); 655172066Smarius } 65690618Stmm if (ro->ro_rt == 0) { 657172066Smarius ip6stat.ip6s_noroute++; 658172066Smarius error = EHOSTUNREACH; 65990618Stmm /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */ 660172066Smarius goto bad; 661172066Smarius } 662172066Smarius /* XXX rt not locked */ 663172066Smarius ia = ifatoia6(ro->ro_rt->rt_ifa); 664107474Stmm ifp = ro->ro_rt->rt_ifp; 66590618Stmm ro->ro_rt->rt_rmx.rmx_pksent++; 666172066Smarius if (ro->ro_rt->rt_flags & RTF_GATEWAY) 667172066Smarius dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway; 668172066Smarius m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */ 669172066Smarius 670172066Smarius in6_ifstat_inc(ifp, ifs6_out_request); 671172066Smarius 672172066Smarius /* 673172066Smarius * Check if the outgoing interface conflicts with 674172066Smarius * the interface specified by ifi6_ifindex (if specified). 675172066Smarius * Note that loopback interface is always okay. 676172066Smarius * (this may happen when we are sending a packet to one of 677172066Smarius * our own addresses.) 678172066Smarius */ 679172066Smarius if (opt && opt->ip6po_pktinfo 680172066Smarius && opt->ip6po_pktinfo->ipi6_ifindex) { 681172066Smarius if (!(ifp->if_flags & IFF_LOOPBACK) 68290618Stmm && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) { 683172066Smarius ip6stat.ip6s_noroute++; 684172066Smarius in6_ifstat_inc(ifp, ifs6_out_discard); 685172066Smarius error = EHOSTUNREACH; 686172066Smarius goto bad; 687172066Smarius } 68890618Stmm } 68990618Stmm 69090618Stmm if (opt && opt->ip6po_hlim != -1) 691172066Smarius ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 692172066Smarius } else { 69390618Stmm /* Multicast */ 694172066Smarius struct in6_multi *in6m; 695172066Smarius 69690618Stmm m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST; 697172066Smarius 69890618Stmm /* 699172066Smarius * See if the caller provided any multicast options 700172066Smarius */ 701225931Smarius ifp = NULL; 702172066Smarius if (im6o != NULL) { 703172066Smarius ip6->ip6_hlim = im6o->im6o_multicast_hlim; 704172066Smarius if (im6o->im6o_multicast_ifp != NULL) 705225931Smarius ifp = im6o->im6o_multicast_ifp; 706225931Smarius } else 707172066Smarius ip6->ip6_hlim = ip6_defmcasthlim; 708172066Smarius 70990618Stmm /* 71090618Stmm * See if the caller provided the outgoing interface 71190618Stmm * as an ancillary data. 71290618Stmm * Boundary check for ifindex is assumed to be already done. 71390618Stmm */ 71490618Stmm if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex) 71590618Stmm ifp = ifnet_byindex(opt->ip6po_pktinfo->ipi6_ifindex); 71690618Stmm 71790618Stmm /* 71890618Stmm * If the destination is a node-local scope multicast, 71990618Stmm * the packet should be loop-backed only. 720145186Smarius */ 72190618Stmm if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) { 72290618Stmm /* 723145186Smarius * If the outgoing interface is already specified, 724225931Smarius * it should be a loopback interface. 72590618Stmm */ 726145185Smarius if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) { 727145186Smarius ip6stat.ip6s_badscope++; 728145186Smarius error = ENETUNREACH; /* XXX: better error? */ 729145186Smarius /* XXX correct ifp? */ 730145186Smarius in6_ifstat_inc(ifp, ifs6_out_discard); 73190618Stmm goto bad; 73290618Stmm } else { 733145186Smarius ifp = &loif[0]; 734145186Smarius } 73590618Stmm } 736145186Smarius 737145186Smarius if (opt && opt->ip6po_hlim != -1) 738145186Smarius ip6->ip6_hlim = opt->ip6po_hlim & 0xff; 739145186Smarius 740145186Smarius /* 741145186Smarius * If caller did not provide an interface lookup a 742145186Smarius * default in the routing table. This is either a 743145186Smarius * default for the speicfied group (i.e. a host 744145186Smarius * route), or a multicast default (a route for the 745145186Smarius * ``net'' ff00::/8). 746145186Smarius */ 747145186Smarius if (ifp == NULL) { 74890618Stmm if (ro->ro_rt == 0) 749145186Smarius ro->ro_rt = rtalloc1((struct sockaddr *) 750145186Smarius &ro->ro_dst, 0, 0UL); 751172066Smarius else 752145186Smarius RT_LOCK(ro->ro_rt); 75390618Stmm if (ro->ro_rt == 0) { 754145186Smarius ip6stat.ip6s_noroute++; 75590618Stmm error = EHOSTUNREACH; 75690618Stmm /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */ 75790618Stmm goto bad; 75890618Stmm } 75990618Stmm ia = ifatoia6(ro->ro_rt->rt_ifa); 76090618Stmm ifp = ro->ro_rt->rt_ifp; 76190618Stmm ro->ro_rt->rt_rmx.rmx_pksent++; 76290618Stmm RT_UNLOCK(ro->ro_rt); 76390618Stmm } 76490618Stmm 765159413Smarius if ((flags & IPV6_FORWARDING) == 0) 76690618Stmm in6_ifstat_inc(ifp, ifs6_out_request); 767159413Smarius in6_ifstat_inc(ifp, ifs6_out_mcast); 76890618Stmm 769225931Smarius /* 770225931Smarius * Confirm that the outgoing interface supports multicast. 771225931Smarius */ 77290618Stmm if ((ifp->if_flags & IFF_MULTICAST) == 0) { 77390618Stmm ip6stat.ip6s_noroute++; 774157896Simp in6_ifstat_inc(ifp, ifs6_out_discard); 775225931Smarius error = ENETUNREACH; 776225931Smarius goto bad; 777225931Smarius } 778225931Smarius IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m); 779225931Smarius if (in6m != NULL && 78090618Stmm (im6o == NULL || im6o->im6o_multicast_loop)) { 781145186Smarius /* 782145186Smarius * If we belong to the destination multicast group 783145186Smarius * on the outgoing interface, and the caller did not 78490618Stmm * forbid loopback, loop back a copy. 78590618Stmm */ 78690618Stmm ip6_mloopback(ifp, m, dst); 78790618Stmm } else { 78890618Stmm /* 78990618Stmm * If we are acting as a multicast router, perform 79090618Stmm * multicast forwarding as if the packet had just 79190618Stmm * arrived on the interface to which we are about 79290618Stmm * to send. The multicast forwarding function 793225931Smarius * recursively calls this function, using the 794225931Smarius * IPV6_FORWARDING flag to prevent infinite recursion. 795225931Smarius * 79690618Stmm * Multicasts that are looped back by ip6_mloopback(), 797225931Smarius * above, will be forwarded by the ip6_input() routine, 798225931Smarius * if necessary. 799225931Smarius */ 800225931Smarius if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) { 801225931Smarius if (ip6_mforward(ip6, ifp, m) != 0) { 802225931Smarius m_freem(m); 803225931Smarius goto done; 804225931Smarius } 805225931Smarius } 806225931Smarius } 807225931Smarius /* 808225931Smarius * Multicasts with a hoplimit of zero may be looped back, 809225931Smarius * above, but must not be transmitted on a network. 810225931Smarius * Also, multicasts addressed to the loopback interface 811225931Smarius * are not sent -- the above call to ip6_mloopback() will 812225931Smarius * loop back a copy if this host actually belongs to the 813225931Smarius * destination group on the loopback interface. 814225931Smarius */ 815225931Smarius if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) || 816225931Smarius IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) { 817225931Smarius m_freem(m); 818225931Smarius goto done; 819225931Smarius } 820225931Smarius } 821108798Stmm 82290618Stmm /* 82390618Stmm * Fill the outgoing inteface to tell the upper layer 82490618Stmm * to increment per-interface statistics. 825225931Smarius */ 826225931Smarius if (ifpp) 82790618Stmm *ifpp = ifp; 828225931Smarius 829225931Smarius /* Determine path MTU. */ 83090618Stmm if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu, 831159413Smarius &alwaysfrag)) != 0) 832225931Smarius goto bad; 833225931Smarius 834225931Smarius /* 835225931Smarius * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting 836225931Smarius */ 837225931Smarius if ((flags & IPV6_MINMTU) != 0 && mtu > IPV6_MMTU) 838159413Smarius mtu = IPV6_MMTU; 839225931Smarius 84090618Stmm /* Fake scoped addresses */ 84190618Stmm if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 84290618Stmm /* 84390618Stmm * If source or destination address is a scoped address, and 84490618Stmm * the packet is going to be sent to a loopback interface, 84590618Stmm * we should keep the original interface. 846145186Smarius */ 847108798Stmm 848145186Smarius /* 84990618Stmm * XXX: this is a very experimental and temporary solution. 850145186Smarius * We eventually have sockaddr_in6 and use the sin6_scope_id 851145186Smarius * field of the structure here. 852225931Smarius * We rely on the consistency between two scope zone ids 853225931Smarius * of source and destination, which should already be assured. 854225931Smarius * Larger scopes than link will be supported in the future. 855225931Smarius */ 856225931Smarius origifp = NULL; 857225931Smarius if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 858225931Smarius origifp = ifnet_byindex(ntohs(ip6->ip6_src.s6_addr16[1])); 859108798Stmm else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 860108798Stmm origifp = ifnet_byindex(ntohs(ip6->ip6_dst.s6_addr16[1])); 861225931Smarius /* 862225931Smarius * XXX: origifp can be NULL even in those two cases above. 863225931Smarius * For example, if we remove the (only) link-local address 864225931Smarius * from the loopback interface, and try to send a link-local 865225931Smarius * address without link-id information. Then the source 866225931Smarius * address is ::1, and the destination address is the 867225931Smarius * link-local address with its s6_addr16[1] being zero. 868225931Smarius * What is worse, if the packet goes to the loopback interface 869225931Smarius * by a default rejected route, the null pointer would be 87090618Stmm * passed to looutput, and the kernel would hang. 871225931Smarius * The following last resort would prevent such disaster. 87290618Stmm */ 87390618Stmm if (origifp == NULL) 874167308Smarius origifp = ifp; 875167308Smarius } 876167308Smarius else 877167308Smarius origifp = ifp; 878167308Smarius /* 879167308Smarius * clear embedded scope identifiers if necessary. 880167308Smarius * in6_clearscope will touch the addresses only when necessary. 881167308Smarius */ 882167308Smarius in6_clearscope(&ip6->ip6_src); 883152684Smarius in6_clearscope(&ip6->ip6_dst); 884152684Smarius 885152684Smarius /* 886152684Smarius * Check with the firewall... 887152684Smarius */ 888152684Smarius if (ip6_fw_enable && ip6_fw_chk_ptr) { 889152684Smarius u_short port = 0; 890152684Smarius m->m_pkthdr.rcvif = NULL; /* XXX */ 891152684Smarius /* If ipfw says divert, we have to just drop packet */ 89290618Stmm if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) { 89390618Stmm m_freem(m); 89490618Stmm goto done; 89590618Stmm } 89690618Stmm if (!m) { 89790618Stmm error = EACCES; 89890618Stmm goto done; 89990618Stmm } 900247621Smarius } 901247601Smarius 90290618Stmm /* 903172066Smarius * If the outgoing packet contains a hop-by-hop options header, 90490618Stmm * it must be examined and processed even by the source node. 905172066Smarius * (RFC 2460, section 4.) 906172066Smarius */ 907247621Smarius if (exthdrs.ip6e_hbh) { 908172066Smarius struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *); 90990618Stmm u_int32_t dummy1; /* XXX unused */ 91090618Stmm u_int32_t dummy2; /* XXX unused */ 91190618Stmm 91290618Stmm#ifdef DIAGNOSTIC 91390618Stmm if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len) 914247621Smarius panic("ip6e_hbh is not continuous"); 915247601Smarius#endif 91690618Stmm /* 917172066Smarius * XXX: if we have to send an ICMPv6 error to the sender, 91890618Stmm * we need the M_LOOP flag since icmp6_error() expects 919172066Smarius * the IPv6 and the hop-by-hop options header are 920172066Smarius * continuous unless the flag is set. 921247621Smarius */ 922172066Smarius m->m_flags |= M_LOOP; 92390618Stmm m->m_pkthdr.rcvif = ifp; 924247621Smarius if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1), 92590618Stmm ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh), 92690618Stmm &dummy1, &dummy2) < 0) { 927152684Smarius /* m was already freed at this point */ 928152684Smarius error = EINVAL;/* better error? */ 929133589Smarius goto done; 930152684Smarius } 931133589Smarius m->m_flags &= ~M_LOOP; /* XXX */ 932152684Smarius m->m_pkthdr.rcvif = NULL; 933152684Smarius } 934152684Smarius 935152684Smarius#ifdef PFIL_HOOKS 936152684Smarius /* 937152684Smarius * Run through list of hooks for output packets. 938133589Smarius */ 939 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT); 940 if (error != 0 || m == NULL) 941 goto done; 942 ip6 = mtod(m, struct ip6_hdr *); 943#endif /* PFIL_HOOKS */ 944 945 /* 946 * Send the packet to the outgoing interface. 947 * If necessary, do IPv6 fragmentation before sending. 948 * 949 * the logic here is rather complex: 950 * 1: normal case (dontfrag == 0, alwaysfrag == 0) 951 * 1-a: send as is if tlen <= path mtu 952 * 1-b: fragment if tlen > path mtu 953 * 954 * 2: if user asks us not to fragment (dontfrag == 1) 955 * 2-a: send as is if tlen <= interface mtu 956 * 2-b: error if tlen > interface mtu 957 * 958 * 3: if we always need to attach fragment header (alwaysfrag == 1) 959 * always fragment 960 * 961 * 4: if dontfrag == 1 && alwaysfrag == 1 962 * error, as we cannot handle this conflicting request 963 */ 964 tlen = m->m_pkthdr.len; 965 966 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) 967 dontfrag = 1; 968 else 969 dontfrag = 0; 970 if (dontfrag && alwaysfrag) { /* case 4 */ 971 /* conflicting request - can't transmit */ 972 error = EMSGSIZE; 973 goto bad; 974 } 975 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */ 976 /* 977 * Even if the DONTFRAG option is specified, we cannot send the 978 * packet when the data length is larger than the MTU of the 979 * outgoing interface. 980 * Notify the error by sending IPV6_PATHMTU ancillary data as 981 * well as returning an error code (the latter is not described 982 * in the API spec.) 983 */ 984 u_int32_t mtu32; 985 struct ip6ctlparam ip6cp; 986 987 mtu32 = (u_int32_t)mtu; 988 bzero(&ip6cp, sizeof(ip6cp)); 989 ip6cp.ip6c_cmdarg = (void *)&mtu32; 990 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst, 991 (void *)&ip6cp); 992 993 error = EMSGSIZE; 994 goto bad; 995 } 996 997 /* 998 * transmit packet without fragmentation 999 */ 1000 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */ 1001 struct in6_ifaddr *ia6; 1002 1003 ip6 = mtod(m, struct ip6_hdr *); 1004 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src); 1005 if (ia6) { 1006 /* Record statistics for this interface address. */ 1007 ia6->ia_ifa.if_opackets++; 1008 ia6->ia_ifa.if_obytes += m->m_pkthdr.len; 1009 } 1010#ifdef IPSEC 1011 /* clean ipsec history once it goes out of the node */ 1012 ipsec_delaux(m); 1013#endif 1014 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 1015 goto done; 1016 } 1017 1018 /* 1019 * try to fragment the packet. case 1-b and 3 1020 */ 1021 if (mtu < IPV6_MMTU) { 1022 /* path MTU cannot be less than IPV6_MMTU */ 1023 error = EMSGSIZE; 1024 in6_ifstat_inc(ifp, ifs6_out_fragfail); 1025 goto bad; 1026 } else if (ip6->ip6_plen == 0) { 1027 /* jumbo payload cannot be fragmented */ 1028 error = EMSGSIZE; 1029 in6_ifstat_inc(ifp, ifs6_out_fragfail); 1030 goto bad; 1031 } else { 1032 struct mbuf **mnext, *m_frgpart; 1033 struct ip6_frag *ip6f; 1034#ifdef RANDOM_IP_ID 1035 u_int32_t id = htonl(ip6_randomid()); 1036#else 1037 u_int32_t id = htonl(ip6_id++); 1038#endif 1039 u_char nextproto; 1040 1041 /* 1042 * Too large for the destination or interface; 1043 * fragment if possible. 1044 * Must be able to put at least 8 bytes per fragment. 1045 */ 1046 hlen = unfragpartlen; 1047 if (mtu > IPV6_MAXPACKET) 1048 mtu = IPV6_MAXPACKET; 1049 1050 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7; 1051 if (len < 8) { 1052 error = EMSGSIZE; 1053 in6_ifstat_inc(ifp, ifs6_out_fragfail); 1054 goto bad; 1055 } 1056 1057 mnext = &m->m_nextpkt; 1058 1059 /* 1060 * Change the next header field of the last header in the 1061 * unfragmentable part. 1062 */ 1063 if (exthdrs.ip6e_rthdr) { 1064 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *); 1065 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT; 1066 } else if (exthdrs.ip6e_dest1) { 1067 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *); 1068 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT; 1069 } else if (exthdrs.ip6e_hbh) { 1070 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *); 1071 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT; 1072 } else { 1073 nextproto = ip6->ip6_nxt; 1074 ip6->ip6_nxt = IPPROTO_FRAGMENT; 1075 } 1076 1077 /* 1078 * Loop through length of segment after first fragment, 1079 * make new header and copy data of each part and link onto 1080 * chain. 1081 */ 1082 m0 = m; 1083 for (off = hlen; off < tlen; off += len) { 1084 MGETHDR(m, M_DONTWAIT, MT_HEADER); 1085 if (!m) { 1086 error = ENOBUFS; 1087 ip6stat.ip6s_odropped++; 1088 goto sendorfree; 1089 } 1090 m->m_pkthdr.rcvif = NULL; 1091 m->m_flags = m0->m_flags & M_COPYFLAGS; 1092 *mnext = m; 1093 mnext = &m->m_nextpkt; 1094 m->m_data += max_linkhdr; 1095 mhip6 = mtod(m, struct ip6_hdr *); 1096 *mhip6 = *ip6; 1097 m->m_len = sizeof(*mhip6); 1098 error = ip6_insertfraghdr(m0, m, hlen, &ip6f); 1099 if (error) { 1100 ip6stat.ip6s_odropped++; 1101 goto sendorfree; 1102 } 1103 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7)); 1104 if (off + len >= tlen) 1105 len = tlen - off; 1106 else 1107 ip6f->ip6f_offlg |= IP6F_MORE_FRAG; 1108 mhip6->ip6_plen = htons((u_short)(len + hlen + 1109 sizeof(*ip6f) - sizeof(struct ip6_hdr))); 1110 if ((m_frgpart = m_copy(m0, off, len)) == 0) { 1111 error = ENOBUFS; 1112 ip6stat.ip6s_odropped++; 1113 goto sendorfree; 1114 } 1115 m_cat(m, m_frgpart); 1116 m->m_pkthdr.len = len + hlen + sizeof(*ip6f); 1117 m->m_pkthdr.rcvif = (struct ifnet *)0; 1118 ip6f->ip6f_reserved = 0; 1119 ip6f->ip6f_ident = id; 1120 ip6f->ip6f_nxt = nextproto; 1121 ip6stat.ip6s_ofragments++; 1122 in6_ifstat_inc(ifp, ifs6_out_fragcreat); 1123 } 1124 1125 in6_ifstat_inc(ifp, ifs6_out_fragok); 1126 } 1127 1128 /* 1129 * Remove leading garbages. 1130 */ 1131sendorfree: 1132 m = m0->m_nextpkt; 1133 m0->m_nextpkt = 0; 1134 m_freem(m0); 1135 for (m0 = m; m; m = m0) { 1136 m0 = m->m_nextpkt; 1137 m->m_nextpkt = 0; 1138 if (error == 0) { 1139 /* Record statistics for this interface address. */ 1140 if (ia) { 1141 ia->ia_ifa.if_opackets++; 1142 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 1143 } 1144#ifdef IPSEC 1145 /* clean ipsec history once it goes out of the node */ 1146 ipsec_delaux(m); 1147#endif 1148 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt); 1149 } else 1150 m_freem(m); 1151 } 1152 1153 if (error == 0) 1154 ip6stat.ip6s_fragmented++; 1155 1156done: 1157 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */ 1158 RTFREE(ro->ro_rt); 1159 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) { 1160 RTFREE(ro_pmtu->ro_rt); 1161 } 1162 1163#ifdef IPSEC 1164 if (sp != NULL) 1165 key_freesp(sp); 1166#endif /* IPSEC */ 1167#ifdef FAST_IPSEC 1168 if (sp != NULL) 1169 KEY_FREESP(&sp); 1170#endif /* FAST_IPSEC */ 1171 1172 return (error); 1173 1174freehdrs: 1175 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */ 1176 m_freem(exthdrs.ip6e_dest1); 1177 m_freem(exthdrs.ip6e_rthdr); 1178 m_freem(exthdrs.ip6e_dest2); 1179 /* FALLTHROUGH */ 1180bad: 1181 m_freem(m); 1182 goto done; 1183} 1184 1185static int 1186ip6_copyexthdr(mp, hdr, hlen) 1187 struct mbuf **mp; 1188 caddr_t hdr; 1189 int hlen; 1190{ 1191 struct mbuf *m; 1192 1193 if (hlen > MCLBYTES) 1194 return (ENOBUFS); /* XXX */ 1195 1196 MGET(m, M_DONTWAIT, MT_DATA); 1197 if (!m) 1198 return (ENOBUFS); 1199 1200 if (hlen > MLEN) { 1201 MCLGET(m, M_DONTWAIT); 1202 if ((m->m_flags & M_EXT) == 0) { 1203 m_free(m); 1204 return (ENOBUFS); 1205 } 1206 } 1207 m->m_len = hlen; 1208 if (hdr) 1209 bcopy(hdr, mtod(m, caddr_t), hlen); 1210 1211 *mp = m; 1212 return (0); 1213} 1214 1215/* 1216 * Insert jumbo payload option. 1217 */ 1218static int 1219ip6_insert_jumboopt(exthdrs, plen) 1220 struct ip6_exthdrs *exthdrs; 1221 u_int32_t plen; 1222{ 1223 struct mbuf *mopt; 1224 u_char *optbuf; 1225 u_int32_t v; 1226 1227#define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */ 1228 1229 /* 1230 * If there is no hop-by-hop options header, allocate new one. 1231 * If there is one but it doesn't have enough space to store the 1232 * jumbo payload option, allocate a cluster to store the whole options. 1233 * Otherwise, use it to store the options. 1234 */ 1235 if (exthdrs->ip6e_hbh == 0) { 1236 MGET(mopt, M_DONTWAIT, MT_DATA); 1237 if (mopt == 0) 1238 return (ENOBUFS); 1239 mopt->m_len = JUMBOOPTLEN; 1240 optbuf = mtod(mopt, u_char *); 1241 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */ 1242 exthdrs->ip6e_hbh = mopt; 1243 } else { 1244 struct ip6_hbh *hbh; 1245 1246 mopt = exthdrs->ip6e_hbh; 1247 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) { 1248 /* 1249 * XXX assumption: 1250 * - exthdrs->ip6e_hbh is not referenced from places 1251 * other than exthdrs. 1252 * - exthdrs->ip6e_hbh is not an mbuf chain. 1253 */ 1254 int oldoptlen = mopt->m_len; 1255 struct mbuf *n; 1256 1257 /* 1258 * XXX: give up if the whole (new) hbh header does 1259 * not fit even in an mbuf cluster. 1260 */ 1261 if (oldoptlen + JUMBOOPTLEN > MCLBYTES) 1262 return (ENOBUFS); 1263 1264 /* 1265 * As a consequence, we must always prepare a cluster 1266 * at this point. 1267 */ 1268 MGET(n, M_DONTWAIT, MT_DATA); 1269 if (n) { 1270 MCLGET(n, M_DONTWAIT); 1271 if ((n->m_flags & M_EXT) == 0) { 1272 m_freem(n); 1273 n = NULL; 1274 } 1275 } 1276 if (!n) 1277 return (ENOBUFS); 1278 n->m_len = oldoptlen + JUMBOOPTLEN; 1279 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), 1280 oldoptlen); 1281 optbuf = mtod(n, caddr_t) + oldoptlen; 1282 m_freem(mopt); 1283 mopt = exthdrs->ip6e_hbh = n; 1284 } else { 1285 optbuf = mtod(mopt, u_char *) + mopt->m_len; 1286 mopt->m_len += JUMBOOPTLEN; 1287 } 1288 optbuf[0] = IP6OPT_PADN; 1289 optbuf[1] = 1; 1290 1291 /* 1292 * Adjust the header length according to the pad and 1293 * the jumbo payload option. 1294 */ 1295 hbh = mtod(mopt, struct ip6_hbh *); 1296 hbh->ip6h_len += (JUMBOOPTLEN >> 3); 1297 } 1298 1299 /* fill in the option. */ 1300 optbuf[2] = IP6OPT_JUMBO; 1301 optbuf[3] = 4; 1302 v = (u_int32_t)htonl(plen + JUMBOOPTLEN); 1303 bcopy(&v, &optbuf[4], sizeof(u_int32_t)); 1304 1305 /* finally, adjust the packet header length */ 1306 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN; 1307 1308 return (0); 1309#undef JUMBOOPTLEN 1310} 1311 1312/* 1313 * Insert fragment header and copy unfragmentable header portions. 1314 */ 1315static int 1316ip6_insertfraghdr(m0, m, hlen, frghdrp) 1317 struct mbuf *m0, *m; 1318 int hlen; 1319 struct ip6_frag **frghdrp; 1320{ 1321 struct mbuf *n, *mlast; 1322 1323 if (hlen > sizeof(struct ip6_hdr)) { 1324 n = m_copym(m0, sizeof(struct ip6_hdr), 1325 hlen - sizeof(struct ip6_hdr), M_DONTWAIT); 1326 if (n == 0) 1327 return (ENOBUFS); 1328 m->m_next = n; 1329 } else 1330 n = m; 1331 1332 /* Search for the last mbuf of unfragmentable part. */ 1333 for (mlast = n; mlast->m_next; mlast = mlast->m_next) 1334 ; 1335 1336 if ((mlast->m_flags & M_EXT) == 0 && 1337 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) { 1338 /* use the trailing space of the last mbuf for the fragment hdr */ 1339 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) + 1340 mlast->m_len); 1341 mlast->m_len += sizeof(struct ip6_frag); 1342 m->m_pkthdr.len += sizeof(struct ip6_frag); 1343 } else { 1344 /* allocate a new mbuf for the fragment header */ 1345 struct mbuf *mfrg; 1346 1347 MGET(mfrg, M_DONTWAIT, MT_DATA); 1348 if (mfrg == 0) 1349 return (ENOBUFS); 1350 mfrg->m_len = sizeof(struct ip6_frag); 1351 *frghdrp = mtod(mfrg, struct ip6_frag *); 1352 mlast->m_next = mfrg; 1353 } 1354 1355 return (0); 1356} 1357 1358static int 1359ip6_getpmtu(ro_pmtu, ro, ifp, dst, mtup, alwaysfragp) 1360 struct route_in6 *ro_pmtu, *ro; 1361 struct ifnet *ifp; 1362 struct in6_addr *dst; 1363 u_long *mtup; 1364 int *alwaysfragp; 1365{ 1366 u_int32_t mtu = 0; 1367 int alwaysfrag = 0; 1368 int error = 0; 1369 1370 if (ro_pmtu != ro) { 1371 /* The first hop and the final destination may differ. */ 1372 struct sockaddr_in6 *sa6_dst = 1373 (struct sockaddr_in6 *)&ro_pmtu->ro_dst; 1374 if (ro_pmtu->ro_rt && 1375 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 || 1376 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) { 1377 RTFREE(ro_pmtu->ro_rt); 1378 ro_pmtu->ro_rt = (struct rtentry *)NULL; 1379 } 1380 if (ro_pmtu->ro_rt == NULL) { 1381 bzero(sa6_dst, sizeof(*sa6_dst)); 1382 sa6_dst->sin6_family = AF_INET6; 1383 sa6_dst->sin6_len = sizeof(struct sockaddr_in6); 1384 sa6_dst->sin6_addr = *dst; 1385 1386 rtalloc((struct route *)ro_pmtu); 1387 } 1388 } 1389 if (ro_pmtu->ro_rt) { 1390 u_int32_t ifmtu; 1391 struct in_conninfo inc; 1392 1393 bzero(&inc, sizeof(inc)); 1394 inc.inc_flags = 1; /* IPv6 */ 1395 inc.inc6_faddr = *dst; 1396 1397 if (ifp == NULL) 1398 ifp = ro_pmtu->ro_rt->rt_ifp; 1399 ifmtu = IN6_LINKMTU(ifp); 1400 mtu = tcp_hc_getmtu(&inc); 1401 if (mtu) 1402 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu); 1403 else 1404 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu; 1405 if (mtu == 0) 1406 mtu = ifmtu; 1407 else if (mtu < IPV6_MMTU) { 1408 /* 1409 * RFC2460 section 5, last paragraph: 1410 * if we record ICMPv6 too big message with 1411 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU 1412 * or smaller, with framgent header attached. 1413 * (fragment header is needed regardless from the 1414 * packet size, for translators to identify packets) 1415 */ 1416 alwaysfrag = 1; 1417 mtu = IPV6_MMTU; 1418 } else if (mtu > ifmtu) { 1419 /* 1420 * The MTU on the route is larger than the MTU on 1421 * the interface! This shouldn't happen, unless the 1422 * MTU of the interface has been changed after the 1423 * interface was brought up. Change the MTU in the 1424 * route to match the interface MTU (as long as the 1425 * field isn't locked). 1426 */ 1427 mtu = ifmtu; 1428 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; 1429 } 1430 } else if (ifp) { 1431 mtu = IN6_LINKMTU(ifp); 1432 } else 1433 error = EHOSTUNREACH; /* XXX */ 1434 1435 *mtup = mtu; 1436 if (alwaysfragp) 1437 *alwaysfragp = alwaysfrag; 1438 return (error); 1439} 1440 1441/* 1442 * IP6 socket option processing. 1443 */ 1444int 1445ip6_ctloutput(so, sopt) 1446 struct socket *so; 1447 struct sockopt *sopt; 1448{ 1449 int privileged, optdatalen, uproto; 1450 void *optdata; 1451 struct inpcb *in6p = sotoinpcb(so); 1452 int error, optval; 1453 int level, op, optname; 1454 int optlen; 1455 struct thread *td; 1456 1457 if (sopt) { 1458 level = sopt->sopt_level; 1459 op = sopt->sopt_dir; 1460 optname = sopt->sopt_name; 1461 optlen = sopt->sopt_valsize; 1462 td = sopt->sopt_td; 1463 } else { 1464 panic("ip6_ctloutput: arg soopt is NULL"); 1465 } 1466 error = optval = 0; 1467 1468 privileged = (td == 0 || suser(td)) ? 0 : 1; 1469 uproto = (int)so->so_proto->pr_protocol; 1470 1471 if (level == IPPROTO_IPV6) { 1472 switch (op) { 1473 1474 case SOPT_SET: 1475 switch (optname) { 1476 case IPV6_2292PKTOPTIONS: 1477#ifdef IPV6_PKTOPTIONS 1478 case IPV6_PKTOPTIONS: 1479#endif 1480 { 1481 struct mbuf *m; 1482 1483 error = soopt_getm(sopt, &m); /* XXX */ 1484 if (error != NULL) 1485 break; 1486 error = soopt_mcopyin(sopt, m); /* XXX */ 1487 if (error != NULL) 1488 break; 1489 error = ip6_pcbopts(&in6p->in6p_outputopts, 1490 m, so, sopt); 1491 m_freem(m); /* XXX */ 1492 break; 1493 } 1494 1495 /* 1496 * Use of some Hop-by-Hop options or some 1497 * Destination options, might require special 1498 * privilege. That is, normal applications 1499 * (without special privilege) might be forbidden 1500 * from setting certain options in outgoing packets, 1501 * and might never see certain options in received 1502 * packets. [RFC 2292 Section 6] 1503 * KAME specific note: 1504 * KAME prevents non-privileged users from sending or 1505 * receiving ANY hbh/dst options in order to avoid 1506 * overhead of parsing options in the kernel. 1507 */ 1508 case IPV6_RECVHOPOPTS: 1509 case IPV6_RECVDSTOPTS: 1510 case IPV6_RECVRTHDRDSTOPTS: 1511 if (!privileged) { 1512 error = EPERM; 1513 break; 1514 } 1515 /* FALLTHROUGH */ 1516 case IPV6_UNICAST_HOPS: 1517 case IPV6_HOPLIMIT: 1518 case IPV6_FAITH: 1519 1520 case IPV6_RECVPKTINFO: 1521 case IPV6_RECVHOPLIMIT: 1522 case IPV6_RECVRTHDR: 1523 case IPV6_RECVPATHMTU: 1524 case IPV6_RECVTCLASS: 1525 case IPV6_V6ONLY: 1526 case IPV6_AUTOFLOWLABEL: 1527 if (optlen != sizeof(int)) { 1528 error = EINVAL; 1529 break; 1530 } 1531 error = sooptcopyin(sopt, &optval, 1532 sizeof optval, sizeof optval); 1533 if (error) 1534 break; 1535 switch (optname) { 1536 1537 case IPV6_UNICAST_HOPS: 1538 if (optval < -1 || optval >= 256) 1539 error = EINVAL; 1540 else { 1541 /* -1 = kernel default */ 1542 in6p->in6p_hops = optval; 1543 if ((in6p->in6p_vflag & 1544 INP_IPV4) != 0) 1545 in6p->inp_ip_ttl = optval; 1546 } 1547 break; 1548#define OPTSET(bit) \ 1549do { \ 1550 if (optval) \ 1551 in6p->in6p_flags |= (bit); \ 1552 else \ 1553 in6p->in6p_flags &= ~(bit); \ 1554} while (/*CONSTCOND*/ 0) 1555#define OPTSET2292(bit) \ 1556do { \ 1557 in6p->in6p_flags |= IN6P_RFC2292; \ 1558 if (optval) \ 1559 in6p->in6p_flags |= (bit); \ 1560 else \ 1561 in6p->in6p_flags &= ~(bit); \ 1562} while (/*CONSTCOND*/ 0) 1563#define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0) 1564 1565 case IPV6_RECVPKTINFO: 1566 /* cannot mix with RFC2292 */ 1567 if (OPTBIT(IN6P_RFC2292)) { 1568 error = EINVAL; 1569 break; 1570 } 1571 OPTSET(IN6P_PKTINFO); 1572 break; 1573 1574 case IPV6_HOPLIMIT: 1575 { 1576 struct ip6_pktopts **optp; 1577 1578 /* cannot mix with RFC2292 */ 1579 if (OPTBIT(IN6P_RFC2292)) { 1580 error = EINVAL; 1581 break; 1582 } 1583 optp = &in6p->in6p_outputopts; 1584 error = ip6_pcbopt(IPV6_HOPLIMIT, 1585 (u_char *)&optval, 1586 sizeof(optval), 1587 optp, 1588 privileged, uproto); 1589 break; 1590 } 1591 1592 case IPV6_RECVHOPLIMIT: 1593 /* cannot mix with RFC2292 */ 1594 if (OPTBIT(IN6P_RFC2292)) { 1595 error = EINVAL; 1596 break; 1597 } 1598 OPTSET(IN6P_HOPLIMIT); 1599 break; 1600 1601 case IPV6_RECVHOPOPTS: 1602 /* cannot mix with RFC2292 */ 1603 if (OPTBIT(IN6P_RFC2292)) { 1604 error = EINVAL; 1605 break; 1606 } 1607 OPTSET(IN6P_HOPOPTS); 1608 break; 1609 1610 case IPV6_RECVDSTOPTS: 1611 /* cannot mix with RFC2292 */ 1612 if (OPTBIT(IN6P_RFC2292)) { 1613 error = EINVAL; 1614 break; 1615 } 1616 OPTSET(IN6P_DSTOPTS); 1617 break; 1618 1619 case IPV6_RECVRTHDRDSTOPTS: 1620 /* cannot mix with RFC2292 */ 1621 if (OPTBIT(IN6P_RFC2292)) { 1622 error = EINVAL; 1623 break; 1624 } 1625 OPTSET(IN6P_RTHDRDSTOPTS); 1626 break; 1627 1628 case IPV6_RECVRTHDR: 1629 /* cannot mix with RFC2292 */ 1630 if (OPTBIT(IN6P_RFC2292)) { 1631 error = EINVAL; 1632 break; 1633 } 1634 OPTSET(IN6P_RTHDR); 1635 break; 1636 1637 case IPV6_FAITH: 1638 OPTSET(IN6P_FAITH); 1639 break; 1640 1641 case IPV6_RECVPATHMTU: 1642 /* 1643 * We ignore this option for TCP 1644 * sockets. 1645 * (rfc2292bis leaves this case 1646 * unspecified.) 1647 */ 1648 if (uproto != IPPROTO_TCP) 1649 OPTSET(IN6P_MTU); 1650 break; 1651 1652 case IPV6_V6ONLY: 1653 /* 1654 * make setsockopt(IPV6_V6ONLY) 1655 * available only prior to bind(2). 1656 * see ipng mailing list, Jun 22 2001. 1657 */ 1658 if (in6p->in6p_lport || 1659 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 1660 error = EINVAL; 1661 break; 1662 } 1663 OPTSET(IN6P_IPV6_V6ONLY); 1664 if (optval) 1665 in6p->in6p_vflag &= ~INP_IPV4; 1666 else 1667 in6p->in6p_vflag |= INP_IPV4; 1668 break; 1669 case IPV6_RECVTCLASS: 1670 /* cannot mix with RFC2292 XXX */ 1671 if (OPTBIT(IN6P_RFC2292)) { 1672 error = EINVAL; 1673 break; 1674 } 1675 OPTSET(IN6P_TCLASS); 1676 break; 1677 case IPV6_AUTOFLOWLABEL: 1678 OPTSET(IN6P_AUTOFLOWLABEL); 1679 break; 1680 1681 } 1682 break; 1683 1684 case IPV6_TCLASS: 1685 case IPV6_DONTFRAG: 1686 case IPV6_USE_MIN_MTU: 1687 case IPV6_PREFER_TEMPADDR: 1688 if (optlen != sizeof(optval)) { 1689 error = EINVAL; 1690 break; 1691 } 1692 error = sooptcopyin(sopt, &optval, 1693 sizeof optval, sizeof optval); 1694 if (error) 1695 break; 1696 { 1697 struct ip6_pktopts **optp; 1698 optp = &in6p->in6p_outputopts; 1699 error = ip6_pcbopt(optname, 1700 (u_char *)&optval, 1701 sizeof(optval), 1702 optp, 1703 privileged, uproto); 1704 break; 1705 } 1706 1707 case IPV6_2292PKTINFO: 1708 case IPV6_2292HOPLIMIT: 1709 case IPV6_2292HOPOPTS: 1710 case IPV6_2292DSTOPTS: 1711 case IPV6_2292RTHDR: 1712 /* RFC 2292 */ 1713 if (optlen != sizeof(int)) { 1714 error = EINVAL; 1715 break; 1716 } 1717 error = sooptcopyin(sopt, &optval, 1718 sizeof optval, sizeof optval); 1719 if (error) 1720 break; 1721 switch (optname) { 1722 case IPV6_2292PKTINFO: 1723 OPTSET2292(IN6P_PKTINFO); 1724 break; 1725 case IPV6_2292HOPLIMIT: 1726 OPTSET2292(IN6P_HOPLIMIT); 1727 break; 1728 case IPV6_2292HOPOPTS: 1729 /* 1730 * Check super-user privilege. 1731 * See comments for IPV6_RECVHOPOPTS. 1732 */ 1733 if (!privileged) 1734 return (EPERM); 1735 OPTSET2292(IN6P_HOPOPTS); 1736 break; 1737 case IPV6_2292DSTOPTS: 1738 if (!privileged) 1739 return (EPERM); 1740 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */ 1741 break; 1742 case IPV6_2292RTHDR: 1743 OPTSET2292(IN6P_RTHDR); 1744 break; 1745 } 1746 break; 1747 case IPV6_PKTINFO: 1748 case IPV6_HOPOPTS: 1749 case IPV6_RTHDR: 1750 case IPV6_DSTOPTS: 1751 case IPV6_RTHDRDSTOPTS: 1752 case IPV6_NEXTHOP: 1753 { 1754 /* new advanced API (2292bis) */ 1755 u_char *optbuf; 1756 int optlen; 1757 struct ip6_pktopts **optp; 1758 1759 /* cannot mix with RFC2292 */ 1760 if (OPTBIT(IN6P_RFC2292)) { 1761 error = EINVAL; 1762 break; 1763 } 1764 1765 optbuf = sopt->sopt_val; 1766 optlen = sopt->sopt_valsize; 1767 optp = &in6p->in6p_outputopts; 1768 error = ip6_pcbopt(optname, 1769 optbuf, optlen, 1770 optp, privileged, uproto); 1771 break; 1772 } 1773#undef OPTSET 1774 1775 case IPV6_MULTICAST_IF: 1776 case IPV6_MULTICAST_HOPS: 1777 case IPV6_MULTICAST_LOOP: 1778 case IPV6_JOIN_GROUP: 1779 case IPV6_LEAVE_GROUP: 1780 { 1781 if (sopt->sopt_valsize > MLEN) { 1782 error = EMSGSIZE; 1783 break; 1784 } 1785 /* XXX */ 1786 } 1787 /* FALLTHROUGH */ 1788 { 1789 struct mbuf *m; 1790 1791 if (sopt->sopt_valsize > MCLBYTES) { 1792 error = EMSGSIZE; 1793 break; 1794 } 1795 /* XXX */ 1796 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_HEADER); 1797 if (m == 0) { 1798 error = ENOBUFS; 1799 break; 1800 } 1801 if (sopt->sopt_valsize > MLEN) { 1802 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT); 1803 if ((m->m_flags & M_EXT) == 0) { 1804 m_free(m); 1805 error = ENOBUFS; 1806 break; 1807 } 1808 } 1809 m->m_len = sopt->sopt_valsize; 1810 error = sooptcopyin(sopt, mtod(m, char *), 1811 m->m_len, m->m_len); 1812 if (error) { 1813 (void)m_free(m); 1814 break; 1815 } 1816 error = ip6_setmoptions(sopt->sopt_name, 1817 &in6p->in6p_moptions, 1818 m); 1819 (void)m_free(m); 1820 } 1821 break; 1822 1823 case IPV6_PORTRANGE: 1824 error = sooptcopyin(sopt, &optval, 1825 sizeof optval, sizeof optval); 1826 if (error) 1827 break; 1828 1829 switch (optval) { 1830 case IPV6_PORTRANGE_DEFAULT: 1831 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1832 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1833 break; 1834 1835 case IPV6_PORTRANGE_HIGH: 1836 in6p->in6p_flags &= ~(IN6P_LOWPORT); 1837 in6p->in6p_flags |= IN6P_HIGHPORT; 1838 break; 1839 1840 case IPV6_PORTRANGE_LOW: 1841 in6p->in6p_flags &= ~(IN6P_HIGHPORT); 1842 in6p->in6p_flags |= IN6P_LOWPORT; 1843 break; 1844 1845 default: 1846 error = EINVAL; 1847 break; 1848 } 1849 break; 1850 1851#if defined(IPSEC) || defined(FAST_IPSEC) 1852 case IPV6_IPSEC_POLICY: 1853 { 1854 caddr_t req = NULL; 1855 size_t len = 0; 1856 struct mbuf *m; 1857 1858 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1859 break; 1860 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1861 break; 1862 if (m) { 1863 req = mtod(m, caddr_t); 1864 len = m->m_len; 1865 } 1866 error = ipsec6_set_policy(in6p, optname, req, 1867 len, privileged); 1868 m_freem(m); 1869 } 1870 break; 1871#endif /* KAME IPSEC */ 1872 1873 case IPV6_FW_ADD: 1874 case IPV6_FW_DEL: 1875 case IPV6_FW_FLUSH: 1876 case IPV6_FW_ZERO: 1877 { 1878 struct mbuf *m; 1879 struct mbuf **mp = &m; 1880 1881 if (ip6_fw_ctl_ptr == NULL) 1882 return EINVAL; 1883 /* XXX */ 1884 if ((error = soopt_getm(sopt, &m)) != 0) 1885 break; 1886 /* XXX */ 1887 if ((error = soopt_mcopyin(sopt, m)) != 0) 1888 break; 1889 error = (*ip6_fw_ctl_ptr)(optname, mp); 1890 m = *mp; 1891 } 1892 break; 1893 1894 default: 1895 error = ENOPROTOOPT; 1896 break; 1897 } 1898 break; 1899 1900 case SOPT_GET: 1901 switch (optname) { 1902 1903 case IPV6_2292PKTOPTIONS: 1904#ifdef IPV6_PKTOPTIONS 1905 case IPV6_PKTOPTIONS: 1906#endif 1907 /* 1908 * RFC3542 (effectively) deprecated the 1909 * semantics of the 2292-style pktoptions. 1910 * Since it was not reliable in nature (i.e., 1911 * applications had to expect the lack of some 1912 * information after all), it would make sense 1913 * to simplify this part by always returning 1914 * empty data. 1915 */ 1916 sopt->sopt_valsize = 0; 1917 break; 1918 1919 case IPV6_RECVHOPOPTS: 1920 case IPV6_RECVDSTOPTS: 1921 case IPV6_RECVRTHDRDSTOPTS: 1922 case IPV6_UNICAST_HOPS: 1923 case IPV6_RECVPKTINFO: 1924 case IPV6_RECVHOPLIMIT: 1925 case IPV6_RECVRTHDR: 1926 case IPV6_RECVPATHMTU: 1927 1928 case IPV6_FAITH: 1929 case IPV6_V6ONLY: 1930 case IPV6_PORTRANGE: 1931 case IPV6_RECVTCLASS: 1932 case IPV6_AUTOFLOWLABEL: 1933 switch (optname) { 1934 1935 case IPV6_RECVHOPOPTS: 1936 optval = OPTBIT(IN6P_HOPOPTS); 1937 break; 1938 1939 case IPV6_RECVDSTOPTS: 1940 optval = OPTBIT(IN6P_DSTOPTS); 1941 break; 1942 1943 case IPV6_RECVRTHDRDSTOPTS: 1944 optval = OPTBIT(IN6P_RTHDRDSTOPTS); 1945 break; 1946 1947 case IPV6_UNICAST_HOPS: 1948 optval = in6p->in6p_hops; 1949 break; 1950 1951 case IPV6_RECVPKTINFO: 1952 optval = OPTBIT(IN6P_PKTINFO); 1953 break; 1954 1955 case IPV6_RECVHOPLIMIT: 1956 optval = OPTBIT(IN6P_HOPLIMIT); 1957 break; 1958 1959 case IPV6_RECVRTHDR: 1960 optval = OPTBIT(IN6P_RTHDR); 1961 break; 1962 1963 case IPV6_RECVPATHMTU: 1964 optval = OPTBIT(IN6P_MTU); 1965 break; 1966 1967 case IPV6_FAITH: 1968 optval = OPTBIT(IN6P_FAITH); 1969 break; 1970 1971 case IPV6_V6ONLY: 1972 optval = OPTBIT(IN6P_IPV6_V6ONLY); 1973 break; 1974 1975 case IPV6_PORTRANGE: 1976 { 1977 int flags; 1978 flags = in6p->in6p_flags; 1979 if (flags & IN6P_HIGHPORT) 1980 optval = IPV6_PORTRANGE_HIGH; 1981 else if (flags & IN6P_LOWPORT) 1982 optval = IPV6_PORTRANGE_LOW; 1983 else 1984 optval = 0; 1985 break; 1986 } 1987 case IPV6_RECVTCLASS: 1988 optval = OPTBIT(IN6P_TCLASS); 1989 break; 1990 1991 case IPV6_AUTOFLOWLABEL: 1992 optval = OPTBIT(IN6P_AUTOFLOWLABEL); 1993 break; 1994 } 1995 if (error) 1996 break; 1997 error = sooptcopyout(sopt, &optval, 1998 sizeof optval); 1999 break; 2000 2001 case IPV6_PATHMTU: 2002 { 2003 u_long pmtu = 0; 2004 struct ip6_mtuinfo mtuinfo; 2005 struct route_in6 sro; 2006 2007 bzero(&sro, sizeof(sro)); 2008 2009 if (!(so->so_state & SS_ISCONNECTED)) 2010 return (ENOTCONN); 2011 /* 2012 * XXX: we dot not consider the case of source 2013 * routing, or optional information to specify 2014 * the outgoing interface. 2015 */ 2016 error = ip6_getpmtu(&sro, NULL, NULL, 2017 &in6p->in6p_faddr, &pmtu, NULL); 2018 if (sro.ro_rt) 2019 RTFREE(sro.ro_rt); 2020 if (error) 2021 break; 2022 if (pmtu > IPV6_MAXPACKET) 2023 pmtu = IPV6_MAXPACKET; 2024 2025 bzero(&mtuinfo, sizeof(mtuinfo)); 2026 mtuinfo.ip6m_mtu = (u_int32_t)pmtu; 2027 optdata = (void *)&mtuinfo; 2028 optdatalen = sizeof(mtuinfo); 2029 error = sooptcopyout(sopt, optdata, 2030 optdatalen); 2031 break; 2032 } 2033 2034 case IPV6_2292PKTINFO: 2035 case IPV6_2292HOPLIMIT: 2036 case IPV6_2292HOPOPTS: 2037 case IPV6_2292RTHDR: 2038 case IPV6_2292DSTOPTS: 2039 switch (optname) { 2040 case IPV6_2292PKTINFO: 2041 optval = OPTBIT(IN6P_PKTINFO); 2042 break; 2043 case IPV6_2292HOPLIMIT: 2044 optval = OPTBIT(IN6P_HOPLIMIT); 2045 break; 2046 case IPV6_2292HOPOPTS: 2047 optval = OPTBIT(IN6P_HOPOPTS); 2048 break; 2049 case IPV6_2292RTHDR: 2050 optval = OPTBIT(IN6P_RTHDR); 2051 break; 2052 case IPV6_2292DSTOPTS: 2053 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); 2054 break; 2055 } 2056 error = sooptcopyout(sopt, &optval, 2057 sizeof optval); 2058 break; 2059 case IPV6_PKTINFO: 2060 case IPV6_HOPOPTS: 2061 case IPV6_RTHDR: 2062 case IPV6_DSTOPTS: 2063 case IPV6_RTHDRDSTOPTS: 2064 case IPV6_NEXTHOP: 2065 case IPV6_TCLASS: 2066 case IPV6_DONTFRAG: 2067 case IPV6_USE_MIN_MTU: 2068 case IPV6_PREFER_TEMPADDR: 2069 error = ip6_getpcbopt(in6p->in6p_outputopts, 2070 optname, sopt); 2071 break; 2072 2073 case IPV6_MULTICAST_IF: 2074 case IPV6_MULTICAST_HOPS: 2075 case IPV6_MULTICAST_LOOP: 2076 case IPV6_JOIN_GROUP: 2077 case IPV6_LEAVE_GROUP: 2078 { 2079 struct mbuf *m; 2080 error = ip6_getmoptions(sopt->sopt_name, 2081 in6p->in6p_moptions, &m); 2082 if (error == 0) 2083 error = sooptcopyout(sopt, 2084 mtod(m, char *), m->m_len); 2085 m_freem(m); 2086 } 2087 break; 2088 2089#if defined(IPSEC) || defined(FAST_IPSEC) 2090 case IPV6_IPSEC_POLICY: 2091 { 2092 caddr_t req = NULL; 2093 size_t len = 0; 2094 struct mbuf *m = NULL; 2095 struct mbuf **mp = &m; 2096 size_t ovalsize = sopt->sopt_valsize; 2097 caddr_t oval = (caddr_t)sopt->sopt_val; 2098 2099 error = soopt_getm(sopt, &m); /* XXX */ 2100 if (error != NULL) 2101 break; 2102 error = soopt_mcopyin(sopt, m); /* XXX */ 2103 if (error != NULL) 2104 break; 2105 sopt->sopt_valsize = ovalsize; 2106 sopt->sopt_val = oval; 2107 if (m) { 2108 req = mtod(m, caddr_t); 2109 len = m->m_len; 2110 } 2111 error = ipsec6_get_policy(in6p, req, len, mp); 2112 if (error == 0) 2113 error = soopt_mcopyout(sopt, m); /* XXX */ 2114 if (error == 0 && m) 2115 m_freem(m); 2116 break; 2117 } 2118#endif /* KAME IPSEC */ 2119 2120 case IPV6_FW_GET: 2121 { 2122 struct mbuf *m; 2123 struct mbuf **mp = &m; 2124 2125 if (ip6_fw_ctl_ptr == NULL) 2126 { 2127 return EINVAL; 2128 } 2129 error = (*ip6_fw_ctl_ptr)(optname, mp); 2130 if (error == 0) 2131 error = soopt_mcopyout(sopt, m); /* XXX */ 2132 if (error == 0 && m) 2133 m_freem(m); 2134 } 2135 break; 2136 2137 default: 2138 error = ENOPROTOOPT; 2139 break; 2140 } 2141 break; 2142 } 2143 } else { /* level != IPPROTO_IPV6 */ 2144 error = EINVAL; 2145 } 2146 return (error); 2147} 2148 2149int 2150ip6_raw_ctloutput(so, sopt) 2151 struct socket *so; 2152 struct sockopt *sopt; 2153{ 2154 int error = 0, optval, optlen; 2155 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum); 2156 struct in6pcb *in6p = sotoin6pcb(so); 2157 int level, op, optname; 2158 2159 if (sopt) { 2160 level = sopt->sopt_level; 2161 op = sopt->sopt_dir; 2162 optname = sopt->sopt_name; 2163 optlen = sopt->sopt_valsize; 2164 } else 2165 panic("ip6_raw_ctloutput: arg soopt is NULL"); 2166 2167 if (level != IPPROTO_IPV6) { 2168 return (EINVAL); 2169 } 2170 2171 switch (optname) { 2172 case IPV6_CHECKSUM: 2173 /* 2174 * For ICMPv6 sockets, no modification allowed for checksum 2175 * offset, permit "no change" values to help existing apps. 2176 * 2177 * XXX 2292bis says: "An attempt to set IPV6_CHECKSUM 2178 * for an ICMPv6 socket will fail." 2179 * The current behavior does not meet 2292bis. 2180 */ 2181 switch (op) { 2182 case SOPT_SET: 2183 if (optlen != sizeof(int)) { 2184 error = EINVAL; 2185 break; 2186 } 2187 error = sooptcopyin(sopt, &optval, sizeof(optval), 2188 sizeof(optval)); 2189 if (error) 2190 break; 2191 if ((optval % 2) != 0) { 2192 /* the API assumes even offset values */ 2193 error = EINVAL; 2194 } else if (so->so_proto->pr_protocol == 2195 IPPROTO_ICMPV6) { 2196 if (optval != icmp6off) 2197 error = EINVAL; 2198 } else 2199 in6p->in6p_cksum = optval; 2200 break; 2201 2202 case SOPT_GET: 2203 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) 2204 optval = icmp6off; 2205 else 2206 optval = in6p->in6p_cksum; 2207 2208 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2209 break; 2210 2211 default: 2212 error = EINVAL; 2213 break; 2214 } 2215 break; 2216 2217 default: 2218 error = ENOPROTOOPT; 2219 break; 2220 } 2221 2222 return (error); 2223} 2224 2225/* 2226 * Set up IP6 options in pcb for insertion in output packets or 2227 * specifying behavior of outgoing packets. 2228 */ 2229static int 2230ip6_pcbopts(pktopt, m, so, sopt) 2231 struct ip6_pktopts **pktopt; 2232 struct mbuf *m; 2233 struct socket *so; 2234 struct sockopt *sopt; 2235{ 2236 struct ip6_pktopts *opt = *pktopt; 2237 int error = 0; 2238 struct thread *td = sopt->sopt_td; 2239 int priv = 0; 2240 2241 /* turn off any old options. */ 2242 if (opt) { 2243#ifdef DIAGNOSTIC 2244 if (opt->ip6po_pktinfo || opt->ip6po_nexthop || 2245 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 || 2246 opt->ip6po_rhinfo.ip6po_rhi_rthdr) 2247 printf("ip6_pcbopts: all specified options are cleared.\n"); 2248#endif 2249 ip6_clearpktopts(opt, -1); 2250 } else 2251 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK); 2252 *pktopt = NULL; 2253 2254 if (!m || m->m_len == 0) { 2255 /* 2256 * Only turning off any previous options, regardless of 2257 * whether the opt is just created or given. 2258 */ 2259 free(opt, M_IP6OPT); 2260 return (0); 2261 } 2262 2263 /* set options specified by user. */ 2264 if (td && !suser(td)) 2265 priv = 1; 2266 if ((error = ip6_setpktoptions(m, opt, NULL, priv, 1, 2267 so->so_proto->pr_protocol)) != 0) { 2268 ip6_clearpktopts(opt, -1); /* XXX: discard all options */ 2269 free(opt, M_IP6OPT); 2270 return (error); 2271 } 2272 *pktopt = opt; 2273 return (0); 2274} 2275 2276/* 2277 * initialize ip6_pktopts. beware that there are non-zero default values in 2278 * the struct. 2279 */ 2280void 2281init_ip6pktopts(opt) 2282 struct ip6_pktopts *opt; 2283{ 2284 2285 bzero(opt, sizeof(*opt)); 2286 opt->ip6po_hlim = -1; /* -1 means default hop limit */ 2287 opt->ip6po_tclass = -1; /* -1 means default traffic class */ 2288 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY; 2289 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM; 2290} 2291 2292static int 2293ip6_pcbopt(optname, buf, len, pktopt, priv, uproto) 2294 int optname, len, priv; 2295 u_char *buf; 2296 struct ip6_pktopts **pktopt; 2297 int uproto; 2298{ 2299 struct ip6_pktopts *opt; 2300 2301 if (*pktopt == NULL) { 2302 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT, 2303 M_WAITOK); 2304 init_ip6pktopts(*pktopt); 2305 (*pktopt)->needfree = 1; 2306 } 2307 opt = *pktopt; 2308 2309 return (ip6_setpktoption(optname, buf, len, opt, priv, 1, 0, uproto)); 2310} 2311 2312static int 2313ip6_getpcbopt(pktopt, optname, sopt) 2314 struct ip6_pktopts *pktopt; 2315 struct sockopt *sopt; 2316 int optname; 2317{ 2318 void *optdata = NULL; 2319 int optdatalen = 0; 2320 struct ip6_ext *ip6e; 2321 int error = 0; 2322 struct in6_pktinfo null_pktinfo; 2323 int deftclass = 0, on; 2324 int defminmtu = IP6PO_MINMTU_MCASTONLY; 2325 int defpreftemp = IP6PO_TEMPADDR_SYSTEM; 2326 2327 switch (optname) { 2328 case IPV6_PKTINFO: 2329 if (pktopt && pktopt->ip6po_pktinfo) 2330 optdata = (void *)pktopt->ip6po_pktinfo; 2331 else { 2332 /* XXX: we don't have to do this every time... */ 2333 bzero(&null_pktinfo, sizeof(null_pktinfo)); 2334 optdata = (void *)&null_pktinfo; 2335 } 2336 optdatalen = sizeof(struct in6_pktinfo); 2337 break; 2338 case IPV6_TCLASS: 2339 if (pktopt && pktopt->ip6po_tclass >= 0) 2340 optdata = (void *)&pktopt->ip6po_tclass; 2341 else 2342 optdata = (void *)&deftclass; 2343 optdatalen = sizeof(int); 2344 break; 2345 case IPV6_HOPOPTS: 2346 if (pktopt && pktopt->ip6po_hbh) { 2347 optdata = (void *)pktopt->ip6po_hbh; 2348 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh; 2349 optdatalen = (ip6e->ip6e_len + 1) << 3; 2350 } 2351 break; 2352 case IPV6_RTHDR: 2353 if (pktopt && pktopt->ip6po_rthdr) { 2354 optdata = (void *)pktopt->ip6po_rthdr; 2355 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr; 2356 optdatalen = (ip6e->ip6e_len + 1) << 3; 2357 } 2358 break; 2359 case IPV6_RTHDRDSTOPTS: 2360 if (pktopt && pktopt->ip6po_dest1) { 2361 optdata = (void *)pktopt->ip6po_dest1; 2362 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1; 2363 optdatalen = (ip6e->ip6e_len + 1) << 3; 2364 } 2365 break; 2366 case IPV6_DSTOPTS: 2367 if (pktopt && pktopt->ip6po_dest2) { 2368 optdata = (void *)pktopt->ip6po_dest2; 2369 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2; 2370 optdatalen = (ip6e->ip6e_len + 1) << 3; 2371 } 2372 break; 2373 case IPV6_NEXTHOP: 2374 if (pktopt && pktopt->ip6po_nexthop) { 2375 optdata = (void *)pktopt->ip6po_nexthop; 2376 optdatalen = pktopt->ip6po_nexthop->sa_len; 2377 } 2378 break; 2379 case IPV6_USE_MIN_MTU: 2380 if (pktopt) 2381 optdata = (void *)&pktopt->ip6po_minmtu; 2382 else 2383 optdata = (void *)&defminmtu; 2384 optdatalen = sizeof(int); 2385 break; 2386 case IPV6_DONTFRAG: 2387 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG)) 2388 on = 1; 2389 else 2390 on = 0; 2391 optdata = (void *)&on; 2392 optdatalen = sizeof(on); 2393 break; 2394 case IPV6_PREFER_TEMPADDR: 2395 if (pktopt) 2396 optdata = (void *)&pktopt->ip6po_prefer_tempaddr; 2397 else 2398 optdata = (void *)&defpreftemp; 2399 optdatalen = sizeof(int); 2400 break; 2401 default: /* should not happen */ 2402#ifdef DIAGNOSTIC 2403 panic("ip6_getpcbopt: unexpected option\n"); 2404#endif 2405 return (ENOPROTOOPT); 2406 } 2407 2408 error = sooptcopyout(sopt, optdata, optdatalen); 2409 2410 return (error); 2411} 2412 2413void 2414ip6_clearpktopts(pktopt, optname) 2415 struct ip6_pktopts *pktopt; 2416 int optname; 2417{ 2418 int needfree; 2419 2420 needfree = pktopt->needfree; 2421 2422 if (optname == -1 || optname == IPV6_PKTINFO) { 2423 if (needfree && pktopt->ip6po_pktinfo) 2424 free(pktopt->ip6po_pktinfo, M_IP6OPT); 2425 pktopt->ip6po_pktinfo = NULL; 2426 } 2427 if (optname == -1 || optname == IPV6_HOPLIMIT) 2428 pktopt->ip6po_hlim = -1; 2429 if (optname == -1 || optname == IPV6_TCLASS) 2430 pktopt->ip6po_tclass = -1; 2431 if (optname == -1 || optname == IPV6_NEXTHOP) { 2432 if (pktopt->ip6po_nextroute.ro_rt) { 2433 RTFREE(pktopt->ip6po_nextroute.ro_rt); 2434 pktopt->ip6po_nextroute.ro_rt = NULL; 2435 } 2436 if (needfree && pktopt->ip6po_nexthop) 2437 free(pktopt->ip6po_nexthop, M_IP6OPT); 2438 pktopt->ip6po_nexthop = NULL; 2439 } 2440 if (optname == -1 || optname == IPV6_HOPOPTS) { 2441 if (needfree && pktopt->ip6po_hbh) 2442 free(pktopt->ip6po_hbh, M_IP6OPT); 2443 pktopt->ip6po_hbh = NULL; 2444 } 2445 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) { 2446 if (needfree && pktopt->ip6po_dest1) 2447 free(pktopt->ip6po_dest1, M_IP6OPT); 2448 pktopt->ip6po_dest1 = NULL; 2449 } 2450 if (optname == -1 || optname == IPV6_RTHDR) { 2451 if (needfree && pktopt->ip6po_rhinfo.ip6po_rhi_rthdr) 2452 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT); 2453 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL; 2454 if (pktopt->ip6po_route.ro_rt) { 2455 RTFREE(pktopt->ip6po_route.ro_rt); 2456 pktopt->ip6po_route.ro_rt = NULL; 2457 } 2458 } 2459 if (optname == -1 || optname == IPV6_DSTOPTS) { 2460 if (needfree && pktopt->ip6po_dest2) 2461 free(pktopt->ip6po_dest2, M_IP6OPT); 2462 pktopt->ip6po_dest2 = NULL; 2463 } 2464} 2465 2466#define PKTOPT_EXTHDRCPY(type) \ 2467do {\ 2468 if (src->type) {\ 2469 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\ 2470 dst->type = malloc(hlen, M_IP6OPT, canwait);\ 2471 if (dst->type == NULL && canwait == M_NOWAIT)\ 2472 goto bad;\ 2473 bcopy(src->type, dst->type, hlen);\ 2474 }\ 2475} while (/*CONSTCOND*/ 0) 2476 2477struct ip6_pktopts * 2478ip6_copypktopts(src, canwait) 2479 struct ip6_pktopts *src; 2480 int canwait; 2481{ 2482 struct ip6_pktopts *dst; 2483 2484 if (src == NULL) { 2485 printf("ip6_clearpktopts: invalid argument\n"); 2486 return (NULL); 2487 } 2488 2489 dst = malloc(sizeof(*dst), M_IP6OPT, canwait); 2490 if (dst == NULL && canwait == M_NOWAIT) 2491 return (NULL); 2492 bzero(dst, sizeof(*dst)); 2493 dst->needfree = 1; 2494 2495 dst->ip6po_hlim = src->ip6po_hlim; 2496 dst->ip6po_tclass = src->ip6po_tclass; 2497 dst->ip6po_flags = src->ip6po_flags; 2498 if (src->ip6po_pktinfo) { 2499 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo), 2500 M_IP6OPT, canwait); 2501 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT) 2502 goto bad; 2503 *dst->ip6po_pktinfo = *src->ip6po_pktinfo; 2504 } 2505 if (src->ip6po_nexthop) { 2506 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len, 2507 M_IP6OPT, canwait); 2508 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT) 2509 goto bad; 2510 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop, 2511 src->ip6po_nexthop->sa_len); 2512 } 2513 PKTOPT_EXTHDRCPY(ip6po_hbh); 2514 PKTOPT_EXTHDRCPY(ip6po_dest1); 2515 PKTOPT_EXTHDRCPY(ip6po_dest2); 2516 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */ 2517 return (dst); 2518 2519 bad: 2520 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT); 2521 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT); 2522 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT); 2523 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT); 2524 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT); 2525 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT); 2526 free(dst, M_IP6OPT); 2527 return (NULL); 2528} 2529#undef PKTOPT_EXTHDRCPY 2530 2531void 2532ip6_freepcbopts(pktopt) 2533 struct ip6_pktopts *pktopt; 2534{ 2535 if (pktopt == NULL) 2536 return; 2537 2538 ip6_clearpktopts(pktopt, -1); 2539 2540 free(pktopt, M_IP6OPT); 2541} 2542 2543/* 2544 * Set the IP6 multicast options in response to user setsockopt(). 2545 */ 2546static int 2547ip6_setmoptions(optname, im6op, m) 2548 int optname; 2549 struct ip6_moptions **im6op; 2550 struct mbuf *m; 2551{ 2552 int error = 0; 2553 u_int loop, ifindex; 2554 struct ipv6_mreq *mreq; 2555 struct ifnet *ifp; 2556 struct ip6_moptions *im6o = *im6op; 2557 struct route_in6 ro; 2558 struct sockaddr_in6 *dst; 2559 struct in6_multi_mship *imm; 2560 struct thread *td = curthread; 2561 2562 if (im6o == NULL) { 2563 /* 2564 * No multicast option buffer attached to the pcb; 2565 * allocate one and initialize to default values. 2566 */ 2567 im6o = (struct ip6_moptions *) 2568 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK); 2569 2570 if (im6o == NULL) 2571 return (ENOBUFS); 2572 *im6op = im6o; 2573 im6o->im6o_multicast_ifp = NULL; 2574 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 2575 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP; 2576 LIST_INIT(&im6o->im6o_memberships); 2577 } 2578 2579 switch (optname) { 2580 2581 case IPV6_MULTICAST_IF: 2582 /* 2583 * Select the interface for outgoing multicast packets. 2584 */ 2585 if (m == NULL || m->m_len != sizeof(u_int)) { 2586 error = EINVAL; 2587 break; 2588 } 2589 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex)); 2590 if (ifindex < 0 || if_index < ifindex) { 2591 error = ENXIO; /* XXX EINVAL? */ 2592 break; 2593 } 2594 ifp = ifnet_byindex(ifindex); 2595 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2596 error = EADDRNOTAVAIL; 2597 break; 2598 } 2599 im6o->im6o_multicast_ifp = ifp; 2600 break; 2601 2602 case IPV6_MULTICAST_HOPS: 2603 { 2604 /* 2605 * Set the IP6 hoplimit for outgoing multicast packets. 2606 */ 2607 int optval; 2608 if (m == NULL || m->m_len != sizeof(int)) { 2609 error = EINVAL; 2610 break; 2611 } 2612 bcopy(mtod(m, u_int *), &optval, sizeof(optval)); 2613 if (optval < -1 || optval >= 256) 2614 error = EINVAL; 2615 else if (optval == -1) 2616 im6o->im6o_multicast_hlim = ip6_defmcasthlim; 2617 else 2618 im6o->im6o_multicast_hlim = optval; 2619 break; 2620 } 2621 2622 case IPV6_MULTICAST_LOOP: 2623 /* 2624 * Set the loopback flag for outgoing multicast packets. 2625 * Must be zero or one. 2626 */ 2627 if (m == NULL || m->m_len != sizeof(u_int)) { 2628 error = EINVAL; 2629 break; 2630 } 2631 bcopy(mtod(m, u_int *), &loop, sizeof(loop)); 2632 if (loop > 1) { 2633 error = EINVAL; 2634 break; 2635 } 2636 im6o->im6o_multicast_loop = loop; 2637 break; 2638 2639 case IPV6_JOIN_GROUP: 2640 /* 2641 * Add a multicast group membership. 2642 * Group must be a valid IP6 multicast address. 2643 */ 2644 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 2645 error = EINVAL; 2646 break; 2647 } 2648 mreq = mtod(m, struct ipv6_mreq *); 2649 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 2650 /* 2651 * We use the unspecified address to specify to accept 2652 * all multicast addresses. Only super user is allowed 2653 * to do this. 2654 */ 2655 if (suser(td)) { 2656 error = EACCES; 2657 break; 2658 } 2659 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 2660 error = EINVAL; 2661 break; 2662 } 2663 2664 /* 2665 * If the interface is specified, validate it. 2666 */ 2667 if (mreq->ipv6mr_interface < 0 || 2668 if_index < mreq->ipv6mr_interface) { 2669 error = ENXIO; /* XXX EINVAL? */ 2670 break; 2671 } 2672 /* 2673 * If no interface was explicitly specified, choose an 2674 * appropriate one according to the given multicast address. 2675 */ 2676 if (mreq->ipv6mr_interface == 0) { 2677 /* 2678 * If the multicast address is in node-local scope, 2679 * the interface should be a loopback interface. 2680 * Otherwise, look up the routing table for the 2681 * address, and choose the outgoing interface. 2682 * XXX: is it a good approach? 2683 */ 2684 if (IN6_IS_ADDR_MC_INTFACELOCAL(&mreq->ipv6mr_multiaddr)) { 2685 ifp = &loif[0]; 2686 } else { 2687 ro.ro_rt = NULL; 2688 dst = (struct sockaddr_in6 *)&ro.ro_dst; 2689 bzero(dst, sizeof(*dst)); 2690 dst->sin6_len = sizeof(struct sockaddr_in6); 2691 dst->sin6_family = AF_INET6; 2692 dst->sin6_addr = mreq->ipv6mr_multiaddr; 2693 rtalloc((struct route *)&ro); 2694 if (ro.ro_rt == NULL) { 2695 error = EADDRNOTAVAIL; 2696 break; 2697 } 2698 ifp = ro.ro_rt->rt_ifp; 2699 RTFREE(ro.ro_rt); 2700 } 2701 } else 2702 ifp = ifnet_byindex(mreq->ipv6mr_interface); 2703 2704 /* 2705 * See if we found an interface, and confirm that it 2706 * supports multicast 2707 */ 2708 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 2709 error = EADDRNOTAVAIL; 2710 break; 2711 } 2712 /* 2713 * Put interface index into the multicast address, 2714 * if the address has link-local scope. 2715 */ 2716 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) { 2717 mreq->ipv6mr_multiaddr.s6_addr16[1] = 2718 htons(ifp->if_index); 2719 } 2720 /* 2721 * See if the membership already exists. 2722 */ 2723 for (imm = im6o->im6o_memberships.lh_first; 2724 imm != NULL; imm = imm->i6mm_chain.le_next) 2725 if (imm->i6mm_maddr->in6m_ifp == ifp && 2726 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2727 &mreq->ipv6mr_multiaddr)) 2728 break; 2729 if (imm != NULL) { 2730 error = EADDRINUSE; 2731 break; 2732 } 2733 /* 2734 * Everything looks good; add a new record to the multicast 2735 * address list for the given interface. 2736 */ 2737 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK); 2738 if (imm == NULL) { 2739 error = ENOBUFS; 2740 break; 2741 } 2742 if ((imm->i6mm_maddr = 2743 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) { 2744 free(imm, M_IPMADDR); 2745 break; 2746 } 2747 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 2748 break; 2749 2750 case IPV6_LEAVE_GROUP: 2751 /* 2752 * Drop a multicast group membership. 2753 * Group must be a valid IP6 multicast address. 2754 */ 2755 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) { 2756 error = EINVAL; 2757 break; 2758 } 2759 mreq = mtod(m, struct ipv6_mreq *); 2760 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) { 2761 if (suser(td)) { 2762 error = EACCES; 2763 break; 2764 } 2765 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) { 2766 error = EINVAL; 2767 break; 2768 } 2769 /* 2770 * If an interface address was specified, get a pointer 2771 * to its ifnet structure. 2772 */ 2773 if (mreq->ipv6mr_interface < 0 2774 || if_index < mreq->ipv6mr_interface) { 2775 error = ENXIO; /* XXX EINVAL? */ 2776 break; 2777 } 2778 ifp = ifnet_byindex(mreq->ipv6mr_interface); 2779 /* 2780 * Put interface index into the multicast address, 2781 * if the address has link-local scope. 2782 */ 2783 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) { 2784 mreq->ipv6mr_multiaddr.s6_addr16[1] 2785 = htons(mreq->ipv6mr_interface); 2786 } 2787 2788 /* 2789 * Find the membership in the membership list. 2790 */ 2791 for (imm = im6o->im6o_memberships.lh_first; 2792 imm != NULL; imm = imm->i6mm_chain.le_next) { 2793 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) && 2794 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 2795 &mreq->ipv6mr_multiaddr)) 2796 break; 2797 } 2798 if (imm == NULL) { 2799 /* Unable to resolve interface */ 2800 error = EADDRNOTAVAIL; 2801 break; 2802 } 2803 /* 2804 * Give up the multicast address record to which the 2805 * membership points. 2806 */ 2807 LIST_REMOVE(imm, i6mm_chain); 2808 in6_delmulti(imm->i6mm_maddr); 2809 free(imm, M_IPMADDR); 2810 break; 2811 2812 default: 2813 error = EOPNOTSUPP; 2814 break; 2815 } 2816 2817 /* 2818 * If all options have default values, no need to keep the mbuf. 2819 */ 2820 if (im6o->im6o_multicast_ifp == NULL && 2821 im6o->im6o_multicast_hlim == ip6_defmcasthlim && 2822 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP && 2823 im6o->im6o_memberships.lh_first == NULL) { 2824 free(*im6op, M_IPMOPTS); 2825 *im6op = NULL; 2826 } 2827 2828 return (error); 2829} 2830 2831/* 2832 * Return the IP6 multicast options in response to user getsockopt(). 2833 */ 2834static int 2835ip6_getmoptions(optname, im6o, mp) 2836 int optname; 2837 struct ip6_moptions *im6o; 2838 struct mbuf **mp; 2839{ 2840 u_int *hlim, *loop, *ifindex; 2841 2842 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */ 2843 2844 switch (optname) { 2845 2846 case IPV6_MULTICAST_IF: 2847 ifindex = mtod(*mp, u_int *); 2848 (*mp)->m_len = sizeof(u_int); 2849 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) 2850 *ifindex = 0; 2851 else 2852 *ifindex = im6o->im6o_multicast_ifp->if_index; 2853 return (0); 2854 2855 case IPV6_MULTICAST_HOPS: 2856 hlim = mtod(*mp, u_int *); 2857 (*mp)->m_len = sizeof(u_int); 2858 if (im6o == NULL) 2859 *hlim = ip6_defmcasthlim; 2860 else 2861 *hlim = im6o->im6o_multicast_hlim; 2862 return (0); 2863 2864 case IPV6_MULTICAST_LOOP: 2865 loop = mtod(*mp, u_int *); 2866 (*mp)->m_len = sizeof(u_int); 2867 if (im6o == NULL) 2868 *loop = ip6_defmcasthlim; 2869 else 2870 *loop = im6o->im6o_multicast_loop; 2871 return (0); 2872 2873 default: 2874 return (EOPNOTSUPP); 2875 } 2876} 2877 2878/* 2879 * Discard the IP6 multicast options. 2880 */ 2881void 2882ip6_freemoptions(im6o) 2883 struct ip6_moptions *im6o; 2884{ 2885 struct in6_multi_mship *imm; 2886 2887 if (im6o == NULL) 2888 return; 2889 2890 while ((imm = im6o->im6o_memberships.lh_first) != NULL) { 2891 LIST_REMOVE(imm, i6mm_chain); 2892 if (imm->i6mm_maddr) 2893 in6_delmulti(imm->i6mm_maddr); 2894 free(imm, M_IPMADDR); 2895 } 2896 free(im6o, M_IPMOPTS); 2897} 2898 2899/* 2900 * Set IPv6 outgoing packet options based on advanced API. 2901 */ 2902int 2903ip6_setpktoptions(control, opt, stickyopt, priv, needcopy, uproto) 2904 struct mbuf *control; 2905 struct ip6_pktopts *opt, *stickyopt; 2906 int priv, needcopy, uproto; 2907{ 2908 struct cmsghdr *cm = 0; 2909 2910 if (control == 0 || opt == 0) 2911 return (EINVAL); 2912 2913 if (stickyopt) { 2914 /* 2915 * If stickyopt is provided, make a local copy of the options 2916 * for this particular packet, then override them by ancillary 2917 * objects. 2918 * XXX: need to gain a reference for the cached route of the 2919 * next hop in case of the overriding. 2920 */ 2921 *opt = *stickyopt; 2922 if (opt->ip6po_nextroute.ro_rt) { 2923 RT_LOCK(opt->ip6po_nextroute.ro_rt); 2924 RT_ADDREF(opt->ip6po_nextroute.ro_rt); 2925 RT_UNLOCK(opt->ip6po_nextroute.ro_rt); 2926 } 2927 } else 2928 init_ip6pktopts(opt); 2929 opt->needfree = needcopy; 2930 2931 /* 2932 * XXX: Currently, we assume all the optional information is stored 2933 * in a single mbuf. 2934 */ 2935 if (control->m_next) 2936 return (EINVAL); 2937 2938 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len), 2939 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { 2940 int error; 2941 2942 if (control->m_len < CMSG_LEN(0)) 2943 return (EINVAL); 2944 2945 cm = mtod(control, struct cmsghdr *); 2946 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) 2947 return (EINVAL); 2948 if (cm->cmsg_level != IPPROTO_IPV6) 2949 continue; 2950 2951 error = ip6_setpktoption(cm->cmsg_type, CMSG_DATA(cm), 2952 cm->cmsg_len - CMSG_LEN(0), opt, priv, needcopy, 1, uproto); 2953 if (error) 2954 return (error); 2955 } 2956 2957 return (0); 2958} 2959 2960/* 2961 * Set a particular packet option, as a sticky option or an ancillary data 2962 * item. "len" can be 0 only when it's a sticky option. 2963 * We have 4 cases of combination of "sticky" and "cmsg": 2964 * "sticky=0, cmsg=0": impossible 2965 * "sticky=0, cmsg=1": RFC2292 or rfc2292bis ancillary data 2966 * "sticky=1, cmsg=0": rfc2292bis socket option 2967 * "sticky=1, cmsg=1": RFC2292 socket option 2968 */ 2969static int 2970ip6_setpktoption(optname, buf, len, opt, priv, sticky, cmsg, uproto) 2971 int optname, len, priv, sticky, cmsg, uproto; 2972 u_char *buf; 2973 struct ip6_pktopts *opt; 2974{ 2975 int minmtupolicy, preftemp; 2976 2977 if (!sticky && !cmsg) { 2978#ifdef DIAGNOSTIC 2979 printf("ip6_setpktoption: impossible case\n"); 2980#endif 2981 return (EINVAL); 2982 } 2983 2984 /* 2985 * IPV6_2292xxx is for backward compatibility to RFC2292, and should 2986 * not be specified in the context of rfc2292bis. Conversely, 2987 * rfc2292bis types should not be specified in the context of RFC2292. 2988 */ 2989 if (!cmsg) { 2990 switch (optname) { 2991 case IPV6_2292PKTINFO: 2992 case IPV6_2292HOPLIMIT: 2993 case IPV6_2292NEXTHOP: 2994 case IPV6_2292HOPOPTS: 2995 case IPV6_2292DSTOPTS: 2996 case IPV6_2292RTHDR: 2997 case IPV6_2292PKTOPTIONS: 2998 return (ENOPROTOOPT); 2999 } 3000 } 3001 if (sticky && cmsg) { 3002 switch (optname) { 3003 case IPV6_PKTINFO: 3004 case IPV6_HOPLIMIT: 3005 case IPV6_NEXTHOP: 3006 case IPV6_HOPOPTS: 3007 case IPV6_DSTOPTS: 3008 case IPV6_RTHDRDSTOPTS: 3009 case IPV6_RTHDR: 3010 case IPV6_USE_MIN_MTU: 3011 case IPV6_DONTFRAG: 3012 case IPV6_TCLASS: 3013 case IPV6_PREFER_TEMPADDR: /* XXX: not an rfc2292bis option */ 3014 return (ENOPROTOOPT); 3015 } 3016 } 3017 3018 switch (optname) { 3019 case IPV6_2292PKTINFO: 3020 case IPV6_PKTINFO: 3021 { 3022 struct ifnet *ifp = NULL; 3023 struct in6_pktinfo *pktinfo; 3024 3025 if (len != sizeof(struct in6_pktinfo)) 3026 return (EINVAL); 3027 3028 pktinfo = (struct in6_pktinfo *)buf; 3029 3030 /* 3031 * An application can clear any sticky IPV6_PKTINFO option by 3032 * doing a "regular" setsockopt with ipi6_addr being 3033 * in6addr_any and ipi6_ifindex being zero. 3034 * [RFC 3542, Section 6] 3035 */ 3036 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo && 3037 pktinfo->ipi6_ifindex == 0 && 3038 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 3039 ip6_clearpktopts(opt, optname); 3040 break; 3041 } 3042 3043 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO && 3044 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) { 3045 return (EINVAL); 3046 } 3047 3048 /* validate the interface index if specified. */ 3049 if (pktinfo->ipi6_ifindex > if_index || 3050 pktinfo->ipi6_ifindex < 0) { 3051 return (ENXIO); 3052 } 3053 if (pktinfo->ipi6_ifindex) { 3054 ifp = ifnet_byindex(pktinfo->ipi6_ifindex); 3055 if (ifp == NULL) 3056 return (ENXIO); 3057 } 3058 3059 /* 3060 * We store the address anyway, and let in6_selectsrc() 3061 * validate the specified address. This is because ipi6_addr 3062 * may not have enough information about its scope zone, and 3063 * we may need additional information (such as outgoing 3064 * interface or the scope zone of a destination address) to 3065 * disambiguate the scope. 3066 * XXX: the delay of the validation may confuse the 3067 * application when it is used as a sticky option. 3068 */ 3069 if (sticky) { 3070 if (opt->ip6po_pktinfo == NULL) { 3071 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo), 3072 M_IP6OPT, M_WAITOK); 3073 } 3074 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo)); 3075 } else 3076 opt->ip6po_pktinfo = pktinfo; 3077 break; 3078 } 3079 3080 case IPV6_2292HOPLIMIT: 3081 case IPV6_HOPLIMIT: 3082 { 3083 int *hlimp; 3084 3085 /* 3086 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT 3087 * to simplify the ordering among hoplimit options. 3088 */ 3089 if (optname == IPV6_HOPLIMIT && sticky) 3090 return (ENOPROTOOPT); 3091 3092 if (len != sizeof(int)) 3093 return (EINVAL); 3094 hlimp = (int *)buf; 3095 if (*hlimp < -1 || *hlimp > 255) 3096 return (EINVAL); 3097 3098 opt->ip6po_hlim = *hlimp; 3099 break; 3100 } 3101 3102 case IPV6_TCLASS: 3103 { 3104 int tclass; 3105 3106 if (len != sizeof(int)) 3107 return (EINVAL); 3108 tclass = *(int *)buf; 3109 if (tclass < -1 || tclass > 255) 3110 return (EINVAL); 3111 3112 opt->ip6po_tclass = tclass; 3113 break; 3114 } 3115 3116 case IPV6_2292NEXTHOP: 3117 case IPV6_NEXTHOP: 3118 if (!priv) 3119 return (EPERM); 3120 3121 if (len == 0) { /* just remove the option */ 3122 ip6_clearpktopts(opt, IPV6_NEXTHOP); 3123 break; 3124 } 3125 3126 /* check if cmsg_len is large enough for sa_len */ 3127 if (len < sizeof(struct sockaddr) || len < *buf) 3128 return (EINVAL); 3129 3130 switch (((struct sockaddr *)buf)->sa_family) { 3131 case AF_INET6: 3132 { 3133 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf; 3134#if 0 3135 int error; 3136#endif 3137 3138 if (sa6->sin6_len != sizeof(struct sockaddr_in6)) 3139 return (EINVAL); 3140 3141 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) || 3142 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 3143 return (EINVAL); 3144 } 3145#if 0 3146 if ((error = scope6_check_id(sa6, ip6_use_defzone)) 3147 != 0) { 3148 return (error); 3149 } 3150#endif 3151 sa6->sin6_scope_id = 0; /* XXX */ 3152 break; 3153 } 3154 case AF_LINK: /* should eventually be supported */ 3155 default: 3156 return (EAFNOSUPPORT); 3157 } 3158 3159 /* turn off the previous option, then set the new option. */ 3160 ip6_clearpktopts(opt, IPV6_NEXTHOP); 3161 if (sticky) { 3162 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_WAITOK); 3163 bcopy(buf, opt->ip6po_nexthop, *buf); 3164 } else 3165 opt->ip6po_nexthop = (struct sockaddr *)buf; 3166 break; 3167 3168 case IPV6_2292HOPOPTS: 3169 case IPV6_HOPOPTS: 3170 { 3171 struct ip6_hbh *hbh; 3172 int hbhlen; 3173 3174 /* 3175 * XXX: We don't allow a non-privileged user to set ANY HbH 3176 * options, since per-option restriction has too much 3177 * overhead. 3178 */ 3179 if (!priv) 3180 return (EPERM); 3181 3182 if (len == 0) { 3183 ip6_clearpktopts(opt, IPV6_HOPOPTS); 3184 break; /* just remove the option */ 3185 } 3186 3187 /* message length validation */ 3188 if (len < sizeof(struct ip6_hbh)) 3189 return (EINVAL); 3190 hbh = (struct ip6_hbh *)buf; 3191 hbhlen = (hbh->ip6h_len + 1) << 3; 3192 if (len != hbhlen) 3193 return (EINVAL); 3194 3195 /* turn off the previous option, then set the new option. */ 3196 ip6_clearpktopts(opt, IPV6_HOPOPTS); 3197 if (sticky) { 3198 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_WAITOK); 3199 bcopy(hbh, opt->ip6po_hbh, hbhlen); 3200 } else 3201 opt->ip6po_hbh = hbh; 3202 3203 break; 3204 } 3205 3206 case IPV6_2292DSTOPTS: 3207 case IPV6_DSTOPTS: 3208 case IPV6_RTHDRDSTOPTS: 3209 { 3210 struct ip6_dest *dest, **newdest = NULL; 3211 int destlen; 3212 3213 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */ 3214 return (EPERM); 3215 3216 if (len == 0) { 3217 ip6_clearpktopts(opt, optname); 3218 break; /* just remove the option */ 3219 } 3220 3221 /* message length validation */ 3222 if (len < sizeof(struct ip6_dest)) 3223 return (EINVAL); 3224 dest = (struct ip6_dest *)buf; 3225 destlen = (dest->ip6d_len + 1) << 3; 3226 if (len != destlen) 3227 return (EINVAL); 3228 3229 /* 3230 * Determine the position that the destination options header 3231 * should be inserted; before or after the routing header. 3232 */ 3233 switch (optname) { 3234 case IPV6_2292DSTOPTS: 3235 /* 3236 * The old advacned API is ambiguous on this point. 3237 * Our approach is to determine the position based 3238 * according to the existence of a routing header. 3239 * Note, however, that this depends on the order of the 3240 * extension headers in the ancillary data; the 1st 3241 * part of the destination options header must appear 3242 * before the routing header in the ancillary data, 3243 * too. 3244 * RFC2292bis solved the ambiguity by introducing 3245 * separate ancillary data or option types. 3246 */ 3247 if (opt->ip6po_rthdr == NULL) 3248 newdest = &opt->ip6po_dest1; 3249 else 3250 newdest = &opt->ip6po_dest2; 3251 break; 3252 case IPV6_RTHDRDSTOPTS: 3253 newdest = &opt->ip6po_dest1; 3254 break; 3255 case IPV6_DSTOPTS: 3256 newdest = &opt->ip6po_dest2; 3257 break; 3258 } 3259 3260 /* turn off the previous option, then set the new option. */ 3261 ip6_clearpktopts(opt, optname); 3262 if (sticky) { 3263 *newdest = malloc(destlen, M_IP6OPT, M_WAITOK); 3264 bcopy(dest, *newdest, destlen); 3265 } else 3266 *newdest = dest; 3267 3268 break; 3269 } 3270 3271 case IPV6_2292RTHDR: 3272 case IPV6_RTHDR: 3273 { 3274 struct ip6_rthdr *rth; 3275 int rthlen; 3276 3277 if (len == 0) { 3278 ip6_clearpktopts(opt, IPV6_RTHDR); 3279 break; /* just remove the option */ 3280 } 3281 3282 /* message length validation */ 3283 if (len < sizeof(struct ip6_rthdr)) 3284 return (EINVAL); 3285 rth = (struct ip6_rthdr *)buf; 3286 rthlen = (rth->ip6r_len + 1) << 3; 3287 if (len != rthlen) 3288 return (EINVAL); 3289 3290 switch (rth->ip6r_type) { 3291 case IPV6_RTHDR_TYPE_0: 3292 if (rth->ip6r_len == 0) /* must contain one addr */ 3293 return (EINVAL); 3294 if (rth->ip6r_len % 2) /* length must be even */ 3295 return (EINVAL); 3296 if (rth->ip6r_len / 2 != rth->ip6r_segleft) 3297 return (EINVAL); 3298 break; 3299 default: 3300 return (EINVAL); /* not supported */ 3301 } 3302 3303 /* turn off the previous option */ 3304 ip6_clearpktopts(opt, IPV6_RTHDR); 3305 if (sticky) { 3306 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_WAITOK); 3307 bcopy(rth, opt->ip6po_rthdr, rthlen); 3308 } else 3309 opt->ip6po_rthdr = rth; 3310 3311 break; 3312 } 3313 3314 case IPV6_USE_MIN_MTU: 3315 if (len != sizeof(int)) 3316 return (EINVAL); 3317 minmtupolicy = *(int *)buf; 3318 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY && 3319 minmtupolicy != IP6PO_MINMTU_DISABLE && 3320 minmtupolicy != IP6PO_MINMTU_ALL) { 3321 return (EINVAL); 3322 } 3323 opt->ip6po_minmtu = minmtupolicy; 3324 break; 3325 3326 case IPV6_DONTFRAG: 3327 if (len != sizeof(int)) 3328 return (EINVAL); 3329 3330 if (uproto == IPPROTO_TCP || *(int *)buf == 0) { 3331 /* 3332 * we ignore this option for TCP sockets. 3333 * (rfc2292bis leaves this case unspecified.) 3334 */ 3335 opt->ip6po_flags &= ~IP6PO_DONTFRAG; 3336 } else 3337 opt->ip6po_flags |= IP6PO_DONTFRAG; 3338 break; 3339 3340 case IPV6_PREFER_TEMPADDR: 3341 if (len != sizeof(int)) 3342 return (EINVAL); 3343 preftemp = *(int *)buf; 3344 if (preftemp != IP6PO_TEMPADDR_SYSTEM && 3345 preftemp != IP6PO_TEMPADDR_NOTPREFER && 3346 preftemp != IP6PO_TEMPADDR_PREFER) { 3347 return (EINVAL); 3348 } 3349 opt->ip6po_prefer_tempaddr = preftemp; 3350 break; 3351 3352 default: 3353 return (ENOPROTOOPT); 3354 } /* end of switch */ 3355 3356 return (0); 3357} 3358 3359/* 3360 * Routine called from ip6_output() to loop back a copy of an IP6 multicast 3361 * packet to the input queue of a specified interface. Note that this 3362 * calls the output routine of the loopback "driver", but with an interface 3363 * pointer that might NOT be &loif -- easier than replicating that code here. 3364 */ 3365void 3366ip6_mloopback(ifp, m, dst) 3367 struct ifnet *ifp; 3368 struct mbuf *m; 3369 struct sockaddr_in6 *dst; 3370{ 3371 struct mbuf *copym; 3372 struct ip6_hdr *ip6; 3373 3374 copym = m_copy(m, 0, M_COPYALL); 3375 if (copym == NULL) 3376 return; 3377 3378 /* 3379 * Make sure to deep-copy IPv6 header portion in case the data 3380 * is in an mbuf cluster, so that we can safely override the IPv6 3381 * header portion later. 3382 */ 3383 if ((copym->m_flags & M_EXT) != 0 || 3384 copym->m_len < sizeof(struct ip6_hdr)) { 3385 copym = m_pullup(copym, sizeof(struct ip6_hdr)); 3386 if (copym == NULL) 3387 return; 3388 } 3389 3390#ifdef DIAGNOSTIC 3391 if (copym->m_len < sizeof(*ip6)) { 3392 m_freem(copym); 3393 return; 3394 } 3395#endif 3396 3397 ip6 = mtod(copym, struct ip6_hdr *); 3398 /* 3399 * clear embedded scope identifiers if necessary. 3400 * in6_clearscope will touch the addresses only when necessary. 3401 */ 3402 in6_clearscope(&ip6->ip6_src); 3403 in6_clearscope(&ip6->ip6_dst); 3404 3405 (void)if_simloop(ifp, copym, dst->sin6_family, NULL); 3406} 3407 3408/* 3409 * Chop IPv6 header off from the payload. 3410 */ 3411static int 3412ip6_splithdr(m, exthdrs) 3413 struct mbuf *m; 3414 struct ip6_exthdrs *exthdrs; 3415{ 3416 struct mbuf *mh; 3417 struct ip6_hdr *ip6; 3418 3419 ip6 = mtod(m, struct ip6_hdr *); 3420 if (m->m_len > sizeof(*ip6)) { 3421 MGETHDR(mh, M_DONTWAIT, MT_HEADER); 3422 if (mh == 0) { 3423 m_freem(m); 3424 return ENOBUFS; 3425 } 3426 M_MOVE_PKTHDR(mh, m); 3427 MH_ALIGN(mh, sizeof(*ip6)); 3428 m->m_len -= sizeof(*ip6); 3429 m->m_data += sizeof(*ip6); 3430 mh->m_next = m; 3431 m = mh; 3432 m->m_len = sizeof(*ip6); 3433 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6)); 3434 } 3435 exthdrs->ip6e_ip6 = m; 3436 return 0; 3437} 3438 3439/* 3440 * Compute IPv6 extension header length. 3441 */ 3442int 3443ip6_optlen(in6p) 3444 struct in6pcb *in6p; 3445{ 3446 int len; 3447 3448 if (!in6p->in6p_outputopts) 3449 return 0; 3450 3451 len = 0; 3452#define elen(x) \ 3453 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0) 3454 3455 len += elen(in6p->in6p_outputopts->ip6po_hbh); 3456 if (in6p->in6p_outputopts->ip6po_rthdr) 3457 /* dest1 is valid with rthdr only */ 3458 len += elen(in6p->in6p_outputopts->ip6po_dest1); 3459 len += elen(in6p->in6p_outputopts->ip6po_rthdr); 3460 len += elen(in6p->in6p_outputopts->ip6po_dest2); 3461 return len; 3462#undef elen 3463} 3464