icmp.c 29 KB

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