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