ip_sockglue.c 37 KB

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