ip_sockglue.c 36 KB

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