ip_sockglue.c 31 KB

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