icmp.c 28 KB

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