icmp.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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/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. __u32 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);
  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;
  183. int sysctl_icmp_echo_ignore_broadcasts;
  184. /* Control parameter - ignore bogus broadcast responses? */
  185. int sysctl_icmp_ignore_bogus_error_responses;
  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 = 1 * HZ;
  198. int sysctl_icmp_ratemask = 0x1818;
  199. int sysctl_icmp_errors_use_inbound_ifaddr;
  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 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. unsigned int 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 = skb->h.icmph;
  327. unsigned int 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. u32 daddr;
  350. if (ip_options_echo(&icmp_param->replyopts, skb))
  351. goto out;
  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 = skb->nh.iph->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(skb->nh.iph->tos) } },
  369. .proto = IPPROTO_ICMP };
  370. if (ip_route_output_key(&rt, &fl))
  371. goto out_unlock;
  372. }
  373. if (icmpv4_xrlim_allow(rt, icmp_param->data.icmph.type,
  374. icmp_param->data.icmph.code))
  375. icmp_push_reply(icmp_param, &ipc, rt);
  376. ip_rt_put(rt);
  377. out_unlock:
  378. icmp_xmit_unlock();
  379. out:;
  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 ende;
  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(
  558. printk(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(
  571. printk(KERN_INFO "ICMP: %u.%u.%u.%u: Source "
  572. "Route Failed.\n",
  573. NIPQUAD(iph->daddr)));
  574. break;
  575. default:
  576. break;
  577. }
  578. if (icmph->code > NR_ICMP_UNREACH)
  579. goto out;
  580. } else if (icmph->type == ICMP_PARAMETERPROB)
  581. info = ntohl(icmph->un.gateway) >> 24;
  582. /*
  583. * Throw it at our lower layers
  584. *
  585. * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
  586. * header.
  587. * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
  588. * transport layer.
  589. * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
  590. * transport layer.
  591. */
  592. /*
  593. * Check the other end isnt violating RFC 1122. Some routers send
  594. * bogus responses to broadcast frames. If you see this message
  595. * first check your netmask matches at both ends, if it does then
  596. * get the other vendor to fix their kit.
  597. */
  598. if (!sysctl_icmp_ignore_bogus_error_responses &&
  599. inet_addr_type(iph->daddr) == RTN_BROADCAST) {
  600. if (net_ratelimit())
  601. printk(KERN_WARNING "%u.%u.%u.%u sent an invalid ICMP "
  602. "type %u, code %u "
  603. "error to a broadcast: %u.%u.%u.%u on %s\n",
  604. NIPQUAD(skb->nh.iph->saddr),
  605. icmph->type, icmph->code,
  606. NIPQUAD(iph->daddr),
  607. skb->dev->name);
  608. goto out;
  609. }
  610. /* Checkin full IP header plus 8 bytes of protocol to
  611. * avoid additional coding at protocol handlers.
  612. */
  613. if (!pskb_may_pull(skb, iph->ihl * 4 + 8))
  614. goto out;
  615. iph = (struct iphdr *)skb->data;
  616. protocol = iph->protocol;
  617. /*
  618. * Deliver ICMP message to raw sockets. Pretty useless feature?
  619. */
  620. /* Note: See raw.c and net/raw.h, RAWV4_HTABLE_SIZE==MAX_INET_PROTOS */
  621. hash = protocol & (MAX_INET_PROTOS - 1);
  622. read_lock(&raw_v4_lock);
  623. if ((raw_sk = sk_head(&raw_v4_htable[hash])) != NULL) {
  624. while ((raw_sk = __raw_v4_lookup(raw_sk, protocol, iph->daddr,
  625. iph->saddr,
  626. skb->dev->ifindex)) != NULL) {
  627. raw_err(raw_sk, skb, info);
  628. raw_sk = sk_next(raw_sk);
  629. iph = (struct iphdr *)skb->data;
  630. }
  631. }
  632. read_unlock(&raw_v4_lock);
  633. rcu_read_lock();
  634. ipprot = rcu_dereference(inet_protos[hash]);
  635. if (ipprot && ipprot->err_handler)
  636. ipprot->err_handler(skb, info);
  637. rcu_read_unlock();
  638. out:
  639. return;
  640. out_err:
  641. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  642. goto out;
  643. }
  644. /*
  645. * Handle ICMP_REDIRECT.
  646. */
  647. static void icmp_redirect(struct sk_buff *skb)
  648. {
  649. struct iphdr *iph;
  650. unsigned long ip;
  651. if (skb->len < sizeof(struct iphdr))
  652. goto out_err;
  653. /*
  654. * Get the copied header of the packet that caused the redirect
  655. */
  656. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  657. goto out;
  658. iph = (struct iphdr *)skb->data;
  659. ip = iph->daddr;
  660. switch (skb->h.icmph->code & 7) {
  661. case ICMP_REDIR_NET:
  662. case ICMP_REDIR_NETTOS:
  663. /*
  664. * As per RFC recommendations now handle it as a host redirect.
  665. */
  666. case ICMP_REDIR_HOST:
  667. case ICMP_REDIR_HOSTTOS:
  668. ip_rt_redirect(skb->nh.iph->saddr, ip, skb->h.icmph->un.gateway,
  669. iph->saddr, iph->tos, skb->dev);
  670. break;
  671. }
  672. out:
  673. return;
  674. out_err:
  675. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  676. goto out;
  677. }
  678. /*
  679. * Handle ICMP_ECHO ("ping") requests.
  680. *
  681. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  682. * requests.
  683. * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
  684. * included in the reply.
  685. * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
  686. * echo requests, MUST have default=NOT.
  687. * See also WRT handling of options once they are done and working.
  688. */
  689. static void icmp_echo(struct sk_buff *skb)
  690. {
  691. if (!sysctl_icmp_echo_ignore_all) {
  692. struct icmp_bxm icmp_param;
  693. icmp_param.data.icmph = *skb->h.icmph;
  694. icmp_param.data.icmph.type = ICMP_ECHOREPLY;
  695. icmp_param.skb = skb;
  696. icmp_param.offset = 0;
  697. icmp_param.data_len = skb->len;
  698. icmp_param.head_len = sizeof(struct icmphdr);
  699. icmp_reply(&icmp_param, skb);
  700. }
  701. }
  702. /*
  703. * Handle ICMP Timestamp requests.
  704. * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
  705. * SHOULD be in the kernel for minimum random latency.
  706. * MUST be accurate to a few minutes.
  707. * MUST be updated at least at 15Hz.
  708. */
  709. static void icmp_timestamp(struct sk_buff *skb)
  710. {
  711. struct timeval tv;
  712. struct icmp_bxm icmp_param;
  713. /*
  714. * Too short.
  715. */
  716. if (skb->len < 4)
  717. goto out_err;
  718. /*
  719. * Fill in the current time as ms since midnight UT:
  720. */
  721. do_gettimeofday(&tv);
  722. icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * 1000 +
  723. tv.tv_usec / 1000);
  724. icmp_param.data.times[2] = icmp_param.data.times[1];
  725. if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
  726. BUG();
  727. icmp_param.data.icmph = *skb->h.icmph;
  728. icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
  729. icmp_param.data.icmph.code = 0;
  730. icmp_param.skb = skb;
  731. icmp_param.offset = 0;
  732. icmp_param.data_len = 0;
  733. icmp_param.head_len = sizeof(struct icmphdr) + 12;
  734. icmp_reply(&icmp_param, skb);
  735. out:
  736. return;
  737. out_err:
  738. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  739. goto out;
  740. }
  741. /*
  742. * Handle ICMP_ADDRESS_MASK requests. (RFC950)
  743. *
  744. * RFC1122 (3.2.2.9). A host MUST only send replies to
  745. * ADDRESS_MASK requests if it's been configured as an address mask
  746. * agent. Receiving a request doesn't constitute implicit permission to
  747. * act as one. Of course, implementing this correctly requires (SHOULD)
  748. * a way to turn the functionality on and off. Another one for sysctl(),
  749. * I guess. -- MS
  750. *
  751. * RFC1812 (4.3.3.9). A router MUST implement it.
  752. * A router SHOULD have switch turning it on/off.
  753. * This switch MUST be ON by default.
  754. *
  755. * Gratuitous replies, zero-source replies are not implemented,
  756. * that complies with RFC. DO NOT implement them!!! All the idea
  757. * of broadcast addrmask replies as specified in RFC950 is broken.
  758. * The problem is that it is not uncommon to have several prefixes
  759. * on one physical interface. Moreover, addrmask agent can even be
  760. * not aware of existing another prefixes.
  761. * If source is zero, addrmask agent cannot choose correct prefix.
  762. * Gratuitous mask announcements suffer from the same problem.
  763. * RFC1812 explains it, but still allows to use ADDRMASK,
  764. * that is pretty silly. --ANK
  765. *
  766. * All these rules are so bizarre, that I removed kernel addrmask
  767. * support at all. It is wrong, it is obsolete, nobody uses it in
  768. * any case. --ANK
  769. *
  770. * Furthermore you can do it with a usermode address agent program
  771. * anyway...
  772. */
  773. static void icmp_address(struct sk_buff *skb)
  774. {
  775. #if 0
  776. if (net_ratelimit())
  777. printk(KERN_DEBUG "a guy asks for address mask. Who is it?\n");
  778. #endif
  779. }
  780. /*
  781. * RFC1812 (4.3.3.9). A router SHOULD listen all replies, and complain
  782. * loudly if an inconsistency is found.
  783. */
  784. static void icmp_address_reply(struct sk_buff *skb)
  785. {
  786. struct rtable *rt = (struct rtable *)skb->dst;
  787. struct net_device *dev = skb->dev;
  788. struct in_device *in_dev;
  789. struct in_ifaddr *ifa;
  790. if (skb->len < 4 || !(rt->rt_flags&RTCF_DIRECTSRC))
  791. goto out;
  792. in_dev = in_dev_get(dev);
  793. if (!in_dev)
  794. goto out;
  795. rcu_read_lock();
  796. if (in_dev->ifa_list &&
  797. IN_DEV_LOG_MARTIANS(in_dev) &&
  798. IN_DEV_FORWARD(in_dev)) {
  799. u32 _mask, *mp;
  800. mp = skb_header_pointer(skb, 0, sizeof(_mask), &_mask);
  801. if (mp == NULL)
  802. BUG();
  803. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  804. if (*mp == ifa->ifa_mask &&
  805. inet_ifa_match(rt->rt_src, ifa))
  806. break;
  807. }
  808. if (!ifa && net_ratelimit()) {
  809. printk(KERN_INFO "Wrong address mask %u.%u.%u.%u from "
  810. "%s/%u.%u.%u.%u\n",
  811. NIPQUAD(*mp), dev->name, NIPQUAD(rt->rt_src));
  812. }
  813. }
  814. rcu_read_unlock();
  815. in_dev_put(in_dev);
  816. out:;
  817. }
  818. static void icmp_discard(struct sk_buff *skb)
  819. {
  820. }
  821. /*
  822. * Deal with incoming ICMP packets.
  823. */
  824. int icmp_rcv(struct sk_buff *skb)
  825. {
  826. struct icmphdr *icmph;
  827. struct rtable *rt = (struct rtable *)skb->dst;
  828. ICMP_INC_STATS_BH(ICMP_MIB_INMSGS);
  829. switch (skb->ip_summed) {
  830. case CHECKSUM_HW:
  831. if (!(u16)csum_fold(skb->csum))
  832. break;
  833. LIMIT_NETDEBUG(printk(KERN_DEBUG "icmp v4 hw csum failure\n"));
  834. case CHECKSUM_NONE:
  835. if ((u16)csum_fold(skb_checksum(skb, 0, skb->len, 0)))
  836. goto error;
  837. default:;
  838. }
  839. if (!pskb_pull(skb, sizeof(struct icmphdr)))
  840. goto error;
  841. icmph = skb->h.icmph;
  842. /*
  843. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  844. *
  845. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  846. * discarded.
  847. */
  848. if (icmph->type > NR_ICMP_TYPES)
  849. goto error;
  850. /*
  851. * Parse the ICMP message
  852. */
  853. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  854. /*
  855. * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
  856. * silently ignored (we let user decide with a sysctl).
  857. * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
  858. * discarded if to broadcast/multicast.
  859. */
  860. if ((icmph->type == ICMP_ECHO ||
  861. icmph->type == ICMP_TIMESTAMP) &&
  862. sysctl_icmp_echo_ignore_broadcasts) {
  863. goto error;
  864. }
  865. if (icmph->type != ICMP_ECHO &&
  866. icmph->type != ICMP_TIMESTAMP &&
  867. icmph->type != ICMP_ADDRESS &&
  868. icmph->type != ICMP_ADDRESSREPLY) {
  869. goto error;
  870. }
  871. }
  872. ICMP_INC_STATS_BH(icmp_pointers[icmph->type].input_entry);
  873. icmp_pointers[icmph->type].handler(skb);
  874. drop:
  875. kfree_skb(skb);
  876. return 0;
  877. error:
  878. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  879. goto drop;
  880. }
  881. /*
  882. * This table is the definition of how we handle ICMP.
  883. */
  884. static struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
  885. [ICMP_ECHOREPLY] = {
  886. .output_entry = ICMP_MIB_OUTECHOREPS,
  887. .input_entry = ICMP_MIB_INECHOREPS,
  888. .handler = icmp_discard,
  889. },
  890. [1] = {
  891. .output_entry = ICMP_MIB_DUMMY,
  892. .input_entry = ICMP_MIB_INERRORS,
  893. .handler = icmp_discard,
  894. .error = 1,
  895. },
  896. [2] = {
  897. .output_entry = ICMP_MIB_DUMMY,
  898. .input_entry = ICMP_MIB_INERRORS,
  899. .handler = icmp_discard,
  900. .error = 1,
  901. },
  902. [ICMP_DEST_UNREACH] = {
  903. .output_entry = ICMP_MIB_OUTDESTUNREACHS,
  904. .input_entry = ICMP_MIB_INDESTUNREACHS,
  905. .handler = icmp_unreach,
  906. .error = 1,
  907. },
  908. [ICMP_SOURCE_QUENCH] = {
  909. .output_entry = ICMP_MIB_OUTSRCQUENCHS,
  910. .input_entry = ICMP_MIB_INSRCQUENCHS,
  911. .handler = icmp_unreach,
  912. .error = 1,
  913. },
  914. [ICMP_REDIRECT] = {
  915. .output_entry = ICMP_MIB_OUTREDIRECTS,
  916. .input_entry = ICMP_MIB_INREDIRECTS,
  917. .handler = icmp_redirect,
  918. .error = 1,
  919. },
  920. [6] = {
  921. .output_entry = ICMP_MIB_DUMMY,
  922. .input_entry = ICMP_MIB_INERRORS,
  923. .handler = icmp_discard,
  924. .error = 1,
  925. },
  926. [7] = {
  927. .output_entry = ICMP_MIB_DUMMY,
  928. .input_entry = ICMP_MIB_INERRORS,
  929. .handler = icmp_discard,
  930. .error = 1,
  931. },
  932. [ICMP_ECHO] = {
  933. .output_entry = ICMP_MIB_OUTECHOS,
  934. .input_entry = ICMP_MIB_INECHOS,
  935. .handler = icmp_echo,
  936. },
  937. [9] = {
  938. .output_entry = ICMP_MIB_DUMMY,
  939. .input_entry = ICMP_MIB_INERRORS,
  940. .handler = icmp_discard,
  941. .error = 1,
  942. },
  943. [10] = {
  944. .output_entry = ICMP_MIB_DUMMY,
  945. .input_entry = ICMP_MIB_INERRORS,
  946. .handler = icmp_discard,
  947. .error = 1,
  948. },
  949. [ICMP_TIME_EXCEEDED] = {
  950. .output_entry = ICMP_MIB_OUTTIMEEXCDS,
  951. .input_entry = ICMP_MIB_INTIMEEXCDS,
  952. .handler = icmp_unreach,
  953. .error = 1,
  954. },
  955. [ICMP_PARAMETERPROB] = {
  956. .output_entry = ICMP_MIB_OUTPARMPROBS,
  957. .input_entry = ICMP_MIB_INPARMPROBS,
  958. .handler = icmp_unreach,
  959. .error = 1,
  960. },
  961. [ICMP_TIMESTAMP] = {
  962. .output_entry = ICMP_MIB_OUTTIMESTAMPS,
  963. .input_entry = ICMP_MIB_INTIMESTAMPS,
  964. .handler = icmp_timestamp,
  965. },
  966. [ICMP_TIMESTAMPREPLY] = {
  967. .output_entry = ICMP_MIB_OUTTIMESTAMPREPS,
  968. .input_entry = ICMP_MIB_INTIMESTAMPREPS,
  969. .handler = icmp_discard,
  970. },
  971. [ICMP_INFO_REQUEST] = {
  972. .output_entry = ICMP_MIB_DUMMY,
  973. .input_entry = ICMP_MIB_DUMMY,
  974. .handler = icmp_discard,
  975. },
  976. [ICMP_INFO_REPLY] = {
  977. .output_entry = ICMP_MIB_DUMMY,
  978. .input_entry = ICMP_MIB_DUMMY,
  979. .handler = icmp_discard,
  980. },
  981. [ICMP_ADDRESS] = {
  982. .output_entry = ICMP_MIB_OUTADDRMASKS,
  983. .input_entry = ICMP_MIB_INADDRMASKS,
  984. .handler = icmp_address,
  985. },
  986. [ICMP_ADDRESSREPLY] = {
  987. .output_entry = ICMP_MIB_OUTADDRMASKREPS,
  988. .input_entry = ICMP_MIB_INADDRMASKREPS,
  989. .handler = icmp_address_reply,
  990. },
  991. };
  992. void __init icmp_init(struct net_proto_family *ops)
  993. {
  994. struct inet_sock *inet;
  995. int i;
  996. for (i = 0; i < NR_CPUS; i++) {
  997. int err;
  998. if (!cpu_possible(i))
  999. continue;
  1000. err = sock_create_kern(PF_INET, SOCK_RAW, IPPROTO_ICMP,
  1001. &per_cpu(__icmp_socket, i));
  1002. if (err < 0)
  1003. panic("Failed to create the ICMP control socket.\n");
  1004. per_cpu(__icmp_socket, i)->sk->sk_allocation = GFP_ATOMIC;
  1005. /* Enough space for 2 64K ICMP packets, including
  1006. * sk_buff struct overhead.
  1007. */
  1008. per_cpu(__icmp_socket, i)->sk->sk_sndbuf =
  1009. (2 * ((64 * 1024) + sizeof(struct sk_buff)));
  1010. inet = inet_sk(per_cpu(__icmp_socket, i)->sk);
  1011. inet->uc_ttl = -1;
  1012. inet->pmtudisc = IP_PMTUDISC_DONT;
  1013. /* Unhash it so that IP input processing does not even
  1014. * see it, we do not wish this socket to see incoming
  1015. * packets.
  1016. */
  1017. per_cpu(__icmp_socket, i)->sk->sk_prot->unhash(per_cpu(__icmp_socket, i)->sk);
  1018. }
  1019. }
  1020. EXPORT_SYMBOL(icmp_err_convert);
  1021. EXPORT_SYMBOL(icmp_send);
  1022. EXPORT_SYMBOL(icmp_statistics);
  1023. EXPORT_SYMBOL(xrlim_allow);