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> |
c12b395a | 58 | |
59 | ||
eccc1bb8 | 60 | static bool log_ecn_error = true; |
61 | module_param(log_ecn_error, bool, 0644); | |
62 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |
63 | ||
e87a8f24 JK |
64 | #define IP6_GRE_HASH_SIZE_SHIFT 5 |
65 | #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT) | |
c12b395a | 66 | |
c7d03a00 | 67 | static unsigned int ip6gre_net_id __read_mostly; |
c12b395a | 68 | struct ip6gre_net { |
e87a8f24 | 69 | struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE]; |
c12b395a | 70 | |
71 | struct net_device *fb_tunnel_dev; | |
72 | }; | |
73 | ||
74 | static struct rtnl_link_ops ip6gre_link_ops __read_mostly; | |
22f08069 | 75 | static struct rtnl_link_ops ip6gre_tap_ops __read_mostly; |
c12b395a | 76 | static int ip6gre_tunnel_init(struct net_device *dev); |
77 | static void ip6gre_tunnel_setup(struct net_device *dev); | |
78 | static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t); | |
79 | static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu); | |
80 | ||
81 | /* Tunnel hash table */ | |
82 | ||
83 | /* | |
84 | 4 hash tables: | |
85 | ||
86 | 3: (remote,local) | |
87 | 2: (remote,*) | |
88 | 1: (*,local) | |
89 | 0: (*,*) | |
90 | ||
91 | We require exact key match i.e. if a key is present in packet | |
92 | it will match only tunnel with the same key; if it is not present, | |
93 | it will match only keyless tunnel. | |
94 | ||
95 | All keysless packets, if not matched configured keyless tunnels | |
96 | will match fallback tunnel. | |
97 | */ | |
98 | ||
e87a8f24 | 99 | #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1)) |
c12b395a | 100 | static u32 HASH_ADDR(const struct in6_addr *addr) |
101 | { | |
102 | u32 hash = ipv6_addr_hash(addr); | |
103 | ||
e87a8f24 | 104 | return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT); |
c12b395a | 105 | } |
106 | ||
107 | #define tunnels_r_l tunnels[3] | |
108 | #define tunnels_r tunnels[2] | |
109 | #define tunnels_l tunnels[1] | |
110 | #define tunnels_wc tunnels[0] | |
c12b395a | 111 | |
c12b395a | 112 | /* Given src, dst and key, find appropriate for input tunnel. */ |
113 | ||
114 | static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev, | |
115 | const struct in6_addr *remote, const struct in6_addr *local, | |
116 | __be32 key, __be16 gre_proto) | |
117 | { | |
118 | struct net *net = dev_net(dev); | |
119 | int link = dev->ifindex; | |
120 | unsigned int h0 = HASH_ADDR(remote); | |
121 | unsigned int h1 = HASH_KEY(key); | |
122 | struct ip6_tnl *t, *cand = NULL; | |
123 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
124 | int dev_type = (gre_proto == htons(ETH_P_TEB)) ? | |
125 | ARPHRD_ETHER : ARPHRD_IP6GRE; | |
126 | int score, cand_score = 4; | |
127 | ||
e086cadc | 128 | for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) { |
c12b395a | 129 | if (!ipv6_addr_equal(local, &t->parms.laddr) || |
130 | !ipv6_addr_equal(remote, &t->parms.raddr) || | |
131 | key != t->parms.i_key || | |
132 | !(t->dev->flags & IFF_UP)) | |
133 | continue; | |
134 | ||
135 | if (t->dev->type != ARPHRD_IP6GRE && | |
136 | t->dev->type != dev_type) | |
137 | continue; | |
138 | ||
139 | score = 0; | |
140 | if (t->parms.link != link) | |
141 | score |= 1; | |
142 | if (t->dev->type != dev_type) | |
143 | score |= 2; | |
144 | if (score == 0) | |
145 | return t; | |
146 | ||
147 | if (score < cand_score) { | |
148 | cand = t; | |
149 | cand_score = score; | |
150 | } | |
151 | } | |
152 | ||
e086cadc | 153 | for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) { |
c12b395a | 154 | if (!ipv6_addr_equal(remote, &t->parms.raddr) || |
155 | key != t->parms.i_key || | |
156 | !(t->dev->flags & IFF_UP)) | |
157 | continue; | |
158 | ||
159 | if (t->dev->type != ARPHRD_IP6GRE && | |
160 | t->dev->type != dev_type) | |
161 | continue; | |
162 | ||
163 | score = 0; | |
164 | if (t->parms.link != link) | |
165 | score |= 1; | |
166 | if (t->dev->type != dev_type) | |
167 | score |= 2; | |
168 | if (score == 0) | |
169 | return t; | |
170 | ||
171 | if (score < cand_score) { | |
172 | cand = t; | |
173 | cand_score = score; | |
174 | } | |
175 | } | |
176 | ||
e086cadc | 177 | for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) { |
c12b395a | 178 | if ((!ipv6_addr_equal(local, &t->parms.laddr) && |
179 | (!ipv6_addr_equal(local, &t->parms.raddr) || | |
180 | !ipv6_addr_is_multicast(local))) || | |
181 | key != t->parms.i_key || | |
182 | !(t->dev->flags & IFF_UP)) | |
183 | continue; | |
184 | ||
185 | if (t->dev->type != ARPHRD_IP6GRE && | |
186 | t->dev->type != dev_type) | |
187 | continue; | |
188 | ||
189 | score = 0; | |
190 | if (t->parms.link != link) | |
191 | score |= 1; | |
192 | if (t->dev->type != dev_type) | |
193 | score |= 2; | |
194 | if (score == 0) | |
195 | return t; | |
196 | ||
197 | if (score < cand_score) { | |
198 | cand = t; | |
199 | cand_score = score; | |
200 | } | |
201 | } | |
202 | ||
e086cadc | 203 | for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) { |
c12b395a | 204 | if (t->parms.i_key != key || |
205 | !(t->dev->flags & IFF_UP)) | |
206 | continue; | |
207 | ||
208 | if (t->dev->type != ARPHRD_IP6GRE && | |
209 | t->dev->type != dev_type) | |
210 | continue; | |
211 | ||
212 | score = 0; | |
213 | if (t->parms.link != link) | |
214 | score |= 1; | |
215 | if (t->dev->type != dev_type) | |
216 | score |= 2; | |
217 | if (score == 0) | |
218 | return t; | |
219 | ||
220 | if (score < cand_score) { | |
221 | cand = t; | |
222 | cand_score = score; | |
223 | } | |
224 | } | |
225 | ||
53b24b8f | 226 | if (cand) |
c12b395a | 227 | return cand; |
228 | ||
229 | dev = ign->fb_tunnel_dev; | |
230 | if (dev->flags & IFF_UP) | |
231 | return netdev_priv(dev); | |
232 | ||
233 | return NULL; | |
234 | } | |
235 | ||
236 | static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign, | |
237 | const struct __ip6_tnl_parm *p) | |
238 | { | |
239 | const struct in6_addr *remote = &p->raddr; | |
240 | const struct in6_addr *local = &p->laddr; | |
241 | unsigned int h = HASH_KEY(p->i_key); | |
242 | int prio = 0; | |
243 | ||
244 | if (!ipv6_addr_any(local)) | |
245 | prio |= 1; | |
246 | if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) { | |
247 | prio |= 2; | |
248 | h ^= HASH_ADDR(remote); | |
249 | } | |
250 | ||
251 | return &ign->tunnels[prio][h]; | |
252 | } | |
253 | ||
254 | static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign, | |
255 | const struct ip6_tnl *t) | |
256 | { | |
257 | return __ip6gre_bucket(ign, &t->parms); | |
258 | } | |
259 | ||
260 | static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t) | |
261 | { | |
262 | struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t); | |
263 | ||
264 | rcu_assign_pointer(t->next, rtnl_dereference(*tp)); | |
265 | rcu_assign_pointer(*tp, t); | |
266 | } | |
267 | ||
268 | static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t) | |
269 | { | |
270 | struct ip6_tnl __rcu **tp; | |
271 | struct ip6_tnl *iter; | |
272 | ||
273 | for (tp = ip6gre_bucket(ign, t); | |
274 | (iter = rtnl_dereference(*tp)) != NULL; | |
275 | tp = &iter->next) { | |
276 | if (t == iter) { | |
277 | rcu_assign_pointer(*tp, t->next); | |
278 | break; | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
283 | static struct ip6_tnl *ip6gre_tunnel_find(struct net *net, | |
284 | const struct __ip6_tnl_parm *parms, | |
285 | int type) | |
286 | { | |
287 | const struct in6_addr *remote = &parms->raddr; | |
288 | const struct in6_addr *local = &parms->laddr; | |
289 | __be32 key = parms->i_key; | |
290 | int link = parms->link; | |
291 | struct ip6_tnl *t; | |
292 | struct ip6_tnl __rcu **tp; | |
293 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
294 | ||
295 | for (tp = __ip6gre_bucket(ign, parms); | |
296 | (t = rtnl_dereference(*tp)) != NULL; | |
297 | tp = &t->next) | |
298 | if (ipv6_addr_equal(local, &t->parms.laddr) && | |
299 | ipv6_addr_equal(remote, &t->parms.raddr) && | |
300 | key == t->parms.i_key && | |
301 | link == t->parms.link && | |
302 | type == t->dev->type) | |
303 | break; | |
304 | ||
305 | return t; | |
306 | } | |
307 | ||
308 | static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net, | |
309 | const struct __ip6_tnl_parm *parms, int create) | |
310 | { | |
311 | struct ip6_tnl *t, *nt; | |
312 | struct net_device *dev; | |
313 | char name[IFNAMSIZ]; | |
314 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
315 | ||
316 | t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE); | |
cd0a0bd9 SK |
317 | if (t && create) |
318 | return NULL; | |
c12b395a | 319 | if (t || !create) |
320 | return t; | |
321 | ||
322 | if (parms->name[0]) | |
323 | strlcpy(name, parms->name, IFNAMSIZ); | |
324 | else | |
325 | strcpy(name, "ip6gre%d"); | |
326 | ||
c835a677 TG |
327 | dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, |
328 | ip6gre_tunnel_setup); | |
c12b395a | 329 | if (!dev) |
330 | return NULL; | |
331 | ||
332 | dev_net_set(dev, net); | |
333 | ||
334 | nt = netdev_priv(dev); | |
335 | nt->parms = *parms; | |
336 | dev->rtnl_link_ops = &ip6gre_link_ops; | |
337 | ||
338 | nt->dev = dev; | |
0bd87628 | 339 | nt->net = dev_net(dev); |
c12b395a | 340 | ip6gre_tnl_link_config(nt, 1); |
341 | ||
342 | if (register_netdevice(dev) < 0) | |
343 | goto failed_free; | |
344 | ||
345 | /* Can use a lockless transmit, unless we generate output sequences */ | |
b45bd1d7 | 346 | if (!(nt->parms.o_flags & TUNNEL_SEQ)) |
c12b395a | 347 | dev->features |= NETIF_F_LLTX; |
348 | ||
349 | dev_hold(dev); | |
350 | ip6gre_tunnel_link(ign, nt); | |
351 | return nt; | |
352 | ||
353 | failed_free: | |
354 | free_netdev(dev); | |
355 | return NULL; | |
356 | } | |
357 | ||
358 | static void ip6gre_tunnel_uninit(struct net_device *dev) | |
359 | { | |
22f08069 ND |
360 | struct ip6_tnl *t = netdev_priv(dev); |
361 | struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id); | |
c12b395a | 362 | |
22f08069 | 363 | ip6gre_tunnel_unlink(ign, t); |
607f725f | 364 | dst_cache_reset(&t->dst_cache); |
c12b395a | 365 | dev_put(dev); |
366 | } | |
367 | ||
368 | ||
369 | static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
370 | u8 type, u8 code, int offset, __be32 info) | |
371 | { | |
372 | const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data; | |
b87fb39e ED |
373 | __be16 *p = (__be16 *)(skb->data + offset); |
374 | int grehlen = offset + 4; | |
c12b395a | 375 | struct ip6_tnl *t; |
376 | __be16 flags; | |
377 | ||
378 | flags = p[0]; | |
379 | if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) { | |
380 | if (flags&(GRE_VERSION|GRE_ROUTING)) | |
381 | return; | |
382 | if (flags&GRE_KEY) { | |
383 | grehlen += 4; | |
384 | if (flags&GRE_CSUM) | |
385 | grehlen += 4; | |
386 | } | |
387 | } | |
388 | ||
389 | /* If only 8 bytes returned, keyed message will be dropped here */ | |
b87fb39e | 390 | if (!pskb_may_pull(skb, grehlen)) |
c12b395a | 391 | return; |
b87fb39e ED |
392 | ipv6h = (const struct ipv6hdr *)skb->data; |
393 | p = (__be16 *)(skb->data + offset); | |
c12b395a | 394 | |
c12b395a | 395 | t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr, |
396 | flags & GRE_KEY ? | |
397 | *(((__be32 *)p) + (grehlen / 4) - 1) : 0, | |
398 | p[1]); | |
63159f29 | 399 | if (!t) |
0c5794a6 | 400 | return; |
c12b395a | 401 | |
402 | switch (type) { | |
403 | __u32 teli; | |
404 | struct ipv6_tlv_tnl_enc_lim *tel; | |
405 | __u32 mtu; | |
406 | case ICMPV6_DEST_UNREACH: | |
a46496ce MB |
407 | net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n", |
408 | t->parms.name); | |
c12b395a | 409 | break; |
410 | case ICMPV6_TIME_EXCEED: | |
411 | if (code == ICMPV6_EXC_HOPLIMIT) { | |
a46496ce MB |
412 | net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n", |
413 | t->parms.name); | |
c12b395a | 414 | } |
415 | break; | |
416 | case ICMPV6_PARAMPROB: | |
417 | teli = 0; | |
418 | if (code == ICMPV6_HDR_FIELD) | |
419 | teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data); | |
420 | ||
d1e158e2 | 421 | if (teli && teli == be32_to_cpu(info) - 2) { |
c12b395a | 422 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; |
423 | if (tel->encap_limit == 0) { | |
a46496ce MB |
424 | net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n", |
425 | t->parms.name); | |
c12b395a | 426 | } |
427 | } else { | |
a46496ce MB |
428 | net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n", |
429 | t->parms.name); | |
c12b395a | 430 | } |
431 | break; | |
432 | case ICMPV6_PKT_TOOBIG: | |
d1e158e2 | 433 | mtu = be32_to_cpu(info) - offset; |
c12b395a | 434 | if (mtu < IPV6_MIN_MTU) |
435 | mtu = IPV6_MIN_MTU; | |
436 | t->dev->mtu = mtu; | |
437 | break; | |
438 | } | |
439 | ||
440 | if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO)) | |
441 | t->err_count++; | |
442 | else | |
443 | t->err_count = 1; | |
444 | t->err_time = jiffies; | |
c12b395a | 445 | } |
446 | ||
308edfdf | 447 | static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi) |
c12b395a | 448 | { |
449 | const struct ipv6hdr *ipv6h; | |
c12b395a | 450 | struct ip6_tnl *tunnel; |
c12b395a | 451 | |
452 | ipv6h = ipv6_hdr(skb); | |
c12b395a | 453 | tunnel = ip6gre_tunnel_lookup(skb->dev, |
308edfdf TH |
454 | &ipv6h->saddr, &ipv6h->daddr, tpi->key, |
455 | tpi->proto); | |
c12b395a | 456 | if (tunnel) { |
308edfdf | 457 | ip6_tnl_rcv(tunnel, skb, tpi, NULL, false); |
c12b395a | 458 | |
308edfdf TH |
459 | return PACKET_RCVD; |
460 | } | |
eccc1bb8 | 461 | |
308edfdf TH |
462 | return PACKET_REJECT; |
463 | } | |
eccc1bb8 | 464 | |
308edfdf TH |
465 | static int gre_rcv(struct sk_buff *skb) |
466 | { | |
467 | struct tnl_ptk_info tpi; | |
468 | bool csum_err = false; | |
469 | int hdr_len; | |
eccc1bb8 | 470 | |
e582615a | 471 | hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0); |
f132ae7c | 472 | if (hdr_len < 0) |
308edfdf | 473 | goto drop; |
c12b395a | 474 | |
308edfdf TH |
475 | if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false)) |
476 | goto drop; | |
c12b395a | 477 | |
308edfdf | 478 | if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD) |
c12b395a | 479 | return 0; |
c12b395a | 480 | |
308edfdf | 481 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); |
c12b395a | 482 | drop: |
c12b395a | 483 | kfree_skb(skb); |
484 | return 0; | |
485 | } | |
486 | ||
b05229f4 | 487 | static int gre_handle_offloads(struct sk_buff *skb, bool csum) |
c12b395a | 488 | { |
b05229f4 TH |
489 | return iptunnel_handle_offloads(skb, |
490 | csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE); | |
c12b395a | 491 | } |
492 | ||
b05229f4 TH |
493 | static netdev_tx_t __gre6_xmit(struct sk_buff *skb, |
494 | struct net_device *dev, __u8 dsfield, | |
495 | struct flowi6 *fl6, int encap_limit, | |
496 | __u32 *pmtu, __be16 proto) | |
c12b395a | 497 | { |
c12b395a | 498 | struct ip6_tnl *tunnel = netdev_priv(dev); |
b05229f4 TH |
499 | __be16 protocol = (dev->type == ARPHRD_ETHER) ? |
500 | htons(ETH_P_TEB) : proto; | |
c12b395a | 501 | |
502 | if (dev->type == ARPHRD_ETHER) | |
503 | IPCB(skb)->flags = 0; | |
504 | ||
b05229f4 TH |
505 | if (dev->header_ops && dev->type == ARPHRD_IP6GRE) |
506 | fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr; | |
507 | else | |
c12b395a | 508 | fl6->daddr = tunnel->parms.raddr; |
c12b395a | 509 | |
b05229f4 TH |
510 | if (tunnel->parms.o_flags & TUNNEL_SEQ) |
511 | tunnel->o_seqno++; | |
c12b395a | 512 | |
b05229f4 TH |
513 | /* Push GRE header. */ |
514 | gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags, | |
515 | protocol, tunnel->parms.o_key, htonl(tunnel->o_seqno)); | |
c12b395a | 516 | |
b05229f4 TH |
517 | return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu, |
518 | NEXTHDR_GRE); | |
c12b395a | 519 | } |
520 | ||
521 | static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev) | |
522 | { | |
523 | struct ip6_tnl *t = netdev_priv(dev); | |
524 | const struct iphdr *iph = ip_hdr(skb); | |
525 | int encap_limit = -1; | |
526 | struct flowi6 fl6; | |
527 | __u8 dsfield; | |
528 | __u32 mtu; | |
529 | int err; | |
530 | ||
5146d1f1 BH |
531 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
532 | ||
c12b395a | 533 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
534 | encap_limit = t->parms.encap_limit; | |
535 | ||
536 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); | |
c12b395a | 537 | |
538 | dsfield = ipv4_get_dsfield(iph); | |
539 | ||
540 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) | |
541 | fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT) | |
542 | & IPV6_TCLASS_MASK; | |
543 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) | |
544 | fl6.flowi6_mark = skb->mark; | |
545 | ||
e2d118a1 LC |
546 | fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL); |
547 | ||
b05229f4 TH |
548 | err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)); |
549 | if (err) | |
550 | return -1; | |
551 | ||
552 | err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu, | |
553 | skb->protocol); | |
c12b395a | 554 | if (err != 0) { |
555 | /* XXX: send ICMP error even if DF is not set. */ | |
556 | if (err == -EMSGSIZE) | |
557 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, | |
558 | htonl(mtu)); | |
559 | return -1; | |
560 | } | |
561 | ||
562 | return 0; | |
563 | } | |
564 | ||
565 | static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev) | |
566 | { | |
567 | struct ip6_tnl *t = netdev_priv(dev); | |
568 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); | |
569 | int encap_limit = -1; | |
570 | __u16 offset; | |
571 | struct flowi6 fl6; | |
572 | __u8 dsfield; | |
573 | __u32 mtu; | |
574 | int err; | |
575 | ||
576 | if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr)) | |
577 | return -1; | |
578 | ||
579 | offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb)); | |
580 | if (offset > 0) { | |
581 | struct ipv6_tlv_tnl_enc_lim *tel; | |
582 | tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset]; | |
583 | if (tel->encap_limit == 0) { | |
584 | icmpv6_send(skb, ICMPV6_PARAMPROB, | |
585 | ICMPV6_HDR_FIELD, offset + 2); | |
586 | return -1; | |
587 | } | |
588 | encap_limit = tel->encap_limit - 1; | |
589 | } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
590 | encap_limit = t->parms.encap_limit; | |
591 | ||
592 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); | |
c12b395a | 593 | |
594 | dsfield = ipv6_get_dsfield(ipv6h); | |
595 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) | |
596 | fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK); | |
597 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) | |
3308de2b | 598 | fl6.flowlabel |= ip6_flowlabel(ipv6h); |
c12b395a | 599 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
600 | fl6.flowi6_mark = skb->mark; | |
601 | ||
e2d118a1 LC |
602 | fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL); |
603 | ||
b05229f4 TH |
604 | if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM))) |
605 | return -1; | |
606 | ||
607 | err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, | |
608 | &mtu, skb->protocol); | |
c12b395a | 609 | if (err != 0) { |
610 | if (err == -EMSGSIZE) | |
611 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); | |
612 | return -1; | |
613 | } | |
614 | ||
615 | return 0; | |
616 | } | |
617 | ||
618 | /** | |
619 | * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own | |
620 | * @t: the outgoing tunnel device | |
621 | * @hdr: IPv6 header from the incoming packet | |
622 | * | |
623 | * Description: | |
624 | * Avoid trivial tunneling loop by checking that tunnel exit-point | |
625 | * doesn't match source of incoming packet. | |
626 | * | |
627 | * Return: | |
628 | * 1 if conflict, | |
629 | * 0 else | |
630 | **/ | |
631 | ||
632 | static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t, | |
633 | const struct ipv6hdr *hdr) | |
634 | { | |
635 | return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); | |
636 | } | |
637 | ||
638 | static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev) | |
639 | { | |
640 | struct ip6_tnl *t = netdev_priv(dev); | |
641 | int encap_limit = -1; | |
642 | struct flowi6 fl6; | |
643 | __u32 mtu; | |
644 | int err; | |
645 | ||
646 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) | |
647 | encap_limit = t->parms.encap_limit; | |
648 | ||
649 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); | |
c12b395a | 650 | |
b05229f4 TH |
651 | err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)); |
652 | if (err) | |
653 | return err; | |
654 | ||
655 | err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol); | |
c12b395a | 656 | |
657 | return err; | |
658 | } | |
659 | ||
660 | static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb, | |
661 | struct net_device *dev) | |
662 | { | |
663 | struct ip6_tnl *t = netdev_priv(dev); | |
664 | struct net_device_stats *stats = &t->dev->stats; | |
665 | int ret; | |
666 | ||
d5005140 | 667 | if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr)) |
41ab3e31 | 668 | goto tx_err; |
c12b395a | 669 | |
670 | switch (skb->protocol) { | |
671 | case htons(ETH_P_IP): | |
672 | ret = ip6gre_xmit_ipv4(skb, dev); | |
673 | break; | |
674 | case htons(ETH_P_IPV6): | |
675 | ret = ip6gre_xmit_ipv6(skb, dev); | |
676 | break; | |
677 | default: | |
678 | ret = ip6gre_xmit_other(skb, dev); | |
679 | break; | |
680 | } | |
681 | ||
682 | if (ret < 0) | |
683 | goto tx_err; | |
684 | ||
685 | return NETDEV_TX_OK; | |
686 | ||
687 | tx_err: | |
688 | stats->tx_errors++; | |
689 | stats->tx_dropped++; | |
690 | kfree_skb(skb); | |
691 | return NETDEV_TX_OK; | |
692 | } | |
693 | ||
694 | static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu) | |
695 | { | |
696 | struct net_device *dev = t->dev; | |
697 | struct __ip6_tnl_parm *p = &t->parms; | |
698 | struct flowi6 *fl6 = &t->fl.u.ip6; | |
db2ec95d | 699 | int t_hlen; |
c12b395a | 700 | |
701 | if (dev->type != ARPHRD_ETHER) { | |
702 | memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); | |
703 | memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); | |
704 | } | |
705 | ||
706 | /* Set up flowi template */ | |
707 | fl6->saddr = p->laddr; | |
708 | fl6->daddr = p->raddr; | |
709 | fl6->flowi6_oif = p->link; | |
710 | fl6->flowlabel = 0; | |
252f3f5a | 711 | fl6->flowi6_proto = IPPROTO_GRE; |
c12b395a | 712 | |
713 | if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) | |
714 | fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; | |
715 | if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) | |
716 | fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; | |
717 | ||
718 | p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); | |
719 | p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); | |
720 | ||
721 | if (p->flags&IP6_TNL_F_CAP_XMIT && | |
722 | p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER) | |
723 | dev->flags |= IFF_POINTOPOINT; | |
724 | else | |
725 | dev->flags &= ~IFF_POINTOPOINT; | |
726 | ||
db2ec95d TH |
727 | t->tun_hlen = gre_calc_hlen(t->parms.o_flags); |
728 | ||
1faf3d9f | 729 | t->hlen = t->encap_hlen + t->tun_hlen; |
db2ec95d TH |
730 | |
731 | t_hlen = t->hlen + sizeof(struct ipv6hdr); | |
c12b395a | 732 | |
733 | if (p->flags & IP6_TNL_F_CAP_XMIT) { | |
734 | int strict = (ipv6_addr_type(&p->raddr) & | |
735 | (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); | |
736 | ||
22f08069 | 737 | struct rt6_info *rt = rt6_lookup(t->net, |
c12b395a | 738 | &p->raddr, &p->laddr, |
739 | p->link, strict); | |
740 | ||
63159f29 | 741 | if (!rt) |
c12b395a | 742 | return; |
743 | ||
744 | if (rt->dst.dev) { | |
db2ec95d TH |
745 | dev->hard_header_len = rt->dst.dev->hard_header_len + |
746 | t_hlen; | |
c12b395a | 747 | |
748 | if (set_mtu) { | |
db2ec95d | 749 | dev->mtu = rt->dst.dev->mtu - t_hlen; |
c12b395a | 750 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
751 | dev->mtu -= 8; | |
a9e242ca AD |
752 | if (dev->type == ARPHRD_ETHER) |
753 | dev->mtu -= ETH_HLEN; | |
c12b395a | 754 | |
755 | if (dev->mtu < IPV6_MIN_MTU) | |
756 | dev->mtu = IPV6_MIN_MTU; | |
757 | } | |
758 | } | |
94e187c0 | 759 | ip6_rt_put(rt); |
c12b395a | 760 | } |
c12b395a | 761 | } |
762 | ||
763 | static int ip6gre_tnl_change(struct ip6_tnl *t, | |
764 | const struct __ip6_tnl_parm *p, int set_mtu) | |
765 | { | |
766 | t->parms.laddr = p->laddr; | |
767 | t->parms.raddr = p->raddr; | |
768 | t->parms.flags = p->flags; | |
769 | t->parms.hop_limit = p->hop_limit; | |
770 | t->parms.encap_limit = p->encap_limit; | |
771 | t->parms.flowinfo = p->flowinfo; | |
772 | t->parms.link = p->link; | |
773 | t->parms.proto = p->proto; | |
774 | t->parms.i_key = p->i_key; | |
775 | t->parms.o_key = p->o_key; | |
776 | t->parms.i_flags = p->i_flags; | |
777 | t->parms.o_flags = p->o_flags; | |
607f725f | 778 | dst_cache_reset(&t->dst_cache); |
c12b395a | 779 | ip6gre_tnl_link_config(t, set_mtu); |
780 | return 0; | |
781 | } | |
782 | ||
783 | static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p, | |
784 | const struct ip6_tnl_parm2 *u) | |
785 | { | |
786 | p->laddr = u->laddr; | |
787 | p->raddr = u->raddr; | |
788 | p->flags = u->flags; | |
789 | p->hop_limit = u->hop_limit; | |
790 | p->encap_limit = u->encap_limit; | |
791 | p->flowinfo = u->flowinfo; | |
792 | p->link = u->link; | |
793 | p->i_key = u->i_key; | |
794 | p->o_key = u->o_key; | |
f41fe3c2 TH |
795 | p->i_flags = gre_flags_to_tnl_flags(u->i_flags); |
796 | p->o_flags = gre_flags_to_tnl_flags(u->o_flags); | |
c12b395a | 797 | memcpy(p->name, u->name, sizeof(u->name)); |
798 | } | |
799 | ||
800 | static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u, | |
801 | const struct __ip6_tnl_parm *p) | |
802 | { | |
803 | u->proto = IPPROTO_GRE; | |
804 | u->laddr = p->laddr; | |
805 | u->raddr = p->raddr; | |
806 | u->flags = p->flags; | |
807 | u->hop_limit = p->hop_limit; | |
808 | u->encap_limit = p->encap_limit; | |
809 | u->flowinfo = p->flowinfo; | |
810 | u->link = p->link; | |
811 | u->i_key = p->i_key; | |
812 | u->o_key = p->o_key; | |
f41fe3c2 TH |
813 | u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags); |
814 | u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags); | |
c12b395a | 815 | memcpy(u->name, p->name, sizeof(u->name)); |
816 | } | |
817 | ||
818 | static int ip6gre_tunnel_ioctl(struct net_device *dev, | |
819 | struct ifreq *ifr, int cmd) | |
820 | { | |
821 | int err = 0; | |
822 | struct ip6_tnl_parm2 p; | |
823 | struct __ip6_tnl_parm p1; | |
22f08069 ND |
824 | struct ip6_tnl *t = netdev_priv(dev); |
825 | struct net *net = t->net; | |
c12b395a | 826 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
827 | ||
308edfdf TH |
828 | memset(&p1, 0, sizeof(p1)); |
829 | ||
c12b395a | 830 | switch (cmd) { |
831 | case SIOCGETTUNNEL: | |
c12b395a | 832 | if (dev == ign->fb_tunnel_dev) { |
833 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { | |
834 | err = -EFAULT; | |
835 | break; | |
836 | } | |
837 | ip6gre_tnl_parm_from_user(&p1, &p); | |
838 | t = ip6gre_tunnel_locate(net, &p1, 0); | |
63159f29 | 839 | if (!t) |
22f08069 | 840 | t = netdev_priv(dev); |
c12b395a | 841 | } |
5dbd5068 | 842 | memset(&p, 0, sizeof(p)); |
c12b395a | 843 | ip6gre_tnl_parm_to_user(&p, &t->parms); |
844 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | |
845 | err = -EFAULT; | |
846 | break; | |
847 | ||
848 | case SIOCADDTUNNEL: | |
849 | case SIOCCHGTUNNEL: | |
850 | err = -EPERM; | |
af31f412 | 851 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
c12b395a | 852 | goto done; |
853 | ||
854 | err = -EFAULT; | |
855 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | |
856 | goto done; | |
857 | ||
858 | err = -EINVAL; | |
859 | if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)) | |
860 | goto done; | |
861 | ||
862 | if (!(p.i_flags&GRE_KEY)) | |
863 | p.i_key = 0; | |
864 | if (!(p.o_flags&GRE_KEY)) | |
865 | p.o_key = 0; | |
866 | ||
867 | ip6gre_tnl_parm_from_user(&p1, &p); | |
868 | t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL); | |
869 | ||
870 | if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { | |
53b24b8f | 871 | if (t) { |
c12b395a | 872 | if (t->dev != dev) { |
873 | err = -EEXIST; | |
874 | break; | |
875 | } | |
876 | } else { | |
877 | t = netdev_priv(dev); | |
878 | ||
879 | ip6gre_tunnel_unlink(ign, t); | |
880 | synchronize_net(); | |
881 | ip6gre_tnl_change(t, &p1, 1); | |
882 | ip6gre_tunnel_link(ign, t); | |
883 | netdev_state_change(dev); | |
884 | } | |
885 | } | |
886 | ||
887 | if (t) { | |
888 | err = 0; | |
889 | ||
5dbd5068 | 890 | memset(&p, 0, sizeof(p)); |
c12b395a | 891 | ip6gre_tnl_parm_to_user(&p, &t->parms); |
892 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | |
893 | err = -EFAULT; | |
894 | } else | |
895 | err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT); | |
896 | break; | |
897 | ||
898 | case SIOCDELTUNNEL: | |
899 | err = -EPERM; | |
af31f412 | 900 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
c12b395a | 901 | goto done; |
902 | ||
903 | if (dev == ign->fb_tunnel_dev) { | |
904 | err = -EFAULT; | |
905 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) | |
906 | goto done; | |
907 | err = -ENOENT; | |
908 | ip6gre_tnl_parm_from_user(&p1, &p); | |
909 | t = ip6gre_tunnel_locate(net, &p1, 0); | |
63159f29 | 910 | if (!t) |
c12b395a | 911 | goto done; |
912 | err = -EPERM; | |
913 | if (t == netdev_priv(ign->fb_tunnel_dev)) | |
914 | goto done; | |
915 | dev = t->dev; | |
916 | } | |
917 | unregister_netdevice(dev); | |
918 | err = 0; | |
919 | break; | |
920 | ||
921 | default: | |
922 | err = -EINVAL; | |
923 | } | |
924 | ||
925 | done: | |
926 | return err; | |
927 | } | |
928 | ||
c12b395a | 929 | static int ip6gre_header(struct sk_buff *skb, struct net_device *dev, |
930 | unsigned short type, | |
931 | const void *daddr, const void *saddr, unsigned int len) | |
932 | { | |
933 | struct ip6_tnl *t = netdev_priv(dev); | |
934 | struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen); | |
935 | __be16 *p = (__be16 *)(ipv6h+1); | |
936 | ||
cb1ce2ef TH |
937 | ip6_flow_hdr(ipv6h, 0, |
938 | ip6_make_flowlabel(dev_net(dev), skb, | |
42240901 | 939 | t->fl.u.ip6.flowlabel, true, |
67800f9b | 940 | &t->fl.u.ip6)); |
c12b395a | 941 | ipv6h->hop_limit = t->parms.hop_limit; |
942 | ipv6h->nexthdr = NEXTHDR_GRE; | |
943 | ipv6h->saddr = t->parms.laddr; | |
944 | ipv6h->daddr = t->parms.raddr; | |
945 | ||
946 | p[0] = t->parms.o_flags; | |
947 | p[1] = htons(type); | |
948 | ||
949 | /* | |
950 | * Set the source hardware address. | |
951 | */ | |
952 | ||
953 | if (saddr) | |
954 | memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr)); | |
955 | if (daddr) | |
956 | memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr)); | |
957 | if (!ipv6_addr_any(&ipv6h->daddr)) | |
958 | return t->hlen; | |
959 | ||
960 | return -t->hlen; | |
961 | } | |
962 | ||
c12b395a | 963 | static const struct header_ops ip6gre_header_ops = { |
964 | .create = ip6gre_header, | |
c12b395a | 965 | }; |
966 | ||
967 | static const struct net_device_ops ip6gre_netdev_ops = { | |
968 | .ndo_init = ip6gre_tunnel_init, | |
969 | .ndo_uninit = ip6gre_tunnel_uninit, | |
970 | .ndo_start_xmit = ip6gre_tunnel_xmit, | |
971 | .ndo_do_ioctl = ip6gre_tunnel_ioctl, | |
b05229f4 | 972 | .ndo_change_mtu = ip6_tnl_change_mtu, |
f61dd388 | 973 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
ecf2c06a | 974 | .ndo_get_iflink = ip6_tnl_get_iflink, |
c12b395a | 975 | }; |
976 | ||
977 | static void ip6gre_dev_free(struct net_device *dev) | |
978 | { | |
cdf3464e MKL |
979 | struct ip6_tnl *t = netdev_priv(dev); |
980 | ||
607f725f | 981 | dst_cache_destroy(&t->dst_cache); |
c12b395a | 982 | free_percpu(dev->tstats); |
983 | free_netdev(dev); | |
984 | } | |
985 | ||
986 | static void ip6gre_tunnel_setup(struct net_device *dev) | |
987 | { | |
c12b395a | 988 | dev->netdev_ops = &ip6gre_netdev_ops; |
989 | dev->destructor = ip6gre_dev_free; | |
990 | ||
991 | dev->type = ARPHRD_IP6GRE; | |
b05229f4 | 992 | |
c12b395a | 993 | dev->flags |= IFF_NOARP; |
c12b395a | 994 | dev->addr_len = sizeof(struct in6_addr); |
02875878 | 995 | netif_keep_dst(dev); |
c12b395a | 996 | } |
997 | ||
a3c119d3 | 998 | static int ip6gre_tunnel_init_common(struct net_device *dev) |
c12b395a | 999 | { |
1000 | struct ip6_tnl *tunnel; | |
cdf3464e | 1001 | int ret; |
b05229f4 | 1002 | int t_hlen; |
c12b395a | 1003 | |
1004 | tunnel = netdev_priv(dev); | |
1005 | ||
1006 | tunnel->dev = dev; | |
0bd87628 | 1007 | tunnel->net = dev_net(dev); |
c12b395a | 1008 | strcpy(tunnel->parms.name, dev->name); |
1009 | ||
a3c119d3 MKL |
1010 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
1011 | if (!dev->tstats) | |
1012 | return -ENOMEM; | |
1013 | ||
607f725f | 1014 | ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL); |
cdf3464e MKL |
1015 | if (ret) { |
1016 | free_percpu(dev->tstats); | |
1017 | dev->tstats = NULL; | |
1018 | return ret; | |
1019 | } | |
1020 | ||
b05229f4 | 1021 | tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags); |
1faf3d9f | 1022 | tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen; |
db2ec95d | 1023 | t_hlen = tunnel->hlen + sizeof(struct ipv6hdr); |
b05229f4 | 1024 | |
db2ec95d TH |
1025 | dev->hard_header_len = LL_MAX_HEADER + t_hlen; |
1026 | dev->mtu = ETH_DATA_LEN - t_hlen; | |
1b227e53 HY |
1027 | if (dev->type == ARPHRD_ETHER) |
1028 | dev->mtu -= ETH_HLEN; | |
b05229f4 TH |
1029 | if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
1030 | dev->mtu -= 8; | |
1031 | ||
a3c119d3 MKL |
1032 | return 0; |
1033 | } | |
1034 | ||
1035 | static int ip6gre_tunnel_init(struct net_device *dev) | |
1036 | { | |
1037 | struct ip6_tnl *tunnel; | |
1038 | int ret; | |
1039 | ||
1040 | ret = ip6gre_tunnel_init_common(dev); | |
1041 | if (ret) | |
1042 | return ret; | |
1043 | ||
1044 | tunnel = netdev_priv(dev); | |
1045 | ||
c12b395a | 1046 | memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr)); |
1047 | memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr)); | |
1048 | ||
1049 | if (ipv6_addr_any(&tunnel->parms.raddr)) | |
1050 | dev->header_ops = &ip6gre_header_ops; | |
1051 | ||
c12b395a | 1052 | return 0; |
1053 | } | |
1054 | ||
1055 | static void ip6gre_fb_tunnel_init(struct net_device *dev) | |
1056 | { | |
1057 | struct ip6_tnl *tunnel = netdev_priv(dev); | |
1058 | ||
1059 | tunnel->dev = dev; | |
0bd87628 | 1060 | tunnel->net = dev_net(dev); |
c12b395a | 1061 | strcpy(tunnel->parms.name, dev->name); |
1062 | ||
1063 | tunnel->hlen = sizeof(struct ipv6hdr) + 4; | |
1064 | ||
1065 | dev_hold(dev); | |
1066 | } | |
1067 | ||
1068 | ||
1069 | static struct inet6_protocol ip6gre_protocol __read_mostly = { | |
308edfdf | 1070 | .handler = gre_rcv, |
c12b395a | 1071 | .err_handler = ip6gre_err, |
1072 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, | |
1073 | }; | |
1074 | ||
22f08069 | 1075 | static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head) |
c12b395a | 1076 | { |
22f08069 ND |
1077 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
1078 | struct net_device *dev, *aux; | |
c12b395a | 1079 | int prio; |
1080 | ||
22f08069 ND |
1081 | for_each_netdev_safe(net, dev, aux) |
1082 | if (dev->rtnl_link_ops == &ip6gre_link_ops || | |
1083 | dev->rtnl_link_ops == &ip6gre_tap_ops) | |
1084 | unregister_netdevice_queue(dev, head); | |
1085 | ||
c12b395a | 1086 | for (prio = 0; prio < 4; prio++) { |
1087 | int h; | |
e87a8f24 | 1088 | for (h = 0; h < IP6_GRE_HASH_SIZE; h++) { |
c12b395a | 1089 | struct ip6_tnl *t; |
1090 | ||
1091 | t = rtnl_dereference(ign->tunnels[prio][h]); | |
1092 | ||
53b24b8f | 1093 | while (t) { |
22f08069 ND |
1094 | /* If dev is in the same netns, it has already |
1095 | * been added to the list by the previous loop. | |
1096 | */ | |
1097 | if (!net_eq(dev_net(t->dev), net)) | |
1098 | unregister_netdevice_queue(t->dev, | |
1099 | head); | |
c12b395a | 1100 | t = rtnl_dereference(t->next); |
1101 | } | |
1102 | } | |
1103 | } | |
1104 | } | |
1105 | ||
1106 | static int __net_init ip6gre_init_net(struct net *net) | |
1107 | { | |
1108 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
1109 | int err; | |
1110 | ||
1111 | ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0", | |
c835a677 TG |
1112 | NET_NAME_UNKNOWN, |
1113 | ip6gre_tunnel_setup); | |
c12b395a | 1114 | if (!ign->fb_tunnel_dev) { |
1115 | err = -ENOMEM; | |
1116 | goto err_alloc_dev; | |
1117 | } | |
1118 | dev_net_set(ign->fb_tunnel_dev, net); | |
22f08069 ND |
1119 | /* FB netdevice is special: we have one, and only one per netns. |
1120 | * Allowing to move it to another netns is clearly unsafe. | |
1121 | */ | |
1122 | ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL; | |
1123 | ||
c12b395a | 1124 | |
1125 | ip6gre_fb_tunnel_init(ign->fb_tunnel_dev); | |
1126 | ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops; | |
1127 | ||
1128 | err = register_netdev(ign->fb_tunnel_dev); | |
1129 | if (err) | |
1130 | goto err_reg_dev; | |
1131 | ||
1132 | rcu_assign_pointer(ign->tunnels_wc[0], | |
1133 | netdev_priv(ign->fb_tunnel_dev)); | |
1134 | return 0; | |
1135 | ||
1136 | err_reg_dev: | |
1137 | ip6gre_dev_free(ign->fb_tunnel_dev); | |
1138 | err_alloc_dev: | |
1139 | return err; | |
1140 | } | |
1141 | ||
1142 | static void __net_exit ip6gre_exit_net(struct net *net) | |
1143 | { | |
c12b395a | 1144 | LIST_HEAD(list); |
1145 | ||
c12b395a | 1146 | rtnl_lock(); |
22f08069 | 1147 | ip6gre_destroy_tunnels(net, &list); |
c12b395a | 1148 | unregister_netdevice_many(&list); |
1149 | rtnl_unlock(); | |
1150 | } | |
1151 | ||
1152 | static struct pernet_operations ip6gre_net_ops = { | |
1153 | .init = ip6gre_init_net, | |
1154 | .exit = ip6gre_exit_net, | |
1155 | .id = &ip6gre_net_id, | |
1156 | .size = sizeof(struct ip6gre_net), | |
1157 | }; | |
1158 | ||
1159 | static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[]) | |
1160 | { | |
1161 | __be16 flags; | |
1162 | ||
1163 | if (!data) | |
1164 | return 0; | |
1165 | ||
1166 | flags = 0; | |
1167 | if (data[IFLA_GRE_IFLAGS]) | |
1168 | flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); | |
1169 | if (data[IFLA_GRE_OFLAGS]) | |
1170 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); | |
1171 | if (flags & (GRE_VERSION|GRE_ROUTING)) | |
1172 | return -EINVAL; | |
1173 | ||
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[]) | |
1178 | { | |
1179 | struct in6_addr daddr; | |
1180 | ||
1181 | if (tb[IFLA_ADDRESS]) { | |
1182 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
1183 | return -EINVAL; | |
1184 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
1185 | return -EADDRNOTAVAIL; | |
1186 | } | |
1187 | ||
1188 | if (!data) | |
1189 | goto out; | |
1190 | ||
1191 | if (data[IFLA_GRE_REMOTE]) { | |
67b61f6c | 1192 | daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]); |
c12b395a | 1193 | if (ipv6_addr_any(&daddr)) |
1194 | return -EINVAL; | |
1195 | } | |
1196 | ||
1197 | out: | |
1198 | return ip6gre_tunnel_validate(tb, data); | |
1199 | } | |
1200 | ||
1201 | ||
1202 | static void ip6gre_netlink_parms(struct nlattr *data[], | |
1203 | struct __ip6_tnl_parm *parms) | |
1204 | { | |
1205 | memset(parms, 0, sizeof(*parms)); | |
1206 | ||
1207 | if (!data) | |
1208 | return; | |
1209 | ||
1210 | if (data[IFLA_GRE_LINK]) | |
1211 | parms->link = nla_get_u32(data[IFLA_GRE_LINK]); | |
1212 | ||
1213 | if (data[IFLA_GRE_IFLAGS]) | |
f41fe3c2 TH |
1214 | parms->i_flags = gre_flags_to_tnl_flags( |
1215 | nla_get_be16(data[IFLA_GRE_IFLAGS])); | |
c12b395a | 1216 | |
1217 | if (data[IFLA_GRE_OFLAGS]) | |
f41fe3c2 TH |
1218 | parms->o_flags = gre_flags_to_tnl_flags( |
1219 | nla_get_be16(data[IFLA_GRE_OFLAGS])); | |
c12b395a | 1220 | |
1221 | if (data[IFLA_GRE_IKEY]) | |
1222 | parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); | |
1223 | ||
1224 | if (data[IFLA_GRE_OKEY]) | |
1225 | parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); | |
1226 | ||
1227 | if (data[IFLA_GRE_LOCAL]) | |
67b61f6c | 1228 | parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]); |
c12b395a | 1229 | |
1230 | if (data[IFLA_GRE_REMOTE]) | |
67b61f6c | 1231 | parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]); |
c12b395a | 1232 | |
1233 | if (data[IFLA_GRE_TTL]) | |
1234 | parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]); | |
1235 | ||
1236 | if (data[IFLA_GRE_ENCAP_LIMIT]) | |
1237 | parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]); | |
1238 | ||
1239 | if (data[IFLA_GRE_FLOWINFO]) | |
c2675de4 | 1240 | parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]); |
c12b395a | 1241 | |
1242 | if (data[IFLA_GRE_FLAGS]) | |
1243 | parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]); | |
1244 | } | |
1245 | ||
1246 | static int ip6gre_tap_init(struct net_device *dev) | |
1247 | { | |
1248 | struct ip6_tnl *tunnel; | |
a3c119d3 | 1249 | int ret; |
c12b395a | 1250 | |
a3c119d3 MKL |
1251 | ret = ip6gre_tunnel_init_common(dev); |
1252 | if (ret) | |
1253 | return ret; | |
c12b395a | 1254 | |
0a46baaf SC |
1255 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
1256 | ||
a3c119d3 | 1257 | tunnel = netdev_priv(dev); |
c12b395a | 1258 | |
1259 | ip6gre_tnl_link_config(tunnel, 1); | |
1260 | ||
c12b395a | 1261 | return 0; |
1262 | } | |
1263 | ||
1264 | static const struct net_device_ops ip6gre_tap_netdev_ops = { | |
1265 | .ndo_init = ip6gre_tap_init, | |
1266 | .ndo_uninit = ip6gre_tunnel_uninit, | |
1267 | .ndo_start_xmit = ip6gre_tunnel_xmit, | |
1268 | .ndo_set_mac_address = eth_mac_addr, | |
1269 | .ndo_validate_addr = eth_validate_addr, | |
b05229f4 | 1270 | .ndo_change_mtu = ip6_tnl_change_mtu, |
f61dd388 | 1271 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
ecf2c06a | 1272 | .ndo_get_iflink = ip6_tnl_get_iflink, |
c12b395a | 1273 | }; |
1274 | ||
ac4eb009 AD |
1275 | #define GRE6_FEATURES (NETIF_F_SG | \ |
1276 | NETIF_F_FRAGLIST | \ | |
1277 | NETIF_F_HIGHDMA | \ | |
1278 | NETIF_F_HW_CSUM) | |
1279 | ||
c12b395a | 1280 | static void ip6gre_tap_setup(struct net_device *dev) |
1281 | { | |
1282 | ||
1283 | ether_setup(dev); | |
1284 | ||
1285 | dev->netdev_ops = &ip6gre_tap_netdev_ops; | |
1286 | dev->destructor = ip6gre_dev_free; | |
1287 | ||
c12b395a | 1288 | dev->features |= NETIF_F_NETNS_LOCAL; |
d13b161c | 1289 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
0a46baaf | 1290 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
c12b395a | 1291 | } |
1292 | ||
1faf3d9f TH |
1293 | static bool ip6gre_netlink_encap_parms(struct nlattr *data[], |
1294 | struct ip_tunnel_encap *ipencap) | |
1295 | { | |
1296 | bool ret = false; | |
1297 | ||
1298 | memset(ipencap, 0, sizeof(*ipencap)); | |
1299 | ||
1300 | if (!data) | |
1301 | return ret; | |
1302 | ||
1303 | if (data[IFLA_GRE_ENCAP_TYPE]) { | |
1304 | ret = true; | |
1305 | ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]); | |
1306 | } | |
1307 | ||
1308 | if (data[IFLA_GRE_ENCAP_FLAGS]) { | |
1309 | ret = true; | |
1310 | ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]); | |
1311 | } | |
1312 | ||
1313 | if (data[IFLA_GRE_ENCAP_SPORT]) { | |
1314 | ret = true; | |
1315 | ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]); | |
1316 | } | |
1317 | ||
1318 | if (data[IFLA_GRE_ENCAP_DPORT]) { | |
1319 | ret = true; | |
1320 | ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]); | |
1321 | } | |
1322 | ||
1323 | return ret; | |
1324 | } | |
1325 | ||
c12b395a | 1326 | static int ip6gre_newlink(struct net *src_net, struct net_device *dev, |
1327 | struct nlattr *tb[], struct nlattr *data[]) | |
1328 | { | |
1329 | struct ip6_tnl *nt; | |
1330 | struct net *net = dev_net(dev); | |
1331 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
1faf3d9f | 1332 | struct ip_tunnel_encap ipencap; |
c12b395a | 1333 | int err; |
1334 | ||
1335 | nt = netdev_priv(dev); | |
1faf3d9f TH |
1336 | |
1337 | if (ip6gre_netlink_encap_parms(data, &ipencap)) { | |
1338 | int err = ip6_tnl_encap_setup(nt, &ipencap); | |
1339 | ||
1340 | if (err < 0) | |
1341 | return err; | |
1342 | } | |
1343 | ||
c12b395a | 1344 | ip6gre_netlink_parms(data, &nt->parms); |
1345 | ||
1346 | if (ip6gre_tunnel_find(net, &nt->parms, dev->type)) | |
1347 | return -EEXIST; | |
1348 | ||
1349 | if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS]) | |
1350 | eth_hw_addr_random(dev); | |
1351 | ||
1352 | nt->dev = dev; | |
0bd87628 | 1353 | nt->net = dev_net(dev); |
c12b395a | 1354 | ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]); |
1355 | ||
ac4eb009 AD |
1356 | dev->features |= GRE6_FEATURES; |
1357 | dev->hw_features |= GRE6_FEATURES; | |
1358 | ||
b45bd1d7 | 1359 | if (!(nt->parms.o_flags & TUNNEL_SEQ)) { |
6a553681 AD |
1360 | /* TCP offload with GRE SEQ is not supported, nor |
1361 | * can we support 2 levels of outer headers requiring | |
1362 | * an update. | |
3a80e1fa | 1363 | */ |
6a553681 AD |
1364 | if (!(nt->parms.o_flags & TUNNEL_CSUM) || |
1365 | (nt->encap.type == TUNNEL_ENCAP_NONE)) { | |
1366 | dev->features |= NETIF_F_GSO_SOFTWARE; | |
1367 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; | |
1368 | } | |
3a80e1fa AD |
1369 | |
1370 | /* Can use a lockless transmit, unless we generate | |
1371 | * output sequences | |
1372 | */ | |
c12b395a | 1373 | dev->features |= NETIF_F_LLTX; |
3a80e1fa | 1374 | } |
c12b395a | 1375 | |
1376 | err = register_netdevice(dev); | |
1377 | if (err) | |
1378 | goto out; | |
1379 | ||
1380 | dev_hold(dev); | |
1381 | ip6gre_tunnel_link(ign, nt); | |
1382 | ||
1383 | out: | |
1384 | return err; | |
1385 | } | |
1386 | ||
1387 | static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[], | |
1388 | struct nlattr *data[]) | |
1389 | { | |
22f08069 ND |
1390 | struct ip6_tnl *t, *nt = netdev_priv(dev); |
1391 | struct net *net = nt->net; | |
c12b395a | 1392 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); |
1393 | struct __ip6_tnl_parm p; | |
1faf3d9f | 1394 | struct ip_tunnel_encap ipencap; |
c12b395a | 1395 | |
1396 | if (dev == ign->fb_tunnel_dev) | |
1397 | return -EINVAL; | |
1398 | ||
1faf3d9f TH |
1399 | if (ip6gre_netlink_encap_parms(data, &ipencap)) { |
1400 | int err = ip6_tnl_encap_setup(nt, &ipencap); | |
1401 | ||
1402 | if (err < 0) | |
1403 | return err; | |
1404 | } | |
1405 | ||
c12b395a | 1406 | ip6gre_netlink_parms(data, &p); |
1407 | ||
1408 | t = ip6gre_tunnel_locate(net, &p, 0); | |
1409 | ||
1410 | if (t) { | |
1411 | if (t->dev != dev) | |
1412 | return -EEXIST; | |
1413 | } else { | |
1414 | t = nt; | |
c12b395a | 1415 | } |
1416 | ||
6a61d4db ND |
1417 | ip6gre_tunnel_unlink(ign, t); |
1418 | ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]); | |
1419 | ip6gre_tunnel_link(ign, t); | |
c12b395a | 1420 | return 0; |
1421 | } | |
1422 | ||
54d63f78 ND |
1423 | static void ip6gre_dellink(struct net_device *dev, struct list_head *head) |
1424 | { | |
1425 | struct net *net = dev_net(dev); | |
1426 | struct ip6gre_net *ign = net_generic(net, ip6gre_net_id); | |
1427 | ||
1428 | if (dev != ign->fb_tunnel_dev) | |
1429 | unregister_netdevice_queue(dev, head); | |
1430 | } | |
1431 | ||
c12b395a | 1432 | static size_t ip6gre_get_size(const struct net_device *dev) |
1433 | { | |
1434 | return | |
1435 | /* IFLA_GRE_LINK */ | |
1436 | nla_total_size(4) + | |
1437 | /* IFLA_GRE_IFLAGS */ | |
1438 | nla_total_size(2) + | |
1439 | /* IFLA_GRE_OFLAGS */ | |
1440 | nla_total_size(2) + | |
1441 | /* IFLA_GRE_IKEY */ | |
1442 | nla_total_size(4) + | |
1443 | /* IFLA_GRE_OKEY */ | |
1444 | nla_total_size(4) + | |
1445 | /* IFLA_GRE_LOCAL */ | |
a3754133 | 1446 | nla_total_size(sizeof(struct in6_addr)) + |
c12b395a | 1447 | /* IFLA_GRE_REMOTE */ |
a3754133 | 1448 | nla_total_size(sizeof(struct in6_addr)) + |
c12b395a | 1449 | /* IFLA_GRE_TTL */ |
1450 | nla_total_size(1) + | |
c12b395a | 1451 | /* IFLA_GRE_ENCAP_LIMIT */ |
1452 | nla_total_size(1) + | |
1453 | /* IFLA_GRE_FLOWINFO */ | |
1454 | nla_total_size(4) + | |
1455 | /* IFLA_GRE_FLAGS */ | |
1456 | nla_total_size(4) + | |
1faf3d9f TH |
1457 | /* IFLA_GRE_ENCAP_TYPE */ |
1458 | nla_total_size(2) + | |
1459 | /* IFLA_GRE_ENCAP_FLAGS */ | |
1460 | nla_total_size(2) + | |
1461 | /* IFLA_GRE_ENCAP_SPORT */ | |
1462 | nla_total_size(2) + | |
1463 | /* IFLA_GRE_ENCAP_DPORT */ | |
1464 | nla_total_size(2) + | |
c12b395a | 1465 | 0; |
1466 | } | |
1467 | ||
1468 | static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
1469 | { | |
1470 | struct ip6_tnl *t = netdev_priv(dev); | |
1471 | struct __ip6_tnl_parm *p = &t->parms; | |
1472 | ||
1473 | if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || | |
f41fe3c2 TH |
1474 | nla_put_be16(skb, IFLA_GRE_IFLAGS, |
1475 | gre_tnl_flags_to_gre_flags(p->i_flags)) || | |
1476 | nla_put_be16(skb, IFLA_GRE_OFLAGS, | |
1477 | gre_tnl_flags_to_gre_flags(p->o_flags)) || | |
c12b395a | 1478 | nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || |
1479 | nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || | |
930345ea JB |
1480 | nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) || |
1481 | nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) || | |
c12b395a | 1482 | nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) || |
c12b395a | 1483 | nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) || |
1484 | nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) || | |
1485 | nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags)) | |
1486 | goto nla_put_failure; | |
1faf3d9f TH |
1487 | |
1488 | if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE, | |
1489 | t->encap.type) || | |
1490 | nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT, | |
1491 | t->encap.sport) || | |
1492 | nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT, | |
1493 | t->encap.dport) || | |
1494 | nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS, | |
1495 | t->encap.flags)) | |
1496 | goto nla_put_failure; | |
1497 | ||
c12b395a | 1498 | return 0; |
1499 | ||
1500 | nla_put_failure: | |
1501 | return -EMSGSIZE; | |
1502 | } | |
1503 | ||
1504 | static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = { | |
1505 | [IFLA_GRE_LINK] = { .type = NLA_U32 }, | |
1506 | [IFLA_GRE_IFLAGS] = { .type = NLA_U16 }, | |
1507 | [IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, | |
1508 | [IFLA_GRE_IKEY] = { .type = NLA_U32 }, | |
1509 | [IFLA_GRE_OKEY] = { .type = NLA_U32 }, | |
1510 | [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) }, | |
1511 | [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) }, | |
1512 | [IFLA_GRE_TTL] = { .type = NLA_U8 }, | |
1513 | [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 }, | |
1514 | [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 }, | |
1515 | [IFLA_GRE_FLAGS] = { .type = NLA_U32 }, | |
1faf3d9f TH |
1516 | [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 }, |
1517 | [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 }, | |
1518 | [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 }, | |
1519 | [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 }, | |
c12b395a | 1520 | }; |
1521 | ||
1522 | static struct rtnl_link_ops ip6gre_link_ops __read_mostly = { | |
1523 | .kind = "ip6gre", | |
1524 | .maxtype = IFLA_GRE_MAX, | |
1525 | .policy = ip6gre_policy, | |
1526 | .priv_size = sizeof(struct ip6_tnl), | |
1527 | .setup = ip6gre_tunnel_setup, | |
1528 | .validate = ip6gre_tunnel_validate, | |
1529 | .newlink = ip6gre_newlink, | |
1530 | .changelink = ip6gre_changelink, | |
54d63f78 | 1531 | .dellink = ip6gre_dellink, |
c12b395a | 1532 | .get_size = ip6gre_get_size, |
1533 | .fill_info = ip6gre_fill_info, | |
1728d4fa | 1534 | .get_link_net = ip6_tnl_get_link_net, |
c12b395a | 1535 | }; |
1536 | ||
1537 | static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = { | |
1538 | .kind = "ip6gretap", | |
1539 | .maxtype = IFLA_GRE_MAX, | |
1540 | .policy = ip6gre_policy, | |
1541 | .priv_size = sizeof(struct ip6_tnl), | |
1542 | .setup = ip6gre_tap_setup, | |
1543 | .validate = ip6gre_tap_validate, | |
1544 | .newlink = ip6gre_newlink, | |
1545 | .changelink = ip6gre_changelink, | |
1546 | .get_size = ip6gre_get_size, | |
1547 | .fill_info = ip6gre_fill_info, | |
3390e397 | 1548 | .get_link_net = ip6_tnl_get_link_net, |
c12b395a | 1549 | }; |
1550 | ||
1551 | /* | |
1552 | * And now the modules code and kernel interface. | |
1553 | */ | |
1554 | ||
1555 | static int __init ip6gre_init(void) | |
1556 | { | |
1557 | int err; | |
1558 | ||
1559 | pr_info("GRE over IPv6 tunneling driver\n"); | |
1560 | ||
1561 | err = register_pernet_device(&ip6gre_net_ops); | |
1562 | if (err < 0) | |
1563 | return err; | |
1564 | ||
1565 | err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE); | |
1566 | if (err < 0) { | |
1567 | pr_info("%s: can't add protocol\n", __func__); | |
1568 | goto add_proto_failed; | |
1569 | } | |
1570 | ||
1571 | err = rtnl_link_register(&ip6gre_link_ops); | |
1572 | if (err < 0) | |
1573 | goto rtnl_link_failed; | |
1574 | ||
1575 | err = rtnl_link_register(&ip6gre_tap_ops); | |
1576 | if (err < 0) | |
1577 | goto tap_ops_failed; | |
1578 | ||
1579 | out: | |
1580 | return err; | |
1581 | ||
1582 | tap_ops_failed: | |
1583 | rtnl_link_unregister(&ip6gre_link_ops); | |
1584 | rtnl_link_failed: | |
1585 | inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); | |
1586 | add_proto_failed: | |
1587 | unregister_pernet_device(&ip6gre_net_ops); | |
1588 | goto out; | |
1589 | } | |
1590 | ||
1591 | static void __exit ip6gre_fini(void) | |
1592 | { | |
1593 | rtnl_link_unregister(&ip6gre_tap_ops); | |
1594 | rtnl_link_unregister(&ip6gre_link_ops); | |
1595 | inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE); | |
1596 | unregister_pernet_device(&ip6gre_net_ops); | |
1597 | } | |
1598 | ||
1599 | module_init(ip6gre_init); | |
1600 | module_exit(ip6gre_fini); | |
1601 | MODULE_LICENSE("GPL"); | |
1602 | MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)"); | |
1603 | MODULE_DESCRIPTION("GRE over IPv6 tunneling device"); | |
1604 | MODULE_ALIAS_RTNL_LINK("ip6gre"); | |
5a4ee9a9 | 1605 | MODULE_ALIAS_RTNL_LINK("ip6gretap"); |
c12b395a | 1606 | MODULE_ALIAS_NETDEV("ip6gre0"); |