icmp.c 29 KB

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