icmp.c 29 KB

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