ipv6_sockglue.c 29 KB

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