icmp.c 22 KB

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