icmp.c 28 KB

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