icmp.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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. /*
  218. * Send an ICMP frame.
  219. */
  220. static inline bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
  221. struct flowi4 *fl4, int type, int code)
  222. {
  223. struct dst_entry *dst = &rt->dst;
  224. bool rc = true;
  225. if (type > NR_ICMP_TYPES)
  226. goto out;
  227. /* Don't limit PMTU discovery. */
  228. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
  229. goto out;
  230. /* No rate limit on loopback */
  231. if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
  232. goto out;
  233. /* Limit if icmp type is enabled in ratemask. */
  234. if ((1 << type) & net->ipv4.sysctl_icmp_ratemask) {
  235. struct inet_peer *peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr, 1);
  236. rc = inet_peer_xrlim_allow(peer,
  237. net->ipv4.sysctl_icmp_ratelimit);
  238. inet_putpeer(peer);
  239. }
  240. out:
  241. return rc;
  242. }
  243. /*
  244. * Maintain the counters used in the SNMP statistics for outgoing ICMP
  245. */
  246. void icmp_out_count(struct net *net, unsigned char type)
  247. {
  248. ICMPMSGOUT_INC_STATS(net, type);
  249. ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS);
  250. }
  251. /*
  252. * Checksum each fragment, and on the first include the headers and final
  253. * checksum.
  254. */
  255. static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
  256. struct sk_buff *skb)
  257. {
  258. struct icmp_bxm *icmp_param = (struct icmp_bxm *)from;
  259. __wsum csum;
  260. csum = skb_copy_and_csum_bits(icmp_param->skb,
  261. icmp_param->offset + offset,
  262. to, len, 0);
  263. skb->csum = csum_block_add(skb->csum, csum, odd);
  264. if (icmp_pointers[icmp_param->data.icmph.type].error)
  265. nf_ct_attach(skb, icmp_param->skb);
  266. return 0;
  267. }
  268. static void icmp_push_reply(struct icmp_bxm *icmp_param,
  269. struct flowi4 *fl4,
  270. struct ipcm_cookie *ipc, struct rtable **rt)
  271. {
  272. struct sock *sk;
  273. struct sk_buff *skb;
  274. sk = icmp_sk(dev_net((*rt)->dst.dev));
  275. if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
  276. icmp_param->data_len+icmp_param->head_len,
  277. icmp_param->head_len,
  278. ipc, rt, MSG_DONTWAIT) < 0) {
  279. ICMP_INC_STATS_BH(sock_net(sk), ICMP_MIB_OUTERRORS);
  280. ip_flush_pending_frames(sk);
  281. } else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
  282. struct icmphdr *icmph = icmp_hdr(skb);
  283. __wsum csum = 0;
  284. struct sk_buff *skb1;
  285. skb_queue_walk(&sk->sk_write_queue, skb1) {
  286. csum = csum_add(csum, skb1->csum);
  287. }
  288. csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
  289. (char *)icmph,
  290. icmp_param->head_len, csum);
  291. icmph->checksum = csum_fold(csum);
  292. skb->ip_summed = CHECKSUM_NONE;
  293. ip_push_pending_frames(sk, fl4);
  294. }
  295. }
  296. /*
  297. * Driving logic for building and sending ICMP messages.
  298. */
  299. static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
  300. {
  301. struct ipcm_cookie ipc;
  302. struct rtable *rt = skb_rtable(skb);
  303. struct net *net = dev_net(rt->dst.dev);
  304. struct flowi4 fl4;
  305. struct sock *sk;
  306. struct inet_sock *inet;
  307. __be32 daddr, saddr;
  308. if (ip_options_echo(&icmp_param->replyopts.opt.opt, skb))
  309. return;
  310. sk = icmp_xmit_lock(net);
  311. if (sk == NULL)
  312. return;
  313. inet = inet_sk(sk);
  314. icmp_param->data.icmph.checksum = 0;
  315. inet->tos = ip_hdr(skb)->tos;
  316. daddr = ipc.addr = ip_hdr(skb)->saddr;
  317. saddr = fib_compute_spec_dst(skb);
  318. ipc.opt = NULL;
  319. ipc.tx_flags = 0;
  320. if (icmp_param->replyopts.opt.opt.optlen) {
  321. ipc.opt = &icmp_param->replyopts.opt;
  322. if (ipc.opt->opt.srr)
  323. daddr = icmp_param->replyopts.opt.opt.faddr;
  324. }
  325. memset(&fl4, 0, sizeof(fl4));
  326. fl4.daddr = daddr;
  327. fl4.saddr = saddr;
  328. fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
  329. fl4.flowi4_proto = IPPROTO_ICMP;
  330. security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
  331. rt = ip_route_output_key(net, &fl4);
  332. if (IS_ERR(rt))
  333. goto out_unlock;
  334. if (icmpv4_xrlim_allow(net, rt, &fl4, icmp_param->data.icmph.type,
  335. icmp_param->data.icmph.code))
  336. icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
  337. ip_rt_put(rt);
  338. out_unlock:
  339. icmp_xmit_unlock(sk);
  340. }
  341. static struct rtable *icmp_route_lookup(struct net *net,
  342. struct flowi4 *fl4,
  343. struct sk_buff *skb_in,
  344. const struct iphdr *iph,
  345. __be32 saddr, u8 tos,
  346. int type, int code,
  347. struct icmp_bxm *param)
  348. {
  349. struct rtable *rt, *rt2;
  350. struct flowi4 fl4_dec;
  351. int err;
  352. memset(fl4, 0, sizeof(*fl4));
  353. fl4->daddr = (param->replyopts.opt.opt.srr ?
  354. param->replyopts.opt.opt.faddr : iph->saddr);
  355. fl4->saddr = saddr;
  356. fl4->flowi4_tos = RT_TOS(tos);
  357. fl4->flowi4_proto = IPPROTO_ICMP;
  358. fl4->fl4_icmp_type = type;
  359. fl4->fl4_icmp_code = code;
  360. security_skb_classify_flow(skb_in, flowi4_to_flowi(fl4));
  361. rt = __ip_route_output_key(net, fl4);
  362. if (IS_ERR(rt))
  363. return rt;
  364. /* No need to clone since we're just using its address. */
  365. rt2 = rt;
  366. rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
  367. flowi4_to_flowi(fl4), NULL, 0);
  368. if (!IS_ERR(rt)) {
  369. if (rt != rt2)
  370. return rt;
  371. } else if (PTR_ERR(rt) == -EPERM) {
  372. rt = NULL;
  373. } else
  374. return rt;
  375. err = xfrm_decode_session_reverse(skb_in, flowi4_to_flowi(&fl4_dec), AF_INET);
  376. if (err)
  377. goto relookup_failed;
  378. if (inet_addr_type(net, fl4_dec.saddr) == RTN_LOCAL) {
  379. rt2 = __ip_route_output_key(net, &fl4_dec);
  380. if (IS_ERR(rt2))
  381. err = PTR_ERR(rt2);
  382. } else {
  383. struct flowi4 fl4_2 = {};
  384. unsigned long orefdst;
  385. fl4_2.daddr = fl4_dec.saddr;
  386. rt2 = ip_route_output_key(net, &fl4_2);
  387. if (IS_ERR(rt2)) {
  388. err = PTR_ERR(rt2);
  389. goto relookup_failed;
  390. }
  391. /* Ugh! */
  392. orefdst = skb_in->_skb_refdst; /* save old refdst */
  393. err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr,
  394. RT_TOS(tos), rt2->dst.dev);
  395. dst_release(&rt2->dst);
  396. rt2 = skb_rtable(skb_in);
  397. skb_in->_skb_refdst = orefdst; /* restore old refdst */
  398. }
  399. if (err)
  400. goto relookup_failed;
  401. rt2 = (struct rtable *) xfrm_lookup(net, &rt2->dst,
  402. flowi4_to_flowi(&fl4_dec), NULL,
  403. XFRM_LOOKUP_ICMP);
  404. if (!IS_ERR(rt2)) {
  405. dst_release(&rt->dst);
  406. memcpy(fl4, &fl4_dec, sizeof(*fl4));
  407. rt = rt2;
  408. } else if (PTR_ERR(rt2) == -EPERM) {
  409. if (rt)
  410. dst_release(&rt->dst);
  411. return rt2;
  412. } else {
  413. err = PTR_ERR(rt2);
  414. goto relookup_failed;
  415. }
  416. return rt;
  417. relookup_failed:
  418. if (rt)
  419. return rt;
  420. return ERR_PTR(err);
  421. }
  422. /*
  423. * Send an ICMP message in response to a situation
  424. *
  425. * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
  426. * MAY send more (we do).
  427. * MUST NOT change this header information.
  428. * MUST NOT reply to a multicast/broadcast IP address.
  429. * MUST NOT reply to a multicast/broadcast MAC address.
  430. * MUST reply to only the first fragment.
  431. */
  432. void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
  433. {
  434. struct iphdr *iph;
  435. int room;
  436. struct icmp_bxm icmp_param;
  437. struct rtable *rt = skb_rtable(skb_in);
  438. struct ipcm_cookie ipc;
  439. struct flowi4 fl4;
  440. __be32 saddr;
  441. u8 tos;
  442. struct net *net;
  443. struct sock *sk;
  444. if (!rt)
  445. goto out;
  446. net = dev_net(rt->dst.dev);
  447. /*
  448. * Find the original header. It is expected to be valid, of course.
  449. * Check this, icmp_send is called from the most obscure devices
  450. * sometimes.
  451. */
  452. iph = ip_hdr(skb_in);
  453. if ((u8 *)iph < skb_in->head ||
  454. (skb_in->network_header + sizeof(*iph)) > skb_in->tail)
  455. goto out;
  456. /*
  457. * No replies to physical multicast/broadcast
  458. */
  459. if (skb_in->pkt_type != PACKET_HOST)
  460. goto out;
  461. /*
  462. * Now check at the protocol level
  463. */
  464. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  465. goto out;
  466. /*
  467. * Only reply to fragment 0. We byte re-order the constant
  468. * mask for efficiency.
  469. */
  470. if (iph->frag_off & htons(IP_OFFSET))
  471. goto out;
  472. /*
  473. * If we send an ICMP error to an ICMP error a mess would result..
  474. */
  475. if (icmp_pointers[type].error) {
  476. /*
  477. * We are an error, check if we are replying to an
  478. * ICMP error
  479. */
  480. if (iph->protocol == IPPROTO_ICMP) {
  481. u8 _inner_type, *itp;
  482. itp = skb_header_pointer(skb_in,
  483. skb_network_header(skb_in) +
  484. (iph->ihl << 2) +
  485. offsetof(struct icmphdr,
  486. type) -
  487. skb_in->data,
  488. sizeof(_inner_type),
  489. &_inner_type);
  490. if (itp == NULL)
  491. goto out;
  492. /*
  493. * Assume any unknown ICMP type is an error. This
  494. * isn't specified by the RFC, but think about it..
  495. */
  496. if (*itp > NR_ICMP_TYPES ||
  497. icmp_pointers[*itp].error)
  498. goto out;
  499. }
  500. }
  501. sk = icmp_xmit_lock(net);
  502. if (sk == NULL)
  503. return;
  504. /*
  505. * Construct source address and options.
  506. */
  507. saddr = iph->daddr;
  508. if (!(rt->rt_flags & RTCF_LOCAL)) {
  509. struct net_device *dev = NULL;
  510. rcu_read_lock();
  511. if (rt_is_input_route(rt) &&
  512. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)
  513. dev = dev_get_by_index_rcu(net, rt->rt_iif);
  514. if (dev)
  515. saddr = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  516. else
  517. saddr = 0;
  518. rcu_read_unlock();
  519. }
  520. tos = icmp_pointers[type].error ? ((iph->tos & IPTOS_TOS_MASK) |
  521. IPTOS_PREC_INTERNETCONTROL) :
  522. iph->tos;
  523. if (ip_options_echo(&icmp_param.replyopts.opt.opt, skb_in))
  524. goto out_unlock;
  525. /*
  526. * Prepare data for ICMP header.
  527. */
  528. icmp_param.data.icmph.type = type;
  529. icmp_param.data.icmph.code = code;
  530. icmp_param.data.icmph.un.gateway = info;
  531. icmp_param.data.icmph.checksum = 0;
  532. icmp_param.skb = skb_in;
  533. icmp_param.offset = skb_network_offset(skb_in);
  534. inet_sk(sk)->tos = tos;
  535. ipc.addr = iph->saddr;
  536. ipc.opt = &icmp_param.replyopts.opt;
  537. ipc.tx_flags = 0;
  538. rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos,
  539. type, code, &icmp_param);
  540. if (IS_ERR(rt))
  541. goto out_unlock;
  542. if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code))
  543. goto ende;
  544. /* RFC says return as much as we can without exceeding 576 bytes. */
  545. room = dst_mtu(&rt->dst);
  546. if (room > 576)
  547. room = 576;
  548. room -= sizeof(struct iphdr) + icmp_param.replyopts.opt.opt.optlen;
  549. room -= sizeof(struct icmphdr);
  550. icmp_param.data_len = skb_in->len - icmp_param.offset;
  551. if (icmp_param.data_len > room)
  552. icmp_param.data_len = room;
  553. icmp_param.head_len = sizeof(struct icmphdr);
  554. icmp_push_reply(&icmp_param, &fl4, &ipc, &rt);
  555. ende:
  556. ip_rt_put(rt);
  557. out_unlock:
  558. icmp_xmit_unlock(sk);
  559. out:;
  560. }
  561. EXPORT_SYMBOL(icmp_send);
  562. static void icmp_socket_deliver(struct sk_buff *skb, u32 info)
  563. {
  564. const struct iphdr *iph = (const struct iphdr *) skb->data;
  565. const struct net_protocol *ipprot;
  566. int protocol = iph->protocol;
  567. raw_icmp_error(skb, protocol, info);
  568. rcu_read_lock();
  569. ipprot = rcu_dereference(inet_protos[protocol]);
  570. if (ipprot && ipprot->err_handler)
  571. ipprot->err_handler(skb, info);
  572. rcu_read_unlock();
  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. const struct iphdr *iph;
  580. struct icmphdr *icmph;
  581. struct net *net;
  582. u32 info = 0;
  583. net = dev_net(skb_dst(skb)->dev);
  584. /*
  585. * Incomplete header ?
  586. * Only checks for the IP header, there should be an
  587. * additional check for longer headers in upper levels.
  588. */
  589. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  590. goto out_err;
  591. icmph = icmp_hdr(skb);
  592. iph = (const struct iphdr *)skb->data;
  593. if (iph->ihl < 5) /* Mangled header, drop. */
  594. goto out_err;
  595. if (icmph->type == ICMP_DEST_UNREACH) {
  596. switch (icmph->code & 15) {
  597. case ICMP_NET_UNREACH:
  598. case ICMP_HOST_UNREACH:
  599. case ICMP_PROT_UNREACH:
  600. case ICMP_PORT_UNREACH:
  601. break;
  602. case ICMP_FRAG_NEEDED:
  603. if (ipv4_config.no_pmtu_disc) {
  604. LIMIT_NETDEBUG(KERN_INFO pr_fmt("%pI4: fragmentation needed and DF set\n"),
  605. &iph->daddr);
  606. } else {
  607. info = ntohs(icmph->un.frag.mtu);
  608. if (!info)
  609. goto out;
  610. }
  611. break;
  612. case ICMP_SR_FAILED:
  613. LIMIT_NETDEBUG(KERN_INFO pr_fmt("%pI4: Source Route Failed\n"),
  614. &iph->daddr);
  615. break;
  616. default:
  617. break;
  618. }
  619. if (icmph->code > NR_ICMP_UNREACH)
  620. goto out;
  621. } else if (icmph->type == ICMP_PARAMETERPROB)
  622. info = ntohl(icmph->un.gateway) >> 24;
  623. /*
  624. * Throw it at our lower layers
  625. *
  626. * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
  627. * header.
  628. * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
  629. * transport layer.
  630. * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
  631. * transport layer.
  632. */
  633. /*
  634. * Check the other end isn't violating RFC 1122. Some routers send
  635. * bogus responses to broadcast frames. If you see this message
  636. * first check your netmask matches at both ends, if it does then
  637. * get the other vendor to fix their kit.
  638. */
  639. if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
  640. inet_addr_type(net, iph->daddr) == RTN_BROADCAST) {
  641. net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
  642. &ip_hdr(skb)->saddr,
  643. icmph->type, icmph->code,
  644. &iph->daddr, skb->dev->name);
  645. goto out;
  646. }
  647. /* Checkin full IP header plus 8 bytes of protocol to
  648. * avoid additional coding at protocol handlers.
  649. */
  650. if (!pskb_may_pull(skb, iph->ihl * 4 + 8))
  651. goto out;
  652. icmp_socket_deliver(skb, info);
  653. out:
  654. return;
  655. out_err:
  656. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  657. goto out;
  658. }
  659. /*
  660. * Handle ICMP_REDIRECT.
  661. */
  662. static void icmp_redirect(struct sk_buff *skb)
  663. {
  664. if (skb->len < sizeof(struct iphdr)) {
  665. ICMP_INC_STATS_BH(dev_net(skb->dev), ICMP_MIB_INERRORS);
  666. return;
  667. }
  668. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  669. return;
  670. ip_rt_redirect(skb, icmp_hdr(skb)->un.gateway);
  671. icmp_socket_deliver(skb, icmp_hdr(skb)->un.gateway);
  672. }
  673. /*
  674. * Handle ICMP_ECHO ("ping") requests.
  675. *
  676. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  677. * requests.
  678. * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
  679. * included in the reply.
  680. * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
  681. * echo requests, MUST have default=NOT.
  682. * See also WRT handling of options once they are done and working.
  683. */
  684. static void icmp_echo(struct sk_buff *skb)
  685. {
  686. struct net *net;
  687. net = dev_net(skb_dst(skb)->dev);
  688. if (!net->ipv4.sysctl_icmp_echo_ignore_all) {
  689. struct icmp_bxm icmp_param;
  690. icmp_param.data.icmph = *icmp_hdr(skb);
  691. icmp_param.data.icmph.type = ICMP_ECHOREPLY;
  692. icmp_param.skb = skb;
  693. icmp_param.offset = 0;
  694. icmp_param.data_len = skb->len;
  695. icmp_param.head_len = sizeof(struct icmphdr);
  696. icmp_reply(&icmp_param, skb);
  697. }
  698. }
  699. /*
  700. * Handle ICMP Timestamp requests.
  701. * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
  702. * SHOULD be in the kernel for minimum random latency.
  703. * MUST be accurate to a few minutes.
  704. * MUST be updated at least at 15Hz.
  705. */
  706. static void icmp_timestamp(struct sk_buff *skb)
  707. {
  708. struct timespec tv;
  709. struct icmp_bxm icmp_param;
  710. /*
  711. * Too short.
  712. */
  713. if (skb->len < 4)
  714. goto out_err;
  715. /*
  716. * Fill in the current time as ms since midnight UT:
  717. */
  718. getnstimeofday(&tv);
  719. icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC +
  720. tv.tv_nsec / NSEC_PER_MSEC);
  721. icmp_param.data.times[2] = icmp_param.data.times[1];
  722. if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
  723. BUG();
  724. icmp_param.data.icmph = *icmp_hdr(skb);
  725. icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
  726. icmp_param.data.icmph.code = 0;
  727. icmp_param.skb = skb;
  728. icmp_param.offset = 0;
  729. icmp_param.data_len = 0;
  730. icmp_param.head_len = sizeof(struct icmphdr) + 12;
  731. icmp_reply(&icmp_param, skb);
  732. out:
  733. return;
  734. out_err:
  735. ICMP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
  736. goto out;
  737. }
  738. /*
  739. * Handle ICMP_ADDRESS_MASK requests. (RFC950)
  740. *
  741. * RFC1122 (3.2.2.9). A host MUST only send replies to
  742. * ADDRESS_MASK requests if it's been configured as an address mask
  743. * agent. Receiving a request doesn't constitute implicit permission to
  744. * act as one. Of course, implementing this correctly requires (SHOULD)
  745. * a way to turn the functionality on and off. Another one for sysctl(),
  746. * I guess. -- MS
  747. *
  748. * RFC1812 (4.3.3.9). A router MUST implement it.
  749. * A router SHOULD have switch turning it on/off.
  750. * This switch MUST be ON by default.
  751. *
  752. * Gratuitous replies, zero-source replies are not implemented,
  753. * that complies with RFC. DO NOT implement them!!! All the idea
  754. * of broadcast addrmask replies as specified in RFC950 is broken.
  755. * The problem is that it is not uncommon to have several prefixes
  756. * on one physical interface. Moreover, addrmask agent can even be
  757. * not aware of existing another prefixes.
  758. * If source is zero, addrmask agent cannot choose correct prefix.
  759. * Gratuitous mask announcements suffer from the same problem.
  760. * RFC1812 explains it, but still allows to use ADDRMASK,
  761. * that is pretty silly. --ANK
  762. *
  763. * All these rules are so bizarre, that I removed kernel addrmask
  764. * support at all. It is wrong, it is obsolete, nobody uses it in
  765. * any case. --ANK
  766. *
  767. * Furthermore you can do it with a usermode address agent program
  768. * anyway...
  769. */
  770. static void icmp_address(struct sk_buff *skb)
  771. {
  772. #if 0
  773. net_dbg_ratelimited("a guy asks for address mask. Who is it?\n");
  774. #endif
  775. }
  776. /*
  777. * RFC1812 (4.3.3.9). A router SHOULD listen all replies, and complain
  778. * loudly if an inconsistency is found.
  779. * called with rcu_read_lock()
  780. */
  781. static void icmp_address_reply(struct sk_buff *skb)
  782. {
  783. struct rtable *rt = skb_rtable(skb);
  784. struct net_device *dev = skb->dev;
  785. struct in_device *in_dev;
  786. struct in_ifaddr *ifa;
  787. if (skb->len < 4 || !(rt->rt_flags&RTCF_DIRECTSRC))
  788. return;
  789. in_dev = __in_dev_get_rcu(dev);
  790. if (!in_dev)
  791. return;
  792. if (in_dev->ifa_list &&
  793. IN_DEV_LOG_MARTIANS(in_dev) &&
  794. IN_DEV_FORWARD(in_dev)) {
  795. __be32 _mask, *mp;
  796. mp = skb_header_pointer(skb, 0, sizeof(_mask), &_mask);
  797. BUG_ON(mp == NULL);
  798. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  799. if (*mp == ifa->ifa_mask &&
  800. inet_ifa_match(ip_hdr(skb)->saddr, ifa))
  801. break;
  802. }
  803. if (!ifa)
  804. net_info_ratelimited("Wrong address mask %pI4 from %s/%pI4\n",
  805. mp,
  806. dev->name, &ip_hdr(skb)->saddr);
  807. }
  808. }
  809. static void icmp_discard(struct sk_buff *skb)
  810. {
  811. }
  812. /*
  813. * Deal with incoming ICMP packets.
  814. */
  815. int icmp_rcv(struct sk_buff *skb)
  816. {
  817. struct icmphdr *icmph;
  818. struct rtable *rt = skb_rtable(skb);
  819. struct net *net = dev_net(rt->dst.dev);
  820. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  821. struct sec_path *sp = skb_sec_path(skb);
  822. int nh;
  823. if (!(sp && sp->xvec[sp->len - 1]->props.flags &
  824. XFRM_STATE_ICMP))
  825. goto drop;
  826. if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
  827. goto drop;
  828. nh = skb_network_offset(skb);
  829. skb_set_network_header(skb, sizeof(*icmph));
  830. if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
  831. goto drop;
  832. skb_set_network_header(skb, nh);
  833. }
  834. ICMP_INC_STATS_BH(net, ICMP_MIB_INMSGS);
  835. switch (skb->ip_summed) {
  836. case CHECKSUM_COMPLETE:
  837. if (!csum_fold(skb->csum))
  838. break;
  839. /* fall through */
  840. case CHECKSUM_NONE:
  841. skb->csum = 0;
  842. if (__skb_checksum_complete(skb))
  843. goto error;
  844. }
  845. if (!pskb_pull(skb, sizeof(*icmph)))
  846. goto error;
  847. icmph = icmp_hdr(skb);
  848. ICMPMSGIN_INC_STATS_BH(net, icmph->type);
  849. /*
  850. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  851. *
  852. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  853. * discarded.
  854. */
  855. if (icmph->type > NR_ICMP_TYPES)
  856. goto error;
  857. /*
  858. * Parse the ICMP message
  859. */
  860. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  861. /*
  862. * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
  863. * silently ignored (we let user decide with a sysctl).
  864. * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
  865. * discarded if to broadcast/multicast.
  866. */
  867. if ((icmph->type == ICMP_ECHO ||
  868. icmph->type == ICMP_TIMESTAMP) &&
  869. net->ipv4.sysctl_icmp_echo_ignore_broadcasts) {
  870. goto error;
  871. }
  872. if (icmph->type != ICMP_ECHO &&
  873. icmph->type != ICMP_TIMESTAMP &&
  874. icmph->type != ICMP_ADDRESS &&
  875. icmph->type != ICMP_ADDRESSREPLY) {
  876. goto error;
  877. }
  878. }
  879. icmp_pointers[icmph->type].handler(skb);
  880. drop:
  881. kfree_skb(skb);
  882. return 0;
  883. error:
  884. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  885. goto drop;
  886. }
  887. /*
  888. * This table is the definition of how we handle ICMP.
  889. */
  890. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
  891. [ICMP_ECHOREPLY] = {
  892. .handler = ping_rcv,
  893. },
  894. [1] = {
  895. .handler = icmp_discard,
  896. .error = 1,
  897. },
  898. [2] = {
  899. .handler = icmp_discard,
  900. .error = 1,
  901. },
  902. [ICMP_DEST_UNREACH] = {
  903. .handler = icmp_unreach,
  904. .error = 1,
  905. },
  906. [ICMP_SOURCE_QUENCH] = {
  907. .handler = icmp_unreach,
  908. .error = 1,
  909. },
  910. [ICMP_REDIRECT] = {
  911. .handler = icmp_redirect,
  912. .error = 1,
  913. },
  914. [6] = {
  915. .handler = icmp_discard,
  916. .error = 1,
  917. },
  918. [7] = {
  919. .handler = icmp_discard,
  920. .error = 1,
  921. },
  922. [ICMP_ECHO] = {
  923. .handler = icmp_echo,
  924. },
  925. [9] = {
  926. .handler = icmp_discard,
  927. .error = 1,
  928. },
  929. [10] = {
  930. .handler = icmp_discard,
  931. .error = 1,
  932. },
  933. [ICMP_TIME_EXCEEDED] = {
  934. .handler = icmp_unreach,
  935. .error = 1,
  936. },
  937. [ICMP_PARAMETERPROB] = {
  938. .handler = icmp_unreach,
  939. .error = 1,
  940. },
  941. [ICMP_TIMESTAMP] = {
  942. .handler = icmp_timestamp,
  943. },
  944. [ICMP_TIMESTAMPREPLY] = {
  945. .handler = icmp_discard,
  946. },
  947. [ICMP_INFO_REQUEST] = {
  948. .handler = icmp_discard,
  949. },
  950. [ICMP_INFO_REPLY] = {
  951. .handler = icmp_discard,
  952. },
  953. [ICMP_ADDRESS] = {
  954. .handler = icmp_address,
  955. },
  956. [ICMP_ADDRESSREPLY] = {
  957. .handler = icmp_address_reply,
  958. },
  959. };
  960. static void __net_exit icmp_sk_exit(struct net *net)
  961. {
  962. int i;
  963. for_each_possible_cpu(i)
  964. inet_ctl_sock_destroy(net->ipv4.icmp_sk[i]);
  965. kfree(net->ipv4.icmp_sk);
  966. net->ipv4.icmp_sk = NULL;
  967. }
  968. static int __net_init icmp_sk_init(struct net *net)
  969. {
  970. int i, err;
  971. net->ipv4.icmp_sk =
  972. kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL);
  973. if (net->ipv4.icmp_sk == NULL)
  974. return -ENOMEM;
  975. for_each_possible_cpu(i) {
  976. struct sock *sk;
  977. err = inet_ctl_sock_create(&sk, PF_INET,
  978. SOCK_RAW, IPPROTO_ICMP, net);
  979. if (err < 0)
  980. goto fail;
  981. net->ipv4.icmp_sk[i] = sk;
  982. /* Enough space for 2 64K ICMP packets, including
  983. * sk_buff/skb_shared_info struct overhead.
  984. */
  985. sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
  986. /*
  987. * Speedup sock_wfree()
  988. */
  989. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  990. inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
  991. }
  992. /* Control parameters for ECHO replies. */
  993. net->ipv4.sysctl_icmp_echo_ignore_all = 0;
  994. net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
  995. /* Control parameter - ignore bogus broadcast responses? */
  996. net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
  997. /*
  998. * Configurable global rate limit.
  999. *
  1000. * ratelimit defines tokens/packet consumed for dst->rate_token
  1001. * bucket ratemask defines which icmp types are ratelimited by
  1002. * setting it's bit position.
  1003. *
  1004. * default:
  1005. * dest unreachable (3), source quench (4),
  1006. * time exceeded (11), parameter problem (12)
  1007. */
  1008. net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
  1009. net->ipv4.sysctl_icmp_ratemask = 0x1818;
  1010. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
  1011. return 0;
  1012. fail:
  1013. for_each_possible_cpu(i)
  1014. inet_ctl_sock_destroy(net->ipv4.icmp_sk[i]);
  1015. kfree(net->ipv4.icmp_sk);
  1016. return err;
  1017. }
  1018. static struct pernet_operations __net_initdata icmp_sk_ops = {
  1019. .init = icmp_sk_init,
  1020. .exit = icmp_sk_exit,
  1021. };
  1022. int __init icmp_init(void)
  1023. {
  1024. return register_pernet_subsys(&icmp_sk_ops);
  1025. }