Blame view

net/ipv6/ip6_gre.c 38.1 KB
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  /*
   *	GRE over IPv6 protocol decoder.
   *
   *	Authors: Dmitry Kozlov (xeb@mail.ru)
   *
   *	This program is free software; you can redistribute it and/or
   *	modify it under the terms of the GNU General Public License
   *	as published by the Free Software Foundation; either version
   *	2 of the License, or (at your option) any later version.
   *
   */
  
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  
  #include <linux/capability.h>
  #include <linux/module.h>
  #include <linux/types.h>
  #include <linux/kernel.h>
  #include <linux/slab.h>
  #include <linux/uaccess.h>
  #include <linux/skbuff.h>
  #include <linux/netdevice.h>
  #include <linux/in.h>
  #include <linux/tcp.h>
  #include <linux/udp.h>
  #include <linux/if_arp.h>
c12b395a4   xeb@mail.ru   gre: Support GRE ...
27
28
29
30
31
32
33
34
35
36
37
38
39
  #include <linux/init.h>
  #include <linux/in6.h>
  #include <linux/inetdevice.h>
  #include <linux/igmp.h>
  #include <linux/netfilter_ipv4.h>
  #include <linux/etherdevice.h>
  #include <linux/if_ether.h>
  #include <linux/hash.h>
  #include <linux/if_tunnel.h>
  #include <linux/ip6_tunnel.h>
  
  #include <net/sock.h>
  #include <net/ip.h>
c54419321   Pravin B Shelar   GRE: Refactor GRE...
40
  #include <net/ip_tunnels.h>
c12b395a4   xeb@mail.ru   gre: Support GRE ...
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  #include <net/icmp.h>
  #include <net/protocol.h>
  #include <net/addrconf.h>
  #include <net/arp.h>
  #include <net/checksum.h>
  #include <net/dsfield.h>
  #include <net/inet_ecn.h>
  #include <net/xfrm.h>
  #include <net/net_namespace.h>
  #include <net/netns/generic.h>
  #include <net/rtnetlink.h>
  
  #include <net/ipv6.h>
  #include <net/ip6_fib.h>
  #include <net/ip6_route.h>
  #include <net/ip6_tunnel.h>
308edfdf1   Tom Herbert   gre6: Cleanup GRE...
57
  #include <net/gre.h>
c12b395a4   xeb@mail.ru   gre: Support GRE ...
58

eccc1bb8d   stephen hemminger   tunnel: drop pack...
59
60
61
  static bool log_ecn_error = true;
  module_param(log_ecn_error, bool, 0644);
  MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
e87a8f24c   Jiri Kosina   net: resolve symb...
62
63
  #define IP6_GRE_HASH_SIZE_SHIFT  5
  #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
c12b395a4   xeb@mail.ru   gre: Support GRE ...
64
65
66
  
  static int ip6gre_net_id __read_mostly;
  struct ip6gre_net {
e87a8f24c   Jiri Kosina   net: resolve symb...
67
  	struct ip6_tnl __rcu *tunnels[4][IP6_GRE_HASH_SIZE];
c12b395a4   xeb@mail.ru   gre: Support GRE ...
68
69
70
71
72
  
  	struct net_device *fb_tunnel_dev;
  };
  
  static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
22f08069e   Nicolas Dichtel   ip6gre: add x-net...
73
  static struct rtnl_link_ops ip6gre_tap_ops __read_mostly;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  static int ip6gre_tunnel_init(struct net_device *dev);
  static void ip6gre_tunnel_setup(struct net_device *dev);
  static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
  static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
  
  /* Tunnel hash table */
  
  /*
     4 hash tables:
  
     3: (remote,local)
     2: (remote,*)
     1: (*,local)
     0: (*,*)
  
     We require exact key match i.e. if a key is present in packet
     it will match only tunnel with the same key; if it is not present,
     it will match only keyless tunnel.
  
     All keysless packets, if not matched configured keyless tunnels
     will match fallback tunnel.
   */
e87a8f24c   Jiri Kosina   net: resolve symb...
96
  #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
c12b395a4   xeb@mail.ru   gre: Support GRE ...
97
98
99
  static u32 HASH_ADDR(const struct in6_addr *addr)
  {
  	u32 hash = ipv6_addr_hash(addr);
e87a8f24c   Jiri Kosina   net: resolve symb...
100
  	return hash_32(hash, IP6_GRE_HASH_SIZE_SHIFT);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
101
102
103
104
105
106
  }
  
  #define tunnels_r_l	tunnels[3]
  #define tunnels_r	tunnels[2]
  #define tunnels_l	tunnels[1]
  #define tunnels_wc	tunnels[0]
c12b395a4   xeb@mail.ru   gre: Support GRE ...
107

c12b395a4   xeb@mail.ru   gre: Support GRE ...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  /* Given src, dst and key, find appropriate for input tunnel. */
  
  static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
  		const struct in6_addr *remote, const struct in6_addr *local,
  		__be32 key, __be16 gre_proto)
  {
  	struct net *net = dev_net(dev);
  	int link = dev->ifindex;
  	unsigned int h0 = HASH_ADDR(remote);
  	unsigned int h1 = HASH_KEY(key);
  	struct ip6_tnl *t, *cand = NULL;
  	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  	int dev_type = (gre_proto == htons(ETH_P_TEB)) ?
  		       ARPHRD_ETHER : ARPHRD_IP6GRE;
  	int score, cand_score = 4;
e086cadc0   Amerigo Wang   net: unify for_ea...
123
  	for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
c12b395a4   xeb@mail.ru   gre: Support GRE ...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  		if (!ipv6_addr_equal(local, &t->parms.laddr) ||
  		    !ipv6_addr_equal(remote, &t->parms.raddr) ||
  		    key != t->parms.i_key ||
  		    !(t->dev->flags & IFF_UP))
  			continue;
  
  		if (t->dev->type != ARPHRD_IP6GRE &&
  		    t->dev->type != dev_type)
  			continue;
  
  		score = 0;
  		if (t->parms.link != link)
  			score |= 1;
  		if (t->dev->type != dev_type)
  			score |= 2;
  		if (score == 0)
  			return t;
  
  		if (score < cand_score) {
  			cand = t;
  			cand_score = score;
  		}
  	}
e086cadc0   Amerigo Wang   net: unify for_ea...
147
  	for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
c12b395a4   xeb@mail.ru   gre: Support GRE ...
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  		if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
  		    key != t->parms.i_key ||
  		    !(t->dev->flags & IFF_UP))
  			continue;
  
  		if (t->dev->type != ARPHRD_IP6GRE &&
  		    t->dev->type != dev_type)
  			continue;
  
  		score = 0;
  		if (t->parms.link != link)
  			score |= 1;
  		if (t->dev->type != dev_type)
  			score |= 2;
  		if (score == 0)
  			return t;
  
  		if (score < cand_score) {
  			cand = t;
  			cand_score = score;
  		}
  	}
e086cadc0   Amerigo Wang   net: unify for_ea...
170
  	for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
c12b395a4   xeb@mail.ru   gre: Support GRE ...
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  		if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
  			  (!ipv6_addr_equal(local, &t->parms.raddr) ||
  				 !ipv6_addr_is_multicast(local))) ||
  		    key != t->parms.i_key ||
  		    !(t->dev->flags & IFF_UP))
  			continue;
  
  		if (t->dev->type != ARPHRD_IP6GRE &&
  		    t->dev->type != dev_type)
  			continue;
  
  		score = 0;
  		if (t->parms.link != link)
  			score |= 1;
  		if (t->dev->type != dev_type)
  			score |= 2;
  		if (score == 0)
  			return t;
  
  		if (score < cand_score) {
  			cand = t;
  			cand_score = score;
  		}
  	}
e086cadc0   Amerigo Wang   net: unify for_ea...
195
  	for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
c12b395a4   xeb@mail.ru   gre: Support GRE ...
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  		if (t->parms.i_key != key ||
  		    !(t->dev->flags & IFF_UP))
  			continue;
  
  		if (t->dev->type != ARPHRD_IP6GRE &&
  		    t->dev->type != dev_type)
  			continue;
  
  		score = 0;
  		if (t->parms.link != link)
  			score |= 1;
  		if (t->dev->type != dev_type)
  			score |= 2;
  		if (score == 0)
  			return t;
  
  		if (score < cand_score) {
  			cand = t;
  			cand_score = score;
  		}
  	}
53b24b8f9   Ian Morris   ipv6: coding styl...
217
  	if (cand)
