icmp.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /*
  2. * NET3: Implementation of the ICMP protocol layer.
  3. *
  4. * Alan Cox, <alan@lxorguk.ukuu.org.uk>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Some of the function names and the icmp unreach table for this
  12. * module were derived from [icmp.c 1.0.11 06/02/93] by
  13. * Ross Biro, Fred N. van Kempen, Mark Evans, Alan Cox, Gerhard Koerting.
  14. * Other than that this module is a complete rewrite.
  15. *
  16. * Fixes:
  17. * Clemens Fruhwirth : introduce global icmp rate limiting
  18. * with icmp type masking ability instead
  19. * of broken per type icmp timeouts.
  20. * Mike Shaver : RFC1122 checks.
  21. * Alan Cox : Multicast ping reply as self.
  22. * Alan Cox : Fix atomicity lockup in ip_build_xmit
  23. * call.
  24. * Alan Cox : Added 216,128 byte paths to the MTU
  25. * code.
  26. * Martin Mares : RFC1812 checks.
  27. * Martin Mares : Can be configured to follow redirects
  28. * if acting as a router _without_ a
  29. * routing protocol (RFC 1812).
  30. * Martin Mares : Echo requests may be configured to
  31. * be ignored (RFC 1812).
  32. * Martin Mares : Limitation of ICMP error message
  33. * transmit rate (RFC 1812).
  34. * Martin Mares : TOS and Precedence set correctly
  35. * (RFC 1812).
  36. * Martin Mares : Now copying as much data from the
  37. * original packet as we can without
  38. * exceeding 576 bytes (RFC 1812).
  39. * Willy Konynenberg : Transparent proxying support.
  40. * Keith Owens : RFC1191 correction for 4.2BSD based
  41. * path MTU bug.
  42. * Thomas Quinot : ICMP Dest Unreach codes up to 15 are
  43. * valid (RFC 1812).
  44. * Andi Kleen : Check all packet lengths properly
  45. * and moved all kfree_skb() up to
  46. * icmp_rcv.
  47. * Andi Kleen : Move the rate limit bookkeeping
  48. * into the dest entry and use a token
  49. * bucket filter (thanks to ANK). Make
  50. * the rates sysctl configurable.
  51. * Yu Tianli : Fixed two ugly bugs in icmp_send
  52. * - IP option length was accounted wrongly
  53. * - ICMP header length was not accounted
  54. * at all.
  55. * Tristan Greaves : Added sysctl option to ignore bogus
  56. * broadcast responses from broken routers.
  57. *
  58. * To Fix:
  59. *
  60. * - Should use skb_pull() instead of all the manual checking.
  61. * This would also greatly simply some upper layer error handlers. --AK
  62. *
  63. */
  64. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  65. #include <linux/module.h>
  66. #include <linux/types.h>
  67. #include <linux/jiffies.h>
  68. #include <linux/kernel.h>
  69. #include <linux/fcntl.h>
  70. #include <linux/socket.h>
  71. #include <linux/in.h>
  72. #include <linux/inet.h>
  73. #include <linux/inetdevice.h>
  74. #include <linux/netdevice.h>
  75. #include <linux/string.h>
  76. #include <linux/netfilter_ipv4.h>
  77. #include <linux/slab.h>
  78. #include <net/snmp.h>
  79. #include <net/ip.h>
  80. #include <net/route.h>
  81. #include <net/protocol.h>
  82. #include <net/icmp.h>
  83. #include <net/tcp.h>
  84. #include <net/udp.h>
  85. #include <net/raw.h>
  86. #include <net/ping.h>
  87. #include <linux/skbuff.h>
  88. #include <net/sock.h>
  89. #include <linux/errno.h>
  90. #include <linux/timer.h>
  91. #include <linux/init.h>
  92. #include <asm/uaccess.h>
  93. #include <net/checksum.h>
  94. #include <net/xfrm.h>
  95. #include <net/inet_common.h>
  96. #include <net/ip_fib.h>
  97. #include <net/l3mdev.h>
  98. /*
  99. * Build xmit assembly blocks
  100. */
  101. struct icmp_bxm {
  102. struct sk_buff *skb;
  103. int offset;
  104. int data_len;
  105. struct {
  106. struct icmphdr icmph;
  107. __be32 times[3];
  108. } data;
  109. int head_len;
  110. struct ip_options_data replyopts;
  111. };
  112. /* An array of errno for error messages from dest unreach. */
  113. /* RFC 1122: 3.2.2.1 States that NET_UNREACH, HOST_UNREACH and SR_FAILED MUST be considered 'transient errs'. */
  114. const struct icmp_err icmp_err_convert[] = {
  115. {
  116. .errno = ENETUNREACH, /* ICMP_NET_UNREACH */
  117. .fatal = 0,
  118. },
  119. {
  120. .errno = EHOSTUNREACH, /* ICMP_HOST_UNREACH */
  121. .fatal = 0,
  122. },
  123. {
  124. .errno = ENOPROTOOPT /* ICMP_PROT_UNREACH */,
  125. .fatal = 1,
  126. },
  127. {
  128. .errno = ECONNREFUSED, /* ICMP_PORT_UNREACH */
  129. .fatal = 1,
  130. },
  131. {
  132. .errno = EMSGSIZE, /* ICMP_FRAG_NEEDED */
  133. .fatal = 0,
  134. },
  135. {
  136. .errno = EOPNOTSUPP, /* ICMP_SR_FAILED */
  137. .fatal = 0,
  138. },
  139. {
  140. .errno = ENETUNREACH, /* ICMP_NET_UNKNOWN */
  141. .fatal = 1,
  142. },
  143. {
  144. .errno = EHOSTDOWN, /* ICMP_HOST_UNKNOWN */
  145. .fatal = 1,
  146. },
  147. {
  148. .errno = ENONET, /* ICMP_HOST_ISOLATED */
  149. .fatal = 1,
  150. },
  151. {
  152. .errno = ENETUNREACH, /* ICMP_NET_ANO */
  153. .fatal = 1,
  154. },
  155. {
  156. .errno = EHOSTUNREACH, /* ICMP_HOST_ANO */
  157. .fatal = 1,
  158. },
  159. {
  160. .errno = ENETUNREACH, /* ICMP_NET_UNR_TOS */
  161. .fatal = 0,
  162. },
  163. {
  164. .errno = EHOSTUNREACH, /* ICMP_HOST_UNR_TOS */
  165. .fatal = 0,
  166. },
  167. {
  168. .errno = EHOSTUNREACH, /* ICMP_PKT_FILTERED */
  169. .fatal = 1,
  170. },
  171. {
  172. .errno = EHOSTUNREACH, /* ICMP_PREC_VIOLATION */
  173. .fatal = 1,
  174. },
  175. {
  176. .errno = EHOSTUNREACH, /* ICMP_PREC_CUTOFF */
  177. .fatal = 1,
  178. },
  179. };
  180. EXPORT_SYMBOL(icmp_err_convert);
  181. /*
  182. * ICMP control array. This specifies what to do with each ICMP.
  183. */
  184. struct icmp_control {
  185. bool (*handler)(struct sk_buff *skb);
  186. short error; /* This ICMP is classed as an error message */
  187. };
  188. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
  189. /*
  190. * The ICMP socket(s). This is the most convenient way to flow control
  191. * our ICMP output as well as maintain a clean interface throughout
  192. * all layers. All Socketless IP sends will soon be gone.
  193. *
  194. * On SMP we have one ICMP socket per-cpu.
  195. */
  196. static struct sock *icmp_sk(struct net *net)
  197. {
  198. return *this_cpu_ptr(net->ipv4.icmp_sk);
  199. }
  200. static inline struct sock *icmp_xmit_lock(struct net *net)
  201. {
  202. struct sock *sk;
  203. local_bh_disable();
  204. sk = icmp_sk(net);
  205. if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
  206. /* This can happen if the output path signals a
  207. * dst_link_failure() for an outgoing ICMP packet.
  208. */
  209. local_bh_enable();
  210. return NULL;
  211. }
  212. return sk;
  213. }
  214. static inline void icmp_xmit_unlock(struct sock *sk)
  215. {
  216. spin_unlock_bh(&sk->sk_lock.slock);
  217. }
  218. int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
  219. int sysctl_icmp_msgs_burst __read_mostly = 50;
  220. static struct {
  221. spinlock_t lock;
  222. u32 credit;
  223. u32 stamp;
  224. } icmp_global = {
  225. .lock = __SPIN_LOCK_UNLOCKED(icmp_global.lock),
  226. };
  227. /**
  228. * icmp_global_allow - Are we allowed to send one more ICMP message ?
  229. *
  230. * Uses a token bucket to limit our ICMP messages to sysctl_icmp_msgs_per_sec.
  231. * Returns false if we reached the limit and can not send another packet.
  232. * Note: called with BH disabled
  233. */
  234. bool icmp_global_allow(void)
  235. {
  236. u32 credit, delta, incr = 0, now = (u32)jiffies;
  237. bool rc = false;
  238. /* Check if token bucket is empty and cannot be refilled
  239. * without taking the spinlock.
  240. */
  241. if (!icmp_global.credit) {
  242. delta = min_t(u32, now - icmp_global.stamp, HZ);
  243. if (delta < HZ / 50)
  244. return false;
  245. }
  246. spin_lock(&icmp_global.lock);
  247. delta = min_t(u32, now - icmp_global.stamp, HZ);
  248. if (delta >= HZ / 50) {
  249. incr = sysctl_icmp_msgs_per_sec * delta / HZ ;
  250. if (incr)
  251. icmp_global.stamp = now;
  252. }
  253. credit = min_t(u32, icmp_global.credit + incr, sysctl_icmp_msgs_burst);
  254. if (credit) {
  255. credit--;
  256. rc = true;
  257. }
  258. icmp_global.credit = credit;
  259. spin_unlock(&icmp_global.lock);
  260. return rc;
  261. }
  262. EXPORT_SYMBOL(icmp_global_allow);
  263. /*
  264. * Send an ICMP frame.
  265. */
  266. static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
  267. struct flowi4 *fl4, int type, int code)
  268. {
  269. struct dst_entry *dst = &rt->dst;
  270. bool rc = true;
  271. if (type > NR_ICMP_TYPES)
  272. goto out;
  273. /* Don't limit PMTU discovery. */
  274. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
  275. goto out;
  276. /* No rate limit on loopback */
  277. if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
  278. goto out;
  279. /* Limit if icmp type is enabled in ratemask. */
  280. if (!((1 << type) & net->ipv4.sysctl_icmp_ratemask))
  281. goto out;
  282. rc = false;
  283. if (icmp_global_allow()) {
  284. int vif = l3mdev_master_ifindex(dst->dev);
  285. struct inet_peer *peer;
  286. peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr, vif, 1);
  287. rc = inet_peer_xrlim_allow(peer,
  288. net->ipv4.sysctl_icmp_ratelimit);
  289. if (peer)
  290. inet_putpeer(peer);
  291. }
  292. out:
  293. return rc;
  294. }
  295. /*
  296. * Maintain the counters used in the SNMP statistics for outgoing ICMP
  297. */
  298. void icmp_out_count(struct net *net, unsigned char type)
  299. {
  300. ICMPMSGOUT_INC_STATS(net, type);
  301. ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS);
  302. }
  303. /*
  304. * Checksum each fragment, and on the first include the headers and final
  305. * checksum.
  306. */
  307. static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
  308. struct sk_buff *skb)
  309. {
  310. struct icmp_bxm *icmp_param = (struct icmp_bxm *)from;
  311. __wsum csum;
  312. csum = skb_copy_and_csum_bits(icmp_param->skb,
  313. icmp_param->offset + offset,
  314. to, len, 0);
  315. skb->csum = csum_block_add(skb->csum, csum, odd);
  316. if (icmp_pointers[icmp_param->data.icmph.type].error)
  317. nf_ct_attach(skb, icmp_param->skb);
  318. return 0;
  319. }
  320. static void icmp_push_reply(struct icmp_bxm *icmp_param,
  321. struct flowi4 *fl4,
  322. struct ipcm_cookie *ipc, struct rtable **rt)
  323. {
  324. struct sock *sk;
  325. struct sk_buff *skb;
  326. sk = icmp_sk(dev_net((*rt)->dst.dev));
  327. if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
  328. icmp_param->data_len+icmp_param->head_len,
  329. icmp_param->head_len,
  330. ipc, rt, MSG_DONTWAIT) < 0) {
  331. ICMP_INC_STATS_BH(sock_net(sk), ICMP_MIB_OUTERRORS);
  332. ip_flush_pending_frames(sk);
  333. } else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
  334. struct icmphdr *icmph = icmp_hdr(skb);
  335. __wsum csum = 0;
  336. struct sk_buff *skb1;
  337. skb_queue_walk(&sk->sk_write_queue, skb1) {
  338. csum = csum_add(csum, skb1->csum);
  339. }
  340. csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
  341. (char *)icmph,
  342. icmp_param->head_len, csum);
  343. icmph->checksum = csum_fold(csum);
  344. skb->ip_summed = CHECKSUM_NONE;
  345. ip_push_pending_frames(sk, fl4);
  346. }
  347. }
  348. /*
  349. * Driving logic for building and sending ICMP messages.
  350. */
  351. static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
  352. {
  353. struct ipcm_cookie ipc;
  354. struct rtable *rt = skb_rtable(skb);
  355. struct net *net = dev_net(rt->dst.dev);
  356. struct flowi4 fl4;
  357. struct sock *sk;
  358. struct inet_sock *inet;
  359. __be32 daddr, saddr;
  360. u32 mark = IP4_REPLY_MARK(net, skb->mark);
  361. if (ip_options_echo(&icmp_param->replyopts.opt.opt, skb))
  362. return;
  363. sk = icmp_xmit_lock(net);
  364. if (!sk)
  365. return;
  366. inet = inet_sk(sk);
  367. icmp_param->data.icmph.checksum = 0;
  368. inet->tos = ip_hdr(skb)->tos;
  369. sk->sk_mark = mark;
  370. daddr = ipc.addr = ip_hdr(skb)->saddr;
  371. saddr = fib_compute_spec_dst(skb);
  372. ipc.opt = NULL;
  373. ipc.tx_flags = 0;
  374. ipc.ttl = 0;
  375. ipc.tos = -1;
  376. if (icmp_param->replyopts.opt.opt.optlen) {
  377. ipc.opt = &icmp_param->replyopts.opt;
  378. if (ipc.opt->opt.srr)
  379. daddr = icmp_param->replyopts.opt.opt.faddr;
  380. }
  381. memset(&fl4, 0, sizeof(fl4));
  382. fl4.daddr = daddr;
  383. fl4.saddr = saddr;
  384. fl4.flowi4_mark = mark;
  385. fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
  386. fl4.flowi4_proto = IPPROTO_ICMP;
  387. fl4.flowi4_oif = l3mdev_master_ifindex(skb->dev);
  388. security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
  389. rt = ip_route_output_key(net, &fl4);
  390. if (IS_ERR(rt))
  391. goto out_unlock;
  392. if (icmpv4_xrlim_allow(net, rt, &fl4, icmp_param->data.icmph.type,
  393. icmp_param->data.icmph.code))
  394. icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
  395. ip_rt_put(rt);
  396. out_unlock:
  397. icmp_xmit_unlock(sk);
  398. }
  399. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  400. /* Source and destination is swapped. See ip_multipath_icmp_hash */
  401. static int icmp_multipath_hash_skb(const struct sk_buff *skb)
  402. {
  403. const struct iphdr *iph = ip_hdr(skb);
  404. return fib_multipath_hash(iph->daddr, iph->saddr);
  405. }
  406. #else
  407. #define icmp_multipath_hash_skb(skb) (-1)
  408. #endif
  409. static struct rtable *icmp_route_lookup(struct net *net,
  410. struct flowi4 *fl4,
  411. struct sk_buff *skb_in,
  412. const struct iphdr *iph,
  413. __be32 saddr, u8 tos, u32 mark,
  414. int type, int code,
  415. struct icmp_bxm *param)
  416. {
  417. struct rtable *rt, *rt2;
  418. struct flowi4 fl4_dec;
  419. int err;
  420. memset(fl4, 0, sizeof(*fl4));
  421. fl4->daddr = (param->replyopts.opt.opt.srr ?
  422. param->replyopts.opt.opt.faddr : iph->saddr);
  423. fl4->saddr = saddr;
  424. fl4->flowi4_mark = mark;
  425. fl4->flowi4_tos = RT_TOS(tos);
  426. fl4->flowi4_proto = IPPROTO_ICMP;
  427. fl4->fl4_icmp_type = type;
  428. fl4->fl4_icmp_code = code;
  429. fl4->flowi4_oif = l3mdev_master_ifindex(skb_in->dev);
  430. security_skb_classify_flow(skb_in, flowi4_to_flowi(fl4));
  431. rt = __ip_route_output_key_hash(net, fl4,
  432. icmp_multipath_hash_skb(skb_in));
  433. if (IS_ERR(rt))
  434. return rt;
  435. /* No need to clone since we're just using its address. */
  436. rt2 = rt;
  437. rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
  438. flowi4_to_flowi(fl4), NULL, 0);
  439. if (!IS_ERR(rt)) {
  440. if (rt != rt2)
  441. return rt;
  442. } else if (PTR_ERR(rt) == -EPERM) {
  443. rt = NULL;
  444. } else
  445. return rt;
  446. err = xfrm_decode_session_reverse(skb_in, flowi4_to_flowi(&fl4_dec), AF_INET);
  447. if (err)
  448. goto relookup_failed;
  449. if (inet_addr_type_dev_table(net, skb_in->dev,
  450. fl4_dec.saddr) == RTN_LOCAL) {
  451. rt2 = __ip_route_output_key(net, &fl4_dec);
  452. if (IS_ERR(rt2))
  453. err = PTR_ERR(rt2);
  454. } else {
  455. struct flowi4 fl4_2 = {};
  456. unsigned long orefdst;
  457. fl4_2.daddr = fl4_dec.saddr;
  458. rt2 = ip_route_output_key(net, &fl4_2);
  459. if (IS_ERR(rt2)) {
  460. err = PTR_ERR(rt2);
  461. goto relookup_failed;
  462. }
  463. /* Ugh! */
  464. orefdst = skb_in->_skb_refdst; /* save old refdst */
  465. skb_dst_set(skb_in, NULL);
  466. err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr,
  467. RT_TOS(tos), rt2->dst.dev);
  468. dst_release(&rt2->dst);
  469. rt2 = skb_rtable(skb_in);
  470. skb_in->_skb_refdst = orefdst; /* restore old refdst */
  471. }
  472. if (err)
  473. goto relookup_failed;
  474. rt2 = (struct rtable *) xfrm_lookup(net, &rt2->dst,
  475. flowi4_to_flowi(&fl4_dec), NULL,
  476. XFRM_LOOKUP_ICMP);
  477. if (!IS_ERR(rt2)) {
  478. dst_release(&rt->dst);
  479. memcpy(fl4, &fl4_dec, sizeof(*fl4));
  480. rt = rt2;
  481. } else if (PTR_ERR(rt2) == -EPERM) {
  482. if (rt)
  483. dst_release(&rt->dst);
  484. return rt2;
  485. } else {
  486. err = PTR_ERR(rt2);
  487. goto relookup_failed;
  488. }
  489. return rt;
  490. relookup_failed:
  491. if (rt)
  492. return rt;
  493. return ERR_PTR(err);
  494. }
  495. /*
  496. * Send an ICMP message in response to a situation
  497. *
  498. * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
  499. * MAY send more (we do).
  500. * MUST NOT change this header information.
  501. * MUST NOT reply to a multicast/broadcast IP address.
  502. * MUST NOT reply to a multicast/broadcast MAC address.
  503. * MUST reply to only the first fragment.
  504. */
  505. void __icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info,
  506. const struct ip_options *opt)
  507. {
  508. struct iphdr *iph;
  509. int room;
  510. struct icmp_bxm *icmp_param;
  511. struct rtable *rt = skb_rtable(skb_in);
  512. struct ipcm_cookie ipc;
  513. struct flowi4 fl4;
  514. __be32 saddr;
  515. u8 tos;
  516. u32 mark;
  517. struct net *net;
  518. struct sock *sk;
  519. if (!rt)
  520. goto out;
  521. net = dev_net(rt->dst.dev);
  522. /*
  523. * Find the original header. It is expected to be valid, of course.
  524. * Check this, icmp_send is called from the most obscure devices
  525. * sometimes.
  526. */
  527. iph = ip_hdr(skb_in);
  528. if ((u8 *)iph < skb_in->head ||
  529. (skb_network_header(skb_in) + sizeof(*iph)) >
  530. skb_tail_pointer(skb_in))
  531. goto out;
  532. /*
  533. * No replies to physical multicast/broadcast
  534. */
  535. if (skb_in->pkt_type != PACKET_HOST)
  536. goto out;
  537. /*
  538. * Now check at the protocol level
  539. */
  540. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  541. goto out;
  542. /*
  543. * Only reply to fragment 0. We byte re-order the constant
  544. * mask for efficiency.
  545. */
  546. if (iph->frag_off & htons(IP_OFFSET))
  547. goto out;
  548. /*
  549. * If we send an ICMP error to an ICMP error a mess would result..
  550. */
  551. if (icmp_pointers[type].error) {
  552. /*
  553. * We are an error, check if we are replying to an
  554. * ICMP error
  555. */
  556. if (iph->protocol == IPPROTO_ICMP) {
  557. u8 _inner_type, *itp;
  558. itp = skb_header_pointer(skb_in,
  559. skb_network_header(skb_in) +
  560. (iph->ihl << 2) +
  561. offsetof(struct icmphdr,
  562. type) -
  563. skb_in->data,
  564. sizeof(_inner_type),
  565. &_inner_type);
  566. if (!itp)
  567. goto out;
  568. /*
  569. * Assume any unknown ICMP type is an error. This
  570. * isn't specified by the RFC, but think about it..
  571. */
  572. if (*itp > NR_ICMP_TYPES ||
  573. icmp_pointers[*itp].error)
  574. goto out;
  575. }
  576. }
  577. icmp_param = kmalloc(sizeof(*icmp_param), GFP_ATOMIC);
  578. if (!icmp_param)
  579. return;
  580. sk = icmp_xmit_lock(net);
  581. if (!sk)
  582. goto out_free;
  583. /*
  584. * Construct source address and options.
  585. */
  586. saddr = iph->daddr;
  587. if (!(rt->rt_flags & RTCF_LOCAL)) {
  588. struct net_device *dev = NULL;
  589. rcu_read_lock();
  590. if (rt_is_input_route(rt) &&
  591. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)
  592. dev = dev_get_by_index_rcu(net, inet_iif(skb_in));
  593. if (dev)
  594. saddr = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  595. else
  596. saddr = 0;
  597. rcu_read_unlock();
  598. }
  599. tos = icmp_pointers[type].error ? ((iph->tos & IPTOS_TOS_MASK) |
  600. IPTOS_PREC_INTERNETCONTROL) :
  601. iph->tos;
  602. mark = IP4_REPLY_MARK(net, skb_in->mark);
  603. if (__ip_options_echo(&icmp_param->replyopts.opt.opt, skb_in, opt))
  604. goto out_unlock;
  605. /*
  606. * Prepare data for ICMP header.
  607. */
  608. icmp_param->data.icmph.type = type;
  609. icmp_param->data.icmph.code = code;
  610. icmp_param->data.icmph.un.gateway = info;
  611. icmp_param->data.icmph.checksum = 0;
  612. icmp_param->skb = skb_in;
  613. icmp_param->offset = skb_network_offset(skb_in);
  614. inet_sk(sk)->tos = tos;
  615. sk->sk_mark = mark;
  616. ipc.addr = iph->saddr;
  617. ipc.opt = &icmp_param->replyopts.opt;
  618. ipc.tx_flags = 0;
  619. ipc.ttl = 0;
  620. ipc.tos = -1;
  621. rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos, mark,
  622. type, code, icmp_param);
  623. if (IS_ERR(rt))
  624. goto out_unlock;
  625. if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code))
  626. goto ende;
  627. /* RFC says return as much as we can without exceeding 576 bytes. */
  628. room = dst_mtu(&rt->dst);
  629. if (room > 576)
  630. room = 576;
  631. room -= sizeof(struct iphdr) + icmp_param->replyopts.opt.opt.optlen;
  632. room -= sizeof(struct icmphdr);
  633. icmp_param->data_len = skb_in->len - icmp_param->offset;
  634. if (icmp_param->data_len > room)
  635. icmp_param->data_len = room;
  636. icmp_param->head_len = sizeof(struct icmphdr);
  637. icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
  638. ende:
  639. ip_rt_put(rt);
  640. out_unlock:
  641. icmp_xmit_unlock(sk);
  642. out_free:
  643. kfree(icmp_param);
  644. out:;
  645. }
  646. EXPORT_SYMBOL(__icmp_send);
  647. static void icmp_socket_deliver(struct sk_buff *skb, u32 info)
  648. {
  649. const struct iphdr *iph = (const struct iphdr *) skb->data;
  650. const struct net_protocol *ipprot;
  651. int protocol = iph->protocol;
  652. /* Checkin full IP header plus 8 bytes of protocol to
  653. * avoid additional coding at protocol handlers.
  654. */
  655. if (!pskb_may_pull(skb, iph->ihl * 4 + 8)) {
  656. ICMP_INC_STATS_BH(dev_net(skb->dev), ICMP_MIB_INERRORS);
  657. return;
  658. }
  659. raw_icmp_error(skb, protocol, info);
  660. ipprot = rcu_dereference(inet_protos[protocol]);
  661. if (ipprot && ipprot->err_handler)
  662. ipprot->err_handler(skb, info);
  663. }
  664. static bool icmp_tag_validation(int proto)
  665. {
  666. bool ok;
  667. rcu_read_lock();
  668. ok = rcu_dereference(inet_protos[proto])->icmp_strict_tag_validation;
  669. rcu_read_unlock();
  670. return ok;
  671. }
  672. /*
  673. * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEED, ICMP_QUENCH, and
  674. * ICMP_PARAMETERPROB.
  675. */
  676. static bool icmp_unreach(struct sk_buff *skb)
  677. {
  678. const struct iphdr *iph;
  679. struct icmphdr *icmph;
  680. struct net *net;
  681. u32 info = 0;
  682. net = dev_net(skb_dst(skb)->dev);
  683. /*
  684. * Incomplete header ?
  685. * Only checks for the IP header, there should be an
  686. * additional check for longer headers in upper levels.
  687. */
  688. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  689. goto out_err;
  690. icmph = icmp_hdr(skb);
  691. iph = (const struct iphdr *)skb->data;
  692. if (iph->ihl < 5) /* Mangled header, drop. */
  693. goto out_err;
  694. if (icmph->type == ICMP_DEST_UNREACH) {
  695. switch (icmph->code & 15) {
  696. case ICMP_NET_UNREACH:
  697. case ICMP_HOST_UNREACH:
  698. case ICMP_PROT_UNREACH:
  699. case ICMP_PORT_UNREACH:
  700. break;
  701. case ICMP_FRAG_NEEDED:
  702. /* for documentation of the ip_no_pmtu_disc
  703. * values please see
  704. * Documentation/networking/ip-sysctl.txt
  705. */
  706. switch (net->ipv4.sysctl_ip_no_pmtu_disc) {
  707. default:
  708. net_dbg_ratelimited("%pI4: fragmentation needed and DF set\n",
  709. &iph->daddr);
  710. break;
  711. case 2:
  712. goto out;
  713. case 3:
  714. if (!icmp_tag_validation(iph->protocol))
  715. goto out;
  716. /* fall through */
  717. case 0:
  718. info = ntohs(icmph->un.frag.mtu);
  719. }
  720. break;
  721. case ICMP_SR_FAILED:
  722. net_dbg_ratelimited("%pI4: Source Route Failed\n",
  723. &iph->daddr);
  724. break;
  725. default:
  726. break;
  727. }
  728. if (icmph->code > NR_ICMP_UNREACH)
  729. goto out;
  730. } else if (icmph->type == ICMP_PARAMETERPROB)
  731. info = ntohl(icmph->un.gateway) >> 24;
  732. /*
  733. * Throw it at our lower layers
  734. *
  735. * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
  736. * header.
  737. * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
  738. * transport layer.
  739. * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
  740. * transport layer.
  741. */
  742. /*
  743. * Check the other end isn't violating RFC 1122. Some routers send
  744. * bogus responses to broadcast frames. If you see this message
  745. * first check your netmask matches at both ends, if it does then
  746. * get the other vendor to fix their kit.
  747. */
  748. if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
  749. inet_addr_type_dev_table(net, skb->dev, iph->daddr) == RTN_BROADCAST) {
  750. net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
  751. &ip_hdr(skb)->saddr,
  752. icmph->type, icmph->code,
  753. &iph->daddr, skb->dev->name);
  754. goto out;
  755. }
  756. icmp_socket_deliver(skb, info);
  757. out:
  758. return true;
  759. out_err:
  760. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  761. return false;
  762. }
  763. /*
  764. * Handle ICMP_REDIRECT.
  765. */
  766. static bool icmp_redirect(struct sk_buff *skb)
  767. {
  768. if (skb->len < sizeof(struct iphdr)) {
  769. ICMP_INC_STATS_BH(dev_net(skb->dev), ICMP_MIB_INERRORS);
  770. return false;
  771. }
  772. if (!pskb_may_pull(skb, sizeof(struct iphdr))) {
  773. /* there aught to be a stat */
  774. return false;
  775. }
  776. icmp_socket_deliver(skb, icmp_hdr(skb)->un.gateway);
  777. return true;
  778. }
  779. /*
  780. * Handle ICMP_ECHO ("ping") requests.
  781. *
  782. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  783. * requests.
  784. * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
  785. * included in the reply.
  786. * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
  787. * echo requests, MUST have default=NOT.
  788. * See also WRT handling of options once they are done and working.
  789. */
  790. static bool icmp_echo(struct sk_buff *skb)
  791. {
  792. struct net *net;
  793. net = dev_net(skb_dst(skb)->dev);
  794. if (!net->ipv4.sysctl_icmp_echo_ignore_all) {
  795. struct icmp_bxm icmp_param;
  796. icmp_param.data.icmph = *icmp_hdr(skb);
  797. icmp_param.data.icmph.type = ICMP_ECHOREPLY;
  798. icmp_param.skb = skb;
  799. icmp_param.offset = 0;
  800. icmp_param.data_len = skb->len;
  801. icmp_param.head_len = sizeof(struct icmphdr);
  802. icmp_reply(&icmp_param, skb);
  803. }
  804. /* should there be an ICMP stat for ignored echos? */
  805. return true;
  806. }
  807. /*
  808. * Handle ICMP Timestamp requests.
  809. * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
  810. * SHOULD be in the kernel for minimum random latency.
  811. * MUST be accurate to a few minutes.
  812. * MUST be updated at least at 15Hz.
  813. */
  814. static bool icmp_timestamp(struct sk_buff *skb)
  815. {
  816. struct timespec tv;
  817. struct icmp_bxm icmp_param;
  818. /*
  819. * Too short.
  820. */
  821. if (skb->len < 4)
  822. goto out_err;
  823. /*
  824. * Fill in the current time as ms since midnight UT:
  825. */
  826. getnstimeofday(&tv);
  827. icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC +
  828. tv.tv_nsec / NSEC_PER_MSEC);
  829. icmp_param.data.times[2] = icmp_param.data.times[1];
  830. if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
  831. BUG();
  832. icmp_param.data.icmph = *icmp_hdr(skb);
  833. icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
  834. icmp_param.data.icmph.code = 0;
  835. icmp_param.skb = skb;
  836. icmp_param.offset = 0;
  837. icmp_param.data_len = 0;
  838. icmp_param.head_len = sizeof(struct icmphdr) + 12;
  839. icmp_reply(&icmp_param, skb);
  840. return true;
  841. out_err:
  842. ICMP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
  843. return false;
  844. }
  845. static bool icmp_discard(struct sk_buff *skb)
  846. {
  847. /* pretend it was a success */
  848. return true;
  849. }
  850. /*
  851. * Deal with incoming ICMP packets.
  852. */
  853. int icmp_rcv(struct sk_buff *skb)
  854. {
  855. struct icmphdr *icmph;
  856. struct rtable *rt = skb_rtable(skb);
  857. struct net *net = dev_net(rt->dst.dev);
  858. bool success;
  859. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  860. struct sec_path *sp = skb_sec_path(skb);
  861. int nh;
  862. if (!(sp && sp->xvec[sp->len - 1]->props.flags &
  863. XFRM_STATE_ICMP))
  864. goto drop;
  865. if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
  866. goto drop;
  867. nh = skb_network_offset(skb);
  868. skb_set_network_header(skb, sizeof(*icmph));
  869. if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
  870. goto drop;
  871. skb_set_network_header(skb, nh);
  872. }
  873. ICMP_INC_STATS_BH(net, ICMP_MIB_INMSGS);
  874. if (skb_checksum_simple_validate(skb))
  875. goto csum_error;
  876. if (!pskb_pull(skb, sizeof(*icmph)))
  877. goto error;
  878. icmph = icmp_hdr(skb);
  879. ICMPMSGIN_INC_STATS_BH(net, icmph->type);
  880. /*
  881. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  882. *
  883. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  884. * discarded.
  885. */
  886. if (icmph->type > NR_ICMP_TYPES)
  887. goto error;
  888. /*
  889. * Parse the ICMP message
  890. */
  891. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  892. /*
  893. * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
  894. * silently ignored (we let user decide with a sysctl).
  895. * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
  896. * discarded if to broadcast/multicast.
  897. */
  898. if ((icmph->type == ICMP_ECHO ||
  899. icmph->type == ICMP_TIMESTAMP) &&
  900. net->ipv4.sysctl_icmp_echo_ignore_broadcasts) {
  901. goto error;
  902. }
  903. if (icmph->type != ICMP_ECHO &&
  904. icmph->type != ICMP_TIMESTAMP &&
  905. icmph->type != ICMP_ADDRESS &&
  906. icmph->type != ICMP_ADDRESSREPLY) {
  907. goto error;
  908. }
  909. }
  910. success = icmp_pointers[icmph->type].handler(skb);
  911. if (success) {
  912. consume_skb(skb);
  913. return 0;
  914. }
  915. drop:
  916. kfree_skb(skb);
  917. return 0;
  918. csum_error:
  919. ICMP_INC_STATS_BH(net, ICMP_MIB_CSUMERRORS);
  920. error:
  921. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  922. goto drop;
  923. }
  924. void icmp_err(struct sk_buff *skb, u32 info)
  925. {
  926. struct iphdr *iph = (struct iphdr *)skb->data;
  927. int offset = iph->ihl<<2;
  928. struct icmphdr *icmph = (struct icmphdr *)(skb->data + offset);
  929. int type = icmp_hdr(skb)->type;
  930. int code = icmp_hdr(skb)->code;
  931. struct net *net = dev_net(skb->dev);
  932. /*
  933. * Use ping_err to handle all icmp errors except those
  934. * triggered by ICMP_ECHOREPLY which sent from kernel.
  935. */
  936. if (icmph->type != ICMP_ECHOREPLY) {
  937. ping_err(skb, offset, info);
  938. return;
  939. }
  940. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
  941. ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_ICMP, 0);
  942. else if (type == ICMP_REDIRECT)
  943. ipv4_redirect(skb, net, 0, 0, IPPROTO_ICMP, 0);
  944. }
  945. /*
  946. * This table is the definition of how we handle ICMP.
  947. */
  948. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
  949. [ICMP_ECHOREPLY] = {
  950. .handler = ping_rcv,
  951. },
  952. [1] = {
  953. .handler = icmp_discard,
  954. .error = 1,
  955. },
  956. [2] = {
  957. .handler = icmp_discard,
  958. .error = 1,
  959. },
  960. [ICMP_DEST_UNREACH] = {
  961. .handler = icmp_unreach,
  962. .error = 1,
  963. },
  964. [ICMP_SOURCE_QUENCH] = {
  965. .handler = icmp_unreach,
  966. .error = 1,
  967. },
  968. [ICMP_REDIRECT] = {
  969. .handler = icmp_redirect,
  970. .error = 1,
  971. },
  972. [6] = {
  973. .handler = icmp_discard,
  974. .error = 1,
  975. },
  976. [7] = {
  977. .handler = icmp_discard,
  978. .error = 1,
  979. },
  980. [ICMP_ECHO] = {
  981. .handler = icmp_echo,
  982. },
  983. [9] = {
  984. .handler = icmp_discard,
  985. .error = 1,
  986. },
  987. [10] = {
  988. .handler = icmp_discard,
  989. .error = 1,
  990. },
  991. [ICMP_TIME_EXCEEDED] = {
  992. .handler = icmp_unreach,
  993. .error = 1,
  994. },
  995. [ICMP_PARAMETERPROB] = {
  996. .handler = icmp_unreach,
  997. .error = 1,
  998. },
  999. [ICMP_TIMESTAMP] = {
  1000. .handler = icmp_timestamp,
  1001. },
  1002. [ICMP_TIMESTAMPREPLY] = {
  1003. .handler = icmp_discard,
  1004. },
  1005. [ICMP_INFO_REQUEST] = {
  1006. .handler = icmp_discard,
  1007. },
  1008. [ICMP_INFO_REPLY] = {
  1009. .handler = icmp_discard,
  1010. },
  1011. [ICMP_ADDRESS] = {
  1012. .handler = icmp_discard,
  1013. },
  1014. [ICMP_ADDRESSREPLY] = {
  1015. .handler = icmp_discard,
  1016. },
  1017. };
  1018. static void __net_exit icmp_sk_exit(struct net *net)
  1019. {
  1020. int i;
  1021. for_each_possible_cpu(i)
  1022. inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.icmp_sk, i));
  1023. free_percpu(net->ipv4.icmp_sk);
  1024. net->ipv4.icmp_sk = NULL;
  1025. }
  1026. static int __net_init icmp_sk_init(struct net *net)
  1027. {
  1028. int i, err;
  1029. net->ipv4.icmp_sk = alloc_percpu(struct sock *);
  1030. if (!net->ipv4.icmp_sk)
  1031. return -ENOMEM;
  1032. for_each_possible_cpu(i) {
  1033. struct sock *sk;
  1034. err = inet_ctl_sock_create(&sk, PF_INET,
  1035. SOCK_RAW, IPPROTO_ICMP, net);
  1036. if (err < 0)
  1037. goto fail;
  1038. *per_cpu_ptr(net->ipv4.icmp_sk, i) = sk;
  1039. /* Enough space for 2 64K ICMP packets, including
  1040. * sk_buff/skb_shared_info struct overhead.
  1041. */
  1042. sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
  1043. /*
  1044. * Speedup sock_wfree()
  1045. */
  1046. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  1047. inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
  1048. }
  1049. /* Control parameters for ECHO replies. */
  1050. net->ipv4.sysctl_icmp_echo_ignore_all = 0;
  1051. net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
  1052. /* Control parameter - ignore bogus broadcast responses? */
  1053. net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
  1054. /*
  1055. * Configurable global rate limit.
  1056. *
  1057. * ratelimit defines tokens/packet consumed for dst->rate_token
  1058. * bucket ratemask defines which icmp types are ratelimited by
  1059. * setting it's bit position.
  1060. *
  1061. * default:
  1062. * dest unreachable (3), source quench (4),
  1063. * time exceeded (11), parameter problem (12)
  1064. */
  1065. net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
  1066. net->ipv4.sysctl_icmp_ratemask = 0x1818;
  1067. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
  1068. return 0;
  1069. fail:
  1070. for_each_possible_cpu(i)
  1071. inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.icmp_sk, i));
  1072. free_percpu(net->ipv4.icmp_sk);
  1073. return err;
  1074. }
  1075. static struct pernet_operations __net_initdata icmp_sk_ops = {
  1076. .init = icmp_sk_init,
  1077. .exit = icmp_sk_exit,
  1078. };
  1079. int __init icmp_init(void)
  1080. {
  1081. return register_pernet_subsys(&icmp_sk_ops);
  1082. }