ipv6_sockglue.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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. #include <linux/module.h>
  25. #include <linux/capability.h>
  26. #include <linux/errno.h>
  27. #include <linux/types.h>
  28. #include <linux/socket.h>
  29. #include <linux/sockios.h>
  30. #include <linux/net.h>
  31. #include <linux/in6.h>
  32. #include <linux/mroute6.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/if_arp.h>
  35. #include <linux/init.h>
  36. #include <linux/sysctl.h>
  37. #include <linux/netfilter.h>
  38. #include <linux/slab.h>
  39. #include <net/sock.h>
  40. #include <net/snmp.h>
  41. #include <net/ipv6.h>
  42. #include <net/ndisc.h>
  43. #include <net/protocol.h>
  44. #include <net/transp_v6.h>
  45. #include <net/ip6_route.h>
  46. #include <net/addrconf.h>
  47. #include <net/inet_common.h>
  48. #include <net/tcp.h>
  49. #include <net/udp.h>
  50. #include <net/udplite.h>
  51. #include <net/xfrm.h>
  52. #include <net/compat.h>
  53. #include <asm/uaccess.h>
  54. struct ip6_ra_chain *ip6_ra_chain;
  55. DEFINE_RWLOCK(ip6_ra_lock);
  56. int ip6_ra_control(struct sock *sk, int sel)
  57. {
  58. struct ip6_ra_chain *ra, *new_ra, **rap;
  59. /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
  60. if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW)
  61. return -ENOPROTOOPT;
  62. new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
  63. write_lock_bh(&ip6_ra_lock);
  64. for (rap = &ip6_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
  65. if (ra->sk == sk) {
  66. if (sel >= 0) {
  67. write_unlock_bh(&ip6_ra_lock);
  68. kfree(new_ra);
  69. return -EADDRINUSE;
  70. }
  71. *rap = ra->next;
  72. write_unlock_bh(&ip6_ra_lock);
  73. sock_put(sk);
  74. kfree(ra);
  75. return 0;
  76. }
  77. }
  78. if (!new_ra) {
  79. write_unlock_bh(&ip6_ra_lock);
  80. return -ENOBUFS;
  81. }
  82. new_ra->sk = sk;
  83. new_ra->sel = sel;
  84. new_ra->next = ra;
  85. *rap = new_ra;
  86. sock_hold(sk);
  87. write_unlock_bh(&ip6_ra_lock);
  88. return 0;
  89. }
  90. static
  91. struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
  92. struct ipv6_txoptions *opt)
  93. {
  94. if (inet_sk(sk)->is_icsk) {
  95. if (opt &&
  96. !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
  97. inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
  98. struct inet_connection_sock *icsk = inet_csk(sk);
  99. icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
  100. icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
  101. }
  102. }
  103. opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt,
  104. opt);
  105. sk_dst_reset(sk);
  106. return opt;
  107. }
  108. static bool setsockopt_needs_rtnl(int optname)
  109. {
  110. switch (optname) {
  111. case IPV6_ADD_MEMBERSHIP:
  112. case IPV6_DROP_MEMBERSHIP:
  113. case IPV6_JOIN_ANYCAST:
  114. case IPV6_LEAVE_ANYCAST:
  115. case MCAST_JOIN_GROUP:
  116. case MCAST_LEAVE_GROUP:
  117. case MCAST_JOIN_SOURCE_GROUP:
  118. case MCAST_LEAVE_SOURCE_GROUP:
  119. case MCAST_BLOCK_SOURCE:
  120. case MCAST_UNBLOCK_SOURCE:
  121. case MCAST_MSFILTER:
  122. return true;
  123. }
  124. return false;
  125. }
  126. static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
  127. char __user *optval, unsigned int optlen)
  128. {
  129. struct ipv6_pinfo *np = inet6_sk(sk);
  130. struct net *net = sock_net(sk);
  131. int val, valbool;
  132. int retv = -ENOPROTOOPT;
  133. bool needs_rtnl = setsockopt_needs_rtnl(optname);
  134. if (!optval)
  135. val = 0;
  136. else {
  137. if (optlen >= sizeof(int)) {
  138. if (get_user(val, (int __user *) optval))
  139. return -EFAULT;
  140. } else
  141. val = 0;
  142. }
  143. valbool = (val != 0);
  144. if (ip6_mroute_opt(optname))
  145. return ip6_mroute_setsockopt(sk, optname, optval, optlen);
  146. if (needs_rtnl)
  147. rtnl_lock();
  148. lock_sock(sk);
  149. switch (optname) {
  150. case IPV6_ADDRFORM:
  151. if (optlen < sizeof(int))
  152. goto e_inval;
  153. if (val == PF_INET) {
  154. struct ipv6_txoptions *opt;
  155. struct sk_buff *pktopt;
  156. if (sk->sk_type == SOCK_RAW)
  157. break;
  158. if (sk->sk_protocol == IPPROTO_UDP ||
  159. sk->sk_protocol == IPPROTO_UDPLITE) {
  160. struct udp_sock *up = udp_sk(sk);
  161. if (up->pending == AF_INET6) {
  162. retv = -EBUSY;
  163. break;
  164. }
  165. } else if (sk->sk_protocol != IPPROTO_TCP)
  166. break;
  167. if (sk->sk_state != TCP_ESTABLISHED) {
  168. retv = -ENOTCONN;
  169. break;
  170. }
  171. if (ipv6_only_sock(sk) ||
  172. !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
  173. retv = -EADDRNOTAVAIL;
  174. break;
  175. }
  176. fl6_free_socklist(sk);
  177. ipv6_sock_mc_close(sk);
  178. /*
  179. * Sock is moving from IPv6 to IPv4 (sk_prot), so
  180. * remove it from the refcnt debug socks count in the
  181. * original family...
  182. */
  183. sk_refcnt_debug_dec(sk);
  184. if (sk->sk_protocol == IPPROTO_TCP) {
  185. struct inet_connection_sock *icsk = inet_csk(sk);
  186. local_bh_disable();
  187. sock_prot_inuse_add(net, sk->sk_prot, -1);
  188. sock_prot_inuse_add(net, &tcp_prot, 1);
  189. local_bh_enable();
  190. sk->sk_prot = &tcp_prot;
  191. icsk->icsk_af_ops = &ipv4_specific;
  192. sk->sk_socket->ops = &inet_stream_ops;
  193. sk->sk_family = PF_INET;
  194. tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
  195. } else {
  196. struct proto *prot = &udp_prot;
  197. if (sk->sk_protocol == IPPROTO_UDPLITE)
  198. prot = &udplite_prot;
  199. local_bh_disable();
  200. sock_prot_inuse_add(net, sk->sk_prot, -1);
  201. sock_prot_inuse_add(net, prot, 1);
  202. local_bh_enable();
  203. sk->sk_prot = prot;
  204. sk->sk_socket->ops = &inet_dgram_ops;
  205. sk->sk_family = PF_INET;
  206. }
  207. opt = xchg((__force struct ipv6_txoptions **)&np->opt,
  208. NULL);
  209. if (opt) {
  210. atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
  211. txopt_put(opt);
  212. }
  213. pktopt = xchg(&np->pktoptions, NULL);
  214. kfree_skb(pktopt);
  215. sk->sk_destruct = inet_sock_destruct;
  216. /*
  217. * ... and add it to the refcnt debug socks count
  218. * in the new family. -acme
  219. */
  220. sk_refcnt_debug_inc(sk);
  221. module_put(THIS_MODULE);
  222. retv = 0;
  223. break;
  224. }
  225. goto e_inval;
  226. case IPV6_V6ONLY:
  227. if (optlen < sizeof(int) ||
  228. inet_sk(sk)->inet_num)
  229. goto e_inval;
  230. sk->sk_ipv6only = valbool;
  231. retv = 0;
  232. break;
  233. case IPV6_RECVPKTINFO:
  234. if (optlen < sizeof(int))
  235. goto e_inval;
  236. np->rxopt.bits.rxinfo = valbool;
  237. retv = 0;
  238. break;
  239. case IPV6_2292PKTINFO:
  240. if (optlen < sizeof(int))
  241. goto e_inval;
  242. np->rxopt.bits.rxoinfo = valbool;
  243. retv = 0;
  244. break;
  245. case IPV6_RECVHOPLIMIT:
  246. if (optlen < sizeof(int))
  247. goto e_inval;
  248. np->rxopt.bits.rxhlim = valbool;
  249. retv = 0;
  250. break;
  251. case IPV6_2292HOPLIMIT:
  252. if (optlen < sizeof(int))
  253. goto e_inval;
  254. np->rxopt.bits.rxohlim = valbool;
  255. retv = 0;
  256. break;
  257. case IPV6_RECVRTHDR:
  258. if (optlen < sizeof(int))
  259. goto e_inval;
  260. np->rxopt.bits.srcrt = valbool;
  261. retv = 0;
  262. break;
  263. case IPV6_2292RTHDR:
  264. if (optlen < sizeof(int))
  265. goto e_inval;
  266. np->rxopt.bits.osrcrt = valbool;
  267. retv = 0;
  268. break;
  269. case IPV6_RECVHOPOPTS:
  270. if (optlen < sizeof(int))
  271. goto e_inval;
  272. np->rxopt.bits.hopopts = valbool;
  273. retv = 0;
  274. break;
  275. case IPV6_2292HOPOPTS:
  276. if (optlen < sizeof(int))
  277. goto e_inval;
  278. np->rxopt.bits.ohopopts = valbool;
  279. retv = 0;
  280. break;
  281. case IPV6_RECVDSTOPTS:
  282. if (optlen < sizeof(int))
  283. goto e_inval;
  284. np->rxopt.bits.dstopts = valbool;
  285. retv = 0;
  286. break;
  287. case IPV6_2292DSTOPTS:
  288. if (optlen < sizeof(int))
  289. goto e_inval;
  290. np->rxopt.bits.odstopts = valbool;
  291. retv = 0;
  292. break;
  293. case IPV6_TCLASS:
  294. if (optlen < sizeof(int))
  295. goto e_inval;
  296. if (val < -1 || val > 0xff)
  297. goto e_inval;
  298. /* RFC 3542, 6.5: default traffic class of 0x0 */
  299. if (val == -1)
  300. val = 0;
  301. np->tclass = val;
  302. retv = 0;
  303. break;
  304. case IPV6_RECVTCLASS:
  305. if (optlen < sizeof(int))
  306. goto e_inval;
  307. np->rxopt.bits.rxtclass = valbool;
  308. retv = 0;
  309. break;
  310. case IPV6_FLOWINFO:
  311. if (optlen < sizeof(int))
  312. goto e_inval;
  313. np->rxopt.bits.rxflow = valbool;
  314. retv = 0;
  315. break;
  316. case IPV6_RECVPATHMTU:
  317. if (optlen < sizeof(int))
  318. goto e_inval;
  319. np->rxopt.bits.rxpmtu = valbool;
  320. retv = 0;
  321. break;
  322. case IPV6_TRANSPARENT:
  323. if (valbool && !ns_capable(net->user_ns, CAP_NET_ADMIN) &&
  324. !ns_capable(net->user_ns, CAP_NET_RAW)) {
  325. retv = -EPERM;
  326. break;
  327. }
  328. if (optlen < sizeof(int))
  329. goto e_inval;
  330. /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
  331. inet_sk(sk)->transparent = valbool;
  332. retv = 0;
  333. break;
  334. case IPV6_RECVORIGDSTADDR:
  335. if (optlen < sizeof(int))
  336. goto e_inval;
  337. np->rxopt.bits.rxorigdstaddr = valbool;
  338. retv = 0;
  339. break;
  340. case IPV6_HOPOPTS:
  341. case IPV6_RTHDRDSTOPTS:
  342. case IPV6_RTHDR:
  343. case IPV6_DSTOPTS:
  344. {
  345. struct ipv6_txoptions *opt;
  346. /* remove any sticky options header with a zero option
  347. * length, per RFC3542.
  348. */
  349. if (optlen == 0)
  350. optval = NULL;
  351. else if (!optval)
  352. goto e_inval;
  353. else if (optlen < sizeof(struct ipv6_opt_hdr) ||
  354. optlen & 0x7 || optlen > 8 * 255)
  355. goto e_inval;
  356. /* hop-by-hop / destination options are privileged option */
  357. retv = -EPERM;
  358. if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
  359. break;
  360. opt = rcu_dereference_protected(np->opt, sock_owned_by_user(sk));
  361. opt = ipv6_renew_options(sk, opt, optname,
  362. (struct ipv6_opt_hdr __user *)optval,
  363. optlen);
  364. if (IS_ERR(opt)) {
  365. retv = PTR_ERR(opt);
  366. break;
  367. }
  368. /* routing header option needs extra check */
  369. retv = -EINVAL;
  370. if (optname == IPV6_RTHDR && opt && opt->srcrt) {
  371. struct ipv6_rt_hdr *rthdr = opt->srcrt;
  372. switch (rthdr->type) {
  373. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  374. case IPV6_SRCRT_TYPE_2:
  375. if (rthdr->hdrlen != 2 ||
  376. rthdr->segments_left != 1)
  377. goto sticky_done;
  378. break;
  379. #endif
  380. default:
  381. goto sticky_done;
  382. }
  383. }
  384. retv = 0;
  385. opt = ipv6_update_options(sk, opt);
  386. sticky_done:
  387. if (opt) {
  388. atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
  389. txopt_put(opt);
  390. }
  391. break;
  392. }
  393. case IPV6_PKTINFO:
  394. {
  395. struct in6_pktinfo pkt;
  396. if (optlen == 0)
  397. goto e_inval;
  398. else if (optlen < sizeof(struct in6_pktinfo) || !optval)
  399. goto e_inval;
  400. if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
  401. retv = -EFAULT;
  402. break;
  403. }
  404. if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
  405. goto e_inval;
  406. np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
  407. np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
  408. retv = 0;
  409. break;
  410. }
  411. case IPV6_2292PKTOPTIONS:
  412. {
  413. struct ipv6_txoptions *opt = NULL;
  414. struct msghdr msg;
  415. struct flowi6 fl6;
  416. int junk;
  417. memset(&fl6, 0, sizeof(fl6));
  418. fl6.flowi6_oif = sk->sk_bound_dev_if;
  419. fl6.flowi6_mark = sk->sk_mark;
  420. if (optlen == 0)
  421. goto update;
  422. /* 1K is probably excessive
  423. * 1K is surely not enough, 2K per standard header is 16K.
  424. */
  425. retv = -EINVAL;
  426. if (optlen > 64*1024)
  427. break;
  428. opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
  429. retv = -ENOBUFS;
  430. if (!opt)
  431. break;
  432. memset(opt, 0, sizeof(*opt));
  433. atomic_set(&opt->refcnt, 1);
  434. opt->tot_len = sizeof(*opt) + optlen;
  435. retv = -EFAULT;
  436. if (copy_from_user(opt+1, optval, optlen))
  437. goto done;
  438. msg.msg_controllen = optlen;
  439. msg.msg_control = (void *)(opt+1);
  440. retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, opt, &junk,
  441. &junk, &junk);
  442. if (retv)
  443. goto done;
  444. update:
  445. retv = 0;
  446. opt = ipv6_update_options(sk, opt);
  447. done:
  448. if (opt) {
  449. atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
  450. txopt_put(opt);
  451. }
  452. break;
  453. }
  454. case IPV6_UNICAST_HOPS:
  455. if (optlen < sizeof(int))
  456. goto e_inval;
  457. if (val > 255 || val < -1)
  458. goto e_inval;
  459. np->hop_limit = val;
  460. retv = 0;
  461. break;
  462. case IPV6_MULTICAST_HOPS:
  463. if (sk->sk_type == SOCK_STREAM)
  464. break;
  465. if (optlen < sizeof(int))
  466. goto e_inval;
  467. if (val > 255 || val < -1)
  468. goto e_inval;
  469. np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
  470. retv = 0;
  471. break;
  472. case IPV6_MULTICAST_LOOP:
  473. if (optlen < sizeof(int))
  474. goto e_inval;
  475. if (val != valbool)
  476. goto e_inval;
  477. np->mc_loop = valbool;
  478. retv = 0;
  479. break;
  480. case IPV6_UNICAST_IF:
  481. {
  482. struct net_device *dev = NULL;
  483. int ifindex;
  484. if (optlen != sizeof(int))
  485. goto e_inval;
  486. ifindex = (__force int)ntohl((__force __be32)val);
  487. if (ifindex == 0) {
  488. np->ucast_oif = 0;
  489. retv = 0;
  490. break;
  491. }
  492. dev = dev_get_by_index(net, ifindex);
  493. retv = -EADDRNOTAVAIL;
  494. if (!dev)
  495. break;
  496. dev_put(dev);
  497. retv = -EINVAL;
  498. if (sk->sk_bound_dev_if)
  499. break;
  500. np->ucast_oif = ifindex;
  501. retv = 0;
  502. break;
  503. }
  504. case IPV6_MULTICAST_IF:
  505. if (sk->sk_type == SOCK_STREAM)
  506. break;
  507. if (optlen < sizeof(int))
  508. goto e_inval;
  509. if (val) {
  510. struct net_device *dev;
  511. int midx;
  512. rcu_read_lock();
  513. dev = dev_get_by_index_rcu(net, val);
  514. if (!dev) {
  515. rcu_read_unlock();
  516. retv = -ENODEV;
  517. break;
  518. }
  519. midx = l3mdev_master_ifindex_rcu(dev);
  520. rcu_read_unlock();
  521. if (sk->sk_bound_dev_if &&
  522. sk->sk_bound_dev_if != val &&
  523. (!midx || midx != sk->sk_bound_dev_if))
  524. goto e_inval;
  525. }
  526. np->mcast_oif = val;
  527. retv = 0;
  528. break;
  529. case IPV6_ADD_MEMBERSHIP:
  530. case IPV6_DROP_MEMBERSHIP:
  531. {
  532. struct ipv6_mreq mreq;
  533. if (optlen < sizeof(struct ipv6_mreq))
  534. goto e_inval;
  535. retv = -EPROTO;
  536. if (inet_sk(sk)->is_icsk)
  537. break;
  538. retv = -EFAULT;
  539. if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
  540. break;
  541. if (optname == IPV6_ADD_MEMBERSHIP)
  542. retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
  543. else
  544. retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
  545. break;
  546. }
  547. case IPV6_JOIN_ANYCAST:
  548. case IPV6_LEAVE_ANYCAST:
  549. {
  550. struct ipv6_mreq mreq;
  551. if (optlen < sizeof(struct ipv6_mreq))
  552. goto e_inval;
  553. retv = -EFAULT;
  554. if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
  555. break;
  556. if (optname == IPV6_JOIN_ANYCAST)
  557. retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
  558. else
  559. retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
  560. break;
  561. }
  562. case MCAST_JOIN_GROUP:
  563. case MCAST_LEAVE_GROUP:
  564. {
  565. struct group_req greq;
  566. struct sockaddr_in6 *psin6;
  567. if (optlen < sizeof(struct group_req))
  568. goto e_inval;
  569. retv = -EFAULT;
  570. if (copy_from_user(&greq, optval, sizeof(struct group_req)))
  571. break;
  572. if (greq.gr_group.ss_family != AF_INET6) {
  573. retv = -EADDRNOTAVAIL;
  574. break;
  575. }
  576. psin6 = (struct sockaddr_in6 *)&greq.gr_group;
  577. if (optname == MCAST_JOIN_GROUP)
  578. retv = ipv6_sock_mc_join(sk, greq.gr_interface,
  579. &psin6->sin6_addr);
  580. else
  581. retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
  582. &psin6->sin6_addr);
  583. break;
  584. }
  585. case MCAST_JOIN_SOURCE_GROUP:
  586. case MCAST_LEAVE_SOURCE_GROUP:
  587. case MCAST_BLOCK_SOURCE:
  588. case MCAST_UNBLOCK_SOURCE:
  589. {
  590. struct group_source_req greqs;
  591. int omode, add;
  592. if (optlen < sizeof(struct group_source_req))
  593. goto e_inval;
  594. if (copy_from_user(&greqs, optval, sizeof(greqs))) {
  595. retv = -EFAULT;
  596. break;
  597. }
  598. if (greqs.gsr_group.ss_family != AF_INET6 ||
  599. greqs.gsr_source.ss_family != AF_INET6) {
  600. retv = -EADDRNOTAVAIL;
  601. break;
  602. }
  603. if (optname == MCAST_BLOCK_SOURCE) {
  604. omode = MCAST_EXCLUDE;
  605. add = 1;
  606. } else if (optname == MCAST_UNBLOCK_SOURCE) {
  607. omode = MCAST_EXCLUDE;
  608. add = 0;
  609. } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
  610. struct sockaddr_in6 *psin6;
  611. psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
  612. retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
  613. &psin6->sin6_addr);
  614. /* prior join w/ different source is ok */
  615. if (retv && retv != -EADDRINUSE)
  616. break;
  617. omode = MCAST_INCLUDE;
  618. add = 1;
  619. } else /* MCAST_LEAVE_SOURCE_GROUP */ {
  620. omode = MCAST_INCLUDE;
  621. add = 0;
  622. }
  623. retv = ip6_mc_source(add, omode, sk, &greqs);
  624. break;
  625. }
  626. case MCAST_MSFILTER:
  627. {
  628. struct group_filter *gsf;
  629. if (optlen < GROUP_FILTER_SIZE(0))
  630. goto e_inval;
  631. if (optlen > sysctl_optmem_max) {
  632. retv = -ENOBUFS;
  633. break;
  634. }
  635. gsf = kmalloc(optlen, GFP_KERNEL);
  636. if (!gsf) {
  637. retv = -ENOBUFS;
  638. break;
  639. }
  640. retv = -EFAULT;
  641. if (copy_from_user(gsf, optval, optlen)) {
  642. kfree(gsf);
  643. break;
  644. }
  645. /* numsrc >= (4G-140)/128 overflow in 32 bits */
  646. if (gsf->gf_numsrc >= 0x1ffffffU ||
  647. gsf->gf_numsrc > sysctl_mld_max_msf) {
  648. kfree(gsf);
  649. retv = -ENOBUFS;
  650. break;
  651. }
  652. if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
  653. kfree(gsf);
  654. retv = -EINVAL;
  655. break;
  656. }
  657. retv = ip6_mc_msfilter(sk, gsf);
  658. kfree(gsf);
  659. break;
  660. }
  661. case IPV6_ROUTER_ALERT:
  662. if (optlen < sizeof(int))
  663. goto e_inval;
  664. retv = ip6_ra_control(sk, val);
  665. break;
  666. case IPV6_MTU_DISCOVER:
  667. if (optlen < sizeof(int))
  668. goto e_inval;
  669. if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
  670. goto e_inval;
  671. np->pmtudisc = val;
  672. retv = 0;
  673. break;
  674. case IPV6_MTU:
  675. if (optlen < sizeof(int))
  676. goto e_inval;
  677. if (val && val < IPV6_MIN_MTU)
  678. goto e_inval;
  679. np->frag_size = val;
  680. retv = 0;
  681. break;
  682. case IPV6_RECVERR:
  683. if (optlen < sizeof(int))
  684. goto e_inval;
  685. np->recverr = valbool;
  686. if (!val)
  687. skb_queue_purge(&sk->sk_error_queue);
  688. retv = 0;
  689. break;
  690. case IPV6_FLOWINFO_SEND:
  691. if (optlen < sizeof(int))
  692. goto e_inval;
  693. np->sndflow = valbool;
  694. retv = 0;
  695. break;
  696. case IPV6_FLOWLABEL_MGR:
  697. retv = ipv6_flowlabel_opt(sk, optval, optlen);
  698. break;
  699. case IPV6_IPSEC_POLICY:
  700. case IPV6_XFRM_POLICY:
  701. retv = -EPERM;
  702. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  703. break;
  704. retv = xfrm_user_policy(sk, optname, optval, optlen);
  705. break;
  706. case IPV6_ADDR_PREFERENCES:
  707. {
  708. unsigned int pref = 0;
  709. unsigned int prefmask = ~0;
  710. if (optlen < sizeof(int))
  711. goto e_inval;
  712. retv = -EINVAL;
  713. /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
  714. switch (val & (IPV6_PREFER_SRC_PUBLIC|
  715. IPV6_PREFER_SRC_TMP|
  716. IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
  717. case IPV6_PREFER_SRC_PUBLIC:
  718. pref |= IPV6_PREFER_SRC_PUBLIC;
  719. break;
  720. case IPV6_PREFER_SRC_TMP:
  721. pref |= IPV6_PREFER_SRC_TMP;
  722. break;
  723. case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
  724. break;
  725. case 0:
  726. goto pref_skip_pubtmp;
  727. default:
  728. goto e_inval;
  729. }
  730. prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
  731. IPV6_PREFER_SRC_TMP);
  732. pref_skip_pubtmp:
  733. /* check HOME/COA conflicts */
  734. switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
  735. case IPV6_PREFER_SRC_HOME:
  736. break;
  737. case IPV6_PREFER_SRC_COA:
  738. pref |= IPV6_PREFER_SRC_COA;
  739. case 0:
  740. goto pref_skip_coa;
  741. default:
  742. goto e_inval;
  743. }
  744. prefmask &= ~IPV6_PREFER_SRC_COA;
  745. pref_skip_coa:
  746. /* check CGA/NONCGA conflicts */
  747. switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
  748. case IPV6_PREFER_SRC_CGA:
  749. case IPV6_PREFER_SRC_NONCGA:
  750. case 0:
  751. break;
  752. default:
  753. goto e_inval;
  754. }
  755. np->srcprefs = (np->srcprefs & prefmask) | pref;
  756. retv = 0;
  757. break;
  758. }
  759. case IPV6_MINHOPCOUNT:
  760. if (optlen < sizeof(int))
  761. goto e_inval;
  762. if (val < 0 || val > 255)
  763. goto e_inval;
  764. np->min_hopcount = val;
  765. retv = 0;
  766. break;
  767. case IPV6_DONTFRAG:
  768. np->dontfrag = valbool;
  769. retv = 0;
  770. break;
  771. case IPV6_AUTOFLOWLABEL:
  772. np->autoflowlabel = valbool;
  773. np->autoflowlabel_set = 1;
  774. retv = 0;
  775. break;
  776. }
  777. release_sock(sk);
  778. if (needs_rtnl)
  779. rtnl_unlock();
  780. return retv;
  781. e_inval:
  782. release_sock(sk);
  783. if (needs_rtnl)
  784. rtnl_unlock();
  785. return -EINVAL;
  786. }
  787. int ipv6_setsockopt(struct sock *sk, int level, int optname,
  788. char __user *optval, unsigned int optlen)
  789. {
  790. int err;
  791. if (level == SOL_IP && sk->sk_type != SOCK_RAW)
  792. return udp_prot.setsockopt(sk, level, optname, optval, optlen);
  793. if (level != SOL_IPV6)
  794. return -ENOPROTOOPT;
  795. err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
  796. #ifdef CONFIG_NETFILTER
  797. /* we need to exclude all possible ENOPROTOOPTs except default case */
  798. if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
  799. optname != IPV6_XFRM_POLICY)
  800. err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
  801. #endif
  802. return err;
  803. }
  804. EXPORT_SYMBOL(ipv6_setsockopt);
  805. #ifdef CONFIG_COMPAT
  806. int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
  807. char __user *optval, unsigned int optlen)
  808. {
  809. int err;
  810. if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
  811. if (udp_prot.compat_setsockopt != NULL)
  812. return udp_prot.compat_setsockopt(sk, level, optname,
  813. optval, optlen);
  814. return udp_prot.setsockopt(sk, level, optname, optval, optlen);
  815. }
  816. if (level != SOL_IPV6)
  817. return -ENOPROTOOPT;
  818. if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
  819. return compat_mc_setsockopt(sk, level, optname, optval, optlen,
  820. ipv6_setsockopt);
  821. err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
  822. #ifdef CONFIG_NETFILTER
  823. /* we need to exclude all possible ENOPROTOOPTs except default case */
  824. if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
  825. optname != IPV6_XFRM_POLICY)
  826. err = compat_nf_setsockopt(sk, PF_INET6, optname, optval,
  827. optlen);
  828. #endif
  829. return err;
  830. }
  831. EXPORT_SYMBOL(compat_ipv6_setsockopt);
  832. #endif
  833. static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
  834. int optname, char __user *optval, int len)
  835. {
  836. struct ipv6_opt_hdr *hdr;
  837. if (!opt)
  838. return 0;
  839. switch (optname) {
  840. case IPV6_HOPOPTS:
  841. hdr = opt->hopopt;
  842. break;
  843. case IPV6_RTHDRDSTOPTS:
  844. hdr = opt->dst0opt;
  845. break;
  846. case IPV6_RTHDR:
  847. hdr = (struct ipv6_opt_hdr *)opt->srcrt;
  848. break;
  849. case IPV6_DSTOPTS:
  850. hdr = opt->dst1opt;
  851. break;
  852. default:
  853. return -EINVAL; /* should not happen */
  854. }
  855. if (!hdr)
  856. return 0;
  857. len = min_t(unsigned int, len, ipv6_optlen(hdr));
  858. if (copy_to_user(optval, hdr, len))
  859. return -EFAULT;
  860. return len;
  861. }
  862. static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
  863. char __user *optval, int __user *optlen, unsigned int flags)
  864. {
  865. struct ipv6_pinfo *np = inet6_sk(sk);
  866. int len;
  867. int val;
  868. if (ip6_mroute_opt(optname))
  869. return ip6_mroute_getsockopt(sk, optname, optval, optlen);
  870. if (get_user(len, optlen))
  871. return -EFAULT;
  872. switch (optname) {
  873. case IPV6_ADDRFORM:
  874. if (sk->sk_protocol != IPPROTO_UDP &&
  875. sk->sk_protocol != IPPROTO_UDPLITE &&
  876. sk->sk_protocol != IPPROTO_TCP)
  877. return -ENOPROTOOPT;
  878. if (sk->sk_state != TCP_ESTABLISHED)
  879. return -ENOTCONN;
  880. val = sk->sk_family;
  881. break;
  882. case MCAST_MSFILTER:
  883. {
  884. struct group_filter gsf;
  885. int err;
  886. if (len < GROUP_FILTER_SIZE(0))
  887. return -EINVAL;
  888. if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
  889. return -EFAULT;
  890. if (gsf.gf_group.ss_family != AF_INET6)
  891. return -EADDRNOTAVAIL;
  892. lock_sock(sk);
  893. err = ip6_mc_msfget(sk, &gsf,
  894. (struct group_filter __user *)optval, optlen);
  895. release_sock(sk);
  896. return err;
  897. }
  898. case IPV6_2292PKTOPTIONS:
  899. {
  900. struct msghdr msg;
  901. struct sk_buff *skb;
  902. if (sk->sk_type != SOCK_STREAM)
  903. return -ENOPROTOOPT;
  904. msg.msg_control = optval;
  905. msg.msg_controllen = len;
  906. msg.msg_flags = flags;
  907. lock_sock(sk);
  908. skb = np->pktoptions;
  909. if (skb)
  910. ip6_datagram_recv_ctl(sk, &msg, skb);
  911. release_sock(sk);
  912. if (!skb) {
  913. if (np->rxopt.bits.rxinfo) {
  914. struct in6_pktinfo src_info;
  915. src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
  916. np->sticky_pktinfo.ipi6_ifindex;
  917. src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
  918. put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
  919. }
  920. if (np->rxopt.bits.rxhlim) {
  921. int hlim = np->mcast_hops;
  922. put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
  923. }
  924. if (np->rxopt.bits.rxtclass) {
  925. int tclass = (int)ip6_tclass(np->rcv_flowinfo);
  926. put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
  927. }
  928. if (np->rxopt.bits.rxoinfo) {
  929. struct in6_pktinfo src_info;
  930. src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
  931. np->sticky_pktinfo.ipi6_ifindex;
  932. src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
  933. np->sticky_pktinfo.ipi6_addr;
  934. put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
  935. }
  936. if (np->rxopt.bits.rxohlim) {
  937. int hlim = np->mcast_hops;
  938. put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
  939. }
  940. if (np->rxopt.bits.rxflow) {
  941. __be32 flowinfo = np->rcv_flowinfo;
  942. put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
  943. }
  944. }
  945. len -= msg.msg_controllen;
  946. return put_user(len, optlen);
  947. }
  948. case IPV6_MTU:
  949. {
  950. struct dst_entry *dst;
  951. val = 0;
  952. rcu_read_lock();
  953. dst = __sk_dst_get(sk);
  954. if (dst)
  955. val = dst_mtu(dst);
  956. rcu_read_unlock();
  957. if (!val)
  958. return -ENOTCONN;
  959. break;
  960. }
  961. case IPV6_V6ONLY:
  962. val = sk->sk_ipv6only;
  963. break;
  964. case IPV6_RECVPKTINFO:
  965. val = np->rxopt.bits.rxinfo;
  966. break;
  967. case IPV6_2292PKTINFO:
  968. val = np->rxopt.bits.rxoinfo;
  969. break;
  970. case IPV6_RECVHOPLIMIT:
  971. val = np->rxopt.bits.rxhlim;
  972. break;
  973. case IPV6_2292HOPLIMIT:
  974. val = np->rxopt.bits.rxohlim;
  975. break;
  976. case IPV6_RECVRTHDR:
  977. val = np->rxopt.bits.srcrt;
  978. break;
  979. case IPV6_2292RTHDR:
  980. val = np->rxopt.bits.osrcrt;
  981. break;
  982. case IPV6_HOPOPTS:
  983. case IPV6_RTHDRDSTOPTS:
  984. case IPV6_RTHDR:
  985. case IPV6_DSTOPTS:
  986. {
  987. struct ipv6_txoptions *opt;
  988. lock_sock(sk);
  989. opt = rcu_dereference_protected(np->opt, sock_owned_by_user(sk));
  990. len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
  991. release_sock(sk);
  992. /* check if ipv6_getsockopt_sticky() returns err code */
  993. if (len < 0)
  994. return len;
  995. return put_user(len, optlen);
  996. }
  997. case IPV6_RECVHOPOPTS:
  998. val = np->rxopt.bits.hopopts;
  999. break;
  1000. case IPV6_2292HOPOPTS:
  1001. val = np->rxopt.bits.ohopopts;
  1002. break;
  1003. case IPV6_RECVDSTOPTS:
  1004. val = np->rxopt.bits.dstopts;
  1005. break;
  1006. case IPV6_2292DSTOPTS:
  1007. val = np->rxopt.bits.odstopts;
  1008. break;
  1009. case IPV6_TCLASS:
  1010. val = np->tclass;
  1011. break;
  1012. case IPV6_RECVTCLASS:
  1013. val = np->rxopt.bits.rxtclass;
  1014. break;
  1015. case IPV6_FLOWINFO:
  1016. val = np->rxopt.bits.rxflow;
  1017. break;
  1018. case IPV6_RECVPATHMTU:
  1019. val = np->rxopt.bits.rxpmtu;
  1020. break;
  1021. case IPV6_PATHMTU:
  1022. {
  1023. struct dst_entry *dst;
  1024. struct ip6_mtuinfo mtuinfo;
  1025. if (len < sizeof(mtuinfo))
  1026. return -EINVAL;
  1027. len = sizeof(mtuinfo);
  1028. memset(&mtuinfo, 0, sizeof(mtuinfo));
  1029. rcu_read_lock();
  1030. dst = __sk_dst_get(sk);
  1031. if (dst)
  1032. mtuinfo.ip6m_mtu = dst_mtu(dst);
  1033. rcu_read_unlock();
  1034. if (!mtuinfo.ip6m_mtu)
  1035. return -ENOTCONN;
  1036. if (put_user(len, optlen))
  1037. return -EFAULT;
  1038. if (copy_to_user(optval, &mtuinfo, len))
  1039. return -EFAULT;
  1040. return 0;
  1041. }
  1042. case IPV6_TRANSPARENT:
  1043. val = inet_sk(sk)->transparent;
  1044. break;
  1045. case IPV6_RECVORIGDSTADDR:
  1046. val = np->rxopt.bits.rxorigdstaddr;
  1047. break;
  1048. case IPV6_UNICAST_HOPS:
  1049. case IPV6_MULTICAST_HOPS:
  1050. {
  1051. struct dst_entry *dst;
  1052. if (optname == IPV6_UNICAST_HOPS)
  1053. val = np->hop_limit;
  1054. else
  1055. val = np->mcast_hops;
  1056. if (val < 0) {
  1057. rcu_read_lock();
  1058. dst = __sk_dst_get(sk);
  1059. if (dst)
  1060. val = ip6_dst_hoplimit(dst);
  1061. rcu_read_unlock();
  1062. }
  1063. if (val < 0)
  1064. val = sock_net(sk)->ipv6.devconf_all->hop_limit;
  1065. break;
  1066. }
  1067. case IPV6_MULTICAST_LOOP:
  1068. val = np->mc_loop;
  1069. break;
  1070. case IPV6_MULTICAST_IF:
  1071. val = np->mcast_oif;
  1072. break;
  1073. case IPV6_UNICAST_IF:
  1074. val = (__force int)htonl((__u32) np->ucast_oif);
  1075. break;
  1076. case IPV6_MTU_DISCOVER:
  1077. val = np->pmtudisc;
  1078. break;
  1079. case IPV6_RECVERR:
  1080. val = np->recverr;
  1081. break;
  1082. case IPV6_FLOWINFO_SEND:
  1083. val = np->sndflow;
  1084. break;
  1085. case IPV6_FLOWLABEL_MGR:
  1086. {
  1087. struct in6_flowlabel_req freq;
  1088. int flags;
  1089. if (len < sizeof(freq))
  1090. return -EINVAL;
  1091. if (copy_from_user(&freq, optval, sizeof(freq)))
  1092. return -EFAULT;
  1093. if (freq.flr_action != IPV6_FL_A_GET)
  1094. return -EINVAL;
  1095. len = sizeof(freq);
  1096. flags = freq.flr_flags;
  1097. memset(&freq, 0, sizeof(freq));
  1098. val = ipv6_flowlabel_opt_get(sk, &freq, flags);
  1099. if (val < 0)
  1100. return val;
  1101. if (put_user(len, optlen))
  1102. return -EFAULT;
  1103. if (copy_to_user(optval, &freq, len))
  1104. return -EFAULT;
  1105. return 0;
  1106. }
  1107. case IPV6_ADDR_PREFERENCES:
  1108. val = 0;
  1109. if (np->srcprefs & IPV6_PREFER_SRC_TMP)
  1110. val |= IPV6_PREFER_SRC_TMP;
  1111. else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
  1112. val |= IPV6_PREFER_SRC_PUBLIC;
  1113. else {
  1114. /* XXX: should we return system default? */
  1115. val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
  1116. }
  1117. if (np->srcprefs & IPV6_PREFER_SRC_COA)
  1118. val |= IPV6_PREFER_SRC_COA;
  1119. else
  1120. val |= IPV6_PREFER_SRC_HOME;
  1121. break;
  1122. case IPV6_MINHOPCOUNT:
  1123. val = np->min_hopcount;
  1124. break;
  1125. case IPV6_DONTFRAG:
  1126. val = np->dontfrag;
  1127. break;
  1128. case IPV6_AUTOFLOWLABEL:
  1129. val = ip6_autoflowlabel(sock_net(sk), np);
  1130. break;
  1131. default:
  1132. return -ENOPROTOOPT;
  1133. }
  1134. len = min_t(unsigned int, sizeof(int), len);
  1135. if (put_user(len, optlen))
  1136. return -EFAULT;
  1137. if (copy_to_user(optval, &val, len))
  1138. return -EFAULT;
  1139. return 0;
  1140. }
  1141. int ipv6_getsockopt(struct sock *sk, int level, int optname,
  1142. char __user *optval, int __user *optlen)
  1143. {
  1144. int err;
  1145. if (level == SOL_IP && sk->sk_type != SOCK_RAW)
  1146. return udp_prot.getsockopt(sk, level, optname, optval, optlen);
  1147. if (level != SOL_IPV6)
  1148. return -ENOPROTOOPT;
  1149. err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
  1150. #ifdef CONFIG_NETFILTER
  1151. /* we need to exclude all possible ENOPROTOOPTs except default case */
  1152. if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
  1153. int len;
  1154. if (get_user(len, optlen))
  1155. return -EFAULT;
  1156. err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
  1157. if (err >= 0)
  1158. err = put_user(len, optlen);
  1159. }
  1160. #endif
  1161. return err;
  1162. }
  1163. EXPORT_SYMBOL(ipv6_getsockopt);
  1164. #ifdef CONFIG_COMPAT
  1165. int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
  1166. char __user *optval, int __user *optlen)
  1167. {
  1168. int err;
  1169. if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
  1170. if (udp_prot.compat_getsockopt != NULL)
  1171. return udp_prot.compat_getsockopt(sk, level, optname,
  1172. optval, optlen);
  1173. return udp_prot.getsockopt(sk, level, optname, optval, optlen);
  1174. }
  1175. if (level != SOL_IPV6)
  1176. return -ENOPROTOOPT;
  1177. if (optname == MCAST_MSFILTER)
  1178. return compat_mc_getsockopt(sk, level, optname, optval, optlen,
  1179. ipv6_getsockopt);
  1180. err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
  1181. MSG_CMSG_COMPAT);
  1182. #ifdef CONFIG_NETFILTER
  1183. /* we need to exclude all possible ENOPROTOOPTs except default case */
  1184. if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
  1185. int len;
  1186. if (get_user(len, optlen))
  1187. return -EFAULT;
  1188. err = compat_nf_getsockopt(sk, PF_INET6, optname, optval, &len);
  1189. if (err >= 0)
  1190. err = put_user(len, optlen);
  1191. }
  1192. #endif
  1193. return err;
  1194. }
  1195. EXPORT_SYMBOL(compat_ipv6_getsockopt);
  1196. #endif