icmp.c 27 KB

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