icmp.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  65. #include <linux/module.h>
  66. #include <linux/types.h>
  67. #include <linux/jiffies.h>
  68. #include <linux/kernel.h>
  69. #include <linux/fcntl.h>
  70. #include <linux/socket.h>
  71. #include <linux/in.h>
  72. #include <linux/inet.h>
  73. #include <linux/inetdevice.h>
  74. #include <linux/netdevice.h>
  75. #include <linux/string.h>
  76. #include <linux/netfilter_ipv4.h>
  77. #include <linux/slab.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 <net/ping.h>
  87. #include <linux/skbuff.h>
  88. #include <net/sock.h>
  89. #include <linux/errno.h>
  90. #include <linux/timer.h>
  91. #include <linux/init.h>
  92. #include <linux/uaccess.h>
  93. #include <net/checksum.h>
  94. #include <net/xfrm.h>
  95. #include <net/inet_common.h>
  96. #include <net/ip_fib.h>
  97. #include <net/l3mdev.h>
  98. /*
  99. * Build xmit assembly blocks
  100. */
  101. struct icmp_bxm {
  102. struct sk_buff *skb;
  103. int offset;
  104. int data_len;
  105. struct {
  106. struct icmphdr icmph;
  107. __be32 times[3];
  108. } data;
  109. int head_len;
  110. struct ip_options_data replyopts;
  111. };
  112. /* An array of errno for error messages from dest unreach. */
  113. /* RFC 1122: 3.2.2.1 States that NET_UNREACH, HOST_UNREACH and SR_FAILED MUST be considered 'transient errs'. */
  114. const struct icmp_err icmp_err_convert[] = {
  115. {
  116. .errno = ENETUNREACH, /* ICMP_NET_UNREACH */
  117. .fatal = 0,
  118. },
  119. {
  120. .errno = EHOSTUNREACH, /* ICMP_HOST_UNREACH */
  121. .fatal = 0,
  122. },
  123. {
  124. .errno = ENOPROTOOPT /* ICMP_PROT_UNREACH */,
  125. .fatal = 1,
  126. },
  127. {
  128. .errno = ECONNREFUSED, /* ICMP_PORT_UNREACH */
  129. .fatal = 1,
  130. },
  131. {
  132. .errno = EMSGSIZE, /* ICMP_FRAG_NEEDED */
  133. .fatal = 0,
  134. },
  135. {
  136. .errno = EOPNOTSUPP, /* ICMP_SR_FAILED */
  137. .fatal = 0,
  138. },
  139. {
  140. .errno = ENETUNREACH, /* ICMP_NET_UNKNOWN */
  141. .fatal = 1,
  142. },
  143. {
  144. .errno = EHOSTDOWN, /* ICMP_HOST_UNKNOWN */
  145. .fatal = 1,
  146. },
  147. {
  148. .errno = ENONET, /* ICMP_HOST_ISOLATED */
  149. .fatal = 1,
  150. },
  151. {
  152. .errno = ENETUNREACH, /* ICMP_NET_ANO */
  153. .fatal = 1,
  154. },
  155. {
  156. .errno = EHOSTUNREACH, /* ICMP_HOST_ANO */
  157. .fatal = 1,
  158. },
  159. {
  160. .errno = ENETUNREACH, /* ICMP_NET_UNR_TOS */
  161. .fatal = 0,
  162. },
  163. {
  164. .errno = EHOSTUNREACH, /* ICMP_HOST_UNR_TOS */
  165. .fatal = 0,
  166. },
  167. {
  168. .errno = EHOSTUNREACH, /* ICMP_PKT_FILTERED */
  169. .fatal = 1,
  170. },
  171. {
  172. .errno = EHOSTUNREACH, /* ICMP_PREC_VIOLATION */
  173. .fatal = 1,
  174. },
  175. {
  176. .errno = EHOSTUNREACH, /* ICMP_PREC_CUTOFF */
  177. .fatal = 1,
  178. },
  179. };
  180. EXPORT_SYMBOL(icmp_err_convert);
  181. /*
  182. * ICMP control array. This specifies what to do with each ICMP.
  183. */
  184. struct icmp_control {
  185. bool (*handler)(struct sk_buff *skb);
  186. short error; /* This ICMP is classed as an error message */
  187. };
  188. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
  189. /*
  190. * The ICMP socket(s). This is the most convenient way to flow control
  191. * our ICMP output as well as maintain a clean interface throughout
  192. * all layers. All Socketless IP sends will soon be gone.
  193. *
  194. * On SMP we have one ICMP socket per-cpu.
  195. */
  196. static struct sock *icmp_sk(struct net *net)
  197. {
  198. return *this_cpu_ptr(net->ipv4.icmp_sk);
  199. }
  200. /* Called with BH disabled */
  201. static inline struct sock *icmp_xmit_lock(struct net *net)
  202. {
  203. struct sock *sk;
  204. sk = icmp_sk(net);
  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. return NULL;
  210. }
  211. return sk;
  212. }
  213. static inline void icmp_xmit_unlock(struct sock *sk)
  214. {
  215. spin_unlock(&sk->sk_lock.slock);
  216. }
  217. int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
  218. int sysctl_icmp_msgs_burst __read_mostly = 50;
  219. static struct {
  220. spinlock_t lock;
  221. u32 credit;
  222. u32 stamp;
  223. } icmp_global = {
  224. .lock = __SPIN_LOCK_UNLOCKED(icmp_global.lock),
  225. };
  226. /**
  227. * icmp_global_allow - Are we allowed to send one more ICMP message ?
  228. *
  229. * Uses a token bucket to limit our ICMP messages to sysctl_icmp_msgs_per_sec.
  230. * Returns false if we reached the limit and can not send another packet.
  231. * Note: called with BH disabled
  232. */
  233. bool icmp_global_allow(void)
  234. {
  235. u32 credit, delta, incr = 0, now = (u32)jiffies;
  236. bool rc = false;
  237. /* Check if token bucket is empty and cannot be refilled
  238. * without taking the spinlock.
  239. */
  240. if (!icmp_global.credit) {
  241. delta = min_t(u32, now - icmp_global.stamp, HZ);
  242. if (delta < HZ / 50)
  243. return false;
  244. }
  245. spin_lock(&icmp_global.lock);
  246. delta = min_t(u32, now - icmp_global.stamp, HZ);
  247. if (delta >= HZ / 50) {
  248. incr = sysctl_icmp_msgs_per_sec * delta / HZ ;
  249. if (incr)
  250. icmp_global.stamp = now;
  251. }
  252. credit = min_t(u32, icmp_global.credit + incr, sysctl_icmp_msgs_burst);
  253. if (credit) {
  254. credit--;
  255. rc = true;
  256. }
  257. icmp_global.credit = credit;
  258. spin_unlock(&icmp_global.lock);
  259. return rc;
  260. }
  261. EXPORT_SYMBOL(icmp_global_allow);
  262. static bool icmpv4_mask_allow(struct net *net, int type, int code)
  263. {
  264. if (type > NR_ICMP_TYPES)
  265. return true;
  266. /* Don't limit PMTU discovery. */
  267. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
  268. return true;
  269. /* Limit if icmp type is enabled in ratemask. */
  270. if (!((1 << type) & net->ipv4.sysctl_icmp_ratemask))
  271. return true;
  272. return false;
  273. }
  274. static bool icmpv4_global_allow(struct net *net, int type, int code)
  275. {
  276. if (icmpv4_mask_allow(net, type, code))
  277. return true;
  278. if (icmp_global_allow())
  279. return true;
  280. return false;
  281. }
  282. /*
  283. * Send an ICMP frame.
  284. */
  285. static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
  286. struct flowi4 *fl4, int type, int code)
  287. {
  288. struct dst_entry *dst = &rt->dst;
  289. struct inet_peer *peer;
  290. bool rc = true;
  291. int vif;
  292. if (icmpv4_mask_allow(net, type, code))
  293. goto out;
  294. /* No rate limit on loopback */
  295. if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
  296. goto out;
  297. vif = l3mdev_master_ifindex(dst->dev);
  298. peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr, vif, 1);
  299. rc = inet_peer_xrlim_allow(peer, net->ipv4.sysctl_icmp_ratelimit);
  300. if (peer)
  301. inet_putpeer(peer);
  302. out:
  303. return rc;
  304. }
  305. /*
  306. * Maintain the counters used in the SNMP statistics for outgoing ICMP
  307. */
  308. void icmp_out_count(struct net *net, unsigned char type)
  309. {
  310. ICMPMSGOUT_INC_STATS(net, type);
  311. ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS);
  312. }
  313. /*
  314. * Checksum each fragment, and on the first include the headers and final
  315. * checksum.
  316. */
  317. static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
  318. struct sk_buff *skb)
  319. {
  320. struct icmp_bxm *icmp_param = (struct icmp_bxm *)from;
  321. __wsum csum;
  322. csum = skb_copy_and_csum_bits(icmp_param->skb,
  323. icmp_param->offset + offset,
  324. to, len, 0);
  325. skb->csum = csum_block_add(skb->csum, csum, odd);
  326. if (icmp_pointers[icmp_param->data.icmph.type].error)
  327. nf_ct_attach(skb, icmp_param->skb);
  328. return 0;
  329. }
  330. static void icmp_push_reply(struct icmp_bxm *icmp_param,
  331. struct flowi4 *fl4,
  332. struct ipcm_cookie *ipc, struct rtable **rt)
  333. {
  334. struct sock *sk;
  335. struct sk_buff *skb;
  336. sk = icmp_sk(dev_net((*rt)->dst.dev));
  337. if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
  338. icmp_param->data_len+icmp_param->head_len,
  339. icmp_param->head_len,
  340. ipc, rt, MSG_DONTWAIT) < 0) {
  341. __ICMP_INC_STATS(sock_net(sk), ICMP_MIB_OUTERRORS);
  342. ip_flush_pending_frames(sk);
  343. } else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
  344. struct icmphdr *icmph = icmp_hdr(skb);
  345. __wsum csum = 0;
  346. struct sk_buff *skb1;
  347. skb_queue_walk(&sk->sk_write_queue, skb1) {
  348. csum = csum_add(csum, skb1->csum);
  349. }
  350. csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
  351. (char *)icmph,
  352. icmp_param->head_len, csum);
  353. icmph->checksum = csum_fold(csum);
  354. skb->ip_summed = CHECKSUM_NONE;
  355. ip_push_pending_frames(sk, fl4);
  356. }
  357. }
  358. /*
  359. * Driving logic for building and sending ICMP messages.
  360. */
  361. static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
  362. {
  363. struct ipcm_cookie ipc;
  364. struct rtable *rt = skb_rtable(skb);
  365. struct net *net = dev_net(rt->dst.dev);
  366. struct flowi4 fl4;
  367. struct sock *sk;
  368. struct inet_sock *inet;
  369. __be32 daddr, saddr;
  370. u32 mark = IP4_REPLY_MARK(net, skb->mark);
  371. int type = icmp_param->data.icmph.type;
  372. int code = icmp_param->data.icmph.code;
  373. if (ip_options_echo(net, &icmp_param->replyopts.opt.opt, skb))
  374. return;
  375. /* Needed by both icmp_global_allow and icmp_xmit_lock */
  376. local_bh_disable();
  377. /* global icmp_msgs_per_sec */
  378. if (!icmpv4_global_allow(net, type, code))
  379. goto out_bh_enable;
  380. sk = icmp_xmit_lock(net);
  381. if (!sk)
  382. goto out_bh_enable;
  383. inet = inet_sk(sk);
  384. icmp_param->data.icmph.checksum = 0;
  385. inet->tos = ip_hdr(skb)->tos;
  386. sk->sk_mark = mark;
  387. daddr = ipc.addr = ip_hdr(skb)->saddr;
  388. saddr = fib_compute_spec_dst(skb);
  389. ipc.opt = NULL;
  390. ipc.tx_flags = 0;
  391. ipc.ttl = 0;
  392. ipc.tos = -1;
  393. if (icmp_param->replyopts.opt.opt.optlen) {
  394. ipc.opt = &icmp_param->replyopts.opt;
  395. if (ipc.opt->opt.srr)
  396. daddr = icmp_param->replyopts.opt.opt.faddr;
  397. }
  398. memset(&fl4, 0, sizeof(fl4));
  399. fl4.daddr = daddr;
  400. fl4.saddr = saddr;
  401. fl4.flowi4_mark = mark;
  402. fl4.flowi4_uid = sock_net_uid(net, NULL);
  403. fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
  404. fl4.flowi4_proto = IPPROTO_ICMP;
  405. fl4.flowi4_oif = l3mdev_master_ifindex(skb->dev);
  406. security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
  407. rt = ip_route_output_key(net, &fl4);
  408. if (IS_ERR(rt))
  409. goto out_unlock;
  410. if (icmpv4_xrlim_allow(net, rt, &fl4, type, code))
  411. icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
  412. ip_rt_put(rt);
  413. out_unlock:
  414. icmp_xmit_unlock(sk);
  415. out_bh_enable:
  416. local_bh_enable();
  417. }
  418. static struct rtable *icmp_route_lookup(struct net *net,
  419. struct flowi4 *fl4,
  420. struct sk_buff *skb_in,
  421. const struct iphdr *iph,
  422. __be32 saddr, u8 tos, u32 mark,
  423. int type, int code,
  424. struct icmp_bxm *param)
  425. {
  426. struct rtable *rt, *rt2;
  427. struct flowi4 fl4_dec;
  428. int err;
  429. memset(fl4, 0, sizeof(*fl4));
  430. fl4->daddr = (param->replyopts.opt.opt.srr ?
  431. param->replyopts.opt.opt.faddr : iph->saddr);
  432. fl4->saddr = saddr;
  433. fl4->flowi4_mark = mark;
  434. fl4->flowi4_uid = sock_net_uid(net, NULL);
  435. fl4->flowi4_tos = RT_TOS(tos);
  436. fl4->flowi4_proto = IPPROTO_ICMP;
  437. fl4->fl4_icmp_type = type;
  438. fl4->fl4_icmp_code = code;
  439. fl4->flowi4_oif = l3mdev_master_ifindex(skb_dst(skb_in)->dev);
  440. security_skb_classify_flow(skb_in, flowi4_to_flowi(fl4));
  441. rt = ip_route_output_key_hash(net, fl4, skb_in);
  442. if (IS_ERR(rt))
  443. return rt;
  444. /* No need to clone since we're just using its address. */
  445. rt2 = rt;
  446. rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
  447. flowi4_to_flowi(fl4), NULL, 0);
  448. if (!IS_ERR(rt)) {
  449. if (rt != rt2)
  450. return rt;
  451. } else if (PTR_ERR(rt) == -EPERM) {
  452. rt = NULL;
  453. } else
  454. return rt;
  455. err = xfrm_decode_session_reverse(skb_in, flowi4_to_flowi(&fl4_dec), AF_INET);
  456. if (err)
  457. goto relookup_failed;
  458. if (inet_addr_type_dev_table(net, skb_dst(skb_in)->dev,
  459. fl4_dec.saddr) == RTN_LOCAL) {
  460. rt2 = __ip_route_output_key(net, &fl4_dec);
  461. if (IS_ERR(rt2))
  462. err = PTR_ERR(rt2);
  463. } else {
  464. struct flowi4 fl4_2 = {};
  465. unsigned long orefdst;
  466. fl4_2.daddr = fl4_dec.saddr;
  467. rt2 = ip_route_output_key(net, &fl4_2);
  468. if (IS_ERR(rt2)) {
  469. err = PTR_ERR(rt2);
  470. goto relookup_failed;
  471. }
  472. /* Ugh! */
  473. orefdst = skb_in->_skb_refdst; /* save old refdst */
  474. skb_dst_set(skb_in, NULL);
  475. err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr,
  476. RT_TOS(tos), rt2->dst.dev);
  477. dst_release(&rt2->dst);
  478. rt2 = skb_rtable(skb_in);
  479. skb_in->_skb_refdst = orefdst; /* restore old refdst */
  480. }
  481. if (err)
  482. goto relookup_failed;
  483. rt2 = (struct rtable *) xfrm_lookup(net, &rt2->dst,
  484. flowi4_to_flowi(&fl4_dec), NULL,
  485. XFRM_LOOKUP_ICMP);
  486. if (!IS_ERR(rt2)) {
  487. dst_release(&rt->dst);
  488. memcpy(fl4, &fl4_dec, sizeof(*fl4));
  489. rt = rt2;
  490. } else if (PTR_ERR(rt2) == -EPERM) {
  491. if (rt)
  492. dst_release(&rt->dst);
  493. return rt2;
  494. } else {
  495. err = PTR_ERR(rt2);
  496. goto relookup_failed;
  497. }
  498. return rt;
  499. relookup_failed:
  500. if (rt)
  501. return rt;
  502. return ERR_PTR(err);
  503. }
  504. /*
  505. * Send an ICMP message in response to a situation
  506. *
  507. * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
  508. * MAY send more (we do).
  509. * MUST NOT change this header information.
  510. * MUST NOT reply to a multicast/broadcast IP address.
  511. * MUST NOT reply to a multicast/broadcast MAC address.
  512. * MUST reply to only the first fragment.
  513. */
  514. void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
  515. {
  516. struct iphdr *iph;
  517. int room;
  518. struct icmp_bxm icmp_param;
  519. struct rtable *rt = skb_rtable(skb_in);
  520. struct ipcm_cookie ipc;
  521. struct flowi4 fl4;
  522. __be32 saddr;
  523. u8 tos;
  524. u32 mark;
  525. struct net *net;
  526. struct sock *sk;
  527. if (!rt)
  528. goto out;
  529. net = dev_net(rt->dst.dev);
  530. /*
  531. * Find the original header. It is expected to be valid, of course.
  532. * Check this, icmp_send is called from the most obscure devices
  533. * sometimes.
  534. */
  535. iph = ip_hdr(skb_in);
  536. if ((u8 *)iph < skb_in->head ||
  537. (skb_network_header(skb_in) + sizeof(*iph)) >
  538. skb_tail_pointer(skb_in))
  539. goto out;
  540. /*
  541. * No replies to physical multicast/broadcast
  542. */
  543. if (skb_in->pkt_type != PACKET_HOST)
  544. goto out;
  545. /*
  546. * Now check at the protocol level
  547. */
  548. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  549. goto out;
  550. /*
  551. * Only reply to fragment 0. We byte re-order the constant
  552. * mask for efficiency.
  553. */
  554. if (iph->frag_off & htons(IP_OFFSET))
  555. goto out;
  556. /*
  557. * If we send an ICMP error to an ICMP error a mess would result..
  558. */
  559. if (icmp_pointers[type].error) {
  560. /*
  561. * We are an error, check if we are replying to an
  562. * ICMP error
  563. */
  564. if (iph->protocol == IPPROTO_ICMP) {
  565. u8 _inner_type, *itp;
  566. itp = skb_header_pointer(skb_in,
  567. skb_network_header(skb_in) +
  568. (iph->ihl << 2) +
  569. offsetof(struct icmphdr,
  570. type) -
  571. skb_in->data,
  572. sizeof(_inner_type),
  573. &_inner_type);
  574. if (!itp)
  575. goto out;
  576. /*
  577. * Assume any unknown ICMP type is an error. This
  578. * isn't specified by the RFC, but think about it..
  579. */
  580. if (*itp > NR_ICMP_TYPES ||
  581. icmp_pointers[*itp].error)
  582. goto out;
  583. }
  584. }
  585. /* Needed by both icmp_global_allow and icmp_xmit_lock */
  586. local_bh_disable();
  587. /* Check global sysctl_icmp_msgs_per_sec ratelimit, unless
  588. * incoming dev is loopback. If outgoing dev change to not be
  589. * loopback, then peer ratelimit still work (in icmpv4_xrlim_allow)
  590. */
  591. if (!(skb_in->dev && (skb_in->dev->flags&IFF_LOOPBACK)) &&
  592. !icmpv4_global_allow(net, type, code))
  593. goto out_bh_enable;
  594. sk = icmp_xmit_lock(net);
  595. if (!sk)
  596. goto out_bh_enable;
  597. /*
  598. * Construct source address and options.
  599. */
  600. saddr = iph->daddr;
  601. if (!(rt->rt_flags & RTCF_LOCAL)) {
  602. struct net_device *dev = NULL;
  603. rcu_read_lock();
  604. if (rt_is_input_route(rt) &&
  605. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)
  606. dev = dev_get_by_index_rcu(net, inet_iif(skb_in));
  607. if (dev)
  608. saddr = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  609. else
  610. saddr = 0;
  611. rcu_read_unlock();
  612. }
  613. tos = icmp_pointers[type].error ? ((iph->tos & IPTOS_TOS_MASK) |
  614. IPTOS_PREC_INTERNETCONTROL) :
  615. iph->tos;
  616. mark = IP4_REPLY_MARK(net, skb_in->mark);
  617. if (ip_options_echo(net, &icmp_param.replyopts.opt.opt, skb_in))
  618. goto out_unlock;
  619. /*
  620. * Prepare data for ICMP header.
  621. */
  622. icmp_param.data.icmph.type = type;
  623. icmp_param.data.icmph.code = code;
  624. icmp_param.data.icmph.un.gateway = info;
  625. icmp_param.data.icmph.checksum = 0;
  626. icmp_param.skb = skb_in;
  627. icmp_param.offset = skb_network_offset(skb_in);
  628. inet_sk(sk)->tos = tos;
  629. sk->sk_mark = mark;
  630. ipc.addr = iph->saddr;
  631. ipc.opt = &icmp_param.replyopts.opt;
  632. ipc.tx_flags = 0;
  633. ipc.ttl = 0;
  634. ipc.tos = -1;
  635. rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos, mark,
  636. type, code, &icmp_param);
  637. if (IS_ERR(rt))
  638. goto out_unlock;
  639. /* peer icmp_ratelimit */
  640. if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code))
  641. goto ende;
  642. /* RFC says return as much as we can without exceeding 576 bytes. */
  643. room = dst_mtu(&rt->dst);
  644. if (room > 576)
  645. room = 576;
  646. room -= sizeof(struct iphdr) + icmp_param.replyopts.opt.opt.optlen;
  647. room -= sizeof(struct icmphdr);
  648. icmp_param.data_len = skb_in->len - icmp_param.offset;
  649. if (icmp_param.data_len > room)
  650. icmp_param.data_len = room;
  651. icmp_param.head_len = sizeof(struct icmphdr);
  652. icmp_push_reply(&icmp_param, &fl4, &ipc, &rt);
  653. ende:
  654. ip_rt_put(rt);
  655. out_unlock:
  656. icmp_xmit_unlock(sk);
  657. out_bh_enable:
  658. local_bh_enable();
  659. out:;
  660. }
  661. EXPORT_SYMBOL(icmp_send);
  662. static void icmp_socket_deliver(struct sk_buff *skb, u32 info)
  663. {
  664. const struct iphdr *iph = (const struct iphdr *) skb->data;
  665. const struct net_protocol *ipprot;
  666. int protocol = iph->protocol;
  667. /* Checkin full IP header plus 8 bytes of protocol to
  668. * avoid additional coding at protocol handlers.
  669. */
  670. if (!pskb_may_pull(skb, iph->ihl * 4 + 8)) {
  671. __ICMP_INC_STATS(dev_net(skb->dev), ICMP_MIB_INERRORS);
  672. return;
  673. }
  674. raw_icmp_error(skb, protocol, info);
  675. ipprot = rcu_dereference(inet_protos[protocol]);
  676. if (ipprot && ipprot->err_handler)
  677. ipprot->err_handler(skb, info);
  678. }
  679. static bool icmp_tag_validation(int proto)
  680. {
  681. bool ok;
  682. rcu_read_lock();
  683. ok = rcu_dereference(inet_protos[proto])->icmp_strict_tag_validation;
  684. rcu_read_unlock();
  685. return ok;
  686. }
  687. /*
  688. * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEEDED, ICMP_QUENCH, and
  689. * ICMP_PARAMETERPROB.
  690. */
  691. static bool icmp_unreach(struct sk_buff *skb)
  692. {
  693. const struct iphdr *iph;
  694. struct icmphdr *icmph;
  695. struct net *net;
  696. u32 info = 0;
  697. net = dev_net(skb_dst(skb)->dev);
  698. /*
  699. * Incomplete header ?
  700. * Only checks for the IP header, there should be an
  701. * additional check for longer headers in upper levels.
  702. */
  703. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  704. goto out_err;
  705. icmph = icmp_hdr(skb);
  706. iph = (const struct iphdr *)skb->data;
  707. if (iph->ihl < 5) /* Mangled header, drop. */
  708. goto out_err;
  709. switch (icmph->type) {
  710. case ICMP_DEST_UNREACH:
  711. switch (icmph->code & 15) {
  712. case ICMP_NET_UNREACH:
  713. case ICMP_HOST_UNREACH:
  714. case ICMP_PROT_UNREACH:
  715. case ICMP_PORT_UNREACH:
  716. break;
  717. case ICMP_FRAG_NEEDED:
  718. /* for documentation of the ip_no_pmtu_disc
  719. * values please see
  720. * Documentation/networking/ip-sysctl.txt
  721. */
  722. switch (net->ipv4.sysctl_ip_no_pmtu_disc) {
  723. default:
  724. net_dbg_ratelimited("%pI4: fragmentation needed and DF set\n",
  725. &iph->daddr);
  726. break;
  727. case 2:
  728. goto out;
  729. case 3:
  730. if (!icmp_tag_validation(iph->protocol))
  731. goto out;
  732. /* fall through */
  733. case 0:
  734. info = ntohs(icmph->un.frag.mtu);
  735. }
  736. break;
  737. case ICMP_SR_FAILED:
  738. net_dbg_ratelimited("%pI4: Source Route Failed\n",
  739. &iph->daddr);
  740. break;
  741. default:
  742. break;
  743. }
  744. if (icmph->code > NR_ICMP_UNREACH)
  745. goto out;
  746. break;
  747. case ICMP_PARAMETERPROB:
  748. info = ntohl(icmph->un.gateway) >> 24;
  749. break;
  750. case ICMP_TIME_EXCEEDED:
  751. __ICMP_INC_STATS(net, ICMP_MIB_INTIMEEXCDS);
  752. if (icmph->code == ICMP_EXC_FRAGTIME)
  753. goto out;
  754. break;
  755. }
  756. /*
  757. * Throw it at our lower layers
  758. *
  759. * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
  760. * header.
  761. * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
  762. * transport layer.
  763. * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
  764. * transport layer.
  765. */
  766. /*
  767. * Check the other end isn't violating RFC 1122. Some routers send
  768. * bogus responses to broadcast frames. If you see this message
  769. * first check your netmask matches at both ends, if it does then
  770. * get the other vendor to fix their kit.
  771. */
  772. if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
  773. inet_addr_type_dev_table(net, skb->dev, iph->daddr) == RTN_BROADCAST) {
  774. net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
  775. &ip_hdr(skb)->saddr,
  776. icmph->type, icmph->code,
  777. &iph->daddr, skb->dev->name);
  778. goto out;
  779. }
  780. icmp_socket_deliver(skb, info);
  781. out:
  782. return true;
  783. out_err:
  784. __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
  785. return false;
  786. }
  787. /*
  788. * Handle ICMP_REDIRECT.
  789. */
  790. static bool icmp_redirect(struct sk_buff *skb)
  791. {
  792. if (skb->len < sizeof(struct iphdr)) {
  793. __ICMP_INC_STATS(dev_net(skb->dev), ICMP_MIB_INERRORS);
  794. return false;
  795. }
  796. if (!pskb_may_pull(skb, sizeof(struct iphdr))) {
  797. /* there aught to be a stat */
  798. return false;
  799. }
  800. icmp_socket_deliver(skb, icmp_hdr(skb)->un.gateway);
  801. return true;
  802. }
  803. /*
  804. * Handle ICMP_ECHO ("ping") requests.
  805. *
  806. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  807. * requests.
  808. * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
  809. * included in the reply.
  810. * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
  811. * echo requests, MUST have default=NOT.
  812. * See also WRT handling of options once they are done and working.
  813. */
  814. static bool icmp_echo(struct sk_buff *skb)
  815. {
  816. struct net *net;
  817. net = dev_net(skb_dst(skb)->dev);
  818. if (!net->ipv4.sysctl_icmp_echo_ignore_all) {
  819. struct icmp_bxm icmp_param;
  820. icmp_param.data.icmph = *icmp_hdr(skb);
  821. icmp_param.data.icmph.type = ICMP_ECHOREPLY;
  822. icmp_param.skb = skb;
  823. icmp_param.offset = 0;
  824. icmp_param.data_len = skb->len;
  825. icmp_param.head_len = sizeof(struct icmphdr);
  826. icmp_reply(&icmp_param, skb);
  827. }
  828. /* should there be an ICMP stat for ignored echos? */
  829. return true;
  830. }
  831. /*
  832. * Handle ICMP Timestamp requests.
  833. * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
  834. * SHOULD be in the kernel for minimum random latency.
  835. * MUST be accurate to a few minutes.
  836. * MUST be updated at least at 15Hz.
  837. */
  838. static bool icmp_timestamp(struct sk_buff *skb)
  839. {
  840. struct icmp_bxm icmp_param;
  841. /*
  842. * Too short.
  843. */
  844. if (skb->len < 4)
  845. goto out_err;
  846. /*
  847. * Fill in the current time as ms since midnight UT:
  848. */
  849. icmp_param.data.times[1] = inet_current_timestamp();
  850. icmp_param.data.times[2] = icmp_param.data.times[1];
  851. BUG_ON(skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4));
  852. icmp_param.data.icmph = *icmp_hdr(skb);
  853. icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
  854. icmp_param.data.icmph.code = 0;
  855. icmp_param.skb = skb;
  856. icmp_param.offset = 0;
  857. icmp_param.data_len = 0;
  858. icmp_param.head_len = sizeof(struct icmphdr) + 12;
  859. icmp_reply(&icmp_param, skb);
  860. return true;
  861. out_err:
  862. __ICMP_INC_STATS(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
  863. return false;
  864. }
  865. static bool icmp_discard(struct sk_buff *skb)
  866. {
  867. /* pretend it was a success */
  868. return true;
  869. }
  870. /*
  871. * Deal with incoming ICMP packets.
  872. */
  873. int icmp_rcv(struct sk_buff *skb)
  874. {
  875. struct icmphdr *icmph;
  876. struct rtable *rt = skb_rtable(skb);
  877. struct net *net = dev_net(rt->dst.dev);
  878. bool success;
  879. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  880. struct sec_path *sp = skb_sec_path(skb);
  881. int nh;
  882. if (!(sp && sp->xvec[sp->len - 1]->props.flags &
  883. XFRM_STATE_ICMP))
  884. goto drop;
  885. if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
  886. goto drop;
  887. nh = skb_network_offset(skb);
  888. skb_set_network_header(skb, sizeof(*icmph));
  889. if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
  890. goto drop;
  891. skb_set_network_header(skb, nh);
  892. }
  893. __ICMP_INC_STATS(net, ICMP_MIB_INMSGS);
  894. if (skb_checksum_simple_validate(skb))
  895. goto csum_error;
  896. if (!pskb_pull(skb, sizeof(*icmph)))
  897. goto error;
  898. icmph = icmp_hdr(skb);
  899. ICMPMSGIN_INC_STATS(net, icmph->type);
  900. /*
  901. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  902. *
  903. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  904. * discarded.
  905. */
  906. if (icmph->type > NR_ICMP_TYPES)
  907. goto error;
  908. /*
  909. * Parse the ICMP message
  910. */
  911. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  912. /*
  913. * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
  914. * silently ignored (we let user decide with a sysctl).
  915. * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
  916. * discarded if to broadcast/multicast.
  917. */
  918. if ((icmph->type == ICMP_ECHO ||
  919. icmph->type == ICMP_TIMESTAMP) &&
  920. net->ipv4.sysctl_icmp_echo_ignore_broadcasts) {
  921. goto error;
  922. }
  923. if (icmph->type != ICMP_ECHO &&
  924. icmph->type != ICMP_TIMESTAMP &&
  925. icmph->type != ICMP_ADDRESS &&
  926. icmph->type != ICMP_ADDRESSREPLY) {
  927. goto error;
  928. }
  929. }
  930. success = icmp_pointers[icmph->type].handler(skb);
  931. if (success) {
  932. consume_skb(skb);
  933. return NET_RX_SUCCESS;
  934. }
  935. drop:
  936. kfree_skb(skb);
  937. return NET_RX_DROP;
  938. csum_error:
  939. __ICMP_INC_STATS(net, ICMP_MIB_CSUMERRORS);
  940. error:
  941. __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
  942. goto drop;
  943. }
  944. void icmp_err(struct sk_buff *skb, u32 info)
  945. {
  946. struct iphdr *iph = (struct iphdr *)skb->data;
  947. int offset = iph->ihl<<2;
  948. struct icmphdr *icmph = (struct icmphdr *)(skb->data + offset);
  949. int type = icmp_hdr(skb)->type;
  950. int code = icmp_hdr(skb)->code;
  951. struct net *net = dev_net(skb->dev);
  952. /*
  953. * Use ping_err to handle all icmp errors except those
  954. * triggered by ICMP_ECHOREPLY which sent from kernel.
  955. */
  956. if (icmph->type != ICMP_ECHOREPLY) {
  957. ping_err(skb, offset, info);
  958. return;
  959. }
  960. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
  961. ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_ICMP, 0);
  962. else if (type == ICMP_REDIRECT)
  963. ipv4_redirect(skb, net, 0, 0, IPPROTO_ICMP, 0);
  964. }
  965. /*
  966. * This table is the definition of how we handle ICMP.
  967. */
  968. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
  969. [ICMP_ECHOREPLY] = {
  970. .handler = ping_rcv,
  971. },
  972. [1] = {
  973. .handler = icmp_discard,
  974. .error = 1,
  975. },
  976. [2] = {
  977. .handler = icmp_discard,
  978. .error = 1,
  979. },
  980. [ICMP_DEST_UNREACH] = {
  981. .handler = icmp_unreach,
  982. .error = 1,
  983. },
  984. [ICMP_SOURCE_QUENCH] = {
  985. .handler = icmp_unreach,
  986. .error = 1,
  987. },
  988. [ICMP_REDIRECT] = {
  989. .handler = icmp_redirect,
  990. .error = 1,
  991. },
  992. [6] = {
  993. .handler = icmp_discard,
  994. .error = 1,
  995. },
  996. [7] = {
  997. .handler = icmp_discard,
  998. .error = 1,
  999. },
  1000. [ICMP_ECHO] = {
  1001. .handler = icmp_echo,
  1002. },
  1003. [9] = {
  1004. .handler = icmp_discard,
  1005. .error = 1,
  1006. },
  1007. [10] = {
  1008. .handler = icmp_discard,
  1009. .error = 1,
  1010. },
  1011. [ICMP_TIME_EXCEEDED] = {
  1012. .handler = icmp_unreach,
  1013. .error = 1,
  1014. },
  1015. [ICMP_PARAMETERPROB] = {
  1016. .handler = icmp_unreach,
  1017. .error = 1,
  1018. },
  1019. [ICMP_TIMESTAMP] = {
  1020. .handler = icmp_timestamp,
  1021. },
  1022. [ICMP_TIMESTAMPREPLY] = {
  1023. .handler = icmp_discard,
  1024. },
  1025. [ICMP_INFO_REQUEST] = {
  1026. .handler = icmp_discard,
  1027. },
  1028. [ICMP_INFO_REPLY] = {
  1029. .handler = icmp_discard,
  1030. },
  1031. [ICMP_ADDRESS] = {
  1032. .handler = icmp_discard,
  1033. },
  1034. [ICMP_ADDRESSREPLY] = {
  1035. .handler = icmp_discard,
  1036. },
  1037. };
  1038. static void __net_exit icmp_sk_exit(struct net *net)
  1039. {
  1040. int i;
  1041. for_each_possible_cpu(i)
  1042. inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.icmp_sk, i));
  1043. free_percpu(net->ipv4.icmp_sk);
  1044. net->ipv4.icmp_sk = NULL;
  1045. }
  1046. static int __net_init icmp_sk_init(struct net *net)
  1047. {
  1048. int i, err;
  1049. net->ipv4.icmp_sk = alloc_percpu(struct sock *);
  1050. if (!net->ipv4.icmp_sk)
  1051. return -ENOMEM;
  1052. for_each_possible_cpu(i) {
  1053. struct sock *sk;
  1054. err = inet_ctl_sock_create(&sk, PF_INET,
  1055. SOCK_RAW, IPPROTO_ICMP, net);
  1056. if (err < 0)
  1057. goto fail;
  1058. *per_cpu_ptr(net->ipv4.icmp_sk, i) = sk;
  1059. /* Enough space for 2 64K ICMP packets, including
  1060. * sk_buff/skb_shared_info struct overhead.
  1061. */
  1062. sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
  1063. /*
  1064. * Speedup sock_wfree()
  1065. */
  1066. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  1067. inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
  1068. }
  1069. /* Control parameters for ECHO replies. */
  1070. net->ipv4.sysctl_icmp_echo_ignore_all = 0;
  1071. net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
  1072. /* Control parameter - ignore bogus broadcast responses? */
  1073. net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
  1074. /*
  1075. * Configurable global rate limit.
  1076. *
  1077. * ratelimit defines tokens/packet consumed for dst->rate_token
  1078. * bucket ratemask defines which icmp types are ratelimited by
  1079. * setting it's bit position.
  1080. *
  1081. * default:
  1082. * dest unreachable (3), source quench (4),
  1083. * time exceeded (11), parameter problem (12)
  1084. */
  1085. net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
  1086. net->ipv4.sysctl_icmp_ratemask = 0x1818;
  1087. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
  1088. return 0;
  1089. fail:
  1090. for_each_possible_cpu(i)
  1091. inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.icmp_sk, i));
  1092. free_percpu(net->ipv4.icmp_sk);
  1093. return err;
  1094. }
  1095. static struct pernet_operations __net_initdata icmp_sk_ops = {
  1096. .init = icmp_sk_init,
  1097. .exit = icmp_sk_exit,
  1098. };
  1099. int __init icmp_init(void)
  1100. {
  1101. return register_pernet_subsys(&icmp_sk_ops);
  1102. }