icmp.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * NET3: Implementation of the ICMP protocol layer.
  4. *
  5. * Alan Cox, <alan@lxorguk.ukuu.org.uk>
  6. *
  7. * Some of the function names and the icmp unreach table for this
  8. * module were derived from [icmp.c 1.0.11 06/02/93] by
  9. * Ross Biro, Fred N. van Kempen, Mark Evans, Alan Cox, Gerhard Koerting.
  10. * Other than that this module is a complete rewrite.
  11. *
  12. * Fixes:
  13. * Clemens Fruhwirth : introduce global icmp rate limiting
  14. * with icmp type masking ability instead
  15. * of broken per type icmp timeouts.
  16. * Mike Shaver : RFC1122 checks.
  17. * Alan Cox : Multicast ping reply as self.
  18. * Alan Cox : Fix atomicity lockup in ip_build_xmit
  19. * call.
  20. * Alan Cox : Added 216,128 byte paths to the MTU
  21. * code.
  22. * Martin Mares : RFC1812 checks.
  23. * Martin Mares : Can be configured to follow redirects
  24. * if acting as a router _without_ a
  25. * routing protocol (RFC 1812).
  26. * Martin Mares : Echo requests may be configured to
  27. * be ignored (RFC 1812).
  28. * Martin Mares : Limitation of ICMP error message
  29. * transmit rate (RFC 1812).
  30. * Martin Mares : TOS and Precedence set correctly
  31. * (RFC 1812).
  32. * Martin Mares : Now copying as much data from the
  33. * original packet as we can without
  34. * exceeding 576 bytes (RFC 1812).
  35. * Willy Konynenberg : Transparent proxying support.
  36. * Keith Owens : RFC1191 correction for 4.2BSD based
  37. * path MTU bug.
  38. * Thomas Quinot : ICMP Dest Unreach codes up to 15 are
  39. * valid (RFC 1812).
  40. * Andi Kleen : Check all packet lengths properly
  41. * and moved all kfree_skb() up to
  42. * icmp_rcv.
  43. * Andi Kleen : Move the rate limit bookkeeping
  44. * into the dest entry and use a token
  45. * bucket filter (thanks to ANK). Make
  46. * the rates sysctl configurable.
  47. * Yu Tianli : Fixed two ugly bugs in icmp_send
  48. * - IP option length was accounted wrongly
  49. * - ICMP header length was not accounted
  50. * at all.
  51. * Tristan Greaves : Added sysctl option to ignore bogus
  52. * broadcast responses from broken routers.
  53. *
  54. * To Fix:
  55. *
  56. * - Should use skb_pull() instead of all the manual checking.
  57. * This would also greatly simply some upper layer error handlers. --AK
  58. */
  59. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  60. #include <linux/module.h>
  61. #include <linux/types.h>
  62. #include <linux/jiffies.h>
  63. #include <linux/kernel.h>
  64. #include <linux/fcntl.h>
  65. #include <linux/socket.h>
  66. #include <linux/in.h>
  67. #include <linux/inet.h>
  68. #include <linux/inetdevice.h>
  69. #include <linux/netdevice.h>
  70. #include <linux/string.h>
  71. #include <linux/netfilter_ipv4.h>
  72. #include <linux/slab.h>
  73. #include <net/snmp.h>
  74. #include <net/ip.h>
  75. #include <net/route.h>
  76. #include <net/protocol.h>
  77. #include <net/icmp.h>
  78. #include <net/tcp.h>
  79. #include <net/udp.h>
  80. #include <net/raw.h>
  81. #include <net/ping.h>
  82. #include <linux/skbuff.h>
  83. #include <net/sock.h>
  84. #include <linux/errno.h>
  85. #include <linux/timer.h>
  86. #include <linux/init.h>
  87. #include <linux/uaccess.h>
  88. #include <net/checksum.h>
  89. #include <net/xfrm.h>
  90. #include <net/inet_common.h>
  91. #include <net/ip_fib.h>
  92. #include <net/l3mdev.h>
  93. #include <net/addrconf.h>
  94. #include <net/inet_dscp.h>
  95. #define CREATE_TRACE_POINTS
  96. #include <trace/events/icmp.h>
  97. /*
  98. * Build xmit assembly blocks
  99. */
  100. struct icmp_bxm {
  101. struct sk_buff *skb;
  102. int offset;
  103. int data_len;
  104. struct {
  105. struct icmphdr icmph;
  106. __be32 times[3];
  107. } data;
  108. int head_len;
  109. struct ip_options_data replyopts;
  110. };
  111. /* An array of errno for error messages from dest unreach. */
  112. /* RFC 1122: 3.2.2.1 States that NET_UNREACH, HOST_UNREACH and SR_FAILED MUST be considered 'transient errs'. */
  113. const struct icmp_err icmp_err_convert[] = {
  114. {
  115. .errno = ENETUNREACH, /* ICMP_NET_UNREACH */
  116. .fatal = 0,
  117. },
  118. {
  119. .errno = EHOSTUNREACH, /* ICMP_HOST_UNREACH */
  120. .fatal = 0,
  121. },
  122. {
  123. .errno = ENOPROTOOPT /* ICMP_PROT_UNREACH */,
  124. .fatal = 1,
  125. },
  126. {
  127. .errno = ECONNREFUSED, /* ICMP_PORT_UNREACH */
  128. .fatal = 1,
  129. },
  130. {
  131. .errno = EMSGSIZE, /* ICMP_FRAG_NEEDED */
  132. .fatal = 0,
  133. },
  134. {
  135. .errno = EOPNOTSUPP, /* ICMP_SR_FAILED */
  136. .fatal = 0,
  137. },
  138. {
  139. .errno = ENETUNREACH, /* ICMP_NET_UNKNOWN */
  140. .fatal = 1,
  141. },
  142. {
  143. .errno = EHOSTDOWN, /* ICMP_HOST_UNKNOWN */
  144. .fatal = 1,
  145. },
  146. {
  147. .errno = ENONET, /* ICMP_HOST_ISOLATED */
  148. .fatal = 1,
  149. },
  150. {
  151. .errno = ENETUNREACH, /* ICMP_NET_ANO */
  152. .fatal = 1,
  153. },
  154. {
  155. .errno = EHOSTUNREACH, /* ICMP_HOST_ANO */
  156. .fatal = 1,
  157. },
  158. {
  159. .errno = ENETUNREACH, /* ICMP_NET_UNR_TOS */
  160. .fatal = 0,
  161. },
  162. {
  163. .errno = EHOSTUNREACH, /* ICMP_HOST_UNR_TOS */
  164. .fatal = 0,
  165. },
  166. {
  167. .errno = EHOSTUNREACH, /* ICMP_PKT_FILTERED */
  168. .fatal = 1,
  169. },
  170. {
  171. .errno = EHOSTUNREACH, /* ICMP_PREC_VIOLATION */
  172. .fatal = 1,
  173. },
  174. {
  175. .errno = EHOSTUNREACH, /* ICMP_PREC_CUTOFF */
  176. .fatal = 1,
  177. },
  178. };
  179. EXPORT_SYMBOL(icmp_err_convert);
  180. /*
  181. * ICMP control array. This specifies what to do with each ICMP.
  182. */
  183. struct icmp_control {
  184. enum skb_drop_reason (*handler)(struct sk_buff *skb);
  185. short error; /* This ICMP is classed as an error message */
  186. };
  187. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
  188. static DEFINE_PER_CPU(struct sock *, ipv4_icmp_sk);
  189. /* Called with BH disabled */
  190. static inline struct sock *icmp_xmit_lock(struct net *net)
  191. {
  192. struct sock *sk;
  193. sk = this_cpu_read(ipv4_icmp_sk);
  194. if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
  195. /* This can happen if the output path signals a
  196. * dst_link_failure() for an outgoing ICMP packet.
  197. */
  198. return NULL;
  199. }
  200. sock_net_set(sk, net);
  201. return sk;
  202. }
  203. static inline void icmp_xmit_unlock(struct sock *sk)
  204. {
  205. sock_net_set(sk, &init_net);
  206. spin_unlock(&sk->sk_lock.slock);
  207. }
  208. /**
  209. * icmp_global_allow - Are we allowed to send one more ICMP message ?
  210. * @net: network namespace
  211. *
  212. * Uses a token bucket to limit our ICMP messages to ~sysctl_icmp_msgs_per_sec.
  213. * Returns false if we reached the limit and can not send another packet.
  214. * Works in tandem with icmp_global_consume().
  215. */
  216. bool icmp_global_allow(struct net *net)
  217. {
  218. u32 delta, now, oldstamp;
  219. int incr, new, old;
  220. /* Note: many cpus could find this condition true.
  221. * Then later icmp_global_consume() could consume more credits,
  222. * this is an acceptable race.
  223. */
  224. if (atomic_read(&net->ipv4.icmp_global_credit) > 0)
  225. return true;
  226. now = jiffies;
  227. oldstamp = READ_ONCE(net->ipv4.icmp_global_stamp);
  228. delta = min_t(u32, now - oldstamp, HZ);
  229. if (delta < HZ / 50)
  230. return false;
  231. incr = READ_ONCE(net->ipv4.sysctl_icmp_msgs_per_sec) * delta / HZ;
  232. if (!incr)
  233. return false;
  234. if (cmpxchg(&net->ipv4.icmp_global_stamp, oldstamp, now) == oldstamp) {
  235. old = atomic_read(&net->ipv4.icmp_global_credit);
  236. do {
  237. new = min(old + incr, READ_ONCE(net->ipv4.sysctl_icmp_msgs_burst));
  238. } while (!atomic_try_cmpxchg(&net->ipv4.icmp_global_credit, &old, new));
  239. }
  240. return true;
  241. }
  242. EXPORT_SYMBOL(icmp_global_allow);
  243. void icmp_global_consume(struct net *net)
  244. {
  245. int credits = get_random_u32_below(3);
  246. /* Note: this might make icmp_global.credit negative. */
  247. if (credits)
  248. atomic_sub(credits, &net->ipv4.icmp_global_credit);
  249. }
  250. EXPORT_SYMBOL(icmp_global_consume);
  251. static bool icmpv4_mask_allow(struct net *net, int type, int code)
  252. {
  253. if (type > NR_ICMP_TYPES)
  254. return true;
  255. /* Don't limit PMTU discovery. */
  256. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
  257. return true;
  258. /* Limit if icmp type is enabled in ratemask. */
  259. if (!((1 << type) & READ_ONCE(net->ipv4.sysctl_icmp_ratemask)))
  260. return true;
  261. return false;
  262. }
  263. static bool icmpv4_global_allow(struct net *net, int type, int code,
  264. bool *apply_ratelimit)
  265. {
  266. if (icmpv4_mask_allow(net, type, code))
  267. return true;
  268. if (icmp_global_allow(net)) {
  269. *apply_ratelimit = true;
  270. return true;
  271. }
  272. __ICMP_INC_STATS(net, ICMP_MIB_RATELIMITGLOBAL);
  273. return false;
  274. }
  275. /*
  276. * Send an ICMP frame.
  277. */
  278. static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
  279. struct flowi4 *fl4, int type, int code,
  280. bool apply_ratelimit)
  281. {
  282. struct dst_entry *dst = &rt->dst;
  283. struct inet_peer *peer;
  284. struct net_device *dev;
  285. bool rc = true;
  286. if (!apply_ratelimit)
  287. return true;
  288. /* No rate limit on loopback */
  289. dev = dst_dev(dst);
  290. if (dev && (dev->flags & IFF_LOOPBACK))
  291. goto out;
  292. rcu_read_lock();
  293. peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr,
  294. l3mdev_master_ifindex_rcu(dev));
  295. rc = inet_peer_xrlim_allow(peer,
  296. READ_ONCE(net->ipv4.sysctl_icmp_ratelimit));
  297. rcu_read_unlock();
  298. out:
  299. if (!rc)
  300. __ICMP_INC_STATS(net, ICMP_MIB_RATELIMITHOST);
  301. else
  302. icmp_global_consume(net);
  303. return rc;
  304. }
  305. /*
  306. * Maintain the counters used in the SNMP statistics for outgoing ICMP
  307. */
  308. void icmp_out_count(struct net *net, unsigned char type)
  309. {
  310. ICMPMSGOUT_INC_STATS(net, type);
  311. ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS);
  312. }
  313. /*
  314. * Checksum each fragment, and on the first include the headers and final
  315. * checksum.
  316. */
  317. static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
  318. struct sk_buff *skb)
  319. {
  320. struct icmp_bxm *icmp_param = from;
  321. __wsum csum;
  322. csum = skb_copy_and_csum_bits(icmp_param->skb,
  323. icmp_param->offset + offset,
  324. to, len);
  325. skb->csum = csum_block_add(skb->csum, csum, odd);
  326. if (icmp_pointers[icmp_param->data.icmph.type].error)
  327. nf_ct_attach(skb, icmp_param->skb);
  328. return 0;
  329. }
  330. static void icmp_push_reply(struct sock *sk,
  331. struct icmp_bxm *icmp_param,
  332. struct flowi4 *fl4,
  333. struct ipcm_cookie *ipc, struct rtable **rt)
  334. {
  335. struct sk_buff *skb;
  336. if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
  337. icmp_param->data_len+icmp_param->head_len,
  338. icmp_param->head_len,
  339. ipc, rt, MSG_DONTWAIT) < 0) {
  340. __ICMP_INC_STATS(sock_net(sk), ICMP_MIB_OUTERRORS);
  341. ip_flush_pending_frames(sk);
  342. } else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
  343. struct icmphdr *icmph = icmp_hdr(skb);
  344. __wsum csum;
  345. struct sk_buff *skb1;
  346. csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
  347. (char *)icmph,
  348. icmp_param->head_len);
  349. skb_queue_walk(&sk->sk_write_queue, skb1) {
  350. csum = csum_add(csum, skb1->csum);
  351. }
  352. icmph->checksum = csum_fold(csum);
  353. skb->ip_summed = CHECKSUM_NONE;
  354. ip_push_pending_frames(sk, fl4);
  355. }
  356. }
  357. /*
  358. * Driving logic for building and sending ICMP messages.
  359. */
  360. static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
  361. {
  362. struct rtable *rt = skb_rtable(skb);
  363. struct net *net = dev_net_rcu(rt->dst.dev);
  364. bool apply_ratelimit = false;
  365. struct ipcm_cookie ipc;
  366. struct flowi4 fl4;
  367. struct sock *sk;
  368. struct inet_sock *inet;
  369. __be32 daddr, saddr;
  370. u32 mark = IP4_REPLY_MARK(net, skb->mark);
  371. int type = icmp_param->data.icmph.type;
  372. int code = icmp_param->data.icmph.code;
  373. if (ip_options_echo(net, &icmp_param->replyopts.opt.opt, skb))
  374. return;
  375. /* Needed by both icmpv4_global_allow and icmp_xmit_lock */
  376. local_bh_disable();
  377. /* is global icmp_msgs_per_sec exhausted ? */
  378. if (!icmpv4_global_allow(net, type, code, &apply_ratelimit))
  379. goto out_bh_enable;
  380. sk = icmp_xmit_lock(net);
  381. if (!sk)
  382. goto out_bh_enable;
  383. inet = inet_sk(sk);
  384. icmp_param->data.icmph.checksum = 0;
  385. ipcm_init(&ipc);
  386. inet->tos = ip_hdr(skb)->tos;
  387. ipc.sockc.mark = mark;
  388. daddr = ipc.addr = ip_hdr(skb)->saddr;
  389. saddr = fib_compute_spec_dst(skb);
  390. if (icmp_param->replyopts.opt.opt.optlen) {
  391. ipc.opt = &icmp_param->replyopts.opt;
  392. if (ipc.opt->opt.srr)
  393. daddr = icmp_param->replyopts.opt.opt.faddr;
  394. }
  395. memset(&fl4, 0, sizeof(fl4));
  396. fl4.daddr = daddr;
  397. fl4.saddr = saddr;
  398. fl4.flowi4_mark = mark;
  399. fl4.flowi4_uid = sock_net_uid(net, NULL);
  400. fl4.flowi4_tos = ip_hdr(skb)->tos & INET_DSCP_MASK;
  401. fl4.flowi4_proto = IPPROTO_ICMP;
  402. fl4.flowi4_oif = l3mdev_master_ifindex(skb->dev);
  403. security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4));
  404. rt = ip_route_output_key(net, &fl4);
  405. if (IS_ERR(rt))
  406. goto out_unlock;
  407. if (icmpv4_xrlim_allow(net, rt, &fl4, type, code, apply_ratelimit))
  408. icmp_push_reply(sk, icmp_param, &fl4, &ipc, &rt);
  409. ip_rt_put(rt);
  410. out_unlock:
  411. icmp_xmit_unlock(sk);
  412. out_bh_enable:
  413. local_bh_enable();
  414. }
  415. /*
  416. * The device used for looking up which routing table to use for sending an ICMP
  417. * error is preferably the source whenever it is set, which should ensure the
  418. * icmp error can be sent to the source host, else lookup using the routing
  419. * table of the destination device, else use the main routing table (index 0).
  420. */
  421. static struct net_device *icmp_get_route_lookup_dev(struct sk_buff *skb)
  422. {
  423. struct net_device *dev = skb->dev;
  424. const struct dst_entry *dst;
  425. if (dev)
  426. return dev;
  427. dst = skb_dst(skb);
  428. return dst ? dst_dev(dst) : NULL;
  429. }
  430. static struct rtable *icmp_route_lookup(struct net *net, struct flowi4 *fl4,
  431. struct sk_buff *skb_in,
  432. const struct iphdr *iph, __be32 saddr,
  433. dscp_t dscp, u32 mark, int type,
  434. int code, struct icmp_bxm *param)
  435. {
  436. struct net_device *route_lookup_dev;
  437. struct dst_entry *dst, *dst2;
  438. struct rtable *rt, *rt2;
  439. struct flowi4 fl4_dec;
  440. int err;
  441. memset(fl4, 0, sizeof(*fl4));
  442. fl4->daddr = (param->replyopts.opt.opt.srr ?
  443. param->replyopts.opt.opt.faddr : iph->saddr);
  444. fl4->saddr = saddr;
  445. fl4->flowi4_mark = mark;
  446. fl4->flowi4_uid = sock_net_uid(net, NULL);
  447. fl4->flowi4_tos = inet_dscp_to_dsfield(dscp);
  448. fl4->flowi4_proto = IPPROTO_ICMP;
  449. fl4->fl4_icmp_type = type;
  450. fl4->fl4_icmp_code = code;
  451. route_lookup_dev = icmp_get_route_lookup_dev(skb_in);
  452. fl4->flowi4_oif = l3mdev_master_ifindex(route_lookup_dev);
  453. security_skb_classify_flow(skb_in, flowi4_to_flowi_common(fl4));
  454. rt = ip_route_output_key_hash(net, fl4, skb_in);
  455. if (IS_ERR(rt))
  456. return rt;
  457. /* No need to clone since we're just using its address. */
  458. rt2 = rt;
  459. dst = xfrm_lookup(net, &rt->dst,
  460. flowi4_to_flowi(fl4), NULL, 0);
  461. rt = dst_rtable(dst);
  462. if (!IS_ERR(dst)) {
  463. if (rt != rt2)
  464. return rt;
  465. if (inet_addr_type_dev_table(net, route_lookup_dev,
  466. fl4->daddr) == RTN_LOCAL)
  467. return rt;
  468. } else if (PTR_ERR(dst) == -EPERM) {
  469. rt = NULL;
  470. } else {
  471. return rt;
  472. }
  473. err = xfrm_decode_session_reverse(net, skb_in, flowi4_to_flowi(&fl4_dec), AF_INET);
  474. if (err)
  475. goto relookup_failed;
  476. if (inet_addr_type_dev_table(net, route_lookup_dev,
  477. fl4_dec.saddr) == RTN_LOCAL) {
  478. rt2 = __ip_route_output_key(net, &fl4_dec);
  479. if (IS_ERR(rt2))
  480. err = PTR_ERR(rt2);
  481. } else {
  482. struct flowi4 fl4_2 = {};
  483. unsigned long orefdst;
  484. fl4_2.daddr = fl4_dec.saddr;
  485. rt2 = ip_route_output_key(net, &fl4_2);
  486. if (IS_ERR(rt2)) {
  487. err = PTR_ERR(rt2);
  488. goto relookup_failed;
  489. }
  490. /* Ugh! */
  491. orefdst = skb_in->_skb_refdst; /* save old refdst */
  492. skb_dst_set(skb_in, NULL);
  493. err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr,
  494. dscp, rt2->dst.dev);
  495. dst_release(&rt2->dst);
  496. rt2 = skb_rtable(skb_in);
  497. skb_in->_skb_refdst = orefdst; /* restore old refdst */
  498. }
  499. if (err)
  500. goto relookup_failed;
  501. dst2 = xfrm_lookup(net, &rt2->dst, flowi4_to_flowi(&fl4_dec), NULL,
  502. XFRM_LOOKUP_ICMP);
  503. rt2 = dst_rtable(dst2);
  504. if (!IS_ERR(dst2)) {
  505. dst_release(&rt->dst);
  506. memcpy(fl4, &fl4_dec, sizeof(*fl4));
  507. rt = rt2;
  508. } else if (PTR_ERR(dst2) == -EPERM) {
  509. if (rt)
  510. dst_release(&rt->dst);
  511. return rt2;
  512. } else {
  513. err = PTR_ERR(dst2);
  514. goto relookup_failed;
  515. }
  516. return rt;
  517. relookup_failed:
  518. if (rt)
  519. return rt;
  520. return ERR_PTR(err);
  521. }
  522. /*
  523. * Send an ICMP message in response to a situation
  524. *
  525. * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
  526. * MAY send more (we do).
  527. * MUST NOT change this header information.
  528. * MUST NOT reply to a multicast/broadcast IP address.
  529. * MUST NOT reply to a multicast/broadcast MAC address.
  530. * MUST reply to only the first fragment.
  531. */
  532. void __icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info,
  533. const struct ip_options *opt)
  534. {
  535. struct iphdr *iph;
  536. int room;
  537. struct icmp_bxm icmp_param;
  538. struct rtable *rt = skb_rtable(skb_in);
  539. bool apply_ratelimit = false;
  540. struct ipcm_cookie ipc;
  541. struct flowi4 fl4;
  542. __be32 saddr;
  543. u8 tos;
  544. u32 mark;
  545. struct net *net;
  546. struct sock *sk;
  547. if (!rt)
  548. return;
  549. rcu_read_lock();
  550. if (rt->dst.dev)
  551. net = dev_net_rcu(rt->dst.dev);
  552. else if (skb_in->dev)
  553. net = dev_net_rcu(skb_in->dev);
  554. else
  555. goto out;
  556. /*
  557. * Find the original header. It is expected to be valid, of course.
  558. * Check this, icmp_send is called from the most obscure devices
  559. * sometimes.
  560. */
  561. iph = ip_hdr(skb_in);
  562. if ((u8 *)iph < skb_in->head ||
  563. (skb_network_header(skb_in) + sizeof(*iph)) >
  564. skb_tail_pointer(skb_in))
  565. goto out;
  566. /*
  567. * No replies to physical multicast/broadcast
  568. */
  569. if (skb_in->pkt_type != PACKET_HOST)
  570. goto out;
  571. /*
  572. * Now check at the protocol level
  573. */
  574. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  575. goto out;
  576. /*
  577. * Only reply to fragment 0. We byte re-order the constant
  578. * mask for efficiency.
  579. */
  580. if (iph->frag_off & htons(IP_OFFSET))
  581. goto out;
  582. /*
  583. * If we send an ICMP error to an ICMP error a mess would result..
  584. */
  585. if (icmp_pointers[type].error) {
  586. /*
  587. * We are an error, check if we are replying to an
  588. * ICMP error
  589. */
  590. if (iph->protocol == IPPROTO_ICMP) {
  591. u8 _inner_type, *itp;
  592. itp = skb_header_pointer(skb_in,
  593. skb_network_header(skb_in) +
  594. (iph->ihl << 2) +
  595. offsetof(struct icmphdr,
  596. type) -
  597. skb_in->data,
  598. sizeof(_inner_type),
  599. &_inner_type);
  600. if (!itp)
  601. goto out;
  602. /*
  603. * Assume any unknown ICMP type is an error. This
  604. * isn't specified by the RFC, but think about it..
  605. */
  606. if (*itp > NR_ICMP_TYPES ||
  607. icmp_pointers[*itp].error)
  608. goto out;
  609. }
  610. }
  611. /* Needed by both icmpv4_global_allow and icmp_xmit_lock */
  612. local_bh_disable();
  613. /* Check global sysctl_icmp_msgs_per_sec ratelimit, unless
  614. * incoming dev is loopback. If outgoing dev change to not be
  615. * loopback, then peer ratelimit still work (in icmpv4_xrlim_allow)
  616. */
  617. if (!(skb_in->dev && (skb_in->dev->flags&IFF_LOOPBACK)) &&
  618. !icmpv4_global_allow(net, type, code, &apply_ratelimit))
  619. goto out_bh_enable;
  620. sk = icmp_xmit_lock(net);
  621. if (!sk)
  622. goto out_bh_enable;
  623. /*
  624. * Construct source address and options.
  625. */
  626. saddr = iph->daddr;
  627. if (!(rt->rt_flags & RTCF_LOCAL)) {
  628. struct net_device *dev = NULL;
  629. rcu_read_lock();
  630. if (rt_is_input_route(rt) &&
  631. READ_ONCE(net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr))
  632. dev = dev_get_by_index_rcu(net, inet_iif(skb_in));
  633. if (dev)
  634. saddr = inet_select_addr(dev, iph->saddr,
  635. RT_SCOPE_LINK);
  636. else
  637. saddr = 0;
  638. rcu_read_unlock();
  639. }
  640. tos = icmp_pointers[type].error ? (RT_TOS(iph->tos) |
  641. IPTOS_PREC_INTERNETCONTROL) :
  642. iph->tos;
  643. mark = IP4_REPLY_MARK(net, skb_in->mark);
  644. if (__ip_options_echo(net, &icmp_param.replyopts.opt.opt, skb_in, opt))
  645. goto out_unlock;
  646. /*
  647. * Prepare data for ICMP header.
  648. */
  649. icmp_param.data.icmph.type = type;
  650. icmp_param.data.icmph.code = code;
  651. icmp_param.data.icmph.un.gateway = info;
  652. icmp_param.data.icmph.checksum = 0;
  653. icmp_param.skb = skb_in;
  654. icmp_param.offset = skb_network_offset(skb_in);
  655. inet_sk(sk)->tos = tos;
  656. ipcm_init(&ipc);
  657. ipc.addr = iph->saddr;
  658. ipc.opt = &icmp_param.replyopts.opt;
  659. ipc.sockc.mark = mark;
  660. rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr,
  661. inet_dsfield_to_dscp(tos), mark, type, code,
  662. &icmp_param);
  663. if (IS_ERR(rt))
  664. goto out_unlock;
  665. /* peer icmp_ratelimit */
  666. if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code, apply_ratelimit))
  667. goto ende;
  668. /* RFC says return as much as we can without exceeding 576 bytes. */
  669. room = dst_mtu(&rt->dst);
  670. if (room > 576)
  671. room = 576;
  672. room -= sizeof(struct iphdr) + icmp_param.replyopts.opt.opt.optlen;
  673. room -= sizeof(struct icmphdr);
  674. /* Guard against tiny mtu. We need to include at least one
  675. * IP network header for this message to make any sense.
  676. */
  677. if (room <= (int)sizeof(struct iphdr))
  678. goto ende;
  679. icmp_param.data_len = skb_in->len - icmp_param.offset;
  680. if (icmp_param.data_len > room)
  681. icmp_param.data_len = room;
  682. icmp_param.head_len = sizeof(struct icmphdr);
  683. /* if we don't have a source address at this point, fall back to the
  684. * dummy address instead of sending out a packet with a source address
  685. * of 0.0.0.0
  686. */
  687. if (!fl4.saddr)
  688. fl4.saddr = htonl(INADDR_DUMMY);
  689. trace_icmp_send(skb_in, type, code);
  690. icmp_push_reply(sk, &icmp_param, &fl4, &ipc, &rt);
  691. ende:
  692. ip_rt_put(rt);
  693. out_unlock:
  694. icmp_xmit_unlock(sk);
  695. out_bh_enable:
  696. local_bh_enable();
  697. out:
  698. rcu_read_unlock();
  699. }
  700. EXPORT_SYMBOL(__icmp_send);
  701. #if IS_ENABLED(CONFIG_NF_NAT)
  702. #include <net/netfilter/nf_conntrack.h>
  703. void icmp_ndo_send(struct sk_buff *skb_in, int type, int code, __be32 info)
  704. {
  705. struct sk_buff *cloned_skb = NULL;
  706. struct ip_options opts = { 0 };
  707. enum ip_conntrack_info ctinfo;
  708. enum ip_conntrack_dir dir;
  709. struct nf_conn *ct;
  710. __be32 orig_ip;
  711. ct = nf_ct_get(skb_in, &ctinfo);
  712. if (!ct || !(READ_ONCE(ct->status) & IPS_NAT_MASK)) {
  713. __icmp_send(skb_in, type, code, info, &opts);
  714. return;
  715. }
  716. if (skb_shared(skb_in))
  717. skb_in = cloned_skb = skb_clone(skb_in, GFP_ATOMIC);
  718. if (unlikely(!skb_in || skb_network_header(skb_in) < skb_in->head ||
  719. (skb_network_header(skb_in) + sizeof(struct iphdr)) >
  720. skb_tail_pointer(skb_in) || skb_ensure_writable(skb_in,
  721. skb_network_offset(skb_in) + sizeof(struct iphdr))))
  722. goto out;
  723. orig_ip = ip_hdr(skb_in)->saddr;
  724. dir = CTINFO2DIR(ctinfo);
  725. ip_hdr(skb_in)->saddr = ct->tuplehash[dir].tuple.src.u3.ip;
  726. __icmp_send(skb_in, type, code, info, &opts);
  727. ip_hdr(skb_in)->saddr = orig_ip;
  728. out:
  729. consume_skb(cloned_skb);
  730. }
  731. EXPORT_SYMBOL(icmp_ndo_send);
  732. #endif
  733. static void icmp_socket_deliver(struct sk_buff *skb, u32 info)
  734. {
  735. const struct iphdr *iph = (const struct iphdr *)skb->data;
  736. const struct net_protocol *ipprot;
  737. int protocol = iph->protocol;
  738. /* Checkin full IP header plus 8 bytes of protocol to
  739. * avoid additional coding at protocol handlers.
  740. */
  741. if (!pskb_may_pull(skb, iph->ihl * 4 + 8)) {
  742. __ICMP_INC_STATS(dev_net_rcu(skb->dev), ICMP_MIB_INERRORS);
  743. return;
  744. }
  745. raw_icmp_error(skb, protocol, info);
  746. ipprot = rcu_dereference(inet_protos[protocol]);
  747. if (ipprot && ipprot->err_handler)
  748. ipprot->err_handler(skb, info);
  749. }
  750. static bool icmp_tag_validation(int proto)
  751. {
  752. bool ok;
  753. rcu_read_lock();
  754. ok = rcu_dereference(inet_protos[proto])->icmp_strict_tag_validation;
  755. rcu_read_unlock();
  756. return ok;
  757. }
  758. /*
  759. * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEEDED, ICMP_QUENCH, and
  760. * ICMP_PARAMETERPROB.
  761. */
  762. static enum skb_drop_reason icmp_unreach(struct sk_buff *skb)
  763. {
  764. enum skb_drop_reason reason = SKB_NOT_DROPPED_YET;
  765. const struct iphdr *iph;
  766. struct icmphdr *icmph;
  767. struct net *net;
  768. u32 info = 0;
  769. net = skb_dst_dev_net_rcu(skb);
  770. /*
  771. * Incomplete header ?
  772. * Only checks for the IP header, there should be an
  773. * additional check for longer headers in upper levels.
  774. */
  775. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  776. goto out_err;
  777. icmph = icmp_hdr(skb);
  778. iph = (const struct iphdr *)skb->data;
  779. if (iph->ihl < 5) { /* Mangled header, drop. */
  780. reason = SKB_DROP_REASON_IP_INHDR;
  781. goto out_err;
  782. }
  783. switch (icmph->type) {
  784. case ICMP_DEST_UNREACH:
  785. switch (icmph->code & 15) {
  786. case ICMP_NET_UNREACH:
  787. case ICMP_HOST_UNREACH:
  788. case ICMP_PROT_UNREACH:
  789. case ICMP_PORT_UNREACH:
  790. break;
  791. case ICMP_FRAG_NEEDED:
  792. /* for documentation of the ip_no_pmtu_disc
  793. * values please see
  794. * Documentation/networking/ip-sysctl.rst
  795. */
  796. switch (READ_ONCE(net->ipv4.sysctl_ip_no_pmtu_disc)) {
  797. default:
  798. net_dbg_ratelimited("%pI4: fragmentation needed and DF set\n",
  799. &iph->daddr);
  800. break;
  801. case 2:
  802. goto out;
  803. case 3:
  804. if (!icmp_tag_validation(iph->protocol))
  805. goto out;
  806. fallthrough;
  807. case 0:
  808. info = ntohs(icmph->un.frag.mtu);
  809. }
  810. break;
  811. case ICMP_SR_FAILED:
  812. net_dbg_ratelimited("%pI4: Source Route Failed\n",
  813. &iph->daddr);
  814. break;
  815. default:
  816. break;
  817. }
  818. if (icmph->code > NR_ICMP_UNREACH)
  819. goto out;
  820. break;
  821. case ICMP_PARAMETERPROB:
  822. info = ntohl(icmph->un.gateway) >> 24;
  823. break;
  824. case ICMP_TIME_EXCEEDED:
  825. __ICMP_INC_STATS(net, ICMP_MIB_INTIMEEXCDS);
  826. if (icmph->code == ICMP_EXC_FRAGTIME)
  827. goto out;
  828. break;
  829. }
  830. /*
  831. * Throw it at our lower layers
  832. *
  833. * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
  834. * header.
  835. * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
  836. * transport layer.
  837. * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
  838. * transport layer.
  839. */
  840. /*
  841. * Check the other end isn't violating RFC 1122. Some routers send
  842. * bogus responses to broadcast frames. If you see this message
  843. * first check your netmask matches at both ends, if it does then
  844. * get the other vendor to fix their kit.
  845. */
  846. if (!READ_ONCE(net->ipv4.sysctl_icmp_ignore_bogus_error_responses) &&
  847. inet_addr_type_dev_table(net, skb->dev, iph->daddr) == RTN_BROADCAST) {
  848. net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
  849. &ip_hdr(skb)->saddr,
  850. icmph->type, icmph->code,
  851. &iph->daddr, skb->dev->name);
  852. goto out;
  853. }
  854. icmp_socket_deliver(skb, info);
  855. out:
  856. return reason;
  857. out_err:
  858. __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
  859. return reason ?: SKB_DROP_REASON_NOT_SPECIFIED;
  860. }
  861. /*
  862. * Handle ICMP_REDIRECT.
  863. */
  864. static enum skb_drop_reason icmp_redirect(struct sk_buff *skb)
  865. {
  866. if (skb->len < sizeof(struct iphdr)) {
  867. __ICMP_INC_STATS(dev_net_rcu(skb->dev), ICMP_MIB_INERRORS);
  868. return SKB_DROP_REASON_PKT_TOO_SMALL;
  869. }
  870. if (!pskb_may_pull(skb, sizeof(struct iphdr))) {
  871. /* there aught to be a stat */
  872. return SKB_DROP_REASON_NOMEM;
  873. }
  874. icmp_socket_deliver(skb, ntohl(icmp_hdr(skb)->un.gateway));
  875. return SKB_NOT_DROPPED_YET;
  876. }
  877. /*
  878. * Handle ICMP_ECHO ("ping") and ICMP_EXT_ECHO ("PROBE") requests.
  879. *
  880. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  881. * requests.
  882. * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
  883. * included in the reply.
  884. * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
  885. * echo requests, MUST have default=NOT.
  886. * RFC 8335: 8 MUST have a config option to enable/disable ICMP
  887. * Extended Echo Functionality, MUST be disabled by default
  888. * See also WRT handling of options once they are done and working.
  889. */
  890. static enum skb_drop_reason icmp_echo(struct sk_buff *skb)
  891. {
  892. struct icmp_bxm icmp_param;
  893. struct net *net;
  894. net = skb_dst_dev_net_rcu(skb);
  895. /* should there be an ICMP stat for ignored echos? */
  896. if (READ_ONCE(net->ipv4.sysctl_icmp_echo_ignore_all))
  897. return SKB_NOT_DROPPED_YET;
  898. icmp_param.data.icmph = *icmp_hdr(skb);
  899. icmp_param.skb = skb;
  900. icmp_param.offset = 0;
  901. icmp_param.data_len = skb->len;
  902. icmp_param.head_len = sizeof(struct icmphdr);
  903. if (icmp_param.data.icmph.type == ICMP_ECHO)
  904. icmp_param.data.icmph.type = ICMP_ECHOREPLY;
  905. else if (!icmp_build_probe(skb, &icmp_param.data.icmph))
  906. return SKB_NOT_DROPPED_YET;
  907. icmp_reply(&icmp_param, skb);
  908. return SKB_NOT_DROPPED_YET;
  909. }
  910. /* Helper for icmp_echo and icmpv6_echo_reply.
  911. * Searches for net_device that matches PROBE interface identifier
  912. * and builds PROBE reply message in icmphdr.
  913. *
  914. * Returns false if PROBE responses are disabled via sysctl
  915. */
  916. bool icmp_build_probe(struct sk_buff *skb, struct icmphdr *icmphdr)
  917. {
  918. struct net *net = dev_net_rcu(skb->dev);
  919. struct icmp_ext_hdr *ext_hdr, _ext_hdr;
  920. struct icmp_ext_echo_iio *iio, _iio;
  921. struct inet6_dev *in6_dev;
  922. struct in_device *in_dev;
  923. struct net_device *dev;
  924. char buff[IFNAMSIZ];
  925. u16 ident_len;
  926. u8 status;
  927. if (!READ_ONCE(net->ipv4.sysctl_icmp_echo_enable_probe))
  928. return false;
  929. /* We currently only support probing interfaces on the proxy node
  930. * Check to ensure L-bit is set
  931. */
  932. if (!(ntohs(icmphdr->un.echo.sequence) & 1))
  933. return false;
  934. /* Clear status bits in reply message */
  935. icmphdr->un.echo.sequence &= htons(0xFF00);
  936. if (icmphdr->type == ICMP_EXT_ECHO)
  937. icmphdr->type = ICMP_EXT_ECHOREPLY;
  938. else
  939. icmphdr->type = ICMPV6_EXT_ECHO_REPLY;
  940. ext_hdr = skb_header_pointer(skb, 0, sizeof(_ext_hdr), &_ext_hdr);
  941. /* Size of iio is class_type dependent.
  942. * Only check header here and assign length based on ctype in the switch statement
  943. */
  944. iio = skb_header_pointer(skb, sizeof(_ext_hdr), sizeof(iio->extobj_hdr), &_iio);
  945. if (!ext_hdr || !iio)
  946. goto send_mal_query;
  947. if (ntohs(iio->extobj_hdr.length) <= sizeof(iio->extobj_hdr) ||
  948. ntohs(iio->extobj_hdr.length) > sizeof(_iio))
  949. goto send_mal_query;
  950. ident_len = ntohs(iio->extobj_hdr.length) - sizeof(iio->extobj_hdr);
  951. iio = skb_header_pointer(skb, sizeof(_ext_hdr),
  952. sizeof(iio->extobj_hdr) + ident_len, &_iio);
  953. if (!iio)
  954. goto send_mal_query;
  955. status = 0;
  956. dev = NULL;
  957. switch (iio->extobj_hdr.class_type) {
  958. case ICMP_EXT_ECHO_CTYPE_NAME:
  959. if (ident_len >= IFNAMSIZ)
  960. goto send_mal_query;
  961. memset(buff, 0, sizeof(buff));
  962. memcpy(buff, &iio->ident.name, ident_len);
  963. dev = dev_get_by_name(net, buff);
  964. break;
  965. case ICMP_EXT_ECHO_CTYPE_INDEX:
  966. if (ident_len != sizeof(iio->ident.ifindex))
  967. goto send_mal_query;
  968. dev = dev_get_by_index(net, ntohl(iio->ident.ifindex));
  969. break;
  970. case ICMP_EXT_ECHO_CTYPE_ADDR:
  971. if (ident_len < sizeof(iio->ident.addr.ctype3_hdr) ||
  972. ident_len != sizeof(iio->ident.addr.ctype3_hdr) +
  973. iio->ident.addr.ctype3_hdr.addrlen)
  974. goto send_mal_query;
  975. switch (ntohs(iio->ident.addr.ctype3_hdr.afi)) {
  976. case ICMP_AFI_IP:
  977. if (iio->ident.addr.ctype3_hdr.addrlen != sizeof(struct in_addr))
  978. goto send_mal_query;
  979. dev = ip_dev_find(net, iio->ident.addr.ip_addr.ipv4_addr);
  980. break;
  981. #if IS_ENABLED(CONFIG_IPV6)
  982. case ICMP_AFI_IP6:
  983. if (iio->ident.addr.ctype3_hdr.addrlen != sizeof(struct in6_addr))
  984. goto send_mal_query;
  985. dev = ipv6_stub->ipv6_dev_find(net, &iio->ident.addr.ip_addr.ipv6_addr, dev);
  986. dev_hold(dev);
  987. break;
  988. #endif
  989. default:
  990. goto send_mal_query;
  991. }
  992. break;
  993. default:
  994. goto send_mal_query;
  995. }
  996. if (!dev) {
  997. icmphdr->code = ICMP_EXT_CODE_NO_IF;
  998. return true;
  999. }
  1000. /* Fill bits in reply message */
  1001. if (dev->flags & IFF_UP)
  1002. status |= ICMP_EXT_ECHOREPLY_ACTIVE;
  1003. in_dev = __in_dev_get_rcu(dev);
  1004. if (in_dev && rcu_access_pointer(in_dev->ifa_list))
  1005. status |= ICMP_EXT_ECHOREPLY_IPV4;
  1006. in6_dev = __in6_dev_get(dev);
  1007. if (in6_dev && !list_empty(&in6_dev->addr_list))
  1008. status |= ICMP_EXT_ECHOREPLY_IPV6;
  1009. dev_put(dev);
  1010. icmphdr->un.echo.sequence |= htons(status);
  1011. return true;
  1012. send_mal_query:
  1013. icmphdr->code = ICMP_EXT_CODE_MAL_QUERY;
  1014. return true;
  1015. }
  1016. EXPORT_SYMBOL_GPL(icmp_build_probe);
  1017. /*
  1018. * Handle ICMP Timestamp requests.
  1019. * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
  1020. * SHOULD be in the kernel for minimum random latency.
  1021. * MUST be accurate to a few minutes.
  1022. * MUST be updated at least at 15Hz.
  1023. */
  1024. static enum skb_drop_reason icmp_timestamp(struct sk_buff *skb)
  1025. {
  1026. struct icmp_bxm icmp_param;
  1027. /*
  1028. * Too short.
  1029. */
  1030. if (skb->len < 4)
  1031. goto out_err;
  1032. /*
  1033. * Fill in the current time as ms since midnight UT:
  1034. */
  1035. icmp_param.data.times[1] = inet_current_timestamp();
  1036. icmp_param.data.times[2] = icmp_param.data.times[1];
  1037. BUG_ON(skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4));
  1038. icmp_param.data.icmph = *icmp_hdr(skb);
  1039. icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
  1040. icmp_param.data.icmph.code = 0;
  1041. icmp_param.skb = skb;
  1042. icmp_param.offset = 0;
  1043. icmp_param.data_len = 0;
  1044. icmp_param.head_len = sizeof(struct icmphdr) + 12;
  1045. icmp_reply(&icmp_param, skb);
  1046. return SKB_NOT_DROPPED_YET;
  1047. out_err:
  1048. __ICMP_INC_STATS(skb_dst_dev_net_rcu(skb), ICMP_MIB_INERRORS);
  1049. return SKB_DROP_REASON_PKT_TOO_SMALL;
  1050. }
  1051. static enum skb_drop_reason icmp_discard(struct sk_buff *skb)
  1052. {
  1053. /* pretend it was a success */
  1054. return SKB_NOT_DROPPED_YET;
  1055. }
  1056. /*
  1057. * Deal with incoming ICMP packets.
  1058. */
  1059. int icmp_rcv(struct sk_buff *skb)
  1060. {
  1061. enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
  1062. struct rtable *rt = skb_rtable(skb);
  1063. struct net *net = dev_net_rcu(rt->dst.dev);
  1064. struct icmphdr *icmph;
  1065. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  1066. struct sec_path *sp = skb_sec_path(skb);
  1067. int nh;
  1068. if (!(sp && sp->xvec[sp->len - 1]->props.flags &
  1069. XFRM_STATE_ICMP)) {
  1070. reason = SKB_DROP_REASON_XFRM_POLICY;
  1071. goto drop;
  1072. }
  1073. if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
  1074. goto drop;
  1075. nh = skb_network_offset(skb);
  1076. skb_set_network_header(skb, sizeof(*icmph));
  1077. if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN,
  1078. skb)) {
  1079. reason = SKB_DROP_REASON_XFRM_POLICY;
  1080. goto drop;
  1081. }
  1082. skb_set_network_header(skb, nh);
  1083. }
  1084. __ICMP_INC_STATS(net, ICMP_MIB_INMSGS);
  1085. if (skb_checksum_simple_validate(skb))
  1086. goto csum_error;
  1087. if (!pskb_pull(skb, sizeof(*icmph)))
  1088. goto error;
  1089. icmph = icmp_hdr(skb);
  1090. ICMPMSGIN_INC_STATS(net, icmph->type);
  1091. /* Check for ICMP Extended Echo (PROBE) messages */
  1092. if (icmph->type == ICMP_EXT_ECHO) {
  1093. /* We can't use icmp_pointers[].handler() because it is an array of
  1094. * size NR_ICMP_TYPES + 1 (19 elements) and PROBE has code 42.
  1095. */
  1096. reason = icmp_echo(skb);
  1097. goto reason_check;
  1098. }
  1099. if (icmph->type == ICMP_EXT_ECHOREPLY) {
  1100. reason = ping_rcv(skb);
  1101. goto reason_check;
  1102. }
  1103. /*
  1104. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  1105. *
  1106. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  1107. * discarded.
  1108. */
  1109. if (icmph->type > NR_ICMP_TYPES) {
  1110. reason = SKB_DROP_REASON_UNHANDLED_PROTO;
  1111. goto error;
  1112. }
  1113. /*
  1114. * Parse the ICMP message
  1115. */
  1116. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  1117. /*
  1118. * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
  1119. * silently ignored (we let user decide with a sysctl).
  1120. * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
  1121. * discarded if to broadcast/multicast.
  1122. */
  1123. if ((icmph->type == ICMP_ECHO ||
  1124. icmph->type == ICMP_TIMESTAMP) &&
  1125. READ_ONCE(net->ipv4.sysctl_icmp_echo_ignore_broadcasts)) {
  1126. reason = SKB_DROP_REASON_INVALID_PROTO;
  1127. goto error;
  1128. }
  1129. if (icmph->type != ICMP_ECHO &&
  1130. icmph->type != ICMP_TIMESTAMP &&
  1131. icmph->type != ICMP_ADDRESS &&
  1132. icmph->type != ICMP_ADDRESSREPLY) {
  1133. reason = SKB_DROP_REASON_INVALID_PROTO;
  1134. goto error;
  1135. }
  1136. }
  1137. reason = icmp_pointers[icmph->type].handler(skb);
  1138. reason_check:
  1139. if (!reason) {
  1140. consume_skb(skb);
  1141. return NET_RX_SUCCESS;
  1142. }
  1143. drop:
  1144. kfree_skb_reason(skb, reason);
  1145. return NET_RX_DROP;
  1146. csum_error:
  1147. reason = SKB_DROP_REASON_ICMP_CSUM;
  1148. __ICMP_INC_STATS(net, ICMP_MIB_CSUMERRORS);
  1149. error:
  1150. __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
  1151. goto drop;
  1152. }
  1153. static bool ip_icmp_error_rfc4884_validate(const struct sk_buff *skb, int off)
  1154. {
  1155. struct icmp_extobj_hdr *objh, _objh;
  1156. struct icmp_ext_hdr *exth, _exth;
  1157. u16 olen;
  1158. exth = skb_header_pointer(skb, off, sizeof(_exth), &_exth);
  1159. if (!exth)
  1160. return false;
  1161. if (exth->version != 2)
  1162. return true;
  1163. if (exth->checksum &&
  1164. csum_fold(skb_checksum(skb, off, skb->len - off, 0)))
  1165. return false;
  1166. off += sizeof(_exth);
  1167. while (off < skb->len) {
  1168. objh = skb_header_pointer(skb, off, sizeof(_objh), &_objh);
  1169. if (!objh)
  1170. return false;
  1171. olen = ntohs(objh->length);
  1172. if (olen < sizeof(_objh))
  1173. return false;
  1174. off += olen;
  1175. if (off > skb->len)
  1176. return false;
  1177. }
  1178. return true;
  1179. }
  1180. void ip_icmp_error_rfc4884(const struct sk_buff *skb,
  1181. struct sock_ee_data_rfc4884 *out,
  1182. int thlen, int off)
  1183. {
  1184. int hlen;
  1185. /* original datagram headers: end of icmph to payload (skb->data) */
  1186. hlen = -skb_transport_offset(skb) - thlen;
  1187. /* per rfc 4884: minimal datagram length of 128 bytes */
  1188. if (off < 128 || off < hlen)
  1189. return;
  1190. /* kernel has stripped headers: return payload offset in bytes */
  1191. off -= hlen;
  1192. if (off + sizeof(struct icmp_ext_hdr) > skb->len)
  1193. return;
  1194. out->len = off;
  1195. if (!ip_icmp_error_rfc4884_validate(skb, off))
  1196. out->flags |= SO_EE_RFC4884_FLAG_INVALID;
  1197. }
  1198. EXPORT_SYMBOL_GPL(ip_icmp_error_rfc4884);
  1199. int icmp_err(struct sk_buff *skb, u32 info)
  1200. {
  1201. struct iphdr *iph = (struct iphdr *)skb->data;
  1202. int offset = iph->ihl<<2;
  1203. struct icmphdr *icmph = (struct icmphdr *)(skb->data + offset);
  1204. struct net *net = dev_net_rcu(skb->dev);
  1205. int type = icmp_hdr(skb)->type;
  1206. int code = icmp_hdr(skb)->code;
  1207. /*
  1208. * Use ping_err to handle all icmp errors except those
  1209. * triggered by ICMP_ECHOREPLY which sent from kernel.
  1210. */
  1211. if (icmph->type != ICMP_ECHOREPLY) {
  1212. ping_err(skb, offset, info);
  1213. return 0;
  1214. }
  1215. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
  1216. ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ICMP);
  1217. else if (type == ICMP_REDIRECT)
  1218. ipv4_redirect(skb, net, 0, IPPROTO_ICMP);
  1219. return 0;
  1220. }
  1221. /*
  1222. * This table is the definition of how we handle ICMP.
  1223. */
  1224. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
  1225. [ICMP_ECHOREPLY] = {
  1226. .handler = ping_rcv,
  1227. },
  1228. [1] = {
  1229. .handler = icmp_discard,
  1230. .error = 1,
  1231. },
  1232. [2] = {
  1233. .handler = icmp_discard,
  1234. .error = 1,
  1235. },
  1236. [ICMP_DEST_UNREACH] = {
  1237. .handler = icmp_unreach,
  1238. .error = 1,
  1239. },
  1240. [ICMP_SOURCE_QUENCH] = {
  1241. .handler = icmp_unreach,
  1242. .error = 1,
  1243. },
  1244. [ICMP_REDIRECT] = {
  1245. .handler = icmp_redirect,
  1246. .error = 1,
  1247. },
  1248. [6] = {
  1249. .handler = icmp_discard,
  1250. .error = 1,
  1251. },
  1252. [7] = {
  1253. .handler = icmp_discard,
  1254. .error = 1,
  1255. },
  1256. [ICMP_ECHO] = {
  1257. .handler = icmp_echo,
  1258. },
  1259. [9] = {
  1260. .handler = icmp_discard,
  1261. .error = 1,
  1262. },
  1263. [10] = {
  1264. .handler = icmp_discard,
  1265. .error = 1,
  1266. },
  1267. [ICMP_TIME_EXCEEDED] = {
  1268. .handler = icmp_unreach,
  1269. .error = 1,
  1270. },
  1271. [ICMP_PARAMETERPROB] = {
  1272. .handler = icmp_unreach,
  1273. .error = 1,
  1274. },
  1275. [ICMP_TIMESTAMP] = {
  1276. .handler = icmp_timestamp,
  1277. },
  1278. [ICMP_TIMESTAMPREPLY] = {
  1279. .handler = icmp_discard,
  1280. },
  1281. [ICMP_INFO_REQUEST] = {
  1282. .handler = icmp_discard,
  1283. },
  1284. [ICMP_INFO_REPLY] = {
  1285. .handler = icmp_discard,
  1286. },
  1287. [ICMP_ADDRESS] = {
  1288. .handler = icmp_discard,
  1289. },
  1290. [ICMP_ADDRESSREPLY] = {
  1291. .handler = icmp_discard,
  1292. },
  1293. };
  1294. static int __net_init icmp_sk_init(struct net *net)
  1295. {
  1296. /* Control parameters for ECHO replies. */
  1297. net->ipv4.sysctl_icmp_echo_ignore_all = 0;
  1298. net->ipv4.sysctl_icmp_echo_enable_probe = 0;
  1299. net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
  1300. /* Control parameter - ignore bogus broadcast responses? */
  1301. net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
  1302. /*
  1303. * Configurable global rate limit.
  1304. *
  1305. * ratelimit defines tokens/packet consumed for dst->rate_token
  1306. * bucket ratemask defines which icmp types are ratelimited by
  1307. * setting it's bit position.
  1308. *
  1309. * default:
  1310. * dest unreachable (3), source quench (4),
  1311. * time exceeded (11), parameter problem (12)
  1312. */
  1313. net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
  1314. net->ipv4.sysctl_icmp_ratemask = 0x1818;
  1315. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
  1316. net->ipv4.sysctl_icmp_msgs_per_sec = 1000;
  1317. net->ipv4.sysctl_icmp_msgs_burst = 50;
  1318. return 0;
  1319. }
  1320. static struct pernet_operations __net_initdata icmp_sk_ops = {
  1321. .init = icmp_sk_init,
  1322. };
  1323. int __init icmp_init(void)
  1324. {
  1325. int err, i;
  1326. for_each_possible_cpu(i) {
  1327. struct sock *sk;
  1328. err = inet_ctl_sock_create(&sk, PF_INET,
  1329. SOCK_RAW, IPPROTO_ICMP, &init_net);
  1330. if (err < 0)
  1331. return err;
  1332. per_cpu(ipv4_icmp_sk, i) = sk;
  1333. /* Enough space for 2 64K ICMP packets, including
  1334. * sk_buff/skb_shared_info struct overhead.
  1335. */
  1336. sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
  1337. /*
  1338. * Speedup sock_wfree()
  1339. */
  1340. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  1341. inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
  1342. }
  1343. return register_pernet_subsys(&icmp_sk_ops);
  1344. }