icmp.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  65. #include <linux/module.h>
  66. #include <linux/types.h>
  67. #include <linux/jiffies.h>
  68. #include <linux/kernel.h>
  69. #include <linux/fcntl.h>
  70. #include <linux/socket.h>
  71. #include <linux/in.h>
  72. #include <linux/inet.h>
  73. #include <linux/inetdevice.h>
  74. #include <linux/netdevice.h>
  75. #include <linux/string.h>
  76. #include <linux/netfilter_ipv4.h>
  77. #include <linux/slab.h>
  78. #include <net/snmp.h>
  79. #include <net/ip.h>
  80. #include <net/route.h>
  81. #include <net/protocol.h>
  82. #include <net/icmp.h>
  83. #include <net/tcp.h>
  84. #include <net/udp.h>
  85. #include <net/raw.h>
  86. #include <net/ping.h>
  87. #include <linux/skbuff.h>
  88. #include <net/sock.h>
  89. #include <linux/errno.h>
  90. #include <linux/timer.h>
  91. #include <linux/init.h>
  92. #include <asm/uaccess.h>
  93. #include <net/checksum.h>
  94. #include <net/xfrm.h>
  95. #include <net/inet_common.h>
  96. /*
  97. * Build xmit assembly blocks
  98. */
  99. struct icmp_bxm {
  100. struct sk_buff *skb;
  101. int offset;
  102. int data_len;
  103. struct {
  104. struct icmphdr icmph;
  105. __be32 times[3];
  106. } data;
  107. int head_len;
  108. struct ip_options_data replyopts;
  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. struct flowi4 *fl4, 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, fl4->daddr, 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 flowi4 *fl4,
  269. struct ipcm_cookie *ipc, struct rtable **rt)
  270. {
  271. struct sock *sk;
  272. struct sk_buff *skb;
  273. sk = icmp_sk(dev_net((*rt)->dst.dev));
  274. if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
  275. icmp_param->data_len+icmp_param->head_len,
  276. icmp_param->head_len,
  277. ipc, rt, MSG_DONTWAIT) < 0) {
  278. ICMP_INC_STATS_BH(sock_net(sk), ICMP_MIB_OUTERRORS);
  279. ip_flush_pending_frames(sk);
  280. } else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
  281. struct icmphdr *icmph = icmp_hdr(skb);
  282. __wsum csum = 0;
  283. struct sk_buff *skb1;
  284. skb_queue_walk(&sk->sk_write_queue, skb1) {
  285. csum = csum_add(csum, skb1->csum);
  286. }
  287. csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
  288. (char *)icmph,
  289. icmp_param->head_len, csum);
  290. icmph->checksum = csum_fold(csum);
  291. skb->ip_summed = CHECKSUM_NONE;
  292. ip_push_pending_frames(sk, fl4);
  293. }
  294. }
  295. /*
  296. * Driving logic for building and sending ICMP messages.
  297. */
  298. static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
  299. {
  300. struct ipcm_cookie ipc;
  301. struct rtable *rt = skb_rtable(skb);
  302. struct net *net = dev_net(rt->dst.dev);
  303. struct flowi4 fl4;
  304. struct sock *sk;
  305. struct inet_sock *inet;
  306. __be32 daddr;
  307. u32 mark = IP4_REPLY_MARK(net, skb->mark);
  308. if (ip_options_echo(&icmp_param->replyopts.opt.opt, skb))
  309. return;
  310. sk = icmp_xmit_lock(net);
  311. if (sk == NULL)
  312. return;
  313. inet = inet_sk(sk);
  314. icmp_param->data.icmph.checksum = 0;
  315. inet->tos = ip_hdr(skb)->tos;
  316. sk->sk_mark = mark;
  317. daddr = ipc.addr = ip_hdr(skb)->saddr;
  318. ipc.opt = NULL;
  319. ipc.tx_flags = 0;
  320. if (icmp_param->replyopts.opt.opt.optlen) {
  321. ipc.opt = &icmp_param->replyopts.opt;
  322. if (ipc.opt->opt.srr)
  323. daddr = icmp_param->replyopts.opt.opt.faddr;
  324. }
  325. memset(&fl4, 0, sizeof(fl4));
  326. fl4.daddr = daddr;
  327. fl4.saddr = rt->rt_spec_dst;
  328. fl4.flowi4_mark = mark;
  329. fl4.flowi4_uid = sock_net_uid(net, NULL);
  330. fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
  331. fl4.flowi4_proto = IPPROTO_ICMP;
  332. security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
  333. rt = ip_route_output_key(net, &fl4);
  334. if (IS_ERR(rt))
  335. goto out_unlock;
  336. if (icmpv4_xrlim_allow(net, rt, &fl4, icmp_param->data.icmph.type,
  337. icmp_param->data.icmph.code))
  338. icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
  339. ip_rt_put(rt);
  340. out_unlock:
  341. icmp_xmit_unlock(sk);
  342. }
  343. static struct rtable *icmp_route_lookup(struct net *net,
  344. struct flowi4 *fl4,
  345. struct sk_buff *skb_in,
  346. const struct iphdr *iph,
  347. __be32 saddr, u8 tos, u32 mark,
  348. int type, int code,
  349. struct icmp_bxm *param)
  350. {
  351. struct rtable *rt, *rt2;
  352. struct flowi4 fl4_dec;
  353. int err;
  354. memset(fl4, 0, sizeof(*fl4));
  355. fl4->daddr = (param->replyopts.opt.opt.srr ?
  356. param->replyopts.opt.opt.faddr : iph->saddr);
  357. fl4->saddr = saddr;
  358. fl4->flowi4_mark = mark;
  359. fl4->flowi4_uid = sock_net_uid(net, NULL);
  360. fl4->flowi4_tos = RT_TOS(tos);
  361. fl4->flowi4_proto = IPPROTO_ICMP;
  362. fl4->fl4_icmp_type = type;
  363. fl4->fl4_icmp_code = code;
  364. security_skb_classify_flow(skb_in, flowi4_to_flowi(fl4));
  365. rt = __ip_route_output_key(net, fl4);
  366. if (IS_ERR(rt))
  367. return rt;
  368. /* No need to clone since we're just using its address. */
  369. rt2 = rt;
  370. rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
  371. flowi4_to_flowi(fl4), NULL, 0);
  372. if (!IS_ERR(rt)) {
  373. if (rt != rt2)
  374. return rt;
  375. } else if (PTR_ERR(rt) == -EPERM) {
  376. rt = NULL;
  377. } else
  378. return rt;
  379. err = xfrm_decode_session_reverse(skb_in, flowi4_to_flowi(&fl4_dec), AF_INET);
  380. if (err)
  381. goto relookup_failed;
  382. if (inet_addr_type(net, fl4_dec.saddr) == RTN_LOCAL) {
  383. rt2 = __ip_route_output_key(net, &fl4_dec);
  384. if (IS_ERR(rt2))
  385. err = PTR_ERR(rt2);
  386. } else {
  387. struct flowi4 fl4_2 = {};
  388. unsigned long orefdst;
  389. fl4_2.daddr = fl4_dec.saddr;
  390. rt2 = ip_route_output_key(net, &fl4_2);
  391. if (IS_ERR(rt2)) {
  392. err = PTR_ERR(rt2);
  393. goto relookup_failed;
  394. }
  395. /* Ugh! */
  396. orefdst = skb_in->_skb_refdst; /* save old refdst */
  397. err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr,
  398. RT_TOS(tos), rt2->dst.dev);
  399. dst_release(&rt2->dst);
  400. rt2 = skb_rtable(skb_in);
  401. skb_in->_skb_refdst = orefdst; /* restore old refdst */
  402. }
  403. if (err)
  404. goto relookup_failed;
  405. rt2 = (struct rtable *) xfrm_lookup(net, &rt2->dst,
  406. flowi4_to_flowi(&fl4_dec), NULL,
  407. XFRM_LOOKUP_ICMP);
  408. if (!IS_ERR(rt2)) {
  409. dst_release(&rt->dst);
  410. memcpy(fl4, &fl4_dec, sizeof(*fl4));
  411. rt = rt2;
  412. } else if (PTR_ERR(rt2) == -EPERM) {
  413. if (rt)
  414. dst_release(&rt->dst);
  415. return rt2;
  416. } else {
  417. err = PTR_ERR(rt2);
  418. goto relookup_failed;
  419. }
  420. return rt;
  421. relookup_failed:
  422. if (rt)
  423. return rt;
  424. return ERR_PTR(err);
  425. }
  426. /*
  427. * Send an ICMP message in response to a situation
  428. *
  429. * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
  430. * MAY send more (we do).
  431. * MUST NOT change this header information.
  432. * MUST NOT reply to a multicast/broadcast IP address.
  433. * MUST NOT reply to a multicast/broadcast MAC address.
  434. * MUST reply to only the first fragment.
  435. */
  436. void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
  437. {
  438. struct iphdr *iph;
  439. int room;
  440. struct icmp_bxm icmp_param;
  441. struct rtable *rt = skb_rtable(skb_in);
  442. struct ipcm_cookie ipc;
  443. struct flowi4 fl4;
  444. __be32 saddr;
  445. u8 tos;
  446. u32 mark;
  447. struct net *net;
  448. struct sock *sk;
  449. if (!rt)
  450. goto out;
  451. net = dev_net(rt->dst.dev);
  452. /*
  453. * Find the original header. It is expected to be valid, of course.
  454. * Check this, icmp_send is called from the most obscure devices
  455. * sometimes.
  456. */
  457. iph = ip_hdr(skb_in);
  458. if ((u8 *)iph < skb_in->head ||
  459. (skb_in->network_header + sizeof(*iph)) > skb_in->tail)
  460. goto out;
  461. /*
  462. * No replies to physical multicast/broadcast
  463. */
  464. if (skb_in->pkt_type != PACKET_HOST)
  465. goto out;
  466. /*
  467. * Now check at the protocol level
  468. */
  469. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  470. goto out;
  471. /*
  472. * Only reply to fragment 0. We byte re-order the constant
  473. * mask for efficiency.
  474. */
  475. if (iph->frag_off & htons(IP_OFFSET))
  476. goto out;
  477. /*
  478. * If we send an ICMP error to an ICMP error a mess would result..
  479. */
  480. if (icmp_pointers[type].error) {
  481. /*
  482. * We are an error, check if we are replying to an
  483. * ICMP error
  484. */
  485. if (iph->protocol == IPPROTO_ICMP) {
  486. u8 _inner_type, *itp;
  487. itp = skb_header_pointer(skb_in,
  488. skb_network_header(skb_in) +
  489. (iph->ihl << 2) +
  490. offsetof(struct icmphdr,
  491. type) -
  492. skb_in->data,
  493. sizeof(_inner_type),
  494. &_inner_type);
  495. if (itp == NULL)
  496. goto out;
  497. /*
  498. * Assume any unknown ICMP type is an error. This
  499. * isn't specified by the RFC, but think about it..
  500. */
  501. if (*itp > NR_ICMP_TYPES ||
  502. icmp_pointers[*itp].error)
  503. goto out;
  504. }
  505. }
  506. sk = icmp_xmit_lock(net);
  507. if (sk == NULL)
  508. return;
  509. /*
  510. * Construct source address and options.
  511. */
  512. saddr = iph->daddr;
  513. if (!(rt->rt_flags & RTCF_LOCAL)) {
  514. struct net_device *dev = NULL;
  515. rcu_read_lock();
  516. if (rt_is_input_route(rt) &&
  517. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)
  518. dev = dev_get_by_index_rcu(net, rt->rt_iif);
  519. if (dev)
  520. saddr = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  521. else
  522. saddr = 0;
  523. rcu_read_unlock();
  524. }
  525. tos = icmp_pointers[type].error ? ((iph->tos & IPTOS_TOS_MASK) |
  526. IPTOS_PREC_INTERNETCONTROL) :
  527. iph->tos;
  528. mark = IP4_REPLY_MARK(net, skb_in->mark);
  529. if (ip_options_echo(&icmp_param.replyopts.opt.opt, skb_in))
  530. goto out_unlock;
  531. /*
  532. * Prepare data for ICMP header.
  533. */
  534. icmp_param.data.icmph.type = type;
  535. icmp_param.data.icmph.code = code;
  536. icmp_param.data.icmph.un.gateway = info;
  537. icmp_param.data.icmph.checksum = 0;
  538. icmp_param.skb = skb_in;
  539. icmp_param.offset = skb_network_offset(skb_in);
  540. inet_sk(sk)->tos = tos;
  541. sk->sk_mark = mark;
  542. ipc.addr = iph->saddr;
  543. ipc.opt = &icmp_param.replyopts.opt;
  544. ipc.tx_flags = 0;
  545. rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos, mark,
  546. type, code, &icmp_param);
  547. if (IS_ERR(rt))
  548. goto out_unlock;
  549. if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code))
  550. goto ende;
  551. /* RFC says return as much as we can without exceeding 576 bytes. */
  552. room = dst_mtu(&rt->dst);
  553. if (room > 576)
  554. room = 576;
  555. room -= sizeof(struct iphdr) + icmp_param.replyopts.opt.opt.optlen;
  556. room -= sizeof(struct icmphdr);
  557. icmp_param.data_len = skb_in->len - icmp_param.offset;
  558. if (icmp_param.data_len > room)
  559. icmp_param.data_len = room;
  560. icmp_param.head_len = sizeof(struct icmphdr);
  561. icmp_push_reply(&icmp_param, &fl4, &ipc, &rt);
  562. ende:
  563. ip_rt_put(rt);
  564. out_unlock:
  565. icmp_xmit_unlock(sk);
  566. out:;
  567. }
  568. EXPORT_SYMBOL(icmp_send);
  569. /*
  570. * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEED, and ICMP_QUENCH.
  571. */
  572. static void icmp_unreach(struct sk_buff *skb)
  573. {
  574. const struct iphdr *iph;
  575. struct icmphdr *icmph;
  576. int hash, protocol;
  577. const struct net_protocol *ipprot;
  578. u32 info = 0;
  579. struct net *net;
  580. net = dev_net(skb_dst(skb)->dev);
  581. /*
  582. * Incomplete header ?
  583. * Only checks for the IP header, there should be an
  584. * additional check for longer headers in upper levels.
  585. */
  586. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  587. goto out_err;
  588. icmph = icmp_hdr(skb);
  589. iph = (const struct iphdr *)skb->data;
  590. if (iph->ihl < 5) /* Mangled header, drop. */
  591. goto out_err;
  592. if (icmph->type == ICMP_DEST_UNREACH) {
  593. switch (icmph->code & 15) {
  594. case ICMP_NET_UNREACH:
  595. case ICMP_HOST_UNREACH:
  596. case ICMP_PROT_UNREACH:
  597. case ICMP_PORT_UNREACH:
  598. break;
  599. case ICMP_FRAG_NEEDED:
  600. if (ipv4_config.no_pmtu_disc) {
  601. LIMIT_NETDEBUG(KERN_INFO pr_fmt("%pI4: fragmentation needed and DF set\n"),
  602. &iph->daddr);
  603. } else {
  604. info = ntohs(icmph->un.frag.mtu);
  605. if (!info)
  606. goto out;
  607. }
  608. break;
  609. case ICMP_SR_FAILED:
  610. LIMIT_NETDEBUG(KERN_INFO pr_fmt("%pI4: Source Route Failed\n"),
  611. &iph->daddr);
  612. break;
  613. default:
  614. break;
  615. }
  616. if (icmph->code > NR_ICMP_UNREACH)
  617. goto out;
  618. } else if (icmph->type == ICMP_PARAMETERPROB)
  619. info = ntohl(icmph->un.gateway) >> 24;
  620. /*
  621. * Throw it at our lower layers
  622. *
  623. * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
  624. * header.
  625. * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
  626. * transport layer.
  627. * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
  628. * transport layer.
  629. */
  630. /*
  631. * Check the other end isn't violating RFC 1122. Some routers send
  632. * bogus responses to broadcast frames. If you see this message
  633. * first check your netmask matches at both ends, if it does then
  634. * get the other vendor to fix their kit.
  635. */
  636. if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
  637. inet_addr_type(net, iph->daddr) == RTN_BROADCAST) {
  638. net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
  639. &ip_hdr(skb)->saddr,
  640. icmph->type, icmph->code,
  641. &iph->daddr, skb->dev->name);
  642. goto out;
  643. }
  644. /* Checkin full IP header plus 8 bytes of protocol to
  645. * avoid additional coding at protocol handlers.
  646. */
  647. if (!pskb_may_pull(skb, iph->ihl * 4 + 8))
  648. goto out;
  649. iph = (const struct iphdr *)skb->data;
  650. protocol = iph->protocol;
  651. /*
  652. * Deliver ICMP message to raw sockets. Pretty useless feature?
  653. */
  654. raw_icmp_error(skb, protocol, info);
  655. hash = protocol & (MAX_INET_PROTOS - 1);
  656. rcu_read_lock();
  657. ipprot = rcu_dereference(inet_protos[hash]);
  658. if (ipprot && ipprot->err_handler)
  659. ipprot->err_handler(skb, info);
  660. rcu_read_unlock();
  661. out:
  662. return;
  663. out_err:
  664. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  665. goto out;
  666. }
  667. /*
  668. * Handle ICMP_REDIRECT.
  669. */
  670. static void icmp_redirect(struct sk_buff *skb)
  671. {
  672. const struct iphdr *iph;
  673. if (skb->len < sizeof(struct iphdr))
  674. goto out_err;
  675. /*
  676. * Get the copied header of the packet that caused the redirect
  677. */
  678. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  679. goto out;
  680. iph = (const struct iphdr *)skb->data;
  681. switch (icmp_hdr(skb)->code & 7) {
  682. case ICMP_REDIR_NET:
  683. case ICMP_REDIR_NETTOS:
  684. /*
  685. * As per RFC recommendations now handle it as a host redirect.
  686. */
  687. case ICMP_REDIR_HOST:
  688. case ICMP_REDIR_HOSTTOS:
  689. ip_rt_redirect(ip_hdr(skb)->saddr, iph->daddr,
  690. icmp_hdr(skb)->un.gateway,
  691. iph->saddr, skb->dev);
  692. break;
  693. }
  694. /* Ping wants to see redirects.
  695. * Let's pretend they are errors of sorts... */
  696. if (iph->protocol == IPPROTO_ICMP &&
  697. iph->ihl >= 5 &&
  698. pskb_may_pull(skb, (iph->ihl<<2)+8)) {
  699. ping_v4_err(skb, icmp_hdr(skb)->un.gateway);
  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(skb)->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(skb)->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. net_dbg_ratelimited("a guy asks for address mask. Who is it?\n");
  808. #endif
  809. }
  810. /*
  811. * RFC1812 (4.3.3.9). A router SHOULD listen all replies, and complain
  812. * loudly if an inconsistency is found.
  813. * called with rcu_read_lock()
  814. */
  815. static void icmp_address_reply(struct sk_buff *skb)
  816. {
  817. struct rtable *rt = skb_rtable(skb);
  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. return;
  823. in_dev = __in_dev_get_rcu(dev);
  824. if (!in_dev)
  825. return;
  826. if (in_dev->ifa_list &&
  827. IN_DEV_LOG_MARTIANS(in_dev) &&
  828. IN_DEV_FORWARD(in_dev)) {
  829. __be32 _mask, *mp;
  830. mp = skb_header_pointer(skb, 0, sizeof(_mask), &_mask);
  831. BUG_ON(mp == NULL);
  832. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  833. if (*mp == ifa->ifa_mask &&
  834. inet_ifa_match(ip_hdr(skb)->saddr, ifa))
  835. break;
  836. }
  837. if (!ifa)
  838. net_info_ratelimited("Wrong address mask %pI4 from %s/%pI4\n",
  839. mp,
  840. dev->name, &ip_hdr(skb)->saddr);
  841. }
  842. }
  843. static void icmp_discard(struct sk_buff *skb)
  844. {
  845. }
  846. /*
  847. * Deal with incoming ICMP packets.
  848. */
  849. int icmp_rcv(struct sk_buff *skb)
  850. {
  851. struct icmphdr *icmph;
  852. struct rtable *rt = skb_rtable(skb);
  853. struct net *net = dev_net(rt->dst.dev);
  854. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  855. struct sec_path *sp = skb_sec_path(skb);
  856. int nh;
  857. if (!(sp && sp->xvec[sp->len - 1]->props.flags &
  858. XFRM_STATE_ICMP))
  859. goto drop;
  860. if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
  861. goto drop;
  862. nh = skb_network_offset(skb);
  863. skb_set_network_header(skb, sizeof(*icmph));
  864. if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
  865. goto drop;
  866. skb_set_network_header(skb, nh);
  867. }
  868. ICMP_INC_STATS_BH(net, ICMP_MIB_INMSGS);
  869. switch (skb->ip_summed) {
  870. case CHECKSUM_COMPLETE:
  871. if (!csum_fold(skb->csum))
  872. break;
  873. /* fall through */
  874. case CHECKSUM_NONE:
  875. skb->csum = 0;
  876. if (__skb_checksum_complete(skb))
  877. goto error;
  878. }
  879. if (!pskb_pull(skb, sizeof(*icmph)))
  880. goto error;
  881. icmph = icmp_hdr(skb);
  882. ICMPMSGIN_INC_STATS_BH(net, icmph->type);
  883. /*
  884. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  885. *
  886. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  887. * discarded.
  888. */
  889. if (icmph->type > NR_ICMP_TYPES)
  890. goto error;
  891. /*
  892. * Parse the ICMP message
  893. */
  894. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  895. /*
  896. * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
  897. * silently ignored (we let user decide with a sysctl).
  898. * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
  899. * discarded if to broadcast/multicast.
  900. */
  901. if ((icmph->type == ICMP_ECHO ||
  902. icmph->type == ICMP_TIMESTAMP) &&
  903. net->ipv4.sysctl_icmp_echo_ignore_broadcasts) {
  904. goto error;
  905. }
  906. if (icmph->type != ICMP_ECHO &&
  907. icmph->type != ICMP_TIMESTAMP &&
  908. icmph->type != ICMP_ADDRESS &&
  909. icmph->type != ICMP_ADDRESSREPLY) {
  910. goto error;
  911. }
  912. }
  913. icmp_pointers[icmph->type].handler(skb);
  914. drop:
  915. kfree_skb(skb);
  916. return 0;
  917. error:
  918. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  919. goto drop;
  920. }
  921. /*
  922. * This table is the definition of how we handle ICMP.
  923. */
  924. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
  925. [ICMP_ECHOREPLY] = {
  926. .handler = ping_rcv,
  927. },
  928. [1] = {
  929. .handler = icmp_discard,
  930. .error = 1,
  931. },
  932. [2] = {
  933. .handler = icmp_discard,
  934. .error = 1,
  935. },
  936. [ICMP_DEST_UNREACH] = {
  937. .handler = icmp_unreach,
  938. .error = 1,
  939. },
  940. [ICMP_SOURCE_QUENCH] = {
  941. .handler = icmp_unreach,
  942. .error = 1,
  943. },
  944. [ICMP_REDIRECT] = {
  945. .handler = icmp_redirect,
  946. .error = 1,
  947. },
  948. [6] = {
  949. .handler = icmp_discard,
  950. .error = 1,
  951. },
  952. [7] = {
  953. .handler = icmp_discard,
  954. .error = 1,
  955. },
  956. [ICMP_ECHO] = {
  957. .handler = icmp_echo,
  958. },
  959. [9] = {
  960. .handler = icmp_discard,
  961. .error = 1,
  962. },
  963. [10] = {
  964. .handler = icmp_discard,
  965. .error = 1,
  966. },
  967. [ICMP_TIME_EXCEEDED] = {
  968. .handler = icmp_unreach,
  969. .error = 1,
  970. },
  971. [ICMP_PARAMETERPROB] = {
  972. .handler = icmp_unreach,
  973. .error = 1,
  974. },
  975. [ICMP_TIMESTAMP] = {
  976. .handler = icmp_timestamp,
  977. },
  978. [ICMP_TIMESTAMPREPLY] = {
  979. .handler = icmp_discard,
  980. },
  981. [ICMP_INFO_REQUEST] = {
  982. .handler = icmp_discard,
  983. },
  984. [ICMP_INFO_REPLY] = {
  985. .handler = icmp_discard,
  986. },
  987. [ICMP_ADDRESS] = {
  988. .handler = icmp_address,
  989. },
  990. [ICMP_ADDRESSREPLY] = {
  991. .handler = icmp_address_reply,
  992. },
  993. };
  994. static void __net_exit icmp_sk_exit(struct net *net)
  995. {
  996. int i;
  997. for_each_possible_cpu(i)
  998. inet_ctl_sock_destroy(net->ipv4.icmp_sk[i]);
  999. kfree(net->ipv4.icmp_sk);
  1000. net->ipv4.icmp_sk = NULL;
  1001. }
  1002. static int __net_init icmp_sk_init(struct net *net)
  1003. {
  1004. int i, err;
  1005. net->ipv4.icmp_sk =
  1006. kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL);
  1007. if (net->ipv4.icmp_sk == NULL)
  1008. return -ENOMEM;
  1009. for_each_possible_cpu(i) {
  1010. struct sock *sk;
  1011. err = inet_ctl_sock_create(&sk, PF_INET,
  1012. SOCK_RAW, IPPROTO_ICMP, net);
  1013. if (err < 0)
  1014. goto fail;
  1015. net->ipv4.icmp_sk[i] = sk;
  1016. /* Enough space for 2 64K ICMP packets, including
  1017. * sk_buff/skb_shared_info struct overhead.
  1018. */
  1019. sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
  1020. /*
  1021. * Speedup sock_wfree()
  1022. */
  1023. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  1024. inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
  1025. }
  1026. /* Control parameters for ECHO replies. */
  1027. net->ipv4.sysctl_icmp_echo_ignore_all = 0;
  1028. net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
  1029. /* Control parameter - ignore bogus broadcast responses? */
  1030. net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
  1031. /*
  1032. * Configurable global rate limit.
  1033. *
  1034. * ratelimit defines tokens/packet consumed for dst->rate_token
  1035. * bucket ratemask defines which icmp types are ratelimited by
  1036. * setting it's bit position.
  1037. *
  1038. * default:
  1039. * dest unreachable (3), source quench (4),
  1040. * time exceeded (11), parameter problem (12)
  1041. */
  1042. net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
  1043. net->ipv4.sysctl_icmp_ratemask = 0x1818;
  1044. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
  1045. return 0;
  1046. fail:
  1047. for_each_possible_cpu(i)
  1048. inet_ctl_sock_destroy(net->ipv4.icmp_sk[i]);
  1049. kfree(net->ipv4.icmp_sk);
  1050. return err;
  1051. }
  1052. static struct pernet_operations __net_initdata icmp_sk_ops = {
  1053. .init = icmp_sk_init,
  1054. .exit = icmp_sk_exit,
  1055. };
  1056. int __init icmp_init(void)
  1057. {
  1058. return register_pernet_subsys(&icmp_sk_ops);
  1059. }