blob: a53a543fe0555e891fa61fee36a8f8a47007764d [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003 * Linux NET3: GRE over IP protocol decoder.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Joe Perchesafd465032012-03-12 07:03:32 +00008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Randy Dunlap4fc268d2006-01-11 12:17:47 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080015#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/skbuff.h>
17#include <linux/netdevice.h>
18#include <linux/in.h>
19#include <linux/tcp.h>
20#include <linux/udp.h>
21#include <linux/if_arp.h>
Pravin B Shelar2e15ea32015-08-07 23:51:42 -070022#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/in6.h>
25#include <linux/inetdevice.h>
26#include <linux/igmp.h>
27#include <linux/netfilter_ipv4.h>
Herbert Xue1a80002008-10-09 12:00:17 -070028#include <linux/etherdevice.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080029#include <linux/if_ether.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <net/sock.h>
32#include <net/ip.h>
33#include <net/icmp.h>
34#include <net/protocol.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000035#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <net/arp.h>
37#include <net/checksum.h>
38#include <net/dsfield.h>
39#include <net/inet_ecn.h>
40#include <net/xfrm.h>
Pavel Emelyanov59a4c752008-04-16 01:08:53 -070041#include <net/net_namespace.h>
42#include <net/netns/generic.h>
Herbert Xuc19e6542008-10-09 11:59:55 -070043#include <net/rtnetlink.h>
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070044#include <net/gre.h>
Pravin B Shelar2e15ea32015-08-07 23:51:42 -070045#include <net/dst_metadata.h>
William Tu84e54fe2017-08-22 09:40:28 -070046#include <net/erspan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 Problems & solutions
50 --------------------
51
52 1. The most important issue is detecting local dead loops.
53 They would cause complete host lockup in transmit, which
54 would be "resolved" by stack overflow or, if queueing is enabled,
55 with infinite looping in net_bh.
56
57 We cannot track such dead loops during route installation,
58 it is infeasible task. The most general solutions would be
59 to keep skb->encapsulation counter (sort of local ttl),
Eric Dumazet6d0722a2010-09-29 23:35:10 -070060 and silently drop packet when it expires. It is a good
stephen hemmingerbff52852012-02-24 08:08:20 +000061 solution, but it supposes maintaining new variable in ALL
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 skb, even if no tunneling is used.
63
Eric Dumazet6d0722a2010-09-29 23:35:10 -070064 Current solution: xmit_recursion breaks dead loops. This is a percpu
65 counter, since when we enter the first ndo_xmit(), cpu migration is
66 forbidden. We force an exit if this counter reaches RECURSION_LIMIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 2. Networking dead loops would not kill routers, but would really
69 kill network. IP hop limit plays role of "t->recursion" in this case,
70 if we copy it from packet being encapsulated to upper header.
71 It is very good solution, but it introduces two problems:
72
73 - Routing protocols, using packets with ttl=1 (OSPF, RIP2),
74 do not work over tunnels.
75 - traceroute does not work. I planned to relay ICMP from tunnel,
76 so that this problem would be solved and traceroute output
77 would even more informative. This idea appeared to be wrong:
78 only Linux complies to rfc1812 now (yes, guys, Linux is the only
79 true router now :-)), all routers (at least, in neighbourhood of mine)
80 return only 8 bytes of payload. It is the end.
81
82 Hence, if we want that OSPF worked or traceroute said something reasonable,
83 we should search for another solution.
84
85 One of them is to parse packet trying to detect inner encapsulation
86 made by our node. It is difficult or even impossible, especially,
stephen hemmingerbff52852012-02-24 08:08:20 +000087 taking into account fragmentation. TO be short, ttl is not solution at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 Current solution: The solution was UNEXPECTEDLY SIMPLE.
90 We force DF flag on tunnels with preconfigured hop limit,
91 that is ALL. :-) Well, it does not remove the problem completely,
92 but exponential growth of network traffic is changed to linear
93 (branches, that exceed pmtu are pruned) and tunnel mtu
stephen hemmingerbff52852012-02-24 08:08:20 +000094 rapidly degrades to value <68, where looping stops.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 Yes, it is not good if there exists a router in the loop,
96 which does not force DF, even when encapsulating packets have DF set.
97 But it is not our problem! Nobody could accuse us, we made
98 all that we could make. Even if it is your gated who injected
99 fatal route to network, even if it were you who configured
100 fatal static route: you are innocent. :-)
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 Alexey Kuznetsov.
103 */
104
stephen hemmingereccc1bb2012-09-25 11:02:48 +0000105static bool log_ecn_error = true;
106module_param(log_ecn_error, bool, 0644);
107MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
108
Herbert Xuc19e6542008-10-09 11:59:55 -0700109static struct rtnl_link_ops ipgre_link_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static int ipgre_tunnel_init(struct net_device *dev);
William Tu1a66a832017-08-25 09:21:28 -0700111static void erspan_build_header(struct sk_buff *skb,
William Tuc69de582018-01-25 13:20:09 -0800112 u32 id, u32 index,
William Tua3222dc2017-11-30 11:51:27 -0800113 bool truncate, bool is_ipv4);
Pavel Emelyanoveb8ce742008-04-16 01:10:26 -0700114
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300115static unsigned int ipgre_net_id __read_mostly;
116static unsigned int gre_tap_net_id __read_mostly;
William Tu84e54fe2017-08-22 09:40:28 -0700117static unsigned int erspan_net_id __read_mostly;
Pavel Emelyanoveb8ce742008-04-16 01:10:26 -0700118
Stefano Brivio32bbd872018-11-08 12:19:21 +0100119static int ipgre_err(struct sk_buff *skb, u32 info,
120 const struct tnl_ptk_info *tpi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Pravin B Shelarc5441932013-03-25 14:49:35 +0000123 /* All the routers (except for Linux) return only
124 8 bytes of packet payload. It means, that precise relaying of
125 ICMP in the real Internet is absolutely infeasible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Pravin B Shelarc5441932013-03-25 14:49:35 +0000127 Moreover, Cisco "wise men" put GRE key to the third word
128 in GRE header. It makes impossible maintaining even soft
129 state for keyed GRE tunnels with enabled checksum. Tell
130 them "thank you".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Pravin B Shelarc5441932013-03-25 14:49:35 +0000132 Well, I wonder, rfc1812 was written by Cisco employee,
133 what the hell these idiots break standards established
134 by themselves???
135 */
136 struct net *net = dev_net(skb->dev);
137 struct ip_tunnel_net *itn;
Eric Dumazet96f5a842013-05-18 08:36:03 +0000138 const struct iphdr *iph;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300139 const int type = icmp_hdr(skb)->type;
140 const int code = icmp_hdr(skb)->code;
Eric Dumazet20e19542016-06-18 21:52:06 -0700141 unsigned int data_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct ip_tunnel *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Pravin B Shelarbda7bb42013-06-17 17:49:38 -0700144 if (tpi->proto == htons(ETH_P_TEB))
Pravin B Shelarc5441932013-03-25 14:49:35 +0000145 itn = net_generic(net, gre_tap_net_id);
Haishuang Yan51dc63e2018-09-10 22:19:48 +0800146 else if (tpi->proto == htons(ETH_P_ERSPAN) ||
147 tpi->proto == htons(ETH_P_ERSPAN2))
148 itn = net_generic(net, erspan_net_id);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000149 else
150 itn = net_generic(net, ipgre_net_id);
151
Duan Jiongc0c0c502014-01-28 11:49:43 +0800152 iph = (const struct iphdr *)(icmp_hdr(skb) + 1);
Pravin B Shelarbda7bb42013-06-17 17:49:38 -0700153 t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
154 iph->daddr, iph->saddr, tpi->key);
stephen hemmingerd2083282012-09-24 18:12:23 +0000155
Ian Morris51456b22015-04-03 09:17:26 +0100156 if (!t)
Stefano Brivio32bbd872018-11-08 12:19:21 +0100157 return -ENOENT;
158
159 switch (type) {
160 default:
161 case ICMP_PARAMETERPROB:
162 return 0;
163
164 case ICMP_DEST_UNREACH:
165 switch (code) {
166 case ICMP_SR_FAILED:
167 case ICMP_PORT_UNREACH:
168 /* Impossible event. */
169 return 0;
170 default:
171 /* All others are translated to HOST_UNREACH.
172 rfc2003 contains "deep thoughts" about NET_UNREACH,
173 I believe they are just ether pollution. --ANK
174 */
175 break;
176 }
177 break;
178
179 case ICMP_TIME_EXCEEDED:
180 if (code != ICMP_EXC_TTL)
181 return 0;
182 data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */
183 break;
184
185 case ICMP_REDIRECT:
186 break;
187 }
David S. Miller36393392012-06-14 22:21:46 -0700188
Eric Dumazet9b8c6d72016-06-18 21:52:05 -0700189#if IS_ENABLED(CONFIG_IPV6)
190 if (tpi->proto == htons(ETH_P_IPV6) &&
Eric Dumazet20e19542016-06-18 21:52:06 -0700191 !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4 + tpi->hdr_len,
192 type, data_len))
Stefano Brivio32bbd872018-11-08 12:19:21 +0100193 return 0;
Eric Dumazet9b8c6d72016-06-18 21:52:05 -0700194#endif
195
David S. Miller36393392012-06-14 22:21:46 -0700196 if (t->parms.iph.daddr == 0 ||
Joe Perchesf97c1e02007-12-16 13:45:43 -0800197 ipv4_is_multicast(t->parms.iph.daddr))
Stefano Brivio32bbd872018-11-08 12:19:21 +0100198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
Stefano Brivio32bbd872018-11-08 12:19:21 +0100201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Wei Yongjunda6185d82009-02-24 23:34:48 -0800203 if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 t->err_count++;
205 else
206 t->err_count = 1;
207 t->err_time = jiffies;
Stefano Brivio32bbd872018-11-08 12:19:21 +0100208
209 return 0;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700210}
211
212static void gre_err(struct sk_buff *skb, u32 info)
213{
214 /* All the routers (except for Linux) return only
215 * 8 bytes of packet payload. It means, that precise relaying of
216 * ICMP in the real Internet is absolutely infeasible.
217 *
218 * Moreover, Cisco "wise men" put GRE key to the third word
219 * in GRE header. It makes impossible maintaining even soft
220 * state for keyed
221 * GRE tunnels with enabled checksum. Tell them "thank you".
222 *
223 * Well, I wonder, rfc1812 was written by Cisco employee,
224 * what the hell these idiots break standards established
225 * by themselves???
226 */
227
Eric Dumazete582615ad2016-06-15 06:24:00 -0700228 const struct iphdr *iph = (struct iphdr *)skb->data;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700229 const int type = icmp_hdr(skb)->type;
230 const int code = icmp_hdr(skb)->code;
231 struct tnl_ptk_info tpi;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700232
Haishuang Yanb0350d52018-09-14 12:26:47 +0800233 if (gre_parse_header(skb, &tpi, NULL, htons(ETH_P_IP),
234 iph->ihl * 4) < 0)
235 return;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700236
237 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
238 ipv4_update_pmtu(skb, dev_net(skb->dev), info,
Maciej Żenczykowskid888f392018-09-25 20:56:26 -0700239 skb->dev->ifindex, IPPROTO_GRE);
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700240 return;
241 }
242 if (type == ICMP_REDIRECT) {
Maciej Żenczykowski1042caa2018-09-25 20:56:27 -0700243 ipv4_redirect(skb, dev_net(skb->dev), skb->dev->ifindex,
244 IPPROTO_GRE);
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700245 return;
246 }
247
248 ipgre_err(skb, info, &tpi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
William Tu84e54fe2017-08-22 09:40:28 -0700251static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi,
252 int gre_hdr_len)
253{
254 struct net *net = dev_net(skb->dev);
255 struct metadata_dst *tun_dst = NULL;
William Tu1d7e2ed2017-12-13 16:38:55 -0800256 struct erspan_base_hdr *ershdr;
William Tu84e54fe2017-08-22 09:40:28 -0700257 struct ip_tunnel_net *itn;
258 struct ip_tunnel *tunnel;
William Tu84e54fe2017-08-22 09:40:28 -0700259 const struct iphdr *iph;
William Tu3df192832018-02-05 13:35:34 -0800260 struct erspan_md2 *md2;
William Tu1d7e2ed2017-12-13 16:38:55 -0800261 int ver;
William Tu84e54fe2017-08-22 09:40:28 -0700262 int len;
263
264 itn = net_generic(net, erspan_net_id);
William Tu84e54fe2017-08-22 09:40:28 -0700265
266 iph = ip_hdr(skb);
William Tu1d7e2ed2017-12-13 16:38:55 -0800267 ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len);
William Tuc69de582018-01-25 13:20:09 -0800268 ver = ershdr->ver;
William Tu84e54fe2017-08-22 09:40:28 -0700269
William Tu84e54fe2017-08-22 09:40:28 -0700270 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex,
271 tpi->flags | TUNNEL_KEY,
272 iph->saddr, iph->daddr, tpi->key);
273
274 if (tunnel) {
William Tu1d7e2ed2017-12-13 16:38:55 -0800275 len = gre_hdr_len + erspan_hdr_len(ver);
276 if (unlikely(!pskb_may_pull(skb, len)))
William Tuae3e1332017-12-15 14:27:43 -0800277 return PACKET_REJECT;
William Tu1d7e2ed2017-12-13 16:38:55 -0800278
William Tu84e54fe2017-08-22 09:40:28 -0700279 if (__iptunnel_pull_header(skb,
William Tu1d7e2ed2017-12-13 16:38:55 -0800280 len,
William Tu84e54fe2017-08-22 09:40:28 -0700281 htons(ETH_P_TEB),
282 false, false) < 0)
283 goto drop;
284
William Tu1a66a832017-08-25 09:21:28 -0700285 if (tunnel->collect_md) {
Lorenzo Bianconi492b67e2019-04-06 17:16:52 +0200286 struct erspan_metadata *pkt_md, *md;
William Tu1a66a832017-08-25 09:21:28 -0700287 struct ip_tunnel_info *info;
Lorenzo Bianconi492b67e2019-04-06 17:16:52 +0200288 unsigned char *gh;
William Tu1a66a832017-08-25 09:21:28 -0700289 __be64 tun_id;
290 __be16 flags;
291
292 tpi->flags |= TUNNEL_KEY;
293 flags = tpi->flags;
294 tun_id = key32_to_tunnel_id(tpi->key);
295
296 tun_dst = ip_tun_rx_dst(skb, flags,
297 tun_id, sizeof(*md));
298 if (!tun_dst)
299 return PACKET_REJECT;
300
Lorenzo Bianconi492b67e2019-04-06 17:16:52 +0200301 /* skb can be uncloned in __iptunnel_pull_header, so
302 * old pkt_md is no longer valid and we need to reset
303 * it
304 */
305 gh = skb_network_header(skb) +
306 skb_network_header_len(skb);
307 pkt_md = (struct erspan_metadata *)(gh + gre_hdr_len +
308 sizeof(*ershdr));
William Tu1a66a832017-08-25 09:21:28 -0700309 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
William Tuf551c912017-12-13 16:38:56 -0800310 md->version = ver;
William Tu3df192832018-02-05 13:35:34 -0800311 md2 = &md->u.md2;
312 memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
313 ERSPAN_V2_MDSIZE);
William Tuf551c912017-12-13 16:38:56 -0800314
William Tu1a66a832017-08-25 09:21:28 -0700315 info = &tun_dst->u.tun_info;
316 info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
317 info->options_len = sizeof(*md);
William Tu1a66a832017-08-25 09:21:28 -0700318 }
319
William Tu84e54fe2017-08-22 09:40:28 -0700320 skb_reset_mac_header(skb);
321 ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
322 return PACKET_RCVD;
323 }
Haishuang Yan5a645062018-09-10 22:19:47 +0800324 return PACKET_REJECT;
325
William Tu84e54fe2017-08-22 09:40:28 -0700326drop:
327 kfree_skb(skb);
328 return PACKET_RCVD;
329}
330
Jiri Benc125372f2016-05-03 17:10:08 +0200331static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
332 struct ip_tunnel_net *itn, int hdr_len, bool raw_proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700334 struct metadata_dst *tun_dst = NULL;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000335 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct ip_tunnel *tunnel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700338 iph = ip_hdr(skb);
Pravin B Shelarbda7bb42013-06-17 17:49:38 -0700339 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
340 iph->saddr, iph->daddr, tpi->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
stephen hemmingerd2083282012-09-24 18:12:23 +0000342 if (tunnel) {
Jiri Benc125372f2016-05-03 17:10:08 +0200343 if (__iptunnel_pull_header(skb, hdr_len, tpi->proto,
344 raw_proto, false) < 0)
Jiri Benc244a7972016-05-03 17:10:07 +0200345 goto drop;
346
Jiri Bence271c7b2016-05-11 15:53:57 +0200347 if (tunnel->dev->type != ARPHRD_NONE)
348 skb_pop_mac_header(skb);
349 else
350 skb_reset_mac_header(skb);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700351 if (tunnel->collect_md) {
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700352 __be16 flags;
353 __be64 tun_id;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700354
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700355 flags = tpi->flags & (TUNNEL_CSUM | TUNNEL_KEY);
Amir Vadaid817f432016-09-08 16:23:45 +0300356 tun_id = key32_to_tunnel_id(tpi->key);
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700357 tun_dst = ip_tun_rx_dst(skb, flags, tun_id, 0);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700358 if (!tun_dst)
359 return PACKET_REJECT;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700360 }
361
362 ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
Pravin B Shelarbda7bb42013-06-17 17:49:38 -0700363 return PACKET_RCVD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Jiri Benc125372f2016-05-03 17:10:08 +0200365 return PACKET_NEXT;
Jiri Benc244a7972016-05-03 17:10:07 +0200366
367drop:
368 kfree_skb(skb);
369 return PACKET_RCVD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Jiri Benc125372f2016-05-03 17:10:08 +0200372static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
373 int hdr_len)
374{
375 struct net *net = dev_net(skb->dev);
376 struct ip_tunnel_net *itn;
377 int res;
378
379 if (tpi->proto == htons(ETH_P_TEB))
380 itn = net_generic(net, gre_tap_net_id);
381 else
382 itn = net_generic(net, ipgre_net_id);
383
384 res = __ipgre_rcv(skb, tpi, itn, hdr_len, false);
385 if (res == PACKET_NEXT && tpi->proto == htons(ETH_P_TEB)) {
386 /* ipgre tunnels in collect metadata mode should receive
387 * also ETH_P_TEB traffic.
388 */
389 itn = net_generic(net, ipgre_net_id);
390 res = __ipgre_rcv(skb, tpi, itn, hdr_len, true);
391 }
392 return res;
393}
394
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700395static int gre_rcv(struct sk_buff *skb)
396{
397 struct tnl_ptk_info tpi;
398 bool csum_err = false;
Tom Herbert95f5c642016-04-29 17:12:16 -0700399 int hdr_len;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700400
401#ifdef CONFIG_NET_IPGRE_BROADCAST
402 if (ipv4_is_multicast(ip_hdr(skb)->daddr)) {
403 /* Looped back packet, drop it! */
404 if (rt_is_output_route(skb_rtable(skb)))
405 goto drop;
406 }
407#endif
408
Eric Dumazete582615ad2016-06-15 06:24:00 -0700409 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP), 0);
Jiri Bencf132ae72016-05-03 15:00:21 +0200410 if (hdr_len < 0)
Tom Herbert95f5c642016-04-29 17:12:16 -0700411 goto drop;
412
William Tuf551c912017-12-13 16:38:56 -0800413 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
414 tpi.proto == htons(ETH_P_ERSPAN2))) {
William Tu84e54fe2017-08-22 09:40:28 -0700415 if (erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
416 return 0;
Haishuang Yandd8d5b82017-12-20 10:21:46 +0800417 goto out;
William Tu84e54fe2017-08-22 09:40:28 -0700418 }
419
Jiri Benc244a7972016-05-03 17:10:07 +0200420 if (ipgre_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700421 return 0;
422
Haishuang Yandd8d5b82017-12-20 10:21:46 +0800423out:
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700424 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
425drop:
426 kfree_skb(skb);
427 return 0;
428}
429
Pravin B Shelarc5441932013-03-25 14:49:35 +0000430static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
431 const struct iphdr *tnl_params,
432 __be16 proto)
433{
434 struct ip_tunnel *tunnel = netdev_priv(dev);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000435
Pravin B Shelarc5441932013-03-25 14:49:35 +0000436 if (tunnel->parms.o_flags & TUNNEL_SEQ)
437 tunnel->o_seqno++;
Eric Dumazetcef401d2013-01-25 20:34:37 +0000438
Pravin B Shelarc5441932013-03-25 14:49:35 +0000439 /* Push GRE header. */
Tom Herbert182a3522016-04-29 17:12:19 -0700440 gre_build_header(skb, tunnel->tun_hlen,
441 tunnel->parms.o_flags, proto, tunnel->parms.o_key,
442 htonl(tunnel->o_seqno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Nicolas Dichtelbf3d6a82013-05-27 23:48:15 +0000444 ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Alexander Duyckaed069d2016-04-14 15:33:37 -0400447static int gre_handle_offloads(struct sk_buff *skb, bool csum)
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -0700448{
Edward Cree6fa79662016-02-11 21:02:31 +0000449 return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -0700450}
451
William Tu862a03c2017-08-25 09:21:27 -0700452static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev,
453 __be16 proto)
454{
William Tu77a51962018-03-01 13:49:57 -0800455 struct ip_tunnel *tunnel = netdev_priv(dev);
William Tu862a03c2017-08-25 09:21:27 -0700456 struct ip_tunnel_info *tun_info;
457 const struct ip_tunnel_key *key;
William Tu862a03c2017-08-25 09:21:27 -0700458 int tunnel_hlen;
wenxu962924f2019-01-22 18:39:52 +0800459 __be16 flags;
William Tu862a03c2017-08-25 09:21:27 -0700460
461 tun_info = skb_tunnel_info(skb);
462 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
463 ip_tunnel_info_af(tun_info) != AF_INET))
464 goto err_free_skb;
465
466 key = &tun_info->key;
467 tunnel_hlen = gre_calc_hlen(key->tun_flags);
468
wenxu962924f2019-01-22 18:39:52 +0800469 if (skb_cow_head(skb, dev->needed_headroom))
470 goto err_free_skb;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700471
472 /* Push Tunnel header. */
Alexander Duyckaed069d2016-04-14 15:33:37 -0400473 if (gre_handle_offloads(skb, !!(tun_info->key.tun_flags & TUNNEL_CSUM)))
wenxu962924f2019-01-22 18:39:52 +0800474 goto err_free_skb;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700475
William Tu77a51962018-03-01 13:49:57 -0800476 flags = tun_info->key.tun_flags &
477 (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ);
David S. Millercba653212016-05-04 00:52:29 -0400478 gre_build_header(skb, tunnel_hlen, flags, proto,
William Tu77a51962018-03-01 13:49:57 -0800479 tunnel_id_to_key32(tun_info->key.tun_id),
Colin Ian King15746392018-03-21 19:34:58 +0000480 (flags & TUNNEL_SEQ) ? htonl(tunnel->o_seqno++) : 0);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700481
wenxu962924f2019-01-22 18:39:52 +0800482 ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen);
Pravin B Shelar039f5062015-12-24 14:34:54 -0800483
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700484 return;
485
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700486err_free_skb:
487 kfree_skb(skb);
488 dev->stats.tx_dropped++;
489}
490
Xin Long20704bd2019-01-14 18:10:06 +0800491static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev)
William Tu1a66a832017-08-25 09:21:28 -0700492{
493 struct ip_tunnel *tunnel = netdev_priv(dev);
494 struct ip_tunnel_info *tun_info;
495 const struct ip_tunnel_key *key;
496 struct erspan_metadata *md;
William Tu1a66a832017-08-25 09:21:28 -0700497 bool truncate = false;
wenxu962924f2019-01-22 18:39:52 +0800498 __be16 proto;
William Tu1a66a832017-08-25 09:21:28 -0700499 int tunnel_hlen;
William Tuf551c912017-12-13 16:38:56 -0800500 int version;
William Tu1baf5eb2018-04-27 14:16:32 -0700501 int nhoff;
William Tud5db21a2018-05-11 05:49:47 -0700502 int thoff;
William Tu1a66a832017-08-25 09:21:28 -0700503
504 tun_info = skb_tunnel_info(skb);
505 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
506 ip_tunnel_info_af(tun_info) != AF_INET))
507 goto err_free_skb;
508
509 key = &tun_info->key;
Pieter Jansen van Vuuren256c87c12018-06-26 21:39:36 -0700510 if (!(tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT))
wenxu962924f2019-01-22 18:39:52 +0800511 goto err_free_skb;
William Tuf551c912017-12-13 16:38:56 -0800512 md = ip_tunnel_info_opts(tun_info);
513 if (!md)
wenxu962924f2019-01-22 18:39:52 +0800514 goto err_free_skb;
William Tu1a66a832017-08-25 09:21:28 -0700515
516 /* ERSPAN has fixed 8 byte GRE header */
William Tuf551c912017-12-13 16:38:56 -0800517 version = md->version;
518 tunnel_hlen = 8 + erspan_hdr_len(version);
William Tu1a66a832017-08-25 09:21:28 -0700519
wenxu962924f2019-01-22 18:39:52 +0800520 if (skb_cow_head(skb, dev->needed_headroom))
521 goto err_free_skb;
William Tu1a66a832017-08-25 09:21:28 -0700522
523 if (gre_handle_offloads(skb, false))
wenxu962924f2019-01-22 18:39:52 +0800524 goto err_free_skb;
William Tu1a66a832017-08-25 09:21:28 -0700525
William Tuf1929702017-10-05 12:07:12 -0700526 if (skb->len > dev->mtu + dev->hard_header_len) {
527 pskb_trim(skb, dev->mtu + dev->hard_header_len);
William Tu1a66a832017-08-25 09:21:28 -0700528 truncate = true;
529 }
530
William Tu1baf5eb2018-04-27 14:16:32 -0700531 nhoff = skb_network_header(skb) - skb_mac_header(skb);
532 if (skb->protocol == htons(ETH_P_IP) &&
533 (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
534 truncate = true;
535
William Tud5db21a2018-05-11 05:49:47 -0700536 thoff = skb_transport_header(skb) - skb_mac_header(skb);
537 if (skb->protocol == htons(ETH_P_IPV6) &&
538 (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff))
539 truncate = true;
540
William Tuf551c912017-12-13 16:38:56 -0800541 if (version == 1) {
William Tuc69de582018-01-25 13:20:09 -0800542 erspan_build_header(skb, ntohl(tunnel_id_to_key32(key->tun_id)),
William Tuf551c912017-12-13 16:38:56 -0800543 ntohl(md->u.index), truncate, true);
Xin Long20704bd2019-01-14 18:10:06 +0800544 proto = htons(ETH_P_ERSPAN);
William Tuf551c912017-12-13 16:38:56 -0800545 } else if (version == 2) {
William Tuc69de582018-01-25 13:20:09 -0800546 erspan_build_header_v2(skb,
547 ntohl(tunnel_id_to_key32(key->tun_id)),
548 md->u.md2.dir,
549 get_hwid(&md->u.md2),
550 truncate, true);
Xin Long20704bd2019-01-14 18:10:06 +0800551 proto = htons(ETH_P_ERSPAN2);
William Tuf551c912017-12-13 16:38:56 -0800552 } else {
wenxu962924f2019-01-22 18:39:52 +0800553 goto err_free_skb;
William Tuf551c912017-12-13 16:38:56 -0800554 }
William Tu1a66a832017-08-25 09:21:28 -0700555
556 gre_build_header(skb, 8, TUNNEL_SEQ,
Xin Long20704bd2019-01-14 18:10:06 +0800557 proto, 0, htonl(tunnel->o_seqno++));
William Tu1a66a832017-08-25 09:21:28 -0700558
wenxu962924f2019-01-22 18:39:52 +0800559 ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen);
William Tu1a66a832017-08-25 09:21:28 -0700560
William Tu1a66a832017-08-25 09:21:28 -0700561 return;
562
William Tu1a66a832017-08-25 09:21:28 -0700563err_free_skb:
564 kfree_skb(skb);
565 dev->stats.tx_dropped++;
566}
567
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700568static int gre_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
569{
570 struct ip_tunnel_info *info = skb_tunnel_info(skb);
wenxu962924f2019-01-22 18:39:52 +0800571 const struct ip_tunnel_key *key;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700572 struct rtable *rt;
573 struct flowi4 fl4;
574
575 if (ip_tunnel_info_af(info) != AF_INET)
576 return -EINVAL;
577
wenxu962924f2019-01-22 18:39:52 +0800578 key = &info->key;
579 ip_tunnel_init_flow(&fl4, IPPROTO_GRE, key->u.ipv4.dst, key->u.ipv4.src,
580 tunnel_id_to_key32(key->tun_id), key->tos, 0,
wenxu24ba1442019-02-24 11:36:20 +0800581 skb->mark, skb_get_hash(skb));
wenxu962924f2019-01-22 18:39:52 +0800582 rt = ip_route_output_key(dev_net(dev), &fl4);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700583 if (IS_ERR(rt))
584 return PTR_ERR(rt);
585
586 ip_rt_put(rt);
587 info->key.u.ipv4.src = fl4.saddr;
588 return 0;
589}
590
Pravin B Shelarc5441932013-03-25 14:49:35 +0000591static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
592 struct net_device *dev)
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800593{
Pravin B Shelarc5441932013-03-25 14:49:35 +0000594 struct ip_tunnel *tunnel = netdev_priv(dev);
595 const struct iphdr *tnl_params;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800596
Willem de Bruijncb9f1b72018-12-30 17:24:36 -0500597 if (!pskb_inet_may_pull(skb))
598 goto free_skb;
599
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700600 if (tunnel->collect_md) {
Jiri Benc20907142016-04-27 11:29:07 +0200601 gre_fb_xmit(skb, dev, skb->protocol);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700602 return NETDEV_TX_OK;
603 }
604
Pravin B Shelarc5441932013-03-25 14:49:35 +0000605 if (dev->header_ops) {
606 /* Need space for new headers */
607 if (skb_cow_head(skb, dev->needed_headroom -
Chen Gang2bac7cb2013-04-22 20:45:42 +0000608 (tunnel->hlen + sizeof(struct iphdr))))
Pravin B Shelarc5441932013-03-25 14:49:35 +0000609 goto free_skb;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800610
Pravin B Shelarc5441932013-03-25 14:49:35 +0000611 tnl_params = (const struct iphdr *)skb->data;
Eric Dumazete985aad2010-09-27 03:57:11 +0000612
Pravin B Shelarc5441932013-03-25 14:49:35 +0000613 /* Pull skb since ip_tunnel_xmit() needs skb->data pointing
614 * to gre header.
615 */
616 skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
Timo Teräs8a0033a2014-12-15 09:24:13 +0200617 skb_reset_mac_header(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000618 } else {
619 if (skb_cow_head(skb, dev->needed_headroom))
620 goto free_skb;
Herbert Xue1a80002008-10-09 12:00:17 -0700621
Pravin B Shelarc5441932013-03-25 14:49:35 +0000622 tnl_params = &tunnel->parms.iph;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800623 }
624
Alexander Duyckaed069d2016-04-14 15:33:37 -0400625 if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
626 goto free_skb;
Timo Teräs8a0033a2014-12-15 09:24:13 +0200627
Pravin B Shelarc5441932013-03-25 14:49:35 +0000628 __gre_xmit(skb, dev, tnl_params, skb->protocol);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000629 return NETDEV_TX_OK;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800630
Pravin B Shelarc5441932013-03-25 14:49:35 +0000631free_skb:
Eric Dumazet3acfa1e2014-01-18 18:27:49 -0800632 kfree_skb(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000633 dev->stats.tx_dropped++;
634 return NETDEV_TX_OK;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800635}
636
William Tu84e54fe2017-08-22 09:40:28 -0700637static netdev_tx_t erspan_xmit(struct sk_buff *skb,
638 struct net_device *dev)
639{
640 struct ip_tunnel *tunnel = netdev_priv(dev);
641 bool truncate = false;
Xin Long20704bd2019-01-14 18:10:06 +0800642 __be16 proto;
William Tu84e54fe2017-08-22 09:40:28 -0700643
Willem de Bruijncb9f1b72018-12-30 17:24:36 -0500644 if (!pskb_inet_may_pull(skb))
645 goto free_skb;
646
William Tu1a66a832017-08-25 09:21:28 -0700647 if (tunnel->collect_md) {
Xin Long20704bd2019-01-14 18:10:06 +0800648 erspan_fb_xmit(skb, dev);
William Tu1a66a832017-08-25 09:21:28 -0700649 return NETDEV_TX_OK;
650 }
651
William Tu84e54fe2017-08-22 09:40:28 -0700652 if (gre_handle_offloads(skb, false))
653 goto free_skb;
654
655 if (skb_cow_head(skb, dev->needed_headroom))
656 goto free_skb;
657
William Tuf1929702017-10-05 12:07:12 -0700658 if (skb->len > dev->mtu + dev->hard_header_len) {
659 pskb_trim(skb, dev->mtu + dev->hard_header_len);
William Tu84e54fe2017-08-22 09:40:28 -0700660 truncate = true;
661 }
662
663 /* Push ERSPAN header */
Xin Long20704bd2019-01-14 18:10:06 +0800664 if (tunnel->erspan_ver == 1) {
William Tuc69de582018-01-25 13:20:09 -0800665 erspan_build_header(skb, ntohl(tunnel->parms.o_key),
666 tunnel->index,
William Tuf551c912017-12-13 16:38:56 -0800667 truncate, true);
Xin Long20704bd2019-01-14 18:10:06 +0800668 proto = htons(ETH_P_ERSPAN);
669 } else if (tunnel->erspan_ver == 2) {
William Tuc69de582018-01-25 13:20:09 -0800670 erspan_build_header_v2(skb, ntohl(tunnel->parms.o_key),
William Tuf551c912017-12-13 16:38:56 -0800671 tunnel->dir, tunnel->hwid,
672 truncate, true);
Xin Long20704bd2019-01-14 18:10:06 +0800673 proto = htons(ETH_P_ERSPAN2);
674 } else {
William Tu02f99df2018-05-16 17:24:32 -0700675 goto free_skb;
Xin Long20704bd2019-01-14 18:10:06 +0800676 }
William Tuf551c912017-12-13 16:38:56 -0800677
William Tu84e54fe2017-08-22 09:40:28 -0700678 tunnel->parms.o_flags &= ~TUNNEL_KEY;
Xin Long20704bd2019-01-14 18:10:06 +0800679 __gre_xmit(skb, dev, &tunnel->parms.iph, proto);
William Tu84e54fe2017-08-22 09:40:28 -0700680 return NETDEV_TX_OK;
681
682free_skb:
683 kfree_skb(skb);
684 dev->stats.tx_dropped++;
685 return NETDEV_TX_OK;
686}
687
Pravin B Shelarc5441932013-03-25 14:49:35 +0000688static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
689 struct net_device *dev)
690{
691 struct ip_tunnel *tunnel = netdev_priv(dev);
692
Willem de Bruijncb9f1b72018-12-30 17:24:36 -0500693 if (!pskb_inet_may_pull(skb))
694 goto free_skb;
695
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700696 if (tunnel->collect_md) {
Jiri Benc20907142016-04-27 11:29:07 +0200697 gre_fb_xmit(skb, dev, htons(ETH_P_TEB));
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700698 return NETDEV_TX_OK;
699 }
700
Alexander Duyckaed069d2016-04-14 15:33:37 -0400701 if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
702 goto free_skb;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000703
704 if (skb_cow_head(skb, dev->needed_headroom))
705 goto free_skb;
706
707 __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB));
Pravin B Shelarc5441932013-03-25 14:49:35 +0000708 return NETDEV_TX_OK;
709
710free_skb:
Eric Dumazet3acfa1e2014-01-18 18:27:49 -0800711 kfree_skb(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000712 dev->stats.tx_dropped++;
713 return NETDEV_TX_OK;
714}
715
Xin Longdd9d5982017-11-07 16:33:08 +0800716static void ipgre_link_update(struct net_device *dev, bool set_mtu)
717{
718 struct ip_tunnel *tunnel = netdev_priv(dev);
719 int len;
720
721 len = tunnel->tun_hlen;
722 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
723 len = tunnel->tun_hlen - len;
724 tunnel->hlen = tunnel->hlen + len;
725
726 dev->needed_headroom = dev->needed_headroom + len;
727 if (set_mtu)
728 dev->mtu = max_t(int, dev->mtu - len, 68);
729
730 if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
731 if (!(tunnel->parms.o_flags & TUNNEL_CSUM) ||
732 tunnel->encap.type == TUNNEL_ENCAP_NONE) {
733 dev->features |= NETIF_F_GSO_SOFTWARE;
734 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Sabrina Dubroca1cc59542018-04-10 12:57:18 +0200735 } else {
736 dev->features &= ~NETIF_F_GSO_SOFTWARE;
737 dev->hw_features &= ~NETIF_F_GSO_SOFTWARE;
Xin Longdd9d5982017-11-07 16:33:08 +0800738 }
739 dev->features |= NETIF_F_LLTX;
Sabrina Dubroca1cc59542018-04-10 12:57:18 +0200740 } else {
741 dev->hw_features &= ~NETIF_F_GSO_SOFTWARE;
742 dev->features &= ~(NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE);
Xin Longdd9d5982017-11-07 16:33:08 +0800743 }
744}
745
Pravin B Shelarc5441932013-03-25 14:49:35 +0000746static int ipgre_tunnel_ioctl(struct net_device *dev,
747 struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct ip_tunnel_parm p;
Xin Longa0efab62017-11-07 16:33:09 +0800750 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Pravin B Shelarc5441932013-03-25 14:49:35 +0000752 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
753 return -EFAULT;
Xin Longa0efab62017-11-07 16:33:09 +0800754
Cong Wang6c734fb2013-06-29 12:02:59 +0800755 if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
756 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||
Xin Longa0efab62017-11-07 16:33:09 +0800757 p.iph.ihl != 5 || (p.iph.frag_off & htons(~IP_DF)) ||
758 ((p.i_flags | p.o_flags) & (GRE_VERSION | GRE_ROUTING)))
Cong Wang6c734fb2013-06-29 12:02:59 +0800759 return -EINVAL;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000760 }
Xin Longa0efab62017-11-07 16:33:09 +0800761
Pravin B Shelarc5441932013-03-25 14:49:35 +0000762 p.i_flags = gre_flags_to_tnl_flags(p.i_flags);
763 p.o_flags = gre_flags_to_tnl_flags(p.o_flags);
764
765 err = ip_tunnel_ioctl(dev, &p, cmd);
766 if (err)
767 return err;
768
Xin Longa0efab62017-11-07 16:33:09 +0800769 if (cmd == SIOCCHGTUNNEL) {
770 struct ip_tunnel *t = netdev_priv(dev);
771
772 t->parms.i_flags = p.i_flags;
773 t->parms.o_flags = p.o_flags;
774
775 if (strcmp(dev->rtnl_link_ops->kind, "erspan"))
776 ipgre_link_update(dev, true);
777 }
778
Tom Herbert95f5c642016-04-29 17:12:16 -0700779 p.i_flags = gre_tnl_flags_to_gre_flags(p.i_flags);
780 p.o_flags = gre_tnl_flags_to_gre_flags(p.o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000781
782 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
783 return -EFAULT;
Xin Longa0efab62017-11-07 16:33:09 +0800784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788/* Nice toy. Unfortunately, useless in real life :-)
789 It allows to construct virtual multiprotocol broadcast "LAN"
790 over the Internet, provided multicast routing is tuned.
791
792
793 I have no idea was this bicycle invented before me,
794 so that I had to set ARPHRD_IPGRE to a random value.
795 I have an impression, that Cisco could make something similar,
796 but this feature is apparently missing in IOS<=11.2(8).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
799 with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
800
801 ping -t 255 224.66.66.66
802
803 If nobody answers, mbone does not work.
804
805 ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
806 ip addr add 10.66.66.<somewhat>/24 dev Universe
807 ifconfig Universe up
808 ifconfig Universe add fe80::<Your_real_addr>/10
809 ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
810 ftp 10.66.66.66
811 ...
812 ftp fec0:6666:6666::193.233.7.65
813 ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 */
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700815static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
816 unsigned short type,
Eric Dumazet15078502010-09-15 11:07:53 +0000817 const void *daddr, const void *saddr, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Patrick McHardy2941a482006-01-08 22:05:26 -0800819 struct ip_tunnel *t = netdev_priv(dev);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000820 struct iphdr *iph;
821 struct gre_base_hdr *greh;
822
Johannes Bergd58ff352017-06-16 14:29:23 +0200823 iph = skb_push(skb, t->hlen + sizeof(*iph));
Pravin B Shelarc5441932013-03-25 14:49:35 +0000824 greh = (struct gre_base_hdr *)(iph+1);
Tom Herbert95f5c642016-04-29 17:12:16 -0700825 greh->flags = gre_tnl_flags_to_gre_flags(t->parms.o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000826 greh->protocol = htons(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Pravin B Shelarc5441932013-03-25 14:49:35 +0000830 /* Set the source hardware address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (saddr)
832 memcpy(&iph->saddr, saddr, 4);
Timo Teräs6d55cb92010-03-03 04:01:13 +0000833 if (daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 memcpy(&iph->daddr, daddr, 4);
Timo Teräs6d55cb92010-03-03 04:01:13 +0000835 if (iph->daddr)
Timo Teräs77a482b2013-08-06 13:45:43 +0300836 return t->hlen + sizeof(*iph);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900837
Pravin B Shelarc5441932013-03-25 14:49:35 +0000838 return -(t->hlen + sizeof(*iph));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Timo Teras6a5f44d2007-10-23 20:31:53 -0700841static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
842{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000843 const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb);
Timo Teras6a5f44d2007-10-23 20:31:53 -0700844 memcpy(haddr, &iph->saddr, 4);
845 return 4;
846}
847
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700848static const struct header_ops ipgre_header_ops = {
849 .create = ipgre_header,
Timo Teras6a5f44d2007-10-23 20:31:53 -0700850 .parse = ipgre_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700851};
852
Timo Teras6a5f44d2007-10-23 20:31:53 -0700853#ifdef CONFIG_NET_IPGRE_BROADCAST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854static int ipgre_open(struct net_device *dev)
855{
Patrick McHardy2941a482006-01-08 22:05:26 -0800856 struct ip_tunnel *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Joe Perchesf97c1e02007-12-16 13:45:43 -0800858 if (ipv4_is_multicast(t->parms.iph.daddr)) {
David S. Millercbb1e852011-05-04 12:33:34 -0700859 struct flowi4 fl4;
860 struct rtable *rt;
Eric Dumazete985aad2010-09-27 03:57:11 +0000861
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200862 rt = ip_route_output_gre(t->net, &fl4,
David S. Millercbb1e852011-05-04 12:33:34 -0700863 t->parms.iph.daddr,
864 t->parms.iph.saddr,
865 t->parms.o_key,
866 RT_TOS(t->parms.iph.tos),
867 t->parms.link);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800868 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return -EADDRNOTAVAIL;
Changli Gaod8d1f302010-06-10 23:31:35 -0700870 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 ip_rt_put(rt);
Ian Morris51456b22015-04-03 09:17:26 +0100872 if (!__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return -EADDRNOTAVAIL;
874 t->mlink = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -0700875 ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877 return 0;
878}
879
880static int ipgre_close(struct net_device *dev)
881{
Patrick McHardy2941a482006-01-08 22:05:26 -0800882 struct ip_tunnel *t = netdev_priv(dev);
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800883
Joe Perchesf97c1e02007-12-16 13:45:43 -0800884 if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
Denis V. Lunev7fee0ca2008-01-21 17:32:38 -0800885 struct in_device *in_dev;
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200886 in_dev = inetdev_by_index(t->net, t->mlink);
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000887 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 ip_mc_dec_group(in_dev, t->parms.iph.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890 return 0;
891}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892#endif
893
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800894static const struct net_device_ops ipgre_netdev_ops = {
895 .ndo_init = ipgre_tunnel_init,
Pravin B Shelarc5441932013-03-25 14:49:35 +0000896 .ndo_uninit = ip_tunnel_uninit,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800897#ifdef CONFIG_NET_IPGRE_BROADCAST
898 .ndo_open = ipgre_open,
899 .ndo_stop = ipgre_close,
900#endif
Pravin B Shelarc5441932013-03-25 14:49:35 +0000901 .ndo_start_xmit = ipgre_xmit,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800902 .ndo_do_ioctl = ipgre_tunnel_ioctl,
Pravin B Shelarc5441932013-03-25 14:49:35 +0000903 .ndo_change_mtu = ip_tunnel_change_mtu,
904 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtel1e995842015-04-02 17:07:02 +0200905 .ndo_get_iflink = ip_tunnel_get_iflink,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800906};
907
Eric Dumazet6b78f162012-09-13 21:25:33 +0000908#define GRE_FEATURES (NETIF_F_SG | \
909 NETIF_F_FRAGLIST | \
910 NETIF_F_HIGHDMA | \
911 NETIF_F_HW_CSUM)
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913static void ipgre_tunnel_setup(struct net_device *dev)
914{
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800915 dev->netdev_ops = &ipgre_netdev_ops;
Nicolas Dichtel5a455272014-04-11 15:51:18 +0200916 dev->type = ARPHRD_IPGRE;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000917 ip_tunnel_setup(dev, ipgre_net_id);
918}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Pravin B Shelarc5441932013-03-25 14:49:35 +0000920static void __gre_tunnel_init(struct net_device *dev)
921{
922 struct ip_tunnel *tunnel;
923
924 tunnel = netdev_priv(dev);
Tom Herbert95f5c642016-04-29 17:12:16 -0700925 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000926 tunnel->parms.iph.protocol = IPPROTO_GRE;
927
Tom Herbert4565e992014-09-17 12:26:01 -0700928 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
929
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200930 dev->features |= GRE_FEATURES;
Eric Dumazet6b78f162012-09-13 21:25:33 +0000931 dev->hw_features |= GRE_FEATURES;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000932
933 if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
Alexander Duycka0ca1532016-04-05 09:13:39 -0700934 /* TCP offload with GRE SEQ is not supported, nor
935 * can we support 2 levels of outer headers requiring
936 * an update.
937 */
938 if (!(tunnel->parms.o_flags & TUNNEL_CSUM) ||
939 (tunnel->encap.type == TUNNEL_ENCAP_NONE)) {
940 dev->features |= NETIF_F_GSO_SOFTWARE;
941 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
942 }
943
Pravin B Shelarc5441932013-03-25 14:49:35 +0000944 /* Can use a lockless transmit, unless we generate
945 * output sequences
946 */
947 dev->features |= NETIF_F_LLTX;
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951static int ipgre_tunnel_init(struct net_device *dev)
952{
Pravin B Shelarc5441932013-03-25 14:49:35 +0000953 struct ip_tunnel *tunnel = netdev_priv(dev);
954 struct iphdr *iph = &tunnel->parms.iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Pravin B Shelarc5441932013-03-25 14:49:35 +0000956 __gre_tunnel_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Pravin B Shelarc5441932013-03-25 14:49:35 +0000958 memcpy(dev->dev_addr, &iph->saddr, 4);
959 memcpy(dev->broadcast, &iph->daddr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Pravin B Shelarc5441932013-03-25 14:49:35 +0000961 dev->flags = IFF_NOARP;
Eric Dumazet02875872014-10-05 18:38:35 -0700962 netif_keep_dst(dev);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000963 dev->addr_len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Jiri Benca64b04d2016-04-27 11:29:06 +0200965 if (iph->daddr && !tunnel->collect_md) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966#ifdef CONFIG_NET_IPGRE_BROADCAST
Joe Perchesf97c1e02007-12-16 13:45:43 -0800967 if (ipv4_is_multicast(iph->daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (!iph->saddr)
969 return -EINVAL;
970 dev->flags = IFF_BROADCAST;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700971 dev->header_ops = &ipgre_header_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973#endif
Jiri Benca64b04d2016-04-27 11:29:06 +0200974 } else if (!tunnel->collect_md) {
Timo Teras6a5f44d2007-10-23 20:31:53 -0700975 dev->header_ops = &ipgre_header_ops;
Jiri Benca64b04d2016-04-27 11:29:06 +0200976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Pravin B Shelarc5441932013-03-25 14:49:35 +0000978 return ip_tunnel_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700981static const struct gre_protocol ipgre_protocol = {
982 .handler = gre_rcv,
983 .err_handler = gre_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984};
985
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000986static int __net_init ipgre_init_net(struct net *net)
Pavel Emelyanov59a4c752008-04-16 01:08:53 -0700987{
Pravin B Shelarc5441932013-03-25 14:49:35 +0000988 return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -0700989}
990
Eric Dumazet64bc1782017-09-19 16:27:09 -0700991static void __net_exit ipgre_exit_batch_net(struct list_head *list_net)
Pavel Emelyanov59a4c752008-04-16 01:08:53 -0700992{
Eric Dumazet64bc1782017-09-19 16:27:09 -0700993 ip_tunnel_delete_nets(list_net, ipgre_net_id, &ipgre_link_ops);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -0700994}
995
996static struct pernet_operations ipgre_net_ops = {
997 .init = ipgre_init_net,
Eric Dumazet64bc1782017-09-19 16:27:09 -0700998 .exit_batch = ipgre_exit_batch_net,
Eric W. Biedermancfb8fbf2009-11-29 15:46:13 +0000999 .id = &ipgre_net_id,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001000 .size = sizeof(struct ip_tunnel_net),
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001001};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001003static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1004 struct netlink_ext_ack *extack)
Herbert Xuc19e6542008-10-09 11:59:55 -07001005{
1006 __be16 flags;
1007
1008 if (!data)
1009 return 0;
1010
1011 flags = 0;
1012 if (data[IFLA_GRE_IFLAGS])
1013 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1014 if (data[IFLA_GRE_OFLAGS])
1015 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1016 if (flags & (GRE_VERSION|GRE_ROUTING))
1017 return -EINVAL;
1018
Jiri Benc946b6362016-04-27 14:08:01 +02001019 if (data[IFLA_GRE_COLLECT_METADATA] &&
1020 data[IFLA_GRE_ENCAP_TYPE] &&
1021 nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE)
1022 return -EINVAL;
1023
Herbert Xuc19e6542008-10-09 11:59:55 -07001024 return 0;
1025}
1026
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001027static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1028 struct netlink_ext_ack *extack)
Herbert Xue1a80002008-10-09 12:00:17 -07001029{
1030 __be32 daddr;
1031
1032 if (tb[IFLA_ADDRESS]) {
1033 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1034 return -EINVAL;
1035 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1036 return -EADDRNOTAVAIL;
1037 }
1038
1039 if (!data)
1040 goto out;
1041
1042 if (data[IFLA_GRE_REMOTE]) {
1043 memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4);
1044 if (!daddr)
1045 return -EINVAL;
1046 }
1047
1048out:
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001049 return ipgre_tunnel_validate(tb, data, extack);
Herbert Xue1a80002008-10-09 12:00:17 -07001050}
1051
William Tu84e54fe2017-08-22 09:40:28 -07001052static int erspan_validate(struct nlattr *tb[], struct nlattr *data[],
1053 struct netlink_ext_ack *extack)
1054{
1055 __be16 flags = 0;
1056 int ret;
1057
1058 if (!data)
1059 return 0;
1060
1061 ret = ipgre_tap_validate(tb, data, extack);
1062 if (ret)
1063 return ret;
1064
1065 /* ERSPAN should only have GRE sequence and key flag */
William Tu1a66a832017-08-25 09:21:28 -07001066 if (data[IFLA_GRE_OFLAGS])
1067 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1068 if (data[IFLA_GRE_IFLAGS])
1069 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1070 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1071 flags != (GRE_SEQ | GRE_KEY))
William Tu84e54fe2017-08-22 09:40:28 -07001072 return -EINVAL;
1073
1074 /* ERSPAN Session ID only has 10-bit. Since we reuse
1075 * 32-bit key field as ID, check it's range.
1076 */
1077 if (data[IFLA_GRE_IKEY] &&
1078 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1079 return -EINVAL;
1080
1081 if (data[IFLA_GRE_OKEY] &&
1082 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1083 return -EINVAL;
1084
1085 return 0;
1086}
1087
Philip Prindeville22a59be2016-06-14 15:53:02 -06001088static int ipgre_netlink_parms(struct net_device *dev,
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001089 struct nlattr *data[],
1090 struct nlattr *tb[],
Craig Gallek9830ad42017-04-19 12:30:54 -04001091 struct ip_tunnel_parm *parms,
1092 __u32 *fwmark)
Herbert Xuc19e6542008-10-09 11:59:55 -07001093{
Philip Prindeville22a59be2016-06-14 15:53:02 -06001094 struct ip_tunnel *t = netdev_priv(dev);
1095
Herbert Xu7bb82d92008-10-11 12:20:15 -07001096 memset(parms, 0, sizeof(*parms));
Herbert Xuc19e6542008-10-09 11:59:55 -07001097
1098 parms->iph.protocol = IPPROTO_GRE;
1099
1100 if (!data)
Philip Prindeville22a59be2016-06-14 15:53:02 -06001101 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001102
1103 if (data[IFLA_GRE_LINK])
1104 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1105
1106 if (data[IFLA_GRE_IFLAGS])
Pravin B Shelarc5441932013-03-25 14:49:35 +00001107 parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS]));
Herbert Xuc19e6542008-10-09 11:59:55 -07001108
1109 if (data[IFLA_GRE_OFLAGS])
Pravin B Shelarc5441932013-03-25 14:49:35 +00001110 parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS]));
Herbert Xuc19e6542008-10-09 11:59:55 -07001111
1112 if (data[IFLA_GRE_IKEY])
1113 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1114
1115 if (data[IFLA_GRE_OKEY])
1116 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1117
1118 if (data[IFLA_GRE_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001119 parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]);
Herbert Xuc19e6542008-10-09 11:59:55 -07001120
1121 if (data[IFLA_GRE_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001122 parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]);
Herbert Xuc19e6542008-10-09 11:59:55 -07001123
1124 if (data[IFLA_GRE_TTL])
1125 parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
1126
1127 if (data[IFLA_GRE_TOS])
1128 parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]);
1129
Philip Prindeville22a59be2016-06-14 15:53:02 -06001130 if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC])) {
1131 if (t->ignore_df)
1132 return -EINVAL;
Herbert Xuc19e6542008-10-09 11:59:55 -07001133 parms->iph.frag_off = htons(IP_DF);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001134 }
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001135
1136 if (data[IFLA_GRE_COLLECT_METADATA]) {
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001137 t->collect_md = true;
Jiri Bence271c7b2016-05-11 15:53:57 +02001138 if (dev->type == ARPHRD_IPGRE)
1139 dev->type = ARPHRD_NONE;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001140 }
Philip Prindeville22a59be2016-06-14 15:53:02 -06001141
1142 if (data[IFLA_GRE_IGNORE_DF]) {
1143 if (nla_get_u8(data[IFLA_GRE_IGNORE_DF])
1144 && (parms->iph.frag_off & htons(IP_DF)))
1145 return -EINVAL;
1146 t->ignore_df = !!nla_get_u8(data[IFLA_GRE_IGNORE_DF]);
1147 }
1148
Craig Gallek9830ad42017-04-19 12:30:54 -04001149 if (data[IFLA_GRE_FWMARK])
1150 *fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
1151
William Tuf551c912017-12-13 16:38:56 -08001152 if (data[IFLA_GRE_ERSPAN_VER]) {
1153 t->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
William Tu84e54fe2017-08-22 09:40:28 -07001154
William Tuf551c912017-12-13 16:38:56 -08001155 if (t->erspan_ver != 1 && t->erspan_ver != 2)
William Tu84e54fe2017-08-22 09:40:28 -07001156 return -EINVAL;
1157 }
1158
William Tuf551c912017-12-13 16:38:56 -08001159 if (t->erspan_ver == 1) {
1160 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1161 t->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1162 if (t->index & ~INDEX_MASK)
1163 return -EINVAL;
1164 }
1165 } else if (t->erspan_ver == 2) {
1166 if (data[IFLA_GRE_ERSPAN_DIR]) {
1167 t->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1168 if (t->dir & ~(DIR_MASK >> DIR_OFFSET))
1169 return -EINVAL;
1170 }
1171 if (data[IFLA_GRE_ERSPAN_HWID]) {
1172 t->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1173 if (t->hwid & ~(HWID_MASK >> HWID_OFFSET))
1174 return -EINVAL;
1175 }
1176 }
1177
Philip Prindeville22a59be2016-06-14 15:53:02 -06001178 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001179}
1180
Tom Herbert4565e992014-09-17 12:26:01 -07001181/* This function returns true when ENCAP attributes are present in the nl msg */
1182static bool ipgre_netlink_encap_parms(struct nlattr *data[],
1183 struct ip_tunnel_encap *ipencap)
1184{
1185 bool ret = false;
1186
1187 memset(ipencap, 0, sizeof(*ipencap));
1188
1189 if (!data)
1190 return ret;
1191
1192 if (data[IFLA_GRE_ENCAP_TYPE]) {
1193 ret = true;
1194 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1195 }
1196
1197 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1198 ret = true;
1199 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1200 }
1201
1202 if (data[IFLA_GRE_ENCAP_SPORT]) {
1203 ret = true;
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001204 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
Tom Herbert4565e992014-09-17 12:26:01 -07001205 }
1206
1207 if (data[IFLA_GRE_ENCAP_DPORT]) {
1208 ret = true;
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001209 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
Tom Herbert4565e992014-09-17 12:26:01 -07001210 }
1211
1212 return ret;
1213}
1214
Pravin B Shelarc5441932013-03-25 14:49:35 +00001215static int gre_tap_init(struct net_device *dev)
Herbert Xue1a80002008-10-09 12:00:17 -07001216{
Pravin B Shelarc5441932013-03-25 14:49:35 +00001217 __gre_tunnel_init(dev);
stephen hemmingerbec94d4302014-12-27 10:01:42 -08001218 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Longd51711c2017-09-28 13:23:31 +08001219 netif_keep_dst(dev);
Herbert Xue1a80002008-10-09 12:00:17 -07001220
Pravin B Shelarc5441932013-03-25 14:49:35 +00001221 return ip_tunnel_init(dev);
Herbert Xue1a80002008-10-09 12:00:17 -07001222}
1223
Pravin B Shelarc5441932013-03-25 14:49:35 +00001224static const struct net_device_ops gre_tap_netdev_ops = {
1225 .ndo_init = gre_tap_init,
1226 .ndo_uninit = ip_tunnel_uninit,
1227 .ndo_start_xmit = gre_tap_xmit,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -08001228 .ndo_set_mac_address = eth_mac_addr,
1229 .ndo_validate_addr = eth_validate_addr,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001230 .ndo_change_mtu = ip_tunnel_change_mtu,
1231 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtel1e995842015-04-02 17:07:02 +02001232 .ndo_get_iflink = ip_tunnel_get_iflink,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001233 .ndo_fill_metadata_dst = gre_fill_metadata_dst,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -08001234};
1235
William Tu84e54fe2017-08-22 09:40:28 -07001236static int erspan_tunnel_init(struct net_device *dev)
1237{
1238 struct ip_tunnel *tunnel = netdev_priv(dev);
William Tu84e54fe2017-08-22 09:40:28 -07001239
1240 tunnel->tun_hlen = 8;
1241 tunnel->parms.iph.protocol = IPPROTO_GRE;
Xin Longc122fda2017-10-01 22:00:55 +08001242 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
William Tuf551c912017-12-13 16:38:56 -08001243 erspan_hdr_len(tunnel->erspan_ver);
William Tu84e54fe2017-08-22 09:40:28 -07001244
William Tu84e54fe2017-08-22 09:40:28 -07001245 dev->features |= GRE_FEATURES;
1246 dev->hw_features |= GRE_FEATURES;
1247 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Longc84bed42017-10-01 22:00:56 +08001248 netif_keep_dst(dev);
William Tu84e54fe2017-08-22 09:40:28 -07001249
1250 return ip_tunnel_init(dev);
1251}
1252
1253static const struct net_device_ops erspan_netdev_ops = {
1254 .ndo_init = erspan_tunnel_init,
1255 .ndo_uninit = ip_tunnel_uninit,
1256 .ndo_start_xmit = erspan_xmit,
1257 .ndo_set_mac_address = eth_mac_addr,
1258 .ndo_validate_addr = eth_validate_addr,
1259 .ndo_change_mtu = ip_tunnel_change_mtu,
1260 .ndo_get_stats64 = ip_tunnel_get_stats64,
1261 .ndo_get_iflink = ip_tunnel_get_iflink,
1262 .ndo_fill_metadata_dst = gre_fill_metadata_dst,
1263};
1264
Herbert Xue1a80002008-10-09 12:00:17 -07001265static void ipgre_tap_setup(struct net_device *dev)
1266{
Herbert Xue1a80002008-10-09 12:00:17 -07001267 ether_setup(dev);
Xin Longcfddd4c2017-12-18 14:24:35 +08001268 dev->max_mtu = 0;
Jiri Bencd13b1612016-02-17 15:32:53 +01001269 dev->netdev_ops = &gre_tap_netdev_ops;
1270 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1271 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Pravin B Shelarc5441932013-03-25 14:49:35 +00001272 ip_tunnel_setup(dev, gre_tap_net_id);
Herbert Xue1a80002008-10-09 12:00:17 -07001273}
1274
Pravin B Shelarc5441932013-03-25 14:49:35 +00001275static int ipgre_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001276 struct nlattr *tb[], struct nlattr *data[],
1277 struct netlink_ext_ack *extack)
Herbert Xuc19e6542008-10-09 11:59:55 -07001278{
Pravin B Shelarc5441932013-03-25 14:49:35 +00001279 struct ip_tunnel_parm p;
Tom Herbert4565e992014-09-17 12:26:01 -07001280 struct ip_tunnel_encap ipencap;
Craig Gallek9830ad42017-04-19 12:30:54 -04001281 __u32 fwmark = 0;
Philip Prindeville22a59be2016-06-14 15:53:02 -06001282 int err;
Tom Herbert4565e992014-09-17 12:26:01 -07001283
1284 if (ipgre_netlink_encap_parms(data, &ipencap)) {
1285 struct ip_tunnel *t = netdev_priv(dev);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001286 err = ip_tunnel_encap_setup(t, &ipencap);
Tom Herbert4565e992014-09-17 12:26:01 -07001287
1288 if (err < 0)
1289 return err;
1290 }
Herbert Xuc19e6542008-10-09 11:59:55 -07001291
Craig Gallek9830ad42017-04-19 12:30:54 -04001292 err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001293 if (err < 0)
1294 return err;
Craig Gallek9830ad42017-04-19 12:30:54 -04001295 return ip_tunnel_newlink(dev, tb, &p, fwmark);
Herbert Xuc19e6542008-10-09 11:59:55 -07001296}
1297
1298static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001299 struct nlattr *data[],
1300 struct netlink_ext_ack *extack)
Herbert Xuc19e6542008-10-09 11:59:55 -07001301{
Craig Gallek9830ad42017-04-19 12:30:54 -04001302 struct ip_tunnel *t = netdev_priv(dev);
Tom Herbert4565e992014-09-17 12:26:01 -07001303 struct ip_tunnel_encap ipencap;
Craig Gallek9830ad42017-04-19 12:30:54 -04001304 __u32 fwmark = t->fwmark;
Xin Longdd9d5982017-11-07 16:33:08 +08001305 struct ip_tunnel_parm p;
Philip Prindeville22a59be2016-06-14 15:53:02 -06001306 int err;
Tom Herbert4565e992014-09-17 12:26:01 -07001307
1308 if (ipgre_netlink_encap_parms(data, &ipencap)) {
Philip Prindeville22a59be2016-06-14 15:53:02 -06001309 err = ip_tunnel_encap_setup(t, &ipencap);
Tom Herbert4565e992014-09-17 12:26:01 -07001310
1311 if (err < 0)
1312 return err;
1313 }
Herbert Xuc19e6542008-10-09 11:59:55 -07001314
Craig Gallek9830ad42017-04-19 12:30:54 -04001315 err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001316 if (err < 0)
1317 return err;
Xin Longdd9d5982017-11-07 16:33:08 +08001318
1319 err = ip_tunnel_changelink(dev, tb, &p, fwmark);
1320 if (err < 0)
1321 return err;
1322
1323 t->parms.i_flags = p.i_flags;
1324 t->parms.o_flags = p.o_flags;
1325
1326 if (strcmp(dev->rtnl_link_ops->kind, "erspan"))
1327 ipgre_link_update(dev, !tb[IFLA_MTU]);
1328
1329 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001330}
1331
1332static size_t ipgre_get_size(const struct net_device *dev)
1333{
1334 return
1335 /* IFLA_GRE_LINK */
1336 nla_total_size(4) +
1337 /* IFLA_GRE_IFLAGS */
1338 nla_total_size(2) +
1339 /* IFLA_GRE_OFLAGS */
1340 nla_total_size(2) +
1341 /* IFLA_GRE_IKEY */
1342 nla_total_size(4) +
1343 /* IFLA_GRE_OKEY */
1344 nla_total_size(4) +
1345 /* IFLA_GRE_LOCAL */
1346 nla_total_size(4) +
1347 /* IFLA_GRE_REMOTE */
1348 nla_total_size(4) +
1349 /* IFLA_GRE_TTL */
1350 nla_total_size(1) +
1351 /* IFLA_GRE_TOS */
1352 nla_total_size(1) +
1353 /* IFLA_GRE_PMTUDISC */
1354 nla_total_size(1) +
Tom Herbert4565e992014-09-17 12:26:01 -07001355 /* IFLA_GRE_ENCAP_TYPE */
1356 nla_total_size(2) +
1357 /* IFLA_GRE_ENCAP_FLAGS */
1358 nla_total_size(2) +
1359 /* IFLA_GRE_ENCAP_SPORT */
1360 nla_total_size(2) +
1361 /* IFLA_GRE_ENCAP_DPORT */
1362 nla_total_size(2) +
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001363 /* IFLA_GRE_COLLECT_METADATA */
1364 nla_total_size(0) +
Philip Prindeville22a59be2016-06-14 15:53:02 -06001365 /* IFLA_GRE_IGNORE_DF */
1366 nla_total_size(1) +
Craig Gallek9830ad42017-04-19 12:30:54 -04001367 /* IFLA_GRE_FWMARK */
1368 nla_total_size(4) +
William Tu84e54fe2017-08-22 09:40:28 -07001369 /* IFLA_GRE_ERSPAN_INDEX */
1370 nla_total_size(4) +
William Tuf551c912017-12-13 16:38:56 -08001371 /* IFLA_GRE_ERSPAN_VER */
1372 nla_total_size(1) +
1373 /* IFLA_GRE_ERSPAN_DIR */
1374 nla_total_size(1) +
1375 /* IFLA_GRE_ERSPAN_HWID */
1376 nla_total_size(2) +
Herbert Xuc19e6542008-10-09 11:59:55 -07001377 0;
1378}
1379
1380static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1381{
1382 struct ip_tunnel *t = netdev_priv(dev);
1383 struct ip_tunnel_parm *p = &t->parms;
Lorenzo Bianconifeaf5c72019-01-28 22:23:48 +01001384 __be16 o_flags = p->o_flags;
1385
Lorenzo Bianconi2bdf7002019-02-19 17:42:05 +01001386 if (t->erspan_ver == 1 || t->erspan_ver == 2) {
1387 if (!t->collect_md)
1388 o_flags |= TUNNEL_KEY;
1389
1390 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, t->erspan_ver))
1391 goto nla_put_failure;
1392
1393 if (t->erspan_ver == 1) {
1394 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, t->index))
1395 goto nla_put_failure;
1396 } else {
1397 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, t->dir))
1398 goto nla_put_failure;
1399 if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, t->hwid))
1400 goto nla_put_failure;
1401 }
1402 }
Herbert Xuc19e6542008-10-09 11:59:55 -07001403
David S. Millerf3756b72012-04-01 20:39:02 -04001404 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
Tom Herbert95f5c642016-04-29 17:12:16 -07001405 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1406 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1407 nla_put_be16(skb, IFLA_GRE_OFLAGS,
Lorenzo Bianconifeaf5c72019-01-28 22:23:48 +01001408 gre_tnl_flags_to_gre_flags(o_flags)) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001409 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1410 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001411 nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
1412 nla_put_in_addr(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001413 nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
1414 nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
1415 nla_put_u8(skb, IFLA_GRE_PMTUDISC,
Craig Gallek9830ad42017-04-19 12:30:54 -04001416 !!(p->iph.frag_off & htons(IP_DF))) ||
1417 nla_put_u32(skb, IFLA_GRE_FWMARK, t->fwmark))
David S. Millerf3756b72012-04-01 20:39:02 -04001418 goto nla_put_failure;
Tom Herbert4565e992014-09-17 12:26:01 -07001419
1420 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1421 t->encap.type) ||
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001422 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1423 t->encap.sport) ||
1424 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1425 t->encap.dport) ||
Tom Herbert4565e992014-09-17 12:26:01 -07001426 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
Tom Herberte1b2cb62014-11-05 16:49:38 -08001427 t->encap.flags))
Tom Herbert4565e992014-09-17 12:26:01 -07001428 goto nla_put_failure;
1429
Philip Prindeville22a59be2016-06-14 15:53:02 -06001430 if (nla_put_u8(skb, IFLA_GRE_IGNORE_DF, t->ignore_df))
1431 goto nla_put_failure;
1432
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001433 if (t->collect_md) {
1434 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
1435 goto nla_put_failure;
1436 }
1437
Herbert Xuc19e6542008-10-09 11:59:55 -07001438 return 0;
1439
1440nla_put_failure:
1441 return -EMSGSIZE;
1442}
1443
William Tu84e54fe2017-08-22 09:40:28 -07001444static void erspan_setup(struct net_device *dev)
1445{
Xin Long84581bd2018-08-27 18:41:32 +08001446 struct ip_tunnel *t = netdev_priv(dev);
1447
William Tu84e54fe2017-08-22 09:40:28 -07001448 ether_setup(dev);
1449 dev->netdev_ops = &erspan_netdev_ops;
1450 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1451 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1452 ip_tunnel_setup(dev, erspan_net_id);
Xin Long84581bd2018-08-27 18:41:32 +08001453 t->erspan_ver = 1;
William Tu84e54fe2017-08-22 09:40:28 -07001454}
1455
Herbert Xuc19e6542008-10-09 11:59:55 -07001456static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
1457 [IFLA_GRE_LINK] = { .type = NLA_U32 },
1458 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
1459 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
1460 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
1461 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
Patrick McHardy4d74f8b2008-10-10 12:11:06 -07001462 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1463 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
Herbert Xuc19e6542008-10-09 11:59:55 -07001464 [IFLA_GRE_TTL] = { .type = NLA_U8 },
1465 [IFLA_GRE_TOS] = { .type = NLA_U8 },
1466 [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
Tom Herbert4565e992014-09-17 12:26:01 -07001467 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
1468 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
1469 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
1470 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001471 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
Philip Prindeville22a59be2016-06-14 15:53:02 -06001472 [IFLA_GRE_IGNORE_DF] = { .type = NLA_U8 },
Craig Gallek9830ad42017-04-19 12:30:54 -04001473 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
William Tu84e54fe2017-08-22 09:40:28 -07001474 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
William Tuf551c912017-12-13 16:38:56 -08001475 [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
1476 [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
1477 [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
Herbert Xuc19e6542008-10-09 11:59:55 -07001478};
1479
1480static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
1481 .kind = "gre",
1482 .maxtype = IFLA_GRE_MAX,
1483 .policy = ipgre_policy,
1484 .priv_size = sizeof(struct ip_tunnel),
1485 .setup = ipgre_tunnel_setup,
1486 .validate = ipgre_tunnel_validate,
1487 .newlink = ipgre_newlink,
1488 .changelink = ipgre_changelink,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001489 .dellink = ip_tunnel_dellink,
Herbert Xuc19e6542008-10-09 11:59:55 -07001490 .get_size = ipgre_get_size,
1491 .fill_info = ipgre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001492 .get_link_net = ip_tunnel_get_link_net,
Herbert Xuc19e6542008-10-09 11:59:55 -07001493};
1494
Herbert Xue1a80002008-10-09 12:00:17 -07001495static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
1496 .kind = "gretap",
1497 .maxtype = IFLA_GRE_MAX,
1498 .policy = ipgre_policy,
1499 .priv_size = sizeof(struct ip_tunnel),
1500 .setup = ipgre_tap_setup,
1501 .validate = ipgre_tap_validate,
1502 .newlink = ipgre_newlink,
1503 .changelink = ipgre_changelink,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001504 .dellink = ip_tunnel_dellink,
Herbert Xue1a80002008-10-09 12:00:17 -07001505 .get_size = ipgre_get_size,
1506 .fill_info = ipgre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001507 .get_link_net = ip_tunnel_get_link_net,
Herbert Xue1a80002008-10-09 12:00:17 -07001508};
1509
William Tu84e54fe2017-08-22 09:40:28 -07001510static struct rtnl_link_ops erspan_link_ops __read_mostly = {
1511 .kind = "erspan",
1512 .maxtype = IFLA_GRE_MAX,
1513 .policy = ipgre_policy,
1514 .priv_size = sizeof(struct ip_tunnel),
1515 .setup = erspan_setup,
1516 .validate = erspan_validate,
1517 .newlink = ipgre_newlink,
1518 .changelink = ipgre_changelink,
1519 .dellink = ip_tunnel_dellink,
1520 .get_size = ipgre_get_size,
1521 .fill_info = ipgre_fill_info,
1522 .get_link_net = ip_tunnel_get_link_net,
1523};
1524
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001525struct net_device *gretap_fb_dev_create(struct net *net, const char *name,
1526 u8 name_assign_type)
1527{
1528 struct nlattr *tb[IFLA_MAX + 1];
1529 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001530 LIST_HEAD(list_kill);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001531 struct ip_tunnel *t;
1532 int err;
1533
1534 memset(&tb, 0, sizeof(tb));
1535
1536 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08001537 &ipgre_tap_ops, tb, NULL);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001538 if (IS_ERR(dev))
1539 return dev;
1540
1541 /* Configure flow based GRE device. */
1542 t = netdev_priv(dev);
1543 t->collect_md = true;
1544
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001545 err = ipgre_newlink(net, dev, tb, NULL, NULL);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001546 if (err < 0) {
1547 free_netdev(dev);
1548 return ERR_PTR(err);
1549 }
David Wragg7e059152016-02-10 00:05:58 +00001550
1551 /* openvswitch users expect packet sizes to be unrestricted,
1552 * so set the largest MTU we can.
1553 */
1554 err = __ip_tunnel_change_mtu(dev, IP_MAX_MTU, false);
1555 if (err)
1556 goto out;
1557
Nicolas Dichtelda6f1da2016-06-13 10:31:06 +02001558 err = rtnl_configure_link(dev, NULL);
1559 if (err < 0)
1560 goto out;
1561
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001562 return dev;
1563out:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001564 ip_tunnel_dellink(dev, &list_kill);
1565 unregister_netdevice_many(&list_kill);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001566 return ERR_PTR(err);
1567}
1568EXPORT_SYMBOL_GPL(gretap_fb_dev_create);
1569
Pravin B Shelarc5441932013-03-25 14:49:35 +00001570static int __net_init ipgre_tap_init_net(struct net *net)
1571{
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001572 return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, "gretap0");
Pravin B Shelarc5441932013-03-25 14:49:35 +00001573}
1574
Eric Dumazet64bc1782017-09-19 16:27:09 -07001575static void __net_exit ipgre_tap_exit_batch_net(struct list_head *list_net)
Pravin B Shelarc5441932013-03-25 14:49:35 +00001576{
Eric Dumazet64bc1782017-09-19 16:27:09 -07001577 ip_tunnel_delete_nets(list_net, gre_tap_net_id, &ipgre_tap_ops);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001578}
1579
1580static struct pernet_operations ipgre_tap_net_ops = {
1581 .init = ipgre_tap_init_net,
Eric Dumazet64bc1782017-09-19 16:27:09 -07001582 .exit_batch = ipgre_tap_exit_batch_net,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001583 .id = &gre_tap_net_id,
1584 .size = sizeof(struct ip_tunnel_net),
1585};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
William Tu84e54fe2017-08-22 09:40:28 -07001587static int __net_init erspan_init_net(struct net *net)
1588{
1589 return ip_tunnel_init_net(net, erspan_net_id,
1590 &erspan_link_ops, "erspan0");
1591}
1592
Eric Dumazet64bc1782017-09-19 16:27:09 -07001593static void __net_exit erspan_exit_batch_net(struct list_head *net_list)
William Tu84e54fe2017-08-22 09:40:28 -07001594{
Eric Dumazet64bc1782017-09-19 16:27:09 -07001595 ip_tunnel_delete_nets(net_list, erspan_net_id, &erspan_link_ops);
William Tu84e54fe2017-08-22 09:40:28 -07001596}
1597
1598static struct pernet_operations erspan_net_ops = {
1599 .init = erspan_init_net,
Eric Dumazet64bc1782017-09-19 16:27:09 -07001600 .exit_batch = erspan_exit_batch_net,
William Tu84e54fe2017-08-22 09:40:28 -07001601 .id = &erspan_net_id,
1602 .size = sizeof(struct ip_tunnel_net),
1603};
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605static int __init ipgre_init(void)
1606{
1607 int err;
1608
Joe Perches058bd4d2012-03-11 18:36:11 +00001609 pr_info("GRE over IPv4 tunneling driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Eric W. Biedermancfb8fbf2009-11-29 15:46:13 +00001611 err = register_pernet_device(&ipgre_net_ops);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001612 if (err < 0)
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001613 return err;
1614
Pravin B Shelarc5441932013-03-25 14:49:35 +00001615 err = register_pernet_device(&ipgre_tap_net_ops);
1616 if (err < 0)
William Tue3d03282017-08-22 17:04:05 -07001617 goto pnet_tap_failed;
Pravin B Shelarc5441932013-03-25 14:49:35 +00001618
William Tu84e54fe2017-08-22 09:40:28 -07001619 err = register_pernet_device(&erspan_net_ops);
1620 if (err < 0)
1621 goto pnet_erspan_failed;
1622
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001623 err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001624 if (err < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001625 pr_info("%s: can't add protocol\n", __func__);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001626 goto add_proto_failed;
1627 }
Pavel Emelyanov7daa0002008-04-16 01:10:05 -07001628
Herbert Xuc19e6542008-10-09 11:59:55 -07001629 err = rtnl_link_register(&ipgre_link_ops);
1630 if (err < 0)
1631 goto rtnl_link_failed;
1632
Herbert Xue1a80002008-10-09 12:00:17 -07001633 err = rtnl_link_register(&ipgre_tap_ops);
1634 if (err < 0)
1635 goto tap_ops_failed;
1636
William Tu84e54fe2017-08-22 09:40:28 -07001637 err = rtnl_link_register(&erspan_link_ops);
1638 if (err < 0)
1639 goto erspan_link_failed;
1640
Pravin B Shelarc5441932013-03-25 14:49:35 +00001641 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001642
William Tu84e54fe2017-08-22 09:40:28 -07001643erspan_link_failed:
1644 rtnl_link_unregister(&ipgre_tap_ops);
Herbert Xue1a80002008-10-09 12:00:17 -07001645tap_ops_failed:
1646 rtnl_link_unregister(&ipgre_link_ops);
Herbert Xuc19e6542008-10-09 11:59:55 -07001647rtnl_link_failed:
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001648 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001649add_proto_failed:
William Tu84e54fe2017-08-22 09:40:28 -07001650 unregister_pernet_device(&erspan_net_ops);
1651pnet_erspan_failed:
Pravin B Shelarc5441932013-03-25 14:49:35 +00001652 unregister_pernet_device(&ipgre_tap_net_ops);
William Tue3d03282017-08-22 17:04:05 -07001653pnet_tap_failed:
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001654 unregister_pernet_device(&ipgre_net_ops);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001655 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001658static void __exit ipgre_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Herbert Xue1a80002008-10-09 12:00:17 -07001660 rtnl_link_unregister(&ipgre_tap_ops);
Herbert Xuc19e6542008-10-09 11:59:55 -07001661 rtnl_link_unregister(&ipgre_link_ops);
William Tu84e54fe2017-08-22 09:40:28 -07001662 rtnl_link_unregister(&erspan_link_ops);
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001663 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001664 unregister_pernet_device(&ipgre_tap_net_ops);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001665 unregister_pernet_device(&ipgre_net_ops);
William Tu84e54fe2017-08-22 09:40:28 -07001666 unregister_pernet_device(&erspan_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
1669module_init(ipgre_init);
1670module_exit(ipgre_fini);
1671MODULE_LICENSE("GPL");
Patrick McHardy4d74f8b2008-10-10 12:11:06 -07001672MODULE_ALIAS_RTNL_LINK("gre");
1673MODULE_ALIAS_RTNL_LINK("gretap");
William Tu84e54fe2017-08-22 09:40:28 -07001674MODULE_ALIAS_RTNL_LINK("erspan");
Vasiliy Kulikov8909c9a2011-03-02 00:33:13 +03001675MODULE_ALIAS_NETDEV("gre0");
Pravin B Shelarc5441932013-03-25 14:49:35 +00001676MODULE_ALIAS_NETDEV("gretap0");
William Tu84e54fe2017-08-22 09:40:28 -07001677MODULE_ALIAS_NETDEV("erspan0");