c12b395a4   xeb@mail.ru   gre: Support GRE ...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
  		return cand;
  
  	dev = ign->fb_tunnel_dev;
  	if (dev->flags & IFF_UP)
  		return netdev_priv(dev);
  
  	return NULL;
  }
  
  static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
  		const struct __ip6_tnl_parm *p)
  {
  	const struct in6_addr *remote = &p->raddr;
  	const struct in6_addr *local = &p->laddr;
  	unsigned int h = HASH_KEY(p->i_key);
  	int prio = 0;
  
  	if (!ipv6_addr_any(local))
  		prio |= 1;
  	if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
  		prio |= 2;
  		h ^= HASH_ADDR(remote);
  	}
  
  	return &ign->tunnels[prio][h];
  }
  
  static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
  		const struct ip6_tnl *t)
  {
  	return __ip6gre_bucket(ign, &t->parms);
  }
  
  static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
  {
  	struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
  
  	rcu_assign_pointer(t->next, rtnl_dereference(*tp));
  	rcu_assign_pointer(*tp, t);
  }
  
  static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
  {
  	struct ip6_tnl __rcu **tp;
  	struct ip6_tnl *iter;
  
  	for (tp = ip6gre_bucket(ign, t);
  	     (iter = rtnl_dereference(*tp)) != NULL;
  	     tp = &iter->next) {
  		if (t == iter) {
  			rcu_assign_pointer(*tp, t->next);
  			break;
  		}
  	}
  }
  
  static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
  					   const struct __ip6_tnl_parm *parms,
  					   int type)
  {
  	const struct in6_addr *remote = &parms->raddr;
  	const struct in6_addr *local = &parms->laddr;
  	__be32 key = parms->i_key;
  	int link = parms->link;
  	struct ip6_tnl *t;
  	struct ip6_tnl __rcu **tp;
  	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  
  	for (tp = __ip6gre_bucket(ign, parms);
  	     (t = rtnl_dereference(*tp)) != NULL;
  	     tp = &t->next)
  		if (ipv6_addr_equal(local, &t->parms.laddr) &&
  		    ipv6_addr_equal(remote, &t->parms.raddr) &&
  		    key == t->parms.i_key &&
  		    link == t->parms.link &&
  		    type == t->dev->type)
  			break;
  
  	return t;
  }
  
  static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
  		const struct __ip6_tnl_parm *parms, int create)
  {
  	struct ip6_tnl *t, *nt;
  	struct net_device *dev;
  	char name[IFNAMSIZ];
  	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  
  	t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
cd0a0bd9b   Steffen Klassert   ip6_gre: Return a...
308
309
  	if (t && create)
  		return NULL;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
310
311
312
313
314
315
316
  	if (t || !create)
  		return t;
  
  	if (parms->name[0])
  		strlcpy(name, parms->name, IFNAMSIZ);
  	else
  		strcpy(name, "ip6gre%d");
c835a6773   Tom Gundersen   net: set name_ass...
317
318
  	dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  			   ip6gre_tunnel_setup);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
319
320
321
322
323
324
325
326
327
328
  	if (!dev)
  		return NULL;
  
  	dev_net_set(dev, net);
  
  	nt = netdev_priv(dev);
  	nt->parms = *parms;
  	dev->rtnl_link_ops = &ip6gre_link_ops;
  
  	nt->dev = dev;
0bd876282   Nicolas Dichtel   ip6tnl: add x-net...
329
  	nt->net = dev_net(dev);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
330
331
332
333
334
335
  	ip6gre_tnl_link_config(nt, 1);
  
  	if (register_netdevice(dev) < 0)
  		goto failed_free;
  
  	/* Can use a lockless transmit, unless we generate output sequences */
b45bd1d78   Tom Herbert   ip6_gre: Use corr...
336
  	if (!(nt->parms.o_flags & TUNNEL_SEQ))
c12b395a4   xeb@mail.ru   gre: Support GRE ...
337
338
339
340
341
342
343
344
345
346
347
348
349
  		dev->features |= NETIF_F_LLTX;
  
  	dev_hold(dev);
  	ip6gre_tunnel_link(ign, nt);
  	return nt;
  
  failed_free:
  	free_netdev(dev);
  	return NULL;
  }
  
  static void ip6gre_tunnel_uninit(struct net_device *dev)
  {
22f08069e   Nicolas Dichtel   ip6gre: add x-net...
350
351
  	struct ip6_tnl *t = netdev_priv(dev);
  	struct ip6gre_net *ign = net_generic(t->net, ip6gre_net_id);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
352

22f08069e   Nicolas Dichtel   ip6gre: add x-net...
353
  	ip6gre_tunnel_unlink(ign, t);
607f725f6   Paolo Abeni   net: replace dst_...
354
  	dst_cache_reset(&t->dst_cache);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
355
356
357
358
359
  	dev_put(dev);
  }
  
  
  static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
ae1768bbb   Eric Dumazet   ip6_gre: fix ip6g...
360
  		       u8 type, u8 code, int offset, __be32 info)
c12b395a4   xeb@mail.ru   gre: Support GRE ...
361
  {
ae1768bbb   Eric Dumazet   ip6_gre: fix ip6g...
362
363
364
  	const struct gre_base_hdr *greh;
  	const struct ipv6hdr *ipv6h;
  	int grehlen = sizeof(*greh);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
365
  	struct ip6_tnl *t;
ae1768bbb   Eric Dumazet   ip6_gre: fix ip6g...
366
  	int key_off = 0;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
367
  	__be16 flags;
ae1768bbb   Eric Dumazet   ip6_gre: fix ip6g...
368
  	__be32 key;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
369

ae1768bbb   Eric Dumazet   ip6_gre: fix ip6g...
370
371
372
373
374
375
376
377
378
379
380
  	if (!pskb_may_pull(skb, offset + grehlen))
  		return;
  	greh = (const struct gre_base_hdr *)(skb->data + offset);
  	flags = greh->flags;
  	if (flags & (GRE_VERSION | GRE_ROUTING))
  		return;
  	if (flags & GRE_CSUM)
  		grehlen += 4;
  	if (flags & GRE_KEY) {
  		key_off = grehlen + offset;
  		grehlen += 4;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
381
  	}
ae1768bbb   Eric Dumazet   ip6_gre: fix ip6g...
382
  	if (!pskb_may_pull(skb, offset + grehlen))
c12b395a4   xeb@mail.ru   gre: Support GRE ...
383
  		return;
b87fb39e3   Eric Dumazet   ipv6: gre: fix ip...
384
  	ipv6h = (const struct ipv6hdr *)skb->data;
ae1768bbb   Eric Dumazet   ip6_gre: fix ip6g...
385
386
  	greh = (const struct gre_base_hdr *)(skb->data + offset);
  	key = key_off ? *(__be32 *)(skb->data + key_off) : 0;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
387

c12b395a4   xeb@mail.ru   gre: Support GRE ...
388
  	t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
ae1768bbb   Eric Dumazet   ip6_gre: fix ip6g...
389
  				 key, greh->protocol);
63159f29b   Ian Morris   ipv6: coding styl...
390
  	if (!t)
0c5794a66   stephen hemminger   gre: remove unnec...
391
  		return;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
392
393
394
395
396
397
  
  	switch (type) {
  		__u32 teli;
  		struct ipv6_tlv_tnl_enc_lim *tel;
  		__u32 mtu;
  	case ICMPV6_DEST_UNREACH:
a46496ce3   Matt Bennett   ip6_gre: Reduce l...
398
399
400
  		net_dbg_ratelimited("%s: Path to destination invalid or inactive!
  ",
  				    t->parms.name);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
401
402
403
  		break;
  	case ICMPV6_TIME_EXCEED:
  		if (code == ICMPV6_EXC_HOPLIMIT) {
a46496ce3   Matt Bennett   ip6_gre: Reduce l...
404
405
406
  			net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!
  ",
  					    t->parms.name);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
407
408
409
410
411
412
  		}
  		break;
  	case ICMPV6_PARAMPROB:
  		teli = 0;
  		if (code == ICMPV6_HDR_FIELD)
  			teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
d1e158e2d   Sabrina Dubroca   ip6_gre: fix endi...
413
  		if (teli && teli == be32_to_cpu(info) - 2) {
c12b395a4   xeb@mail.ru   gre: Support GRE ...
414
415
  			tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  			if (tel->encap_limit == 0) {
a46496ce3   Matt Bennett   ip6_gre: Reduce l...
416
417
418
  				net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!
  ",
  						    t->parms.name);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
419
420
  			}
  		} else {
a46496ce3   Matt Bennett   ip6_gre: Reduce l...
421
422
423
  			net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!
  ",
  					    t->parms.name);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
