icmp.c 29 KB

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