ip_sockglue.c 35 KB

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