424
425
426
  		}
  		break;
  	case ICMPV6_PKT_TOOBIG:
d1e158e2d   Sabrina Dubroca   ip6_gre: fix endi...
427
  		mtu = be32_to_cpu(info) - offset;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
428
429
430
431
432
433
434
435
436
437
438
  		if (mtu < IPV6_MIN_MTU)
  			mtu = IPV6_MIN_MTU;
  		t->dev->mtu = mtu;
  		break;
  	}
  
  	if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
  		t->err_count++;
  	else
  		t->err_count = 1;
  	t->err_time = jiffies;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
439
  }
308edfdf1   Tom Herbert   gre6: Cleanup GRE...
440
  static int ip6gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
c12b395a4   xeb@mail.ru   gre: Support GRE ...
441
442
  {
  	const struct ipv6hdr *ipv6h;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
443
  	struct ip6_tnl *tunnel;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
444
445
  
  	ipv6h = ipv6_hdr(skb);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
446
  	tunnel = ip6gre_tunnel_lookup(skb->dev,
308edfdf1   Tom Herbert   gre6: Cleanup GRE...
447
448
  				      &ipv6h->saddr, &ipv6h->daddr, tpi->key,
  				      tpi->proto);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
449
  	if (tunnel) {
308edfdf1   Tom Herbert   gre6: Cleanup GRE...
450
  		ip6_tnl_rcv(tunnel, skb, tpi, NULL, false);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
451

308edfdf1   Tom Herbert   gre6: Cleanup GRE...
452
453
  		return PACKET_RCVD;
  	}
eccc1bb8d   stephen hemminger   tunnel: drop pack...
454

308edfdf1   Tom Herbert   gre6: Cleanup GRE...
455
456
  	return PACKET_REJECT;
  }
eccc1bb8d   stephen hemminger   tunnel: drop pack...
457

308edfdf1   Tom Herbert   gre6: Cleanup GRE...
458
459
460
461
462
  static int gre_rcv(struct sk_buff *skb)
  {
  	struct tnl_ptk_info tpi;
  	bool csum_err = false;
  	int hdr_len;
eccc1bb8d   stephen hemminger   tunnel: drop pack...
463

e582615ad   Eric Dumazet   gre: fix error ha...
464
  	hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IPV6), 0);
f132ae7c4   Jiri Benc   gre: change gre_p...
465
  	if (hdr_len < 0)
308edfdf1   Tom Herbert   gre6: Cleanup GRE...
466
  		goto drop;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
467

308edfdf1   Tom Herbert   gre6: Cleanup GRE...
468
469
  	if (iptunnel_pull_header(skb, hdr_len, tpi.proto, false))
  		goto drop;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
470

308edfdf1   Tom Herbert   gre6: Cleanup GRE...
471
  	if (ip6gre_rcv(skb, &tpi) == PACKET_RCVD)
c12b395a4   xeb@mail.ru   gre: Support GRE ...
472
  		return 0;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
473

308edfdf1   Tom Herbert   gre6: Cleanup GRE...
474
  	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
475
  drop:
c12b395a4   xeb@mail.ru   gre: Support GRE ...
476
477
478
479
480
481
482
483
  	kfree_skb(skb);
  	return 0;
  }
  
  struct ipv6_tel_txoption {
  	struct ipv6_txoptions ops;
  	__u8 dst_opt[8];
  };
b05229f44   Tom Herbert   gre6: Cleanup GRE...
484
  static int gre_handle_offloads(struct sk_buff *skb, bool csum)
