ipv6_sockglue.c 27 KB

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