]>
Commit | Line | Data |
---|---|---|
c12b395a | 1 | /* |
2 | * GRE over IPv6 protocol decoder. | |
3 | * | |
4 | * Authors: Dmitry Kozlov (xeb@mail.ru) | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | */ | |
12 | ||
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
14 | ||
15 | #include <linux/capability.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/types.h> | |
18 | #include <linux/kernel.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/uaccess.h> | |
21 | #include <linux/skbuff.h> | |
22 | #include <linux/netdevice.h> | |
23 | #include <linux/in.h> | |
24 | #include <linux/tcp.h> | |
25 | #include <linux/udp.h> | |
26 | #include <linux/if_arp.h> | |
c12b395a | 27 | #include <linux/init.h> |
28 | #include <linux/in6.h> | |
29 | #include <linux/inetdevice.h> | |
30 | #include <linux/igmp.h> | |
31 | #include <linux/netfilter_ipv4.h> | |
32 | #include <linux/etherdevice.h> | |
33 | #include <linux/if_ether.h> | |
34 | #include <linux/hash.h> | |
35 | #include <linux/if_tunnel.h> | |
36 | #include <linux/ip6_tunnel.h> | |
37 | ||
38 | #include <net/sock.h> | |
39 | #include <net/ip.h> | |
c5441932 | 40 | #include <net/ip_tunnels.h> |
c12b395a | 41 | #include <net/icmp.h> |
42 | #include <net/protocol.h> | |
43 | #include <net/addrconf.h> | |
44 | #include <net/arp.h> | |
45 | #include <net/checksum.h> | |
46 | #include <net/dsfield.h> | |
47 | #include <net/inet_ecn.h> | |
48 | #include <net/xfrm.h> | |
49 | #include <net/net_namespace.h> | |
50 | #include <net/netns/generic.h> | |
51 | #include <net/rtnetlink.h> | |
52 | ||
53 | #include <net/ipv6.h> | |
54 | #include <net/ip6_fib.h> | |
55 | #include <net/ip6_route.h> | |
56 | #include <net/ip6_tunnel.h> | |
308edfdf | 57 | #include <net/gre.h> |
5a963eb6 | 58 | #include <net/erspan.h> |
6712abc1 | 59 | #include <net/dst_metadata.h> |
c12b395a | 60 | |
61 | ||
eccc1bb8 | 62 | static bool log_ecn_error = true; |
63 | module_param(log_ecn_error, bool, 0644); | |
64 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |
65 | ||
e87a8f24 JK |
66 | #define IP6_GRE_HASH_SIZE_SHIFT 5 |
67 | #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT) | |
c12b395a | 68 | |
c7d03a00 | 69 | static unsigned int ip6gre_net_id __read_mostly; |
c12b395a | 70 | struct ip6gre_net { |
e87a8f24 | 71 | struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE]; |
c12b395a | 72 | |
6712abc1 | 73 | struct ip6_tnl __rcu *collect_md_tun; |
c12b395a | 74 | struct net_device *fb_tunnel_dev; |
75 | }; | |
76 | ||
77 | static struct rtnl_link_ops ip6gre_link_ops __read_mostly; | |
22f08069 | 78 | static struct rtnl_link_ops ip6gre_tap_ops __read_mostly; |
5a963eb6 | 79 | static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly; |
c12b395a | 80 | static int ip6gre_tunnel_init(struct net_device *dev); |
81 | static void ip6gre_tunnel_setup(struct net_device *dev); | |
82 | static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t); | |
83 | static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu); | |
84 | ||
85 | /* Tunnel hash table */ | |
86 | ||
87 | /* | |
88 | 4 hash tables: | |
89 | ||
90 | 3: (remote,local) | |
91 | 2: (remote,*) | |
92 | 1: (*,local) | |
93 | 0: (*,*) | |
94 | ||
95 | We require exact key match i.e. if a key is present in packet | |
96 | it will match only tunnel with the same key; if it is not present, | |
97 | it will match only keyless tunnel. | |
98 | ||
99 | All keysless packets, if not matched configured keyless tunnels | |
100 | will match fallback tunnel. | |
101 | */ | |
102 | ||
e87a8f24 | 103 | #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1)) |
c12b395a | 104 | static u32 HASH_ADDR(const struct in6_addr *addr) |
105 | { | |
106 | u32 hash = ipv6_addr_hash(addr); | |
107 | ||
e87a8f24 | 108 | return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT); |
c12b395a | 109 | } |
110 | ||
111 | #define tunnels_r_l tunnels[3] | |
112 | #define tunnels_r tunnels[2] | |
113 | #define tunnels_l tunnels[1] | |
114 | #define tunnels_wc tunnels[0] | |
c12b395a | 115 | |
c12b395a | 116 | /* Given src, dst and key, find appropriate for input tunnel. */ |
117 | ||
118 | static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev, | |
119 | const struct in6_addr *remote, const struct in6_addr *local, | |
120 | __be32 key, __be16 gre_proto) | |
121 | { | |
122 | struct net *net = dev_net(dev); | |
123 | int link = dev->ifindex; | |
124 | unsigned int h0 = HASH_ADDR(remote); | |
125 | unsigned int h1 = HASH_KEY(key); | |
126 | struct ip6_tnl *t, *cand = NULL; | |
127 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
5a963eb6 WT |
128 | int dev_type = (gre_proto == htons(ETH_P_TEB) || |
129 | gre_proto == htons(ETH_P_ERSPAN)) ? | |
c12b395a | 130 | ARPHRD_ETHER : ARPHRD_IP6GRE; |
131 | int score, cand_score = 4; | |
132 | ||
e086cadc | 133 | for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) { |
c12b395a | 134 | if (!ipv6_addr_equal(local, &t->parms.laddr) || |
135 | !ipv6_addr_equal(remote, &t->parms.raddr) || | |
136 | key != t->parms.i_key || | |
137 | !(t->dev->flags & IFF_UP)) | |
138 | continue; | |
139 | ||
140 | if (t->dev->type != ARPHRD_IP6GRE && | |
141 | t->dev->type != dev_type) | |
142 | continue; | |
143 | ||
144 | score = 0; | |
145 | if (t->parms.link != link) | |
146 | score |= 1; | |
147 | if (t->dev->type != dev_type) | |
148 | score |= 2; | |
149 | if (score == 0) | |
150 | return t; | |
151 | ||
152 | if (score < cand_score) { | |
153 | cand = t; | |
154 | cand_score = score; | |
155 | } | |
156 | } | |
157 | ||
e086cadc | 158 | for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) { |
c12b395a | 159 | if (!ipv6_addr_equal(remote, &t->parms.raddr) || |
160 | key != t->parms.i_key || | |
161 | !(t->dev->flags & IFF_UP)) | |
162 | continue; | |
163 | ||
164 | if (t->dev->type != ARPHRD_IP6GRE && | |
165 | t->dev->type != dev_type) | |
166 | continue; | |
167 | ||
168 | score = 0; | |
169 | if (t->parms.link != link) | |
170 | score |= 1; | |
171 | if (t->dev->type != dev_type) | |
172 | score |= 2; | |
173 | if (score == 0) | |
174 | return t; | |
175 | ||
176 | if (score < cand_score) { | |
177 | cand = t; | |
178 | cand_score = score; | |
179 | } | |
180 | } | |
181 | ||
e086cadc | 182 | for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) { |
c12b395a | 183 | if ((!ipv6_addr_equal(local, &t->parms.laddr) && |
184 | (!ipv6_addr_equal(local, &t->parms.raddr) || | |
185 | !ipv6_addr_is_multicast(local))) || | |
186 | key != t->parms.i_key || | |
187 | !(t->dev->flags & IFF_UP)) | |
188 | continue; | |
189 | ||
190 | if (t->dev->type != ARPHRD_IP6GRE && | |
191 | t->dev->type != dev_type) | |
192 | continue; | |
193 | ||
194 | score = 0; | |
195 | if (t->parms.link != link) | |
196 | score |= 1; | |
197 | if (t->dev->type != dev_type) | |
198 | score |= 2; | |
199 | if (score == 0) | |
200 | return t; | |
201 | ||
202 | if (score < cand_score) { | |
203 | cand = t; | |
204 | cand_score = score; | |
205 | } | |
206 | } | |
207 | ||
e086cadc | 208 | for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) { |
c12b395a | 209 | if (t->parms.i_key != key || |
210 | !(t->dev->flags & IFF_UP)) | |
211 | continue; | |
212 | ||
213 | if (t->dev->type != ARPHRD_IP6GRE && | |
214 | t->dev->type != dev_type) | |
215 | continue; | |
216 | ||
217 | score = 0; | |
218 | if (t->parms.link != link) | |
219 | score |= 1; | |
220 | if (t->dev->type != dev_type) | |
221 | score |= 2; | |
222 | if (score == 0) | |
223 | return t; | |
224 | ||
225 | if (score < cand_score) { | |
226 | cand = t; | |
227 | cand_score = score; | |
228 | } | |
229 | } | |
230 | ||
53b24b8f | 231 | if (cand) |
c12b395a | 232 | return cand; |
233 | ||
6712abc1 WT |
234 | t = rcu_dereference(ign->collect_md_tun); |
235 | if (t && t->dev->flags & IFF_UP) | |
236 | return t; | |
237 | ||
c12b395a | 238 | dev = ign->fb_tunnel_dev; |
239 | if (dev->flags & IFF_UP) | |
240 | return netdev_priv(dev); | |
241 | ||
242 | return NULL; | |
243 | } | |
244 | ||
245 | static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign, | |
246 | const struct __ip6_tnl_parm *p) | |
247 | { | |
248 | const struct in6_addr *remote = &p->raddr; | |
249 | const struct in6_addr *local = &p->laddr; | |
250 | unsigned int h = HASH_KEY(p->i_key); | |
251 | int prio = 0; | |
252 | ||
253 | if (!ipv6_addr_any(local)) | |
254 | prio |= 1; | |
255 | if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) { | |
256 | prio |= 2; | |
257 | h ^= HASH_ADDR(remote); | |
258 | } | |
259 | ||
260 | return &ign->tunnels[prio][h]; | |
261 | } | |
262 | ||
263 | static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign, | |
264 | const struct ip6_tnl *t) | |
265 | { | |
266 | return __ip6gre_bucket(ign, &t->parms); | |
267 | } | |
268 | ||
269 | static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t) | |
270 | { | |
271 | struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t); | |
272 | ||
6712abc1 WT |
273 | if (t->parms.collect_md) |
274 | rcu_assign_pointer(ign->collect_md_tun, t); | |
275 | ||
c12b395a | 276 | rcu_assign_pointer(t->next, rtnl_dereference(*tp)); |
277 | rcu_assign_pointer(*tp, t); | |
278 | } | |
279 | ||
280 | static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t) | |
281 | { | |
282 | struct ip6_tnl __rcu **tp; | |
283 | struct ip6_tnl *iter; | |
284 | ||
6712abc1 WT |
285 | if (t->parms.collect_md) |
286 | rcu_assign_pointer(ign->collect_md_tun, NULL); | |
287 | ||
c12b395a | 288 | for (tp = ip6gre_bucket(ign, t); |
289 | (iter = rtnl_dereference(*tp)) != NULL; | |
290 | tp = &iter->next) { | |
291 | if (t == iter) { | |
292 | rcu_assign_pointer(*tp, t->next); | |
293 | break; | |
294 | } | |
295 | } | |
296 | } | |
297 | ||
298 | static struct ip6_tnl *ip6gre_tunnel_find(struct net *net, | |
299 | const struct __ip6_tnl_parm *parms, | |
300 | int type) | |
301 | { | |
302 | const struct in6_addr *remote = &parms->raddr; | |
303 | const struct in6_addr *local = &parms->laddr; | |
304 | __be32 key = parms->i_key; | |
305 | int link = parms->link; | |
306 | struct ip6_tnl *t; | |
307 | struct ip6_tnl __rcu **tp; | |
308 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
309 | ||
310 | for (tp = __ip6gre_bucket(ign, parms); | |
311 | (t = rtnl_dereference(*tp)) != NULL; | |
312 | tp = &t->next) | |
313 | if (ipv6_addr_equal(local, &t->parms.laddr) && | |
314 | ipv6_addr_equal(remote, &t->parms.raddr) && | |
315 | key == t->parms.i_key && | |
316 | link == t->parms.link && | |
317 | type == t->dev->type) | |
318 | break; | |
319 | ||
320 | return t; | |
321 | } | |
322 | ||
323 | static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net, | |
324 | const struct __ip6_tnl_parm *parms, int create) | |
325 | { | |
326 | struct ip6_tnl *t, *nt; | |
327 | struct net_device *dev; | |
328 | char name[IFNAMSIZ]; | |
329 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
330 | ||
331 | t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE); | |
cd0a0bd9 SK |
332 | if (t && create) |
333 | return NULL; | |
c12b395a | 334 | if (t || !create) |
335 | return t; | |
336 | ||
337 | if (parms->name[0]) | |
338 | strlcpy(name, parms->name, IFNAMSIZ); | |
339 | else | |
340 | strcpy(name, "ip6gre%d"); | |
341 | ||
c835a677 TG |
342 | dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, |
343 | ip6gre_tunnel_setup); | |
c12b395a | 344 | if (!dev) |
345 | return NULL; | |
346 | ||
347 | dev_net_set(dev, net); | |
348 | ||
349 | nt = netdev_priv(dev); | |
350 | nt->parms = *parms; | |
351 | dev->rtnl_link_ops = &ip6gre_link_ops; | |
352 | ||
353 | nt->dev = dev; | |
0bd87628 | 354 | nt->net = dev_net(dev); |
c12b395a | 355 | |
356 | if (register_netdevice(dev) < 0) | |
357 | goto failed_free; | |
358 | ||
128bb975 AK |
359 | ip6gre_tnl_link_config(nt, 1); |
360 | ||
c12b395a | 361 | /* Can use a lockless transmit, unless we generate output sequences */ |
b45bd1d7 | 362 | if (!(nt->parms.o_flags & TUNNEL_SEQ)) |
c12b395a | 363 | dev->features |= NETIF_F_LLTX; |
364 | ||
365 | dev_hold(dev); | |
366 | ip6gre_tunnel_link(ign, nt); | |
367 | return nt; | |
368 | ||
369 | failed_free: | |
370 | free_netdev(dev); | |
371 | return NULL; | |
372 | } | |
373 | ||
374 | static void ip6gre_tunnel_uninit(struct net_device *dev) | |
375 | { | |
22f08069 ND |
376 | struct ip6_tnl *t = netdev_priv(dev); |
377 | struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id); | |
c12b395a | 378 | |
22f08069 | 379 | ip6gre_tunnel_unlink(ign, t); |
607f725f | 380 | dst_cache_reset(&t->dst_cache); |
c12b395a | 381 | dev_put(dev); |
382 | } | |
383 | ||
384 | ||
385 | static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
7892032c | 386 | u8 type, u8 code, int offset, __be32 info) |
c12b395a | 387 | { |
929fc032 | 388 | struct net *net = dev_net(skb->dev); |
7892032c ED |
389 | const struct gre_base_hdr *greh; |
390 | const struct ipv6hdr *ipv6h; | |
391 | int grehlen = sizeof(*greh); | |
c12b395a | 392 | struct ip6_tnl *t; |
7892032c | 393 | int key_off = 0; |
c12b395a | 394 | __be16 flags; |
7892032c | 395 | __be32 key; |
c12b395a | 396 | |
7892032c ED |
397 | if (!pskb_may_pull(skb, offset + grehlen)) |
398 | return; | |
399 | greh = (const struct gre_base_hdr *)(skb->data + offset); | |
400 | flags = greh->flags; | |
401 | if (flags & (GRE_VERSION | GRE_ROUTING)) | |
402 | return; | |
403 | if (flags & GRE_CSUM) | |
404 | grehlen += 4; | |
405 | if (flags & GRE_KEY) { | |
406 | key_off = grehlen + offset; | |
407 | grehlen += 4; | |
c12b395a | 408 | } |
409 | ||
7892032c | 410 | if (!pskb_may_pull(skb, offset + grehlen)) |
c12b395a | 411 | return; |
b87fb39e | 412 | ipv6h = (const struct ipv6hdr *)skb->data; |
7892032c ED |
413 | greh = (const struct gre_base_hdr *)(skb->data + offset); |
414 | key = key_off ? *(__be32 *)(skb->data + key_off) : 0; | |
c12b395a | 415 | |
c12b395a | 416 | t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr, |
7892032c | 417 | key, greh->protocol); |
63159f29 | 418 | if (!t) |
0c5794a6 | 419 | return; |
c12b395a | 420 | |
421 | switch (type) { | |
c12b395a | 422 | struct ipv6_tlv_tnl_enc_lim *tel; |
fe1a4ca0 | 423 | __u32 teli; |
c12b395a | 424 | case ICMPV6_DEST_UNREACH: |
a46496ce MB |
425 | net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n", |
426 | t->parms.name); | |
f8d20b46 XL |
427 | if (code != ICMPV6_PORT_UNREACH) |
428 | break; | |
429 | return; | |
c12b395a | 430 | case ICMPV6_TIME_EXCEED: |
431 | if (code == ICMPV6_EXC_HOPLIMIT) { | |
a46496ce MB |
432 | net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n", |
433 | t->parms.name); | |
f8d20b46 | 434 | break; |
c12b395a | 435 | } |
f8d20b46 | 436 | return; |
c12b395a | 437 | case ICMPV6_PARAMPROB: |
438 | teli = 0; | |
439 | if (code == ICMPV6_HDR_FIELD) | |
440 | teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data); | |
441 | ||
d1e158e2 | 442 | if (teli && teli == be32_to_cpu(info) - 2) { |
c12b395a | 443 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; |
444 | if (tel->encap_limit == 0) { | |
a46496ce MB |
445 | net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n", |
446 | t->parms.name); | |
c12b395a | 447 | } |
448 | } else { | |
a46496ce MB |
449 | net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n", |
450 | t->parms.name); | |
c12b395a | 451 | } |
f8d20b46 | 452 | return; |
c12b395a | 453 | case ICMPV6_PKT_TOOBIG: |
fe1a4ca0 | 454 | ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL)); |
f8d20b46 | 455 | return; |
929fc032 XL |
456 | case NDISC_REDIRECT: |
457 | ip6_redirect(skb, net, skb->dev->ifindex, 0, | |
458 | sock_net_uid(net, NULL)); | |
459 | return; | |
c12b395a | 460 | } |
461 | ||
462 | if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO)) | |
463 | t->err_count++; | |
464 | else | |
465 | t->err_count = 1; | |
466 | t->err_time = jiffies; | |
c12b395a | 467 | } |
468 | ||
308edfdf | 469 | static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi) |
c12b395a | 470 | { |
471 | const struct ipv6hdr *ipv6h; | |
c12b395a | 472 | struct ip6_tnl *tunnel; |
c12b395a | 473 | |
474 | ipv6h = ipv6_hdr(skb); | |
c12b395a | 475 | tunnel = ip6gre_tunnel_lookup(skb->dev, |
308edfdf TH |
476 | &ipv6h->saddr, &ipv6h->daddr, tpi->key, |
477 | tpi->proto); | |
c12b395a | 478 | if (tunnel) { |
6712abc1 WT |
479 | if (tunnel->parms.collect_md) { |
480 | struct metadata_dst *tun_dst; | |
481 | __be64 tun_id; | |
482 | __be16 flags; | |
483 | ||
484 | flags = tpi->flags; | |
485 | tun_id = key32_to_tunnel_id(tpi->key); | |
486 | ||
487 | tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, 0); | |
488 | if (!tun_dst) | |
489 | return PACKET_REJECT; | |
490 | ||
491 | ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); | |
492 | } else { | |
493 | ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error); | |
494 | } | |
c12b395a | 495 | |
308edfdf TH |
496 | return PACKET_RCVD; |
497 | } | |
eccc1bb8 | 498 | |
308edfdf TH |
499 | return PACKET_REJECT; |
500 | } | |
eccc1bb8 | 501 | |
5a963eb6 WT |
502 | static int ip6erspan_rcv(struct sk_buff *skb, int gre_hdr_len, |
503 | struct tnl_ptk_info *tpi) | |
504 | { | |
1d7e2ed2 WT |
505 | struct erspan_base_hdr *ershdr; |
506 | struct erspan_metadata *pkt_md; | |
5a963eb6 | 507 | const struct ipv6hdr *ipv6h; |
5a963eb6 | 508 | struct ip6_tnl *tunnel; |
1d7e2ed2 | 509 | u8 ver; |
5a963eb6 | 510 | |
5a963eb6 WT |
511 | if (unlikely(!pskb_may_pull(skb, sizeof(*ershdr)))) |
512 | return PACKET_REJECT; | |
513 | ||
293a1991 HY |
514 | ipv6h = ipv6_hdr(skb); |
515 | ershdr = (struct erspan_base_hdr *)skb->data; | |
1d7e2ed2 | 516 | ver = (ntohs(ershdr->ver_vlan) & VER_MASK) >> VER_OFFSET; |
5a963eb6 | 517 | tpi->key = cpu_to_be32(ntohs(ershdr->session_id) & ID_MASK); |
5a963eb6 WT |
518 | |
519 | tunnel = ip6gre_tunnel_lookup(skb->dev, | |
520 | &ipv6h->saddr, &ipv6h->daddr, tpi->key, | |
521 | tpi->proto); | |
522 | if (tunnel) { | |
1d7e2ed2 WT |
523 | int len = erspan_hdr_len(ver); |
524 | ||
525 | if (unlikely(!pskb_may_pull(skb, len))) | |
ae3e1337 | 526 | return PACKET_REJECT; |
1d7e2ed2 | 527 | |
d91e8db5 WT |
528 | ershdr = (struct erspan_base_hdr *)skb->data; |
529 | pkt_md = (struct erspan_metadata *)(ershdr + 1); | |
530 | ||
1d7e2ed2 | 531 | if (__iptunnel_pull_header(skb, len, |
5a963eb6 WT |
532 | htons(ETH_P_TEB), |
533 | false, false) < 0) | |
534 | return PACKET_REJECT; | |
535 | ||
ef7baf5e WT |
536 | if (tunnel->parms.collect_md) { |
537 | struct metadata_dst *tun_dst; | |
538 | struct ip_tunnel_info *info; | |
539 | struct erspan_metadata *md; | |
540 | __be64 tun_id; | |
541 | __be16 flags; | |
542 | ||
543 | tpi->flags |= TUNNEL_KEY; | |
544 | flags = tpi->flags; | |
545 | tun_id = key32_to_tunnel_id(tpi->key); | |
546 | ||
547 | tun_dst = ipv6_tun_rx_dst(skb, flags, tun_id, | |
548 | sizeof(*md)); | |
549 | if (!tun_dst) | |
550 | return PACKET_REJECT; | |
551 | ||
552 | info = &tun_dst->u.tun_info; | |
553 | md = ip_tunnel_info_opts(info); | |
ef7baf5e | 554 | |
1d7e2ed2 | 555 | memcpy(md, pkt_md, sizeof(*md)); |
94d7d8f2 | 556 | md->version = ver; |
ef7baf5e WT |
557 | info->key.tun_flags |= TUNNEL_ERSPAN_OPT; |
558 | info->options_len = sizeof(*md); | |
559 | ||
560 | ip6_tnl_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); | |
561 | ||
562 | } else { | |
94d7d8f2 WT |
563 | tunnel->parms.erspan_ver = ver; |
564 | ||
565 | if (ver == 1) { | |
566 | tunnel->parms.index = ntohl(pkt_md->u.index); | |
567 | } else { | |
568 | u16 md2_flags; | |
569 | u16 dir, hwid; | |
570 | ||
571 | md2_flags = ntohs(pkt_md->u.md2.flags); | |
572 | dir = (md2_flags & DIR_MASK) >> DIR_OFFSET; | |
573 | hwid = (md2_flags & HWID_MASK) >> HWID_OFFSET; | |
574 | tunnel->parms.dir = dir; | |
575 | tunnel->parms.hwid = hwid; | |
576 | } | |
577 | ||
ef7baf5e WT |
578 | ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error); |
579 | } | |
5a963eb6 WT |
580 | |
581 | return PACKET_RCVD; | |
582 | } | |
583 | ||
584 | return PACKET_REJECT; | |
585 | } | |
586 | ||
308edfdf TH |
587 | static int gre_rcv(struct sk_buff *skb) |
588 | { | |
589 | struct tnl_ptk_info tpi; | |
590 | bool csum_err = false; | |
591 | int hdr_len; | |
eccc1bb8 | 592 | |
e582615a | 593 | hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0); |
f132ae7c | 594 | if (hdr_len < 0) |
308edfdf | 595 | goto drop; |
c12b395a | 596 | |
308edfdf TH |
597 | if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false)) |
598 | goto drop; | |
c12b395a | 599 | |
94d7d8f2 WT |
600 | if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) || |
601 | tpi.proto == htons(ETH_P_ERSPAN2))) { | |
5a963eb6 WT |
602 | if (ip6erspan_rcv(skb, hdr_len, &tpi) == PACKET_RCVD) |
603 | return 0; | |
a7343211 | 604 | goto out; |
5a963eb6 WT |
605 | } |
606 | ||
308edfdf | 607 | if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD) |
c12b395a | 608 | return 0; |
c12b395a | 609 | |
a7343211 | 610 | out: |
308edfdf | 611 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
c12b395a | 612 | drop: |
c12b395a | 613 | kfree_skb(skb); |
614 | return 0; | |
615 | } | |
616 | ||
b05229f4 | 617 | static int gre_handle_offloads(struct sk_buff *skb, bool csum) |
c12b395a | 618 | { |
b05229f4 TH |
619 | return iptunnel_handle_offloads(skb, |
620 | csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE); | |
c12b395a | 621 | } |
622 | ||
898b2979 WT |
623 | static void prepare_ip6gre_xmit_ipv4(struct sk_buff *skb, |
624 | struct net_device *dev, | |
625 | struct flowi6 *fl6, __u8 *dsfield, | |
626 | int *encap_limit) | |
627 | { | |
628 | const struct iphdr *iph = ip_hdr(skb); | |
629 | struct ip6_tnl *t = netdev_priv(dev); | |
630 | ||
631 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
632 | *encap_limit = t->parms.encap_limit; | |
633 | ||
634 | memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6)); | |
635 | ||
636 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) | |
637 | *dsfield = ipv4_get_dsfield(iph); | |
638 | else | |
639 | *dsfield = ip6_tclass(t->parms.flowinfo); | |
640 | ||
641 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) | |
642 | fl6->flowi6_mark = skb->mark; | |
643 | else | |
644 | fl6->flowi6_mark = t->parms.fwmark; | |
645 | ||
646 | fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL); | |
647 | } | |
648 | ||
649 | static int prepare_ip6gre_xmit_ipv6(struct sk_buff *skb, | |
650 | struct net_device *dev, | |
651 | struct flowi6 *fl6, __u8 *dsfield, | |
652 | int *encap_limit) | |
653 | { | |
654 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); | |
655 | struct ip6_tnl *t = netdev_priv(dev); | |
656 | __u16 offset; | |
657 | ||
658 | offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb)); | |
659 | /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */ | |
660 | ||
661 | if (offset > 0) { | |
662 | struct ipv6_tlv_tnl_enc_lim *tel; | |
663 | ||
664 | tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset]; | |
665 | if (tel->encap_limit == 0) { | |
666 | icmpv6_send(skb, ICMPV6_PARAMPROB, | |
667 | ICMPV6_HDR_FIELD, offset + 2); | |
668 | return -1; | |
669 | } | |
670 | *encap_limit = tel->encap_limit - 1; | |
671 | } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) { | |
672 | *encap_limit = t->parms.encap_limit; | |
673 | } | |
674 | ||
675 | memcpy(fl6, &t->fl.u.ip6, sizeof(*fl6)); | |
676 | ||
677 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) | |
678 | *dsfield = ipv6_get_dsfield(ipv6h); | |
679 | else | |
680 | *dsfield = ip6_tclass(t->parms.flowinfo); | |
681 | ||
682 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) | |
683 | fl6->flowlabel |= ip6_flowlabel(ipv6h); | |
684 | ||
685 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) | |
686 | fl6->flowi6_mark = skb->mark; | |
687 | else | |
688 | fl6->flowi6_mark = t->parms.fwmark; | |
689 | ||
690 | fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL); | |
691 | ||
692 | return 0; | |
693 | } | |
694 | ||
b05229f4 TH |
695 | static netdev_tx_t __gre6_xmit(struct sk_buff *skb, |
696 | struct net_device *dev, __u8 dsfield, | |
697 | struct flowi6 *fl6, int encap_limit, | |
698 | __u32 *pmtu, __be16 proto) | |
c12b395a | 699 | { |
c12b395a | 700 | struct ip6_tnl *tunnel = netdev_priv(dev); |
8aec4959 | 701 | __be16 protocol; |
c12b395a | 702 | |
703 | if (dev->type == ARPHRD_ETHER) | |
704 | IPCB(skb)->flags = 0; | |
705 | ||
b05229f4 TH |
706 | if (dev->header_ops && dev->type == ARPHRD_IP6GRE) |
707 | fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr; | |
708 | else | |
c12b395a | 709 | fl6->daddr = tunnel->parms.raddr; |
c12b395a | 710 | |
b05229f4 TH |
711 | if (tunnel->parms.o_flags & TUNNEL_SEQ) |
712 | tunnel->o_seqno++; | |
c12b395a | 713 | |
b05229f4 | 714 | /* Push GRE header. */ |
8aec4959 | 715 | protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto; |
6712abc1 WT |
716 | |
717 | if (tunnel->parms.collect_md) { | |
718 | struct ip_tunnel_info *tun_info; | |
719 | const struct ip_tunnel_key *key; | |
720 | __be16 flags; | |
721 | ||
722 | tun_info = skb_tunnel_info(skb); | |
723 | if (unlikely(!tun_info || | |
724 | !(tun_info->mode & IP_TUNNEL_INFO_TX) || | |
725 | ip_tunnel_info_af(tun_info) != AF_INET6)) | |
726 | return -EINVAL; | |
727 | ||
728 | key = &tun_info->key; | |
729 | memset(fl6, 0, sizeof(*fl6)); | |
730 | fl6->flowi6_proto = IPPROTO_GRE; | |
731 | fl6->daddr = key->u.ipv6.dst; | |
732 | fl6->flowlabel = key->label; | |
733 | fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL); | |
734 | ||
735 | dsfield = key->tos; | |
736 | flags = key->tun_flags & (TUNNEL_CSUM | TUNNEL_KEY); | |
737 | tunnel->tun_hlen = gre_calc_hlen(flags); | |
738 | ||
739 | gre_build_header(skb, tunnel->tun_hlen, | |
740 | flags, protocol, | |
741 | tunnel_id_to_key32(tun_info->key.tun_id), 0); | |
742 | ||
743 | } else { | |
744 | gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags, | |
745 | protocol, tunnel->parms.o_key, | |
746 | htonl(tunnel->o_seqno)); | |
747 | } | |
c12b395a | 748 | |
b05229f4 TH |
749 | return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu, |
750 | NEXTHDR_GRE); | |
c12b395a | 751 | } |
752 | ||
753 | static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev) | |
754 | { | |
755 | struct ip6_tnl *t = netdev_priv(dev); | |
c12b395a | 756 | int encap_limit = -1; |
757 | struct flowi6 fl6; | |
6712abc1 | 758 | __u8 dsfield = 0; |
c12b395a | 759 | __u32 mtu; |
760 | int err; | |
761 | ||
5146d1f1 BH |
762 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
763 | ||
6712abc1 WT |
764 | if (!t->parms.collect_md) |
765 | prepare_ip6gre_xmit_ipv4(skb, dev, &fl6, | |
766 | &dsfield, &encap_limit); | |
e2d118a1 | 767 | |
b05229f4 TH |
768 | err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)); |
769 | if (err) | |
770 | return -1; | |
771 | ||
772 | err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, | |
773 | skb->protocol); | |
c12b395a | 774 | if (err != 0) { |
775 | /* XXX: send ICMP error even if DF is not set. */ | |
776 | if (err == -EMSGSIZE) | |
777 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, | |
778 | htonl(mtu)); | |
779 | return -1; | |
780 | } | |
781 | ||
782 | return 0; | |
783 | } | |
784 | ||
785 | static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev) | |
786 | { | |
787 | struct ip6_tnl *t = netdev_priv(dev); | |
788 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); | |
789 | int encap_limit = -1; | |
c12b395a | 790 | struct flowi6 fl6; |
6712abc1 | 791 | __u8 dsfield = 0; |
c12b395a | 792 | __u32 mtu; |
793 | int err; | |
794 | ||
795 | if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr)) | |
796 | return -1; | |
797 | ||
6712abc1 WT |
798 | if (!t->parms.collect_md && |
799 | prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, &dsfield, &encap_limit)) | |
898b2979 | 800 | return -1; |
e2d118a1 | 801 | |
b05229f4 TH |
802 | if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM))) |
803 | return -1; | |
804 | ||
805 | err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, | |
806 | &mtu, skb->protocol); | |
c12b395a | 807 | if (err != 0) { |
808 | if (err == -EMSGSIZE) | |
809 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); | |
810 | return -1; | |
811 | } | |
812 | ||
813 | return 0; | |
814 | } | |
815 | ||
816 | /** | |
817 | * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own | |
818 | * @t: the outgoing tunnel device | |
819 | * @hdr: IPv6 header from the incoming packet | |
820 | * | |
821 | * Description: | |
822 | * Avoid trivial tunneling loop by checking that tunnel exit-point | |
823 | * doesn't match source of incoming packet. | |
824 | * | |
825 | * Return: | |
826 | * 1 if conflict, | |
827 | * 0 else | |
828 | **/ | |
829 | ||
830 | static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t, | |
831 | const struct ipv6hdr *hdr) | |
832 | { | |
833 | return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); | |
834 | } | |
835 | ||
836 | static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev) | |
837 | { | |
838 | struct ip6_tnl *t = netdev_priv(dev); | |
839 | int encap_limit = -1; | |
840 | struct flowi6 fl6; | |
841 | __u32 mtu; | |
842 | int err; | |
843 | ||
844 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
845 | encap_limit = t->parms.encap_limit; | |
846 | ||
6712abc1 WT |
847 | if (!t->parms.collect_md) |
848 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); | |
c12b395a | 849 | |
b05229f4 TH |
850 | err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)); |
851 | if (err) | |
852 | return err; | |
853 | ||
854 | err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol); | |
c12b395a | 855 | |
856 | return err; | |
857 | } | |
858 | ||
859 | static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb, | |
860 | struct net_device *dev) | |
861 | { | |
862 | struct ip6_tnl *t = netdev_priv(dev); | |
863 | struct net_device_stats *stats = &t->dev->stats; | |
864 | int ret; | |
865 | ||
d5005140 | 866 | if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr)) |
41ab3e31 | 867 | goto tx_err; |
c12b395a | 868 | |
869 | switch (skb->protocol) { | |
870 | case htons(ETH_P_IP): | |
871 | ret = ip6gre_xmit_ipv4(skb, dev); | |
872 | break; | |
873 | case htons(ETH_P_IPV6): | |
874 | ret = ip6gre_xmit_ipv6(skb, dev); | |
875 | break; | |
876 | default: | |
877 | ret = ip6gre_xmit_other(skb, dev); | |
878 | break; | |
879 | } | |
880 | ||
881 | if (ret < 0) | |
882 | goto tx_err; | |
883 | ||
884 | return NETDEV_TX_OK; | |
885 | ||
886 | tx_err: | |
887 | stats->tx_errors++; | |
888 | stats->tx_dropped++; | |
889 | kfree_skb(skb); | |
890 | return NETDEV_TX_OK; | |
891 | } | |
892 | ||
5a963eb6 WT |
893 | static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb, |
894 | struct net_device *dev) | |
895 | { | |
896 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); | |
897 | struct ip6_tnl *t = netdev_priv(dev); | |
898 | struct dst_entry *dst = skb_dst(skb); | |
899 | struct net_device_stats *stats; | |
900 | bool truncate = false; | |
901 | int encap_limit = -1; | |
902 | __u8 dsfield = false; | |
903 | struct flowi6 fl6; | |
904 | int err = -EINVAL; | |
905 | __u32 mtu; | |
906 | ||
907 | if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr)) | |
908 | goto tx_err; | |
909 | ||
910 | if (gre_handle_offloads(skb, false)) | |
911 | goto tx_err; | |
912 | ||
5a963eb6 WT |
913 | if (skb->len > dev->mtu + dev->hard_header_len) { |
914 | pskb_trim(skb, dev->mtu + dev->hard_header_len); | |
915 | truncate = true; | |
916 | } | |
917 | ||
5a963eb6 | 918 | t->parms.o_flags &= ~TUNNEL_KEY; |
5a963eb6 | 919 | IPCB(skb)->flags = 0; |
ef7baf5e WT |
920 | |
921 | /* For collect_md mode, derive fl6 from the tunnel key, | |
922 | * for native mode, call prepare_ip6gre_xmit_{ipv4,ipv6}. | |
923 | */ | |
924 | if (t->parms.collect_md) { | |
925 | struct ip_tunnel_info *tun_info; | |
926 | const struct ip_tunnel_key *key; | |
927 | struct erspan_metadata *md; | |
928 | ||
929 | tun_info = skb_tunnel_info(skb); | |
930 | if (unlikely(!tun_info || | |
931 | !(tun_info->mode & IP_TUNNEL_INFO_TX) || | |
932 | ip_tunnel_info_af(tun_info) != AF_INET6)) | |
933 | return -EINVAL; | |
934 | ||
935 | key = &tun_info->key; | |
936 | memset(&fl6, 0, sizeof(fl6)); | |
937 | fl6.flowi6_proto = IPPROTO_GRE; | |
938 | fl6.daddr = key->u.ipv6.dst; | |
939 | fl6.flowlabel = key->label; | |
940 | fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL); | |
941 | ||
942 | dsfield = key->tos; | |
943 | md = ip_tunnel_info_opts(tun_info); | |
944 | if (!md) | |
945 | goto tx_err; | |
946 | ||
94d7d8f2 WT |
947 | if (md->version == 1) { |
948 | erspan_build_header(skb, | |
949 | tunnel_id_to_key32(key->tun_id), | |
950 | ntohl(md->u.index), truncate, | |
951 | false); | |
952 | } else if (md->version == 2) { | |
953 | u16 md2_flags; | |
954 | u16 dir, hwid; | |
955 | ||
956 | md2_flags = ntohs(md->u.md2.flags); | |
957 | dir = (md2_flags & DIR_MASK) >> DIR_OFFSET; | |
958 | hwid = (md2_flags & HWID_MASK) >> HWID_OFFSET; | |
959 | ||
960 | erspan_build_header_v2(skb, | |
961 | tunnel_id_to_key32(key->tun_id), | |
962 | dir, hwid, truncate, | |
963 | false); | |
964 | } | |
ef7baf5e WT |
965 | } else { |
966 | switch (skb->protocol) { | |
967 | case htons(ETH_P_IP): | |
968 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); | |
969 | prepare_ip6gre_xmit_ipv4(skb, dev, &fl6, | |
970 | &dsfield, &encap_limit); | |
971 | break; | |
972 | case htons(ETH_P_IPV6): | |
973 | if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr)) | |
974 | goto tx_err; | |
975 | if (prepare_ip6gre_xmit_ipv6(skb, dev, &fl6, | |
976 | &dsfield, &encap_limit)) | |
977 | goto tx_err; | |
978 | break; | |
979 | default: | |
980 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); | |
981 | break; | |
982 | } | |
983 | ||
94d7d8f2 WT |
984 | if (t->parms.erspan_ver == 1) |
985 | erspan_build_header(skb, t->parms.o_key, | |
986 | t->parms.index, | |
987 | truncate, false); | |
988 | else | |
989 | erspan_build_header_v2(skb, t->parms.o_key, | |
990 | t->parms.dir, | |
991 | t->parms.hwid, | |
992 | truncate, false); | |
ef7baf5e WT |
993 | fl6.daddr = t->parms.raddr; |
994 | } | |
5a963eb6 WT |
995 | |
996 | /* Push GRE header. */ | |
997 | gre_build_header(skb, 8, TUNNEL_SEQ, | |
998 | htons(ETH_P_ERSPAN), 0, htonl(t->o_seqno++)); | |
999 | ||
1000 | /* TooBig packet may have updated dst->dev's mtu */ | |
ef7baf5e | 1001 | if (!t->parms.collect_md && dst && dst_mtu(dst) > dst->dev->mtu) |
5a963eb6 WT |
1002 | dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu); |
1003 | ||
1004 | err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, | |
1005 | NEXTHDR_GRE); | |
1006 | if (err != 0) { | |
1007 | /* XXX: send ICMP error even if DF is not set. */ | |
1008 | if (err == -EMSGSIZE) { | |
1009 | if (skb->protocol == htons(ETH_P_IP)) | |
1010 | icmp_send(skb, ICMP_DEST_UNREACH, | |
1011 | ICMP_FRAG_NEEDED, htonl(mtu)); | |
1012 | else | |
1013 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); | |
1014 | } | |
1015 | ||
1016 | goto tx_err; | |
1017 | } | |
1018 | return NETDEV_TX_OK; | |
1019 | ||
1020 | tx_err: | |
1021 | stats = &t->dev->stats; | |
1022 | stats->tx_errors++; | |
1023 | stats->tx_dropped++; | |
1024 | kfree_skb(skb); | |
1025 | return NETDEV_TX_OK; | |
1026 | } | |
1027 | ||
c12b395a | 1028 | static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu) |
1029 | { | |
1030 | struct net_device *dev = t->dev; | |
1031 | struct __ip6_tnl_parm *p = &t->parms; | |
1032 | struct flowi6 *fl6 = &t->fl.u.ip6; | |
db2ec95d | 1033 | int t_hlen; |
c12b395a | 1034 | |
1035 | if (dev->type != ARPHRD_ETHER) { | |
1036 | memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); | |
1037 | memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); | |
1038 | } | |
1039 | ||
1040 | /* Set up flowi template */ | |
1041 | fl6->saddr = p->laddr; | |
1042 | fl6->daddr = p->raddr; | |
1043 | fl6->flowi6_oif = p->link; | |
1044 | fl6->flowlabel = 0; | |
252f3f5a | 1045 | fl6->flowi6_proto = IPPROTO_GRE; |
c12b395a | 1046 | |
1047 | if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) | |
1048 | fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; | |
1049 | if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) | |
1050 | fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; | |
1051 | ||
1052 | p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); | |
1053 | p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); | |
1054 | ||
1055 | if (p->flags&IP6_TNL_F_CAP_XMIT && | |
1056 | p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER) | |
1057 | dev->flags |= IFF_POINTOPOINT; | |
1058 | else | |
1059 | dev->flags &= ~IFF_POINTOPOINT; | |
1060 | ||
db2ec95d TH |
1061 | t->tun_hlen = gre_calc_hlen(t->parms.o_flags); |
1062 | ||
1faf3d9f | 1063 | t->hlen = t->encap_hlen + t->tun_hlen; |
db2ec95d TH |
1064 | |
1065 | t_hlen = t->hlen + sizeof(struct ipv6hdr); | |
c12b395a | 1066 | |
1067 | if (p->flags & IP6_TNL_F_CAP_XMIT) { | |
1068 | int strict = (ipv6_addr_type(&p->raddr) & | |
1069 | (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); | |
1070 | ||
22f08069 | 1071 | struct rt6_info *rt = rt6_lookup(t->net, |
c12b395a | 1072 | &p->raddr, &p->laddr, |
1073 | p->link, strict); | |
1074 | ||
63159f29 | 1075 | if (!rt) |
c12b395a | 1076 | return; |
1077 | ||
1078 | if (rt->dst.dev) { | |
db2ec95d TH |
1079 | dev->hard_header_len = rt->dst.dev->hard_header_len + |
1080 | t_hlen; | |
c12b395a | 1081 | |
1082 | if (set_mtu) { | |
db2ec95d | 1083 | dev->mtu = rt->dst.dev->mtu - t_hlen; |
c12b395a | 1084 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
1085 | dev->mtu -= 8; | |
a9e242ca AD |
1086 | if (dev->type == ARPHRD_ETHER) |
1087 | dev->mtu -= ETH_HLEN; | |
c12b395a | 1088 | |
1089 | if (dev->mtu < IPV6_MIN_MTU) | |
1090 | dev->mtu = IPV6_MIN_MTU; | |
1091 | } | |
1092 | } | |
94e187c0 | 1093 | ip6_rt_put(rt); |
c12b395a | 1094 | } |
c12b395a | 1095 | } |
1096 | ||
1097 | static int ip6gre_tnl_change(struct ip6_tnl *t, | |
1098 | const struct __ip6_tnl_parm *p, int set_mtu) | |
1099 | { | |
1100 | t->parms.laddr = p->laddr; | |
1101 | t->parms.raddr = p->raddr; | |
1102 | t->parms.flags = p->flags; | |
1103 | t->parms.hop_limit = p->hop_limit; | |
1104 | t->parms.encap_limit = p->encap_limit; | |
1105 | t->parms.flowinfo = p->flowinfo; | |
1106 | t->parms.link = p->link; | |
1107 | t->parms.proto = p->proto; | |
1108 | t->parms.i_key = p->i_key; | |
1109 | t->parms.o_key = p->o_key; | |
1110 | t->parms.i_flags = p->i_flags; | |
1111 | t->parms.o_flags = p->o_flags; | |
0a473b82 | 1112 | t->parms.fwmark = p->fwmark; |
607f725f | 1113 | dst_cache_reset(&t->dst_cache); |
c12b395a | 1114 | ip6gre_tnl_link_config(t, set_mtu); |
1115 | return 0; | |
1116 | } | |
1117 | ||
1118 | static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p, | |
1119 | const struct ip6_tnl_parm2 *u) | |
1120 | { | |
1121 | p->laddr = u->laddr; | |
1122 | p->raddr = u->raddr; | |
1123 | p->flags = u->flags; | |
1124 | p->hop_limit = u->hop_limit; | |
1125 | p->encap_limit = u->encap_limit; | |
1126 | p->flowinfo = u->flowinfo; | |
1127 | p->link = u->link; | |
1128 | p->i_key = u->i_key; | |
1129 | p->o_key = u->o_key; | |
f41fe3c2 TH |
1130 | p->i_flags = gre_flags_to_tnl_flags(u->i_flags); |
1131 | p->o_flags = gre_flags_to_tnl_flags(u->o_flags); | |
c12b395a | 1132 | memcpy(p->name, u->name, sizeof(u->name)); |
1133 | } | |
1134 | ||
1135 | static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u, | |
1136 | const struct __ip6_tnl_parm *p) | |
1137 | { | |
1138 | u->proto = IPPROTO_GRE; | |
1139 | u->laddr = p->laddr; | |
1140 | u->raddr = p->raddr; | |
1141 | u->flags = p->flags; | |
1142 | u->hop_limit = p->hop_limit; | |
1143 | u->encap_limit = p->encap_limit; | |
1144 | u->flowinfo = p->flowinfo; | |
1145 | u->link = p->link; | |
1146 | u->i_key = p->i_key; | |
1147 | u->o_key = p->o_key; | |
f41fe3c2 TH |
1148 | u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags); |
1149 | u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags); | |
c12b395a | 1150 | memcpy(u->name, p->name, sizeof(u->name)); |
1151 | } | |
1152 | ||
1153 | static int ip6gre_tunnel_ioctl(struct net_device *dev, | |
1154 | struct ifreq *ifr, int cmd) | |
1155 | { | |
1156 | int err = 0; | |
1157 | struct ip6_tnl_parm2 p; | |
1158 | struct __ip6_tnl_parm p1; | |
22f08069 ND |
1159 | struct ip6_tnl *t = netdev_priv(dev); |
1160 | struct net *net = t->net; | |
c12b395a | 1161 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
1162 | ||
308edfdf TH |
1163 | memset(&p1, 0, sizeof(p1)); |
1164 | ||
c12b395a | 1165 | switch (cmd) { |
1166 | case SIOCGETTUNNEL: | |
c12b395a | 1167 | if (dev == ign->fb_tunnel_dev) { |
1168 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { | |
1169 | err = -EFAULT; | |
1170 | break; | |
1171 | } | |
1172 | ip6gre_tnl_parm_from_user(&p1, &p); | |
1173 | t = ip6gre_tunnel_locate(net, &p1, 0); | |
63159f29 | 1174 | if (!t) |
22f08069 | 1175 | t = netdev_priv(dev); |
c12b395a | 1176 | } |
5dbd5068 | 1177 | memset(&p, 0, sizeof(p)); |
c12b395a | 1178 | ip6gre_tnl_parm_to_user(&p, &t->parms); |
1179 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | |
1180 | err = -EFAULT; | |
1181 | break; | |
1182 | ||
1183 | case SIOCADDTUNNEL: | |
1184 | case SIOCCHGTUNNEL: | |
1185 | err = -EPERM; | |
af31f412 | 1186 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
c12b395a | 1187 | goto done; |
1188 | ||
1189 | err = -EFAULT; | |
1190 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | |
1191 | goto done; | |
1192 | ||
1193 | err = -EINVAL; | |
1194 | if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)) | |
1195 | goto done; | |
1196 | ||
1197 | if (!(p.i_flags&GRE_KEY)) | |
1198 | p.i_key = 0; | |
1199 | if (!(p.o_flags&GRE_KEY)) | |
1200 | p.o_key = 0; | |
1201 | ||
1202 | ip6gre_tnl_parm_from_user(&p1, &p); | |
1203 | t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL); | |
1204 | ||
1205 | if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { | |
53b24b8f | 1206 | if (t) { |
c12b395a | 1207 | if (t->dev != dev) { |
1208 | err = -EEXIST; | |
1209 | break; | |
1210 | } | |
1211 | } else { | |
1212 | t = netdev_priv(dev); | |
1213 | ||
1214 | ip6gre_tunnel_unlink(ign, t); | |
1215 | synchronize_net(); | |
1216 | ip6gre_tnl_change(t, &p1, 1); | |
1217 | ip6gre_tunnel_link(ign, t); | |
1218 | netdev_state_change(dev); | |
1219 | } | |
1220 | } | |
1221 | ||
1222 | if (t) { | |
1223 | err = 0; | |
1224 | ||
5dbd5068 | 1225 | memset(&p, 0, sizeof(p)); |
c12b395a | 1226 | ip6gre_tnl_parm_to_user(&p, &t->parms); |
1227 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | |
1228 | err = -EFAULT; | |
1229 | } else | |
1230 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); | |
1231 | break; | |
1232 | ||
1233 | case SIOCDELTUNNEL: | |
1234 | err = -EPERM; | |
af31f412 | 1235 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
c12b395a | 1236 | goto done; |
1237 | ||
1238 | if (dev == ign->fb_tunnel_dev) { | |
1239 | err = -EFAULT; | |
1240 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | |
1241 | goto done; | |
1242 | err = -ENOENT; | |
1243 | ip6gre_tnl_parm_from_user(&p1, &p); | |
1244 | t = ip6gre_tunnel_locate(net, &p1, 0); | |
63159f29 | 1245 | if (!t) |
c12b395a | 1246 | goto done; |
1247 | err = -EPERM; | |
1248 | if (t == netdev_priv(ign->fb_tunnel_dev)) | |
1249 | goto done; | |
1250 | dev = t->dev; | |
1251 | } | |
1252 | unregister_netdevice(dev); | |
1253 | err = 0; | |
1254 | break; | |
1255 | ||
1256 | default: | |
1257 | err = -EINVAL; | |
1258 | } | |
1259 | ||
1260 | done: | |
1261 | return err; | |
1262 | } | |
1263 | ||
c12b395a | 1264 | static int ip6gre_header(struct sk_buff *skb, struct net_device *dev, |
76cc0d32 XL |
1265 | unsigned short type, const void *daddr, |
1266 | const void *saddr, unsigned int len) | |
c12b395a | 1267 | { |
1268 | struct ip6_tnl *t = netdev_priv(dev); | |
76cc0d32 XL |
1269 | struct ipv6hdr *ipv6h; |
1270 | __be16 *p; | |
c12b395a | 1271 | |
76cc0d32 XL |
1272 | ipv6h = skb_push(skb, t->hlen + sizeof(*ipv6h)); |
1273 | ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb, | |
1274 | t->fl.u.ip6.flowlabel, | |
1275 | true, &t->fl.u.ip6)); | |
c12b395a | 1276 | ipv6h->hop_limit = t->parms.hop_limit; |
1277 | ipv6h->nexthdr = NEXTHDR_GRE; | |
1278 | ipv6h->saddr = t->parms.laddr; | |
1279 | ipv6h->daddr = t->parms.raddr; | |
1280 | ||
76cc0d32 XL |
1281 | p = (__be16 *)(ipv6h + 1); |
1282 | p[0] = t->parms.o_flags; | |
1283 | p[1] = htons(type); | |
c12b395a | 1284 | |
1285 | /* | |
1286 | * Set the source hardware address. | |
1287 | */ | |
1288 | ||
1289 | if (saddr) | |
1290 | memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr)); | |
1291 | if (daddr) | |
1292 | memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr)); | |
1293 | if (!ipv6_addr_any(&ipv6h->daddr)) | |
1294 | return t->hlen; | |
1295 | ||
1296 | return -t->hlen; | |
1297 | } | |
1298 | ||
c12b395a | 1299 | static const struct header_ops ip6gre_header_ops = { |
1300 | .create = ip6gre_header, | |
c12b395a | 1301 | }; |
1302 | ||
1303 | static const struct net_device_ops ip6gre_netdev_ops = { | |
1304 | .ndo_init = ip6gre_tunnel_init, | |
1305 | .ndo_uninit = ip6gre_tunnel_uninit, | |
1306 | .ndo_start_xmit = ip6gre_tunnel_xmit, | |
1307 | .ndo_do_ioctl = ip6gre_tunnel_ioctl, | |
b05229f4 | 1308 | .ndo_change_mtu = ip6_tnl_change_mtu, |
f61dd388 | 1309 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
ecf2c06a | 1310 | .ndo_get_iflink = ip6_tnl_get_iflink, |
c12b395a | 1311 | }; |
1312 | ||
1313 | static void ip6gre_dev_free(struct net_device *dev) | |
1314 | { | |
cdf3464e MKL |
1315 | struct ip6_tnl *t = netdev_priv(dev); |
1316 | ||
607f725f | 1317 | dst_cache_destroy(&t->dst_cache); |
c12b395a | 1318 | free_percpu(dev->tstats); |
c12b395a | 1319 | } |
1320 | ||
1321 | static void ip6gre_tunnel_setup(struct net_device *dev) | |
1322 | { | |
c12b395a | 1323 | dev->netdev_ops = &ip6gre_netdev_ops; |
cf124db5 DM |
1324 | dev->needs_free_netdev = true; |
1325 | dev->priv_destructor = ip6gre_dev_free; | |
c12b395a | 1326 | |
1327 | dev->type = ARPHRD_IP6GRE; | |
b05229f4 | 1328 | |
c12b395a | 1329 | dev->flags |= IFF_NOARP; |
c12b395a | 1330 | dev->addr_len = sizeof(struct in6_addr); |
02875878 | 1331 | netif_keep_dst(dev); |
45ce0fd1 FJ |
1332 | /* This perm addr will be used as interface identifier by IPv6 */ |
1333 | dev->addr_assign_type = NET_ADDR_RANDOM; | |
1334 | eth_random_addr(dev->perm_addr); | |
c12b395a | 1335 | } |
1336 | ||
e5a9336a AK |
1337 | #define GRE6_FEATURES (NETIF_F_SG | \ |
1338 | NETIF_F_FRAGLIST | \ | |
1339 | NETIF_F_HIGHDMA | \ | |
1340 | NETIF_F_HW_CSUM) | |
1341 | ||
1342 | static void ip6gre_tnl_init_features(struct net_device *dev) | |
1343 | { | |
1344 | struct ip6_tnl *nt = netdev_priv(dev); | |
1345 | ||
1346 | dev->features |= GRE6_FEATURES; | |
1347 | dev->hw_features |= GRE6_FEATURES; | |
1348 | ||
1349 | if (!(nt->parms.o_flags & TUNNEL_SEQ)) { | |
1350 | /* TCP offload with GRE SEQ is not supported, nor | |
1351 | * can we support 2 levels of outer headers requiring | |
1352 | * an update. | |
1353 | */ | |
1354 | if (!(nt->parms.o_flags & TUNNEL_CSUM) || | |
1355 | nt->encap.type == TUNNEL_ENCAP_NONE) { | |
1356 | dev->features |= NETIF_F_GSO_SOFTWARE; | |
1357 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; | |
1358 | } | |
1359 | ||
1360 | /* Can use a lockless transmit, unless we generate | |
1361 | * output sequences | |
1362 | */ | |
1363 | dev->features |= NETIF_F_LLTX; | |
1364 | } | |
1365 | } | |
1366 | ||
a3c119d3 | 1367 | static int ip6gre_tunnel_init_common(struct net_device *dev) |
c12b395a | 1368 | { |
1369 | struct ip6_tnl *tunnel; | |
cdf3464e | 1370 | int ret; |
b05229f4 | 1371 | int t_hlen; |
c12b395a | 1372 | |
1373 | tunnel = netdev_priv(dev); | |
1374 | ||
1375 | tunnel->dev = dev; | |
0bd87628 | 1376 | tunnel->net = dev_net(dev); |
c12b395a | 1377 | strcpy(tunnel->parms.name, dev->name); |
1378 | ||
a3c119d3 MKL |
1379 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
1380 | if (!dev->tstats) | |
1381 | return -ENOMEM; | |
1382 | ||
607f725f | 1383 | ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL); |
cdf3464e MKL |
1384 | if (ret) { |
1385 | free_percpu(dev->tstats); | |
1386 | dev->tstats = NULL; | |
1387 | return ret; | |
1388 | } | |
1389 | ||
b05229f4 | 1390 | tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags); |
1faf3d9f | 1391 | tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen; |
db2ec95d | 1392 | t_hlen = tunnel->hlen + sizeof(struct ipv6hdr); |
b05229f4 | 1393 | |
db2ec95d TH |
1394 | dev->hard_header_len = LL_MAX_HEADER + t_hlen; |
1395 | dev->mtu = ETH_DATA_LEN - t_hlen; | |
1b227e53 HY |
1396 | if (dev->type == ARPHRD_ETHER) |
1397 | dev->mtu -= ETH_HLEN; | |
b05229f4 TH |
1398 | if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
1399 | dev->mtu -= 8; | |
1400 | ||
6712abc1 WT |
1401 | if (tunnel->parms.collect_md) { |
1402 | dev->features |= NETIF_F_NETNS_LOCAL; | |
1403 | netif_keep_dst(dev); | |
1404 | } | |
e5a9336a | 1405 | ip6gre_tnl_init_features(dev); |
6712abc1 | 1406 | |
a3c119d3 MKL |
1407 | return 0; |
1408 | } | |
1409 | ||
1410 | static int ip6gre_tunnel_init(struct net_device *dev) | |
1411 | { | |
1412 | struct ip6_tnl *tunnel; | |
1413 | int ret; | |
1414 | ||
1415 | ret = ip6gre_tunnel_init_common(dev); | |
1416 | if (ret) | |
1417 | return ret; | |
1418 | ||
1419 | tunnel = netdev_priv(dev); | |
1420 | ||
6712abc1 WT |
1421 | if (tunnel->parms.collect_md) |
1422 | return 0; | |
1423 | ||
c12b395a | 1424 | memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr)); |
1425 | memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr)); | |
1426 | ||
1427 | if (ipv6_addr_any(&tunnel->parms.raddr)) | |
1428 | dev->header_ops = &ip6gre_header_ops; | |
1429 | ||
c12b395a | 1430 | return 0; |
1431 | } | |
1432 | ||
1433 | static void ip6gre_fb_tunnel_init(struct net_device *dev) | |
1434 | { | |
1435 | struct ip6_tnl *tunnel = netdev_priv(dev); | |
1436 | ||
1437 | tunnel->dev = dev; | |
0bd87628 | 1438 | tunnel->net = dev_net(dev); |
c12b395a | 1439 | strcpy(tunnel->parms.name, dev->name); |
1440 | ||
1441 | tunnel->hlen = sizeof(struct ipv6hdr) + 4; | |
1442 | ||
1443 | dev_hold(dev); | |
1444 | } | |
1445 | ||
c12b395a | 1446 | static struct inet6_protocol ip6gre_protocol __read_mostly = { |
308edfdf | 1447 | .handler = gre_rcv, |
c12b395a | 1448 | .err_handler = ip6gre_err, |
1449 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, | |
1450 | }; | |
1451 | ||
22f08069 | 1452 | static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head) |
c12b395a | 1453 | { |
22f08069 ND |
1454 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
1455 | struct net_device *dev, *aux; | |
c12b395a | 1456 | int prio; |
1457 | ||
22f08069 ND |
1458 | for_each_netdev_safe(net, dev, aux) |
1459 | if (dev->rtnl_link_ops == &ip6gre_link_ops || | |
5a963eb6 WT |
1460 | dev->rtnl_link_ops == &ip6gre_tap_ops || |
1461 | dev->rtnl_link_ops == &ip6erspan_tap_ops) | |
22f08069 ND |
1462 | unregister_netdevice_queue(dev, head); |
1463 | ||
c12b395a | 1464 | for (prio = 0; prio < 4; prio++) { |
1465 | int h; | |
e87a8f24 | 1466 | for (h = 0; h < IP6_GRE_HASH_SIZE; h++) { |
c12b395a | 1467 | struct ip6_tnl *t; |
1468 | ||
1469 | t = rtnl_dereference(ign->tunnels[prio][h]); | |
1470 | ||
53b24b8f | 1471 | while (t) { |
22f08069 ND |
1472 | /* If dev is in the same netns, it has already |
1473 | * been added to the list by the previous loop. | |
1474 | */ | |
1475 | if (!net_eq(dev_net(t->dev), net)) | |
1476 | unregister_netdevice_queue(t->dev, | |
1477 | head); | |
c12b395a | 1478 | t = rtnl_dereference(t->next); |
1479 | } | |
1480 | } | |
1481 | } | |
1482 | } | |
1483 | ||
1484 | static int __net_init ip6gre_init_net(struct net *net) | |
1485 | { | |
1486 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
1487 | int err; | |
1488 | ||
1489 | ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0", | |
c835a677 TG |
1490 | NET_NAME_UNKNOWN, |
1491 | ip6gre_tunnel_setup); | |
c12b395a | 1492 | if (!ign->fb_tunnel_dev) { |
1493 | err = -ENOMEM; | |
1494 | goto err_alloc_dev; | |
1495 | } | |
1496 | dev_net_set(ign->fb_tunnel_dev, net); | |
22f08069 ND |
1497 | /* FB netdevice is special: we have one, and only one per netns. |
1498 | * Allowing to move it to another netns is clearly unsafe. | |
1499 | */ | |
1500 | ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL; | |
1501 | ||
c12b395a | 1502 | |
1503 | ip6gre_fb_tunnel_init(ign->fb_tunnel_dev); | |
1504 | ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops; | |
1505 | ||
1506 | err = register_netdev(ign->fb_tunnel_dev); | |
1507 | if (err) | |
1508 | goto err_reg_dev; | |
1509 | ||
1510 | rcu_assign_pointer(ign->tunnels_wc[0], | |
1511 | netdev_priv(ign->fb_tunnel_dev)); | |
1512 | return 0; | |
1513 | ||
1514 | err_reg_dev: | |
cf124db5 | 1515 | free_netdev(ign->fb_tunnel_dev); |
c12b395a | 1516 | err_alloc_dev: |
1517 | return err; | |
1518 | } | |
1519 | ||
bb401cae | 1520 | static void __net_exit ip6gre_exit_batch_net(struct list_head *net_list) |
c12b395a | 1521 | { |
bb401cae | 1522 | struct net *net; |
c12b395a | 1523 | LIST_HEAD(list); |
1524 | ||
c12b395a | 1525 | rtnl_lock(); |
bb401cae ED |
1526 | list_for_each_entry(net, net_list, exit_list) |
1527 | ip6gre_destroy_tunnels(net, &list); | |
c12b395a | 1528 | unregister_netdevice_many(&list); |
1529 | rtnl_unlock(); | |
1530 | } | |
1531 | ||
1532 | static struct pernet_operations ip6gre_net_ops = { | |
1533 | .init = ip6gre_init_net, | |
bb401cae | 1534 | .exit_batch = ip6gre_exit_batch_net, |
c12b395a | 1535 | .id = &ip6gre_net_id, |
1536 | .size = sizeof(struct ip6gre_net), | |
1537 | }; | |
1538 | ||
a8b8a889 MS |
1539 | static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[], |
1540 | struct netlink_ext_ack *extack) | |
c12b395a | 1541 | { |
1542 | __be16 flags; | |
1543 | ||
1544 | if (!data) | |
1545 | return 0; | |
1546 | ||
1547 | flags = 0; | |
1548 | if (data[IFLA_GRE_IFLAGS]) | |
1549 | flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); | |
1550 | if (data[IFLA_GRE_OFLAGS]) | |
1551 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); | |
1552 | if (flags & (GRE_VERSION|GRE_ROUTING)) | |
1553 | return -EINVAL; | |
1554 | ||
1555 | return 0; | |
1556 | } | |
1557 | ||
a8b8a889 MS |
1558 | static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[], |
1559 | struct netlink_ext_ack *extack) | |
c12b395a | 1560 | { |
1561 | struct in6_addr daddr; | |
1562 | ||
1563 | if (tb[IFLA_ADDRESS]) { | |
1564 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
1565 | return -EINVAL; | |
1566 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
1567 | return -EADDRNOTAVAIL; | |
1568 | } | |
1569 | ||
1570 | if (!data) | |
1571 | goto out; | |
1572 | ||
1573 | if (data[IFLA_GRE_REMOTE]) { | |
67b61f6c | 1574 | daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]); |
c12b395a | 1575 | if (ipv6_addr_any(&daddr)) |
1576 | return -EINVAL; | |
1577 | } | |
1578 | ||
1579 | out: | |
a8b8a889 | 1580 | return ip6gre_tunnel_validate(tb, data, extack); |
c12b395a | 1581 | } |
1582 | ||
5a963eb6 WT |
1583 | static int ip6erspan_tap_validate(struct nlattr *tb[], struct nlattr *data[], |
1584 | struct netlink_ext_ack *extack) | |
1585 | { | |
1586 | __be16 flags = 0; | |
94d7d8f2 | 1587 | int ret, ver = 0; |
5a963eb6 WT |
1588 | |
1589 | if (!data) | |
1590 | return 0; | |
1591 | ||
1592 | ret = ip6gre_tap_validate(tb, data, extack); | |
1593 | if (ret) | |
1594 | return ret; | |
1595 | ||
1596 | /* ERSPAN should only have GRE sequence and key flag */ | |
1597 | if (data[IFLA_GRE_OFLAGS]) | |
1598 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); | |
1599 | if (data[IFLA_GRE_IFLAGS]) | |
1600 | flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); | |
1601 | if (!data[IFLA_GRE_COLLECT_METADATA] && | |
1602 | flags != (GRE_SEQ | GRE_KEY)) | |
1603 | return -EINVAL; | |
1604 | ||
1605 | /* ERSPAN Session ID only has 10-bit. Since we reuse | |
1606 | * 32-bit key field as ID, check it's range. | |
1607 | */ | |
1608 | if (data[IFLA_GRE_IKEY] && | |
1609 | (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK)) | |
1610 | return -EINVAL; | |
1611 | ||
1612 | if (data[IFLA_GRE_OKEY] && | |
1613 | (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK)) | |
1614 | return -EINVAL; | |
1615 | ||
94d7d8f2 WT |
1616 | if (data[IFLA_GRE_ERSPAN_VER]) { |
1617 | ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]); | |
1618 | if (ver != 1 && ver != 2) | |
5a963eb6 WT |
1619 | return -EINVAL; |
1620 | } | |
94d7d8f2 WT |
1621 | |
1622 | if (ver == 1) { | |
1623 | if (data[IFLA_GRE_ERSPAN_INDEX]) { | |
1624 | u32 index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]); | |
1625 | ||
1626 | if (index & ~INDEX_MASK) | |
1627 | return -EINVAL; | |
1628 | } | |
1629 | } else if (ver == 2) { | |
1630 | if (data[IFLA_GRE_ERSPAN_DIR]) { | |
1631 | u16 dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]); | |
1632 | ||
1633 | if (dir & ~(DIR_MASK >> DIR_OFFSET)) | |
1634 | return -EINVAL; | |
1635 | } | |
1636 | ||
1637 | if (data[IFLA_GRE_ERSPAN_HWID]) { | |
1638 | u16 hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]); | |
1639 | ||
1640 | if (hwid & ~(HWID_MASK >> HWID_OFFSET)) | |
1641 | return -EINVAL; | |
1642 | } | |
1643 | } | |
1644 | ||
5a963eb6 WT |
1645 | return 0; |
1646 | } | |
c12b395a | 1647 | |
1648 | static void ip6gre_netlink_parms(struct nlattr *data[], | |
1649 | struct __ip6_tnl_parm *parms) | |
1650 | { | |
1651 | memset(parms, 0, sizeof(*parms)); | |
1652 | ||
1653 | if (!data) | |
1654 | return; | |
1655 | ||
1656 | if (data[IFLA_GRE_LINK]) | |
1657 | parms->link = nla_get_u32(data[IFLA_GRE_LINK]); | |
1658 | ||
1659 | if (data[IFLA_GRE_IFLAGS]) | |
f41fe3c2 TH |
1660 | parms->i_flags = gre_flags_to_tnl_flags( |
1661 | nla_get_be16(data[IFLA_GRE_IFLAGS])); | |
c12b395a | 1662 | |
1663 | if (data[IFLA_GRE_OFLAGS]) | |
f41fe3c2 TH |
1664 | parms->o_flags = gre_flags_to_tnl_flags( |
1665 | nla_get_be16(data[IFLA_GRE_OFLAGS])); | |
c12b395a | 1666 | |
1667 | if (data[IFLA_GRE_IKEY]) | |
1668 | parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); | |
1669 | ||
1670 | if (data[IFLA_GRE_OKEY]) | |
1671 | parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); | |
1672 | ||
1673 | if (data[IFLA_GRE_LOCAL]) | |
67b61f6c | 1674 | parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]); |
c12b395a | 1675 | |
1676 | if (data[IFLA_GRE_REMOTE]) | |
67b61f6c | 1677 | parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]); |
c12b395a | 1678 | |
1679 | if (data[IFLA_GRE_TTL]) | |
1680 | parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]); | |
1681 | ||
1682 | if (data[IFLA_GRE_ENCAP_LIMIT]) | |
1683 | parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]); | |
1684 | ||
1685 | if (data[IFLA_GRE_FLOWINFO]) | |
c2675de4 | 1686 | parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]); |
c12b395a | 1687 | |
1688 | if (data[IFLA_GRE_FLAGS]) | |
1689 | parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]); | |
0a473b82 CG |
1690 | |
1691 | if (data[IFLA_GRE_FWMARK]) | |
1692 | parms->fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]); | |
5a963eb6 | 1693 | |
6712abc1 WT |
1694 | if (data[IFLA_GRE_COLLECT_METADATA]) |
1695 | parms->collect_md = true; | |
94d7d8f2 WT |
1696 | |
1697 | if (data[IFLA_GRE_ERSPAN_VER]) | |
1698 | parms->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]); | |
1699 | ||
1700 | if (parms->erspan_ver == 1) { | |
1701 | if (data[IFLA_GRE_ERSPAN_INDEX]) | |
1702 | parms->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]); | |
1703 | } else if (parms->erspan_ver == 2) { | |
1704 | if (data[IFLA_GRE_ERSPAN_DIR]) | |
1705 | parms->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]); | |
1706 | if (data[IFLA_GRE_ERSPAN_HWID]) | |
1707 | parms->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]); | |
1708 | } | |
c12b395a | 1709 | } |
1710 | ||
1711 | static int ip6gre_tap_init(struct net_device *dev) | |
1712 | { | |
a3c119d3 | 1713 | int ret; |
c12b395a | 1714 | |
a3c119d3 MKL |
1715 | ret = ip6gre_tunnel_init_common(dev); |
1716 | if (ret) | |
1717 | return ret; | |
c12b395a | 1718 | |
0a46baaf SC |
1719 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
1720 | ||
c12b395a | 1721 | return 0; |
1722 | } | |
1723 | ||
1724 | static const struct net_device_ops ip6gre_tap_netdev_ops = { | |
1725 | .ndo_init = ip6gre_tap_init, | |
1726 | .ndo_uninit = ip6gre_tunnel_uninit, | |
1727 | .ndo_start_xmit = ip6gre_tunnel_xmit, | |
1728 | .ndo_set_mac_address = eth_mac_addr, | |
1729 | .ndo_validate_addr = eth_validate_addr, | |
b05229f4 | 1730 | .ndo_change_mtu = ip6_tnl_change_mtu, |
f61dd388 | 1731 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
ecf2c06a | 1732 | .ndo_get_iflink = ip6_tnl_get_iflink, |
c12b395a | 1733 | }; |
1734 | ||
5a963eb6 WT |
1735 | static int ip6erspan_tap_init(struct net_device *dev) |
1736 | { | |
1737 | struct ip6_tnl *tunnel; | |
1738 | int t_hlen; | |
1739 | int ret; | |
1740 | ||
1741 | tunnel = netdev_priv(dev); | |
1742 | ||
1743 | tunnel->dev = dev; | |
1744 | tunnel->net = dev_net(dev); | |
1745 | strcpy(tunnel->parms.name, dev->name); | |
1746 | ||
1747 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); | |
1748 | if (!dev->tstats) | |
1749 | return -ENOMEM; | |
1750 | ||
1751 | ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL); | |
1752 | if (ret) { | |
1753 | free_percpu(dev->tstats); | |
1754 | dev->tstats = NULL; | |
1755 | return ret; | |
1756 | } | |
1757 | ||
1758 | tunnel->tun_hlen = 8; | |
1759 | tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen + | |
94d7d8f2 | 1760 | erspan_hdr_len(tunnel->parms.erspan_ver); |
5a963eb6 WT |
1761 | t_hlen = tunnel->hlen + sizeof(struct ipv6hdr); |
1762 | ||
1763 | dev->hard_header_len = LL_MAX_HEADER + t_hlen; | |
1764 | dev->mtu = ETH_DATA_LEN - t_hlen; | |
1765 | if (dev->type == ARPHRD_ETHER) | |
1766 | dev->mtu -= ETH_HLEN; | |
1767 | if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
1768 | dev->mtu -= 8; | |
1769 | ||
1770 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; | |
1771 | tunnel = netdev_priv(dev); | |
1772 | ip6gre_tnl_link_config(tunnel, 1); | |
1773 | ||
1774 | return 0; | |
1775 | } | |
1776 | ||
1777 | static const struct net_device_ops ip6erspan_netdev_ops = { | |
1778 | .ndo_init = ip6erspan_tap_init, | |
1779 | .ndo_uninit = ip6gre_tunnel_uninit, | |
1780 | .ndo_start_xmit = ip6erspan_tunnel_xmit, | |
1781 | .ndo_set_mac_address = eth_mac_addr, | |
1782 | .ndo_validate_addr = eth_validate_addr, | |
1783 | .ndo_change_mtu = ip6_tnl_change_mtu, | |
1784 | .ndo_get_stats64 = ip_tunnel_get_stats64, | |
1785 | .ndo_get_iflink = ip6_tnl_get_iflink, | |
1786 | }; | |
1787 | ||
c12b395a | 1788 | static void ip6gre_tap_setup(struct net_device *dev) |
1789 | { | |
1790 | ||
1791 | ether_setup(dev); | |
1792 | ||
2c52129a | 1793 | dev->max_mtu = 0; |
c12b395a | 1794 | dev->netdev_ops = &ip6gre_tap_netdev_ops; |
cf124db5 DM |
1795 | dev->needs_free_netdev = true; |
1796 | dev->priv_destructor = ip6gre_dev_free; | |
c12b395a | 1797 | |
c12b395a | 1798 | dev->features |= NETIF_F_NETNS_LOCAL; |
d13b161c | 1799 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
0a46baaf | 1800 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
2d40557c | 1801 | netif_keep_dst(dev); |
c12b395a | 1802 | } |
1803 | ||
1faf3d9f TH |
1804 | static bool ip6gre_netlink_encap_parms(struct nlattr *data[], |
1805 | struct ip_tunnel_encap *ipencap) | |
1806 | { | |
1807 | bool ret = false; | |
1808 | ||
1809 | memset(ipencap, 0, sizeof(*ipencap)); | |
1810 | ||
1811 | if (!data) | |
1812 | return ret; | |
1813 | ||
1814 | if (data[IFLA_GRE_ENCAP_TYPE]) { | |
1815 | ret = true; | |
1816 | ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]); | |
1817 | } | |
1818 | ||
1819 | if (data[IFLA_GRE_ENCAP_FLAGS]) { | |
1820 | ret = true; | |
1821 | ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]); | |
1822 | } | |
1823 | ||
1824 | if (data[IFLA_GRE_ENCAP_SPORT]) { | |
1825 | ret = true; | |
1826 | ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]); | |
1827 | } | |
1828 | ||
1829 | if (data[IFLA_GRE_ENCAP_DPORT]) { | |
1830 | ret = true; | |
1831 | ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]); | |
1832 | } | |
1833 | ||
1834 | return ret; | |
1835 | } | |
1836 | ||
c12b395a | 1837 | static int ip6gre_newlink(struct net *src_net, struct net_device *dev, |
7a3f4a18 MS |
1838 | struct nlattr *tb[], struct nlattr *data[], |
1839 | struct netlink_ext_ack *extack) | |
c12b395a | 1840 | { |
1841 | struct ip6_tnl *nt; | |
1842 | struct net *net = dev_net(dev); | |
1843 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
1faf3d9f | 1844 | struct ip_tunnel_encap ipencap; |
c12b395a | 1845 | int err; |
1846 | ||
1847 | nt = netdev_priv(dev); | |
1faf3d9f TH |
1848 | |
1849 | if (ip6gre_netlink_encap_parms(data, &ipencap)) { | |
1850 | int err = ip6_tnl_encap_setup(nt, &ipencap); | |
1851 | ||
1852 | if (err < 0) | |
1853 | return err; | |
1854 | } | |
1855 | ||
c12b395a | 1856 | ip6gre_netlink_parms(data, &nt->parms); |
1857 | ||
6712abc1 WT |
1858 | if (nt->parms.collect_md) { |
1859 | if (rtnl_dereference(ign->collect_md_tun)) | |
1860 | return -EEXIST; | |
1861 | } else { | |
1862 | if (ip6gre_tunnel_find(net, &nt->parms, dev->type)) | |
1863 | return -EEXIST; | |
1864 | } | |
c12b395a | 1865 | |
1866 | if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS]) | |
1867 | eth_hw_addr_random(dev); | |
1868 | ||
1869 | nt->dev = dev; | |
0bd87628 | 1870 | nt->net = dev_net(dev); |
c12b395a | 1871 | |
c12b395a | 1872 | err = register_netdevice(dev); |
1873 | if (err) | |
1874 | goto out; | |
1875 | ||
128bb975 AK |
1876 | ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]); |
1877 | ||
1878 | if (tb[IFLA_MTU]) | |
1879 | ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU])); | |
1880 | ||
c12b395a | 1881 | dev_hold(dev); |
1882 | ip6gre_tunnel_link(ign, nt); | |
1883 | ||
1884 | out: | |
1885 | return err; | |
1886 | } | |
1887 | ||
1888 | static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[], | |
ad744b22 MS |
1889 | struct nlattr *data[], |
1890 | struct netlink_ext_ack *extack) | |
c12b395a | 1891 | { |
22f08069 ND |
1892 | struct ip6_tnl *t, *nt = netdev_priv(dev); |
1893 | struct net *net = nt->net; | |
c12b395a | 1894 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
1895 | struct __ip6_tnl_parm p; | |
1faf3d9f | 1896 | struct ip_tunnel_encap ipencap; |
c12b395a | 1897 | |
1898 | if (dev == ign->fb_tunnel_dev) | |
1899 | return -EINVAL; | |
1900 | ||
1faf3d9f TH |
1901 | if (ip6gre_netlink_encap_parms(data, &ipencap)) { |
1902 | int err = ip6_tnl_encap_setup(nt, &ipencap); | |
1903 | ||
1904 | if (err < 0) | |
1905 | return err; | |
1906 | } | |
1907 | ||
c12b395a | 1908 | ip6gre_netlink_parms(data, &p); |
1909 | ||
1910 | t = ip6gre_tunnel_locate(net, &p, 0); | |
1911 | ||
1912 | if (t) { | |
1913 | if (t->dev != dev) | |
1914 | return -EEXIST; | |
1915 | } else { | |
1916 | t = nt; | |
c12b395a | 1917 | } |
1918 | ||
6a61d4db ND |
1919 | ip6gre_tunnel_unlink(ign, t); |
1920 | ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]); | |
1921 | ip6gre_tunnel_link(ign, t); | |
c12b395a | 1922 | return 0; |
1923 | } | |
1924 | ||
54d63f78 ND |
1925 | static void ip6gre_dellink(struct net_device *dev, struct list_head *head) |
1926 | { | |
1927 | struct net *net = dev_net(dev); | |
1928 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
1929 | ||
1930 | if (dev != ign->fb_tunnel_dev) | |
1931 | unregister_netdevice_queue(dev, head); | |
1932 | } | |
1933 | ||
c12b395a | 1934 | static size_t ip6gre_get_size(const struct net_device *dev) |
1935 | { | |
1936 | return | |
1937 | /* IFLA_GRE_LINK */ | |
1938 | nla_total_size(4) + | |
1939 | /* IFLA_GRE_IFLAGS */ | |
1940 | nla_total_size(2) + | |
1941 | /* IFLA_GRE_OFLAGS */ | |
1942 | nla_total_size(2) + | |
1943 | /* IFLA_GRE_IKEY */ | |
1944 | nla_total_size(4) + | |
1945 | /* IFLA_GRE_OKEY */ | |
1946 | nla_total_size(4) + | |
1947 | /* IFLA_GRE_LOCAL */ | |
a3754133 | 1948 | nla_total_size(sizeof(struct in6_addr)) + |
c12b395a | 1949 | /* IFLA_GRE_REMOTE */ |
a3754133 | 1950 | nla_total_size(sizeof(struct in6_addr)) + |
c12b395a | 1951 | /* IFLA_GRE_TTL */ |
1952 | nla_total_size(1) + | |
c12b395a | 1953 | /* IFLA_GRE_ENCAP_LIMIT */ |
1954 | nla_total_size(1) + | |
1955 | /* IFLA_GRE_FLOWINFO */ | |
1956 | nla_total_size(4) + | |
1957 | /* IFLA_GRE_FLAGS */ | |
1958 | nla_total_size(4) + | |
1faf3d9f TH |
1959 | /* IFLA_GRE_ENCAP_TYPE */ |
1960 | nla_total_size(2) + | |
1961 | /* IFLA_GRE_ENCAP_FLAGS */ | |
1962 | nla_total_size(2) + | |
1963 | /* IFLA_GRE_ENCAP_SPORT */ | |
1964 | nla_total_size(2) + | |
1965 | /* IFLA_GRE_ENCAP_DPORT */ | |
1966 | nla_total_size(2) + | |
6712abc1 WT |
1967 | /* IFLA_GRE_COLLECT_METADATA */ |
1968 | nla_total_size(0) + | |
0a473b82 CG |
1969 | /* IFLA_GRE_FWMARK */ |
1970 | nla_total_size(4) + | |
5a963eb6 WT |
1971 | /* IFLA_GRE_ERSPAN_INDEX */ |
1972 | nla_total_size(4) + | |
c12b395a | 1973 | 0; |
1974 | } | |
1975 | ||
1976 | static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
1977 | { | |
1978 | struct ip6_tnl *t = netdev_priv(dev); | |
1979 | struct __ip6_tnl_parm *p = &t->parms; | |
1980 | ||
1981 | if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || | |
f41fe3c2 TH |
1982 | nla_put_be16(skb, IFLA_GRE_IFLAGS, |
1983 | gre_tnl_flags_to_gre_flags(p->i_flags)) || | |
1984 | nla_put_be16(skb, IFLA_GRE_OFLAGS, | |
1985 | gre_tnl_flags_to_gre_flags(p->o_flags)) || | |
c12b395a | 1986 | nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || |
1987 | nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || | |
930345ea JB |
1988 | nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) || |
1989 | nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) || | |
c12b395a | 1990 | nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) || |
c12b395a | 1991 | nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) || |
1992 | nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) || | |
0a473b82 | 1993 | nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags) || |
5a963eb6 WT |
1994 | nla_put_u32(skb, IFLA_GRE_FWMARK, p->fwmark) || |
1995 | nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index)) | |
c12b395a | 1996 | goto nla_put_failure; |
1faf3d9f TH |
1997 | |
1998 | if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE, | |
1999 | t->encap.type) || | |
2000 | nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT, | |
2001 | t->encap.sport) || | |
2002 | nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT, | |
2003 | t->encap.dport) || | |
2004 | nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS, | |
2005 | t->encap.flags)) | |
2006 | goto nla_put_failure; | |
2007 | ||
6712abc1 WT |
2008 | if (p->collect_md) { |
2009 | if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA)) | |
2010 | goto nla_put_failure; | |
2011 | } | |
2012 | ||
94d7d8f2 WT |
2013 | if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, p->erspan_ver)) |
2014 | goto nla_put_failure; | |
2015 | ||
2016 | if (p->erspan_ver == 1) { | |
2017 | if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, p->index)) | |
2018 | goto nla_put_failure; | |
2019 | } else if (p->erspan_ver == 2) { | |
2020 | if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, p->dir)) | |
2021 | goto nla_put_failure; | |
2022 | if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, p->hwid)) | |
2023 | goto nla_put_failure; | |
2024 | } | |
2025 | ||
c12b395a | 2026 | return 0; |
2027 | ||
2028 | nla_put_failure: | |
2029 | return -EMSGSIZE; | |
2030 | } | |
2031 | ||
2032 | static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = { | |
2033 | [IFLA_GRE_LINK] = { .type = NLA_U32 }, | |
2034 | [IFLA_GRE_IFLAGS] = { .type = NLA_U16 }, | |
2035 | [IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, | |
2036 | [IFLA_GRE_IKEY] = { .type = NLA_U32 }, | |
2037 | [IFLA_GRE_OKEY] = { .type = NLA_U32 }, | |
2038 | [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) }, | |
2039 | [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) }, | |
2040 | [IFLA_GRE_TTL] = { .type = NLA_U8 }, | |
2041 | [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 }, | |
2042 | [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 }, | |
2043 | [IFLA_GRE_FLAGS] = { .type = NLA_U32 }, | |
1faf3d9f TH |
2044 | [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 }, |
2045 | [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 }, | |
2046 | [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 }, | |
2047 | [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 }, | |
6712abc1 | 2048 | [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG }, |
0a473b82 | 2049 | [IFLA_GRE_FWMARK] = { .type = NLA_U32 }, |
5a963eb6 | 2050 | [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 }, |
94d7d8f2 WT |
2051 | [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 }, |
2052 | [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 }, | |
2053 | [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 }, | |
c12b395a | 2054 | }; |
2055 | ||
5a963eb6 WT |
2056 | static void ip6erspan_tap_setup(struct net_device *dev) |
2057 | { | |
2058 | ether_setup(dev); | |
2059 | ||
2060 | dev->netdev_ops = &ip6erspan_netdev_ops; | |
2061 | dev->needs_free_netdev = true; | |
2062 | dev->priv_destructor = ip6gre_dev_free; | |
2063 | ||
2064 | dev->features |= NETIF_F_NETNS_LOCAL; | |
2065 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; | |
2066 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; | |
2067 | netif_keep_dst(dev); | |
2068 | } | |
2069 | ||
c12b395a | 2070 | static struct rtnl_link_ops ip6gre_link_ops __read_mostly = { |
2071 | .kind = "ip6gre", | |
2072 | .maxtype = IFLA_GRE_MAX, | |
2073 | .policy = ip6gre_policy, | |
2074 | .priv_size = sizeof(struct ip6_tnl), | |
2075 | .setup = ip6gre_tunnel_setup, | |
2076 | .validate = ip6gre_tunnel_validate, | |
2077 | .newlink = ip6gre_newlink, | |
2078 | .changelink = ip6gre_changelink, | |
54d63f78 | 2079 | .dellink = ip6gre_dellink, |
c12b395a | 2080 | .get_size = ip6gre_get_size, |
2081 | .fill_info = ip6gre_fill_info, | |
1728d4fa | 2082 | .get_link_net = ip6_tnl_get_link_net, |
c12b395a | 2083 | }; |
2084 | ||
2085 | static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = { | |
2086 | .kind = "ip6gretap", | |
2087 | .maxtype = IFLA_GRE_MAX, | |
2088 | .policy = ip6gre_policy, | |
2089 | .priv_size = sizeof(struct ip6_tnl), | |
2090 | .setup = ip6gre_tap_setup, | |
2091 | .validate = ip6gre_tap_validate, | |
2092 | .newlink = ip6gre_newlink, | |
2093 | .changelink = ip6gre_changelink, | |
2094 | .get_size = ip6gre_get_size, | |
2095 | .fill_info = ip6gre_fill_info, | |
3390e397 | 2096 | .get_link_net = ip6_tnl_get_link_net, |
c12b395a | 2097 | }; |
2098 | ||
5a963eb6 WT |
2099 | static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly = { |
2100 | .kind = "ip6erspan", | |
2101 | .maxtype = IFLA_GRE_MAX, | |
2102 | .policy = ip6gre_policy, | |
2103 | .priv_size = sizeof(struct ip6_tnl), | |
2104 | .setup = ip6erspan_tap_setup, | |
2105 | .validate = ip6erspan_tap_validate, | |
2106 | .newlink = ip6gre_newlink, | |
2107 | .changelink = ip6gre_changelink, | |
2108 | .get_size = ip6gre_get_size, | |
2109 | .fill_info = ip6gre_fill_info, | |
2110 | .get_link_net = ip6_tnl_get_link_net, | |
2111 | }; | |
2112 | ||
c12b395a | 2113 | /* |
2114 | * And now the modules code and kernel interface. | |
2115 | */ | |
2116 | ||
2117 | static int __init ip6gre_init(void) | |
2118 | { | |
2119 | int err; | |
2120 | ||
2121 | pr_info("GRE over IPv6 tunneling driver\n"); | |
2122 | ||
2123 | err = register_pernet_device(&ip6gre_net_ops); | |
2124 | if (err < 0) | |
2125 | return err; | |
2126 | ||
2127 | err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE); | |
2128 | if (err < 0) { | |
2129 | pr_info("%s: can't add protocol\n", __func__); | |
2130 | goto add_proto_failed; | |
2131 | } | |
2132 | ||
2133 | err = rtnl_link_register(&ip6gre_link_ops); | |
2134 | if (err < 0) | |
2135 | goto rtnl_link_failed; | |
2136 | ||
2137 | err = rtnl_link_register(&ip6gre_tap_ops); | |
2138 | if (err < 0) | |
2139 | goto tap_ops_failed; | |
2140 | ||
5a963eb6 WT |
2141 | err = rtnl_link_register(&ip6erspan_tap_ops); |
2142 | if (err < 0) | |
2143 | goto erspan_link_failed; | |
2144 | ||
c12b395a | 2145 | out: |
2146 | return err; | |
2147 | ||
5a963eb6 WT |
2148 | erspan_link_failed: |
2149 | rtnl_link_unregister(&ip6gre_tap_ops); | |
c12b395a | 2150 | tap_ops_failed: |
2151 | rtnl_link_unregister(&ip6gre_link_ops); | |
2152 | rtnl_link_failed: | |
2153 | inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); | |
2154 | add_proto_failed: | |
2155 | unregister_pernet_device(&ip6gre_net_ops); | |
2156 | goto out; | |
2157 | } | |
2158 | ||
2159 | static void __exit ip6gre_fini(void) | |
2160 | { | |
2161 | rtnl_link_unregister(&ip6gre_tap_ops); | |
2162 | rtnl_link_unregister(&ip6gre_link_ops); | |
5a963eb6 | 2163 | rtnl_link_unregister(&ip6erspan_tap_ops); |
c12b395a | 2164 | inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); |
2165 | unregister_pernet_device(&ip6gre_net_ops); | |
2166 | } | |
2167 | ||
2168 | module_init(ip6gre_init); | |
2169 | module_exit(ip6gre_fini); | |
2170 | MODULE_LICENSE("GPL"); | |
2171 | MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)"); | |
2172 | MODULE_DESCRIPTION("GRE over IPv6 tunneling device"); | |
2173 | MODULE_ALIAS_RTNL_LINK("ip6gre"); | |
5a4ee9a9 | 2174 | MODULE_ALIAS_RTNL_LINK("ip6gretap"); |
94d7d8f2 | 2175 | MODULE_ALIAS_RTNL_LINK("ip6erspan"); |
c12b395a | 2176 | MODULE_ALIAS_NETDEV("ip6gre0"); |