c12b395a4   xeb@mail.ru   gre: Support GRE ...
485
  {
b05229f44   Tom Herbert   gre6: Cleanup GRE...
486
487
  	return iptunnel_handle_offloads(skb,
  					csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
488
  }
b05229f44   Tom Herbert   gre6: Cleanup GRE...
489
490
491
492
  static netdev_tx_t __gre6_xmit(struct sk_buff *skb,
  			       struct net_device *dev, __u8 dsfield,
  			       struct flowi6 *fl6, int encap_limit,
  			       __u32 *pmtu, __be16 proto)
c12b395a4   xeb@mail.ru   gre: Support GRE ...
493
  {
c12b395a4   xeb@mail.ru   gre: Support GRE ...
494
  	struct ip6_tnl *tunnel = netdev_priv(dev);
b05229f44   Tom Herbert   gre6: Cleanup GRE...
495
496
  	__be16 protocol = (dev->type == ARPHRD_ETHER) ?
  			  htons(ETH_P_TEB) : proto;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
497
498
499
  
  	if (dev->type == ARPHRD_ETHER)
  		IPCB(skb)->flags = 0;
b05229f44   Tom Herbert   gre6: Cleanup GRE...
500
501
502
  	if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
  		fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
  	else
c12b395a4   xeb@mail.ru   gre: Support GRE ...
503
  		fl6->daddr = tunnel->parms.raddr;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
504

b05229f44   Tom Herbert   gre6: Cleanup GRE...
505
506
  	if (tunnel->parms.o_flags & TUNNEL_SEQ)
  		tunnel->o_seqno++;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
507

b05229f44   Tom Herbert   gre6: Cleanup GRE...
508
509
510
  	/* Push GRE header. */
  	gre_build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
  			 protocol, tunnel->parms.o_key, htonl(tunnel->o_seqno));
c12b395a4   xeb@mail.ru   gre: Support GRE ...
511

b05229f44   Tom Herbert   gre6: Cleanup GRE...
512
513
  	return ip6_tnl_xmit(skb, dev, dsfield, fl6, encap_limit, pmtu,
  			    NEXTHDR_GRE);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
514
515
516
517
518
519
520
521
522
523
524
  }
  
  static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
  {
  	struct ip6_tnl *t = netdev_priv(dev);
  	const struct iphdr  *iph = ip_hdr(skb);
  	int encap_limit = -1;
  	struct flowi6 fl6;
  	__u8 dsfield;
  	__u32 mtu;
  	int err;
5146d1f15   Bernie Harris   tunnel: Clear IPC...
525
  	memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
c12b395a4   xeb@mail.ru   gre: Support GRE ...
526
527
528
529
  	if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  		encap_limit = t->parms.encap_limit;
  
  	memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
c12b395a4   xeb@mail.ru   gre: Support GRE ...
530
531
532
533
534
535
536
537
  
  	dsfield = ipv4_get_dsfield(iph);
  
  	if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  		fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  					  & IPV6_TCLASS_MASK;
  	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  		fl6.flowi6_mark = skb->mark;
b05229f44   Tom Herbert   gre6: Cleanup GRE...
538
539
540
541
542
543
  	err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  	if (err)
  		return -1;
  
  	err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  			  skb->protocol);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
  	if (err != 0) {
  		/* XXX: send ICMP error even if DF is not set. */
  		if (err == -EMSGSIZE)
  			icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  				  htonl(mtu));
  		return -1;
  	}
  
  	return 0;
  }
  
  static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
  {
  	struct ip6_tnl *t = netdev_priv(dev);
  	struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  	int encap_limit = -1;
  	__u16 offset;
  	struct flowi6 fl6;
  	__u8 dsfield;
  	__u32 mtu;
  	int err;
  
  	if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
  		return -1;
  
  	offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  	if (offset > 0) {
  		struct ipv6_tlv_tnl_enc_lim *tel;
  		tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  		if (tel->encap_limit == 0) {
  			icmpv6_send(skb, ICMPV6_PARAMPROB,
  				    ICMPV6_HDR_FIELD, offset + 2);
  			return -1;
  		}
  		encap_limit = tel->encap_limit - 1;
  	} else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  		encap_limit = t->parms.encap_limit;
  
  	memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
c12b395a4   xeb@mail.ru   gre: Support GRE ...
583
584
585
586
587
  
  	dsfield = ipv6_get_dsfield(ipv6h);
  	if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  		fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
3308de2b8   Florent Fourcot   ipv6: add ip6_flo...
588
  		fl6.flowlabel |= ip6_flowlabel(ipv6h);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
589
590
  	if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  		fl6.flowi6_mark = skb->mark;
b05229f44   Tom Herbert   gre6: Cleanup GRE...
591
592
593
594
595
  	if (gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM)))
  		return -1;
  
  	err = __gre6_xmit(skb, dev, dsfield, &fl6, encap_limit,
  			  &mtu, skb->protocol);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
  	if (err != 0) {
  		if (err == -EMSGSIZE)
  			icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  		return -1;
  	}
  
  	return 0;
  }
  
  /**
   * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
   *   @t: the outgoing tunnel device
   *   @hdr: IPv6 header from the incoming packet
   *
   * Description:
   *   Avoid trivial tunneling loop by checking that tunnel exit-point
   *   doesn't match source of incoming packet.
   *
   * Return:
   *   1 if conflict,
   *   0 else
   **/
  
  static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
  	const struct ipv6hdr *hdr)
  {
  	return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  }
  
  static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
  {
  	struct ip6_tnl *t = netdev_priv(dev);
  	int encap_limit = -1;
  	struct flowi6 fl6;
  	__u32 mtu;
  	int err;
  
  	if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  		encap_limit = t->parms.encap_limit;
  
  	memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
c12b395a4   xeb@mail.ru   gre: Support GRE ...
637

b05229f44   Tom Herbert   gre6: Cleanup GRE...
638
639
640
641
642
  	err = gre_handle_offloads(skb, !!(t->parms.o_flags & TUNNEL_CSUM));
  	if (err)
  		return err;
  
  	err = __gre6_xmit(skb, dev, 0, &fl6, encap_limit, &mtu, skb->protocol);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
643
644
645
646
647
648
649
650
651
652
  
  	return err;
  }
  
  static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
  	struct net_device *dev)
  {
  	struct ip6_tnl *t = netdev_priv(dev);
  	struct net_device_stats *stats = &t->dev->stats;
  	int ret;
d50051407   Steffen Klassert   ipv6: Allow sendi...
653
  	if (!ip6_tnl_xmit_ctl(t, &t->parms.laddr, &t->parms.raddr))
41ab3e31b   Tommi Rantala   ipv6/ip6_gre: fix...
654
  		goto tx_err;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
  
  	switch (skb->protocol) {
  	case htons(ETH_P_IP):
  		ret = ip6gre_xmit_ipv4(skb, dev);
  		break;
  	case htons(ETH_P_IPV6):
  		ret = ip6gre_xmit_ipv6(skb, dev);
  		break;
  	default:
  		ret = ip6gre_xmit_other(skb, dev);
  		break;
  	}
  
  	if (ret < 0)
  		goto tx_err;
  
  	return NETDEV_TX_OK;
  
  tx_err:
  	stats->tx_errors++;
  	stats->tx_dropped++;
  	kfree_skb(skb);
  	return NETDEV_TX_OK;
  }
  
  static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
  {
  	struct net_device *dev = t->dev;
  	struct __ip6_tnl_parm *p = &t->parms;
  	struct flowi6 *fl6 = &t->fl.u.ip6;
db2ec95d1   Tom Herbert   ip6_gre: Fix MTU ...
685
  	int t_hlen;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
686
687
688
689
690
691
692
693
694
695
696
  
  	if (dev->type != ARPHRD_ETHER) {
  		memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  		memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  	}
  
  	/* Set up flowi template */
  	fl6->saddr = p->laddr;
  	fl6->daddr = p->raddr;
  	fl6->flowi6_oif = p->link;
  	fl6->flowlabel = 0;
252f3f5a1   Haishuang Yan   ip6_gre: Set flow...
697
  	fl6->flowi6_proto = IPPROTO_GRE;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
698
699
700
701
702
703
704
705
706
707
708
709
710
711
  
  	if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  		fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  	if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  		fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  
  	p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  	p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  
  	if (p->flags&IP6_TNL_F_CAP_XMIT &&
  			p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
  		dev->flags |= IFF_POINTOPOINT;
  	else
  		dev->flags &= ~IFF_POINTOPOINT;
db2ec95d1   Tom Herbert   ip6_gre: Fix MTU ...
712
  	t->tun_hlen = gre_calc_hlen(t->parms.o_flags);
1faf3d9f7   Tom Herbert   ip6_gre: Add supp...
713
  	t->hlen = t->encap_hlen + t->tun_hlen;
db2ec95d1   Tom Herbert   ip6_gre: Fix MTU ...
714
715
  
  	t_hlen = t->hlen + sizeof(struct ipv6hdr);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
716
717
718
719
  
  	if (p->flags & IP6_TNL_F_CAP_XMIT) {
  		int strict = (ipv6_addr_type(&p->raddr) &
  			      (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
22f08069e   Nicolas Dichtel   ip6gre: add x-net...
720
  		struct rt6_info *rt = rt6_lookup(t->net,
c12b395a4   xeb@mail.ru   gre: Support GRE ...
721
722
  						 &p->raddr, &p->laddr,
  						 p->link, strict);
63159f29b   Ian Morris   ipv6: coding styl...
723
  		if (!rt)
c12b395a4   xeb@mail.ru   gre: Support GRE ...
724
725
726
  			return;
  
  		if (rt->dst.dev) {
db2ec95d1   Tom Herbert   ip6_gre: Fix MTU ...
727
728
  			dev->hard_header_len = rt->dst.dev->hard_header_len +
  					       t_hlen;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
729
730
  
  			if (set_mtu) {
db2ec95d1   Tom Herbert   ip6_gre: Fix MTU ...
731
  				dev->mtu = rt->dst.dev->mtu - t_hlen;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
732
733
  				if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  					dev->mtu -= 8;
a9e242ca4   Alexander Duyck   ip6gretap: Fix MT...
734
735
  				if (dev->type == ARPHRD_ETHER)
  					dev->mtu -= ETH_HLEN;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
736
737
738
739
740
  
  				if (dev->mtu < IPV6_MIN_MTU)
  					dev->mtu = IPV6_MIN_MTU;
  			}
  		}
94e187c01   Amerigo Wang   ipv6: introduce i...
741
  		ip6_rt_put(rt);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
742
  	}
c12b395a4   xeb@mail.ru   gre: Support GRE ...
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
  }
  
  static int ip6gre_tnl_change(struct ip6_tnl *t,
  	const struct __ip6_tnl_parm *p, int set_mtu)
  {
  	t->parms.laddr = p->laddr;
  	t->parms.raddr = p->raddr;
  	t->parms.flags = p->flags;
  	t->parms.hop_limit = p->hop_limit;
  	t->parms.encap_limit = p->encap_limit;
  	t->parms.flowinfo = p->flowinfo;
  	t->parms.link = p->link;
  	t->parms.proto = p->proto;
  	t->parms.i_key = p->i_key;
  	t->parms.o_key = p->o_key;
  	t->parms.i_flags = p->i_flags;
  	t->parms.o_flags = p->o_flags;
607f725f6   Paolo Abeni   net: replace dst_...
760
  	dst_cache_reset(&t->dst_cache);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
  	ip6gre_tnl_link_config(t, set_mtu);
  	return 0;
  }
  
  static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
  	const struct ip6_tnl_parm2 *u)
  {
  	p->laddr = u->laddr;
  	p->raddr = u->raddr;
  	p->flags = u->flags;
  	p->hop_limit = u->hop_limit;
  	p->encap_limit = u->encap_limit;
  	p->flowinfo = u->flowinfo;
  	p->link = u->link;
  	p->i_key = u->i_key;
  	p->o_key = u->o_key;
f41fe3c2a   Tom Herbert   gre6: Fix flag tr...
777
778
  	p->i_flags = gre_flags_to_tnl_flags(u->i_flags);
  	p->o_flags = gre_flags_to_tnl_flags(u->o_flags);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
  	memcpy(p->name, u->name, sizeof(u->name));
  }
  
  static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
  	const struct __ip6_tnl_parm *p)
  {
  	u->proto = IPPROTO_GRE;
  	u->laddr = p->laddr;
  	u->raddr = p->raddr;
  	u->flags = p->flags;
  	u->hop_limit = p->hop_limit;
  	u->encap_limit = p->encap_limit;
  	u->flowinfo = p->flowinfo;
  	u->link = p->link;
  	u->i_key = p->i_key;
  	u->o_key = p->o_key;
f41fe3c2a   Tom Herbert   gre6: Fix flag tr...
795
796
  	u->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
  	u->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
797
798
799
800
801
802
803
804
805
  	memcpy(u->name, p->name, sizeof(u->name));
  }
  
  static int ip6gre_tunnel_ioctl(struct net_device *dev,
  	struct ifreq *ifr, int cmd)
  {
  	int err = 0;
  	struct ip6_tnl_parm2 p;
  	struct __ip6_tnl_parm p1;
22f08069e   Nicolas Dichtel   ip6gre: add x-net...
806
807
  	struct ip6_tnl *t = netdev_priv(dev);
  	struct net *net = t->net;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
808
  	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
308edfdf1   Tom Herbert   gre6: Cleanup GRE...
809
  	memset(&p1, 0, sizeof(p1));
c12b395a4   xeb@mail.ru   gre: Support GRE ...
810
811
  	switch (cmd) {
  	case SIOCGETTUNNEL:
c12b395a4   xeb@mail.ru   gre: Support GRE ...
812
813
814
815
816
817
818
  		if (dev == ign->fb_tunnel_dev) {
  			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  				err = -EFAULT;
  				break;
  			}
  			ip6gre_tnl_parm_from_user(&p1, &p);
  			t = ip6gre_tunnel_locate(net, &p1, 0);
63159f29b   Ian Morris   ipv6: coding styl...
819
  			if (!t)
22f08069e   Nicolas Dichtel   ip6gre: add x-net...
820
  				t = netdev_priv(dev);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
821
  		}
5dbd50684   Amerigo Wang   ipv6,gre: do not ...
822
  		memset(&p, 0, sizeof(p));
c12b395a4   xeb@mail.ru   gre: Support GRE ...
823
824
825
826
827
828
829
830
  		ip6gre_tnl_parm_to_user(&p, &t->parms);
  		if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  			err = -EFAULT;
  		break;
  
  	case SIOCADDTUNNEL:
  	case SIOCCHGTUNNEL:
  		err = -EPERM;
af31f412c   Eric W. Biederman   net: Allow userns...
831
  		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
c12b395a4   xeb@mail.ru   gre: Support GRE ...
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
  			goto done;
  
  		err = -EFAULT;
  		if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  			goto done;
  
  		err = -EINVAL;
  		if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
  			goto done;
  
  		if (!(p.i_flags&GRE_KEY))
  			p.i_key = 0;
  		if (!(p.o_flags&GRE_KEY))
  			p.o_key = 0;
  
  		ip6gre_tnl_parm_from_user(&p1, &p);
  		t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
  
  		if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
53b24b8f9   Ian Morris   ipv6: coding styl...
851
  			if (t) {
c12b395a4   xeb@mail.ru   gre: Support GRE ...
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
  				if (t->dev != dev) {
  					err = -EEXIST;
  					break;
  				}
  			} else {
  				t = netdev_priv(dev);
  
  				ip6gre_tunnel_unlink(ign, t);
  				synchronize_net();
  				ip6gre_tnl_change(t, &p1, 1);
  				ip6gre_tunnel_link(ign, t);
  				netdev_state_change(dev);
  			}
  		}
  
  		if (t) {
  			err = 0;
5dbd50684   Amerigo Wang   ipv6,gre: do not ...
869
  			memset(&p, 0, sizeof(p));
c12b395a4   xeb@mail.ru   gre: Support GRE ...
870
871
872
873
874
875
876
877
878
  			ip6gre_tnl_parm_to_user(&p, &t->parms);
  			if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  				err = -EFAULT;
  		} else
  			err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  		break;
  
  	case SIOCDELTUNNEL:
  		err = -EPERM;
af31f412c   Eric W. Biederman   net: Allow userns...
879
  		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
c12b395a4   xeb@mail.ru   gre: Support GRE ...
880
881
882
883
884
885
886
887
888
  			goto done;
  
  		if (dev == ign->fb_tunnel_dev) {
  			err = -EFAULT;
  			if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  				goto done;
  			err = -ENOENT;
  			ip6gre_tnl_parm_from_user(&p1, &p);
  			t = ip6gre_tunnel_locate(net, &p1, 0);
63159f29b   Ian Morris   ipv6: coding styl...
889
  			if (!t)
c12b395a4   xeb@mail.ru   gre: Support GRE ...
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
  				goto done;
  			err = -EPERM;
  			if (t == netdev_priv(ign->fb_tunnel_dev))
  				goto done;
  			dev = t->dev;
  		}
  		unregister_netdevice(dev);
  		err = 0;
  		break;
  
  	default:
  		err = -EINVAL;
  	}
  
  done:
  	return err;
  }
