icmp.c 29 KB

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