icmp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * Internet Control Message Protocol (ICMPv6)
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * $Id: icmp.c,v 1.38 2002/02/08 03:57:19 davem Exp $
  9. *
  10. * Based on net/ipv4/icmp.c
  11. *
  12. * RFC 1885
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. */
  19. /*
  20. * Changes:
  21. *
  22. * Andi Kleen : exception handling
  23. * Andi Kleen add rate limits. never reply to a icmp.
  24. * add more length checks and other fixes.
  25. * yoshfuji : ensure to sent parameter problem for
  26. * fragments.
  27. * YOSHIFUJI Hideaki @USAGI: added sysctl for icmp rate limit.
  28. * Randy Dunlap and
  29. * YOSHIFUJI Hideaki @USAGI: Per-interface statistics support
  30. * Kazunori MIYAZAWA @USAGI: change output process to use ip6_append_data
  31. */
  32. #include <linux/module.h>
  33. #include <linux/errno.h>
  34. #include <linux/types.h>
  35. #include <linux/socket.h>
  36. #include <linux/in.h>
  37. #include <linux/kernel.h>
  38. #include <linux/sched.h>
  39. #include <linux/sockios.h>
  40. #include <linux/net.h>
  41. #include <linux/skbuff.h>
  42. #include <linux/init.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/protocol.h>
  54. #include <net/raw.h>
  55. #include <net/rawv6.h>
  56. #include <net/transp_v6.h>
  57. #include <net/ip6_route.h>
  58. #include <net/addrconf.h>
  59. #include <net/icmp.h>
  60. #include <asm/uaccess.h>
  61. #include <asm/system.h>
  62. DEFINE_SNMP_STAT(struct icmpv6_mib, icmpv6_statistics) __read_mostly;
  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 DEFINE_PER_CPU(struct socket *, __icmpv6_socket) = NULL;
  71. #define icmpv6_socket __get_cpu_var(__icmpv6_socket)
  72. static int icmpv6_rcv(struct sk_buff **pskb);
  73. static struct inet6_protocol icmpv6_protocol = {
  74. .handler = icmpv6_rcv,
  75. .flags = INET6_PROTO_FINAL,
  76. };
  77. static __inline__ int icmpv6_xmit_lock(void)
  78. {
  79. local_bh_disable();
  80. if (unlikely(!spin_trylock(&icmpv6_socket->sk->sk_lock.slock))) {
  81. /* This can happen if the output path (f.e. SIT or
  82. * ip6ip6 tunnel) signals dst_link_failure() for an
  83. * outgoing ICMP6 packet.
  84. */
  85. local_bh_enable();
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. static __inline__ void icmpv6_xmit_unlock(void)
  91. {
  92. spin_unlock_bh(&icmpv6_socket->sk->sk_lock.slock);
  93. }
  94. /*
  95. * Slightly more convenient version of icmpv6_send.
  96. */
  97. void icmpv6_param_prob(struct sk_buff *skb, int code, int pos)
  98. {
  99. icmpv6_send(skb, ICMPV6_PARAMPROB, code, pos, skb->dev);
  100. kfree_skb(skb);
  101. }
  102. /*
  103. * Figure out, may we reply to this packet with icmp error.
  104. *
  105. * We do not reply, if:
  106. * - it was icmp error message.
  107. * - it is truncated, so that it is known, that protocol is ICMPV6
  108. * (i.e. in the middle of some exthdr)
  109. *
  110. * --ANK (980726)
  111. */
  112. static int is_ineligible(struct sk_buff *skb)
  113. {
  114. int ptr = (u8*)(skb->nh.ipv6h+1) - skb->data;
  115. int len = skb->len - ptr;
  116. __u8 nexthdr = skb->nh.ipv6h->nexthdr;
  117. if (len < 0)
  118. return 1;
  119. ptr = ipv6_skip_exthdr(skb, ptr, &nexthdr);
  120. if (ptr < 0)
  121. return 0;
  122. if (nexthdr == IPPROTO_ICMPV6) {
  123. u8 _type, *tp;
  124. tp = skb_header_pointer(skb,
  125. ptr+offsetof(struct icmp6hdr, icmp6_type),
  126. sizeof(_type), &_type);
  127. if (tp == NULL ||
  128. !(*tp & ICMPV6_INFOMSG_MASK))
  129. return 1;
  130. }
  131. return 0;
  132. }
  133. static int sysctl_icmpv6_time = 1*HZ;
  134. /*
  135. * Check the ICMP output rate limit
  136. */
  137. static inline int icmpv6_xrlim_allow(struct sock *sk, int type,
  138. struct flowi *fl)
  139. {
  140. struct dst_entry *dst;
  141. int res = 0;
  142. /* Informational messages are not limited. */
  143. if (type & ICMPV6_INFOMSG_MASK)
  144. return 1;
  145. /* Do not limit pmtu discovery, it would break it. */
  146. if (type == ICMPV6_PKT_TOOBIG)
  147. return 1;
  148. /*
  149. * Look up the output route.
  150. * XXX: perhaps the expire for routing entries cloned by
  151. * this lookup should be more aggressive (not longer than timeout).
  152. */
  153. dst = ip6_route_output(sk, fl);
  154. if (dst->error) {
  155. IP6_INC_STATS(IPSTATS_MIB_OUTNOROUTES);
  156. } else if (dst->dev && (dst->dev->flags&IFF_LOOPBACK)) {
  157. res = 1;
  158. } else {
  159. struct rt6_info *rt = (struct rt6_info *)dst;
  160. int tmo = sysctl_icmpv6_time;
  161. /* Give more bandwidth to wider prefixes. */
  162. if (rt->rt6i_dst.plen < 128)
  163. tmo >>= ((128 - rt->rt6i_dst.plen)>>5);
  164. res = xrlim_allow(dst, tmo);
  165. }
  166. dst_release(dst);
  167. return res;
  168. }
  169. /*
  170. * an inline helper for the "simple" if statement below
  171. * checks if parameter problem report is caused by an
  172. * unrecognized IPv6 option that has the Option Type
  173. * highest-order two bits set to 10
  174. */
  175. static __inline__ int opt_unrec(struct sk_buff *skb, __u32 offset)
  176. {
  177. u8 _optval, *op;
  178. offset += skb->nh.raw - skb->data;
  179. op = skb_header_pointer(skb, offset, sizeof(_optval), &_optval);
  180. if (op == NULL)
  181. return 1;
  182. return (*op & 0xC0) == 0x80;
  183. }
  184. static int icmpv6_push_pending_frames(struct sock *sk, struct flowi *fl, struct icmp6hdr *thdr, int len)
  185. {
  186. struct sk_buff *skb;
  187. struct icmp6hdr *icmp6h;
  188. int err = 0;
  189. if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
  190. goto out;
  191. icmp6h = (struct icmp6hdr*) skb->h.raw;
  192. memcpy(icmp6h, thdr, sizeof(struct icmp6hdr));
  193. icmp6h->icmp6_cksum = 0;
  194. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  195. skb->csum = csum_partial((char *)icmp6h,
  196. sizeof(struct icmp6hdr), skb->csum);
  197. icmp6h->icmp6_cksum = csum_ipv6_magic(&fl->fl6_src,
  198. &fl->fl6_dst,
  199. len, fl->proto,
  200. skb->csum);
  201. } else {
  202. u32 tmp_csum = 0;
  203. skb_queue_walk(&sk->sk_write_queue, skb) {
  204. tmp_csum = csum_add(tmp_csum, skb->csum);
  205. }
  206. tmp_csum = csum_partial((char *)icmp6h,
  207. sizeof(struct icmp6hdr), tmp_csum);
  208. tmp_csum = csum_ipv6_magic(&fl->fl6_src,
  209. &fl->fl6_dst,
  210. len, fl->proto, tmp_csum);
  211. icmp6h->icmp6_cksum = tmp_csum;
  212. }
  213. if (icmp6h->icmp6_cksum == 0)
  214. icmp6h->icmp6_cksum = -1;
  215. ip6_push_pending_frames(sk);
  216. out:
  217. return err;
  218. }
  219. struct icmpv6_msg {
  220. struct sk_buff *skb;
  221. int offset;
  222. };
  223. static int icmpv6_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
  224. {
  225. struct icmpv6_msg *msg = (struct icmpv6_msg *) from;
  226. struct sk_buff *org_skb = msg->skb;
  227. __u32 csum = 0;
  228. csum = skb_copy_and_csum_bits(org_skb, msg->offset + offset,
  229. to, len, csum);
  230. skb->csum = csum_block_add(skb->csum, csum, odd);
  231. return 0;
  232. }
  233. /*
  234. * Send an ICMP message in response to a packet in error
  235. */
  236. void icmpv6_send(struct sk_buff *skb, int type, int code, __u32 info,
  237. struct net_device *dev)
  238. {
  239. struct inet6_dev *idev = NULL;
  240. struct ipv6hdr *hdr = skb->nh.ipv6h;
  241. struct sock *sk;
  242. struct ipv6_pinfo *np;
  243. struct in6_addr *saddr = NULL;
  244. struct dst_entry *dst;
  245. struct icmp6hdr tmp_hdr;
  246. struct flowi fl;
  247. struct icmpv6_msg msg;
  248. int iif = 0;
  249. int addr_type = 0;
  250. int len;
  251. int hlimit, tclass;
  252. int err = 0;
  253. if ((u8*)hdr < skb->head || (u8*)(hdr+1) > skb->tail)
  254. return;
  255. /*
  256. * Make sure we respect the rules
  257. * i.e. RFC 1885 2.4(e)
  258. * Rule (e.1) is enforced by not using icmpv6_send
  259. * in any code that processes icmp errors.
  260. */
  261. addr_type = ipv6_addr_type(&hdr->daddr);
  262. if (ipv6_chk_addr(&hdr->daddr, skb->dev, 0))
  263. saddr = &hdr->daddr;
  264. /*
  265. * Dest addr check
  266. */
  267. if ((addr_type & IPV6_ADDR_MULTICAST || skb->pkt_type != PACKET_HOST)) {
  268. if (type != ICMPV6_PKT_TOOBIG &&
  269. !(type == ICMPV6_PARAMPROB &&
  270. code == ICMPV6_UNK_OPTION &&
  271. (opt_unrec(skb, info))))
  272. return;
  273. saddr = NULL;
  274. }
  275. addr_type = ipv6_addr_type(&hdr->saddr);
  276. /*
  277. * Source addr check
  278. */
  279. if (addr_type & IPV6_ADDR_LINKLOCAL)
  280. iif = skb->dev->ifindex;
  281. /*
  282. * Must not send error if the source does not uniquely
  283. * identify a single node (RFC2463 Section 2.4).
  284. * We check unspecified / multicast addresses here,
  285. * and anycast addresses will be checked later.
  286. */
  287. if ((addr_type == IPV6_ADDR_ANY) || (addr_type & IPV6_ADDR_MULTICAST)) {
  288. LIMIT_NETDEBUG(KERN_DEBUG "icmpv6_send: addr_any/mcast source\n");
  289. return;
  290. }
  291. /*
  292. * Never answer to a ICMP packet.
  293. */
  294. if (is_ineligible(skb)) {
  295. LIMIT_NETDEBUG(KERN_DEBUG "icmpv6_send: no reply to icmp error\n");
  296. return;
  297. }
  298. memset(&fl, 0, sizeof(fl));
  299. fl.proto = IPPROTO_ICMPV6;
  300. ipv6_addr_copy(&fl.fl6_dst, &hdr->saddr);
  301. if (saddr)
  302. ipv6_addr_copy(&fl.fl6_src, saddr);
  303. fl.oif = iif;
  304. fl.fl_icmp_type = type;
  305. fl.fl_icmp_code = code;
  306. if (icmpv6_xmit_lock())
  307. return;
  308. sk = icmpv6_socket->sk;
  309. np = inet6_sk(sk);
  310. if (!icmpv6_xrlim_allow(sk, type, &fl))
  311. goto out;
  312. tmp_hdr.icmp6_type = type;
  313. tmp_hdr.icmp6_code = code;
  314. tmp_hdr.icmp6_cksum = 0;
  315. tmp_hdr.icmp6_pointer = htonl(info);
  316. if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst))
  317. fl.oif = np->mcast_oif;
  318. err = ip6_dst_lookup(sk, &dst, &fl);
  319. if (err)
  320. goto out;
  321. /*
  322. * We won't send icmp if the destination is known
  323. * anycast.
  324. */
  325. if (((struct rt6_info *)dst)->rt6i_flags & RTF_ANYCAST) {
  326. LIMIT_NETDEBUG(KERN_DEBUG "icmpv6_send: acast source\n");
  327. goto out_dst_release;
  328. }
  329. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0)
  330. goto out;
  331. if (ipv6_addr_is_multicast(&fl.fl6_dst))
  332. hlimit = np->mcast_hops;
  333. else
  334. hlimit = np->hop_limit;
  335. if (hlimit < 0)
  336. hlimit = dst_metric(dst, RTAX_HOPLIMIT);
  337. if (hlimit < 0)
  338. hlimit = ipv6_get_hoplimit(dst->dev);
  339. tclass = np->cork.tclass;
  340. if (tclass < 0)
  341. tclass = 0;
  342. msg.skb = skb;
  343. msg.offset = skb->nh.raw - skb->data;
  344. len = skb->len - msg.offset;
  345. len = min_t(unsigned int, len, IPV6_MIN_MTU - sizeof(struct ipv6hdr) -sizeof(struct icmp6hdr));
  346. if (len < 0) {
  347. LIMIT_NETDEBUG(KERN_DEBUG "icmp: len problem\n");
  348. goto out_dst_release;
  349. }
  350. idev = in6_dev_get(skb->dev);
  351. err = ip6_append_data(sk, icmpv6_getfrag, &msg,
  352. len + sizeof(struct icmp6hdr),
  353. sizeof(struct icmp6hdr),
  354. hlimit, tclass, NULL, &fl, (struct rt6_info*)dst,
  355. MSG_DONTWAIT);
  356. if (err) {
  357. ip6_flush_pending_frames(sk);
  358. goto out_put;
  359. }
  360. err = icmpv6_push_pending_frames(sk, &fl, &tmp_hdr, len + sizeof(struct icmp6hdr));
  361. if (type >= ICMPV6_DEST_UNREACH && type <= ICMPV6_PARAMPROB)
  362. ICMP6_INC_STATS_OFFSET_BH(idev, ICMP6_MIB_OUTDESTUNREACHS, type - ICMPV6_DEST_UNREACH);
  363. ICMP6_INC_STATS_BH(idev, ICMP6_MIB_OUTMSGS);
  364. out_put:
  365. if (likely(idev != NULL))
  366. in6_dev_put(idev);
  367. out_dst_release:
  368. dst_release(dst);
  369. out:
  370. icmpv6_xmit_unlock();
  371. }
  372. static void icmpv6_echo_reply(struct sk_buff *skb)
  373. {
  374. struct sock *sk;
  375. struct inet6_dev *idev;
  376. struct ipv6_pinfo *np;
  377. struct in6_addr *saddr = NULL;
  378. struct icmp6hdr *icmph = (struct icmp6hdr *) skb->h.raw;
  379. struct icmp6hdr tmp_hdr;
  380. struct flowi fl;
  381. struct icmpv6_msg msg;
  382. struct dst_entry *dst;
  383. int err = 0;
  384. int hlimit;
  385. int tclass;
  386. saddr = &skb->nh.ipv6h->daddr;
  387. if (!ipv6_unicast_destination(skb))
  388. saddr = NULL;
  389. memcpy(&tmp_hdr, icmph, sizeof(tmp_hdr));
  390. tmp_hdr.icmp6_type = ICMPV6_ECHO_REPLY;
  391. memset(&fl, 0, sizeof(fl));
  392. fl.proto = IPPROTO_ICMPV6;
  393. ipv6_addr_copy(&fl.fl6_dst, &skb->nh.ipv6h->saddr);
  394. if (saddr)
  395. ipv6_addr_copy(&fl.fl6_src, saddr);
  396. fl.oif = skb->dev->ifindex;
  397. fl.fl_icmp_type = ICMPV6_ECHO_REPLY;
  398. if (icmpv6_xmit_lock())
  399. return;
  400. sk = icmpv6_socket->sk;
  401. np = inet6_sk(sk);
  402. if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst))
  403. fl.oif = np->mcast_oif;
  404. err = ip6_dst_lookup(sk, &dst, &fl);
  405. if (err)
  406. goto out;
  407. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0)
  408. goto out;
  409. if (ipv6_addr_is_multicast(&fl.fl6_dst))
  410. hlimit = np->mcast_hops;
  411. else
  412. hlimit = np->hop_limit;
  413. if (hlimit < 0)
  414. hlimit = dst_metric(dst, RTAX_HOPLIMIT);
  415. if (hlimit < 0)
  416. hlimit = ipv6_get_hoplimit(dst->dev);
  417. tclass = np->cork.tclass;
  418. if (tclass < 0)
  419. tclass = 0;
  420. idev = in6_dev_get(skb->dev);
  421. msg.skb = skb;
  422. msg.offset = 0;
  423. err = ip6_append_data(sk, icmpv6_getfrag, &msg, skb->len + sizeof(struct icmp6hdr),
  424. sizeof(struct icmp6hdr), hlimit, tclass, NULL, &fl,
  425. (struct rt6_info*)dst, MSG_DONTWAIT);
  426. if (err) {
  427. ip6_flush_pending_frames(sk);
  428. goto out_put;
  429. }
  430. err = icmpv6_push_pending_frames(sk, &fl, &tmp_hdr, skb->len + sizeof(struct icmp6hdr));
  431. ICMP6_INC_STATS_BH(idev, ICMP6_MIB_OUTECHOREPLIES);
  432. ICMP6_INC_STATS_BH(idev, ICMP6_MIB_OUTMSGS);
  433. out_put:
  434. if (likely(idev != NULL))
  435. in6_dev_put(idev);
  436. dst_release(dst);
  437. out:
  438. icmpv6_xmit_unlock();
  439. }
  440. static void icmpv6_notify(struct sk_buff *skb, int type, int code, u32 info)
  441. {
  442. struct in6_addr *saddr, *daddr;
  443. struct inet6_protocol *ipprot;
  444. struct sock *sk;
  445. int inner_offset;
  446. int hash;
  447. u8 nexthdr;
  448. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  449. return;
  450. nexthdr = ((struct ipv6hdr *)skb->data)->nexthdr;
  451. if (ipv6_ext_hdr(nexthdr)) {
  452. /* now skip over extension headers */
  453. inner_offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr);
  454. if (inner_offset<0)
  455. return;
  456. } else {
  457. inner_offset = sizeof(struct ipv6hdr);
  458. }
  459. /* Checkin header including 8 bytes of inner protocol header. */
  460. if (!pskb_may_pull(skb, inner_offset+8))
  461. return;
  462. saddr = &skb->nh.ipv6h->saddr;
  463. daddr = &skb->nh.ipv6h->daddr;
  464. /* BUGGG_FUTURE: we should try to parse exthdrs in this packet.
  465. Without this we will not able f.e. to make source routed
  466. pmtu discovery.
  467. Corresponding argument (opt) to notifiers is already added.
  468. --ANK (980726)
  469. */
  470. hash = nexthdr & (MAX_INET_PROTOS - 1);
  471. rcu_read_lock();
  472. ipprot = rcu_dereference(inet6_protos[hash]);
  473. if (ipprot && ipprot->err_handler)
  474. ipprot->err_handler(skb, NULL, type, code, inner_offset, info);
  475. rcu_read_unlock();
  476. read_lock(&raw_v6_lock);
  477. if ((sk = sk_head(&raw_v6_htable[hash])) != NULL) {
  478. while((sk = __raw_v6_lookup(sk, nexthdr, daddr, saddr,
  479. IP6CB(skb)->iif))) {
  480. rawv6_err(sk, skb, NULL, type, code, inner_offset, info);
  481. sk = sk_next(sk);
  482. }
  483. }
  484. read_unlock(&raw_v6_lock);
  485. }
  486. /*
  487. * Handle icmp messages
  488. */
  489. static int icmpv6_rcv(struct sk_buff **pskb)
  490. {
  491. struct sk_buff *skb = *pskb;
  492. struct net_device *dev = skb->dev;
  493. struct inet6_dev *idev = __in6_dev_get(dev);
  494. struct in6_addr *saddr, *daddr;
  495. struct ipv6hdr *orig_hdr;
  496. struct icmp6hdr *hdr;
  497. int type;
  498. ICMP6_INC_STATS_BH(idev, ICMP6_MIB_INMSGS);
  499. saddr = &skb->nh.ipv6h->saddr;
  500. daddr = &skb->nh.ipv6h->daddr;
  501. /* Perform checksum. */
  502. switch (skb->ip_summed) {
  503. case CHECKSUM_HW:
  504. if (!csum_ipv6_magic(saddr, daddr, skb->len, IPPROTO_ICMPV6,
  505. skb->csum))
  506. break;
  507. /* fall through */
  508. case CHECKSUM_NONE:
  509. skb->csum = ~csum_ipv6_magic(saddr, daddr, skb->len,
  510. IPPROTO_ICMPV6, 0);
  511. if (__skb_checksum_complete(skb)) {
  512. LIMIT_NETDEBUG(KERN_DEBUG "ICMPv6 checksum failed [" NIP6_FMT " > " NIP6_FMT "]\n",
  513. NIP6(*saddr), NIP6(*daddr));
  514. goto discard_it;
  515. }
  516. }
  517. if (!pskb_pull(skb, sizeof(struct icmp6hdr)))
  518. goto discard_it;
  519. hdr = (struct icmp6hdr *) skb->h.raw;
  520. type = hdr->icmp6_type;
  521. if (type >= ICMPV6_DEST_UNREACH && type <= ICMPV6_PARAMPROB)
  522. ICMP6_INC_STATS_OFFSET_BH(idev, ICMP6_MIB_INDESTUNREACHS, type - ICMPV6_DEST_UNREACH);
  523. else if (type >= ICMPV6_ECHO_REQUEST && type <= NDISC_REDIRECT)
  524. ICMP6_INC_STATS_OFFSET_BH(idev, ICMP6_MIB_INECHOS, type - ICMPV6_ECHO_REQUEST);
  525. switch (type) {
  526. case ICMPV6_ECHO_REQUEST:
  527. icmpv6_echo_reply(skb);
  528. break;
  529. case ICMPV6_ECHO_REPLY:
  530. /* we couldn't care less */
  531. break;
  532. case ICMPV6_PKT_TOOBIG:
  533. /* BUGGG_FUTURE: if packet contains rthdr, we cannot update
  534. standard destination cache. Seems, only "advanced"
  535. destination cache will allow to solve this problem
  536. --ANK (980726)
  537. */
  538. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  539. goto discard_it;
  540. hdr = (struct icmp6hdr *) skb->h.raw;
  541. orig_hdr = (struct ipv6hdr *) (hdr + 1);
  542. rt6_pmtu_discovery(&orig_hdr->daddr, &orig_hdr->saddr, dev,
  543. ntohl(hdr->icmp6_mtu));
  544. /*
  545. * Drop through to notify
  546. */
  547. case ICMPV6_DEST_UNREACH:
  548. case ICMPV6_TIME_EXCEED:
  549. case ICMPV6_PARAMPROB:
  550. icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu);
  551. break;
  552. case NDISC_ROUTER_SOLICITATION:
  553. case NDISC_ROUTER_ADVERTISEMENT:
  554. case NDISC_NEIGHBOUR_SOLICITATION:
  555. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  556. case NDISC_REDIRECT:
  557. ndisc_rcv(skb);
  558. break;
  559. case ICMPV6_MGM_QUERY:
  560. igmp6_event_query(skb);
  561. break;
  562. case ICMPV6_MGM_REPORT:
  563. igmp6_event_report(skb);
  564. break;
  565. case ICMPV6_MGM_REDUCTION:
  566. case ICMPV6_NI_QUERY:
  567. case ICMPV6_NI_REPLY:
  568. case ICMPV6_MLD2_REPORT:
  569. case ICMPV6_DHAAD_REQUEST:
  570. case ICMPV6_DHAAD_REPLY:
  571. case ICMPV6_MOBILE_PREFIX_SOL:
  572. case ICMPV6_MOBILE_PREFIX_ADV:
  573. break;
  574. default:
  575. LIMIT_NETDEBUG(KERN_DEBUG "icmpv6: msg of unknown type\n");
  576. /* informational */
  577. if (type & ICMPV6_INFOMSG_MASK)
  578. break;
  579. /*
  580. * error of unknown type.
  581. * must pass to upper level
  582. */
  583. icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu);
  584. };
  585. kfree_skb(skb);
  586. return 0;
  587. discard_it:
  588. ICMP6_INC_STATS_BH(idev, ICMP6_MIB_INERRORS);
  589. kfree_skb(skb);
  590. return 0;
  591. }
  592. int __init icmpv6_init(struct net_proto_family *ops)
  593. {
  594. struct sock *sk;
  595. int err, i, j;
  596. for_each_cpu(i) {
  597. err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6,
  598. &per_cpu(__icmpv6_socket, i));
  599. if (err < 0) {
  600. printk(KERN_ERR
  601. "Failed to initialize the ICMP6 control socket "
  602. "(err %d).\n",
  603. err);
  604. goto fail;
  605. }
  606. sk = per_cpu(__icmpv6_socket, i)->sk;
  607. sk->sk_allocation = GFP_ATOMIC;
  608. /* Enough space for 2 64K ICMP packets, including
  609. * sk_buff struct overhead.
  610. */
  611. sk->sk_sndbuf =
  612. (2 * ((64 * 1024) + sizeof(struct sk_buff)));
  613. sk->sk_prot->unhash(sk);
  614. }
  615. if (inet6_add_protocol(&icmpv6_protocol, IPPROTO_ICMPV6) < 0) {
  616. printk(KERN_ERR "Failed to register ICMP6 protocol\n");
  617. err = -EAGAIN;
  618. goto fail;
  619. }
  620. return 0;
  621. fail:
  622. for (j = 0; j < i; j++) {
  623. if (!cpu_possible(j))
  624. continue;
  625. sock_release(per_cpu(__icmpv6_socket, j));
  626. }
  627. return err;
  628. }
  629. void icmpv6_cleanup(void)
  630. {
  631. int i;
  632. for_each_cpu(i) {
  633. sock_release(per_cpu(__icmpv6_socket, i));
  634. }
  635. inet6_del_protocol(&icmpv6_protocol, IPPROTO_ICMPV6);
  636. }
  637. static const struct icmp6_err {
  638. int err;
  639. int fatal;
  640. } tab_unreach[] = {
  641. { /* NOROUTE */
  642. .err = ENETUNREACH,
  643. .fatal = 0,
  644. },
  645. { /* ADM_PROHIBITED */
  646. .err = EACCES,
  647. .fatal = 1,
  648. },
  649. { /* Was NOT_NEIGHBOUR, now reserved */
  650. .err = EHOSTUNREACH,
  651. .fatal = 0,
  652. },
  653. { /* ADDR_UNREACH */
  654. .err = EHOSTUNREACH,
  655. .fatal = 0,
  656. },
  657. { /* PORT_UNREACH */
  658. .err = ECONNREFUSED,
  659. .fatal = 1,
  660. },
  661. };
  662. int icmpv6_err_convert(int type, int code, int *err)
  663. {
  664. int fatal = 0;
  665. *err = EPROTO;
  666. switch (type) {
  667. case ICMPV6_DEST_UNREACH:
  668. fatal = 1;
  669. if (code <= ICMPV6_PORT_UNREACH) {
  670. *err = tab_unreach[code].err;
  671. fatal = tab_unreach[code].fatal;
  672. }
  673. break;
  674. case ICMPV6_PKT_TOOBIG:
  675. *err = EMSGSIZE;
  676. break;
  677. case ICMPV6_PARAMPROB:
  678. *err = EPROTO;
  679. fatal = 1;
  680. break;
  681. case ICMPV6_TIME_EXCEED:
  682. *err = EHOSTUNREACH;
  683. break;
  684. };
  685. return fatal;
  686. }
  687. #ifdef CONFIG_SYSCTL
  688. ctl_table ipv6_icmp_table[] = {
  689. {
  690. .ctl_name = NET_IPV6_ICMP_RATELIMIT,
  691. .procname = "ratelimit",
  692. .data = &sysctl_icmpv6_time,
  693. .maxlen = sizeof(int),
  694. .mode = 0644,
  695. .proc_handler = &proc_dointvec
  696. },
  697. { .ctl_name = 0 },
  698. };
  699. #endif