blob: 4e31f23e4117ecdb30a5d8522e2100b79cac654b [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 Tuf989d542020-05-05 09:05:06 -0700251static bool is_erspan_type1(int gre_hdr_len)
252{
253 /* Both ERSPAN type I (version 0) and type II (version 1) use
254 * protocol 0x88BE, but the type I has only 4-byte GRE header,
255 * while type II has 8-byte.
256 */
257 return gre_hdr_len == 4;
258}
259
William Tu84e54fe2017-08-22 09:40:28 -0700260static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi,
261 int gre_hdr_len)
262{
263 struct net *net = dev_net(skb->dev);
264 struct metadata_dst *tun_dst = NULL;
William Tu1d7e2ed2017-12-13 16:38:55 -0800265 struct erspan_base_hdr *ershdr;
William Tu84e54fe2017-08-22 09:40:28 -0700266 struct ip_tunnel_net *itn;
267 struct ip_tunnel *tunnel;
William Tu84e54fe2017-08-22 09:40:28 -0700268 const struct iphdr *iph;
William Tu3df192832018-02-05 13:35:34 -0800269 struct erspan_md2 *md2;
William Tu1d7e2ed2017-12-13 16:38:55 -0800270 int ver;
William Tu84e54fe2017-08-22 09:40:28 -0700271 int len;
272
273 itn = net_generic(net, erspan_net_id);
William Tu84e54fe2017-08-22 09:40:28 -0700274 iph = ip_hdr(skb);
William Tuf989d542020-05-05 09:05:06 -0700275 if (is_erspan_type1(gre_hdr_len)) {
276 ver = 0;
277 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex,
278 tpi->flags | TUNNEL_NO_KEY,
279 iph->saddr, iph->daddr, 0);
280 } else {
281 ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len);
282 ver = ershdr->ver;
283 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex,
284 tpi->flags | TUNNEL_KEY,
285 iph->saddr, iph->daddr, tpi->key);
286 }
William Tu84e54fe2017-08-22 09:40:28 -0700287
288 if (tunnel) {
William Tuf989d542020-05-05 09:05:06 -0700289 if (is_erspan_type1(gre_hdr_len))
290 len = gre_hdr_len;
291 else
292 len = gre_hdr_len + erspan_hdr_len(ver);
293
William Tu1d7e2ed2017-12-13 16:38:55 -0800294 if (unlikely(!pskb_may_pull(skb, len)))
William Tuae3e1332017-12-15 14:27:43 -0800295 return PACKET_REJECT;
William Tu1d7e2ed2017-12-13 16:38:55 -0800296
William Tu84e54fe2017-08-22 09:40:28 -0700297 if (__iptunnel_pull_header(skb,
William Tu1d7e2ed2017-12-13 16:38:55 -0800298 len,
William Tu84e54fe2017-08-22 09:40:28 -0700299 htons(ETH_P_TEB),
300 false, false) < 0)
301 goto drop;
302
William Tu1a66a832017-08-25 09:21:28 -0700303 if (tunnel->collect_md) {
Lorenzo Bianconi492b67e2019-04-06 17:16:52 +0200304 struct erspan_metadata *pkt_md, *md;
William Tu1a66a832017-08-25 09:21:28 -0700305 struct ip_tunnel_info *info;
Lorenzo Bianconi492b67e2019-04-06 17:16:52 +0200306 unsigned char *gh;
William Tu1a66a832017-08-25 09:21:28 -0700307 __be64 tun_id;
308 __be16 flags;
309
310 tpi->flags |= TUNNEL_KEY;
311 flags = tpi->flags;
312 tun_id = key32_to_tunnel_id(tpi->key);
313
314 tun_dst = ip_tun_rx_dst(skb, flags,
315 tun_id, sizeof(*md));
316 if (!tun_dst)
317 return PACKET_REJECT;
318
Lorenzo Bianconi492b67e2019-04-06 17:16:52 +0200319 /* skb can be uncloned in __iptunnel_pull_header, so
320 * old pkt_md is no longer valid and we need to reset
321 * it
322 */
323 gh = skb_network_header(skb) +
324 skb_network_header_len(skb);
325 pkt_md = (struct erspan_metadata *)(gh + gre_hdr_len +
326 sizeof(*ershdr));
William Tu1a66a832017-08-25 09:21:28 -0700327 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
William Tuf551c912017-12-13 16:38:56 -0800328 md->version = ver;
William Tu3df192832018-02-05 13:35:34 -0800329 md2 = &md->u.md2;
330 memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
331 ERSPAN_V2_MDSIZE);
William Tuf551c912017-12-13 16:38:56 -0800332
William Tu1a66a832017-08-25 09:21:28 -0700333 info = &tun_dst->u.tun_info;
334 info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
335 info->options_len = sizeof(*md);
William Tu1a66a832017-08-25 09:21:28 -0700336 }
337
William Tu84e54fe2017-08-22 09:40:28 -0700338 skb_reset_mac_header(skb);
339 ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
340 return PACKET_RCVD;
341 }
Haishuang Yan5a645062018-09-10 22:19:47 +0800342 return PACKET_REJECT;
343
William Tu84e54fe2017-08-22 09:40:28 -0700344drop:
345 kfree_skb(skb);
346 return PACKET_RCVD;
347}
348
Jiri Benc125372f2016-05-03 17:10:08 +0200349static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
350 struct ip_tunnel_net *itn, int hdr_len, bool raw_proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700352 struct metadata_dst *tun_dst = NULL;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000353 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct ip_tunnel *tunnel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700356 iph = ip_hdr(skb);
Pravin B Shelarbda7bb42013-06-17 17:49:38 -0700357 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
358 iph->saddr, iph->daddr, tpi->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
stephen hemmingerd2083282012-09-24 18:12:23 +0000360 if (tunnel) {
wenxuc0d59da2019-11-20 10:59:39 +0800361 const struct iphdr *tnl_params;
362
Jiri Benc125372f2016-05-03 17:10:08 +0200363 if (__iptunnel_pull_header(skb, hdr_len, tpi->proto,
364 raw_proto, false) < 0)
Jiri Benc244a7972016-05-03 17:10:07 +0200365 goto drop;
366
Jiri Bence271c7b2016-05-11 15:53:57 +0200367 if (tunnel->dev->type != ARPHRD_NONE)
368 skb_pop_mac_header(skb);
369 else
370 skb_reset_mac_header(skb);
wenxuc0d59da2019-11-20 10:59:39 +0800371
372 tnl_params = &tunnel->parms.iph;
373 if (tunnel->collect_md || tnl_params->daddr == 0) {
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700374 __be16 flags;
375 __be64 tun_id;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700376
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700377 flags = tpi->flags & (TUNNEL_CSUM | TUNNEL_KEY);
Amir Vadaid817f432016-09-08 16:23:45 +0300378 tun_id = key32_to_tunnel_id(tpi->key);
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700379 tun_dst = ip_tun_rx_dst(skb, flags, tun_id, 0);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700380 if (!tun_dst)
381 return PACKET_REJECT;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700382 }
383
384 ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
Pravin B Shelarbda7bb42013-06-17 17:49:38 -0700385 return PACKET_RCVD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
Jiri Benc125372f2016-05-03 17:10:08 +0200387 return PACKET_NEXT;
Jiri Benc244a7972016-05-03 17:10:07 +0200388
389drop:
390 kfree_skb(skb);
391 return PACKET_RCVD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Jiri Benc125372f2016-05-03 17:10:08 +0200394static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
395 int hdr_len)
396{
397 struct net *net = dev_net(skb->dev);
398 struct ip_tunnel_net *itn;
399 int res;
400
401 if (tpi->proto == htons(ETH_P_TEB))
402 itn = net_generic(net, gre_tap_net_id);
403 else
404 itn = net_generic(net, ipgre_net_id);
405
406 res = __ipgre_rcv(skb, tpi, itn, hdr_len, false);
407 if (res == PACKET_NEXT && tpi->proto == htons(ETH_P_TEB)) {
408 /* ipgre tunnels in collect metadata mode should receive
409 * also ETH_P_TEB traffic.
410 */
411 itn = net_generic(net, ipgre_net_id);
412 res = __ipgre_rcv(skb, tpi, itn, hdr_len, true);
413 }
414 return res;
415}
416
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700417static int gre_rcv(struct sk_buff *skb)
418{
419 struct tnl_ptk_info tpi;
420 bool csum_err = false;
Tom Herbert95f5c642016-04-29 17:12:16 -0700421 int hdr_len;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700422
423#ifdef CONFIG_NET_IPGRE_BROADCAST
424 if (ipv4_is_multicast(ip_hdr(skb)->daddr)) {
425 /* Looped back packet, drop it! */
426 if (rt_is_output_route(skb_rtable(skb)))
427 goto drop;
428 }
429#endif
430
Eric Dumazete582615ad2016-06-15 06:24:00 -0700431 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP), 0);
Jiri Bencf132ae72016-05-03 15:00:21 +0200432 if (hdr_len < 0)
Tom Herbert95f5c642016-04-29 17:12:16 -0700433 goto drop;
434
William Tuf551c912017-12-13 16:38:56 -0800435 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
436 tpi.proto == htons(ETH_P_ERSPAN2))) {
William Tu84e54fe2017-08-22 09:40:28 -0700437 if (erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
438 return 0;
Haishuang Yandd8d5b82017-12-20 10:21:46 +0800439 goto out;
William Tu84e54fe2017-08-22 09:40:28 -0700440 }
441
Jiri Benc244a7972016-05-03 17:10:07 +0200442 if (ipgre_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700443 return 0;
444
Haishuang Yandd8d5b82017-12-20 10:21:46 +0800445out:
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700446 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
447drop:
448 kfree_skb(skb);
449 return 0;
450}
451
Pravin B Shelarc5441932013-03-25 14:49:35 +0000452static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
453 const struct iphdr *tnl_params,
454 __be16 proto)
455{
456 struct ip_tunnel *tunnel = netdev_priv(dev);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000457
Pravin B Shelarc5441932013-03-25 14:49:35 +0000458 if (tunnel->parms.o_flags & TUNNEL_SEQ)
459 tunnel->o_seqno++;
Eric Dumazetcef401d2013-01-25 20:34:37 +0000460
Pravin B Shelarc5441932013-03-25 14:49:35 +0000461 /* Push GRE header. */
Tom Herbert182a3522016-04-29 17:12:19 -0700462 gre_build_header(skb, tunnel->tun_hlen,
463 tunnel->parms.o_flags, proto, tunnel->parms.o_key,
464 htonl(tunnel->o_seqno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Nicolas Dichtelbf3d6a82013-05-27 23:48:15 +0000466 ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Alexander Duyckaed069d2016-04-14 15:33:37 -0400469static int gre_handle_offloads(struct sk_buff *skb, bool csum)
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -0700470{
Edward Cree6fa79662016-02-11 21:02:31 +0000471 return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -0700472}
473
William Tu862a03c2017-08-25 09:21:27 -0700474static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev,
475 __be16 proto)
476{
William Tu77a51962018-03-01 13:49:57 -0800477 struct ip_tunnel *tunnel = netdev_priv(dev);
William Tu862a03c2017-08-25 09:21:27 -0700478 struct ip_tunnel_info *tun_info;
479 const struct ip_tunnel_key *key;
William Tu862a03c2017-08-25 09:21:27 -0700480 int tunnel_hlen;
wenxu962924f2019-01-22 18:39:52 +0800481 __be16 flags;
William Tu862a03c2017-08-25 09:21:27 -0700482
483 tun_info = skb_tunnel_info(skb);
484 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
485 ip_tunnel_info_af(tun_info) != AF_INET))
486 goto err_free_skb;
487
488 key = &tun_info->key;
489 tunnel_hlen = gre_calc_hlen(key->tun_flags);
490
wenxu962924f2019-01-22 18:39:52 +0800491 if (skb_cow_head(skb, dev->needed_headroom))
492 goto err_free_skb;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700493
494 /* Push Tunnel header. */
Alexander Duyckaed069d2016-04-14 15:33:37 -0400495 if (gre_handle_offloads(skb, !!(tun_info->key.tun_flags & TUNNEL_CSUM)))
wenxu962924f2019-01-22 18:39:52 +0800496 goto err_free_skb;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700497
William Tu77a51962018-03-01 13:49:57 -0800498 flags = tun_info->key.tun_flags &
499 (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ);
David S. Millercba653212016-05-04 00:52:29 -0400500 gre_build_header(skb, tunnel_hlen, flags, proto,
William Tu77a51962018-03-01 13:49:57 -0800501 tunnel_id_to_key32(tun_info->key.tun_id),
Colin Ian King15746392018-03-21 19:34:58 +0000502 (flags & TUNNEL_SEQ) ? htonl(tunnel->o_seqno++) : 0);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700503
wenxu962924f2019-01-22 18:39:52 +0800504 ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen);
Pravin B Shelar039f5062015-12-24 14:34:54 -0800505
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700506 return;
507
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700508err_free_skb:
509 kfree_skb(skb);
510 dev->stats.tx_dropped++;
511}
512
Xin Long20704bd2019-01-14 18:10:06 +0800513static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev)
William Tu1a66a832017-08-25 09:21:28 -0700514{
515 struct ip_tunnel *tunnel = netdev_priv(dev);
516 struct ip_tunnel_info *tun_info;
517 const struct ip_tunnel_key *key;
518 struct erspan_metadata *md;
William Tu1a66a832017-08-25 09:21:28 -0700519 bool truncate = false;
wenxu962924f2019-01-22 18:39:52 +0800520 __be16 proto;
William Tu1a66a832017-08-25 09:21:28 -0700521 int tunnel_hlen;
William Tuf551c912017-12-13 16:38:56 -0800522 int version;
William Tu1baf5eb2018-04-27 14:16:32 -0700523 int nhoff;
William Tud5db21a2018-05-11 05:49:47 -0700524 int thoff;
William Tu1a66a832017-08-25 09:21:28 -0700525
526 tun_info = skb_tunnel_info(skb);
527 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
528 ip_tunnel_info_af(tun_info) != AF_INET))
529 goto err_free_skb;
530
531 key = &tun_info->key;
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700532 if (!(tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT))
wenxu962924f2019-01-22 18:39:52 +0800533 goto err_free_skb;
Xin Long2eb8d6d2019-10-28 23:19:35 +0800534 if (tun_info->options_len < sizeof(*md))
wenxu962924f2019-01-22 18:39:52 +0800535 goto err_free_skb;
Xin Long2eb8d6d2019-10-28 23:19:35 +0800536 md = ip_tunnel_info_opts(tun_info);
William Tu1a66a832017-08-25 09:21:28 -0700537
538 /* ERSPAN has fixed 8 byte GRE header */
William Tuf551c912017-12-13 16:38:56 -0800539 version = md->version;
540 tunnel_hlen = 8 + erspan_hdr_len(version);
William Tu1a66a832017-08-25 09:21:28 -0700541
wenxu962924f2019-01-22 18:39:52 +0800542 if (skb_cow_head(skb, dev->needed_headroom))
543 goto err_free_skb;
William Tu1a66a832017-08-25 09:21:28 -0700544
545 if (gre_handle_offloads(skb, false))
wenxu962924f2019-01-22 18:39:52 +0800546 goto err_free_skb;
William Tu1a66a832017-08-25 09:21:28 -0700547
William Tuf1929702017-10-05 12:07:12 -0700548 if (skb->len > dev->mtu + dev->hard_header_len) {
549 pskb_trim(skb, dev->mtu + dev->hard_header_len);
William Tu1a66a832017-08-25 09:21:28 -0700550 truncate = true;
551 }
552
William Tu1baf5eb2018-04-27 14:16:32 -0700553 nhoff = skb_network_header(skb) - skb_mac_header(skb);
554 if (skb->protocol == htons(ETH_P_IP) &&
555 (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
556 truncate = true;
557
William Tud5db21a2018-05-11 05:49:47 -0700558 thoff = skb_transport_header(skb) - skb_mac_header(skb);
559 if (skb->protocol == htons(ETH_P_IPV6) &&
560 (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff))
561 truncate = true;
562
William Tuf551c912017-12-13 16:38:56 -0800563 if (version == 1) {
William Tuc69de582018-01-25 13:20:09 -0800564 erspan_build_header(skb, ntohl(tunnel_id_to_key32(key->tun_id)),
William Tuf551c912017-12-13 16:38:56 -0800565 ntohl(md->u.index), truncate, true);
Xin Long20704bd2019-01-14 18:10:06 +0800566 proto = htons(ETH_P_ERSPAN);
William Tuf551c912017-12-13 16:38:56 -0800567 } else if (version == 2) {
William Tuc69de582018-01-25 13:20:09 -0800568 erspan_build_header_v2(skb,
569 ntohl(tunnel_id_to_key32(key->tun_id)),
570 md->u.md2.dir,
571 get_hwid(&md->u.md2),
572 truncate, true);
Xin Long20704bd2019-01-14 18:10:06 +0800573 proto = htons(ETH_P_ERSPAN2);
William Tuf551c912017-12-13 16:38:56 -0800574 } else {
wenxu962924f2019-01-22 18:39:52 +0800575 goto err_free_skb;
William Tuf551c912017-12-13 16:38:56 -0800576 }
William Tu1a66a832017-08-25 09:21:28 -0700577
578 gre_build_header(skb, 8, TUNNEL_SEQ,
Xin Long20704bd2019-01-14 18:10:06 +0800579 proto, 0, htonl(tunnel->o_seqno++));
William Tu1a66a832017-08-25 09:21:28 -0700580
wenxu962924f2019-01-22 18:39:52 +0800581 ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen);
William Tu1a66a832017-08-25 09:21:28 -0700582
William Tu1a66a832017-08-25 09:21:28 -0700583 return;
584
William Tu1a66a832017-08-25 09:21:28 -0700585err_free_skb:
586 kfree_skb(skb);
587 dev->stats.tx_dropped++;
588}
589
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700590static int gre_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
591{
592 struct ip_tunnel_info *info = skb_tunnel_info(skb);
wenxu962924f2019-01-22 18:39:52 +0800593 const struct ip_tunnel_key *key;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700594 struct rtable *rt;
595 struct flowi4 fl4;
596
597 if (ip_tunnel_info_af(info) != AF_INET)
598 return -EINVAL;
599
wenxu962924f2019-01-22 18:39:52 +0800600 key = &info->key;
601 ip_tunnel_init_flow(&fl4, IPPROTO_GRE, key->u.ipv4.dst, key->u.ipv4.src,
602 tunnel_id_to_key32(key->tun_id), key->tos, 0,
wenxu24ba1442019-02-24 11:36:20 +0800603 skb->mark, skb_get_hash(skb));
wenxu962924f2019-01-22 18:39:52 +0800604 rt = ip_route_output_key(dev_net(dev), &fl4);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700605 if (IS_ERR(rt))
606 return PTR_ERR(rt);
607
608 ip_rt_put(rt);
609 info->key.u.ipv4.src = fl4.saddr;
610 return 0;
611}
612
Pravin B Shelarc5441932013-03-25 14:49:35 +0000613static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
614 struct net_device *dev)
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800615{
Pravin B Shelarc5441932013-03-25 14:49:35 +0000616 struct ip_tunnel *tunnel = netdev_priv(dev);
617 const struct iphdr *tnl_params;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800618
Willem de Bruijncb9f1b72018-12-30 17:24:36 -0500619 if (!pskb_inet_may_pull(skb))
620 goto free_skb;
621
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700622 if (tunnel->collect_md) {
Jiri Benc20907142016-04-27 11:29:07 +0200623 gre_fb_xmit(skb, dev, skb->protocol);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700624 return NETDEV_TX_OK;
625 }
626
Pravin B Shelarc5441932013-03-25 14:49:35 +0000627 if (dev->header_ops) {
628 /* Need space for new headers */
629 if (skb_cow_head(skb, dev->needed_headroom -
Chen Gang2bac7cb2013-04-22 20:45:42 +0000630 (tunnel->hlen + sizeof(struct iphdr))))
Pravin B Shelarc5441932013-03-25 14:49:35 +0000631 goto free_skb;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800632
Pravin B Shelarc5441932013-03-25 14:49:35 +0000633 tnl_params = (const struct iphdr *)skb->data;
Eric Dumazete985aad2010-09-27 03:57:11 +0000634
Pravin B Shelarc5441932013-03-25 14:49:35 +0000635 /* Pull skb since ip_tunnel_xmit() needs skb->data pointing
636 * to gre header.
637 */
638 skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
Timo Teräs8a0033a2014-12-15 09:24:13 +0200639 skb_reset_mac_header(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000640 } else {
641 if (skb_cow_head(skb, dev->needed_headroom))
642 goto free_skb;
Herbert Xue1a80002008-10-09 12:00:17 -0700643
Pravin B Shelarc5441932013-03-25 14:49:35 +0000644 tnl_params = &tunnel->parms.iph;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800645 }
646
Alexander Duyckaed069d2016-04-14 15:33:37 -0400647 if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
648 goto free_skb;
Timo Teräs8a0033a2014-12-15 09:24:13 +0200649
Pravin B Shelarc5441932013-03-25 14:49:35 +0000650 __gre_xmit(skb, dev, tnl_params, skb->protocol);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000651 return NETDEV_TX_OK;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800652
Pravin B Shelarc5441932013-03-25 14:49:35 +0000653free_skb:
Eric Dumazet3acfa1e2014-01-18 18:27:49 -0800654 kfree_skb(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000655 dev->stats.tx_dropped++;
656 return NETDEV_TX_OK;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800657}
658
William Tu84e54fe2017-08-22 09:40:28 -0700659static netdev_tx_t erspan_xmit(struct sk_buff *skb,
660 struct net_device *dev)
661{
662 struct ip_tunnel *tunnel = netdev_priv(dev);
663 bool truncate = false;
Xin Long20704bd2019-01-14 18:10:06 +0800664 __be16 proto;
William Tu84e54fe2017-08-22 09:40:28 -0700665
Willem de Bruijncb9f1b72018-12-30 17:24:36 -0500666 if (!pskb_inet_may_pull(skb))
667 goto free_skb;
668
William Tu1a66a832017-08-25 09:21:28 -0700669 if (tunnel->collect_md) {
Xin Long20704bd2019-01-14 18:10:06 +0800670 erspan_fb_xmit(skb, dev);
William Tu1a66a832017-08-25 09:21:28 -0700671 return NETDEV_TX_OK;
672 }
673
William Tu84e54fe2017-08-22 09:40:28 -0700674 if (gre_handle_offloads(skb, false))
675 goto free_skb;
676
677 if (skb_cow_head(skb, dev->needed_headroom))
678 goto free_skb;
679
William Tuf1929702017-10-05 12:07:12 -0700680 if (skb->len > dev->mtu + dev->hard_header_len) {
681 pskb_trim(skb, dev->mtu + dev->hard_header_len);
William Tu84e54fe2017-08-22 09:40:28 -0700682 truncate = true;
683 }
684
685 /* Push ERSPAN header */
William Tuf989d542020-05-05 09:05:06 -0700686 if (tunnel->erspan_ver == 0) {
687 proto = htons(ETH_P_ERSPAN);
688 tunnel->parms.o_flags &= ~TUNNEL_SEQ;
689 } else if (tunnel->erspan_ver == 1) {
William Tuc69de582018-01-25 13:20:09 -0800690 erspan_build_header(skb, ntohl(tunnel->parms.o_key),
691 tunnel->index,
William Tuf551c912017-12-13 16:38:56 -0800692 truncate, true);
Xin Long20704bd2019-01-14 18:10:06 +0800693 proto = htons(ETH_P_ERSPAN);
694 } else if (tunnel->erspan_ver == 2) {
William Tuc69de582018-01-25 13:20:09 -0800695 erspan_build_header_v2(skb, ntohl(tunnel->parms.o_key),
William Tuf551c912017-12-13 16:38:56 -0800696 tunnel->dir, tunnel->hwid,
697 truncate, true);
Xin Long20704bd2019-01-14 18:10:06 +0800698 proto = htons(ETH_P_ERSPAN2);
699 } else {
William Tu02f99df2018-05-16 17:24:32 -0700700 goto free_skb;
Xin Long20704bd2019-01-14 18:10:06 +0800701 }
William Tuf551c912017-12-13 16:38:56 -0800702
William Tu84e54fe2017-08-22 09:40:28 -0700703 tunnel->parms.o_flags &= ~TUNNEL_KEY;
Xin Long20704bd2019-01-14 18:10:06 +0800704 __gre_xmit(skb, dev, &tunnel->parms.iph, proto);
William Tu84e54fe2017-08-22 09:40:28 -0700705 return NETDEV_TX_OK;
706
707free_skb:
708 kfree_skb(skb);
709 dev->stats.tx_dropped++;
710 return NETDEV_TX_OK;
711}
712
Pravin B Shelarc5441932013-03-25 14:49:35 +0000713static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
714 struct net_device *dev)
715{
716 struct ip_tunnel *tunnel = netdev_priv(dev);
717
Willem de Bruijncb9f1b72018-12-30 17:24:36 -0500718 if (!pskb_inet_may_pull(skb))
719 goto free_skb;
720
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700721 if (tunnel->collect_md) {
Jiri Benc20907142016-04-27 11:29:07 +0200722 gre_fb_xmit(skb, dev, htons(ETH_P_TEB));
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700723 return NETDEV_TX_OK;
724 }
725
Alexander Duyckaed069d2016-04-14 15:33:37 -0400726 if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
727 goto free_skb;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000728
729 if (skb_cow_head(skb, dev->needed_headroom))
730 goto free_skb;
731
732 __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB));
Pravin B Shelarc5441932013-03-25 14:49:35 +0000733 return NETDEV_TX_OK;
734
735free_skb:
Eric Dumazet3acfa1e2014-01-18 18:27:49 -0800736 kfree_skb(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000737 dev->stats.tx_dropped++;
738 return NETDEV_TX_OK;
739}
740
Xin Longdd9d5982017-11-07 16:33:08 +0800741static void ipgre_link_update(struct net_device *dev, bool set_mtu)
742{
743 struct ip_tunnel *tunnel = netdev_priv(dev);
744 int len;
745
746 len = tunnel->tun_hlen;
747 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
748 len = tunnel->tun_hlen - len;
749 tunnel->hlen = tunnel->hlen + len;
750
751 dev->needed_headroom = dev->needed_headroom + len;
752 if (set_mtu)
753 dev->mtu = max_t(int, dev->mtu - len, 68);
754
755 if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
756 if (!(tunnel->parms.o_flags & TUNNEL_CSUM) ||
757 tunnel->encap.type == TUNNEL_ENCAP_NONE) {
758 dev->features |= NETIF_F_GSO_SOFTWARE;
759 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Sabrina Dubroca1cc59542018-04-10 12:57:18 +0200760 } else {
761 dev->features &= ~NETIF_F_GSO_SOFTWARE;
762 dev->hw_features &= ~NETIF_F_GSO_SOFTWARE;
Xin Longdd9d5982017-11-07 16:33:08 +0800763 }
764 dev->features |= NETIF_F_LLTX;
Sabrina Dubroca1cc59542018-04-10 12:57:18 +0200765 } else {
766 dev->hw_features &= ~NETIF_F_GSO_SOFTWARE;
767 dev->features &= ~(NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE);
Xin Longdd9d5982017-11-07 16:33:08 +0800768 }
769}
770
Christoph Hellwig607259a62020-05-19 15:03:13 +0200771static int ipgre_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p,
772 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Xin Longa0efab62017-11-07 16:33:09 +0800774 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Cong Wang6c734fb2013-06-29 12:02:59 +0800776 if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
Christoph Hellwig607259a62020-05-19 15:03:13 +0200777 if (p->iph.version != 4 || p->iph.protocol != IPPROTO_GRE ||
778 p->iph.ihl != 5 || (p->iph.frag_off & htons(~IP_DF)) ||
779 ((p->i_flags | p->o_flags) & (GRE_VERSION | GRE_ROUTING)))
Cong Wang6c734fb2013-06-29 12:02:59 +0800780 return -EINVAL;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000781 }
Xin Longa0efab62017-11-07 16:33:09 +0800782
Christoph Hellwig607259a62020-05-19 15:03:13 +0200783 p->i_flags = gre_flags_to_tnl_flags(p->i_flags);
784 p->o_flags = gre_flags_to_tnl_flags(p->o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000785
Christoph Hellwig607259a62020-05-19 15:03:13 +0200786 err = ip_tunnel_ctl(dev, p, cmd);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000787 if (err)
788 return err;
789
Xin Longa0efab62017-11-07 16:33:09 +0800790 if (cmd == SIOCCHGTUNNEL) {
791 struct ip_tunnel *t = netdev_priv(dev);
792
Christoph Hellwig607259a62020-05-19 15:03:13 +0200793 t->parms.i_flags = p->i_flags;
794 t->parms.o_flags = p->o_flags;
Xin Longa0efab62017-11-07 16:33:09 +0800795
796 if (strcmp(dev->rtnl_link_ops->kind, "erspan"))
797 ipgre_link_update(dev, true);
798 }
799
Christoph Hellwig607259a62020-05-19 15:03:13 +0200800 p->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
801 p->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return 0;
803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805/* Nice toy. Unfortunately, useless in real life :-)
806 It allows to construct virtual multiprotocol broadcast "LAN"
807 over the Internet, provided multicast routing is tuned.
808
809
810 I have no idea was this bicycle invented before me,
811 so that I had to set ARPHRD_IPGRE to a random value.
812 I have an impression, that Cisco could make something similar,
813 but this feature is apparently missing in IOS<=11.2(8).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
816 with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
817
818 ping -t 255 224.66.66.66
819
820 If nobody answers, mbone does not work.
821
822 ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
823 ip addr add 10.66.66.<somewhat>/24 dev Universe
824 ifconfig Universe up
825 ifconfig Universe add fe80::<Your_real_addr>/10
826 ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
827 ftp 10.66.66.66
828 ...
829 ftp fec0:6666:6666::193.233.7.65
830 ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 */
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700832static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
833 unsigned short type,
Eric Dumazet15078502010-09-15 11:07:53 +0000834 const void *daddr, const void *saddr, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Patrick McHardy2941a482006-01-08 22:05:26 -0800836 struct ip_tunnel *t = netdev_priv(dev);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000837 struct iphdr *iph;
838 struct gre_base_hdr *greh;
839
Johannes Bergd58ff352017-06-16 14:29:23 +0200840 iph = skb_push(skb, t->hlen + sizeof(*iph));
Pravin B Shelarc5441932013-03-25 14:49:35 +0000841 greh = (struct gre_base_hdr *)(iph+1);
Tom Herbert95f5c642016-04-29 17:12:16 -0700842 greh->flags = gre_tnl_flags_to_gre_flags(t->parms.o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000843 greh->protocol = htons(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Pravin B Shelarc5441932013-03-25 14:49:35 +0000847 /* Set the source hardware address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (saddr)
849 memcpy(&iph->saddr, saddr, 4);
Timo Teräs6d55cb92010-03-03 04:01:13 +0000850 if (daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 memcpy(&iph->daddr, daddr, 4);
Timo Teräs6d55cb92010-03-03 04:01:13 +0000852 if (iph->daddr)
Timo Teräs77a482b2013-08-06 13:45:43 +0300853 return t->hlen + sizeof(*iph);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900854
Pravin B Shelarc5441932013-03-25 14:49:35 +0000855 return -(t->hlen + sizeof(*iph));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Timo Teras6a5f44d2007-10-23 20:31:53 -0700858static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
859{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000860 const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb);
Timo Teras6a5f44d2007-10-23 20:31:53 -0700861 memcpy(haddr, &iph->saddr, 4);
862 return 4;
863}
864
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700865static const struct header_ops ipgre_header_ops = {
866 .create = ipgre_header,
Timo Teras6a5f44d2007-10-23 20:31:53 -0700867 .parse = ipgre_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700868};
869
Timo Teras6a5f44d2007-10-23 20:31:53 -0700870#ifdef CONFIG_NET_IPGRE_BROADCAST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871static int ipgre_open(struct net_device *dev)
872{
Patrick McHardy2941a482006-01-08 22:05:26 -0800873 struct ip_tunnel *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Joe Perchesf97c1e02007-12-16 13:45:43 -0800875 if (ipv4_is_multicast(t->parms.iph.daddr)) {
David S. Millercbb1e852011-05-04 12:33:34 -0700876 struct flowi4 fl4;
877 struct rtable *rt;
Eric Dumazete985aad2010-09-27 03:57:11 +0000878
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200879 rt = ip_route_output_gre(t->net, &fl4,
David S. Millercbb1e852011-05-04 12:33:34 -0700880 t->parms.iph.daddr,
881 t->parms.iph.saddr,
882 t->parms.o_key,
883 RT_TOS(t->parms.iph.tos),
884 t->parms.link);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800885 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return -EADDRNOTAVAIL;
Changli Gaod8d1f302010-06-10 23:31:35 -0700887 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 ip_rt_put(rt);
Ian Morris51456b22015-04-03 09:17:26 +0100889 if (!__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return -EADDRNOTAVAIL;
891 t->mlink = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -0700892 ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894 return 0;
895}
896
897static int ipgre_close(struct net_device *dev)
898{
Patrick McHardy2941a482006-01-08 22:05:26 -0800899 struct ip_tunnel *t = netdev_priv(dev);
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800900
Joe Perchesf97c1e02007-12-16 13:45:43 -0800901 if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
Denis V. Lunev7fee0ca2008-01-21 17:32:38 -0800902 struct in_device *in_dev;
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200903 in_dev = inetdev_by_index(t->net, t->mlink);
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000904 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 ip_mc_dec_group(in_dev, t->parms.iph.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907 return 0;
908}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909#endif
910
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800911static const struct net_device_ops ipgre_netdev_ops = {
912 .ndo_init = ipgre_tunnel_init,
Pravin B Shelarc5441932013-03-25 14:49:35 +0000913 .ndo_uninit = ip_tunnel_uninit,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800914#ifdef CONFIG_NET_IPGRE_BROADCAST
915 .ndo_open = ipgre_open,
916 .ndo_stop = ipgre_close,
917#endif
Pravin B Shelarc5441932013-03-25 14:49:35 +0000918 .ndo_start_xmit = ipgre_xmit,
Christoph Hellwig607259a62020-05-19 15:03:13 +0200919 .ndo_do_ioctl = ip_tunnel_ioctl,
Pravin B Shelarc5441932013-03-25 14:49:35 +0000920 .ndo_change_mtu = ip_tunnel_change_mtu,
921 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtel1e995842015-04-02 17:07:02 +0200922 .ndo_get_iflink = ip_tunnel_get_iflink,
Christoph Hellwig607259a62020-05-19 15:03:13 +0200923 .ndo_tunnel_ctl = ipgre_tunnel_ctl,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800924};
925
Eric Dumazet6b78f162012-09-13 21:25:33 +0000926#define GRE_FEATURES (NETIF_F_SG | \
927 NETIF_F_FRAGLIST | \
928 NETIF_F_HIGHDMA | \
929 NETIF_F_HW_CSUM)
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931static void ipgre_tunnel_setup(struct net_device *dev)
932{
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800933 dev->netdev_ops = &ipgre_netdev_ops;
Nicolas Dichtel5a455272014-04-11 15:51:18 +0200934 dev->type = ARPHRD_IPGRE;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000935 ip_tunnel_setup(dev, ipgre_net_id);
936}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Pravin B Shelarc5441932013-03-25 14:49:35 +0000938static void __gre_tunnel_init(struct net_device *dev)
939{
940 struct ip_tunnel *tunnel;
941
942 tunnel = netdev_priv(dev);
Tom Herbert95f5c642016-04-29 17:12:16 -0700943 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000944 tunnel->parms.iph.protocol = IPPROTO_GRE;
945
Tom Herbert4565e992014-09-17 12:26:01 -0700946 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
947
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200948 dev->features |= GRE_FEATURES;
Eric Dumazet6b78f162012-09-13 21:25:33 +0000949 dev->hw_features |= GRE_FEATURES;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000950
951 if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
Alexander Duycka0ca1532016-04-05 09:13:39 -0700952 /* TCP offload with GRE SEQ is not supported, nor
953 * can we support 2 levels of outer headers requiring
954 * an update.
955 */
956 if (!(tunnel->parms.o_flags & TUNNEL_CSUM) ||
957 (tunnel->encap.type == TUNNEL_ENCAP_NONE)) {
958 dev->features |= NETIF_F_GSO_SOFTWARE;
959 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
960 }
961
Pravin B Shelarc5441932013-03-25 14:49:35 +0000962 /* Can use a lockless transmit, unless we generate
963 * output sequences
964 */
965 dev->features |= NETIF_F_LLTX;
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
969static int ipgre_tunnel_init(struct net_device *dev)
970{
Pravin B Shelarc5441932013-03-25 14:49:35 +0000971 struct ip_tunnel *tunnel = netdev_priv(dev);
972 struct iphdr *iph = &tunnel->parms.iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Pravin B Shelarc5441932013-03-25 14:49:35 +0000974 __gre_tunnel_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Pravin B Shelarc5441932013-03-25 14:49:35 +0000976 memcpy(dev->dev_addr, &iph->saddr, 4);
977 memcpy(dev->broadcast, &iph->daddr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Pravin B Shelarc5441932013-03-25 14:49:35 +0000979 dev->flags = IFF_NOARP;
Eric Dumazet02875872014-10-05 18:38:35 -0700980 netif_keep_dst(dev);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000981 dev->addr_len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Jiri Benca64b04d2016-04-27 11:29:06 +0200983 if (iph->daddr && !tunnel->collect_md) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984#ifdef CONFIG_NET_IPGRE_BROADCAST
Joe Perchesf97c1e02007-12-16 13:45:43 -0800985 if (ipv4_is_multicast(iph->daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (!iph->saddr)
987 return -EINVAL;
988 dev->flags = IFF_BROADCAST;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700989 dev->header_ops = &ipgre_header_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991#endif
Jiri Benca64b04d2016-04-27 11:29:06 +0200992 } else if (!tunnel->collect_md) {
Timo Teras6a5f44d2007-10-23 20:31:53 -0700993 dev->header_ops = &ipgre_header_ops;
Jiri Benca64b04d2016-04-27 11:29:06 +0200994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Pravin B Shelarc5441932013-03-25 14:49:35 +0000996 return ip_tunnel_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700999static const struct gre_protocol ipgre_protocol = {
1000 .handler = gre_rcv,
1001 .err_handler = gre_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002};
1003
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001004static int __net_init ipgre_init_net(struct net *net)
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001005{
Pravin B Shelarc5441932013-03-25 14:49:35 +00001006 return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001007}
1008
Eric Dumazet64bc1782017-09-19 16:27:09 -07001009static void __net_exit ipgre_exit_batch_net(struct list_head *list_net)
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001010{
Eric Dumazet64bc1782017-09-19 16:27:09 -07001011 ip_tunnel_delete_nets(list_net, ipgre_net_id, &ipgre_link_ops);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001012}
1013
1014static struct pernet_operations ipgre_net_ops = {
1015 .init = ipgre_init_net,
Eric Dumazet64bc1782017-09-19 16:27:09 -07001016 .exit_batch = ipgre_exit_batch_net,
Eric W. Biedermancfb8fbf2009-11-29 15:46:13 +00001017 .id = &ipgre_net_id,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001018 .size = sizeof(struct ip_tunnel_net),
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001019};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001021static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1022 struct netlink_ext_ack *extack)
Herbert Xuc19e6542008-10-09 11:59:55 -07001023{
1024 __be16 flags;
1025
1026 if (!data)
1027 return 0;
1028
1029 flags = 0;
1030 if (data[IFLA_GRE_IFLAGS])
1031 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1032 if (data[IFLA_GRE_OFLAGS])
1033 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1034 if (flags & (GRE_VERSION|GRE_ROUTING))
1035 return -EINVAL;
1036
Jiri Benc946b6362016-04-27 14:08:01 +02001037 if (data[IFLA_GRE_COLLECT_METADATA] &&
1038 data[IFLA_GRE_ENCAP_TYPE] &&
1039 nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE)
1040 return -EINVAL;
1041
Herbert Xuc19e6542008-10-09 11:59:55 -07001042 return 0;
1043}
1044
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001045static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1046 struct netlink_ext_ack *extack)
Herbert Xue1a80002008-10-09 12:00:17 -07001047{
1048 __be32 daddr;
1049
1050 if (tb[IFLA_ADDRESS]) {
1051 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1052 return -EINVAL;
1053 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1054 return -EADDRNOTAVAIL;
1055 }
1056
1057 if (!data)
1058 goto out;
1059
1060 if (data[IFLA_GRE_REMOTE]) {
1061 memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4);
1062 if (!daddr)
1063 return -EINVAL;
1064 }
1065
1066out:
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001067 return ipgre_tunnel_validate(tb, data, extack);
Herbert Xue1a80002008-10-09 12:00:17 -07001068}
1069
William Tu84e54fe2017-08-22 09:40:28 -07001070static int erspan_validate(struct nlattr *tb[], struct nlattr *data[],
1071 struct netlink_ext_ack *extack)
1072{
1073 __be16 flags = 0;
1074 int ret;
1075
1076 if (!data)
1077 return 0;
1078
1079 ret = ipgre_tap_validate(tb, data, extack);
1080 if (ret)
1081 return ret;
1082
William Tu51fa9602020-05-12 10:36:23 -07001083 if (data[IFLA_GRE_ERSPAN_VER] &&
1084 nla_get_u8(data[IFLA_GRE_ERSPAN_VER]) == 0)
William Tuf989d542020-05-05 09:05:06 -07001085 return 0;
1086
1087 /* ERSPAN type II/III should only have GRE sequence and key flag */
William Tu1a66a832017-08-25 09:21:28 -07001088 if (data[IFLA_GRE_OFLAGS])
1089 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1090 if (data[IFLA_GRE_IFLAGS])
1091 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1092 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1093 flags != (GRE_SEQ | GRE_KEY))
William Tu84e54fe2017-08-22 09:40:28 -07001094 return -EINVAL;
1095
1096 /* ERSPAN Session ID only has 10-bit. Since we reuse
1097 * 32-bit key field as ID, check it's range.
1098 */
1099 if (data[IFLA_GRE_IKEY] &&
1100 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1101 return -EINVAL;
1102
1103 if (data[IFLA_GRE_OKEY] &&
1104 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1105 return -EINVAL;
1106
1107 return 0;
1108}
1109
Philip Prindeville22a59be2016-06-14 15:53:02 -06001110static int ipgre_netlink_parms(struct net_device *dev,
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001111 struct nlattr *data[],
1112 struct nlattr *tb[],
Craig Gallek9830ad42017-04-19 12:30:54 -04001113 struct ip_tunnel_parm *parms,
1114 __u32 *fwmark)
Herbert Xuc19e6542008-10-09 11:59:55 -07001115{
Philip Prindeville22a59be2016-06-14 15:53:02 -06001116 struct ip_tunnel *t = netdev_priv(dev);
1117
Herbert Xu7bb82d92008-10-11 12:20:15 -07001118 memset(parms, 0, sizeof(*parms));
Herbert Xuc19e6542008-10-09 11:59:55 -07001119
1120 parms->iph.protocol = IPPROTO_GRE;
1121
1122 if (!data)
Philip Prindeville22a59be2016-06-14 15:53:02 -06001123 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001124
1125 if (data[IFLA_GRE_LINK])
1126 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1127
1128 if (data[IFLA_GRE_IFLAGS])
Pravin B Shelarc5441932013-03-25 14:49:35 +00001129 parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS]));
Herbert Xuc19e6542008-10-09 11:59:55 -07001130
1131 if (data[IFLA_GRE_OFLAGS])
Pravin B Shelarc5441932013-03-25 14:49:35 +00001132 parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS]));
Herbert Xuc19e6542008-10-09 11:59:55 -07001133
1134 if (data[IFLA_GRE_IKEY])
1135 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1136
1137 if (data[IFLA_GRE_OKEY])
1138 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1139
1140 if (data[IFLA_GRE_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001141 parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]);
Herbert Xuc19e6542008-10-09 11:59:55 -07001142
1143 if (data[IFLA_GRE_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001144 parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]);
Herbert Xuc19e6542008-10-09 11:59:55 -07001145
1146 if (data[IFLA_GRE_TTL])
1147 parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
1148
1149 if (data[IFLA_GRE_TOS])
1150 parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]);
1151
Philip Prindeville22a59be2016-06-14 15:53:02 -06001152 if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC])) {
1153 if (t->ignore_df)
1154 return -EINVAL;
Herbert Xuc19e6542008-10-09 11:59:55 -07001155 parms->iph.frag_off = htons(IP_DF);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001156 }
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001157
1158 if (data[IFLA_GRE_COLLECT_METADATA]) {
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001159 t->collect_md = true;
Jiri Bence271c7b2016-05-11 15:53:57 +02001160 if (dev->type == ARPHRD_IPGRE)
1161 dev->type = ARPHRD_NONE;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001162 }
Philip Prindeville22a59be2016-06-14 15:53:02 -06001163
1164 if (data[IFLA_GRE_IGNORE_DF]) {
1165 if (nla_get_u8(data[IFLA_GRE_IGNORE_DF])
1166 && (parms->iph.frag_off & htons(IP_DF)))
1167 return -EINVAL;
1168 t->ignore_df = !!nla_get_u8(data[IFLA_GRE_IGNORE_DF]);
1169 }
1170
Craig Gallek9830ad42017-04-19 12:30:54 -04001171 if (data[IFLA_GRE_FWMARK])
1172 *fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
1173
Petr Machatae1f8f782020-03-13 13:39:36 +02001174 return 0;
1175}
1176
1177static int erspan_netlink_parms(struct net_device *dev,
1178 struct nlattr *data[],
1179 struct nlattr *tb[],
1180 struct ip_tunnel_parm *parms,
1181 __u32 *fwmark)
1182{
1183 struct ip_tunnel *t = netdev_priv(dev);
1184 int err;
1185
1186 err = ipgre_netlink_parms(dev, data, tb, parms, fwmark);
1187 if (err)
1188 return err;
Petr Machata32ca98f2020-03-16 19:53:00 +02001189 if (!data)
1190 return 0;
Petr Machatae1f8f782020-03-13 13:39:36 +02001191
William Tuf551c912017-12-13 16:38:56 -08001192 if (data[IFLA_GRE_ERSPAN_VER]) {
1193 t->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
William Tu84e54fe2017-08-22 09:40:28 -07001194
William Tuf989d542020-05-05 09:05:06 -07001195 if (t->erspan_ver > 2)
William Tu84e54fe2017-08-22 09:40:28 -07001196 return -EINVAL;
1197 }
1198
William Tuf551c912017-12-13 16:38:56 -08001199 if (t->erspan_ver == 1) {
1200 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1201 t->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1202 if (t->index & ~INDEX_MASK)
1203 return -EINVAL;
1204 }
1205 } else if (t->erspan_ver == 2) {
1206 if (data[IFLA_GRE_ERSPAN_DIR]) {
1207 t->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1208 if (t->dir & ~(DIR_MASK >> DIR_OFFSET))
1209 return -EINVAL;
1210 }
1211 if (data[IFLA_GRE_ERSPAN_HWID]) {
1212 t->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1213 if (t->hwid & ~(HWID_MASK >> HWID_OFFSET))
1214 return -EINVAL;
1215 }
1216 }
1217
Philip Prindeville22a59be2016-06-14 15:53:02 -06001218 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001219}
1220
Tom Herbert4565e992014-09-17 12:26:01 -07001221/* This function returns true when ENCAP attributes are present in the nl msg */
1222static bool ipgre_netlink_encap_parms(struct nlattr *data[],
1223 struct ip_tunnel_encap *ipencap)
1224{
1225 bool ret = false;
1226
1227 memset(ipencap, 0, sizeof(*ipencap));
1228
1229 if (!data)
1230 return ret;
1231
1232 if (data[IFLA_GRE_ENCAP_TYPE]) {
1233 ret = true;
1234 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1235 }
1236
1237 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1238 ret = true;
1239 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1240 }
1241
1242 if (data[IFLA_GRE_ENCAP_SPORT]) {
1243 ret = true;
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001244 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
Tom Herbert4565e992014-09-17 12:26:01 -07001245 }
1246
1247 if (data[IFLA_GRE_ENCAP_DPORT]) {
1248 ret = true;
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001249 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
Tom Herbert4565e992014-09-17 12:26:01 -07001250 }
1251
1252 return ret;
1253}
1254
Pravin B Shelarc5441932013-03-25 14:49:35 +00001255static int gre_tap_init(struct net_device *dev)
Herbert Xue1a80002008-10-09 12:00:17 -07001256{
Pravin B Shelarc5441932013-03-25 14:49:35 +00001257 __gre_tunnel_init(dev);
stephen hemmingerbec94d4302014-12-27 10:01:42 -08001258 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Longd51711c2017-09-28 13:23:31 +08001259 netif_keep_dst(dev);
Herbert Xue1a80002008-10-09 12:00:17 -07001260
Pravin B Shelarc5441932013-03-25 14:49:35 +00001261 return ip_tunnel_init(dev);
Herbert Xue1a80002008-10-09 12:00:17 -07001262}
1263
Pravin B Shelarc5441932013-03-25 14:49:35 +00001264static const struct net_device_ops gre_tap_netdev_ops = {
1265 .ndo_init = gre_tap_init,
1266 .ndo_uninit = ip_tunnel_uninit,
1267 .ndo_start_xmit = gre_tap_xmit,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -08001268 .ndo_set_mac_address = eth_mac_addr,
1269 .ndo_validate_addr = eth_validate_addr,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001270 .ndo_change_mtu = ip_tunnel_change_mtu,
1271 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtel1e995842015-04-02 17:07:02 +02001272 .ndo_get_iflink = ip_tunnel_get_iflink,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001273 .ndo_fill_metadata_dst = gre_fill_metadata_dst,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -08001274};
1275
William Tu84e54fe2017-08-22 09:40:28 -07001276static int erspan_tunnel_init(struct net_device *dev)
1277{
1278 struct ip_tunnel *tunnel = netdev_priv(dev);
William Tu84e54fe2017-08-22 09:40:28 -07001279
William Tuf989d542020-05-05 09:05:06 -07001280 if (tunnel->erspan_ver == 0)
1281 tunnel->tun_hlen = 4; /* 4-byte GRE hdr. */
1282 else
1283 tunnel->tun_hlen = 8; /* 8-byte GRE hdr. */
1284
William Tu84e54fe2017-08-22 09:40:28 -07001285 tunnel->parms.iph.protocol = IPPROTO_GRE;
Xin Longc122fda2017-10-01 22:00:55 +08001286 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
William Tuf551c912017-12-13 16:38:56 -08001287 erspan_hdr_len(tunnel->erspan_ver);
William Tu84e54fe2017-08-22 09:40:28 -07001288
William Tu84e54fe2017-08-22 09:40:28 -07001289 dev->features |= GRE_FEATURES;
1290 dev->hw_features |= GRE_FEATURES;
1291 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Longc84bed42017-10-01 22:00:56 +08001292 netif_keep_dst(dev);
William Tu84e54fe2017-08-22 09:40:28 -07001293
1294 return ip_tunnel_init(dev);
1295}
1296
1297static const struct net_device_ops erspan_netdev_ops = {
1298 .ndo_init = erspan_tunnel_init,
1299 .ndo_uninit = ip_tunnel_uninit,
1300 .ndo_start_xmit = erspan_xmit,
1301 .ndo_set_mac_address = eth_mac_addr,
1302 .ndo_validate_addr = eth_validate_addr,
1303 .ndo_change_mtu = ip_tunnel_change_mtu,
1304 .ndo_get_stats64 = ip_tunnel_get_stats64,
1305 .ndo_get_iflink = ip_tunnel_get_iflink,
1306 .ndo_fill_metadata_dst = gre_fill_metadata_dst,
1307};
1308
Herbert Xue1a80002008-10-09 12:00:17 -07001309static void ipgre_tap_setup(struct net_device *dev)
1310{
Herbert Xue1a80002008-10-09 12:00:17 -07001311 ether_setup(dev);
Xin Longcfddd4c2017-12-18 14:24:35 +08001312 dev->max_mtu = 0;
Jiri Bencd13b1612016-02-17 15:32:53 +01001313 dev->netdev_ops = &gre_tap_netdev_ops;
1314 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1315 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Pravin B Shelarc5441932013-03-25 14:49:35 +00001316 ip_tunnel_setup(dev, gre_tap_net_id);
Herbert Xue1a80002008-10-09 12:00:17 -07001317}
1318
Petr Machatae1f8f782020-03-13 13:39:36 +02001319static int
1320ipgre_newlink_encap_setup(struct net_device *dev, struct nlattr *data[])
Herbert Xuc19e6542008-10-09 11:59:55 -07001321{
Tom Herbert4565e992014-09-17 12:26:01 -07001322 struct ip_tunnel_encap ipencap;
1323
1324 if (ipgre_netlink_encap_parms(data, &ipencap)) {
1325 struct ip_tunnel *t = netdev_priv(dev);
Petr Machatae1f8f782020-03-13 13:39:36 +02001326 int err = ip_tunnel_encap_setup(t, &ipencap);
Tom Herbert4565e992014-09-17 12:26:01 -07001327
1328 if (err < 0)
1329 return err;
1330 }
Herbert Xuc19e6542008-10-09 11:59:55 -07001331
Petr Machatae1f8f782020-03-13 13:39:36 +02001332 return 0;
1333}
1334
1335static int ipgre_newlink(struct net *src_net, struct net_device *dev,
1336 struct nlattr *tb[], struct nlattr *data[],
1337 struct netlink_ext_ack *extack)
1338{
1339 struct ip_tunnel_parm p;
1340 __u32 fwmark = 0;
1341 int err;
1342
1343 err = ipgre_newlink_encap_setup(dev, data);
1344 if (err)
1345 return err;
1346
Craig Gallek9830ad42017-04-19 12:30:54 -04001347 err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001348 if (err < 0)
1349 return err;
Craig Gallek9830ad42017-04-19 12:30:54 -04001350 return ip_tunnel_newlink(dev, tb, &p, fwmark);
Herbert Xuc19e6542008-10-09 11:59:55 -07001351}
1352
Petr Machatae1f8f782020-03-13 13:39:36 +02001353static int erspan_newlink(struct net *src_net, struct net_device *dev,
1354 struct nlattr *tb[], struct nlattr *data[],
1355 struct netlink_ext_ack *extack)
1356{
1357 struct ip_tunnel_parm p;
1358 __u32 fwmark = 0;
1359 int err;
1360
1361 err = ipgre_newlink_encap_setup(dev, data);
1362 if (err)
1363 return err;
1364
1365 err = erspan_netlink_parms(dev, data, tb, &p, &fwmark);
1366 if (err)
1367 return err;
1368 return ip_tunnel_newlink(dev, tb, &p, fwmark);
1369}
1370
Herbert Xuc19e6542008-10-09 11:59:55 -07001371static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001372 struct nlattr *data[],
1373 struct netlink_ext_ack *extack)
Herbert Xuc19e6542008-10-09 11:59:55 -07001374{
Craig Gallek9830ad42017-04-19 12:30:54 -04001375 struct ip_tunnel *t = netdev_priv(dev);
Craig Gallek9830ad42017-04-19 12:30:54 -04001376 __u32 fwmark = t->fwmark;
Xin Longdd9d5982017-11-07 16:33:08 +08001377 struct ip_tunnel_parm p;
Philip Prindeville22a59be2016-06-14 15:53:02 -06001378 int err;
Tom Herbert4565e992014-09-17 12:26:01 -07001379
Petr Machatae1f8f782020-03-13 13:39:36 +02001380 err = ipgre_newlink_encap_setup(dev, data);
1381 if (err)
1382 return err;
Herbert Xuc19e6542008-10-09 11:59:55 -07001383
Craig Gallek9830ad42017-04-19 12:30:54 -04001384 err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001385 if (err < 0)
1386 return err;
Xin Longdd9d5982017-11-07 16:33:08 +08001387
1388 err = ip_tunnel_changelink(dev, tb, &p, fwmark);
1389 if (err < 0)
1390 return err;
1391
1392 t->parms.i_flags = p.i_flags;
1393 t->parms.o_flags = p.o_flags;
1394
Petr Machatae1f8f782020-03-13 13:39:36 +02001395 ipgre_link_update(dev, !tb[IFLA_MTU]);
1396
1397 return 0;
1398}
1399
1400static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],
1401 struct nlattr *data[],
1402 struct netlink_ext_ack *extack)
1403{
1404 struct ip_tunnel *t = netdev_priv(dev);
1405 __u32 fwmark = t->fwmark;
1406 struct ip_tunnel_parm p;
1407 int err;
1408
1409 err = ipgre_newlink_encap_setup(dev, data);
1410 if (err)
1411 return err;
1412
1413 err = erspan_netlink_parms(dev, data, tb, &p, &fwmark);
1414 if (err < 0)
1415 return err;
1416
1417 err = ip_tunnel_changelink(dev, tb, &p, fwmark);
1418 if (err < 0)
1419 return err;
1420
1421 t->parms.i_flags = p.i_flags;
1422 t->parms.o_flags = p.o_flags;
Xin Longdd9d5982017-11-07 16:33:08 +08001423
1424 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001425}
1426
1427static size_t ipgre_get_size(const struct net_device *dev)
1428{
1429 return
1430 /* IFLA_GRE_LINK */
1431 nla_total_size(4) +
1432 /* IFLA_GRE_IFLAGS */
1433 nla_total_size(2) +
1434 /* IFLA_GRE_OFLAGS */
1435 nla_total_size(2) +
1436 /* IFLA_GRE_IKEY */
1437 nla_total_size(4) +
1438 /* IFLA_GRE_OKEY */
1439 nla_total_size(4) +
1440 /* IFLA_GRE_LOCAL */
1441 nla_total_size(4) +
1442 /* IFLA_GRE_REMOTE */
1443 nla_total_size(4) +
1444 /* IFLA_GRE_TTL */
1445 nla_total_size(1) +
1446 /* IFLA_GRE_TOS */
1447 nla_total_size(1) +
1448 /* IFLA_GRE_PMTUDISC */
1449 nla_total_size(1) +
Tom Herbert4565e992014-09-17 12:26:01 -07001450 /* IFLA_GRE_ENCAP_TYPE */
1451 nla_total_size(2) +
1452 /* IFLA_GRE_ENCAP_FLAGS */
1453 nla_total_size(2) +
1454 /* IFLA_GRE_ENCAP_SPORT */
1455 nla_total_size(2) +
1456 /* IFLA_GRE_ENCAP_DPORT */
1457 nla_total_size(2) +
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001458 /* IFLA_GRE_COLLECT_METADATA */
1459 nla_total_size(0) +
Philip Prindeville22a59be2016-06-14 15:53:02 -06001460 /* IFLA_GRE_IGNORE_DF */
1461 nla_total_size(1) +
Craig Gallek9830ad42017-04-19 12:30:54 -04001462 /* IFLA_GRE_FWMARK */
1463 nla_total_size(4) +
William Tu84e54fe2017-08-22 09:40:28 -07001464 /* IFLA_GRE_ERSPAN_INDEX */
1465 nla_total_size(4) +
William Tuf551c912017-12-13 16:38:56 -08001466 /* IFLA_GRE_ERSPAN_VER */
1467 nla_total_size(1) +
1468 /* IFLA_GRE_ERSPAN_DIR */
1469 nla_total_size(1) +
1470 /* IFLA_GRE_ERSPAN_HWID */
1471 nla_total_size(2) +
Herbert Xuc19e6542008-10-09 11:59:55 -07001472 0;
1473}
1474
1475static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1476{
1477 struct ip_tunnel *t = netdev_priv(dev);
1478 struct ip_tunnel_parm *p = &t->parms;
Lorenzo Bianconifeaf5c72019-01-28 22:23:48 +01001479 __be16 o_flags = p->o_flags;
1480
William Tuf989d542020-05-05 09:05:06 -07001481 if (t->erspan_ver <= 2) {
1482 if (t->erspan_ver != 0 && !t->collect_md)
Lorenzo Bianconi2bdf7002019-02-19 17:42:05 +01001483 o_flags |= TUNNEL_KEY;
1484
1485 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, t->erspan_ver))
1486 goto nla_put_failure;
1487
1488 if (t->erspan_ver == 1) {
1489 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, t->index))
1490 goto nla_put_failure;
William Tuf989d542020-05-05 09:05:06 -07001491 } else if (t->erspan_ver == 2) {
Lorenzo Bianconi2bdf7002019-02-19 17:42:05 +01001492 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, t->dir))
1493 goto nla_put_failure;
1494 if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, t->hwid))
1495 goto nla_put_failure;
1496 }
1497 }
Herbert Xuc19e6542008-10-09 11:59:55 -07001498
David S. Millerf3756b72012-04-01 20:39:02 -04001499 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
Tom Herbert95f5c642016-04-29 17:12:16 -07001500 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1501 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1502 nla_put_be16(skb, IFLA_GRE_OFLAGS,
Lorenzo Bianconifeaf5c72019-01-28 22:23:48 +01001503 gre_tnl_flags_to_gre_flags(o_flags)) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001504 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1505 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001506 nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
1507 nla_put_in_addr(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001508 nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
1509 nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
1510 nla_put_u8(skb, IFLA_GRE_PMTUDISC,
Craig Gallek9830ad42017-04-19 12:30:54 -04001511 !!(p->iph.frag_off & htons(IP_DF))) ||
1512 nla_put_u32(skb, IFLA_GRE_FWMARK, t->fwmark))
David S. Millerf3756b72012-04-01 20:39:02 -04001513 goto nla_put_failure;
Tom Herbert4565e992014-09-17 12:26:01 -07001514
1515 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1516 t->encap.type) ||
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001517 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1518 t->encap.sport) ||
1519 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1520 t->encap.dport) ||
Tom Herbert4565e992014-09-17 12:26:01 -07001521 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
Tom Herberte1b2cb62014-11-05 16:49:38 -08001522 t->encap.flags))
Tom Herbert4565e992014-09-17 12:26:01 -07001523 goto nla_put_failure;
1524
Philip Prindeville22a59be2016-06-14 15:53:02 -06001525 if (nla_put_u8(skb, IFLA_GRE_IGNORE_DF, t->ignore_df))
1526 goto nla_put_failure;
1527
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001528 if (t->collect_md) {
1529 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
1530 goto nla_put_failure;
1531 }
1532
Herbert Xuc19e6542008-10-09 11:59:55 -07001533 return 0;
1534
1535nla_put_failure:
1536 return -EMSGSIZE;
1537}
1538
William Tu84e54fe2017-08-22 09:40:28 -07001539static void erspan_setup(struct net_device *dev)
1540{
Xin Long84581bd2018-08-27 18:41:32 +08001541 struct ip_tunnel *t = netdev_priv(dev);
1542
William Tu84e54fe2017-08-22 09:40:28 -07001543 ether_setup(dev);
Haishuang Yan0e141f72019-09-27 14:58:20 +08001544 dev->max_mtu = 0;
William Tu84e54fe2017-08-22 09:40:28 -07001545 dev->netdev_ops = &erspan_netdev_ops;
1546 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1547 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1548 ip_tunnel_setup(dev, erspan_net_id);
Xin Long84581bd2018-08-27 18:41:32 +08001549 t->erspan_ver = 1;
William Tu84e54fe2017-08-22 09:40:28 -07001550}
1551
Herbert Xuc19e6542008-10-09 11:59:55 -07001552static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
1553 [IFLA_GRE_LINK] = { .type = NLA_U32 },
1554 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
1555 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
1556 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
1557 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001558 [IFLA_GRE_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
1559 [IFLA_GRE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
Herbert Xuc19e6542008-10-09 11:59:55 -07001560 [IFLA_GRE_TTL] = { .type = NLA_U8 },
1561 [IFLA_GRE_TOS] = { .type = NLA_U8 },
1562 [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
Tom Herbert4565e992014-09-17 12:26:01 -07001563 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
1564 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
1565 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
1566 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001567 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
Philip Prindeville22a59be2016-06-14 15:53:02 -06001568 [IFLA_GRE_IGNORE_DF] = { .type = NLA_U8 },
Craig Gallek9830ad42017-04-19 12:30:54 -04001569 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
William Tu84e54fe2017-08-22 09:40:28 -07001570 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
William Tuf551c912017-12-13 16:38:56 -08001571 [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
1572 [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
1573 [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
Herbert Xuc19e6542008-10-09 11:59:55 -07001574};
1575
1576static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
1577 .kind = "gre",
1578 .maxtype = IFLA_GRE_MAX,
1579 .policy = ipgre_policy,
1580 .priv_size = sizeof(struct ip_tunnel),
1581 .setup = ipgre_tunnel_setup,
1582 .validate = ipgre_tunnel_validate,
1583 .newlink = ipgre_newlink,
1584 .changelink = ipgre_changelink,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001585 .dellink = ip_tunnel_dellink,
Herbert Xuc19e6542008-10-09 11:59:55 -07001586 .get_size = ipgre_get_size,
1587 .fill_info = ipgre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001588 .get_link_net = ip_tunnel_get_link_net,
Herbert Xuc19e6542008-10-09 11:59:55 -07001589};
1590
Herbert Xue1a80002008-10-09 12:00:17 -07001591static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
1592 .kind = "gretap",
1593 .maxtype = IFLA_GRE_MAX,
1594 .policy = ipgre_policy,
1595 .priv_size = sizeof(struct ip_tunnel),
1596 .setup = ipgre_tap_setup,
1597 .validate = ipgre_tap_validate,
1598 .newlink = ipgre_newlink,
1599 .changelink = ipgre_changelink,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001600 .dellink = ip_tunnel_dellink,
Herbert Xue1a80002008-10-09 12:00:17 -07001601 .get_size = ipgre_get_size,
1602 .fill_info = ipgre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001603 .get_link_net = ip_tunnel_get_link_net,
Herbert Xue1a80002008-10-09 12:00:17 -07001604};
1605
William Tu84e54fe2017-08-22 09:40:28 -07001606static struct rtnl_link_ops erspan_link_ops __read_mostly = {
1607 .kind = "erspan",
1608 .maxtype = IFLA_GRE_MAX,
1609 .policy = ipgre_policy,
1610 .priv_size = sizeof(struct ip_tunnel),
1611 .setup = erspan_setup,
1612 .validate = erspan_validate,
Petr Machatae1f8f782020-03-13 13:39:36 +02001613 .newlink = erspan_newlink,
1614 .changelink = erspan_changelink,
William Tu84e54fe2017-08-22 09:40:28 -07001615 .dellink = ip_tunnel_dellink,
1616 .get_size = ipgre_get_size,
1617 .fill_info = ipgre_fill_info,
1618 .get_link_net = ip_tunnel_get_link_net,
1619};
1620
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001621struct net_device *gretap_fb_dev_create(struct net *net, const char *name,
1622 u8 name_assign_type)
1623{
1624 struct nlattr *tb[IFLA_MAX + 1];
1625 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001626 LIST_HEAD(list_kill);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001627 struct ip_tunnel *t;
1628 int err;
1629
1630 memset(&tb, 0, sizeof(tb));
1631
1632 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08001633 &ipgre_tap_ops, tb, NULL);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001634 if (IS_ERR(dev))
1635 return dev;
1636
1637 /* Configure flow based GRE device. */
1638 t = netdev_priv(dev);
1639 t->collect_md = true;
1640
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001641 err = ipgre_newlink(net, dev, tb, NULL, NULL);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001642 if (err < 0) {
1643 free_netdev(dev);
1644 return ERR_PTR(err);
1645 }
David Wragg7e059152016-02-10 00:05:58 +00001646
1647 /* openvswitch users expect packet sizes to be unrestricted,
1648 * so set the largest MTU we can.
1649 */
1650 err = __ip_tunnel_change_mtu(dev, IP_MAX_MTU, false);
1651 if (err)
1652 goto out;
1653
Nicolas Dichtelda6f1da2016-06-13 10:31:06 +02001654 err = rtnl_configure_link(dev, NULL);
1655 if (err < 0)
1656 goto out;
1657
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001658 return dev;
1659out:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001660 ip_tunnel_dellink(dev, &list_kill);
1661 unregister_netdevice_many(&list_kill);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001662 return ERR_PTR(err);
1663}
1664EXPORT_SYMBOL_GPL(gretap_fb_dev_create);
1665
Pravin B Shelarc5441932013-03-25 14:49:35 +00001666static int __net_init ipgre_tap_init_net(struct net *net)
1667{
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001668 return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, "gretap0");
Pravin B Shelarc5441932013-03-25 14:49:35 +00001669}
1670
Eric Dumazet64bc1782017-09-19 16:27:09 -07001671static void __net_exit ipgre_tap_exit_batch_net(struct list_head *list_net)
Pravin B Shelarc5441932013-03-25 14:49:35 +00001672{
Eric Dumazet64bc1782017-09-19 16:27:09 -07001673 ip_tunnel_delete_nets(list_net, gre_tap_net_id, &ipgre_tap_ops);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001674}
1675
1676static struct pernet_operations ipgre_tap_net_ops = {
1677 .init = ipgre_tap_init_net,
Eric Dumazet64bc1782017-09-19 16:27:09 -07001678 .exit_batch = ipgre_tap_exit_batch_net,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001679 .id = &gre_tap_net_id,
1680 .size = sizeof(struct ip_tunnel_net),
1681};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
William Tu84e54fe2017-08-22 09:40:28 -07001683static int __net_init erspan_init_net(struct net *net)
1684{
1685 return ip_tunnel_init_net(net, erspan_net_id,
1686 &erspan_link_ops, "erspan0");
1687}
1688
Eric Dumazet64bc1782017-09-19 16:27:09 -07001689static void __net_exit erspan_exit_batch_net(struct list_head *net_list)
William Tu84e54fe2017-08-22 09:40:28 -07001690{
Eric Dumazet64bc1782017-09-19 16:27:09 -07001691 ip_tunnel_delete_nets(net_list, erspan_net_id, &erspan_link_ops);
William Tu84e54fe2017-08-22 09:40:28 -07001692}
1693
1694static struct pernet_operations erspan_net_ops = {
1695 .init = erspan_init_net,
Eric Dumazet64bc1782017-09-19 16:27:09 -07001696 .exit_batch = erspan_exit_batch_net,
William Tu84e54fe2017-08-22 09:40:28 -07001697 .id = &erspan_net_id,
1698 .size = sizeof(struct ip_tunnel_net),
1699};
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701static int __init ipgre_init(void)
1702{
1703 int err;
1704
Joe Perches058bd4d2012-03-11 18:36:11 +00001705 pr_info("GRE over IPv4 tunneling driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Eric W. Biedermancfb8fbf2009-11-29 15:46:13 +00001707 err = register_pernet_device(&ipgre_net_ops);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001708 if (err < 0)
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001709 return err;
1710
Pravin B Shelarc5441932013-03-25 14:49:35 +00001711 err = register_pernet_device(&ipgre_tap_net_ops);
1712 if (err < 0)
William Tue3d03282017-08-22 17:04:05 -07001713 goto pnet_tap_failed;
Pravin B Shelarc5441932013-03-25 14:49:35 +00001714
William Tu84e54fe2017-08-22 09:40:28 -07001715 err = register_pernet_device(&erspan_net_ops);
1716 if (err < 0)
1717 goto pnet_erspan_failed;
1718
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001719 err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001720 if (err < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001721 pr_info("%s: can't add protocol\n", __func__);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001722 goto add_proto_failed;
1723 }
Pavel Emelyanov7daa0002008-04-16 01:10:05 -07001724
Herbert Xuc19e6542008-10-09 11:59:55 -07001725 err = rtnl_link_register(&ipgre_link_ops);
1726 if (err < 0)
1727 goto rtnl_link_failed;
1728
Herbert Xue1a80002008-10-09 12:00:17 -07001729 err = rtnl_link_register(&ipgre_tap_ops);
1730 if (err < 0)
1731 goto tap_ops_failed;
1732
William Tu84e54fe2017-08-22 09:40:28 -07001733 err = rtnl_link_register(&erspan_link_ops);
1734 if (err < 0)
1735 goto erspan_link_failed;
1736
Pravin B Shelarc5441932013-03-25 14:49:35 +00001737 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001738
William Tu84e54fe2017-08-22 09:40:28 -07001739erspan_link_failed:
1740 rtnl_link_unregister(&ipgre_tap_ops);
Herbert Xue1a80002008-10-09 12:00:17 -07001741tap_ops_failed:
1742 rtnl_link_unregister(&ipgre_link_ops);
Herbert Xuc19e6542008-10-09 11:59:55 -07001743rtnl_link_failed:
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001744 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001745add_proto_failed:
William Tu84e54fe2017-08-22 09:40:28 -07001746 unregister_pernet_device(&erspan_net_ops);
1747pnet_erspan_failed:
Pravin B Shelarc5441932013-03-25 14:49:35 +00001748 unregister_pernet_device(&ipgre_tap_net_ops);
William Tue3d03282017-08-22 17:04:05 -07001749pnet_tap_failed:
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001750 unregister_pernet_device(&ipgre_net_ops);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001751 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001754static void __exit ipgre_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Herbert Xue1a80002008-10-09 12:00:17 -07001756 rtnl_link_unregister(&ipgre_tap_ops);
Herbert Xuc19e6542008-10-09 11:59:55 -07001757 rtnl_link_unregister(&ipgre_link_ops);
William Tu84e54fe2017-08-22 09:40:28 -07001758 rtnl_link_unregister(&erspan_link_ops);
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001759 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001760 unregister_pernet_device(&ipgre_tap_net_ops);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001761 unregister_pernet_device(&ipgre_net_ops);
William Tu84e54fe2017-08-22 09:40:28 -07001762 unregister_pernet_device(&erspan_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
1765module_init(ipgre_init);
1766module_exit(ipgre_fini);
1767MODULE_LICENSE("GPL");
Patrick McHardy4d74f8b2008-10-10 12:11:06 -07001768MODULE_ALIAS_RTNL_LINK("gre");
1769MODULE_ALIAS_RTNL_LINK("gretap");
William Tu84e54fe2017-08-22 09:40:28 -07001770MODULE_ALIAS_RTNL_LINK("erspan");
Vasiliy Kulikov8909c9a2011-03-02 00:33:13 +03001771MODULE_ALIAS_NETDEV("gre0");
Pravin B Shelarc5441932013-03-25 14:49:35 +00001772MODULE_ALIAS_NETDEV("gretap0");
William Tu84e54fe2017-08-22 09:40:28 -07001773MODULE_ALIAS_NETDEV("erspan0");