icmp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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;
  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. goto out;
  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. out:;
  381. }
  382. /*
  383. * Send an ICMP message in response to a situation
  384. *
  385. * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
  386. * MAY send more (we do).
  387. * MUST NOT change this header information.
  388. * MUST NOT reply to a multicast/broadcast IP address.
  389. * MUST NOT reply to a multicast/broadcast MAC address.
  390. * MUST reply to only the first fragment.
  391. */
  392. void icmp_send(struct sk_buff *skb_in, int type, int code, u32 info)
  393. {
  394. struct iphdr *iph;
  395. int room;
  396. struct icmp_bxm icmp_param;
  397. struct rtable *rt = (struct rtable *)skb_in->dst;
  398. struct ipcm_cookie ipc;
  399. u32 saddr;
  400. u8 tos;
  401. if (!rt)
  402. goto out;
  403. /*
  404. * Find the original header. It is expected to be valid, of course.
  405. * Check this, icmp_send is called from the most obscure devices
  406. * sometimes.
  407. */
  408. iph = skb_in->nh.iph;
  409. if ((u8 *)iph < skb_in->head || (u8 *)(iph + 1) > skb_in->tail)
  410. goto out;
  411. /*
  412. * No replies to physical multicast/broadcast
  413. */
  414. if (skb_in->pkt_type != PACKET_HOST)
  415. goto out;
  416. /*
  417. * Now check at the protocol level
  418. */
  419. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  420. goto out;
  421. /*
  422. * Only reply to fragment 0. We byte re-order the constant
  423. * mask for efficiency.
  424. */
  425. if (iph->frag_off & htons(IP_OFFSET))
  426. goto out;
  427. /*
  428. * If we send an ICMP error to an ICMP error a mess would result..
  429. */
  430. if (icmp_pointers[type].error) {
  431. /*
  432. * We are an error, check if we are replying to an
  433. * ICMP error
  434. */
  435. if (iph->protocol == IPPROTO_ICMP) {
  436. u8 _inner_type, *itp;
  437. itp = skb_header_pointer(skb_in,
  438. skb_in->nh.raw +
  439. (iph->ihl << 2) +
  440. offsetof(struct icmphdr,
  441. type) -
  442. skb_in->data,
  443. sizeof(_inner_type),
  444. &_inner_type);
  445. if (itp == NULL)
  446. goto out;
  447. /*
  448. * Assume any unknown ICMP type is an error. This
  449. * isn't specified by the RFC, but think about it..
  450. */
  451. if (*itp > NR_ICMP_TYPES ||
  452. icmp_pointers[*itp].error)
  453. goto out;
  454. }
  455. }
  456. if (icmp_xmit_lock())
  457. return;
  458. /*
  459. * Construct source address and options.
  460. */
  461. saddr = iph->daddr;
  462. if (!(rt->rt_flags & RTCF_LOCAL)) {
  463. if (sysctl_icmp_errors_use_inbound_ifaddr)
  464. saddr = inet_select_addr(skb_in->dev, 0, RT_SCOPE_LINK);
  465. else
  466. saddr = 0;
  467. }
  468. tos = icmp_pointers[type].error ? ((iph->tos & IPTOS_TOS_MASK) |
  469. IPTOS_PREC_INTERNETCONTROL) :
  470. iph->tos;
  471. if (ip_options_echo(&icmp_param.replyopts, skb_in))
  472. goto ende;
  473. /*
  474. * Prepare data for ICMP header.
  475. */
  476. icmp_param.data.icmph.type = type;
  477. icmp_param.data.icmph.code = code;
  478. icmp_param.data.icmph.un.gateway = info;
  479. icmp_param.data.icmph.checksum = 0;
  480. icmp_param.skb = skb_in;
  481. icmp_param.offset = skb_in->nh.raw - skb_in->data;
  482. icmp_out_count(icmp_param.data.icmph.type);
  483. inet_sk(icmp_socket->sk)->tos = tos;
  484. ipc.addr = iph->saddr;
  485. ipc.opt = &icmp_param.replyopts;
  486. {
  487. struct flowi fl = {
  488. .nl_u = {
  489. .ip4_u = {
  490. .daddr = icmp_param.replyopts.srr ?
  491. icmp_param.replyopts.faddr :
  492. iph->saddr,
  493. .saddr = saddr,
  494. .tos = RT_TOS(tos)
  495. }
  496. },
  497. .proto = IPPROTO_ICMP,
  498. .uli_u = {
  499. .icmpt = {
  500. .type = type,
  501. .code = code
  502. }
  503. }
  504. };
  505. if (ip_route_output_key(&rt, &fl))
  506. goto out_unlock;
  507. }
  508. if (!icmpv4_xrlim_allow(rt, type, code))
  509. goto ende;
  510. /* RFC says return as much as we can without exceeding 576 bytes. */
  511. room = dst_mtu(&rt->u.dst);
  512. if (room > 576)
  513. room = 576;
  514. room -= sizeof(struct iphdr) + icmp_param.replyopts.optlen;
  515. room -= sizeof(struct icmphdr);
  516. icmp_param.data_len = skb_in->len - icmp_param.offset;
  517. if (icmp_param.data_len > room)
  518. icmp_param.data_len = room;
  519. icmp_param.head_len = sizeof(struct icmphdr);
  520. icmp_push_reply(&icmp_param, &ipc, rt);
  521. ende:
  522. ip_rt_put(rt);
  523. out_unlock:
  524. icmp_xmit_unlock();
  525. out:;
  526. }
  527. /*
  528. * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEED, and ICMP_QUENCH.
  529. */
  530. static void icmp_unreach(struct sk_buff *skb)
  531. {
  532. struct iphdr *iph;
  533. struct icmphdr *icmph;
  534. int hash, protocol;
  535. struct net_protocol *ipprot;
  536. struct sock *raw_sk;
  537. u32 info = 0;
  538. /*
  539. * Incomplete header ?
  540. * Only checks for the IP header, there should be an
  541. * additional check for longer headers in upper levels.
  542. */
  543. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  544. goto out_err;
  545. icmph = skb->h.icmph;
  546. iph = (struct iphdr *)skb->data;
  547. if (iph->ihl < 5) /* Mangled header, drop. */
  548. goto out_err;
  549. if (icmph->type == ICMP_DEST_UNREACH) {
  550. switch (icmph->code & 15) {
  551. case ICMP_NET_UNREACH:
  552. case ICMP_HOST_UNREACH:
  553. case ICMP_PROT_UNREACH:
  554. case ICMP_PORT_UNREACH:
  555. break;
  556. case ICMP_FRAG_NEEDED:
  557. if (ipv4_config.no_pmtu_disc) {
  558. LIMIT_NETDEBUG(KERN_INFO "ICMP: %u.%u.%u.%u: "
  559. "fragmentation needed "
  560. "and DF set.\n",
  561. NIPQUAD(iph->daddr));
  562. } else {
  563. info = ip_rt_frag_needed(iph,
  564. ntohs(icmph->un.frag.mtu));
  565. if (!info)
  566. goto out;
  567. }
  568. break;
  569. case ICMP_SR_FAILED:
  570. LIMIT_NETDEBUG(KERN_INFO "ICMP: %u.%u.%u.%u: Source "
  571. "Route Failed.\n",
  572. NIPQUAD(iph->daddr));
  573. break;
  574. default:
  575. break;
  576. }
  577. if (icmph->code > NR_ICMP_UNREACH)
  578. goto out;
  579. } else if (icmph->type == ICMP_PARAMETERPROB)
  580. info = ntohl(icmph->un.gateway) >> 24;
  581. /*
  582. * Throw it at our lower layers
  583. *
  584. * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
  585. * header.
  586. * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
  587. * transport layer.
  588. * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
  589. * transport layer.
  590. */
  591. /*
  592. * Check the other end isnt violating RFC 1122. Some routers send
  593. * bogus responses to broadcast frames. If you see this message
  594. * first check your netmask matches at both ends, if it does then
  595. * get the other vendor to fix their kit.
  596. */
  597. if (!sysctl_icmp_ignore_bogus_error_responses &&
  598. inet_addr_type(iph->daddr) == RTN_BROADCAST) {
  599. if (net_ratelimit())
  600. printk(KERN_WARNING "%u.%u.%u.%u sent an invalid ICMP "
  601. "type %u, code %u "
  602. "error to a broadcast: %u.%u.%u.%u on %s\n",
  603. NIPQUAD(skb->nh.iph->saddr),
  604. icmph->type, icmph->code,
  605. NIPQUAD(iph->daddr),
  606. skb->dev->name);
  607. goto out;
  608. }
  609. /* Checkin full IP header plus 8 bytes of protocol to
  610. * avoid additional coding at protocol handlers.
  611. */
  612. if (!pskb_may_pull(skb, iph->ihl * 4 + 8))
  613. goto out;
  614. iph = (struct iphdr *)skb->data;
  615. protocol = iph->protocol;
  616. /*
  617. * Deliver ICMP message to raw sockets. Pretty useless feature?
  618. */
  619. /* Note: See raw.c and net/raw.h, RAWV4_HTABLE_SIZE==MAX_INET_PROTOS */
  620. hash = protocol & (MAX_INET_PROTOS - 1);
  621. read_lock(&raw_v4_lock);
  622. if ((raw_sk = sk_head(&raw_v4_htable[hash])) != NULL) {
  623. while ((raw_sk = __raw_v4_lookup(raw_sk, protocol, iph->daddr,
  624. iph->saddr,
  625. skb->dev->ifindex)) != NULL) {
  626. raw_err(raw_sk, skb, info);
  627. raw_sk = sk_next(raw_sk);
  628. iph = (struct iphdr *)skb->data;
  629. }
  630. }
  631. read_unlock(&raw_v4_lock);
  632. rcu_read_lock();
  633. ipprot = rcu_dereference(inet_protos[hash]);
  634. if (ipprot && ipprot->err_handler)
  635. ipprot->err_handler(skb, info);
  636. rcu_read_unlock();
  637. out:
  638. return;
  639. out_err:
  640. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  641. goto out;
  642. }
  643. /*
  644. * Handle ICMP_REDIRECT.
  645. */
  646. static void icmp_redirect(struct sk_buff *skb)
  647. {
  648. struct iphdr *iph;
  649. unsigned long ip;
  650. if (skb->len < sizeof(struct iphdr))
  651. goto out_err;
  652. /*
  653. * Get the copied header of the packet that caused the redirect
  654. */
  655. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  656. goto out;
  657. iph = (struct iphdr *)skb->data;
  658. ip = iph->daddr;
  659. switch (skb->h.icmph->code & 7) {
  660. case ICMP_REDIR_NET:
  661. case ICMP_REDIR_NETTOS:
  662. /*
  663. * As per RFC recommendations now handle it as a host redirect.
  664. */
  665. case ICMP_REDIR_HOST:
  666. case ICMP_REDIR_HOSTTOS:
  667. ip_rt_redirect(skb->nh.iph->saddr, ip, skb->h.icmph->un.gateway,
  668. iph->saddr, iph->tos, skb->dev);
  669. break;
  670. }
  671. out:
  672. return;
  673. out_err:
  674. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  675. goto out;
  676. }
  677. /*
  678. * Handle ICMP_ECHO ("ping") requests.
  679. *
  680. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  681. * requests.
  682. * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
  683. * included in the reply.
  684. * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
  685. * echo requests, MUST have default=NOT.
  686. * See also WRT handling of options once they are done and working.
  687. */
  688. static void icmp_echo(struct sk_buff *skb)
  689. {
  690. if (!sysctl_icmp_echo_ignore_all) {
  691. struct icmp_bxm icmp_param;
  692. icmp_param.data.icmph = *skb->h.icmph;
  693. icmp_param.data.icmph.type = ICMP_ECHOREPLY;
  694. icmp_param.skb = skb;
  695. icmp_param.offset = 0;
  696. icmp_param.data_len = skb->len;
  697. icmp_param.head_len = sizeof(struct icmphdr);
  698. icmp_reply(&icmp_param, skb);
  699. }
  700. }
  701. /*
  702. * Handle ICMP Timestamp requests.
  703. * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
  704. * SHOULD be in the kernel for minimum random latency.
  705. * MUST be accurate to a few minutes.
  706. * MUST be updated at least at 15Hz.
  707. */
  708. static void icmp_timestamp(struct sk_buff *skb)
  709. {
  710. struct timeval tv;
  711. struct icmp_bxm icmp_param;
  712. /*
  713. * Too short.
  714. */
  715. if (skb->len < 4)
  716. goto out_err;
  717. /*
  718. * Fill in the current time as ms since midnight UT:
  719. */
  720. do_gettimeofday(&tv);
  721. icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * 1000 +
  722. tv.tv_usec / 1000);
  723. icmp_param.data.times[2] = icmp_param.data.times[1];
  724. if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
  725. BUG();
  726. icmp_param.data.icmph = *skb->h.icmph;
  727. icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
  728. icmp_param.data.icmph.code = 0;
  729. icmp_param.skb = skb;
  730. icmp_param.offset = 0;
  731. icmp_param.data_len = 0;
  732. icmp_param.head_len = sizeof(struct icmphdr) + 12;
  733. icmp_reply(&icmp_param, skb);
  734. out:
  735. return;
  736. out_err:
  737. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  738. goto out;
  739. }
  740. /*
  741. * Handle ICMP_ADDRESS_MASK requests. (RFC950)
  742. *
  743. * RFC1122 (3.2.2.9). A host MUST only send replies to
  744. * ADDRESS_MASK requests if it's been configured as an address mask
  745. * agent. Receiving a request doesn't constitute implicit permission to
  746. * act as one. Of course, implementing this correctly requires (SHOULD)
  747. * a way to turn the functionality on and off. Another one for sysctl(),
  748. * I guess. -- MS
  749. *
  750. * RFC1812 (4.3.3.9). A router MUST implement it.
  751. * A router SHOULD have switch turning it on/off.
  752. * This switch MUST be ON by default.
  753. *
  754. * Gratuitous replies, zero-source replies are not implemented,
  755. * that complies with RFC. DO NOT implement them!!! All the idea
  756. * of broadcast addrmask replies as specified in RFC950 is broken.
  757. * The problem is that it is not uncommon to have several prefixes
  758. * on one physical interface. Moreover, addrmask agent can even be
  759. * not aware of existing another prefixes.
  760. * If source is zero, addrmask agent cannot choose correct prefix.
  761. * Gratuitous mask announcements suffer from the same problem.
  762. * RFC1812 explains it, but still allows to use ADDRMASK,
  763. * that is pretty silly. --ANK
  764. *
  765. * All these rules are so bizarre, that I removed kernel addrmask
  766. * support at all. It is wrong, it is obsolete, nobody uses it in
  767. * any case. --ANK
  768. *
  769. * Furthermore you can do it with a usermode address agent program
  770. * anyway...
  771. */
  772. static void icmp_address(struct sk_buff *skb)
  773. {
  774. #if 0
  775. if (net_ratelimit())
  776. printk(KERN_DEBUG "a guy asks for address mask. Who is it?\n");
  777. #endif
  778. }
  779. /*
  780. * RFC1812 (4.3.3.9). A router SHOULD listen all replies, and complain
  781. * loudly if an inconsistency is found.
  782. */
  783. static void icmp_address_reply(struct sk_buff *skb)
  784. {
  785. struct rtable *rt = (struct rtable *)skb->dst;
  786. struct net_device *dev = skb->dev;
  787. struct in_device *in_dev;
  788. struct in_ifaddr *ifa;
  789. if (skb->len < 4 || !(rt->rt_flags&RTCF_DIRECTSRC))
  790. goto out;
  791. in_dev = in_dev_get(dev);
  792. if (!in_dev)
  793. goto out;
  794. rcu_read_lock();
  795. if (in_dev->ifa_list &&
  796. IN_DEV_LOG_MARTIANS(in_dev) &&
  797. IN_DEV_FORWARD(in_dev)) {
  798. u32 _mask, *mp;
  799. mp = skb_header_pointer(skb, 0, sizeof(_mask), &_mask);
  800. BUG_ON(mp == NULL);
  801. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  802. if (*mp == ifa->ifa_mask &&
  803. inet_ifa_match(rt->rt_src, ifa))
  804. break;
  805. }
  806. if (!ifa && net_ratelimit()) {
  807. printk(KERN_INFO "Wrong address mask %u.%u.%u.%u from "
  808. "%s/%u.%u.%u.%u\n",
  809. NIPQUAD(*mp), dev->name, NIPQUAD(rt->rt_src));
  810. }
  811. }
  812. rcu_read_unlock();
  813. in_dev_put(in_dev);
  814. out:;
  815. }
  816. static void icmp_discard(struct sk_buff *skb)
  817. {
  818. }
  819. /*
  820. * Deal with incoming ICMP packets.
  821. */
  822. int icmp_rcv(struct sk_buff *skb)
  823. {
  824. struct icmphdr *icmph;
  825. struct rtable *rt = (struct rtable *)skb->dst;
  826. ICMP_INC_STATS_BH(ICMP_MIB_INMSGS);
  827. switch (skb->ip_summed) {
  828. case CHECKSUM_HW:
  829. if (!(u16)csum_fold(skb->csum))
  830. break;
  831. /* fall through */
  832. case CHECKSUM_NONE:
  833. skb->csum = 0;
  834. if (__skb_checksum_complete(skb))
  835. goto error;
  836. }
  837. if (!pskb_pull(skb, sizeof(struct icmphdr)))
  838. goto error;
  839. icmph = skb->h.icmph;
  840. /*
  841. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  842. *
  843. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  844. * discarded.
  845. */
  846. if (icmph->type > NR_ICMP_TYPES)
  847. goto error;
  848. /*
  849. * Parse the ICMP message
  850. */
  851. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  852. /*
  853. * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
  854. * silently ignored (we let user decide with a sysctl).
  855. * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
  856. * discarded if to broadcast/multicast.
  857. */
  858. if ((icmph->type == ICMP_ECHO ||
  859. icmph->type == ICMP_TIMESTAMP) &&
  860. sysctl_icmp_echo_ignore_broadcasts) {
  861. goto error;
  862. }
  863. if (icmph->type != ICMP_ECHO &&
  864. icmph->type != ICMP_TIMESTAMP &&
  865. icmph->type != ICMP_ADDRESS &&
  866. icmph->type != ICMP_ADDRESSREPLY) {
  867. goto error;
  868. }
  869. }
  870. ICMP_INC_STATS_BH(icmp_pointers[icmph->type].input_entry);
  871. icmp_pointers[icmph->type].handler(skb);
  872. drop:
  873. kfree_skb(skb);
  874. return 0;
  875. error:
  876. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  877. goto drop;
  878. }
  879. /*
  880. * This table is the definition of how we handle ICMP.
  881. */
  882. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
  883. [ICMP_ECHOREPLY] = {
  884. .output_entry = ICMP_MIB_OUTECHOREPS,
  885. .input_entry = ICMP_MIB_INECHOREPS,
  886. .handler = icmp_discard,
  887. },
  888. [1] = {
  889. .output_entry = ICMP_MIB_DUMMY,
  890. .input_entry = ICMP_MIB_INERRORS,
  891. .handler = icmp_discard,
  892. .error = 1,
  893. },
  894. [2] = {
  895. .output_entry = ICMP_MIB_DUMMY,
  896. .input_entry = ICMP_MIB_INERRORS,
  897. .handler = icmp_discard,
  898. .error = 1,
  899. },
  900. [ICMP_DEST_UNREACH] = {
  901. .output_entry = ICMP_MIB_OUTDESTUNREACHS,
  902. .input_entry = ICMP_MIB_INDESTUNREACHS,
  903. .handler = icmp_unreach,
  904. .error = 1,
  905. },
  906. [ICMP_SOURCE_QUENCH] = {
  907. .output_entry = ICMP_MIB_OUTSRCQUENCHS,
  908. .input_entry = ICMP_MIB_INSRCQUENCHS,
  909. .handler = icmp_unreach,
  910. .error = 1,
  911. },
  912. [ICMP_REDIRECT] = {
  913. .output_entry = ICMP_MIB_OUTREDIRECTS,
  914. .input_entry = ICMP_MIB_INREDIRECTS,
  915. .handler = icmp_redirect,
  916. .error = 1,
  917. },
  918. [6] = {
  919. .output_entry = ICMP_MIB_DUMMY,
  920. .input_entry = ICMP_MIB_INERRORS,
  921. .handler = icmp_discard,
  922. .error = 1,
  923. },
  924. [7] = {
  925. .output_entry = ICMP_MIB_DUMMY,
  926. .input_entry = ICMP_MIB_INERRORS,
  927. .handler = icmp_discard,
  928. .error = 1,
  929. },
  930. [ICMP_ECHO] = {
  931. .output_entry = ICMP_MIB_OUTECHOS,
  932. .input_entry = ICMP_MIB_INECHOS,
  933. .handler = icmp_echo,
  934. },
  935. [9] = {
  936. .output_entry = ICMP_MIB_DUMMY,
  937. .input_entry = ICMP_MIB_INERRORS,
  938. .handler = icmp_discard,
  939. .error = 1,
  940. },
  941. [10] = {
  942. .output_entry = ICMP_MIB_DUMMY,
  943. .input_entry = ICMP_MIB_INERRORS,
  944. .handler = icmp_discard,
  945. .error = 1,
  946. },
  947. [ICMP_TIME_EXCEEDED] = {
  948. .output_entry = ICMP_MIB_OUTTIMEEXCDS,
  949. .input_entry = ICMP_MIB_INTIMEEXCDS,
  950. .handler = icmp_unreach,
  951. .error = 1,
  952. },
  953. [ICMP_PARAMETERPROB] = {
  954. .output_entry = ICMP_MIB_OUTPARMPROBS,
  955. .input_entry = ICMP_MIB_INPARMPROBS,
  956. .handler = icmp_unreach,
  957. .error = 1,
  958. },
  959. [ICMP_TIMESTAMP] = {
  960. .output_entry = ICMP_MIB_OUTTIMESTAMPS,
  961. .input_entry = ICMP_MIB_INTIMESTAMPS,
  962. .handler = icmp_timestamp,
  963. },
  964. [ICMP_TIMESTAMPREPLY] = {
  965. .output_entry = ICMP_MIB_OUTTIMESTAMPREPS,
  966. .input_entry = ICMP_MIB_INTIMESTAMPREPS,
  967. .handler = icmp_discard,
  968. },
  969. [ICMP_INFO_REQUEST] = {
  970. .output_entry = ICMP_MIB_DUMMY,
  971. .input_entry = ICMP_MIB_DUMMY,
  972. .handler = icmp_discard,
  973. },
  974. [ICMP_INFO_REPLY] = {
  975. .output_entry = ICMP_MIB_DUMMY,
  976. .input_entry = ICMP_MIB_DUMMY,
  977. .handler = icmp_discard,
  978. },
  979. [ICMP_ADDRESS] = {
  980. .output_entry = ICMP_MIB_OUTADDRMASKS,
  981. .input_entry = ICMP_MIB_INADDRMASKS,
  982. .handler = icmp_address,
  983. },
  984. [ICMP_ADDRESSREPLY] = {
  985. .output_entry = ICMP_MIB_OUTADDRMASKREPS,
  986. .input_entry = ICMP_MIB_INADDRMASKREPS,
  987. .handler = icmp_address_reply,
  988. },
  989. };
  990. void __init icmp_init(struct net_proto_family *ops)
  991. {
  992. struct inet_sock *inet;
  993. int i;
  994. for_each_cpu(i) {
  995. int err;
  996. err = sock_create_kern(PF_INET, SOCK_RAW, IPPROTO_ICMP,
  997. &per_cpu(__icmp_socket, i));
  998. if (err < 0)
  999. panic("Failed to create the ICMP control socket.\n");
  1000. per_cpu(__icmp_socket, i)->sk->sk_allocation = GFP_ATOMIC;
  1001. /* Enough space for 2 64K ICMP packets, including
  1002. * sk_buff struct overhead.
  1003. */
  1004. per_cpu(__icmp_socket, i)->sk->sk_sndbuf =
  1005. (2 * ((64 * 1024) + sizeof(struct sk_buff)));
  1006. inet = inet_sk(per_cpu(__icmp_socket, i)->sk);
  1007. inet->uc_ttl = -1;
  1008. inet->pmtudisc = IP_PMTUDISC_DONT;
  1009. /* Unhash it so that IP input processing does not even
  1010. * see it, we do not wish this socket to see incoming
  1011. * packets.
  1012. */
  1013. per_cpu(__icmp_socket, i)->sk->sk_prot->unhash(per_cpu(__icmp_socket, i)->sk);
  1014. }
  1015. }
  1016. EXPORT_SYMBOL(icmp_err_convert);
  1017. EXPORT_SYMBOL(icmp_send);
  1018. EXPORT_SYMBOL(icmp_statistics);
  1019. EXPORT_SYMBOL(xrlim_allow);