icmp.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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(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_dst(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_dst(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. {
  507. struct iphdr *iph;
  508. int room;
  509. struct icmp_bxm *icmp_param;
  510. struct rtable *rt = skb_rtable(skb_in);
  511. struct ipcm_cookie ipc;
  512. struct flowi4 fl4;
  513. __be32 saddr;
  514. u8 tos;
  515. u32 mark;
  516. struct net *net;
  517. struct sock *sk;
  518. if (!rt)
  519. goto out;
  520. net = dev_net(rt->dst.dev);
  521. /*
  522. * Find the original header. It is expected to be valid, of course.
  523. * Check this, icmp_send is called from the most obscure devices
  524. * sometimes.
  525. */
  526. iph = ip_hdr(skb_in);
  527. if ((u8 *)iph < skb_in->head ||
  528. (skb_network_header(skb_in) + sizeof(*iph)) >
  529. skb_tail_pointer(skb_in))
  530. goto out;
  531. /*
  532. * No replies to physical multicast/broadcast
  533. */
  534. if (skb_in->pkt_type != PACKET_HOST)
  535. goto out;
  536. /*
  537. * Now check at the protocol level
  538. */
  539. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  540. goto out;
  541. /*
  542. * Only reply to fragment 0. We byte re-order the constant
  543. * mask for efficiency.
  544. */
  545. if (iph->frag_off & htons(IP_OFFSET))
  546. goto out;
  547. /*
  548. * If we send an ICMP error to an ICMP error a mess would result..
  549. */
  550. if (icmp_pointers[type].error) {
  551. /*
  552. * We are an error, check if we are replying to an
  553. * ICMP error
  554. */
  555. if (iph->protocol == IPPROTO_ICMP) {
  556. u8 _inner_type, *itp;
  557. itp = skb_header_pointer(skb_in,
  558. skb_network_header(skb_in) +
  559. (iph->ihl << 2) +
  560. offsetof(struct icmphdr,
  561. type) -
  562. skb_in->data,
  563. sizeof(_inner_type),
  564. &_inner_type);
  565. if (!itp)
  566. goto out;
  567. /*
  568. * Assume any unknown ICMP type is an error. This
  569. * isn't specified by the RFC, but think about it..
  570. */
  571. if (*itp > NR_ICMP_TYPES ||
  572. icmp_pointers[*itp].error)
  573. goto out;
  574. }
  575. }
  576. icmp_param = kmalloc(sizeof(*icmp_param), GFP_ATOMIC);
  577. if (!icmp_param)
  578. return;
  579. sk = icmp_xmit_lock(net);
  580. if (!sk)
  581. goto out_free;
  582. /*
  583. * Construct source address and options.
  584. */
  585. saddr = iph->daddr;
  586. if (!(rt->rt_flags & RTCF_LOCAL)) {
  587. struct net_device *dev = NULL;
  588. rcu_read_lock();
  589. if (rt_is_input_route(rt) &&
  590. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)
  591. dev = dev_get_by_index_rcu(net, inet_iif(skb_in));
  592. if (dev)
  593. saddr = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  594. else
  595. saddr = 0;
  596. rcu_read_unlock();
  597. }
  598. tos = icmp_pointers[type].error ? ((iph->tos & IPTOS_TOS_MASK) |
  599. IPTOS_PREC_INTERNETCONTROL) :
  600. iph->tos;
  601. mark = IP4_REPLY_MARK(net, skb_in->mark);
  602. if (ip_options_echo(&icmp_param->replyopts.opt.opt, skb_in))
  603. goto out_unlock;
  604. /*
  605. * Prepare data for ICMP header.
  606. */
  607. icmp_param->data.icmph.type = type;
  608. icmp_param->data.icmph.code = code;
  609. icmp_param->data.icmph.un.gateway = info;
  610. icmp_param->data.icmph.checksum = 0;
  611. icmp_param->skb = skb_in;
  612. icmp_param->offset = skb_network_offset(skb_in);
  613. inet_sk(sk)->tos = tos;
  614. sk->sk_mark = mark;
  615. ipc.addr = iph->saddr;
  616. ipc.opt = &icmp_param->replyopts.opt;
  617. ipc.tx_flags = 0;
  618. ipc.ttl = 0;
  619. ipc.tos = -1;
  620. rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos, mark,
  621. type, code, icmp_param);
  622. if (IS_ERR(rt))
  623. goto out_unlock;
  624. if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code))
  625. goto ende;
  626. /* RFC says return as much as we can without exceeding 576 bytes. */
  627. room = dst_mtu(&rt->dst);
  628. if (room > 576)
  629. room = 576;
  630. room -= sizeof(struct iphdr) + icmp_param->replyopts.opt.opt.optlen;
  631. room -= sizeof(struct icmphdr);
  632. icmp_param->data_len = skb_in->len - icmp_param->offset;
  633. if (icmp_param->data_len > room)
  634. icmp_param->data_len = room;
  635. icmp_param->head_len = sizeof(struct icmphdr);
  636. icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
  637. ende:
  638. ip_rt_put(rt);
  639. out_unlock:
  640. icmp_xmit_unlock(sk);
  641. out_free:
  642. kfree(icmp_param);
  643. out:;
  644. }
  645. EXPORT_SYMBOL(icmp_send);
  646. static void icmp_socket_deliver(struct sk_buff *skb, u32 info)
  647. {
  648. const struct iphdr *iph = (const struct iphdr *) skb->data;
  649. const struct net_protocol *ipprot;
  650. int protocol = iph->protocol;
  651. /* Checkin full IP header plus 8 bytes of protocol to
  652. * avoid additional coding at protocol handlers.
  653. */
  654. if (!pskb_may_pull(skb, iph->ihl * 4 + 8)) {
  655. __ICMP_INC_STATS(dev_net(skb->dev), ICMP_MIB_INERRORS);
  656. return;
  657. }
  658. raw_icmp_error(skb, protocol, info);
  659. ipprot = rcu_dereference(inet_protos[protocol]);
  660. if (ipprot && ipprot->err_handler)
  661. ipprot->err_handler(skb, info);
  662. }
  663. static bool icmp_tag_validation(int proto)
  664. {
  665. bool ok;
  666. rcu_read_lock();
  667. ok = rcu_dereference(inet_protos[proto])->icmp_strict_tag_validation;
  668. rcu_read_unlock();
  669. return ok;
  670. }
  671. /*
  672. * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEEDED, ICMP_QUENCH, and
  673. * ICMP_PARAMETERPROB.
  674. */
  675. static bool icmp_unreach(struct sk_buff *skb)
  676. {
  677. const struct iphdr *iph;
  678. struct icmphdr *icmph;
  679. struct net *net;
  680. u32 info = 0;
  681. net = dev_net(skb_dst(skb)->dev);
  682. /*
  683. * Incomplete header ?
  684. * Only checks for the IP header, there should be an
  685. * additional check for longer headers in upper levels.
  686. */
  687. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  688. goto out_err;
  689. icmph = icmp_hdr(skb);
  690. iph = (const struct iphdr *)skb->data;
  691. if (iph->ihl < 5) /* Mangled header, drop. */
  692. goto out_err;
  693. switch (icmph->type) {
  694. case 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. break;
  731. case ICMP_PARAMETERPROB:
  732. info = ntohl(icmph->un.gateway) >> 24;
  733. break;
  734. case ICMP_TIME_EXCEEDED:
  735. __ICMP_INC_STATS(net, ICMP_MIB_INTIMEEXCDS);
  736. if (icmph->code == ICMP_EXC_FRAGTIME)
  737. goto out;
  738. break;
  739. }
  740. /*
  741. * Throw it at our lower layers
  742. *
  743. * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
  744. * header.
  745. * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
  746. * transport layer.
  747. * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
  748. * transport layer.
  749. */
  750. /*
  751. * Check the other end isn't violating RFC 1122. Some routers send
  752. * bogus responses to broadcast frames. If you see this message
  753. * first check your netmask matches at both ends, if it does then
  754. * get the other vendor to fix their kit.
  755. */
  756. if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
  757. inet_addr_type_dev_table(net, skb->dev, iph->daddr) == RTN_BROADCAST) {
  758. net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
  759. &ip_hdr(skb)->saddr,
  760. icmph->type, icmph->code,
  761. &iph->daddr, skb->dev->name);
  762. goto out;
  763. }
  764. icmp_socket_deliver(skb, info);
  765. out:
  766. return true;
  767. out_err:
  768. __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
  769. return false;
  770. }
  771. /*
  772. * Handle ICMP_REDIRECT.
  773. */
  774. static bool icmp_redirect(struct sk_buff *skb)
  775. {
  776. if (skb->len < sizeof(struct iphdr)) {
  777. __ICMP_INC_STATS(dev_net(skb->dev), ICMP_MIB_INERRORS);
  778. return false;
  779. }
  780. if (!pskb_may_pull(skb, sizeof(struct iphdr))) {
  781. /* there aught to be a stat */
  782. return false;
  783. }
  784. icmp_socket_deliver(skb, icmp_hdr(skb)->un.gateway);
  785. return true;
  786. }
  787. /*
  788. * Handle ICMP_ECHO ("ping") requests.
  789. *
  790. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  791. * requests.
  792. * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
  793. * included in the reply.
  794. * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
  795. * echo requests, MUST have default=NOT.
  796. * See also WRT handling of options once they are done and working.
  797. */
  798. static bool icmp_echo(struct sk_buff *skb)
  799. {
  800. struct net *net;
  801. net = dev_net(skb_dst(skb)->dev);
  802. if (!net->ipv4.sysctl_icmp_echo_ignore_all) {
  803. struct icmp_bxm icmp_param;
  804. icmp_param.data.icmph = *icmp_hdr(skb);
  805. icmp_param.data.icmph.type = ICMP_ECHOREPLY;
  806. icmp_param.skb = skb;
  807. icmp_param.offset = 0;
  808. icmp_param.data_len = skb->len;
  809. icmp_param.head_len = sizeof(struct icmphdr);
  810. icmp_reply(&icmp_param, skb);
  811. }
  812. /* should there be an ICMP stat for ignored echos? */
  813. return true;
  814. }
  815. /*
  816. * Handle ICMP Timestamp requests.
  817. * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
  818. * SHOULD be in the kernel for minimum random latency.
  819. * MUST be accurate to a few minutes.
  820. * MUST be updated at least at 15Hz.
  821. */
  822. static bool icmp_timestamp(struct sk_buff *skb)
  823. {
  824. struct icmp_bxm icmp_param;
  825. /*
  826. * Too short.
  827. */
  828. if (skb->len < 4)
  829. goto out_err;
  830. /*
  831. * Fill in the current time as ms since midnight UT:
  832. */
  833. icmp_param.data.times[1] = inet_current_timestamp();
  834. icmp_param.data.times[2] = icmp_param.data.times[1];
  835. if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
  836. BUG();
  837. icmp_param.data.icmph = *icmp_hdr(skb);
  838. icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
  839. icmp_param.data.icmph.code = 0;
  840. icmp_param.skb = skb;
  841. icmp_param.offset = 0;
  842. icmp_param.data_len = 0;
  843. icmp_param.head_len = sizeof(struct icmphdr) + 12;
  844. icmp_reply(&icmp_param, skb);
  845. return true;
  846. out_err:
  847. __ICMP_INC_STATS(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
  848. return false;
  849. }
  850. static bool icmp_discard(struct sk_buff *skb)
  851. {
  852. /* pretend it was a success */
  853. return true;
  854. }
  855. /*
  856. * Deal with incoming ICMP packets.
  857. */
  858. int icmp_rcv(struct sk_buff *skb)
  859. {
  860. struct icmphdr *icmph;
  861. struct rtable *rt = skb_rtable(skb);
  862. struct net *net = dev_net(rt->dst.dev);
  863. bool success;
  864. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  865. struct sec_path *sp = skb_sec_path(skb);
  866. int nh;
  867. if (!(sp && sp->xvec[sp->len - 1]->props.flags &
  868. XFRM_STATE_ICMP))
  869. goto drop;
  870. if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
  871. goto drop;
  872. nh = skb_network_offset(skb);
  873. skb_set_network_header(skb, sizeof(*icmph));
  874. if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
  875. goto drop;
  876. skb_set_network_header(skb, nh);
  877. }
  878. __ICMP_INC_STATS(net, ICMP_MIB_INMSGS);
  879. if (skb_checksum_simple_validate(skb))
  880. goto csum_error;
  881. if (!pskb_pull(skb, sizeof(*icmph)))
  882. goto error;
  883. icmph = icmp_hdr(skb);
  884. ICMPMSGIN_INC_STATS(net, icmph->type);
  885. /*
  886. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  887. *
  888. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  889. * discarded.
  890. */
  891. if (icmph->type > NR_ICMP_TYPES)
  892. goto error;
  893. /*
  894. * Parse the ICMP message
  895. */
  896. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  897. /*
  898. * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
  899. * silently ignored (we let user decide with a sysctl).
  900. * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
  901. * discarded if to broadcast/multicast.
  902. */
  903. if ((icmph->type == ICMP_ECHO ||
  904. icmph->type == ICMP_TIMESTAMP) &&
  905. net->ipv4.sysctl_icmp_echo_ignore_broadcasts) {
  906. goto error;
  907. }
  908. if (icmph->type != ICMP_ECHO &&
  909. icmph->type != ICMP_TIMESTAMP &&
  910. icmph->type != ICMP_ADDRESS &&
  911. icmph->type != ICMP_ADDRESSREPLY) {
  912. goto error;
  913. }
  914. }
  915. success = icmp_pointers[icmph->type].handler(skb);
  916. if (success) {
  917. consume_skb(skb);
  918. return 0;
  919. }
  920. drop:
  921. kfree_skb(skb);
  922. return 0;
  923. csum_error:
  924. __ICMP_INC_STATS(net, ICMP_MIB_CSUMERRORS);
  925. error:
  926. __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
  927. goto drop;
  928. }
  929. void icmp_err(struct sk_buff *skb, u32 info)
  930. {
  931. struct iphdr *iph = (struct iphdr *)skb->data;
  932. int offset = iph->ihl<<2;
  933. struct icmphdr *icmph = (struct icmphdr *)(skb->data + offset);
  934. int type = icmp_hdr(skb)->type;
  935. int code = icmp_hdr(skb)->code;
  936. struct net *net = dev_net(skb->dev);
  937. /*
  938. * Use ping_err to handle all icmp errors except those
  939. * triggered by ICMP_ECHOREPLY which sent from kernel.
  940. */
  941. if (icmph->type != ICMP_ECHOREPLY) {
  942. ping_err(skb, offset, info);
  943. return;
  944. }
  945. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
  946. ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_ICMP, 0);
  947. else if (type == ICMP_REDIRECT)
  948. ipv4_redirect(skb, net, 0, 0, IPPROTO_ICMP, 0);
  949. }
  950. /*
  951. * This table is the definition of how we handle ICMP.
  952. */
  953. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
  954. [ICMP_ECHOREPLY] = {
  955. .handler = ping_rcv,
  956. },
  957. [1] = {
  958. .handler = icmp_discard,
  959. .error = 1,
  960. },
  961. [2] = {
  962. .handler = icmp_discard,
  963. .error = 1,
  964. },
  965. [ICMP_DEST_UNREACH] = {
  966. .handler = icmp_unreach,
  967. .error = 1,
  968. },
  969. [ICMP_SOURCE_QUENCH] = {
  970. .handler = icmp_unreach,
  971. .error = 1,
  972. },
  973. [ICMP_REDIRECT] = {
  974. .handler = icmp_redirect,
  975. .error = 1,
  976. },
  977. [6] = {
  978. .handler = icmp_discard,
  979. .error = 1,
  980. },
  981. [7] = {
  982. .handler = icmp_discard,
  983. .error = 1,
  984. },
  985. [ICMP_ECHO] = {
  986. .handler = icmp_echo,
  987. },
  988. [9] = {
  989. .handler = icmp_discard,
  990. .error = 1,
  991. },
  992. [10] = {
  993. .handler = icmp_discard,
  994. .error = 1,
  995. },
  996. [ICMP_TIME_EXCEEDED] = {
  997. .handler = icmp_unreach,
  998. .error = 1,
  999. },
  1000. [ICMP_PARAMETERPROB] = {
  1001. .handler = icmp_unreach,
  1002. .error = 1,
  1003. },
  1004. [ICMP_TIMESTAMP] = {
  1005. .handler = icmp_timestamp,
  1006. },
  1007. [ICMP_TIMESTAMPREPLY] = {
  1008. .handler = icmp_discard,
  1009. },
  1010. [ICMP_INFO_REQUEST] = {
  1011. .handler = icmp_discard,
  1012. },
  1013. [ICMP_INFO_REPLY] = {
  1014. .handler = icmp_discard,
  1015. },
  1016. [ICMP_ADDRESS] = {
  1017. .handler = icmp_discard,
  1018. },
  1019. [ICMP_ADDRESSREPLY] = {
  1020. .handler = icmp_discard,
  1021. },
  1022. };
  1023. static void __net_exit icmp_sk_exit(struct net *net)
  1024. {
  1025. int i;
  1026. for_each_possible_cpu(i)
  1027. inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.icmp_sk, i));
  1028. free_percpu(net->ipv4.icmp_sk);
  1029. net->ipv4.icmp_sk = NULL;
  1030. }
  1031. static int __net_init icmp_sk_init(struct net *net)
  1032. {
  1033. int i, err;
  1034. net->ipv4.icmp_sk = alloc_percpu(struct sock *);
  1035. if (!net->ipv4.icmp_sk)
  1036. return -ENOMEM;
  1037. for_each_possible_cpu(i) {
  1038. struct sock *sk;
  1039. err = inet_ctl_sock_create(&sk, PF_INET,
  1040. SOCK_RAW, IPPROTO_ICMP, net);
  1041. if (err < 0)
  1042. goto fail;
  1043. *per_cpu_ptr(net->ipv4.icmp_sk, i) = sk;
  1044. /* Enough space for 2 64K ICMP packets, including
  1045. * sk_buff/skb_shared_info struct overhead.
  1046. */
  1047. sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
  1048. /*
  1049. * Speedup sock_wfree()
  1050. */
  1051. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  1052. inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
  1053. }
  1054. /* Control parameters for ECHO replies. */
  1055. net->ipv4.sysctl_icmp_echo_ignore_all = 0;
  1056. net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
  1057. /* Control parameter - ignore bogus broadcast responses? */
  1058. net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
  1059. /*
  1060. * Configurable global rate limit.
  1061. *
  1062. * ratelimit defines tokens/packet consumed for dst->rate_token
  1063. * bucket ratemask defines which icmp types are ratelimited by
  1064. * setting it's bit position.
  1065. *
  1066. * default:
  1067. * dest unreachable (3), source quench (4),
  1068. * time exceeded (11), parameter problem (12)
  1069. */
  1070. net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
  1071. net->ipv4.sysctl_icmp_ratemask = 0x1818;
  1072. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
  1073. return 0;
  1074. fail:
  1075. for_each_possible_cpu(i)
  1076. inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.icmp_sk, i));
  1077. free_percpu(net->ipv4.icmp_sk);
  1078. return err;
  1079. }
  1080. static struct pernet_operations __net_initdata icmp_sk_ops = {
  1081. .init = icmp_sk_init,
  1082. .exit = icmp_sk_exit,
  1083. };
  1084. int __init icmp_init(void)
  1085. {
  1086. return register_pernet_subsys(&icmp_sk_ops);
  1087. }