ipv6_sockglue.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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. struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
  91. struct ipv6_txoptions *opt)
  92. {
  93. if (inet_sk(sk)->is_icsk) {
  94. if (opt &&
  95. !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
  96. inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
  97. struct inet_connection_sock *icsk = inet_csk(sk);
  98. icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
  99. icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
  100. }
  101. }
  102. opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt,
  103. 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_ADDRFORM:
  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. struct ipv6_opt_hdr *new = NULL;
  347. /* hop-by-hop / destination options are privileged option */
  348. retv = -EPERM;
  349. if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
  350. break;
  351. /* remove any sticky options header with a zero option
  352. * length, per RFC3542.
  353. */
  354. if (optlen == 0)
  355. optval = NULL;
  356. else if (!optval)
  357. goto e_inval;
  358. else if (optlen < sizeof(struct ipv6_opt_hdr) ||
  359. optlen & 0x7 || optlen > 8 * 255)
  360. goto e_inval;
  361. else {
  362. new = memdup_user(optval, optlen);
  363. if (IS_ERR(new)) {
  364. retv = PTR_ERR(new);
  365. break;
  366. }
  367. if (unlikely(ipv6_optlen(new) > optlen)) {
  368. kfree(new);
  369. goto e_inval;
  370. }
  371. }
  372. opt = rcu_dereference_protected(np->opt,
  373. lockdep_sock_is_held(sk));
  374. opt = ipv6_renew_options(sk, opt, optname, new);
  375. kfree(new);
  376. if (IS_ERR(opt)) {
  377. retv = PTR_ERR(opt);
  378. break;
  379. }
  380. /* routing header option needs extra check */
  381. retv = -EINVAL;
  382. if (optname == IPV6_RTHDR && opt && opt->srcrt) {
  383. struct ipv6_rt_hdr *rthdr = opt->srcrt;
  384. switch (rthdr->type) {
  385. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  386. case IPV6_SRCRT_TYPE_2:
  387. if (rthdr->hdrlen != 2 ||
  388. rthdr->segments_left != 1)
  389. goto sticky_done;
  390. break;
  391. #endif
  392. default:
  393. goto sticky_done;
  394. }
  395. }
  396. retv = 0;
  397. opt = ipv6_update_options(sk, opt);
  398. sticky_done:
  399. if (opt) {
  400. atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
  401. txopt_put(opt);
  402. }
  403. break;
  404. }
  405. case IPV6_PKTINFO:
  406. {
  407. struct in6_pktinfo pkt;
  408. if (optlen == 0)
  409. goto e_inval;
  410. else if (optlen < sizeof(struct in6_pktinfo) || !optval)
  411. goto e_inval;
  412. if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
  413. retv = -EFAULT;
  414. break;
  415. }
  416. if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
  417. goto e_inval;
  418. np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
  419. np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
  420. retv = 0;
  421. break;
  422. }
  423. case IPV6_2292PKTOPTIONS:
  424. {
  425. struct ipv6_txoptions *opt = NULL;
  426. struct msghdr msg;
  427. struct flowi6 fl6;
  428. struct sockcm_cookie sockc_junk;
  429. struct ipcm6_cookie ipc6;
  430. memset(&fl6, 0, sizeof(fl6));
  431. fl6.flowi6_oif = sk->sk_bound_dev_if;
  432. fl6.flowi6_mark = sk->sk_mark;
  433. if (optlen == 0)
  434. goto update;
  435. /* 1K is probably excessive
  436. * 1K is surely not enough, 2K per standard header is 16K.
  437. */
  438. retv = -EINVAL;
  439. if (optlen > 64*1024)
  440. break;
  441. opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
  442. retv = -ENOBUFS;
  443. if (!opt)
  444. break;
  445. memset(opt, 0, sizeof(*opt));
  446. atomic_set(&opt->refcnt, 1);
  447. opt->tot_len = sizeof(*opt) + optlen;
  448. retv = -EFAULT;
  449. if (copy_from_user(opt+1, optval, optlen))
  450. goto done;
  451. msg.msg_controllen = optlen;
  452. msg.msg_control = (void *)(opt+1);
  453. ipc6.opt = opt;
  454. retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6, &sockc_junk);
  455. if (retv)
  456. goto done;
  457. update:
  458. retv = 0;
  459. opt = ipv6_update_options(sk, opt);
  460. done:
  461. if (opt) {
  462. atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
  463. txopt_put(opt);
  464. }
  465. break;
  466. }
  467. case IPV6_UNICAST_HOPS:
  468. if (optlen < sizeof(int))
  469. goto e_inval;
  470. if (val > 255 || val < -1)
  471. goto e_inval;
  472. np->hop_limit = val;
  473. retv = 0;
  474. break;
  475. case IPV6_MULTICAST_HOPS:
  476. if (sk->sk_type == SOCK_STREAM)
  477. break;
  478. if (optlen < sizeof(int))
  479. goto e_inval;
  480. if (val > 255 || val < -1)
  481. goto e_inval;
  482. np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
  483. retv = 0;
  484. break;
  485. case IPV6_MULTICAST_LOOP:
  486. if (optlen < sizeof(int))
  487. goto e_inval;
  488. if (val != valbool)
  489. goto e_inval;
  490. np->mc_loop = valbool;
  491. retv = 0;
  492. break;
  493. case IPV6_UNICAST_IF:
  494. {
  495. struct net_device *dev = NULL;
  496. int ifindex;
  497. if (optlen != sizeof(int))
  498. goto e_inval;
  499. ifindex = (__force int)ntohl((__force __be32)val);
  500. if (ifindex == 0) {
  501. np->ucast_oif = 0;
  502. retv = 0;
  503. break;
  504. }
  505. dev = dev_get_by_index(net, ifindex);
  506. retv = -EADDRNOTAVAIL;
  507. if (!dev)
  508. break;
  509. dev_put(dev);
  510. retv = -EINVAL;
  511. if (sk->sk_bound_dev_if)
  512. break;
  513. np->ucast_oif = ifindex;
  514. retv = 0;
  515. break;
  516. }
  517. case IPV6_MULTICAST_IF:
  518. if (sk->sk_type == SOCK_STREAM)
  519. break;
  520. if (optlen < sizeof(int))
  521. goto e_inval;
  522. if (val) {
  523. struct net_device *dev;
  524. int midx;
  525. rcu_read_lock();
  526. dev = dev_get_by_index_rcu(net, val);
  527. if (!dev) {
  528. rcu_read_unlock();
  529. retv = -ENODEV;
  530. break;
  531. }
  532. midx = l3mdev_master_ifindex_rcu(dev);
  533. rcu_read_unlock();
  534. if (sk->sk_bound_dev_if &&
  535. sk->sk_bound_dev_if != val &&
  536. (!midx || midx != sk->sk_bound_dev_if))
  537. goto e_inval;
  538. }
  539. np->mcast_oif = val;
  540. retv = 0;
  541. break;
  542. case IPV6_ADD_MEMBERSHIP:
  543. case IPV6_DROP_MEMBERSHIP:
  544. {
  545. struct ipv6_mreq mreq;
  546. if (optlen < sizeof(struct ipv6_mreq))
  547. goto e_inval;
  548. retv = -EPROTO;
  549. if (inet_sk(sk)->is_icsk)
  550. break;
  551. retv = -EFAULT;
  552. if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
  553. break;
  554. if (optname == IPV6_ADD_MEMBERSHIP)
  555. retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
  556. else
  557. retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
  558. break;
  559. }
  560. case IPV6_JOIN_ANYCAST:
  561. case IPV6_LEAVE_ANYCAST:
  562. {
  563. struct ipv6_mreq mreq;
  564. if (optlen < sizeof(struct ipv6_mreq))
  565. goto e_inval;
  566. retv = -EFAULT;
  567. if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
  568. break;
  569. if (optname == IPV6_JOIN_ANYCAST)
  570. retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
  571. else
  572. retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
  573. break;
  574. }
  575. case MCAST_JOIN_GROUP:
  576. case MCAST_LEAVE_GROUP:
  577. {
  578. struct group_req greq;
  579. struct sockaddr_in6 *psin6;
  580. if (optlen < sizeof(struct group_req))
  581. goto e_inval;
  582. retv = -EFAULT;
  583. if (copy_from_user(&greq, optval, sizeof(struct group_req)))
  584. break;
  585. if (greq.gr_group.ss_family != AF_INET6) {
  586. retv = -EADDRNOTAVAIL;
  587. break;
  588. }
  589. psin6 = (struct sockaddr_in6 *)&greq.gr_group;
  590. if (optname == MCAST_JOIN_GROUP)
  591. retv = ipv6_sock_mc_join(sk, greq.gr_interface,
  592. &psin6->sin6_addr);
  593. else
  594. retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
  595. &psin6->sin6_addr);
  596. break;
  597. }
  598. case MCAST_JOIN_SOURCE_GROUP:
  599. case MCAST_LEAVE_SOURCE_GROUP:
  600. case MCAST_BLOCK_SOURCE:
  601. case MCAST_UNBLOCK_SOURCE:
  602. {
  603. struct group_source_req greqs;
  604. int omode, add;
  605. if (optlen < sizeof(struct group_source_req))
  606. goto e_inval;
  607. if (copy_from_user(&greqs, optval, sizeof(greqs))) {
  608. retv = -EFAULT;
  609. break;
  610. }
  611. if (greqs.gsr_group.ss_family != AF_INET6 ||
  612. greqs.gsr_source.ss_family != AF_INET6) {
  613. retv = -EADDRNOTAVAIL;
  614. break;
  615. }
  616. if (optname == MCAST_BLOCK_SOURCE) {
  617. omode = MCAST_EXCLUDE;
  618. add = 1;
  619. } else if (optname == MCAST_UNBLOCK_SOURCE) {
  620. omode = MCAST_EXCLUDE;
  621. add = 0;
  622. } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
  623. struct sockaddr_in6 *psin6;
  624. psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
  625. retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
  626. &psin6->sin6_addr);
  627. /* prior join w/ different source is ok */
  628. if (retv && retv != -EADDRINUSE)
  629. break;
  630. omode = MCAST_INCLUDE;
  631. add = 1;
  632. } else /* MCAST_LEAVE_SOURCE_GROUP */ {
  633. omode = MCAST_INCLUDE;
  634. add = 0;
  635. }
  636. retv = ip6_mc_source(add, omode, sk, &greqs);
  637. break;
  638. }
  639. case MCAST_MSFILTER:
  640. {
  641. struct group_filter *gsf;
  642. if (optlen < GROUP_FILTER_SIZE(0))
  643. goto e_inval;
  644. if (optlen > sysctl_optmem_max) {
  645. retv = -ENOBUFS;
  646. break;
  647. }
  648. gsf = kmalloc(optlen, GFP_KERNEL);
  649. if (!gsf) {
  650. retv = -ENOBUFS;
  651. break;
  652. }
  653. retv = -EFAULT;
  654. if (copy_from_user(gsf, optval, optlen)) {
  655. kfree(gsf);
  656. break;
  657. }
  658. /* numsrc >= (4G-140)/128 overflow in 32 bits */
  659. if (gsf->gf_numsrc >= 0x1ffffffU ||
  660. gsf->gf_numsrc > sysctl_mld_max_msf) {
  661. kfree(gsf);
  662. retv = -ENOBUFS;
  663. break;
  664. }
  665. if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
  666. kfree(gsf);
  667. retv = -EINVAL;
  668. break;
  669. }
  670. retv = ip6_mc_msfilter(sk, gsf);
  671. kfree(gsf);
  672. break;
  673. }
  674. case IPV6_ROUTER_ALERT:
  675. if (optlen < sizeof(int))
  676. goto e_inval;
  677. retv = ip6_ra_control(sk, val);
  678. break;
  679. case IPV6_MTU_DISCOVER:
  680. if (optlen < sizeof(int))
  681. goto e_inval;
  682. if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
  683. goto e_inval;
  684. np->pmtudisc = val;
  685. retv = 0;
  686. break;
  687. case IPV6_MTU:
  688. if (optlen < sizeof(int))
  689. goto e_inval;
  690. if (val && val < IPV6_MIN_MTU)
  691. goto e_inval;
  692. np->frag_size = val;
  693. retv = 0;
  694. break;
  695. case IPV6_RECVERR:
  696. if (optlen < sizeof(int))
  697. goto e_inval;
  698. np->recverr = valbool;
  699. if (!val)
  700. skb_queue_purge(&sk->sk_error_queue);
  701. retv = 0;
  702. break;
  703. case IPV6_FLOWINFO_SEND:
  704. if (optlen < sizeof(int))
  705. goto e_inval;
  706. np->sndflow = valbool;
  707. retv = 0;
  708. break;
  709. case IPV6_FLOWLABEL_MGR:
  710. retv = ipv6_flowlabel_opt(sk, optval, optlen);
  711. break;
  712. case IPV6_IPSEC_POLICY:
  713. case IPV6_XFRM_POLICY:
  714. retv = -EPERM;
  715. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  716. break;
  717. retv = xfrm_user_policy(sk, optname, optval, optlen);
  718. break;
  719. case IPV6_ADDR_PREFERENCES:
  720. {
  721. unsigned int pref = 0;
  722. unsigned int prefmask = ~0;
  723. if (optlen < sizeof(int))
  724. goto e_inval;
  725. retv = -EINVAL;
  726. /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
  727. switch (val & (IPV6_PREFER_SRC_PUBLIC|
  728. IPV6_PREFER_SRC_TMP|
  729. IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
  730. case IPV6_PREFER_SRC_PUBLIC:
  731. pref |= IPV6_PREFER_SRC_PUBLIC;
  732. break;
  733. case IPV6_PREFER_SRC_TMP:
  734. pref |= IPV6_PREFER_SRC_TMP;
  735. break;
  736. case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
  737. break;
  738. case 0:
  739. goto pref_skip_pubtmp;
  740. default:
  741. goto e_inval;
  742. }
  743. prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
  744. IPV6_PREFER_SRC_TMP);
  745. pref_skip_pubtmp:
  746. /* check HOME/COA conflicts */
  747. switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
  748. case IPV6_PREFER_SRC_HOME:
  749. break;
  750. case IPV6_PREFER_SRC_COA:
  751. pref |= IPV6_PREFER_SRC_COA;
  752. case 0:
  753. goto pref_skip_coa;
  754. default:
  755. goto e_inval;
  756. }
  757. prefmask &= ~IPV6_PREFER_SRC_COA;
  758. pref_skip_coa:
  759. /* check CGA/NONCGA conflicts */
  760. switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
  761. case IPV6_PREFER_SRC_CGA:
  762. case IPV6_PREFER_SRC_NONCGA:
  763. case 0:
  764. break;
  765. default:
  766. goto e_inval;
  767. }
  768. np->srcprefs = (np->srcprefs & prefmask) | pref;
  769. retv = 0;
  770. break;
  771. }
  772. case IPV6_MINHOPCOUNT:
  773. if (optlen < sizeof(int))
  774. goto e_inval;
  775. if (val < 0 || val > 255)
  776. goto e_inval;
  777. np->min_hopcount = val;
  778. retv = 0;
  779. break;
  780. case IPV6_DONTFRAG:
  781. np->dontfrag = valbool;
  782. retv = 0;
  783. break;
  784. case IPV6_AUTOFLOWLABEL:
  785. np->autoflowlabel = valbool;
  786. np->autoflowlabel_set = 1;
  787. retv = 0;
  788. break;
  789. }
  790. release_sock(sk);
  791. if (needs_rtnl)
  792. rtnl_unlock();
  793. return retv;
  794. e_inval:
  795. release_sock(sk);
  796. if (needs_rtnl)
  797. rtnl_unlock();
  798. return -EINVAL;
  799. }
  800. int ipv6_setsockopt(struct sock *sk, int level, int optname,
  801. char __user *optval, unsigned int optlen)
  802. {
  803. int err;
  804. if (level == SOL_IP && sk->sk_type != SOCK_RAW)
  805. return udp_prot.setsockopt(sk, level, optname, optval, optlen);
  806. if (level != SOL_IPV6)
  807. return -ENOPROTOOPT;
  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. err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
  814. #endif
  815. return err;
  816. }
  817. EXPORT_SYMBOL(ipv6_setsockopt);
  818. #ifdef CONFIG_COMPAT
  819. int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
  820. char __user *optval, unsigned int optlen)
  821. {
  822. int err;
  823. if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
  824. if (udp_prot.compat_setsockopt != NULL)
  825. return udp_prot.compat_setsockopt(sk, level, optname,
  826. optval, optlen);
  827. return udp_prot.setsockopt(sk, level, optname, optval, optlen);
  828. }
  829. if (level != SOL_IPV6)
  830. return -ENOPROTOOPT;
  831. if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
  832. return compat_mc_setsockopt(sk, level, optname, optval, optlen,
  833. ipv6_setsockopt);
  834. err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
  835. #ifdef CONFIG_NETFILTER
  836. /* we need to exclude all possible ENOPROTOOPTs except default case */
  837. if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
  838. optname != IPV6_XFRM_POLICY)
  839. err = compat_nf_setsockopt(sk, PF_INET6, optname, optval,
  840. optlen);
  841. #endif
  842. return err;
  843. }
  844. EXPORT_SYMBOL(compat_ipv6_setsockopt);
  845. #endif
  846. static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
  847. int optname, char __user *optval, int len)
  848. {
  849. struct ipv6_opt_hdr *hdr;
  850. if (!opt)
  851. return 0;
  852. switch (optname) {
  853. case IPV6_HOPOPTS:
  854. hdr = opt->hopopt;
  855. break;
  856. case IPV6_RTHDRDSTOPTS:
  857. hdr = opt->dst0opt;
  858. break;
  859. case IPV6_RTHDR:
  860. hdr = (struct ipv6_opt_hdr *)opt->srcrt;
  861. break;
  862. case IPV6_DSTOPTS:
  863. hdr = opt->dst1opt;
  864. break;
  865. default:
  866. return -EINVAL; /* should not happen */
  867. }
  868. if (!hdr)
  869. return 0;
  870. len = min_t(unsigned int, len, ipv6_optlen(hdr));
  871. if (copy_to_user(optval, hdr, len))
  872. return -EFAULT;
  873. return len;
  874. }
  875. static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
  876. char __user *optval, int __user *optlen, unsigned int flags)
  877. {
  878. struct ipv6_pinfo *np = inet6_sk(sk);
  879. int len;
  880. int val;
  881. if (ip6_mroute_opt(optname))
  882. return ip6_mroute_getsockopt(sk, optname, optval, optlen);
  883. if (get_user(len, optlen))
  884. return -EFAULT;
  885. switch (optname) {
  886. case IPV6_ADDRFORM:
  887. if (sk->sk_protocol != IPPROTO_UDP &&
  888. sk->sk_protocol != IPPROTO_UDPLITE &&
  889. sk->sk_protocol != IPPROTO_TCP)
  890. return -ENOPROTOOPT;
  891. if (sk->sk_state != TCP_ESTABLISHED)
  892. return -ENOTCONN;
  893. val = sk->sk_family;
  894. break;
  895. case MCAST_MSFILTER:
  896. {
  897. struct group_filter gsf;
  898. int err;
  899. if (len < GROUP_FILTER_SIZE(0))
  900. return -EINVAL;
  901. if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
  902. return -EFAULT;
  903. if (gsf.gf_group.ss_family != AF_INET6)
  904. return -EADDRNOTAVAIL;
  905. lock_sock(sk);
  906. err = ip6_mc_msfget(sk, &gsf,
  907. (struct group_filter __user *)optval, optlen);
  908. release_sock(sk);
  909. return err;
  910. }
  911. case IPV6_2292PKTOPTIONS:
  912. {
  913. struct msghdr msg;
  914. struct sk_buff *skb;
  915. if (sk->sk_type != SOCK_STREAM)
  916. return -ENOPROTOOPT;
  917. msg.msg_control = optval;
  918. msg.msg_controllen = len;
  919. msg.msg_flags = flags;
  920. lock_sock(sk);
  921. skb = np->pktoptions;
  922. if (skb)
  923. ip6_datagram_recv_ctl(sk, &msg, skb);
  924. release_sock(sk);
  925. if (!skb) {
  926. if (np->rxopt.bits.rxinfo) {
  927. struct in6_pktinfo src_info;
  928. src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
  929. np->sticky_pktinfo.ipi6_ifindex;
  930. src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
  931. put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
  932. }
  933. if (np->rxopt.bits.rxhlim) {
  934. int hlim = np->mcast_hops;
  935. put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
  936. }
  937. if (np->rxopt.bits.rxtclass) {
  938. int tclass = (int)ip6_tclass(np->rcv_flowinfo);
  939. put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
  940. }
  941. if (np->rxopt.bits.rxoinfo) {
  942. struct in6_pktinfo src_info;
  943. src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
  944. np->sticky_pktinfo.ipi6_ifindex;
  945. src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
  946. np->sticky_pktinfo.ipi6_addr;
  947. put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
  948. }
  949. if (np->rxopt.bits.rxohlim) {
  950. int hlim = np->mcast_hops;
  951. put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
  952. }
  953. if (np->rxopt.bits.rxflow) {
  954. __be32 flowinfo = np->rcv_flowinfo;
  955. put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
  956. }
  957. }
  958. len -= msg.msg_controllen;
  959. return put_user(len, optlen);
  960. }
  961. case IPV6_MTU:
  962. {
  963. struct dst_entry *dst;
  964. val = 0;
  965. rcu_read_lock();
  966. dst = __sk_dst_get(sk);
  967. if (dst)
  968. val = dst_mtu(dst);
  969. rcu_read_unlock();
  970. if (!val)
  971. return -ENOTCONN;
  972. break;
  973. }
  974. case IPV6_V6ONLY:
  975. val = sk->sk_ipv6only;
  976. break;
  977. case IPV6_RECVPKTINFO:
  978. val = np->rxopt.bits.rxinfo;
  979. break;
  980. case IPV6_2292PKTINFO:
  981. val = np->rxopt.bits.rxoinfo;
  982. break;
  983. case IPV6_RECVHOPLIMIT:
  984. val = np->rxopt.bits.rxhlim;
  985. break;
  986. case IPV6_2292HOPLIMIT:
  987. val = np->rxopt.bits.rxohlim;
  988. break;
  989. case IPV6_RECVRTHDR:
  990. val = np->rxopt.bits.srcrt;
  991. break;
  992. case IPV6_2292RTHDR:
  993. val = np->rxopt.bits.osrcrt;
  994. break;
  995. case IPV6_HOPOPTS:
  996. case IPV6_RTHDRDSTOPTS:
  997. case IPV6_RTHDR:
  998. case IPV6_DSTOPTS:
  999. {
  1000. struct ipv6_txoptions *opt;
  1001. lock_sock(sk);
  1002. opt = rcu_dereference_protected(np->opt,
  1003. lockdep_sock_is_held(sk));
  1004. len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
  1005. release_sock(sk);
  1006. /* check if ipv6_getsockopt_sticky() returns err code */
  1007. if (len < 0)
  1008. return len;
  1009. return put_user(len, optlen);
  1010. }
  1011. case IPV6_RECVHOPOPTS:
  1012. val = np->rxopt.bits.hopopts;
  1013. break;
  1014. case IPV6_2292HOPOPTS:
  1015. val = np->rxopt.bits.ohopopts;
  1016. break;
  1017. case IPV6_RECVDSTOPTS:
  1018. val = np->rxopt.bits.dstopts;
  1019. break;
  1020. case IPV6_2292DSTOPTS:
  1021. val = np->rxopt.bits.odstopts;
  1022. break;
  1023. case IPV6_TCLASS:
  1024. val = np->tclass;
  1025. break;
  1026. case IPV6_RECVTCLASS:
  1027. val = np->rxopt.bits.rxtclass;
  1028. break;
  1029. case IPV6_FLOWINFO:
  1030. val = np->rxopt.bits.rxflow;
  1031. break;
  1032. case IPV6_RECVPATHMTU:
  1033. val = np->rxopt.bits.rxpmtu;
  1034. break;
  1035. case IPV6_PATHMTU:
  1036. {
  1037. struct dst_entry *dst;
  1038. struct ip6_mtuinfo mtuinfo;
  1039. if (len < sizeof(mtuinfo))
  1040. return -EINVAL;
  1041. len = sizeof(mtuinfo);
  1042. memset(&mtuinfo, 0, sizeof(mtuinfo));
  1043. rcu_read_lock();
  1044. dst = __sk_dst_get(sk);
  1045. if (dst)
  1046. mtuinfo.ip6m_mtu = dst_mtu(dst);
  1047. rcu_read_unlock();
  1048. if (!mtuinfo.ip6m_mtu)
  1049. return -ENOTCONN;
  1050. if (put_user(len, optlen))
  1051. return -EFAULT;
  1052. if (copy_to_user(optval, &mtuinfo, len))
  1053. return -EFAULT;
  1054. return 0;
  1055. }
  1056. case IPV6_TRANSPARENT:
  1057. val = inet_sk(sk)->transparent;
  1058. break;
  1059. case IPV6_RECVORIGDSTADDR:
  1060. val = np->rxopt.bits.rxorigdstaddr;
  1061. break;
  1062. case IPV6_UNICAST_HOPS:
  1063. case IPV6_MULTICAST_HOPS:
  1064. {
  1065. struct dst_entry *dst;
  1066. if (optname == IPV6_UNICAST_HOPS)
  1067. val = np->hop_limit;
  1068. else
  1069. val = np->mcast_hops;
  1070. if (val < 0) {
  1071. rcu_read_lock();
  1072. dst = __sk_dst_get(sk);
  1073. if (dst)
  1074. val = ip6_dst_hoplimit(dst);
  1075. rcu_read_unlock();
  1076. }
  1077. if (val < 0)
  1078. val = sock_net(sk)->ipv6.devconf_all->hop_limit;
  1079. break;
  1080. }
  1081. case IPV6_MULTICAST_LOOP:
  1082. val = np->mc_loop;
  1083. break;
  1084. case IPV6_MULTICAST_IF:
  1085. val = np->mcast_oif;
  1086. break;
  1087. case IPV6_UNICAST_IF:
  1088. val = (__force int)htonl((__u32) np->ucast_oif);
  1089. break;
  1090. case IPV6_MTU_DISCOVER:
  1091. val = np->pmtudisc;
  1092. break;
  1093. case IPV6_RECVERR:
  1094. val = np->recverr;
  1095. break;
  1096. case IPV6_FLOWINFO_SEND:
  1097. val = np->sndflow;
  1098. break;
  1099. case IPV6_FLOWLABEL_MGR:
  1100. {
  1101. struct in6_flowlabel_req freq;
  1102. int flags;
  1103. if (len < sizeof(freq))
  1104. return -EINVAL;
  1105. if (copy_from_user(&freq, optval, sizeof(freq)))
  1106. return -EFAULT;
  1107. if (freq.flr_action != IPV6_FL_A_GET)
  1108. return -EINVAL;
  1109. len = sizeof(freq);
  1110. flags = freq.flr_flags;
  1111. memset(&freq, 0, sizeof(freq));
  1112. val = ipv6_flowlabel_opt_get(sk, &freq, flags);
  1113. if (val < 0)
  1114. return val;
  1115. if (put_user(len, optlen))
  1116. return -EFAULT;
  1117. if (copy_to_user(optval, &freq, len))
  1118. return -EFAULT;
  1119. return 0;
  1120. }
  1121. case IPV6_ADDR_PREFERENCES:
  1122. val = 0;
  1123. if (np->srcprefs & IPV6_PREFER_SRC_TMP)
  1124. val |= IPV6_PREFER_SRC_TMP;
  1125. else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
  1126. val |= IPV6_PREFER_SRC_PUBLIC;
  1127. else {
  1128. /* XXX: should we return system default? */
  1129. val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
  1130. }
  1131. if (np->srcprefs & IPV6_PREFER_SRC_COA)
  1132. val |= IPV6_PREFER_SRC_COA;
  1133. else
  1134. val |= IPV6_PREFER_SRC_HOME;
  1135. break;
  1136. case IPV6_MINHOPCOUNT:
  1137. val = np->min_hopcount;
  1138. break;
  1139. case IPV6_DONTFRAG:
  1140. val = np->dontfrag;
  1141. break;
  1142. case IPV6_AUTOFLOWLABEL:
  1143. val = ip6_autoflowlabel(sock_net(sk), np);
  1144. break;
  1145. default:
  1146. return -ENOPROTOOPT;
  1147. }
  1148. len = min_t(unsigned int, sizeof(int), len);
  1149. if (put_user(len, optlen))
  1150. return -EFAULT;
  1151. if (copy_to_user(optval, &val, len))
  1152. return -EFAULT;
  1153. return 0;
  1154. }
  1155. int ipv6_getsockopt(struct sock *sk, int level, int optname,
  1156. char __user *optval, int __user *optlen)
  1157. {
  1158. int err;
  1159. if (level == SOL_IP && sk->sk_type != SOCK_RAW)
  1160. return udp_prot.getsockopt(sk, level, optname, optval, optlen);
  1161. if (level != SOL_IPV6)
  1162. return -ENOPROTOOPT;
  1163. err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
  1164. #ifdef CONFIG_NETFILTER
  1165. /* we need to exclude all possible ENOPROTOOPTs except default case */
  1166. if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
  1167. int len;
  1168. if (get_user(len, optlen))
  1169. return -EFAULT;
  1170. err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
  1171. if (err >= 0)
  1172. err = put_user(len, optlen);
  1173. }
  1174. #endif
  1175. return err;
  1176. }
  1177. EXPORT_SYMBOL(ipv6_getsockopt);
  1178. #ifdef CONFIG_COMPAT
  1179. int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
  1180. char __user *optval, int __user *optlen)
  1181. {
  1182. int err;
  1183. if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
  1184. if (udp_prot.compat_getsockopt != NULL)
  1185. return udp_prot.compat_getsockopt(sk, level, optname,
  1186. optval, optlen);
  1187. return udp_prot.getsockopt(sk, level, optname, optval, optlen);
  1188. }
  1189. if (level != SOL_IPV6)
  1190. return -ENOPROTOOPT;
  1191. if (optname == MCAST_MSFILTER)
  1192. return compat_mc_getsockopt(sk, level, optname, optval, optlen,
  1193. ipv6_getsockopt);
  1194. err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
  1195. MSG_CMSG_COMPAT);
  1196. #ifdef CONFIG_NETFILTER
  1197. /* we need to exclude all possible ENOPROTOOPTs except default case */
  1198. if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
  1199. int len;
  1200. if (get_user(len, optlen))
  1201. return -EFAULT;
  1202. err = compat_nf_getsockopt(sk, PF_INET6, optname, optval, &len);
  1203. if (err >= 0)
  1204. err = put_user(len, optlen);
  1205. }
  1206. #endif
  1207. return err;
  1208. }
  1209. EXPORT_SYMBOL(compat_ipv6_getsockopt);
  1210. #endif