icmp.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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 flowi fl = {
  323. .fl4_dst = daddr,
  324. .fl4_src = rt->rt_spec_dst,
  325. .fl4_tos = RT_TOS(ip_hdr(skb)->tos),
  326. .flowi_proto = IPPROTO_ICMP,
  327. };
  328. security_skb_classify_flow(skb, &fl);
  329. rt = ip_route_output_key(net, &fl);
  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 flowi fl = {
  347. .fl4_dst = (param->replyopts.srr ?
  348. param->replyopts.faddr : iph->saddr),
  349. .fl4_src = saddr,
  350. .fl4_tos = RT_TOS(tos),
  351. .flowi_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, &fl);
  358. rt = __ip_route_output_key(net, &fl);
  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 (!fl.fl4_src)
  364. fl.fl4_src = rt->rt_src;
  365. rt = (struct rtable *) xfrm_lookup(net, &rt->dst, &fl, NULL, 0);
  366. if (!IS_ERR(rt)) {
  367. if (rt != rt2)
  368. return rt;
  369. } else if (PTR_ERR(rt) == -EPERM) {
  370. rt = NULL;
  371. } else
  372. return rt;
  373. err = xfrm_decode_session_reverse(skb_in, &fl, AF_INET);
  374. if (err)
  375. goto relookup_failed;
  376. if (inet_addr_type(net, fl.fl4_src) == RTN_LOCAL) {
  377. rt2 = __ip_route_output_key(net, &fl);
  378. if (IS_ERR(rt2))
  379. err = PTR_ERR(rt2);
  380. } else {
  381. struct flowi fl2 = {};
  382. unsigned long orefdst;
  383. fl2.fl4_dst = fl.fl4_src;
  384. rt2 = ip_route_output_key(net, &fl2);
  385. if (IS_ERR(rt2)) {
  386. err = PTR_ERR(rt2);
  387. goto relookup_failed;
  388. }
  389. /* Ugh! */
  390. orefdst = skb_in->_skb_refdst; /* save old refdst */
  391. err = ip_route_input(skb_in, fl.fl4_dst, fl.fl4_src,
  392. RT_TOS(tos), rt2->dst.dev);
  393. dst_release(&rt2->dst);
  394. rt2 = skb_rtable(skb_in);
  395. skb_in->_skb_refdst = orefdst; /* restore old refdst */
  396. }
  397. if (err)
  398. goto relookup_failed;
  399. rt2 = (struct rtable *) xfrm_lookup(net, &rt2->dst, &fl, NULL, XFRM_LOOKUP_ICMP);
  400. if (!IS_ERR(rt2)) {
  401. dst_release(&rt->dst);
  402. rt = rt2;
  403. } else if (PTR_ERR(rt2) == -EPERM) {
  404. if (rt)
  405. dst_release(&rt->dst);
  406. return rt2;
  407. } else {
  408. err = PTR_ERR(rt2);
  409. goto relookup_failed;
  410. }
  411. return rt;
  412. relookup_failed:
  413. if (rt)
  414. return rt;
  415. return ERR_PTR(err);
  416. }
  417. /*
  418. * Send an ICMP message in response to a situation
  419. *
  420. * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
  421. * MAY send more (we do).
  422. * MUST NOT change this header information.
  423. * MUST NOT reply to a multicast/broadcast IP address.
  424. * MUST NOT reply to a multicast/broadcast MAC address.
  425. * MUST reply to only the first fragment.
  426. */
  427. void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
  428. {
  429. struct iphdr *iph;
  430. int room;
  431. struct icmp_bxm icmp_param;
  432. struct rtable *rt = skb_rtable(skb_in);
  433. struct ipcm_cookie ipc;
  434. __be32 saddr;
  435. u8 tos;
  436. struct net *net;
  437. struct sock *sk;
  438. if (!rt)
  439. goto out;
  440. net = dev_net(rt->dst.dev);
  441. /*
  442. * Find the original header. It is expected to be valid, of course.
  443. * Check this, icmp_send is called from the most obscure devices
  444. * sometimes.
  445. */
  446. iph = ip_hdr(skb_in);
  447. if ((u8 *)iph < skb_in->head ||
  448. (skb_in->network_header + sizeof(*iph)) > skb_in->tail)
  449. goto out;
  450. /*
  451. * No replies to physical multicast/broadcast
  452. */
  453. if (skb_in->pkt_type != PACKET_HOST)
  454. goto out;
  455. /*
  456. * Now check at the protocol level
  457. */
  458. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  459. goto out;
  460. /*
  461. * Only reply to fragment 0. We byte re-order the constant
  462. * mask for efficiency.
  463. */
  464. if (iph->frag_off & htons(IP_OFFSET))
  465. goto out;
  466. /*
  467. * If we send an ICMP error to an ICMP error a mess would result..
  468. */
  469. if (icmp_pointers[type].error) {
  470. /*
  471. * We are an error, check if we are replying to an
  472. * ICMP error
  473. */
  474. if (iph->protocol == IPPROTO_ICMP) {
  475. u8 _inner_type, *itp;
  476. itp = skb_header_pointer(skb_in,
  477. skb_network_header(skb_in) +
  478. (iph->ihl << 2) +
  479. offsetof(struct icmphdr,
  480. type) -
  481. skb_in->data,
  482. sizeof(_inner_type),
  483. &_inner_type);
  484. if (itp == NULL)
  485. goto out;
  486. /*
  487. * Assume any unknown ICMP type is an error. This
  488. * isn't specified by the RFC, but think about it..
  489. */
  490. if (*itp > NR_ICMP_TYPES ||
  491. icmp_pointers[*itp].error)
  492. goto out;
  493. }
  494. }
  495. sk = icmp_xmit_lock(net);
  496. if (sk == NULL)
  497. return;
  498. /*
  499. * Construct source address and options.
  500. */
  501. saddr = iph->daddr;
  502. if (!(rt->rt_flags & RTCF_LOCAL)) {
  503. struct net_device *dev = NULL;
  504. rcu_read_lock();
  505. if (rt_is_input_route(rt) &&
  506. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)
  507. dev = dev_get_by_index_rcu(net, rt->rt_iif);
  508. if (dev)
  509. saddr = inet_select_addr(dev, 0, RT_SCOPE_LINK);
  510. else
  511. saddr = 0;
  512. rcu_read_unlock();
  513. }
  514. tos = icmp_pointers[type].error ? ((iph->tos & IPTOS_TOS_MASK) |
  515. IPTOS_PREC_INTERNETCONTROL) :
  516. iph->tos;
  517. if (ip_options_echo(&icmp_param.replyopts, skb_in))
  518. goto out_unlock;
  519. /*
  520. * Prepare data for ICMP header.
  521. */
  522. icmp_param.data.icmph.type = type;
  523. icmp_param.data.icmph.code = code;
  524. icmp_param.data.icmph.un.gateway = info;
  525. icmp_param.data.icmph.checksum = 0;
  526. icmp_param.skb = skb_in;
  527. icmp_param.offset = skb_network_offset(skb_in);
  528. inet_sk(sk)->tos = tos;
  529. ipc.addr = iph->saddr;
  530. ipc.opt = &icmp_param.replyopts;
  531. ipc.tx_flags = 0;
  532. rt = icmp_route_lookup(net, skb_in, iph, saddr, tos,
  533. type, code, &icmp_param);
  534. if (IS_ERR(rt))
  535. goto out_unlock;
  536. if (!icmpv4_xrlim_allow(net, rt, type, code))
  537. goto ende;
  538. /* RFC says return as much as we can without exceeding 576 bytes. */
  539. room = dst_mtu(&rt->dst);
  540. if (room > 576)
  541. room = 576;
  542. room -= sizeof(struct iphdr) + icmp_param.replyopts.optlen;
  543. room -= sizeof(struct icmphdr);
  544. icmp_param.data_len = skb_in->len - icmp_param.offset;
  545. if (icmp_param.data_len > room)
  546. icmp_param.data_len = room;
  547. icmp_param.head_len = sizeof(struct icmphdr);
  548. icmp_push_reply(&icmp_param, &ipc, &rt);
  549. ende:
  550. ip_rt_put(rt);
  551. out_unlock:
  552. icmp_xmit_unlock(sk);
  553. out:;
  554. }
  555. EXPORT_SYMBOL(icmp_send);
  556. /*
  557. * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEED, and ICMP_QUENCH.
  558. */
  559. static void icmp_unreach(struct sk_buff *skb)
  560. {
  561. struct iphdr *iph;
  562. struct icmphdr *icmph;
  563. int hash, protocol;
  564. const struct net_protocol *ipprot;
  565. u32 info = 0;
  566. struct net *net;
  567. net = dev_net(skb_dst(skb)->dev);
  568. /*
  569. * Incomplete header ?
  570. * Only checks for the IP header, there should be an
  571. * additional check for longer headers in upper levels.
  572. */
  573. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  574. goto out_err;
  575. icmph = icmp_hdr(skb);
  576. iph = (struct iphdr *)skb->data;
  577. if (iph->ihl < 5) /* Mangled header, drop. */
  578. goto out_err;
  579. if (icmph->type == ICMP_DEST_UNREACH) {
  580. switch (icmph->code & 15) {
  581. case ICMP_NET_UNREACH:
  582. case ICMP_HOST_UNREACH:
  583. case ICMP_PROT_UNREACH:
  584. case ICMP_PORT_UNREACH:
  585. break;
  586. case ICMP_FRAG_NEEDED:
  587. if (ipv4_config.no_pmtu_disc) {
  588. LIMIT_NETDEBUG(KERN_INFO "ICMP: %pI4: fragmentation needed and DF set.\n",
  589. &iph->daddr);
  590. } else {
  591. info = ip_rt_frag_needed(net, iph,
  592. ntohs(icmph->un.frag.mtu),
  593. skb->dev);
  594. if (!info)
  595. goto out;
  596. }
  597. break;
  598. case ICMP_SR_FAILED:
  599. LIMIT_NETDEBUG(KERN_INFO "ICMP: %pI4: Source Route Failed.\n",
  600. &iph->daddr);
  601. break;
  602. default:
  603. break;
  604. }
  605. if (icmph->code > NR_ICMP_UNREACH)
  606. goto out;
  607. } else if (icmph->type == ICMP_PARAMETERPROB)
  608. info = ntohl(icmph->un.gateway) >> 24;
  609. /*
  610. * Throw it at our lower layers
  611. *
  612. * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
  613. * header.
  614. * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
  615. * transport layer.
  616. * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
  617. * transport layer.
  618. */
  619. /*
  620. * Check the other end isnt violating RFC 1122. Some routers send
  621. * bogus responses to broadcast frames. If you see this message
  622. * first check your netmask matches at both ends, if it does then
  623. * get the other vendor to fix their kit.
  624. */
  625. if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
  626. inet_addr_type(net, iph->daddr) == RTN_BROADCAST) {
  627. if (net_ratelimit())
  628. printk(KERN_WARNING "%pI4 sent an invalid ICMP "
  629. "type %u, code %u "
  630. "error to a broadcast: %pI4 on %s\n",
  631. &ip_hdr(skb)->saddr,
  632. icmph->type, icmph->code,
  633. &iph->daddr,
  634. skb->dev->name);
  635. goto out;
  636. }
  637. /* Checkin full IP header plus 8 bytes of protocol to
  638. * avoid additional coding at protocol handlers.
  639. */
  640. if (!pskb_may_pull(skb, iph->ihl * 4 + 8))
  641. goto out;
  642. iph = (struct iphdr *)skb->data;
  643. protocol = iph->protocol;
  644. /*
  645. * Deliver ICMP message to raw sockets. Pretty useless feature?
  646. */
  647. raw_icmp_error(skb, protocol, info);
  648. hash = protocol & (MAX_INET_PROTOS - 1);
  649. rcu_read_lock();
  650. ipprot = rcu_dereference(inet_protos[hash]);
  651. if (ipprot && ipprot->err_handler)
  652. ipprot->err_handler(skb, info);
  653. rcu_read_unlock();
  654. out:
  655. return;
  656. out_err:
  657. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  658. goto out;
  659. }
  660. /*
  661. * Handle ICMP_REDIRECT.
  662. */
  663. static void icmp_redirect(struct sk_buff *skb)
  664. {
  665. struct iphdr *iph;
  666. if (skb->len < sizeof(struct iphdr))
  667. goto out_err;
  668. /*
  669. * Get the copied header of the packet that caused the redirect
  670. */
  671. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  672. goto out;
  673. iph = (struct iphdr *)skb->data;
  674. switch (icmp_hdr(skb)->code & 7) {
  675. case ICMP_REDIR_NET:
  676. case ICMP_REDIR_NETTOS:
  677. /*
  678. * As per RFC recommendations now handle it as a host redirect.
  679. */
  680. case ICMP_REDIR_HOST:
  681. case ICMP_REDIR_HOSTTOS:
  682. ip_rt_redirect(ip_hdr(skb)->saddr, iph->daddr,
  683. icmp_hdr(skb)->un.gateway,
  684. iph->saddr, skb->dev);
  685. break;
  686. }
  687. out:
  688. return;
  689. out_err:
  690. ICMP_INC_STATS_BH(dev_net(skb->dev), ICMP_MIB_INERRORS);
  691. goto out;
  692. }
  693. /*
  694. * Handle ICMP_ECHO ("ping") requests.
  695. *
  696. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  697. * requests.
  698. * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
  699. * included in the reply.
  700. * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
  701. * echo requests, MUST have default=NOT.
  702. * See also WRT handling of options once they are done and working.
  703. */
  704. static void icmp_echo(struct sk_buff *skb)
  705. {
  706. struct net *net;
  707. net = dev_net(skb_dst(skb)->dev);
  708. if (!net->ipv4.sysctl_icmp_echo_ignore_all) {
  709. struct icmp_bxm icmp_param;
  710. icmp_param.data.icmph = *icmp_hdr(skb);
  711. icmp_param.data.icmph.type = ICMP_ECHOREPLY;
  712. icmp_param.skb = skb;
  713. icmp_param.offset = 0;
  714. icmp_param.data_len = skb->len;
  715. icmp_param.head_len = sizeof(struct icmphdr);
  716. icmp_reply(&icmp_param, skb);
  717. }
  718. }
  719. /*
  720. * Handle ICMP Timestamp requests.
  721. * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
  722. * SHOULD be in the kernel for minimum random latency.
  723. * MUST be accurate to a few minutes.
  724. * MUST be updated at least at 15Hz.
  725. */
  726. static void icmp_timestamp(struct sk_buff *skb)
  727. {
  728. struct timespec tv;
  729. struct icmp_bxm icmp_param;
  730. /*
  731. * Too short.
  732. */
  733. if (skb->len < 4)
  734. goto out_err;
  735. /*
  736. * Fill in the current time as ms since midnight UT:
  737. */
  738. getnstimeofday(&tv);
  739. icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC +
  740. tv.tv_nsec / NSEC_PER_MSEC);
  741. icmp_param.data.times[2] = icmp_param.data.times[1];
  742. if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
  743. BUG();
  744. icmp_param.data.icmph = *icmp_hdr(skb);
  745. icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
  746. icmp_param.data.icmph.code = 0;
  747. icmp_param.skb = skb;
  748. icmp_param.offset = 0;
  749. icmp_param.data_len = 0;
  750. icmp_param.head_len = sizeof(struct icmphdr) + 12;
  751. icmp_reply(&icmp_param, skb);
  752. out:
  753. return;
  754. out_err:
  755. ICMP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
  756. goto out;
  757. }
  758. /*
  759. * Handle ICMP_ADDRESS_MASK requests. (RFC950)
  760. *
  761. * RFC1122 (3.2.2.9). A host MUST only send replies to
  762. * ADDRESS_MASK requests if it's been configured as an address mask
  763. * agent. Receiving a request doesn't constitute implicit permission to
  764. * act as one. Of course, implementing this correctly requires (SHOULD)
  765. * a way to turn the functionality on and off. Another one for sysctl(),
  766. * I guess. -- MS
  767. *
  768. * RFC1812 (4.3.3.9). A router MUST implement it.
  769. * A router SHOULD have switch turning it on/off.
  770. * This switch MUST be ON by default.
  771. *
  772. * Gratuitous replies, zero-source replies are not implemented,
  773. * that complies with RFC. DO NOT implement them!!! All the idea
  774. * of broadcast addrmask replies as specified in RFC950 is broken.
  775. * The problem is that it is not uncommon to have several prefixes
  776. * on one physical interface. Moreover, addrmask agent can even be
  777. * not aware of existing another prefixes.
  778. * If source is zero, addrmask agent cannot choose correct prefix.
  779. * Gratuitous mask announcements suffer from the same problem.
  780. * RFC1812 explains it, but still allows to use ADDRMASK,
  781. * that is pretty silly. --ANK
  782. *
  783. * All these rules are so bizarre, that I removed kernel addrmask
  784. * support at all. It is wrong, it is obsolete, nobody uses it in
  785. * any case. --ANK
  786. *
  787. * Furthermore you can do it with a usermode address agent program
  788. * anyway...
  789. */
  790. static void icmp_address(struct sk_buff *skb)
  791. {
  792. #if 0
  793. if (net_ratelimit())
  794. printk(KERN_DEBUG "a guy asks for address mask. Who is it?\n");
  795. #endif
  796. }
  797. /*
  798. * RFC1812 (4.3.3.9). A router SHOULD listen all replies, and complain
  799. * loudly if an inconsistency is found.
  800. * called with rcu_read_lock()
  801. */
  802. static void icmp_address_reply(struct sk_buff *skb)
  803. {
  804. struct rtable *rt = skb_rtable(skb);
  805. struct net_device *dev = skb->dev;
  806. struct in_device *in_dev;
  807. struct in_ifaddr *ifa;
  808. if (skb->len < 4 || !(rt->rt_flags&RTCF_DIRECTSRC))
  809. return;
  810. in_dev = __in_dev_get_rcu(dev);
  811. if (!in_dev)
  812. return;
  813. if (in_dev->ifa_list &&
  814. IN_DEV_LOG_MARTIANS(in_dev) &&
  815. IN_DEV_FORWARD(in_dev)) {
  816. __be32 _mask, *mp;
  817. mp = skb_header_pointer(skb, 0, sizeof(_mask), &_mask);
  818. BUG_ON(mp == NULL);
  819. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  820. if (*mp == ifa->ifa_mask &&
  821. inet_ifa_match(rt->rt_src, ifa))
  822. break;
  823. }
  824. if (!ifa && net_ratelimit()) {
  825. printk(KERN_INFO "Wrong address mask %pI4 from %s/%pI4\n",
  826. mp, dev->name, &rt->rt_src);
  827. }
  828. }
  829. }
  830. static void icmp_discard(struct sk_buff *skb)
  831. {
  832. }
  833. /*
  834. * Deal with incoming ICMP packets.
  835. */
  836. int icmp_rcv(struct sk_buff *skb)
  837. {
  838. struct icmphdr *icmph;
  839. struct rtable *rt = skb_rtable(skb);
  840. struct net *net = dev_net(rt->dst.dev);
  841. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  842. struct sec_path *sp = skb_sec_path(skb);
  843. int nh;
  844. if (!(sp && sp->xvec[sp->len - 1]->props.flags &
  845. XFRM_STATE_ICMP))
  846. goto drop;
  847. if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
  848. goto drop;
  849. nh = skb_network_offset(skb);
  850. skb_set_network_header(skb, sizeof(*icmph));
  851. if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
  852. goto drop;
  853. skb_set_network_header(skb, nh);
  854. }
  855. ICMP_INC_STATS_BH(net, ICMP_MIB_INMSGS);
  856. switch (skb->ip_summed) {
  857. case CHECKSUM_COMPLETE:
  858. if (!csum_fold(skb->csum))
  859. break;
  860. /* fall through */
  861. case CHECKSUM_NONE:
  862. skb->csum = 0;
  863. if (__skb_checksum_complete(skb))
  864. goto error;
  865. }
  866. if (!pskb_pull(skb, sizeof(*icmph)))
  867. goto error;
  868. icmph = icmp_hdr(skb);
  869. ICMPMSGIN_INC_STATS_BH(net, icmph->type);
  870. /*
  871. * 18 is the highest 'known' ICMP type. Anything else is a mystery
  872. *
  873. * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
  874. * discarded.
  875. */
  876. if (icmph->type > NR_ICMP_TYPES)
  877. goto error;
  878. /*
  879. * Parse the ICMP message
  880. */
  881. if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
  882. /*
  883. * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
  884. * silently ignored (we let user decide with a sysctl).
  885. * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
  886. * discarded if to broadcast/multicast.
  887. */
  888. if ((icmph->type == ICMP_ECHO ||
  889. icmph->type == ICMP_TIMESTAMP) &&
  890. net->ipv4.sysctl_icmp_echo_ignore_broadcasts) {
  891. goto error;
  892. }
  893. if (icmph->type != ICMP_ECHO &&
  894. icmph->type != ICMP_TIMESTAMP &&
  895. icmph->type != ICMP_ADDRESS &&
  896. icmph->type != ICMP_ADDRESSREPLY) {
  897. goto error;
  898. }
  899. }
  900. icmp_pointers[icmph->type].handler(skb);
  901. drop:
  902. kfree_skb(skb);
  903. return 0;
  904. error:
  905. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  906. goto drop;
  907. }
  908. /*
  909. * This table is the definition of how we handle ICMP.
  910. */
  911. static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
  912. [ICMP_ECHOREPLY] = {
  913. .handler = icmp_discard,
  914. },
  915. [1] = {
  916. .handler = icmp_discard,
  917. .error = 1,
  918. },
  919. [2] = {
  920. .handler = icmp_discard,
  921. .error = 1,
  922. },
  923. [ICMP_DEST_UNREACH] = {
  924. .handler = icmp_unreach,
  925. .error = 1,
  926. },
  927. [ICMP_SOURCE_QUENCH] = {
  928. .handler = icmp_unreach,
  929. .error = 1,
  930. },
  931. [ICMP_REDIRECT] = {
  932. .handler = icmp_redirect,
  933. .error = 1,
  934. },
  935. [6] = {
  936. .handler = icmp_discard,
  937. .error = 1,
  938. },
  939. [7] = {
  940. .handler = icmp_discard,
  941. .error = 1,
  942. },
  943. [ICMP_ECHO] = {
  944. .handler = icmp_echo,
  945. },
  946. [9] = {
  947. .handler = icmp_discard,
  948. .error = 1,
  949. },
  950. [10] = {
  951. .handler = icmp_discard,
  952. .error = 1,
  953. },
  954. [ICMP_TIME_EXCEEDED] = {
  955. .handler = icmp_unreach,
  956. .error = 1,
  957. },
  958. [ICMP_PARAMETERPROB] = {
  959. .handler = icmp_unreach,
  960. .error = 1,
  961. },
  962. [ICMP_TIMESTAMP] = {
  963. .handler = icmp_timestamp,
  964. },
  965. [ICMP_TIMESTAMPREPLY] = {
  966. .handler = icmp_discard,
  967. },
  968. [ICMP_INFO_REQUEST] = {
  969. .handler = icmp_discard,
  970. },
  971. [ICMP_INFO_REPLY] = {
  972. .handler = icmp_discard,
  973. },
  974. [ICMP_ADDRESS] = {
  975. .handler = icmp_address,
  976. },
  977. [ICMP_ADDRESSREPLY] = {
  978. .handler = icmp_address_reply,
  979. },
  980. };
  981. static void __net_exit icmp_sk_exit(struct net *net)
  982. {
  983. int i;
  984. for_each_possible_cpu(i)
  985. inet_ctl_sock_destroy(net->ipv4.icmp_sk[i]);
  986. kfree(net->ipv4.icmp_sk);
  987. net->ipv4.icmp_sk = NULL;
  988. }
  989. static int __net_init icmp_sk_init(struct net *net)
  990. {
  991. int i, err;
  992. net->ipv4.icmp_sk =
  993. kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL);
  994. if (net->ipv4.icmp_sk == NULL)
  995. return -ENOMEM;
  996. for_each_possible_cpu(i) {
  997. struct sock *sk;
  998. err = inet_ctl_sock_create(&sk, PF_INET,
  999. SOCK_RAW, IPPROTO_ICMP, net);
  1000. if (err < 0)
  1001. goto fail;
  1002. net->ipv4.icmp_sk[i] = sk;
  1003. /* Enough space for 2 64K ICMP packets, including
  1004. * sk_buff struct overhead.
  1005. */
  1006. sk->sk_sndbuf =
  1007. (2 * ((64 * 1024) + sizeof(struct sk_buff)));
  1008. /*
  1009. * Speedup sock_wfree()
  1010. */
  1011. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  1012. inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
  1013. }
  1014. /* Control parameters for ECHO replies. */
  1015. net->ipv4.sysctl_icmp_echo_ignore_all = 0;
  1016. net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
  1017. /* Control parameter - ignore bogus broadcast responses? */
  1018. net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
  1019. /*
  1020. * Configurable global rate limit.
  1021. *
  1022. * ratelimit defines tokens/packet consumed for dst->rate_token
  1023. * bucket ratemask defines which icmp types are ratelimited by
  1024. * setting it's bit position.
  1025. *
  1026. * default:
  1027. * dest unreachable (3), source quench (4),
  1028. * time exceeded (11), parameter problem (12)
  1029. */
  1030. net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
  1031. net->ipv4.sysctl_icmp_ratemask = 0x1818;
  1032. net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
  1033. return 0;
  1034. fail:
  1035. for_each_possible_cpu(i)
  1036. inet_ctl_sock_destroy(net->ipv4.icmp_sk[i]);
  1037. kfree(net->ipv4.icmp_sk);
  1038. return err;
  1039. }
  1040. static struct pernet_operations __net_initdata icmp_sk_ops = {
  1041. .init = icmp_sk_init,
  1042. .exit = icmp_sk_exit,
  1043. };
  1044. int __init icmp_init(void)
  1045. {
  1046. return register_pernet_subsys(&icmp_sk_ops);
  1047. }