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