ipv6_sockglue.c 30 KB

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