icmp.c 29 KB

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