icmp.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. /*
  220. * Check transmit rate limitation for given message.
  221. * The rate information is held in the destination cache now.
  222. * This function is generic and could be used for other purposes
  223. * too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
  224. *
  225. * Note that the same dst_entry fields are modified by functions in
  226. * route.c too, but these work for packet destinations while xrlim_allow
  227. * works for icmp destinations. This means the rate limiting information
  228. * for one "ip object" is shared - and these ICMPs are twice limited:
  229. * by source and by destination.
  230. *
  231. * RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
  232. * SHOULD allow setting of rate limits
  233. *
  234. * Shared between ICMPv4 and ICMPv6.
  235. */
  236. #define XRLIM_BURST_FACTOR 6
  237. int xrlim_allow(struct dst_entry *dst, int timeout)
  238. {
  239. unsigned long now, token = dst->rate_tokens;
  240. int rc = 0;
  241. now = jiffies;
  242. token += now - dst->rate_last;
  243. dst->rate_last = now;
  244. if (token > XRLIM_BURST_FACTOR * timeout)
  245. token = XRLIM_BURST_FACTOR * timeout;
  246. if (token >= timeout) {
  247. token -= timeout;
  248. rc = 1;
  249. }
  250. dst->rate_tokens = token;
  251. return rc;
  252. }
  253. EXPORT_SYMBOL(xrlim_allow);
  254. static inline int icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
  255. int type, int code)
  256. {
  257. struct dst_entry *dst = &rt->dst;
  258. int rc = 1;
  259. if (type > NR_ICMP_TYPES)
  260. goto out;
  261. /* Don't limit PMTU discovery. */
  262. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
  263. goto out;
  264. /* No rate limit on loopback */
  265. if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
  266. goto out;
  267. /* Limit if icmp type is enabled in ratemask. */
  268. if ((1 << type) & net->ipv4.sysctl_icmp_ratemask)
  269. rc = xrlim_allow(dst, net->ipv4.sysctl_icmp_ratelimit);
  270. out:
  271. return rc;
  272. }
  273. /*
  274. * Maintain the counters used in the SNMP statistics for outgoing ICMP
  275. */
  276. void icmp_out_count(struct net *net, unsigned char type)
  277. {
  278. ICMPMSGOUT_INC_STATS(net, type);
  279. ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS);
  280. }
  281. /*
  282. * Checksum each fragment, and on the first include the headers and final
  283. * checksum.
  284. */
  285. static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
  286. struct sk_buff *skb)
  287. {
  288. struct icmp_bxm *icmp_param = (struct icmp_bxm *)from;
  289. __wsum csum;
  290. csum = skb_copy_and_csum_bits(icmp_param->skb,
  291. icmp_param->offset + offset,
  292. to, len, 0);
  293. skb->csum = csum_block_add(skb->csum, csum, odd);
  294. if (icmp_pointers[icmp_param->data.icmph.type].error)
  295. nf_ct_attach(skb, icmp_param->skb);
  296. return 0;
  297. }
  298. static void icmp_push_reply(struct icmp_bxm *icmp_param,
  299. struct ipcm_cookie *ipc, struct rtable **rt)
  300. {
  301. struct sock *sk;
  302. struct sk_buff *skb;
  303. sk = icmp_sk(dev_net((*rt)->dst.dev));
  304. if (ip_append_data(sk, icmp_glue_bits, icmp_param,
  305. icmp_param->data_len+icmp_param->head_len,
  306. icmp_param->head_len,
  307. ipc, rt, MSG_DONTWAIT) < 0) {
  308. ICMP_INC_STATS_BH(sock_net(sk), ICMP_MIB_OUTERRORS);
  309. ip_flush_pending_frames(sk);
  310. } else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
  311. struct icmphdr *icmph = icmp_hdr(skb);
  312. __wsum csum = 0;
  313. struct sk_buff *skb1;
  314. skb_queue_walk(&sk->sk_write_queue, skb1) {
  315. csum = csum_add(csum, skb1->csum);
  316. }
  317. csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
  318. (char *)icmph,
  319. icmp_param->head_len, csum);
  320. icmph->checksum = csum_fold(csum);
  321. skb->ip_summed = CHECKSUM_NONE;
  322. ip_push_pending_frames(sk);
  323. }
  324. }
  325. /*
  326. * Driving logic for building and sending ICMP messages.
  327. */
  328. static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
  329. {
  330. struct ipcm_cookie ipc;
  331. struct rtable *rt = skb_rtable(skb);
  332. struct net *net = dev_net(rt->dst.dev);
  333. struct sock *sk;
  334. struct inet_sock *inet;
  335. __be32 daddr;
  336. if (ip_options_echo(&icmp_param->replyopts, skb))
  337. return;
  338. sk = icmp_xmit_lock(net);
  339. if (sk == NULL)
  340. return;
  341. inet = inet_sk(sk);
  342. icmp_param->data.icmph.checksum = 0;
  343. inet->tos = ip_hdr(skb)->tos;
  344. daddr = ipc.addr = rt->rt_src;
  345. ipc.opt = NULL;
  346. ipc.tx_flags = 0;
  347. if (icmp_param->replyopts.optlen) {
  348. ipc.opt = &icmp_param->replyopts;
  349. if (ipc.opt->srr)
  350. daddr = icmp_param->replyopts.faddr;
  351. }
  352. {
  353. struct flowi fl = { .nl_u = { .ip4_u =
  354. { .daddr = daddr,
  355. .saddr = rt->rt_spec_dst,
  356. .tos = RT_TOS(ip_hdr(skb)->tos) } },
  357. .proto = IPPROTO_ICMP };
  358. security_skb_classify_flow(skb, &fl);
  359. if (ip_route_output_key(net, &rt, &fl))
  360. goto out_unlock;
  361. }
  362. if (icmpv4_xrlim_allow(net, rt, icmp_param->data.icmph.type,
  363. icmp_param->data.icmph.code))
  364. icmp_push_reply(icmp_param, &ipc, &rt);
  365. ip_rt_put(rt);
  366. out_unlock:
  367. icmp_xmit_unlock(sk);
  368. }
  369. /*
  370. * Send an ICMP message in response to a situation
  371. *
  372. * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
  373. * MAY send more (we do).
  374. * MUST NOT change this header information.
  375. * MUST NOT reply to a multicast/broadcast IP address.
  376. * MUST NOT reply to a multicast/broadcast MAC address.
  377. * MUST reply to only the first fragment.
  378. */
  379. void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
  380. {
  381. struct iphdr *iph;
  382. int room;
  383. struct icmp_bxm icmp_param;
  384. struct rtable *rt = skb_rtable(skb_in);
  385. struct ipcm_cookie ipc;
  386. __be32 saddr;
  387. u8 tos;
  388. struct net *net;
  389. struct sock *sk;
  390. if (!rt)
  391. goto out;
  392. net = dev_net(rt->dst.dev);
  393. /*
  394. * Find the original header. It is expected to be valid, of course.
  395. * Check this, icmp_send is called from the most obscure devices
  396. * sometimes.
  397. */
  398. iph = ip_hdr(skb_in);
  399. if ((u8 *)iph < skb_in->head ||
  400. (skb_in->network_header + sizeof(*iph)) > skb_in->tail)
  401. goto out;
  402. /*
  403. * No replies to physical multicast/broadcast
  404. */
  405. if (skb_in->pkt_type != PACKET_HOST)
  406. goto out;
  407. /*
  408. * Now check at the protocol level
  409. */
  410. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  411. goto out;
  412. /*
  413. * Only reply to fragment 0. We byte re-order the constant
  414. * mask for efficiency.
  415. */
  416. if (iph->frag_off & htons(IP_OFFSET))
  417. goto out;
  418. /*
  419. * If we send an ICMP error to an ICMP error a mess would result..
  420. */
  421. if (icmp_pointers[type].error) {
  422. /*
  423. * We are an error, check if we are replying to an
  424. * ICMP error
  425. */
  426. if (iph->protocol == IPPROTO_ICMP) {
  427. u8 _inner_type, *itp;
  428. itp = skb_header_pointer(skb_in,
  429. skb_network_header(skb_in) +
  430. (iph->ihl << 2) +
  431. offsetof(struct icmphdr,
  432. type) -
  433. skb_in->data,
  434. sizeof(_inner_type),
  435. &_inner_type);
  436. if (itp == NULL)
  437. goto out;
  438. /*
  439. * Assume any unknown ICMP type is an error. This
  440. * isn't specified by the RFC, but think about it..
  441. */
  442. if (*itp > NR_ICMP_TYPES ||
  443. icmp_pointers[*itp].error)
  444. goto out;
  445. }
  446. }
  447. sk = icmp_xmit_lock(net);
  448. if (sk == NULL)
  449. return;
  450. /*
  451. * Construct source address and options.
  452. */
  453. saddr = iph->daddr;
  454. if (!(rt->rt_flags & RTCF_LOCAL)) {
  455. struct net_device *dev = NULL;
  456. rcu_read_lock();
  457. if (rt->fl.iif &&
  458. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)
  459. dev = dev_get_by_index_rcu(net, rt->fl.iif);
  460. if (dev)
  461. saddr = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  462. else
  463. saddr = 0;
  464. rcu_read_unlock();
  465. }
  466. tos = icmp_pointers[type].error ? ((iph->tos & IPTOS_TOS_MASK) |
  467. IPTOS_PREC_INTERNETCONTROL) :
  468. iph->tos;
  469. if (ip_options_echo(&icmp_param.replyopts, skb_in))
  470. goto out_unlock;
  471. /*
  472. * Prepare data for ICMP header.
  473. */
  474. icmp_param.data.icmph.type = type;
  475. icmp_param.data.icmph.code = code;
  476. icmp_param.data.icmph.un.gateway = info;
  477. icmp_param.data.icmph.checksum = 0;
  478. icmp_param.skb = skb_in;
  479. icmp_param.offset = skb_network_offset(skb_in);
  480. inet_sk(sk)->tos = tos;
  481. ipc.addr = iph->saddr;
  482. ipc.opt = &icmp_param.replyopts;
  483. ipc.tx_flags = 0;
  484. {
  485. struct flowi fl = {
  486. .nl_u = {
  487. .ip4_u = {
  488. .daddr = icmp_param.replyopts.srr ?
  489. icmp_param.replyopts.faddr :
  490. iph->saddr,
  491. .saddr = saddr,
  492. .tos = RT_TOS(tos)
  493. }
  494. },
  495. .proto = IPPROTO_ICMP,
  496. .uli_u = {
  497. .icmpt = {
  498. .type = type,
  499. .code = code
  500. }
  501. }
  502. };
  503. int err;
  504. struct rtable *rt2;
  505. security_skb_classify_flow(skb_in, &fl);
  506. if (__ip_route_output_key(net, &rt, &fl))
  507. goto out_unlock;
  508. /* No need to clone since we're just using its address. */
  509. rt2 = rt;
  510. if (!fl.nl_u.ip4_u.saddr)
  511. fl.nl_u.ip4_u.saddr = rt->rt_src;
  512. err = xfrm_lookup(net, (struct dst_entry **)&rt, &fl, NULL, 0);
  513. switch (err) {
  514. case 0:
  515. if (rt != rt2)
  516. goto route_done;
  517. break;
  518. case -EPERM:
  519. rt = NULL;
  520. break;
  521. default:
  522. goto out_unlock;
  523. }
  524. if (xfrm_decode_session_reverse(skb_in, &fl, AF_INET))
  525. goto relookup_failed;
  526. if (inet_addr_type(net, fl.fl4_src) == RTN_LOCAL)
  527. err = __ip_route_output_key(net, &rt2, &fl);
  528. else {
  529. struct flowi fl2 = {};
  530. unsigned long orefdst;
  531. fl2.fl4_dst = fl.fl4_src;
  532. if (ip_route_output_key(net, &rt2, &fl2))
  533. goto relookup_failed;
  534. /* Ugh! */
  535. orefdst = skb_in->_skb_refdst; /* save old refdst */
  536. err = ip_route_input(skb_in, fl.fl4_dst, fl.fl4_src,
  537. RT_TOS(tos), rt2->dst.dev);
  538. dst_release(&rt2->dst);
  539. rt2 = skb_rtable(skb_in);
  540. skb_in->_skb_refdst = orefdst; /* restore old refdst */
  541. }
  542. if (err)
  543. goto relookup_failed;
  544. err = xfrm_lookup(net, (struct dst_entry **)&rt2, &fl, NULL,
  545. XFRM_LOOKUP_ICMP);
  546. switch (err) {
  547. case 0:
  548. dst_release(&rt->dst);
  549. rt = rt2;
  550. break;
  551. case -EPERM:
  552. goto ende;
  553. default:
  554. relookup_failed:
  555. if (!rt)
  556. goto out_unlock;
  557. break;
  558. }
  559. }
  560. route_done:
  561. if (!icmpv4_xrlim_allow(net, rt, type, code))
  562. goto ende;
  563. /* RFC says return as much as we can without exceeding 576 bytes. */
  564. room = dst_mtu(&rt->dst);
  565. if (room > 576)
  566. room = 576;
  567. room -= sizeof(struct iphdr) + icmp_param.replyopts.optlen;
  568. room -= sizeof(struct icmphdr);
  569. icmp_param.data_len = skb_in->len - icmp_param.offset;
  570. if (icmp_param.data_len > room)
  571. icmp_param.data_len = room;
  572. icmp_param.head_len = sizeof(struct icmphdr);
  573. icmp_push_reply(&icmp_param, &ipc, &rt);
  574. ende:
  575. ip_rt_put(rt);
  576. out_unlock:
  577. icmp_xmit_unlock(sk);
  578. out:;
  579. }
  580. EXPORT_SYMBOL(icmp_send);
  581. /*
  582. * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEED, and ICMP_QUENCH.
  583. */
  584. static void icmp_unreach(struct sk_buff *skb)
  585. {
  586. struct iphdr *iph;
  587. struct icmphdr *icmph;
  588. int hash, protocol;
  589. const struct net_protocol *ipprot;
  590. u32 info = 0;
  591. struct net *net;
  592. net = dev_net(skb_dst(skb)->dev);
  593. /*
  594. * Incomplete header ?
  595. * Only checks for the IP header, there should be an
  596. * additional check for longer headers in upper levels.
  597. */
  598. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  599. goto out_err;
  600. icmph = icmp_hdr(skb);
  601. iph = (struct iphdr *)skb->data;
  602. if (iph->ihl < 5) /* Mangled header, drop. */
  603. goto out_err;
  604. if (icmph->type == ICMP_DEST_UNREACH) {
  605. switch (icmph->code & 15) {
  606. case ICMP_NET_UNREACH:
  607. case ICMP_HOST_UNREACH:
  608. case ICMP_PROT_UNREACH:
  609. case ICMP_PORT_UNREACH:
  610. break;
  611. case ICMP_FRAG_NEEDED:
  612. if (ipv4_config.no_pmtu_disc) {
  613. LIMIT_NETDEBUG(KERN_INFO "ICMP: %pI4: fragmentation needed and DF set.\n",
  614. &iph->daddr);
  615. } else {
  616. info = ip_rt_frag_needed(net, iph,
  617. ntohs(icmph->un.frag.mtu),
  618. skb->dev);
  619. if (!info)
  620. goto out;
  621. }
  622. break;
  623. case ICMP_SR_FAILED:
  624. LIMIT_NETDEBUG(KERN_INFO "ICMP: %pI4: Source Route Failed.\n",
  625. &iph->daddr);
  626. break;
  627. default:
  628. break;
  629. }
  630. if (icmph->code > NR_ICMP_UNREACH)
  631. goto out;
  632. } else if (icmph->type == ICMP_PARAMETERPROB)
  633. info = ntohl(icmph->un.gateway) >> 24;
  634. /*
  635. * Throw it at our lower layers
  636. *
  637. * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
  638. * header.
  639. * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
  640. * transport layer.
  641. * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
  642. * transport layer.
  643. */
  644. /*
  645. * Check the other end isnt violating RFC 1122. Some routers send
  646. * bogus responses to broadcast frames. If you see this message
  647. * first check your netmask matches at both ends, if it does then
  648. * get the other vendor to fix their kit.
  649. */
  650. if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
  651. inet_addr_type(net, iph->daddr) == RTN_BROADCAST) {
  652. if (net_ratelimit())
  653. printk(KERN_WARNING "%pI4 sent an invalid ICMP "
  654. "type %u, code %u "
  655. "error to a broadcast: %pI4 on %s\n",
  656. &ip_hdr(skb)->saddr,
  657. icmph->type, icmph->code,
  658. &iph->daddr,
  659. skb->dev->name);
  660. goto out;
  661. }
  662. /* Checkin full IP header plus 8 bytes of protocol to
  663. * avoid additional coding at protocol handlers.
  664. */
  665. if (!pskb_may_pull(skb, iph->ihl * 4 + 8))
  666. goto out;
  667. iph = (struct iphdr *)skb->data;
  668. protocol = iph->protocol;
  669. /*
  670. * Deliver ICMP message to raw sockets. Pretty useless feature?
  671. */
  672. raw_icmp_error(skb, protocol, info);
  673. hash = protocol & (MAX_INET_PROTOS - 1);
  674. rcu_read_lock();
  675. ipprot = rcu_dereference(inet_protos[hash]);
  676. if (ipprot && ipprot->err_handler)
  677. ipprot->err_handler(skb, info);
  678. rcu_read_unlock();
  679. out:
  680. return;
  681. out_err:
  682. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  683. goto out;
  684. }
  685. /*
  686. * Handle ICMP_REDIRECT.
  687. */
  688. static void icmp_redirect(struct sk_buff *skb)
  689. {
  690. struct iphdr *iph;
  691. if (skb->len < sizeof(struct iphdr))
  692. goto out_err;
  693. /*
  694. * Get the copied header of the packet that caused the redirect
  695. */
  696. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  697. goto out;
  698. iph = (struct iphdr *)skb->data;
  699. switch (icmp_hdr(skb)->code & 7) {
  700. case ICMP_REDIR_NET:
  701. case ICMP_REDIR_NETTOS:
  702. /*
  703. * As per RFC recommendations now handle it as a host redirect.
  704. */
  705. case ICMP_REDIR_HOST:
  706. case ICMP_REDIR_HOSTTOS:
  707. ip_rt_redirect(ip_hdr(skb)->saddr, iph->daddr,
  708. icmp_hdr(skb)->un.gateway,
  709. iph->saddr, skb->dev);
  710. break;
  711. }
  712. out:
  713. return;
  714. out_err:
  715. ICMP_INC_STATS_BH(dev_net(skb->dev), ICMP_MIB_INERRORS);
  716. goto out;
  717. }
  718. /*
  719. * Handle ICMP_ECHO ("ping") requests.
  720. *
  721. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  722. * requests.
  723. * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
  724. * included in the reply.
  725. * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
  726. * echo requests, MUST have default=NOT.
  727. * See also WRT handling of options once they are done and working.
  728. */
  729. static void icmp_echo(struct sk_buff *skb)
  730. {
  731. struct net *net;
  732. net = dev_net(skb_dst(skb)->dev);
  733. if (!net->ipv4.sysctl_icmp_echo_ignore_all) {
  734. struct icmp_bxm icmp_param;
  735. icmp_param.data.icmph = *icmp_hdr(skb);
  736. icmp_param.data.icmph.type = ICMP_ECHOREPLY;
  737. icmp_param.skb = skb;
  738. icmp_param.offset = 0;
  739. icmp_param.data_len = skb->len;
  740. icmp_param.head_len = sizeof(struct icmphdr);
  741. icmp_reply(&icmp_param, skb);
  742. }
  743. }
  744. /*
  745. * Handle ICMP Timestamp requests.
  746. * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
  747. * SHOULD be in the kernel for minimum random latency.
  748. * MUST be accurate to a few minutes.
  749. * MUST be updated at least at 15Hz.
  750. */
  751. static void icmp_timestamp(struct sk_buff *skb)
  752. {
  753. struct timespec tv;
  754. struct icmp_bxm icmp_param;
  755. /*
  756. * Too short.
  757. */
  758. if (skb->len < 4)
  759. goto out_err;
  760. /*
  761. * Fill in the current time as ms since midnight UT:
  762. */
  763. getnstimeofday(&tv);
  764. icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC +
  765. tv.tv_nsec / NSEC_PER_MSEC);
  766. icmp_param.data.times[2] = icmp_param.data.times[1];
  767. if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
  768. BUG();
  769. icmp_param.data.icmph = *icmp_hdr(skb);
  770. icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
  771. icmp_param.data.icmph.code = 0;
  772. icmp_param.skb = skb;
  773. icmp_param.offset = 0;
  774. icmp_param.data_len = 0;
  775. icmp_param.head_len = sizeof(struct icmphdr) + 12;
  776. icmp_reply(&icmp_param, skb);
  777. out:
  778. return;
  779. out_err:
  780. ICMP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
  781. goto out;
  782. }
  783. /*
  784. * Handle ICMP_ADDRESS_MASK requests. (RFC950)
  785. *
  786. * RFC1122 (3.2.2.9). A host MUST only send replies to
  787. * ADDRESS_MASK requests if it's been configured as an address mask
  788. * agent. Receiving a request doesn't constitute implicit permission to
  789. * act as one. Of course, implementing this correctly requires (SHOULD)
  790. * a way to turn the functionality on and off. Another one for sysctl(),
  791. * I guess. -- MS
  792. *
  793. * RFC1812 (4.3.3.9). A router MUST implement it.
  794. * A router SHOULD have switch turning it on/off.
  795. * This switch MUST be ON by default.
  796. *
  797. * Gratuitous replies, zero-source replies are not implemented,
  798. * that complies with RFC. DO NOT implement them!!! All the idea
  799. * of broadcast addrmask replies as specified in RFC950 is broken.
  800. * The problem is that it is not uncommon to have several prefixes
  801. * on one physical interface. Moreover, addrmask agent can even be
  802. * not aware of existing another prefixes.
  803. * If source is zero, addrmask agent cannot choose correct prefix.
  804. * Gratuitous mask announcements suffer from the same problem.
  805. * RFC1812 explains it, but still allows to use ADDRMASK,
  806. * that is pretty silly. --ANK
  807. *
  808. * All these rules are so bizarre, that I removed kernel addrmask
  809. * support at all. It is wrong, it is obsolete, nobody uses it in
  810. * any case. --ANK
  811. *
  812. * Furthermore you can do it with a usermode address agent program
  813. * anyway...
  814. */
  815. static void icmp_address(struct sk_buff *skb)
  816. {
  817. #if 0
  818. if (net_ratelimit())
  819. printk(KERN_DEBUG "a guy asks for address mask. Who is it?\n");
  820. #endif
  821. }
  822. /*
  823. * RFC1812 (4.3.3.9). A router SHOULD listen all replies, and complain
  824. * loudly if an inconsistency is found.
  825. * called with rcu_read_lock()
  826. */
  827. static void icmp_address_reply(struct sk_buff *skb)
  828. {
  829. struct rtable *rt = skb_rtable(skb);
  830. struct net_device *dev = skb->dev;
  831. struct in_device *in_dev;
  832. struct in_ifaddr *ifa;
  833. if (skb->len < 4 || !(rt->rt_flags&RTCF_DIRECTSRC))
  834. return;
  835. in_dev = __in_dev_get_rcu(dev);
  836. if (!in_dev)
  837. return;
  838. if (in_dev->ifa_list &&
  839. IN_DEV_LOG_MARTIANS(in_dev) &&
  840. IN_DEV_FORWARD(in_dev)) {
  841. __be32 _mask, *mp;
  842. mp = skb_header_pointer(skb, 0, sizeof(_mask), &_mask);
  843. BUG_ON(mp == NULL);
  844. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  845. if (*mp == ifa->ifa_mask &&
  846. inet_ifa_match(rt->rt_src, ifa))
  847. break;
  848. }
  849. if (!ifa && net_ratelimit()) {
  850. printk(KERN_INFO "Wrong address mask %pI4 from %s/%pI4\n",
  851. mp, dev->name, &rt->rt_src);
  852. }
  853. }
  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(skb);
  865. struct net *net = dev_net(rt->dst.dev);
  866. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  867. struct sec_path *sp = skb_sec_path(skb);
  868. int nh;
  869. if (!(sp && sp->xvec[sp->len - 1]->props.flags &
  870. XFRM_STATE_ICMP))
  871. goto drop;
  872. if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
  873. goto drop;
  874. nh = skb_network_offset(skb);
  875. skb_set_network_header(skb, sizeof(*icmph));
  876. if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
  877. goto drop;
  878. skb_set_network_header(skb, nh);
  879. }
  880. ICMP_INC_STATS_BH(net, ICMP_MIB_INMSGS);
  881. switch (skb->ip_summed) {
  882. case CHECKSUM_COMPLETE:
  883. if (!csum_fold(skb->csum))
  884. break;
  885. /* fall through */
  886. case CHECKSUM_NONE:
  887. skb->csum = 0;
  888. if (__skb_checksum_complete(skb))
  889. goto error;
  890. }
  891. if (!pskb_pull(skb, sizeof(*icmph)))
  892. goto error;
  893. icmph = icmp_hdr(skb);
  894. ICMPMSGIN_INC_STATS_BH(net, icmph->type);
  895. /*
  896. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  897. *
  898. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  899. * discarded.
  900. */
  901. if (icmph->type > NR_ICMP_TYPES)
  902. goto error;
  903. /*
  904. * Parse the ICMP message
  905. */
  906. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  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(net, 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. /*
  1034. * Speedup sock_wfree()
  1035. */
  1036. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  1037. inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
  1038. }
  1039. /* Control parameters for ECHO replies. */
  1040. net->ipv4.sysctl_icmp_echo_ignore_all = 0;
  1041. net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
  1042. /* Control parameter - ignore bogus broadcast responses? */
  1043. net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
  1044. /*
  1045. * Configurable global rate limit.
  1046. *
  1047. * ratelimit defines tokens/packet consumed for dst->rate_token
  1048. * bucket ratemask defines which icmp types are ratelimited by
  1049. * setting it's bit position.
  1050. *
  1051. * default:
  1052. * dest unreachable (3), source quench (4),
  1053. * time exceeded (11), parameter problem (12)
  1054. */
  1055. net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
  1056. net->ipv4.sysctl_icmp_ratemask = 0x1818;
  1057. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
  1058. return 0;
  1059. fail:
  1060. for_each_possible_cpu(i)
  1061. inet_ctl_sock_destroy(net->ipv4.icmp_sk[i]);
  1062. kfree(net->ipv4.icmp_sk);
  1063. return err;
  1064. }
  1065. static struct pernet_operations __net_initdata icmp_sk_ops = {
  1066. .init = icmp_sk_init,
  1067. .exit = icmp_sk_exit,
  1068. };
  1069. int __init icmp_init(void)
  1070. {
  1071. return register_pernet_subsys(&icmp_sk_ops);
  1072. }