icmp.c 29 KB

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