icmp.c 29 KB

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