c12b395a4   xeb@mail.ru   gre: Support GRE ...
907
908
909
910
911
912
913
  static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
  			unsigned short type,
  			const void *daddr, const void *saddr, unsigned int len)
  {
  	struct ip6_tnl *t = netdev_priv(dev);
  	struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen);
  	__be16 *p = (__be16 *)(ipv6h+1);
cb1ce2ef3   Tom Herbert   ipv6: Implement a...
914
915
  	ip6_flow_hdr(ipv6h, 0,
  		     ip6_make_flowlabel(dev_net(dev), skb,
42240901f   Tom Herbert   ipv6: Implement d...
916
  					t->fl.u.ip6.flowlabel, true,
67800f9b1   Tom Herbert   ipv6: Call skb_ge...
917
  					&t->fl.u.ip6));
c12b395a4   xeb@mail.ru   gre: Support GRE ...
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
  	ipv6h->hop_limit = t->parms.hop_limit;
  	ipv6h->nexthdr = NEXTHDR_GRE;
  	ipv6h->saddr = t->parms.laddr;
  	ipv6h->daddr = t->parms.raddr;
  
  	p[0]		= t->parms.o_flags;
  	p[1]		= htons(type);
  
  	/*
  	 *	Set the source hardware address.
  	 */
  
  	if (saddr)
  		memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
  	if (daddr)
  		memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
  	if (!ipv6_addr_any(&ipv6h->daddr))
  		return t->hlen;
  
  	return -t->hlen;
  }
c12b395a4   xeb@mail.ru   gre: Support GRE ...
939
940
  static const struct header_ops ip6gre_header_ops = {
  	.create	= ip6gre_header,
c12b395a4   xeb@mail.ru   gre: Support GRE ...
941
942
943
944
945
946
947
  };
  
  static const struct net_device_ops ip6gre_netdev_ops = {
  	.ndo_init		= ip6gre_tunnel_init,
  	.ndo_uninit		= ip6gre_tunnel_uninit,
  	.ndo_start_xmit		= ip6gre_tunnel_xmit,
  	.ndo_do_ioctl		= ip6gre_tunnel_ioctl,
b05229f44   Tom Herbert   gre6: Cleanup GRE...
948
  	.ndo_change_mtu		= ip6_tnl_change_mtu,
f61dd388a   Pravin B Shelar   Tunneling: use IP...
949
  	.ndo_get_stats64	= ip_tunnel_get_stats64,
ecf2c06a8   Nicolas Dichtel   ip6tnl,gre6,vti6:...
950
  	.ndo_get_iflink		= ip6_tnl_get_iflink,
c12b395a4   xeb@mail.ru   gre: Support GRE ...
951
952
953
954
  };
  
  static void ip6gre_dev_free(struct net_device *dev)
  {
cdf3464e6   Martin KaFai Lau   ipv6: Fix dst_ent...
955
  	struct ip6_tnl *t = netdev_priv(dev);
607f725f6   Paolo Abeni   net: replace dst_...
956
  	dst_cache_destroy(&t->dst_cache);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
957
958
959
960
961
962
  	free_percpu(dev->tstats);
  	free_netdev(dev);
  }
  
  static void ip6gre_tunnel_setup(struct net_device *dev)
  {
c12b395a4   xeb@mail.ru   gre: Support GRE ...
963
964
965
966
  	dev->netdev_ops = &ip6gre_netdev_ops;
  	dev->destructor = ip6gre_dev_free;
  
  	dev->type = ARPHRD_IP6GRE;
b05229f44   Tom Herbert   gre6: Cleanup GRE...
967

c12b395a4   xeb@mail.ru   gre: Support GRE ...
968
  	dev->flags |= IFF_NOARP;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
969
  	dev->addr_len = sizeof(struct in6_addr);
028758788   Eric Dumazet   net: better IFF_X...
970
  	netif_keep_dst(dev);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
971
  }
a3c119d39   Martin KaFai Lau   ipv6: Refactor co...
972
  static int ip6gre_tunnel_init_common(struct net_device *dev)
