icmp.c 31 KB

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