ip_gre: add the support for i/o_flags update via netlink
[linux-2.6-block.git] / net / ipv4 / ip_gre.c
CommitLineData
1da177e4 1/*
e905a9ed 2 * Linux NET3: GRE over IP protocol decoder.
1da177e4
LT
3 *
4 * Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.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
afd46503
JP
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
4fc268d2 15#include <linux/capability.h>
1da177e4
LT
16#include <linux/module.h>
17#include <linux/types.h>
1da177e4 18#include <linux/kernel.h>
5a0e3ad6 19#include <linux/slab.h>
7c0f6ba6 20#include <linux/uaccess.h>
1da177e4
LT
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>
2e15ea39 27#include <linux/if_vlan.h>
1da177e4
LT
28#include <linux/init.h>
29#include <linux/in6.h>
30#include <linux/inetdevice.h>
31#include <linux/igmp.h>
32#include <linux/netfilter_ipv4.h>
e1a80002 33#include <linux/etherdevice.h>
46f25dff 34#include <linux/if_ether.h>
1da177e4
LT
35
36#include <net/sock.h>
37#include <net/ip.h>
38#include <net/icmp.h>
39#include <net/protocol.h>
c5441932 40#include <net/ip_tunnels.h>
1da177e4
LT
41#include <net/arp.h>
42#include <net/checksum.h>
43#include <net/dsfield.h>
44#include <net/inet_ecn.h>
45#include <net/xfrm.h>
59a4c759
PE
46#include <net/net_namespace.h>
47#include <net/netns/generic.h>
c19e654d 48#include <net/rtnetlink.h>
00959ade 49#include <net/gre.h>
2e15ea39 50#include <net/dst_metadata.h>
84e54fe0 51#include <net/erspan.h>
1da177e4 52
1da177e4
LT
53/*
54 Problems & solutions
55 --------------------
56
57 1. The most important issue is detecting local dead loops.
58 They would cause complete host lockup in transmit, which
59 would be "resolved" by stack overflow or, if queueing is enabled,
60 with infinite looping in net_bh.
61
62 We cannot track such dead loops during route installation,
63 it is infeasible task. The most general solutions would be
64 to keep skb->encapsulation counter (sort of local ttl),
6d0722a2 65 and silently drop packet when it expires. It is a good
bff52857 66 solution, but it supposes maintaining new variable in ALL
1da177e4
LT
67 skb, even if no tunneling is used.
68
6d0722a2
ED
69 Current solution: xmit_recursion breaks dead loops. This is a percpu
70 counter, since when we enter the first ndo_xmit(), cpu migration is
71 forbidden. We force an exit if this counter reaches RECURSION_LIMIT
1da177e4
LT
72
73 2. Networking dead loops would not kill routers, but would really
74 kill network. IP hop limit plays role of "t->recursion" in this case,
75 if we copy it from packet being encapsulated to upper header.
76 It is very good solution, but it introduces two problems:
77
78 - Routing protocols, using packets with ttl=1 (OSPF, RIP2),
79 do not work over tunnels.
80 - traceroute does not work. I planned to relay ICMP from tunnel,
81 so that this problem would be solved and traceroute output
82 would even more informative. This idea appeared to be wrong:
83 only Linux complies to rfc1812 now (yes, guys, Linux is the only
84 true router now :-)), all routers (at least, in neighbourhood of mine)
85 return only 8 bytes of payload. It is the end.
86
87 Hence, if we want that OSPF worked or traceroute said something reasonable,
88 we should search for another solution.
89
90 One of them is to parse packet trying to detect inner encapsulation
91 made by our node. It is difficult or even impossible, especially,
bff52857 92 taking into account fragmentation. TO be short, ttl is not solution at all.
1da177e4
LT
93
94 Current solution: The solution was UNEXPECTEDLY SIMPLE.
95 We force DF flag on tunnels with preconfigured hop limit,
96 that is ALL. :-) Well, it does not remove the problem completely,
97 but exponential growth of network traffic is changed to linear
98 (branches, that exceed pmtu are pruned) and tunnel mtu
bff52857 99 rapidly degrades to value <68, where looping stops.
1da177e4
LT
100 Yes, it is not good if there exists a router in the loop,
101 which does not force DF, even when encapsulating packets have DF set.
102 But it is not our problem! Nobody could accuse us, we made
103 all that we could make. Even if it is your gated who injected
104 fatal route to network, even if it were you who configured
105 fatal static route: you are innocent. :-)
106
1da177e4
LT
107 Alexey Kuznetsov.
108 */
109
eccc1bb8 110static bool log_ecn_error = true;
111module_param(log_ecn_error, bool, 0644);
112MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
113
c19e654d 114static struct rtnl_link_ops ipgre_link_ops __read_mostly;
1da177e4 115static int ipgre_tunnel_init(struct net_device *dev);
1a66a836
WT
116static void erspan_build_header(struct sk_buff *skb,
117 __be32 id, u32 index, bool truncate);
eb8ce741 118
c7d03a00
AD
119static unsigned int ipgre_net_id __read_mostly;
120static unsigned int gre_tap_net_id __read_mostly;
84e54fe0 121static unsigned int erspan_net_id __read_mostly;
1da177e4 122
9f57c67c
PS
123static void ipgre_err(struct sk_buff *skb, u32 info,
124 const struct tnl_ptk_info *tpi)
1da177e4 125{
1da177e4 126
c5441932
PS
127 /* All the routers (except for Linux) return only
128 8 bytes of packet payload. It means, that precise relaying of
129 ICMP in the real Internet is absolutely infeasible.
1da177e4 130
c5441932
PS
131 Moreover, Cisco "wise men" put GRE key to the third word
132 in GRE header. It makes impossible maintaining even soft
133 state for keyed GRE tunnels with enabled checksum. Tell
134 them "thank you".
1da177e4 135
c5441932
PS
136 Well, I wonder, rfc1812 was written by Cisco employee,
137 what the hell these idiots break standards established
138 by themselves???
139 */
140 struct net *net = dev_net(skb->dev);
141 struct ip_tunnel_net *itn;
96f5a846 142 const struct iphdr *iph;
88c7664f
ACM
143 const int type = icmp_hdr(skb)->type;
144 const int code = icmp_hdr(skb)->code;
20e1954f 145 unsigned int data_len = 0;
1da177e4 146 struct ip_tunnel *t;
1da177e4 147
1da177e4
LT
148 switch (type) {
149 default:
150 case ICMP_PARAMETERPROB:
9f57c67c 151 return;
1da177e4
LT
152
153 case ICMP_DEST_UNREACH:
154 switch (code) {
155 case ICMP_SR_FAILED:
156 case ICMP_PORT_UNREACH:
157 /* Impossible event. */
9f57c67c 158 return;
1da177e4
LT
159 default:
160 /* All others are translated to HOST_UNREACH.
161 rfc2003 contains "deep thoughts" about NET_UNREACH,
162 I believe they are just ether pollution. --ANK
163 */
164 break;
165 }
166 break;
9f57c67c 167
1da177e4
LT
168 case ICMP_TIME_EXCEEDED:
169 if (code != ICMP_EXC_TTL)
9f57c67c 170 return;
20e1954f 171 data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */
1da177e4 172 break;
55be7a9c
DM
173
174 case ICMP_REDIRECT:
175 break;
1da177e4
LT
176 }
177
bda7bb46 178 if (tpi->proto == htons(ETH_P_TEB))
c5441932
PS
179 itn = net_generic(net, gre_tap_net_id);
180 else
181 itn = net_generic(net, ipgre_net_id);
182
c0c0c50f 183 iph = (const struct iphdr *)(icmp_hdr(skb) + 1);
bda7bb46
PS
184 t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
185 iph->daddr, iph->saddr, tpi->key);
d2083287 186
51456b29 187 if (!t)
9f57c67c 188 return;
36393395 189
9b8c6d7b
ED
190#if IS_ENABLED(CONFIG_IPV6)
191 if (tpi->proto == htons(ETH_P_IPV6) &&
20e1954f
ED
192 !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4 + tpi->hdr_len,
193 type, data_len))
9b8c6d7b
ED
194 return;
195#endif
196
36393395 197 if (t->parms.iph.daddr == 0 ||
f97c1e0c 198 ipv4_is_multicast(t->parms.iph.daddr))
9f57c67c 199 return;
1da177e4
LT
200
201 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
9f57c67c 202 return;
1da177e4 203
da6185d8 204 if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
1da177e4
LT
205 t->err_count++;
206 else
207 t->err_count = 1;
208 t->err_time = jiffies;
9f57c67c
PS
209}
210
211static void gre_err(struct sk_buff *skb, u32 info)
212{
213 /* All the routers (except for Linux) return only
214 * 8 bytes of packet payload. It means, that precise relaying of
215 * ICMP in the real Internet is absolutely infeasible.
216 *
217 * Moreover, Cisco "wise men" put GRE key to the third word
218 * in GRE header. It makes impossible maintaining even soft
219 * state for keyed
220 * GRE tunnels with enabled checksum. Tell them "thank you".
221 *
222 * Well, I wonder, rfc1812 was written by Cisco employee,
223 * what the hell these idiots break standards established
224 * by themselves???
225 */
226
e582615a 227 const struct iphdr *iph = (struct iphdr *)skb->data;
9f57c67c
PS
228 const int type = icmp_hdr(skb)->type;
229 const int code = icmp_hdr(skb)->code;
230 struct tnl_ptk_info tpi;
231 bool csum_err = false;
232
e582615a
ED
233 if (gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP),
234 iph->ihl * 4) < 0) {
9f57c67c
PS
235 if (!csum_err) /* ignore csum errors. */
236 return;
237 }
238
239 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
240 ipv4_update_pmtu(skb, dev_net(skb->dev), info,
241 skb->dev->ifindex, 0, IPPROTO_GRE, 0);
242 return;
243 }
244 if (type == ICMP_REDIRECT) {
245 ipv4_redirect(skb, dev_net(skb->dev), skb->dev->ifindex, 0,
246 IPPROTO_GRE, 0);
247 return;
248 }
249
250 ipgre_err(skb, info, &tpi);
1da177e4
LT
251}
252
84e54fe0
WT
253static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi,
254 int gre_hdr_len)
255{
256 struct net *net = dev_net(skb->dev);
257 struct metadata_dst *tun_dst = NULL;
258 struct ip_tunnel_net *itn;
259 struct ip_tunnel *tunnel;
260 struct erspanhdr *ershdr;
261 const struct iphdr *iph;
84e54fe0
WT
262 __be32 index;
263 int len;
264
265 itn = net_generic(net, erspan_net_id);
84e54fe0
WT
266 len = gre_hdr_len + sizeof(*ershdr);
267
268 if (unlikely(!pskb_may_pull(skb, len)))
269 return -ENOMEM;
270
271 iph = ip_hdr(skb);
272 ershdr = (struct erspanhdr *)(skb->data + gre_hdr_len);
273
274 /* The original GRE header does not have key field,
275 * Use ERSPAN 10-bit session ID as key.
276 */
935a9749 277 tpi->key = cpu_to_be32(ntohs(ershdr->session_id) & ID_MASK);
84e54fe0
WT
278 index = ershdr->md.index;
279 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex,
280 tpi->flags | TUNNEL_KEY,
281 iph->saddr, iph->daddr, tpi->key);
282
283 if (tunnel) {
284 if (__iptunnel_pull_header(skb,
285 gre_hdr_len + sizeof(*ershdr),
286 htons(ETH_P_TEB),
287 false, false) < 0)
288 goto drop;
289
1a66a836
WT
290 if (tunnel->collect_md) {
291 struct ip_tunnel_info *info;
292 struct erspan_metadata *md;
293 __be64 tun_id;
294 __be16 flags;
295
296 tpi->flags |= TUNNEL_KEY;
297 flags = tpi->flags;
298 tun_id = key32_to_tunnel_id(tpi->key);
299
300 tun_dst = ip_tun_rx_dst(skb, flags,
301 tun_id, sizeof(*md));
302 if (!tun_dst)
303 return PACKET_REJECT;
304
305 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
306 if (!md)
307 return PACKET_REJECT;
308
309 md->index = index;
310 info = &tun_dst->u.tun_info;
311 info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
312 info->options_len = sizeof(*md);
313 } else {
314 tunnel->index = ntohl(index);
315 }
316
84e54fe0
WT
317 skb_reset_mac_header(skb);
318 ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
319 return PACKET_RCVD;
320 }
321drop:
322 kfree_skb(skb);
323 return PACKET_RCVD;
324}
325
125372fa
JB
326static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
327 struct ip_tunnel_net *itn, int hdr_len, bool raw_proto)
1da177e4 328{
2e15ea39 329 struct metadata_dst *tun_dst = NULL;
b71d1d42 330 const struct iphdr *iph;
1da177e4 331 struct ip_tunnel *tunnel;
1da177e4 332
c5441932 333 iph = ip_hdr(skb);
bda7bb46
PS
334 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
335 iph->saddr, iph->daddr, tpi->key);
e1a80002 336
d2083287 337 if (tunnel) {
125372fa
JB
338 if (__iptunnel_pull_header(skb, hdr_len, tpi->proto,
339 raw_proto, false) < 0)
244a797b
JB
340 goto drop;
341
e271c7b4
JB
342 if (tunnel->dev->type != ARPHRD_NONE)
343 skb_pop_mac_header(skb);
344 else
345 skb_reset_mac_header(skb);
2e15ea39 346 if (tunnel->collect_md) {
c29a70d2
PS
347 __be16 flags;
348 __be64 tun_id;
2e15ea39 349
c29a70d2 350 flags = tpi->flags & (TUNNEL_CSUM | TUNNEL_KEY);
d817f432 351 tun_id = key32_to_tunnel_id(tpi->key);
c29a70d2 352 tun_dst = ip_tun_rx_dst(skb, flags, tun_id, 0);
2e15ea39
PS
353 if (!tun_dst)
354 return PACKET_REJECT;
2e15ea39
PS
355 }
356
357 ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
bda7bb46 358 return PACKET_RCVD;
1da177e4 359 }
125372fa 360 return PACKET_NEXT;
244a797b
JB
361
362drop:
363 kfree_skb(skb);
364 return PACKET_RCVD;
1da177e4
LT
365}
366
125372fa
JB
367static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
368 int hdr_len)
369{
370 struct net *net = dev_net(skb->dev);
371 struct ip_tunnel_net *itn;
372 int res;
373
374 if (tpi->proto == htons(ETH_P_TEB))
375 itn = net_generic(net, gre_tap_net_id);
376 else
377 itn = net_generic(net, ipgre_net_id);
378
379 res = __ipgre_rcv(skb, tpi, itn, hdr_len, false);
380 if (res == PACKET_NEXT && tpi->proto == htons(ETH_P_TEB)) {
381 /* ipgre tunnels in collect metadata mode should receive
382 * also ETH_P_TEB traffic.
383 */
384 itn = net_generic(net, ipgre_net_id);
385 res = __ipgre_rcv(skb, tpi, itn, hdr_len, true);
386 }
387 return res;
388}
389
9f57c67c
PS
390static int gre_rcv(struct sk_buff *skb)
391{
392 struct tnl_ptk_info tpi;
393 bool csum_err = false;
95f5c64c 394 int hdr_len;
9f57c67c
PS
395
396#ifdef CONFIG_NET_IPGRE_BROADCAST
397 if (ipv4_is_multicast(ip_hdr(skb)->daddr)) {
398 /* Looped back packet, drop it! */
399 if (rt_is_output_route(skb_rtable(skb)))
400 goto drop;
401 }
402#endif
403
e582615a 404 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP), 0);
f132ae7c 405 if (hdr_len < 0)
95f5c64c
TH
406 goto drop;
407
84e54fe0
WT
408 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN))) {
409 if (erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
410 return 0;
411 }
412
244a797b 413 if (ipgre_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
9f57c67c
PS
414 return 0;
415
416 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
417drop:
418 kfree_skb(skb);
419 return 0;
420}
421
c5441932
PS
422static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
423 const struct iphdr *tnl_params,
424 __be16 proto)
425{
426 struct ip_tunnel *tunnel = netdev_priv(dev);
1da177e4 427
c5441932
PS
428 if (tunnel->parms.o_flags & TUNNEL_SEQ)
429 tunnel->o_seqno++;
1da177e4 430
c5441932 431 /* Push GRE header. */
182a352d
TH
432 gre_build_header(skb, tunnel->tun_hlen,
433 tunnel->parms.o_flags, proto, tunnel->parms.o_key,
434 htonl(tunnel->o_seqno));
54bc9bac 435
bf3d6a8f 436 ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol);
c5441932 437}
1da177e4 438
aed069df 439static int gre_handle_offloads(struct sk_buff *skb, bool csum)
b2acd1dc 440{
6fa79666 441 return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
b2acd1dc
PS
442}
443
fc4099f1
PS
444static struct rtable *gre_get_rt(struct sk_buff *skb,
445 struct net_device *dev,
446 struct flowi4 *fl,
447 const struct ip_tunnel_key *key)
448{
449 struct net *net = dev_net(dev);
450
451 memset(fl, 0, sizeof(*fl));
452 fl->daddr = key->u.ipv4.dst;
453 fl->saddr = key->u.ipv4.src;
454 fl->flowi4_tos = RT_TOS(key->tos);
455 fl->flowi4_mark = skb->mark;
456 fl->flowi4_proto = IPPROTO_GRE;
457
458 return ip_route_output_key(net, fl);
459}
460
862a03c3
WT
461static struct rtable *prepare_fb_xmit(struct sk_buff *skb,
462 struct net_device *dev,
463 struct flowi4 *fl,
464 int tunnel_hlen)
2e15ea39
PS
465{
466 struct ip_tunnel_info *tun_info;
2e15ea39 467 const struct ip_tunnel_key *key;
db3c6139 468 struct rtable *rt = NULL;
2e15ea39 469 int min_headroom;
db3c6139 470 bool use_cache;
2e15ea39
PS
471 int err;
472
61adedf3 473 tun_info = skb_tunnel_info(skb);
2e15ea39 474 key = &tun_info->key;
db3c6139 475 use_cache = ip_tunnel_dst_cache_usable(skb, tun_info);
862a03c3 476
db3c6139 477 if (use_cache)
862a03c3 478 rt = dst_cache_get_ip4(&tun_info->dst_cache, &fl->saddr);
3c1cb4d2 479 if (!rt) {
862a03c3 480 rt = gre_get_rt(skb, dev, fl, key);
3c1cb4d2 481 if (IS_ERR(rt))
862a03c3 482 goto err_free_skb;
db3c6139 483 if (use_cache)
3c1cb4d2 484 dst_cache_set_ip4(&tun_info->dst_cache, &rt->dst,
862a03c3 485 fl->saddr);
3c1cb4d2 486 }
2e15ea39 487
2e15ea39
PS
488 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
489 + tunnel_hlen + sizeof(struct iphdr);
490 if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
491 int head_delta = SKB_DATA_ALIGN(min_headroom -
492 skb_headroom(skb) +
493 16);
494 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
495 0, GFP_ATOMIC);
496 if (unlikely(err))
497 goto err_free_rt;
498 }
862a03c3
WT
499 return rt;
500
501err_free_rt:
502 ip_rt_put(rt);
503err_free_skb:
504 kfree_skb(skb);
505 dev->stats.tx_dropped++;
506 return NULL;
507}
508
509static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev,
510 __be16 proto)
511{
512 struct ip_tunnel_info *tun_info;
513 const struct ip_tunnel_key *key;
514 struct rtable *rt = NULL;
515 struct flowi4 fl;
516 int tunnel_hlen;
517 __be16 df, flags;
518
519 tun_info = skb_tunnel_info(skb);
520 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
521 ip_tunnel_info_af(tun_info) != AF_INET))
522 goto err_free_skb;
523
524 key = &tun_info->key;
525 tunnel_hlen = gre_calc_hlen(key->tun_flags);
526
527 rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen);
528 if (!rt)
529 return;
2e15ea39
PS
530
531 /* Push Tunnel header. */
aed069df 532 if (gre_handle_offloads(skb, !!(tun_info->key.tun_flags & TUNNEL_CSUM)))
2e15ea39 533 goto err_free_rt;
2e15ea39
PS
534
535 flags = tun_info->key.tun_flags & (TUNNEL_CSUM | TUNNEL_KEY);
cba65321 536 gre_build_header(skb, tunnel_hlen, flags, proto,
d817f432 537 tunnel_id_to_key32(tun_info->key.tun_id), 0);
2e15ea39
PS
538
539 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
039f5062
PS
540
541 iptunnel_xmit(skb->sk, rt, skb, fl.saddr, key->u.ipv4.dst, IPPROTO_GRE,
542 key->tos, key->ttl, df, false);
2e15ea39
PS
543 return;
544
545err_free_rt:
546 ip_rt_put(rt);
547err_free_skb:
548 kfree_skb(skb);
549 dev->stats.tx_dropped++;
550}
551
1a66a836
WT
552static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev,
553 __be16 proto)
554{
555 struct ip_tunnel *tunnel = netdev_priv(dev);
556 struct ip_tunnel_info *tun_info;
557 const struct ip_tunnel_key *key;
558 struct erspan_metadata *md;
559 struct rtable *rt = NULL;
560 bool truncate = false;
561 struct flowi4 fl;
562 int tunnel_hlen;
563 __be16 df;
564
565 tun_info = skb_tunnel_info(skb);
566 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
567 ip_tunnel_info_af(tun_info) != AF_INET))
568 goto err_free_skb;
569
570 key = &tun_info->key;
571
572 /* ERSPAN has fixed 8 byte GRE header */
573 tunnel_hlen = 8 + sizeof(struct erspanhdr);
574
575 rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen);
576 if (!rt)
577 return;
578
579 if (gre_handle_offloads(skb, false))
580 goto err_free_rt;
581
f192970d
WT
582 if (skb->len > dev->mtu + dev->hard_header_len) {
583 pskb_trim(skb, dev->mtu + dev->hard_header_len);
1a66a836
WT
584 truncate = true;
585 }
586
587 md = ip_tunnel_info_opts(tun_info);
588 if (!md)
589 goto err_free_rt;
590
591 erspan_build_header(skb, tunnel_id_to_key32(key->tun_id),
592 ntohl(md->index), truncate);
593
594 gre_build_header(skb, 8, TUNNEL_SEQ,
595 htons(ETH_P_ERSPAN), 0, htonl(tunnel->o_seqno++));
596
597 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
598
599 iptunnel_xmit(skb->sk, rt, skb, fl.saddr, key->u.ipv4.dst, IPPROTO_GRE,
600 key->tos, key->ttl, df, false);
601 return;
602
603err_free_rt:
604 ip_rt_put(rt);
605err_free_skb:
606 kfree_skb(skb);
607 dev->stats.tx_dropped++;
608}
609
fc4099f1
PS
610static int gre_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
611{
612 struct ip_tunnel_info *info = skb_tunnel_info(skb);
613 struct rtable *rt;
614 struct flowi4 fl4;
615
616 if (ip_tunnel_info_af(info) != AF_INET)
617 return -EINVAL;
618
619 rt = gre_get_rt(skb, dev, &fl4, &info->key);
620 if (IS_ERR(rt))
621 return PTR_ERR(rt);
622
623 ip_rt_put(rt);
624 info->key.u.ipv4.src = fl4.saddr;
625 return 0;
626}
627
c5441932
PS
628static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
629 struct net_device *dev)
630{
631 struct ip_tunnel *tunnel = netdev_priv(dev);
632 const struct iphdr *tnl_params;
1da177e4 633
2e15ea39 634 if (tunnel->collect_md) {
2090714e 635 gre_fb_xmit(skb, dev, skb->protocol);
2e15ea39
PS
636 return NETDEV_TX_OK;
637 }
638
c5441932
PS
639 if (dev->header_ops) {
640 /* Need space for new headers */
641 if (skb_cow_head(skb, dev->needed_headroom -
2bac7cb3 642 (tunnel->hlen + sizeof(struct iphdr))))
c5441932 643 goto free_skb;
1da177e4 644
c5441932 645 tnl_params = (const struct iphdr *)skb->data;
1da177e4 646
c5441932
PS
647 /* Pull skb since ip_tunnel_xmit() needs skb->data pointing
648 * to gre header.
649 */
650 skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
8a0033a9 651 skb_reset_mac_header(skb);
c5441932
PS
652 } else {
653 if (skb_cow_head(skb, dev->needed_headroom))
654 goto free_skb;
1da177e4 655
c5441932 656 tnl_params = &tunnel->parms.iph;
1da177e4
LT
657 }
658
aed069df
AD
659 if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
660 goto free_skb;
8a0033a9 661
c5441932 662 __gre_xmit(skb, dev, tnl_params, skb->protocol);
6ed10654 663 return NETDEV_TX_OK;
1da177e4 664
c5441932 665free_skb:
3acfa1e7 666 kfree_skb(skb);
c5441932 667 dev->stats.tx_dropped++;
6ed10654 668 return NETDEV_TX_OK;
1da177e4
LT
669}
670
84e54fe0
WT
671static inline u8 tos_to_cos(u8 tos)
672{
673 u8 dscp, cos;
674
675 dscp = tos >> 2;
676 cos = dscp >> 3;
677 return cos;
678}
679
680static void erspan_build_header(struct sk_buff *skb,
681 __be32 id, u32 index, bool truncate)
682{
683 struct iphdr *iphdr = ip_hdr(skb);
684 struct ethhdr *eth = eth_hdr(skb);
685 enum erspan_encap_type enc_type;
686 struct erspanhdr *ershdr;
687 struct qtag_prefix {
688 __be16 eth_type;
689 __be16 tci;
690 } *qp;
691 u16 vlan_tci = 0;
692
693 enc_type = ERSPAN_ENCAP_NOVLAN;
694
695 /* If mirrored packet has vlan tag, extract tci and
696 * perserve vlan header in the mirrored frame.
697 */
698 if (eth->h_proto == htons(ETH_P_8021Q)) {
699 qp = (struct qtag_prefix *)(skb->data + 2 * ETH_ALEN);
700 vlan_tci = ntohs(qp->tci);
701 enc_type = ERSPAN_ENCAP_INFRAME;
702 }
703
704 skb_push(skb, sizeof(*ershdr));
705 ershdr = (struct erspanhdr *)skb->data;
706 memset(ershdr, 0, sizeof(*ershdr));
707
708 ershdr->ver_vlan = htons((vlan_tci & VLAN_MASK) |
709 (ERSPAN_VERSION << VER_OFFSET));
710 ershdr->session_id = htons((u16)(ntohl(id) & ID_MASK) |
711 ((tos_to_cos(iphdr->tos) << COS_OFFSET) & COS_MASK) |
712 (enc_type << EN_OFFSET & EN_MASK) |
713 ((truncate << T_OFFSET) & T_MASK));
714 ershdr->md.index = htonl(index & INDEX_MASK);
715}
716
717static netdev_tx_t erspan_xmit(struct sk_buff *skb,
718 struct net_device *dev)
719{
720 struct ip_tunnel *tunnel = netdev_priv(dev);
721 bool truncate = false;
722
1a66a836
WT
723 if (tunnel->collect_md) {
724 erspan_fb_xmit(skb, dev, skb->protocol);
725 return NETDEV_TX_OK;
726 }
727
84e54fe0
WT
728 if (gre_handle_offloads(skb, false))
729 goto free_skb;
730
731 if (skb_cow_head(skb, dev->needed_headroom))
732 goto free_skb;
733
f192970d
WT
734 if (skb->len > dev->mtu + dev->hard_header_len) {
735 pskb_trim(skb, dev->mtu + dev->hard_header_len);
84e54fe0
WT
736 truncate = true;
737 }
738
739 /* Push ERSPAN header */
740 erspan_build_header(skb, tunnel->parms.o_key, tunnel->index, truncate);
741 tunnel->parms.o_flags &= ~TUNNEL_KEY;
742 __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_ERSPAN));
743 return NETDEV_TX_OK;
744
745free_skb:
746 kfree_skb(skb);
747 dev->stats.tx_dropped++;
748 return NETDEV_TX_OK;
749}
750
c5441932
PS
751static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
752 struct net_device *dev)
ee34c1eb 753{
c5441932 754 struct ip_tunnel *tunnel = netdev_priv(dev);
ee34c1eb 755
2e15ea39 756 if (tunnel->collect_md) {
2090714e 757 gre_fb_xmit(skb, dev, htons(ETH_P_TEB));
2e15ea39
PS
758 return NETDEV_TX_OK;
759 }
760
aed069df
AD
761 if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
762 goto free_skb;
ee34c1eb 763
c5441932
PS
764 if (skb_cow_head(skb, dev->needed_headroom))
765 goto free_skb;
42aa9162 766
c5441932 767 __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB));
c5441932 768 return NETDEV_TX_OK;
ee34c1eb 769
c5441932 770free_skb:
3acfa1e7 771 kfree_skb(skb);
c5441932
PS
772 dev->stats.tx_dropped++;
773 return NETDEV_TX_OK;
ee34c1eb
MS
774}
775
dd9d598c
XL
776static void ipgre_link_update(struct net_device *dev, bool set_mtu)
777{
778 struct ip_tunnel *tunnel = netdev_priv(dev);
779 int len;
780
781 len = tunnel->tun_hlen;
782 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
783 len = tunnel->tun_hlen - len;
784 tunnel->hlen = tunnel->hlen + len;
785
786 dev->needed_headroom = dev->needed_headroom + len;
787 if (set_mtu)
788 dev->mtu = max_t(int, dev->mtu - len, 68);
789
790 if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
791 if (!(tunnel->parms.o_flags & TUNNEL_CSUM) ||
792 tunnel->encap.type == TUNNEL_ENCAP_NONE) {
793 dev->features |= NETIF_F_GSO_SOFTWARE;
794 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
795 }
796 dev->features |= NETIF_F_LLTX;
797 }
798}
799
c5441932
PS
800static int ipgre_tunnel_ioctl(struct net_device *dev,
801 struct ifreq *ifr, int cmd)
1da177e4 802{
4565e991 803 int err;
1da177e4 804 struct ip_tunnel_parm p;
1da177e4 805
c5441932
PS
806 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
807 return -EFAULT;
6c734fb8
CW
808 if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
809 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||
810 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) ||
811 ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)))
812 return -EINVAL;
1da177e4 813 }
c5441932
PS
814 p.i_flags = gre_flags_to_tnl_flags(p.i_flags);
815 p.o_flags = gre_flags_to_tnl_flags(p.o_flags);
1da177e4 816
c5441932
PS
817 err = ip_tunnel_ioctl(dev, &p, cmd);
818 if (err)
819 return err;
1da177e4 820
95f5c64c
TH
821 p.i_flags = gre_tnl_flags_to_gre_flags(p.i_flags);
822 p.o_flags = gre_tnl_flags_to_gre_flags(p.o_flags);
c5441932
PS
823
824 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
825 return -EFAULT;
1da177e4
LT
826 return 0;
827}
828
1da177e4
LT
829/* Nice toy. Unfortunately, useless in real life :-)
830 It allows to construct virtual multiprotocol broadcast "LAN"
831 over the Internet, provided multicast routing is tuned.
832
833
834 I have no idea was this bicycle invented before me,
835 so that I had to set ARPHRD_IPGRE to a random value.
836 I have an impression, that Cisco could make something similar,
837 but this feature is apparently missing in IOS<=11.2(8).
e905a9ed 838
1da177e4
LT
839 I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
840 with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
841
842 ping -t 255 224.66.66.66
843
844 If nobody answers, mbone does not work.
845
846 ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
847 ip addr add 10.66.66.<somewhat>/24 dev Universe
848 ifconfig Universe up
849 ifconfig Universe add fe80::<Your_real_addr>/10
850 ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
851 ftp 10.66.66.66
852 ...
853 ftp fec0:6666:6666::193.233.7.65
854 ...
1da177e4 855 */
3b04ddde
SH
856static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
857 unsigned short type,
1507850b 858 const void *daddr, const void *saddr, unsigned int len)
1da177e4 859{
2941a486 860 struct ip_tunnel *t = netdev_priv(dev);
c5441932
PS
861 struct iphdr *iph;
862 struct gre_base_hdr *greh;
1da177e4 863
d58ff351 864 iph = skb_push(skb, t->hlen + sizeof(*iph));
c5441932 865 greh = (struct gre_base_hdr *)(iph+1);
95f5c64c 866 greh->flags = gre_tnl_flags_to_gre_flags(t->parms.o_flags);
c5441932 867 greh->protocol = htons(type);
1da177e4 868
c5441932 869 memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
e905a9ed 870
c5441932 871 /* Set the source hardware address. */
1da177e4
LT
872 if (saddr)
873 memcpy(&iph->saddr, saddr, 4);
6d55cb91 874 if (daddr)
1da177e4 875 memcpy(&iph->daddr, daddr, 4);
6d55cb91 876 if (iph->daddr)
77a482bd 877 return t->hlen + sizeof(*iph);
e905a9ed 878
c5441932 879 return -(t->hlen + sizeof(*iph));
1da177e4
LT
880}
881
6a5f44d7
TT
882static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
883{
b71d1d42 884 const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb);
6a5f44d7
TT
885 memcpy(haddr, &iph->saddr, 4);
886 return 4;
887}
888
3b04ddde
SH
889static const struct header_ops ipgre_header_ops = {
890 .create = ipgre_header,
6a5f44d7 891 .parse = ipgre_header_parse,
3b04ddde
SH
892};
893
6a5f44d7 894#ifdef CONFIG_NET_IPGRE_BROADCAST
1da177e4
LT
895static int ipgre_open(struct net_device *dev)
896{
2941a486 897 struct ip_tunnel *t = netdev_priv(dev);
1da177e4 898
f97c1e0c 899 if (ipv4_is_multicast(t->parms.iph.daddr)) {
cbb1e85f
DM
900 struct flowi4 fl4;
901 struct rtable *rt;
902
b57708ad 903 rt = ip_route_output_gre(t->net, &fl4,
cbb1e85f
DM
904 t->parms.iph.daddr,
905 t->parms.iph.saddr,
906 t->parms.o_key,
907 RT_TOS(t->parms.iph.tos),
908 t->parms.link);
b23dd4fe 909 if (IS_ERR(rt))
1da177e4 910 return -EADDRNOTAVAIL;
d8d1f30b 911 dev = rt->dst.dev;
1da177e4 912 ip_rt_put(rt);
51456b29 913 if (!__in_dev_get_rtnl(dev))
1da177e4
LT
914 return -EADDRNOTAVAIL;
915 t->mlink = dev->ifindex;
e5ed6399 916 ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
1da177e4
LT
917 }
918 return 0;
919}
920
921static int ipgre_close(struct net_device *dev)
922{
2941a486 923 struct ip_tunnel *t = netdev_priv(dev);
b8c26a33 924
f97c1e0c 925 if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
7fee0ca2 926 struct in_device *in_dev;
b57708ad 927 in_dev = inetdev_by_index(t->net, t->mlink);
8723e1b4 928 if (in_dev)
1da177e4 929 ip_mc_dec_group(in_dev, t->parms.iph.daddr);
1da177e4
LT
930 }
931 return 0;
932}
1da177e4
LT
933#endif
934
b8c26a33
SH
935static const struct net_device_ops ipgre_netdev_ops = {
936 .ndo_init = ipgre_tunnel_init,
c5441932 937 .ndo_uninit = ip_tunnel_uninit,
b8c26a33
SH
938#ifdef CONFIG_NET_IPGRE_BROADCAST
939 .ndo_open = ipgre_open,
940 .ndo_stop = ipgre_close,
941#endif
c5441932 942 .ndo_start_xmit = ipgre_xmit,
b8c26a33 943 .ndo_do_ioctl = ipgre_tunnel_ioctl,
c5441932
PS
944 .ndo_change_mtu = ip_tunnel_change_mtu,
945 .ndo_get_stats64 = ip_tunnel_get_stats64,
1e99584b 946 .ndo_get_iflink = ip_tunnel_get_iflink,
b8c26a33
SH
947};
948
6b78f16e
ED
949#define GRE_FEATURES (NETIF_F_SG | \
950 NETIF_F_FRAGLIST | \
951 NETIF_F_HIGHDMA | \
952 NETIF_F_HW_CSUM)
953
1da177e4
LT
954static void ipgre_tunnel_setup(struct net_device *dev)
955{
b8c26a33 956 dev->netdev_ops = &ipgre_netdev_ops;
5a455275 957 dev->type = ARPHRD_IPGRE;
c5441932
PS
958 ip_tunnel_setup(dev, ipgre_net_id);
959}
1da177e4 960
c5441932
PS
961static void __gre_tunnel_init(struct net_device *dev)
962{
963 struct ip_tunnel *tunnel;
4565e991 964 int t_hlen;
c5441932
PS
965
966 tunnel = netdev_priv(dev);
95f5c64c 967 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
c5441932
PS
968 tunnel->parms.iph.protocol = IPPROTO_GRE;
969
4565e991
TH
970 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
971
972 t_hlen = tunnel->hlen + sizeof(struct iphdr);
973
974 dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4;
975 dev->mtu = ETH_DATA_LEN - t_hlen - 4;
6b78f16e 976
b57708ad 977 dev->features |= GRE_FEATURES;
6b78f16e 978 dev->hw_features |= GRE_FEATURES;
c5441932
PS
979
980 if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
a0ca153f
AD
981 /* TCP offload with GRE SEQ is not supported, nor
982 * can we support 2 levels of outer headers requiring
983 * an update.
984 */
985 if (!(tunnel->parms.o_flags & TUNNEL_CSUM) ||
986 (tunnel->encap.type == TUNNEL_ENCAP_NONE)) {
987 dev->features |= NETIF_F_GSO_SOFTWARE;
988 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
989 }
990
c5441932
PS
991 /* Can use a lockless transmit, unless we generate
992 * output sequences
993 */
994 dev->features |= NETIF_F_LLTX;
995 }
1da177e4
LT
996}
997
998static int ipgre_tunnel_init(struct net_device *dev)
999{
c5441932
PS
1000 struct ip_tunnel *tunnel = netdev_priv(dev);
1001 struct iphdr *iph = &tunnel->parms.iph;
1da177e4 1002
c5441932 1003 __gre_tunnel_init(dev);
1da177e4 1004
c5441932
PS
1005 memcpy(dev->dev_addr, &iph->saddr, 4);
1006 memcpy(dev->broadcast, &iph->daddr, 4);
1da177e4 1007
c5441932 1008 dev->flags = IFF_NOARP;
02875878 1009 netif_keep_dst(dev);
c5441932 1010 dev->addr_len = 4;
1da177e4 1011
a64b04d8 1012 if (iph->daddr && !tunnel->collect_md) {
1da177e4 1013#ifdef CONFIG_NET_IPGRE_BROADCAST
f97c1e0c 1014 if (ipv4_is_multicast(iph->daddr)) {
1da177e4
LT
1015 if (!iph->saddr)
1016 return -EINVAL;
1017 dev->flags = IFF_BROADCAST;
3b04ddde 1018 dev->header_ops = &ipgre_header_ops;
1da177e4
LT
1019 }
1020#endif
a64b04d8 1021 } else if (!tunnel->collect_md) {
6a5f44d7 1022 dev->header_ops = &ipgre_header_ops;
a64b04d8 1023 }
1da177e4 1024
c5441932 1025 return ip_tunnel_init(dev);
1da177e4
LT
1026}
1027
9f57c67c
PS
1028static const struct gre_protocol ipgre_protocol = {
1029 .handler = gre_rcv,
1030 .err_handler = gre_err,
1da177e4
LT
1031};
1032
2c8c1e72 1033static int __net_init ipgre_init_net(struct net *net)
59a4c759 1034{
c5441932 1035 return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL);
59a4c759
PE
1036}
1037
64bc1781 1038static void __net_exit ipgre_exit_batch_net(struct list_head *list_net)
59a4c759 1039{
64bc1781 1040 ip_tunnel_delete_nets(list_net, ipgre_net_id, &ipgre_link_ops);
59a4c759
PE
1041}
1042
1043static struct pernet_operations ipgre_net_ops = {
1044 .init = ipgre_init_net,
64bc1781 1045 .exit_batch = ipgre_exit_batch_net,
cfb8fbf2 1046 .id = &ipgre_net_id,
c5441932 1047 .size = sizeof(struct ip_tunnel_net),
59a4c759 1048};
1da177e4 1049
a8b8a889
MS
1050static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1051 struct netlink_ext_ack *extack)
c19e654d
HX
1052{
1053 __be16 flags;
1054
1055 if (!data)
1056 return 0;
1057
1058 flags = 0;
1059 if (data[IFLA_GRE_IFLAGS])
1060 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1061 if (data[IFLA_GRE_OFLAGS])
1062 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1063 if (flags & (GRE_VERSION|GRE_ROUTING))
1064 return -EINVAL;
1065
946b636f
JB
1066 if (data[IFLA_GRE_COLLECT_METADATA] &&
1067 data[IFLA_GRE_ENCAP_TYPE] &&
1068 nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE)
1069 return -EINVAL;
1070
c19e654d
HX
1071 return 0;
1072}
1073
a8b8a889
MS
1074static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1075 struct netlink_ext_ack *extack)
e1a80002
HX
1076{
1077 __be32 daddr;
1078
1079 if (tb[IFLA_ADDRESS]) {
1080 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1081 return -EINVAL;
1082 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1083 return -EADDRNOTAVAIL;
1084 }
1085
1086 if (!data)
1087 goto out;
1088
1089 if (data[IFLA_GRE_REMOTE]) {
1090 memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4);
1091 if (!daddr)
1092 return -EINVAL;
1093 }
1094
1095out:
a8b8a889 1096 return ipgre_tunnel_validate(tb, data, extack);
e1a80002
HX
1097}
1098
84e54fe0
WT
1099static int erspan_validate(struct nlattr *tb[], struct nlattr *data[],
1100 struct netlink_ext_ack *extack)
1101{
1102 __be16 flags = 0;
1103 int ret;
1104
1105 if (!data)
1106 return 0;
1107
1108 ret = ipgre_tap_validate(tb, data, extack);
1109 if (ret)
1110 return ret;
1111
1112 /* ERSPAN should only have GRE sequence and key flag */
1a66a836
WT
1113 if (data[IFLA_GRE_OFLAGS])
1114 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1115 if (data[IFLA_GRE_IFLAGS])
1116 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1117 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1118 flags != (GRE_SEQ | GRE_KEY))
84e54fe0
WT
1119 return -EINVAL;
1120
1121 /* ERSPAN Session ID only has 10-bit. Since we reuse
1122 * 32-bit key field as ID, check it's range.
1123 */
1124 if (data[IFLA_GRE_IKEY] &&
1125 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1126 return -EINVAL;
1127
1128 if (data[IFLA_GRE_OKEY] &&
1129 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1130 return -EINVAL;
1131
1132 return 0;
1133}
1134
22a59be8 1135static int ipgre_netlink_parms(struct net_device *dev,
2e15ea39
PS
1136 struct nlattr *data[],
1137 struct nlattr *tb[],
9830ad4c
CG
1138 struct ip_tunnel_parm *parms,
1139 __u32 *fwmark)
c19e654d 1140{
22a59be8
PP
1141 struct ip_tunnel *t = netdev_priv(dev);
1142
7bb82d92 1143 memset(parms, 0, sizeof(*parms));
c19e654d
HX
1144
1145 parms->iph.protocol = IPPROTO_GRE;
1146
1147 if (!data)
22a59be8 1148 return 0;
c19e654d
HX
1149
1150 if (data[IFLA_GRE_LINK])
1151 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1152
1153 if (data[IFLA_GRE_IFLAGS])
c5441932 1154 parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS]));
c19e654d
HX
1155
1156 if (data[IFLA_GRE_OFLAGS])
c5441932 1157 parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS]));
c19e654d
HX
1158
1159 if (data[IFLA_GRE_IKEY])
1160 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1161
1162 if (data[IFLA_GRE_OKEY])
1163 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1164
1165 if (data[IFLA_GRE_LOCAL])
67b61f6c 1166 parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]);
c19e654d
HX
1167
1168 if (data[IFLA_GRE_REMOTE])
67b61f6c 1169 parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]);
c19e654d
HX
1170
1171 if (data[IFLA_GRE_TTL])
1172 parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
1173
1174 if (data[IFLA_GRE_TOS])
1175 parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]);
1176
22a59be8
PP
1177 if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC])) {
1178 if (t->ignore_df)
1179 return -EINVAL;
c19e654d 1180 parms->iph.frag_off = htons(IP_DF);
22a59be8 1181 }
2e15ea39
PS
1182
1183 if (data[IFLA_GRE_COLLECT_METADATA]) {
2e15ea39 1184 t->collect_md = true;
e271c7b4
JB
1185 if (dev->type == ARPHRD_IPGRE)
1186 dev->type = ARPHRD_NONE;
2e15ea39 1187 }
22a59be8
PP
1188
1189 if (data[IFLA_GRE_IGNORE_DF]) {
1190 if (nla_get_u8(data[IFLA_GRE_IGNORE_DF])
1191 && (parms->iph.frag_off & htons(IP_DF)))
1192 return -EINVAL;
1193 t->ignore_df = !!nla_get_u8(data[IFLA_GRE_IGNORE_DF]);
1194 }
1195
9830ad4c
CG
1196 if (data[IFLA_GRE_FWMARK])
1197 *fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
1198
84e54fe0
WT
1199 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1200 t->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1201
1202 if (t->index & ~INDEX_MASK)
1203 return -EINVAL;
1204 }
1205
22a59be8 1206 return 0;
c19e654d
HX
1207}
1208
4565e991
TH
1209/* This function returns true when ENCAP attributes are present in the nl msg */
1210static bool ipgre_netlink_encap_parms(struct nlattr *data[],
1211 struct ip_tunnel_encap *ipencap)
1212{
1213 bool ret = false;
1214
1215 memset(ipencap, 0, sizeof(*ipencap));
1216
1217 if (!data)
1218 return ret;
1219
1220 if (data[IFLA_GRE_ENCAP_TYPE]) {
1221 ret = true;
1222 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1223 }
1224
1225 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1226 ret = true;
1227 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1228 }
1229
1230 if (data[IFLA_GRE_ENCAP_SPORT]) {
1231 ret = true;
3e97fa70 1232 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
4565e991
TH
1233 }
1234
1235 if (data[IFLA_GRE_ENCAP_DPORT]) {
1236 ret = true;
3e97fa70 1237 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
4565e991
TH
1238 }
1239
1240 return ret;
1241}
1242
c5441932 1243static int gre_tap_init(struct net_device *dev)
e1a80002 1244{
c5441932 1245 __gre_tunnel_init(dev);
bec94d43 1246 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
d51711c0 1247 netif_keep_dst(dev);
e1a80002 1248
c5441932 1249 return ip_tunnel_init(dev);
e1a80002
HX
1250}
1251
c5441932
PS
1252static const struct net_device_ops gre_tap_netdev_ops = {
1253 .ndo_init = gre_tap_init,
1254 .ndo_uninit = ip_tunnel_uninit,
1255 .ndo_start_xmit = gre_tap_xmit,
b8c26a33
SH
1256 .ndo_set_mac_address = eth_mac_addr,
1257 .ndo_validate_addr = eth_validate_addr,
c5441932
PS
1258 .ndo_change_mtu = ip_tunnel_change_mtu,
1259 .ndo_get_stats64 = ip_tunnel_get_stats64,
1e99584b 1260 .ndo_get_iflink = ip_tunnel_get_iflink,
fc4099f1 1261 .ndo_fill_metadata_dst = gre_fill_metadata_dst,
b8c26a33
SH
1262};
1263
84e54fe0
WT
1264static int erspan_tunnel_init(struct net_device *dev)
1265{
1266 struct ip_tunnel *tunnel = netdev_priv(dev);
1267 int t_hlen;
1268
1269 tunnel->tun_hlen = 8;
1270 tunnel->parms.iph.protocol = IPPROTO_GRE;
c122fda2
XL
1271 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
1272 sizeof(struct erspanhdr);
1273 t_hlen = tunnel->hlen + sizeof(struct iphdr);
84e54fe0
WT
1274
1275 dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4;
1276 dev->mtu = ETH_DATA_LEN - t_hlen - 4;
1277 dev->features |= GRE_FEATURES;
1278 dev->hw_features |= GRE_FEATURES;
1279 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
c84bed44 1280 netif_keep_dst(dev);
84e54fe0
WT
1281
1282 return ip_tunnel_init(dev);
1283}
1284
1285static const struct net_device_ops erspan_netdev_ops = {
1286 .ndo_init = erspan_tunnel_init,
1287 .ndo_uninit = ip_tunnel_uninit,
1288 .ndo_start_xmit = erspan_xmit,
1289 .ndo_set_mac_address = eth_mac_addr,
1290 .ndo_validate_addr = eth_validate_addr,
1291 .ndo_change_mtu = ip_tunnel_change_mtu,
1292 .ndo_get_stats64 = ip_tunnel_get_stats64,
1293 .ndo_get_iflink = ip_tunnel_get_iflink,
1294 .ndo_fill_metadata_dst = gre_fill_metadata_dst,
1295};
1296
e1a80002
HX
1297static void ipgre_tap_setup(struct net_device *dev)
1298{
e1a80002 1299 ether_setup(dev);
d13b161c
JB
1300 dev->netdev_ops = &gre_tap_netdev_ops;
1301 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1302 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
c5441932 1303 ip_tunnel_setup(dev, gre_tap_net_id);
e1a80002
HX
1304}
1305
c5441932 1306static int ipgre_newlink(struct net *src_net, struct net_device *dev,
7a3f4a18
MS
1307 struct nlattr *tb[], struct nlattr *data[],
1308 struct netlink_ext_ack *extack)
c19e654d 1309{
c5441932 1310 struct ip_tunnel_parm p;
4565e991 1311 struct ip_tunnel_encap ipencap;
9830ad4c 1312 __u32 fwmark = 0;
22a59be8 1313 int err;
4565e991
TH
1314
1315 if (ipgre_netlink_encap_parms(data, &ipencap)) {
1316 struct ip_tunnel *t = netdev_priv(dev);
22a59be8 1317 err = ip_tunnel_encap_setup(t, &ipencap);
4565e991
TH
1318
1319 if (err < 0)
1320 return err;
1321 }
c19e654d 1322
9830ad4c 1323 err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
22a59be8
PP
1324 if (err < 0)
1325 return err;
9830ad4c 1326 return ip_tunnel_newlink(dev, tb, &p, fwmark);
c19e654d
HX
1327}
1328
1329static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
ad744b22
MS
1330 struct nlattr *data[],
1331 struct netlink_ext_ack *extack)
c19e654d 1332{
9830ad4c 1333 struct ip_tunnel *t = netdev_priv(dev);
4565e991 1334 struct ip_tunnel_encap ipencap;
9830ad4c 1335 __u32 fwmark = t->fwmark;
dd9d598c 1336 struct ip_tunnel_parm p;
22a59be8 1337 int err;
4565e991
TH
1338
1339 if (ipgre_netlink_encap_parms(data, &ipencap)) {
22a59be8 1340 err = ip_tunnel_encap_setup(t, &ipencap);
4565e991
TH
1341
1342 if (err < 0)
1343 return err;
1344 }
c19e654d 1345
9830ad4c 1346 err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
22a59be8
PP
1347 if (err < 0)
1348 return err;
dd9d598c
XL
1349
1350 err = ip_tunnel_changelink(dev, tb, &p, fwmark);
1351 if (err < 0)
1352 return err;
1353
1354 t->parms.i_flags = p.i_flags;
1355 t->parms.o_flags = p.o_flags;
1356
1357 if (strcmp(dev->rtnl_link_ops->kind, "erspan"))
1358 ipgre_link_update(dev, !tb[IFLA_MTU]);
1359
1360 return 0;
c19e654d
HX
1361}
1362
1363static size_t ipgre_get_size(const struct net_device *dev)
1364{
1365 return
1366 /* IFLA_GRE_LINK */
1367 nla_total_size(4) +
1368 /* IFLA_GRE_IFLAGS */
1369 nla_total_size(2) +
1370 /* IFLA_GRE_OFLAGS */
1371 nla_total_size(2) +
1372 /* IFLA_GRE_IKEY */
1373 nla_total_size(4) +
1374 /* IFLA_GRE_OKEY */
1375 nla_total_size(4) +
1376 /* IFLA_GRE_LOCAL */
1377 nla_total_size(4) +
1378 /* IFLA_GRE_REMOTE */
1379 nla_total_size(4) +
1380 /* IFLA_GRE_TTL */
1381 nla_total_size(1) +
1382 /* IFLA_GRE_TOS */
1383 nla_total_size(1) +
1384 /* IFLA_GRE_PMTUDISC */
1385 nla_total_size(1) +
4565e991
TH
1386 /* IFLA_GRE_ENCAP_TYPE */
1387 nla_total_size(2) +
1388 /* IFLA_GRE_ENCAP_FLAGS */
1389 nla_total_size(2) +
1390 /* IFLA_GRE_ENCAP_SPORT */
1391 nla_total_size(2) +
1392 /* IFLA_GRE_ENCAP_DPORT */
1393 nla_total_size(2) +
2e15ea39
PS
1394 /* IFLA_GRE_COLLECT_METADATA */
1395 nla_total_size(0) +
22a59be8
PP
1396 /* IFLA_GRE_IGNORE_DF */
1397 nla_total_size(1) +
9830ad4c
CG
1398 /* IFLA_GRE_FWMARK */
1399 nla_total_size(4) +
84e54fe0
WT
1400 /* IFLA_GRE_ERSPAN_INDEX */
1401 nla_total_size(4) +
c19e654d
HX
1402 0;
1403}
1404
1405static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1406{
1407 struct ip_tunnel *t = netdev_priv(dev);
1408 struct ip_tunnel_parm *p = &t->parms;
1409
f3756b79 1410 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
95f5c64c
TH
1411 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1412 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1413 nla_put_be16(skb, IFLA_GRE_OFLAGS,
1414 gre_tnl_flags_to_gre_flags(p->o_flags)) ||
f3756b79
DM
1415 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1416 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
930345ea
JB
1417 nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
1418 nla_put_in_addr(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
f3756b79
DM
1419 nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
1420 nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
1421 nla_put_u8(skb, IFLA_GRE_PMTUDISC,
9830ad4c
CG
1422 !!(p->iph.frag_off & htons(IP_DF))) ||
1423 nla_put_u32(skb, IFLA_GRE_FWMARK, t->fwmark))
f3756b79 1424 goto nla_put_failure;
4565e991
TH
1425
1426 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1427 t->encap.type) ||
3e97fa70
SD
1428 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1429 t->encap.sport) ||
1430 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1431 t->encap.dport) ||
4565e991 1432 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
e1b2cb65 1433 t->encap.flags))
4565e991
TH
1434 goto nla_put_failure;
1435
22a59be8
PP
1436 if (nla_put_u8(skb, IFLA_GRE_IGNORE_DF, t->ignore_df))
1437 goto nla_put_failure;
1438
2e15ea39
PS
1439 if (t->collect_md) {
1440 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
1441 goto nla_put_failure;
1442 }
1443
84e54fe0
WT
1444 if (t->index)
1445 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, t->index))
1446 goto nla_put_failure;
1447
c19e654d
HX
1448 return 0;
1449
1450nla_put_failure:
1451 return -EMSGSIZE;
1452}
1453
84e54fe0
WT
1454static void erspan_setup(struct net_device *dev)
1455{
1456 ether_setup(dev);
1457 dev->netdev_ops = &erspan_netdev_ops;
1458 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1459 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1460 ip_tunnel_setup(dev, erspan_net_id);
1461}
1462
c19e654d
HX
1463static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
1464 [IFLA_GRE_LINK] = { .type = NLA_U32 },
1465 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
1466 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
1467 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
1468 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
4d74f8ba
PM
1469 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1470 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
c19e654d
HX
1471 [IFLA_GRE_TTL] = { .type = NLA_U8 },
1472 [IFLA_GRE_TOS] = { .type = NLA_U8 },
1473 [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
4565e991
TH
1474 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
1475 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
1476 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
1477 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
2e15ea39 1478 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
22a59be8 1479 [IFLA_GRE_IGNORE_DF] = { .type = NLA_U8 },
9830ad4c 1480 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
84e54fe0 1481 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
c19e654d
HX
1482};
1483
1484static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
1485 .kind = "gre",
1486 .maxtype = IFLA_GRE_MAX,
1487 .policy = ipgre_policy,
1488 .priv_size = sizeof(struct ip_tunnel),
1489 .setup = ipgre_tunnel_setup,
1490 .validate = ipgre_tunnel_validate,
1491 .newlink = ipgre_newlink,
1492 .changelink = ipgre_changelink,
c5441932 1493 .dellink = ip_tunnel_dellink,
c19e654d
HX
1494 .get_size = ipgre_get_size,
1495 .fill_info = ipgre_fill_info,
1728d4fa 1496 .get_link_net = ip_tunnel_get_link_net,
c19e654d
HX
1497};
1498
e1a80002
HX
1499static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
1500 .kind = "gretap",
1501 .maxtype = IFLA_GRE_MAX,
1502 .policy = ipgre_policy,
1503 .priv_size = sizeof(struct ip_tunnel),
1504 .setup = ipgre_tap_setup,
1505 .validate = ipgre_tap_validate,
1506 .newlink = ipgre_newlink,
1507 .changelink = ipgre_changelink,
c5441932 1508 .dellink = ip_tunnel_dellink,
e1a80002
HX
1509 .get_size = ipgre_get_size,
1510 .fill_info = ipgre_fill_info,
1728d4fa 1511 .get_link_net = ip_tunnel_get_link_net,
e1a80002
HX
1512};
1513
84e54fe0
WT
1514static struct rtnl_link_ops erspan_link_ops __read_mostly = {
1515 .kind = "erspan",
1516 .maxtype = IFLA_GRE_MAX,
1517 .policy = ipgre_policy,
1518 .priv_size = sizeof(struct ip_tunnel),
1519 .setup = erspan_setup,
1520 .validate = erspan_validate,
1521 .newlink = ipgre_newlink,
1522 .changelink = ipgre_changelink,
1523 .dellink = ip_tunnel_dellink,
1524 .get_size = ipgre_get_size,
1525 .fill_info = ipgre_fill_info,
1526 .get_link_net = ip_tunnel_get_link_net,
1527};
1528
b2acd1dc
PS
1529struct net_device *gretap_fb_dev_create(struct net *net, const char *name,
1530 u8 name_assign_type)
1531{
1532 struct nlattr *tb[IFLA_MAX + 1];
1533 struct net_device *dev;
106da663 1534 LIST_HEAD(list_kill);
b2acd1dc
PS
1535 struct ip_tunnel *t;
1536 int err;
1537
1538 memset(&tb, 0, sizeof(tb));
1539
1540 dev = rtnl_create_link(net, name, name_assign_type,
1541 &ipgre_tap_ops, tb);
1542 if (IS_ERR(dev))
1543 return dev;
1544
1545 /* Configure flow based GRE device. */
1546 t = netdev_priv(dev);
1547 t->collect_md = true;
1548
7a3f4a18 1549 err = ipgre_newlink(net, dev, tb, NULL, NULL);
106da663
ND
1550 if (err < 0) {
1551 free_netdev(dev);
1552 return ERR_PTR(err);
1553 }
7e059158
DW
1554
1555 /* openvswitch users expect packet sizes to be unrestricted,
1556 * so set the largest MTU we can.
1557 */
1558 err = __ip_tunnel_change_mtu(dev, IP_MAX_MTU, false);
1559 if (err)
1560 goto out;
1561
da6f1da8
ND
1562 err = rtnl_configure_link(dev, NULL);
1563 if (err < 0)
1564 goto out;
1565
b2acd1dc
PS
1566 return dev;
1567out:
106da663
ND
1568 ip_tunnel_dellink(dev, &list_kill);
1569 unregister_netdevice_many(&list_kill);
b2acd1dc
PS
1570 return ERR_PTR(err);
1571}
1572EXPORT_SYMBOL_GPL(gretap_fb_dev_create);
1573
c5441932
PS
1574static int __net_init ipgre_tap_init_net(struct net *net)
1575{
2e15ea39 1576 return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, "gretap0");
c5441932
PS
1577}
1578
64bc1781 1579static void __net_exit ipgre_tap_exit_batch_net(struct list_head *list_net)
c5441932 1580{
64bc1781 1581 ip_tunnel_delete_nets(list_net, gre_tap_net_id, &ipgre_tap_ops);
c5441932
PS
1582}
1583
1584static struct pernet_operations ipgre_tap_net_ops = {
1585 .init = ipgre_tap_init_net,
64bc1781 1586 .exit_batch = ipgre_tap_exit_batch_net,
c5441932
PS
1587 .id = &gre_tap_net_id,
1588 .size = sizeof(struct ip_tunnel_net),
1589};
1da177e4 1590
84e54fe0
WT
1591static int __net_init erspan_init_net(struct net *net)
1592{
1593 return ip_tunnel_init_net(net, erspan_net_id,
1594 &erspan_link_ops, "erspan0");
1595}
1596
64bc1781 1597static void __net_exit erspan_exit_batch_net(struct list_head *net_list)
84e54fe0 1598{
64bc1781 1599 ip_tunnel_delete_nets(net_list, erspan_net_id, &erspan_link_ops);
84e54fe0
WT
1600}
1601
1602static struct pernet_operations erspan_net_ops = {
1603 .init = erspan_init_net,
64bc1781 1604 .exit_batch = erspan_exit_batch_net,
84e54fe0
WT
1605 .id = &erspan_net_id,
1606 .size = sizeof(struct ip_tunnel_net),
1607};
1608
1da177e4
LT
1609static int __init ipgre_init(void)
1610{
1611 int err;
1612
058bd4d2 1613 pr_info("GRE over IPv4 tunneling driver\n");
1da177e4 1614
cfb8fbf2 1615 err = register_pernet_device(&ipgre_net_ops);
59a4c759 1616 if (err < 0)
c2892f02
AD
1617 return err;
1618
c5441932
PS
1619 err = register_pernet_device(&ipgre_tap_net_ops);
1620 if (err < 0)
e3d0328c 1621 goto pnet_tap_failed;
c5441932 1622
84e54fe0
WT
1623 err = register_pernet_device(&erspan_net_ops);
1624 if (err < 0)
1625 goto pnet_erspan_failed;
1626
9f57c67c 1627 err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
c2892f02 1628 if (err < 0) {
058bd4d2 1629 pr_info("%s: can't add protocol\n", __func__);
c2892f02
AD
1630 goto add_proto_failed;
1631 }
7daa0004 1632
c19e654d
HX
1633 err = rtnl_link_register(&ipgre_link_ops);
1634 if (err < 0)
1635 goto rtnl_link_failed;
1636
e1a80002
HX
1637 err = rtnl_link_register(&ipgre_tap_ops);
1638 if (err < 0)
1639 goto tap_ops_failed;
1640
84e54fe0
WT
1641 err = rtnl_link_register(&erspan_link_ops);
1642 if (err < 0)
1643 goto erspan_link_failed;
1644
c5441932 1645 return 0;
c19e654d 1646
84e54fe0
WT
1647erspan_link_failed:
1648 rtnl_link_unregister(&ipgre_tap_ops);
e1a80002
HX
1649tap_ops_failed:
1650 rtnl_link_unregister(&ipgre_link_ops);
c19e654d 1651rtnl_link_failed:
9f57c67c 1652 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
c2892f02 1653add_proto_failed:
84e54fe0
WT
1654 unregister_pernet_device(&erspan_net_ops);
1655pnet_erspan_failed:
c5441932 1656 unregister_pernet_device(&ipgre_tap_net_ops);
e3d0328c 1657pnet_tap_failed:
c2892f02 1658 unregister_pernet_device(&ipgre_net_ops);
c5441932 1659 return err;
1da177e4
LT
1660}
1661
db44575f 1662static void __exit ipgre_fini(void)
1da177e4 1663{
e1a80002 1664 rtnl_link_unregister(&ipgre_tap_ops);
c19e654d 1665 rtnl_link_unregister(&ipgre_link_ops);
84e54fe0 1666 rtnl_link_unregister(&erspan_link_ops);
9f57c67c 1667 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
c5441932 1668 unregister_pernet_device(&ipgre_tap_net_ops);
c2892f02 1669 unregister_pernet_device(&ipgre_net_ops);
84e54fe0 1670 unregister_pernet_device(&erspan_net_ops);
1da177e4
LT
1671}
1672
1673module_init(ipgre_init);
1674module_exit(ipgre_fini);
1675MODULE_LICENSE("GPL");
4d74f8ba
PM
1676MODULE_ALIAS_RTNL_LINK("gre");
1677MODULE_ALIAS_RTNL_LINK("gretap");
84e54fe0 1678MODULE_ALIAS_RTNL_LINK("erspan");
8909c9ad 1679MODULE_ALIAS_NETDEV("gre0");
c5441932 1680MODULE_ALIAS_NETDEV("gretap0");
84e54fe0 1681MODULE_ALIAS_NETDEV("erspan0");