icmp.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. DEFINE_SNMP_STAT(struct icmpmsg_mib, icmpmsg_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 __read_mostly;
  184. int sysctl_icmp_echo_ignore_broadcasts __read_mostly = 1;
  185. /* Control parameter - ignore bogus broadcast responses? */
  186. int sysctl_icmp_ignore_bogus_error_responses __read_mostly = 1;
  187. /*
  188. * Configurable global rate limit.
  189. *
  190. * ratelimit defines tokens/packet consumed for dst->rate_token bucket
  191. * ratemask defines which icmp types are ratelimited by setting
  192. * it's bit position.
  193. *
  194. * default:
  195. * dest unreachable (3), source quench (4),
  196. * time exceeded (11), parameter problem (12)
  197. */
  198. int sysctl_icmp_ratelimit __read_mostly = 1 * HZ;
  199. int sysctl_icmp_ratemask __read_mostly = 0x1818;
  200. int sysctl_icmp_errors_use_inbound_ifaddr __read_mostly;
  201. /*
  202. * ICMP control array. This specifies what to do with each ICMP.
  203. */
  204. struct icmp_control {
  205. void (*handler)(struct sk_buff *skb);
  206. short error; /* This ICMP is classed as an error message */
  207. };
  208. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
  209. /*
  210. * The ICMP socket(s). This is the most convenient way to flow control
  211. * our ICMP output as well as maintain a clean interface throughout
  212. * all layers. All Socketless IP sends will soon be gone.
  213. *
  214. * On SMP we have one ICMP socket per-cpu.
  215. */
  216. static DEFINE_PER_CPU(struct socket *, __icmp_socket) = NULL;
  217. #define icmp_socket __get_cpu_var(__icmp_socket)
  218. static __inline__ int icmp_xmit_lock(void)
  219. {
  220. local_bh_disable();
  221. if (unlikely(!spin_trylock(&icmp_socket->sk->sk_lock.slock))) {
  222. /* This can happen if the output path signals a
  223. * dst_link_failure() for an outgoing ICMP packet.
  224. */
  225. local_bh_enable();
  226. return 1;
  227. }
  228. return 0;
  229. }
  230. static void icmp_xmit_unlock(void)
  231. {
  232. spin_unlock_bh(&icmp_socket->sk->sk_lock.slock);
  233. }
  234. /*
  235. * Send an ICMP frame.
  236. */
  237. /*
  238. * Check transmit rate limitation for given message.
  239. * The rate information is held in the destination cache now.
  240. * This function is generic and could be used for other purposes
  241. * too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
  242. *
  243. * Note that the same dst_entry fields are modified by functions in
  244. * route.c too, but these work for packet destinations while xrlim_allow
  245. * works for icmp destinations. This means the rate limiting information
  246. * for one "ip object" is shared - and these ICMPs are twice limited:
  247. * by source and by destination.
  248. *
  249. * RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
  250. * SHOULD allow setting of rate limits
  251. *
  252. * Shared between ICMPv4 and ICMPv6.
  253. */
  254. #define XRLIM_BURST_FACTOR 6
  255. int xrlim_allow(struct dst_entry *dst, int timeout)
  256. {
  257. unsigned long now;
  258. int rc = 0;
  259. now = jiffies;
  260. dst->rate_tokens += now - dst->rate_last;
  261. dst->rate_last = now;
  262. if (dst->rate_tokens > XRLIM_BURST_FACTOR * timeout)
  263. dst->rate_tokens = XRLIM_BURST_FACTOR * timeout;
  264. if (dst->rate_tokens >= timeout) {
  265. dst->rate_tokens -= timeout;
  266. rc = 1;
  267. }
  268. return rc;
  269. }
  270. static inline int icmpv4_xrlim_allow(struct rtable *rt, int type, int code)
  271. {
  272. struct dst_entry *dst = &rt->u.dst;
  273. int rc = 1;
  274. if (type > NR_ICMP_TYPES)
  275. goto out;
  276. /* Don't limit PMTU discovery. */
  277. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
  278. goto out;
  279. /* No rate limit on loopback */
  280. if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
  281. goto out;
  282. /* Limit if icmp type is enabled in ratemask. */
  283. if ((1 << type) & sysctl_icmp_ratemask)
  284. rc = xrlim_allow(dst, sysctl_icmp_ratelimit);
  285. out:
  286. return rc;
  287. }
  288. /*
  289. * Maintain the counters used in the SNMP statistics for outgoing ICMP
  290. */
  291. void icmp_out_count(unsigned char type)
  292. {
  293. ICMPMSGOUT_INC_STATS(type);
  294. ICMP_INC_STATS(ICMP_MIB_OUTMSGS);
  295. }
  296. /*
  297. * Checksum each fragment, and on the first include the headers and final
  298. * checksum.
  299. */
  300. static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
  301. struct sk_buff *skb)
  302. {
  303. struct icmp_bxm *icmp_param = (struct icmp_bxm *)from;
  304. __wsum csum;
  305. csum = skb_copy_and_csum_bits(icmp_param->skb,
  306. icmp_param->offset + offset,
  307. to, len, 0);
  308. skb->csum = csum_block_add(skb->csum, csum, odd);
  309. if (icmp_pointers[icmp_param->data.icmph.type].error)
  310. nf_ct_attach(skb, icmp_param->skb);
  311. return 0;
  312. }
  313. static void icmp_push_reply(struct icmp_bxm *icmp_param,
  314. struct ipcm_cookie *ipc, struct rtable *rt)
  315. {
  316. struct sk_buff *skb;
  317. if (ip_append_data(icmp_socket->sk, icmp_glue_bits, icmp_param,
  318. icmp_param->data_len+icmp_param->head_len,
  319. icmp_param->head_len,
  320. ipc, rt, MSG_DONTWAIT) < 0)
  321. ip_flush_pending_frames(icmp_socket->sk);
  322. else if ((skb = skb_peek(&icmp_socket->sk->sk_write_queue)) != NULL) {
  323. struct icmphdr *icmph = icmp_hdr(skb);
  324. __wsum csum = 0;
  325. struct sk_buff *skb1;
  326. skb_queue_walk(&icmp_socket->sk->sk_write_queue, skb1) {
  327. csum = csum_add(csum, skb1->csum);
  328. }
  329. csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
  330. (char *)icmph,
  331. icmp_param->head_len, csum);
  332. icmph->checksum = csum_fold(csum);
  333. skb->ip_summed = CHECKSUM_NONE;
  334. ip_push_pending_frames(icmp_socket->sk);
  335. }
  336. }
  337. /*
  338. * Driving logic for building and sending ICMP messages.
  339. */
  340. static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
  341. {
  342. struct sock *sk = icmp_socket->sk;
  343. struct inet_sock *inet = inet_sk(sk);
  344. struct ipcm_cookie ipc;
  345. struct rtable *rt = (struct rtable *)skb->dst;
  346. __be32 daddr;
  347. if (ip_options_echo(&icmp_param->replyopts, skb))
  348. return;
  349. if (icmp_xmit_lock())
  350. return;
  351. icmp_param->data.icmph.checksum = 0;
  352. inet->tos = ip_hdr(skb)->tos;
  353. daddr = ipc.addr = rt->rt_src;
  354. ipc.opt = NULL;
  355. if (icmp_param->replyopts.optlen) {
  356. ipc.opt = &icmp_param->replyopts;
  357. if (ipc.opt->srr)
  358. daddr = icmp_param->replyopts.faddr;
  359. }
  360. {
  361. struct flowi fl = { .nl_u = { .ip4_u =
  362. { .daddr = daddr,
  363. .saddr = rt->rt_spec_dst,
  364. .tos = RT_TOS(ip_hdr(skb)->tos) } },
  365. .proto = IPPROTO_ICMP };
  366. security_skb_classify_flow(skb, &fl);
  367. if (ip_route_output_key(&rt, &fl))
  368. goto out_unlock;
  369. }
  370. if (icmpv4_xrlim_allow(rt, icmp_param->data.icmph.type,
  371. icmp_param->data.icmph.code))
  372. icmp_push_reply(icmp_param, &ipc, rt);
  373. ip_rt_put(rt);
  374. out_unlock:
  375. icmp_xmit_unlock();
  376. }
  377. /*
  378. * Send an ICMP message in response to a situation
  379. *
  380. * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
  381. * MAY send more (we do).
  382. * MUST NOT change this header information.
  383. * MUST NOT reply to a multicast/broadcast IP address.
  384. * MUST NOT reply to a multicast/broadcast MAC address.
  385. * MUST reply to only the first fragment.
  386. */
  387. void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
  388. {
  389. struct iphdr *iph;
  390. int room;
  391. struct icmp_bxm icmp_param;
  392. struct rtable *rt = (struct rtable *)skb_in->dst;
  393. struct ipcm_cookie ipc;
  394. __be32 saddr;
  395. u8 tos;
  396. if (!rt)
  397. goto out;
  398. /*
  399. * Find the original header. It is expected to be valid, of course.
  400. * Check this, icmp_send is called from the most obscure devices
  401. * sometimes.
  402. */
  403. iph = ip_hdr(skb_in);
  404. if ((u8 *)iph < skb_in->head ||
  405. (skb_in->network_header + sizeof(*iph)) > skb_in->tail)
  406. goto out;
  407. /*
  408. * No replies to physical multicast/broadcast
  409. */
  410. if (skb_in->pkt_type != PACKET_HOST)
  411. goto out;
  412. /*
  413. * Now check at the protocol level
  414. */
  415. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  416. goto out;
  417. /*
  418. * Only reply to fragment 0. We byte re-order the constant
  419. * mask for efficiency.
  420. */
  421. if (iph->frag_off & htons(IP_OFFSET))
  422. goto out;
  423. /*
  424. * If we send an ICMP error to an ICMP error a mess would result..
  425. */
  426. if (icmp_pointers[type].error) {
  427. /*
  428. * We are an error, check if we are replying to an
  429. * ICMP error
  430. */
  431. if (iph->protocol == IPPROTO_ICMP) {
  432. u8 _inner_type, *itp;
  433. itp = skb_header_pointer(skb_in,
  434. skb_network_header(skb_in) +
  435. (iph->ihl << 2) +
  436. offsetof(struct icmphdr,
  437. type) -
  438. skb_in->data,
  439. sizeof(_inner_type),
  440. &_inner_type);
  441. if (itp == NULL)
  442. goto out;
  443. /*
  444. * Assume any unknown ICMP type is an error. This
  445. * isn't specified by the RFC, but think about it..
  446. */
  447. if (*itp > NR_ICMP_TYPES ||
  448. icmp_pointers[*itp].error)
  449. goto out;
  450. }
  451. }
  452. if (icmp_xmit_lock())
  453. return;
  454. /*
  455. * Construct source address and options.
  456. */
  457. saddr = iph->daddr;
  458. if (!(rt->rt_flags & RTCF_LOCAL)) {
  459. struct net_device *dev = NULL;
  460. if (rt->fl.iif && sysctl_icmp_errors_use_inbound_ifaddr)
  461. dev = dev_get_by_index(&init_net, rt->fl.iif);
  462. if (dev) {
  463. saddr = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  464. dev_put(dev);
  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 out_unlock;
  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_network_offset(skb_in);
  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. security_skb_classify_flow(skb_in, &fl);
  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 = icmp_hdr(skb);
  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(ip_hdr(skb)->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. if (skb->len < sizeof(struct iphdr))
  650. goto out_err;
  651. /*
  652. * Get the copied header of the packet that caused the redirect
  653. */
  654. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  655. goto out;
  656. iph = (struct iphdr *)skb->data;
  657. switch (icmp_hdr(skb)->code & 7) {
  658. case ICMP_REDIR_NET:
  659. case ICMP_REDIR_NETTOS:
  660. /*
  661. * As per RFC recommendations now handle it as a host redirect.
  662. */
  663. case ICMP_REDIR_HOST:
  664. case ICMP_REDIR_HOSTTOS:
  665. ip_rt_redirect(ip_hdr(skb)->saddr, iph->daddr,
  666. icmp_hdr(skb)->un.gateway,
  667. iph->saddr, skb->dev);
  668. break;
  669. }
  670. out:
  671. return;
  672. out_err:
  673. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  674. goto out;
  675. }
  676. /*
  677. * Handle ICMP_ECHO ("ping") requests.
  678. *
  679. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  680. * requests.
  681. * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
  682. * included in the reply.
  683. * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
  684. * echo requests, MUST have default=NOT.
  685. * See also WRT handling of options once they are done and working.
  686. */
  687. static void icmp_echo(struct sk_buff *skb)
  688. {
  689. if (!sysctl_icmp_echo_ignore_all) {
  690. struct icmp_bxm icmp_param;
  691. icmp_param.data.icmph = *icmp_hdr(skb);
  692. icmp_param.data.icmph.type = ICMP_ECHOREPLY;
  693. icmp_param.skb = skb;
  694. icmp_param.offset = 0;
  695. icmp_param.data_len = skb->len;
  696. icmp_param.head_len = sizeof(struct icmphdr);
  697. icmp_reply(&icmp_param, skb);
  698. }
  699. }
  700. /*
  701. * Handle ICMP Timestamp requests.
  702. * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
  703. * SHOULD be in the kernel for minimum random latency.
  704. * MUST be accurate to a few minutes.
  705. * MUST be updated at least at 15Hz.
  706. */
  707. static void icmp_timestamp(struct sk_buff *skb)
  708. {
  709. struct timeval tv;
  710. struct icmp_bxm icmp_param;
  711. /*
  712. * Too short.
  713. */
  714. if (skb->len < 4)
  715. goto out_err;
  716. /*
  717. * Fill in the current time as ms since midnight UT:
  718. */
  719. do_gettimeofday(&tv);
  720. icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * 1000 +
  721. tv.tv_usec / 1000);
  722. icmp_param.data.times[2] = icmp_param.data.times[1];
  723. if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
  724. BUG();
  725. icmp_param.data.icmph = *icmp_hdr(skb);
  726. icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
  727. icmp_param.data.icmph.code = 0;
  728. icmp_param.skb = skb;
  729. icmp_param.offset = 0;
  730. icmp_param.data_len = 0;
  731. icmp_param.head_len = sizeof(struct icmphdr) + 12;
  732. icmp_reply(&icmp_param, skb);
  733. out:
  734. return;
  735. out_err:
  736. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  737. goto out;
  738. }
  739. /*
  740. * Handle ICMP_ADDRESS_MASK requests. (RFC950)
  741. *
  742. * RFC1122 (3.2.2.9). A host MUST only send replies to
  743. * ADDRESS_MASK requests if it's been configured as an address mask
  744. * agent. Receiving a request doesn't constitute implicit permission to
  745. * act as one. Of course, implementing this correctly requires (SHOULD)
  746. * a way to turn the functionality on and off. Another one for sysctl(),
  747. * I guess. -- MS
  748. *
  749. * RFC1812 (4.3.3.9). A router MUST implement it.
  750. * A router SHOULD have switch turning it on/off.
  751. * This switch MUST be ON by default.
  752. *
  753. * Gratuitous replies, zero-source replies are not implemented,
  754. * that complies with RFC. DO NOT implement them!!! All the idea
  755. * of broadcast addrmask replies as specified in RFC950 is broken.
  756. * The problem is that it is not uncommon to have several prefixes
  757. * on one physical interface. Moreover, addrmask agent can even be
  758. * not aware of existing another prefixes.
  759. * If source is zero, addrmask agent cannot choose correct prefix.
  760. * Gratuitous mask announcements suffer from the same problem.
  761. * RFC1812 explains it, but still allows to use ADDRMASK,
  762. * that is pretty silly. --ANK
  763. *
  764. * All these rules are so bizarre, that I removed kernel addrmask
  765. * support at all. It is wrong, it is obsolete, nobody uses it in
  766. * any case. --ANK
  767. *
  768. * Furthermore you can do it with a usermode address agent program
  769. * anyway...
  770. */
  771. static void icmp_address(struct sk_buff *skb)
  772. {
  773. #if 0
  774. if (net_ratelimit())
  775. printk(KERN_DEBUG "a guy asks for address mask. Who is it?\n");
  776. #endif
  777. }
  778. /*
  779. * RFC1812 (4.3.3.9). A router SHOULD listen all replies, and complain
  780. * loudly if an inconsistency is found.
  781. */
  782. static void icmp_address_reply(struct sk_buff *skb)
  783. {
  784. struct rtable *rt = (struct rtable *)skb->dst;
  785. struct net_device *dev = skb->dev;
  786. struct in_device *in_dev;
  787. struct in_ifaddr *ifa;
  788. if (skb->len < 4 || !(rt->rt_flags&RTCF_DIRECTSRC))
  789. goto out;
  790. in_dev = in_dev_get(dev);
  791. if (!in_dev)
  792. goto out;
  793. rcu_read_lock();
  794. if (in_dev->ifa_list &&
  795. IN_DEV_LOG_MARTIANS(in_dev) &&
  796. IN_DEV_FORWARD(in_dev)) {
  797. __be32 _mask, *mp;
  798. mp = skb_header_pointer(skb, 0, sizeof(_mask), &_mask);
  799. BUG_ON(mp == NULL);
  800. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  801. if (*mp == ifa->ifa_mask &&
  802. inet_ifa_match(rt->rt_src, ifa))
  803. break;
  804. }
  805. if (!ifa && net_ratelimit()) {
  806. printk(KERN_INFO "Wrong address mask %u.%u.%u.%u from "
  807. "%s/%u.%u.%u.%u\n",
  808. NIPQUAD(*mp), dev->name, NIPQUAD(rt->rt_src));
  809. }
  810. }
  811. rcu_read_unlock();
  812. in_dev_put(in_dev);
  813. out:;
  814. }
  815. static void icmp_discard(struct sk_buff *skb)
  816. {
  817. }
  818. /*
  819. * Deal with incoming ICMP packets.
  820. */
  821. int icmp_rcv(struct sk_buff *skb)
  822. {
  823. struct icmphdr *icmph;
  824. struct rtable *rt = (struct rtable *)skb->dst;
  825. ICMP_INC_STATS_BH(ICMP_MIB_INMSGS);
  826. switch (skb->ip_summed) {
  827. case CHECKSUM_COMPLETE:
  828. if (!csum_fold(skb->csum))
  829. break;
  830. /* fall through */
  831. case CHECKSUM_NONE:
  832. skb->csum = 0;
  833. if (__skb_checksum_complete(skb))
  834. goto error;
  835. }
  836. if (!pskb_pull(skb, sizeof(struct icmphdr)))
  837. goto error;
  838. icmph = icmp_hdr(skb);
  839. ICMPMSGIN_INC_STATS_BH(icmph->type);
  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_pointers[icmph->type].handler(skb);
  871. drop:
  872. kfree_skb(skb);
  873. return 0;
  874. error:
  875. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  876. goto drop;
  877. }
  878. /*
  879. * This table is the definition of how we handle ICMP.
  880. */
  881. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
  882. [ICMP_ECHOREPLY] = {
  883. .handler = icmp_discard,
  884. },
  885. [1] = {
  886. .handler = icmp_discard,
  887. .error = 1,
  888. },
  889. [2] = {
  890. .handler = icmp_discard,
  891. .error = 1,
  892. },
  893. [ICMP_DEST_UNREACH] = {
  894. .handler = icmp_unreach,
  895. .error = 1,
  896. },
  897. [ICMP_SOURCE_QUENCH] = {
  898. .handler = icmp_unreach,
  899. .error = 1,
  900. },
  901. [ICMP_REDIRECT] = {
  902. .handler = icmp_redirect,
  903. .error = 1,
  904. },
  905. [6] = {
  906. .handler = icmp_discard,
  907. .error = 1,
  908. },
  909. [7] = {
  910. .handler = icmp_discard,
  911. .error = 1,
  912. },
  913. [ICMP_ECHO] = {
  914. .handler = icmp_echo,
  915. },
  916. [9] = {
  917. .handler = icmp_discard,
  918. .error = 1,
  919. },
  920. [10] = {
  921. .handler = icmp_discard,
  922. .error = 1,
  923. },
  924. [ICMP_TIME_EXCEEDED] = {
  925. .handler = icmp_unreach,
  926. .error = 1,
  927. },
  928. [ICMP_PARAMETERPROB] = {
  929. .handler = icmp_unreach,
  930. .error = 1,
  931. },
  932. [ICMP_TIMESTAMP] = {
  933. .handler = icmp_timestamp,
  934. },
  935. [ICMP_TIMESTAMPREPLY] = {
  936. .handler = icmp_discard,
  937. },
  938. [ICMP_INFO_REQUEST] = {
  939. .handler = icmp_discard,
  940. },
  941. [ICMP_INFO_REPLY] = {
  942. .handler = icmp_discard,
  943. },
  944. [ICMP_ADDRESS] = {
  945. .handler = icmp_address,
  946. },
  947. [ICMP_ADDRESSREPLY] = {
  948. .handler = icmp_address_reply,
  949. },
  950. };
  951. void __init icmp_init(struct net_proto_family *ops)
  952. {
  953. struct inet_sock *inet;
  954. int i;
  955. for_each_possible_cpu(i) {
  956. int err;
  957. err = sock_create_kern(PF_INET, SOCK_RAW, IPPROTO_ICMP,
  958. &per_cpu(__icmp_socket, i));
  959. if (err < 0)
  960. panic("Failed to create the ICMP control socket.\n");
  961. per_cpu(__icmp_socket, i)->sk->sk_allocation = GFP_ATOMIC;
  962. /* Enough space for 2 64K ICMP packets, including
  963. * sk_buff struct overhead.
  964. */
  965. per_cpu(__icmp_socket, i)->sk->sk_sndbuf =
  966. (2 * ((64 * 1024) + sizeof(struct sk_buff)));
  967. inet = inet_sk(per_cpu(__icmp_socket, i)->sk);
  968. inet->uc_ttl = -1;
  969. inet->pmtudisc = IP_PMTUDISC_DONT;
  970. /* Unhash it so that IP input processing does not even
  971. * see it, we do not wish this socket to see incoming
  972. * packets.
  973. */
  974. per_cpu(__icmp_socket, i)->sk->sk_prot->unhash(per_cpu(__icmp_socket, i)->sk);
  975. }
  976. }
  977. EXPORT_SYMBOL(icmp_err_convert);
  978. EXPORT_SYMBOL(icmp_send);
  979. EXPORT_SYMBOL(icmp_statistics);
  980. EXPORT_SYMBOL(xrlim_allow);