Merge with Linux 2.3.40.
[linux-2.6/linux-mips.git] / net / ipv6 / icmp.c
blobcfa18eee80d19298d24117947a727822298cee21
1 /*
2 * Internet Control Message Protocol (ICMPv6)
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: icmp.c,v 1.25 2000/01/09 02:19:54 davem Exp $
10 * Based on net/ipv4/icmp.c
12 * RFC 1885
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.
21 * Changes:
23 * Andi Kleen : exception handling
24 * Andi Kleen add rate limits. never reply to a icmp.
25 * add more length checks and other fixes.
28 #define __NO_VERSION__
29 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/types.h>
32 #include <linux/socket.h>
33 #include <linux/in.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/sockios.h>
37 #include <linux/net.h>
38 #include <linux/skbuff.h>
39 #include <linux/init.h>
41 #include <linux/inet.h>
42 #include <linux/netdevice.h>
43 #include <linux/icmpv6.h>
45 #include <net/ip.h>
46 #include <net/sock.h>
48 #include <net/ipv6.h>
49 #include <net/checksum.h>
50 #include <net/protocol.h>
51 #include <net/raw.h>
52 #include <net/rawv6.h>
53 #include <net/transp_v6.h>
54 #include <net/ip6_route.h>
55 #include <net/addrconf.h>
56 #include <net/icmp.h>
58 #include <asm/uaccess.h>
59 #include <asm/system.h>
61 struct icmpv6_mib icmpv6_statistics[NR_CPUS*2];
64 * ICMP socket for flow control.
67 struct socket *icmpv6_socket;
69 int icmpv6_rcv(struct sk_buff *skb, unsigned long len);
71 static struct inet6_protocol icmpv6_protocol =
73 icmpv6_rcv, /* handler */
74 NULL, /* error control */
75 NULL, /* next */
76 IPPROTO_ICMPV6, /* protocol ID */
77 0, /* copy */
78 NULL, /* data */
79 "ICMPv6" /* name */
82 struct icmpv6_msg {
83 struct icmp6hdr icmph;
84 __u8 *data;
85 struct in6_addr *daddr;
86 int len;
87 __u32 csum;
91 static int icmpv6_xmit_holder = -1;
93 static int icmpv6_xmit_lock_bh(void)
95 if (!spin_trylock(&icmpv6_socket->sk->lock.slock)) {
96 if (icmpv6_xmit_holder == smp_processor_id())
97 return -EAGAIN;
98 spin_lock(&icmpv6_socket->sk->lock.slock);
100 icmpv6_xmit_holder = smp_processor_id();
101 return 0;
104 static __inline__ int icmpv6_xmit_lock(void)
106 int ret;
107 local_bh_disable();
108 ret = icmpv6_xmit_lock_bh();
109 if (ret)
110 local_bh_enable();
111 return ret;
114 static void icmpv6_xmit_unlock_bh(void)
116 icmpv6_xmit_holder = -1;
117 spin_unlock(&icmpv6_socket->sk->lock.slock);
120 static __inline__ void icmpv6_xmit_unlock(void)
122 icmpv6_xmit_unlock_bh();
123 local_bh_enable();
129 * getfrag callback
132 static int icmpv6_getfrag(const void *data, struct in6_addr *saddr,
133 char *buff, unsigned int offset, unsigned int len)
135 struct icmpv6_msg *msg = (struct icmpv6_msg *) data;
136 struct icmp6hdr *icmph;
137 __u32 csum;
140 * in theory offset must be 0 since we never send more
141 * than IPV6_MIN_MTU bytes on an error or more than the path mtu
142 * on an echo reply. (those are the rules on RFC 1883)
144 * Luckily, this statement is obsolete after
145 * draft-ietf-ipngwg-icmp-v2-00 --ANK (980730)
148 if (offset) {
149 csum = csum_partial_copy((void *) msg->data +
150 offset - sizeof(struct icmp6hdr),
151 buff, len, msg->csum);
152 msg->csum = csum;
153 return 0;
156 csum = csum_partial_copy((void *) &msg->icmph, buff,
157 sizeof(struct icmp6hdr), msg->csum);
159 csum = csum_partial_copy((void *) msg->data,
160 buff + sizeof(struct icmp6hdr),
161 len - sizeof(struct icmp6hdr), csum);
163 icmph = (struct icmp6hdr *) buff;
165 icmph->icmp6_cksum = csum_ipv6_magic(saddr, msg->daddr, msg->len,
166 IPPROTO_ICMPV6, csum);
167 return 0;
172 * Slightly more convenient version of icmpv6_send.
174 void icmpv6_param_prob(struct sk_buff *skb, int code, void *pos)
176 int offset = (u8*)pos - (u8*)skb->nh.ipv6h;
178 icmpv6_send(skb, ICMPV6_PARAMPROB, code, offset, skb->dev);
179 kfree_skb(skb);
183 * Figure out, may we reply to this packet with icmp error.
185 * We do not reply, if:
186 * - it was icmp error message.
187 * - it is truncated, so that it is known, that protocol is ICMPV6
188 * (i.e. in the middle of some exthdr)
189 * - it is not the first fragment. BTW IPv6 specs say nothing about
190 * this case, but it is clear, that our reply would be useless
191 * for sender.
193 * --ANK (980726)
196 static int is_ineligible(struct ipv6hdr *hdr, int len)
198 u8 *ptr;
199 __u8 nexthdr = hdr->nexthdr;
201 if (len < (int)sizeof(*hdr))
202 return 1;
204 ptr = ipv6_skip_exthdr((struct ipv6_opt_hdr *)(hdr+1), &nexthdr, len - sizeof(*hdr));
205 if (!ptr)
206 return 0;
207 if (nexthdr == IPPROTO_ICMPV6) {
208 struct icmp6hdr *ihdr = (struct icmp6hdr *)ptr;
209 return (ptr - (u8*)hdr) > len || !(ihdr->icmp6_type & 0x80);
211 return nexthdr == NEXTHDR_FRAGMENT;
214 int sysctl_icmpv6_time = 1*HZ;
217 * Check the ICMP output rate limit
219 static inline int icmpv6_xrlim_allow(struct sock *sk, int type,
220 struct flowi *fl)
222 struct dst_entry *dst;
223 int res = 0;
225 /* Informational messages are not limited. */
226 if (type & 0x80)
227 return 1;
229 /* Do not limit pmtu discovery, it would break it. */
230 if (type == ICMPV6_PKT_TOOBIG)
231 return 1;
234 * Look up the output route.
235 * XXX: perhaps the expire for routing entries cloned by
236 * this lookup should be more aggressive (not longer than timeout).
238 dst = ip6_route_output(sk, fl);
239 if (dst->error) {
240 IP6_INC_STATS(Ip6OutNoRoutes);
241 } else if (dst->dev && (dst->dev->flags&IFF_LOOPBACK)) {
242 res = 1;
243 } else {
244 struct rt6_info *rt = (struct rt6_info *)dst;
245 int tmo = sysctl_icmpv6_time;
247 /* Give more bandwidth to wider prefixes. */
248 if (rt->rt6i_dst.plen < 128)
249 tmo >>= ((128 - rt->rt6i_dst.plen)>>5);
251 res = xrlim_allow(dst, tmo);
253 dst_release(dst);
254 return res;
258 * an inline helper for the "simple" if statement below
259 * checks if parameter problem report is caused by an
260 * unrecognized IPv6 option that has the Option Type
261 * highest-order two bits set to 10
264 static __inline__ int opt_unrec(struct sk_buff *skb, __u32 offset)
266 u8 *buff = skb->nh.raw;
268 return ( ( *(buff + offset) & 0xC0 ) == 0x80 );
272 * Send an ICMP message in response to a packet in error
275 void icmpv6_send(struct sk_buff *skb, int type, int code, __u32 info,
276 struct net_device *dev)
278 struct ipv6hdr *hdr = skb->nh.ipv6h;
279 struct sock *sk = icmpv6_socket->sk;
280 struct in6_addr *saddr = NULL;
281 int iif = 0;
282 struct icmpv6_msg msg;
283 struct flowi fl;
284 int addr_type = 0;
285 int len;
288 * sanity check pointer in case of parameter problem
291 if (type == ICMPV6_PARAMPROB &&
292 (info > (skb->tail - ((unsigned char *) hdr)))) {
293 printk(KERN_DEBUG "icmpv6_send: bug! pointer > skb\n");
294 return;
298 * Make sure we respect the rules
299 * i.e. RFC 1885 2.4(e)
300 * Rule (e.1) is enforced by not using icmpv6_send
301 * in any code that processes icmp errors.
304 addr_type = ipv6_addr_type(&hdr->daddr);
306 if (ipv6_chk_addr(&hdr->daddr, skb->dev))
307 saddr = &hdr->daddr;
310 * Dest addr check
313 if ((addr_type & IPV6_ADDR_MULTICAST || skb->pkt_type != PACKET_HOST)) {
314 if (type != ICMPV6_PKT_TOOBIG &&
315 !(type == ICMPV6_PARAMPROB &&
316 code == ICMPV6_UNK_OPTION &&
317 (opt_unrec(skb, info))))
318 return;
320 saddr = NULL;
323 addr_type = ipv6_addr_type(&hdr->saddr);
326 * Source addr check
329 if (addr_type & IPV6_ADDR_LINKLOCAL)
330 iif = skb->dev->ifindex;
333 * Must not send if we know that source is Anycast also.
334 * for now we don't know that.
336 if ((addr_type == IPV6_ADDR_ANY) || (addr_type & IPV6_ADDR_MULTICAST)) {
337 printk(KERN_DEBUG "icmpv6_send: addr_any/mcast source\n");
338 return;
342 * Never answer to a ICMP packet.
344 if (is_ineligible(hdr, (u8*)skb->tail - (u8*)hdr)) {
345 if (net_ratelimit())
346 printk(KERN_DEBUG "icmpv6_send: no reply to icmp error/fragment\n");
347 return;
350 fl.proto = IPPROTO_ICMPV6;
351 fl.nl_u.ip6_u.daddr = &hdr->saddr;
352 fl.nl_u.ip6_u.saddr = saddr;
353 fl.oif = iif;
354 fl.fl6_flowlabel = 0;
355 fl.uli_u.icmpt.type = type;
356 fl.uli_u.icmpt.code = code;
358 if (icmpv6_xmit_lock())
359 return;
361 if (!icmpv6_xrlim_allow(sk, type, &fl))
362 goto out;
365 * ok. kick it. checksum will be provided by the
366 * getfrag_t callback.
369 msg.icmph.icmp6_type = type;
370 msg.icmph.icmp6_code = code;
371 msg.icmph.icmp6_cksum = 0;
372 msg.icmph.icmp6_pointer = htonl(info);
374 msg.data = skb->nh.raw;
375 msg.csum = 0;
376 msg.daddr = &hdr->saddr;
378 len = min((skb->tail - ((unsigned char *) hdr)) + sizeof(struct icmp6hdr),
379 IPV6_MIN_MTU - sizeof(struct ipv6hdr));
381 if (len < 0) {
382 printk(KERN_DEBUG "icmp: len problem\n");
383 goto out;
386 msg.len = len;
388 ip6_build_xmit(sk, icmpv6_getfrag, &msg, &fl, len, NULL, -1,
389 MSG_DONTWAIT);
390 if (type >= ICMPV6_DEST_UNREACH && type <= ICMPV6_PARAMPROB)
391 (&(icmpv6_statistics[smp_processor_id()*2].Icmp6OutDestUnreachs))[type-1]++;
392 ICMP6_INC_STATS_BH(Icmp6OutMsgs);
393 out:
394 icmpv6_xmit_unlock();
397 static void icmpv6_echo_reply(struct sk_buff *skb)
399 struct sock *sk = icmpv6_socket->sk;
400 struct ipv6hdr *hdr = skb->nh.ipv6h;
401 struct icmp6hdr *icmph = (struct icmp6hdr *) skb->h.raw;
402 struct in6_addr *saddr;
403 struct icmpv6_msg msg;
404 struct flowi fl;
405 unsigned char *data;
406 int len;
408 data = (char *) (icmph + 1);
410 saddr = &hdr->daddr;
412 if (ipv6_addr_type(saddr) & IPV6_ADDR_MULTICAST)
413 saddr = NULL;
415 len = skb->tail - data;
416 len += sizeof(struct icmp6hdr);
418 msg.icmph.icmp6_type = ICMPV6_ECHO_REPLY;
419 msg.icmph.icmp6_code = 0;
420 msg.icmph.icmp6_cksum = 0;
421 msg.icmph.icmp6_identifier = icmph->icmp6_identifier;
422 msg.icmph.icmp6_sequence = icmph->icmp6_sequence;
424 msg.data = data;
425 msg.csum = 0;
426 msg.len = len;
427 msg.daddr = &hdr->saddr;
429 fl.proto = IPPROTO_ICMPV6;
430 fl.nl_u.ip6_u.daddr = &hdr->saddr;
431 fl.nl_u.ip6_u.saddr = saddr;
432 fl.oif = skb->dev->ifindex;
433 fl.fl6_flowlabel = 0;
434 fl.uli_u.icmpt.type = ICMPV6_ECHO_REPLY;
435 fl.uli_u.icmpt.code = 0;
437 if (icmpv6_xmit_lock_bh())
438 return;
440 ip6_build_xmit(sk, icmpv6_getfrag, &msg, &fl, len, NULL, -1,
441 MSG_DONTWAIT);
442 ICMP6_INC_STATS_BH(Icmp6OutEchoReplies);
443 ICMP6_INC_STATS_BH(Icmp6OutMsgs);
445 icmpv6_xmit_unlock_bh();
448 static void icmpv6_notify(struct sk_buff *skb,
449 int type, int code, u32 info, unsigned char *buff, int len)
451 struct in6_addr *saddr = &skb->nh.ipv6h->saddr;
452 struct in6_addr *daddr = &skb->nh.ipv6h->daddr;
453 struct ipv6hdr *hdr = (struct ipv6hdr *) buff;
454 struct inet6_protocol *ipprot;
455 struct sock *sk;
456 u8 *pb;
457 int hash;
458 u8 nexthdr;
460 nexthdr = hdr->nexthdr;
462 len -= sizeof(struct ipv6hdr);
463 if (len < 0)
464 return;
466 /* now skip over extension headers */
467 pb = ipv6_skip_exthdr((struct ipv6_opt_hdr *) (hdr + 1), &nexthdr, len);
468 if (!pb)
469 return;
471 /* BUGGG_FUTURE: we should try to parse exthdrs in this packet.
472 Without this we will not able f.e. to make source routed
473 pmtu discovery.
474 Corresponding argument (opt) to notifiers is already added.
475 --ANK (980726)
478 hash = nexthdr & (MAX_INET_PROTOS - 1);
480 read_lock(&inet6_protocol_lock);
481 for (ipprot = (struct inet6_protocol *) inet6_protos[hash];
482 ipprot != NULL;
483 ipprot=(struct inet6_protocol *)ipprot->next) {
484 if (ipprot->protocol != nexthdr)
485 continue;
487 if (ipprot->err_handler)
488 ipprot->err_handler(skb, hdr, NULL, type, code, pb, info);
490 read_unlock(&inet6_protocol_lock);
492 read_lock(&raw_v6_lock);
493 if ((sk = raw_v6_htable[hash]) != NULL) {
494 while((sk = __raw_v6_lookup(sk, nexthdr, daddr, saddr))) {
495 rawv6_err(sk, skb, hdr, NULL, type, code, pb, info);
496 sk = sk->next;
499 read_unlock(&raw_v6_lock);
503 * Handle icmp messages
506 int icmpv6_rcv(struct sk_buff *skb, unsigned long len)
508 struct net_device *dev = skb->dev;
509 struct in6_addr *saddr = &skb->nh.ipv6h->saddr;
510 struct in6_addr *daddr = &skb->nh.ipv6h->daddr;
511 struct ipv6hdr *orig_hdr;
512 struct icmp6hdr *hdr = (struct icmp6hdr *) skb->h.raw;
513 int ulen;
514 int type;
516 ICMP6_INC_STATS_BH(Icmp6InMsgs);
518 if (len < sizeof(struct icmp6hdr))
519 goto discard_it;
521 /* Perform checksum. */
522 switch (skb->ip_summed) {
523 case CHECKSUM_NONE:
524 skb->csum = csum_partial((char *)hdr, len, 0);
525 case CHECKSUM_HW:
526 if (csum_ipv6_magic(saddr, daddr, len, IPPROTO_ICMPV6,
527 skb->csum)) {
528 printk(KERN_DEBUG "ICMPv6 checksum failed [%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x > %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x]\n",
529 ntohs(saddr->in6_u.u6_addr16[0]),
530 ntohs(saddr->in6_u.u6_addr16[1]),
531 ntohs(saddr->in6_u.u6_addr16[2]),
532 ntohs(saddr->in6_u.u6_addr16[3]),
533 ntohs(saddr->in6_u.u6_addr16[4]),
534 ntohs(saddr->in6_u.u6_addr16[5]),
535 ntohs(saddr->in6_u.u6_addr16[6]),
536 ntohs(saddr->in6_u.u6_addr16[7]),
537 ntohs(daddr->in6_u.u6_addr16[0]),
538 ntohs(daddr->in6_u.u6_addr16[1]),
539 ntohs(daddr->in6_u.u6_addr16[2]),
540 ntohs(daddr->in6_u.u6_addr16[3]),
541 ntohs(daddr->in6_u.u6_addr16[4]),
542 ntohs(daddr->in6_u.u6_addr16[5]),
543 ntohs(daddr->in6_u.u6_addr16[6]),
544 ntohs(daddr->in6_u.u6_addr16[7]));
545 goto discard_it;
547 default:
548 /* CHECKSUM_UNNECESSARY */
552 * length of original packet carried in skb
554 ulen = skb->tail - (unsigned char *) (hdr + 1);
556 type = hdr->icmp6_type;
558 if (type >= ICMPV6_DEST_UNREACH && type <= ICMPV6_PARAMPROB)
559 (&icmpv6_statistics[smp_processor_id()*2].Icmp6InDestUnreachs)[type-ICMPV6_DEST_UNREACH]++;
560 else if (type >= ICMPV6_ECHO_REQUEST && type <= NDISC_REDIRECT)
561 (&icmpv6_statistics[smp_processor_id()*2].Icmp6InEchos)[type-ICMPV6_ECHO_REQUEST]++;
563 switch (type) {
565 case ICMPV6_ECHO_REQUEST:
566 icmpv6_echo_reply(skb);
567 break;
569 case ICMPV6_ECHO_REPLY:
570 /* we coulnd't care less */
571 break;
573 case ICMPV6_PKT_TOOBIG:
574 /* BUGGG_FUTURE: if packet contains rthdr, we cannot update
575 standard destination cache. Seems, only "advanced"
576 destination cache will allow to solve this problem
577 --ANK (980726)
579 orig_hdr = (struct ipv6hdr *) (hdr + 1);
580 if (ulen >= sizeof(struct ipv6hdr))
581 rt6_pmtu_discovery(&orig_hdr->daddr, &orig_hdr->saddr, dev,
582 ntohl(hdr->icmp6_mtu));
585 * Drop through to notify
588 case ICMPV6_DEST_UNREACH:
589 case ICMPV6_TIME_EXCEED:
590 case ICMPV6_PARAMPROB:
591 icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu,
592 (char *) (hdr + 1), ulen);
593 break;
595 case NDISC_ROUTER_SOLICITATION:
596 case NDISC_ROUTER_ADVERTISEMENT:
597 case NDISC_NEIGHBOUR_SOLICITATION:
598 case NDISC_NEIGHBOUR_ADVERTISEMENT:
599 case NDISC_REDIRECT:
600 ndisc_rcv(skb, len);
601 break;
603 case ICMPV6_MGM_QUERY:
604 igmp6_event_query(skb, hdr, len);
605 break;
607 case ICMPV6_MGM_REPORT:
608 igmp6_event_report(skb, hdr, len);
609 break;
611 case ICMPV6_MGM_REDUCTION:
612 break;
614 default:
615 if (net_ratelimit())
616 printk(KERN_DEBUG "icmpv6: msg of unkown type\n");
618 /* informational */
619 if (type & 0x80)
620 break;
623 * error of unkown type.
624 * must pass to upper level
627 icmpv6_notify(skb, type, hdr->icmp6_code, hdr->icmp6_mtu,
628 (char *) (hdr + 1), ulen);
630 kfree_skb(skb);
631 return 0;
633 discard_it:
634 ICMP6_INC_STATS_BH(Icmp6InErrors);
635 kfree_skb(skb);
636 return 0;
639 int __init icmpv6_init(struct net_proto_family *ops)
641 struct sock *sk;
642 int err;
644 icmpv6_socket = sock_alloc();
645 if (icmpv6_socket == NULL) {
646 printk(KERN_ERR
647 "Failed to create the ICMP6 control socket.\n");
648 return -1;
650 icmpv6_socket->inode->i_uid = 0;
651 icmpv6_socket->inode->i_gid = 0;
652 icmpv6_socket->type = SOCK_RAW;
654 if ((err = ops->create(icmpv6_socket, IPPROTO_ICMPV6)) < 0) {
655 printk(KERN_ERR
656 "Failed to initialize the ICMP6 control socket (err %d).\n",
657 err);
658 sock_release(icmpv6_socket);
659 icmpv6_socket = NULL; /* for safety */
660 return err;
663 sk = icmpv6_socket->sk;
664 sk->allocation = GFP_ATOMIC;
665 sk->prot->unhash(sk);
667 inet6_add_protocol(&icmpv6_protocol);
669 return 0;
672 void icmpv6_cleanup(void)
674 sock_release(icmpv6_socket);
675 icmpv6_socket = NULL; /* For safety. */
676 inet6_del_protocol(&icmpv6_protocol);
679 static struct icmp6_err {
680 int err;
681 int fatal;
682 } tab_unreach[] = {
683 { ENETUNREACH, 0}, /* NOROUTE */
684 { EACCES, 1}, /* ADM_PROHIBITED */
685 { EHOSTUNREACH, 0}, /* Was NOT_NEIGHBOUR, now reserved */
686 { EHOSTUNREACH, 0}, /* ADDR_UNREACH */
687 { ECONNREFUSED, 1}, /* PORT_UNREACH */
690 int icmpv6_err_convert(int type, int code, int *err)
692 int fatal = 0;
694 *err = EPROTO;
696 switch (type) {
697 case ICMPV6_DEST_UNREACH:
698 fatal = 1;
699 if (code <= ICMPV6_PORT_UNREACH) {
700 *err = tab_unreach[code].err;
701 fatal = tab_unreach[code].fatal;
703 break;
705 case ICMPV6_PKT_TOOBIG:
706 *err = EMSGSIZE;
707 break;
709 case ICMPV6_PARAMPROB:
710 *err = EPROTO;
711 fatal = 1;
712 break;
714 case ICMPV6_TIME_EXCEED:
715 *err = EHOSTUNREACH;
716 break;
719 return fatal;