Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C)2003,2004 USAGI/WIDE Project |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Authors Mitsuru KANDA <mk@linux-ipv6.org> |
| 19 | * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org> |
| 20 | */ |
| 21 | |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 22 | #include <linux/icmpv6.h> |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 23 | #include <linux/init.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/mutex.h> |
| 26 | #include <linux/netdevice.h> |
| 27 | #include <linux/skbuff.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 29 | #include <net/ipv6.h> |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 30 | #include <net/protocol.h> |
| 31 | #include <net/xfrm.h> |
| 32 | |
| 33 | static struct xfrm6_tunnel *tunnel6_handlers; |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 34 | static struct xfrm6_tunnel *tunnel46_handlers; |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 35 | static DEFINE_MUTEX(tunnel6_mutex); |
| 36 | |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 37 | int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 38 | { |
| 39 | struct xfrm6_tunnel **pprev; |
| 40 | int ret = -EEXIST; |
| 41 | int priority = handler->priority; |
| 42 | |
| 43 | mutex_lock(&tunnel6_mutex); |
| 44 | |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 45 | for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers; |
| 46 | *pprev; pprev = &(*pprev)->next) { |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 47 | if ((*pprev)->priority > priority) |
| 48 | break; |
| 49 | if ((*pprev)->priority == priority) |
| 50 | goto err; |
| 51 | } |
| 52 | |
| 53 | handler->next = *pprev; |
| 54 | *pprev = handler; |
| 55 | |
| 56 | ret = 0; |
| 57 | |
| 58 | err: |
| 59 | mutex_unlock(&tunnel6_mutex); |
| 60 | |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | EXPORT_SYMBOL(xfrm6_tunnel_register); |
| 65 | |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 66 | int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 67 | { |
| 68 | struct xfrm6_tunnel **pprev; |
| 69 | int ret = -ENOENT; |
| 70 | |
| 71 | mutex_lock(&tunnel6_mutex); |
| 72 | |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 73 | for (pprev = (family == AF_INET6) ? &tunnel6_handlers : &tunnel46_handlers; |
| 74 | *pprev; pprev = &(*pprev)->next) { |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 75 | if (*pprev == handler) { |
| 76 | *pprev = handler->next; |
| 77 | ret = 0; |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | mutex_unlock(&tunnel6_mutex); |
| 83 | |
| 84 | synchronize_net(); |
| 85 | |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | EXPORT_SYMBOL(xfrm6_tunnel_deregister); |
| 90 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 91 | static int tunnel6_rcv(struct sk_buff *skb) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 92 | { |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 93 | struct xfrm6_tunnel *handler; |
| 94 | |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 95 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) |
| 96 | goto drop; |
| 97 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 98 | for (handler = tunnel6_handlers; handler; handler = handler->next) |
| 99 | if (!handler->handler(skb)) |
| 100 | return 0; |
| 101 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 102 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 103 | |
| 104 | drop: |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 105 | kfree_skb(skb); |
| 106 | return 0; |
| 107 | } |
| 108 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 109 | static int tunnel46_rcv(struct sk_buff *skb) |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 110 | { |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 111 | struct xfrm6_tunnel *handler; |
| 112 | |
Colin | 8283637 | 2008-05-27 00:04:43 +0800 | [diff] [blame] | 113 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 114 | goto drop; |
| 115 | |
| 116 | for (handler = tunnel46_handlers; handler; handler = handler->next) |
| 117 | if (!handler->handler(skb)) |
| 118 | return 0; |
| 119 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 120 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 121 | |
| 122 | drop: |
| 123 | kfree_skb(skb); |
| 124 | return 0; |
| 125 | } |
| 126 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 127 | static void tunnel6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 128 | u8 type, u8 code, int offset, __be32 info) |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 129 | { |
| 130 | struct xfrm6_tunnel *handler; |
| 131 | |
| 132 | for (handler = tunnel6_handlers; handler; handler = handler->next) |
| 133 | if (!handler->err_handler(skb, opt, type, code, offset, info)) |
| 134 | break; |
| 135 | } |
| 136 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 137 | static const struct inet6_protocol tunnel6_protocol = { |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 138 | .handler = tunnel6_rcv, |
| 139 | .err_handler = tunnel6_err, |
| 140 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, |
| 141 | }; |
| 142 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 143 | static const struct inet6_protocol tunnel46_protocol = { |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 144 | .handler = tunnel46_rcv, |
| 145 | .err_handler = tunnel6_err, |
| 146 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, |
| 147 | }; |
| 148 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 149 | static int __init tunnel6_init(void) |
| 150 | { |
| 151 | if (inet6_add_protocol(&tunnel6_protocol, IPPROTO_IPV6)) { |
| 152 | printk(KERN_ERR "tunnel6 init(): can't add protocol\n"); |
| 153 | return -EAGAIN; |
| 154 | } |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 155 | if (inet6_add_protocol(&tunnel46_protocol, IPPROTO_IPIP)) { |
| 156 | printk(KERN_ERR "tunnel6 init(): can't add protocol\n"); |
| 157 | inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6); |
| 158 | return -EAGAIN; |
| 159 | } |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static void __exit tunnel6_fini(void) |
| 164 | { |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 165 | if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP)) |
| 166 | printk(KERN_ERR "tunnel6 close: can't remove protocol\n"); |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 167 | if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6)) |
| 168 | printk(KERN_ERR "tunnel6 close: can't remove protocol\n"); |
| 169 | } |
| 170 | |
| 171 | module_init(tunnel6_init); |
| 172 | module_exit(tunnel6_fini); |
| 173 | MODULE_LICENSE("GPL"); |