Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
c4d3efaf | 2 | * IPv6 tunneling device |
1da177e4 LT |
3 | * Linux INET6 implementation |
4 | * | |
5 | * Authors: | |
1ab1457c | 6 | * Ville Nuorvala <vnuorval@tcs.hut.fi> |
c4d3efaf | 7 | * Yasuyuki Kozakai <kozakai@linux-ipv6.org> |
1da177e4 | 8 | * |
1da177e4 | 9 | * Based on: |
c4d3efaf | 10 | * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c |
1da177e4 LT |
11 | * |
12 | * RFC 2473 | |
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 | ||
f3213831 JP |
21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
22 | ||
1da177e4 | 23 | #include <linux/module.h> |
4fc268d2 | 24 | #include <linux/capability.h> |
1da177e4 LT |
25 | #include <linux/errno.h> |
26 | #include <linux/types.h> | |
27 | #include <linux/sockios.h> | |
c4d3efaf | 28 | #include <linux/icmp.h> |
1da177e4 LT |
29 | #include <linux/if.h> |
30 | #include <linux/in.h> | |
31 | #include <linux/ip.h> | |
32 | #include <linux/if_tunnel.h> | |
33 | #include <linux/net.h> | |
34 | #include <linux/in6.h> | |
35 | #include <linux/netdevice.h> | |
36 | #include <linux/if_arp.h> | |
37 | #include <linux/icmpv6.h> | |
38 | #include <linux/init.h> | |
39 | #include <linux/route.h> | |
40 | #include <linux/rtnetlink.h> | |
41 | #include <linux/netfilter_ipv6.h> | |
5a0e3ad6 | 42 | #include <linux/slab.h> |
ddbe5032 | 43 | #include <linux/hash.h> |
1da177e4 LT |
44 | |
45 | #include <asm/uaccess.h> | |
60063497 | 46 | #include <linux/atomic.h> |
1da177e4 | 47 | |
c4d3efaf | 48 | #include <net/icmp.h> |
1da177e4 | 49 | #include <net/ip.h> |
c5441932 | 50 | #include <net/ip_tunnels.h> |
1da177e4 | 51 | #include <net/ipv6.h> |
1da177e4 LT |
52 | #include <net/ip6_route.h> |
53 | #include <net/addrconf.h> | |
54 | #include <net/ip6_tunnel.h> | |
55 | #include <net/xfrm.h> | |
56 | #include <net/dsfield.h> | |
57 | #include <net/inet_ecn.h> | |
13eeb8e9 PE |
58 | #include <net/net_namespace.h> |
59 | #include <net/netns/generic.h> | |
1da177e4 LT |
60 | |
61 | MODULE_AUTHOR("Ville Nuorvala"); | |
c4d3efaf | 62 | MODULE_DESCRIPTION("IPv6 tunneling device"); |
1da177e4 | 63 | MODULE_LICENSE("GPL"); |
57b1ec60 | 64 | MODULE_ALIAS_RTNL_LINK("ip6tnl"); |
6dfbd87a | 65 | MODULE_ALIAS_NETDEV("ip6tnl0"); |
1da177e4 | 66 | |
1da177e4 | 67 | #ifdef IP6_TNL_DEBUG |
91df42be | 68 | #define IP6_TNL_TRACE(x...) pr_debug("%s:" x "\n", __func__) |
1da177e4 LT |
69 | #else |
70 | #define IP6_TNL_TRACE(x...) do {;} while(0) | |
71 | #endif | |
72 | ||
73 | #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK) | |
c4d3efaf | 74 | #define IPV6_TCLASS_SHIFT 20 |
1da177e4 | 75 | |
ddbe5032 ED |
76 | #define HASH_SIZE_SHIFT 5 |
77 | #define HASH_SIZE (1 << HASH_SIZE_SHIFT) | |
1da177e4 | 78 | |
f4e0b4c5 ND |
79 | static bool log_ecn_error = true; |
80 | module_param(log_ecn_error, bool, 0644); | |
81 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |
82 | ||
ddbe5032 ED |
83 | static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2) |
84 | { | |
85 | u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2); | |
86 | ||
87 | return hash_32(hash, HASH_SIZE_SHIFT); | |
88 | } | |
1da177e4 | 89 | |
8560f226 | 90 | static int ip6_tnl_dev_init(struct net_device *dev); |
3144581c | 91 | static void ip6_tnl_dev_setup(struct net_device *dev); |
c075b130 | 92 | static struct rtnl_link_ops ip6_link_ops __read_mostly; |
1da177e4 | 93 | |
f99189b1 | 94 | static int ip6_tnl_net_id __read_mostly; |
13eeb8e9 | 95 | struct ip6_tnl_net { |
15820e12 PE |
96 | /* the IPv6 tunnel fallback device */ |
97 | struct net_device *fb_tnl_dev; | |
3e6c9fb5 | 98 | /* lists for storing tunnels in use */ |
94767632 ED |
99 | struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE]; |
100 | struct ip6_tnl __rcu *tnls_wc[1]; | |
101 | struct ip6_tnl __rcu **tnls[2]; | |
13eeb8e9 PE |
102 | }; |
103 | ||
8560f226 ED |
104 | static struct net_device_stats *ip6_get_stats(struct net_device *dev) |
105 | { | |
abcfcd04 | 106 | struct pcpu_tstats tmp, sum = { 0 }; |
8560f226 ED |
107 | int i; |
108 | ||
109 | for_each_possible_cpu(i) { | |
abcfcd04 | 110 | unsigned int start; |
8560f226 ED |
111 | const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i); |
112 | ||
abcfcd04 LR |
113 | do { |
114 | start = u64_stats_fetch_begin_bh(&tstats->syncp); | |
115 | tmp.rx_packets = tstats->rx_packets; | |
116 | tmp.rx_bytes = tstats->rx_bytes; | |
117 | tmp.tx_packets = tstats->tx_packets; | |
118 | tmp.tx_bytes = tstats->tx_bytes; | |
119 | } while (u64_stats_fetch_retry_bh(&tstats->syncp, start)); | |
120 | ||
121 | sum.rx_packets += tmp.rx_packets; | |
122 | sum.rx_bytes += tmp.rx_bytes; | |
123 | sum.tx_packets += tmp.tx_packets; | |
124 | sum.tx_bytes += tmp.tx_bytes; | |
8560f226 ED |
125 | } |
126 | dev->stats.rx_packets = sum.rx_packets; | |
127 | dev->stats.rx_bytes = sum.rx_bytes; | |
128 | dev->stats.tx_packets = sum.tx_packets; | |
129 | dev->stats.tx_bytes = sum.tx_bytes; | |
130 | return &dev->stats; | |
131 | } | |
132 | ||
2922bc8a | 133 | /* |
94767632 | 134 | * Locking : hash tables are protected by RCU and RTNL |
2922bc8a | 135 | */ |
1da177e4 | 136 | |
c12b395a | 137 | struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t) |
1da177e4 LT |
138 | { |
139 | struct dst_entry *dst = t->dst_cache; | |
140 | ||
1ab1457c | 141 | if (dst && dst->obsolete && |
1da177e4 LT |
142 | dst->ops->check(dst, t->dst_cookie) == NULL) { |
143 | t->dst_cache = NULL; | |
144 | dst_release(dst); | |
145 | return NULL; | |
146 | } | |
147 | ||
148 | return dst; | |
149 | } | |
c12b395a | 150 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_check); |
1da177e4 | 151 | |
c12b395a | 152 | void ip6_tnl_dst_reset(struct ip6_tnl *t) |
1da177e4 LT |
153 | { |
154 | dst_release(t->dst_cache); | |
155 | t->dst_cache = NULL; | |
156 | } | |
c12b395a | 157 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset); |
1da177e4 | 158 | |
c12b395a | 159 | void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst) |
1da177e4 LT |
160 | { |
161 | struct rt6_info *rt = (struct rt6_info *) dst; | |
162 | t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0; | |
163 | dst_release(t->dst_cache); | |
164 | t->dst_cache = dst; | |
165 | } | |
c12b395a | 166 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_store); |
1da177e4 LT |
167 | |
168 | /** | |
3144581c | 169 | * ip6_tnl_lookup - fetch tunnel matching the end-point addresses |
1ab1457c YH |
170 | * @remote: the address of the tunnel exit-point |
171 | * @local: the address of the tunnel entry-point | |
1da177e4 | 172 | * |
1ab1457c | 173 | * Return: |
1da177e4 | 174 | * tunnel matching given end-points if found, |
1ab1457c | 175 | * else fallback tunnel if its device is up, |
1da177e4 LT |
176 | * else %NULL |
177 | **/ | |
178 | ||
2922bc8a ED |
179 | #define for_each_ip6_tunnel_rcu(start) \ |
180 | for (t = rcu_dereference(start); t; t = rcu_dereference(t->next)) | |
181 | ||
1da177e4 | 182 | static struct ip6_tnl * |
b71d1d42 | 183 | ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local) |
1da177e4 | 184 | { |
ddbe5032 | 185 | unsigned int hash = HASH(remote, local); |
1da177e4 | 186 | struct ip6_tnl *t; |
3e6c9fb5 | 187 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
1da177e4 | 188 | |
ddbe5032 | 189 | for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { |
1da177e4 LT |
190 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
191 | ipv6_addr_equal(remote, &t->parms.raddr) && | |
192 | (t->dev->flags & IFF_UP)) | |
193 | return t; | |
194 | } | |
2922bc8a ED |
195 | t = rcu_dereference(ip6n->tnls_wc[0]); |
196 | if (t && (t->dev->flags & IFF_UP)) | |
1da177e4 LT |
197 | return t; |
198 | ||
199 | return NULL; | |
200 | } | |
201 | ||
202 | /** | |
3144581c | 203 | * ip6_tnl_bucket - get head of list matching given tunnel parameters |
1ab1457c | 204 | * @p: parameters containing tunnel end-points |
1da177e4 LT |
205 | * |
206 | * Description: | |
3144581c | 207 | * ip6_tnl_bucket() returns the head of the list matching the |
1da177e4 LT |
208 | * &struct in6_addr entries laddr and raddr in @p. |
209 | * | |
1ab1457c | 210 | * Return: head of IPv6 tunnel list |
1da177e4 LT |
211 | **/ |
212 | ||
94767632 | 213 | static struct ip6_tnl __rcu ** |
c12b395a | 214 | ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p) |
1da177e4 | 215 | { |
b71d1d42 ED |
216 | const struct in6_addr *remote = &p->raddr; |
217 | const struct in6_addr *local = &p->laddr; | |
95c96174 | 218 | unsigned int h = 0; |
1da177e4 LT |
219 | int prio = 0; |
220 | ||
221 | if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) { | |
222 | prio = 1; | |
ddbe5032 | 223 | h = HASH(remote, local); |
1da177e4 | 224 | } |
3e6c9fb5 | 225 | return &ip6n->tnls[prio][h]; |
1da177e4 LT |
226 | } |
227 | ||
228 | /** | |
3144581c | 229 | * ip6_tnl_link - add tunnel to hash table |
1da177e4 LT |
230 | * @t: tunnel to be added |
231 | **/ | |
232 | ||
233 | static void | |
2dd02c89 | 234 | ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t) |
1da177e4 | 235 | { |
94767632 | 236 | struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms); |
1da177e4 | 237 | |
cf778b00 ED |
238 | rcu_assign_pointer(t->next , rtnl_dereference(*tp)); |
239 | rcu_assign_pointer(*tp, t); | |
1da177e4 LT |
240 | } |
241 | ||
242 | /** | |
3144581c | 243 | * ip6_tnl_unlink - remove tunnel from hash table |
1da177e4 LT |
244 | * @t: tunnel to be removed |
245 | **/ | |
246 | ||
247 | static void | |
2dd02c89 | 248 | ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t) |
1da177e4 | 249 | { |
94767632 ED |
250 | struct ip6_tnl __rcu **tp; |
251 | struct ip6_tnl *iter; | |
252 | ||
253 | for (tp = ip6_tnl_bucket(ip6n, &t->parms); | |
254 | (iter = rtnl_dereference(*tp)) != NULL; | |
255 | tp = &iter->next) { | |
256 | if (t == iter) { | |
cf778b00 | 257 | rcu_assign_pointer(*tp, t->next); |
1da177e4 LT |
258 | break; |
259 | } | |
260 | } | |
261 | } | |
262 | ||
8560f226 ED |
263 | static void ip6_dev_free(struct net_device *dev) |
264 | { | |
265 | free_percpu(dev->tstats); | |
266 | free_netdev(dev); | |
267 | } | |
268 | ||
0b112457 ND |
269 | static int ip6_tnl_create2(struct net_device *dev) |
270 | { | |
271 | struct ip6_tnl *t = netdev_priv(dev); | |
272 | struct net *net = dev_net(dev); | |
273 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); | |
274 | int err; | |
275 | ||
276 | t = netdev_priv(dev); | |
0b112457 | 277 | |
d840a0cb | 278 | dev->rtnl_link_ops = &ip6_link_ops; |
0b112457 ND |
279 | err = register_netdevice(dev); |
280 | if (err < 0) | |
281 | goto out; | |
282 | ||
283 | strcpy(t->parms.name, dev->name); | |
0b112457 ND |
284 | |
285 | dev_hold(dev); | |
286 | ip6_tnl_link(ip6n, t); | |
287 | return 0; | |
288 | ||
289 | out: | |
290 | return err; | |
291 | } | |
292 | ||
1da177e4 | 293 | /** |
2c53040f | 294 | * ip6_tnl_create - create a new tunnel |
1da177e4 LT |
295 | * @p: tunnel parameters |
296 | * @pt: pointer to new tunnel | |
297 | * | |
298 | * Description: | |
299 | * Create tunnel matching given parameters. | |
1ab1457c YH |
300 | * |
301 | * Return: | |
567131a7 | 302 | * created tunnel or NULL |
1da177e4 LT |
303 | **/ |
304 | ||
c12b395a | 305 | static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p) |
1da177e4 LT |
306 | { |
307 | struct net_device *dev; | |
308 | struct ip6_tnl *t; | |
309 | char name[IFNAMSIZ]; | |
310 | int err; | |
311 | ||
34cc7ba6 | 312 | if (p->name[0]) |
1da177e4 | 313 | strlcpy(name, p->name, IFNAMSIZ); |
34cc7ba6 PE |
314 | else |
315 | sprintf(name, "ip6tnl%%d"); | |
316 | ||
3144581c | 317 | dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup); |
1da177e4 | 318 | if (dev == NULL) |
567131a7 | 319 | goto failed; |
1da177e4 | 320 | |
554eb277 PE |
321 | dev_net_set(dev, net); |
322 | ||
2941a486 | 323 | t = netdev_priv(dev); |
1da177e4 | 324 | t->parms = *p; |
0b112457 | 325 | err = ip6_tnl_create2(dev); |
8560f226 ED |
326 | if (err < 0) |
327 | goto failed_free; | |
1da177e4 | 328 | |
567131a7 | 329 | return t; |
b37d428b PE |
330 | |
331 | failed_free: | |
8560f226 | 332 | ip6_dev_free(dev); |
567131a7 VN |
333 | failed: |
334 | return NULL; | |
1da177e4 LT |
335 | } |
336 | ||
337 | /** | |
3144581c | 338 | * ip6_tnl_locate - find or create tunnel matching given parameters |
1ab1457c | 339 | * @p: tunnel parameters |
1da177e4 LT |
340 | * @create: != 0 if allowed to create new tunnel if no match found |
341 | * | |
342 | * Description: | |
3144581c | 343 | * ip6_tnl_locate() first tries to locate an existing tunnel |
1da177e4 LT |
344 | * based on @parms. If this is unsuccessful, but @create is set a new |
345 | * tunnel device is created and registered for use. | |
346 | * | |
347 | * Return: | |
567131a7 | 348 | * matching tunnel or NULL |
1da177e4 LT |
349 | **/ |
350 | ||
2dd02c89 | 351 | static struct ip6_tnl *ip6_tnl_locate(struct net *net, |
c12b395a | 352 | struct __ip6_tnl_parm *p, int create) |
1da177e4 | 353 | { |
b71d1d42 ED |
354 | const struct in6_addr *remote = &p->raddr; |
355 | const struct in6_addr *local = &p->laddr; | |
94767632 | 356 | struct ip6_tnl __rcu **tp; |
1da177e4 | 357 | struct ip6_tnl *t; |
2dd02c89 | 358 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
1da177e4 | 359 | |
94767632 ED |
360 | for (tp = ip6_tnl_bucket(ip6n, p); |
361 | (t = rtnl_dereference(*tp)) != NULL; | |
362 | tp = &t->next) { | |
1da177e4 | 363 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
567131a7 VN |
364 | ipv6_addr_equal(remote, &t->parms.raddr)) |
365 | return t; | |
1da177e4 LT |
366 | } |
367 | if (!create) | |
567131a7 | 368 | return NULL; |
2dd02c89 | 369 | return ip6_tnl_create(net, p); |
1da177e4 LT |
370 | } |
371 | ||
372 | /** | |
3144581c | 373 | * ip6_tnl_dev_uninit - tunnel device uninitializer |
1da177e4 | 374 | * @dev: the device to be destroyed |
1ab1457c | 375 | * |
1da177e4 | 376 | * Description: |
3144581c | 377 | * ip6_tnl_dev_uninit() removes tunnel from its list |
1da177e4 LT |
378 | **/ |
379 | ||
380 | static void | |
3144581c | 381 | ip6_tnl_dev_uninit(struct net_device *dev) |
1da177e4 | 382 | { |
2941a486 | 383 | struct ip6_tnl *t = netdev_priv(dev); |
2dd02c89 PE |
384 | struct net *net = dev_net(dev); |
385 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); | |
1da177e4 | 386 | |
94767632 | 387 | if (dev == ip6n->fb_tnl_dev) |
a9b3cd7f | 388 | RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL); |
94767632 | 389 | else |
2dd02c89 | 390 | ip6_tnl_unlink(ip6n, t); |
1da177e4 LT |
391 | ip6_tnl_dst_reset(t); |
392 | dev_put(dev); | |
393 | } | |
394 | ||
395 | /** | |
396 | * parse_tvl_tnl_enc_lim - handle encapsulation limit option | |
397 | * @skb: received socket buffer | |
398 | * | |
1ab1457c YH |
399 | * Return: |
400 | * 0 if none was found, | |
1da177e4 LT |
401 | * else index to encapsulation limit |
402 | **/ | |
403 | ||
c12b395a | 404 | __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw) |
1da177e4 | 405 | { |
72bbf335 ED |
406 | const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw; |
407 | unsigned int nhoff = raw - skb->data; | |
408 | unsigned int off = nhoff + sizeof(*ipv6h); | |
409 | u8 next, nexthdr = ipv6h->nexthdr; | |
1da177e4 LT |
410 | |
411 | while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) { | |
1da177e4 | 412 | struct ipv6_opt_hdr *hdr; |
72bbf335 ED |
413 | u16 optlen; |
414 | ||
415 | if (!pskb_may_pull(skb, off + sizeof(*hdr))) | |
1da177e4 LT |
416 | break; |
417 | ||
72bbf335 | 418 | hdr = (struct ipv6_opt_hdr *)(skb->data + off); |
1da177e4 LT |
419 | if (nexthdr == NEXTHDR_FRAGMENT) { |
420 | struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr; | |
421 | if (frag_hdr->frag_off) | |
422 | break; | |
423 | optlen = 8; | |
424 | } else if (nexthdr == NEXTHDR_AUTH) { | |
425 | optlen = (hdr->hdrlen + 2) << 2; | |
426 | } else { | |
427 | optlen = ipv6_optlen(hdr); | |
428 | } | |
72bbf335 ED |
429 | /* cache hdr->nexthdr, since pskb_may_pull() might |
430 | * invalidate hdr | |
431 | */ | |
432 | next = hdr->nexthdr; | |
1da177e4 | 433 | if (nexthdr == NEXTHDR_DEST) { |
72bbf335 ED |
434 | u16 i = 2; |
435 | ||
436 | /* Remember : hdr is no longer valid at this point. */ | |
437 | if (!pskb_may_pull(skb, off + optlen)) | |
438 | break; | |
439 | ||
1da177e4 LT |
440 | while (1) { |
441 | struct ipv6_tlv_tnl_enc_lim *tel; | |
442 | ||
443 | /* No more room for encapsulation limit */ | |
72bbf335 | 444 | if (i + sizeof(*tel) > optlen) |
1da177e4 LT |
445 | break; |
446 | ||
e246c098 | 447 | tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i); |
1da177e4 LT |
448 | /* return index of option if found and valid */ |
449 | if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT && | |
450 | tel->length == 1) | |
72bbf335 | 451 | return i + off - nhoff; |
1da177e4 LT |
452 | /* else jump to next option */ |
453 | if (tel->type) | |
454 | i += tel->length + 2; | |
455 | else | |
456 | i++; | |
457 | } | |
458 | } | |
72bbf335 | 459 | nexthdr = next; |
1da177e4 LT |
460 | off += optlen; |
461 | } | |
462 | return 0; | |
463 | } | |
c12b395a | 464 | EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim); |
1da177e4 LT |
465 | |
466 | /** | |
e490d1d8 | 467 | * ip6_tnl_err - tunnel error handler |
1da177e4 LT |
468 | * |
469 | * Description: | |
e490d1d8 | 470 | * ip6_tnl_err() should handle errors in the tunnel according |
1da177e4 LT |
471 | * to the specifications in RFC 2473. |
472 | **/ | |
473 | ||
d2acc347 | 474 | static int |
502b0935 | 475 | ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt, |
d5fdd6ba | 476 | u8 *type, u8 *code, int *msg, __u32 *info, int offset) |
1da177e4 | 477 | { |
b71d1d42 | 478 | const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data; |
1da177e4 LT |
479 | struct ip6_tnl *t; |
480 | int rel_msg = 0; | |
d5fdd6ba BH |
481 | u8 rel_type = ICMPV6_DEST_UNREACH; |
482 | u8 rel_code = ICMPV6_ADDR_UNREACH; | |
1da177e4 LT |
483 | __u32 rel_info = 0; |
484 | __u16 len; | |
d2acc347 | 485 | int err = -ENOENT; |
1da177e4 | 486 | |
1ab1457c YH |
487 | /* If the packet doesn't contain the original IPv6 header we are |
488 | in trouble since we might need the source address for further | |
1da177e4 LT |
489 | processing of the error. */ |
490 | ||
2922bc8a | 491 | rcu_read_lock(); |
8704ca7e | 492 | if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, |
2dd02c89 | 493 | &ipv6h->saddr)) == NULL) |
1da177e4 LT |
494 | goto out; |
495 | ||
502b0935 YK |
496 | if (t->parms.proto != ipproto && t->parms.proto != 0) |
497 | goto out; | |
498 | ||
d2acc347 HX |
499 | err = 0; |
500 | ||
e490d1d8 | 501 | switch (*type) { |
1da177e4 LT |
502 | __u32 teli; |
503 | struct ipv6_tlv_tnl_enc_lim *tel; | |
504 | __u32 mtu; | |
505 | case ICMPV6_DEST_UNREACH: | |
e87cc472 JP |
506 | net_warn_ratelimited("%s: Path to destination invalid or inactive!\n", |
507 | t->parms.name); | |
1da177e4 LT |
508 | rel_msg = 1; |
509 | break; | |
510 | case ICMPV6_TIME_EXCEED: | |
e490d1d8 | 511 | if ((*code) == ICMPV6_EXC_HOPLIMIT) { |
e87cc472 JP |
512 | net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n", |
513 | t->parms.name); | |
1da177e4 LT |
514 | rel_msg = 1; |
515 | } | |
516 | break; | |
517 | case ICMPV6_PARAMPROB: | |
107a5fe6 | 518 | teli = 0; |
e490d1d8 | 519 | if ((*code) == ICMPV6_HDR_FIELD) |
c12b395a | 520 | teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data); |
1da177e4 | 521 | |
704eae1f | 522 | if (teli && teli == *info - 2) { |
1da177e4 LT |
523 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; |
524 | if (tel->encap_limit == 0) { | |
e87cc472 JP |
525 | net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n", |
526 | t->parms.name); | |
1da177e4 LT |
527 | rel_msg = 1; |
528 | } | |
e87cc472 JP |
529 | } else { |
530 | net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n", | |
531 | t->parms.name); | |
1da177e4 LT |
532 | } |
533 | break; | |
534 | case ICMPV6_PKT_TOOBIG: | |
704eae1f | 535 | mtu = *info - offset; |
1da177e4 LT |
536 | if (mtu < IPV6_MIN_MTU) |
537 | mtu = IPV6_MIN_MTU; | |
538 | t->dev->mtu = mtu; | |
539 | ||
cc6cdac0 | 540 | if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) { |
1da177e4 LT |
541 | rel_type = ICMPV6_PKT_TOOBIG; |
542 | rel_code = 0; | |
543 | rel_info = mtu; | |
544 | rel_msg = 1; | |
545 | } | |
546 | break; | |
547 | } | |
e490d1d8 YK |
548 | |
549 | *type = rel_type; | |
550 | *code = rel_code; | |
551 | *info = rel_info; | |
552 | *msg = rel_msg; | |
553 | ||
554 | out: | |
2922bc8a | 555 | rcu_read_unlock(); |
e490d1d8 YK |
556 | return err; |
557 | } | |
558 | ||
c4d3efaf YK |
559 | static int |
560 | ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
d5fdd6ba | 561 | u8 type, u8 code, int offset, __be32 info) |
c4d3efaf YK |
562 | { |
563 | int rel_msg = 0; | |
d5fdd6ba BH |
564 | u8 rel_type = type; |
565 | u8 rel_code = code; | |
704eae1f | 566 | __u32 rel_info = ntohl(info); |
c4d3efaf YK |
567 | int err; |
568 | struct sk_buff *skb2; | |
b71d1d42 | 569 | const struct iphdr *eiph; |
c4d3efaf | 570 | struct rtable *rt; |
31e4543d | 571 | struct flowi4 fl4; |
c4d3efaf | 572 | |
502b0935 YK |
573 | err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code, |
574 | &rel_msg, &rel_info, offset); | |
c4d3efaf YK |
575 | if (err < 0) |
576 | return err; | |
577 | ||
578 | if (rel_msg == 0) | |
579 | return 0; | |
580 | ||
581 | switch (rel_type) { | |
582 | case ICMPV6_DEST_UNREACH: | |
583 | if (rel_code != ICMPV6_ADDR_UNREACH) | |
584 | return 0; | |
585 | rel_type = ICMP_DEST_UNREACH; | |
586 | rel_code = ICMP_HOST_UNREACH; | |
587 | break; | |
588 | case ICMPV6_PKT_TOOBIG: | |
589 | if (rel_code != 0) | |
590 | return 0; | |
591 | rel_type = ICMP_DEST_UNREACH; | |
592 | rel_code = ICMP_FRAG_NEEDED; | |
593 | break; | |
ec18d9a2 DM |
594 | case NDISC_REDIRECT: |
595 | rel_type = ICMP_REDIRECT; | |
596 | rel_code = ICMP_REDIR_HOST; | |
c4d3efaf YK |
597 | default: |
598 | return 0; | |
599 | } | |
600 | ||
601 | if (!pskb_may_pull(skb, offset + sizeof(struct iphdr))) | |
602 | return 0; | |
603 | ||
604 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
605 | if (!skb2) | |
606 | return 0; | |
607 | ||
adf30907 ED |
608 | skb_dst_drop(skb2); |
609 | ||
c4d3efaf | 610 | skb_pull(skb2, offset); |
c1d2bbe1 | 611 | skb_reset_network_header(skb2); |
eddc9ec5 | 612 | eiph = ip_hdr(skb2); |
c4d3efaf YK |
613 | |
614 | /* Try to guess incoming interface */ | |
31e4543d | 615 | rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, |
78fbfd8a DM |
616 | eiph->saddr, 0, |
617 | 0, 0, | |
618 | IPPROTO_IPIP, RT_TOS(eiph->tos), 0); | |
b23dd4fe | 619 | if (IS_ERR(rt)) |
c4d3efaf YK |
620 | goto out; |
621 | ||
d8d1f30b | 622 | skb2->dev = rt->dst.dev; |
c4d3efaf YK |
623 | |
624 | /* route "incoming" packet */ | |
625 | if (rt->rt_flags & RTCF_LOCAL) { | |
626 | ip_rt_put(rt); | |
627 | rt = NULL; | |
31e4543d | 628 | rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, |
78fbfd8a DM |
629 | eiph->daddr, eiph->saddr, |
630 | 0, 0, | |
631 | IPPROTO_IPIP, | |
632 | RT_TOS(eiph->tos), 0); | |
b23dd4fe | 633 | if (IS_ERR(rt) || |
d8d1f30b | 634 | rt->dst.dev->type != ARPHRD_TUNNEL) { |
b23dd4fe DM |
635 | if (!IS_ERR(rt)) |
636 | ip_rt_put(rt); | |
c4d3efaf YK |
637 | goto out; |
638 | } | |
b23dd4fe | 639 | skb_dst_set(skb2, &rt->dst); |
c4d3efaf YK |
640 | } else { |
641 | ip_rt_put(rt); | |
642 | if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos, | |
643 | skb2->dev) || | |
adf30907 | 644 | skb_dst(skb2)->dev->type != ARPHRD_TUNNEL) |
c4d3efaf YK |
645 | goto out; |
646 | } | |
647 | ||
648 | /* change mtu on this route */ | |
649 | if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) { | |
adf30907 | 650 | if (rel_info > dst_mtu(skb_dst(skb2))) |
c4d3efaf YK |
651 | goto out; |
652 | ||
6700c270 | 653 | skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info); |
c4d3efaf | 654 | } |
1ed5c48f | 655 | if (rel_type == ICMP_REDIRECT) |
6700c270 | 656 | skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2); |
c4d3efaf | 657 | |
704eae1f | 658 | icmp_send(skb2, rel_type, rel_code, htonl(rel_info)); |
c4d3efaf YK |
659 | |
660 | out: | |
661 | kfree_skb(skb2); | |
662 | return 0; | |
663 | } | |
664 | ||
e490d1d8 YK |
665 | static int |
666 | ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
d5fdd6ba | 667 | u8 type, u8 code, int offset, __be32 info) |
e490d1d8 YK |
668 | { |
669 | int rel_msg = 0; | |
d5fdd6ba BH |
670 | u8 rel_type = type; |
671 | u8 rel_code = code; | |
704eae1f | 672 | __u32 rel_info = ntohl(info); |
e490d1d8 YK |
673 | int err; |
674 | ||
502b0935 YK |
675 | err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code, |
676 | &rel_msg, &rel_info, offset); | |
e490d1d8 YK |
677 | if (err < 0) |
678 | return err; | |
679 | ||
680 | if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) { | |
1da177e4 LT |
681 | struct rt6_info *rt; |
682 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); | |
305d4b3c | 683 | |
1da177e4 | 684 | if (!skb2) |
e490d1d8 | 685 | return 0; |
1da177e4 | 686 | |
adf30907 | 687 | skb_dst_drop(skb2); |
1da177e4 | 688 | skb_pull(skb2, offset); |
c1d2bbe1 | 689 | skb_reset_network_header(skb2); |
1da177e4 LT |
690 | |
691 | /* Try to guess incoming interface */ | |
2f7f54b7 PE |
692 | rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, |
693 | NULL, 0, 0); | |
1da177e4 | 694 | |
d1918542 DM |
695 | if (rt && rt->dst.dev) |
696 | skb2->dev = rt->dst.dev; | |
1da177e4 | 697 | |
3ffe533c | 698 | icmpv6_send(skb2, rel_type, rel_code, rel_info); |
1da177e4 | 699 | |
94e187c0 | 700 | ip6_rt_put(rt); |
1da177e4 LT |
701 | |
702 | kfree_skb(skb2); | |
703 | } | |
e490d1d8 YK |
704 | |
705 | return 0; | |
1da177e4 LT |
706 | } |
707 | ||
f4e0b4c5 ND |
708 | static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, |
709 | const struct ipv6hdr *ipv6h, | |
710 | struct sk_buff *skb) | |
c4d3efaf YK |
711 | { |
712 | __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK; | |
713 | ||
714 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) | |
eddc9ec5 | 715 | ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield); |
c4d3efaf | 716 | |
f4e0b4c5 | 717 | return IP6_ECN_decapsulate(ipv6h, skb); |
c4d3efaf YK |
718 | } |
719 | ||
f4e0b4c5 ND |
720 | static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, |
721 | const struct ipv6hdr *ipv6h, | |
722 | struct sk_buff *skb) | |
1da177e4 | 723 | { |
8359925b | 724 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) |
29bb43b4 | 725 | ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb)); |
1da177e4 | 726 | |
f4e0b4c5 | 727 | return IP6_ECN_decapsulate(ipv6h, skb); |
1da177e4 | 728 | } |
8359925b | 729 | |
c12b395a | 730 | __u32 ip6_tnl_get_cap(struct ip6_tnl *t, |
d0087b29 VN |
731 | const struct in6_addr *laddr, |
732 | const struct in6_addr *raddr) | |
733 | { | |
c12b395a | 734 | struct __ip6_tnl_parm *p = &t->parms; |
d0087b29 VN |
735 | int ltype = ipv6_addr_type(laddr); |
736 | int rtype = ipv6_addr_type(raddr); | |
737 | __u32 flags = 0; | |
738 | ||
739 | if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) { | |
740 | flags = IP6_TNL_F_CAP_PER_PACKET; | |
741 | } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) && | |
742 | rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) && | |
743 | !((ltype|rtype) & IPV6_ADDR_LOOPBACK) && | |
744 | (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) { | |
745 | if (ltype&IPV6_ADDR_UNICAST) | |
746 | flags |= IP6_TNL_F_CAP_XMIT; | |
747 | if (rtype&IPV6_ADDR_UNICAST) | |
748 | flags |= IP6_TNL_F_CAP_RCV; | |
749 | } | |
750 | return flags; | |
751 | } | |
c12b395a | 752 | EXPORT_SYMBOL(ip6_tnl_get_cap); |
d0087b29 | 753 | |
f1a28eab | 754 | /* called with rcu_read_lock() */ |
c12b395a | 755 | int ip6_tnl_rcv_ctl(struct ip6_tnl *t, |
d0087b29 VN |
756 | const struct in6_addr *laddr, |
757 | const struct in6_addr *raddr) | |
09c6bbf0 | 758 | { |
c12b395a | 759 | struct __ip6_tnl_parm *p = &t->parms; |
09c6bbf0 | 760 | int ret = 0; |
2f7f54b7 | 761 | struct net *net = dev_net(t->dev); |
09c6bbf0 | 762 | |
d0087b29 VN |
763 | if ((p->flags & IP6_TNL_F_CAP_RCV) || |
764 | ((p->flags & IP6_TNL_F_CAP_PER_PACKET) && | |
765 | (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) { | |
1ab1457c | 766 | struct net_device *ldev = NULL; |
09c6bbf0 VN |
767 | |
768 | if (p->link) | |
f1a28eab | 769 | ldev = dev_get_by_index_rcu(net, p->link); |
09c6bbf0 | 770 | |
d0087b29 VN |
771 | if ((ipv6_addr_is_multicast(laddr) || |
772 | likely(ipv6_chk_addr(net, laddr, ldev, 0))) && | |
773 | likely(!ipv6_chk_addr(net, raddr, NULL, 0))) | |
09c6bbf0 | 774 | ret = 1; |
09c6bbf0 VN |
775 | } |
776 | return ret; | |
777 | } | |
c12b395a | 778 | EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl); |
1da177e4 LT |
779 | |
780 | /** | |
3144581c | 781 | * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally |
1da177e4 | 782 | * @skb: received socket buffer |
8359925b YK |
783 | * @protocol: ethernet protocol ID |
784 | * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN | |
1da177e4 LT |
785 | * |
786 | * Return: 0 | |
787 | **/ | |
788 | ||
8359925b | 789 | static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol, |
502b0935 | 790 | __u8 ipproto, |
f4e0b4c5 ND |
791 | int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t, |
792 | const struct ipv6hdr *ipv6h, | |
793 | struct sk_buff *skb)) | |
1da177e4 | 794 | { |
1da177e4 | 795 | struct ip6_tnl *t; |
b71d1d42 | 796 | const struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
f4e0b4c5 | 797 | int err; |
1da177e4 | 798 | |
2922bc8a | 799 | rcu_read_lock(); |
1da177e4 | 800 | |
8704ca7e | 801 | if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, |
2dd02c89 | 802 | &ipv6h->daddr)) != NULL) { |
8560f226 ED |
803 | struct pcpu_tstats *tstats; |
804 | ||
502b0935 | 805 | if (t->parms.proto != ipproto && t->parms.proto != 0) { |
2922bc8a | 806 | rcu_read_unlock(); |
502b0935 YK |
807 | goto discard; |
808 | } | |
809 | ||
1da177e4 | 810 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { |
2922bc8a | 811 | rcu_read_unlock(); |
50fba2aa | 812 | goto discard; |
1da177e4 LT |
813 | } |
814 | ||
d0087b29 | 815 | if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) { |
3dca02af | 816 | t->dev->stats.rx_dropped++; |
2922bc8a | 817 | rcu_read_unlock(); |
1da177e4 LT |
818 | goto discard; |
819 | } | |
820 | secpath_reset(skb); | |
b0e380b1 | 821 | skb->mac_header = skb->network_header; |
c1d2bbe1 | 822 | skb_reset_network_header(skb); |
8359925b | 823 | skb->protocol = htons(protocol); |
1da177e4 LT |
824 | skb->pkt_type = PACKET_HOST; |
825 | memset(skb->cb, 0, sizeof(struct inet6_skb_parm)); | |
8359925b | 826 | |
f4e0b4c5 ND |
827 | __skb_tunnel_rx(skb, t->dev); |
828 | ||
829 | err = dscp_ecn_decapsulate(t, ipv6h, skb); | |
830 | if (unlikely(err)) { | |
831 | if (log_ecn_error) | |
832 | net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n", | |
833 | &ipv6h->saddr, | |
834 | ipv6_get_dsfield(ipv6h)); | |
835 | if (err > 1) { | |
836 | ++t->dev->stats.rx_frame_errors; | |
837 | ++t->dev->stats.rx_errors; | |
838 | rcu_read_unlock(); | |
839 | goto discard; | |
840 | } | |
841 | } | |
842 | ||
8560f226 | 843 | tstats = this_cpu_ptr(t->dev->tstats); |
abcfcd04 | 844 | u64_stats_update_begin(&tstats->syncp); |
8560f226 ED |
845 | tstats->rx_packets++; |
846 | tstats->rx_bytes += skb->len; | |
abcfcd04 | 847 | u64_stats_update_end(&tstats->syncp); |
8560f226 | 848 | |
caf586e5 | 849 | netif_rx(skb); |
8990f468 | 850 | |
2922bc8a | 851 | rcu_read_unlock(); |
1da177e4 LT |
852 | return 0; |
853 | } | |
2922bc8a | 854 | rcu_read_unlock(); |
1da177e4 | 855 | return 1; |
50fba2aa HX |
856 | |
857 | discard: | |
858 | kfree_skb(skb); | |
859 | return 0; | |
1da177e4 LT |
860 | } |
861 | ||
c4d3efaf YK |
862 | static int ip4ip6_rcv(struct sk_buff *skb) |
863 | { | |
502b0935 YK |
864 | return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP, |
865 | ip4ip6_dscp_ecn_decapsulate); | |
c4d3efaf YK |
866 | } |
867 | ||
8359925b YK |
868 | static int ip6ip6_rcv(struct sk_buff *skb) |
869 | { | |
502b0935 YK |
870 | return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6, |
871 | ip6ip6_dscp_ecn_decapsulate); | |
8359925b YK |
872 | } |
873 | ||
6fb32dde VN |
874 | struct ipv6_tel_txoption { |
875 | struct ipv6_txoptions ops; | |
876 | __u8 dst_opt[8]; | |
877 | }; | |
1da177e4 | 878 | |
6fb32dde VN |
879 | static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit) |
880 | { | |
881 | memset(opt, 0, sizeof(struct ipv6_tel_txoption)); | |
1da177e4 | 882 | |
6fb32dde VN |
883 | opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT; |
884 | opt->dst_opt[3] = 1; | |
885 | opt->dst_opt[4] = encap_limit; | |
886 | opt->dst_opt[5] = IPV6_TLV_PADN; | |
887 | opt->dst_opt[6] = 1; | |
1da177e4 | 888 | |
6fb32dde VN |
889 | opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt; |
890 | opt->ops.opt_nflen = 8; | |
1da177e4 LT |
891 | } |
892 | ||
893 | /** | |
3144581c | 894 | * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own |
1da177e4 | 895 | * @t: the outgoing tunnel device |
1ab1457c | 896 | * @hdr: IPv6 header from the incoming packet |
1da177e4 LT |
897 | * |
898 | * Description: | |
1ab1457c | 899 | * Avoid trivial tunneling loop by checking that tunnel exit-point |
1da177e4 LT |
900 | * doesn't match source of incoming packet. |
901 | * | |
1ab1457c | 902 | * Return: |
1da177e4 LT |
903 | * 1 if conflict, |
904 | * 0 else | |
905 | **/ | |
906 | ||
92113bfd | 907 | static inline bool |
b71d1d42 | 908 | ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr) |
1da177e4 LT |
909 | { |
910 | return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); | |
911 | } | |
912 | ||
c12b395a | 913 | int ip6_tnl_xmit_ctl(struct ip6_tnl *t) |
09c6bbf0 | 914 | { |
c12b395a | 915 | struct __ip6_tnl_parm *p = &t->parms; |
09c6bbf0 | 916 | int ret = 0; |
2f7f54b7 | 917 | struct net *net = dev_net(t->dev); |
09c6bbf0 | 918 | |
1ab1457c | 919 | if (p->flags & IP6_TNL_F_CAP_XMIT) { |
09c6bbf0 VN |
920 | struct net_device *ldev = NULL; |
921 | ||
f1a28eab | 922 | rcu_read_lock(); |
09c6bbf0 | 923 | if (p->link) |
f1a28eab | 924 | ldev = dev_get_by_index_rcu(net, p->link); |
09c6bbf0 | 925 | |
2f7f54b7 | 926 | if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0))) |
f3213831 JP |
927 | pr_warn("%s xmit: Local address not yet configured!\n", |
928 | p->name); | |
09c6bbf0 | 929 | else if (!ipv6_addr_is_multicast(&p->raddr) && |
2f7f54b7 | 930 | unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0))) |
f3213831 JP |
931 | pr_warn("%s xmit: Routing loop! Remote address found on this node!\n", |
932 | p->name); | |
09c6bbf0 VN |
933 | else |
934 | ret = 1; | |
f1a28eab | 935 | rcu_read_unlock(); |
09c6bbf0 VN |
936 | } |
937 | return ret; | |
938 | } | |
c12b395a | 939 | EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl); |
940 | ||
1da177e4 | 941 | /** |
61ec2aec | 942 | * ip6_tnl_xmit2 - encapsulate packet and send |
1da177e4 | 943 | * @skb: the outgoing socket buffer |
1ab1457c | 944 | * @dev: the outgoing tunnel device |
61ec2aec YK |
945 | * @dsfield: dscp code for outer header |
946 | * @fl: flow of tunneled packet | |
947 | * @encap_limit: encapsulation limit | |
948 | * @pmtu: Path MTU is stored if packet is too big | |
1da177e4 LT |
949 | * |
950 | * Description: | |
951 | * Build new header and do some sanity checks on the packet before sending | |
952 | * it. | |
953 | * | |
1ab1457c | 954 | * Return: |
c4d3efaf | 955 | * 0 on success |
61ec2aec YK |
956 | * -1 fail |
957 | * %-EMSGSIZE message too big. return mtu in this case. | |
1da177e4 LT |
958 | **/ |
959 | ||
61ec2aec YK |
960 | static int ip6_tnl_xmit2(struct sk_buff *skb, |
961 | struct net_device *dev, | |
962 | __u8 dsfield, | |
4c9483b2 | 963 | struct flowi6 *fl6, |
61ec2aec YK |
964 | int encap_limit, |
965 | __u32 *pmtu) | |
1da177e4 | 966 | { |
52479b62 | 967 | struct net *net = dev_net(dev); |
2941a486 | 968 | struct ip6_tnl *t = netdev_priv(dev); |
3dca02af | 969 | struct net_device_stats *stats = &t->dev->stats; |
0660e03f | 970 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
6fb32dde | 971 | struct ipv6_tel_txoption opt; |
d24f22f3 | 972 | struct dst_entry *dst = NULL, *ndst = NULL; |
1da177e4 | 973 | struct net_device *tdev; |
68836e49 | 974 | bool use_cache = false; |
1da177e4 | 975 | int mtu; |
c2636b4d | 976 | unsigned int max_headroom = sizeof(struct ipv6hdr); |
1da177e4 | 977 | u8 proto; |
61ec2aec | 978 | int err = -1; |
1da177e4 | 979 | |
68836e49 PA |
980 | if (!(t->parms.flags & |
981 | (IP6_TNL_F_USE_ORIG_TCLASS | IP6_TNL_F_USE_ORIG_FWMARK))) { | |
982 | /* enable the cache only only if the routing decision does | |
983 | * not depend on the current inner header value | |
984 | */ | |
985 | use_cache = true; | |
986 | } | |
987 | ||
988 | if (use_cache) | |
d24f22f3 | 989 | dst = ip6_tnl_dst_check(t); |
89b02126 ED |
990 | if (!dst) { |
991 | ndst = ip6_route_output(net, NULL, fl6); | |
1da177e4 | 992 | |
89b02126 | 993 | if (ndst->error) |
a57ebc90 | 994 | goto tx_err_link_failure; |
89b02126 ED |
995 | ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0); |
996 | if (IS_ERR(ndst)) { | |
997 | err = PTR_ERR(ndst); | |
998 | ndst = NULL; | |
452edd59 DM |
999 | goto tx_err_link_failure; |
1000 | } | |
89b02126 | 1001 | dst = ndst; |
a57ebc90 | 1002 | } |
1da177e4 LT |
1003 | |
1004 | tdev = dst->dev; | |
1005 | ||
1006 | if (tdev == dev) { | |
1007 | stats->collisions++; | |
e87cc472 JP |
1008 | net_warn_ratelimited("%s: Local routing loop detected!\n", |
1009 | t->parms.name); | |
1da177e4 LT |
1010 | goto tx_err_dst_release; |
1011 | } | |
1012 | mtu = dst_mtu(dst) - sizeof (*ipv6h); | |
6fb32dde | 1013 | if (encap_limit >= 0) { |
1da177e4 LT |
1014 | max_headroom += 8; |
1015 | mtu -= 8; | |
1016 | } | |
1017 | if (mtu < IPV6_MIN_MTU) | |
1018 | mtu = IPV6_MIN_MTU; | |
adf30907 | 1019 | if (skb_dst(skb)) |
6700c270 | 1020 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu); |
1da177e4 | 1021 | if (skb->len > mtu) { |
61ec2aec YK |
1022 | *pmtu = mtu; |
1023 | err = -EMSGSIZE; | |
1da177e4 LT |
1024 | goto tx_err_dst_release; |
1025 | } | |
1026 | ||
1027 | /* | |
1028 | * Okay, now see if we can stuff it in the buffer as-is. | |
1029 | */ | |
1030 | max_headroom += LL_RESERVED_SPACE(tdev); | |
1ab1457c | 1031 | |
cfbba49d PM |
1032 | if (skb_headroom(skb) < max_headroom || skb_shared(skb) || |
1033 | (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { | |
1da177e4 | 1034 | struct sk_buff *new_skb; |
1ab1457c | 1035 | |
1da177e4 LT |
1036 | if (!(new_skb = skb_realloc_headroom(skb, max_headroom))) |
1037 | goto tx_err_dst_release; | |
1038 | ||
1039 | if (skb->sk) | |
1040 | skb_set_owner_w(new_skb, skb->sk); | |
9ff26449 | 1041 | consume_skb(skb); |
1da177e4 LT |
1042 | skb = new_skb; |
1043 | } | |
adf30907 | 1044 | skb_dst_drop(skb); |
68836e49 | 1045 | if (!use_cache) { |
d24f22f3 ED |
1046 | skb_dst_set(skb, dst); |
1047 | ndst = NULL; | |
1048 | } else { | |
1049 | skb_dst_set_noref(skb, dst); | |
1050 | } | |
b0e380b1 | 1051 | skb->transport_header = skb->network_header; |
1da177e4 | 1052 | |
4c9483b2 | 1053 | proto = fl6->flowi6_proto; |
6fb32dde VN |
1054 | if (encap_limit >= 0) { |
1055 | init_tel_txopt(&opt, encap_limit); | |
1056 | ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL); | |
1057 | } | |
e2d1bca7 ACM |
1058 | skb_push(skb, sizeof(struct ipv6hdr)); |
1059 | skb_reset_network_header(skb); | |
0660e03f | 1060 | ipv6h = ipv6_hdr(skb); |
3e4e4c1f | 1061 | ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield), fl6->flowlabel); |
1da177e4 LT |
1062 | ipv6h->hop_limit = t->parms.hop_limit; |
1063 | ipv6h->nexthdr = proto; | |
4e3fd7a0 AD |
1064 | ipv6h->saddr = fl6->saddr; |
1065 | ipv6h->daddr = fl6->daddr; | |
e8f72ea4 | 1066 | ip6tunnel_xmit(skb, dev); |
89b02126 ED |
1067 | if (ndst) |
1068 | ip6_tnl_dst_store(t, ndst); | |
1da177e4 LT |
1069 | return 0; |
1070 | tx_err_link_failure: | |
1071 | stats->tx_carrier_errors++; | |
1072 | dst_link_failure(skb); | |
1073 | tx_err_dst_release: | |
89b02126 | 1074 | dst_release(ndst); |
61ec2aec YK |
1075 | return err; |
1076 | } | |
1077 | ||
c4d3efaf YK |
1078 | static inline int |
1079 | ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) | |
1080 | { | |
1081 | struct ip6_tnl *t = netdev_priv(dev); | |
b71d1d42 | 1082 | const struct iphdr *iph = ip_hdr(skb); |
c4d3efaf | 1083 | int encap_limit = -1; |
4c9483b2 | 1084 | struct flowi6 fl6; |
c4d3efaf YK |
1085 | __u8 dsfield; |
1086 | __u32 mtu; | |
1087 | int err; | |
1088 | ||
502b0935 YK |
1089 | if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) || |
1090 | !ip6_tnl_xmit_ctl(t)) | |
c4d3efaf YK |
1091 | return -1; |
1092 | ||
1093 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
1094 | encap_limit = t->parms.encap_limit; | |
1095 | ||
4c9483b2 DM |
1096 | memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6)); |
1097 | fl6.flowi6_proto = IPPROTO_IPIP; | |
c4d3efaf YK |
1098 | |
1099 | dsfield = ipv4_get_dsfield(iph); | |
1100 | ||
d24f22f3 | 1101 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
4c9483b2 | 1102 | fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT) |
b77f2fa6 | 1103 | & IPV6_TCLASS_MASK; |
d24f22f3 ED |
1104 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
1105 | fl6.flowi6_mark = skb->mark; | |
c4d3efaf | 1106 | |
4c9483b2 | 1107 | err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu); |
c4d3efaf YK |
1108 | if (err != 0) { |
1109 | /* XXX: send ICMP error even if DF is not set. */ | |
1110 | if (err == -EMSGSIZE) | |
1111 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, | |
1112 | htonl(mtu)); | |
1113 | return -1; | |
1114 | } | |
1115 | ||
1116 | return 0; | |
1117 | } | |
1118 | ||
61ec2aec YK |
1119 | static inline int |
1120 | ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) | |
1121 | { | |
1122 | struct ip6_tnl *t = netdev_priv(dev); | |
0660e03f | 1123 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
61ec2aec YK |
1124 | int encap_limit = -1; |
1125 | __u16 offset; | |
4c9483b2 | 1126 | struct flowi6 fl6; |
61ec2aec YK |
1127 | __u8 dsfield; |
1128 | __u32 mtu; | |
1129 | int err; | |
1130 | ||
502b0935 YK |
1131 | if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) || |
1132 | !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h)) | |
61ec2aec YK |
1133 | return -1; |
1134 | ||
c12b395a | 1135 | offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb)); |
d56f90a7 | 1136 | if (offset > 0) { |
61ec2aec | 1137 | struct ipv6_tlv_tnl_enc_lim *tel; |
d56f90a7 | 1138 | tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset]; |
61ec2aec YK |
1139 | if (tel->encap_limit == 0) { |
1140 | icmpv6_send(skb, ICMPV6_PARAMPROB, | |
3ffe533c | 1141 | ICMPV6_HDR_FIELD, offset + 2); |
61ec2aec YK |
1142 | return -1; |
1143 | } | |
1144 | encap_limit = tel->encap_limit - 1; | |
1145 | } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
1146 | encap_limit = t->parms.encap_limit; | |
1147 | ||
4c9483b2 DM |
1148 | memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6)); |
1149 | fl6.flowi6_proto = IPPROTO_IPV6; | |
61ec2aec YK |
1150 | |
1151 | dsfield = ipv6_get_dsfield(ipv6h); | |
d24f22f3 | 1152 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
4c9483b2 | 1153 | fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK); |
d24f22f3 | 1154 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) |
4c9483b2 | 1155 | fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK); |
d24f22f3 ED |
1156 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
1157 | fl6.flowi6_mark = skb->mark; | |
61ec2aec | 1158 | |
4c9483b2 | 1159 | err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu); |
61ec2aec YK |
1160 | if (err != 0) { |
1161 | if (err == -EMSGSIZE) | |
3ffe533c | 1162 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
61ec2aec YK |
1163 | return -1; |
1164 | } | |
1165 | ||
1166 | return 0; | |
1167 | } | |
1168 | ||
6fef4c0c | 1169 | static netdev_tx_t |
61ec2aec YK |
1170 | ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) |
1171 | { | |
1172 | struct ip6_tnl *t = netdev_priv(dev); | |
3dca02af | 1173 | struct net_device_stats *stats = &t->dev->stats; |
61ec2aec YK |
1174 | int ret; |
1175 | ||
61ec2aec | 1176 | switch (skb->protocol) { |
60678040 | 1177 | case htons(ETH_P_IP): |
c4d3efaf YK |
1178 | ret = ip4ip6_tnl_xmit(skb, dev); |
1179 | break; | |
60678040 | 1180 | case htons(ETH_P_IPV6): |
61ec2aec YK |
1181 | ret = ip6ip6_tnl_xmit(skb, dev); |
1182 | break; | |
1183 | default: | |
1184 | goto tx_err; | |
1185 | } | |
1186 | ||
1187 | if (ret < 0) | |
1188 | goto tx_err; | |
1189 | ||
6ed10654 | 1190 | return NETDEV_TX_OK; |
61ec2aec | 1191 | |
1da177e4 LT |
1192 | tx_err: |
1193 | stats->tx_errors++; | |
1194 | stats->tx_dropped++; | |
1195 | kfree_skb(skb); | |
6ed10654 | 1196 | return NETDEV_TX_OK; |
1da177e4 LT |
1197 | } |
1198 | ||
3144581c | 1199 | static void ip6_tnl_link_config(struct ip6_tnl *t) |
1da177e4 LT |
1200 | { |
1201 | struct net_device *dev = t->dev; | |
c12b395a | 1202 | struct __ip6_tnl_parm *p = &t->parms; |
4c9483b2 | 1203 | struct flowi6 *fl6 = &t->fl.u.ip6; |
1da177e4 | 1204 | |
3a6d54c5 JP |
1205 | memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); |
1206 | memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); | |
1da177e4 LT |
1207 | |
1208 | /* Set up flowi template */ | |
4e3fd7a0 AD |
1209 | fl6->saddr = p->laddr; |
1210 | fl6->daddr = p->raddr; | |
4c9483b2 DM |
1211 | fl6->flowi6_oif = p->link; |
1212 | fl6->flowlabel = 0; | |
1da177e4 LT |
1213 | |
1214 | if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) | |
4c9483b2 | 1215 | fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; |
1da177e4 | 1216 | if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) |
4c9483b2 | 1217 | fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; |
1da177e4 | 1218 | |
d0087b29 VN |
1219 | p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); |
1220 | p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); | |
1da177e4 LT |
1221 | |
1222 | if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV) | |
1223 | dev->flags |= IFF_POINTOPOINT; | |
1224 | else | |
1225 | dev->flags &= ~IFF_POINTOPOINT; | |
1226 | ||
1227 | dev->iflink = p->link; | |
1228 | ||
1229 | if (p->flags & IP6_TNL_F_CAP_XMIT) { | |
305d4b3c VN |
1230 | int strict = (ipv6_addr_type(&p->raddr) & |
1231 | (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); | |
1232 | ||
2f7f54b7 PE |
1233 | struct rt6_info *rt = rt6_lookup(dev_net(dev), |
1234 | &p->raddr, &p->laddr, | |
305d4b3c | 1235 | p->link, strict); |
1da177e4 LT |
1236 | |
1237 | if (rt == NULL) | |
1238 | return; | |
1239 | ||
d1918542 DM |
1240 | if (rt->dst.dev) { |
1241 | dev->hard_header_len = rt->dst.dev->hard_header_len + | |
1da177e4 LT |
1242 | sizeof (struct ipv6hdr); |
1243 | ||
d1918542 | 1244 | dev->mtu = rt->dst.dev->mtu - sizeof (struct ipv6hdr); |
381601e5 AF |
1245 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
1246 | dev->mtu-=8; | |
1da177e4 LT |
1247 | |
1248 | if (dev->mtu < IPV6_MIN_MTU) | |
1249 | dev->mtu = IPV6_MIN_MTU; | |
1250 | } | |
94e187c0 | 1251 | ip6_rt_put(rt); |
1da177e4 LT |
1252 | } |
1253 | } | |
1254 | ||
1255 | /** | |
3144581c | 1256 | * ip6_tnl_change - update the tunnel parameters |
1da177e4 LT |
1257 | * @t: tunnel to be changed |
1258 | * @p: tunnel configuration parameters | |
1da177e4 LT |
1259 | * |
1260 | * Description: | |
3144581c | 1261 | * ip6_tnl_change() updates the tunnel parameters |
1da177e4 LT |
1262 | **/ |
1263 | ||
1264 | static int | |
c12b395a | 1265 | ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p) |
1da177e4 | 1266 | { |
4e3fd7a0 AD |
1267 | t->parms.laddr = p->laddr; |
1268 | t->parms.raddr = p->raddr; | |
1da177e4 LT |
1269 | t->parms.flags = p->flags; |
1270 | t->parms.hop_limit = p->hop_limit; | |
1271 | t->parms.encap_limit = p->encap_limit; | |
1272 | t->parms.flowinfo = p->flowinfo; | |
8181b8c1 | 1273 | t->parms.link = p->link; |
502b0935 | 1274 | t->parms.proto = p->proto; |
0c088890 | 1275 | ip6_tnl_dst_reset(t); |
3144581c | 1276 | ip6_tnl_link_config(t); |
1da177e4 LT |
1277 | return 0; |
1278 | } | |
1279 | ||
0b112457 ND |
1280 | static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p) |
1281 | { | |
1282 | struct net *net = dev_net(t->dev); | |
1283 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); | |
1284 | int err; | |
1285 | ||
1286 | ip6_tnl_unlink(ip6n, t); | |
1287 | synchronize_net(); | |
1288 | err = ip6_tnl_change(t, p); | |
1289 | ip6_tnl_link(ip6n, t); | |
1290 | netdev_state_change(t->dev); | |
1291 | return err; | |
1292 | } | |
1293 | ||
c12b395a | 1294 | static void |
1295 | ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u) | |
1296 | { | |
1297 | p->laddr = u->laddr; | |
1298 | p->raddr = u->raddr; | |
1299 | p->flags = u->flags; | |
1300 | p->hop_limit = u->hop_limit; | |
1301 | p->encap_limit = u->encap_limit; | |
1302 | p->flowinfo = u->flowinfo; | |
1303 | p->link = u->link; | |
1304 | p->proto = u->proto; | |
1305 | memcpy(p->name, u->name, sizeof(u->name)); | |
1306 | } | |
1307 | ||
1308 | static void | |
1309 | ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p) | |
1310 | { | |
1311 | u->laddr = p->laddr; | |
1312 | u->raddr = p->raddr; | |
1313 | u->flags = p->flags; | |
1314 | u->hop_limit = p->hop_limit; | |
1315 | u->encap_limit = p->encap_limit; | |
1316 | u->flowinfo = p->flowinfo; | |
1317 | u->link = p->link; | |
1318 | u->proto = p->proto; | |
1319 | memcpy(u->name, p->name, sizeof(u->name)); | |
1320 | } | |
1321 | ||
1da177e4 | 1322 | /** |
3144581c | 1323 | * ip6_tnl_ioctl - configure ipv6 tunnels from userspace |
1da177e4 LT |
1324 | * @dev: virtual device associated with tunnel |
1325 | * @ifr: parameters passed from userspace | |
1326 | * @cmd: command to be performed | |
1327 | * | |
1328 | * Description: | |
3144581c | 1329 | * ip6_tnl_ioctl() is used for managing IPv6 tunnels |
1ab1457c | 1330 | * from userspace. |
1da177e4 LT |
1331 | * |
1332 | * The possible commands are the following: | |
1333 | * %SIOCGETTUNNEL: get tunnel parameters for device | |
1334 | * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters | |
1335 | * %SIOCCHGTUNNEL: change tunnel parameters to those given | |
1336 | * %SIOCDELTUNNEL: delete tunnel | |
1337 | * | |
1ab1457c | 1338 | * The fallback device "ip6tnl0", created during module |
1da177e4 LT |
1339 | * initialization, can be used for creating other tunnel devices. |
1340 | * | |
1341 | * Return: | |
1342 | * 0 on success, | |
1343 | * %-EFAULT if unable to copy data to or from userspace, | |
1344 | * %-EPERM if current process hasn't %CAP_NET_ADMIN set | |
1345 | * %-EINVAL if passed tunnel parameters are invalid, | |
1346 | * %-EEXIST if changing a tunnel's parameters would cause a conflict | |
1347 | * %-ENODEV if attempting to change or delete a nonexisting device | |
1348 | **/ | |
1349 | ||
1350 | static int | |
3144581c | 1351 | ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
1da177e4 LT |
1352 | { |
1353 | int err = 0; | |
1da177e4 | 1354 | struct ip6_tnl_parm p; |
c12b395a | 1355 | struct __ip6_tnl_parm p1; |
1da177e4 | 1356 | struct ip6_tnl *t = NULL; |
2dd02c89 PE |
1357 | struct net *net = dev_net(dev); |
1358 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); | |
1da177e4 LT |
1359 | |
1360 | switch (cmd) { | |
1361 | case SIOCGETTUNNEL: | |
15820e12 | 1362 | if (dev == ip6n->fb_tnl_dev) { |
567131a7 | 1363 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) { |
1da177e4 LT |
1364 | err = -EFAULT; |
1365 | break; | |
1366 | } | |
c12b395a | 1367 | ip6_tnl_parm_from_user(&p1, &p); |
1368 | t = ip6_tnl_locate(net, &p1, 0); | |
5ef5d6c5 DC |
1369 | } else { |
1370 | memset(&p, 0, sizeof(p)); | |
567131a7 VN |
1371 | } |
1372 | if (t == NULL) | |
2941a486 | 1373 | t = netdev_priv(dev); |
c12b395a | 1374 | ip6_tnl_parm_to_user(&p, &t->parms); |
1da177e4 LT |
1375 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) { |
1376 | err = -EFAULT; | |
1377 | } | |
1378 | break; | |
1379 | case SIOCADDTUNNEL: | |
1380 | case SIOCCHGTUNNEL: | |
1381 | err = -EPERM; | |
af31f412 | 1382 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 | 1383 | break; |
567131a7 VN |
1384 | err = -EFAULT; |
1385 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) | |
1da177e4 | 1386 | break; |
567131a7 | 1387 | err = -EINVAL; |
502b0935 YK |
1388 | if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP && |
1389 | p.proto != 0) | |
1da177e4 | 1390 | break; |
c12b395a | 1391 | ip6_tnl_parm_from_user(&p1, &p); |
1392 | t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL); | |
15820e12 | 1393 | if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) { |
567131a7 VN |
1394 | if (t != NULL) { |
1395 | if (t->dev != dev) { | |
1396 | err = -EEXIST; | |
1397 | break; | |
1398 | } | |
1399 | } else | |
1400 | t = netdev_priv(dev); | |
1401 | ||
0b112457 | 1402 | err = ip6_tnl_update(t, &p1); |
1da177e4 | 1403 | } |
567131a7 | 1404 | if (t) { |
1da177e4 | 1405 | err = 0; |
c12b395a | 1406 | ip6_tnl_parm_to_user(&p, &t->parms); |
1407 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | |
567131a7 VN |
1408 | err = -EFAULT; |
1409 | ||
1410 | } else | |
1411 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); | |
1da177e4 LT |
1412 | break; |
1413 | case SIOCDELTUNNEL: | |
1414 | err = -EPERM; | |
af31f412 | 1415 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
1416 | break; |
1417 | ||
15820e12 | 1418 | if (dev == ip6n->fb_tnl_dev) { |
567131a7 VN |
1419 | err = -EFAULT; |
1420 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) | |
1da177e4 | 1421 | break; |
567131a7 | 1422 | err = -ENOENT; |
c12b395a | 1423 | ip6_tnl_parm_from_user(&p1, &p); |
1424 | t = ip6_tnl_locate(net, &p1, 0); | |
1425 | if (t == NULL) | |
1da177e4 | 1426 | break; |
567131a7 | 1427 | err = -EPERM; |
15820e12 | 1428 | if (t->dev == ip6n->fb_tnl_dev) |
1da177e4 | 1429 | break; |
567131a7 | 1430 | dev = t->dev; |
1da177e4 | 1431 | } |
22f8cde5 SH |
1432 | err = 0; |
1433 | unregister_netdevice(dev); | |
1da177e4 LT |
1434 | break; |
1435 | default: | |
1436 | err = -EINVAL; | |
1437 | } | |
1438 | return err; | |
1439 | } | |
1440 | ||
1da177e4 | 1441 | /** |
3144581c | 1442 | * ip6_tnl_change_mtu - change mtu manually for tunnel device |
1da177e4 LT |
1443 | * @dev: virtual device associated with tunnel |
1444 | * @new_mtu: the new mtu | |
1445 | * | |
1446 | * Return: | |
1447 | * 0 on success, | |
1448 | * %-EINVAL if mtu too small | |
1449 | **/ | |
1450 | ||
1451 | static int | |
3144581c | 1452 | ip6_tnl_change_mtu(struct net_device *dev, int new_mtu) |
1da177e4 LT |
1453 | { |
1454 | if (new_mtu < IPV6_MIN_MTU) { | |
1455 | return -EINVAL; | |
1456 | } | |
1457 | dev->mtu = new_mtu; | |
1458 | return 0; | |
1459 | } | |
1460 | ||
1326c3d5 SH |
1461 | |
1462 | static const struct net_device_ops ip6_tnl_netdev_ops = { | |
2d2a8385 | 1463 | .ndo_init = ip6_tnl_dev_init, |
8560f226 | 1464 | .ndo_uninit = ip6_tnl_dev_uninit, |
1326c3d5 | 1465 | .ndo_start_xmit = ip6_tnl_xmit, |
8560f226 | 1466 | .ndo_do_ioctl = ip6_tnl_ioctl, |
1326c3d5 | 1467 | .ndo_change_mtu = ip6_tnl_change_mtu, |
8560f226 | 1468 | .ndo_get_stats = ip6_get_stats, |
1326c3d5 SH |
1469 | }; |
1470 | ||
8560f226 | 1471 | |
1da177e4 | 1472 | /** |
3144581c | 1473 | * ip6_tnl_dev_setup - setup virtual tunnel device |
1da177e4 LT |
1474 | * @dev: virtual device associated with tunnel |
1475 | * | |
1476 | * Description: | |
1477 | * Initialize function pointers and device parameters | |
1478 | **/ | |
1479 | ||
3144581c | 1480 | static void ip6_tnl_dev_setup(struct net_device *dev) |
1da177e4 | 1481 | { |
381601e5 AF |
1482 | struct ip6_tnl *t; |
1483 | ||
1326c3d5 | 1484 | dev->netdev_ops = &ip6_tnl_netdev_ops; |
8560f226 | 1485 | dev->destructor = ip6_dev_free; |
1da177e4 LT |
1486 | |
1487 | dev->type = ARPHRD_TUNNEL6; | |
1488 | dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr); | |
1489 | dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr); | |
381601e5 AF |
1490 | t = netdev_priv(dev); |
1491 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
1492 | dev->mtu-=8; | |
1da177e4 LT |
1493 | dev->flags |= IFF_NOARP; |
1494 | dev->addr_len = sizeof(struct in6_addr); | |
554eb277 | 1495 | dev->features |= NETIF_F_NETNS_LOCAL; |
7e223de8 | 1496 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; |
1da177e4 LT |
1497 | } |
1498 | ||
1499 | ||
1500 | /** | |
3144581c | 1501 | * ip6_tnl_dev_init_gen - general initializer for all tunnel devices |
1da177e4 LT |
1502 | * @dev: virtual device associated with tunnel |
1503 | **/ | |
1504 | ||
8560f226 | 1505 | static inline int |
3144581c | 1506 | ip6_tnl_dev_init_gen(struct net_device *dev) |
1da177e4 | 1507 | { |
2941a486 | 1508 | struct ip6_tnl *t = netdev_priv(dev); |
8560f226 | 1509 | |
1da177e4 | 1510 | t->dev = dev; |
8560f226 ED |
1511 | dev->tstats = alloc_percpu(struct pcpu_tstats); |
1512 | if (!dev->tstats) | |
1513 | return -ENOMEM; | |
1514 | return 0; | |
1da177e4 LT |
1515 | } |
1516 | ||
1517 | /** | |
3144581c | 1518 | * ip6_tnl_dev_init - initializer for all non fallback tunnel devices |
1da177e4 LT |
1519 | * @dev: virtual device associated with tunnel |
1520 | **/ | |
1521 | ||
8560f226 | 1522 | static int ip6_tnl_dev_init(struct net_device *dev) |
1da177e4 | 1523 | { |
2941a486 | 1524 | struct ip6_tnl *t = netdev_priv(dev); |
8560f226 ED |
1525 | int err = ip6_tnl_dev_init_gen(dev); |
1526 | ||
1527 | if (err) | |
1528 | return err; | |
3144581c | 1529 | ip6_tnl_link_config(t); |
8560f226 | 1530 | return 0; |
1da177e4 LT |
1531 | } |
1532 | ||
1533 | /** | |
3144581c | 1534 | * ip6_fb_tnl_dev_init - initializer for fallback tunnel device |
1da177e4 LT |
1535 | * @dev: fallback device |
1536 | * | |
1537 | * Return: 0 | |
1538 | **/ | |
1539 | ||
8560f226 | 1540 | static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev) |
1da177e4 | 1541 | { |
2941a486 | 1542 | struct ip6_tnl *t = netdev_priv(dev); |
3e6c9fb5 PE |
1543 | struct net *net = dev_net(dev); |
1544 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); | |
1545 | ||
502b0935 | 1546 | t->parms.proto = IPPROTO_IPV6; |
1da177e4 | 1547 | dev_hold(dev); |
d0087b29 | 1548 | |
cf778b00 | 1549 | rcu_assign_pointer(ip6n->tnls_wc[0], t); |
8560f226 | 1550 | return 0; |
1da177e4 LT |
1551 | } |
1552 | ||
0b112457 ND |
1553 | static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[]) |
1554 | { | |
1555 | u8 proto; | |
1556 | ||
4cba246a | 1557 | if (!data || !data[IFLA_IPTUN_PROTO]) |
0b112457 ND |
1558 | return 0; |
1559 | ||
1560 | proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); | |
1561 | if (proto != IPPROTO_IPV6 && | |
1562 | proto != IPPROTO_IPIP && | |
1563 | proto != 0) | |
1564 | return -EINVAL; | |
1565 | ||
1566 | return 0; | |
1567 | } | |
1568 | ||
1569 | static void ip6_tnl_netlink_parms(struct nlattr *data[], | |
1570 | struct __ip6_tnl_parm *parms) | |
1571 | { | |
1572 | memset(parms, 0, sizeof(*parms)); | |
1573 | ||
1574 | if (!data) | |
1575 | return; | |
1576 | ||
1577 | if (data[IFLA_IPTUN_LINK]) | |
1578 | parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]); | |
1579 | ||
1580 | if (data[IFLA_IPTUN_LOCAL]) | |
1581 | nla_memcpy(&parms->laddr, data[IFLA_IPTUN_LOCAL], | |
1582 | sizeof(struct in6_addr)); | |
1583 | ||
1584 | if (data[IFLA_IPTUN_REMOTE]) | |
1585 | nla_memcpy(&parms->raddr, data[IFLA_IPTUN_REMOTE], | |
1586 | sizeof(struct in6_addr)); | |
1587 | ||
1588 | if (data[IFLA_IPTUN_TTL]) | |
1589 | parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]); | |
1590 | ||
1591 | if (data[IFLA_IPTUN_ENCAP_LIMIT]) | |
1592 | parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]); | |
1593 | ||
1594 | if (data[IFLA_IPTUN_FLOWINFO]) | |
1ff05fb7 | 1595 | parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]); |
0b112457 ND |
1596 | |
1597 | if (data[IFLA_IPTUN_FLAGS]) | |
1598 | parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]); | |
1599 | ||
1600 | if (data[IFLA_IPTUN_PROTO]) | |
1601 | parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); | |
1602 | } | |
1603 | ||
1604 | static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev, | |
1605 | struct nlattr *tb[], struct nlattr *data[]) | |
1606 | { | |
1607 | struct net *net = dev_net(dev); | |
1608 | struct ip6_tnl *nt; | |
1609 | ||
1610 | nt = netdev_priv(dev); | |
1611 | ip6_tnl_netlink_parms(data, &nt->parms); | |
1612 | ||
1613 | if (ip6_tnl_locate(net, &nt->parms, 0)) | |
1614 | return -EEXIST; | |
1615 | ||
1616 | return ip6_tnl_create2(dev); | |
1617 | } | |
1618 | ||
1619 | static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[], | |
1620 | struct nlattr *data[]) | |
1621 | { | |
1622 | struct ip6_tnl *t; | |
1623 | struct __ip6_tnl_parm p; | |
1624 | struct net *net = dev_net(dev); | |
1625 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); | |
1626 | ||
1627 | if (dev == ip6n->fb_tnl_dev) | |
1628 | return -EINVAL; | |
1629 | ||
1630 | ip6_tnl_netlink_parms(data, &p); | |
1631 | ||
1632 | t = ip6_tnl_locate(net, &p, 0); | |
1633 | ||
1634 | if (t) { | |
1635 | if (t->dev != dev) | |
1636 | return -EEXIST; | |
1637 | } else | |
1638 | t = netdev_priv(dev); | |
1639 | ||
1640 | return ip6_tnl_update(t, &p); | |
1641 | } | |
1642 | ||
38c963f2 ND |
1643 | static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head) |
1644 | { | |
1645 | struct net *net = dev_net(dev); | |
1646 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); | |
1647 | ||
1648 | if (dev != ip6n->fb_tnl_dev) | |
1649 | unregister_netdevice_queue(dev, head); | |
1650 | } | |
1651 | ||
b58d731a | 1652 | static size_t ip6_tnl_get_size(const struct net_device *dev) |
c075b130 ND |
1653 | { |
1654 | return | |
1655 | /* IFLA_IPTUN_LINK */ | |
1656 | nla_total_size(4) + | |
1657 | /* IFLA_IPTUN_LOCAL */ | |
1658 | nla_total_size(sizeof(struct in6_addr)) + | |
1659 | /* IFLA_IPTUN_REMOTE */ | |
1660 | nla_total_size(sizeof(struct in6_addr)) + | |
1661 | /* IFLA_IPTUN_TTL */ | |
1662 | nla_total_size(1) + | |
1663 | /* IFLA_IPTUN_ENCAP_LIMIT */ | |
1664 | nla_total_size(1) + | |
1665 | /* IFLA_IPTUN_FLOWINFO */ | |
1666 | nla_total_size(4) + | |
1667 | /* IFLA_IPTUN_FLAGS */ | |
1668 | nla_total_size(4) + | |
cfa323b6 ND |
1669 | /* IFLA_IPTUN_PROTO */ |
1670 | nla_total_size(1) + | |
c075b130 ND |
1671 | 0; |
1672 | } | |
1673 | ||
b58d731a | 1674 | static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev) |
c075b130 ND |
1675 | { |
1676 | struct ip6_tnl *tunnel = netdev_priv(dev); | |
1677 | struct __ip6_tnl_parm *parm = &tunnel->parms; | |
1678 | ||
1679 | if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) || | |
1680 | nla_put(skb, IFLA_IPTUN_LOCAL, sizeof(struct in6_addr), | |
c075b130 | 1681 | &parm->laddr) || |
fa42a01c DZ |
1682 | nla_put(skb, IFLA_IPTUN_REMOTE, sizeof(struct in6_addr), |
1683 | &parm->raddr) || | |
c075b130 ND |
1684 | nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) || |
1685 | nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) || | |
1686 | nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) || | |
cfa323b6 ND |
1687 | nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) || |
1688 | nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto)) | |
c075b130 ND |
1689 | goto nla_put_failure; |
1690 | return 0; | |
1691 | ||
1692 | nla_put_failure: | |
1693 | return -EMSGSIZE; | |
1694 | } | |
1695 | ||
0b112457 ND |
1696 | static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = { |
1697 | [IFLA_IPTUN_LINK] = { .type = NLA_U32 }, | |
1698 | [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) }, | |
1699 | [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) }, | |
1700 | [IFLA_IPTUN_TTL] = { .type = NLA_U8 }, | |
1701 | [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 }, | |
1702 | [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 }, | |
1703 | [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 }, | |
1704 | [IFLA_IPTUN_PROTO] = { .type = NLA_U8 }, | |
1705 | }; | |
1706 | ||
c075b130 ND |
1707 | static struct rtnl_link_ops ip6_link_ops __read_mostly = { |
1708 | .kind = "ip6tnl", | |
1709 | .maxtype = IFLA_IPTUN_MAX, | |
0b112457 | 1710 | .policy = ip6_tnl_policy, |
c075b130 | 1711 | .priv_size = sizeof(struct ip6_tnl), |
0b112457 ND |
1712 | .setup = ip6_tnl_dev_setup, |
1713 | .validate = ip6_tnl_validate, | |
1714 | .newlink = ip6_tnl_newlink, | |
1715 | .changelink = ip6_tnl_changelink, | |
38c963f2 | 1716 | .dellink = ip6_tnl_dellink, |
b58d731a ND |
1717 | .get_size = ip6_tnl_get_size, |
1718 | .fill_info = ip6_tnl_fill_info, | |
c075b130 ND |
1719 | }; |
1720 | ||
3ff2cfa5 | 1721 | static struct xfrm6_tunnel ip4ip6_handler __read_mostly = { |
c4d3efaf YK |
1722 | .handler = ip4ip6_rcv, |
1723 | .err_handler = ip4ip6_err, | |
1724 | .priority = 1, | |
1725 | }; | |
1726 | ||
3ff2cfa5 | 1727 | static struct xfrm6_tunnel ip6ip6_handler __read_mostly = { |
0303770d PM |
1728 | .handler = ip6ip6_rcv, |
1729 | .err_handler = ip6ip6_err, | |
d2acc347 | 1730 | .priority = 1, |
1da177e4 LT |
1731 | }; |
1732 | ||
2c8c1e72 | 1733 | static void __net_exit ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n) |
3e6c9fb5 PE |
1734 | { |
1735 | int h; | |
1736 | struct ip6_tnl *t; | |
cf4432f5 | 1737 | LIST_HEAD(list); |
3e6c9fb5 PE |
1738 | |
1739 | for (h = 0; h < HASH_SIZE; h++) { | |
94767632 | 1740 | t = rtnl_dereference(ip6n->tnls_r_l[h]); |
cf4432f5 ED |
1741 | while (t != NULL) { |
1742 | unregister_netdevice_queue(t->dev, &list); | |
94767632 | 1743 | t = rtnl_dereference(t->next); |
cf4432f5 | 1744 | } |
3e6c9fb5 PE |
1745 | } |
1746 | ||
89ed31c6 ND |
1747 | t = rtnl_dereference(ip6n->tnls_wc[0]); |
1748 | unregister_netdevice_queue(t->dev, &list); | |
cf4432f5 | 1749 | unregister_netdevice_many(&list); |
3e6c9fb5 PE |
1750 | } |
1751 | ||
2c8c1e72 | 1752 | static int __net_init ip6_tnl_init_net(struct net *net) |
13eeb8e9 | 1753 | { |
ac31cd3c | 1754 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
731abb9c | 1755 | struct ip6_tnl *t = NULL; |
13eeb8e9 | 1756 | int err; |
13eeb8e9 | 1757 | |
3e6c9fb5 PE |
1758 | ip6n->tnls[0] = ip6n->tnls_wc; |
1759 | ip6n->tnls[1] = ip6n->tnls_r_l; | |
1760 | ||
15820e12 PE |
1761 | err = -ENOMEM; |
1762 | ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0", | |
1763 | ip6_tnl_dev_setup); | |
1764 | ||
1765 | if (!ip6n->fb_tnl_dev) | |
1766 | goto err_alloc_dev; | |
be77e593 | 1767 | dev_net_set(ip6n->fb_tnl_dev, net); |
506cdb89 | 1768 | ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops; |
15820e12 | 1769 | |
8560f226 ED |
1770 | err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev); |
1771 | if (err < 0) | |
1772 | goto err_register; | |
15820e12 PE |
1773 | |
1774 | err = register_netdev(ip6n->fb_tnl_dev); | |
1775 | if (err < 0) | |
1776 | goto err_register; | |
731abb9c JB |
1777 | |
1778 | t = netdev_priv(ip6n->fb_tnl_dev); | |
1779 | ||
1780 | strcpy(t->parms.name, ip6n->fb_tnl_dev->name); | |
13eeb8e9 PE |
1781 | return 0; |
1782 | ||
15820e12 | 1783 | err_register: |
8560f226 | 1784 | ip6_dev_free(ip6n->fb_tnl_dev); |
15820e12 | 1785 | err_alloc_dev: |
13eeb8e9 PE |
1786 | return err; |
1787 | } | |
1788 | ||
2c8c1e72 | 1789 | static void __net_exit ip6_tnl_exit_net(struct net *net) |
13eeb8e9 | 1790 | { |
ac31cd3c | 1791 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
13eeb8e9 | 1792 | |
3e6c9fb5 PE |
1793 | rtnl_lock(); |
1794 | ip6_tnl_destroy_tunnels(ip6n); | |
1795 | rtnl_unlock(); | |
13eeb8e9 PE |
1796 | } |
1797 | ||
1798 | static struct pernet_operations ip6_tnl_net_ops = { | |
1799 | .init = ip6_tnl_init_net, | |
1800 | .exit = ip6_tnl_exit_net, | |
ac31cd3c EB |
1801 | .id = &ip6_tnl_net_id, |
1802 | .size = sizeof(struct ip6_tnl_net), | |
13eeb8e9 PE |
1803 | }; |
1804 | ||
1da177e4 LT |
1805 | /** |
1806 | * ip6_tunnel_init - register protocol and reserve needed resources | |
1807 | * | |
1808 | * Return: 0 on success | |
1809 | **/ | |
1810 | ||
1811 | static int __init ip6_tunnel_init(void) | |
1812 | { | |
1813 | int err; | |
1814 | ||
d5aa407f AD |
1815 | err = register_pernet_device(&ip6_tnl_net_ops); |
1816 | if (err < 0) | |
1817 | goto out_pernet; | |
1818 | ||
1819 | err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET); | |
1820 | if (err < 0) { | |
f3213831 | 1821 | pr_err("%s: can't register ip4ip6\n", __func__); |
d5aa407f | 1822 | goto out_ip4ip6; |
c4d3efaf YK |
1823 | } |
1824 | ||
d5aa407f AD |
1825 | err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6); |
1826 | if (err < 0) { | |
f3213831 | 1827 | pr_err("%s: can't register ip6ip6\n", __func__); |
d5aa407f | 1828 | goto out_ip6ip6; |
1da177e4 | 1829 | } |
c075b130 ND |
1830 | err = rtnl_link_register(&ip6_link_ops); |
1831 | if (err < 0) | |
1832 | goto rtnl_link_failed; | |
13eeb8e9 | 1833 | |
1da177e4 | 1834 | return 0; |
d5aa407f | 1835 | |
c075b130 ND |
1836 | rtnl_link_failed: |
1837 | xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6); | |
d5aa407f | 1838 | out_ip6ip6: |
c4d3efaf | 1839 | xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET); |
d5aa407f AD |
1840 | out_ip4ip6: |
1841 | unregister_pernet_device(&ip6_tnl_net_ops); | |
1842 | out_pernet: | |
1da177e4 LT |
1843 | return err; |
1844 | } | |
1845 | ||
1846 | /** | |
1847 | * ip6_tunnel_cleanup - free resources and unregister protocol | |
1848 | **/ | |
1849 | ||
1850 | static void __exit ip6_tunnel_cleanup(void) | |
1851 | { | |
c075b130 | 1852 | rtnl_link_unregister(&ip6_link_ops); |
c4d3efaf | 1853 | if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET)) |
f3213831 | 1854 | pr_info("%s: can't deregister ip4ip6\n", __func__); |
c4d3efaf | 1855 | |
73d605d1 | 1856 | if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6)) |
f3213831 | 1857 | pr_info("%s: can't deregister ip6ip6\n", __func__); |
1da177e4 | 1858 | |
ac31cd3c | 1859 | unregister_pernet_device(&ip6_tnl_net_ops); |
1da177e4 LT |
1860 | } |
1861 | ||
1862 | module_init(ip6_tunnel_init); | |
1863 | module_exit(ip6_tunnel_cleanup); |