c12b395a4   xeb@mail.ru   gre: Support GRE ...
973
974
  {
  	struct ip6_tnl *tunnel;
cdf3464e6   Martin KaFai Lau   ipv6: Fix dst_ent...
975
  	int ret;
b05229f44   Tom Herbert   gre6: Cleanup GRE...
976
  	int t_hlen;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
977
978
979
980
  
  	tunnel = netdev_priv(dev);
  
  	tunnel->dev = dev;
0bd876282   Nicolas Dichtel   ip6tnl: add x-net...
981
  	tunnel->net = dev_net(dev);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
982
  	strcpy(tunnel->parms.name, dev->name);
a3c119d39   Martin KaFai Lau   ipv6: Refactor co...
983
984
985
  	dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  	if (!dev->tstats)
  		return -ENOMEM;
607f725f6   Paolo Abeni   net: replace dst_...
986
  	ret = dst_cache_init(&tunnel->dst_cache, GFP_KERNEL);
cdf3464e6   Martin KaFai Lau   ipv6: Fix dst_ent...
987
988
989
990
991
  	if (ret) {
  		free_percpu(dev->tstats);
  		dev->tstats = NULL;
  		return ret;
  	}
b05229f44   Tom Herbert   gre6: Cleanup GRE...
992
  	tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
1faf3d9f7   Tom Herbert   ip6_gre: Add supp...
993
  	tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
db2ec95d1   Tom Herbert   ip6_gre: Fix MTU ...
994
  	t_hlen = tunnel->hlen + sizeof(struct ipv6hdr);
b05229f44   Tom Herbert   gre6: Cleanup GRE...
995

db2ec95d1   Tom Herbert   ip6_gre: Fix MTU ...
996
997
  	dev->hard_header_len = LL_MAX_HEADER + t_hlen;
  	dev->mtu = ETH_DATA_LEN - t_hlen;
1b227e536   Haishuang Yan   ip6_gre: Fix MTU ...
998
999
  	if (dev->type == ARPHRD_ETHER)
  		dev->mtu -= ETH_HLEN;
b05229f44   Tom Herbert   gre6: Cleanup GRE...
1000
1001
  	if (!(tunnel->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  		dev->mtu -= 8;
a3c119d39   Martin KaFai Lau   ipv6: Refactor co...
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
  	return 0;
  }
  
  static int ip6gre_tunnel_init(struct net_device *dev)
  {
  	struct ip6_tnl *tunnel;
  	int ret;
  
  	ret = ip6gre_tunnel_init_common(dev);
  	if (ret)
  		return ret;
  
  	tunnel = netdev_priv(dev);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1015
1016
1017
1018
1019
  	memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
  	memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
  
  	if (ipv6_addr_any(&tunnel->parms.raddr))
  		dev->header_ops = &ip6gre_header_ops;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1020
1021
1022
1023
1024
1025
1026
1027
  	return 0;
  }
  
  static void ip6gre_fb_tunnel_init(struct net_device *dev)
  {
  	struct ip6_tnl *tunnel = netdev_priv(dev);
  
  	tunnel->dev = dev;
0bd876282   Nicolas Dichtel   ip6tnl: add x-net...
1028
  	tunnel->net = dev_net(dev);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1029
1030
1031
1032
1033
1034
1035
1036
1037
  	strcpy(tunnel->parms.name, dev->name);
  
  	tunnel->hlen		= sizeof(struct ipv6hdr) + 4;
  
  	dev_hold(dev);
  }
  
  
  static struct inet6_protocol ip6gre_protocol __read_mostly = {
308edfdf1   Tom Herbert   gre6: Cleanup GRE...
1038
  	.handler     = gre_rcv,
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1039
1040
1041
  	.err_handler = ip6gre_err,
  	.flags       = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  };
22f08069e   Nicolas Dichtel   ip6gre: add x-net...
1042
  static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1043
  {
22f08069e   Nicolas Dichtel   ip6gre: add x-net...
1044
1045
  	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  	struct net_device *dev, *aux;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1046
  	int prio;
22f08069e   Nicolas Dichtel   ip6gre: add x-net...
1047
1048
1049
1050
  	for_each_netdev_safe(net, dev, aux)
  		if (dev->rtnl_link_ops == &ip6gre_link_ops ||
  		    dev->rtnl_link_ops == &ip6gre_tap_ops)
  			unregister_netdevice_queue(dev, head);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1051
1052
  	for (prio = 0; prio < 4; prio++) {
  		int h;
e87a8f24c   Jiri Kosina   net: resolve symb...
1053
  		for (h = 0; h < IP6_GRE_HASH_SIZE; h++) {
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1054
1055
1056
  			struct ip6_tnl *t;
  
  			t = rtnl_dereference(ign->tunnels[prio][h]);
53b24b8f9   Ian Morris   ipv6: coding styl...
1057
  			while (t) {
22f08069e   Nicolas Dichtel   ip6gre: add x-net...
1058
1059
1060
1061
1062
1063
  				/* If dev is in the same netns, it has already
  				 * been added to the list by the previous loop.
  				 */
  				if (!net_eq(dev_net(t->dev), net))
  					unregister_netdevice_queue(t->dev,
  								   head);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
  				t = rtnl_dereference(t->next);
  			}
  		}
  	}
  }
  
  static int __net_init ip6gre_init_net(struct net *net)
  {
  	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  	int err;
  
  	ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
c835a6773   Tom Gundersen   net: set name_ass...
1076
1077
  					  NET_NAME_UNKNOWN,
  					  ip6gre_tunnel_setup);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1078
1079
1080
1081
1082
  	if (!ign->fb_tunnel_dev) {
  		err = -ENOMEM;
  		goto err_alloc_dev;
  	}
  	dev_net_set(ign->fb_tunnel_dev, net);
22f08069e   Nicolas Dichtel   ip6gre: add x-net...
1083
1084
1085
1086
  	/* FB netdevice is special: we have one, and only one per netns.
  	 * Allowing to move it to another netns is clearly unsafe.
  	 */
  	ign->fb_tunnel_dev->features |= NETIF_F_NETNS_LOCAL;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
  
  	ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
  	ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
  
  	err = register_netdev(ign->fb_tunnel_dev);
  	if (err)
  		goto err_reg_dev;
  
  	rcu_assign_pointer(ign->tunnels_wc[0],
  			   netdev_priv(ign->fb_tunnel_dev));
  	return 0;
  
  err_reg_dev:
  	ip6gre_dev_free(ign->fb_tunnel_dev);
  err_alloc_dev:
  	return err;
  }
  
  static void __net_exit ip6gre_exit_net(struct net *net)
  {
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1107
  	LIST_HEAD(list);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1108
  	rtnl_lock();
22f08069e   Nicolas Dichtel   ip6gre: add x-net...
1109
  	ip6gre_destroy_tunnels(net, &list);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
  	unregister_netdevice_many(&list);
  	rtnl_unlock();
  }
  
  static struct pernet_operations ip6gre_net_ops = {
  	.init = ip6gre_init_net,
  	.exit = ip6gre_exit_net,
  	.id   = &ip6gre_net_id,
  	.size = sizeof(struct ip6gre_net),
  };
  
  static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
  {
  	__be16 flags;
  
  	if (!data)
  		return 0;
  
  	flags = 0;
  	if (data[IFLA_GRE_IFLAGS])
  		flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
  	if (data[IFLA_GRE_OFLAGS])
  		flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
  	if (flags & (GRE_VERSION|GRE_ROUTING))
  		return -EINVAL;
  
  	return 0;
  }
  
  static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
  {
  	struct in6_addr daddr;
  
  	if (tb[IFLA_ADDRESS]) {
  		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  			return -EINVAL;
  		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  			return -EADDRNOTAVAIL;
  	}
  
  	if (!data)
  		goto out;
  
  	if (data[IFLA_GRE_REMOTE]) {
67b61f6c1   Jiri Benc   netlink: implemen...
1154
  		daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
  		if (ipv6_addr_any(&daddr))
  			return -EINVAL;
  	}
  
  out:
  	return ip6gre_tunnel_validate(tb, data);
  }
  
  
  static void ip6gre_netlink_parms(struct nlattr *data[],
  				struct __ip6_tnl_parm *parms)
  {
  	memset(parms, 0, sizeof(*parms));
  
  	if (!data)
  		return;
  
  	if (data[IFLA_GRE_LINK])
  		parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
  
  	if (data[IFLA_GRE_IFLAGS])
f41fe3c2a   Tom Herbert   gre6: Fix flag tr...
1176
1177
  		parms->i_flags = gre_flags_to_tnl_flags(
  				nla_get_be16(data[IFLA_GRE_IFLAGS]));
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1178
1179
  
  	if (data[IFLA_GRE_OFLAGS])
f41fe3c2a   Tom Herbert   gre6: Fix flag tr...
1180
1181
  		parms->o_flags = gre_flags_to_tnl_flags(
  				nla_get_be16(data[IFLA_GRE_OFLAGS]));
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1182
1183
1184
1185
1186
1187
1188
1189
  
  	if (data[IFLA_GRE_IKEY])
  		parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
  
  	if (data[IFLA_GRE_OKEY])
  		parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
  
  	if (data[IFLA_GRE_LOCAL])
67b61f6c1   Jiri Benc   netlink: implemen...
1190
  		parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1191
1192
  
  	if (data[IFLA_GRE_REMOTE])
67b61f6c1   Jiri Benc   netlink: implemen...
1193
  		parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1194
1195
1196
1197
1198
1199
1200
1201
  
  	if (data[IFLA_GRE_TTL])
  		parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
  
  	if (data[IFLA_GRE_ENCAP_LIMIT])
  		parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
  
  	if (data[IFLA_GRE_FLOWINFO])
c2675de44   Lance Richardson   gre: use nla_get_...
1202
  		parms->flowinfo = nla_get_be32(data[IFLA_GRE_FLOWINFO]);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1203
1204
1205
1206
1207
1208
1209
1210
  
  	if (data[IFLA_GRE_FLAGS])
  		parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
  }
  
  static int ip6gre_tap_init(struct net_device *dev)
  {
  	struct ip6_tnl *tunnel;
a3c119d39   Martin KaFai Lau   ipv6: Refactor co...
1211
  	int ret;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1212

a3c119d39   Martin KaFai Lau   ipv6: Refactor co...
1213
1214
1215
  	ret = ip6gre_tunnel_init_common(dev);
  	if (ret)
  		return ret;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1216

0a46baaf6   Shweta Choudaha   ip6gre: Allow liv...
1217
  	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
a3c119d39   Martin KaFai Lau   ipv6: Refactor co...
1218
  	tunnel = netdev_priv(dev);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1219
1220
  
  	ip6gre_tnl_link_config(tunnel, 1);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1221
1222
1223
1224
1225
1226
1227
1228
1229
  	return 0;
  }
  
  static const struct net_device_ops ip6gre_tap_netdev_ops = {
  	.ndo_init = ip6gre_tap_init,
  	.ndo_uninit = ip6gre_tunnel_uninit,
  	.ndo_start_xmit = ip6gre_tunnel_xmit,
  	.ndo_set_mac_address = eth_mac_addr,
  	.ndo_validate_addr = eth_validate_addr,
b05229f44   Tom Herbert   gre6: Cleanup GRE...
1230
  	.ndo_change_mtu = ip6_tnl_change_mtu,
f61dd388a   Pravin B Shelar   Tunneling: use IP...
1231
  	.ndo_get_stats64 = ip_tunnel_get_stats64,
ecf2c06a8   Nicolas Dichtel   ip6tnl,gre6,vti6:...
1232
  	.ndo_get_iflink = ip6_tnl_get_iflink,
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1233
  };
ac4eb009e   Alexander Duyck   ip6gre: Add suppo...
1234
1235
1236
1237
  #define GRE6_FEATURES (NETIF_F_SG |		\
  		       NETIF_F_FRAGLIST |	\
  		       NETIF_F_HIGHDMA |		\
  		       NETIF_F_HW_CSUM)
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1238
1239
1240
1241
1242
1243
1244
  static void ip6gre_tap_setup(struct net_device *dev)
  {
  
  	ether_setup(dev);
  
  	dev->netdev_ops = &ip6gre_tap_netdev_ops;
  	dev->destructor = ip6gre_dev_free;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1245
  	dev->features |= NETIF_F_NETNS_LOCAL;
d13b161c2   Jiri Benc   gre: clear IFF_TX...
1246
  	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
0a46baaf6   Shweta Choudaha   ip6gre: Allow liv...
1247
  	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1248
  }
