icmp.c 28 KB

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