ip_sockglue.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * The IP to API glue.
  7. *
  8. * Authors: see ip.c
  9. *
  10. * Fixes:
  11. * Many : Split from ip.c , see ip.c for history.
  12. * Martin Mares : TOS setting fixed.
  13. * Alan Cox : Fixed a couple of oopses in Martin's
  14. * TOS tweaks.
  15. * Mike McLagan : Routing by source
  16. */
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/mm.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/ip.h>
  22. #include <linux/icmp.h>
  23. #include <linux/inetdevice.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/slab.h>
  26. #include <net/sock.h>
  27. #include <net/ip.h>
  28. #include <net/icmp.h>
  29. #include <net/tcp_states.h>
  30. #include <linux/udp.h>
  31. #include <linux/igmp.h>
  32. #include <linux/netfilter.h>
  33. #include <linux/route.h>
  34. #include <linux/mroute.h>
  35. #include <net/route.h>
  36. #include <net/xfrm.h>
  37. #include <net/compat.h>
  38. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  39. #include <net/transp_v6.h>
  40. #endif
  41. #include <linux/errqueue.h>
  42. #include <asm/uaccess.h>
  43. #define IP_CMSG_PKTINFO 1
  44. #define IP_CMSG_TTL 2
  45. #define IP_CMSG_TOS 4
  46. #define IP_CMSG_RECVOPTS 8
  47. #define IP_CMSG_RETOPTS 16
  48. #define IP_CMSG_PASSSEC 32
  49. #define IP_CMSG_ORIGDSTADDR 64
  50. /*
  51. * SOL_IP control messages.
  52. */
  53. static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
  54. {
  55. struct in_pktinfo info;
  56. struct rtable *rt = skb_rtable(skb);
  57. info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
  58. if (rt) {
  59. info.ipi_ifindex = rt->rt_iif;
  60. info.ipi_spec_dst.s_addr = rt->rt_spec_dst;
  61. } else {
  62. info.ipi_ifindex = 0;
  63. info.ipi_spec_dst.s_addr = 0;
  64. }
  65. put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
  66. }
  67. static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
  68. {
  69. int ttl = ip_hdr(skb)->ttl;
  70. put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
  71. }
  72. static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
  73. {
  74. put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
  75. }
  76. static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
  77. {
  78. if (IPCB(skb)->opt.optlen == 0)
  79. return;
  80. put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
  81. ip_hdr(skb) + 1);
  82. }
  83. static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
  84. {
  85. unsigned char optbuf[sizeof(struct ip_options) + 40];
  86. struct ip_options * opt = (struct ip_options *)optbuf;
  87. if (IPCB(skb)->opt.optlen == 0)
  88. return;
  89. if (ip_options_echo(opt, skb)) {
  90. msg->msg_flags |= MSG_CTRUNC;
  91. return;
  92. }
  93. ip_options_undo(opt);
  94. put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
  95. }
  96. static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
  97. {
  98. char *secdata;
  99. u32 seclen, secid;
  100. int err;
  101. err = security_socket_getpeersec_dgram(NULL, skb, &secid);
  102. if (err)
  103. return;
  104. err = security_secid_to_secctx(secid, &secdata, &seclen);
  105. if (err)
  106. return;
  107. put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
  108. security_release_secctx(secdata, seclen);
  109. }
  110. static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
  111. {
  112. struct sockaddr_in sin;
  113. const struct iphdr *iph = ip_hdr(skb);
  114. __be16 *ports = (__be16 *)skb_transport_header(skb);
  115. if (skb_transport_offset(skb) + 4 > skb->len)
  116. return;
  117. /* All current transport protocols have the port numbers in the
  118. * first four bytes of the transport header and this function is
  119. * written with this assumption in mind.
  120. */
  121. sin.sin_family = AF_INET;
  122. sin.sin_addr.s_addr = iph->daddr;
  123. sin.sin_port = ports[1];
  124. memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
  125. put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
  126. }
  127. void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
  128. {
  129. struct inet_sock *inet = inet_sk(skb->sk);
  130. unsigned flags = inet->cmsg_flags;
  131. /* Ordered by supposed usage frequency */
  132. if (flags & 1)
  133. ip_cmsg_recv_pktinfo(msg, skb);
  134. if ((flags >>= 1) == 0)
  135. return;
  136. if (flags & 1)
  137. ip_cmsg_recv_ttl(msg, skb);
  138. if ((flags >>= 1) == 0)
  139. return;
  140. if (flags & 1)
  141. ip_cmsg_recv_tos(msg, skb);
  142. if ((flags >>= 1) == 0)
  143. return;
  144. if (flags & 1)
  145. ip_cmsg_recv_opts(msg, skb);
  146. if ((flags >>= 1) == 0)
  147. return;
  148. if (flags & 1)
  149. ip_cmsg_recv_retopts(msg, skb);
  150. if ((flags >>= 1) == 0)
  151. return;
  152. if (flags & 1)
  153. ip_cmsg_recv_security(msg, skb);
  154. if ((flags >>= 1) == 0)
  155. return;
  156. if (flags & 1)
  157. ip_cmsg_recv_dstaddr(msg, skb);
  158. }
  159. EXPORT_SYMBOL(ip_cmsg_recv);
  160. int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
  161. {
  162. int err;
  163. struct cmsghdr *cmsg;
  164. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  165. if (!CMSG_OK(msg, cmsg))
  166. return -EINVAL;
  167. if (cmsg->cmsg_level != SOL_IP)
  168. continue;
  169. switch (cmsg->cmsg_type) {
  170. case IP_RETOPTS:
  171. err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
  172. err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
  173. err < 40 ? err : 40);
  174. if (err)
  175. return err;
  176. break;
  177. case IP_PKTINFO:
  178. {
  179. struct in_pktinfo *info;
  180. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
  181. return -EINVAL;
  182. info = (struct in_pktinfo *)CMSG_DATA(cmsg);
  183. ipc->oif = info->ipi_ifindex;
  184. ipc->addr = info->ipi_spec_dst.s_addr;
  185. break;
  186. }
  187. default:
  188. return -EINVAL;
  189. }
  190. }
  191. return 0;
  192. }
  193. /* Special input handler for packets caught by router alert option.
  194. They are selected only by protocol field, and then processed likely
  195. local ones; but only if someone wants them! Otherwise, router
  196. not running rsvpd will kill RSVP.
  197. It is user level problem, what it will make with them.
  198. I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
  199. but receiver should be enough clever f.e. to forward mtrace requests,
  200. sent to multicast group to reach destination designated router.
  201. */
  202. struct ip_ra_chain __rcu *ip_ra_chain;
  203. static DEFINE_SPINLOCK(ip_ra_lock);
  204. static void ip_ra_destroy_rcu(struct rcu_head *head)
  205. {
  206. struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
  207. sock_put(ra->saved_sk);
  208. kfree(ra);
  209. }
  210. int ip_ra_control(struct sock *sk, unsigned char on,
  211. void (*destructor)(struct sock *))
  212. {
  213. struct ip_ra_chain *ra, *new_ra;
  214. struct ip_ra_chain __rcu **rap;
  215. if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
  216. return -EINVAL;
  217. new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
  218. spin_lock_bh(&ip_ra_lock);
  219. for (rap = &ip_ra_chain;
  220. (ra = rcu_dereference_protected(*rap,
  221. lockdep_is_held(&ip_ra_lock))) != NULL;
  222. rap = &ra->next) {
  223. if (ra->sk == sk) {
  224. if (on) {
  225. spin_unlock_bh(&ip_ra_lock);
  226. kfree(new_ra);
  227. return -EADDRINUSE;
  228. }
  229. /* dont let ip_call_ra_chain() use sk again */
  230. ra->sk = NULL;
  231. rcu_assign_pointer(*rap, ra->next);
  232. spin_unlock_bh(&ip_ra_lock);
  233. if (ra->destructor)
  234. ra->destructor(sk);
  235. /*
  236. * Delay sock_put(sk) and kfree(ra) after one rcu grace
  237. * period. This guarantee ip_call_ra_chain() dont need
  238. * to mess with socket refcounts.
  239. */
  240. ra->saved_sk = sk;
  241. call_rcu(&ra->rcu, ip_ra_destroy_rcu);
  242. return 0;
  243. }
  244. }
  245. if (new_ra == NULL) {
  246. spin_unlock_bh(&ip_ra_lock);
  247. return -ENOBUFS;
  248. }
  249. new_ra->sk = sk;
  250. new_ra->destructor = destructor;
  251. new_ra->next = ra;
  252. rcu_assign_pointer(*rap, new_ra);
  253. sock_hold(sk);
  254. spin_unlock_bh(&ip_ra_lock);
  255. return 0;
  256. }
  257. void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
  258. __be16 port, u32 info, u8 *payload)
  259. {
  260. struct sock_exterr_skb *serr;
  261. skb = skb_clone(skb, GFP_ATOMIC);
  262. if (!skb)
  263. return;
  264. serr = SKB_EXT_ERR(skb);
  265. serr->ee.ee_errno = err;
  266. serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
  267. serr->ee.ee_type = icmp_hdr(skb)->type;
  268. serr->ee.ee_code = icmp_hdr(skb)->code;
  269. serr->ee.ee_pad = 0;
  270. serr->ee.ee_info = info;
  271. serr->ee.ee_data = 0;
  272. serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
  273. skb_network_header(skb);
  274. serr->port = port;
  275. if (skb_pull(skb, payload - skb->data) != NULL) {
  276. skb_reset_transport_header(skb);
  277. if (sock_queue_err_skb(sk, skb) == 0)
  278. return;
  279. }
  280. kfree_skb(skb);
  281. }
  282. void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
  283. {
  284. struct inet_sock *inet = inet_sk(sk);
  285. struct sock_exterr_skb *serr;
  286. struct iphdr *iph;
  287. struct sk_buff *skb;
  288. if (!inet->recverr)
  289. return;
  290. skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
  291. if (!skb)
  292. return;
  293. skb_put(skb, sizeof(struct iphdr));
  294. skb_reset_network_header(skb);
  295. iph = ip_hdr(skb);
  296. iph->daddr = daddr;
  297. serr = SKB_EXT_ERR(skb);
  298. serr->ee.ee_errno = err;
  299. serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
  300. serr->ee.ee_type = 0;
  301. serr->ee.ee_code = 0;
  302. serr->ee.ee_pad = 0;
  303. serr->ee.ee_info = info;
  304. serr->ee.ee_data = 0;
  305. serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
  306. serr->port = port;
  307. __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
  308. skb_reset_transport_header(skb);
  309. if (sock_queue_err_skb(sk, skb))
  310. kfree_skb(skb);
  311. }
  312. /*
  313. * Handle MSG_ERRQUEUE
  314. */
  315. int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
  316. {
  317. struct sock_exterr_skb *serr;
  318. struct sk_buff *skb, *skb2;
  319. struct sockaddr_in *sin;
  320. struct {
  321. struct sock_extended_err ee;
  322. struct sockaddr_in offender;
  323. } errhdr;
  324. int err;
  325. int copied;
  326. err = -EAGAIN;
  327. skb = skb_dequeue(&sk->sk_error_queue);
  328. if (skb == NULL)
  329. goto out;
  330. copied = skb->len;
  331. if (copied > len) {
  332. msg->msg_flags |= MSG_TRUNC;
  333. copied = len;
  334. }
  335. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  336. if (err)
  337. goto out_free_skb;
  338. sock_recv_timestamp(msg, sk, skb);
  339. serr = SKB_EXT_ERR(skb);
  340. sin = (struct sockaddr_in *)msg->msg_name;
  341. if (sin) {
  342. sin->sin_family = AF_INET;
  343. sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
  344. serr->addr_offset);
  345. sin->sin_port = serr->port;
  346. memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
  347. }
  348. memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
  349. sin = &errhdr.offender;
  350. sin->sin_family = AF_UNSPEC;
  351. if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
  352. struct inet_sock *inet = inet_sk(sk);
  353. sin->sin_family = AF_INET;
  354. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  355. sin->sin_port = 0;
  356. memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
  357. if (inet->cmsg_flags)
  358. ip_cmsg_recv(msg, skb);
  359. }
  360. put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
  361. /* Now we could try to dump offended packet options */
  362. msg->msg_flags |= MSG_ERRQUEUE;
  363. err = copied;
  364. /* Reset and regenerate socket error */
  365. spin_lock_bh(&sk->sk_error_queue.lock);
  366. sk->sk_err = 0;
  367. skb2 = skb_peek(&sk->sk_error_queue);
  368. if (skb2 != NULL) {
  369. sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
  370. spin_unlock_bh(&sk->sk_error_queue.lock);
  371. sk->sk_error_report(sk);
  372. } else
  373. spin_unlock_bh(&sk->sk_error_queue.lock);
  374. out_free_skb:
  375. kfree_skb(skb);
  376. out:
  377. return err;
  378. }
  379. static void opt_kfree_rcu(struct rcu_head *head)
  380. {
  381. kfree(container_of(head, struct ip_options_rcu, rcu));
  382. }
  383. /*
  384. * Socket option code for IP. This is the end of the line after any
  385. * TCP,UDP etc options on an IP socket.
  386. */
  387. static int do_ip_setsockopt(struct sock *sk, int level,
  388. int optname, char __user *optval, unsigned int optlen)
  389. {
  390. struct inet_sock *inet = inet_sk(sk);
  391. int val = 0, err;
  392. if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
  393. (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
  394. (1<<IP_RETOPTS) | (1<<IP_TOS) |
  395. (1<<IP_TTL) | (1<<IP_HDRINCL) |
  396. (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
  397. (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
  398. (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT) |
  399. (1<<IP_MINTTL) | (1<<IP_NODEFRAG))) ||
  400. optname == IP_MULTICAST_TTL ||
  401. optname == IP_MULTICAST_ALL ||
  402. optname == IP_MULTICAST_LOOP ||
  403. optname == IP_RECVORIGDSTADDR) {
  404. if (optlen >= sizeof(int)) {
  405. if (get_user(val, (int __user *) optval))
  406. return -EFAULT;
  407. } else if (optlen >= sizeof(char)) {
  408. unsigned char ucval;
  409. if (get_user(ucval, (unsigned char __user *) optval))
  410. return -EFAULT;
  411. val = (int) ucval;
  412. }
  413. }
  414. /* If optlen==0, it is equivalent to val == 0 */
  415. if (ip_mroute_opt(optname))
  416. return ip_mroute_setsockopt(sk, optname, optval, optlen);
  417. err = 0;
  418. lock_sock(sk);
  419. switch (optname) {
  420. case IP_OPTIONS:
  421. {
  422. struct ip_options_rcu *old, *opt = NULL;
  423. if (optlen > 40)
  424. goto e_inval;
  425. err = ip_options_get_from_user(sock_net(sk), &opt,
  426. optval, optlen);
  427. if (err)
  428. break;
  429. old = rcu_dereference_protected(inet->inet_opt,
  430. sock_owned_by_user(sk));
  431. if (inet->is_icsk) {
  432. struct inet_connection_sock *icsk = inet_csk(sk);
  433. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  434. if (sk->sk_family == PF_INET ||
  435. (!((1 << sk->sk_state) &
  436. (TCPF_LISTEN | TCPF_CLOSE)) &&
  437. inet->inet_daddr != LOOPBACK4_IPV6)) {
  438. #endif
  439. if (old)
  440. icsk->icsk_ext_hdr_len -= old->opt.optlen;
  441. if (opt)
  442. icsk->icsk_ext_hdr_len += opt->opt.optlen;
  443. icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
  444. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  445. }
  446. #endif
  447. }
  448. rcu_assign_pointer(inet->inet_opt, opt);
  449. if (old)
  450. call_rcu(&old->rcu, opt_kfree_rcu);
  451. break;
  452. }
  453. case IP_PKTINFO:
  454. if (val)
  455. inet->cmsg_flags |= IP_CMSG_PKTINFO;
  456. else
  457. inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
  458. break;
  459. case IP_RECVTTL:
  460. if (val)
  461. inet->cmsg_flags |= IP_CMSG_TTL;
  462. else
  463. inet->cmsg_flags &= ~IP_CMSG_TTL;
  464. break;
  465. case IP_RECVTOS:
  466. if (val)
  467. inet->cmsg_flags |= IP_CMSG_TOS;
  468. else
  469. inet->cmsg_flags &= ~IP_CMSG_TOS;
  470. break;
  471. case IP_RECVOPTS:
  472. if (val)
  473. inet->cmsg_flags |= IP_CMSG_RECVOPTS;
  474. else
  475. inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
  476. break;
  477. case IP_RETOPTS:
  478. if (val)
  479. inet->cmsg_flags |= IP_CMSG_RETOPTS;
  480. else
  481. inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
  482. break;
  483. case IP_PASSSEC:
  484. if (val)
  485. inet->cmsg_flags |= IP_CMSG_PASSSEC;
  486. else
  487. inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
  488. break;
  489. case IP_RECVORIGDSTADDR:
  490. if (val)
  491. inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
  492. else
  493. inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
  494. break;
  495. case IP_TOS: /* This sets both TOS and Precedence */
  496. if (sk->sk_type == SOCK_STREAM) {
  497. val &= ~3;
  498. val |= inet->tos & 3;
  499. }
  500. if (inet->tos != val) {
  501. inet->tos = val;
  502. sk->sk_priority = rt_tos2priority(val);
  503. sk_dst_reset(sk);
  504. }
  505. break;
  506. case IP_TTL:
  507. if (optlen < 1)
  508. goto e_inval;
  509. if (val != -1 && (val < 0 || val > 255))
  510. goto e_inval;
  511. inet->uc_ttl = val;
  512. break;
  513. case IP_HDRINCL:
  514. if (sk->sk_type != SOCK_RAW) {
  515. err = -ENOPROTOOPT;
  516. break;
  517. }
  518. inet->hdrincl = val ? 1 : 0;
  519. break;
  520. case IP_NODEFRAG:
  521. if (sk->sk_type != SOCK_RAW) {
  522. err = -ENOPROTOOPT;
  523. break;
  524. }
  525. inet->nodefrag = val ? 1 : 0;
  526. break;
  527. case IP_MTU_DISCOVER:
  528. if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE)
  529. goto e_inval;
  530. inet->pmtudisc = val;
  531. break;
  532. case IP_RECVERR:
  533. inet->recverr = !!val;
  534. if (!val)
  535. skb_queue_purge(&sk->sk_error_queue);
  536. break;
  537. case IP_MULTICAST_TTL:
  538. if (sk->sk_type == SOCK_STREAM)
  539. goto e_inval;
  540. if (optlen < 1)
  541. goto e_inval;
  542. if (val == -1)
  543. val = 1;
  544. if (val < 0 || val > 255)
  545. goto e_inval;
  546. inet->mc_ttl = val;
  547. break;
  548. case IP_MULTICAST_LOOP:
  549. if (optlen < 1)
  550. goto e_inval;
  551. inet->mc_loop = !!val;
  552. break;
  553. case IP_MULTICAST_IF:
  554. {
  555. struct ip_mreqn mreq;
  556. struct net_device *dev = NULL;
  557. if (sk->sk_type == SOCK_STREAM)
  558. goto e_inval;
  559. /*
  560. * Check the arguments are allowable
  561. */
  562. if (optlen < sizeof(struct in_addr))
  563. goto e_inval;
  564. err = -EFAULT;
  565. if (optlen >= sizeof(struct ip_mreqn)) {
  566. if (copy_from_user(&mreq, optval, sizeof(mreq)))
  567. break;
  568. } else {
  569. memset(&mreq, 0, sizeof(mreq));
  570. if (optlen >= sizeof(struct in_addr) &&
  571. copy_from_user(&mreq.imr_address, optval,
  572. sizeof(struct in_addr)))
  573. break;
  574. }
  575. if (!mreq.imr_ifindex) {
  576. if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
  577. inet->mc_index = 0;
  578. inet->mc_addr = 0;
  579. err = 0;
  580. break;
  581. }
  582. dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
  583. if (dev)
  584. mreq.imr_ifindex = dev->ifindex;
  585. } else
  586. dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
  587. err = -EADDRNOTAVAIL;
  588. if (!dev)
  589. break;
  590. dev_put(dev);
  591. err = -EINVAL;
  592. if (sk->sk_bound_dev_if &&
  593. mreq.imr_ifindex != sk->sk_bound_dev_if)
  594. break;
  595. inet->mc_index = mreq.imr_ifindex;
  596. inet->mc_addr = mreq.imr_address.s_addr;
  597. err = 0;
  598. break;
  599. }
  600. case IP_ADD_MEMBERSHIP:
  601. case IP_DROP_MEMBERSHIP:
  602. {
  603. struct ip_mreqn mreq;
  604. err = -EPROTO;
  605. if (inet_sk(sk)->is_icsk)
  606. break;
  607. if (optlen < sizeof(struct ip_mreq))
  608. goto e_inval;
  609. err = -EFAULT;
  610. if (optlen >= sizeof(struct ip_mreqn)) {
  611. if (copy_from_user(&mreq, optval, sizeof(mreq)))
  612. break;
  613. } else {
  614. memset(&mreq, 0, sizeof(mreq));
  615. if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
  616. break;
  617. }
  618. if (optname == IP_ADD_MEMBERSHIP)
  619. err = ip_mc_join_group(sk, &mreq);
  620. else
  621. err = ip_mc_leave_group(sk, &mreq);
  622. break;
  623. }
  624. case IP_MSFILTER:
  625. {
  626. struct ip_msfilter *msf;
  627. if (optlen < IP_MSFILTER_SIZE(0))
  628. goto e_inval;
  629. if (optlen > sysctl_optmem_max) {
  630. err = -ENOBUFS;
  631. break;
  632. }
  633. msf = kmalloc(optlen, GFP_KERNEL);
  634. if (!msf) {
  635. err = -ENOBUFS;
  636. break;
  637. }
  638. err = -EFAULT;
  639. if (copy_from_user(msf, optval, optlen)) {
  640. kfree(msf);
  641. break;
  642. }
  643. /* numsrc >= (1G-4) overflow in 32 bits */
  644. if (msf->imsf_numsrc >= 0x3ffffffcU ||
  645. msf->imsf_numsrc > sysctl_igmp_max_msf) {
  646. kfree(msf);
  647. err = -ENOBUFS;
  648. break;
  649. }
  650. if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
  651. kfree(msf);
  652. err = -EINVAL;
  653. break;
  654. }
  655. err = ip_mc_msfilter(sk, msf, 0);
  656. kfree(msf);
  657. break;
  658. }
  659. case IP_BLOCK_SOURCE:
  660. case IP_UNBLOCK_SOURCE:
  661. case IP_ADD_SOURCE_MEMBERSHIP:
  662. case IP_DROP_SOURCE_MEMBERSHIP:
  663. {
  664. struct ip_mreq_source mreqs;
  665. int omode, add;
  666. if (optlen != sizeof(struct ip_mreq_source))
  667. goto e_inval;
  668. if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
  669. err = -EFAULT;
  670. break;
  671. }
  672. if (optname == IP_BLOCK_SOURCE) {
  673. omode = MCAST_EXCLUDE;
  674. add = 1;
  675. } else if (optname == IP_UNBLOCK_SOURCE) {
  676. omode = MCAST_EXCLUDE;
  677. add = 0;
  678. } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
  679. struct ip_mreqn mreq;
  680. mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
  681. mreq.imr_address.s_addr = mreqs.imr_interface;
  682. mreq.imr_ifindex = 0;
  683. err = ip_mc_join_group(sk, &mreq);
  684. if (err && err != -EADDRINUSE)
  685. break;
  686. omode = MCAST_INCLUDE;
  687. add = 1;
  688. } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
  689. omode = MCAST_INCLUDE;
  690. add = 0;
  691. }
  692. err = ip_mc_source(add, omode, sk, &mreqs, 0);
  693. break;
  694. }
  695. case MCAST_JOIN_GROUP:
  696. case MCAST_LEAVE_GROUP:
  697. {
  698. struct group_req greq;
  699. struct sockaddr_in *psin;
  700. struct ip_mreqn mreq;
  701. if (optlen < sizeof(struct group_req))
  702. goto e_inval;
  703. err = -EFAULT;
  704. if (copy_from_user(&greq, optval, sizeof(greq)))
  705. break;
  706. psin = (struct sockaddr_in *)&greq.gr_group;
  707. if (psin->sin_family != AF_INET)
  708. goto e_inval;
  709. memset(&mreq, 0, sizeof(mreq));
  710. mreq.imr_multiaddr = psin->sin_addr;
  711. mreq.imr_ifindex = greq.gr_interface;
  712. if (optname == MCAST_JOIN_GROUP)
  713. err = ip_mc_join_group(sk, &mreq);
  714. else
  715. err = ip_mc_leave_group(sk, &mreq);
  716. break;
  717. }
  718. case MCAST_JOIN_SOURCE_GROUP:
  719. case MCAST_LEAVE_SOURCE_GROUP:
  720. case MCAST_BLOCK_SOURCE:
  721. case MCAST_UNBLOCK_SOURCE:
  722. {
  723. struct group_source_req greqs;
  724. struct ip_mreq_source mreqs;
  725. struct sockaddr_in *psin;
  726. int omode, add;
  727. if (optlen != sizeof(struct group_source_req))
  728. goto e_inval;
  729. if (copy_from_user(&greqs, optval, sizeof(greqs))) {
  730. err = -EFAULT;
  731. break;
  732. }
  733. if (greqs.gsr_group.ss_family != AF_INET ||
  734. greqs.gsr_source.ss_family != AF_INET) {
  735. err = -EADDRNOTAVAIL;
  736. break;
  737. }
  738. psin = (struct sockaddr_in *)&greqs.gsr_group;
  739. mreqs.imr_multiaddr = psin->sin_addr.s_addr;
  740. psin = (struct sockaddr_in *)&greqs.gsr_source;
  741. mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
  742. mreqs.imr_interface = 0; /* use index for mc_source */
  743. if (optname == MCAST_BLOCK_SOURCE) {
  744. omode = MCAST_EXCLUDE;
  745. add = 1;
  746. } else if (optname == MCAST_UNBLOCK_SOURCE) {
  747. omode = MCAST_EXCLUDE;
  748. add = 0;
  749. } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
  750. struct ip_mreqn mreq;
  751. psin = (struct sockaddr_in *)&greqs.gsr_group;
  752. mreq.imr_multiaddr = psin->sin_addr;
  753. mreq.imr_address.s_addr = 0;
  754. mreq.imr_ifindex = greqs.gsr_interface;
  755. err = ip_mc_join_group(sk, &mreq);
  756. if (err && err != -EADDRINUSE)
  757. break;
  758. greqs.gsr_interface = mreq.imr_ifindex;
  759. omode = MCAST_INCLUDE;
  760. add = 1;
  761. } else /* MCAST_LEAVE_SOURCE_GROUP */ {
  762. omode = MCAST_INCLUDE;
  763. add = 0;
  764. }
  765. err = ip_mc_source(add, omode, sk, &mreqs,
  766. greqs.gsr_interface);
  767. break;
  768. }
  769. case MCAST_MSFILTER:
  770. {
  771. struct sockaddr_in *psin;
  772. struct ip_msfilter *msf = NULL;
  773. struct group_filter *gsf = NULL;
  774. int msize, i, ifindex;
  775. if (optlen < GROUP_FILTER_SIZE(0))
  776. goto e_inval;
  777. if (optlen > sysctl_optmem_max) {
  778. err = -ENOBUFS;
  779. break;
  780. }
  781. gsf = kmalloc(optlen, GFP_KERNEL);
  782. if (!gsf) {
  783. err = -ENOBUFS;
  784. break;
  785. }
  786. err = -EFAULT;
  787. if (copy_from_user(gsf, optval, optlen))
  788. goto mc_msf_out;
  789. /* numsrc >= (4G-140)/128 overflow in 32 bits */
  790. if (gsf->gf_numsrc >= 0x1ffffff ||
  791. gsf->gf_numsrc > sysctl_igmp_max_msf) {
  792. err = -ENOBUFS;
  793. goto mc_msf_out;
  794. }
  795. if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
  796. err = -EINVAL;
  797. goto mc_msf_out;
  798. }
  799. msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
  800. msf = kmalloc(msize, GFP_KERNEL);
  801. if (!msf) {
  802. err = -ENOBUFS;
  803. goto mc_msf_out;
  804. }
  805. ifindex = gsf->gf_interface;
  806. psin = (struct sockaddr_in *)&gsf->gf_group;
  807. if (psin->sin_family != AF_INET) {
  808. err = -EADDRNOTAVAIL;
  809. goto mc_msf_out;
  810. }
  811. msf->imsf_multiaddr = psin->sin_addr.s_addr;
  812. msf->imsf_interface = 0;
  813. msf->imsf_fmode = gsf->gf_fmode;
  814. msf->imsf_numsrc = gsf->gf_numsrc;
  815. err = -EADDRNOTAVAIL;
  816. for (i = 0; i < gsf->gf_numsrc; ++i) {
  817. psin = (struct sockaddr_in *)&gsf->gf_slist[i];
  818. if (psin->sin_family != AF_INET)
  819. goto mc_msf_out;
  820. msf->imsf_slist[i] = psin->sin_addr.s_addr;
  821. }
  822. kfree(gsf);
  823. gsf = NULL;
  824. err = ip_mc_msfilter(sk, msf, ifindex);
  825. mc_msf_out:
  826. kfree(msf);
  827. kfree(gsf);
  828. break;
  829. }
  830. case IP_MULTICAST_ALL:
  831. if (optlen < 1)
  832. goto e_inval;
  833. if (val != 0 && val != 1)
  834. goto e_inval;
  835. inet->mc_all = val;
  836. break;
  837. case IP_ROUTER_ALERT:
  838. err = ip_ra_control(sk, val ? 1 : 0, NULL);
  839. break;
  840. case IP_FREEBIND:
  841. if (optlen < 1)
  842. goto e_inval;
  843. inet->freebind = !!val;
  844. break;
  845. case IP_IPSEC_POLICY:
  846. case IP_XFRM_POLICY:
  847. err = -EPERM;
  848. if (!capable(CAP_NET_ADMIN))
  849. break;
  850. err = xfrm_user_policy(sk, optname, optval, optlen);
  851. break;
  852. case IP_TRANSPARENT:
  853. if (!capable(CAP_NET_ADMIN)) {
  854. err = -EPERM;
  855. break;
  856. }
  857. if (optlen < 1)
  858. goto e_inval;
  859. inet->transparent = !!val;
  860. break;
  861. case IP_MINTTL:
  862. if (optlen < 1)
  863. goto e_inval;
  864. if (val < 0 || val > 255)
  865. goto e_inval;
  866. inet->min_ttl = val;
  867. break;
  868. default:
  869. err = -ENOPROTOOPT;
  870. break;
  871. }
  872. release_sock(sk);
  873. return err;
  874. e_inval:
  875. release_sock(sk);
  876. return -EINVAL;
  877. }
  878. /**
  879. * ip_queue_rcv_skb - Queue an skb into sock receive queue
  880. * @sk: socket
  881. * @skb: buffer
  882. *
  883. * Queues an skb into socket receive queue. If IP_CMSG_PKTINFO option
  884. * is not set, we drop skb dst entry now, while dst cache line is hot.
  885. */
  886. int ip_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
  887. {
  888. if (!(inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO))
  889. skb_dst_drop(skb);
  890. return sock_queue_rcv_skb(sk, skb);
  891. }
  892. EXPORT_SYMBOL(ip_queue_rcv_skb);
  893. int ip_setsockopt(struct sock *sk, int level,
  894. int optname, char __user *optval, unsigned int optlen)
  895. {
  896. int err;
  897. if (level != SOL_IP)
  898. return -ENOPROTOOPT;
  899. err = do_ip_setsockopt(sk, level, optname, optval, optlen);
  900. #ifdef CONFIG_NETFILTER
  901. /* we need to exclude all possible ENOPROTOOPTs except default case */
  902. if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
  903. optname != IP_IPSEC_POLICY &&
  904. optname != IP_XFRM_POLICY &&
  905. !ip_mroute_opt(optname)) {
  906. lock_sock(sk);
  907. err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
  908. release_sock(sk);
  909. }
  910. #endif
  911. return err;
  912. }
  913. EXPORT_SYMBOL(ip_setsockopt);
  914. #ifdef CONFIG_COMPAT
  915. int compat_ip_setsockopt(struct sock *sk, int level, int optname,
  916. char __user *optval, unsigned int optlen)
  917. {
  918. int err;
  919. if (level != SOL_IP)
  920. return -ENOPROTOOPT;
  921. if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
  922. return compat_mc_setsockopt(sk, level, optname, optval, optlen,
  923. ip_setsockopt);
  924. err = do_ip_setsockopt(sk, level, optname, optval, optlen);
  925. #ifdef CONFIG_NETFILTER
  926. /* we need to exclude all possible ENOPROTOOPTs except default case */
  927. if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
  928. optname != IP_IPSEC_POLICY &&
  929. optname != IP_XFRM_POLICY &&
  930. !ip_mroute_opt(optname)) {
  931. lock_sock(sk);
  932. err = compat_nf_setsockopt(sk, PF_INET, optname,
  933. optval, optlen);
  934. release_sock(sk);
  935. }
  936. #endif
  937. return err;
  938. }
  939. EXPORT_SYMBOL(compat_ip_setsockopt);
  940. #endif
  941. /*
  942. * Get the options. Note for future reference. The GET of IP options gets
  943. * the _received_ ones. The set sets the _sent_ ones.
  944. */
  945. static int do_ip_getsockopt(struct sock *sk, int level, int optname,
  946. char __user *optval, int __user *optlen)
  947. {
  948. struct inet_sock *inet = inet_sk(sk);
  949. int val;
  950. int len;
  951. if (level != SOL_IP)
  952. return -EOPNOTSUPP;
  953. if (ip_mroute_opt(optname))
  954. return ip_mroute_getsockopt(sk, optname, optval, optlen);
  955. if (get_user(len, optlen))
  956. return -EFAULT;
  957. if (len < 0)
  958. return -EINVAL;
  959. lock_sock(sk);
  960. switch (optname) {
  961. case IP_OPTIONS:
  962. {
  963. unsigned char optbuf[sizeof(struct ip_options)+40];
  964. struct ip_options *opt = (struct ip_options *)optbuf;
  965. struct ip_options_rcu *inet_opt;
  966. inet_opt = rcu_dereference_protected(inet->inet_opt,
  967. sock_owned_by_user(sk));
  968. opt->optlen = 0;
  969. if (inet_opt)
  970. memcpy(optbuf, &inet_opt->opt,
  971. sizeof(struct ip_options) +
  972. inet_opt->opt.optlen);
  973. release_sock(sk);
  974. if (opt->optlen == 0)
  975. return put_user(0, optlen);
  976. ip_options_undo(opt);
  977. len = min_t(unsigned int, len, opt->optlen);
  978. if (put_user(len, optlen))
  979. return -EFAULT;
  980. if (copy_to_user(optval, opt->__data, len))
  981. return -EFAULT;
  982. return 0;
  983. }
  984. case IP_PKTINFO:
  985. val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
  986. break;
  987. case IP_RECVTTL:
  988. val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
  989. break;
  990. case IP_RECVTOS:
  991. val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
  992. break;
  993. case IP_RECVOPTS:
  994. val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
  995. break;
  996. case IP_RETOPTS:
  997. val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
  998. break;
  999. case IP_PASSSEC:
  1000. val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
  1001. break;
  1002. case IP_RECVORIGDSTADDR:
  1003. val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
  1004. break;
  1005. case IP_TOS:
  1006. val = inet->tos;
  1007. break;
  1008. case IP_TTL:
  1009. val = (inet->uc_ttl == -1 ?
  1010. sysctl_ip_default_ttl :
  1011. inet->uc_ttl);
  1012. break;
  1013. case IP_HDRINCL:
  1014. val = inet->hdrincl;
  1015. break;
  1016. case IP_NODEFRAG:
  1017. val = inet->nodefrag;
  1018. break;
  1019. case IP_MTU_DISCOVER:
  1020. val = inet->pmtudisc;
  1021. break;
  1022. case IP_MTU:
  1023. {
  1024. struct dst_entry *dst;
  1025. val = 0;
  1026. dst = sk_dst_get(sk);
  1027. if (dst) {
  1028. val = dst_mtu(dst);
  1029. dst_release(dst);
  1030. }
  1031. if (!val) {
  1032. release_sock(sk);
  1033. return -ENOTCONN;
  1034. }
  1035. break;
  1036. }
  1037. case IP_RECVERR:
  1038. val = inet->recverr;
  1039. break;
  1040. case IP_MULTICAST_TTL:
  1041. val = inet->mc_ttl;
  1042. break;
  1043. case IP_MULTICAST_LOOP:
  1044. val = inet->mc_loop;
  1045. break;
  1046. case IP_MULTICAST_IF:
  1047. {
  1048. struct in_addr addr;
  1049. len = min_t(unsigned int, len, sizeof(struct in_addr));
  1050. addr.s_addr = inet->mc_addr;
  1051. release_sock(sk);
  1052. if (put_user(len, optlen))
  1053. return -EFAULT;
  1054. if (copy_to_user(optval, &addr, len))
  1055. return -EFAULT;
  1056. return 0;
  1057. }
  1058. case IP_MSFILTER:
  1059. {
  1060. struct ip_msfilter msf;
  1061. int err;
  1062. if (len < IP_MSFILTER_SIZE(0)) {
  1063. release_sock(sk);
  1064. return -EINVAL;
  1065. }
  1066. if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
  1067. release_sock(sk);
  1068. return -EFAULT;
  1069. }
  1070. err = ip_mc_msfget(sk, &msf,
  1071. (struct ip_msfilter __user *)optval, optlen);
  1072. release_sock(sk);
  1073. return err;
  1074. }
  1075. case MCAST_MSFILTER:
  1076. {
  1077. struct group_filter gsf;
  1078. int err;
  1079. if (len < GROUP_FILTER_SIZE(0)) {
  1080. release_sock(sk);
  1081. return -EINVAL;
  1082. }
  1083. if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
  1084. release_sock(sk);
  1085. return -EFAULT;
  1086. }
  1087. err = ip_mc_gsfget(sk, &gsf,
  1088. (struct group_filter __user *)optval,
  1089. optlen);
  1090. release_sock(sk);
  1091. return err;
  1092. }
  1093. case IP_MULTICAST_ALL:
  1094. val = inet->mc_all;
  1095. break;
  1096. case IP_PKTOPTIONS:
  1097. {
  1098. struct msghdr msg;
  1099. release_sock(sk);
  1100. if (sk->sk_type != SOCK_STREAM)
  1101. return -ENOPROTOOPT;
  1102. msg.msg_control = optval;
  1103. msg.msg_controllen = len;
  1104. msg.msg_flags = 0;
  1105. if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
  1106. struct in_pktinfo info;
  1107. info.ipi_addr.s_addr = inet->inet_rcv_saddr;
  1108. info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
  1109. info.ipi_ifindex = inet->mc_index;
  1110. put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
  1111. }
  1112. if (inet->cmsg_flags & IP_CMSG_TTL) {
  1113. int hlim = inet->mc_ttl;
  1114. put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
  1115. }
  1116. len -= msg.msg_controllen;
  1117. return put_user(len, optlen);
  1118. }
  1119. case IP_FREEBIND:
  1120. val = inet->freebind;
  1121. break;
  1122. case IP_TRANSPARENT:
  1123. val = inet->transparent;
  1124. break;
  1125. case IP_MINTTL:
  1126. val = inet->min_ttl;
  1127. break;
  1128. default:
  1129. release_sock(sk);
  1130. return -ENOPROTOOPT;
  1131. }
  1132. release_sock(sk);
  1133. if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
  1134. unsigned char ucval = (unsigned char)val;
  1135. len = 1;
  1136. if (put_user(len, optlen))
  1137. return -EFAULT;
  1138. if (copy_to_user(optval, &ucval, 1))
  1139. return -EFAULT;
  1140. } else {
  1141. len = min_t(unsigned int, sizeof(int), len);
  1142. if (put_user(len, optlen))
  1143. return -EFAULT;
  1144. if (copy_to_user(optval, &val, len))
  1145. return -EFAULT;
  1146. }
  1147. return 0;
  1148. }
  1149. int ip_getsockopt(struct sock *sk, int level,
  1150. int optname, char __user *optval, int __user *optlen)
  1151. {
  1152. int err;
  1153. err = do_ip_getsockopt(sk, level, optname, optval, optlen);
  1154. #ifdef CONFIG_NETFILTER
  1155. /* we need to exclude all possible ENOPROTOOPTs except default case */
  1156. if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
  1157. !ip_mroute_opt(optname)) {
  1158. int len;
  1159. if (get_user(len, optlen))
  1160. return -EFAULT;
  1161. lock_sock(sk);
  1162. err = nf_getsockopt(sk, PF_INET, optname, optval,
  1163. &len);
  1164. release_sock(sk);
  1165. if (err >= 0)
  1166. err = put_user(len, optlen);
  1167. return err;
  1168. }
  1169. #endif
  1170. return err;
  1171. }
  1172. EXPORT_SYMBOL(ip_getsockopt);
  1173. #ifdef CONFIG_COMPAT
  1174. int compat_ip_getsockopt(struct sock *sk, int level, int optname,
  1175. char __user *optval, int __user *optlen)
  1176. {
  1177. int err;
  1178. if (optname == MCAST_MSFILTER)
  1179. return compat_mc_getsockopt(sk, level, optname, optval, optlen,
  1180. ip_getsockopt);
  1181. err = do_ip_getsockopt(sk, level, optname, optval, optlen);
  1182. #ifdef CONFIG_NETFILTER
  1183. /* we need to exclude all possible ENOPROTOOPTs except default case */
  1184. if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
  1185. !ip_mroute_opt(optname)) {
  1186. int len;
  1187. if (get_user(len, optlen))
  1188. return -EFAULT;
  1189. lock_sock(sk);
  1190. err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
  1191. release_sock(sk);
  1192. if (err >= 0)
  1193. err = put_user(len, optlen);
  1194. return err;
  1195. }
  1196. #endif
  1197. return err;
  1198. }
  1199. EXPORT_SYMBOL(compat_ip_getsockopt);
  1200. #endif