icmp.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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.shtx.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.shtx.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. err = xfrm_lookup(net, (struct dst_entry **)&rt, &fl, NULL, 0);
  511. switch (err) {
  512. case 0:
  513. if (rt != rt2)
  514. goto route_done;
  515. break;
  516. case -EPERM:
  517. rt = NULL;
  518. break;
  519. default:
  520. goto out_unlock;
  521. }
  522. if (xfrm_decode_session_reverse(skb_in, &fl, AF_INET))
  523. goto relookup_failed;
  524. if (inet_addr_type(net, fl.fl4_src) == RTN_LOCAL)
  525. err = __ip_route_output_key(net, &rt2, &fl);
  526. else {
  527. struct flowi fl2 = {};
  528. unsigned long orefdst;
  529. fl2.fl4_dst = fl.fl4_src;
  530. if (ip_route_output_key(net, &rt2, &fl2))
  531. goto relookup_failed;
  532. /* Ugh! */
  533. orefdst = skb_in->_skb_refdst; /* save old refdst */
  534. err = ip_route_input(skb_in, fl.fl4_dst, fl.fl4_src,
  535. RT_TOS(tos), rt2->dst.dev);
  536. dst_release(&rt2->dst);
  537. rt2 = skb_rtable(skb_in);
  538. skb_in->_skb_refdst = orefdst; /* restore old refdst */
  539. }
  540. if (err)
  541. goto relookup_failed;
  542. err = xfrm_lookup(net, (struct dst_entry **)&rt2, &fl, NULL,
  543. XFRM_LOOKUP_ICMP);
  544. switch (err) {
  545. case 0:
  546. dst_release(&rt->dst);
  547. rt = rt2;
  548. break;
  549. case -EPERM:
  550. goto ende;
  551. default:
  552. relookup_failed:
  553. if (!rt)
  554. goto out_unlock;
  555. break;
  556. }
  557. }
  558. route_done:
  559. if (!icmpv4_xrlim_allow(net, rt, type, code))
  560. goto ende;
  561. /* RFC says return as much as we can without exceeding 576 bytes. */
  562. room = dst_mtu(&rt->dst);
  563. if (room > 576)
  564. room = 576;
  565. room -= sizeof(struct iphdr) + icmp_param.replyopts.optlen;
  566. room -= sizeof(struct icmphdr);
  567. icmp_param.data_len = skb_in->len - icmp_param.offset;
  568. if (icmp_param.data_len > room)
  569. icmp_param.data_len = room;
  570. icmp_param.head_len = sizeof(struct icmphdr);
  571. icmp_push_reply(&icmp_param, &ipc, &rt);
  572. ende:
  573. ip_rt_put(rt);
  574. out_unlock:
  575. icmp_xmit_unlock(sk);
  576. out:;
  577. }
  578. EXPORT_SYMBOL(icmp_send);
  579. /*
  580. * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEED, and ICMP_QUENCH.
  581. */
  582. static void icmp_unreach(struct sk_buff *skb)
  583. {
  584. struct iphdr *iph;
  585. struct icmphdr *icmph;
  586. int hash, protocol;
  587. const struct net_protocol *ipprot;
  588. u32 info = 0;
  589. struct net *net;
  590. net = dev_net(skb_dst(skb)->dev);
  591. /*
  592. * Incomplete header ?
  593. * Only checks for the IP header, there should be an
  594. * additional check for longer headers in upper levels.
  595. */
  596. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  597. goto out_err;
  598. icmph = icmp_hdr(skb);
  599. iph = (struct iphdr *)skb->data;
  600. if (iph->ihl < 5) /* Mangled header, drop. */
  601. goto out_err;
  602. if (icmph->type == ICMP_DEST_UNREACH) {
  603. switch (icmph->code & 15) {
  604. case ICMP_NET_UNREACH:
  605. case ICMP_HOST_UNREACH:
  606. case ICMP_PROT_UNREACH:
  607. case ICMP_PORT_UNREACH:
  608. break;
  609. case ICMP_FRAG_NEEDED:
  610. if (ipv4_config.no_pmtu_disc) {
  611. LIMIT_NETDEBUG(KERN_INFO "ICMP: %pI4: fragmentation needed and DF set.\n",
  612. &iph->daddr);
  613. } else {
  614. info = ip_rt_frag_needed(net, iph,
  615. ntohs(icmph->un.frag.mtu),
  616. skb->dev);
  617. if (!info)
  618. goto out;
  619. }
  620. break;
  621. case ICMP_SR_FAILED:
  622. LIMIT_NETDEBUG(KERN_INFO "ICMP: %pI4: Source Route Failed.\n",
  623. &iph->daddr);
  624. break;
  625. default:
  626. break;
  627. }
  628. if (icmph->code > NR_ICMP_UNREACH)
  629. goto out;
  630. } else if (icmph->type == ICMP_PARAMETERPROB)
  631. info = ntohl(icmph->un.gateway) >> 24;
  632. /*
  633. * Throw it at our lower layers
  634. *
  635. * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
  636. * header.
  637. * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
  638. * transport layer.
  639. * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
  640. * transport layer.
  641. */
  642. /*
  643. * Check the other end isnt violating RFC 1122. Some routers send
  644. * bogus responses to broadcast frames. If you see this message
  645. * first check your netmask matches at both ends, if it does then
  646. * get the other vendor to fix their kit.
  647. */
  648. if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
  649. inet_addr_type(net, iph->daddr) == RTN_BROADCAST) {
  650. if (net_ratelimit())
  651. printk(KERN_WARNING "%pI4 sent an invalid ICMP "
  652. "type %u, code %u "
  653. "error to a broadcast: %pI4 on %s\n",
  654. &ip_hdr(skb)->saddr,
  655. icmph->type, icmph->code,
  656. &iph->daddr,
  657. skb->dev->name);
  658. goto out;
  659. }
  660. /* Checkin full IP header plus 8 bytes of protocol to
  661. * avoid additional coding at protocol handlers.
  662. */
  663. if (!pskb_may_pull(skb, iph->ihl * 4 + 8))
  664. goto out;
  665. iph = (struct iphdr *)skb->data;
  666. protocol = iph->protocol;
  667. /*
  668. * Deliver ICMP message to raw sockets. Pretty useless feature?
  669. */
  670. raw_icmp_error(skb, protocol, info);
  671. hash = protocol & (MAX_INET_PROTOS - 1);
  672. rcu_read_lock();
  673. ipprot = rcu_dereference(inet_protos[hash]);
  674. if (ipprot && ipprot->err_handler)
  675. ipprot->err_handler(skb, info);
  676. rcu_read_unlock();
  677. out:
  678. return;
  679. out_err:
  680. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  681. goto out;
  682. }
  683. /*
  684. * Handle ICMP_REDIRECT.
  685. */
  686. static void icmp_redirect(struct sk_buff *skb)
  687. {
  688. struct iphdr *iph;
  689. if (skb->len < sizeof(struct iphdr))
  690. goto out_err;
  691. /*
  692. * Get the copied header of the packet that caused the redirect
  693. */
  694. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  695. goto out;
  696. iph = (struct iphdr *)skb->data;
  697. switch (icmp_hdr(skb)->code & 7) {
  698. case ICMP_REDIR_NET:
  699. case ICMP_REDIR_NETTOS:
  700. /*
  701. * As per RFC recommendations now handle it as a host redirect.
  702. */
  703. case ICMP_REDIR_HOST:
  704. case ICMP_REDIR_HOSTTOS:
  705. ip_rt_redirect(ip_hdr(skb)->saddr, iph->daddr,
  706. icmp_hdr(skb)->un.gateway,
  707. iph->saddr, skb->dev);
  708. break;
  709. }
  710. out:
  711. return;
  712. out_err:
  713. ICMP_INC_STATS_BH(dev_net(skb->dev), ICMP_MIB_INERRORS);
  714. goto out;
  715. }
  716. /*
  717. * Handle ICMP_ECHO ("ping") requests.
  718. *
  719. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  720. * requests.
  721. * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
  722. * included in the reply.
  723. * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
  724. * echo requests, MUST have default=NOT.
  725. * See also WRT handling of options once they are done and working.
  726. */
  727. static void icmp_echo(struct sk_buff *skb)
  728. {
  729. struct net *net;
  730. net = dev_net(skb_dst(skb)->dev);
  731. if (!net->ipv4.sysctl_icmp_echo_ignore_all) {
  732. struct icmp_bxm icmp_param;
  733. icmp_param.data.icmph = *icmp_hdr(skb);
  734. icmp_param.data.icmph.type = ICMP_ECHOREPLY;
  735. icmp_param.skb = skb;
  736. icmp_param.offset = 0;
  737. icmp_param.data_len = skb->len;
  738. icmp_param.head_len = sizeof(struct icmphdr);
  739. icmp_reply(&icmp_param, skb);
  740. }
  741. }
  742. /*
  743. * Handle ICMP Timestamp requests.
  744. * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
  745. * SHOULD be in the kernel for minimum random latency.
  746. * MUST be accurate to a few minutes.
  747. * MUST be updated at least at 15Hz.
  748. */
  749. static void icmp_timestamp(struct sk_buff *skb)
  750. {
  751. struct timespec tv;
  752. struct icmp_bxm icmp_param;
  753. /*
  754. * Too short.
  755. */
  756. if (skb->len < 4)
  757. goto out_err;
  758. /*
  759. * Fill in the current time as ms since midnight UT:
  760. */
  761. getnstimeofday(&tv);
  762. icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC +
  763. tv.tv_nsec / NSEC_PER_MSEC);
  764. icmp_param.data.times[2] = icmp_param.data.times[1];
  765. if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
  766. BUG();
  767. icmp_param.data.icmph = *icmp_hdr(skb);
  768. icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
  769. icmp_param.data.icmph.code = 0;
  770. icmp_param.skb = skb;
  771. icmp_param.offset = 0;
  772. icmp_param.data_len = 0;
  773. icmp_param.head_len = sizeof(struct icmphdr) + 12;
  774. icmp_reply(&icmp_param, skb);
  775. out:
  776. return;
  777. out_err:
  778. ICMP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
  779. goto out;
  780. }
  781. /*
  782. * Handle ICMP_ADDRESS_MASK requests. (RFC950)
  783. *
  784. * RFC1122 (3.2.2.9). A host MUST only send replies to
  785. * ADDRESS_MASK requests if it's been configured as an address mask
  786. * agent. Receiving a request doesn't constitute implicit permission to
  787. * act as one. Of course, implementing this correctly requires (SHOULD)
  788. * a way to turn the functionality on and off. Another one for sysctl(),
  789. * I guess. -- MS
  790. *
  791. * RFC1812 (4.3.3.9). A router MUST implement it.
  792. * A router SHOULD have switch turning it on/off.
  793. * This switch MUST be ON by default.
  794. *
  795. * Gratuitous replies, zero-source replies are not implemented,
  796. * that complies with RFC. DO NOT implement them!!! All the idea
  797. * of broadcast addrmask replies as specified in RFC950 is broken.
  798. * The problem is that it is not uncommon to have several prefixes
  799. * on one physical interface. Moreover, addrmask agent can even be
  800. * not aware of existing another prefixes.
  801. * If source is zero, addrmask agent cannot choose correct prefix.
  802. * Gratuitous mask announcements suffer from the same problem.
  803. * RFC1812 explains it, but still allows to use ADDRMASK,
  804. * that is pretty silly. --ANK
  805. *
  806. * All these rules are so bizarre, that I removed kernel addrmask
  807. * support at all. It is wrong, it is obsolete, nobody uses it in
  808. * any case. --ANK
  809. *
  810. * Furthermore you can do it with a usermode address agent program
  811. * anyway...
  812. */
  813. static void icmp_address(struct sk_buff *skb)
  814. {
  815. #if 0
  816. if (net_ratelimit())
  817. printk(KERN_DEBUG "a guy asks for address mask. Who is it?\n");
  818. #endif
  819. }
  820. /*
  821. * RFC1812 (4.3.3.9). A router SHOULD listen all replies, and complain
  822. * loudly if an inconsistency is found.
  823. * called with rcu_read_lock()
  824. */
  825. static void icmp_address_reply(struct sk_buff *skb)
  826. {
  827. struct rtable *rt = skb_rtable(skb);
  828. struct net_device *dev = skb->dev;
  829. struct in_device *in_dev;
  830. struct in_ifaddr *ifa;
  831. if (skb->len < 4 || !(rt->rt_flags&RTCF_DIRECTSRC))
  832. return;
  833. in_dev = __in_dev_get_rcu(dev);
  834. if (!in_dev)
  835. return;
  836. if (in_dev->ifa_list &&
  837. IN_DEV_LOG_MARTIANS(in_dev) &&
  838. IN_DEV_FORWARD(in_dev)) {
  839. __be32 _mask, *mp;
  840. mp = skb_header_pointer(skb, 0, sizeof(_mask), &_mask);
  841. BUG_ON(mp == NULL);
  842. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  843. if (*mp == ifa->ifa_mask &&
  844. inet_ifa_match(rt->rt_src, ifa))
  845. break;
  846. }
  847. if (!ifa && net_ratelimit()) {
  848. printk(KERN_INFO "Wrong address mask %pI4 from %s/%pI4\n",
  849. mp, dev->name, &rt->rt_src);
  850. }
  851. }
  852. }
  853. static void icmp_discard(struct sk_buff *skb)
  854. {
  855. }
  856. /*
  857. * Deal with incoming ICMP packets.
  858. */
  859. int icmp_rcv(struct sk_buff *skb)
  860. {
  861. struct icmphdr *icmph;
  862. struct rtable *rt = skb_rtable(skb);
  863. struct net *net = dev_net(rt->dst.dev);
  864. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  865. struct sec_path *sp = skb_sec_path(skb);
  866. int nh;
  867. if (!(sp && sp->xvec[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(net, 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(net, 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. /*
  906. * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
  907. * silently ignored (we let user decide with a sysctl).
  908. * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
  909. * discarded if to broadcast/multicast.
  910. */
  911. if ((icmph->type == ICMP_ECHO ||
  912. icmph->type == ICMP_TIMESTAMP) &&
  913. net->ipv4.sysctl_icmp_echo_ignore_broadcasts) {
  914. goto error;
  915. }
  916. if (icmph->type != ICMP_ECHO &&
  917. icmph->type != ICMP_TIMESTAMP &&
  918. icmph->type != ICMP_ADDRESS &&
  919. icmph->type != ICMP_ADDRESSREPLY) {
  920. goto error;
  921. }
  922. }
  923. icmp_pointers[icmph->type].handler(skb);
  924. drop:
  925. kfree_skb(skb);
  926. return 0;
  927. error:
  928. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  929. goto drop;
  930. }
  931. /*
  932. * This table is the definition of how we handle ICMP.
  933. */
  934. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
  935. [ICMP_ECHOREPLY] = {
  936. .handler = icmp_discard,
  937. },
  938. [1] = {
  939. .handler = icmp_discard,
  940. .error = 1,
  941. },
  942. [2] = {
  943. .handler = icmp_discard,
  944. .error = 1,
  945. },
  946. [ICMP_DEST_UNREACH] = {
  947. .handler = icmp_unreach,
  948. .error = 1,
  949. },
  950. [ICMP_SOURCE_QUENCH] = {
  951. .handler = icmp_unreach,
  952. .error = 1,
  953. },
  954. [ICMP_REDIRECT] = {
  955. .handler = icmp_redirect,
  956. .error = 1,
  957. },
  958. [6] = {
  959. .handler = icmp_discard,
  960. .error = 1,
  961. },
  962. [7] = {
  963. .handler = icmp_discard,
  964. .error = 1,
  965. },
  966. [ICMP_ECHO] = {
  967. .handler = icmp_echo,
  968. },
  969. [9] = {
  970. .handler = icmp_discard,
  971. .error = 1,
  972. },
  973. [10] = {
  974. .handler = icmp_discard,
  975. .error = 1,
  976. },
  977. [ICMP_TIME_EXCEEDED] = {
  978. .handler = icmp_unreach,
  979. .error = 1,
  980. },
  981. [ICMP_PARAMETERPROB] = {
  982. .handler = icmp_unreach,
  983. .error = 1,
  984. },
  985. [ICMP_TIMESTAMP] = {
  986. .handler = icmp_timestamp,
  987. },
  988. [ICMP_TIMESTAMPREPLY] = {
  989. .handler = icmp_discard,
  990. },
  991. [ICMP_INFO_REQUEST] = {
  992. .handler = icmp_discard,
  993. },
  994. [ICMP_INFO_REPLY] = {
  995. .handler = icmp_discard,
  996. },
  997. [ICMP_ADDRESS] = {
  998. .handler = icmp_address,
  999. },
  1000. [ICMP_ADDRESSREPLY] = {
  1001. .handler = icmp_address_reply,
  1002. },
  1003. };
  1004. static void __net_exit icmp_sk_exit(struct net *net)
  1005. {
  1006. int i;
  1007. for_each_possible_cpu(i)
  1008. inet_ctl_sock_destroy(net->ipv4.icmp_sk[i]);
  1009. kfree(net->ipv4.icmp_sk);
  1010. net->ipv4.icmp_sk = NULL;
  1011. }
  1012. static int __net_init icmp_sk_init(struct net *net)
  1013. {
  1014. int i, err;
  1015. net->ipv4.icmp_sk =
  1016. kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL);
  1017. if (net->ipv4.icmp_sk == NULL)
  1018. return -ENOMEM;
  1019. for_each_possible_cpu(i) {
  1020. struct sock *sk;
  1021. err = inet_ctl_sock_create(&sk, PF_INET,
  1022. SOCK_RAW, IPPROTO_ICMP, net);
  1023. if (err < 0)
  1024. goto fail;
  1025. net->ipv4.icmp_sk[i] = sk;
  1026. /* Enough space for 2 64K ICMP packets, including
  1027. * sk_buff struct overhead.
  1028. */
  1029. sk->sk_sndbuf =
  1030. (2 * ((64 * 1024) + sizeof(struct sk_buff)));
  1031. /*
  1032. * Speedup sock_wfree()
  1033. */
  1034. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  1035. inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
  1036. }
  1037. /* Control parameters for ECHO replies. */
  1038. net->ipv4.sysctl_icmp_echo_ignore_all = 0;
  1039. net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
  1040. /* Control parameter - ignore bogus broadcast responses? */
  1041. net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
  1042. /*
  1043. * Configurable global rate limit.
  1044. *
  1045. * ratelimit defines tokens/packet consumed for dst->rate_token
  1046. * bucket ratemask defines which icmp types are ratelimited by
  1047. * setting it's bit position.
  1048. *
  1049. * default:
  1050. * dest unreachable (3), source quench (4),
  1051. * time exceeded (11), parameter problem (12)
  1052. */
  1053. net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
  1054. net->ipv4.sysctl_icmp_ratemask = 0x1818;
  1055. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
  1056. return 0;
  1057. fail:
  1058. for_each_possible_cpu(i)
  1059. inet_ctl_sock_destroy(net->ipv4.icmp_sk[i]);
  1060. kfree(net->ipv4.icmp_sk);
  1061. return err;
  1062. }
  1063. static struct pernet_operations __net_initdata icmp_sk_ops = {
  1064. .init = icmp_sk_init,
  1065. .exit = icmp_sk_exit,
  1066. };
  1067. int __init icmp_init(void)
  1068. {
  1069. return register_pernet_subsys(&icmp_sk_ops);
  1070. }