icmp.c 27 KB

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