icmp.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. * NET3: Implementation of the ICMP protocol layer.
  3. *
  4. * Alan Cox, <alan@redhat.com>
  5. *
  6. * Version: $Id: icmp.c,v 1.85 2002/02/01 22:01:03 davem Exp $
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * Some of the function names and the icmp unreach table for this
  14. * module were derived from [icmp.c 1.0.11 06/02/93] by
  15. * Ross Biro, Fred N. van Kempen, Mark Evans, Alan Cox, Gerhard Koerting.
  16. * Other than that this module is a complete rewrite.
  17. *
  18. * Fixes:
  19. * Clemens Fruhwirth : introduce global icmp rate limiting
  20. * with icmp type masking ability instead
  21. * of broken per type icmp timeouts.
  22. * Mike Shaver : RFC1122 checks.
  23. * Alan Cox : Multicast ping reply as self.
  24. * Alan Cox : Fix atomicity lockup in ip_build_xmit
  25. * call.
  26. * Alan Cox : Added 216,128 byte paths to the MTU
  27. * code.
  28. * Martin Mares : RFC1812 checks.
  29. * Martin Mares : Can be configured to follow redirects
  30. * if acting as a router _without_ a
  31. * routing protocol (RFC 1812).
  32. * Martin Mares : Echo requests may be configured to
  33. * be ignored (RFC 1812).
  34. * Martin Mares : Limitation of ICMP error message
  35. * transmit rate (RFC 1812).
  36. * Martin Mares : TOS and Precedence set correctly
  37. * (RFC 1812).
  38. * Martin Mares : Now copying as much data from the
  39. * original packet as we can without
  40. * exceeding 576 bytes (RFC 1812).
  41. * Willy Konynenberg : Transparent proxying support.
  42. * Keith Owens : RFC1191 correction for 4.2BSD based
  43. * path MTU bug.
  44. * Thomas Quinot : ICMP Dest Unreach codes up to 15 are
  45. * valid (RFC 1812).
  46. * Andi Kleen : Check all packet lengths properly
  47. * and moved all kfree_skb() up to
  48. * icmp_rcv.
  49. * Andi Kleen : Move the rate limit bookkeeping
  50. * into the dest entry and use a token
  51. * bucket filter (thanks to ANK). Make
  52. * the rates sysctl configurable.
  53. * Yu Tianli : Fixed two ugly bugs in icmp_send
  54. * - IP option length was accounted wrongly
  55. * - ICMP header length was not accounted
  56. * at all.
  57. * Tristan Greaves : Added sysctl option to ignore bogus
  58. * broadcast responses from broken routers.
  59. *
  60. * To Fix:
  61. *
  62. * - Should use skb_pull() instead of all the manual checking.
  63. * This would also greatly simply some upper layer error handlers. --AK
  64. *
  65. */
  66. #include <linux/config.h>
  67. #include <linux/module.h>
  68. #include <linux/types.h>
  69. #include <linux/jiffies.h>
  70. #include <linux/kernel.h>
  71. #include <linux/fcntl.h>
  72. #include <linux/socket.h>
  73. #include <linux/in.h>
  74. #include <linux/inet.h>
  75. #include <linux/inetdevice.h>
  76. #include <linux/netdevice.h>
  77. #include <linux/string.h>
  78. #include <linux/netfilter_ipv4.h>
  79. #include <net/snmp.h>
  80. #include <net/ip.h>
  81. #include <net/route.h>
  82. #include <net/protocol.h>
  83. #include <net/icmp.h>
  84. #include <net/tcp.h>
  85. #include <net/udp.h>
  86. #include <net/raw.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/system.h>
  93. #include <asm/uaccess.h>
  94. #include <net/checksum.h>
  95. /*
  96. * Build xmit assembly blocks
  97. */
  98. struct icmp_bxm {
  99. struct sk_buff *skb;
  100. int offset;
  101. int data_len;
  102. struct {
  103. struct icmphdr icmph;
  104. __u32 times[3];
  105. } data;
  106. int head_len;
  107. struct ip_options replyopts;
  108. unsigned char optbuf[40];
  109. };
  110. /*
  111. * Statistics
  112. */
  113. DEFINE_SNMP_STAT(struct icmp_mib, icmp_statistics) __read_mostly;
  114. /* An array of errno for error messages from dest unreach. */
  115. /* RFC 1122: 3.2.2.1 States that NET_UNREACH, HOST_UNREACH and SR_FAILED MUST be considered 'transient errs'. */
  116. struct icmp_err icmp_err_convert[] = {
  117. {
  118. .errno = ENETUNREACH, /* ICMP_NET_UNREACH */
  119. .fatal = 0,
  120. },
  121. {
  122. .errno = EHOSTUNREACH, /* ICMP_HOST_UNREACH */
  123. .fatal = 0,
  124. },
  125. {
  126. .errno = ENOPROTOOPT /* ICMP_PROT_UNREACH */,
  127. .fatal = 1,
  128. },
  129. {
  130. .errno = ECONNREFUSED, /* ICMP_PORT_UNREACH */
  131. .fatal = 1,
  132. },
  133. {
  134. .errno = EMSGSIZE, /* ICMP_FRAG_NEEDED */
  135. .fatal = 0,
  136. },
  137. {
  138. .errno = EOPNOTSUPP, /* ICMP_SR_FAILED */
  139. .fatal = 0,
  140. },
  141. {
  142. .errno = ENETUNREACH, /* ICMP_NET_UNKNOWN */
  143. .fatal = 1,
  144. },
  145. {
  146. .errno = EHOSTDOWN, /* ICMP_HOST_UNKNOWN */
  147. .fatal = 1,
  148. },
  149. {
  150. .errno = ENONET, /* ICMP_HOST_ISOLATED */
  151. .fatal = 1,
  152. },
  153. {
  154. .errno = ENETUNREACH, /* ICMP_NET_ANO */
  155. .fatal = 1,
  156. },
  157. {
  158. .errno = EHOSTUNREACH, /* ICMP_HOST_ANO */
  159. .fatal = 1,
  160. },
  161. {
  162. .errno = ENETUNREACH, /* ICMP_NET_UNR_TOS */
  163. .fatal = 0,
  164. },
  165. {
  166. .errno = EHOSTUNREACH, /* ICMP_HOST_UNR_TOS */
  167. .fatal = 0,
  168. },
  169. {
  170. .errno = EHOSTUNREACH, /* ICMP_PKT_FILTERED */
  171. .fatal = 1,
  172. },
  173. {
  174. .errno = EHOSTUNREACH, /* ICMP_PREC_VIOLATION */
  175. .fatal = 1,
  176. },
  177. {
  178. .errno = EHOSTUNREACH, /* ICMP_PREC_CUTOFF */
  179. .fatal = 1,
  180. },
  181. };
  182. /* Control parameters for ECHO replies. */
  183. int sysctl_icmp_echo_ignore_all;
  184. int sysctl_icmp_echo_ignore_broadcasts = 1;
  185. /* Control parameter - ignore bogus broadcast responses? */
  186. int sysctl_icmp_ignore_bogus_error_responses = 1;
  187. /*
  188. * Configurable global rate limit.
  189. *
  190. * ratelimit defines tokens/packet consumed for dst->rate_token bucket
  191. * ratemask defines which icmp types are ratelimited by setting
  192. * it's bit position.
  193. *
  194. * default:
  195. * dest unreachable (3), source quench (4),
  196. * time exceeded (11), parameter problem (12)
  197. */
  198. int sysctl_icmp_ratelimit = 1 * HZ;
  199. int sysctl_icmp_ratemask = 0x1818;
  200. int sysctl_icmp_errors_use_inbound_ifaddr;
  201. /*
  202. * ICMP control array. This specifies what to do with each ICMP.
  203. */
  204. struct icmp_control {
  205. int output_entry; /* Field for increment on output */
  206. int input_entry; /* Field for increment on input */
  207. void (*handler)(struct sk_buff *skb);
  208. short error; /* This ICMP is classed as an error message */
  209. };
  210. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
  211. /*
  212. * The ICMP socket(s). This is the most convenient way to flow control
  213. * our ICMP output as well as maintain a clean interface throughout
  214. * all layers. All Socketless IP sends will soon be gone.
  215. *
  216. * On SMP we have one ICMP socket per-cpu.
  217. */
  218. static DEFINE_PER_CPU(struct socket *, __icmp_socket) = NULL;
  219. #define icmp_socket __get_cpu_var(__icmp_socket)
  220. static __inline__ int icmp_xmit_lock(void)
  221. {
  222. local_bh_disable();
  223. if (unlikely(!spin_trylock(&icmp_socket->sk->sk_lock.slock))) {
  224. /* This can happen if the output path signals a
  225. * dst_link_failure() for an outgoing ICMP packet.
  226. */
  227. local_bh_enable();
  228. return 1;
  229. }
  230. return 0;
  231. }
  232. static void icmp_xmit_unlock(void)
  233. {
  234. spin_unlock_bh(&icmp_socket->sk->sk_lock.slock);
  235. }
  236. /*
  237. * Send an ICMP frame.
  238. */
  239. /*
  240. * Check transmit rate limitation for given message.
  241. * The rate information is held in the destination cache now.
  242. * This function is generic and could be used for other purposes
  243. * too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
  244. *
  245. * Note that the same dst_entry fields are modified by functions in
  246. * route.c too, but these work for packet destinations while xrlim_allow
  247. * works for icmp destinations. This means the rate limiting information
  248. * for one "ip object" is shared - and these ICMPs are twice limited:
  249. * by source and by destination.
  250. *
  251. * RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
  252. * SHOULD allow setting of rate limits
  253. *
  254. * Shared between ICMPv4 and ICMPv6.
  255. */
  256. #define XRLIM_BURST_FACTOR 6
  257. int xrlim_allow(struct dst_entry *dst, int timeout)
  258. {
  259. unsigned long now;
  260. int rc = 0;
  261. now = jiffies;
  262. dst->rate_tokens += now - dst->rate_last;
  263. dst->rate_last = now;
  264. if (dst->rate_tokens > XRLIM_BURST_FACTOR * timeout)
  265. dst->rate_tokens = XRLIM_BURST_FACTOR * timeout;
  266. if (dst->rate_tokens >= timeout) {
  267. dst->rate_tokens -= timeout;
  268. rc = 1;
  269. }
  270. return rc;
  271. }
  272. static inline int icmpv4_xrlim_allow(struct rtable *rt, int type, int code)
  273. {
  274. struct dst_entry *dst = &rt->u.dst;
  275. int rc = 1;
  276. if (type > NR_ICMP_TYPES)
  277. goto out;
  278. /* Don't limit PMTU discovery. */
  279. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
  280. goto out;
  281. /* No rate limit on loopback */
  282. if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
  283. goto out;
  284. /* Limit if icmp type is enabled in ratemask. */
  285. if ((1 << type) & sysctl_icmp_ratemask)
  286. rc = xrlim_allow(dst, sysctl_icmp_ratelimit);
  287. out:
  288. return rc;
  289. }
  290. /*
  291. * Maintain the counters used in the SNMP statistics for outgoing ICMP
  292. */
  293. static void icmp_out_count(int type)
  294. {
  295. if (type <= NR_ICMP_TYPES) {
  296. ICMP_INC_STATS(icmp_pointers[type].output_entry);
  297. ICMP_INC_STATS(ICMP_MIB_OUTMSGS);
  298. }
  299. }
  300. /*
  301. * Checksum each fragment, and on the first include the headers and final
  302. * checksum.
  303. */
  304. static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
  305. struct sk_buff *skb)
  306. {
  307. struct icmp_bxm *icmp_param = (struct icmp_bxm *)from;
  308. unsigned int csum;
  309. csum = skb_copy_and_csum_bits(icmp_param->skb,
  310. icmp_param->offset + offset,
  311. to, len, 0);
  312. skb->csum = csum_block_add(skb->csum, csum, odd);
  313. if (icmp_pointers[icmp_param->data.icmph.type].error)
  314. nf_ct_attach(skb, icmp_param->skb);
  315. return 0;
  316. }
  317. static void icmp_push_reply(struct icmp_bxm *icmp_param,
  318. struct ipcm_cookie *ipc, struct rtable *rt)
  319. {
  320. struct sk_buff *skb;
  321. if (ip_append_data(icmp_socket->sk, icmp_glue_bits, icmp_param,
  322. icmp_param->data_len+icmp_param->head_len,
  323. icmp_param->head_len,
  324. ipc, rt, MSG_DONTWAIT) < 0)
  325. ip_flush_pending_frames(icmp_socket->sk);
  326. else if ((skb = skb_peek(&icmp_socket->sk->sk_write_queue)) != NULL) {
  327. struct icmphdr *icmph = skb->h.icmph;
  328. unsigned int csum = 0;
  329. struct sk_buff *skb1;
  330. skb_queue_walk(&icmp_socket->sk->sk_write_queue, skb1) {
  331. csum = csum_add(csum, skb1->csum);
  332. }
  333. csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
  334. (char *)icmph,
  335. icmp_param->head_len, csum);
  336. icmph->checksum = csum_fold(csum);
  337. skb->ip_summed = CHECKSUM_NONE;
  338. ip_push_pending_frames(icmp_socket->sk);
  339. }
  340. }
  341. /*
  342. * Driving logic for building and sending ICMP messages.
  343. */
  344. static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
  345. {
  346. struct sock *sk = icmp_socket->sk;
  347. struct inet_sock *inet = inet_sk(sk);
  348. struct ipcm_cookie ipc;
  349. struct rtable *rt = (struct rtable *)skb->dst;
  350. u32 daddr;
  351. if (ip_options_echo(&icmp_param->replyopts, skb))
  352. return;
  353. if (icmp_xmit_lock())
  354. return;
  355. icmp_param->data.icmph.checksum = 0;
  356. icmp_out_count(icmp_param->data.icmph.type);
  357. inet->tos = skb->nh.iph->tos;
  358. daddr = ipc.addr = rt->rt_src;
  359. ipc.opt = NULL;
  360. if (icmp_param->replyopts.optlen) {
  361. ipc.opt = &icmp_param->replyopts;
  362. if (ipc.opt->srr)
  363. daddr = icmp_param->replyopts.faddr;
  364. }
  365. {
  366. struct flowi fl = { .nl_u = { .ip4_u =
  367. { .daddr = daddr,
  368. .saddr = rt->rt_spec_dst,
  369. .tos = RT_TOS(skb->nh.iph->tos) } },
  370. .proto = IPPROTO_ICMP };
  371. if (ip_route_output_key(&rt, &fl))
  372. goto out_unlock;
  373. }
  374. if (icmpv4_xrlim_allow(rt, icmp_param->data.icmph.type,
  375. icmp_param->data.icmph.code))
  376. icmp_push_reply(icmp_param, &ipc, rt);
  377. ip_rt_put(rt);
  378. out_unlock:
  379. icmp_xmit_unlock();
  380. }
  381. /*
  382. * Send an ICMP message in response to a situation
  383. *
  384. * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
  385. * MAY send more (we do).
  386. * MUST NOT change this header information.
  387. * MUST NOT reply to a multicast/broadcast IP address.
  388. * MUST NOT reply to a multicast/broadcast MAC address.
  389. * MUST reply to only the first fragment.
  390. */
  391. void icmp_send(struct sk_buff *skb_in, int type, int code, u32 info)
  392. {
  393. struct iphdr *iph;
  394. int room;
  395. struct icmp_bxm icmp_param;
  396. struct rtable *rt = (struct rtable *)skb_in->dst;
  397. struct ipcm_cookie ipc;
  398. u32 saddr;
  399. u8 tos;
  400. if (!rt)
  401. goto out;
  402. /*
  403. * Find the original header. It is expected to be valid, of course.
  404. * Check this, icmp_send is called from the most obscure devices
  405. * sometimes.
  406. */
  407. iph = skb_in->nh.iph;
  408. if ((u8 *)iph < skb_in->head || (u8 *)(iph + 1) > skb_in->tail)
  409. goto out;
  410. /*
  411. * No replies to physical multicast/broadcast
  412. */
  413. if (skb_in->pkt_type != PACKET_HOST)
  414. goto out;
  415. /*
  416. * Now check at the protocol level
  417. */
  418. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  419. goto out;
  420. /*
  421. * Only reply to fragment 0. We byte re-order the constant
  422. * mask for efficiency.
  423. */
  424. if (iph->frag_off & htons(IP_OFFSET))
  425. goto out;
  426. /*
  427. * If we send an ICMP error to an ICMP error a mess would result..
  428. */
  429. if (icmp_pointers[type].error) {
  430. /*
  431. * We are an error, check if we are replying to an
  432. * ICMP error
  433. */
  434. if (iph->protocol == IPPROTO_ICMP) {
  435. u8 _inner_type, *itp;
  436. itp = skb_header_pointer(skb_in,
  437. skb_in->nh.raw +
  438. (iph->ihl << 2) +
  439. offsetof(struct icmphdr,
  440. type) -
  441. skb_in->data,
  442. sizeof(_inner_type),
  443. &_inner_type);
  444. if (itp == NULL)
  445. goto out;
  446. /*
  447. * Assume any unknown ICMP type is an error. This
  448. * isn't specified by the RFC, but think about it..
  449. */
  450. if (*itp > NR_ICMP_TYPES ||
  451. icmp_pointers[*itp].error)
  452. goto out;
  453. }
  454. }
  455. if (icmp_xmit_lock())
  456. return;
  457. /*
  458. * Construct source address and options.
  459. */
  460. saddr = iph->daddr;
  461. if (!(rt->rt_flags & RTCF_LOCAL)) {
  462. if (sysctl_icmp_errors_use_inbound_ifaddr)
  463. saddr = inet_select_addr(skb_in->dev, 0, RT_SCOPE_LINK);
  464. else
  465. saddr = 0;
  466. }
  467. tos = icmp_pointers[type].error ? ((iph->tos & IPTOS_TOS_MASK) |
  468. IPTOS_PREC_INTERNETCONTROL) :
  469. iph->tos;
  470. if (ip_options_echo(&icmp_param.replyopts, skb_in))
  471. goto out_unlock;
  472. /*
  473. * Prepare data for ICMP header.
  474. */
  475. icmp_param.data.icmph.type = type;
  476. icmp_param.data.icmph.code = code;
  477. icmp_param.data.icmph.un.gateway = info;
  478. icmp_param.data.icmph.checksum = 0;
  479. icmp_param.skb = skb_in;
  480. icmp_param.offset = skb_in->nh.raw - skb_in->data;
  481. icmp_out_count(icmp_param.data.icmph.type);
  482. inet_sk(icmp_socket->sk)->tos = tos;
  483. ipc.addr = iph->saddr;
  484. ipc.opt = &icmp_param.replyopts;
  485. {
  486. struct flowi fl = {
  487. .nl_u = {
  488. .ip4_u = {
  489. .daddr = icmp_param.replyopts.srr ?
  490. icmp_param.replyopts.faddr :
  491. iph->saddr,
  492. .saddr = saddr,
  493. .tos = RT_TOS(tos)
  494. }
  495. },
  496. .proto = IPPROTO_ICMP,
  497. .uli_u = {
  498. .icmpt = {
  499. .type = type,
  500. .code = code
  501. }
  502. }
  503. };
  504. if (ip_route_output_key(&rt, &fl))
  505. goto out_unlock;
  506. }
  507. if (!icmpv4_xrlim_allow(rt, type, code))
  508. goto ende;
  509. /* RFC says return as much as we can without exceeding 576 bytes. */
  510. room = dst_mtu(&rt->u.dst);
  511. if (room > 576)
  512. room = 576;
  513. room -= sizeof(struct iphdr) + icmp_param.replyopts.optlen;
  514. room -= sizeof(struct icmphdr);
  515. icmp_param.data_len = skb_in->len - icmp_param.offset;
  516. if (icmp_param.data_len > room)
  517. icmp_param.data_len = room;
  518. icmp_param.head_len = sizeof(struct icmphdr);
  519. icmp_push_reply(&icmp_param, &ipc, rt);
  520. ende:
  521. ip_rt_put(rt);
  522. out_unlock:
  523. icmp_xmit_unlock();
  524. out:;
  525. }
  526. /*
  527. * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEED, and ICMP_QUENCH.
  528. */
  529. static void icmp_unreach(struct sk_buff *skb)
  530. {
  531. struct iphdr *iph;
  532. struct icmphdr *icmph;
  533. int hash, protocol;
  534. struct net_protocol *ipprot;
  535. struct sock *raw_sk;
  536. u32 info = 0;
  537. /*
  538. * Incomplete header ?
  539. * Only checks for the IP header, there should be an
  540. * additional check for longer headers in upper levels.
  541. */
  542. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  543. goto out_err;
  544. icmph = skb->h.icmph;
  545. iph = (struct iphdr *)skb->data;
  546. if (iph->ihl < 5) /* Mangled header, drop. */
  547. goto out_err;
  548. if (icmph->type == ICMP_DEST_UNREACH) {
  549. switch (icmph->code & 15) {
  550. case ICMP_NET_UNREACH:
  551. case ICMP_HOST_UNREACH:
  552. case ICMP_PROT_UNREACH:
  553. case ICMP_PORT_UNREACH:
  554. break;
  555. case ICMP_FRAG_NEEDED:
  556. if (ipv4_config.no_pmtu_disc) {
  557. LIMIT_NETDEBUG(KERN_INFO "ICMP: %u.%u.%u.%u: "
  558. "fragmentation needed "
  559. "and DF set.\n",
  560. NIPQUAD(iph->daddr));
  561. } else {
  562. info = ip_rt_frag_needed(iph,
  563. ntohs(icmph->un.frag.mtu));
  564. if (!info)
  565. goto out;
  566. }
  567. break;
  568. case ICMP_SR_FAILED:
  569. LIMIT_NETDEBUG(KERN_INFO "ICMP: %u.%u.%u.%u: Source "
  570. "Route Failed.\n",
  571. NIPQUAD(iph->daddr));
  572. break;
  573. default:
  574. break;
  575. }
  576. if (icmph->code > NR_ICMP_UNREACH)
  577. goto out;
  578. } else if (icmph->type == ICMP_PARAMETERPROB)
  579. info = ntohl(icmph->un.gateway) >> 24;
  580. /*
  581. * Throw it at our lower layers
  582. *
  583. * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
  584. * header.
  585. * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
  586. * transport layer.
  587. * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
  588. * transport layer.
  589. */
  590. /*
  591. * Check the other end isnt violating RFC 1122. Some routers send
  592. * bogus responses to broadcast frames. If you see this message
  593. * first check your netmask matches at both ends, if it does then
  594. * get the other vendor to fix their kit.
  595. */
  596. if (!sysctl_icmp_ignore_bogus_error_responses &&
  597. inet_addr_type(iph->daddr) == RTN_BROADCAST) {
  598. if (net_ratelimit())
  599. printk(KERN_WARNING "%u.%u.%u.%u sent an invalid ICMP "
  600. "type %u, code %u "
  601. "error to a broadcast: %u.%u.%u.%u on %s\n",
  602. NIPQUAD(skb->nh.iph->saddr),
  603. icmph->type, icmph->code,
  604. NIPQUAD(iph->daddr),
  605. skb->dev->name);
  606. goto out;
  607. }
  608. /* Checkin full IP header plus 8 bytes of protocol to
  609. * avoid additional coding at protocol handlers.
  610. */
  611. if (!pskb_may_pull(skb, iph->ihl * 4 + 8))
  612. goto out;
  613. iph = (struct iphdr *)skb->data;
  614. protocol = iph->protocol;
  615. /*
  616. * Deliver ICMP message to raw sockets. Pretty useless feature?
  617. */
  618. /* Note: See raw.c and net/raw.h, RAWV4_HTABLE_SIZE==MAX_INET_PROTOS */
  619. hash = protocol & (MAX_INET_PROTOS - 1);
  620. read_lock(&raw_v4_lock);
  621. if ((raw_sk = sk_head(&raw_v4_htable[hash])) != NULL) {
  622. while ((raw_sk = __raw_v4_lookup(raw_sk, protocol, iph->daddr,
  623. iph->saddr,
  624. skb->dev->ifindex)) != NULL) {
  625. raw_err(raw_sk, skb, info);
  626. raw_sk = sk_next(raw_sk);
  627. iph = (struct iphdr *)skb->data;
  628. }
  629. }
  630. read_unlock(&raw_v4_lock);
  631. rcu_read_lock();
  632. ipprot = rcu_dereference(inet_protos[hash]);
  633. if (ipprot && ipprot->err_handler)
  634. ipprot->err_handler(skb, info);
  635. rcu_read_unlock();
  636. out:
  637. return;
  638. out_err:
  639. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  640. goto out;
  641. }
  642. /*
  643. * Handle ICMP_REDIRECT.
  644. */
  645. static void icmp_redirect(struct sk_buff *skb)
  646. {
  647. struct iphdr *iph;
  648. if (skb->len < sizeof(struct iphdr))
  649. goto out_err;
  650. /*
  651. * Get the copied header of the packet that caused the redirect
  652. */
  653. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  654. goto out;
  655. iph = (struct iphdr *)skb->data;
  656. switch (skb->h.icmph->code & 7) {
  657. case ICMP_REDIR_NET:
  658. case ICMP_REDIR_NETTOS:
  659. /*
  660. * As per RFC recommendations now handle it as a host redirect.
  661. */
  662. case ICMP_REDIR_HOST:
  663. case ICMP_REDIR_HOSTTOS:
  664. ip_rt_redirect(skb->nh.iph->saddr, iph->daddr,
  665. skb->h.icmph->un.gateway,
  666. iph->saddr, skb->dev);
  667. break;
  668. }
  669. out:
  670. return;
  671. out_err:
  672. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  673. goto out;
  674. }
  675. /*
  676. * Handle ICMP_ECHO ("ping") requests.
  677. *
  678. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  679. * requests.
  680. * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
  681. * included in the reply.
  682. * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
  683. * echo requests, MUST have default=NOT.
  684. * See also WRT handling of options once they are done and working.
  685. */
  686. static void icmp_echo(struct sk_buff *skb)
  687. {
  688. if (!sysctl_icmp_echo_ignore_all) {
  689. struct icmp_bxm icmp_param;
  690. icmp_param.data.icmph = *skb->h.icmph;
  691. icmp_param.data.icmph.type = ICMP_ECHOREPLY;
  692. icmp_param.skb = skb;
  693. icmp_param.offset = 0;
  694. icmp_param.data_len = skb->len;
  695. icmp_param.head_len = sizeof(struct icmphdr);
  696. icmp_reply(&icmp_param, skb);
  697. }
  698. }
  699. /*
  700. * Handle ICMP Timestamp requests.
  701. * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
  702. * SHOULD be in the kernel for minimum random latency.
  703. * MUST be accurate to a few minutes.
  704. * MUST be updated at least at 15Hz.
  705. */
  706. static void icmp_timestamp(struct sk_buff *skb)
  707. {
  708. struct timeval tv;
  709. struct icmp_bxm icmp_param;
  710. /*
  711. * Too short.
  712. */
  713. if (skb->len < 4)
  714. goto out_err;
  715. /*
  716. * Fill in the current time as ms since midnight UT:
  717. */
  718. do_gettimeofday(&tv);
  719. icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * 1000 +
  720. tv.tv_usec / 1000);
  721. icmp_param.data.times[2] = icmp_param.data.times[1];
  722. if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
  723. BUG();
  724. icmp_param.data.icmph = *skb->h.icmph;
  725. icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
  726. icmp_param.data.icmph.code = 0;
  727. icmp_param.skb = skb;
  728. icmp_param.offset = 0;
  729. icmp_param.data_len = 0;
  730. icmp_param.head_len = sizeof(struct icmphdr) + 12;
  731. icmp_reply(&icmp_param, skb);
  732. out:
  733. return;
  734. out_err:
  735. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  736. goto out;
  737. }
  738. /*
  739. * Handle ICMP_ADDRESS_MASK requests. (RFC950)
  740. *
  741. * RFC1122 (3.2.2.9). A host MUST only send replies to
  742. * ADDRESS_MASK requests if it's been configured as an address mask
  743. * agent. Receiving a request doesn't constitute implicit permission to
  744. * act as one. Of course, implementing this correctly requires (SHOULD)
  745. * a way to turn the functionality on and off. Another one for sysctl(),
  746. * I guess. -- MS
  747. *
  748. * RFC1812 (4.3.3.9). A router MUST implement it.
  749. * A router SHOULD have switch turning it on/off.
  750. * This switch MUST be ON by default.
  751. *
  752. * Gratuitous replies, zero-source replies are not implemented,
  753. * that complies with RFC. DO NOT implement them!!! All the idea
  754. * of broadcast addrmask replies as specified in RFC950 is broken.
  755. * The problem is that it is not uncommon to have several prefixes
  756. * on one physical interface. Moreover, addrmask agent can even be
  757. * not aware of existing another prefixes.
  758. * If source is zero, addrmask agent cannot choose correct prefix.
  759. * Gratuitous mask announcements suffer from the same problem.
  760. * RFC1812 explains it, but still allows to use ADDRMASK,
  761. * that is pretty silly. --ANK
  762. *
  763. * All these rules are so bizarre, that I removed kernel addrmask
  764. * support at all. It is wrong, it is obsolete, nobody uses it in
  765. * any case. --ANK
  766. *
  767. * Furthermore you can do it with a usermode address agent program
  768. * anyway...
  769. */
  770. static void icmp_address(struct sk_buff *skb)
  771. {
  772. #if 0
  773. if (net_ratelimit())
  774. printk(KERN_DEBUG "a guy asks for address mask. Who is it?\n");
  775. #endif
  776. }
  777. /*
  778. * RFC1812 (4.3.3.9). A router SHOULD listen all replies, and complain
  779. * loudly if an inconsistency is found.
  780. */
  781. static void icmp_address_reply(struct sk_buff *skb)
  782. {
  783. struct rtable *rt = (struct rtable *)skb->dst;
  784. struct net_device *dev = skb->dev;
  785. struct in_device *in_dev;
  786. struct in_ifaddr *ifa;
  787. if (skb->len < 4 || !(rt->rt_flags&RTCF_DIRECTSRC))
  788. goto out;
  789. in_dev = in_dev_get(dev);
  790. if (!in_dev)
  791. goto out;
  792. rcu_read_lock();
  793. if (in_dev->ifa_list &&
  794. IN_DEV_LOG_MARTIANS(in_dev) &&
  795. IN_DEV_FORWARD(in_dev)) {
  796. u32 _mask, *mp;
  797. mp = skb_header_pointer(skb, 0, sizeof(_mask), &_mask);
  798. BUG_ON(mp == NULL);
  799. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  800. if (*mp == ifa->ifa_mask &&
  801. inet_ifa_match(rt->rt_src, ifa))
  802. break;
  803. }
  804. if (!ifa && net_ratelimit()) {
  805. printk(KERN_INFO "Wrong address mask %u.%u.%u.%u from "
  806. "%s/%u.%u.%u.%u\n",
  807. NIPQUAD(*mp), dev->name, NIPQUAD(rt->rt_src));
  808. }
  809. }
  810. rcu_read_unlock();
  811. in_dev_put(in_dev);
  812. out:;
  813. }
  814. static void icmp_discard(struct sk_buff *skb)
  815. {
  816. }
  817. /*
  818. * Deal with incoming ICMP packets.
  819. */
  820. int icmp_rcv(struct sk_buff *skb)
  821. {
  822. struct icmphdr *icmph;
  823. struct rtable *rt = (struct rtable *)skb->dst;
  824. ICMP_INC_STATS_BH(ICMP_MIB_INMSGS);
  825. switch (skb->ip_summed) {
  826. case CHECKSUM_HW:
  827. if (!(u16)csum_fold(skb->csum))
  828. break;
  829. /* fall through */
  830. case CHECKSUM_NONE:
  831. skb->csum = 0;
  832. if (__skb_checksum_complete(skb))
  833. goto error;
  834. }
  835. if (!pskb_pull(skb, sizeof(struct icmphdr)))
  836. goto error;
  837. icmph = skb->h.icmph;
  838. /*
  839. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  840. *
  841. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  842. * discarded.
  843. */
  844. if (icmph->type > NR_ICMP_TYPES)
  845. goto error;
  846. /*
  847. * Parse the ICMP message
  848. */
  849. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  850. /*
  851. * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
  852. * silently ignored (we let user decide with a sysctl).
  853. * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
  854. * discarded if to broadcast/multicast.
  855. */
  856. if ((icmph->type == ICMP_ECHO ||
  857. icmph->type == ICMP_TIMESTAMP) &&
  858. sysctl_icmp_echo_ignore_broadcasts) {
  859. goto error;
  860. }
  861. if (icmph->type != ICMP_ECHO &&
  862. icmph->type != ICMP_TIMESTAMP &&
  863. icmph->type != ICMP_ADDRESS &&
  864. icmph->type != ICMP_ADDRESSREPLY) {
  865. goto error;
  866. }
  867. }
  868. ICMP_INC_STATS_BH(icmp_pointers[icmph->type].input_entry);
  869. icmp_pointers[icmph->type].handler(skb);
  870. drop:
  871. kfree_skb(skb);
  872. return 0;
  873. error:
  874. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  875. goto drop;
  876. }
  877. /*
  878. * This table is the definition of how we handle ICMP.
  879. */
  880. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
  881. [ICMP_ECHOREPLY] = {
  882. .output_entry = ICMP_MIB_OUTECHOREPS,
  883. .input_entry = ICMP_MIB_INECHOREPS,
  884. .handler = icmp_discard,
  885. },
  886. [1] = {
  887. .output_entry = ICMP_MIB_DUMMY,
  888. .input_entry = ICMP_MIB_INERRORS,
  889. .handler = icmp_discard,
  890. .error = 1,
  891. },
  892. [2] = {
  893. .output_entry = ICMP_MIB_DUMMY,
  894. .input_entry = ICMP_MIB_INERRORS,
  895. .handler = icmp_discard,
  896. .error = 1,
  897. },
  898. [ICMP_DEST_UNREACH] = {
  899. .output_entry = ICMP_MIB_OUTDESTUNREACHS,
  900. .input_entry = ICMP_MIB_INDESTUNREACHS,
  901. .handler = icmp_unreach,
  902. .error = 1,
  903. },
  904. [ICMP_SOURCE_QUENCH] = {
  905. .output_entry = ICMP_MIB_OUTSRCQUENCHS,
  906. .input_entry = ICMP_MIB_INSRCQUENCHS,
  907. .handler = icmp_unreach,
  908. .error = 1,
  909. },
  910. [ICMP_REDIRECT] = {
  911. .output_entry = ICMP_MIB_OUTREDIRECTS,
  912. .input_entry = ICMP_MIB_INREDIRECTS,
  913. .handler = icmp_redirect,
  914. .error = 1,
  915. },
  916. [6] = {
  917. .output_entry = ICMP_MIB_DUMMY,
  918. .input_entry = ICMP_MIB_INERRORS,
  919. .handler = icmp_discard,
  920. .error = 1,
  921. },
  922. [7] = {
  923. .output_entry = ICMP_MIB_DUMMY,
  924. .input_entry = ICMP_MIB_INERRORS,
  925. .handler = icmp_discard,
  926. .error = 1,
  927. },
  928. [ICMP_ECHO] = {
  929. .output_entry = ICMP_MIB_OUTECHOS,
  930. .input_entry = ICMP_MIB_INECHOS,
  931. .handler = icmp_echo,
  932. },
  933. [9] = {
  934. .output_entry = ICMP_MIB_DUMMY,
  935. .input_entry = ICMP_MIB_INERRORS,
  936. .handler = icmp_discard,
  937. .error = 1,
  938. },
  939. [10] = {
  940. .output_entry = ICMP_MIB_DUMMY,
  941. .input_entry = ICMP_MIB_INERRORS,
  942. .handler = icmp_discard,
  943. .error = 1,
  944. },
  945. [ICMP_TIME_EXCEEDED] = {
  946. .output_entry = ICMP_MIB_OUTTIMEEXCDS,
  947. .input_entry = ICMP_MIB_INTIMEEXCDS,
  948. .handler = icmp_unreach,
  949. .error = 1,
  950. },
  951. [ICMP_PARAMETERPROB] = {
  952. .output_entry = ICMP_MIB_OUTPARMPROBS,
  953. .input_entry = ICMP_MIB_INPARMPROBS,
  954. .handler = icmp_unreach,
  955. .error = 1,
  956. },
  957. [ICMP_TIMESTAMP] = {
  958. .output_entry = ICMP_MIB_OUTTIMESTAMPS,
  959. .input_entry = ICMP_MIB_INTIMESTAMPS,
  960. .handler = icmp_timestamp,
  961. },
  962. [ICMP_TIMESTAMPREPLY] = {
  963. .output_entry = ICMP_MIB_OUTTIMESTAMPREPS,
  964. .input_entry = ICMP_MIB_INTIMESTAMPREPS,
  965. .handler = icmp_discard,
  966. },
  967. [ICMP_INFO_REQUEST] = {
  968. .output_entry = ICMP_MIB_DUMMY,
  969. .input_entry = ICMP_MIB_DUMMY,
  970. .handler = icmp_discard,
  971. },
  972. [ICMP_INFO_REPLY] = {
  973. .output_entry = ICMP_MIB_DUMMY,
  974. .input_entry = ICMP_MIB_DUMMY,
  975. .handler = icmp_discard,
  976. },
  977. [ICMP_ADDRESS] = {
  978. .output_entry = ICMP_MIB_OUTADDRMASKS,
  979. .input_entry = ICMP_MIB_INADDRMASKS,
  980. .handler = icmp_address,
  981. },
  982. [ICMP_ADDRESSREPLY] = {
  983. .output_entry = ICMP_MIB_OUTADDRMASKREPS,
  984. .input_entry = ICMP_MIB_INADDRMASKREPS,
  985. .handler = icmp_address_reply,
  986. },
  987. };
  988. void __init icmp_init(struct net_proto_family *ops)
  989. {
  990. struct inet_sock *inet;
  991. int i;
  992. for_each_possible_cpu(i) {
  993. int err;
  994. err = sock_create_kern(PF_INET, SOCK_RAW, IPPROTO_ICMP,
  995. &per_cpu(__icmp_socket, i));
  996. if (err < 0)
  997. panic("Failed to create the ICMP control socket.\n");
  998. per_cpu(__icmp_socket, i)->sk->sk_allocation = GFP_ATOMIC;
  999. /* Enough space for 2 64K ICMP packets, including
  1000. * sk_buff struct overhead.
  1001. */
  1002. per_cpu(__icmp_socket, i)->sk->sk_sndbuf =
  1003. (2 * ((64 * 1024) + sizeof(struct sk_buff)));
  1004. inet = inet_sk(per_cpu(__icmp_socket, i)->sk);
  1005. inet->uc_ttl = -1;
  1006. inet->pmtudisc = IP_PMTUDISC_DONT;
  1007. /* Unhash it so that IP input processing does not even
  1008. * see it, we do not wish this socket to see incoming
  1009. * packets.
  1010. */
  1011. per_cpu(__icmp_socket, i)->sk->sk_prot->unhash(per_cpu(__icmp_socket, i)->sk);
  1012. }
  1013. }
  1014. EXPORT_SYMBOL(icmp_err_convert);
  1015. EXPORT_SYMBOL(icmp_send);
  1016. EXPORT_SYMBOL(icmp_statistics);
  1017. EXPORT_SYMBOL(xrlim_allow);