icmp.c 28 KB

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