1faf3d9f7   Tom Herbert   ip6_gre: Add supp...
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
  static bool ip6gre_netlink_encap_parms(struct nlattr *data[],
  				       struct ip_tunnel_encap *ipencap)
  {
  	bool ret = false;
  
  	memset(ipencap, 0, sizeof(*ipencap));
  
  	if (!data)
  		return ret;
  
  	if (data[IFLA_GRE_ENCAP_TYPE]) {
  		ret = true;
  		ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
  	}
  
  	if (data[IFLA_GRE_ENCAP_FLAGS]) {
  		ret = true;
  		ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
  	}
  
  	if (data[IFLA_GRE_ENCAP_SPORT]) {
  		ret = true;
  		ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
  	}
  
  	if (data[IFLA_GRE_ENCAP_DPORT]) {
  		ret = true;
  		ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
  	}
  
  	return ret;
  }
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1281
1282
1283
1284
1285
1286
  static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
  	struct nlattr *tb[], struct nlattr *data[])
  {
  	struct ip6_tnl *nt;
  	struct net *net = dev_net(dev);
  	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1faf3d9f7   Tom Herbert   ip6_gre: Add supp...
1287
  	struct ip_tunnel_encap ipencap;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1288
1289
1290
  	int err;
  
  	nt = netdev_priv(dev);
1faf3d9f7   Tom Herbert   ip6_gre: Add supp...
1291
1292
1293
1294
1295
1296
1297
  
  	if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  		int err = ip6_tnl_encap_setup(nt, &ipencap);
  
  		if (err < 0)
  			return err;
  	}
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1298
1299
1300
1301
1302
1303
1304
1305
1306
  	ip6gre_netlink_parms(data, &nt->parms);
  
  	if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
  		return -EEXIST;
  
  	if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
  		eth_hw_addr_random(dev);
  
  	nt->dev = dev;
0bd876282   Nicolas Dichtel   ip6tnl: add x-net...
1307
  	nt->net = dev_net(dev);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1308
  	ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
ac4eb009e   Alexander Duyck   ip6gre: Add suppo...
1309
1310
  	dev->features		|= GRE6_FEATURES;
  	dev->hw_features	|= GRE6_FEATURES;
b45bd1d78   Tom Herbert   ip6_gre: Use corr...
1311
  	if (!(nt->parms.o_flags & TUNNEL_SEQ)) {
6a5536810   Alexander Duyck   ip6_gre: Do not a...
1312
1313
1314
  		/* TCP offload with GRE SEQ is not supported, nor
  		 * can we support 2 levels of outer headers requiring
  		 * an update.
3a80e1fac   Alexander Duyck   ip6gre: Add suppo...
1315
  		 */
6a5536810   Alexander Duyck   ip6_gre: Do not a...
1316
1317
1318
1319
1320
  		if (!(nt->parms.o_flags & TUNNEL_CSUM) ||
  		    (nt->encap.type == TUNNEL_ENCAP_NONE)) {
  			dev->features    |= NETIF_F_GSO_SOFTWARE;
  			dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  		}
3a80e1fac   Alexander Duyck   ip6gre: Add suppo...
1321
1322
1323
1324
  
  		/* Can use a lockless transmit, unless we generate
  		 * output sequences
  		 */
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1325
  		dev->features |= NETIF_F_LLTX;
3a80e1fac   Alexander Duyck   ip6gre: Add suppo...
1326
  	}
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
  
  	err = register_netdevice(dev);
  	if (err)
  		goto out;
  
  	dev_hold(dev);
  	ip6gre_tunnel_link(ign, nt);
  
  out:
  	return err;
  }
  
  static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
  			    struct nlattr *data[])
  {
22f08069e   Nicolas Dichtel   ip6gre: add x-net...
1342
1343
  	struct ip6_tnl *t, *nt = netdev_priv(dev);
  	struct net *net = nt->net;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1344
1345
  	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  	struct __ip6_tnl_parm p;
1faf3d9f7   Tom Herbert   ip6_gre: Add supp...
1346
  	struct ip_tunnel_encap ipencap;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1347
1348
1349
  
  	if (dev == ign->fb_tunnel_dev)
  		return -EINVAL;
1faf3d9f7   Tom Herbert   ip6_gre: Add supp...
1350
1351
1352
1353
1354
1355
  	if (ip6gre_netlink_encap_parms(data, &ipencap)) {
  		int err = ip6_tnl_encap_setup(nt, &ipencap);
  
  		if (err < 0)
  			return err;
  	}
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1356
1357
1358
1359
1360
1361
1362
1363
1364
  	ip6gre_netlink_parms(data, &p);
  
  	t = ip6gre_tunnel_locate(net, &p, 0);
  
  	if (t) {
  		if (t->dev != dev)
  			return -EEXIST;
  	} else {
  		t = nt;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1365
  	}
6a61d4dbf   Nicolas Dichtel   gre6: allow to up...
1366
1367
1368
  	ip6gre_tunnel_unlink(ign, t);
  	ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
  	ip6gre_tunnel_link(ign, t);
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1369
1370
  	return 0;
  }
