blob: a88aff02b579be88796edc318d413daf12a828ff [file] [log] [blame]
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001/*
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>
xeb@mail.ruc12b3952012-08-10 00:51:50 +000027#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>
Pravin B Shelarc5441932013-03-25 14:49:35 +000040#include <net/ip_tunnels.h>
xeb@mail.ruc12b3952012-08-10 00:51:50 +000041#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>
Tom Herbert308edfdf2016-04-29 17:12:17 -070057#include <net/gre.h>
xeb@mail.ruc12b3952012-08-10 00:51:50 +000058
59
stephen hemmingereccc1bb2012-09-25 11:02:48 +000060static bool log_ecn_error = true;
61module_param(log_ecn_error, bool, 0644);
62MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
63
Jiri Kosinae87a8f22016-08-10 11:03:35 +020064#define IP6_GRE_HASH_SIZE_SHIFT 5
65#define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
xeb@mail.ruc12b3952012-08-10 00:51:50 +000066
67static int ip6gre_net_id __read_mostly;
68struct ip6gre_net {
Jiri Kosinae87a8f22016-08-10 11:03:35 +020069 struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE];
xeb@mail.ruc12b3952012-08-10 00:51:50 +000070
71 struct net_device *fb_tunnel_dev;
72};
73
74static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
Nicolas Dichtel22f08062014-04-22 10:15:24 +020075static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
xeb@mail.ruc12b3952012-08-10 00:51:50 +000076static int ip6gre_tunnel_init(struct net_device *dev);
77static void ip6gre_tunnel_setup(struct net_device *dev);
78static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
79static 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
Jiri Kosinae87a8f22016-08-10 11:03:35 +020099#define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000100static u32 HASH_ADDR(const struct in6_addr *addr)
101{
102 u32 hash = ipv6_addr_hash(addr);
103
Jiri Kosinae87a8f22016-08-10 11:03:35 +0200104 return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000105}
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]
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000111
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000112/* Given src, dst and key, find appropriate for input tunnel. */
113
114static 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
Amerigo Wange086cad2012-11-11 21:52:34 +0000128 for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000129 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
Amerigo Wange086cad2012-11-11 21:52:34 +0000153 for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000154 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
Amerigo Wange086cad2012-11-11 21:52:34 +0000177 for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000178 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
Amerigo Wange086cad2012-11-11 21:52:34 +0000203 for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000204 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
Ian Morris53b24b82015-03-29 14:00:05 +0100226 if (cand)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000227 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
236static 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
254static 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
260static 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
268static 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
283static 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
308static 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);
Steffen Klassertcd0a0bd2014-09-22 10:07:26 +0200317 if (t && create)
318 return NULL;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000319 if (t || !create)
320 return t;
321
Eric Dumazet5dcf6eb2018-04-05 06:39:29 -0700322 if (parms->name[0]) {
323 if (!dev_valid_name(parms->name))
324 return NULL;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000325 strlcpy(name, parms->name, IFNAMSIZ);
Eric Dumazet5dcf6eb2018-04-05 06:39:29 -0700326 } else {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000327 strcpy(name, "ip6gre%d");
Eric Dumazet5dcf6eb2018-04-05 06:39:29 -0700328 }
Tom Gundersenc835a672014-07-14 16:37:24 +0200329 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
330 ip6gre_tunnel_setup);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000331 if (!dev)
332 return NULL;
333
334 dev_net_set(dev, net);
335
336 nt = netdev_priv(dev);
337 nt->parms = *parms;
338 dev->rtnl_link_ops = &ip6gre_link_ops;
339
340 nt->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +0200341 nt->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000342
343 if (register_netdevice(dev) < 0)
344 goto failed_free;
345
Alexey Kodanevcc99c6d2018-01-18 20:51:12 +0300346 ip6gre_tnl_link_config(nt, 1);
347
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000348 /* Can use a lockless transmit, unless we generate output sequences */
Tom Herbertb45bd1d2016-05-09 17:12:12 -0700349 if (!(nt->parms.o_flags & TUNNEL_SEQ))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000350 dev->features |= NETIF_F_LLTX;
351
352 dev_hold(dev);
353 ip6gre_tunnel_link(ign, nt);
354 return nt;
355
356failed_free:
357 free_netdev(dev);
358 return NULL;
359}
360
361static void ip6gre_tunnel_uninit(struct net_device *dev)
362{
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200363 struct ip6_tnl *t = netdev_priv(dev);
364 struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000365
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200366 ip6gre_tunnel_unlink(ign, t);
Paolo Abeni607f7252016-02-12 15:43:54 +0100367 dst_cache_reset(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000368 dev_put(dev);
369}
370
371
372static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Eric Dumazetae1768b2017-02-04 23:18:55 -0800373 u8 type, u8 code, int offset, __be32 info)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000374{
Eric Dumazetae1768b2017-02-04 23:18:55 -0800375 const struct gre_base_hdr *greh;
376 const struct ipv6hdr *ipv6h;
377 int grehlen = sizeof(*greh);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000378 struct ip6_tnl *t;
Eric Dumazetae1768b2017-02-04 23:18:55 -0800379 int key_off = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000380 __be16 flags;
Eric Dumazetae1768b2017-02-04 23:18:55 -0800381 __be32 key;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000382
Eric Dumazetae1768b2017-02-04 23:18:55 -0800383 if (!pskb_may_pull(skb, offset + grehlen))
384 return;
385 greh = (const struct gre_base_hdr *)(skb->data + offset);
386 flags = greh->flags;
387 if (flags & (GRE_VERSION | GRE_ROUTING))
388 return;
389 if (flags & GRE_CSUM)
390 grehlen += 4;
391 if (flags & GRE_KEY) {
392 key_off = grehlen + offset;
393 grehlen += 4;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000394 }
395
Eric Dumazetae1768b2017-02-04 23:18:55 -0800396 if (!pskb_may_pull(skb, offset + grehlen))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000397 return;
Eric Dumazetb87fb392012-08-19 03:47:30 +0000398 ipv6h = (const struct ipv6hdr *)skb->data;
Eric Dumazetae1768b2017-02-04 23:18:55 -0800399 greh = (const struct gre_base_hdr *)(skb->data + offset);
400 key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000401
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000402 t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
Eric Dumazetae1768b2017-02-04 23:18:55 -0800403 key, greh->protocol);
Ian Morris63159f22015-03-29 14:00:04 +0100404 if (!t)
stephen hemminger0c5794a2012-09-24 18:12:24 +0000405 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000406
407 switch (type) {
408 __u32 teli;
409 struct ipv6_tlv_tnl_enc_lim *tel;
410 __u32 mtu;
411 case ICMPV6_DEST_UNREACH:
Matt Bennetta46496c2015-09-23 16:58:31 +1200412 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
413 t->parms.name);
Xin Long6d428bc2017-10-26 19:23:27 +0800414 if (code != ICMPV6_PORT_UNREACH)
415 break;
416 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000417 case ICMPV6_TIME_EXCEED:
418 if (code == ICMPV6_EXC_HOPLIMIT) {
Matt Bennetta46496c2015-09-23 16:58:31 +1200419 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
420 t->parms.name);
Xin Long6d428bc2017-10-26 19:23:27 +0800421 break;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000422 }
Xin Long6d428bc2017-10-26 19:23:27 +0800423 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000424 case ICMPV6_PARAMPROB:
425 teli = 0;
426 if (code == ICMPV6_HDR_FIELD)
427 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
428
Sabrina Dubrocad1e158e2015-02-04 15:25:09 +0100429 if (teli && teli == be32_to_cpu(info) - 2) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000430 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
431 if (tel->encap_limit == 0) {
Matt Bennetta46496c2015-09-23 16:58:31 +1200432 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
433 t->parms.name);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000434 }
435 } else {
Matt Bennetta46496c2015-09-23 16:58:31 +1200436 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
437 t->parms.name);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000438 }
Xin Long6d428bc2017-10-26 19:23:27 +0800439 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000440 case ICMPV6_PKT_TOOBIG:
Xin Longca7d8a32017-09-05 17:26:33 +0800441 mtu = be32_to_cpu(info) - offset - t->tun_hlen;
442 if (t->dev->type == ARPHRD_ETHER)
443 mtu -= ETH_HLEN;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000444 if (mtu < IPV6_MIN_MTU)
445 mtu = IPV6_MIN_MTU;
446 t->dev->mtu = mtu;
Xin Long6d428bc2017-10-26 19:23:27 +0800447 return;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000448 }
449
450 if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
451 t->err_count++;
452 else
453 t->err_count = 1;
454 t->err_time = jiffies;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000455}
456
Tom Herbert308edfdf2016-04-29 17:12:17 -0700457static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000458{
459 const struct ipv6hdr *ipv6h;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000460 struct ip6_tnl *tunnel;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000461
462 ipv6h = ipv6_hdr(skb);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000463 tunnel = ip6gre_tunnel_lookup(skb->dev,
Tom Herbert308edfdf2016-04-29 17:12:17 -0700464 &ipv6h->saddr, &ipv6h->daddr, tpi->key,
465 tpi->proto);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000466 if (tunnel) {
Alexey Kodanev2388d522017-11-17 19:16:17 +0300467 ip6_tnl_rcv(tunnel, skb, tpi, NULL, log_ecn_error);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000468
Tom Herbert308edfdf2016-04-29 17:12:17 -0700469 return PACKET_RCVD;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000470 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000471
Tom Herbert308edfdf2016-04-29 17:12:17 -0700472 return PACKET_REJECT;
473}
474
475static int gre_rcv(struct sk_buff *skb)
476{
477 struct tnl_ptk_info tpi;
478 bool csum_err = false;
479 int hdr_len;
480
Eric Dumazete5826152016-06-15 06:24:00 -0700481 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
Jiri Bencf132ae72016-05-03 15:00:21 +0200482 if (hdr_len < 0)
Tom Herbert308edfdf2016-04-29 17:12:17 -0700483 goto drop;
484
485 if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
486 goto drop;
487
488 if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
489 return 0;
490
491 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000492drop:
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000493 kfree_skb(skb);
494 return 0;
495}
496
497struct ipv6_tel_txoption {
498 struct ipv6_txoptions ops;
499 __u8 dst_opt[8];
500};
501
Tom Herbertb05229f2016-04-29 17:12:21 -0700502static int gre_handle_offloads(struct sk_buff *skb, bool csum)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000503{
Tom Herbertb05229f2016-04-29 17:12:21 -0700504 return iptunnel_handle_offloads(skb,
505 csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000506}
507
Tom Herbertb05229f2016-04-29 17:12:21 -0700508static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
509 struct net_device *dev, __u8 dsfield,
510 struct flowi6 *fl6, int encap_limit,
511 __u32 *pmtu, __be16 proto)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000512{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000513 struct ip6_tnl *tunnel = netdev_priv(dev);
Xin Longd6b1aeb2017-10-26 19:27:17 +0800514 struct dst_entry *dst = skb_dst(skb);
515 __be16 protocol;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000516
517 if (dev->type == ARPHRD_ETHER)
518 IPCB(skb)->flags = 0;
519
Tom Herbertb05229f2016-04-29 17:12:21 -0700520 if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
521 fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
522 else
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000523 fl6->daddr = tunnel->parms.raddr;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000524
Tom Herbertb05229f2016-04-29 17:12:21 -0700525 if (tunnel->parms.o_flags & TUNNEL_SEQ)
526 tunnel->o_seqno++;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000527
Tom Herbertb05229f2016-04-29 17:12:21 -0700528 /* Push GRE header. */
Xin Longd6b1aeb2017-10-26 19:27:17 +0800529 protocol = (dev->type == ARPHRD_ETHER) ? htons(ETH_P_TEB) : proto;
Tom Herbertb05229f2016-04-29 17:12:21 -0700530 gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
531 protocol, tunnel->parms.o_key, htonl(tunnel->o_seqno));
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000532
Xin Longd6b1aeb2017-10-26 19:27:17 +0800533 /* TooBig packet may have updated dst->dev's mtu */
534 if (dst && dst_mtu(dst) > dst->dev->mtu)
535 dst->ops->update_pmtu(dst, NULL, skb, dst->dev->mtu);
536
Tom Herbertb05229f2016-04-29 17:12:21 -0700537 return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
538 NEXTHDR_GRE);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000539}
540
541static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
542{
543 struct ip6_tnl *t = netdev_priv(dev);
544 const struct iphdr *iph = ip_hdr(skb);
545 int encap_limit = -1;
546 struct flowi6 fl6;
547 __u8 dsfield;
548 __u32 mtu;
549 int err;
550
Bernie Harris5146d1f2016-02-22 12:58:05 +1300551 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
552
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000553 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
554 encap_limit = t->parms.encap_limit;
555
556 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000557
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000558 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
Peter Dawsonadfe95f2017-05-26 06:35:18 +1000559 dsfield = ipv4_get_dsfield(iph);
560 else
561 dsfield = ip6_tclass(t->parms.flowinfo);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000562 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
563 fl6.flowi6_mark = skb->mark;
564
Lorenzo Colitti50442922016-11-04 02:23:43 +0900565 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
566
Tom Herbertb05229f2016-04-29 17:12:21 -0700567 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
568 if (err)
569 return -1;
570
571 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
572 skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000573 if (err != 0) {
574 /* XXX: send ICMP error even if DF is not set. */
575 if (err == -EMSGSIZE)
576 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
577 htonl(mtu));
578 return -1;
579 }
580
581 return 0;
582}
583
584static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
585{
586 struct ip6_tnl *t = netdev_priv(dev);
587 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
588 int encap_limit = -1;
589 __u16 offset;
590 struct flowi6 fl6;
591 __u8 dsfield;
592 __u32 mtu;
593 int err;
594
595 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
596 return -1;
597
598 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
Eric Dumazetb07bf232017-01-23 16:43:05 -0800599 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
600 ipv6h = ipv6_hdr(skb);
601
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000602 if (offset > 0) {
603 struct ipv6_tlv_tnl_enc_lim *tel;
604 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
605 if (tel->encap_limit == 0) {
606 icmpv6_send(skb, ICMPV6_PARAMPROB,
607 ICMPV6_HDR_FIELD, offset + 2);
608 return -1;
609 }
610 encap_limit = tel->encap_limit - 1;
611 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
612 encap_limit = t->parms.encap_limit;
613
614 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000615
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000616 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
Peter Dawsonadfe95f2017-05-26 06:35:18 +1000617 dsfield = ipv6_get_dsfield(ipv6h);
618 else
619 dsfield = ip6_tclass(t->parms.flowinfo);
620
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000621 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
Florent Fourcot3308de22013-12-08 15:47:00 +0100622 fl6.flowlabel |= ip6_flowlabel(ipv6h);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000623 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
624 fl6.flowi6_mark = skb->mark;
625
Lorenzo Colitti50442922016-11-04 02:23:43 +0900626 fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
627
Tom Herbertb05229f2016-04-29 17:12:21 -0700628 if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
629 return -1;
630
631 err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
632 &mtu, skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000633 if (err != 0) {
634 if (err == -EMSGSIZE)
635 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
636 return -1;
637 }
638
639 return 0;
640}
641
642/**
643 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
644 * @t: the outgoing tunnel device
645 * @hdr: IPv6 header from the incoming packet
646 *
647 * Description:
648 * Avoid trivial tunneling loop by checking that tunnel exit-point
649 * doesn't match source of incoming packet.
650 *
651 * Return:
652 * 1 if conflict,
653 * 0 else
654 **/
655
656static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
657 const struct ipv6hdr *hdr)
658{
659 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
660}
661
662static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
663{
664 struct ip6_tnl *t = netdev_priv(dev);
665 int encap_limit = -1;
666 struct flowi6 fl6;
667 __u32 mtu;
668 int err;
669
670 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
671 encap_limit = t->parms.encap_limit;
672
673 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000674
Tom Herbertb05229f2016-04-29 17:12:21 -0700675 err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
676 if (err)
677 return err;
678
679 err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000680
681 return err;
682}
683
684static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
685 struct net_device *dev)
686{
687 struct ip6_tnl *t = netdev_priv(dev);
688 struct net_device_stats *stats = &t->dev->stats;
689 int ret;
690
Steffen Klassertd5005142014-11-05 08:02:48 +0100691 if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
Tommi Rantala41ab3e32013-02-06 03:24:02 +0000692 goto tx_err;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000693
694 switch (skb->protocol) {
695 case htons(ETH_P_IP):
696 ret = ip6gre_xmit_ipv4(skb, dev);
697 break;
698 case htons(ETH_P_IPV6):
699 ret = ip6gre_xmit_ipv6(skb, dev);
700 break;
701 default:
702 ret = ip6gre_xmit_other(skb, dev);
703 break;
704 }
705
706 if (ret < 0)
707 goto tx_err;
708
709 return NETDEV_TX_OK;
710
711tx_err:
712 stats->tx_errors++;
713 stats->tx_dropped++;
714 kfree_skb(skb);
715 return NETDEV_TX_OK;
716}
717
718static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
719{
720 struct net_device *dev = t->dev;
721 struct __ip6_tnl_parm *p = &t->parms;
722 struct flowi6 *fl6 = &t->fl.u.ip6;
Tom Herbertdb2ec952016-05-09 17:12:08 -0700723 int t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000724
725 if (dev->type != ARPHRD_ETHER) {
726 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
727 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
728 }
729
730 /* Set up flowi template */
731 fl6->saddr = p->laddr;
732 fl6->daddr = p->raddr;
733 fl6->flowi6_oif = p->link;
734 fl6->flowlabel = 0;
Haishuang Yan252f3f52016-05-21 18:17:35 +0800735 fl6->flowi6_proto = IPPROTO_GRE;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000736
737 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
738 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
739 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
740 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
741
742 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
743 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
744
745 if (p->flags&IP6_TNL_F_CAP_XMIT &&
746 p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
747 dev->flags |= IFF_POINTOPOINT;
748 else
749 dev->flags &= ~IFF_POINTOPOINT;
750
Tom Herbertdb2ec952016-05-09 17:12:08 -0700751 t->tun_hlen = gre_calc_hlen(t->parms.o_flags);
752
Tom Herbert1faf3d92016-05-18 09:06:19 -0700753 t->hlen = t->encap_hlen + t->tun_hlen;
Tom Herbertdb2ec952016-05-09 17:12:08 -0700754
755 t_hlen = t->hlen + sizeof(struct ipv6hdr);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000756
757 if (p->flags & IP6_TNL_F_CAP_XMIT) {
758 int strict = (ipv6_addr_type(&p->raddr) &
759 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
760
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200761 struct rt6_info *rt = rt6_lookup(t->net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000762 &p->raddr, &p->laddr,
763 p->link, strict);
764
Ian Morris63159f22015-03-29 14:00:04 +0100765 if (!rt)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000766 return;
767
768 if (rt->dst.dev) {
Tom Herbertdb2ec952016-05-09 17:12:08 -0700769 dev->hard_header_len = rt->dst.dev->hard_header_len +
770 t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000771
772 if (set_mtu) {
Tom Herbertdb2ec952016-05-09 17:12:08 -0700773 dev->mtu = rt->dst.dev->mtu - t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000774 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
775 dev->mtu -= 8;
Alexander Duycka9e242c2016-04-14 15:33:45 -0400776 if (dev->type == ARPHRD_ETHER)
777 dev->mtu -= ETH_HLEN;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000778
779 if (dev->mtu < IPV6_MIN_MTU)
780 dev->mtu = IPV6_MIN_MTU;
781 }
782 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000783 ip6_rt_put(rt);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000784 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000785}
786
787static int ip6gre_tnl_change(struct ip6_tnl *t,
788 const struct __ip6_tnl_parm *p, int set_mtu)
789{
790 t->parms.laddr = p->laddr;
791 t->parms.raddr = p->raddr;
792 t->parms.flags = p->flags;
793 t->parms.hop_limit = p->hop_limit;
794 t->parms.encap_limit = p->encap_limit;
795 t->parms.flowinfo = p->flowinfo;
796 t->parms.link = p->link;
797 t->parms.proto = p->proto;
798 t->parms.i_key = p->i_key;
799 t->parms.o_key = p->o_key;
800 t->parms.i_flags = p->i_flags;
801 t->parms.o_flags = p->o_flags;
Paolo Abeni607f7252016-02-12 15:43:54 +0100802 dst_cache_reset(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000803 ip6gre_tnl_link_config(t, set_mtu);
804 return 0;
805}
806
807static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
808 const struct ip6_tnl_parm2 *u)
809{
810 p->laddr = u->laddr;
811 p->raddr = u->raddr;
812 p->flags = u->flags;
813 p->hop_limit = u->hop_limit;
814 p->encap_limit = u->encap_limit;
815 p->flowinfo = u->flowinfo;
816 p->link = u->link;
817 p->i_key = u->i_key;
818 p->o_key = u->o_key;
Tom Herbertf41fe3c2016-05-09 17:12:09 -0700819 p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
820 p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000821 memcpy(p->name, u->name, sizeof(u->name));
822}
823
824static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
825 const struct __ip6_tnl_parm *p)
826{
827 u->proto = IPPROTO_GRE;
828 u->laddr = p->laddr;
829 u->raddr = p->raddr;
830 u->flags = p->flags;
831 u->hop_limit = p->hop_limit;
832 u->encap_limit = p->encap_limit;
833 u->flowinfo = p->flowinfo;
834 u->link = p->link;
835 u->i_key = p->i_key;
836 u->o_key = p->o_key;
Tom Herbertf41fe3c2016-05-09 17:12:09 -0700837 u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
838 u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000839 memcpy(u->name, p->name, sizeof(u->name));
840}
841
842static int ip6gre_tunnel_ioctl(struct net_device *dev,
843 struct ifreq *ifr, int cmd)
844{
845 int err = 0;
846 struct ip6_tnl_parm2 p;
847 struct __ip6_tnl_parm p1;
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200848 struct ip6_tnl *t = netdev_priv(dev);
849 struct net *net = t->net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000850 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
851
Tom Herbert308edfdf2016-04-29 17:12:17 -0700852 memset(&p1, 0, sizeof(p1));
853
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000854 switch (cmd) {
855 case SIOCGETTUNNEL:
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000856 if (dev == ign->fb_tunnel_dev) {
857 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
858 err = -EFAULT;
859 break;
860 }
861 ip6gre_tnl_parm_from_user(&p1, &p);
862 t = ip6gre_tunnel_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +0100863 if (!t)
Nicolas Dichtel22f08062014-04-22 10:15:24 +0200864 t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000865 }
Amerigo Wang5dbd5062013-05-09 21:56:37 +0000866 memset(&p, 0, sizeof(p));
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000867 ip6gre_tnl_parm_to_user(&p, &t->parms);
868 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
869 err = -EFAULT;
870 break;
871
872 case SIOCADDTUNNEL:
873 case SIOCCHGTUNNEL:
874 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000875 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000876 goto done;
877
878 err = -EFAULT;
879 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
880 goto done;
881
882 err = -EINVAL;
883 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
884 goto done;
885
886 if (!(p.i_flags&GRE_KEY))
887 p.i_key = 0;
888 if (!(p.o_flags&GRE_KEY))
889 p.o_key = 0;
890
891 ip6gre_tnl_parm_from_user(&p1, &p);
892 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
893
894 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
Ian Morris53b24b82015-03-29 14:00:05 +0100895 if (t) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000896 if (t->dev != dev) {
897 err = -EEXIST;
898 break;
899 }
900 } else {
901 t = netdev_priv(dev);
902
903 ip6gre_tunnel_unlink(ign, t);
904 synchronize_net();
905 ip6gre_tnl_change(t, &p1, 1);
906 ip6gre_tunnel_link(ign, t);
907 netdev_state_change(dev);
908 }
909 }
910
911 if (t) {
912 err = 0;
913
Amerigo Wang5dbd5062013-05-09 21:56:37 +0000914 memset(&p, 0, sizeof(p));
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000915 ip6gre_tnl_parm_to_user(&p, &t->parms);
916 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
917 err = -EFAULT;
918 } else
919 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
920 break;
921
922 case SIOCDELTUNNEL:
923 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000924 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000925 goto done;
926
927 if (dev == ign->fb_tunnel_dev) {
928 err = -EFAULT;
929 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
930 goto done;
931 err = -ENOENT;
932 ip6gre_tnl_parm_from_user(&p1, &p);
933 t = ip6gre_tunnel_locate(net, &p1, 0);
Ian Morris63159f22015-03-29 14:00:04 +0100934 if (!t)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000935 goto done;
936 err = -EPERM;
937 if (t == netdev_priv(ign->fb_tunnel_dev))
938 goto done;
939 dev = t->dev;
940 }
941 unregister_netdevice(dev);
942 err = 0;
943 break;
944
945 default:
946 err = -EINVAL;
947 }
948
949done:
950 return err;
951}
952
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000953static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
Xin Longe814bae2017-09-15 12:00:07 +0800954 unsigned short type, const void *daddr,
955 const void *saddr, unsigned int len)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000956{
957 struct ip6_tnl *t = netdev_priv(dev);
Xin Longe814bae2017-09-15 12:00:07 +0800958 struct ipv6hdr *ipv6h;
959 __be16 *p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000960
Xin Longe814bae2017-09-15 12:00:07 +0800961 ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen + sizeof(*ipv6h));
962 ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
963 t->fl.u.ip6.flowlabel,
964 true, &t->fl.u.ip6));
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000965 ipv6h->hop_limit = t->parms.hop_limit;
966 ipv6h->nexthdr = NEXTHDR_GRE;
967 ipv6h->saddr = t->parms.laddr;
968 ipv6h->daddr = t->parms.raddr;
969
Xin Longe814bae2017-09-15 12:00:07 +0800970 p = (__be16 *)(ipv6h + 1);
971 p[0] = t->parms.o_flags;
972 p[1] = htons(type);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000973
974 /*
975 * Set the source hardware address.
976 */
977
978 if (saddr)
979 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
980 if (daddr)
981 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
982 if (!ipv6_addr_any(&ipv6h->daddr))
983 return t->hlen;
984
985 return -t->hlen;
986}
987
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000988static const struct header_ops ip6gre_header_ops = {
989 .create = ip6gre_header,
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000990};
991
992static const struct net_device_ops ip6gre_netdev_ops = {
993 .ndo_init = ip6gre_tunnel_init,
994 .ndo_uninit = ip6gre_tunnel_uninit,
995 .ndo_start_xmit = ip6gre_tunnel_xmit,
996 .ndo_do_ioctl = ip6gre_tunnel_ioctl,
Tom Herbertb05229f2016-04-29 17:12:21 -0700997 .ndo_change_mtu = ip6_tnl_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +0000998 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +0200999 .ndo_get_iflink = ip6_tnl_get_iflink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001000};
1001
1002static void ip6gre_dev_free(struct net_device *dev)
1003{
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001004 struct ip6_tnl *t = netdev_priv(dev);
1005
Paolo Abeni607f7252016-02-12 15:43:54 +01001006 dst_cache_destroy(&t->dst_cache);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001007 free_percpu(dev->tstats);
1008 free_netdev(dev);
1009}
1010
1011static void ip6gre_tunnel_setup(struct net_device *dev)
1012{
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001013 dev->netdev_ops = &ip6gre_netdev_ops;
1014 dev->destructor = ip6gre_dev_free;
1015
1016 dev->type = ARPHRD_IP6GRE;
Tom Herbertb05229f2016-04-29 17:12:21 -07001017
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001018 dev->flags |= IFF_NOARP;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001019 dev->addr_len = sizeof(struct in6_addr);
Eric Dumazet02875872014-10-05 18:38:35 -07001020 netif_keep_dst(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001021}
1022
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001023static int ip6gre_tunnel_init_common(struct net_device *dev)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001024{
1025 struct ip6_tnl *tunnel;
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001026 int ret;
Tom Herbertb05229f2016-04-29 17:12:21 -07001027 int t_hlen;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001028
1029 tunnel = netdev_priv(dev);
1030
1031 tunnel->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001032 tunnel->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001033 strcpy(tunnel->parms.name, dev->name);
1034
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001035 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1036 if (!dev->tstats)
1037 return -ENOMEM;
1038
Paolo Abeni607f7252016-02-12 15:43:54 +01001039 ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
Martin KaFai Laucdf34642015-09-15 14:30:07 -07001040 if (ret) {
1041 free_percpu(dev->tstats);
1042 dev->tstats = NULL;
1043 return ret;
1044 }
1045
Tom Herbertb05229f2016-04-29 17:12:21 -07001046 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001047 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
Tom Herbertb05229f2016-04-29 17:12:21 -07001048 t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
1049
Tom Herbertdb2ec952016-05-09 17:12:08 -07001050 dev->hard_header_len = LL_MAX_HEADER + t_hlen;
1051 dev->mtu = ETH_DATA_LEN - t_hlen;
Haishuang Yan1b227e52016-05-21 18:17:34 +08001052 if (dev->type == ARPHRD_ETHER)
1053 dev->mtu -= ETH_HLEN;
Tom Herbertb05229f2016-04-29 17:12:21 -07001054 if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1055 dev->mtu -= 8;
1056
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001057 return 0;
1058}
1059
1060static int ip6gre_tunnel_init(struct net_device *dev)
1061{
1062 struct ip6_tnl *tunnel;
1063 int ret;
1064
1065 ret = ip6gre_tunnel_init_common(dev);
1066 if (ret)
1067 return ret;
1068
1069 tunnel = netdev_priv(dev);
1070
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001071 memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1072 memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1073
1074 if (ipv6_addr_any(&tunnel->parms.raddr))
1075 dev->header_ops = &ip6gre_header_ops;
1076
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001077 return 0;
1078}
1079
1080static void ip6gre_fb_tunnel_init(struct net_device *dev)
1081{
1082 struct ip6_tnl *tunnel = netdev_priv(dev);
1083
1084 tunnel->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001085 tunnel->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001086 strcpy(tunnel->parms.name, dev->name);
1087
1088 tunnel->hlen = sizeof(struct ipv6hdr) + 4;
1089
1090 dev_hold(dev);
1091}
1092
1093
1094static struct inet6_protocol ip6gre_protocol __read_mostly = {
Tom Herbert308edfdf2016-04-29 17:12:17 -07001095 .handler = gre_rcv,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001096 .err_handler = ip6gre_err,
1097 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1098};
1099
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001100static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001101{
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001102 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1103 struct net_device *dev, *aux;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001104 int prio;
1105
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001106 for_each_netdev_safe(net, dev, aux)
1107 if (dev->rtnl_link_ops == &ip6gre_link_ops ||
1108 dev->rtnl_link_ops == &ip6gre_tap_ops)
1109 unregister_netdevice_queue(dev, head);
1110
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001111 for (prio = 0; prio < 4; prio++) {
1112 int h;
Jiri Kosinae87a8f22016-08-10 11:03:35 +02001113 for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001114 struct ip6_tnl *t;
1115
1116 t = rtnl_dereference(ign->tunnels[prio][h]);
1117
Ian Morris53b24b82015-03-29 14:00:05 +01001118 while (t) {
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001119 /* If dev is in the same netns, it has already
1120 * been added to the list by the previous loop.
1121 */
1122 if (!net_eq(dev_net(t->dev), net))
1123 unregister_netdevice_queue(t->dev,
1124 head);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001125 t = rtnl_dereference(t->next);
1126 }
1127 }
1128 }
1129}
1130
1131static int __net_init ip6gre_init_net(struct net *net)
1132{
1133 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1134 int err;
1135
1136 ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
Tom Gundersenc835a672014-07-14 16:37:24 +02001137 NET_NAME_UNKNOWN,
1138 ip6gre_tunnel_setup);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001139 if (!ign->fb_tunnel_dev) {
1140 err = -ENOMEM;
1141 goto err_alloc_dev;
1142 }
1143 dev_net_set(ign->fb_tunnel_dev, net);
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001144 /* FB netdevice is special: we have one, and only one per netns.
1145 * Allowing to move it to another netns is clearly unsafe.
1146 */
1147 ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
1148
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001149
1150 ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1151 ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1152
1153 err = register_netdev(ign->fb_tunnel_dev);
1154 if (err)
1155 goto err_reg_dev;
1156
1157 rcu_assign_pointer(ign->tunnels_wc[0],
1158 netdev_priv(ign->fb_tunnel_dev));
1159 return 0;
1160
1161err_reg_dev:
1162 ip6gre_dev_free(ign->fb_tunnel_dev);
1163err_alloc_dev:
1164 return err;
1165}
1166
1167static void __net_exit ip6gre_exit_net(struct net *net)
1168{
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001169 LIST_HEAD(list);
1170
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001171 rtnl_lock();
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001172 ip6gre_destroy_tunnels(net, &list);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001173 unregister_netdevice_many(&list);
1174 rtnl_unlock();
1175}
1176
1177static struct pernet_operations ip6gre_net_ops = {
1178 .init = ip6gre_init_net,
1179 .exit = ip6gre_exit_net,
1180 .id = &ip6gre_net_id,
1181 .size = sizeof(struct ip6gre_net),
1182};
1183
1184static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
1185{
1186 __be16 flags;
1187
1188 if (!data)
1189 return 0;
1190
1191 flags = 0;
1192 if (data[IFLA_GRE_IFLAGS])
1193 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1194 if (data[IFLA_GRE_OFLAGS])
1195 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1196 if (flags & (GRE_VERSION|GRE_ROUTING))
1197 return -EINVAL;
1198
1199 return 0;
1200}
1201
1202static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
1203{
1204 struct in6_addr daddr;
1205
1206 if (tb[IFLA_ADDRESS]) {
1207 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1208 return -EINVAL;
1209 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1210 return -EADDRNOTAVAIL;
1211 }
1212
1213 if (!data)
1214 goto out;
1215
1216 if (data[IFLA_GRE_REMOTE]) {
Jiri Benc67b61f62015-03-29 16:59:26 +02001217 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001218 if (ipv6_addr_any(&daddr))
1219 return -EINVAL;
1220 }
1221
1222out:
1223 return ip6gre_tunnel_validate(tb, data);
1224}
1225
1226
1227static void ip6gre_netlink_parms(struct nlattr *data[],
1228 struct __ip6_tnl_parm *parms)
1229{
1230 memset(parms, 0, sizeof(*parms));
1231
1232 if (!data)
1233 return;
1234
1235 if (data[IFLA_GRE_LINK])
1236 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1237
1238 if (data[IFLA_GRE_IFLAGS])
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001239 parms->i_flags = gre_flags_to_tnl_flags(
1240 nla_get_be16(data[IFLA_GRE_IFLAGS]));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001241
1242 if (data[IFLA_GRE_OFLAGS])
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001243 parms->o_flags = gre_flags_to_tnl_flags(
1244 nla_get_be16(data[IFLA_GRE_OFLAGS]));
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001245
1246 if (data[IFLA_GRE_IKEY])
1247 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1248
1249 if (data[IFLA_GRE_OKEY])
1250 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1251
1252 if (data[IFLA_GRE_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001253 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001254
1255 if (data[IFLA_GRE_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001256 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001257
1258 if (data[IFLA_GRE_TTL])
1259 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1260
1261 if (data[IFLA_GRE_ENCAP_LIMIT])
1262 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1263
1264 if (data[IFLA_GRE_FLOWINFO])
Lance Richardsonc2675de2016-09-24 14:01:04 -04001265 parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001266
1267 if (data[IFLA_GRE_FLAGS])
1268 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
1269}
1270
1271static int ip6gre_tap_init(struct net_device *dev)
1272{
Martin KaFai Laua3c119d2015-09-15 14:30:05 -07001273 int ret;
1274
1275 ret = ip6gre_tunnel_init_common(dev);
1276 if (ret)
1277 return ret;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001278
Shweta Choudaha0a46baa2016-06-08 20:15:43 +01001279 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1280
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001281 return 0;
1282}
1283
1284static const struct net_device_ops ip6gre_tap_netdev_ops = {
1285 .ndo_init = ip6gre_tap_init,
1286 .ndo_uninit = ip6gre_tunnel_uninit,
1287 .ndo_start_xmit = ip6gre_tunnel_xmit,
1288 .ndo_set_mac_address = eth_mac_addr,
1289 .ndo_validate_addr = eth_validate_addr,
Tom Herbertb05229f2016-04-29 17:12:21 -07001290 .ndo_change_mtu = ip6_tnl_change_mtu,
Pravin B Shelarf61dd382013-03-25 14:50:00 +00001291 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtelecf2c062015-04-02 17:07:01 +02001292 .ndo_get_iflink = ip6_tnl_get_iflink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001293};
1294
Alexander Duyckac4eb002016-04-14 15:33:51 -04001295#define GRE6_FEATURES (NETIF_F_SG | \
1296 NETIF_F_FRAGLIST | \
1297 NETIF_F_HIGHDMA | \
1298 NETIF_F_HW_CSUM)
1299
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001300static void ip6gre_tap_setup(struct net_device *dev)
1301{
1302
1303 ether_setup(dev);
1304
1305 dev->netdev_ops = &ip6gre_tap_netdev_ops;
1306 dev->destructor = ip6gre_dev_free;
1307
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001308 dev->features |= NETIF_F_NETNS_LOCAL;
Jiri Bencd13b1612016-02-17 15:32:53 +01001309 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Shweta Choudaha0a46baa2016-06-08 20:15:43 +01001310 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Longab4da562017-09-28 13:23:50 +08001311 netif_keep_dst(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001312}
1313
Tom Herbert1faf3d92016-05-18 09:06:19 -07001314static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
1315 struct ip_tunnel_encap *ipencap)
1316{
1317 bool ret = false;
1318
1319 memset(ipencap, 0, sizeof(*ipencap));
1320
1321 if (!data)
1322 return ret;
1323
1324 if (data[IFLA_GRE_ENCAP_TYPE]) {
1325 ret = true;
1326 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1327 }
1328
1329 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1330 ret = true;
1331 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1332 }
1333
1334 if (data[IFLA_GRE_ENCAP_SPORT]) {
1335 ret = true;
1336 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1337 }
1338
1339 if (data[IFLA_GRE_ENCAP_DPORT]) {
1340 ret = true;
1341 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1342 }
1343
1344 return ret;
1345}
1346
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001347static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
1348 struct nlattr *tb[], struct nlattr *data[])
1349{
1350 struct ip6_tnl *nt;
1351 struct net *net = dev_net(dev);
1352 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001353 struct ip_tunnel_encap ipencap;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001354 int err;
1355
1356 nt = netdev_priv(dev);
Tom Herbert1faf3d92016-05-18 09:06:19 -07001357
1358 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1359 int err = ip6_tnl_encap_setup(nt, &ipencap);
1360
1361 if (err < 0)
1362 return err;
1363 }
1364
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001365 ip6gre_netlink_parms(data, &nt->parms);
1366
1367 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1368 return -EEXIST;
1369
1370 if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1371 eth_hw_addr_random(dev);
1372
1373 nt->dev = dev;
Nicolas Dichtel0bd876282013-08-13 17:51:12 +02001374 nt->net = dev_net(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001375
Alexander Duyckac4eb002016-04-14 15:33:51 -04001376 dev->features |= GRE6_FEATURES;
1377 dev->hw_features |= GRE6_FEATURES;
1378
Tom Herbertb45bd1d2016-05-09 17:12:12 -07001379 if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
Alexander Duyck6a553682016-05-18 10:44:47 -07001380 /* TCP offload with GRE SEQ is not supported, nor
1381 * can we support 2 levels of outer headers requiring
1382 * an update.
Alexander Duyck3a80e1f2016-04-14 15:34:04 -04001383 */
Alexander Duyck6a553682016-05-18 10:44:47 -07001384 if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
1385 (nt->encap.type == TUNNEL_ENCAP_NONE)) {
1386 dev->features |= NETIF_F_GSO_SOFTWARE;
1387 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1388 }
Alexander Duyck3a80e1f2016-04-14 15:34:04 -04001389
1390 /* Can use a lockless transmit, unless we generate
1391 * output sequences
1392 */
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001393 dev->features |= NETIF_F_LLTX;
Alexander Duyck3a80e1f2016-04-14 15:34:04 -04001394 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001395
1396 err = register_netdevice(dev);
1397 if (err)
1398 goto out;
1399
Alexey Kodanevcc99c6d2018-01-18 20:51:12 +03001400 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
1401
1402 if (tb[IFLA_MTU])
1403 ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1404
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001405 dev_hold(dev);
1406 ip6gre_tunnel_link(ign, nt);
1407
1408out:
1409 return err;
1410}
1411
1412static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
1413 struct nlattr *data[])
1414{
Nicolas Dichtel22f08062014-04-22 10:15:24 +02001415 struct ip6_tnl *t, *nt = netdev_priv(dev);
1416 struct net *net = nt->net;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001417 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1418 struct __ip6_tnl_parm p;
Tom Herbert1faf3d92016-05-18 09:06:19 -07001419 struct ip_tunnel_encap ipencap;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001420
1421 if (dev == ign->fb_tunnel_dev)
1422 return -EINVAL;
1423
Tom Herbert1faf3d92016-05-18 09:06:19 -07001424 if (ip6gre_netlink_encap_parms(data, &ipencap)) {
1425 int err = ip6_tnl_encap_setup(nt, &ipencap);
1426
1427 if (err < 0)
1428 return err;
1429 }
1430
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001431 ip6gre_netlink_parms(data, &p);
1432
1433 t = ip6gre_tunnel_locate(net, &p, 0);
1434
1435 if (t) {
1436 if (t->dev != dev)
1437 return -EEXIST;
1438 } else {
1439 t = nt;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001440 }
1441
Nicolas Dichtel6a61d4d2015-12-03 17:21:50 +01001442 ip6gre_tunnel_unlink(ign, t);
1443 ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
1444 ip6gre_tunnel_link(ign, t);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001445 return 0;
1446}
1447
Nicolas Dichtel54d63f782014-04-14 17:11:38 +02001448static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
1449{
1450 struct net *net = dev_net(dev);
1451 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1452
1453 if (dev != ign->fb_tunnel_dev)
1454 unregister_netdevice_queue(dev, head);
1455}
1456
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001457static size_t ip6gre_get_size(const struct net_device *dev)
1458{
1459 return
1460 /* IFLA_GRE_LINK */
1461 nla_total_size(4) +
1462 /* IFLA_GRE_IFLAGS */
1463 nla_total_size(2) +
1464 /* IFLA_GRE_OFLAGS */
1465 nla_total_size(2) +
1466 /* IFLA_GRE_IKEY */
1467 nla_total_size(4) +
1468 /* IFLA_GRE_OKEY */
1469 nla_total_size(4) +
1470 /* IFLA_GRE_LOCAL */
Nicolas Dichtela3754132012-11-09 05:34:56 +00001471 nla_total_size(sizeof(struct in6_addr)) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001472 /* IFLA_GRE_REMOTE */
Nicolas Dichtela3754132012-11-09 05:34:56 +00001473 nla_total_size(sizeof(struct in6_addr)) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001474 /* IFLA_GRE_TTL */
1475 nla_total_size(1) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001476 /* IFLA_GRE_ENCAP_LIMIT */
1477 nla_total_size(1) +
1478 /* IFLA_GRE_FLOWINFO */
1479 nla_total_size(4) +
1480 /* IFLA_GRE_FLAGS */
1481 nla_total_size(4) +
Tom Herbert1faf3d92016-05-18 09:06:19 -07001482 /* IFLA_GRE_ENCAP_TYPE */
1483 nla_total_size(2) +
1484 /* IFLA_GRE_ENCAP_FLAGS */
1485 nla_total_size(2) +
1486 /* IFLA_GRE_ENCAP_SPORT */
1487 nla_total_size(2) +
1488 /* IFLA_GRE_ENCAP_DPORT */
1489 nla_total_size(2) +
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001490 0;
1491}
1492
1493static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1494{
1495 struct ip6_tnl *t = netdev_priv(dev);
1496 struct __ip6_tnl_parm *p = &t->parms;
1497
1498 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
Tom Herbertf41fe3c2016-05-09 17:12:09 -07001499 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1500 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1501 nla_put_be16(skb, IFLA_GRE_OFLAGS,
1502 gre_tnl_flags_to_gre_flags(p->o_flags)) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001503 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1504 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001505 nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
1506 nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001507 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001508 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
1509 nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
1510 nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
1511 goto nla_put_failure;
Tom Herbert1faf3d92016-05-18 09:06:19 -07001512
1513 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1514 t->encap.type) ||
1515 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1516 t->encap.sport) ||
1517 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1518 t->encap.dport) ||
1519 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
1520 t->encap.flags))
1521 goto nla_put_failure;
1522
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001523 return 0;
1524
1525nla_put_failure:
1526 return -EMSGSIZE;
1527}
1528
1529static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
1530 [IFLA_GRE_LINK] = { .type = NLA_U32 },
1531 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
1532 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
1533 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
1534 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
1535 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
1536 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
1537 [IFLA_GRE_TTL] = { .type = NLA_U8 },
1538 [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
1539 [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
1540 [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
Tom Herbert1faf3d92016-05-18 09:06:19 -07001541 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
1542 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
1543 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
1544 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001545};
1546
1547static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
1548 .kind = "ip6gre",
1549 .maxtype = IFLA_GRE_MAX,
1550 .policy = ip6gre_policy,
1551 .priv_size = sizeof(struct ip6_tnl),
1552 .setup = ip6gre_tunnel_setup,
1553 .validate = ip6gre_tunnel_validate,
1554 .newlink = ip6gre_newlink,
1555 .changelink = ip6gre_changelink,
Nicolas Dichtel54d63f782014-04-14 17:11:38 +02001556 .dellink = ip6gre_dellink,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001557 .get_size = ip6gre_get_size,
1558 .fill_info = ip6gre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001559 .get_link_net = ip6_tnl_get_link_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001560};
1561
1562static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
1563 .kind = "ip6gretap",
1564 .maxtype = IFLA_GRE_MAX,
1565 .policy = ip6gre_policy,
1566 .priv_size = sizeof(struct ip6_tnl),
1567 .setup = ip6gre_tap_setup,
1568 .validate = ip6gre_tap_validate,
1569 .newlink = ip6gre_newlink,
1570 .changelink = ip6gre_changelink,
1571 .get_size = ip6gre_get_size,
1572 .fill_info = ip6gre_fill_info,
Nicolas Dichtel3390e392015-01-20 15:15:43 +01001573 .get_link_net = ip6_tnl_get_link_net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001574};
1575
1576/*
1577 * And now the modules code and kernel interface.
1578 */
1579
1580static int __init ip6gre_init(void)
1581{
1582 int err;
1583
1584 pr_info("GRE over IPv6 tunneling driver\n");
1585
1586 err = register_pernet_device(&ip6gre_net_ops);
1587 if (err < 0)
1588 return err;
1589
1590 err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
1591 if (err < 0) {
1592 pr_info("%s: can't add protocol\n", __func__);
1593 goto add_proto_failed;
1594 }
1595
1596 err = rtnl_link_register(&ip6gre_link_ops);
1597 if (err < 0)
1598 goto rtnl_link_failed;
1599
1600 err = rtnl_link_register(&ip6gre_tap_ops);
1601 if (err < 0)
1602 goto tap_ops_failed;
1603
1604out:
1605 return err;
1606
1607tap_ops_failed:
1608 rtnl_link_unregister(&ip6gre_link_ops);
1609rtnl_link_failed:
1610 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1611add_proto_failed:
1612 unregister_pernet_device(&ip6gre_net_ops);
1613 goto out;
1614}
1615
1616static void __exit ip6gre_fini(void)
1617{
1618 rtnl_link_unregister(&ip6gre_tap_ops);
1619 rtnl_link_unregister(&ip6gre_link_ops);
1620 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1621 unregister_pernet_device(&ip6gre_net_ops);
1622}
1623
1624module_init(ip6gre_init);
1625module_exit(ip6gre_fini);
1626MODULE_LICENSE("GPL");
1627MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
1628MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
1629MODULE_ALIAS_RTNL_LINK("ip6gre");
Nicolas Dichtel5a4ee9a2014-09-24 11:03:00 +02001630MODULE_ALIAS_RTNL_LINK("ip6gretap");
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001631MODULE_ALIAS_NETDEV("ip6gre0");