icmp.c 28 KB

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