54d63f787   Nicolas Dichtel   ip6_gre: don't al...
1371
1372
1373
1374
1375
1376
1377
1378
  static void ip6gre_dellink(struct net_device *dev, struct list_head *head)
  {
  	struct net *net = dev_net(dev);
  	struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
  
  	if (dev != ign->fb_tunnel_dev)
  		unregister_netdevice_queue(dev, head);
  }
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
  static size_t ip6gre_get_size(const struct net_device *dev)
  {
  	return
  		/* IFLA_GRE_LINK */
  		nla_total_size(4) +
  		/* IFLA_GRE_IFLAGS */
  		nla_total_size(2) +
  		/* IFLA_GRE_OFLAGS */
  		nla_total_size(2) +
  		/* IFLA_GRE_IKEY */
  		nla_total_size(4) +
  		/* IFLA_GRE_OKEY */
  		nla_total_size(4) +
  		/* IFLA_GRE_LOCAL */
a37541331   Nicolas Dichtel   gre6: fix rtnl du...
1393
  		nla_total_size(sizeof(struct in6_addr)) +
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1394
  		/* IFLA_GRE_REMOTE */
a37541331   Nicolas Dichtel   gre6: fix rtnl du...
1395
  		nla_total_size(sizeof(struct in6_addr)) +
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1396
1397
  		/* IFLA_GRE_TTL */
  		nla_total_size(1) +
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1398
1399
1400
1401
1402
1403
  		/* IFLA_GRE_ENCAP_LIMIT */
  		nla_total_size(1) +
  		/* IFLA_GRE_FLOWINFO */
  		nla_total_size(4) +
  		/* IFLA_GRE_FLAGS */
  		nla_total_size(4) +
1faf3d9f7   Tom Herbert   ip6_gre: Add supp...
1404
1405
1406
1407
1408
1409
1410
1411
  		/* IFLA_GRE_ENCAP_TYPE */
  		nla_total_size(2) +
  		/* IFLA_GRE_ENCAP_FLAGS */
  		nla_total_size(2) +
  		/* IFLA_GRE_ENCAP_SPORT */
  		nla_total_size(2) +
  		/* IFLA_GRE_ENCAP_DPORT */
  		nla_total_size(2) +
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1412
1413
1414
1415
1416
1417
1418
1419
1420
  		0;
  }
  
  static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
  {
  	struct ip6_tnl *t = netdev_priv(dev);
  	struct __ip6_tnl_parm *p = &t->parms;
  
  	if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
f41fe3c2a   Tom Herbert   gre6: Fix flag tr...
1421
1422
1423
1424
  	    nla_put_be16(skb, IFLA_GRE_IFLAGS,
  			 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
  	    nla_put_be16(skb, IFLA_GRE_OFLAGS,
  			 gre_tnl_flags_to_gre_flags(p->o_flags)) ||
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1425
1426
  	    nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
  	    nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
930345ea6   Jiri Benc   netlink: implemen...
1427
1428
  	    nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
  	    nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1429
  	    nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1430
1431
1432
1433
  	    nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
  	    nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
  	    nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
  		goto nla_put_failure;
1faf3d9f7   Tom Herbert   ip6_gre: Add supp...
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
  
  	if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
  			t->encap.type) ||
  	    nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
  			 t->encap.sport) ||
  	    nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
  			 t->encap.dport) ||
  	    nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
  			t->encap.flags))
  		goto nla_put_failure;
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
  	return 0;
  
  nla_put_failure:
  	return -EMSGSIZE;
  }
  
  static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
  	[IFLA_GRE_LINK]        = { .type = NLA_U32 },
  	[IFLA_GRE_IFLAGS]      = { .type = NLA_U16 },
  	[IFLA_GRE_OFLAGS]      = { .type = NLA_U16 },
  	[IFLA_GRE_IKEY]        = { .type = NLA_U32 },
  	[IFLA_GRE_OKEY]        = { .type = NLA_U32 },
  	[IFLA_GRE_LOCAL]       = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
  	[IFLA_GRE_REMOTE]      = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
  	[IFLA_GRE_TTL]         = { .type = NLA_U8 },
  	[IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
  	[IFLA_GRE_FLOWINFO]    = { .type = NLA_U32 },
  	[IFLA_GRE_FLAGS]       = { .type = NLA_U32 },
1faf3d9f7   Tom Herbert   ip6_gre: Add supp...
1462
1463
1464
1465
  	[IFLA_GRE_ENCAP_TYPE]   = { .type = NLA_U16 },
  	[IFLA_GRE_ENCAP_FLAGS]  = { .type = NLA_U16 },
  	[IFLA_GRE_ENCAP_SPORT]  = { .type = NLA_U16 },
  	[IFLA_GRE_ENCAP_DPORT]  = { .type = NLA_U16 },
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
  };
  
  static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
  	.kind		= "ip6gre",
  	.maxtype	= IFLA_GRE_MAX,
  	.policy		= ip6gre_policy,
  	.priv_size	= sizeof(struct ip6_tnl),
  	.setup		= ip6gre_tunnel_setup,
  	.validate	= ip6gre_tunnel_validate,
  	.newlink	= ip6gre_newlink,
  	.changelink	= ip6gre_changelink,
54d63f787   Nicolas Dichtel   ip6_gre: don't al...
1477
  	.dellink	= ip6gre_dellink,
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1478
1479
  	.get_size	= ip6gre_get_size,
  	.fill_info	= ip6gre_fill_info,
1728d4fab   Nicolas Dichtel   tunnels: advertis...
1480
  	.get_link_net	= ip6_tnl_get_link_net,
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
  };
  
  static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
  	.kind		= "ip6gretap",
  	.maxtype	= IFLA_GRE_MAX,
  	.policy		= ip6gre_policy,
  	.priv_size	= sizeof(struct ip6_tnl),
  	.setup		= ip6gre_tap_setup,
  	.validate	= ip6gre_tap_validate,
  	.newlink	= ip6gre_newlink,
  	.changelink	= ip6gre_changelink,
  	.get_size	= ip6gre_get_size,
  	.fill_info	= ip6gre_fill_info,
3390e3976   Nicolas Dichtel   ip6gretap: advert...
1494
  	.get_link_net	= ip6_tnl_get_link_net,
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
  };
  
  /*
   *	And now the modules code and kernel interface.
   */
  
  static int __init ip6gre_init(void)
  {
  	int err;
  
  	pr_info("GRE over IPv6 tunneling driver
  ");
  
  	err = register_pernet_device(&ip6gre_net_ops);
  	if (err < 0)
  		return err;
  
  	err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
  	if (err < 0) {
  		pr_info("%s: can't add protocol
  ", __func__);
  		goto add_proto_failed;
  	}
  
  	err = rtnl_link_register(&ip6gre_link_ops);
  	if (err < 0)
  		goto rtnl_link_failed;
  
  	err = rtnl_link_register(&ip6gre_tap_ops);
  	if (err < 0)
  		goto tap_ops_failed;
  
  out:
  	return err;
  
  tap_ops_failed:
  	rtnl_link_unregister(&ip6gre_link_ops);
  rtnl_link_failed:
  	inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  add_proto_failed:
  	unregister_pernet_device(&ip6gre_net_ops);
  	goto out;
  }
  
  static void __exit ip6gre_fini(void)
  {
  	rtnl_link_unregister(&ip6gre_tap_ops);
  	rtnl_link_unregister(&ip6gre_link_ops);
  	inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
  	unregister_pernet_device(&ip6gre_net_ops);
  }
  
  module_init(ip6gre_init);
  module_exit(ip6gre_fini);
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
  MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
  MODULE_ALIAS_RTNL_LINK("ip6gre");
5a4ee9a9a   Nicolas Dichtel   ip6gre: add a rtn...
1553
  MODULE_ALIAS_RTNL_LINK("ip6gretap");
c12b395a4   xeb@mail.ru   gre: Support GRE ...
1554
  MODULE_ALIAS_NETDEV("ip6gre0");