Linux Audio

Check our new training course

Loading...
v3.1
 
   1/*
   2 *	IPv6 BSD socket options interface
   3 *	Linux INET6 implementation
   4 *
   5 *	Authors:
   6 *	Pedro Roque		<roque@di.fc.ul.pt>
   7 *
   8 *	Based on linux/net/ipv4/ip_sockglue.c
   9 *
  10 *	This program is free software; you can redistribute it and/or
  11 *      modify it under the terms of the GNU General Public License
  12 *      as published by the Free Software Foundation; either version
  13 *      2 of the License, or (at your option) any later version.
  14 *
  15 *	FIXME: Make the setsockopt code POSIX compliant: That is
  16 *
  17 *	o	Truncate getsockopt returns
  18 *	o	Return an optlen of the truncated length if need be
  19 *
  20 *	Changes:
  21 *	David L Stevens <dlstevens@us.ibm.com>:
  22 *		- added multicast source filtering API for MLDv2
  23 */
  24
  25#include <linux/module.h>
  26#include <linux/capability.h>
  27#include <linux/errno.h>
  28#include <linux/types.h>
  29#include <linux/socket.h>
  30#include <linux/sockios.h>
  31#include <linux/net.h>
  32#include <linux/in6.h>
  33#include <linux/mroute6.h>
  34#include <linux/netdevice.h>
  35#include <linux/if_arp.h>
  36#include <linux/init.h>
  37#include <linux/sysctl.h>
  38#include <linux/netfilter.h>
  39#include <linux/slab.h>
  40
  41#include <net/sock.h>
  42#include <net/snmp.h>
  43#include <net/ipv6.h>
  44#include <net/ndisc.h>
  45#include <net/protocol.h>
  46#include <net/transp_v6.h>
  47#include <net/ip6_route.h>
  48#include <net/addrconf.h>
  49#include <net/inet_common.h>
  50#include <net/tcp.h>
  51#include <net/udp.h>
  52#include <net/udplite.h>
  53#include <net/xfrm.h>
  54#include <net/compat.h>
 
  55
  56#include <asm/uaccess.h>
  57
  58struct ip6_ra_chain *ip6_ra_chain;
  59DEFINE_RWLOCK(ip6_ra_lock);
  60
 
 
  61int ip6_ra_control(struct sock *sk, int sel)
  62{
  63	struct ip6_ra_chain *ra, *new_ra, **rap;
  64
  65	/* RA packet may be delivered ONLY to IPPROTO_RAW socket */
  66	if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW)
  67		return -ENOPROTOOPT;
  68
  69	new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
 
 
  70
  71	write_lock_bh(&ip6_ra_lock);
  72	for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
  73		if (ra->sk == sk) {
  74			if (sel>=0) {
  75				write_unlock_bh(&ip6_ra_lock);
  76				kfree(new_ra);
  77				return -EADDRINUSE;
  78			}
  79
  80			*rap = ra->next;
  81			write_unlock_bh(&ip6_ra_lock);
  82
  83			sock_put(sk);
  84			kfree(ra);
  85			return 0;
  86		}
  87	}
  88	if (new_ra == NULL) {
  89		write_unlock_bh(&ip6_ra_lock);
  90		return -ENOBUFS;
  91	}
  92	new_ra->sk = sk;
  93	new_ra->sel = sel;
  94	new_ra->next = ra;
  95	*rap = new_ra;
  96	sock_hold(sk);
  97	write_unlock_bh(&ip6_ra_lock);
  98	return 0;
  99}
 100
 101static
 102struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
 103					   struct ipv6_txoptions *opt)
 104{
 105	if (inet_sk(sk)->is_icsk) {
 106		if (opt &&
 107		    !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
 108		    inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
 109			struct inet_connection_sock *icsk = inet_csk(sk);
 110			icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
 111			icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
 112		}
 113		opt = xchg(&inet6_sk(sk)->opt, opt);
 114	} else {
 115		spin_lock(&sk->sk_dst_lock);
 116		opt = xchg(&inet6_sk(sk)->opt, opt);
 117		spin_unlock(&sk->sk_dst_lock);
 118	}
 
 
 119	sk_dst_reset(sk);
 120
 121	return opt;
 122}
 123
 124static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
 125		    char __user *optval, unsigned int optlen)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 126{
 127	struct ipv6_pinfo *np = inet6_sk(sk);
 128	struct net *net = sock_net(sk);
 129	int val, valbool;
 130	int retv = -ENOPROTOOPT;
 
 131
 132	if (optval == NULL)
 133		val=0;
 134	else {
 135		if (optlen >= sizeof(int)) {
 136			if (get_user(val, (int __user *) optval))
 137				return -EFAULT;
 138		} else
 139			val = 0;
 140	}
 141
 142	valbool = (val!=0);
 143
 144	if (ip6_mroute_opt(optname))
 145		return ip6_mroute_setsockopt(sk, optname, optval, optlen);
 146
 147	lock_sock(sk);
 
 
 
 
 
 
 
 
 148
 149	switch (optname) {
 150
 151	case IPV6_ADDRFORM:
 152		if (optlen < sizeof(int))
 153			goto e_inval;
 154		if (val == PF_INET) {
 155			struct ipv6_txoptions *opt;
 156			struct sk_buff *pktopt;
 157
 158			if (sk->sk_type == SOCK_RAW)
 159				break;
 160
 161			if (sk->sk_protocol == IPPROTO_UDP ||
 162			    sk->sk_protocol == IPPROTO_UDPLITE) {
 163				struct udp_sock *up = udp_sk(sk);
 164				if (up->pending == AF_INET6) {
 165					retv = -EBUSY;
 166					break;
 167				}
 168			} else if (sk->sk_protocol != IPPROTO_TCP)
 
 
 
 
 
 169				break;
 
 170
 171			if (sk->sk_state != TCP_ESTABLISHED) {
 172				retv = -ENOTCONN;
 173				break;
 174			}
 175
 176			if (ipv6_only_sock(sk) ||
 177			    !ipv6_addr_v4mapped(&np->daddr)) {
 178				retv = -EADDRNOTAVAIL;
 179				break;
 180			}
 181
 182			fl6_free_socklist(sk);
 183			ipv6_sock_mc_close(sk);
 184
 185			/*
 186			 * Sock is moving from IPv6 to IPv4 (sk_prot), so
 187			 * remove it from the refcnt debug socks count in the
 188			 * original family...
 189			 */
 190			sk_refcnt_debug_dec(sk);
 191
 192			if (sk->sk_protocol == IPPROTO_TCP) {
 193				struct inet_connection_sock *icsk = inet_csk(sk);
 194				local_bh_disable();
 195				sock_prot_inuse_add(net, sk->sk_prot, -1);
 196				sock_prot_inuse_add(net, &tcp_prot, 1);
 197				local_bh_enable();
 198				sk->sk_prot = &tcp_prot;
 199				icsk->icsk_af_ops = &ipv4_specific;
 
 
 200				sk->sk_socket->ops = &inet_stream_ops;
 201				sk->sk_family = PF_INET;
 202				tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
 203			} else {
 204				struct proto *prot = &udp_prot;
 205
 206				if (sk->sk_protocol == IPPROTO_UDPLITE)
 207					prot = &udplite_prot;
 208				local_bh_disable();
 209				sock_prot_inuse_add(net, sk->sk_prot, -1);
 210				sock_prot_inuse_add(net, prot, 1);
 211				local_bh_enable();
 212				sk->sk_prot = prot;
 
 213				sk->sk_socket->ops = &inet_dgram_ops;
 214				sk->sk_family = PF_INET;
 215			}
 216			opt = xchg(&np->opt, NULL);
 217			if (opt)
 218				sock_kfree_s(sk, opt, opt->tot_len);
 219			pktopt = xchg(&np->pktoptions, NULL);
 220			kfree_skb(pktopt);
 221
 222			sk->sk_destruct = inet_sock_destruct;
 
 
 
 
 
 
 
 223			/*
 224			 * ... and add it to the refcnt debug socks count
 225			 * in the new family. -acme
 226			 */
 227			sk_refcnt_debug_inc(sk);
 228			module_put(THIS_MODULE);
 229			retv = 0;
 230			break;
 231		}
 232		goto e_inval;
 233
 234	case IPV6_V6ONLY:
 235		if (optlen < sizeof(int) ||
 236		    inet_sk(sk)->inet_num)
 237			goto e_inval;
 238		np->ipv6only = valbool;
 239		retv = 0;
 240		break;
 241
 242	case IPV6_RECVPKTINFO:
 243		if (optlen < sizeof(int))
 244			goto e_inval;
 245		np->rxopt.bits.rxinfo = valbool;
 246		retv = 0;
 247		break;
 248
 249	case IPV6_2292PKTINFO:
 250		if (optlen < sizeof(int))
 251			goto e_inval;
 252		np->rxopt.bits.rxoinfo = valbool;
 253		retv = 0;
 254		break;
 255
 256	case IPV6_RECVHOPLIMIT:
 257		if (optlen < sizeof(int))
 258			goto e_inval;
 259		np->rxopt.bits.rxhlim = valbool;
 260		retv = 0;
 261		break;
 262
 263	case IPV6_2292HOPLIMIT:
 264		if (optlen < sizeof(int))
 265			goto e_inval;
 266		np->rxopt.bits.rxohlim = valbool;
 267		retv = 0;
 268		break;
 269
 270	case IPV6_RECVRTHDR:
 271		if (optlen < sizeof(int))
 272			goto e_inval;
 273		np->rxopt.bits.srcrt = valbool;
 274		retv = 0;
 275		break;
 276
 277	case IPV6_2292RTHDR:
 278		if (optlen < sizeof(int))
 279			goto e_inval;
 280		np->rxopt.bits.osrcrt = valbool;
 281		retv = 0;
 282		break;
 283
 284	case IPV6_RECVHOPOPTS:
 285		if (optlen < sizeof(int))
 286			goto e_inval;
 287		np->rxopt.bits.hopopts = valbool;
 288		retv = 0;
 289		break;
 290
 291	case IPV6_2292HOPOPTS:
 292		if (optlen < sizeof(int))
 293			goto e_inval;
 294		np->rxopt.bits.ohopopts = valbool;
 295		retv = 0;
 296		break;
 297
 298	case IPV6_RECVDSTOPTS:
 299		if (optlen < sizeof(int))
 300			goto e_inval;
 301		np->rxopt.bits.dstopts = valbool;
 302		retv = 0;
 303		break;
 304
 305	case IPV6_2292DSTOPTS:
 306		if (optlen < sizeof(int))
 307			goto e_inval;
 308		np->rxopt.bits.odstopts = valbool;
 309		retv = 0;
 310		break;
 311
 312	case IPV6_TCLASS:
 313		if (optlen < sizeof(int))
 314			goto e_inval;
 315		if (val < -1 || val > 0xff)
 316			goto e_inval;
 317		/* RFC 3542, 6.5: default traffic class of 0x0 */
 318		if (val == -1)
 319			val = 0;
 320		np->tclass = val;
 
 
 
 
 
 
 
 321		retv = 0;
 322		break;
 323
 324	case IPV6_RECVTCLASS:
 325		if (optlen < sizeof(int))
 326			goto e_inval;
 327		np->rxopt.bits.rxtclass = valbool;
 328		retv = 0;
 329		break;
 330
 331	case IPV6_FLOWINFO:
 332		if (optlen < sizeof(int))
 333			goto e_inval;
 334		np->rxopt.bits.rxflow = valbool;
 335		retv = 0;
 336		break;
 337
 338	case IPV6_RECVPATHMTU:
 339		if (optlen < sizeof(int))
 340			goto e_inval;
 341		np->rxopt.bits.rxpmtu = valbool;
 342		retv = 0;
 343		break;
 344
 345	case IPV6_TRANSPARENT:
 346		if (!capable(CAP_NET_ADMIN)) {
 
 347			retv = -EPERM;
 348			break;
 349		}
 350		if (optlen < sizeof(int))
 351			goto e_inval;
 352		/* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
 353		inet_sk(sk)->transparent = valbool;
 354		retv = 0;
 355		break;
 356
 
 
 
 
 
 
 
 
 357	case IPV6_RECVORIGDSTADDR:
 358		if (optlen < sizeof(int))
 359			goto e_inval;
 360		np->rxopt.bits.rxorigdstaddr = valbool;
 361		retv = 0;
 362		break;
 363
 364	case IPV6_HOPOPTS:
 365	case IPV6_RTHDRDSTOPTS:
 366	case IPV6_RTHDR:
 367	case IPV6_DSTOPTS:
 368	{
 369		struct ipv6_txoptions *opt;
 370
 371		/* remove any sticky options header with a zero option
 372		 * length, per RFC3542.
 373		 */
 374		if (optlen == 0)
 375			optval = NULL;
 376		else if (optval == NULL)
 377			goto e_inval;
 378		else if (optlen < sizeof(struct ipv6_opt_hdr) ||
 379			 optlen & 0x7 || optlen > 8 * 255)
 380			goto e_inval;
 381
 382		/* hop-by-hop / destination options are privileged option */
 383		retv = -EPERM;
 384		if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
 385			break;
 386
 387		opt = ipv6_renew_options(sk, np->opt, optname,
 388					 (struct ipv6_opt_hdr __user *)optval,
 389					 optlen);
 390		if (IS_ERR(opt)) {
 391			retv = PTR_ERR(opt);
 392			break;
 393		}
 394
 395		/* routing header option needs extra check */
 396		retv = -EINVAL;
 397		if (optname == IPV6_RTHDR && opt && opt->srcrt) {
 398			struct ipv6_rt_hdr *rthdr = opt->srcrt;
 399			switch (rthdr->type) {
 400#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
 401			case IPV6_SRCRT_TYPE_2:
 402				if (rthdr->hdrlen != 2 ||
 403				    rthdr->segments_left != 1)
 404					goto sticky_done;
 405
 406				break;
 407#endif
 408			default:
 409				goto sticky_done;
 410			}
 411		}
 412
 413		retv = 0;
 414		opt = ipv6_update_options(sk, opt);
 415sticky_done:
 416		if (opt)
 417			sock_kfree_s(sk, opt, opt->tot_len);
 418		break;
 419	}
 420
 421	case IPV6_PKTINFO:
 422	{
 423		struct in6_pktinfo pkt;
 424
 425		if (optlen == 0)
 426			goto e_inval;
 427		else if (optlen < sizeof(struct in6_pktinfo) || optval == NULL)
 
 428			goto e_inval;
 429
 430		if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
 431				retv = -EFAULT;
 432				break;
 433		}
 434		if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
 435			goto e_inval;
 436
 437		np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
 438		ipv6_addr_copy(&np->sticky_pktinfo.ipi6_addr, &pkt.ipi6_addr);
 439		retv = 0;
 440		break;
 441	}
 442
 443	case IPV6_2292PKTOPTIONS:
 444	{
 445		struct ipv6_txoptions *opt = NULL;
 446		struct msghdr msg;
 447		struct flowi6 fl6;
 448		int junk;
 449
 450		memset(&fl6, 0, sizeof(fl6));
 451		fl6.flowi6_oif = sk->sk_bound_dev_if;
 452		fl6.flowi6_mark = sk->sk_mark;
 453
 454		if (optlen == 0)
 455			goto update;
 456
 457		/* 1K is probably excessive
 458		 * 1K is surely not enough, 2K per standard header is 16K.
 459		 */
 460		retv = -EINVAL;
 461		if (optlen > 64*1024)
 462			break;
 463
 464		opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
 465		retv = -ENOBUFS;
 466		if (opt == NULL)
 467			break;
 468
 469		memset(opt, 0, sizeof(*opt));
 
 470		opt->tot_len = sizeof(*opt) + optlen;
 471		retv = -EFAULT;
 472		if (copy_from_user(opt+1, optval, optlen))
 473			goto done;
 474
 475		msg.msg_controllen = optlen;
 476		msg.msg_control = (void*)(opt+1);
 
 477
 478		retv = datagram_send_ctl(net, sk, &msg, &fl6, opt, &junk, &junk,
 479					 &junk);
 480		if (retv)
 481			goto done;
 482update:
 483		retv = 0;
 484		opt = ipv6_update_options(sk, opt);
 485done:
 486		if (opt)
 487			sock_kfree_s(sk, opt, opt->tot_len);
 
 
 488		break;
 489	}
 490	case IPV6_UNICAST_HOPS:
 491		if (optlen < sizeof(int))
 492			goto e_inval;
 493		if (val > 255 || val < -1)
 494			goto e_inval;
 495		np->hop_limit = val;
 496		retv = 0;
 497		break;
 498
 499	case IPV6_MULTICAST_HOPS:
 500		if (sk->sk_type == SOCK_STREAM)
 501			break;
 502		if (optlen < sizeof(int))
 503			goto e_inval;
 504		if (val > 255 || val < -1)
 505			goto e_inval;
 506		np->mcast_hops = val;
 507		retv = 0;
 508		break;
 509
 510	case IPV6_MULTICAST_LOOP:
 511		if (optlen < sizeof(int))
 512			goto e_inval;
 513		if (val != valbool)
 514			goto e_inval;
 515		np->mc_loop = valbool;
 516		retv = 0;
 517		break;
 518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 519	case IPV6_MULTICAST_IF:
 520		if (sk->sk_type == SOCK_STREAM)
 521			break;
 522		if (optlen < sizeof(int))
 523			goto e_inval;
 524
 525		if (val) {
 526			struct net_device *dev;
 
 527
 528			if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
 529				goto e_inval;
 530
 531			dev = dev_get_by_index(net, val);
 532			if (!dev) {
 
 533				retv = -ENODEV;
 534				break;
 535			}
 536			dev_put(dev);
 
 
 
 
 
 
 
 537		}
 538		np->mcast_oif = val;
 539		retv = 0;
 540		break;
 541	case IPV6_ADD_MEMBERSHIP:
 542	case IPV6_DROP_MEMBERSHIP:
 543	{
 544		struct ipv6_mreq mreq;
 545
 546		if (optlen < sizeof(struct ipv6_mreq))
 547			goto e_inval;
 548
 549		retv = -EPROTO;
 550		if (inet_sk(sk)->is_icsk)
 551			break;
 552
 553		retv = -EFAULT;
 554		if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
 555			break;
 556
 557		if (optname == IPV6_ADD_MEMBERSHIP)
 558			retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
 559		else
 560			retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
 561		break;
 562	}
 563	case IPV6_JOIN_ANYCAST:
 564	case IPV6_LEAVE_ANYCAST:
 565	{
 566		struct ipv6_mreq mreq;
 567
 568		if (optlen < sizeof(struct ipv6_mreq))
 569			goto e_inval;
 570
 571		retv = -EFAULT;
 572		if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
 573			break;
 574
 575		if (optname == IPV6_JOIN_ANYCAST)
 576			retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
 577		else
 578			retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
 579		break;
 580	}
 581	case MCAST_JOIN_GROUP:
 582	case MCAST_LEAVE_GROUP:
 583	{
 584		struct group_req greq;
 585		struct sockaddr_in6 *psin6;
 586
 587		if (optlen < sizeof(struct group_req))
 588			goto e_inval;
 
 
 
 589
 590		retv = -EFAULT;
 591		if (copy_from_user(&greq, optval, sizeof(struct group_req)))
 592			break;
 593		if (greq.gr_group.ss_family != AF_INET6) {
 594			retv = -EADDRNOTAVAIL;
 595			break;
 596		}
 597		psin6 = (struct sockaddr_in6 *)&greq.gr_group;
 598		if (optname == MCAST_JOIN_GROUP)
 599			retv = ipv6_sock_mc_join(sk, greq.gr_interface,
 600				&psin6->sin6_addr);
 601		else
 602			retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
 603				&psin6->sin6_addr);
 604		break;
 605	}
 606	case MCAST_JOIN_SOURCE_GROUP:
 607	case MCAST_LEAVE_SOURCE_GROUP:
 608	case MCAST_BLOCK_SOURCE:
 609	case MCAST_UNBLOCK_SOURCE:
 610	{
 611		struct group_source_req greqs;
 612		int omode, add;
 613
 614		if (optlen < sizeof(struct group_source_req))
 615			goto e_inval;
 616		if (copy_from_user(&greqs, optval, sizeof(greqs))) {
 617			retv = -EFAULT;
 618			break;
 619		}
 620		if (greqs.gsr_group.ss_family != AF_INET6 ||
 621		    greqs.gsr_source.ss_family != AF_INET6) {
 622			retv = -EADDRNOTAVAIL;
 623			break;
 624		}
 625		if (optname == MCAST_BLOCK_SOURCE) {
 626			omode = MCAST_EXCLUDE;
 627			add = 1;
 628		} else if (optname == MCAST_UNBLOCK_SOURCE) {
 629			omode = MCAST_EXCLUDE;
 630			add = 0;
 631		} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
 632			struct sockaddr_in6 *psin6;
 633
 634			psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
 635			retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
 636				&psin6->sin6_addr);
 637			/* prior join w/ different source is ok */
 638			if (retv && retv != -EADDRINUSE)
 639				break;
 640			omode = MCAST_INCLUDE;
 641			add = 1;
 642		} else /* MCAST_LEAVE_SOURCE_GROUP */ {
 643			omode = MCAST_INCLUDE;
 644			add = 0;
 645		}
 646		retv = ip6_mc_source(add, omode, sk, &greqs);
 647		break;
 648	}
 649	case MCAST_MSFILTER:
 650	{
 651		extern int sysctl_mld_max_msf;
 652		struct group_filter *gsf;
 653
 654		if (optlen < GROUP_FILTER_SIZE(0))
 655			goto e_inval;
 656		if (optlen > sysctl_optmem_max) {
 657			retv = -ENOBUFS;
 658			break;
 659		}
 660		gsf = kmalloc(optlen,GFP_KERNEL);
 661		if (!gsf) {
 662			retv = -ENOBUFS;
 663			break;
 664		}
 665		retv = -EFAULT;
 666		if (copy_from_user(gsf, optval, optlen)) {
 667			kfree(gsf);
 668			break;
 669		}
 670		/* numsrc >= (4G-140)/128 overflow in 32 bits */
 671		if (gsf->gf_numsrc >= 0x1ffffffU ||
 672		    gsf->gf_numsrc > sysctl_mld_max_msf) {
 673			kfree(gsf);
 674			retv = -ENOBUFS;
 675			break;
 676		}
 677		if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
 678			kfree(gsf);
 679			retv = -EINVAL;
 680			break;
 681		}
 682		retv = ip6_mc_msfilter(sk, gsf);
 683		kfree(gsf);
 684
 685		break;
 686	}
 687	case IPV6_ROUTER_ALERT:
 688		if (optlen < sizeof(int))
 689			goto e_inval;
 690		retv = ip6_ra_control(sk, val);
 691		break;
 
 
 
 
 
 
 692	case IPV6_MTU_DISCOVER:
 693		if (optlen < sizeof(int))
 694			goto e_inval;
 695		if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE)
 696			goto e_inval;
 697		np->pmtudisc = val;
 698		retv = 0;
 699		break;
 700	case IPV6_MTU:
 701		if (optlen < sizeof(int))
 702			goto e_inval;
 703		if (val && val < IPV6_MIN_MTU)
 704			goto e_inval;
 705		np->frag_size = val;
 706		retv = 0;
 707		break;
 708	case IPV6_RECVERR:
 709		if (optlen < sizeof(int))
 710			goto e_inval;
 711		np->recverr = valbool;
 712		if (!val)
 713			skb_queue_purge(&sk->sk_error_queue);
 714		retv = 0;
 715		break;
 716	case IPV6_FLOWINFO_SEND:
 717		if (optlen < sizeof(int))
 718			goto e_inval;
 719		np->sndflow = valbool;
 720		retv = 0;
 721		break;
 722	case IPV6_FLOWLABEL_MGR:
 723		retv = ipv6_flowlabel_opt(sk, optval, optlen);
 724		break;
 725	case IPV6_IPSEC_POLICY:
 726	case IPV6_XFRM_POLICY:
 727		retv = -EPERM;
 728		if (!capable(CAP_NET_ADMIN))
 729			break;
 730		retv = xfrm_user_policy(sk, optname, optval, optlen);
 731		break;
 732
 733	case IPV6_ADDR_PREFERENCES:
 734	    {
 735		unsigned int pref = 0;
 736		unsigned int prefmask = ~0;
 737
 738		if (optlen < sizeof(int))
 739			goto e_inval;
 740
 741		retv = -EINVAL;
 742
 743		/* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
 744		switch (val & (IPV6_PREFER_SRC_PUBLIC|
 745			       IPV6_PREFER_SRC_TMP|
 746			       IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
 747		case IPV6_PREFER_SRC_PUBLIC:
 748			pref |= IPV6_PREFER_SRC_PUBLIC;
 749			break;
 750		case IPV6_PREFER_SRC_TMP:
 751			pref |= IPV6_PREFER_SRC_TMP;
 752			break;
 753		case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
 754			break;
 755		case 0:
 756			goto pref_skip_pubtmp;
 757		default:
 758			goto e_inval;
 759		}
 760
 761		prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
 762			      IPV6_PREFER_SRC_TMP);
 763pref_skip_pubtmp:
 764
 765		/* check HOME/COA conflicts */
 766		switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
 767		case IPV6_PREFER_SRC_HOME:
 768			break;
 769		case IPV6_PREFER_SRC_COA:
 770			pref |= IPV6_PREFER_SRC_COA;
 771		case 0:
 772			goto pref_skip_coa;
 773		default:
 774			goto e_inval;
 775		}
 776
 777		prefmask &= ~IPV6_PREFER_SRC_COA;
 778pref_skip_coa:
 779
 780		/* check CGA/NONCGA conflicts */
 781		switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
 782		case IPV6_PREFER_SRC_CGA:
 783		case IPV6_PREFER_SRC_NONCGA:
 784		case 0:
 785			break;
 786		default:
 787			goto e_inval;
 788		}
 789
 790		np->srcprefs = (np->srcprefs & prefmask) | pref;
 791		retv = 0;
 792
 793		break;
 794	    }
 795	case IPV6_MINHOPCOUNT:
 796		if (optlen < sizeof(int))
 797			goto e_inval;
 798		if (val < 0 || val > 255)
 799			goto e_inval;
 800		np->min_hopcount = val;
 
 
 
 
 
 
 
 
 801		break;
 802	case IPV6_DONTFRAG:
 803		np->dontfrag = valbool;
 804		retv = 0;
 805		break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 806	}
 807
 808	release_sock(sk);
 
 
 
 809
 810	return retv;
 811
 812e_inval:
 813	release_sock(sk);
 814	return -EINVAL;
 815}
 816
 817int ipv6_setsockopt(struct sock *sk, int level, int optname,
 818		    char __user *optval, unsigned int optlen)
 819{
 820	int err;
 821
 822	if (level == SOL_IP && sk->sk_type != SOCK_RAW)
 823		return udp_prot.setsockopt(sk, level, optname, optval, optlen);
 824
 825	if (level != SOL_IPV6)
 826		return -ENOPROTOOPT;
 827
 828	err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
 829#ifdef CONFIG_NETFILTER
 830	/* we need to exclude all possible ENOPROTOOPTs except default case */
 831	if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
 832			optname != IPV6_XFRM_POLICY) {
 833		lock_sock(sk);
 834		err = nf_setsockopt(sk, PF_INET6, optname, optval,
 835				optlen);
 836		release_sock(sk);
 837	}
 838#endif
 839	return err;
 840}
 841
 842EXPORT_SYMBOL(ipv6_setsockopt);
 843
 844#ifdef CONFIG_COMPAT
 845int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
 846			   char __user *optval, unsigned int optlen)
 847{
 848	int err;
 849
 850	if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
 851		if (udp_prot.compat_setsockopt != NULL)
 852			return udp_prot.compat_setsockopt(sk, level, optname,
 853							  optval, optlen);
 854		return udp_prot.setsockopt(sk, level, optname, optval, optlen);
 855	}
 856
 857	if (level != SOL_IPV6)
 858		return -ENOPROTOOPT;
 859
 860	if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
 861		return compat_mc_setsockopt(sk, level, optname, optval, optlen,
 862			ipv6_setsockopt);
 863
 864	err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
 865#ifdef CONFIG_NETFILTER
 866	/* we need to exclude all possible ENOPROTOOPTs except default case */
 867	if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
 868	    optname != IPV6_XFRM_POLICY) {
 869		lock_sock(sk);
 870		err = compat_nf_setsockopt(sk, PF_INET6, optname,
 871					   optval, optlen);
 872		release_sock(sk);
 873	}
 874#endif
 875	return err;
 876}
 877
 878EXPORT_SYMBOL(compat_ipv6_setsockopt);
 879#endif
 880
 881static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
 882				  int optname, char __user *optval, int len)
 883{
 884	struct ipv6_opt_hdr *hdr;
 885
 886	if (!opt)
 887		return 0;
 888
 889	switch(optname) {
 890	case IPV6_HOPOPTS:
 891		hdr = opt->hopopt;
 892		break;
 893	case IPV6_RTHDRDSTOPTS:
 894		hdr = opt->dst0opt;
 895		break;
 896	case IPV6_RTHDR:
 897		hdr = (struct ipv6_opt_hdr *)opt->srcrt;
 898		break;
 899	case IPV6_DSTOPTS:
 900		hdr = opt->dst1opt;
 901		break;
 902	default:
 903		return -EINVAL;	/* should not happen */
 904	}
 905
 906	if (!hdr)
 907		return 0;
 908
 909	len = min_t(unsigned int, len, ipv6_optlen(hdr));
 910	if (copy_to_user(optval, hdr, len))
 911		return -EFAULT;
 912	return len;
 913}
 914
 915static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
 916		    char __user *optval, int __user *optlen, unsigned flags)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 917{
 918	struct ipv6_pinfo *np = inet6_sk(sk);
 919	int len;
 920	int val;
 921
 922	if (ip6_mroute_opt(optname))
 923		return ip6_mroute_getsockopt(sk, optname, optval, optlen);
 924
 925	if (get_user(len, optlen))
 926		return -EFAULT;
 927	switch (optname) {
 928	case IPV6_ADDRFORM:
 929		if (sk->sk_protocol != IPPROTO_UDP &&
 930		    sk->sk_protocol != IPPROTO_UDPLITE &&
 931		    sk->sk_protocol != IPPROTO_TCP)
 932			return -ENOPROTOOPT;
 933		if (sk->sk_state != TCP_ESTABLISHED)
 934			return -ENOTCONN;
 935		val = sk->sk_family;
 936		break;
 937	case MCAST_MSFILTER:
 938	{
 939		struct group_filter gsf;
 940		int err;
 941
 942		if (len < GROUP_FILTER_SIZE(0))
 943			return -EINVAL;
 944		if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
 945			return -EFAULT;
 946		if (gsf.gf_group.ss_family != AF_INET6)
 947			return -EADDRNOTAVAIL;
 948		lock_sock(sk);
 949		err = ip6_mc_msfget(sk, &gsf,
 950			(struct group_filter __user *)optval, optlen);
 951		release_sock(sk);
 952		return err;
 953	}
 954
 955	case IPV6_2292PKTOPTIONS:
 956	{
 957		struct msghdr msg;
 958		struct sk_buff *skb;
 959
 960		if (sk->sk_type != SOCK_STREAM)
 961			return -ENOPROTOOPT;
 962
 963		msg.msg_control = optval;
 
 
 
 
 
 
 964		msg.msg_controllen = len;
 965		msg.msg_flags = flags;
 966
 967		lock_sock(sk);
 968		skb = np->pktoptions;
 969		if (skb)
 970			atomic_inc(&skb->users);
 971		release_sock(sk);
 972
 973		if (skb) {
 974			int err = datagram_recv_ctl(sk, &msg, skb);
 975			kfree_skb(skb);
 976			if (err)
 977				return err;
 978		} else {
 979			if (np->rxopt.bits.rxinfo) {
 980				struct in6_pktinfo src_info;
 981				src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
 982					np->sticky_pktinfo.ipi6_ifindex;
 983				np->mcast_oif? ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr) :
 984					ipv6_addr_copy(&src_info.ipi6_addr, &(np->sticky_pktinfo.ipi6_addr));
 985				put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
 986			}
 987			if (np->rxopt.bits.rxhlim) {
 988				int hlim = np->mcast_hops;
 989				put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
 990			}
 
 
 
 
 
 991			if (np->rxopt.bits.rxoinfo) {
 992				struct in6_pktinfo src_info;
 993				src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
 994					np->sticky_pktinfo.ipi6_ifindex;
 995				np->mcast_oif? ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr) :
 996					ipv6_addr_copy(&src_info.ipi6_addr, &(np->sticky_pktinfo.ipi6_addr));
 997				put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
 998			}
 999			if (np->rxopt.bits.rxohlim) {
1000				int hlim = np->mcast_hops;
1001				put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1002			}
 
 
 
 
 
1003		}
1004		len -= msg.msg_controllen;
1005		return put_user(len, optlen);
1006	}
1007	case IPV6_MTU:
1008	{
1009		struct dst_entry *dst;
1010
1011		val = 0;
1012		rcu_read_lock();
1013		dst = __sk_dst_get(sk);
1014		if (dst)
1015			val = dst_mtu(dst);
1016		rcu_read_unlock();
1017		if (!val)
1018			return -ENOTCONN;
1019		break;
1020	}
1021
1022	case IPV6_V6ONLY:
1023		val = np->ipv6only;
1024		break;
1025
1026	case IPV6_RECVPKTINFO:
1027		val = np->rxopt.bits.rxinfo;
1028		break;
1029
1030	case IPV6_2292PKTINFO:
1031		val = np->rxopt.bits.rxoinfo;
1032		break;
1033
1034	case IPV6_RECVHOPLIMIT:
1035		val = np->rxopt.bits.rxhlim;
1036		break;
1037
1038	case IPV6_2292HOPLIMIT:
1039		val = np->rxopt.bits.rxohlim;
1040		break;
1041
1042	case IPV6_RECVRTHDR:
1043		val = np->rxopt.bits.srcrt;
1044		break;
1045
1046	case IPV6_2292RTHDR:
1047		val = np->rxopt.bits.osrcrt;
1048		break;
1049
1050	case IPV6_HOPOPTS:
1051	case IPV6_RTHDRDSTOPTS:
1052	case IPV6_RTHDR:
1053	case IPV6_DSTOPTS:
1054	{
 
1055
1056		lock_sock(sk);
1057		len = ipv6_getsockopt_sticky(sk, np->opt,
1058					     optname, optval, len);
1059		release_sock(sk);
 
1060		/* check if ipv6_getsockopt_sticky() returns err code */
1061		if (len < 0)
1062			return len;
1063		return put_user(len, optlen);
1064	}
1065
1066	case IPV6_RECVHOPOPTS:
1067		val = np->rxopt.bits.hopopts;
1068		break;
1069
1070	case IPV6_2292HOPOPTS:
1071		val = np->rxopt.bits.ohopopts;
1072		break;
1073
1074	case IPV6_RECVDSTOPTS:
1075		val = np->rxopt.bits.dstopts;
1076		break;
1077
1078	case IPV6_2292DSTOPTS:
1079		val = np->rxopt.bits.odstopts;
1080		break;
1081
1082	case IPV6_TCLASS:
1083		val = np->tclass;
1084		break;
1085
1086	case IPV6_RECVTCLASS:
1087		val = np->rxopt.bits.rxtclass;
1088		break;
1089
1090	case IPV6_FLOWINFO:
1091		val = np->rxopt.bits.rxflow;
1092		break;
1093
1094	case IPV6_RECVPATHMTU:
1095		val = np->rxopt.bits.rxpmtu;
1096		break;
1097
1098	case IPV6_PATHMTU:
1099	{
1100		struct dst_entry *dst;
1101		struct ip6_mtuinfo mtuinfo;
1102
1103		if (len < sizeof(mtuinfo))
1104			return -EINVAL;
1105
1106		len = sizeof(mtuinfo);
1107		memset(&mtuinfo, 0, sizeof(mtuinfo));
1108
1109		rcu_read_lock();
1110		dst = __sk_dst_get(sk);
1111		if (dst)
1112			mtuinfo.ip6m_mtu = dst_mtu(dst);
1113		rcu_read_unlock();
1114		if (!mtuinfo.ip6m_mtu)
1115			return -ENOTCONN;
1116
1117		if (put_user(len, optlen))
1118			return -EFAULT;
1119		if (copy_to_user(optval, &mtuinfo, len))
1120			return -EFAULT;
1121
1122		return 0;
1123		break;
1124	}
1125
1126	case IPV6_TRANSPARENT:
1127		val = inet_sk(sk)->transparent;
1128		break;
1129
 
 
 
 
1130	case IPV6_RECVORIGDSTADDR:
1131		val = np->rxopt.bits.rxorigdstaddr;
1132		break;
1133
1134	case IPV6_UNICAST_HOPS:
1135	case IPV6_MULTICAST_HOPS:
1136	{
1137		struct dst_entry *dst;
1138
1139		if (optname == IPV6_UNICAST_HOPS)
1140			val = np->hop_limit;
1141		else
1142			val = np->mcast_hops;
1143
1144		if (val < 0) {
1145			rcu_read_lock();
1146			dst = __sk_dst_get(sk);
1147			if (dst)
1148				val = ip6_dst_hoplimit(dst);
1149			rcu_read_unlock();
1150		}
1151
1152		if (val < 0)
1153			val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1154		break;
1155	}
1156
1157	case IPV6_MULTICAST_LOOP:
1158		val = np->mc_loop;
1159		break;
1160
1161	case IPV6_MULTICAST_IF:
1162		val = np->mcast_oif;
1163		break;
1164
 
 
 
 
 
 
 
 
1165	case IPV6_MTU_DISCOVER:
1166		val = np->pmtudisc;
1167		break;
1168
1169	case IPV6_RECVERR:
1170		val = np->recverr;
1171		break;
1172
1173	case IPV6_FLOWINFO_SEND:
1174		val = np->sndflow;
1175		break;
1176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1177	case IPV6_ADDR_PREFERENCES:
1178		val = 0;
1179
1180		if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1181			val |= IPV6_PREFER_SRC_TMP;
1182		else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1183			val |= IPV6_PREFER_SRC_PUBLIC;
1184		else {
1185			/* XXX: should we return system default? */
1186			val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1187		}
1188
1189		if (np->srcprefs & IPV6_PREFER_SRC_COA)
1190			val |= IPV6_PREFER_SRC_COA;
1191		else
1192			val |= IPV6_PREFER_SRC_HOME;
1193		break;
1194
1195	case IPV6_MINHOPCOUNT:
1196		val = np->min_hopcount;
1197		break;
1198
1199	case IPV6_DONTFRAG:
1200		val = np->dontfrag;
1201		break;
1202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1203	default:
1204		return -ENOPROTOOPT;
1205	}
1206	len = min_t(unsigned int, sizeof(int), len);
1207	if(put_user(len, optlen))
1208		return -EFAULT;
1209	if(copy_to_user(optval,&val,len))
1210		return -EFAULT;
1211	return 0;
1212}
1213
1214int ipv6_getsockopt(struct sock *sk, int level, int optname,
1215		    char __user *optval, int __user *optlen)
1216{
1217	int err;
1218
1219	if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1220		return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1221
1222	if(level != SOL_IPV6)
1223		return -ENOPROTOOPT;
1224
1225	err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
1226#ifdef CONFIG_NETFILTER
1227	/* we need to exclude all possible ENOPROTOOPTs except default case */
1228	if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1229		int len;
1230
1231		if (get_user(len, optlen))
1232			return -EFAULT;
1233
1234		lock_sock(sk);
1235		err = nf_getsockopt(sk, PF_INET6, optname, optval,
1236				&len);
1237		release_sock(sk);
1238		if (err >= 0)
1239			err = put_user(len, optlen);
1240	}
1241#endif
1242	return err;
1243}
1244
1245EXPORT_SYMBOL(ipv6_getsockopt);
1246
1247#ifdef CONFIG_COMPAT
1248int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
1249			   char __user *optval, int __user *optlen)
1250{
1251	int err;
1252
1253	if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
1254		if (udp_prot.compat_getsockopt != NULL)
1255			return udp_prot.compat_getsockopt(sk, level, optname,
1256							  optval, optlen);
1257		return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1258	}
1259
1260	if (level != SOL_IPV6)
1261		return -ENOPROTOOPT;
1262
1263	if (optname == MCAST_MSFILTER)
1264		return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1265			ipv6_getsockopt);
1266
1267	err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1268				 MSG_CMSG_COMPAT);
1269#ifdef CONFIG_NETFILTER
1270	/* we need to exclude all possible ENOPROTOOPTs except default case */
1271	if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1272		int len;
1273
1274		if (get_user(len, optlen))
1275			return -EFAULT;
1276
1277		lock_sock(sk);
1278		err = compat_nf_getsockopt(sk, PF_INET6,
1279					   optname, optval, &len);
1280		release_sock(sk);
1281		if (err >= 0)
1282			err = put_user(len, optlen);
1283	}
1284#endif
1285	return err;
1286}
1287
1288EXPORT_SYMBOL(compat_ipv6_getsockopt);
1289#endif
1290
v6.2
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *	IPv6 BSD socket options interface
   4 *	Linux INET6 implementation
   5 *
   6 *	Authors:
   7 *	Pedro Roque		<roque@di.fc.ul.pt>
   8 *
   9 *	Based on linux/net/ipv4/ip_sockglue.c
  10 *
 
 
 
 
 
  11 *	FIXME: Make the setsockopt code POSIX compliant: That is
  12 *
  13 *	o	Truncate getsockopt returns
  14 *	o	Return an optlen of the truncated length if need be
  15 *
  16 *	Changes:
  17 *	David L Stevens <dlstevens@us.ibm.com>:
  18 *		- added multicast source filtering API for MLDv2
  19 */
  20
  21#include <linux/module.h>
  22#include <linux/capability.h>
  23#include <linux/errno.h>
  24#include <linux/types.h>
  25#include <linux/socket.h>
  26#include <linux/sockios.h>
  27#include <linux/net.h>
  28#include <linux/in6.h>
  29#include <linux/mroute6.h>
  30#include <linux/netdevice.h>
  31#include <linux/if_arp.h>
  32#include <linux/init.h>
  33#include <linux/sysctl.h>
  34#include <linux/netfilter.h>
  35#include <linux/slab.h>
  36
  37#include <net/sock.h>
  38#include <net/snmp.h>
  39#include <net/ipv6.h>
  40#include <net/ndisc.h>
  41#include <net/protocol.h>
  42#include <net/transp_v6.h>
  43#include <net/ip6_route.h>
  44#include <net/addrconf.h>
  45#include <net/inet_common.h>
  46#include <net/tcp.h>
  47#include <net/udp.h>
  48#include <net/udplite.h>
  49#include <net/xfrm.h>
  50#include <net/compat.h>
  51#include <net/seg6.h>
  52
  53#include <linux/uaccess.h>
  54
  55struct ip6_ra_chain *ip6_ra_chain;
  56DEFINE_RWLOCK(ip6_ra_lock);
  57
  58DEFINE_STATIC_KEY_FALSE(ip6_min_hopcount);
  59
  60int ip6_ra_control(struct sock *sk, int sel)
  61{
  62	struct ip6_ra_chain *ra, *new_ra, **rap;
  63
  64	/* RA packet may be delivered ONLY to IPPROTO_RAW socket */
  65	if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW)
  66		return -ENOPROTOOPT;
  67
  68	new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
  69	if (sel >= 0 && !new_ra)
  70		return -ENOMEM;
  71
  72	write_lock_bh(&ip6_ra_lock);
  73	for (rap = &ip6_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
  74		if (ra->sk == sk) {
  75			if (sel >= 0) {
  76				write_unlock_bh(&ip6_ra_lock);
  77				kfree(new_ra);
  78				return -EADDRINUSE;
  79			}
  80
  81			*rap = ra->next;
  82			write_unlock_bh(&ip6_ra_lock);
  83
  84			sock_put(sk);
  85			kfree(ra);
  86			return 0;
  87		}
  88	}
  89	if (!new_ra) {
  90		write_unlock_bh(&ip6_ra_lock);
  91		return -ENOBUFS;
  92	}
  93	new_ra->sk = sk;
  94	new_ra->sel = sel;
  95	new_ra->next = ra;
  96	*rap = new_ra;
  97	sock_hold(sk);
  98	write_unlock_bh(&ip6_ra_lock);
  99	return 0;
 100}
 101
 
 102struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
 103					   struct ipv6_txoptions *opt)
 104{
 105	if (inet_sk(sk)->is_icsk) {
 106		if (opt &&
 107		    !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
 108		    inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
 109			struct inet_connection_sock *icsk = inet_csk(sk);
 110			icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
 111			icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
 112		}
 
 
 
 
 
 113	}
 114	opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt,
 115		   opt);
 116	sk_dst_reset(sk);
 117
 118	return opt;
 119}
 120
 121static bool setsockopt_needs_rtnl(int optname)
 122{
 123	switch (optname) {
 124	case IPV6_ADDRFORM:
 125	case IPV6_ADD_MEMBERSHIP:
 126	case IPV6_DROP_MEMBERSHIP:
 127	case IPV6_JOIN_ANYCAST:
 128	case IPV6_LEAVE_ANYCAST:
 129	case MCAST_JOIN_GROUP:
 130	case MCAST_LEAVE_GROUP:
 131	case MCAST_JOIN_SOURCE_GROUP:
 132	case MCAST_LEAVE_SOURCE_GROUP:
 133	case MCAST_BLOCK_SOURCE:
 134	case MCAST_UNBLOCK_SOURCE:
 135	case MCAST_MSFILTER:
 136		return true;
 137	}
 138	return false;
 139}
 140
 141static int copy_group_source_from_sockptr(struct group_source_req *greqs,
 142		sockptr_t optval, int optlen)
 143{
 144	if (in_compat_syscall()) {
 145		struct compat_group_source_req gr32;
 146
 147		if (optlen < sizeof(gr32))
 148			return -EINVAL;
 149		if (copy_from_sockptr(&gr32, optval, sizeof(gr32)))
 150			return -EFAULT;
 151		greqs->gsr_interface = gr32.gsr_interface;
 152		greqs->gsr_group = gr32.gsr_group;
 153		greqs->gsr_source = gr32.gsr_source;
 154	} else {
 155		if (optlen < sizeof(*greqs))
 156			return -EINVAL;
 157		if (copy_from_sockptr(greqs, optval, sizeof(*greqs)))
 158			return -EFAULT;
 159	}
 160
 161	return 0;
 162}
 163
 164static int do_ipv6_mcast_group_source(struct sock *sk, int optname,
 165		sockptr_t optval, int optlen)
 166{
 167	struct group_source_req greqs;
 168	int omode, add;
 169	int ret;
 170
 171	ret = copy_group_source_from_sockptr(&greqs, optval, optlen);
 172	if (ret)
 173		return ret;
 174
 175	if (greqs.gsr_group.ss_family != AF_INET6 ||
 176	    greqs.gsr_source.ss_family != AF_INET6)
 177		return -EADDRNOTAVAIL;
 178
 179	if (optname == MCAST_BLOCK_SOURCE) {
 180		omode = MCAST_EXCLUDE;
 181		add = 1;
 182	} else if (optname == MCAST_UNBLOCK_SOURCE) {
 183		omode = MCAST_EXCLUDE;
 184		add = 0;
 185	} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
 186		struct sockaddr_in6 *psin6;
 187		int retv;
 188
 189		psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
 190		retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface,
 191					     &psin6->sin6_addr,
 192					     MCAST_INCLUDE);
 193		/* prior join w/ different source is ok */
 194		if (retv && retv != -EADDRINUSE)
 195			return retv;
 196		omode = MCAST_INCLUDE;
 197		add = 1;
 198	} else /* MCAST_LEAVE_SOURCE_GROUP */ {
 199		omode = MCAST_INCLUDE;
 200		add = 0;
 201	}
 202	return ip6_mc_source(add, omode, sk, &greqs);
 203}
 204
 205static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
 206		int optlen)
 207{
 208	struct group_filter *gsf;
 209	int ret;
 210
 211	if (optlen < GROUP_FILTER_SIZE(0))
 212		return -EINVAL;
 213	if (optlen > READ_ONCE(sysctl_optmem_max))
 214		return -ENOBUFS;
 215
 216	gsf = memdup_sockptr(optval, optlen);
 217	if (IS_ERR(gsf))
 218		return PTR_ERR(gsf);
 219
 220	/* numsrc >= (4G-140)/128 overflow in 32 bits */
 221	ret = -ENOBUFS;
 222	if (gsf->gf_numsrc >= 0x1ffffffU ||
 223	    gsf->gf_numsrc > sysctl_mld_max_msf)
 224		goto out_free_gsf;
 225
 226	ret = -EINVAL;
 227	if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen)
 228		goto out_free_gsf;
 229
 230	ret = ip6_mc_msfilter(sk, gsf, gsf->gf_slist_flex);
 231out_free_gsf:
 232	kfree(gsf);
 233	return ret;
 234}
 235
 236static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
 237		int optlen)
 238{
 239	const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
 240	struct compat_group_filter *gf32;
 241	void *p;
 242	int ret;
 243	int n;
 244
 245	if (optlen < size0)
 246		return -EINVAL;
 247	if (optlen > READ_ONCE(sysctl_optmem_max) - 4)
 248		return -ENOBUFS;
 249
 250	p = kmalloc(optlen + 4, GFP_KERNEL);
 251	if (!p)
 252		return -ENOMEM;
 253
 254	gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */
 255	ret = -EFAULT;
 256	if (copy_from_sockptr(gf32, optval, optlen))
 257		goto out_free_p;
 258
 259	/* numsrc >= (4G-140)/128 overflow in 32 bits */
 260	ret = -ENOBUFS;
 261	n = gf32->gf_numsrc;
 262	if (n >= 0x1ffffffU || n > sysctl_mld_max_msf)
 263		goto out_free_p;
 264
 265	ret = -EINVAL;
 266	if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen)
 267		goto out_free_p;
 268
 269	ret = ip6_mc_msfilter(sk, &(struct group_filter){
 270			.gf_interface = gf32->gf_interface,
 271			.gf_group = gf32->gf_group,
 272			.gf_fmode = gf32->gf_fmode,
 273			.gf_numsrc = gf32->gf_numsrc}, gf32->gf_slist_flex);
 274
 275out_free_p:
 276	kfree(p);
 277	return ret;
 278}
 279
 280static int ipv6_mcast_join_leave(struct sock *sk, int optname,
 281		sockptr_t optval, int optlen)
 282{
 283	struct sockaddr_in6 *psin6;
 284	struct group_req greq;
 285
 286	if (optlen < sizeof(greq))
 287		return -EINVAL;
 288	if (copy_from_sockptr(&greq, optval, sizeof(greq)))
 289		return -EFAULT;
 290
 291	if (greq.gr_group.ss_family != AF_INET6)
 292		return -EADDRNOTAVAIL;
 293	psin6 = (struct sockaddr_in6 *)&greq.gr_group;
 294	if (optname == MCAST_JOIN_GROUP)
 295		return ipv6_sock_mc_join(sk, greq.gr_interface,
 296					 &psin6->sin6_addr);
 297	return ipv6_sock_mc_drop(sk, greq.gr_interface, &psin6->sin6_addr);
 298}
 299
 300static int compat_ipv6_mcast_join_leave(struct sock *sk, int optname,
 301		sockptr_t optval, int optlen)
 302{
 303	struct compat_group_req gr32;
 304	struct sockaddr_in6 *psin6;
 305
 306	if (optlen < sizeof(gr32))
 307		return -EINVAL;
 308	if (copy_from_sockptr(&gr32, optval, sizeof(gr32)))
 309		return -EFAULT;
 310
 311	if (gr32.gr_group.ss_family != AF_INET6)
 312		return -EADDRNOTAVAIL;
 313	psin6 = (struct sockaddr_in6 *)&gr32.gr_group;
 314	if (optname == MCAST_JOIN_GROUP)
 315		return ipv6_sock_mc_join(sk, gr32.gr_interface,
 316					&psin6->sin6_addr);
 317	return ipv6_sock_mc_drop(sk, gr32.gr_interface, &psin6->sin6_addr);
 318}
 319
 320static int ipv6_set_opt_hdr(struct sock *sk, int optname, sockptr_t optval,
 321		int optlen)
 322{
 323	struct ipv6_pinfo *np = inet6_sk(sk);
 324	struct ipv6_opt_hdr *new = NULL;
 325	struct net *net = sock_net(sk);
 326	struct ipv6_txoptions *opt;
 327	int err;
 328
 329	/* hop-by-hop / destination options are privileged option */
 330	if (optname != IPV6_RTHDR && !sockopt_ns_capable(net->user_ns, CAP_NET_RAW))
 331		return -EPERM;
 332
 333	/* remove any sticky options header with a zero option
 334	 * length, per RFC3542.
 335	 */
 336	if (optlen > 0) {
 337		if (sockptr_is_null(optval))
 338			return -EINVAL;
 339		if (optlen < sizeof(struct ipv6_opt_hdr) ||
 340		    optlen & 0x7 ||
 341		    optlen > 8 * 255)
 342			return -EINVAL;
 343
 344		new = memdup_sockptr(optval, optlen);
 345		if (IS_ERR(new))
 346			return PTR_ERR(new);
 347		if (unlikely(ipv6_optlen(new) > optlen)) {
 348			kfree(new);
 349			return -EINVAL;
 350		}
 351	}
 352
 353	opt = rcu_dereference_protected(np->opt, lockdep_sock_is_held(sk));
 354	opt = ipv6_renew_options(sk, opt, optname, new);
 355	kfree(new);
 356	if (IS_ERR(opt))
 357		return PTR_ERR(opt);
 358
 359	/* routing header option needs extra check */
 360	err = -EINVAL;
 361	if (optname == IPV6_RTHDR && opt && opt->srcrt) {
 362		struct ipv6_rt_hdr *rthdr = opt->srcrt;
 363		switch (rthdr->type) {
 364#if IS_ENABLED(CONFIG_IPV6_MIP6)
 365		case IPV6_SRCRT_TYPE_2:
 366			if (rthdr->hdrlen != 2 || rthdr->segments_left != 1)
 367				goto sticky_done;
 368			break;
 369#endif
 370		case IPV6_SRCRT_TYPE_4:
 371		{
 372			struct ipv6_sr_hdr *srh =
 373				(struct ipv6_sr_hdr *)opt->srcrt;
 374
 375			if (!seg6_validate_srh(srh, optlen, false))
 376				goto sticky_done;
 377			break;
 378		}
 379		default:
 380			goto sticky_done;
 381		}
 382	}
 383
 384	err = 0;
 385	opt = ipv6_update_options(sk, opt);
 386sticky_done:
 387	if (opt) {
 388		atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
 389		txopt_put(opt);
 390	}
 391	return err;
 392}
 393
 394int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
 395		       sockptr_t optval, unsigned int optlen)
 396{
 397	struct ipv6_pinfo *np = inet6_sk(sk);
 398	struct net *net = sock_net(sk);
 399	int val, valbool;
 400	int retv = -ENOPROTOOPT;
 401	bool needs_rtnl = setsockopt_needs_rtnl(optname);
 402
 403	if (sockptr_is_null(optval))
 404		val = 0;
 405	else {
 406		if (optlen >= sizeof(int)) {
 407			if (copy_from_sockptr(&val, optval, sizeof(val)))
 408				return -EFAULT;
 409		} else
 410			val = 0;
 411	}
 412
 413	valbool = (val != 0);
 414
 415	if (ip6_mroute_opt(optname))
 416		return ip6_mroute_setsockopt(sk, optname, optval, optlen);
 417
 418	if (needs_rtnl)
 419		rtnl_lock();
 420	sockopt_lock_sock(sk);
 421
 422	/* Another thread has converted the socket into IPv4 with
 423	 * IPV6_ADDRFORM concurrently.
 424	 */
 425	if (unlikely(sk->sk_family != AF_INET6))
 426		goto unlock;
 427
 428	switch (optname) {
 429
 430	case IPV6_ADDRFORM:
 431		if (optlen < sizeof(int))
 432			goto e_inval;
 433		if (val == PF_INET) {
 
 
 
 434			if (sk->sk_type == SOCK_RAW)
 435				break;
 436
 437			if (sk->sk_protocol == IPPROTO_UDP ||
 438			    sk->sk_protocol == IPPROTO_UDPLITE) {
 439				struct udp_sock *up = udp_sk(sk);
 440				if (up->pending == AF_INET6) {
 441					retv = -EBUSY;
 442					break;
 443				}
 444			} else if (sk->sk_protocol == IPPROTO_TCP) {
 445				if (sk->sk_prot != &tcpv6_prot) {
 446					retv = -EBUSY;
 447					break;
 448				}
 449			} else {
 450				break;
 451			}
 452
 453			if (sk->sk_state != TCP_ESTABLISHED) {
 454				retv = -ENOTCONN;
 455				break;
 456			}
 457
 458			if (ipv6_only_sock(sk) ||
 459			    !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
 460				retv = -EADDRNOTAVAIL;
 461				break;
 462			}
 463
 464			__ipv6_sock_mc_close(sk);
 465			__ipv6_sock_ac_close(sk);
 466
 467			/*
 468			 * Sock is moving from IPv6 to IPv4 (sk_prot), so
 469			 * remove it from the refcnt debug socks count in the
 470			 * original family...
 471			 */
 472			sk_refcnt_debug_dec(sk);
 473
 474			if (sk->sk_protocol == IPPROTO_TCP) {
 475				struct inet_connection_sock *icsk = inet_csk(sk);
 476
 477				sock_prot_inuse_add(net, sk->sk_prot, -1);
 478				sock_prot_inuse_add(net, &tcp_prot, 1);
 479
 480				/* Paired with READ_ONCE(sk->sk_prot) in inet6_stream_ops */
 481				WRITE_ONCE(sk->sk_prot, &tcp_prot);
 482				/* Paired with READ_ONCE() in tcp_(get|set)sockopt() */
 483				WRITE_ONCE(icsk->icsk_af_ops, &ipv4_specific);
 484				sk->sk_socket->ops = &inet_stream_ops;
 485				sk->sk_family = PF_INET;
 486				tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
 487			} else {
 488				struct proto *prot = &udp_prot;
 489
 490				if (sk->sk_protocol == IPPROTO_UDPLITE)
 491					prot = &udplite_prot;
 492
 493				sock_prot_inuse_add(net, sk->sk_prot, -1);
 494				sock_prot_inuse_add(net, prot, 1);
 495
 496				/* Paired with READ_ONCE(sk->sk_prot) in inet6_dgram_ops */
 497				WRITE_ONCE(sk->sk_prot, prot);
 498				sk->sk_socket->ops = &inet_dgram_ops;
 499				sk->sk_family = PF_INET;
 500			}
 
 
 
 
 
 501
 502			/* Disable all options not to allocate memory anymore,
 503			 * but there is still a race.  See the lockless path
 504			 * in udpv6_sendmsg() and ipv6_local_rxpmtu().
 505			 */
 506			np->rxopt.all = 0;
 507
 508			inet6_cleanup_sock(sk);
 509
 510			/*
 511			 * ... and add it to the refcnt debug socks count
 512			 * in the new family. -acme
 513			 */
 514			sk_refcnt_debug_inc(sk);
 515			module_put(THIS_MODULE);
 516			retv = 0;
 517			break;
 518		}
 519		goto e_inval;
 520
 521	case IPV6_V6ONLY:
 522		if (optlen < sizeof(int) ||
 523		    inet_sk(sk)->inet_num)
 524			goto e_inval;
 525		sk->sk_ipv6only = valbool;
 526		retv = 0;
 527		break;
 528
 529	case IPV6_RECVPKTINFO:
 530		if (optlen < sizeof(int))
 531			goto e_inval;
 532		np->rxopt.bits.rxinfo = valbool;
 533		retv = 0;
 534		break;
 535
 536	case IPV6_2292PKTINFO:
 537		if (optlen < sizeof(int))
 538			goto e_inval;
 539		np->rxopt.bits.rxoinfo = valbool;
 540		retv = 0;
 541		break;
 542
 543	case IPV6_RECVHOPLIMIT:
 544		if (optlen < sizeof(int))
 545			goto e_inval;
 546		np->rxopt.bits.rxhlim = valbool;
 547		retv = 0;
 548		break;
 549
 550	case IPV6_2292HOPLIMIT:
 551		if (optlen < sizeof(int))
 552			goto e_inval;
 553		np->rxopt.bits.rxohlim = valbool;
 554		retv = 0;
 555		break;
 556
 557	case IPV6_RECVRTHDR:
 558		if (optlen < sizeof(int))
 559			goto e_inval;
 560		np->rxopt.bits.srcrt = valbool;
 561		retv = 0;
 562		break;
 563
 564	case IPV6_2292RTHDR:
 565		if (optlen < sizeof(int))
 566			goto e_inval;
 567		np->rxopt.bits.osrcrt = valbool;
 568		retv = 0;
 569		break;
 570
 571	case IPV6_RECVHOPOPTS:
 572		if (optlen < sizeof(int))
 573			goto e_inval;
 574		np->rxopt.bits.hopopts = valbool;
 575		retv = 0;
 576		break;
 577
 578	case IPV6_2292HOPOPTS:
 579		if (optlen < sizeof(int))
 580			goto e_inval;
 581		np->rxopt.bits.ohopopts = valbool;
 582		retv = 0;
 583		break;
 584
 585	case IPV6_RECVDSTOPTS:
 586		if (optlen < sizeof(int))
 587			goto e_inval;
 588		np->rxopt.bits.dstopts = valbool;
 589		retv = 0;
 590		break;
 591
 592	case IPV6_2292DSTOPTS:
 593		if (optlen < sizeof(int))
 594			goto e_inval;
 595		np->rxopt.bits.odstopts = valbool;
 596		retv = 0;
 597		break;
 598
 599	case IPV6_TCLASS:
 600		if (optlen < sizeof(int))
 601			goto e_inval;
 602		if (val < -1 || val > 0xff)
 603			goto e_inval;
 604		/* RFC 3542, 6.5: default traffic class of 0x0 */
 605		if (val == -1)
 606			val = 0;
 607		if (sk->sk_type == SOCK_STREAM) {
 608			val &= ~INET_ECN_MASK;
 609			val |= np->tclass & INET_ECN_MASK;
 610		}
 611		if (np->tclass != val) {
 612			np->tclass = val;
 613			sk_dst_reset(sk);
 614		}
 615		retv = 0;
 616		break;
 617
 618	case IPV6_RECVTCLASS:
 619		if (optlen < sizeof(int))
 620			goto e_inval;
 621		np->rxopt.bits.rxtclass = valbool;
 622		retv = 0;
 623		break;
 624
 625	case IPV6_FLOWINFO:
 626		if (optlen < sizeof(int))
 627			goto e_inval;
 628		np->rxopt.bits.rxflow = valbool;
 629		retv = 0;
 630		break;
 631
 632	case IPV6_RECVPATHMTU:
 633		if (optlen < sizeof(int))
 634			goto e_inval;
 635		np->rxopt.bits.rxpmtu = valbool;
 636		retv = 0;
 637		break;
 638
 639	case IPV6_TRANSPARENT:
 640		if (valbool && !sockopt_ns_capable(net->user_ns, CAP_NET_RAW) &&
 641		    !sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN)) {
 642			retv = -EPERM;
 643			break;
 644		}
 645		if (optlen < sizeof(int))
 646			goto e_inval;
 647		/* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
 648		inet_sk(sk)->transparent = valbool;
 649		retv = 0;
 650		break;
 651
 652	case IPV6_FREEBIND:
 653		if (optlen < sizeof(int))
 654			goto e_inval;
 655		/* we also don't have a separate freebind bit for IPV6 */
 656		inet_sk(sk)->freebind = valbool;
 657		retv = 0;
 658		break;
 659
 660	case IPV6_RECVORIGDSTADDR:
 661		if (optlen < sizeof(int))
 662			goto e_inval;
 663		np->rxopt.bits.rxorigdstaddr = valbool;
 664		retv = 0;
 665		break;
 666
 667	case IPV6_HOPOPTS:
 668	case IPV6_RTHDRDSTOPTS:
 669	case IPV6_RTHDR:
 670	case IPV6_DSTOPTS:
 671		retv = ipv6_set_opt_hdr(sk, optname, optval, optlen);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 672		break;
 
 673
 674	case IPV6_PKTINFO:
 675	{
 676		struct in6_pktinfo pkt;
 677
 678		if (optlen == 0)
 679			goto e_inval;
 680		else if (optlen < sizeof(struct in6_pktinfo) ||
 681			 sockptr_is_null(optval))
 682			goto e_inval;
 683
 684		if (copy_from_sockptr(&pkt, optval, sizeof(pkt))) {
 685			retv = -EFAULT;
 686			break;
 687		}
 688		if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex))
 689			goto e_inval;
 690
 691		np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
 692		np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
 693		retv = 0;
 694		break;
 695	}
 696
 697	case IPV6_2292PKTOPTIONS:
 698	{
 699		struct ipv6_txoptions *opt = NULL;
 700		struct msghdr msg;
 701		struct flowi6 fl6;
 702		struct ipcm6_cookie ipc6;
 703
 704		memset(&fl6, 0, sizeof(fl6));
 705		fl6.flowi6_oif = sk->sk_bound_dev_if;
 706		fl6.flowi6_mark = sk->sk_mark;
 707
 708		if (optlen == 0)
 709			goto update;
 710
 711		/* 1K is probably excessive
 712		 * 1K is surely not enough, 2K per standard header is 16K.
 713		 */
 714		retv = -EINVAL;
 715		if (optlen > 64*1024)
 716			break;
 717
 718		opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
 719		retv = -ENOBUFS;
 720		if (!opt)
 721			break;
 722
 723		memset(opt, 0, sizeof(*opt));
 724		refcount_set(&opt->refcnt, 1);
 725		opt->tot_len = sizeof(*opt) + optlen;
 726		retv = -EFAULT;
 727		if (copy_from_sockptr(opt + 1, optval, optlen))
 728			goto done;
 729
 730		msg.msg_controllen = optlen;
 731		msg.msg_control = (void *)(opt+1);
 732		ipc6.opt = opt;
 733
 734		retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
 
 735		if (retv)
 736			goto done;
 737update:
 738		retv = 0;
 739		opt = ipv6_update_options(sk, opt);
 740done:
 741		if (opt) {
 742			atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
 743			txopt_put(opt);
 744		}
 745		break;
 746	}
 747	case IPV6_UNICAST_HOPS:
 748		if (optlen < sizeof(int))
 749			goto e_inval;
 750		if (val > 255 || val < -1)
 751			goto e_inval;
 752		np->hop_limit = val;
 753		retv = 0;
 754		break;
 755
 756	case IPV6_MULTICAST_HOPS:
 757		if (sk->sk_type == SOCK_STREAM)
 758			break;
 759		if (optlen < sizeof(int))
 760			goto e_inval;
 761		if (val > 255 || val < -1)
 762			goto e_inval;
 763		np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
 764		retv = 0;
 765		break;
 766
 767	case IPV6_MULTICAST_LOOP:
 768		if (optlen < sizeof(int))
 769			goto e_inval;
 770		if (val != valbool)
 771			goto e_inval;
 772		np->mc_loop = valbool;
 773		retv = 0;
 774		break;
 775
 776	case IPV6_UNICAST_IF:
 777	{
 778		struct net_device *dev = NULL;
 779		int ifindex;
 780
 781		if (optlen != sizeof(int))
 782			goto e_inval;
 783
 784		ifindex = (__force int)ntohl((__force __be32)val);
 785		if (ifindex == 0) {
 786			np->ucast_oif = 0;
 787			retv = 0;
 788			break;
 789		}
 790
 791		dev = dev_get_by_index(net, ifindex);
 792		retv = -EADDRNOTAVAIL;
 793		if (!dev)
 794			break;
 795		dev_put(dev);
 796
 797		retv = -EINVAL;
 798		if (sk->sk_bound_dev_if)
 799			break;
 800
 801		np->ucast_oif = ifindex;
 802		retv = 0;
 803		break;
 804	}
 805
 806	case IPV6_MULTICAST_IF:
 807		if (sk->sk_type == SOCK_STREAM)
 808			break;
 809		if (optlen < sizeof(int))
 810			goto e_inval;
 811
 812		if (val) {
 813			struct net_device *dev;
 814			int midx;
 815
 816			rcu_read_lock();
 
 817
 818			dev = dev_get_by_index_rcu(net, val);
 819			if (!dev) {
 820				rcu_read_unlock();
 821				retv = -ENODEV;
 822				break;
 823			}
 824			midx = l3mdev_master_ifindex_rcu(dev);
 825
 826			rcu_read_unlock();
 827
 828			if (sk->sk_bound_dev_if &&
 829			    sk->sk_bound_dev_if != val &&
 830			    (!midx || midx != sk->sk_bound_dev_if))
 831				goto e_inval;
 832		}
 833		np->mcast_oif = val;
 834		retv = 0;
 835		break;
 836	case IPV6_ADD_MEMBERSHIP:
 837	case IPV6_DROP_MEMBERSHIP:
 838	{
 839		struct ipv6_mreq mreq;
 840
 841		if (optlen < sizeof(struct ipv6_mreq))
 842			goto e_inval;
 843
 844		retv = -EPROTO;
 845		if (inet_sk(sk)->is_icsk)
 846			break;
 847
 848		retv = -EFAULT;
 849		if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
 850			break;
 851
 852		if (optname == IPV6_ADD_MEMBERSHIP)
 853			retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
 854		else
 855			retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
 856		break;
 857	}
 858	case IPV6_JOIN_ANYCAST:
 859	case IPV6_LEAVE_ANYCAST:
 860	{
 861		struct ipv6_mreq mreq;
 862
 863		if (optlen < sizeof(struct ipv6_mreq))
 864			goto e_inval;
 865
 866		retv = -EFAULT;
 867		if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
 868			break;
 869
 870		if (optname == IPV6_JOIN_ANYCAST)
 871			retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
 872		else
 873			retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
 874		break;
 875	}
 876	case IPV6_MULTICAST_ALL:
 877		if (optlen < sizeof(int))
 
 
 
 
 
 878			goto e_inval;
 879		np->mc_all = valbool;
 880		retv = 0;
 881		break;
 882
 883	case MCAST_JOIN_GROUP:
 884	case MCAST_LEAVE_GROUP:
 885		if (in_compat_syscall())
 886			retv = compat_ipv6_mcast_join_leave(sk, optname, optval,
 887							    optlen);
 
 
 
 
 
 
 888		else
 889			retv = ipv6_mcast_join_leave(sk, optname, optval,
 890						     optlen);
 891		break;
 
 892	case MCAST_JOIN_SOURCE_GROUP:
 893	case MCAST_LEAVE_SOURCE_GROUP:
 894	case MCAST_BLOCK_SOURCE:
 895	case MCAST_UNBLOCK_SOURCE:
 896		retv = do_ipv6_mcast_group_source(sk, optname, optval, optlen);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 897		break;
 
 898	case MCAST_MSFILTER:
 899		if (in_compat_syscall())
 900			retv = compat_ipv6_set_mcast_msfilter(sk, optval,
 901							      optlen);
 902		else
 903			retv = ipv6_set_mcast_msfilter(sk, optval, optlen);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 904		break;
 
 905	case IPV6_ROUTER_ALERT:
 906		if (optlen < sizeof(int))
 907			goto e_inval;
 908		retv = ip6_ra_control(sk, val);
 909		break;
 910	case IPV6_ROUTER_ALERT_ISOLATE:
 911		if (optlen < sizeof(int))
 912			goto e_inval;
 913		np->rtalert_isolate = valbool;
 914		retv = 0;
 915		break;
 916	case IPV6_MTU_DISCOVER:
 917		if (optlen < sizeof(int))
 918			goto e_inval;
 919		if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
 920			goto e_inval;
 921		np->pmtudisc = val;
 922		retv = 0;
 923		break;
 924	case IPV6_MTU:
 925		if (optlen < sizeof(int))
 926			goto e_inval;
 927		if (val && val < IPV6_MIN_MTU)
 928			goto e_inval;
 929		np->frag_size = val;
 930		retv = 0;
 931		break;
 932	case IPV6_RECVERR:
 933		if (optlen < sizeof(int))
 934			goto e_inval;
 935		np->recverr = valbool;
 936		if (!val)
 937			skb_queue_purge(&sk->sk_error_queue);
 938		retv = 0;
 939		break;
 940	case IPV6_FLOWINFO_SEND:
 941		if (optlen < sizeof(int))
 942			goto e_inval;
 943		np->sndflow = valbool;
 944		retv = 0;
 945		break;
 946	case IPV6_FLOWLABEL_MGR:
 947		retv = ipv6_flowlabel_opt(sk, optval, optlen);
 948		break;
 949	case IPV6_IPSEC_POLICY:
 950	case IPV6_XFRM_POLICY:
 951		retv = -EPERM;
 952		if (!sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN))
 953			break;
 954		retv = xfrm_user_policy(sk, optname, optval, optlen);
 955		break;
 956
 957	case IPV6_ADDR_PREFERENCES:
 
 
 
 
 958		if (optlen < sizeof(int))
 959			goto e_inval;
 960		retv = __ip6_sock_set_addr_preferences(sk, val);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 961		break;
 
 962	case IPV6_MINHOPCOUNT:
 963		if (optlen < sizeof(int))
 964			goto e_inval;
 965		if (val < 0 || val > 255)
 966			goto e_inval;
 967
 968		if (val)
 969			static_branch_enable(&ip6_min_hopcount);
 970
 971		/* tcp_v6_err() and tcp_v6_rcv() might read min_hopcount
 972		 * while we are changing it.
 973		 */
 974		WRITE_ONCE(np->min_hopcount, val);
 975		retv = 0;
 976		break;
 977	case IPV6_DONTFRAG:
 978		np->dontfrag = valbool;
 979		retv = 0;
 980		break;
 981	case IPV6_AUTOFLOWLABEL:
 982		np->autoflowlabel = valbool;
 983		np->autoflowlabel_set = 1;
 984		retv = 0;
 985		break;
 986	case IPV6_RECVFRAGSIZE:
 987		np->rxopt.bits.recvfragsize = valbool;
 988		retv = 0;
 989		break;
 990	case IPV6_RECVERR_RFC4884:
 991		if (optlen < sizeof(int))
 992			goto e_inval;
 993		if (val < 0 || val > 1)
 994			goto e_inval;
 995		np->recverr_rfc4884 = valbool;
 996		retv = 0;
 997		break;
 998	}
 999
