Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 | 2 | /* |
c4d3efaf | 3 | * IPv6 tunneling device |
1da177e4 LT |
4 | * Linux INET6 implementation |
5 | * | |
6 | * Authors: | |
1ab1457c | 7 | * Ville Nuorvala <vnuorval@tcs.hut.fi> |
c4d3efaf | 8 | * Yasuyuki Kozakai <kozakai@linux-ipv6.org> |
1da177e4 | 9 | * |
1da177e4 | 10 | * Based on: |
c4d3efaf | 11 | * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c |
1da177e4 LT |
12 | * |
13 | * RFC 2473 | |
1da177e4 LT |
14 | */ |
15 | ||
f3213831 JP |
16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
17 | ||
1da177e4 | 18 | #include <linux/module.h> |
4fc268d2 | 19 | #include <linux/capability.h> |
1da177e4 LT |
20 | #include <linux/errno.h> |
21 | #include <linux/types.h> | |
22 | #include <linux/sockios.h> | |
c4d3efaf | 23 | #include <linux/icmp.h> |
1da177e4 LT |
24 | #include <linux/if.h> |
25 | #include <linux/in.h> | |
26 | #include <linux/ip.h> | |
1da177e4 LT |
27 | #include <linux/net.h> |
28 | #include <linux/in6.h> | |
29 | #include <linux/netdevice.h> | |
30 | #include <linux/if_arp.h> | |
31 | #include <linux/icmpv6.h> | |
32 | #include <linux/init.h> | |
33 | #include <linux/route.h> | |
34 | #include <linux/rtnetlink.h> | |
35 | #include <linux/netfilter_ipv6.h> | |
5a0e3ad6 | 36 | #include <linux/slab.h> |
ddbe5032 | 37 | #include <linux/hash.h> |
e837735e | 38 | #include <linux/etherdevice.h> |
1da177e4 | 39 | |
7c0f6ba6 | 40 | #include <linux/uaccess.h> |
60063497 | 41 | #include <linux/atomic.h> |
1da177e4 | 42 | |
c4d3efaf | 43 | #include <net/icmp.h> |
1da177e4 | 44 | #include <net/ip.h> |
c5441932 | 45 | #include <net/ip_tunnels.h> |
1da177e4 | 46 | #include <net/ipv6.h> |
1da177e4 LT |
47 | #include <net/ip6_route.h> |
48 | #include <net/addrconf.h> | |
49 | #include <net/ip6_tunnel.h> | |
50 | #include <net/xfrm.h> | |
51 | #include <net/dsfield.h> | |
52 | #include <net/inet_ecn.h> | |
13eeb8e9 PE |
53 | #include <net/net_namespace.h> |
54 | #include <net/netns/generic.h> | |
8d79266b | 55 | #include <net/dst_metadata.h> |
1da177e4 LT |
56 | |
57 | MODULE_AUTHOR("Ville Nuorvala"); | |
c4d3efaf | 58 | MODULE_DESCRIPTION("IPv6 tunneling device"); |
1da177e4 | 59 | MODULE_LICENSE("GPL"); |
f98f89a0 | 60 | MODULE_ALIAS_RTNL_LINK("ip6tnl"); |
6dfbd87a | 61 | MODULE_ALIAS_NETDEV("ip6tnl0"); |
1da177e4 | 62 | |
e87a8f24 JK |
63 | #define IP6_TUNNEL_HASH_SIZE_SHIFT 5 |
64 | #define IP6_TUNNEL_HASH_SIZE (1 << IP6_TUNNEL_HASH_SIZE_SHIFT) | |
1da177e4 | 65 | |
f4e0b4c5 ND |
66 | static bool log_ecn_error = true; |
67 | module_param(log_ecn_error, bool, 0644); | |
68 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |
69 | ||
ddbe5032 ED |
70 | static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2) |
71 | { | |
72 | u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2); | |
73 | ||
e87a8f24 | 74 | return hash_32(hash, IP6_TUNNEL_HASH_SIZE_SHIFT); |
ddbe5032 | 75 | } |
1da177e4 | 76 | |
8560f226 | 77 | static int ip6_tnl_dev_init(struct net_device *dev); |
3144581c | 78 | static void ip6_tnl_dev_setup(struct net_device *dev); |
c075b130 | 79 | static struct rtnl_link_ops ip6_link_ops __read_mostly; |
1da177e4 | 80 | |
c7d03a00 | 81 | static unsigned int ip6_tnl_net_id __read_mostly; |
13eeb8e9 | 82 | struct ip6_tnl_net { |
15820e12 PE |
83 | /* the IPv6 tunnel fallback device */ |
84 | struct net_device *fb_tnl_dev; | |
3e6c9fb5 | 85 | /* lists for storing tunnels in use */ |
e87a8f24 | 86 | struct ip6_tnl __rcu *tnls_r_l[IP6_TUNNEL_HASH_SIZE]; |
94767632 ED |
87 | struct ip6_tnl __rcu *tnls_wc[1]; |
88 | struct ip6_tnl __rcu **tnls[2]; | |
8d79266b | 89 | struct ip6_tnl __rcu *collect_md_tun; |
13eeb8e9 PE |
90 | }; |
91 | ||
f200e98d VF |
92 | static inline int ip6_tnl_mpls_supported(void) |
93 | { | |
94 | return IS_ENABLED(CONFIG_MPLS); | |
95 | } | |
96 | ||
8560f226 ED |
97 | static struct net_device_stats *ip6_get_stats(struct net_device *dev) |
98 | { | |
56a4342d | 99 | struct pcpu_sw_netstats tmp, sum = { 0 }; |
8560f226 ED |
100 | int i; |
101 | ||
102 | for_each_possible_cpu(i) { | |
abb6013c | 103 | unsigned int start; |
8f84985f LR |
104 | const struct pcpu_sw_netstats *tstats = |
105 | per_cpu_ptr(dev->tstats, i); | |
8560f226 | 106 | |
abb6013c | 107 | do { |
57a7744e | 108 | start = u64_stats_fetch_begin_irq(&tstats->syncp); |
abb6013c LR |
109 | tmp.rx_packets = tstats->rx_packets; |
110 | tmp.rx_bytes = tstats->rx_bytes; | |
111 | tmp.tx_packets = tstats->tx_packets; | |
112 | tmp.tx_bytes = tstats->tx_bytes; | |
57a7744e | 113 | } while (u64_stats_fetch_retry_irq(&tstats->syncp, start)); |
abb6013c LR |
114 | |
115 | sum.rx_packets += tmp.rx_packets; | |
116 | sum.rx_bytes += tmp.rx_bytes; | |
117 | sum.tx_packets += tmp.tx_packets; | |
118 | sum.tx_bytes += tmp.tx_bytes; | |
8560f226 ED |
119 | } |
120 | dev->stats.rx_packets = sum.rx_packets; | |
121 | dev->stats.rx_bytes = sum.rx_bytes; | |
122 | dev->stats.tx_packets = sum.tx_packets; | |
123 | dev->stats.tx_bytes = sum.tx_bytes; | |
124 | return &dev->stats; | |
125 | } | |
126 | ||
b51cd7c8 AL |
127 | #define for_each_ip6_tunnel_rcu(start) \ |
128 | for (t = rcu_dereference(start); t; t = rcu_dereference(t->next)) | |
129 | ||
1da177e4 | 130 | /** |
3144581c | 131 | * ip6_tnl_lookup - fetch tunnel matching the end-point addresses |
b51cd7c8 | 132 | * @net: network namespace |
5fdcce21 | 133 | * @link: ifindex of underlying interface |
1ab1457c YH |
134 | * @remote: the address of the tunnel exit-point |
135 | * @local: the address of the tunnel entry-point | |
1da177e4 | 136 | * |
1ab1457c | 137 | * Return: |
1da177e4 | 138 | * tunnel matching given end-points if found, |
1ab1457c | 139 | * else fallback tunnel if its device is up, |
1da177e4 LT |
140 | * else %NULL |
141 | **/ | |
142 | ||
143 | static struct ip6_tnl * | |
5fdcce21 WD |
144 | ip6_tnl_lookup(struct net *net, int link, |
145 | const struct in6_addr *remote, const struct in6_addr *local) | |
1da177e4 | 146 | { |
ddbe5032 | 147 | unsigned int hash = HASH(remote, local); |
5fdcce21 | 148 | struct ip6_tnl *t, *cand = NULL; |
3e6c9fb5 | 149 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
ea3dc960 | 150 | struct in6_addr any; |
1da177e4 | 151 | |
ddbe5032 | 152 | for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { |
5fdcce21 WD |
153 | if (!ipv6_addr_equal(local, &t->parms.laddr) || |
154 | !ipv6_addr_equal(remote, &t->parms.raddr) || | |
155 | !(t->dev->flags & IFF_UP)) | |
156 | continue; | |
157 | ||
158 | if (link == t->parms.link) | |
1da177e4 | 159 | return t; |
5fdcce21 WD |
160 | else |
161 | cand = t; | |
1da177e4 | 162 | } |
ea3dc960 SK |
163 | |
164 | memset(&any, 0, sizeof(any)); | |
165 | hash = HASH(&any, local); | |
166 | for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { | |
5fdcce21 WD |
167 | if (!ipv6_addr_equal(local, &t->parms.laddr) || |
168 | !ipv6_addr_any(&t->parms.raddr) || | |
169 | !(t->dev->flags & IFF_UP)) | |
170 | continue; | |
171 | ||
172 | if (link == t->parms.link) | |
ea3dc960 | 173 | return t; |
5fdcce21 WD |
174 | else if (!cand) |
175 | cand = t; | |
ea3dc960 SK |
176 | } |
177 | ||
178 | hash = HASH(remote, &any); | |
179 | for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { | |
5fdcce21 WD |
180 | if (!ipv6_addr_equal(remote, &t->parms.raddr) || |
181 | !ipv6_addr_any(&t->parms.laddr) || | |
182 | !(t->dev->flags & IFF_UP)) | |
183 | continue; | |
184 | ||
185 | if (link == t->parms.link) | |
ea3dc960 | 186 | return t; |
5fdcce21 WD |
187 | else if (!cand) |
188 | cand = t; | |
ea3dc960 SK |
189 | } |
190 | ||
5fdcce21 WD |
191 | if (cand) |
192 | return cand; | |
193 | ||
8d79266b | 194 | t = rcu_dereference(ip6n->collect_md_tun); |
6c1cb439 | 195 | if (t && t->dev->flags & IFF_UP) |
8d79266b AS |
196 | return t; |
197 | ||
2922bc8a ED |
198 | t = rcu_dereference(ip6n->tnls_wc[0]); |
199 | if (t && (t->dev->flags & IFF_UP)) | |
1da177e4 LT |
200 | return t; |
201 | ||
202 | return NULL; | |
203 | } | |
204 | ||
205 | /** | |
3144581c | 206 | * ip6_tnl_bucket - get head of list matching given tunnel parameters |
1ab1457c | 207 | * @p: parameters containing tunnel end-points |
1da177e4 LT |
208 | * |
209 | * Description: | |
3144581c | 210 | * ip6_tnl_bucket() returns the head of the list matching the |
1da177e4 LT |
211 | * &struct in6_addr entries laddr and raddr in @p. |
212 | * | |
1ab1457c | 213 | * Return: head of IPv6 tunnel list |
1da177e4 LT |
214 | **/ |
215 | ||
94767632 | 216 | static struct ip6_tnl __rcu ** |
c12b395a | 217 | ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p) |
1da177e4 | 218 | { |
b71d1d42 ED |
219 | const struct in6_addr *remote = &p->raddr; |
220 | const struct in6_addr *local = &p->laddr; | |
95c96174 | 221 | unsigned int h = 0; |
1da177e4 LT |
222 | int prio = 0; |
223 | ||
224 | if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) { | |
225 | prio = 1; | |
ddbe5032 | 226 | h = HASH(remote, local); |
1da177e4 | 227 | } |
3e6c9fb5 | 228 | return &ip6n->tnls[prio][h]; |
1da177e4 LT |
229 | } |
230 | ||
231 | /** | |
3144581c | 232 | * ip6_tnl_link - add tunnel to hash table |
1da177e4 LT |
233 | * @t: tunnel to be added |
234 | **/ | |
235 | ||
236 | static void | |
2dd02c89 | 237 | ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t) |
1da177e4 | 238 | { |
94767632 | 239 | struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms); |
1da177e4 | 240 | |
8d79266b AS |
241 | if (t->parms.collect_md) |
242 | rcu_assign_pointer(ip6n->collect_md_tun, t); | |
cf778b00 ED |
243 | rcu_assign_pointer(t->next , rtnl_dereference(*tp)); |
244 | rcu_assign_pointer(*tp, t); | |
1da177e4 LT |
245 | } |
246 | ||
247 | /** | |
3144581c | 248 | * ip6_tnl_unlink - remove tunnel from hash table |
1da177e4 LT |
249 | * @t: tunnel to be removed |
250 | **/ | |
251 | ||
252 | static void | |
2dd02c89 | 253 | ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t) |
1da177e4 | 254 | { |
94767632 ED |
255 | struct ip6_tnl __rcu **tp; |
256 | struct ip6_tnl *iter; | |
257 | ||
8d79266b AS |
258 | if (t->parms.collect_md) |
259 | rcu_assign_pointer(ip6n->collect_md_tun, NULL); | |
260 | ||
94767632 ED |
261 | for (tp = ip6_tnl_bucket(ip6n, &t->parms); |
262 | (iter = rtnl_dereference(*tp)) != NULL; | |
263 | tp = &iter->next) { | |
264 | if (t == iter) { | |
cf778b00 | 265 | rcu_assign_pointer(*tp, t->next); |
1da177e4 LT |
266 | break; |
267 | } | |
268 | } | |
269 | } | |
270 | ||
8560f226 ED |
271 | static void ip6_dev_free(struct net_device *dev) |
272 | { | |
cdf3464e MKL |
273 | struct ip6_tnl *t = netdev_priv(dev); |
274 | ||
0d3c703a | 275 | gro_cells_destroy(&t->gro_cells); |
607f725f | 276 | dst_cache_destroy(&t->dst_cache); |
8560f226 | 277 | free_percpu(dev->tstats); |
8560f226 ED |
278 | } |
279 | ||
0b112457 ND |
280 | static int ip6_tnl_create2(struct net_device *dev) |
281 | { | |
282 | struct ip6_tnl *t = netdev_priv(dev); | |
283 | struct net *net = dev_net(dev); | |
284 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); | |
285 | int err; | |
286 | ||
287 | t = netdev_priv(dev); | |
0b112457 | 288 | |
b6ee376c | 289 | dev->rtnl_link_ops = &ip6_link_ops; |
0b112457 ND |
290 | err = register_netdevice(dev); |
291 | if (err < 0) | |
292 | goto out; | |
293 | ||
294 | strcpy(t->parms.name, dev->name); | |
0b112457 ND |
295 | |
296 | dev_hold(dev); | |
297 | ip6_tnl_link(ip6n, t); | |
298 | return 0; | |
299 | ||
300 | out: | |
301 | return err; | |
302 | } | |
303 | ||
1da177e4 | 304 | /** |
2c53040f | 305 | * ip6_tnl_create - create a new tunnel |
b51cd7c8 | 306 | * @net: network namespace |
1da177e4 | 307 | * @p: tunnel parameters |
1da177e4 LT |
308 | * |
309 | * Description: | |
310 | * Create tunnel matching given parameters. | |
1ab1457c YH |
311 | * |
312 | * Return: | |
37355565 | 313 | * created tunnel or error pointer |
1da177e4 LT |
314 | **/ |
315 | ||
c12b395a | 316 | static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p) |
1da177e4 LT |
317 | { |
318 | struct net_device *dev; | |
319 | struct ip6_tnl *t; | |
320 | char name[IFNAMSIZ]; | |
db7a65e3 | 321 | int err = -E2BIG; |
1da177e4 | 322 | |
db7a65e3 ED |
323 | if (p->name[0]) { |
324 | if (!dev_valid_name(p->name)) | |
325 | goto failed; | |
1da177e4 | 326 | strlcpy(name, p->name, IFNAMSIZ); |
db7a65e3 | 327 | } else { |
34cc7ba6 | 328 | sprintf(name, "ip6tnl%%d"); |
db7a65e3 ED |
329 | } |
330 | err = -ENOMEM; | |
c835a677 TG |
331 | dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, |
332 | ip6_tnl_dev_setup); | |
63159f29 | 333 | if (!dev) |
567131a7 | 334 | goto failed; |
1da177e4 | 335 | |
554eb277 PE |
336 | dev_net_set(dev, net); |
337 | ||
2941a486 | 338 | t = netdev_priv(dev); |
1da177e4 | 339 | t->parms = *p; |
0bd87628 | 340 | t->net = dev_net(dev); |
0b112457 | 341 | err = ip6_tnl_create2(dev); |
8560f226 ED |
342 | if (err < 0) |
343 | goto failed_free; | |
1da177e4 | 344 | |
567131a7 | 345 | return t; |
b37d428b PE |
346 | |
347 | failed_free: | |
cf124db5 | 348 | free_netdev(dev); |
567131a7 | 349 | failed: |
37355565 | 350 | return ERR_PTR(err); |
1da177e4 LT |
351 | } |
352 | ||
353 | /** | |
3144581c | 354 | * ip6_tnl_locate - find or create tunnel matching given parameters |
b51cd7c8 | 355 | * @net: network namespace |
1ab1457c | 356 | * @p: tunnel parameters |
1da177e4 LT |
357 | * @create: != 0 if allowed to create new tunnel if no match found |
358 | * | |
359 | * Description: | |
3144581c | 360 | * ip6_tnl_locate() first tries to locate an existing tunnel |
1da177e4 LT |
361 | * based on @parms. If this is unsuccessful, but @create is set a new |
362 | * tunnel device is created and registered for use. | |
363 | * | |
364 | * Return: | |
37355565 | 365 | * matching tunnel or error pointer |
1da177e4 LT |
366 | **/ |
367 | ||
2dd02c89 | 368 | static struct ip6_tnl *ip6_tnl_locate(struct net *net, |
c12b395a | 369 | struct __ip6_tnl_parm *p, int create) |
1da177e4 | 370 | { |
b71d1d42 ED |
371 | const struct in6_addr *remote = &p->raddr; |
372 | const struct in6_addr *local = &p->laddr; | |
94767632 | 373 | struct ip6_tnl __rcu **tp; |
1da177e4 | 374 | struct ip6_tnl *t; |
2dd02c89 | 375 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
1da177e4 | 376 | |
94767632 ED |
377 | for (tp = ip6_tnl_bucket(ip6n, p); |
378 | (t = rtnl_dereference(*tp)) != NULL; | |
379 | tp = &t->next) { | |
1da177e4 | 380 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
5fdcce21 WD |
381 | ipv6_addr_equal(remote, &t->parms.raddr) && |
382 | p->link == t->parms.link) { | |
2b0bb01b | 383 | if (create) |
37355565 | 384 | return ERR_PTR(-EEXIST); |
2b0bb01b | 385 | |
567131a7 | 386 | return t; |
2b0bb01b | 387 | } |
1da177e4 LT |
388 | } |
389 | if (!create) | |
37355565 | 390 | return ERR_PTR(-ENODEV); |
2dd02c89 | 391 | return ip6_tnl_create(net, p); |
1da177e4 LT |
392 | } |
393 | ||
394 | /** | |
3144581c | 395 | * ip6_tnl_dev_uninit - tunnel device uninitializer |
1da177e4 | 396 | * @dev: the device to be destroyed |
1ab1457c | 397 | * |
1da177e4 | 398 | * Description: |
3144581c | 399 | * ip6_tnl_dev_uninit() removes tunnel from its list |
1da177e4 LT |
400 | **/ |
401 | ||
402 | static void | |
3144581c | 403 | ip6_tnl_dev_uninit(struct net_device *dev) |
1da177e4 | 404 | { |
2941a486 | 405 | struct ip6_tnl *t = netdev_priv(dev); |
0bd87628 | 406 | struct net *net = t->net; |
2dd02c89 | 407 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
1da177e4 | 408 | |
94767632 | 409 | if (dev == ip6n->fb_tnl_dev) |
a9b3cd7f | 410 | RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL); |
94767632 | 411 | else |
2dd02c89 | 412 | ip6_tnl_unlink(ip6n, t); |
607f725f | 413 | dst_cache_reset(&t->dst_cache); |
1da177e4 LT |
414 | dev_put(dev); |
415 | } | |
416 | ||
417 | /** | |
418 | * parse_tvl_tnl_enc_lim - handle encapsulation limit option | |
419 | * @skb: received socket buffer | |
420 | * | |
1ab1457c YH |
421 | * Return: |
422 | * 0 if none was found, | |
1da177e4 LT |
423 | * else index to encapsulation limit |
424 | **/ | |
425 | ||
c12b395a | 426 | __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw) |
1da177e4 | 427 | { |
fbfa743a ED |
428 | const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw; |
429 | unsigned int nhoff = raw - skb->data; | |
430 | unsigned int off = nhoff + sizeof(*ipv6h); | |
431 | u8 next, nexthdr = ipv6h->nexthdr; | |
1da177e4 LT |
432 | |
433 | while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) { | |
1da177e4 | 434 | struct ipv6_opt_hdr *hdr; |
fbfa743a ED |
435 | u16 optlen; |
436 | ||
437 | if (!pskb_may_pull(skb, off + sizeof(*hdr))) | |
1da177e4 LT |
438 | break; |
439 | ||
fbfa743a | 440 | hdr = (struct ipv6_opt_hdr *)(skb->data + off); |
1da177e4 LT |
441 | if (nexthdr == NEXTHDR_FRAGMENT) { |
442 | struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr; | |
443 | if (frag_hdr->frag_off) | |
444 | break; | |
445 | optlen = 8; | |
446 | } else if (nexthdr == NEXTHDR_AUTH) { | |
416e8126 | 447 | optlen = ipv6_authlen(hdr); |
1da177e4 LT |
448 | } else { |
449 | optlen = ipv6_optlen(hdr); | |
450 | } | |
fbfa743a ED |
451 | /* cache hdr->nexthdr, since pskb_may_pull() might |
452 | * invalidate hdr | |
453 | */ | |
454 | next = hdr->nexthdr; | |
1da177e4 | 455 | if (nexthdr == NEXTHDR_DEST) { |
fbfa743a ED |
456 | u16 i = 2; |
457 | ||
458 | /* Remember : hdr is no longer valid at this point. */ | |
459 | if (!pskb_may_pull(skb, off + optlen)) | |
460 | break; | |
461 | ||
1da177e4 LT |
462 | while (1) { |
463 | struct ipv6_tlv_tnl_enc_lim *tel; | |
464 | ||
465 | /* No more room for encapsulation limit */ | |
fbfa743a | 466 | if (i + sizeof(*tel) > optlen) |
1da177e4 LT |
467 | break; |
468 | ||
63117f09 | 469 | tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i); |
1da177e4 LT |
470 | /* return index of option if found and valid */ |
471 | if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT && | |
472 | tel->length == 1) | |
fbfa743a | 473 | return i + off - nhoff; |
1da177e4 LT |
474 | /* else jump to next option */ |
475 | if (tel->type) | |
476 | i += tel->length + 2; | |
477 | else | |
478 | i++; | |
479 | } | |
480 | } | |
fbfa743a | 481 | nexthdr = next; |
1da177e4 LT |
482 | off += optlen; |
483 | } | |
484 | return 0; | |
485 | } | |
c12b395a | 486 | EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim); |
1da177e4 LT |
487 | |
488 | /** | |
e490d1d8 | 489 | * ip6_tnl_err - tunnel error handler |
1da177e4 LT |
490 | * |
491 | * Description: | |
e490d1d8 | 492 | * ip6_tnl_err() should handle errors in the tunnel according |
1da177e4 LT |
493 | * to the specifications in RFC 2473. |
494 | **/ | |
495 | ||
d2acc347 | 496 | static int |
502b0935 | 497 | ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt, |
d5fdd6ba | 498 | u8 *type, u8 *code, int *msg, __u32 *info, int offset) |
1da177e4 | 499 | { |
383c1f88 XL |
500 | const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data; |
501 | struct net *net = dev_net(skb->dev); | |
d5fdd6ba BH |
502 | u8 rel_type = ICMPV6_DEST_UNREACH; |
503 | u8 rel_code = ICMPV6_ADDR_UNREACH; | |
1da177e4 | 504 | __u32 rel_info = 0; |
383c1f88 | 505 | struct ip6_tnl *t; |
d2acc347 | 506 | int err = -ENOENT; |
383c1f88 XL |
507 | int rel_msg = 0; |
508 | u8 tproto; | |
509 | __u16 len; | |
1da177e4 | 510 | |
1ab1457c YH |
511 | /* If the packet doesn't contain the original IPv6 header we are |
512 | in trouble since we might need the source address for further | |
1da177e4 LT |
513 | processing of the error. */ |
514 | ||
2922bc8a | 515 | rcu_read_lock(); |
5fdcce21 | 516 | t = ip6_tnl_lookup(dev_net(skb->dev), skb->dev->ifindex, &ipv6h->daddr, &ipv6h->saddr); |
63159f29 | 517 | if (!t) |
1da177e4 LT |
518 | goto out; |
519 | ||
6aa7de05 | 520 | tproto = READ_ONCE(t->parms.proto); |
acf722f7 | 521 | if (tproto != ipproto && tproto != 0) |
502b0935 YK |
522 | goto out; |
523 | ||
d2acc347 HX |
524 | err = 0; |
525 | ||
e490d1d8 | 526 | switch (*type) { |
1da177e4 | 527 | case ICMPV6_DEST_UNREACH: |
17a10c92 MB |
528 | net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n", |
529 | t->parms.name); | |
1da177e4 LT |
530 | rel_msg = 1; |
531 | break; | |
532 | case ICMPV6_TIME_EXCEED: | |
e490d1d8 | 533 | if ((*code) == ICMPV6_EXC_HOPLIMIT) { |
17a10c92 MB |
534 | net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n", |
535 | t->parms.name); | |
1da177e4 LT |
536 | rel_msg = 1; |
537 | } | |
538 | break; | |
46d30cb1 KC |
539 | case ICMPV6_PARAMPROB: { |
540 | struct ipv6_tlv_tnl_enc_lim *tel; | |
541 | __u32 teli; | |
542 | ||
107a5fe6 | 543 | teli = 0; |
e490d1d8 | 544 | if ((*code) == ICMPV6_HDR_FIELD) |
c12b395a | 545 | teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data); |
1da177e4 | 546 | |
704eae1f | 547 | if (teli && teli == *info - 2) { |
1da177e4 LT |
548 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; |
549 | if (tel->encap_limit == 0) { | |
17a10c92 MB |
550 | net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n", |
551 | t->parms.name); | |
1da177e4 LT |
552 | rel_msg = 1; |
553 | } | |
e87cc472 | 554 | } else { |
17a10c92 MB |
555 | net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n", |
556 | t->parms.name); | |
1da177e4 LT |
557 | } |
558 | break; | |
46d30cb1 KC |
559 | } |
560 | case ICMPV6_PKT_TOOBIG: { | |
561 | __u32 mtu; | |
562 | ||
b00f5432 XL |
563 | ip6_update_pmtu(skb, net, htonl(*info), 0, 0, |
564 | sock_net_uid(net, NULL)); | |
704eae1f | 565 | mtu = *info - offset; |
1da177e4 LT |
566 | if (mtu < IPV6_MIN_MTU) |
567 | mtu = IPV6_MIN_MTU; | |
e5d08d71 IM |
568 | len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len); |
569 | if (len > mtu) { | |
1da177e4 LT |
570 | rel_type = ICMPV6_PKT_TOOBIG; |
571 | rel_code = 0; | |
572 | rel_info = mtu; | |
573 | rel_msg = 1; | |
574 | } | |
575 | break; | |
46d30cb1 | 576 | } |
383c1f88 XL |
577 | case NDISC_REDIRECT: |
578 | ip6_redirect(skb, net, skb->dev->ifindex, 0, | |
579 | sock_net_uid(net, NULL)); | |
580 | break; | |
1da177e4 | 581 | } |
e490d1d8 YK |
582 | |
583 | *type = rel_type; | |
584 | *code = rel_code; | |
585 | *info = rel_info; | |
586 | *msg = rel_msg; | |
587 | ||
588 | out: | |
2922bc8a | 589 | rcu_read_unlock(); |
e490d1d8 YK |
590 | return err; |
591 | } | |
592 | ||
c4d3efaf YK |
593 | static int |
594 | ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
d5fdd6ba | 595 | u8 type, u8 code, int offset, __be32 info) |
c4d3efaf | 596 | { |
704eae1f | 597 | __u32 rel_info = ntohl(info); |
b71d1d42 | 598 | const struct iphdr *eiph; |
77552cfa XL |
599 | struct sk_buff *skb2; |
600 | int err, rel_msg = 0; | |
601 | u8 rel_type = type; | |
602 | u8 rel_code = code; | |
c4d3efaf | 603 | struct rtable *rt; |
31e4543d | 604 | struct flowi4 fl4; |
c4d3efaf | 605 | |
502b0935 YK |
606 | err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code, |
607 | &rel_msg, &rel_info, offset); | |
c4d3efaf YK |
608 | if (err < 0) |
609 | return err; | |
610 | ||
611 | if (rel_msg == 0) | |
612 | return 0; | |
613 | ||
614 | switch (rel_type) { | |
615 | case ICMPV6_DEST_UNREACH: | |
616 | if (rel_code != ICMPV6_ADDR_UNREACH) | |
617 | return 0; | |
618 | rel_type = ICMP_DEST_UNREACH; | |
619 | rel_code = ICMP_HOST_UNREACH; | |
620 | break; | |
621 | case ICMPV6_PKT_TOOBIG: | |
622 | if (rel_code != 0) | |
623 | return 0; | |
624 | rel_type = ICMP_DEST_UNREACH; | |
625 | rel_code = ICMP_FRAG_NEEDED; | |
626 | break; | |
627 | default: | |
628 | return 0; | |
629 | } | |
630 | ||
631 | if (!pskb_may_pull(skb, offset + sizeof(struct iphdr))) | |
632 | return 0; | |
633 | ||
634 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
635 | if (!skb2) | |
636 | return 0; | |
637 | ||
adf30907 ED |
638 | skb_dst_drop(skb2); |
639 | ||
c4d3efaf | 640 | skb_pull(skb2, offset); |
c1d2bbe1 | 641 | skb_reset_network_header(skb2); |
eddc9ec5 | 642 | eiph = ip_hdr(skb2); |
c4d3efaf YK |
643 | |
644 | /* Try to guess incoming interface */ | |
77552cfa XL |
645 | rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, eiph->saddr, |
646 | 0, 0, 0, IPPROTO_IPIP, RT_TOS(eiph->tos), 0); | |
b23dd4fe | 647 | if (IS_ERR(rt)) |
c4d3efaf YK |
648 | goto out; |
649 | ||
d8d1f30b | 650 | skb2->dev = rt->dst.dev; |
77552cfa | 651 | ip_rt_put(rt); |
c4d3efaf YK |
652 | |
653 | /* route "incoming" packet */ | |
654 | if (rt->rt_flags & RTCF_LOCAL) { | |
31e4543d | 655 | rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, |
77552cfa XL |
656 | eiph->daddr, eiph->saddr, 0, 0, |
657 | IPPROTO_IPIP, RT_TOS(eiph->tos), 0); | |
b2e54b09 | 658 | if (IS_ERR(rt) || rt->dst.dev->type != ARPHRD_TUNNEL6) { |
b23dd4fe DM |
659 | if (!IS_ERR(rt)) |
660 | ip_rt_put(rt); | |
c4d3efaf YK |
661 | goto out; |
662 | } | |
b23dd4fe | 663 | skb_dst_set(skb2, &rt->dst); |
c4d3efaf | 664 | } else { |
c4d3efaf YK |
665 | if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos, |
666 | skb2->dev) || | |
b2e54b09 | 667 | skb_dst(skb2)->dev->type != ARPHRD_TUNNEL6) |
c4d3efaf YK |
668 | goto out; |
669 | } | |
670 | ||
671 | /* change mtu on this route */ | |
672 | if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) { | |
adf30907 | 673 | if (rel_info > dst_mtu(skb_dst(skb2))) |
c4d3efaf YK |
674 | goto out; |
675 | ||
7a1592bc | 676 | skb_dst_update_pmtu_no_confirm(skb2, rel_info); |
c4d3efaf YK |
677 | } |
678 | ||
704eae1f | 679 | icmp_send(skb2, rel_type, rel_code, htonl(rel_info)); |
c4d3efaf YK |
680 | |
681 | out: | |
682 | kfree_skb(skb2); | |
683 | return 0; | |
684 | } | |
685 | ||
e490d1d8 YK |
686 | static int |
687 | ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
d5fdd6ba | 688 | u8 type, u8 code, int offset, __be32 info) |
e490d1d8 | 689 | { |
77552cfa XL |
690 | __u32 rel_info = ntohl(info); |
691 | int err, rel_msg = 0; | |
d5fdd6ba BH |
692 | u8 rel_type = type; |
693 | u8 rel_code = code; | |
e490d1d8 | 694 | |
502b0935 YK |
695 | err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code, |
696 | &rel_msg, &rel_info, offset); | |
e490d1d8 YK |
697 | if (err < 0) |
698 | return err; | |
699 | ||
700 | if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) { | |
1da177e4 LT |
701 | struct rt6_info *rt; |
702 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); | |
305d4b3c | 703 | |
1da177e4 | 704 | if (!skb2) |
e490d1d8 | 705 | return 0; |
1da177e4 | 706 | |
adf30907 | 707 | skb_dst_drop(skb2); |
1da177e4 | 708 | skb_pull(skb2, offset); |
c1d2bbe1 | 709 | skb_reset_network_header(skb2); |
1da177e4 LT |
710 | |
711 | /* Try to guess incoming interface */ | |
2f7f54b7 | 712 | rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, |
b75cc8f9 | 713 | NULL, 0, skb2, 0); |
1da177e4 | 714 | |
d1918542 DM |
715 | if (rt && rt->dst.dev) |
716 | skb2->dev = rt->dst.dev; | |
1da177e4 | 717 | |
3ffe533c | 718 | icmpv6_send(skb2, rel_type, rel_code, rel_info); |
1da177e4 | 719 | |
94e187c0 | 720 | ip6_rt_put(rt); |
1da177e4 LT |
721 | |
722 | kfree_skb(skb2); | |
723 | } | |
e490d1d8 YK |
724 | |
725 | return 0; | |
1da177e4 LT |
726 | } |
727 | ||
f200e98d VF |
728 | static int |
729 | mplsip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
730 | u8 type, u8 code, int offset, __be32 info) | |
731 | { | |
732 | __u32 rel_info = ntohl(info); | |
733 | int err, rel_msg = 0; | |
734 | u8 rel_type = type; | |
735 | u8 rel_code = code; | |
736 | ||
737 | err = ip6_tnl_err(skb, IPPROTO_MPLS, opt, &rel_type, &rel_code, | |
738 | &rel_msg, &rel_info, offset); | |
739 | return err; | |
740 | } | |
741 | ||
f4e0b4c5 ND |
742 | static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, |
743 | const struct ipv6hdr *ipv6h, | |
744 | struct sk_buff *skb) | |
c4d3efaf YK |
745 | { |
746 | __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK; | |
747 | ||
748 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) | |
eddc9ec5 | 749 | ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield); |
c4d3efaf | 750 | |
f4e0b4c5 | 751 | return IP6_ECN_decapsulate(ipv6h, skb); |
c4d3efaf YK |
752 | } |
753 | ||
f4e0b4c5 ND |
754 | static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, |
755 | const struct ipv6hdr *ipv6h, | |
756 | struct sk_buff *skb) | |
1da177e4 | 757 | { |
8359925b | 758 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) |
29bb43b4 | 759 | ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb)); |
1da177e4 | 760 | |
f4e0b4c5 | 761 | return IP6_ECN_decapsulate(ipv6h, skb); |
1da177e4 | 762 | } |
8359925b | 763 | |
f200e98d VF |
764 | static inline int mplsip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, |
765 | const struct ipv6hdr *ipv6h, | |
766 | struct sk_buff *skb) | |
767 | { | |
768 | /* ECN is not supported in AF_MPLS */ | |
769 | return 0; | |
770 | } | |
771 | ||
c12b395a | 772 | __u32 ip6_tnl_get_cap(struct ip6_tnl *t, |
d0087b29 VN |
773 | const struct in6_addr *laddr, |
774 | const struct in6_addr *raddr) | |
775 | { | |
c12b395a | 776 | struct __ip6_tnl_parm *p = &t->parms; |
d0087b29 VN |
777 | int ltype = ipv6_addr_type(laddr); |
778 | int rtype = ipv6_addr_type(raddr); | |
779 | __u32 flags = 0; | |
780 | ||
781 | if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) { | |
782 | flags = IP6_TNL_F_CAP_PER_PACKET; | |
783 | } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) && | |
784 | rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) && | |
785 | !((ltype|rtype) & IPV6_ADDR_LOOPBACK) && | |
786 | (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) { | |
787 | if (ltype&IPV6_ADDR_UNICAST) | |
788 | flags |= IP6_TNL_F_CAP_XMIT; | |
789 | if (rtype&IPV6_ADDR_UNICAST) | |
790 | flags |= IP6_TNL_F_CAP_RCV; | |
791 | } | |
792 | return flags; | |
793 | } | |
c12b395a | 794 | EXPORT_SYMBOL(ip6_tnl_get_cap); |
d0087b29 | 795 | |
f1a28eab | 796 | /* called with rcu_read_lock() */ |
c12b395a | 797 | int ip6_tnl_rcv_ctl(struct ip6_tnl *t, |
d0087b29 VN |
798 | const struct in6_addr *laddr, |
799 | const struct in6_addr *raddr) | |
09c6bbf0 | 800 | { |
c12b395a | 801 | struct __ip6_tnl_parm *p = &t->parms; |
09c6bbf0 | 802 | int ret = 0; |
0bd87628 | 803 | struct net *net = t->net; |
09c6bbf0 | 804 | |
d0087b29 VN |
805 | if ((p->flags & IP6_TNL_F_CAP_RCV) || |
806 | ((p->flags & IP6_TNL_F_CAP_PER_PACKET) && | |
807 | (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) { | |
1ab1457c | 808 | struct net_device *ldev = NULL; |
09c6bbf0 VN |
809 | |
810 | if (p->link) | |
f1a28eab | 811 | ldev = dev_get_by_index_rcu(net, p->link); |
09c6bbf0 | 812 | |
d0087b29 | 813 | if ((ipv6_addr_is_multicast(laddr) || |
232378e8 DA |
814 | likely(ipv6_chk_addr_and_flags(net, laddr, ldev, false, |
815 | 0, IFA_F_TENTATIVE))) && | |
908d140a | 816 | ((p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) || |
232378e8 DA |
817 | likely(!ipv6_chk_addr_and_flags(net, raddr, ldev, true, |
818 | 0, IFA_F_TENTATIVE)))) | |
09c6bbf0 | 819 | ret = 1; |
09c6bbf0 VN |
820 | } |
821 | return ret; | |
822 | } | |
c12b395a | 823 | EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl); |
1da177e4 | 824 | |
0d3c703a TH |
825 | static int __ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb, |
826 | const struct tnl_ptk_info *tpi, | |
827 | struct metadata_dst *tun_dst, | |
828 | int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t, | |
829 | const struct ipv6hdr *ipv6h, | |
830 | struct sk_buff *skb), | |
831 | bool log_ecn_err) | |
1da177e4 | 832 | { |
0d3c703a | 833 | struct pcpu_sw_netstats *tstats; |
b71d1d42 | 834 | const struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
f4e0b4c5 | 835 | int err; |
1da177e4 | 836 | |
0d3c703a TH |
837 | if ((!(tpi->flags & TUNNEL_CSUM) && |
838 | (tunnel->parms.i_flags & TUNNEL_CSUM)) || | |
839 | ((tpi->flags & TUNNEL_CSUM) && | |
840 | !(tunnel->parms.i_flags & TUNNEL_CSUM))) { | |
841 | tunnel->dev->stats.rx_crc_errors++; | |
842 | tunnel->dev->stats.rx_errors++; | |
843 | goto drop; | |
844 | } | |
8560f226 | 845 | |
0d3c703a TH |
846 | if (tunnel->parms.i_flags & TUNNEL_SEQ) { |
847 | if (!(tpi->flags & TUNNEL_SEQ) || | |
848 | (tunnel->i_seqno && | |
849 | (s32)(ntohl(tpi->seq) - tunnel->i_seqno) < 0)) { | |
850 | tunnel->dev->stats.rx_fifo_errors++; | |
851 | tunnel->dev->stats.rx_errors++; | |
852 | goto drop; | |
502b0935 | 853 | } |
0d3c703a TH |
854 | tunnel->i_seqno = ntohl(tpi->seq) + 1; |
855 | } | |
502b0935 | 856 | |
0d3c703a | 857 | skb->protocol = tpi->proto; |
1da177e4 | 858 | |
0d3c703a TH |
859 | /* Warning: All skb pointers will be invalidated! */ |
860 | if (tunnel->dev->type == ARPHRD_ETHER) { | |
861 | if (!pskb_may_pull(skb, ETH_HLEN)) { | |
862 | tunnel->dev->stats.rx_length_errors++; | |
863 | tunnel->dev->stats.rx_errors++; | |
864 | goto drop; | |
1da177e4 | 865 | } |
0d3c703a TH |
866 | |
867 | ipv6h = ipv6_hdr(skb); | |
868 | skb->protocol = eth_type_trans(skb, tunnel->dev); | |
869 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); | |
870 | } else { | |
871 | skb->dev = tunnel->dev; | |
872 | } | |
873 | ||
874 | skb_reset_network_header(skb); | |
875 | memset(skb->cb, 0, sizeof(struct inet6_skb_parm)); | |
876 | ||
877 | __skb_tunnel_rx(skb, tunnel->dev, tunnel->net); | |
878 | ||
879 | err = dscp_ecn_decapsulate(tunnel, ipv6h, skb); | |
880 | if (unlikely(err)) { | |
881 | if (log_ecn_err) | |
882 | net_info_ratelimited("non-ECT from %pI6 with DS=%#x\n", | |
883 | &ipv6h->saddr, | |
884 | ipv6_get_dsfield(ipv6h)); | |
885 | if (err > 1) { | |
886 | ++tunnel->dev->stats.rx_frame_errors; | |
887 | ++tunnel->dev->stats.rx_errors; | |
888 | goto drop; | |
f4e0b4c5 | 889 | } |
0d3c703a | 890 | } |
f4e0b4c5 | 891 | |
0d3c703a TH |
892 | tstats = this_cpu_ptr(tunnel->dev->tstats); |
893 | u64_stats_update_begin(&tstats->syncp); | |
894 | tstats->rx_packets++; | |
895 | tstats->rx_bytes += skb->len; | |
896 | u64_stats_update_end(&tstats->syncp); | |
8560f226 | 897 | |
0d3c703a | 898 | skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev))); |
8990f468 | 899 | |
8d79266b AS |
900 | if (tun_dst) |
901 | skb_dst_set(skb, (struct dst_entry *)tun_dst); | |
902 | ||
0d3c703a TH |
903 | gro_cells_receive(&tunnel->gro_cells, skb); |
904 | return 0; | |
905 | ||
906 | drop: | |
f1925ca5 HY |
907 | if (tun_dst) |
908 | dst_release((struct dst_entry *)tun_dst); | |
0d3c703a TH |
909 | kfree_skb(skb); |
910 | return 0; | |
911 | } | |
912 | ||
913 | int ip6_tnl_rcv(struct ip6_tnl *t, struct sk_buff *skb, | |
914 | const struct tnl_ptk_info *tpi, | |
915 | struct metadata_dst *tun_dst, | |
916 | bool log_ecn_err) | |
917 | { | |
6712abc1 | 918 | return __ip6_tnl_rcv(t, skb, tpi, tun_dst, ip6ip6_dscp_ecn_decapsulate, |
0d3c703a TH |
919 | log_ecn_err); |
920 | } | |
921 | EXPORT_SYMBOL(ip6_tnl_rcv); | |
922 | ||
923 | static const struct tnl_ptk_info tpi_v6 = { | |
924 | /* no tunnel info required for ipxip6. */ | |
925 | .proto = htons(ETH_P_IPV6), | |
926 | }; | |
927 | ||
928 | static const struct tnl_ptk_info tpi_v4 = { | |
929 | /* no tunnel info required for ipxip6. */ | |
930 | .proto = htons(ETH_P_IP), | |
931 | }; | |
932 | ||
f200e98d VF |
933 | static const struct tnl_ptk_info tpi_mpls = { |
934 | /* no tunnel info required for mplsip6. */ | |
935 | .proto = htons(ETH_P_MPLS_UC), | |
936 | }; | |
937 | ||
0d3c703a TH |
938 | static int ipxip6_rcv(struct sk_buff *skb, u8 ipproto, |
939 | const struct tnl_ptk_info *tpi, | |
940 | int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t, | |
941 | const struct ipv6hdr *ipv6h, | |
942 | struct sk_buff *skb)) | |
943 | { | |
944 | struct ip6_tnl *t; | |
945 | const struct ipv6hdr *ipv6h = ipv6_hdr(skb); | |
8d79266b | 946 | struct metadata_dst *tun_dst = NULL; |
0d3c703a TH |
947 | int ret = -1; |
948 | ||
949 | rcu_read_lock(); | |
5fdcce21 | 950 | t = ip6_tnl_lookup(dev_net(skb->dev), skb->dev->ifindex, &ipv6h->saddr, &ipv6h->daddr); |
0d3c703a TH |
951 | |
952 | if (t) { | |
6aa7de05 | 953 | u8 tproto = READ_ONCE(t->parms.proto); |
0d3c703a TH |
954 | |
955 | if (tproto != ipproto && tproto != 0) | |
956 | goto drop; | |
957 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) | |
958 | goto drop; | |
cbb49697 | 959 | ipv6h = ipv6_hdr(skb); |
0d3c703a TH |
960 | if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) |
961 | goto drop; | |
962 | if (iptunnel_pull_header(skb, 0, tpi->proto, false)) | |
963 | goto drop; | |
8d79266b AS |
964 | if (t->parms.collect_md) { |
965 | tun_dst = ipv6_tun_rx_dst(skb, 0, 0, 0); | |
966 | if (!tun_dst) | |
74c4b656 | 967 | goto drop; |
8d79266b AS |
968 | } |
969 | ret = __ip6_tnl_rcv(t, skb, tpi, tun_dst, dscp_ecn_decapsulate, | |
0d3c703a | 970 | log_ecn_error); |
1da177e4 | 971 | } |
0d3c703a | 972 | |
2922bc8a | 973 | rcu_read_unlock(); |
50fba2aa | 974 | |
0d3c703a TH |
975 | return ret; |
976 | ||
977 | drop: | |
978 | rcu_read_unlock(); | |
50fba2aa HX |
979 | kfree_skb(skb); |
980 | return 0; | |
1da177e4 LT |
981 | } |
982 | ||
c4d3efaf YK |
983 | static int ip4ip6_rcv(struct sk_buff *skb) |
984 | { | |
ca4aa976 | 985 | return ipxip6_rcv(skb, IPPROTO_IPIP, &tpi_v4, |
0d3c703a | 986 | ip4ip6_dscp_ecn_decapsulate); |
c4d3efaf YK |
987 | } |
988 | ||
8359925b YK |
989 | static int ip6ip6_rcv(struct sk_buff *skb) |
990 | { | |
0d3c703a TH |
991 | return ipxip6_rcv(skb, IPPROTO_IPV6, &tpi_v6, |
992 | ip6ip6_dscp_ecn_decapsulate); | |
8359925b YK |
993 | } |
994 | ||
f200e98d VF |
995 | static int mplsip6_rcv(struct sk_buff *skb) |
996 | { | |
997 | return ipxip6_rcv(skb, IPPROTO_MPLS, &tpi_mpls, | |
998 | mplsip6_dscp_ecn_decapsulate); | |
999 | } | |
1000 | ||
6fb32dde VN |
1001 | struct ipv6_tel_txoption { |
1002 | struct ipv6_txoptions ops; | |
1003 | __u8 dst_opt[8]; | |
1004 | }; | |
1da177e4 | 1005 | |
6fb32dde VN |
1006 | static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit) |
1007 | { | |
1008 | memset(opt, 0, sizeof(struct ipv6_tel_txoption)); | |
1da177e4 | 1009 | |
6fb32dde VN |
1010 | opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT; |
1011 | opt->dst_opt[3] = 1; | |
1012 | opt->dst_opt[4] = encap_limit; | |
1013 | opt->dst_opt[5] = IPV6_TLV_PADN; | |
1014 | opt->dst_opt[6] = 1; | |
1da177e4 | 1015 | |
89a23c8b | 1016 | opt->ops.dst1opt = (struct ipv6_opt_hdr *) opt->dst_opt; |
6fb32dde | 1017 | opt->ops.opt_nflen = 8; |
1da177e4 LT |
1018 | } |
1019 | ||
1020 | /** | |
3144581c | 1021 | * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own |
1da177e4 | 1022 | * @t: the outgoing tunnel device |
1ab1457c | 1023 | * @hdr: IPv6 header from the incoming packet |
1da177e4 LT |
1024 | * |
1025 | * Description: | |
1ab1457c | 1026 | * Avoid trivial tunneling loop by checking that tunnel exit-point |
1da177e4 LT |
1027 | * doesn't match source of incoming packet. |
1028 | * | |
1ab1457c | 1029 | * Return: |
1da177e4 LT |
1030 | * 1 if conflict, |
1031 | * 0 else | |
1032 | **/ | |
1033 | ||
92113bfd | 1034 | static inline bool |
b71d1d42 | 1035 | ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr) |
1da177e4 LT |
1036 | { |
1037 | return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); | |
1038 | } | |
1039 | ||
d5005140 SK |
1040 | int ip6_tnl_xmit_ctl(struct ip6_tnl *t, |
1041 | const struct in6_addr *laddr, | |
1042 | const struct in6_addr *raddr) | |
09c6bbf0 | 1043 | { |
c12b395a | 1044 | struct __ip6_tnl_parm *p = &t->parms; |
09c6bbf0 | 1045 | int ret = 0; |
0bd87628 | 1046 | struct net *net = t->net; |
09c6bbf0 | 1047 | |
6712abc1 WT |
1048 | if (t->parms.collect_md) |
1049 | return 1; | |
1050 | ||
d5005140 SK |
1051 | if ((p->flags & IP6_TNL_F_CAP_XMIT) || |
1052 | ((p->flags & IP6_TNL_F_CAP_PER_PACKET) && | |
1053 | (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) { | |
09c6bbf0 VN |
1054 | struct net_device *ldev = NULL; |
1055 | ||
f1a28eab | 1056 | rcu_read_lock(); |
09c6bbf0 | 1057 | if (p->link) |
f1a28eab | 1058 | ldev = dev_get_by_index_rcu(net, p->link); |
09c6bbf0 | 1059 | |
232378e8 DA |
1060 | if (unlikely(!ipv6_chk_addr_and_flags(net, laddr, ldev, false, |
1061 | 0, IFA_F_TENTATIVE))) | |
f3213831 JP |
1062 | pr_warn("%s xmit: Local address not yet configured!\n", |
1063 | p->name); | |
908d140a SL |
1064 | else if (!(p->flags & IP6_TNL_F_ALLOW_LOCAL_REMOTE) && |
1065 | !ipv6_addr_is_multicast(raddr) && | |
232378e8 DA |
1066 | unlikely(ipv6_chk_addr_and_flags(net, raddr, ldev, |
1067 | true, 0, IFA_F_TENTATIVE))) | |
f3213831 JP |
1068 | pr_warn("%s xmit: Routing loop! Remote address found on this node!\n", |
1069 | p->name); | |
09c6bbf0 VN |
1070 | else |
1071 | ret = 1; | |
f1a28eab | 1072 | rcu_read_unlock(); |
09c6bbf0 VN |
1073 | } |
1074 | return ret; | |
1075 | } | |
c12b395a | 1076 | EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl); |
1077 | ||
1da177e4 | 1078 | /** |
8eb30be0 | 1079 | * ip6_tnl_xmit - encapsulate packet and send |
1da177e4 | 1080 | * @skb: the outgoing socket buffer |
1ab1457c | 1081 | * @dev: the outgoing tunnel device |
61ec2aec | 1082 | * @dsfield: dscp code for outer header |
8eb30be0 | 1083 | * @fl6: flow of tunneled packet |
61ec2aec YK |
1084 | * @encap_limit: encapsulation limit |
1085 | * @pmtu: Path MTU is stored if packet is too big | |
8eb30be0 | 1086 | * @proto: next header value |
1da177e4 LT |
1087 | * |
1088 | * Description: | |
1089 | * Build new header and do some sanity checks on the packet before sending | |
1090 | * it. | |
1091 | * | |
1ab1457c | 1092 | * Return: |
c4d3efaf | 1093 | * 0 on success |
61ec2aec YK |
1094 | * -1 fail |
1095 | * %-EMSGSIZE message too big. return mtu in this case. | |
1da177e4 LT |
1096 | **/ |
1097 | ||
8eb30be0 TH |
1098 | int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield, |
1099 | struct flowi6 *fl6, int encap_limit, __u32 *pmtu, | |
1100 | __u8 proto) | |
1da177e4 | 1101 | { |
2941a486 | 1102 | struct ip6_tnl *t = netdev_priv(dev); |
0bd87628 | 1103 | struct net *net = t->net; |
3dca02af | 1104 | struct net_device_stats *stats = &t->dev->stats; |
199ab00f | 1105 | struct ipv6hdr *ipv6h; |
6fb32dde | 1106 | struct ipv6_tel_txoption opt; |
d24f22f3 | 1107 | struct dst_entry *dst = NULL, *ndst = NULL; |
1da177e4 LT |
1108 | struct net_device *tdev; |
1109 | int mtu; | |
d41bb33b | 1110 | unsigned int eth_hlen = t->dev->type == ARPHRD_ETHER ? ETH_HLEN : 0; |
058214a4 TH |
1111 | unsigned int psh_hlen = sizeof(struct ipv6hdr) + t->encap_hlen; |
1112 | unsigned int max_headroom = psh_hlen; | |
b5c2d495 | 1113 | bool use_cache = false; |
8d79266b | 1114 | u8 hop_limit; |
61ec2aec | 1115 | int err = -1; |
1da177e4 | 1116 | |
8d79266b AS |
1117 | if (t->parms.collect_md) { |
1118 | hop_limit = skb_tunnel_info(skb)->key.ttl; | |
1119 | goto route_lookup; | |
1120 | } else { | |
1121 | hop_limit = t->parms.hop_limit; | |
1122 | } | |
1123 | ||
ea3dc960 SK |
1124 | /* NBMA tunnel */ |
1125 | if (ipv6_addr_any(&t->parms.raddr)) { | |
199ab00f WC |
1126 | if (skb->protocol == htons(ETH_P_IPV6)) { |
1127 | struct in6_addr *addr6; | |
1128 | struct neighbour *neigh; | |
1129 | int addr_type; | |
ea3dc960 | 1130 | |
199ab00f WC |
1131 | if (!skb_dst(skb)) |
1132 | goto tx_err_link_failure; | |
ea3dc960 | 1133 | |
199ab00f WC |
1134 | neigh = dst_neigh_lookup(skb_dst(skb), |
1135 | &ipv6_hdr(skb)->daddr); | |
1136 | if (!neigh) | |
1137 | goto tx_err_link_failure; | |
ea3dc960 | 1138 | |
199ab00f WC |
1139 | addr6 = (struct in6_addr *)&neigh->primary_key; |
1140 | addr_type = ipv6_addr_type(addr6); | |
ea3dc960 | 1141 | |
199ab00f WC |
1142 | if (addr_type == IPV6_ADDR_ANY) |
1143 | addr6 = &ipv6_hdr(skb)->daddr; | |
ea3dc960 | 1144 | |
199ab00f WC |
1145 | memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr)); |
1146 | neigh_release(neigh); | |
1147 | } | |
23263ec8 EC |
1148 | } else if (t->parms.proto != 0 && !(t->parms.flags & |
1149 | (IP6_TNL_F_USE_ORIG_TCLASS | | |
1150 | IP6_TNL_F_USE_ORIG_FWMARK))) { | |
1151 | /* enable the cache only if neither the outer protocol nor the | |
1152 | * routing decision depends on the current inner header value | |
b5c2d495 PA |
1153 | */ |
1154 | use_cache = true; | |
1155 | } | |
1156 | ||
1157 | if (use_cache) | |
607f725f | 1158 | dst = dst_cache_get(&t->dst_cache); |
d5005140 SK |
1159 | |
1160 | if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr)) | |
1161 | goto tx_err_link_failure; | |
1162 | ||
89b02126 | 1163 | if (!dst) { |
8d79266b | 1164 | route_lookup: |
5f733ee6 LM |
1165 | /* add dsfield to flowlabel for route lookup */ |
1166 | fl6->flowlabel = ip6_make_flowinfo(dsfield, fl6->flowlabel); | |
1167 | ||
cdf3464e | 1168 | dst = ip6_route_output(net, NULL, fl6); |
1da177e4 | 1169 | |
cdf3464e | 1170 | if (dst->error) |
a57ebc90 | 1171 | goto tx_err_link_failure; |
cdf3464e MKL |
1172 | dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0); |
1173 | if (IS_ERR(dst)) { | |
1174 | err = PTR_ERR(dst); | |
1175 | dst = NULL; | |
452edd59 DM |
1176 | goto tx_err_link_failure; |
1177 | } | |
3789caba | 1178 | if (t->parms.collect_md && ipv6_addr_any(&fl6->saddr) && |
8d79266b AS |
1179 | ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev, |
1180 | &fl6->daddr, 0, &fl6->saddr)) | |
1181 | goto tx_err_link_failure; | |
cdf3464e | 1182 | ndst = dst; |
a57ebc90 | 1183 | } |
1da177e4 LT |
1184 | |
1185 | tdev = dst->dev; | |
1186 | ||
1187 | if (tdev == dev) { | |
1188 | stats->collisions++; | |
e87cc472 JP |
1189 | net_warn_ratelimited("%s: Local routing loop detected!\n", |
1190 | t->parms.name); | |
1da177e4 LT |
1191 | goto tx_err_dst_release; |
1192 | } | |
d41bb33b | 1193 | mtu = dst_mtu(dst) - eth_hlen - psh_hlen - t->tun_hlen; |
6fb32dde | 1194 | if (encap_limit >= 0) { |
1da177e4 LT |
1195 | max_headroom += 8; |
1196 | mtu -= 8; | |
1197 | } | |
82a40777 XL |
1198 | mtu = max(mtu, skb->protocol == htons(ETH_P_IPV6) ? |
1199 | IPV6_MIN_MTU : IPV4_MIN_MTU); | |
c9fefa08 | 1200 | |
7a1592bc | 1201 | skb_dst_update_pmtu_no_confirm(skb, mtu); |
d41bb33b | 1202 | if (skb->len - t->tun_hlen - eth_hlen > mtu && !skb_is_gso(skb)) { |
61ec2aec YK |
1203 | *pmtu = mtu; |
1204 | err = -EMSGSIZE; | |
1da177e4 LT |
1205 | goto tx_err_dst_release; |
1206 | } | |
1207 | ||
8eb30be0 TH |
1208 | if (t->err_count > 0) { |
1209 | if (time_before(jiffies, | |
1210 | t->err_time + IP6TUNNEL_ERR_TIMEO)) { | |
1211 | t->err_count--; | |
1212 | ||
1213 | dst_link_failure(skb); | |
1214 | } else { | |
1215 | t->err_count = 0; | |
1216 | } | |
1217 | } | |
1218 | ||
963a88b3 | 1219 | skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev))); |
0bd87628 | 1220 | |
1da177e4 LT |
1221 | /* |
1222 | * Okay, now see if we can stuff it in the buffer as-is. | |
1223 | */ | |
1224 | max_headroom += LL_RESERVED_SPACE(tdev); | |
1ab1457c | 1225 | |
cfbba49d PM |
1226 | if (skb_headroom(skb) < max_headroom || skb_shared(skb) || |
1227 | (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { | |
1da177e4 | 1228 | struct sk_buff *new_skb; |
1ab1457c | 1229 | |
e5d08d71 IM |
1230 | new_skb = skb_realloc_headroom(skb, max_headroom); |
1231 | if (!new_skb) | |
1da177e4 LT |
1232 | goto tx_err_dst_release; |
1233 | ||
1234 | if (skb->sk) | |
1235 | skb_set_owner_w(new_skb, skb->sk); | |
9ff26449 | 1236 | consume_skb(skb); |
1da177e4 LT |
1237 | skb = new_skb; |
1238 | } | |
cdf3464e | 1239 | |
8d79266b AS |
1240 | if (t->parms.collect_md) { |
1241 | if (t->encap.type != TUNNEL_ENCAP_NONE) | |
1242 | goto tx_err_dst_release; | |
1243 | } else { | |
b5c2d495 | 1244 | if (use_cache && ndst) |
8d79266b AS |
1245 | dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr); |
1246 | } | |
cdf3464e MKL |
1247 | skb_dst_set(skb, dst); |
1248 | ||
36feaac3 HL |
1249 | if (hop_limit == 0) { |
1250 | if (skb->protocol == htons(ETH_P_IP)) | |
1251 | hop_limit = ip_hdr(skb)->ttl; | |
1252 | else if (skb->protocol == htons(ETH_P_IPV6)) | |
1253 | hop_limit = ipv6_hdr(skb)->hop_limit; | |
1254 | else | |
1255 | hop_limit = ip6_dst_hoplimit(dst); | |
1256 | } | |
3d483058 | 1257 | |
058214a4 TH |
1258 | /* Calculate max headroom for all the headers and adjust |
1259 | * needed_headroom if necessary. | |
1260 | */ | |
8eb30be0 | 1261 | max_headroom = LL_RESERVED_SPACE(dst->dev) + sizeof(struct ipv6hdr) |
058214a4 | 1262 | + dst->header_len + t->hlen; |
8eb30be0 TH |
1263 | if (max_headroom > dev->needed_headroom) |
1264 | dev->needed_headroom = max_headroom; | |
1265 | ||
058214a4 TH |
1266 | err = ip6_tnl_encap(skb, t, &proto, fl6); |
1267 | if (err) | |
1268 | return err; | |
1269 | ||
d4d576f5 SB |
1270 | if (encap_limit >= 0) { |
1271 | init_tel_txopt(&opt, encap_limit); | |
1272 | ipv6_push_frag_opts(skb, &opt.ops, &proto); | |
1273 | } | |
1274 | ||
6c11fbf9 VF |
1275 | skb_set_inner_ipproto(skb, proto); |
1276 | ||
e2d1bca7 ACM |
1277 | skb_push(skb, sizeof(struct ipv6hdr)); |
1278 | skb_reset_network_header(skb); | |
0660e03f | 1279 | ipv6h = ipv6_hdr(skb); |
0e9a7095 | 1280 | ip6_flow_hdr(ipv6h, dsfield, |
42240901 | 1281 | ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6)); |
8d79266b | 1282 | ipv6h->hop_limit = hop_limit; |
1da177e4 | 1283 | ipv6h->nexthdr = proto; |
4e3fd7a0 AD |
1284 | ipv6h->saddr = fl6->saddr; |
1285 | ipv6h->daddr = fl6->daddr; | |
79b16aad | 1286 | ip6tunnel_xmit(NULL, skb, dev); |
1da177e4 LT |
1287 | return 0; |
1288 | tx_err_link_failure: | |
1289 | stats->tx_carrier_errors++; | |
1290 | dst_link_failure(skb); | |
1291 | tx_err_dst_release: | |
cdf3464e | 1292 | dst_release(dst); |
61ec2aec YK |
1293 | return err; |
1294 | } | |
8eb30be0 | 1295 | EXPORT_SYMBOL(ip6_tnl_xmit); |
61ec2aec | 1296 | |
c4d3efaf | 1297 | static inline int |
e7bb18e6 VF |
1298 | ipxip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, |
1299 | u8 protocol) | |
c4d3efaf YK |
1300 | { |
1301 | struct ip6_tnl *t = netdev_priv(dev); | |
e7bb18e6 | 1302 | struct ipv6hdr *ipv6h; |
76c0ddd8 | 1303 | const struct iphdr *iph; |
c4d3efaf | 1304 | int encap_limit = -1; |
e7bb18e6 | 1305 | __u16 offset; |
4c9483b2 | 1306 | struct flowi6 fl6; |
e7bb18e6 | 1307 | __u8 dsfield, orig_dsfield; |
c4d3efaf | 1308 | __u32 mtu; |
acf722f7 | 1309 | u8 tproto; |
c4d3efaf YK |
1310 | int err; |
1311 | ||
6aa7de05 | 1312 | tproto = READ_ONCE(t->parms.proto); |
e7bb18e6 | 1313 | if (tproto != protocol && tproto != 0) |
c4d3efaf YK |
1314 | return -1; |
1315 | ||
8d79266b AS |
1316 | if (t->parms.collect_md) { |
1317 | struct ip_tunnel_info *tun_info; | |
1318 | const struct ip_tunnel_key *key; | |
c4d3efaf | 1319 | |
8d79266b AS |
1320 | tun_info = skb_tunnel_info(skb); |
1321 | if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) || | |
1322 | ip_tunnel_info_af(tun_info) != AF_INET6)) | |
1323 | return -1; | |
1324 | key = &tun_info->key; | |
1325 | memset(&fl6, 0, sizeof(fl6)); | |
e7bb18e6 | 1326 | fl6.flowi6_proto = protocol; |
3789caba | 1327 | fl6.saddr = key->u.ipv6.src; |
8d79266b AS |
1328 | fl6.daddr = key->u.ipv6.dst; |
1329 | fl6.flowlabel = key->label; | |
46f8cd9d | 1330 | dsfield = key->tos; |
e7bb18e6 VF |
1331 | switch (protocol) { |
1332 | case IPPROTO_IPIP: | |
1333 | iph = ip_hdr(skb); | |
1334 | orig_dsfield = ipv4_get_dsfield(iph); | |
1335 | break; | |
1336 | case IPPROTO_IPV6: | |
1337 | ipv6h = ipv6_hdr(skb); | |
1338 | orig_dsfield = ipv6_get_dsfield(ipv6h); | |
1339 | break; | |
1340 | default: | |
1341 | orig_dsfield = dsfield; | |
1342 | break; | |
1343 | } | |
8d79266b AS |
1344 | } else { |
1345 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
1346 | encap_limit = t->parms.encap_limit; | |
e7bb18e6 VF |
1347 | if (protocol == IPPROTO_IPV6) { |
1348 | offset = ip6_tnl_parse_tlv_enc_lim(skb, | |
1349 | skb_network_header(skb)); | |
1350 | /* ip6_tnl_parse_tlv_enc_lim() might have | |
1351 | * reallocated skb->head | |
1352 | */ | |
1353 | if (offset > 0) { | |
1354 | struct ipv6_tlv_tnl_enc_lim *tel; | |
c4d3efaf | 1355 | |
e7bb18e6 VF |
1356 | tel = (void *)&skb_network_header(skb)[offset]; |
1357 | if (tel->encap_limit == 0) { | |
1358 | icmpv6_send(skb, ICMPV6_PARAMPROB, | |
1359 | ICMPV6_HDR_FIELD, offset + 2); | |
1360 | return -1; | |
1361 | } | |
1362 | encap_limit = tel->encap_limit - 1; | |
8d79266b | 1363 | } |
61ec2aec | 1364 | } |
61ec2aec | 1365 | |
8d79266b | 1366 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); |
e7bb18e6 | 1367 | fl6.flowi6_proto = protocol; |
61ec2aec | 1368 | |
8d79266b AS |
1369 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
1370 | fl6.flowi6_mark = skb->mark; | |
0a473b82 CG |
1371 | else |
1372 | fl6.flowi6_mark = t->parms.fwmark; | |
e7bb18e6 VF |
1373 | switch (protocol) { |
1374 | case IPPROTO_IPIP: | |
1375 | iph = ip_hdr(skb); | |
1376 | orig_dsfield = ipv4_get_dsfield(iph); | |
1377 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) | |
1378 | dsfield = orig_dsfield; | |
1379 | else | |
1380 | dsfield = ip6_tclass(t->parms.flowinfo); | |
1381 | break; | |
1382 | case IPPROTO_IPV6: | |
1383 | ipv6h = ipv6_hdr(skb); | |
1384 | orig_dsfield = ipv6_get_dsfield(ipv6h); | |
1385 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) | |
1386 | dsfield = orig_dsfield; | |
1387 | else | |
1388 | dsfield = ip6_tclass(t->parms.flowinfo); | |
1389 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) | |
1390 | fl6.flowlabel |= ip6_flowlabel(ipv6h); | |
1391 | break; | |
1392 | default: | |
6c11fbf9 | 1393 | orig_dsfield = dsfield = ip6_tclass(t->parms.flowinfo); |
e7bb18e6 VF |
1394 | break; |
1395 | } | |
8d79266b | 1396 | } |
61ec2aec | 1397 | |
e2d118a1 | 1398 | fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL); |
e7bb18e6 | 1399 | dsfield = INET_ECN_encapsulate(dsfield, orig_dsfield); |
e2d118a1 | 1400 | |
815d22e5 TH |
1401 | if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6)) |
1402 | return -1; | |
1403 | ||
8eb30be0 | 1404 | err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, |
e7bb18e6 | 1405 | protocol); |
61ec2aec | 1406 | if (err != 0) { |
e7bb18e6 | 1407 | /* XXX: send ICMP error even if DF is not set. */ |
61ec2aec | 1408 | if (err == -EMSGSIZE) |
e7bb18e6 VF |
1409 | switch (protocol) { |
1410 | case IPPROTO_IPIP: | |
1411 | icmp_send(skb, ICMP_DEST_UNREACH, | |
1412 | ICMP_FRAG_NEEDED, htonl(mtu)); | |
1413 | break; | |
1414 | case IPPROTO_IPV6: | |
1415 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); | |
1416 | break; | |
1417 | default: | |
1418 | break; | |
1419 | } | |
61ec2aec YK |
1420 | return -1; |
1421 | } | |
1422 | ||
1423 | return 0; | |
1424 | } | |
1425 | ||
6fef4c0c | 1426 | static netdev_tx_t |
8eb30be0 | 1427 | ip6_tnl_start_xmit(struct sk_buff *skb, struct net_device *dev) |
61ec2aec YK |
1428 | { |
1429 | struct ip6_tnl *t = netdev_priv(dev); | |
3dca02af | 1430 | struct net_device_stats *stats = &t->dev->stats; |
e7bb18e6 | 1431 | u8 ipproto; |
61ec2aec YK |
1432 | int ret; |
1433 | ||
cb9f1b78 WB |
1434 | if (!pskb_inet_may_pull(skb)) |
1435 | goto tx_err; | |
1436 | ||
61ec2aec | 1437 | switch (skb->protocol) { |
60678040 | 1438 | case htons(ETH_P_IP): |
e7bb18e6 | 1439 | ipproto = IPPROTO_IPIP; |
c4d3efaf | 1440 | break; |
60678040 | 1441 | case htons(ETH_P_IPV6): |
e7bb18e6 VF |
1442 | if (ip6_tnl_addr_conflict(t, ipv6_hdr(skb))) |
1443 | goto tx_err; | |
1444 | ipproto = IPPROTO_IPV6; | |
61ec2aec | 1445 | break; |
6c11fbf9 VF |
1446 | case htons(ETH_P_MPLS_UC): |
1447 | ipproto = IPPROTO_MPLS; | |
1448 | break; | |
61ec2aec YK |
1449 | default: |
1450 | goto tx_err; | |
1451 | } | |
1452 | ||
e7bb18e6 | 1453 | ret = ipxip6_tnl_xmit(skb, dev, ipproto); |
61ec2aec YK |
1454 | if (ret < 0) |
1455 | goto tx_err; | |
1456 | ||
6ed10654 | 1457 | return NETDEV_TX_OK; |
61ec2aec | 1458 | |
1da177e4 LT |
1459 | tx_err: |
1460 | stats->tx_errors++; | |
1461 | stats->tx_dropped++; | |
1462 | kfree_skb(skb); | |
6ed10654 | 1463 | return NETDEV_TX_OK; |
1da177e4 LT |
1464 | } |
1465 | ||
3144581c | 1466 | static void ip6_tnl_link_config(struct ip6_tnl *t) |
1da177e4 LT |
1467 | { |
1468 | struct net_device *dev = t->dev; | |
5fdcce21 | 1469 | struct net_device *tdev = NULL; |
c12b395a | 1470 | struct __ip6_tnl_parm *p = &t->parms; |
4c9483b2 | 1471 | struct flowi6 *fl6 = &t->fl.u.ip6; |
5fdcce21 | 1472 | unsigned int mtu; |
058214a4 | 1473 | int t_hlen; |
1da177e4 | 1474 | |
3a6d54c5 JP |
1475 | memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); |
1476 | memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); | |
1da177e4 LT |
1477 | |
1478 | /* Set up flowi template */ | |
4e3fd7a0 AD |
1479 | fl6->saddr = p->laddr; |
1480 | fl6->daddr = p->raddr; | |
4c9483b2 DM |
1481 | fl6->flowi6_oif = p->link; |
1482 | fl6->flowlabel = 0; | |
1da177e4 LT |
1483 | |
1484 | if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) | |
4c9483b2 | 1485 | fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; |
1da177e4 | 1486 | if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) |
4c9483b2 | 1487 | fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; |
1da177e4 | 1488 | |
d0087b29 VN |
1489 | p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); |
1490 | p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); | |
1da177e4 LT |
1491 | |
1492 | if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV) | |
1493 | dev->flags |= IFF_POINTOPOINT; | |
1494 | else | |
1495 | dev->flags &= ~IFF_POINTOPOINT; | |
1496 | ||
058214a4 TH |
1497 | t->tun_hlen = 0; |
1498 | t->hlen = t->encap_hlen + t->tun_hlen; | |
1499 | t_hlen = t->hlen + sizeof(struct ipv6hdr); | |
1500 | ||
1da177e4 | 1501 | if (p->flags & IP6_TNL_F_CAP_XMIT) { |
305d4b3c VN |
1502 | int strict = (ipv6_addr_type(&p->raddr) & |
1503 | (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); | |
1504 | ||
0bd87628 | 1505 | struct rt6_info *rt = rt6_lookup(t->net, |
2f7f54b7 | 1506 | &p->raddr, &p->laddr, |
b75cc8f9 | 1507 | p->link, NULL, strict); |
5fdcce21 WD |
1508 | if (rt) { |
1509 | tdev = rt->dst.dev; | |
1510 | ip6_rt_put(rt); | |
1511 | } | |
1da177e4 | 1512 | |
5fdcce21 WD |
1513 | if (!tdev && p->link) |
1514 | tdev = __dev_get_by_index(t->net, p->link); | |
1da177e4 | 1515 | |
5fdcce21 WD |
1516 | if (tdev) { |
1517 | dev->hard_header_len = tdev->hard_header_len + t_hlen; | |
1518 | mtu = min_t(unsigned int, tdev->mtu, IP6_MAX_MTU); | |
1da177e4 | 1519 | |
5fdcce21 | 1520 | dev->mtu = mtu - t_hlen; |
381601e5 | 1521 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
67ba4152 | 1522 | dev->mtu -= 8; |
1da177e4 LT |
1523 | |
1524 | if (dev->mtu < IPV6_MIN_MTU) | |
1525 | dev->mtu = IPV6_MIN_MTU; | |
1526 | } | |
1da177e4 LT |
1527 | } |
1528 | } | |
1529 | ||
1530 | /** | |
3144581c | 1531 | * ip6_tnl_change - update the tunnel parameters |
1da177e4 LT |
1532 | * @t: tunnel to be changed |
1533 | * @p: tunnel configuration parameters | |
1da177e4 LT |
1534 | * |
1535 | * Description: | |
3144581c | 1536 | * ip6_tnl_change() updates the tunnel parameters |
1da177e4 LT |
1537 | **/ |
1538 | ||
1539 | static int | |
c12b395a | 1540 | ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p) |
1da177e4 | 1541 | { |
4e3fd7a0 AD |
1542 | t->parms.laddr = p->laddr; |
1543 | t->parms.raddr = p->raddr; | |
1da177e4 LT |
1544 | t->parms.flags = p->flags; |
1545 | t->parms.hop_limit = p->hop_limit; | |
1546 | t->parms.encap_limit = p->encap_limit; | |
1547 | t->parms.flowinfo = p->flowinfo; | |
8181b8c1 | 1548 | t->parms.link = p->link; |
502b0935 | 1549 | t->parms.proto = p->proto; |
0a473b82 | 1550 | t->parms.fwmark = p->fwmark; |
607f725f | 1551 | dst_cache_reset(&t->dst_cache); |
3144581c | 1552 | ip6_tnl_link_config(t); |
1da177e4 LT |
1553 | return 0; |
1554 | } | |
1555 | ||
0b112457 ND |
1556 | static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p) |
1557 | { | |
0bd87628 | 1558 | struct net *net = t->net; |
0b112457 ND |
1559 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
1560 | int err; | |
1561 | ||
1562 | ip6_tnl_unlink(ip6n, t); | |
1563 | synchronize_net(); | |
1564 | err = ip6_tnl_change(t, p); | |
1565 | ip6_tnl_link(ip6n, t); | |
1566 | netdev_state_change(t->dev); | |
1567 | return err; | |
1568 | } | |
1569 | ||
acf722f7 AA |
1570 | static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p) |
1571 | { | |
1572 | /* for default tnl0 device allow to change only the proto */ | |
1573 | t->parms.proto = p->proto; | |
1574 | netdev_state_change(t->dev); | |
1575 | return 0; | |
1576 | } | |
1577 | ||
c12b395a | 1578 | static void |
1579 | ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u) | |
1580 | { | |
1581 | p->laddr = u->laddr; | |
1582 | p->raddr = u->raddr; | |
1583 | p->flags = u->flags; | |
1584 | p->hop_limit = u->hop_limit; | |
1585 | p->encap_limit = u->encap_limit; | |
1586 | p->flowinfo = u->flowinfo; | |
1587 | p->link = u->link; | |
1588 | p->proto = u->proto; | |
1589 | memcpy(p->name, u->name, sizeof(u->name)); | |
1590 | } | |
1591 | ||
1592 | static void | |
1593 | ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p) | |
1594 | { | |
1595 | u->laddr = p->laddr; | |
1596 | u->raddr = p->raddr; | |
1597 | u->flags = p->flags; | |
1598 | u->hop_limit = p->hop_limit; | |
1599 | u->encap_limit = p->encap_limit; | |
1600 | u->flowinfo = p->flowinfo; | |
1601 | u->link = p->link; | |
1602 | u->proto = p->proto; | |
1603 | memcpy(u->name, p->name, sizeof(u->name)); | |
1604 | } | |
1605 | ||
1da177e4 | 1606 | /** |
3144581c | 1607 | * ip6_tnl_ioctl - configure ipv6 tunnels from userspace |
1da177e4 LT |
1608 | * @dev: virtual device associated with tunnel |
1609 | * @ifr: parameters passed from userspace | |
1610 | * @cmd: command to be performed | |
1611 | * | |
1612 | * Description: | |
3144581c | 1613 | * ip6_tnl_ioctl() is used for managing IPv6 tunnels |
1ab1457c | 1614 | * from userspace. |
1da177e4 LT |
1615 | * |
1616 | * The possible commands are the following: | |
1617 | * %SIOCGETTUNNEL: get tunnel parameters for device | |
1618 | * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters | |
1619 | * %SIOCCHGTUNNEL: change tunnel parameters to those given | |
1620 | * %SIOCDELTUNNEL: delete tunnel | |
1621 | * | |
1ab1457c | 1622 | * The fallback device "ip6tnl0", created during module |
1da177e4 LT |
1623 | * initialization, can be used for creating other tunnel devices. |
1624 | * | |
1625 | * Return: | |
1626 | * 0 on success, | |
1627 | * %-EFAULT if unable to copy data to or from userspace, | |
1628 | * %-EPERM if current process hasn't %CAP_NET_ADMIN set | |
1629 | * %-EINVAL if passed tunnel parameters are invalid, | |
1630 | * %-EEXIST if changing a tunnel's parameters would cause a conflict | |
1631 | * %-ENODEV if attempting to change or delete a nonexisting device | |
1632 | **/ | |
1633 | ||
1634 | static int | |
3144581c | 1635 | ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
1da177e4 LT |
1636 | { |
1637 | int err = 0; | |
1da177e4 | 1638 | struct ip6_tnl_parm p; |
c12b395a | 1639 | struct __ip6_tnl_parm p1; |
74462f0d ND |
1640 | struct ip6_tnl *t = netdev_priv(dev); |
1641 | struct net *net = t->net; | |
2dd02c89 | 1642 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
1da177e4 | 1643 | |
0d3c703a TH |
1644 | memset(&p1, 0, sizeof(p1)); |
1645 | ||
1da177e4 LT |
1646 | switch (cmd) { |
1647 | case SIOCGETTUNNEL: | |
15820e12 | 1648 | if (dev == ip6n->fb_tnl_dev) { |
67ba4152 | 1649 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { |
1da177e4 LT |
1650 | err = -EFAULT; |
1651 | break; | |
1652 | } | |
c12b395a | 1653 | ip6_tnl_parm_from_user(&p1, &p); |
1654 | t = ip6_tnl_locate(net, &p1, 0); | |
37355565 | 1655 | if (IS_ERR(t)) |
74462f0d | 1656 | t = netdev_priv(dev); |
5ef5d6c5 DC |
1657 | } else { |
1658 | memset(&p, 0, sizeof(p)); | |
567131a7 | 1659 | } |
c12b395a | 1660 | ip6_tnl_parm_to_user(&p, &t->parms); |
67ba4152 | 1661 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) { |
1da177e4 LT |
1662 | err = -EFAULT; |
1663 | } | |
1664 | break; | |
1665 | case SIOCADDTUNNEL: | |
1666 | case SIOCCHGTUNNEL: | |
1667 | err = -EPERM; | |
af31f412 | 1668 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 | 1669 | break; |
567131a7 | 1670 | err = -EFAULT; |
67ba4152 | 1671 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
1da177e4 | 1672 | break; |
567131a7 | 1673 | err = -EINVAL; |
502b0935 YK |
1674 | if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP && |
1675 | p.proto != 0) | |
1da177e4 | 1676 | break; |
c12b395a | 1677 | ip6_tnl_parm_from_user(&p1, &p); |
1678 | t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL); | |
acf722f7 | 1679 | if (cmd == SIOCCHGTUNNEL) { |
37355565 | 1680 | if (!IS_ERR(t)) { |
567131a7 VN |
1681 | if (t->dev != dev) { |
1682 | err = -EEXIST; | |
1683 | break; | |
1684 | } | |
1685 | } else | |
1686 | t = netdev_priv(dev); | |
acf722f7 AA |
1687 | if (dev == ip6n->fb_tnl_dev) |
1688 | err = ip6_tnl0_update(t, &p1); | |
1689 | else | |
1690 | err = ip6_tnl_update(t, &p1); | |
1da177e4 | 1691 | } |
37355565 | 1692 | if (!IS_ERR(t)) { |
1da177e4 | 1693 | err = 0; |
c12b395a | 1694 | ip6_tnl_parm_to_user(&p, &t->parms); |
1695 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | |
567131a7 VN |
1696 | err = -EFAULT; |
1697 | ||
37355565 ND |
1698 | } else { |
1699 | err = PTR_ERR(t); | |
1700 | } | |
1da177e4 LT |
1701 | break; |
1702 | case SIOCDELTUNNEL: | |
1703 | err = -EPERM; | |
af31f412 | 1704 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
1705 | break; |
1706 | ||
15820e12 | 1707 | if (dev == ip6n->fb_tnl_dev) { |
567131a7 | 1708 | err = -EFAULT; |
67ba4152 | 1709 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
1da177e4 | 1710 | break; |
567131a7 | 1711 | err = -ENOENT; |
c12b395a | 1712 | ip6_tnl_parm_from_user(&p1, &p); |
1713 | t = ip6_tnl_locate(net, &p1, 0); | |
37355565 | 1714 | if (IS_ERR(t)) |
1da177e4 | 1715 | break; |
567131a7 | 1716 | err = -EPERM; |
15820e12 | 1717 | if (t->dev == ip6n->fb_tnl_dev) |
1da177e4 | 1718 | break; |
567131a7 | 1719 | dev = t->dev; |
1da177e4 | 1720 | } |
22f8cde5 SH |
1721 | err = 0; |
1722 | unregister_netdevice(dev); | |
1da177e4 LT |
1723 | break; |
1724 | default: | |
1725 | err = -EINVAL; | |
1726 | } | |
1727 | return err; | |
1728 | } | |
1729 | ||
1da177e4 | 1730 | /** |
3144581c | 1731 | * ip6_tnl_change_mtu - change mtu manually for tunnel device |
1da177e4 LT |
1732 | * @dev: virtual device associated with tunnel |
1733 | * @new_mtu: the new mtu | |
1734 | * | |
1735 | * Return: | |
1736 | * 0 on success, | |
1737 | * %-EINVAL if mtu too small | |
1738 | **/ | |
1739 | ||
79ecb90e | 1740 | int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu) |
1da177e4 | 1741 | { |
582442d6 OG |
1742 | struct ip6_tnl *tnl = netdev_priv(dev); |
1743 | ||
2fa771be XL |
1744 | if (tnl->parms.proto == IPPROTO_IPV6) { |
1745 | if (new_mtu < IPV6_MIN_MTU) | |
582442d6 OG |
1746 | return -EINVAL; |
1747 | } else { | |
2fa771be | 1748 | if (new_mtu < ETH_MIN_MTU) |
582442d6 | 1749 | return -EINVAL; |
1da177e4 | 1750 | } |
f7ff1fde ND |
1751 | if (tnl->parms.proto == IPPROTO_IPV6 || tnl->parms.proto == 0) { |
1752 | if (new_mtu > IP6_MAX_MTU - dev->hard_header_len) | |
1753 | return -EINVAL; | |
1754 | } else { | |
1755 | if (new_mtu > IP_MAX_MTU - dev->hard_header_len) | |
1756 | return -EINVAL; | |
1757 | } | |
1da177e4 LT |
1758 | dev->mtu = new_mtu; |
1759 | return 0; | |
1760 | } | |
79ecb90e | 1761 | EXPORT_SYMBOL(ip6_tnl_change_mtu); |
1da177e4 | 1762 | |
ecf2c06a ND |
1763 | int ip6_tnl_get_iflink(const struct net_device *dev) |
1764 | { | |
1765 | struct ip6_tnl *t = netdev_priv(dev); | |
1766 | ||
1767 | return t->parms.link; | |
1768 | } | |
1769 | EXPORT_SYMBOL(ip6_tnl_get_iflink); | |
1326c3d5 | 1770 | |
058214a4 TH |
1771 | int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops, |
1772 | unsigned int num) | |
1773 | { | |
1774 | if (num >= MAX_IPTUN_ENCAP_OPS) | |
1775 | return -ERANGE; | |
1776 | ||
1777 | return !cmpxchg((const struct ip6_tnl_encap_ops **) | |
1778 | &ip6tun_encaps[num], | |
1779 | NULL, ops) ? 0 : -1; | |
1780 | } | |
1781 | EXPORT_SYMBOL(ip6_tnl_encap_add_ops); | |
1782 | ||
1783 | int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops, | |
1784 | unsigned int num) | |
1785 | { | |
1786 | int ret; | |
1787 | ||
1788 | if (num >= MAX_IPTUN_ENCAP_OPS) | |
1789 | return -ERANGE; | |
1790 | ||
1791 | ret = (cmpxchg((const struct ip6_tnl_encap_ops **) | |
1792 | &ip6tun_encaps[num], | |
1793 | ops, NULL) == ops) ? 0 : -1; | |
1794 | ||
1795 | synchronize_net(); | |
1796 | ||
1797 | return ret; | |
1798 | } | |
1799 | EXPORT_SYMBOL(ip6_tnl_encap_del_ops); | |
1800 | ||
1801 | int ip6_tnl_encap_setup(struct ip6_tnl *t, | |
1802 | struct ip_tunnel_encap *ipencap) | |
1803 | { | |
1804 | int hlen; | |
1805 | ||
1806 | memset(&t->encap, 0, sizeof(t->encap)); | |
1807 | ||
1808 | hlen = ip6_encap_hlen(ipencap); | |
1809 | if (hlen < 0) | |
1810 | return hlen; | |
1811 | ||
1812 | t->encap.type = ipencap->type; | |
1813 | t->encap.sport = ipencap->sport; | |
1814 | t->encap.dport = ipencap->dport; | |
1815 | t->encap.flags = ipencap->flags; | |
1816 | ||
1817 | t->encap_hlen = hlen; | |
1818 | t->hlen = t->encap_hlen + t->tun_hlen; | |
1819 | ||
1820 | return 0; | |
1821 | } | |
1822 | EXPORT_SYMBOL_GPL(ip6_tnl_encap_setup); | |
1823 | ||
1326c3d5 | 1824 | static const struct net_device_ops ip6_tnl_netdev_ops = { |
6c6151da | 1825 | .ndo_init = ip6_tnl_dev_init, |
8560f226 | 1826 | .ndo_uninit = ip6_tnl_dev_uninit, |
8eb30be0 | 1827 | .ndo_start_xmit = ip6_tnl_start_xmit, |
8560f226 | 1828 | .ndo_do_ioctl = ip6_tnl_ioctl, |
1326c3d5 | 1829 | .ndo_change_mtu = ip6_tnl_change_mtu, |
8560f226 | 1830 | .ndo_get_stats = ip6_get_stats, |
ecf2c06a | 1831 | .ndo_get_iflink = ip6_tnl_get_iflink, |
1326c3d5 SH |
1832 | }; |
1833 | ||
51c052d4 TH |
1834 | #define IPXIPX_FEATURES (NETIF_F_SG | \ |
1835 | NETIF_F_FRAGLIST | \ | |
1836 | NETIF_F_HIGHDMA | \ | |
1837 | NETIF_F_GSO_SOFTWARE | \ | |
1838 | NETIF_F_HW_CSUM) | |
8560f226 | 1839 | |
1da177e4 | 1840 | /** |
3144581c | 1841 | * ip6_tnl_dev_setup - setup virtual tunnel device |
1da177e4 LT |
1842 | * @dev: virtual device associated with tunnel |
1843 | * | |
1844 | * Description: | |
1845 | * Initialize function pointers and device parameters | |
1846 | **/ | |
1847 | ||
3144581c | 1848 | static void ip6_tnl_dev_setup(struct net_device *dev) |
1da177e4 | 1849 | { |
1326c3d5 | 1850 | dev->netdev_ops = &ip6_tnl_netdev_ops; |
e53ac932 | 1851 | dev->header_ops = &ip_tunnel_header_ops; |
cf124db5 DM |
1852 | dev->needs_free_netdev = true; |
1853 | dev->priv_destructor = ip6_dev_free; | |
1da177e4 LT |
1854 | |
1855 | dev->type = ARPHRD_TUNNEL6; | |
1da177e4 LT |
1856 | dev->flags |= IFF_NOARP; |
1857 | dev->addr_len = sizeof(struct in6_addr); | |
058214a4 | 1858 | dev->features |= NETIF_F_LLTX; |
02875878 | 1859 | netif_keep_dst(dev); |
51c052d4 TH |
1860 | |
1861 | dev->features |= IPXIPX_FEATURES; | |
1862 | dev->hw_features |= IPXIPX_FEATURES; | |
1863 | ||
e837735e ND |
1864 | /* This perm addr will be used as interface identifier by IPv6 */ |
1865 | dev->addr_assign_type = NET_ADDR_RANDOM; | |
1866 | eth_random_addr(dev->perm_addr); | |
1da177e4 LT |
1867 | } |
1868 | ||
1869 | ||
1870 | /** | |
3144581c | 1871 | * ip6_tnl_dev_init_gen - general initializer for all tunnel devices |
1da177e4 LT |
1872 | * @dev: virtual device associated with tunnel |
1873 | **/ | |
1874 | ||
8560f226 | 1875 | static inline int |
3144581c | 1876 | ip6_tnl_dev_init_gen(struct net_device *dev) |
1da177e4 | 1877 | { |
2941a486 | 1878 | struct ip6_tnl *t = netdev_priv(dev); |
cdf3464e | 1879 | int ret; |
058214a4 | 1880 | int t_hlen; |
8560f226 | 1881 | |
1da177e4 | 1882 | t->dev = dev; |
0bd87628 | 1883 | t->net = dev_net(dev); |
1c213bd2 | 1884 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
8560f226 ED |
1885 | if (!dev->tstats) |
1886 | return -ENOMEM; | |
cdf3464e | 1887 | |
607f725f | 1888 | ret = dst_cache_init(&t->dst_cache, GFP_KERNEL); |
0d3c703a TH |
1889 | if (ret) |
1890 | goto free_stats; | |
1891 | ||
1892 | ret = gro_cells_init(&t->gro_cells, dev); | |
1893 | if (ret) | |
1894 | goto destroy_dst; | |
cdf3464e | 1895 | |
79ecb90e | 1896 | t->tun_hlen = 0; |
058214a4 TH |
1897 | t->hlen = t->encap_hlen + t->tun_hlen; |
1898 | t_hlen = t->hlen + sizeof(struct ipv6hdr); | |
1899 | ||
1900 | dev->type = ARPHRD_TUNNEL6; | |
1901 | dev->hard_header_len = LL_MAX_HEADER + t_hlen; | |
1902 | dev->mtu = ETH_DATA_LEN - t_hlen; | |
1903 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
1904 | dev->mtu -= 8; | |
b96f9afe | 1905 | dev->min_mtu = ETH_MIN_MTU; |
f7ff1fde | 1906 | dev->max_mtu = IP6_MAX_MTU - dev->hard_header_len; |
79ecb90e | 1907 | |
8560f226 | 1908 | return 0; |
0d3c703a TH |
1909 | |
1910 | destroy_dst: | |
1911 | dst_cache_destroy(&t->dst_cache); | |
1912 | free_stats: | |
1913 | free_percpu(dev->tstats); | |
1914 | dev->tstats = NULL; | |
1915 | ||
1916 | return ret; | |
1da177e4 LT |
1917 | } |
1918 | ||
1919 | /** | |
3144581c | 1920 | * ip6_tnl_dev_init - initializer for all non fallback tunnel devices |
1da177e4 LT |
1921 | * @dev: virtual device associated with tunnel |
1922 | **/ | |
1923 | ||
8560f226 | 1924 | static int ip6_tnl_dev_init(struct net_device *dev) |
1da177e4 | 1925 | { |
2941a486 | 1926 | struct ip6_tnl *t = netdev_priv(dev); |
8560f226 ED |
1927 | int err = ip6_tnl_dev_init_gen(dev); |
1928 | ||
1929 | if (err) | |
1930 | return err; | |
3144581c | 1931 | ip6_tnl_link_config(t); |
5311a69a | 1932 | if (t->parms.collect_md) |
8d79266b | 1933 | netif_keep_dst(dev); |
8560f226 | 1934 | return 0; |
1da177e4 LT |
1935 | } |
1936 | ||
1937 | /** | |
3144581c | 1938 | * ip6_fb_tnl_dev_init - initializer for fallback tunnel device |
1da177e4 LT |
1939 | * @dev: fallback device |
1940 | * | |
1941 | * Return: 0 | |
1942 | **/ | |
1943 | ||
8560f226 | 1944 | static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev) |
1da177e4 | 1945 | { |
2941a486 | 1946 | struct ip6_tnl *t = netdev_priv(dev); |
3e6c9fb5 PE |
1947 | struct net *net = dev_net(dev); |
1948 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); | |
1949 | ||
502b0935 | 1950 | t->parms.proto = IPPROTO_IPV6; |
1da177e4 | 1951 | dev_hold(dev); |
d0087b29 | 1952 | |
cf778b00 | 1953 | rcu_assign_pointer(ip6n->tnls_wc[0], t); |
8560f226 | 1954 | return 0; |
1da177e4 LT |
1955 | } |
1956 | ||
a8b8a889 MS |
1957 | static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[], |
1958 | struct netlink_ext_ack *extack) | |
0b112457 ND |
1959 | { |
1960 | u8 proto; | |
1961 | ||
c8965932 | 1962 | if (!data || !data[IFLA_IPTUN_PROTO]) |
0b112457 ND |
1963 | return 0; |
1964 | ||
1965 | proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); | |
1966 | if (proto != IPPROTO_IPV6 && | |
1967 | proto != IPPROTO_IPIP && | |
1968 | proto != 0) | |
1969 | return -EINVAL; | |
1970 | ||
1971 | return 0; | |
1972 | } | |
1973 | ||
1974 | static void ip6_tnl_netlink_parms(struct nlattr *data[], | |
1975 | struct __ip6_tnl_parm *parms) | |
1976 | { | |
1977 | memset(parms, 0, sizeof(*parms)); | |
1978 | ||
1979 | if (!data) | |
1980 | return; | |
1981 | ||
1982 | if (data[IFLA_IPTUN_LINK]) | |
1983 | parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]); | |
1984 | ||
1985 | if (data[IFLA_IPTUN_LOCAL]) | |
67b61f6c | 1986 | parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]); |
0b112457 ND |
1987 | |
1988 | if (data[IFLA_IPTUN_REMOTE]) | |
67b61f6c | 1989 | parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]); |
0b112457 ND |
1990 | |
1991 | if (data[IFLA_IPTUN_TTL]) | |
1992 | parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]); | |
1993 | ||
1994 | if (data[IFLA_IPTUN_ENCAP_LIMIT]) | |
1995 | parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]); | |
1996 | ||
1997 | if (data[IFLA_IPTUN_FLOWINFO]) | |
1ff05fb7 | 1998 | parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]); |
0b112457 ND |
1999 | |
2000 | if (data[IFLA_IPTUN_FLAGS]) | |
2001 | parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]); | |
2002 | ||
2003 | if (data[IFLA_IPTUN_PROTO]) | |
2004 | parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); | |
8d79266b AS |
2005 | |
2006 | if (data[IFLA_IPTUN_COLLECT_METADATA]) | |
2007 | parms->collect_md = true; | |
0a473b82 CG |
2008 | |
2009 | if (data[IFLA_IPTUN_FWMARK]) | |
2010 | parms->fwmark = nla_get_u32(data[IFLA_IPTUN_FWMARK]); | |
0b112457 ND |
2011 | } |
2012 | ||
b3a27b51 TH |
2013 | static bool ip6_tnl_netlink_encap_parms(struct nlattr *data[], |
2014 | struct ip_tunnel_encap *ipencap) | |
2015 | { | |
2016 | bool ret = false; | |
2017 | ||
2018 | memset(ipencap, 0, sizeof(*ipencap)); | |
2019 | ||
2020 | if (!data) | |
2021 | return ret; | |
2022 | ||
2023 | if (data[IFLA_IPTUN_ENCAP_TYPE]) { | |
2024 | ret = true; | |
2025 | ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]); | |
2026 | } | |
2027 | ||
2028 | if (data[IFLA_IPTUN_ENCAP_FLAGS]) { | |
2029 | ret = true; | |
2030 | ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]); | |
2031 | } | |
2032 | ||
2033 | if (data[IFLA_IPTUN_ENCAP_SPORT]) { | |
2034 | ret = true; | |
2035 | ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]); | |
2036 | } | |
2037 | ||
2038 | if (data[IFLA_IPTUN_ENCAP_DPORT]) { | |
2039 | ret = true; | |
2040 | ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]); | |
2041 | } | |
2042 | ||
2043 | return ret; | |
2044 | } | |
2045 | ||
0b112457 | 2046 | static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev, |
7a3f4a18 MS |
2047 | struct nlattr *tb[], struct nlattr *data[], |
2048 | struct netlink_ext_ack *extack) | |
0b112457 ND |
2049 | { |
2050 | struct net *net = dev_net(dev); | |
8d79266b | 2051 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
b3a27b51 | 2052 | struct ip_tunnel_encap ipencap; |
a6aa8044 XL |
2053 | struct ip6_tnl *nt, *t; |
2054 | int err; | |
0b112457 ND |
2055 | |
2056 | nt = netdev_priv(dev); | |
b3a27b51 TH |
2057 | |
2058 | if (ip6_tnl_netlink_encap_parms(data, &ipencap)) { | |
a6aa8044 | 2059 | err = ip6_tnl_encap_setup(nt, &ipencap); |
b3a27b51 TH |
2060 | if (err < 0) |
2061 | return err; | |
2062 | } | |
2063 | ||
0b112457 ND |
2064 | ip6_tnl_netlink_parms(data, &nt->parms); |
2065 | ||
8d79266b AS |
2066 | if (nt->parms.collect_md) { |
2067 | if (rtnl_dereference(ip6n->collect_md_tun)) | |
2068 | return -EEXIST; | |
2069 | } else { | |
2070 | t = ip6_tnl_locate(net, &nt->parms, 0); | |
2071 | if (!IS_ERR(t)) | |
2072 | return -EEXIST; | |
2073 | } | |
0b112457 | 2074 | |
a6aa8044 XL |
2075 | err = ip6_tnl_create2(dev); |
2076 | if (!err && tb[IFLA_MTU]) | |
2077 | ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU])); | |
2078 | ||
2079 | return err; | |
0b112457 ND |
2080 | } |
2081 | ||
2082 | static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[], | |
ad744b22 MS |
2083 | struct nlattr *data[], |
2084 | struct netlink_ext_ack *extack) | |
0b112457 | 2085 | { |
0bd87628 | 2086 | struct ip6_tnl *t = netdev_priv(dev); |
0b112457 | 2087 | struct __ip6_tnl_parm p; |
0bd87628 | 2088 | struct net *net = t->net; |
0b112457 | 2089 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
b3a27b51 | 2090 | struct ip_tunnel_encap ipencap; |
0b112457 ND |
2091 | |
2092 | if (dev == ip6n->fb_tnl_dev) | |
2093 | return -EINVAL; | |
2094 | ||
b3a27b51 TH |
2095 | if (ip6_tnl_netlink_encap_parms(data, &ipencap)) { |
2096 | int err = ip6_tnl_encap_setup(t, &ipencap); | |
2097 | ||
2098 | if (err < 0) | |
2099 | return err; | |
2100 | } | |
0b112457 | 2101 | ip6_tnl_netlink_parms(data, &p); |
8d79266b AS |
2102 | if (p.collect_md) |
2103 | return -EINVAL; | |
0b112457 ND |
2104 | |
2105 | t = ip6_tnl_locate(net, &p, 0); | |
37355565 | 2106 | if (!IS_ERR(t)) { |
0b112457 ND |
2107 | if (t->dev != dev) |
2108 | return -EEXIST; | |
2109 | } else | |
2110 | t = netdev_priv(dev); | |
2111 | ||
2112 | return ip6_tnl_update(t, &p); | |
2113 | } | |
2114 | ||
1e9f3d6f ND |
2115 | static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head) |
2116 | { | |
2117 | struct net *net = dev_net(dev); | |
2118 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); | |
2119 | ||
2120 | if (dev != ip6n->fb_tnl_dev) | |
2121 | unregister_netdevice_queue(dev, head); | |
2122 | } | |
2123 | ||
b58d731a | 2124 | static size_t ip6_tnl_get_size(const struct net_device *dev) |
c075b130 ND |
2125 | { |
2126 | return | |
2127 | /* IFLA_IPTUN_LINK */ | |
2128 | nla_total_size(4) + | |
2129 | /* IFLA_IPTUN_LOCAL */ | |
2130 | nla_total_size(sizeof(struct in6_addr)) + | |
2131 | /* IFLA_IPTUN_REMOTE */ | |
2132 | nla_total_size(sizeof(struct in6_addr)) + | |
2133 | /* IFLA_IPTUN_TTL */ | |
2134 | nla_total_size(1) + | |
2135 | /* IFLA_IPTUN_ENCAP_LIMIT */ | |
2136 | nla_total_size(1) + | |
2137 | /* IFLA_IPTUN_FLOWINFO */ | |
2138 | nla_total_size(4) + | |
2139 | /* IFLA_IPTUN_FLAGS */ | |
2140 | nla_total_size(4) + | |
cfa323b6 ND |
2141 | /* IFLA_IPTUN_PROTO */ |
2142 | nla_total_size(1) + | |
b3a27b51 TH |
2143 | /* IFLA_IPTUN_ENCAP_TYPE */ |
2144 | nla_total_size(2) + | |
2145 | /* IFLA_IPTUN_ENCAP_FLAGS */ | |
2146 | nla_total_size(2) + | |
2147 | /* IFLA_IPTUN_ENCAP_SPORT */ | |
2148 | nla_total_size(2) + | |
2149 | /* IFLA_IPTUN_ENCAP_DPORT */ | |
2150 | nla_total_size(2) + | |
8d79266b AS |
2151 | /* IFLA_IPTUN_COLLECT_METADATA */ |
2152 | nla_total_size(0) + | |
0a473b82 CG |
2153 | /* IFLA_IPTUN_FWMARK */ |
2154 | nla_total_size(4) + | |
c075b130 ND |
2155 | 0; |
2156 | } | |
2157 | ||
b58d731a | 2158 | static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev) |
c075b130 ND |
2159 | { |
2160 | struct ip6_tnl *tunnel = netdev_priv(dev); | |
2161 | struct __ip6_tnl_parm *parm = &tunnel->parms; | |
2162 | ||
2163 | if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) || | |
930345ea JB |
2164 | nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) || |
2165 | nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) || | |
c075b130 ND |
2166 | nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) || |
2167 | nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) || | |
2168 | nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) || | |
cfa323b6 | 2169 | nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) || |
0a473b82 CG |
2170 | nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto) || |
2171 | nla_put_u32(skb, IFLA_IPTUN_FWMARK, parm->fwmark)) | |
c075b130 | 2172 | goto nla_put_failure; |
b3a27b51 | 2173 | |
8d79266b AS |
2174 | if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE, tunnel->encap.type) || |
2175 | nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT, tunnel->encap.sport) || | |
2176 | nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT, tunnel->encap.dport) || | |
2177 | nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS, tunnel->encap.flags)) | |
b3a27b51 TH |
2178 | goto nla_put_failure; |
2179 | ||
8d79266b AS |
2180 | if (parm->collect_md) |
2181 | if (nla_put_flag(skb, IFLA_IPTUN_COLLECT_METADATA)) | |
2182 | goto nla_put_failure; | |
0a473b82 | 2183 | |
c075b130 ND |
2184 | return 0; |
2185 | ||
2186 | nla_put_failure: | |
2187 | return -EMSGSIZE; | |
2188 | } | |
2189 | ||
1728d4fa ND |
2190 | struct net *ip6_tnl_get_link_net(const struct net_device *dev) |
2191 | { | |
2192 | struct ip6_tnl *tunnel = netdev_priv(dev); | |
2193 | ||
2194 | return tunnel->net; | |
2195 | } | |
2196 | EXPORT_SYMBOL(ip6_tnl_get_link_net); | |
2197 | ||
0b112457 ND |
2198 | static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = { |
2199 | [IFLA_IPTUN_LINK] = { .type = NLA_U32 }, | |
2200 | [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) }, | |
2201 | [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) }, | |
2202 | [IFLA_IPTUN_TTL] = { .type = NLA_U8 }, | |
2203 | [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 }, | |
2204 | [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 }, | |
2205 | [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 }, | |
2206 | [IFLA_IPTUN_PROTO] = { .type = NLA_U8 }, | |
b3a27b51 TH |
2207 | [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 }, |
2208 | [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 }, | |
2209 | [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 }, | |
2210 | [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 }, | |
8d79266b | 2211 | [IFLA_IPTUN_COLLECT_METADATA] = { .type = NLA_FLAG }, |
0a473b82 | 2212 | [IFLA_IPTUN_FWMARK] = { .type = NLA_U32 }, |
0b112457 ND |
2213 | }; |
2214 | ||
c075b130 ND |
2215 | static struct rtnl_link_ops ip6_link_ops __read_mostly = { |
2216 | .kind = "ip6tnl", | |
2217 | .maxtype = IFLA_IPTUN_MAX, | |
0b112457 | 2218 | .policy = ip6_tnl_policy, |
c075b130 | 2219 | .priv_size = sizeof(struct ip6_tnl), |
0b112457 ND |
2220 | .setup = ip6_tnl_dev_setup, |
2221 | .validate = ip6_tnl_validate, | |
2222 | .newlink = ip6_tnl_newlink, | |
2223 | .changelink = ip6_tnl_changelink, | |
1e9f3d6f | 2224 | .dellink = ip6_tnl_dellink, |
b58d731a ND |
2225 | .get_size = ip6_tnl_get_size, |
2226 | .fill_info = ip6_tnl_fill_info, | |
1728d4fa | 2227 | .get_link_net = ip6_tnl_get_link_net, |
c075b130 ND |
2228 | }; |
2229 | ||
3ff2cfa5 | 2230 | static struct xfrm6_tunnel ip4ip6_handler __read_mostly = { |
c4d3efaf YK |
2231 | .handler = ip4ip6_rcv, |
2232 | .err_handler = ip4ip6_err, | |
2233 | .priority = 1, | |
2234 | }; | |
2235 | ||
3ff2cfa5 | 2236 | static struct xfrm6_tunnel ip6ip6_handler __read_mostly = { |
0303770d PM |
2237 | .handler = ip6ip6_rcv, |
2238 | .err_handler = ip6ip6_err, | |
d2acc347 | 2239 | .priority = 1, |
1da177e4 LT |
2240 | }; |
2241 | ||
f200e98d VF |
2242 | static struct xfrm6_tunnel mplsip6_handler __read_mostly = { |
2243 | .handler = mplsip6_rcv, | |
2244 | .err_handler = mplsip6_err, | |
2245 | .priority = 1, | |
2246 | }; | |
2247 | ||
bb401cae | 2248 | static void __net_exit ip6_tnl_destroy_tunnels(struct net *net, struct list_head *list) |
3e6c9fb5 | 2249 | { |
1e9f3d6f | 2250 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
0bd87628 | 2251 | struct net_device *dev, *aux; |
3e6c9fb5 PE |
2252 | int h; |
2253 | struct ip6_tnl *t; | |
2254 | ||
0bd87628 ND |
2255 | for_each_netdev_safe(net, dev, aux) |
2256 | if (dev->rtnl_link_ops == &ip6_link_ops) | |
bb401cae | 2257 | unregister_netdevice_queue(dev, list); |
0bd87628 | 2258 | |
e87a8f24 | 2259 | for (h = 0; h < IP6_TUNNEL_HASH_SIZE; h++) { |
94767632 | 2260 | t = rtnl_dereference(ip6n->tnls_r_l[h]); |
53b24b8f | 2261 | while (t) { |
0bd87628 ND |
2262 | /* If dev is in the same netns, it has already |
2263 | * been added to the list by the previous loop. | |
2264 | */ | |
2265 | if (!net_eq(dev_net(t->dev), net)) | |
bb401cae | 2266 | unregister_netdevice_queue(t->dev, list); |
94767632 | 2267 | t = rtnl_dereference(t->next); |
cf4432f5 | 2268 | } |
3e6c9fb5 | 2269 | } |
3e6c9fb5 PE |
2270 | } |
2271 | ||
2c8c1e72 | 2272 | static int __net_init ip6_tnl_init_net(struct net *net) |
13eeb8e9 | 2273 | { |
ac31cd3c | 2274 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
731abb9c | 2275 | struct ip6_tnl *t = NULL; |
13eeb8e9 | 2276 | int err; |
13eeb8e9 | 2277 | |
3e6c9fb5 PE |
2278 | ip6n->tnls[0] = ip6n->tnls_wc; |
2279 | ip6n->tnls[1] = ip6n->tnls_r_l; | |
2280 | ||
79134e6c ED |
2281 | if (!net_has_fallback_tunnels(net)) |
2282 | return 0; | |
15820e12 PE |
2283 | err = -ENOMEM; |
2284 | ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0", | |
c835a677 | 2285 | NET_NAME_UNKNOWN, ip6_tnl_dev_setup); |
15820e12 PE |
2286 | |
2287 | if (!ip6n->fb_tnl_dev) | |
2288 | goto err_alloc_dev; | |
be77e593 | 2289 | dev_net_set(ip6n->fb_tnl_dev, net); |
bb814094 | 2290 | ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops; |
0bd87628 ND |
2291 | /* FB netdevice is special: we have one, and only one per netns. |
2292 | * Allowing to move it to another netns is clearly unsafe. | |
2293 | */ | |
2294 | ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL; | |
15820e12 | 2295 | |
8560f226 ED |
2296 | err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev); |
2297 | if (err < 0) | |
2298 | goto err_register; | |
15820e12 PE |
2299 | |
2300 | err = register_netdev(ip6n->fb_tnl_dev); | |
2301 | if (err < 0) | |
2302 | goto err_register; | |
731abb9c JB |
2303 | |
2304 | t = netdev_priv(ip6n->fb_tnl_dev); | |
2305 | ||
2306 | strcpy(t->parms.name, ip6n->fb_tnl_dev->name); | |
13eeb8e9 PE |
2307 | return 0; |
2308 | ||
15820e12 | 2309 | err_register: |
cf124db5 | 2310 | free_netdev(ip6n->fb_tnl_dev); |
15820e12 | 2311 | err_alloc_dev: |
13eeb8e9 PE |
2312 | return err; |
2313 | } | |
2314 | ||
bb401cae | 2315 | static void __net_exit ip6_tnl_exit_batch_net(struct list_head *net_list) |
13eeb8e9 | 2316 | { |
bb401cae ED |
2317 | struct net *net; |
2318 | LIST_HEAD(list); | |
2319 | ||
3e6c9fb5 | 2320 | rtnl_lock(); |
bb401cae ED |
2321 | list_for_each_entry(net, net_list, exit_list) |
2322 | ip6_tnl_destroy_tunnels(net, &list); | |
2323 | unregister_netdevice_many(&list); | |
3e6c9fb5 | 2324 | rtnl_unlock(); |
13eeb8e9 PE |
2325 | } |
2326 | ||
2327 | static struct pernet_operations ip6_tnl_net_ops = { | |
2328 | .init = ip6_tnl_init_net, | |
bb401cae | 2329 | .exit_batch = ip6_tnl_exit_batch_net, |
ac31cd3c EB |
2330 | .id = &ip6_tnl_net_id, |
2331 | .size = sizeof(struct ip6_tnl_net), | |
13eeb8e9 PE |
2332 | }; |
2333 | ||
1da177e4 LT |
2334 | /** |
2335 | * ip6_tunnel_init - register protocol and reserve needed resources | |
2336 | * | |
2337 | * Return: 0 on success | |
2338 | **/ | |
2339 | ||
2340 | static int __init ip6_tunnel_init(void) | |
2341 | { | |
2342 | int err; | |
2343 | ||
8c22dab0 XL |
2344 | if (!ipv6_mod_enabled()) |
2345 | return -EOPNOTSUPP; | |
2346 | ||
d5aa407f AD |
2347 | err = register_pernet_device(&ip6_tnl_net_ops); |
2348 | if (err < 0) | |
2349 | goto out_pernet; | |
2350 | ||
2351 | err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET); | |
2352 | if (err < 0) { | |
f3213831 | 2353 | pr_err("%s: can't register ip4ip6\n", __func__); |
d5aa407f | 2354 | goto out_ip4ip6; |
c4d3efaf YK |
2355 | } |
2356 | ||
d5aa407f AD |
2357 | err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6); |
2358 | if (err < 0) { | |
f3213831 | 2359 | pr_err("%s: can't register ip6ip6\n", __func__); |
d5aa407f | 2360 | goto out_ip6ip6; |
1da177e4 | 2361 | } |
f200e98d VF |
2362 | |
2363 | if (ip6_tnl_mpls_supported()) { | |
2364 | err = xfrm6_tunnel_register(&mplsip6_handler, AF_MPLS); | |
2365 | if (err < 0) { | |
2366 | pr_err("%s: can't register mplsip6\n", __func__); | |
2367 | goto out_mplsip6; | |
2368 | } | |
2369 | } | |
2370 | ||
c075b130 ND |
2371 | err = rtnl_link_register(&ip6_link_ops); |
2372 | if (err < 0) | |
2373 | goto rtnl_link_failed; | |
13eeb8e9 | 2374 | |
1da177e4 | 2375 | return 0; |
d5aa407f | 2376 | |
c075b130 | 2377 | rtnl_link_failed: |
f200e98d VF |
2378 | if (ip6_tnl_mpls_supported()) |
2379 | xfrm6_tunnel_deregister(&mplsip6_handler, AF_MPLS); | |
2380 | out_mplsip6: | |
c075b130 | 2381 | xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6); |
d5aa407f | 2382 | out_ip6ip6: |
c4d3efaf | 2383 | xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET); |
d5aa407f AD |
2384 | out_ip4ip6: |
2385 | unregister_pernet_device(&ip6_tnl_net_ops); | |
2386 | out_pernet: | |
1da177e4 LT |
2387 | return err; |
2388 | } | |
2389 | ||
2390 | /** | |
2391 | * ip6_tunnel_cleanup - free resources and unregister protocol | |
2392 | **/ | |
2393 | ||
2394 | static void __exit ip6_tunnel_cleanup(void) | |
2395 | { | |
c075b130 | 2396 | rtnl_link_unregister(&ip6_link_ops); |
c4d3efaf | 2397 | if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET)) |
f3213831 | 2398 | pr_info("%s: can't deregister ip4ip6\n", __func__); |
c4d3efaf | 2399 | |
73d605d1 | 2400 | if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6)) |
f3213831 | 2401 | pr_info("%s: can't deregister ip6ip6\n", __func__); |
1da177e4 | 2402 | |
f200e98d VF |
2403 | if (ip6_tnl_mpls_supported() && |
2404 | xfrm6_tunnel_deregister(&mplsip6_handler, AF_MPLS)) | |
2405 | pr_info("%s: can't deregister mplsip6\n", __func__); | |
ac31cd3c | 2406 | unregister_pernet_device(&ip6_tnl_net_ops); |
1da177e4 LT |
2407 | } |
2408 | ||
2409 | module_init(ip6_tunnel_init); | |
2410 | module_exit(ip6_tunnel_cleanup); |