ipv6_sockglue.c 30 KB

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