blob: e70291748889b306073f2cd4368959f7e0c83aee [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) {
Cong Wangfdafed42020-10-12 16:17:21 -0700628 if (skb_cow_head(skb, 0))
Pravin B Shelarc5441932013-03-25 14:49:35 +0000629 goto free_skb;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800630
Pravin B Shelarc5441932013-03-25 14:49:35 +0000631 tnl_params = (const struct iphdr *)skb->data;
Eric Dumazete985aad2010-09-27 03:57:11 +0000632
Pravin B Shelarc5441932013-03-25 14:49:35 +0000633 /* Pull skb since ip_tunnel_xmit() needs skb->data pointing
634 * to gre header.
635 */
636 skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
Timo Teräs8a0033a2014-12-15 09:24:13 +0200637 skb_reset_mac_header(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000638 } else {
639 if (skb_cow_head(skb, dev->needed_headroom))
640 goto free_skb;
Herbert Xue1a80002008-10-09 12:00:17 -0700641
Pravin B Shelarc5441932013-03-25 14:49:35 +0000642 tnl_params = &tunnel->parms.iph;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800643 }
644
Alexander Duyckaed069d2016-04-14 15:33:37 -0400645 if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
646 goto free_skb;
Timo Teräs8a0033a2014-12-15 09:24:13 +0200647
Pravin B Shelarc5441932013-03-25 14:49:35 +0000648 __gre_xmit(skb, dev, tnl_params, skb->protocol);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000649 return NETDEV_TX_OK;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800650
Pravin B Shelarc5441932013-03-25 14:49:35 +0000651free_skb:
Eric Dumazet3acfa1e2014-01-18 18:27:49 -0800652 kfree_skb(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000653 dev->stats.tx_dropped++;
654 return NETDEV_TX_OK;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800655}
656
William Tu84e54fe2017-08-22 09:40:28 -0700657static netdev_tx_t erspan_xmit(struct sk_buff *skb,
658 struct net_device *dev)
659{
660 struct ip_tunnel *tunnel = netdev_priv(dev);
661 bool truncate = false;
Xin Long20704bd2019-01-14 18:10:06 +0800662 __be16 proto;
William Tu84e54fe2017-08-22 09:40:28 -0700663
Willem de Bruijncb9f1b72018-12-30 17:24:36 -0500664 if (!pskb_inet_may_pull(skb))
665 goto free_skb;
666
William Tu1a66a832017-08-25 09:21:28 -0700667 if (tunnel->collect_md) {
Xin Long20704bd2019-01-14 18:10:06 +0800668 erspan_fb_xmit(skb, dev);
William Tu1a66a832017-08-25 09:21:28 -0700669 return NETDEV_TX_OK;
670 }
671
William Tu84e54fe2017-08-22 09:40:28 -0700672 if (gre_handle_offloads(skb, false))
673 goto free_skb;
674
675 if (skb_cow_head(skb, dev->needed_headroom))
676 goto free_skb;
677
William Tuf1929702017-10-05 12:07:12 -0700678 if (skb->len > dev->mtu + dev->hard_header_len) {
679 pskb_trim(skb, dev->mtu + dev->hard_header_len);
William Tu84e54fe2017-08-22 09:40:28 -0700680 truncate = true;
681 }
682
683 /* Push ERSPAN header */
William Tuf989d542020-05-05 09:05:06 -0700684 if (tunnel->erspan_ver == 0) {
685 proto = htons(ETH_P_ERSPAN);
686 tunnel->parms.o_flags &= ~TUNNEL_SEQ;
687 } else if (tunnel->erspan_ver == 1) {
William Tuc69de582018-01-25 13:20:09 -0800688 erspan_build_header(skb, ntohl(tunnel->parms.o_key),
689 tunnel->index,
William Tuf551c912017-12-13 16:38:56 -0800690 truncate, true);
Xin Long20704bd2019-01-14 18:10:06 +0800691 proto = htons(ETH_P_ERSPAN);
692 } else if (tunnel->erspan_ver == 2) {
William Tuc69de582018-01-25 13:20:09 -0800693 erspan_build_header_v2(skb, ntohl(tunnel->parms.o_key),
William Tuf551c912017-12-13 16:38:56 -0800694 tunnel->dir, tunnel->hwid,
695 truncate, true);
Xin Long20704bd2019-01-14 18:10:06 +0800696 proto = htons(ETH_P_ERSPAN2);
697 } else {
William Tu02f99df2018-05-16 17:24:32 -0700698 goto free_skb;
Xin Long20704bd2019-01-14 18:10:06 +0800699 }
William Tuf551c912017-12-13 16:38:56 -0800700
William Tu84e54fe2017-08-22 09:40:28 -0700701 tunnel->parms.o_flags &= ~TUNNEL_KEY;
Xin Long20704bd2019-01-14 18:10:06 +0800702 __gre_xmit(skb, dev, &tunnel->parms.iph, proto);
William Tu84e54fe2017-08-22 09:40:28 -0700703 return NETDEV_TX_OK;
704
705free_skb:
706 kfree_skb(skb);
707 dev->stats.tx_dropped++;
708 return NETDEV_TX_OK;
709}
710
Pravin B Shelarc5441932013-03-25 14:49:35 +0000711static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
712 struct net_device *dev)
713{
714 struct ip_tunnel *tunnel = netdev_priv(dev);
715
Willem de Bruijncb9f1b72018-12-30 17:24:36 -0500716 if (!pskb_inet_may_pull(skb))
717 goto free_skb;
718
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700719 if (tunnel->collect_md) {
Jiri Benc20907142016-04-27 11:29:07 +0200720 gre_fb_xmit(skb, dev, htons(ETH_P_TEB));
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700721 return NETDEV_TX_OK;
722 }
723
Alexander Duyckaed069d2016-04-14 15:33:37 -0400724 if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
725 goto free_skb;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000726
727 if (skb_cow_head(skb, dev->needed_headroom))
728 goto free_skb;
729
730 __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB));
Pravin B Shelarc5441932013-03-25 14:49:35 +0000731 return NETDEV_TX_OK;
732
733free_skb:
Eric Dumazet3acfa1e2014-01-18 18:27:49 -0800734 kfree_skb(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000735 dev->stats.tx_dropped++;
736 return NETDEV_TX_OK;
737}
738
Xin Longdd9d5982017-11-07 16:33:08 +0800739static void ipgre_link_update(struct net_device *dev, bool set_mtu)
740{
741 struct ip_tunnel *tunnel = netdev_priv(dev);
742 int len;
743
744 len = tunnel->tun_hlen;
745 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
746 len = tunnel->tun_hlen - len;
747 tunnel->hlen = tunnel->hlen + len;
748
Cong Wangfdafed42020-10-12 16:17:21 -0700749 if (dev->header_ops)
750 dev->hard_header_len += len;
751 else
752 dev->needed_headroom += len;
753
Xin Longdd9d5982017-11-07 16:33:08 +0800754 if (set_mtu)
755 dev->mtu = max_t(int, dev->mtu - len, 68);
756
757 if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
758 if (!(tunnel->parms.o_flags & TUNNEL_CSUM) ||
759 tunnel->encap.type == TUNNEL_ENCAP_NONE) {
760 dev->features |= NETIF_F_GSO_SOFTWARE;
761 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Sabrina Dubroca1cc59542018-04-10 12:57:18 +0200762 } else {
763 dev->features &= ~NETIF_F_GSO_SOFTWARE;
764 dev->hw_features &= ~NETIF_F_GSO_SOFTWARE;
Xin Longdd9d5982017-11-07 16:33:08 +0800765 }
766 dev->features |= NETIF_F_LLTX;
Sabrina Dubroca1cc59542018-04-10 12:57:18 +0200767 } else {
768 dev->hw_features &= ~NETIF_F_GSO_SOFTWARE;
769 dev->features &= ~(NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE);
Xin Longdd9d5982017-11-07 16:33:08 +0800770 }
771}
772
Christoph Hellwig607259a62020-05-19 15:03:13 +0200773static int ipgre_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p,
774 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Xin Longa0efab62017-11-07 16:33:09 +0800776 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Cong Wang6c734fb2013-06-29 12:02:59 +0800778 if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
Christoph Hellwig607259a62020-05-19 15:03:13 +0200779 if (p->iph.version != 4 || p->iph.protocol != IPPROTO_GRE ||
780 p->iph.ihl != 5 || (p->iph.frag_off & htons(~IP_DF)) ||
781 ((p->i_flags | p->o_flags) & (GRE_VERSION | GRE_ROUTING)))
Cong Wang6c734fb2013-06-29 12:02:59 +0800782 return -EINVAL;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000783 }
Xin Longa0efab62017-11-07 16:33:09 +0800784
Christoph Hellwig607259a62020-05-19 15:03:13 +0200785 p->i_flags = gre_flags_to_tnl_flags(p->i_flags);
786 p->o_flags = gre_flags_to_tnl_flags(p->o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000787
Christoph Hellwig607259a62020-05-19 15:03:13 +0200788 err = ip_tunnel_ctl(dev, p, cmd);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000789 if (err)
790 return err;
791
Xin Longa0efab62017-11-07 16:33:09 +0800792 if (cmd == SIOCCHGTUNNEL) {
793 struct ip_tunnel *t = netdev_priv(dev);
794
Christoph Hellwig607259a62020-05-19 15:03:13 +0200795 t->parms.i_flags = p->i_flags;
796 t->parms.o_flags = p->o_flags;
Xin Longa0efab62017-11-07 16:33:09 +0800797
798 if (strcmp(dev->rtnl_link_ops->kind, "erspan"))
799 ipgre_link_update(dev, true);
800 }
801
Christoph Hellwig607259a62020-05-19 15:03:13 +0200802 p->i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
803 p->o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return 0;
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807/* Nice toy. Unfortunately, useless in real life :-)
808 It allows to construct virtual multiprotocol broadcast "LAN"
809 over the Internet, provided multicast routing is tuned.
810
811
812 I have no idea was this bicycle invented before me,
813 so that I had to set ARPHRD_IPGRE to a random value.
814 I have an impression, that Cisco could make something similar,
815 but this feature is apparently missing in IOS<=11.2(8).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
818 with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
819
820 ping -t 255 224.66.66.66
821
822 If nobody answers, mbone does not work.
823
824 ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
825 ip addr add 10.66.66.<somewhat>/24 dev Universe
826 ifconfig Universe up
827 ifconfig Universe add fe80::<Your_real_addr>/10
828 ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
829 ftp 10.66.66.66
830 ...
831 ftp fec0:6666:6666::193.233.7.65
832 ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700834static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
835 unsigned short type,
Eric Dumazet15078502010-09-15 11:07:53 +0000836 const void *daddr, const void *saddr, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Patrick McHardy2941a482006-01-08 22:05:26 -0800838 struct ip_tunnel *t = netdev_priv(dev);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000839 struct iphdr *iph;
840 struct gre_base_hdr *greh;
841
Johannes Bergd58ff352017-06-16 14:29:23 +0200842 iph = skb_push(skb, t->hlen + sizeof(*iph));
Pravin B Shelarc5441932013-03-25 14:49:35 +0000843 greh = (struct gre_base_hdr *)(iph+1);
Tom Herbert95f5c642016-04-29 17:12:16 -0700844 greh->flags = gre_tnl_flags_to_gre_flags(t->parms.o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000845 greh->protocol = htons(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Pravin B Shelarc5441932013-03-25 14:49:35 +0000849 /* Set the source hardware address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (saddr)
851 memcpy(&iph->saddr, saddr, 4);
Timo Teräs6d55cb92010-03-03 04:01:13 +0000852 if (daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 memcpy(&iph->daddr, daddr, 4);
Timo Teräs6d55cb92010-03-03 04:01:13 +0000854 if (iph->daddr)
Timo Teräs77a482b2013-08-06 13:45:43 +0300855 return t->hlen + sizeof(*iph);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900856
Pravin B Shelarc5441932013-03-25 14:49:35 +0000857 return -(t->hlen + sizeof(*iph));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Timo Teras6a5f44d2007-10-23 20:31:53 -0700860static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
861{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000862 const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb);
Timo Teras6a5f44d2007-10-23 20:31:53 -0700863 memcpy(haddr, &iph->saddr, 4);
864 return 4;
865}
866
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700867static const struct header_ops ipgre_header_ops = {
868 .create = ipgre_header,
Timo Teras6a5f44d2007-10-23 20:31:53 -0700869 .parse = ipgre_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700870};
871
Timo Teras6a5f44d2007-10-23 20:31:53 -0700872#ifdef CONFIG_NET_IPGRE_BROADCAST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873static int ipgre_open(struct net_device *dev)
874{
Patrick McHardy2941a482006-01-08 22:05:26 -0800875 struct ip_tunnel *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Joe Perchesf97c1e02007-12-16 13:45:43 -0800877 if (ipv4_is_multicast(t->parms.iph.daddr)) {
David S. Millercbb1e852011-05-04 12:33:34 -0700878 struct flowi4 fl4;
879 struct rtable *rt;
Eric Dumazete985aad2010-09-27 03:57:11 +0000880
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200881 rt = ip_route_output_gre(t->net, &fl4,
David S. Millercbb1e852011-05-04 12:33:34 -0700882 t->parms.iph.daddr,
883 t->parms.iph.saddr,
884 t->parms.o_key,
885 RT_TOS(t->parms.iph.tos),
886 t->parms.link);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800887 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -EADDRNOTAVAIL;
Changli Gaod8d1f302010-06-10 23:31:35 -0700889 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ip_rt_put(rt);
Ian Morris51456b22015-04-03 09:17:26 +0100891 if (!__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -EADDRNOTAVAIL;
893 t->mlink = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -0700894 ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 return 0;
897}
898
899static int ipgre_close(struct net_device *dev)
900{
Patrick McHardy2941a482006-01-08 22:05:26 -0800901 struct ip_tunnel *t = netdev_priv(dev);
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800902
Joe Perchesf97c1e02007-12-16 13:45:43 -0800903 if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
Denis V. Lunev7fee0ca2008-01-21 17:32:38 -0800904 struct in_device *in_dev;
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200905 in_dev = inetdev_by_index(t->net, t->mlink);
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000906 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 ip_mc_dec_group(in_dev, t->parms.iph.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909 return 0;
910}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911#endif
912
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800913static const struct net_device_ops ipgre_netdev_ops = {
914 .ndo_init = ipgre_tunnel_init,
Pravin B Shelarc5441932013-03-25 14:49:35 +0000915 .ndo_uninit = ip_tunnel_uninit,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800916#ifdef CONFIG_NET_IPGRE_BROADCAST
917 .ndo_open = ipgre_open,
918 .ndo_stop = ipgre_close,
919#endif
Pravin B Shelarc5441932013-03-25 14:49:35 +0000920 .ndo_start_xmit = ipgre_xmit,
Christoph Hellwig607259a62020-05-19 15:03:13 +0200921 .ndo_do_ioctl = ip_tunnel_ioctl,
Pravin B Shelarc5441932013-03-25 14:49:35 +0000922 .ndo_change_mtu = ip_tunnel_change_mtu,
923 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtel1e995842015-04-02 17:07:02 +0200924 .ndo_get_iflink = ip_tunnel_get_iflink,
Christoph Hellwig607259a62020-05-19 15:03:13 +0200925 .ndo_tunnel_ctl = ipgre_tunnel_ctl,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800926};
927
Eric Dumazet6b78f162012-09-13 21:25:33 +0000928#define GRE_FEATURES (NETIF_F_SG | \
929 NETIF_F_FRAGLIST | \
930 NETIF_F_HIGHDMA | \
931 NETIF_F_HW_CSUM)
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933static void ipgre_tunnel_setup(struct net_device *dev)
934{
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800935 dev->netdev_ops = &ipgre_netdev_ops;
Nicolas Dichtel5a455272014-04-11 15:51:18 +0200936 dev->type = ARPHRD_IPGRE;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000937 ip_tunnel_setup(dev, ipgre_net_id);
938}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Pravin B Shelarc5441932013-03-25 14:49:35 +0000940static void __gre_tunnel_init(struct net_device *dev)
941{
942 struct ip_tunnel *tunnel;
943
944 tunnel = netdev_priv(dev);
Tom Herbert95f5c642016-04-29 17:12:16 -0700945 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000946 tunnel->parms.iph.protocol = IPPROTO_GRE;
947
Tom Herbert4565e992014-09-17 12:26:01 -0700948 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
Cong Wangfdafed42020-10-12 16:17:21 -0700949 dev->needed_headroom = tunnel->hlen + sizeof(tunnel->parms.iph);
Tom Herbert4565e992014-09-17 12:26:01 -0700950
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200951 dev->features |= GRE_FEATURES;
Eric Dumazet6b78f162012-09-13 21:25:33 +0000952 dev->hw_features |= GRE_FEATURES;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000953
954 if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
Alexander Duycka0ca1532016-04-05 09:13:39 -0700955 /* TCP offload with GRE SEQ is not supported, nor
956 * can we support 2 levels of outer headers requiring
957 * an update.
958 */
959 if (!(tunnel->parms.o_flags & TUNNEL_CSUM) ||
960 (tunnel->encap.type == TUNNEL_ENCAP_NONE)) {
961 dev->features |= NETIF_F_GSO_SOFTWARE;
962 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
963 }
964
Pravin B Shelarc5441932013-03-25 14:49:35 +0000965 /* Can use a lockless transmit, unless we generate
966 * output sequences
967 */
968 dev->features |= NETIF_F_LLTX;
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972static int ipgre_tunnel_init(struct net_device *dev)
973{
Pravin B Shelarc5441932013-03-25 14:49:35 +0000974 struct ip_tunnel *tunnel = netdev_priv(dev);
975 struct iphdr *iph = &tunnel->parms.iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Pravin B Shelarc5441932013-03-25 14:49:35 +0000977 __gre_tunnel_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Pravin B Shelarc5441932013-03-25 14:49:35 +0000979 memcpy(dev->dev_addr, &iph->saddr, 4);
980 memcpy(dev->broadcast, &iph->daddr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Pravin B Shelarc5441932013-03-25 14:49:35 +0000982 dev->flags = IFF_NOARP;
Eric Dumazet02875872014-10-05 18:38:35 -0700983 netif_keep_dst(dev);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000984 dev->addr_len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Jiri Benca64b04d2016-04-27 11:29:06 +0200986 if (iph->daddr && !tunnel->collect_md) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987#ifdef CONFIG_NET_IPGRE_BROADCAST
Joe Perchesf97c1e02007-12-16 13:45:43 -0800988 if (ipv4_is_multicast(iph->daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (!iph->saddr)
990 return -EINVAL;
991 dev->flags = IFF_BROADCAST;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700992 dev->header_ops = &ipgre_header_ops;
Cong Wangfdafed42020-10-12 16:17:21 -0700993 dev->hard_header_len = tunnel->hlen + sizeof(*iph);
994 dev->needed_headroom = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996#endif
Jiri Benca64b04d2016-04-27 11:29:06 +0200997 } else if (!tunnel->collect_md) {
Timo Teras6a5f44d2007-10-23 20:31:53 -0700998 dev->header_ops = &ipgre_header_ops;
Cong Wangfdafed42020-10-12 16:17:21 -0700999 dev->hard_header_len = tunnel->hlen + sizeof(*iph);
1000 dev->needed_headroom = 0;
Jiri Benca64b04d2016-04-27 11:29:06 +02001001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Pravin B Shelarc5441932013-03-25 14:49:35 +00001003 return ip_tunnel_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001006static const struct gre_protocol ipgre_protocol = {
1007 .handler = gre_rcv,
1008 .err_handler = gre_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009};
1010
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001011static int __net_init ipgre_init_net(struct net *net)
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001012{
Pravin B Shelarc5441932013-03-25 14:49:35 +00001013 return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001014}
1015
Eric Dumazet64bc1782017-09-19 16:27:09 -07001016static void __net_exit ipgre_exit_batch_net(struct list_head *list_net)
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001017{
Eric Dumazet64bc1782017-09-19 16:27:09 -07001018 ip_tunnel_delete_nets(list_net, ipgre_net_id, &ipgre_link_ops);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001019}
1020
1021static struct pernet_operations ipgre_net_ops = {
1022 .init = ipgre_init_net,
Eric Dumazet64bc1782017-09-19 16:27:09 -07001023 .exit_batch = ipgre_exit_batch_net,
Eric W. Biedermancfb8fbf2009-11-29 15:46:13 +00001024 .id = &ipgre_net_id,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001025 .size = sizeof(struct ip_tunnel_net),
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001026};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001028static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1029 struct netlink_ext_ack *extack)
Herbert Xuc19e6542008-10-09 11:59:55 -07001030{
1031 __be16 flags;
1032
1033 if (!data)
1034 return 0;
1035
1036 flags = 0;
1037 if (data[IFLA_GRE_IFLAGS])
1038 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1039 if (data[IFLA_GRE_OFLAGS])
1040 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1041 if (flags & (GRE_VERSION|GRE_ROUTING))
1042 return -EINVAL;
1043
Jiri Benc946b6362016-04-27 14:08:01 +02001044 if (data[IFLA_GRE_COLLECT_METADATA] &&
1045 data[IFLA_GRE_ENCAP_TYPE] &&
1046 nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE)
1047 return -EINVAL;
1048
Herbert Xuc19e6542008-10-09 11:59:55 -07001049 return 0;
1050}
1051
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001052static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1053 struct netlink_ext_ack *extack)
Herbert Xue1a80002008-10-09 12:00:17 -07001054{
1055 __be32 daddr;
1056
1057 if (tb[IFLA_ADDRESS]) {
1058 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1059 return -EINVAL;
1060 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1061 return -EADDRNOTAVAIL;
1062 }
1063
1064 if (!data)
1065 goto out;
1066
1067 if (data[IFLA_GRE_REMOTE]) {
1068 memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4);
1069 if (!daddr)
1070 return -EINVAL;
1071 }
1072
1073out:
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001074 return ipgre_tunnel_validate(tb, data, extack);
Herbert Xue1a80002008-10-09 12:00:17 -07001075}
1076
William Tu84e54fe2017-08-22 09:40:28 -07001077static int erspan_validate(struct nlattr *tb[], struct nlattr *data[],
1078 struct netlink_ext_ack *extack)
1079{
1080 __be16 flags = 0;
1081 int ret;
1082
1083 if (!data)
1084 return 0;
1085
1086 ret = ipgre_tap_validate(tb, data, extack);
1087 if (ret)
1088 return ret;
1089
William Tu51fa9602020-05-12 10:36:23 -07001090 if (data[IFLA_GRE_ERSPAN_VER] &&
1091 nla_get_u8(data[IFLA_GRE_ERSPAN_VER]) == 0)
William Tuf989d542020-05-05 09:05:06 -07001092 return 0;
1093
1094 /* ERSPAN type II/III should only have GRE sequence and key flag */
William Tu1a66a832017-08-25 09:21:28 -07001095 if (data[IFLA_GRE_OFLAGS])
1096 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1097 if (data[IFLA_GRE_IFLAGS])
1098 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1099 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1100 flags != (GRE_SEQ | GRE_KEY))
William Tu84e54fe2017-08-22 09:40:28 -07001101 return -EINVAL;
1102
1103 /* ERSPAN Session ID only has 10-bit. Since we reuse
1104 * 32-bit key field as ID, check it's range.
1105 */
1106 if (data[IFLA_GRE_IKEY] &&
1107 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1108 return -EINVAL;
1109
1110 if (data[IFLA_GRE_OKEY] &&
1111 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1112 return -EINVAL;
1113
1114 return 0;
1115}
1116
Philip Prindeville22a59be2016-06-14 15:53:02 -06001117static int ipgre_netlink_parms(struct net_device *dev,
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001118 struct nlattr *data[],
1119 struct nlattr *tb[],
Craig Gallek9830ad42017-04-19 12:30:54 -04001120 struct ip_tunnel_parm *parms,
1121 __u32 *fwmark)
Herbert Xuc19e6542008-10-09 11:59:55 -07001122{
Philip Prindeville22a59be2016-06-14 15:53:02 -06001123 struct ip_tunnel *t = netdev_priv(dev);
1124
Herbert Xu7bb82d92008-10-11 12:20:15 -07001125 memset(parms, 0, sizeof(*parms));
Herbert Xuc19e6542008-10-09 11:59:55 -07001126
1127 parms->iph.protocol = IPPROTO_GRE;
1128
1129 if (!data)
Philip Prindeville22a59be2016-06-14 15:53:02 -06001130 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001131
1132 if (data[IFLA_GRE_LINK])
1133 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1134
1135 if (data[IFLA_GRE_IFLAGS])
Pravin B Shelarc5441932013-03-25 14:49:35 +00001136 parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS]));
Herbert Xuc19e6542008-10-09 11:59:55 -07001137
1138 if (data[IFLA_GRE_OFLAGS])
Pravin B Shelarc5441932013-03-25 14:49:35 +00001139 parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS]));
Herbert Xuc19e6542008-10-09 11:59:55 -07001140
1141 if (data[IFLA_GRE_IKEY])
1142 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1143
1144 if (data[IFLA_GRE_OKEY])
1145 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1146
1147 if (data[IFLA_GRE_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001148 parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]);
Herbert Xuc19e6542008-10-09 11:59:55 -07001149
1150 if (data[IFLA_GRE_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001151 parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]);
Herbert Xuc19e6542008-10-09 11:59:55 -07001152
1153 if (data[IFLA_GRE_TTL])
1154 parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
1155
1156 if (data[IFLA_GRE_TOS])
1157 parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]);
1158
Philip Prindeville22a59be2016-06-14 15:53:02 -06001159 if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC])) {
1160 if (t->ignore_df)
1161 return -EINVAL;
Herbert Xuc19e6542008-10-09 11:59:55 -07001162 parms->iph.frag_off = htons(IP_DF);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001163 }
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001164
1165 if (data[IFLA_GRE_COLLECT_METADATA]) {
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001166 t->collect_md = true;
Jiri Bence271c7b2016-05-11 15:53:57 +02001167 if (dev->type == ARPHRD_IPGRE)
1168 dev->type = ARPHRD_NONE;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001169 }
Philip Prindeville22a59be2016-06-14 15:53:02 -06001170
1171 if (data[IFLA_GRE_IGNORE_DF]) {
1172 if (nla_get_u8(data[IFLA_GRE_IGNORE_DF])
1173 && (parms->iph.frag_off & htons(IP_DF)))
1174 return -EINVAL;
1175 t->ignore_df = !!nla_get_u8(data[IFLA_GRE_IGNORE_DF]);
1176 }
1177
Craig Gallek9830ad42017-04-19 12:30:54 -04001178 if (data[IFLA_GRE_FWMARK])
1179 *fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
1180
Petr Machatae1f8f782020-03-13 13:39:36 +02001181 return 0;
1182}
1183
1184static int erspan_netlink_parms(struct net_device *dev,
1185 struct nlattr *data[],
1186 struct nlattr *tb[],
1187 struct ip_tunnel_parm *parms,
1188 __u32 *fwmark)
1189{
1190 struct ip_tunnel *t = netdev_priv(dev);
1191 int err;
1192
1193 err = ipgre_netlink_parms(dev, data, tb, parms, fwmark);
1194 if (err)
1195 return err;
Petr Machata32ca98f2020-03-16 19:53:00 +02001196 if (!data)
1197 return 0;
Petr Machatae1f8f782020-03-13 13:39:36 +02001198
William Tuf551c912017-12-13 16:38:56 -08001199 if (data[IFLA_GRE_ERSPAN_VER]) {
1200 t->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
William Tu84e54fe2017-08-22 09:40:28 -07001201
William Tuf989d542020-05-05 09:05:06 -07001202 if (t->erspan_ver > 2)
William Tu84e54fe2017-08-22 09:40:28 -07001203 return -EINVAL;
1204 }
1205
William Tuf551c912017-12-13 16:38:56 -08001206 if (t->erspan_ver == 1) {
1207 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1208 t->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1209 if (t->index & ~INDEX_MASK)
1210 return -EINVAL;
1211 }
1212 } else if (t->erspan_ver == 2) {
1213 if (data[IFLA_GRE_ERSPAN_DIR]) {
1214 t->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1215 if (t->dir & ~(DIR_MASK >> DIR_OFFSET))
1216 return -EINVAL;
1217 }
1218 if (data[IFLA_GRE_ERSPAN_HWID]) {
1219 t->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1220 if (t->hwid & ~(HWID_MASK >> HWID_OFFSET))
1221 return -EINVAL;
1222 }
1223 }
1224
Philip Prindeville22a59be2016-06-14 15:53:02 -06001225 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001226}
1227
Tom Herbert4565e992014-09-17 12:26:01 -07001228/* This function returns true when ENCAP attributes are present in the nl msg */
1229static bool ipgre_netlink_encap_parms(struct nlattr *data[],
1230 struct ip_tunnel_encap *ipencap)
1231{
1232 bool ret = false;
1233
1234 memset(ipencap, 0, sizeof(*ipencap));
1235
1236 if (!data)
1237 return ret;
1238
1239 if (data[IFLA_GRE_ENCAP_TYPE]) {
1240 ret = true;
1241 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1242 }
1243
1244 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1245 ret = true;
1246 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1247 }
1248
1249 if (data[IFLA_GRE_ENCAP_SPORT]) {
1250 ret = true;
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001251 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
Tom Herbert4565e992014-09-17 12:26:01 -07001252 }
1253
1254 if (data[IFLA_GRE_ENCAP_DPORT]) {
1255 ret = true;
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001256 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
Tom Herbert4565e992014-09-17 12:26:01 -07001257 }
1258
1259 return ret;
1260}
1261
Pravin B Shelarc5441932013-03-25 14:49:35 +00001262static int gre_tap_init(struct net_device *dev)
Herbert Xue1a80002008-10-09 12:00:17 -07001263{
Pravin B Shelarc5441932013-03-25 14:49:35 +00001264 __gre_tunnel_init(dev);
stephen hemmingerbec94d4302014-12-27 10:01:42 -08001265 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Longd51711c2017-09-28 13:23:31 +08001266 netif_keep_dst(dev);
Herbert Xue1a80002008-10-09 12:00:17 -07001267
Pravin B Shelarc5441932013-03-25 14:49:35 +00001268 return ip_tunnel_init(dev);
Herbert Xue1a80002008-10-09 12:00:17 -07001269}
1270
Pravin B Shelarc5441932013-03-25 14:49:35 +00001271static const struct net_device_ops gre_tap_netdev_ops = {
1272 .ndo_init = gre_tap_init,
1273 .ndo_uninit = ip_tunnel_uninit,
1274 .ndo_start_xmit = gre_tap_xmit,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -08001275 .ndo_set_mac_address = eth_mac_addr,
1276 .ndo_validate_addr = eth_validate_addr,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001277 .ndo_change_mtu = ip_tunnel_change_mtu,
1278 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtel1e995842015-04-02 17:07:02 +02001279 .ndo_get_iflink = ip_tunnel_get_iflink,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001280 .ndo_fill_metadata_dst = gre_fill_metadata_dst,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -08001281};
1282
William Tu84e54fe2017-08-22 09:40:28 -07001283static int erspan_tunnel_init(struct net_device *dev)
1284{
1285 struct ip_tunnel *tunnel = netdev_priv(dev);
William Tu84e54fe2017-08-22 09:40:28 -07001286
William Tuf989d542020-05-05 09:05:06 -07001287 if (tunnel->erspan_ver == 0)
1288 tunnel->tun_hlen = 4; /* 4-byte GRE hdr. */
1289 else
1290 tunnel->tun_hlen = 8; /* 8-byte GRE hdr. */
1291
William Tu84e54fe2017-08-22 09:40:28 -07001292 tunnel->parms.iph.protocol = IPPROTO_GRE;
Xin Longc122fda2017-10-01 22:00:55 +08001293 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
William Tuf551c912017-12-13 16:38:56 -08001294 erspan_hdr_len(tunnel->erspan_ver);
William Tu84e54fe2017-08-22 09:40:28 -07001295
William Tu84e54fe2017-08-22 09:40:28 -07001296 dev->features |= GRE_FEATURES;
1297 dev->hw_features |= GRE_FEATURES;
1298 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Longc84bed42017-10-01 22:00:56 +08001299 netif_keep_dst(dev);
William Tu84e54fe2017-08-22 09:40:28 -07001300
1301 return ip_tunnel_init(dev);
1302}
1303
1304static const struct net_device_ops erspan_netdev_ops = {
1305 .ndo_init = erspan_tunnel_init,
1306 .ndo_uninit = ip_tunnel_uninit,
1307 .ndo_start_xmit = erspan_xmit,
1308 .ndo_set_mac_address = eth_mac_addr,
1309 .ndo_validate_addr = eth_validate_addr,
1310 .ndo_change_mtu = ip_tunnel_change_mtu,
1311 .ndo_get_stats64 = ip_tunnel_get_stats64,
1312 .ndo_get_iflink = ip_tunnel_get_iflink,
1313 .ndo_fill_metadata_dst = gre_fill_metadata_dst,
1314};
1315
Herbert Xue1a80002008-10-09 12:00:17 -07001316static void ipgre_tap_setup(struct net_device *dev)
1317{
Herbert Xue1a80002008-10-09 12:00:17 -07001318 ether_setup(dev);
Xin Longcfddd4c2017-12-18 14:24:35 +08001319 dev->max_mtu = 0;
Jiri Bencd13b1612016-02-17 15:32:53 +01001320 dev->netdev_ops = &gre_tap_netdev_ops;
1321 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1322 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Pravin B Shelarc5441932013-03-25 14:49:35 +00001323 ip_tunnel_setup(dev, gre_tap_net_id);
Herbert Xue1a80002008-10-09 12:00:17 -07001324}
1325
Petr Machatae1f8f782020-03-13 13:39:36 +02001326static int
1327ipgre_newlink_encap_setup(struct net_device *dev, struct nlattr *data[])
Herbert Xuc19e6542008-10-09 11:59:55 -07001328{
Tom Herbert4565e992014-09-17 12:26:01 -07001329 struct ip_tunnel_encap ipencap;
1330
1331 if (ipgre_netlink_encap_parms(data, &ipencap)) {
1332 struct ip_tunnel *t = netdev_priv(dev);
Petr Machatae1f8f782020-03-13 13:39:36 +02001333 int err = ip_tunnel_encap_setup(t, &ipencap);
Tom Herbert4565e992014-09-17 12:26:01 -07001334
1335 if (err < 0)
1336 return err;
1337 }
Herbert Xuc19e6542008-10-09 11:59:55 -07001338
Petr Machatae1f8f782020-03-13 13:39:36 +02001339 return 0;
1340}
1341
1342static int ipgre_newlink(struct net *src_net, struct net_device *dev,
1343 struct nlattr *tb[], struct nlattr *data[],
1344 struct netlink_ext_ack *extack)
1345{
1346 struct ip_tunnel_parm p;
1347 __u32 fwmark = 0;
1348 int err;
1349
1350 err = ipgre_newlink_encap_setup(dev, data);
1351 if (err)
1352 return err;
1353
Craig Gallek9830ad42017-04-19 12:30:54 -04001354 err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001355 if (err < 0)
1356 return err;
Craig Gallek9830ad42017-04-19 12:30:54 -04001357 return ip_tunnel_newlink(dev, tb, &p, fwmark);
Herbert Xuc19e6542008-10-09 11:59:55 -07001358}
1359
Petr Machatae1f8f782020-03-13 13:39:36 +02001360static int erspan_newlink(struct net *src_net, struct net_device *dev,
1361 struct nlattr *tb[], struct nlattr *data[],
1362 struct netlink_ext_ack *extack)
1363{
1364 struct ip_tunnel_parm p;
1365 __u32 fwmark = 0;
1366 int err;
1367
1368 err = ipgre_newlink_encap_setup(dev, data);
1369 if (err)
1370 return err;
1371
1372 err = erspan_netlink_parms(dev, data, tb, &p, &fwmark);
1373 if (err)
1374 return err;
1375 return ip_tunnel_newlink(dev, tb, &p, fwmark);
1376}
1377
Herbert Xuc19e6542008-10-09 11:59:55 -07001378static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001379 struct nlattr *data[],
1380 struct netlink_ext_ack *extack)
Herbert Xuc19e6542008-10-09 11:59:55 -07001381{
Craig Gallek9830ad42017-04-19 12:30:54 -04001382 struct ip_tunnel *t = netdev_priv(dev);
Craig Gallek9830ad42017-04-19 12:30:54 -04001383 __u32 fwmark = t->fwmark;
Xin Longdd9d5982017-11-07 16:33:08 +08001384 struct ip_tunnel_parm p;
Philip Prindeville22a59be2016-06-14 15:53:02 -06001385 int err;
Tom Herbert4565e992014-09-17 12:26:01 -07001386
Petr Machatae1f8f782020-03-13 13:39:36 +02001387 err = ipgre_newlink_encap_setup(dev, data);
1388 if (err)
1389 return err;
Herbert Xuc19e6542008-10-09 11:59:55 -07001390
Craig Gallek9830ad42017-04-19 12:30:54 -04001391 err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001392 if (err < 0)
1393 return err;
Xin Longdd9d5982017-11-07 16:33:08 +08001394
1395 err = ip_tunnel_changelink(dev, tb, &p, fwmark);
1396 if (err < 0)
1397 return err;
1398
1399 t->parms.i_flags = p.i_flags;
1400 t->parms.o_flags = p.o_flags;
1401
Petr Machatae1f8f782020-03-13 13:39:36 +02001402 ipgre_link_update(dev, !tb[IFLA_MTU]);
1403
1404 return 0;
1405}
1406
1407static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],
1408 struct nlattr *data[],
1409 struct netlink_ext_ack *extack)
1410{
1411 struct ip_tunnel *t = netdev_priv(dev);
1412 __u32 fwmark = t->fwmark;
1413 struct ip_tunnel_parm p;
1414 int err;
1415
1416 err = ipgre_newlink_encap_setup(dev, data);
1417 if (err)
1418 return err;
1419
1420 err = erspan_netlink_parms(dev, data, tb, &p, &fwmark);
1421 if (err < 0)
1422 return err;
1423
1424 err = ip_tunnel_changelink(dev, tb, &p, fwmark);
1425 if (err < 0)
1426 return err;
1427
1428 t->parms.i_flags = p.i_flags;
1429 t->parms.o_flags = p.o_flags;
Xin Longdd9d5982017-11-07 16:33:08 +08001430
1431 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001432}
1433
1434static size_t ipgre_get_size(const struct net_device *dev)
1435{
1436 return
1437 /* IFLA_GRE_LINK */
1438 nla_total_size(4) +
1439 /* IFLA_GRE_IFLAGS */
1440 nla_total_size(2) +
1441 /* IFLA_GRE_OFLAGS */
1442 nla_total_size(2) +
1443 /* IFLA_GRE_IKEY */
1444 nla_total_size(4) +
1445 /* IFLA_GRE_OKEY */
1446 nla_total_size(4) +
1447 /* IFLA_GRE_LOCAL */
1448 nla_total_size(4) +
1449 /* IFLA_GRE_REMOTE */
1450 nla_total_size(4) +
1451 /* IFLA_GRE_TTL */
1452 nla_total_size(1) +
1453 /* IFLA_GRE_TOS */
1454 nla_total_size(1) +
1455 /* IFLA_GRE_PMTUDISC */
1456 nla_total_size(1) +
Tom Herbert4565e992014-09-17 12:26:01 -07001457 /* IFLA_GRE_ENCAP_TYPE */
1458 nla_total_size(2) +
1459 /* IFLA_GRE_ENCAP_FLAGS */
1460 nla_total_size(2) +
1461 /* IFLA_GRE_ENCAP_SPORT */
1462 nla_total_size(2) +
1463 /* IFLA_GRE_ENCAP_DPORT */
1464 nla_total_size(2) +
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001465 /* IFLA_GRE_COLLECT_METADATA */
1466 nla_total_size(0) +
Philip Prindeville22a59be2016-06-14 15:53:02 -06001467 /* IFLA_GRE_IGNORE_DF */
1468 nla_total_size(1) +
Craig Gallek9830ad42017-04-19 12:30:54 -04001469 /* IFLA_GRE_FWMARK */
1470 nla_total_size(4) +
William Tu84e54fe2017-08-22 09:40:28 -07001471 /* IFLA_GRE_ERSPAN_INDEX */
1472 nla_total_size(4) +
William Tuf551c912017-12-13 16:38:56 -08001473 /* IFLA_GRE_ERSPAN_VER */
1474 nla_total_size(1) +
1475 /* IFLA_GRE_ERSPAN_DIR */
1476 nla_total_size(1) +
1477 /* IFLA_GRE_ERSPAN_HWID */
1478 nla_total_size(2) +
Herbert Xuc19e6542008-10-09 11:59:55 -07001479 0;
1480}
1481
1482static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1483{
1484 struct ip_tunnel *t = netdev_priv(dev);
1485 struct ip_tunnel_parm *p = &t->parms;
Lorenzo Bianconifeaf5c72019-01-28 22:23:48 +01001486 __be16 o_flags = p->o_flags;
1487
William Tuf989d542020-05-05 09:05:06 -07001488 if (t->erspan_ver <= 2) {
1489 if (t->erspan_ver != 0 && !t->collect_md)
Lorenzo Bianconi2bdf7002019-02-19 17:42:05 +01001490 o_flags |= TUNNEL_KEY;
1491
1492 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, t->erspan_ver))
1493 goto nla_put_failure;
1494
1495 if (t->erspan_ver == 1) {
1496 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, t->index))
1497 goto nla_put_failure;
William Tuf989d542020-05-05 09:05:06 -07001498 } else if (t->erspan_ver == 2) {
Lorenzo Bianconi2bdf7002019-02-19 17:42:05 +01001499 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, t->dir))
1500 goto nla_put_failure;
1501 if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, t->hwid))
1502 goto nla_put_failure;
1503 }
1504 }
Herbert Xuc19e6542008-10-09 11:59:55 -07001505
David S. Millerf3756b72012-04-01 20:39:02 -04001506 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
Tom Herbert95f5c642016-04-29 17:12:16 -07001507 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1508 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1509 nla_put_be16(skb, IFLA_GRE_OFLAGS,
Lorenzo Bianconifeaf5c72019-01-28 22:23:48 +01001510 gre_tnl_flags_to_gre_flags(o_flags)) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001511 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1512 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001513 nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
1514 nla_put_in_addr(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001515 nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
1516 nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
1517 nla_put_u8(skb, IFLA_GRE_PMTUDISC,
Craig Gallek9830ad42017-04-19 12:30:54 -04001518 !!(p->iph.frag_off & htons(IP_DF))) ||
1519 nla_put_u32(skb, IFLA_GRE_FWMARK, t->fwmark))
David S. Millerf3756b72012-04-01 20:39:02 -04001520 goto nla_put_failure;
Tom Herbert4565e992014-09-17 12:26:01 -07001521
1522 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1523 t->encap.type) ||
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001524 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1525 t->encap.sport) ||
1526 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1527 t->encap.dport) ||
Tom Herbert4565e992014-09-17 12:26:01 -07001528 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
Tom Herberte1b2cb62014-11-05 16:49:38 -08001529 t->encap.flags))
Tom Herbert4565e992014-09-17 12:26:01 -07001530 goto nla_put_failure;
1531
Philip Prindeville22a59be2016-06-14 15:53:02 -06001532 if (nla_put_u8(skb, IFLA_GRE_IGNORE_DF, t->ignore_df))
1533 goto nla_put_failure;
1534
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001535 if (t->collect_md) {
1536 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
1537 goto nla_put_failure;
1538 }
1539
Herbert Xuc19e6542008-10-09 11:59:55 -07001540 return 0;
1541
1542nla_put_failure:
1543 return -EMSGSIZE;
1544}
1545
William Tu84e54fe2017-08-22 09:40:28 -07001546static void erspan_setup(struct net_device *dev)
1547{
Xin Long84581bd2018-08-27 18:41:32 +08001548 struct ip_tunnel *t = netdev_priv(dev);
1549
William Tu84e54fe2017-08-22 09:40:28 -07001550 ether_setup(dev);
Haishuang Yan0e141f72019-09-27 14:58:20 +08001551 dev->max_mtu = 0;
William Tu84e54fe2017-08-22 09:40:28 -07001552 dev->netdev_ops = &erspan_netdev_ops;
1553 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1554 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1555 ip_tunnel_setup(dev, erspan_net_id);
Xin Long84581bd2018-08-27 18:41:32 +08001556 t->erspan_ver = 1;
William Tu84e54fe2017-08-22 09:40:28 -07001557}
1558
Herbert Xuc19e6542008-10-09 11:59:55 -07001559static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
1560 [IFLA_GRE_LINK] = { .type = NLA_U32 },
1561 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
1562 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
1563 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
1564 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001565 [IFLA_GRE_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
1566 [IFLA_GRE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
Herbert Xuc19e6542008-10-09 11:59:55 -07001567 [IFLA_GRE_TTL] = { .type = NLA_U8 },
1568 [IFLA_GRE_TOS] = { .type = NLA_U8 },
1569 [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
Tom Herbert4565e992014-09-17 12:26:01 -07001570 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
1571 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
1572 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
1573 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001574 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
Philip Prindeville22a59be2016-06-14 15:53:02 -06001575 [IFLA_GRE_IGNORE_DF] = { .type = NLA_U8 },
Craig Gallek9830ad42017-04-19 12:30:54 -04001576 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
William Tu84e54fe2017-08-22 09:40:28 -07001577 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
William Tuf551c912017-12-13 16:38:56 -08001578 [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
1579 [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
1580 [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
Herbert Xuc19e6542008-10-09 11:59:55 -07001581};
1582
1583static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
1584 .kind = "gre",
1585 .maxtype = IFLA_GRE_MAX,
1586 .policy = ipgre_policy,
1587 .priv_size = sizeof(struct ip_tunnel),
1588 .setup = ipgre_tunnel_setup,
1589 .validate = ipgre_tunnel_validate,
1590 .newlink = ipgre_newlink,
1591 .changelink = ipgre_changelink,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001592 .dellink = ip_tunnel_dellink,
Herbert Xuc19e6542008-10-09 11:59:55 -07001593 .get_size = ipgre_get_size,
1594 .fill_info = ipgre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001595 .get_link_net = ip_tunnel_get_link_net,
Herbert Xuc19e6542008-10-09 11:59:55 -07001596};
1597
Herbert Xue1a80002008-10-09 12:00:17 -07001598static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
1599 .kind = "gretap",
1600 .maxtype = IFLA_GRE_MAX,
1601 .policy = ipgre_policy,
1602 .priv_size = sizeof(struct ip_tunnel),
1603 .setup = ipgre_tap_setup,
1604 .validate = ipgre_tap_validate,
1605 .newlink = ipgre_newlink,
1606 .changelink = ipgre_changelink,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001607 .dellink = ip_tunnel_dellink,
Herbert Xue1a80002008-10-09 12:00:17 -07001608 .get_size = ipgre_get_size,
1609 .fill_info = ipgre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001610 .get_link_net = ip_tunnel_get_link_net,
Herbert Xue1a80002008-10-09 12:00:17 -07001611};
1612
William Tu84e54fe2017-08-22 09:40:28 -07001613static struct rtnl_link_ops erspan_link_ops __read_mostly = {
1614 .kind = "erspan",
1615 .maxtype = IFLA_GRE_MAX,
1616 .policy = ipgre_policy,
1617 .priv_size = sizeof(struct ip_tunnel),
1618 .setup = erspan_setup,
1619 .validate = erspan_validate,
Petr Machatae1f8f782020-03-13 13:39:36 +02001620 .newlink = erspan_newlink,
1621 .changelink = erspan_changelink,
William Tu84e54fe2017-08-22 09:40:28 -07001622 .dellink = ip_tunnel_dellink,
1623 .get_size = ipgre_get_size,
1624 .fill_info = ipgre_fill_info,
1625 .get_link_net = ip_tunnel_get_link_net,
1626};
1627
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001628struct net_device *gretap_fb_dev_create(struct net *net, const char *name,
1629 u8 name_assign_type)
1630{
1631 struct nlattr *tb[IFLA_MAX + 1];
1632 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001633 LIST_HEAD(list_kill);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001634 struct ip_tunnel *t;
1635 int err;
1636
1637 memset(&tb, 0, sizeof(tb));
1638
1639 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08001640 &ipgre_tap_ops, tb, NULL);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001641 if (IS_ERR(dev))
1642 return dev;
1643
1644 /* Configure flow based GRE device. */
1645 t = netdev_priv(dev);
1646 t->collect_md = true;
1647
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001648 err = ipgre_newlink(net, dev, tb, NULL, NULL);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001649 if (err < 0) {
1650 free_netdev(dev);
1651 return ERR_PTR(err);
1652 }
David Wragg7e059152016-02-10 00:05:58 +00001653
1654 /* openvswitch users expect packet sizes to be unrestricted,
1655 * so set the largest MTU we can.
1656 */
1657 err = __ip_tunnel_change_mtu(dev, IP_MAX_MTU, false);
1658 if (err)
1659 goto out;
1660
Nicolas Dichtelda6f1da2016-06-13 10:31:06 +02001661 err = rtnl_configure_link(dev, NULL);
1662 if (err < 0)
1663 goto out;
1664
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001665 return dev;
1666out:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001667 ip_tunnel_dellink(dev, &list_kill);
1668 unregister_netdevice_many(&list_kill);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001669 return ERR_PTR(err);
1670}
1671EXPORT_SYMBOL_GPL(gretap_fb_dev_create);
1672
Pravin B Shelarc5441932013-03-25 14:49:35 +00001673static int __net_init ipgre_tap_init_net(struct net *net)
1674{
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001675 return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, "gretap0");
Pravin B Shelarc5441932013-03-25 14:49:35 +00001676}
1677
Eric Dumazet64bc1782017-09-19 16:27:09 -07001678static void __net_exit ipgre_tap_exit_batch_net(struct list_head *list_net)
Pravin B Shelarc5441932013-03-25 14:49:35 +00001679{
Eric Dumazet64bc1782017-09-19 16:27:09 -07001680 ip_tunnel_delete_nets(list_net, gre_tap_net_id, &ipgre_tap_ops);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001681}
1682
1683static struct pernet_operations ipgre_tap_net_ops = {
1684 .init = ipgre_tap_init_net,
Eric Dumazet64bc1782017-09-19 16:27:09 -07001685 .exit_batch = ipgre_tap_exit_batch_net,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001686 .id = &gre_tap_net_id,
1687 .size = sizeof(struct ip_tunnel_net),
1688};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
William Tu84e54fe2017-08-22 09:40:28 -07001690static int __net_init erspan_init_net(struct net *net)
1691{
1692 return ip_tunnel_init_net(net, erspan_net_id,
1693 &erspan_link_ops, "erspan0");
1694}
1695
Eric Dumazet64bc1782017-09-19 16:27:09 -07001696static void __net_exit erspan_exit_batch_net(struct list_head *net_list)
William Tu84e54fe2017-08-22 09:40:28 -07001697{
Eric Dumazet64bc1782017-09-19 16:27:09 -07001698 ip_tunnel_delete_nets(net_list, erspan_net_id, &erspan_link_ops);
William Tu84e54fe2017-08-22 09:40:28 -07001699}
1700
1701static struct pernet_operations erspan_net_ops = {
1702 .init = erspan_init_net,
Eric Dumazet64bc1782017-09-19 16:27:09 -07001703 .exit_batch = erspan_exit_batch_net,
William Tu84e54fe2017-08-22 09:40:28 -07001704 .id = &erspan_net_id,
1705 .size = sizeof(struct ip_tunnel_net),
1706};
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708static int __init ipgre_init(void)
1709{
1710 int err;
1711
Joe Perches058bd4d2012-03-11 18:36:11 +00001712 pr_info("GRE over IPv4 tunneling driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Eric W. Biedermancfb8fbf2009-11-29 15:46:13 +00001714 err = register_pernet_device(&ipgre_net_ops);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001715 if (err < 0)
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001716 return err;
1717
Pravin B Shelarc5441932013-03-25 14:49:35 +00001718 err = register_pernet_device(&ipgre_tap_net_ops);
1719 if (err < 0)
William Tue3d03282017-08-22 17:04:05 -07001720 goto pnet_tap_failed;
Pravin B Shelarc5441932013-03-25 14:49:35 +00001721
William Tu84e54fe2017-08-22 09:40:28 -07001722 err = register_pernet_device(&erspan_net_ops);
1723 if (err < 0)
1724 goto pnet_erspan_failed;
1725
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001726 err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001727 if (err < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001728 pr_info("%s: can't add protocol\n", __func__);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001729 goto add_proto_failed;
1730 }
Pavel Emelyanov7daa0002008-04-16 01:10:05 -07001731
Herbert Xuc19e6542008-10-09 11:59:55 -07001732 err = rtnl_link_register(&ipgre_link_ops);
1733 if (err < 0)
1734 goto rtnl_link_failed;
1735
Herbert Xue1a80002008-10-09 12:00:17 -07001736 err = rtnl_link_register(&ipgre_tap_ops);
1737 if (err < 0)
1738 goto tap_ops_failed;
1739
William Tu84e54fe2017-08-22 09:40:28 -07001740 err = rtnl_link_register(&erspan_link_ops);
1741 if (err < 0)
1742 goto erspan_link_failed;
1743
Pravin B Shelarc5441932013-03-25 14:49:35 +00001744 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001745
William Tu84e54fe2017-08-22 09:40:28 -07001746erspan_link_failed:
1747 rtnl_link_unregister(&ipgre_tap_ops);
Herbert Xue1a80002008-10-09 12:00:17 -07001748tap_ops_failed:
1749 rtnl_link_unregister(&ipgre_link_ops);
Herbert Xuc19e6542008-10-09 11:59:55 -07001750rtnl_link_failed:
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001751 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001752add_proto_failed:
William Tu84e54fe2017-08-22 09:40:28 -07001753 unregister_pernet_device(&erspan_net_ops);
1754pnet_erspan_failed:
Pravin B Shelarc5441932013-03-25 14:49:35 +00001755 unregister_pernet_device(&ipgre_tap_net_ops);
William Tue3d03282017-08-22 17:04:05 -07001756pnet_tap_failed:
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001757 unregister_pernet_device(&ipgre_net_ops);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001758 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001761static void __exit ipgre_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Herbert Xue1a80002008-10-09 12:00:17 -07001763 rtnl_link_unregister(&ipgre_tap_ops);
Herbert Xuc19e6542008-10-09 11:59:55 -07001764 rtnl_link_unregister(&ipgre_link_ops);
William Tu84e54fe2017-08-22 09:40:28 -07001765 rtnl_link_unregister(&erspan_link_ops);
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001766 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001767 unregister_pernet_device(&ipgre_tap_net_ops);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001768 unregister_pernet_device(&ipgre_net_ops);
William Tu84e54fe2017-08-22 09:40:28 -07001769 unregister_pernet_device(&erspan_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
1772module_init(ipgre_init);
1773module_exit(ipgre_fini);
1774MODULE_LICENSE("GPL");
Patrick McHardy4d74f8b2008-10-10 12:11:06 -07001775MODULE_ALIAS_RTNL_LINK("gre");
1776MODULE_ALIAS_RTNL_LINK("gretap");
William Tu84e54fe2017-08-22 09:40:28 -07001777MODULE_ALIAS_RTNL_LINK("erspan");
Vasiliy Kulikov8909c9a2011-03-02 00:33:13 +03001778MODULE_ALIAS_NETDEV("gre0");
Pravin B Shelarc5441932013-03-25 14:49:35 +00001779MODULE_ALIAS_NETDEV("gretap0");
William Tu84e54fe2017-08-22 09:40:28 -07001780MODULE_ALIAS_NETDEV("erspan0");