1000unlock:
1001	sockopt_release_sock(sk);
1002	if (needs_rtnl)
1003		rtnl_unlock();
1004
1005	return retv;
1006
1007e_inval:
1008	retv = -EINVAL;
1009	goto unlock;
1010}
1011
1012int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1013		    unsigned int optlen)
1014{
1015	int err;
1016
1017	if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1018		return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1019
1020	if (level != SOL_IPV6)
1021		return -ENOPROTOOPT;
1022
1023	err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
1024#ifdef CONFIG_NETFILTER
1025	/* we need to exclude all possible ENOPROTOOPTs except default case */
1026	if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
1027			optname != IPV6_XFRM_POLICY)
1028		err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
 
 
 
 
1029#endif
1030	return err;
1031}
 
1032EXPORT_SYMBOL(ipv6_setsockopt);
1033
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1034static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
1035				  int optname, sockptr_t optval, int len)
1036{
1037	struct ipv6_opt_hdr *hdr;
1038
1039	if (!opt)
1040		return 0;
1041
1042	switch (optname) {
1043	case IPV6_HOPOPTS:
1044		hdr = opt->hopopt;
1045		break;
1046	case IPV6_RTHDRDSTOPTS:
1047		hdr = opt->dst0opt;
1048		break;
1049	case IPV6_RTHDR:
1050		hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1051		break;
1052	case IPV6_DSTOPTS:
1053		hdr = opt->dst1opt;
1054		break;
1055	default:
1056		return -EINVAL;	/* should not happen */
1057	}
1058
1059	if (!hdr)
1060		return 0;
1061
1062	len = min_t(unsigned int, len, ipv6_optlen(hdr));
1063	if (copy_to_sockptr(optval, hdr, len))
1064		return -EFAULT;
1065	return len;
1066}
1067
1068static int ipv6_get_msfilter(struct sock *sk, sockptr_t optval,
1069			     sockptr_t optlen, int len)
1070{
1071	const int size0 = offsetof(struct group_filter, gf_slist_flex);
1072	struct group_filter gsf;
1073	int num;
1074	int err;
1075
1076	if (len < size0)
1077		return -EINVAL;
1078	if (copy_from_sockptr(&gsf, optval, size0))
1079		return -EFAULT;
1080	if (gsf.gf_group.ss_family != AF_INET6)
1081		return -EADDRNOTAVAIL;
1082	num = gsf.gf_numsrc;
1083	sockopt_lock_sock(sk);
1084	err = ip6_mc_msfget(sk, &gsf, optval, size0);
1085	if (!err) {
1086		if (num > gsf.gf_numsrc)
1087			num = gsf.gf_numsrc;
1088		len = GROUP_FILTER_SIZE(num);
1089		if (copy_to_sockptr(optlen, &len, sizeof(int)) ||
1090		    copy_to_sockptr(optval, &gsf, size0))
1091			err = -EFAULT;
1092	}
1093	sockopt_release_sock(sk);
1094	return err;
1095}
1096
1097static int compat_ipv6_get_msfilter(struct sock *sk, sockptr_t optval,
1098				    sockptr_t optlen, int len)
1099{
1100	const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
1101	struct compat_group_filter gf32;
1102	struct group_filter gf;
1103	int err;
1104	int num;
1105
1106	if (len < size0)
1107		return -EINVAL;
1108
1109	if (copy_from_sockptr(&gf32, optval, size0))
1110		return -EFAULT;
1111	gf.gf_interface = gf32.gf_interface;
1112	gf.gf_fmode = gf32.gf_fmode;
1113	num = gf.gf_numsrc = gf32.gf_numsrc;
1114	gf.gf_group = gf32.gf_group;
1115
1116	if (gf.gf_group.ss_family != AF_INET6)
1117		return -EADDRNOTAVAIL;
1118
1119	sockopt_lock_sock(sk);
1120	err = ip6_mc_msfget(sk, &gf, optval, size0);
1121	sockopt_release_sock(sk);
1122	if (err)
1123		return err;
1124	if (num > gf.gf_numsrc)
1125		num = gf.gf_numsrc;
1126	len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32));
1127	if (copy_to_sockptr(optlen, &len, sizeof(int)) ||
1128	    copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_fmode),
1129				   &gf.gf_fmode, sizeof(gf32.gf_fmode)) ||
1130	    copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_numsrc),
1131				   &gf.gf_numsrc, sizeof(gf32.gf_numsrc)))
1132		return -EFAULT;
1133	return 0;
1134}
1135
1136int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1137		       sockptr_t optval, sockptr_t optlen)
1138{
1139	struct ipv6_pinfo *np = inet6_sk(sk);
1140	int len;
1141	int val;
1142
1143	if (ip6_mroute_opt(optname))
1144		return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1145
1146	if (copy_from_sockptr(&len, optlen, sizeof(int)))
1147		return -EFAULT;
1148	switch (optname) {
1149	case IPV6_ADDRFORM:
1150		if (sk->sk_protocol != IPPROTO_UDP &&
1151		    sk->sk_protocol != IPPROTO_UDPLITE &&
1152		    sk->sk_protocol != IPPROTO_TCP)
1153			return -ENOPROTOOPT;
1154		if (sk->sk_state != TCP_ESTABLISHED)
1155			return -ENOTCONN;
1156		val = sk->sk_family;
1157		break;
1158	case MCAST_MSFILTER:
1159		if (in_compat_syscall())
1160			return compat_ipv6_get_msfilter(sk, optval, optlen, len);
1161		return ipv6_get_msfilter(sk, optval, optlen, len);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1162	case IPV6_2292PKTOPTIONS:
1163	{
1164		struct msghdr msg;
1165		struct sk_buff *skb;
1166
1167		if (sk->sk_type != SOCK_STREAM)
1168			return -ENOPROTOOPT;
1169
1170		if (optval.is_kernel) {
1171			msg.msg_control_is_user = false;
1172			msg.msg_control = optval.kernel;
1173		} else {
1174			msg.msg_control_is_user = true;
1175			msg.msg_control_user = optval.user;
1176		}
1177		msg.msg_controllen = len;
1178		msg.msg_flags = 0;
1179
1180		sockopt_lock_sock(sk);
1181		skb = np->pktoptions;
1182		if (skb)
1183			ip6_datagram_recv_ctl(sk, &msg, skb);
1184		sockopt_release_sock(sk);
1185		if (!skb) {
 
 
 
 
 
 
1186			if (np->rxopt.bits.rxinfo) {
1187				struct in6_pktinfo src_info;
1188				src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1189					np->sticky_pktinfo.ipi6_ifindex;
1190				src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
 
1191				put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1192			}
1193			if (np->rxopt.bits.rxhlim) {
1194				int hlim = np->mcast_hops;
1195				put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1196			}
1197			if (np->rxopt.bits.rxtclass) {
1198				int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1199
1200				put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1201			}
1202			if (np->rxopt.bits.rxoinfo) {
1203				struct in6_pktinfo src_info;
1204				src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1205					np->sticky_pktinfo.ipi6_ifindex;
1206				src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1207								     np->sticky_pktinfo.ipi6_addr;
1208				put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1209			}
1210			if (np->rxopt.bits.rxohlim) {
1211				int hlim = np->mcast_hops;
1212				put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1213			}
1214			if (np->rxopt.bits.rxflow) {
1215				__be32 flowinfo = np->rcv_flowinfo;
1216
1217				put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1218			}
1219		}
1220		len -= msg.msg_controllen;
1221		return copy_to_sockptr(optlen, &len, sizeof(int));
1222	}
1223	case IPV6_MTU:
1224	{
1225		struct dst_entry *dst;
1226
1227		val = 0;
1228		rcu_read_lock();
1229		dst = __sk_dst_get(sk);
1230		if (dst)
1231			val = dst_mtu(dst);
1232		rcu_read_unlock();
1233		if (!val)
1234			return -ENOTCONN;
1235		break;
1236	}
1237
1238	case IPV6_V6ONLY:
1239		val = sk->sk_ipv6only;
1240		break;
1241
1242	case IPV6_RECVPKTINFO:
1243		val = np->rxopt.bits.rxinfo;
1244		break;
1245
1246	case IPV6_2292PKTINFO:
1247		val = np->rxopt.bits.rxoinfo;
1248		break;
1249
1250	case IPV6_RECVHOPLIMIT:
1251		val = np->rxopt.bits.rxhlim;
1252		break;
1253
1254	case IPV6_2292HOPLIMIT:
1255		val = np->rxopt.bits.rxohlim;
1256		break;
1257
1258	case IPV6_RECVRTHDR:
1259		val = np->rxopt.bits.srcrt;
1260		break;
1261
1262	case IPV6_2292RTHDR:
1263		val = np->rxopt.bits.osrcrt;
1264		break;
1265
1266	case IPV6_HOPOPTS:
1267	case IPV6_RTHDRDSTOPTS:
1268	case IPV6_RTHDR:
1269	case IPV6_DSTOPTS:
1270	{
1271		struct ipv6_txoptions *opt;
1272
1273		sockopt_lock_sock(sk);
1274		opt = rcu_dereference_protected(np->opt,
1275						lockdep_sock_is_held(sk));
1276		len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
1277		sockopt_release_sock(sk);
1278		/* check if ipv6_getsockopt_sticky() returns err code */
1279		if (len < 0)
1280			return len;
1281		return copy_to_sockptr(optlen, &len, sizeof(int));
1282	}
1283
1284	case IPV6_RECVHOPOPTS:
1285		val = np->rxopt.bits.hopopts;
1286		break;
1287
1288	case IPV6_2292HOPOPTS:
1289		val = np->rxopt.bits.ohopopts;
1290		break;
1291
1292	case IPV6_RECVDSTOPTS:
1293		val = np->rxopt.bits.dstopts;
1294		break;
1295
1296	case IPV6_2292DSTOPTS:
1297		val = np->rxopt.bits.odstopts;
1298		break;
1299
1300	case IPV6_TCLASS:
1301		val = np->tclass;
1302		break;
1303
1304	case IPV6_RECVTCLASS:
1305		val = np->rxopt.bits.rxtclass;
1306		break;
1307
1308	case IPV6_FLOWINFO:
1309		val = np->rxopt.bits.rxflow;
1310		break;
1311
1312	case IPV6_RECVPATHMTU:
1313		val = np->rxopt.bits.rxpmtu;
1314		break;
1315
1316	case IPV6_PATHMTU:
1317	{
1318		struct dst_entry *dst;
1319		struct ip6_mtuinfo mtuinfo;
1320
1321		if (len < sizeof(mtuinfo))
1322			return -EINVAL;
1323
1324		len = sizeof(mtuinfo);
1325		memset(&mtuinfo, 0, sizeof(mtuinfo));
1326
1327		rcu_read_lock();
1328		dst = __sk_dst_get(sk);
1329		if (dst)
1330			mtuinfo.ip6m_mtu = dst_mtu(dst);
1331		rcu_read_unlock();
1332		if (!mtuinfo.ip6m_mtu)
1333			return -ENOTCONN;
1334
1335		if (copy_to_sockptr(optlen, &len, sizeof(int)))
1336			return -EFAULT;
1337		if (copy_to_sockptr(optval, &mtuinfo, len))
1338			return -EFAULT;
1339
1340		return 0;
 
1341	}
1342
1343	case IPV6_TRANSPARENT:
1344		val = inet_sk(sk)->transparent;
1345		break;
1346
1347	case IPV6_FREEBIND:
1348		val = inet_sk(sk)->freebind;
1349		break;
1350
1351	case IPV6_RECVORIGDSTADDR:
1352		val = np->rxopt.bits.rxorigdstaddr;
1353		break;
1354
1355	case IPV6_UNICAST_HOPS:
1356	case IPV6_MULTICAST_HOPS:
1357	{
1358		struct dst_entry *dst;
1359
1360		if (optname == IPV6_UNICAST_HOPS)
1361			val = np->hop_limit;
1362		else
1363			val = np->mcast_hops;
1364
1365		if (val < 0) {
1366			rcu_read_lock();
1367			dst = __sk_dst_get(sk);
1368			if (dst)
1369				val = ip6_dst_hoplimit(dst);
1370			rcu_read_unlock();
1371		}
1372
1373		if (val < 0)
1374			val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1375		break;
1376	}
1377
1378	case IPV6_MULTICAST_LOOP:
1379		val = np->mc_loop;
1380		break;
1381
1382	case IPV6_MULTICAST_IF:
1383		val = np->mcast_oif;
1384		break;
1385
1386	case IPV6_MULTICAST_ALL:
1387		val = np->mc_all;
1388		break;
1389
1390	case IPV6_UNICAST_IF:
1391		val = (__force int)htonl((__u32) np->ucast_oif);
1392		break;
1393
1394	case IPV6_MTU_DISCOVER:
1395		val = np->pmtudisc;
1396		break;
1397
1398	case IPV6_RECVERR:
1399		val = np->recverr;
1400		break;
1401
1402	case IPV6_FLOWINFO_SEND:
1403		val = np->sndflow;
1404		break;
1405
1406	case IPV6_FLOWLABEL_MGR:
1407	{
1408		struct in6_flowlabel_req freq;
1409		int flags;
1410
1411		if (len < sizeof(freq))
1412			return -EINVAL;
1413
1414		if (copy_from_sockptr(&freq, optval, sizeof(freq)))
1415			return -EFAULT;
1416
1417		if (freq.flr_action != IPV6_FL_A_GET)
1418			return -EINVAL;
1419
1420		len = sizeof(freq);
1421		flags = freq.flr_flags;
1422
1423		memset(&freq, 0, sizeof(freq));
1424
1425		val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1426		if (val < 0)
1427			return val;
1428
1429		if (copy_to_sockptr(optlen, &len, sizeof(int)))
1430			return -EFAULT;
1431		if (copy_to_sockptr(optval, &freq, len))
1432			return -EFAULT;
1433
1434		return 0;
1435	}
1436
1437	case IPV6_ADDR_PREFERENCES:
1438		val = 0;
1439
1440		if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1441			val |= IPV6_PREFER_SRC_TMP;
1442		else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1443			val |= IPV6_PREFER_SRC_PUBLIC;
1444		else {
1445			/* XXX: should we return system default? */
1446			val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1447		}
1448
1449		if (np->srcprefs & IPV6_PREFER_SRC_COA)
1450			val |= IPV6_PREFER_SRC_COA;
1451		else
1452			val |= IPV6_PREFER_SRC_HOME;
1453		break;
1454
1455	case IPV6_MINHOPCOUNT:
1456		val = np->min_hopcount;
1457		break;
1458
1459	case IPV6_DONTFRAG:
1460		val = np->dontfrag;
1461		break;
1462
1463	case IPV6_AUTOFLOWLABEL:
1464		val = ip6_autoflowlabel(sock_net(sk), np);
1465		break;
1466
1467	case IPV6_RECVFRAGSIZE:
1468		val = np->rxopt.bits.recvfragsize;
1469		break;
1470
1471	case IPV6_ROUTER_ALERT_ISOLATE:
1472		val = np->rtalert_isolate;
1473		break;
1474
1475	case IPV6_RECVERR_RFC4884:
1476		val = np->recverr_rfc4884;
1477		break;
1478
1479	default:
1480		return -ENOPROTOOPT;
1481	}
1482	len = min_t(unsigned int, sizeof(int), len);
1483	if (copy_to_sockptr(optlen, &len, sizeof(int)))
1484		return -EFAULT;
1485	if (copy_to_sockptr(optval, &val, len))
1486		return -EFAULT;
1487	return 0;
1488}
1489
1490int ipv6_getsockopt(struct sock *sk, int level, int optname,
1491		    char __user *optval, int __user *optlen)
1492{
1493	int err;
1494
1495	if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1496		return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1497
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1498	if (level != SOL_IPV6)
1499		return -ENOPROTOOPT;
1500
1501	err = do_ipv6_getsockopt(sk, level, optname,
1502				 USER_SOCKPTR(optval), USER_SOCKPTR(optlen));
 
 
 
 
1503#ifdef CONFIG_NETFILTER
1504	/* we need to exclude all possible ENOPROTOOPTs except default case */
1505	if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1506		int len;
1507
1508		if (get_user(len, optlen))
1509			return -EFAULT;
1510
1511		err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
 
 
 
1512		if (err >= 0)
1513			err = put_user(len, optlen);
1514	}
1515#endif
1516	return err;
1517}
1518EXPORT_SYMBOL(ipv6_getsockopt);