icmp.c 28 KB

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