blob: 8b837f6f553201da37e3909fb0d5fd82748ee34a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002 * Linux NET3: GRE over IP protocol decoder.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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
Joe Perchesafd465032012-03-12 07:03:32 +000013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Randy Dunlap4fc268d2006-01-11 12:17:47 -080015#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080020#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
Pravin B Shelar2e15ea32015-08-07 23:51:42 -070027#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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>
Herbert Xue1a80002008-10-09 12:00:17 -070033#include <linux/etherdevice.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080034#include <linux/if_ether.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <net/sock.h>
37#include <net/ip.h>
38#include <net/icmp.h>
39#include <net/protocol.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000040#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#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>
Pavel Emelyanov59a4c752008-04-16 01:08:53 -070046#include <net/net_namespace.h>
47#include <net/netns/generic.h>
Herbert Xuc19e6542008-10-09 11:59:55 -070048#include <net/rtnetlink.h>
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070049#include <net/gre.h>
Pravin B Shelar2e15ea32015-08-07 23:51:42 -070050#include <net/dst_metadata.h>
William Tu84e54fe2017-08-22 09:40:28 -070051#include <net/erspan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
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),
Eric Dumazet6d0722a2010-09-29 23:35:10 -070065 and silently drop packet when it expires. It is a good
stephen hemmingerbff52852012-02-24 08:08:20 +000066 solution, but it supposes maintaining new variable in ALL
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 skb, even if no tunneling is used.
68
Eric Dumazet6d0722a2010-09-29 23:35:10 -070069 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
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,
stephen hemmingerbff52852012-02-24 08:08:20 +000092 taking into account fragmentation. TO be short, ttl is not solution at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
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
stephen hemmingerbff52852012-02-24 08:08:20 +000099 rapidly degrades to value <68, where looping stops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 Alexey Kuznetsov.
108 */
109
stephen hemmingereccc1bb2012-09-25 11:02:48 +0000110static 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
Herbert Xuc19e6542008-10-09 11:59:55 -0700114static struct rtnl_link_ops ipgre_link_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static int ipgre_tunnel_init(struct net_device *dev);
William Tu1a66a832017-08-25 09:21:28 -0700116static void erspan_build_header(struct sk_buff *skb,
117 __be32 id, u32 index, bool truncate);
Pavel Emelyanoveb8ce742008-04-16 01:10:26 -0700118
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300119static unsigned int ipgre_net_id __read_mostly;
120static unsigned int gre_tap_net_id __read_mostly;
William Tu84e54fe2017-08-22 09:40:28 -0700121static unsigned int erspan_net_id __read_mostly;
Pavel Emelyanoveb8ce742008-04-16 01:10:26 -0700122
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700123static void ipgre_err(struct sk_buff *skb, u32 info,
124 const struct tnl_ptk_info *tpi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Pravin B Shelarc5441932013-03-25 14:49:35 +0000127 /* 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Pravin B Shelarc5441932013-03-25 14:49:35 +0000131 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".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Pravin B Shelarc5441932013-03-25 14:49:35 +0000136 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;
Eric Dumazet96f5a842013-05-18 08:36:03 +0000142 const struct iphdr *iph;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300143 const int type = icmp_hdr(skb)->type;
144 const int code = icmp_hdr(skb)->code;
Eric Dumazet20e19542016-06-18 21:52:06 -0700145 unsigned int data_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct ip_tunnel *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 switch (type) {
149 default:
150 case ICMP_PARAMETERPROB:
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700151 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 case ICMP_DEST_UNREACH:
154 switch (code) {
155 case ICMP_SR_FAILED:
156 case ICMP_PORT_UNREACH:
157 /* Impossible event. */
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700158 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 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;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 case ICMP_TIME_EXCEEDED:
169 if (code != ICMP_EXC_TTL)
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700170 return;
Eric Dumazet20e19542016-06-18 21:52:06 -0700171 data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 break;
David S. Miller55be7a92012-07-11 21:27:49 -0700173
174 case ICMP_REDIRECT:
175 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177
Pravin B Shelarbda7bb42013-06-17 17:49:38 -0700178 if (tpi->proto == htons(ETH_P_TEB))
Pravin B Shelarc5441932013-03-25 14:49:35 +0000179 itn = net_generic(net, gre_tap_net_id);
180 else
181 itn = net_generic(net, ipgre_net_id);
182
Duan Jiongc0c0c502014-01-28 11:49:43 +0800183 iph = (const struct iphdr *)(icmp_hdr(skb) + 1);
Pravin B Shelarbda7bb42013-06-17 17:49:38 -0700184 t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
185 iph->daddr, iph->saddr, tpi->key);
stephen hemmingerd2083282012-09-24 18:12:23 +0000186
Ian Morris51456b22015-04-03 09:17:26 +0100187 if (!t)
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700188 return;
David S. Miller36393392012-06-14 22:21:46 -0700189
Eric Dumazet9b8c6d72016-06-18 21:52:05 -0700190#if IS_ENABLED(CONFIG_IPV6)
191 if (tpi->proto == htons(ETH_P_IPV6) &&
Eric Dumazet20e19542016-06-18 21:52:06 -0700192 !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4 + tpi->hdr_len,
193 type, data_len))
Eric Dumazet9b8c6d72016-06-18 21:52:05 -0700194 return;
195#endif
196
David S. Miller36393392012-06-14 22:21:46 -0700197 if (t->parms.iph.daddr == 0 ||
Joe Perchesf97c1e02007-12-16 13:45:43 -0800198 ipv4_is_multicast(t->parms.iph.daddr))
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700199 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700202 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Wei Yongjunda6185d82009-02-24 23:34:48 -0800204 if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 t->err_count++;
206 else
207 t->err_count = 1;
208 t->err_time = jiffies;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700209}
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
Eric Dumazete582615ad2016-06-15 06:24:00 -0700227 const struct iphdr *iph = (struct iphdr *)skb->data;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700228 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
Eric Dumazete582615ad2016-06-15 06:24:00 -0700233 if (gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP),
234 iph->ihl * 4) < 0) {
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700235 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
William Tu84e54fe2017-08-22 09:40:28 -0700253static 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;
262 __be32 session_id;
263 __be32 index;
264 int len;
265
266 itn = net_generic(net, erspan_net_id);
William Tu84e54fe2017-08-22 09:40:28 -0700267 len = gre_hdr_len + sizeof(*ershdr);
268
269 if (unlikely(!pskb_may_pull(skb, len)))
270 return -ENOMEM;
271
272 iph = ip_hdr(skb);
273 ershdr = (struct erspanhdr *)(skb->data + gre_hdr_len);
274
275 /* The original GRE header does not have key field,
276 * Use ERSPAN 10-bit session ID as key.
277 */
278 session_id = cpu_to_be32(ntohs(ershdr->session_id));
279 tpi->key = session_id;
280 index = ershdr->md.index;
281 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex,
282 tpi->flags | TUNNEL_KEY,
283 iph->saddr, iph->daddr, tpi->key);
284
285 if (tunnel) {
286 if (__iptunnel_pull_header(skb,
287 gre_hdr_len + sizeof(*ershdr),
288 htons(ETH_P_TEB),
289 false, false) < 0)
290 goto drop;
291
William Tu1a66a832017-08-25 09:21:28 -0700292 if (tunnel->collect_md) {
293 struct ip_tunnel_info *info;
294 struct erspan_metadata *md;
295 __be64 tun_id;
296 __be16 flags;
297
298 tpi->flags |= TUNNEL_KEY;
299 flags = tpi->flags;
300 tun_id = key32_to_tunnel_id(tpi->key);
301
302 tun_dst = ip_tun_rx_dst(skb, flags,
303 tun_id, sizeof(*md));
304 if (!tun_dst)
305 return PACKET_REJECT;
306
307 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
308 if (!md)
309 return PACKET_REJECT;
310
311 md->index = index;
312 info = &tun_dst->u.tun_info;
313 info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
314 info->options_len = sizeof(*md);
315 } else {
316 tunnel->index = ntohl(index);
317 }
318
William Tu84e54fe2017-08-22 09:40:28 -0700319 skb_reset_mac_header(skb);
320 ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
321 return PACKET_RCVD;
322 }
323drop:
324 kfree_skb(skb);
325 return PACKET_RCVD;
326}
327
Jiri Benc125372f2016-05-03 17:10:08 +0200328static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
329 struct ip_tunnel_net *itn, int hdr_len, bool raw_proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700331 struct metadata_dst *tun_dst = NULL;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000332 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct ip_tunnel *tunnel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700335 iph = ip_hdr(skb);
Pravin B Shelarbda7bb42013-06-17 17:49:38 -0700336 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
337 iph->saddr, iph->daddr, tpi->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
stephen hemmingerd2083282012-09-24 18:12:23 +0000339 if (tunnel) {
Jiri Benc125372f2016-05-03 17:10:08 +0200340 if (__iptunnel_pull_header(skb, hdr_len, tpi->proto,
341 raw_proto, false) < 0)
Jiri Benc244a7972016-05-03 17:10:07 +0200342 goto drop;
343
Jiri Bence271c7b2016-05-11 15:53:57 +0200344 if (tunnel->dev->type != ARPHRD_NONE)
345 skb_pop_mac_header(skb);
346 else
347 skb_reset_mac_header(skb);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700348 if (tunnel->collect_md) {
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700349 __be16 flags;
350 __be64 tun_id;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700351
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700352 flags = tpi->flags & (TUNNEL_CSUM | TUNNEL_KEY);
Amir Vadaid817f432016-09-08 16:23:45 +0300353 tun_id = key32_to_tunnel_id(tpi->key);
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700354 tun_dst = ip_tun_rx_dst(skb, flags, tun_id, 0);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700355 if (!tun_dst)
356 return PACKET_REJECT;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700357 }
358
359 ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
Pravin B Shelarbda7bb42013-06-17 17:49:38 -0700360 return PACKET_RCVD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Jiri Benc125372f2016-05-03 17:10:08 +0200362 return PACKET_NEXT;
Jiri Benc244a7972016-05-03 17:10:07 +0200363
364drop:
365 kfree_skb(skb);
366 return PACKET_RCVD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Jiri Benc125372f2016-05-03 17:10:08 +0200369static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
370 int hdr_len)
371{
372 struct net *net = dev_net(skb->dev);
373 struct ip_tunnel_net *itn;
374 int res;
375
376 if (tpi->proto == htons(ETH_P_TEB))
377 itn = net_generic(net, gre_tap_net_id);
378 else
379 itn = net_generic(net, ipgre_net_id);
380
381 res = __ipgre_rcv(skb, tpi, itn, hdr_len, false);
382 if (res == PACKET_NEXT && tpi->proto == htons(ETH_P_TEB)) {
383 /* ipgre tunnels in collect metadata mode should receive
384 * also ETH_P_TEB traffic.
385 */
386 itn = net_generic(net, ipgre_net_id);
387 res = __ipgre_rcv(skb, tpi, itn, hdr_len, true);
388 }
389 return res;
390}
391
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700392static int gre_rcv(struct sk_buff *skb)
393{
394 struct tnl_ptk_info tpi;
395 bool csum_err = false;
Tom Herbert95f5c642016-04-29 17:12:16 -0700396 int hdr_len;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700397
398#ifdef CONFIG_NET_IPGRE_BROADCAST
399 if (ipv4_is_multicast(ip_hdr(skb)->daddr)) {
400 /* Looped back packet, drop it! */
401 if (rt_is_output_route(skb_rtable(skb)))
402 goto drop;
403 }
404#endif
405
Eric Dumazete582615ad2016-06-15 06:24:00 -0700406 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP), 0);
Jiri Bencf132ae72016-05-03 15:00:21 +0200407 if (hdr_len < 0)
Tom Herbert95f5c642016-04-29 17:12:16 -0700408 goto drop;
409
William Tu84e54fe2017-08-22 09:40:28 -0700410 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN))) {
411 if (erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
412 return 0;
413 }
414
Jiri Benc244a7972016-05-03 17:10:07 +0200415 if (ipgre_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700416 return 0;
417
418 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
419drop:
420 kfree_skb(skb);
421 return 0;
422}
423
Pravin B Shelarc5441932013-03-25 14:49:35 +0000424static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
425 const struct iphdr *tnl_params,
426 __be16 proto)
427{
428 struct ip_tunnel *tunnel = netdev_priv(dev);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000429
Pravin B Shelarc5441932013-03-25 14:49:35 +0000430 if (tunnel->parms.o_flags & TUNNEL_SEQ)
431 tunnel->o_seqno++;
Eric Dumazetcef401d2013-01-25 20:34:37 +0000432
Pravin B Shelarc5441932013-03-25 14:49:35 +0000433 /* Push GRE header. */
Tom Herbert182a3522016-04-29 17:12:19 -0700434 gre_build_header(skb, tunnel->tun_hlen,
435 tunnel->parms.o_flags, proto, tunnel->parms.o_key,
436 htonl(tunnel->o_seqno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Nicolas Dichtelbf3d6a82013-05-27 23:48:15 +0000438 ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Alexander Duyckaed069d2016-04-14 15:33:37 -0400441static int gre_handle_offloads(struct sk_buff *skb, bool csum)
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -0700442{
Edward Cree6fa79662016-02-11 21:02:31 +0000443 return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -0700444}
445
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700446static struct rtable *gre_get_rt(struct sk_buff *skb,
447 struct net_device *dev,
448 struct flowi4 *fl,
449 const struct ip_tunnel_key *key)
450{
451 struct net *net = dev_net(dev);
452
453 memset(fl, 0, sizeof(*fl));
454 fl->daddr = key->u.ipv4.dst;
455 fl->saddr = key->u.ipv4.src;
456 fl->flowi4_tos = RT_TOS(key->tos);
457 fl->flowi4_mark = skb->mark;
458 fl->flowi4_proto = IPPROTO_GRE;
459
460 return ip_route_output_key(net, fl);
461}
462
William Tu862a03c2017-08-25 09:21:27 -0700463static struct rtable *prepare_fb_xmit(struct sk_buff *skb,
464 struct net_device *dev,
465 struct flowi4 *fl,
466 int tunnel_hlen)
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700467{
468 struct ip_tunnel_info *tun_info;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700469 const struct ip_tunnel_key *key;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100470 struct rtable *rt = NULL;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700471 int min_headroom;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100472 bool use_cache;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700473 int err;
474
Jiri Benc61adedf2015-08-20 13:56:25 +0200475 tun_info = skb_tunnel_info(skb);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700476 key = &tun_info->key;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100477 use_cache = ip_tunnel_dst_cache_usable(skb, tun_info);
William Tu862a03c2017-08-25 09:21:27 -0700478
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100479 if (use_cache)
William Tu862a03c2017-08-25 09:21:27 -0700480 rt = dst_cache_get_ip4(&tun_info->dst_cache, &fl->saddr);
Paolo Abeni3c1cb4d22016-02-12 15:43:59 +0100481 if (!rt) {
William Tu862a03c2017-08-25 09:21:27 -0700482 rt = gre_get_rt(skb, dev, fl, key);
Paolo Abeni3c1cb4d22016-02-12 15:43:59 +0100483 if (IS_ERR(rt))
William Tu862a03c2017-08-25 09:21:27 -0700484 goto err_free_skb;
Daniel Borkmanndb3c6132016-03-04 15:15:07 +0100485 if (use_cache)
Paolo Abeni3c1cb4d22016-02-12 15:43:59 +0100486 dst_cache_set_ip4(&tun_info->dst_cache, &rt->dst,
William Tu862a03c2017-08-25 09:21:27 -0700487 fl->saddr);
Paolo Abeni3c1cb4d22016-02-12 15:43:59 +0100488 }
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700489
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700490 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
491 + tunnel_hlen + sizeof(struct iphdr);
492 if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
493 int head_delta = SKB_DATA_ALIGN(min_headroom -
494 skb_headroom(skb) +
495 16);
496 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
497 0, GFP_ATOMIC);
498 if (unlikely(err))
499 goto err_free_rt;
500 }
William Tu862a03c2017-08-25 09:21:27 -0700501 return rt;
502
503err_free_rt:
504 ip_rt_put(rt);
505err_free_skb:
506 kfree_skb(skb);
507 dev->stats.tx_dropped++;
508 return NULL;
509}
510
511static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev,
512 __be16 proto)
513{
514 struct ip_tunnel_info *tun_info;
515 const struct ip_tunnel_key *key;
516 struct rtable *rt = NULL;
517 struct flowi4 fl;
518 int tunnel_hlen;
519 __be16 df, flags;
520
521 tun_info = skb_tunnel_info(skb);
522 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
523 ip_tunnel_info_af(tun_info) != AF_INET))
524 goto err_free_skb;
525
526 key = &tun_info->key;
527 tunnel_hlen = gre_calc_hlen(key->tun_flags);
528
529 rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen);
530 if (!rt)
531 return;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700532
533 /* Push Tunnel header. */
Alexander Duyckaed069d2016-04-14 15:33:37 -0400534 if (gre_handle_offloads(skb, !!(tun_info->key.tun_flags & TUNNEL_CSUM)))
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700535 goto err_free_rt;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700536
537 flags = tun_info->key.tun_flags & (TUNNEL_CSUM | TUNNEL_KEY);
David S. Millercba653212016-05-04 00:52:29 -0400538 gre_build_header(skb, tunnel_hlen, flags, proto,
Amir Vadaid817f432016-09-08 16:23:45 +0300539 tunnel_id_to_key32(tun_info->key.tun_id), 0);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700540
541 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
Pravin B Shelar039f5062015-12-24 14:34:54 -0800542
543 iptunnel_xmit(skb->sk, rt, skb, fl.saddr, key->u.ipv4.dst, IPPROTO_GRE,
544 key->tos, key->ttl, df, false);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700545 return;
546
547err_free_rt:
548 ip_rt_put(rt);
549err_free_skb:
550 kfree_skb(skb);
551 dev->stats.tx_dropped++;
552}
553
William Tu1a66a832017-08-25 09:21:28 -0700554static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev,
555 __be16 proto)
556{
557 struct ip_tunnel *tunnel = netdev_priv(dev);
558 struct ip_tunnel_info *tun_info;
559 const struct ip_tunnel_key *key;
560 struct erspan_metadata *md;
561 struct rtable *rt = NULL;
562 bool truncate = false;
563 struct flowi4 fl;
564 int tunnel_hlen;
565 __be16 df;
566
567 tun_info = skb_tunnel_info(skb);
568 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
569 ip_tunnel_info_af(tun_info) != AF_INET))
570 goto err_free_skb;
571
572 key = &tun_info->key;
573
574 /* ERSPAN has fixed 8 byte GRE header */
575 tunnel_hlen = 8 + sizeof(struct erspanhdr);
576
577 rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen);
578 if (!rt)
579 return;
580
581 if (gre_handle_offloads(skb, false))
582 goto err_free_rt;
583
584 if (skb->len > dev->mtu) {
585 pskb_trim(skb, dev->mtu);
586 truncate = true;
587 }
588
589 md = ip_tunnel_info_opts(tun_info);
590 if (!md)
591 goto err_free_rt;
592
593 erspan_build_header(skb, tunnel_id_to_key32(key->tun_id),
594 ntohl(md->index), truncate);
595
596 gre_build_header(skb, 8, TUNNEL_SEQ,
597 htons(ETH_P_ERSPAN), 0, htonl(tunnel->o_seqno++));
598
599 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
600
601 iptunnel_xmit(skb->sk, rt, skb, fl.saddr, key->u.ipv4.dst, IPPROTO_GRE,
602 key->tos, key->ttl, df, false);
603 return;
604
605err_free_rt:
606 ip_rt_put(rt);
607err_free_skb:
608 kfree_skb(skb);
609 dev->stats.tx_dropped++;
610}
611
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700612static int gre_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
613{
614 struct ip_tunnel_info *info = skb_tunnel_info(skb);
615 struct rtable *rt;
616 struct flowi4 fl4;
617
618 if (ip_tunnel_info_af(info) != AF_INET)
619 return -EINVAL;
620
621 rt = gre_get_rt(skb, dev, &fl4, &info->key);
622 if (IS_ERR(rt))
623 return PTR_ERR(rt);
624
625 ip_rt_put(rt);
626 info->key.u.ipv4.src = fl4.saddr;
627 return 0;
628}
629
Pravin B Shelarc5441932013-03-25 14:49:35 +0000630static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
631 struct net_device *dev)
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800632{
Pravin B Shelarc5441932013-03-25 14:49:35 +0000633 struct ip_tunnel *tunnel = netdev_priv(dev);
634 const struct iphdr *tnl_params;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800635
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700636 if (tunnel->collect_md) {
Jiri Benc20907142016-04-27 11:29:07 +0200637 gre_fb_xmit(skb, dev, skb->protocol);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700638 return NETDEV_TX_OK;
639 }
640
Pravin B Shelarc5441932013-03-25 14:49:35 +0000641 if (dev->header_ops) {
642 /* Need space for new headers */
643 if (skb_cow_head(skb, dev->needed_headroom -
Chen Gang2bac7cb2013-04-22 20:45:42 +0000644 (tunnel->hlen + sizeof(struct iphdr))))
Pravin B Shelarc5441932013-03-25 14:49:35 +0000645 goto free_skb;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800646
Pravin B Shelarc5441932013-03-25 14:49:35 +0000647 tnl_params = (const struct iphdr *)skb->data;
Eric Dumazete985aad2010-09-27 03:57:11 +0000648
Pravin B Shelarc5441932013-03-25 14:49:35 +0000649 /* Pull skb since ip_tunnel_xmit() needs skb->data pointing
650 * to gre header.
651 */
652 skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
Timo Teräs8a0033a2014-12-15 09:24:13 +0200653 skb_reset_mac_header(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000654 } else {
655 if (skb_cow_head(skb, dev->needed_headroom))
656 goto free_skb;
Herbert Xue1a80002008-10-09 12:00:17 -0700657
Pravin B Shelarc5441932013-03-25 14:49:35 +0000658 tnl_params = &tunnel->parms.iph;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800659 }
660
Alexander Duyckaed069d2016-04-14 15:33:37 -0400661 if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
662 goto free_skb;
Timo Teräs8a0033a2014-12-15 09:24:13 +0200663
Pravin B Shelarc5441932013-03-25 14:49:35 +0000664 __gre_xmit(skb, dev, tnl_params, skb->protocol);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000665 return NETDEV_TX_OK;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800666
Pravin B Shelarc5441932013-03-25 14:49:35 +0000667free_skb:
Eric Dumazet3acfa1e2014-01-18 18:27:49 -0800668 kfree_skb(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000669 dev->stats.tx_dropped++;
670 return NETDEV_TX_OK;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800671}
672
William Tu84e54fe2017-08-22 09:40:28 -0700673static inline u8 tos_to_cos(u8 tos)
674{
675 u8 dscp, cos;
676
677 dscp = tos >> 2;
678 cos = dscp >> 3;
679 return cos;
680}
681
682static void erspan_build_header(struct sk_buff *skb,
683 __be32 id, u32 index, bool truncate)
684{
685 struct iphdr *iphdr = ip_hdr(skb);
686 struct ethhdr *eth = eth_hdr(skb);
687 enum erspan_encap_type enc_type;
688 struct erspanhdr *ershdr;
689 struct qtag_prefix {
690 __be16 eth_type;
691 __be16 tci;
692 } *qp;
693 u16 vlan_tci = 0;
694
695 enc_type = ERSPAN_ENCAP_NOVLAN;
696
697 /* If mirrored packet has vlan tag, extract tci and
698 * perserve vlan header in the mirrored frame.
699 */
700 if (eth->h_proto == htons(ETH_P_8021Q)) {
701 qp = (struct qtag_prefix *)(skb->data + 2 * ETH_ALEN);
702 vlan_tci = ntohs(qp->tci);
703 enc_type = ERSPAN_ENCAP_INFRAME;
704 }
705
706 skb_push(skb, sizeof(*ershdr));
707 ershdr = (struct erspanhdr *)skb->data;
708 memset(ershdr, 0, sizeof(*ershdr));
709
710 ershdr->ver_vlan = htons((vlan_tci & VLAN_MASK) |
711 (ERSPAN_VERSION << VER_OFFSET));
712 ershdr->session_id = htons((u16)(ntohl(id) & ID_MASK) |
713 ((tos_to_cos(iphdr->tos) << COS_OFFSET) & COS_MASK) |
714 (enc_type << EN_OFFSET & EN_MASK) |
715 ((truncate << T_OFFSET) & T_MASK));
716 ershdr->md.index = htonl(index & INDEX_MASK);
717}
718
719static netdev_tx_t erspan_xmit(struct sk_buff *skb,
720 struct net_device *dev)
721{
722 struct ip_tunnel *tunnel = netdev_priv(dev);
723 bool truncate = false;
724
William Tu1a66a832017-08-25 09:21:28 -0700725 if (tunnel->collect_md) {
726 erspan_fb_xmit(skb, dev, skb->protocol);
727 return NETDEV_TX_OK;
728 }
729
William Tu84e54fe2017-08-22 09:40:28 -0700730 if (gre_handle_offloads(skb, false))
731 goto free_skb;
732
733 if (skb_cow_head(skb, dev->needed_headroom))
734 goto free_skb;
735
736 if (skb->len > dev->mtu) {
737 pskb_trim(skb, dev->mtu);
738 truncate = true;
739 }
740
741 /* Push ERSPAN header */
742 erspan_build_header(skb, tunnel->parms.o_key, tunnel->index, truncate);
743 tunnel->parms.o_flags &= ~TUNNEL_KEY;
744 __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_ERSPAN));
745 return NETDEV_TX_OK;
746
747free_skb:
748 kfree_skb(skb);
749 dev->stats.tx_dropped++;
750 return NETDEV_TX_OK;
751}
752
Pravin B Shelarc5441932013-03-25 14:49:35 +0000753static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
754 struct net_device *dev)
755{
756 struct ip_tunnel *tunnel = netdev_priv(dev);
757
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700758 if (tunnel->collect_md) {
Jiri Benc20907142016-04-27 11:29:07 +0200759 gre_fb_xmit(skb, dev, htons(ETH_P_TEB));
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700760 return NETDEV_TX_OK;
761 }
762
Alexander Duyckaed069d2016-04-14 15:33:37 -0400763 if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
764 goto free_skb;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000765
766 if (skb_cow_head(skb, dev->needed_headroom))
767 goto free_skb;
768
769 __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB));
Pravin B Shelarc5441932013-03-25 14:49:35 +0000770 return NETDEV_TX_OK;
771
772free_skb:
Eric Dumazet3acfa1e2014-01-18 18:27:49 -0800773 kfree_skb(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000774 dev->stats.tx_dropped++;
775 return NETDEV_TX_OK;
776}
777
778static int ipgre_tunnel_ioctl(struct net_device *dev,
779 struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Tom Herbert4565e992014-09-17 12:26:01 -0700781 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Pravin B Shelarc5441932013-03-25 14:49:35 +0000784 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
785 return -EFAULT;
Cong Wang6c734fb2013-06-29 12:02:59 +0800786 if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
787 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||
788 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)) ||
789 ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING)))
790 return -EINVAL;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000791 }
792 p.i_flags = gre_flags_to_tnl_flags(p.i_flags);
793 p.o_flags = gre_flags_to_tnl_flags(p.o_flags);
794
795 err = ip_tunnel_ioctl(dev, &p, cmd);
796 if (err)
797 return err;
798
Tom Herbert95f5c642016-04-29 17:12:16 -0700799 p.i_flags = gre_tnl_flags_to_gre_flags(p.i_flags);
800 p.o_flags = gre_tnl_flags_to_gre_flags(p.o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000801
802 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
803 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return 0;
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807/* Nice toy. Unfortunately, useless in real life :-)
808 It allows to construct virtual multiprotocol broadcast "LAN"
809 over the Internet, provided multicast routing is tuned.
810
811
812 I have no idea was this bicycle invented before me,
813 so that I had to set ARPHRD_IPGRE to a random value.
814 I have an impression, that Cisco could make something similar,
815 but this feature is apparently missing in IOS<=11.2(8).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
818 with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
819
820 ping -t 255 224.66.66.66
821
822 If nobody answers, mbone does not work.
823
824 ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
825 ip addr add 10.66.66.<somewhat>/24 dev Universe
826 ifconfig Universe up
827 ifconfig Universe add fe80::<Your_real_addr>/10
828 ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
829 ftp 10.66.66.66
830 ...
831 ftp fec0:6666:6666::193.233.7.65
832 ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700834static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
835 unsigned short type,
Eric Dumazet15078502010-09-15 11:07:53 +0000836 const void *daddr, const void *saddr, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Patrick McHardy2941a482006-01-08 22:05:26 -0800838 struct ip_tunnel *t = netdev_priv(dev);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000839 struct iphdr *iph;
840 struct gre_base_hdr *greh;
841
Johannes Bergd58ff352017-06-16 14:29:23 +0200842 iph = skb_push(skb, t->hlen + sizeof(*iph));
Pravin B Shelarc5441932013-03-25 14:49:35 +0000843 greh = (struct gre_base_hdr *)(iph+1);
Tom Herbert95f5c642016-04-29 17:12:16 -0700844 greh->flags = gre_tnl_flags_to_gre_flags(t->parms.o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000845 greh->protocol = htons(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Pravin B Shelarc5441932013-03-25 14:49:35 +0000849 /* Set the source hardware address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (saddr)
851 memcpy(&iph->saddr, saddr, 4);
Timo Teräs6d55cb92010-03-03 04:01:13 +0000852 if (daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 memcpy(&iph->daddr, daddr, 4);
Timo Teräs6d55cb92010-03-03 04:01:13 +0000854 if (iph->daddr)
Timo Teräs77a482b2013-08-06 13:45:43 +0300855 return t->hlen + sizeof(*iph);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900856
Pravin B Shelarc5441932013-03-25 14:49:35 +0000857 return -(t->hlen + sizeof(*iph));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Timo Teras6a5f44d2007-10-23 20:31:53 -0700860static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
861{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000862 const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb);
Timo Teras6a5f44d2007-10-23 20:31:53 -0700863 memcpy(haddr, &iph->saddr, 4);
864 return 4;
865}
866
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700867static const struct header_ops ipgre_header_ops = {
868 .create = ipgre_header,
Timo Teras6a5f44d2007-10-23 20:31:53 -0700869 .parse = ipgre_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700870};
871
Timo Teras6a5f44d2007-10-23 20:31:53 -0700872#ifdef CONFIG_NET_IPGRE_BROADCAST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873static int ipgre_open(struct net_device *dev)
874{
Patrick McHardy2941a482006-01-08 22:05:26 -0800875 struct ip_tunnel *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Joe Perchesf97c1e02007-12-16 13:45:43 -0800877 if (ipv4_is_multicast(t->parms.iph.daddr)) {
David S. Millercbb1e852011-05-04 12:33:34 -0700878 struct flowi4 fl4;
879 struct rtable *rt;
Eric Dumazete985aad2010-09-27 03:57:11 +0000880
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200881 rt = ip_route_output_gre(t->net, &fl4,
David S. Millercbb1e852011-05-04 12:33:34 -0700882 t->parms.iph.daddr,
883 t->parms.iph.saddr,
884 t->parms.o_key,
885 RT_TOS(t->parms.iph.tos),
886 t->parms.link);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800887 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -EADDRNOTAVAIL;
Changli Gaod8d1f302010-06-10 23:31:35 -0700889 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ip_rt_put(rt);
Ian Morris51456b22015-04-03 09:17:26 +0100891 if (!__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -EADDRNOTAVAIL;
893 t->mlink = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -0700894 ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 return 0;
897}
898
899static int ipgre_close(struct net_device *dev)
900{
Patrick McHardy2941a482006-01-08 22:05:26 -0800901 struct ip_tunnel *t = netdev_priv(dev);
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800902
Joe Perchesf97c1e02007-12-16 13:45:43 -0800903 if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
Denis V. Lunev7fee0ca2008-01-21 17:32:38 -0800904 struct in_device *in_dev;
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200905 in_dev = inetdev_by_index(t->net, t->mlink);
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000906 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 ip_mc_dec_group(in_dev, t->parms.iph.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909 return 0;
910}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911#endif
912
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800913static const struct net_device_ops ipgre_netdev_ops = {
914 .ndo_init = ipgre_tunnel_init,
Pravin B Shelarc5441932013-03-25 14:49:35 +0000915 .ndo_uninit = ip_tunnel_uninit,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800916#ifdef CONFIG_NET_IPGRE_BROADCAST
917 .ndo_open = ipgre_open,
918 .ndo_stop = ipgre_close,
919#endif
Pravin B Shelarc5441932013-03-25 14:49:35 +0000920 .ndo_start_xmit = ipgre_xmit,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800921 .ndo_do_ioctl = ipgre_tunnel_ioctl,
Pravin B Shelarc5441932013-03-25 14:49:35 +0000922 .ndo_change_mtu = ip_tunnel_change_mtu,
923 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtel1e995842015-04-02 17:07:02 +0200924 .ndo_get_iflink = ip_tunnel_get_iflink,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800925};
926
Eric Dumazet6b78f162012-09-13 21:25:33 +0000927#define GRE_FEATURES (NETIF_F_SG | \
928 NETIF_F_FRAGLIST | \
929 NETIF_F_HIGHDMA | \
930 NETIF_F_HW_CSUM)
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932static void ipgre_tunnel_setup(struct net_device *dev)
933{
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800934 dev->netdev_ops = &ipgre_netdev_ops;
Nicolas Dichtel5a455272014-04-11 15:51:18 +0200935 dev->type = ARPHRD_IPGRE;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000936 ip_tunnel_setup(dev, ipgre_net_id);
937}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Pravin B Shelarc5441932013-03-25 14:49:35 +0000939static void __gre_tunnel_init(struct net_device *dev)
940{
941 struct ip_tunnel *tunnel;
Tom Herbert4565e992014-09-17 12:26:01 -0700942 int t_hlen;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000943
944 tunnel = netdev_priv(dev);
Tom Herbert95f5c642016-04-29 17:12:16 -0700945 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000946 tunnel->parms.iph.protocol = IPPROTO_GRE;
947
Tom Herbert4565e992014-09-17 12:26:01 -0700948 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
949
950 t_hlen = tunnel->hlen + sizeof(struct iphdr);
951
952 dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4;
953 dev->mtu = ETH_DATA_LEN - t_hlen - 4;
Eric Dumazet6b78f162012-09-13 21:25:33 +0000954
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200955 dev->features |= GRE_FEATURES;
Eric Dumazet6b78f162012-09-13 21:25:33 +0000956 dev->hw_features |= GRE_FEATURES;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000957
958 if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
Alexander Duycka0ca1532016-04-05 09:13:39 -0700959 /* TCP offload with GRE SEQ is not supported, nor
960 * can we support 2 levels of outer headers requiring
961 * an update.
962 */
963 if (!(tunnel->parms.o_flags & TUNNEL_CSUM) ||
964 (tunnel->encap.type == TUNNEL_ENCAP_NONE)) {
965 dev->features |= NETIF_F_GSO_SOFTWARE;
966 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
967 }
968
Pravin B Shelarc5441932013-03-25 14:49:35 +0000969 /* Can use a lockless transmit, unless we generate
970 * output sequences
971 */
972 dev->features |= NETIF_F_LLTX;
973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976static int ipgre_tunnel_init(struct net_device *dev)
977{
Pravin B Shelarc5441932013-03-25 14:49:35 +0000978 struct ip_tunnel *tunnel = netdev_priv(dev);
979 struct iphdr *iph = &tunnel->parms.iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Pravin B Shelarc5441932013-03-25 14:49:35 +0000981 __gre_tunnel_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Pravin B Shelarc5441932013-03-25 14:49:35 +0000983 memcpy(dev->dev_addr, &iph->saddr, 4);
984 memcpy(dev->broadcast, &iph->daddr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Pravin B Shelarc5441932013-03-25 14:49:35 +0000986 dev->flags = IFF_NOARP;
Eric Dumazet02875872014-10-05 18:38:35 -0700987 netif_keep_dst(dev);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000988 dev->addr_len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Jiri Benca64b04d2016-04-27 11:29:06 +0200990 if (iph->daddr && !tunnel->collect_md) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991#ifdef CONFIG_NET_IPGRE_BROADCAST
Joe Perchesf97c1e02007-12-16 13:45:43 -0800992 if (ipv4_is_multicast(iph->daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (!iph->saddr)
994 return -EINVAL;
995 dev->flags = IFF_BROADCAST;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700996 dev->header_ops = &ipgre_header_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998#endif
Jiri Benca64b04d2016-04-27 11:29:06 +0200999 } else if (!tunnel->collect_md) {
Timo Teras6a5f44d2007-10-23 20:31:53 -07001000 dev->header_ops = &ipgre_header_ops;
Jiri Benca64b04d2016-04-27 11:29:06 +02001001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Pravin B Shelarc5441932013-03-25 14:49:35 +00001003 return ip_tunnel_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001006static const struct gre_protocol ipgre_protocol = {
1007 .handler = gre_rcv,
1008 .err_handler = gre_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009};
1010
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001011static int __net_init ipgre_init_net(struct net *net)
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001012{
Pravin B Shelarc5441932013-03-25 14:49:35 +00001013 return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001014}
1015
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001016static void __net_exit ipgre_exit_net(struct net *net)
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001017{
Pravin B Shelarc5441932013-03-25 14:49:35 +00001018 struct ip_tunnel_net *itn = net_generic(net, ipgre_net_id);
Nicolas Dichtel6c742e72013-08-13 17:51:11 +02001019 ip_tunnel_delete_net(itn, &ipgre_link_ops);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001020}
1021
1022static struct pernet_operations ipgre_net_ops = {
1023 .init = ipgre_init_net,
1024 .exit = ipgre_exit_net,
Eric W. Biedermancfb8fbf2009-11-29 15:46:13 +00001025 .id = &ipgre_net_id,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001026 .size = sizeof(struct ip_tunnel_net),
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001027};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001029static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1030 struct netlink_ext_ack *extack)
Herbert Xuc19e6542008-10-09 11:59:55 -07001031{
1032 __be16 flags;
1033
1034 if (!data)
1035 return 0;
1036
1037 flags = 0;
1038 if (data[IFLA_GRE_IFLAGS])
1039 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1040 if (data[IFLA_GRE_OFLAGS])
1041 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1042 if (flags & (GRE_VERSION|GRE_ROUTING))
1043 return -EINVAL;
1044
Jiri Benc946b6362016-04-27 14:08:01 +02001045 if (data[IFLA_GRE_COLLECT_METADATA] &&
1046 data[IFLA_GRE_ENCAP_TYPE] &&
1047 nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE)
1048 return -EINVAL;
1049
Herbert Xuc19e6542008-10-09 11:59:55 -07001050 return 0;
1051}
1052
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001053static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1054 struct netlink_ext_ack *extack)
Herbert Xue1a80002008-10-09 12:00:17 -07001055{
1056 __be32 daddr;
1057
1058 if (tb[IFLA_ADDRESS]) {
1059 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1060 return -EINVAL;
1061 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1062 return -EADDRNOTAVAIL;
1063 }
1064
1065 if (!data)
1066 goto out;
1067
1068 if (data[IFLA_GRE_REMOTE]) {
1069 memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4);
1070 if (!daddr)
1071 return -EINVAL;
1072 }
1073
1074out:
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001075 return ipgre_tunnel_validate(tb, data, extack);
Herbert Xue1a80002008-10-09 12:00:17 -07001076}
1077
William Tu84e54fe2017-08-22 09:40:28 -07001078static int erspan_validate(struct nlattr *tb[], struct nlattr *data[],
1079 struct netlink_ext_ack *extack)
1080{
1081 __be16 flags = 0;
1082 int ret;
1083
1084 if (!data)
1085 return 0;
1086
1087 ret = ipgre_tap_validate(tb, data, extack);
1088 if (ret)
1089 return ret;
1090
1091 /* ERSPAN should only have GRE sequence and key flag */
William Tu1a66a832017-08-25 09:21:28 -07001092 if (data[IFLA_GRE_OFLAGS])
1093 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1094 if (data[IFLA_GRE_IFLAGS])
1095 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1096 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1097 flags != (GRE_SEQ | GRE_KEY))
William Tu84e54fe2017-08-22 09:40:28 -07001098 return -EINVAL;
1099
1100 /* ERSPAN Session ID only has 10-bit. Since we reuse
1101 * 32-bit key field as ID, check it's range.
1102 */
1103 if (data[IFLA_GRE_IKEY] &&
1104 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1105 return -EINVAL;
1106
1107 if (data[IFLA_GRE_OKEY] &&
1108 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1109 return -EINVAL;
1110
1111 return 0;
1112}
1113
Philip Prindeville22a59be2016-06-14 15:53:02 -06001114static int ipgre_netlink_parms(struct net_device *dev,
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001115 struct nlattr *data[],
1116 struct nlattr *tb[],
Craig Gallek9830ad42017-04-19 12:30:54 -04001117 struct ip_tunnel_parm *parms,
1118 __u32 *fwmark)
Herbert Xuc19e6542008-10-09 11:59:55 -07001119{
Philip Prindeville22a59be2016-06-14 15:53:02 -06001120 struct ip_tunnel *t = netdev_priv(dev);
1121
Herbert Xu7bb82d92008-10-11 12:20:15 -07001122 memset(parms, 0, sizeof(*parms));
Herbert Xuc19e6542008-10-09 11:59:55 -07001123
1124 parms->iph.protocol = IPPROTO_GRE;
1125
1126 if (!data)
Philip Prindeville22a59be2016-06-14 15:53:02 -06001127 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001128
1129 if (data[IFLA_GRE_LINK])
1130 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1131
1132 if (data[IFLA_GRE_IFLAGS])
Pravin B Shelarc5441932013-03-25 14:49:35 +00001133 parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS]));
Herbert Xuc19e6542008-10-09 11:59:55 -07001134
1135 if (data[IFLA_GRE_OFLAGS])
Pravin B Shelarc5441932013-03-25 14:49:35 +00001136 parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS]));
Herbert Xuc19e6542008-10-09 11:59:55 -07001137
1138 if (data[IFLA_GRE_IKEY])
1139 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1140
1141 if (data[IFLA_GRE_OKEY])
1142 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1143
1144 if (data[IFLA_GRE_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001145 parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]);
Herbert Xuc19e6542008-10-09 11:59:55 -07001146
1147 if (data[IFLA_GRE_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001148 parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]);
Herbert Xuc19e6542008-10-09 11:59:55 -07001149
1150 if (data[IFLA_GRE_TTL])
1151 parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
1152
1153 if (data[IFLA_GRE_TOS])
1154 parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]);
1155
Philip Prindeville22a59be2016-06-14 15:53:02 -06001156 if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC])) {
1157 if (t->ignore_df)
1158 return -EINVAL;
Herbert Xuc19e6542008-10-09 11:59:55 -07001159 parms->iph.frag_off = htons(IP_DF);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001160 }
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001161
1162 if (data[IFLA_GRE_COLLECT_METADATA]) {
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001163 t->collect_md = true;
Jiri Bence271c7b2016-05-11 15:53:57 +02001164 if (dev->type == ARPHRD_IPGRE)
1165 dev->type = ARPHRD_NONE;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001166 }
Philip Prindeville22a59be2016-06-14 15:53:02 -06001167
1168 if (data[IFLA_GRE_IGNORE_DF]) {
1169 if (nla_get_u8(data[IFLA_GRE_IGNORE_DF])
1170 && (parms->iph.frag_off & htons(IP_DF)))
1171 return -EINVAL;
1172 t->ignore_df = !!nla_get_u8(data[IFLA_GRE_IGNORE_DF]);
1173 }
1174
Craig Gallek9830ad42017-04-19 12:30:54 -04001175 if (data[IFLA_GRE_FWMARK])
1176 *fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
1177
William Tu84e54fe2017-08-22 09:40:28 -07001178 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1179 t->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1180
1181 if (t->index & ~INDEX_MASK)
1182 return -EINVAL;
1183 }
1184
Philip Prindeville22a59be2016-06-14 15:53:02 -06001185 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001186}
1187
Tom Herbert4565e992014-09-17 12:26:01 -07001188/* This function returns true when ENCAP attributes are present in the nl msg */
1189static bool ipgre_netlink_encap_parms(struct nlattr *data[],
1190 struct ip_tunnel_encap *ipencap)
1191{
1192 bool ret = false;
1193
1194 memset(ipencap, 0, sizeof(*ipencap));
1195
1196 if (!data)
1197 return ret;
1198
1199 if (data[IFLA_GRE_ENCAP_TYPE]) {
1200 ret = true;
1201 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1202 }
1203
1204 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1205 ret = true;
1206 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1207 }
1208
1209 if (data[IFLA_GRE_ENCAP_SPORT]) {
1210 ret = true;
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001211 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
Tom Herbert4565e992014-09-17 12:26:01 -07001212 }
1213
1214 if (data[IFLA_GRE_ENCAP_DPORT]) {
1215 ret = true;
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001216 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
Tom Herbert4565e992014-09-17 12:26:01 -07001217 }
1218
1219 return ret;
1220}
1221
Pravin B Shelarc5441932013-03-25 14:49:35 +00001222static int gre_tap_init(struct net_device *dev)
Herbert Xue1a80002008-10-09 12:00:17 -07001223{
Pravin B Shelarc5441932013-03-25 14:49:35 +00001224 __gre_tunnel_init(dev);
stephen hemmingerbec94d4302014-12-27 10:01:42 -08001225 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Longd51711c2017-09-28 13:23:31 +08001226 netif_keep_dst(dev);
Herbert Xue1a80002008-10-09 12:00:17 -07001227
Pravin B Shelarc5441932013-03-25 14:49:35 +00001228 return ip_tunnel_init(dev);
Herbert Xue1a80002008-10-09 12:00:17 -07001229}
1230
Pravin B Shelarc5441932013-03-25 14:49:35 +00001231static const struct net_device_ops gre_tap_netdev_ops = {
1232 .ndo_init = gre_tap_init,
1233 .ndo_uninit = ip_tunnel_uninit,
1234 .ndo_start_xmit = gre_tap_xmit,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -08001235 .ndo_set_mac_address = eth_mac_addr,
1236 .ndo_validate_addr = eth_validate_addr,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001237 .ndo_change_mtu = ip_tunnel_change_mtu,
1238 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtel1e995842015-04-02 17:07:02 +02001239 .ndo_get_iflink = ip_tunnel_get_iflink,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001240 .ndo_fill_metadata_dst = gre_fill_metadata_dst,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -08001241};
1242
William Tu84e54fe2017-08-22 09:40:28 -07001243static int erspan_tunnel_init(struct net_device *dev)
1244{
1245 struct ip_tunnel *tunnel = netdev_priv(dev);
1246 int t_hlen;
1247
1248 tunnel->tun_hlen = 8;
1249 tunnel->parms.iph.protocol = IPPROTO_GRE;
1250 t_hlen = tunnel->hlen + sizeof(struct iphdr) + sizeof(struct erspanhdr);
1251
1252 dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4;
1253 dev->mtu = ETH_DATA_LEN - t_hlen - 4;
1254 dev->features |= GRE_FEATURES;
1255 dev->hw_features |= GRE_FEATURES;
1256 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1257
1258 return ip_tunnel_init(dev);
1259}
1260
1261static const struct net_device_ops erspan_netdev_ops = {
1262 .ndo_init = erspan_tunnel_init,
1263 .ndo_uninit = ip_tunnel_uninit,
1264 .ndo_start_xmit = erspan_xmit,
1265 .ndo_set_mac_address = eth_mac_addr,
1266 .ndo_validate_addr = eth_validate_addr,
1267 .ndo_change_mtu = ip_tunnel_change_mtu,
1268 .ndo_get_stats64 = ip_tunnel_get_stats64,
1269 .ndo_get_iflink = ip_tunnel_get_iflink,
1270 .ndo_fill_metadata_dst = gre_fill_metadata_dst,
1271};
1272
Herbert Xue1a80002008-10-09 12:00:17 -07001273static void ipgre_tap_setup(struct net_device *dev)
1274{
Herbert Xue1a80002008-10-09 12:00:17 -07001275 ether_setup(dev);
Jiri Bencd13b1612016-02-17 15:32:53 +01001276 dev->netdev_ops = &gre_tap_netdev_ops;
1277 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1278 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Pravin B Shelarc5441932013-03-25 14:49:35 +00001279 ip_tunnel_setup(dev, gre_tap_net_id);
Herbert Xue1a80002008-10-09 12:00:17 -07001280}
1281
Pravin B Shelarc5441932013-03-25 14:49:35 +00001282static int ipgre_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001283 struct nlattr *tb[], struct nlattr *data[],
1284 struct netlink_ext_ack *extack)
Herbert Xuc19e6542008-10-09 11:59:55 -07001285{
Pravin B Shelarc5441932013-03-25 14:49:35 +00001286 struct ip_tunnel_parm p;
Tom Herbert4565e992014-09-17 12:26:01 -07001287 struct ip_tunnel_encap ipencap;
Craig Gallek9830ad42017-04-19 12:30:54 -04001288 __u32 fwmark = 0;
Philip Prindeville22a59be2016-06-14 15:53:02 -06001289 int err;
Tom Herbert4565e992014-09-17 12:26:01 -07001290
1291 if (ipgre_netlink_encap_parms(data, &ipencap)) {
1292 struct ip_tunnel *t = netdev_priv(dev);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001293 err = ip_tunnel_encap_setup(t, &ipencap);
Tom Herbert4565e992014-09-17 12:26:01 -07001294
1295 if (err < 0)
1296 return err;
1297 }
Herbert Xuc19e6542008-10-09 11:59:55 -07001298
Craig Gallek9830ad42017-04-19 12:30:54 -04001299 err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001300 if (err < 0)
1301 return err;
Craig Gallek9830ad42017-04-19 12:30:54 -04001302 return ip_tunnel_newlink(dev, tb, &p, fwmark);
Herbert Xuc19e6542008-10-09 11:59:55 -07001303}
1304
1305static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001306 struct nlattr *data[],
1307 struct netlink_ext_ack *extack)
Herbert Xuc19e6542008-10-09 11:59:55 -07001308{
Craig Gallek9830ad42017-04-19 12:30:54 -04001309 struct ip_tunnel *t = netdev_priv(dev);
Herbert Xuc19e6542008-10-09 11:59:55 -07001310 struct ip_tunnel_parm p;
Tom Herbert4565e992014-09-17 12:26:01 -07001311 struct ip_tunnel_encap ipencap;
Craig Gallek9830ad42017-04-19 12:30:54 -04001312 __u32 fwmark = t->fwmark;
Philip Prindeville22a59be2016-06-14 15:53:02 -06001313 int err;
Tom Herbert4565e992014-09-17 12:26:01 -07001314
1315 if (ipgre_netlink_encap_parms(data, &ipencap)) {
Philip Prindeville22a59be2016-06-14 15:53:02 -06001316 err = ip_tunnel_encap_setup(t, &ipencap);
Tom Herbert4565e992014-09-17 12:26:01 -07001317
1318 if (err < 0)
1319 return err;
1320 }
Herbert Xuc19e6542008-10-09 11:59:55 -07001321
Craig Gallek9830ad42017-04-19 12:30:54 -04001322 err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001323 if (err < 0)
1324 return err;
Craig Gallek9830ad42017-04-19 12:30:54 -04001325 return ip_tunnel_changelink(dev, tb, &p, fwmark);
Herbert Xuc19e6542008-10-09 11:59:55 -07001326}
1327
1328static size_t ipgre_get_size(const struct net_device *dev)
1329{
1330 return
1331 /* IFLA_GRE_LINK */
1332 nla_total_size(4) +
1333 /* IFLA_GRE_IFLAGS */
1334 nla_total_size(2) +
1335 /* IFLA_GRE_OFLAGS */
1336 nla_total_size(2) +
1337 /* IFLA_GRE_IKEY */
1338 nla_total_size(4) +
1339 /* IFLA_GRE_OKEY */
1340 nla_total_size(4) +
1341 /* IFLA_GRE_LOCAL */
1342 nla_total_size(4) +
1343 /* IFLA_GRE_REMOTE */
1344 nla_total_size(4) +
1345 /* IFLA_GRE_TTL */
1346 nla_total_size(1) +
1347 /* IFLA_GRE_TOS */
1348 nla_total_size(1) +
1349 /* IFLA_GRE_PMTUDISC */
1350 nla_total_size(1) +
Tom Herbert4565e992014-09-17 12:26:01 -07001351 /* IFLA_GRE_ENCAP_TYPE */
1352 nla_total_size(2) +
1353 /* IFLA_GRE_ENCAP_FLAGS */
1354 nla_total_size(2) +
1355 /* IFLA_GRE_ENCAP_SPORT */
1356 nla_total_size(2) +
1357 /* IFLA_GRE_ENCAP_DPORT */
1358 nla_total_size(2) +
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001359 /* IFLA_GRE_COLLECT_METADATA */
1360 nla_total_size(0) +
Philip Prindeville22a59be2016-06-14 15:53:02 -06001361 /* IFLA_GRE_IGNORE_DF */
1362 nla_total_size(1) +
Craig Gallek9830ad42017-04-19 12:30:54 -04001363 /* IFLA_GRE_FWMARK */
1364 nla_total_size(4) +
William Tu84e54fe2017-08-22 09:40:28 -07001365 /* IFLA_GRE_ERSPAN_INDEX */
1366 nla_total_size(4) +
Herbert Xuc19e6542008-10-09 11:59:55 -07001367 0;
1368}
1369
1370static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1371{
1372 struct ip_tunnel *t = netdev_priv(dev);
1373 struct ip_tunnel_parm *p = &t->parms;
1374
David S. Millerf3756b72012-04-01 20:39:02 -04001375 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
Tom Herbert95f5c642016-04-29 17:12:16 -07001376 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1377 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1378 nla_put_be16(skb, IFLA_GRE_OFLAGS,
1379 gre_tnl_flags_to_gre_flags(p->o_flags)) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001380 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1381 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001382 nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
1383 nla_put_in_addr(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001384 nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
1385 nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
1386 nla_put_u8(skb, IFLA_GRE_PMTUDISC,
Craig Gallek9830ad42017-04-19 12:30:54 -04001387 !!(p->iph.frag_off & htons(IP_DF))) ||
1388 nla_put_u32(skb, IFLA_GRE_FWMARK, t->fwmark))
David S. Millerf3756b72012-04-01 20:39:02 -04001389 goto nla_put_failure;
Tom Herbert4565e992014-09-17 12:26:01 -07001390
1391 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1392 t->encap.type) ||
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001393 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1394 t->encap.sport) ||
1395 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1396 t->encap.dport) ||
Tom Herbert4565e992014-09-17 12:26:01 -07001397 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
Tom Herberte1b2cb62014-11-05 16:49:38 -08001398 t->encap.flags))
Tom Herbert4565e992014-09-17 12:26:01 -07001399 goto nla_put_failure;
1400
Philip Prindeville22a59be2016-06-14 15:53:02 -06001401 if (nla_put_u8(skb, IFLA_GRE_IGNORE_DF, t->ignore_df))
1402 goto nla_put_failure;
1403
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001404 if (t->collect_md) {
1405 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
1406 goto nla_put_failure;
1407 }
1408
William Tu84e54fe2017-08-22 09:40:28 -07001409 if (t->index)
1410 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, t->index))
1411 goto nla_put_failure;
1412
Herbert Xuc19e6542008-10-09 11:59:55 -07001413 return 0;
1414
1415nla_put_failure:
1416 return -EMSGSIZE;
1417}
1418
William Tu84e54fe2017-08-22 09:40:28 -07001419static void erspan_setup(struct net_device *dev)
1420{
1421 ether_setup(dev);
1422 dev->netdev_ops = &erspan_netdev_ops;
1423 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1424 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1425 ip_tunnel_setup(dev, erspan_net_id);
1426}
1427
Herbert Xuc19e6542008-10-09 11:59:55 -07001428static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
1429 [IFLA_GRE_LINK] = { .type = NLA_U32 },
1430 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
1431 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
1432 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
1433 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
Patrick McHardy4d74f8b2008-10-10 12:11:06 -07001434 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1435 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Herbert Xuc19e6542008-10-09 11:59:55 -07001436 [IFLA_GRE_TTL] = { .type = NLA_U8 },
1437 [IFLA_GRE_TOS] = { .type = NLA_U8 },
1438 [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
Tom Herbert4565e992014-09-17 12:26:01 -07001439 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
1440 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
1441 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
1442 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001443 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
Philip Prindeville22a59be2016-06-14 15:53:02 -06001444 [IFLA_GRE_IGNORE_DF] = { .type = NLA_U8 },
Craig Gallek9830ad42017-04-19 12:30:54 -04001445 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
William Tu84e54fe2017-08-22 09:40:28 -07001446 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
Herbert Xuc19e6542008-10-09 11:59:55 -07001447};
1448
1449static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
1450 .kind = "gre",
1451 .maxtype = IFLA_GRE_MAX,
1452 .policy = ipgre_policy,
1453 .priv_size = sizeof(struct ip_tunnel),
1454 .setup = ipgre_tunnel_setup,
1455 .validate = ipgre_tunnel_validate,
1456 .newlink = ipgre_newlink,
1457 .changelink = ipgre_changelink,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001458 .dellink = ip_tunnel_dellink,
Herbert Xuc19e6542008-10-09 11:59:55 -07001459 .get_size = ipgre_get_size,
1460 .fill_info = ipgre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001461 .get_link_net = ip_tunnel_get_link_net,
Herbert Xuc19e6542008-10-09 11:59:55 -07001462};
1463
Herbert Xue1a80002008-10-09 12:00:17 -07001464static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
1465 .kind = "gretap",
1466 .maxtype = IFLA_GRE_MAX,
1467 .policy = ipgre_policy,
1468 .priv_size = sizeof(struct ip_tunnel),
1469 .setup = ipgre_tap_setup,
1470 .validate = ipgre_tap_validate,
1471 .newlink = ipgre_newlink,
1472 .changelink = ipgre_changelink,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001473 .dellink = ip_tunnel_dellink,
Herbert Xue1a80002008-10-09 12:00:17 -07001474 .get_size = ipgre_get_size,
1475 .fill_info = ipgre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001476 .get_link_net = ip_tunnel_get_link_net,
Herbert Xue1a80002008-10-09 12:00:17 -07001477};
1478
William Tu84e54fe2017-08-22 09:40:28 -07001479static struct rtnl_link_ops erspan_link_ops __read_mostly = {
1480 .kind = "erspan",
1481 .maxtype = IFLA_GRE_MAX,
1482 .policy = ipgre_policy,
1483 .priv_size = sizeof(struct ip_tunnel),
1484 .setup = erspan_setup,
1485 .validate = erspan_validate,
1486 .newlink = ipgre_newlink,
1487 .changelink = ipgre_changelink,
1488 .dellink = ip_tunnel_dellink,
1489 .get_size = ipgre_get_size,
1490 .fill_info = ipgre_fill_info,
1491 .get_link_net = ip_tunnel_get_link_net,
1492};
1493
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001494struct net_device *gretap_fb_dev_create(struct net *net, const char *name,
1495 u8 name_assign_type)
1496{
1497 struct nlattr *tb[IFLA_MAX + 1];
1498 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001499 LIST_HEAD(list_kill);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001500 struct ip_tunnel *t;
1501 int err;
1502
1503 memset(&tb, 0, sizeof(tb));
1504
1505 dev = rtnl_create_link(net, name, name_assign_type,
1506 &ipgre_tap_ops, tb);
1507 if (IS_ERR(dev))
1508 return dev;
1509
1510 /* Configure flow based GRE device. */
1511 t = netdev_priv(dev);
1512 t->collect_md = true;
1513
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001514 err = ipgre_newlink(net, dev, tb, NULL, NULL);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001515 if (err < 0) {
1516 free_netdev(dev);
1517 return ERR_PTR(err);
1518 }
David Wragg7e059152016-02-10 00:05:58 +00001519
1520 /* openvswitch users expect packet sizes to be unrestricted,
1521 * so set the largest MTU we can.
1522 */
1523 err = __ip_tunnel_change_mtu(dev, IP_MAX_MTU, false);
1524 if (err)
1525 goto out;
1526
Nicolas Dichtelda6f1da2016-06-13 10:31:06 +02001527 err = rtnl_configure_link(dev, NULL);
1528 if (err < 0)
1529 goto out;
1530
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001531 return dev;
1532out:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001533 ip_tunnel_dellink(dev, &list_kill);
1534 unregister_netdevice_many(&list_kill);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001535 return ERR_PTR(err);
1536}
1537EXPORT_SYMBOL_GPL(gretap_fb_dev_create);
1538
Pravin B Shelarc5441932013-03-25 14:49:35 +00001539static int __net_init ipgre_tap_init_net(struct net *net)
1540{
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001541 return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, "gretap0");
Pravin B Shelarc5441932013-03-25 14:49:35 +00001542}
1543
1544static void __net_exit ipgre_tap_exit_net(struct net *net)
1545{
1546 struct ip_tunnel_net *itn = net_generic(net, gre_tap_net_id);
Nicolas Dichtel6c742e72013-08-13 17:51:11 +02001547 ip_tunnel_delete_net(itn, &ipgre_tap_ops);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001548}
1549
1550static struct pernet_operations ipgre_tap_net_ops = {
1551 .init = ipgre_tap_init_net,
1552 .exit = ipgre_tap_exit_net,
1553 .id = &gre_tap_net_id,
1554 .size = sizeof(struct ip_tunnel_net),
1555};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
William Tu84e54fe2017-08-22 09:40:28 -07001557static int __net_init erspan_init_net(struct net *net)
1558{
1559 return ip_tunnel_init_net(net, erspan_net_id,
1560 &erspan_link_ops, "erspan0");
1561}
1562
1563static void __net_exit erspan_exit_net(struct net *net)
1564{
1565 struct ip_tunnel_net *itn = net_generic(net, erspan_net_id);
1566
1567 ip_tunnel_delete_net(itn, &erspan_link_ops);
1568}
1569
1570static struct pernet_operations erspan_net_ops = {
1571 .init = erspan_init_net,
1572 .exit = erspan_exit_net,
1573 .id = &erspan_net_id,
1574 .size = sizeof(struct ip_tunnel_net),
1575};
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577static int __init ipgre_init(void)
1578{
1579 int err;
1580
Joe Perches058bd4d2012-03-11 18:36:11 +00001581 pr_info("GRE over IPv4 tunneling driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Eric W. Biedermancfb8fbf2009-11-29 15:46:13 +00001583 err = register_pernet_device(&ipgre_net_ops);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001584 if (err < 0)
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001585 return err;
1586
Pravin B Shelarc5441932013-03-25 14:49:35 +00001587 err = register_pernet_device(&ipgre_tap_net_ops);
1588 if (err < 0)
William Tue3d03282017-08-22 17:04:05 -07001589 goto pnet_tap_failed;
Pravin B Shelarc5441932013-03-25 14:49:35 +00001590
William Tu84e54fe2017-08-22 09:40:28 -07001591 err = register_pernet_device(&erspan_net_ops);
1592 if (err < 0)
1593 goto pnet_erspan_failed;
1594
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001595 err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001596 if (err < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001597 pr_info("%s: can't add protocol\n", __func__);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001598 goto add_proto_failed;
1599 }
Pavel Emelyanov7daa0002008-04-16 01:10:05 -07001600
Herbert Xuc19e6542008-10-09 11:59:55 -07001601 err = rtnl_link_register(&ipgre_link_ops);
1602 if (err < 0)
1603 goto rtnl_link_failed;
1604
Herbert Xue1a80002008-10-09 12:00:17 -07001605 err = rtnl_link_register(&ipgre_tap_ops);
1606 if (err < 0)
1607 goto tap_ops_failed;
1608
William Tu84e54fe2017-08-22 09:40:28 -07001609 err = rtnl_link_register(&erspan_link_ops);
1610 if (err < 0)
1611 goto erspan_link_failed;
1612
Pravin B Shelarc5441932013-03-25 14:49:35 +00001613 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001614
William Tu84e54fe2017-08-22 09:40:28 -07001615erspan_link_failed:
1616 rtnl_link_unregister(&ipgre_tap_ops);
Herbert Xue1a80002008-10-09 12:00:17 -07001617tap_ops_failed:
1618 rtnl_link_unregister(&ipgre_link_ops);
Herbert Xuc19e6542008-10-09 11:59:55 -07001619rtnl_link_failed:
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001620 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001621add_proto_failed:
William Tu84e54fe2017-08-22 09:40:28 -07001622 unregister_pernet_device(&erspan_net_ops);
1623pnet_erspan_failed:
Pravin B Shelarc5441932013-03-25 14:49:35 +00001624 unregister_pernet_device(&ipgre_tap_net_ops);
William Tue3d03282017-08-22 17:04:05 -07001625pnet_tap_failed:
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001626 unregister_pernet_device(&ipgre_net_ops);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001627 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001630static void __exit ipgre_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Herbert Xue1a80002008-10-09 12:00:17 -07001632 rtnl_link_unregister(&ipgre_tap_ops);
Herbert Xuc19e6542008-10-09 11:59:55 -07001633 rtnl_link_unregister(&ipgre_link_ops);
William Tu84e54fe2017-08-22 09:40:28 -07001634 rtnl_link_unregister(&erspan_link_ops);
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001635 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001636 unregister_pernet_device(&ipgre_tap_net_ops);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001637 unregister_pernet_device(&ipgre_net_ops);
William Tu84e54fe2017-08-22 09:40:28 -07001638 unregister_pernet_device(&erspan_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
1641module_init(ipgre_init);
1642module_exit(ipgre_fini);
1643MODULE_LICENSE("GPL");
Patrick McHardy4d74f8b2008-10-10 12:11:06 -07001644MODULE_ALIAS_RTNL_LINK("gre");
1645MODULE_ALIAS_RTNL_LINK("gretap");
William Tu84e54fe2017-08-22 09:40:28 -07001646MODULE_ALIAS_RTNL_LINK("erspan");
Vasiliy Kulikov8909c9a2011-03-02 00:33:13 +03001647MODULE_ALIAS_NETDEV("gre0");
Pravin B Shelarc5441932013-03-25 14:49:35 +00001648MODULE_ALIAS_NETDEV("gretap0");
William Tu84e54fe2017-08-22 09:40:28 -07001649MODULE_ALIAS_NETDEV("erspan0");