icmp.c 23 KB

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