icmp.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*
  2. * Internet Control Message Protocol (ICMPv6)
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Based on net/ipv4/icmp.c
  9. *
  10. * RFC 1885
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. /*
  18. * Changes:
  19. *
  20. * Andi Kleen : exception handling
  21. * Andi Kleen add rate limits. never reply to a icmp.
  22. * add more length checks and other fixes.
  23. * yoshfuji : ensure to sent parameter problem for
  24. * fragments.
  25. * YOSHIFUJI Hideaki @USAGI: added sysctl for icmp rate limit.
  26. * Randy Dunlap and
  27. * YOSHIFUJI Hideaki @USAGI: Per-interface statistics support
  28. * Kazunori MIYAZAWA @USAGI: change output process to use ip6_append_data
  29. */
  30. #define pr_fmt(fmt) "IPv6: " fmt
  31. #include <linux/module.h>
  32. #include <linux/errno.h>
  33. #include <linux/types.h>
  34. #include <linux/socket.h>
  35. #include <linux/in.h>
  36. #include <linux/kernel.h>
  37. #include <linux/sockios.h>
  38. #include <linux/net.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/init.h>
  41. #include <linux/netfilter.h>
  42. #include <linux/slab.h>
  43. #ifdef CONFIG_SYSCTL
  44. #include <linux/sysctl.h>
  45. #endif
  46. #include <linux/inet.h>
  47. #include <linux/netdevice.h>
  48. #include <linux/icmpv6.h>
  49. #include <net/ip.h>
  50. #include <net/sock.h>
  51. #include <net/ipv6.h>
  52. #include <net/ip6_checksum.h>
  53. #include <net/ping.h>
  54. #include <net/protocol.h>
  55. #include <net/raw.h>
  56. #include <net/rawv6.h>
  57. #include <net/transp_v6.h>
  58. #include <net/ip6_route.h>
  59. #include <net/addrconf.h>
  60. #include <net/icmp.h>
  61. #include <net/xfrm.h>
  62. #include <net/inet_common.h>
  63. #include <net/dsfield.h>
  64. #include <net/l3mdev.h>
  65. #include <asm/uaccess.h>
  66. /*
  67. * The ICMP socket(s). This is the most convenient way to flow control
  68. * our ICMP output as well as maintain a clean interface throughout
  69. * all layers. All Socketless IP sends will soon be gone.
  70. *
  71. * On SMP we have one ICMP socket per-cpu.
  72. */
  73. static inline struct sock *icmpv6_sk(struct net *net)
  74. {
  75. return net->ipv6.icmp_sk[smp_processor_id()];
  76. }
  77. static void icmpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  78. u8 type, u8 code, int offset, __be32 info)
  79. {
  80. /* icmpv6_notify checks 8 bytes can be pulled, icmp6hdr is 8 bytes */
  81. struct icmp6hdr *icmp6 = (struct icmp6hdr *) (skb->data + offset);
  82. struct net *net = dev_net(skb->dev);
  83. if (type == ICMPV6_PKT_TOOBIG)
  84. ip6_update_pmtu(skb, net, info, 0, 0);
  85. else if (type == NDISC_REDIRECT)
  86. ip6_redirect(skb, net, skb->dev->ifindex, 0);
  87. if (!(type & ICMPV6_INFOMSG_MASK))
  88. if (icmp6->icmp6_type == ICMPV6_ECHO_REQUEST)
  89. ping_err(skb, offset, ntohl(info));
  90. }
  91. static int icmpv6_rcv(struct sk_buff *skb);
  92. static const struct inet6_protocol icmpv6_protocol = {
  93. .handler = icmpv6_rcv,
  94. .err_handler = icmpv6_err,
  95. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  96. };
  97. static __inline__ struct sock *icmpv6_xmit_lock(struct net *net)
  98. {
  99. struct sock *sk;
  100. local_bh_disable();
  101. sk = icmpv6_sk(net);
  102. if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
  103. /* This can happen if the output path (f.e. SIT or
  104. * ip6ip6 tunnel) signals dst_link_failure() for an
  105. * outgoing ICMP6 packet.
  106. */
  107. local_bh_enable();
  108. return NULL;
  109. }
  110. return sk;
  111. }
  112. static __inline__ void icmpv6_xmit_unlock(struct sock *sk)
  113. {
  114. spin_unlock_bh(&sk->sk_lock.slock);
  115. }
  116. /*
  117. * Figure out, may we reply to this packet with icmp error.
  118. *
  119. * We do not reply, if:
  120. * - it was icmp error message.
  121. * - it is truncated, so that it is known, that protocol is ICMPV6
  122. * (i.e. in the middle of some exthdr)
  123. *
  124. * --ANK (980726)
  125. */
  126. static bool is_ineligible(const struct sk_buff *skb)
  127. {
  128. int ptr = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
  129. int len = skb->len - ptr;
  130. __u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  131. __be16 frag_off;
  132. if (len < 0)
  133. return true;
  134. ptr = ipv6_skip_exthdr(skb, ptr, &nexthdr, &frag_off);
  135. if (ptr < 0)
  136. return false;
  137. if (nexthdr == IPPROTO_ICMPV6) {
  138. u8 _type, *tp;
  139. tp = skb_header_pointer(skb,
  140. ptr+offsetof(struct icmp6hdr, icmp6_type),
  141. sizeof(_type), &_type);
  142. if (!tp || !(*tp & ICMPV6_INFOMSG_MASK))
  143. return true;
  144. }
  145. return false;
  146. }
  147. /*
  148. * Check the ICMP output rate limit
  149. */
  150. static bool icmpv6_xrlim_allow(struct sock *sk, u8 type,
  151. struct flowi6 *fl6)
  152. {
  153. struct net *net = sock_net(sk);
  154. struct dst_entry *dst;
  155. bool res = false;
  156. /* Informational messages are not limited. */
  157. if (type & ICMPV6_INFOMSG_MASK)
  158. return true;
  159. /* Do not limit pmtu discovery, it would break it. */
  160. if (type == ICMPV6_PKT_TOOBIG)
  161. return true;
  162. /*
  163. * Look up the output route.
  164. * XXX: perhaps the expire for routing entries cloned by
  165. * this lookup should be more aggressive (not longer than timeout).
  166. */
  167. dst = ip6_route_output(net, sk, fl6);
  168. if (dst->error) {
  169. IP6_INC_STATS(net, ip6_dst_idev(dst),
  170. IPSTATS_MIB_OUTNOROUTES);
  171. } else if (dst->dev && (dst->dev->flags&IFF_LOOPBACK)) {
  172. res = true;
  173. } else {
  174. struct rt6_info *rt = (struct rt6_info *)dst;
  175. int tmo = net->ipv6.sysctl.icmpv6_time;
  176. /* Give more bandwidth to wider prefixes. */
  177. if (rt->rt6i_dst.plen < 128)
  178. tmo >>= ((128 - rt->rt6i_dst.plen)>>5);
  179. if (icmp_global_allow()) {
  180. struct inet_peer *peer;
  181. peer = inet_getpeer_v6(net->ipv6.peers,
  182. &fl6->daddr, 1);
  183. res = inet_peer_xrlim_allow(peer, tmo);
  184. if (peer)
  185. inet_putpeer(peer);
  186. }
  187. }
  188. dst_release(dst);
  189. return res;
  190. }
  191. /*
  192. * an inline helper for the "simple" if statement below
  193. * checks if parameter problem report is caused by an
  194. * unrecognized IPv6 option that has the Option Type
  195. * highest-order two bits set to 10
  196. */
  197. static bool opt_unrec(struct sk_buff *skb, __u32 offset)
  198. {
  199. u8 _optval, *op;
  200. offset += skb_network_offset(skb);
  201. op = skb_header_pointer(skb, offset, sizeof(_optval), &_optval);
  202. if (!op)
  203. return true;
  204. return (*op & 0xC0) == 0x80;
  205. }
  206. int icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
  207. struct icmp6hdr *thdr, int len)
  208. {
  209. struct sk_buff *skb;
  210. struct icmp6hdr *icmp6h;
  211. int err = 0;
  212. skb = skb_peek(&sk->sk_write_queue);
  213. if (!skb)
  214. goto out;
  215. icmp6h = icmp6_hdr(skb);
  216. memcpy(icmp6h, thdr, sizeof(struct icmp6hdr));
  217. icmp6h->icmp6_cksum = 0;
  218. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  219. skb->csum = csum_partial(icmp6h,
  220. sizeof(struct icmp6hdr), skb->csum);
  221. icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
  222. &fl6->daddr,
  223. len, fl6->flowi6_proto,
  224. skb->csum);
  225. } else {
  226. __wsum tmp_csum = 0;
  227. skb_queue_walk(&sk->sk_write_queue, skb) {
  228. tmp_csum = csum_add(tmp_csum, skb->csum);
  229. }
  230. tmp_csum = csum_partial(icmp6h,
  231. sizeof(struct icmp6hdr), tmp_csum);
  232. icmp6h->icmp6_cksum = csum_ipv6_magic(&fl6->saddr,
  233. &fl6->daddr,
  234. len, fl6->flowi6_proto,
  235. tmp_csum);
  236. }
  237. ip6_push_pending_frames(sk);
  238. out:
  239. return err;
  240. }
  241. struct icmpv6_msg {
  242. struct sk_buff *skb;
  243. int offset;
  244. uint8_t type;
  245. };
  246. static int icmpv6_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
  247. {
  248. struct icmpv6_msg *msg = (struct icmpv6_msg *) from;
  249. struct sk_buff *org_skb = msg->skb;
  250. __wsum csum = 0;
  251. csum = skb_copy_and_csum_bits(org_skb, msg->offset + offset,
  252. to, len, csum);
  253. skb->csum = csum_block_add(skb->csum, csum, odd);
  254. if (!(msg->type & ICMPV6_INFOMSG_MASK))
  255. nf_ct_attach(skb, org_skb);
  256. return 0;
  257. }
  258. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  259. static void mip6_addr_swap(struct sk_buff *skb)
  260. {
  261. struct ipv6hdr *iph = ipv6_hdr(skb);
  262. struct inet6_skb_parm *opt = IP6CB(skb);
  263. struct ipv6_destopt_hao *hao;
  264. struct in6_addr tmp;
  265. int off;
  266. if (opt->dsthao) {
  267. off = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
  268. if (likely(off >= 0)) {
  269. hao = (struct ipv6_destopt_hao *)
  270. (skb_network_header(skb) + off);
  271. tmp = iph->saddr;
  272. iph->saddr = hao->addr;
  273. hao->addr = tmp;
  274. }
  275. }
  276. }
  277. #else
  278. static inline void mip6_addr_swap(struct sk_buff *skb) {}
  279. #endif
  280. static struct dst_entry *icmpv6_route_lookup(struct net *net,
  281. struct sk_buff *skb,
  282. struct sock *sk,
  283. struct flowi6 *fl6)
  284. {
  285. struct dst_entry *dst, *dst2;
  286. struct flowi6 fl2;
  287. int err;
  288. err = ip6_dst_lookup(net, sk, &dst, fl6);
  289. if (err)
  290. return ERR_PTR(err);
  291. /*
  292. * We won't send icmp if the destination is known
  293. * anycast.
  294. */
  295. if (ipv6_anycast_destination(dst, &fl6->daddr)) {
  296. net_dbg_ratelimited("icmp6_send: acast source\n");
  297. dst_release(dst);
  298. return ERR_PTR(-EINVAL);
  299. }
  300. /* No need to clone since we're just using its address. */
  301. dst2 = dst;
  302. dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), sk, 0);
  303. if (!IS_ERR(dst)) {
  304. if (dst != dst2)
  305. return dst;
  306. } else {
  307. if (PTR_ERR(dst) == -EPERM)
  308. dst = NULL;
  309. else
  310. return dst;
  311. }
  312. err = xfrm_decode_session_reverse(skb, flowi6_to_flowi(&fl2), AF_INET6);
  313. if (err)
  314. goto relookup_failed;
  315. err = ip6_dst_lookup(net, sk, &dst2, &fl2);
  316. if (err)
  317. goto relookup_failed;
  318. dst2 = xfrm_lookup(net, dst2, flowi6_to_flowi(&fl2), sk, XFRM_LOOKUP_ICMP);
  319. if (!IS_ERR(dst2)) {
  320. dst_release(dst);
  321. dst = dst2;
  322. } else {
  323. err = PTR_ERR(dst2);
  324. if (err == -EPERM) {
  325. dst_release(dst);
  326. return dst2;
  327. } else
  328. goto relookup_failed;
  329. }
  330. relookup_failed:
  331. if (dst)
  332. return dst;
  333. return ERR_PTR(err);
  334. }
  335. /*
  336. * Send an ICMP message in response to a packet in error
  337. */
  338. static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info)
  339. {
  340. struct net *net = dev_net(skb->dev);
  341. struct inet6_dev *idev = NULL;
  342. struct ipv6hdr *hdr = ipv6_hdr(skb);
  343. struct sock *sk;
  344. struct ipv6_pinfo *np;
  345. const struct in6_addr *saddr = NULL;
  346. struct dst_entry *dst;
  347. struct icmp6hdr tmp_hdr;
  348. struct flowi6 fl6;
  349. struct icmpv6_msg msg;
  350. int iif = 0;
  351. int addr_type = 0;
  352. int len;
  353. int hlimit;
  354. int err = 0;
  355. u32 mark = IP6_REPLY_MARK(net, skb->mark);
  356. if ((u8 *)hdr < skb->head ||
  357. (skb_network_header(skb) + sizeof(*hdr)) > skb_tail_pointer(skb))
  358. return;
  359. /*
  360. * Make sure we respect the rules
  361. * i.e. RFC 1885 2.4(e)
  362. * Rule (e.1) is enforced by not using icmp6_send
  363. * in any code that processes icmp errors.
  364. */
  365. addr_type = ipv6_addr_type(&hdr->daddr);
  366. if (ipv6_chk_addr(net, &hdr->daddr, skb->dev, 0) ||
  367. ipv6_chk_acast_addr_src(net, skb->dev, &hdr->daddr))
  368. saddr = &hdr->daddr;
  369. /*
  370. * Dest addr check
  371. */
  372. if (addr_type & IPV6_ADDR_MULTICAST || skb->pkt_type != PACKET_HOST) {
  373. if (type != ICMPV6_PKT_TOOBIG &&
  374. !(type == ICMPV6_PARAMPROB &&
  375. code == ICMPV6_UNK_OPTION &&
  376. (opt_unrec(skb, info))))
  377. return;
  378. saddr = NULL;
  379. }
  380. addr_type = ipv6_addr_type(&hdr->saddr);
  381. /*
  382. * Source addr check
  383. */
  384. if (__ipv6_addr_needs_scope_id(addr_type))
  385. iif = skb->dev->ifindex;
  386. /*
  387. * Must not send error if the source does not uniquely
  388. * identify a single node (RFC2463 Section 2.4).
  389. * We check unspecified / multicast addresses here,
  390. * and anycast addresses will be checked later.
  391. */
  392. if ((addr_type == IPV6_ADDR_ANY) || (addr_type & IPV6_ADDR_MULTICAST)) {
  393. net_dbg_ratelimited("icmp6_send: addr_any/mcast source [%pI6c > %pI6c]\n",
  394. &hdr->saddr, &hdr->daddr);
  395. return;
  396. }
  397. /*
  398. * Never answer to a ICMP packet.
  399. */
  400. if (is_ineligible(skb)) {
  401. net_dbg_ratelimited("icmp6_send: no reply to icmp error [%pI6c > %pI6c]\n",
  402. &hdr->saddr, &hdr->daddr);
  403. return;
  404. }
  405. mip6_addr_swap(skb);
  406. memset(&fl6, 0, sizeof(fl6));
  407. fl6.flowi6_proto = IPPROTO_ICMPV6;
  408. fl6.daddr = hdr->saddr;
  409. if (saddr)
  410. fl6.saddr = *saddr;
  411. fl6.flowi6_mark = mark;
  412. fl6.flowi6_oif = iif;
  413. fl6.fl6_icmp_type = type;
  414. fl6.fl6_icmp_code = code;
  415. security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
  416. sk = icmpv6_xmit_lock(net);
  417. if (!sk)
  418. return;
  419. sk->sk_mark = mark;
  420. np = inet6_sk(sk);
  421. if (!icmpv6_xrlim_allow(sk, type, &fl6))
  422. goto out;
  423. tmp_hdr.icmp6_type = type;
  424. tmp_hdr.icmp6_code = code;
  425. tmp_hdr.icmp6_cksum = 0;
  426. tmp_hdr.icmp6_pointer = htonl(info);
  427. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  428. fl6.flowi6_oif = np->mcast_oif;
  429. else if (!fl6.flowi6_oif)
  430. fl6.flowi6_oif = np->ucast_oif;
  431. if (!fl6.flowi6_oif)
  432. fl6.flowi6_oif = l3mdev_master_ifindex(skb->dev);
  433. dst = icmpv6_route_lookup(net, skb, sk, &fl6);
  434. if (IS_ERR(dst))
  435. goto out;
  436. hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
  437. msg.skb = skb;
  438. msg.offset = skb_network_offset(skb);
  439. msg.type = type;
  440. len = skb->len - msg.offset;
  441. len = min_t(unsigned int, len, IPV6_MIN_MTU - sizeof(struct ipv6hdr) - sizeof(struct icmp6hdr));
  442. if (len < 0) {
  443. net_dbg_ratelimited("icmp: len problem [%pI6c > %pI6c]\n",
  444. &hdr->saddr, &hdr->daddr);
  445. goto out_dst_release;
  446. }
  447. rcu_read_lock();
  448. idev = __in6_dev_get(skb->dev);
  449. err = ip6_append_data(sk, icmpv6_getfrag, &msg,
  450. len + sizeof(struct icmp6hdr),
  451. sizeof(struct icmp6hdr), hlimit,
  452. np->tclass, NULL, &fl6, (struct rt6_info *)dst,
  453. MSG_DONTWAIT, np->dontfrag);
  454. if (err) {
  455. ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
  456. ip6_flush_pending_frames(sk);
  457. } else {
  458. err = icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
  459. len + sizeof(struct icmp6hdr));
  460. }
  461. rcu_read_unlock();
  462. out_dst_release:
  463. dst_release(dst);
  464. out:
  465. icmpv6_xmit_unlock(sk);
  466. }
  467. /* Slightly more convenient version of icmp6_send.
  468. */
  469. void icmpv6_param_prob(struct sk_buff *skb, u8 code, int pos)
  470. {
  471. icmp6_send(skb, ICMPV6_PARAMPROB, code, pos);
  472. kfree_skb(skb);
  473. }
  474. static void icmpv6_echo_reply(struct sk_buff *skb)
  475. {
  476. struct net *net = dev_net(skb->dev);
  477. struct sock *sk;
  478. struct inet6_dev *idev;
  479. struct ipv6_pinfo *np;
  480. const struct in6_addr *saddr = NULL;
  481. struct icmp6hdr *icmph = icmp6_hdr(skb);
  482. struct icmp6hdr tmp_hdr;
  483. struct flowi6 fl6;
  484. struct icmpv6_msg msg;
  485. struct dst_entry *dst;
  486. int err = 0;
  487. int hlimit;
  488. u8 tclass;
  489. u32 mark = IP6_REPLY_MARK(net, skb->mark);
  490. saddr = &ipv6_hdr(skb)->daddr;
  491. if (!ipv6_unicast_destination(skb) &&
  492. !(net->ipv6.sysctl.anycast_src_echo_reply &&
  493. ipv6_anycast_destination(skb_dst(skb), saddr)))
  494. saddr = NULL;
  495. memcpy(&tmp_hdr, icmph, sizeof(tmp_hdr));
  496. tmp_hdr.icmp6_type = ICMPV6_ECHO_REPLY;
  497. memset(&fl6, 0, sizeof(fl6));
  498. fl6.flowi6_proto = IPPROTO_ICMPV6;
  499. fl6.daddr = ipv6_hdr(skb)->saddr;
  500. if (saddr)
  501. fl6.saddr = *saddr;
  502. fl6.flowi6_oif = l3mdev_fib_oif(skb->dev);
  503. fl6.fl6_icmp_type = ICMPV6_ECHO_REPLY;
  504. fl6.flowi6_mark = mark;
  505. security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
  506. sk = icmpv6_xmit_lock(net);
  507. if (!sk)
  508. return;
  509. sk->sk_mark = mark;
  510. np = inet6_sk(sk);
  511. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  512. fl6.flowi6_oif = np->mcast_oif;
  513. else if (!fl6.flowi6_oif)
  514. fl6.flowi6_oif = np->ucast_oif;
  515. err = ip6_dst_lookup(net, sk, &dst, &fl6);
  516. if (err)
  517. goto out;
  518. dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), sk, 0);
  519. if (IS_ERR(dst))
  520. goto out;
  521. hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
  522. idev = __in6_dev_get(skb->dev);
  523. msg.skb = skb;
  524. msg.offset = 0;
  525. msg.type = ICMPV6_ECHO_REPLY;
  526. tclass = ipv6_get_dsfield(ipv6_hdr(skb));
  527. err = ip6_append_data(sk, icmpv6_getfrag, &msg, skb->len + sizeof(struct icmp6hdr),
  528. sizeof(struct icmp6hdr), hlimit, tclass, NULL, &fl6,
  529. (struct rt6_info *)dst, MSG_DONTWAIT,
  530. np->dontfrag);
  531. if (err) {
  532. ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTERRORS);
  533. ip6_flush_pending_frames(sk);
  534. } else {
  535. err = icmpv6_push_pending_frames(sk, &fl6, &tmp_hdr,
  536. skb->len + sizeof(struct icmp6hdr));
  537. }
  538. dst_release(dst);
  539. out:
  540. icmpv6_xmit_unlock(sk);
  541. }
  542. void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info)
  543. {
  544. const struct inet6_protocol *ipprot;
  545. int inner_offset;
  546. __be16 frag_off;
  547. u8 nexthdr;
  548. struct net *net = dev_net(skb->dev);
  549. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  550. goto out;
  551. nexthdr = ((struct ipv6hdr *)skb->data)->nexthdr;
  552. if (ipv6_ext_hdr(nexthdr)) {
  553. /* now skip over extension headers */
  554. inner_offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
  555. &nexthdr, &frag_off);
  556. if (inner_offset < 0)
  557. goto out;
  558. } else {
  559. inner_offset = sizeof(struct ipv6hdr);
  560. }
  561. /* Checkin header including 8 bytes of inner protocol header. */
  562. if (!pskb_may_pull(skb, inner_offset+8))
  563. goto out;
  564. /* BUGGG_FUTURE: we should try to parse exthdrs in this packet.
  565. Without this we will not able f.e. to make source routed
  566. pmtu discovery.
  567. Corresponding argument (opt) to notifiers is already added.
  568. --ANK (980726)
  569. */
  570. ipprot = rcu_dereference(inet6_protos[nexthdr]);
  571. if (ipprot && ipprot->err_handler)
  572. ipprot->err_handler(skb, NULL, type, code, inner_offset, info);
  573. raw6_icmp_error(skb, nexthdr, type, code, inner_offset, info);
  574. return;
  575. out:
  576. ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
  577. }
  578. /*
  579. * Handle icmp messages
  580. */
  581. static int icmpv6_rcv(struct sk_buff *skb)
  582. {
  583. struct net_device *dev = skb->dev;
  584. struct inet6_dev *idev = __in6_dev_get(dev);
  585. const struct in6_addr *saddr, *daddr;
  586. struct icmp6hdr *hdr;
  587. u8 type;
  588. bool success = false;
  589. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  590. struct sec_path *sp = skb_sec_path(skb);
  591. int nh;
  592. if (!(sp && sp->xvec[sp->len - 1]->props.flags &
  593. XFRM_STATE_ICMP))
  594. goto drop_no_count;
  595. if (!pskb_may_pull(skb, sizeof(*hdr) + sizeof(struct ipv6hdr)))
  596. goto drop_no_count;
  597. nh = skb_network_offset(skb);
  598. skb_set_network_header(skb, sizeof(*hdr));
  599. if (!xfrm6_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
  600. goto drop_no_count;
  601. skb_set_network_header(skb, nh);
  602. }
  603. ICMP6_INC_STATS_BH(dev_net(dev), idev, ICMP6_MIB_INMSGS);
  604. saddr = &ipv6_hdr(skb)->saddr;
  605. daddr = &ipv6_hdr(skb)->daddr;
  606. if (skb_checksum_validate(skb, IPPROTO_ICMPV6, ip6_compute_pseudo)) {
  607. net_dbg_ratelimited("ICMPv6 checksum failed [%pI6c > %pI6c]\n",
  608. saddr, daddr);
  609. goto csum_error;
  610. }
  611. if (!pskb_pull(skb, sizeof(*hdr)))
  612. goto discard_it;
  613. hdr = icmp6_hdr(skb);
  614. type = hdr->icmp6_type;
  615. ICMP6MSGIN_INC_STATS_BH(dev_net(dev), idev, type);
  616. switch (type) {
  617. case ICMPV6_ECHO_REQUEST:
  618. icmpv6_echo_reply(skb);
  619. break;
  620. case ICMPV6_ECHO_REPLY:
  621. success = ping_rcv(skb);
  622. break;
  623. case ICMPV6_PKT_TOOBIG:
  624. /* BUGGG_FUTURE: if packet contains rthdr, we cannot update
  625. standard destination cache. Seems, only "advanced"
  626. destination cache will allow to solve this problem
  627. --ANK (980726)
  628. */
  629. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  630. goto discard_it;
  631. hdr = icmp6_hdr(skb);
  632. /*
  633. * Drop through to notify
  634. */
  635. case ICMPV6_DEST_UNREACH:
  636. case ICMPV6_TIME_EXCEED:
  637. case ICMPV6_PARAMPROB:
  638. icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu);
  639. break;
  640. case NDISC_ROUTER_SOLICITATION:
  641. case NDISC_ROUTER_ADVERTISEMENT:
  642. case NDISC_NEIGHBOUR_SOLICITATION:
  643. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  644. case NDISC_REDIRECT:
  645. ndisc_rcv(skb);
  646. break;
  647. case ICMPV6_MGM_QUERY:
  648. igmp6_event_query(skb);
  649. break;
  650. case ICMPV6_MGM_REPORT:
  651. igmp6_event_report(skb);
  652. break;
  653. case ICMPV6_MGM_REDUCTION:
  654. case ICMPV6_NI_QUERY:
  655. case ICMPV6_NI_REPLY:
  656. case ICMPV6_MLD2_REPORT:
  657. case ICMPV6_DHAAD_REQUEST:
  658. case ICMPV6_DHAAD_REPLY:
  659. case ICMPV6_MOBILE_PREFIX_SOL:
  660. case ICMPV6_MOBILE_PREFIX_ADV:
  661. break;
  662. default:
  663. /* informational */
  664. if (type & ICMPV6_INFOMSG_MASK)
  665. break;
  666. net_dbg_ratelimited("icmpv6: msg of unknown type [%pI6c > %pI6c]\n",
  667. saddr, daddr);
  668. /*
  669. * error of unknown type.
  670. * must pass to upper level
  671. */
  672. icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu);
  673. }
  674. /* until the v6 path can be better sorted assume failure and
  675. * preserve the status quo behaviour for the rest of the paths to here
  676. */
  677. if (success)
  678. consume_skb(skb);
  679. else
  680. kfree_skb(skb);
  681. return 0;
  682. csum_error:
  683. ICMP6_INC_STATS_BH(dev_net(dev), idev, ICMP6_MIB_CSUMERRORS);
  684. discard_it:
  685. ICMP6_INC_STATS_BH(dev_net(dev), idev, ICMP6_MIB_INERRORS);
  686. drop_no_count:
  687. kfree_skb(skb);
  688. return 0;
  689. }
  690. void icmpv6_flow_init(struct sock *sk, struct flowi6 *fl6,
  691. u8 type,
  692. const struct in6_addr *saddr,
  693. const struct in6_addr *daddr,
  694. int oif)
  695. {
  696. memset(fl6, 0, sizeof(*fl6));
  697. fl6->saddr = *saddr;
  698. fl6->daddr = *daddr;
  699. fl6->flowi6_proto = IPPROTO_ICMPV6;
  700. fl6->fl6_icmp_type = type;
  701. fl6->fl6_icmp_code = 0;
  702. fl6->flowi6_oif = oif;
  703. security_sk_classify_flow(sk, flowi6_to_flowi(fl6));
  704. }
  705. static int __net_init icmpv6_sk_init(struct net *net)
  706. {
  707. struct sock *sk;
  708. int err, i, j;
  709. net->ipv6.icmp_sk =
  710. kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL);
  711. if (!net->ipv6.icmp_sk)
  712. return -ENOMEM;
  713. for_each_possible_cpu(i) {
  714. err = inet_ctl_sock_create(&sk, PF_INET6,
  715. SOCK_RAW, IPPROTO_ICMPV6, net);
  716. if (err < 0) {
  717. pr_err("Failed to initialize the ICMP6 control socket (err %d)\n",
  718. err);
  719. goto fail;
  720. }
  721. net->ipv6.icmp_sk[i] = sk;
  722. /* Enough space for 2 64K ICMP packets, including
  723. * sk_buff struct overhead.
  724. */
  725. sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
  726. }
  727. return 0;
  728. fail:
  729. for (j = 0; j < i; j++)
  730. inet_ctl_sock_destroy(net->ipv6.icmp_sk[j]);
  731. kfree(net->ipv6.icmp_sk);
  732. return err;
  733. }
  734. static void __net_exit icmpv6_sk_exit(struct net *net)
  735. {
  736. int i;
  737. for_each_possible_cpu(i) {
  738. inet_ctl_sock_destroy(net->ipv6.icmp_sk[i]);
  739. }
  740. kfree(net->ipv6.icmp_sk);
  741. }
  742. static struct pernet_operations icmpv6_sk_ops = {
  743. .init = icmpv6_sk_init,
  744. .exit = icmpv6_sk_exit,
  745. };
  746. int __init icmpv6_init(void)
  747. {
  748. int err;
  749. err = register_pernet_subsys(&icmpv6_sk_ops);
  750. if (err < 0)
  751. return err;
  752. err = -EAGAIN;
  753. if (inet6_add_protocol(&icmpv6_protocol, IPPROTO_ICMPV6) < 0)
  754. goto fail;
  755. err = inet6_register_icmp_sender(icmp6_send);
  756. if (err)
  757. goto sender_reg_err;
  758. return 0;
  759. sender_reg_err:
  760. inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
  761. fail:
  762. pr_err("Failed to register ICMP6 protocol\n");
  763. unregister_pernet_subsys(&icmpv6_sk_ops);
  764. return err;
  765. }
  766. void icmpv6_cleanup(void)
  767. {
  768. inet6_unregister_icmp_sender(icmp6_send);
  769. unregister_pernet_subsys(&icmpv6_sk_ops);
  770. inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
  771. }
  772. static const struct icmp6_err {
  773. int err;
  774. int fatal;
  775. } tab_unreach[] = {
  776. { /* NOROUTE */
  777. .err = ENETUNREACH,
  778. .fatal = 0,
  779. },
  780. { /* ADM_PROHIBITED */
  781. .err = EACCES,
  782. .fatal = 1,
  783. },
  784. { /* Was NOT_NEIGHBOUR, now reserved */
  785. .err = EHOSTUNREACH,
  786. .fatal = 0,
  787. },
  788. { /* ADDR_UNREACH */
  789. .err = EHOSTUNREACH,
  790. .fatal = 0,
  791. },
  792. { /* PORT_UNREACH */
  793. .err = ECONNREFUSED,
  794. .fatal = 1,
  795. },
  796. { /* POLICY_FAIL */
  797. .err = EACCES,
  798. .fatal = 1,
  799. },
  800. { /* REJECT_ROUTE */
  801. .err = EACCES,
  802. .fatal = 1,
  803. },
  804. };
  805. int icmpv6_err_convert(u8 type, u8 code, int *err)
  806. {
  807. int fatal = 0;
  808. *err = EPROTO;
  809. switch (type) {
  810. case ICMPV6_DEST_UNREACH:
  811. fatal = 1;
  812. if (code < ARRAY_SIZE(tab_unreach)) {
  813. *err = tab_unreach[code].err;
  814. fatal = tab_unreach[code].fatal;
  815. }
  816. break;
  817. case ICMPV6_PKT_TOOBIG:
  818. *err = EMSGSIZE;
  819. break;
  820. case ICMPV6_PARAMPROB:
  821. *err = EPROTO;
  822. fatal = 1;
  823. break;
  824. case ICMPV6_TIME_EXCEED:
  825. *err = EHOSTUNREACH;
  826. break;
  827. }
  828. return fatal;
  829. }
  830. EXPORT_SYMBOL(icmpv6_err_convert);
  831. #ifdef CONFIG_SYSCTL
  832. static struct ctl_table ipv6_icmp_table_template[] = {
  833. {
  834. .procname = "ratelimit",
  835. .data = &init_net.ipv6.sysctl.icmpv6_time,
  836. .maxlen = sizeof(int),
  837. .mode = 0644,
  838. .proc_handler = proc_dointvec_ms_jiffies,
  839. },
  840. { },
  841. };
  842. struct ctl_table * __net_init ipv6_icmp_sysctl_init(struct net *net)
  843. {
  844. struct ctl_table *table;
  845. table = kmemdup(ipv6_icmp_table_template,
  846. sizeof(ipv6_icmp_table_template),
  847. GFP_KERNEL);
  848. if (table)
  849. table[0].data = &net->ipv6.sysctl.icmpv6_time;
  850. return table;
  851. }
  852. #endif