icmp.c 28 KB

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