blob: 029b24eeafbaf8ec28b0f55ff08de58b8fb0efdd [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003 * Linux NET3: GRE over IP protocol decoder.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.ru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Joe Perchesafd465032012-03-12 07:03:32 +00008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Randy Dunlap4fc268d2006-01-11 12:17:47 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080015#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/skbuff.h>
17#include <linux/netdevice.h>
18#include <linux/in.h>
19#include <linux/tcp.h>
20#include <linux/udp.h>
21#include <linux/if_arp.h>
Pravin B Shelar2e15ea32015-08-07 23:51:42 -070022#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/in6.h>
25#include <linux/inetdevice.h>
26#include <linux/igmp.h>
27#include <linux/netfilter_ipv4.h>
Herbert Xue1a80002008-10-09 12:00:17 -070028#include <linux/etherdevice.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080029#include <linux/if_ether.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <net/sock.h>
32#include <net/ip.h>
33#include <net/icmp.h>
34#include <net/protocol.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000035#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <net/arp.h>
37#include <net/checksum.h>
38#include <net/dsfield.h>
39#include <net/inet_ecn.h>
40#include <net/xfrm.h>
Pavel Emelyanov59a4c752008-04-16 01:08:53 -070041#include <net/net_namespace.h>
42#include <net/netns/generic.h>
Herbert Xuc19e6542008-10-09 11:59:55 -070043#include <net/rtnetlink.h>
Dmitry Kozlov00959ad2010-08-21 23:05:39 -070044#include <net/gre.h>
Pravin B Shelar2e15ea32015-08-07 23:51:42 -070045#include <net/dst_metadata.h>
William Tu84e54fe2017-08-22 09:40:28 -070046#include <net/erspan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 Problems & solutions
50 --------------------
51
52 1. The most important issue is detecting local dead loops.
53 They would cause complete host lockup in transmit, which
54 would be "resolved" by stack overflow or, if queueing is enabled,
55 with infinite looping in net_bh.
56
57 We cannot track such dead loops during route installation,
58 it is infeasible task. The most general solutions would be
59 to keep skb->encapsulation counter (sort of local ttl),
Eric Dumazet6d0722a2010-09-29 23:35:10 -070060 and silently drop packet when it expires. It is a good
stephen hemmingerbff52852012-02-24 08:08:20 +000061 solution, but it supposes maintaining new variable in ALL
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 skb, even if no tunneling is used.
63
Eric Dumazet6d0722a2010-09-29 23:35:10 -070064 Current solution: xmit_recursion breaks dead loops. This is a percpu
65 counter, since when we enter the first ndo_xmit(), cpu migration is
66 forbidden. We force an exit if this counter reaches RECURSION_LIMIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 2. Networking dead loops would not kill routers, but would really
69 kill network. IP hop limit plays role of "t->recursion" in this case,
70 if we copy it from packet being encapsulated to upper header.
71 It is very good solution, but it introduces two problems:
72
73 - Routing protocols, using packets with ttl=1 (OSPF, RIP2),
74 do not work over tunnels.
75 - traceroute does not work. I planned to relay ICMP from tunnel,
76 so that this problem would be solved and traceroute output
77 would even more informative. This idea appeared to be wrong:
78 only Linux complies to rfc1812 now (yes, guys, Linux is the only
79 true router now :-)), all routers (at least, in neighbourhood of mine)
80 return only 8 bytes of payload. It is the end.
81
82 Hence, if we want that OSPF worked or traceroute said something reasonable,
83 we should search for another solution.
84
85 One of them is to parse packet trying to detect inner encapsulation
86 made by our node. It is difficult or even impossible, especially,
stephen hemmingerbff52852012-02-24 08:08:20 +000087 taking into account fragmentation. TO be short, ttl is not solution at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 Current solution: The solution was UNEXPECTEDLY SIMPLE.
90 We force DF flag on tunnels with preconfigured hop limit,
91 that is ALL. :-) Well, it does not remove the problem completely,
92 but exponential growth of network traffic is changed to linear
93 (branches, that exceed pmtu are pruned) and tunnel mtu
stephen hemmingerbff52852012-02-24 08:08:20 +000094 rapidly degrades to value <68, where looping stops.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 Yes, it is not good if there exists a router in the loop,
96 which does not force DF, even when encapsulating packets have DF set.
97 But it is not our problem! Nobody could accuse us, we made
98 all that we could make. Even if it is your gated who injected
99 fatal route to network, even if it were you who configured
100 fatal static route: you are innocent. :-)
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 Alexey Kuznetsov.
103 */
104
stephen hemmingereccc1bb2012-09-25 11:02:48 +0000105static bool log_ecn_error = true;
106module_param(log_ecn_error, bool, 0644);
107MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
108
Herbert Xuc19e6542008-10-09 11:59:55 -0700109static struct rtnl_link_ops ipgre_link_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static int ipgre_tunnel_init(struct net_device *dev);
William Tu1a66a832017-08-25 09:21:28 -0700111static void erspan_build_header(struct sk_buff *skb,
William Tuc69de582018-01-25 13:20:09 -0800112 u32 id, u32 index,
William Tua3222dc2017-11-30 11:51:27 -0800113 bool truncate, bool is_ipv4);
Pavel Emelyanoveb8ce742008-04-16 01:10:26 -0700114
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300115static unsigned int ipgre_net_id __read_mostly;
116static unsigned int gre_tap_net_id __read_mostly;
William Tu84e54fe2017-08-22 09:40:28 -0700117static unsigned int erspan_net_id __read_mostly;
Pavel Emelyanoveb8ce742008-04-16 01:10:26 -0700118
Stefano Brivio32bbd872018-11-08 12:19:21 +0100119static int ipgre_err(struct sk_buff *skb, u32 info,
120 const struct tnl_ptk_info *tpi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Pravin B Shelarc5441932013-03-25 14:49:35 +0000123 /* All the routers (except for Linux) return only
124 8 bytes of packet payload. It means, that precise relaying of
125 ICMP in the real Internet is absolutely infeasible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Pravin B Shelarc5441932013-03-25 14:49:35 +0000127 Moreover, Cisco "wise men" put GRE key to the third word
128 in GRE header. It makes impossible maintaining even soft
129 state for keyed GRE tunnels with enabled checksum. Tell
130 them "thank you".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Pravin B Shelarc5441932013-03-25 14:49:35 +0000132 Well, I wonder, rfc1812 was written by Cisco employee,
133 what the hell these idiots break standards established
134 by themselves???
135 */
136 struct net *net = dev_net(skb->dev);
137 struct ip_tunnel_net *itn;
Eric Dumazet96f5a842013-05-18 08:36:03 +0000138 const struct iphdr *iph;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300139 const int type = icmp_hdr(skb)->type;
140 const int code = icmp_hdr(skb)->code;
Eric Dumazet20e19542016-06-18 21:52:06 -0700141 unsigned int data_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct ip_tunnel *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Pravin B Shelarbda7bb42013-06-17 17:49:38 -0700144 if (tpi->proto == htons(ETH_P_TEB))
Pravin B Shelarc5441932013-03-25 14:49:35 +0000145 itn = net_generic(net, gre_tap_net_id);
Haishuang Yan51dc63e2018-09-10 22:19:48 +0800146 else if (tpi->proto == htons(ETH_P_ERSPAN) ||
147 tpi->proto == htons(ETH_P_ERSPAN2))
148 itn = net_generic(net, erspan_net_id);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000149 else
150 itn = net_generic(net, ipgre_net_id);
151
Duan Jiongc0c0c502014-01-28 11:49:43 +0800152 iph = (const struct iphdr *)(icmp_hdr(skb) + 1);
Pravin B Shelarbda7bb42013-06-17 17:49:38 -0700153 t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
154 iph->daddr, iph->saddr, tpi->key);
stephen hemmingerd2083282012-09-24 18:12:23 +0000155
Ian Morris51456b22015-04-03 09:17:26 +0100156 if (!t)
Stefano Brivio32bbd872018-11-08 12:19:21 +0100157 return -ENOENT;
158
159 switch (type) {
160 default:
161 case ICMP_PARAMETERPROB:
162 return 0;
163
164 case ICMP_DEST_UNREACH:
165 switch (code) {
166 case ICMP_SR_FAILED:
167 case ICMP_PORT_UNREACH:
168 /* Impossible event. */
169 return 0;
170 default:
171 /* All others are translated to HOST_UNREACH.
172 rfc2003 contains "deep thoughts" about NET_UNREACH,
173 I believe they are just ether pollution. --ANK
174 */
175 break;
176 }
177 break;
178
179 case ICMP_TIME_EXCEEDED:
180 if (code != ICMP_EXC_TTL)
181 return 0;
182 data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */
183 break;
184
185 case ICMP_REDIRECT:
186 break;
187 }
David S. Miller36393392012-06-14 22:21:46 -0700188
Eric Dumazet9b8c6d72016-06-18 21:52:05 -0700189#if IS_ENABLED(CONFIG_IPV6)
190 if (tpi->proto == htons(ETH_P_IPV6) &&
Eric Dumazet20e19542016-06-18 21:52:06 -0700191 !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4 + tpi->hdr_len,
192 type, data_len))
Stefano Brivio32bbd872018-11-08 12:19:21 +0100193 return 0;
Eric Dumazet9b8c6d72016-06-18 21:52:05 -0700194#endif
195
David S. Miller36393392012-06-14 22:21:46 -0700196 if (t->parms.iph.daddr == 0 ||
Joe Perchesf97c1e02007-12-16 13:45:43 -0800197 ipv4_is_multicast(t->parms.iph.daddr))
Stefano Brivio32bbd872018-11-08 12:19:21 +0100198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
Stefano Brivio32bbd872018-11-08 12:19:21 +0100201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Wei Yongjunda6185d82009-02-24 23:34:48 -0800203 if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 t->err_count++;
205 else
206 t->err_count = 1;
207 t->err_time = jiffies;
Stefano Brivio32bbd872018-11-08 12:19:21 +0100208
209 return 0;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700210}
211
212static void gre_err(struct sk_buff *skb, u32 info)
213{
214 /* All the routers (except for Linux) return only
215 * 8 bytes of packet payload. It means, that precise relaying of
216 * ICMP in the real Internet is absolutely infeasible.
217 *
218 * Moreover, Cisco "wise men" put GRE key to the third word
219 * in GRE header. It makes impossible maintaining even soft
220 * state for keyed
221 * GRE tunnels with enabled checksum. Tell them "thank you".
222 *
223 * Well, I wonder, rfc1812 was written by Cisco employee,
224 * what the hell these idiots break standards established
225 * by themselves???
226 */
227
Eric Dumazete582615ad2016-06-15 06:24:00 -0700228 const struct iphdr *iph = (struct iphdr *)skb->data;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700229 const int type = icmp_hdr(skb)->type;
230 const int code = icmp_hdr(skb)->code;
231 struct tnl_ptk_info tpi;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700232
Haishuang Yanb0350d52018-09-14 12:26:47 +0800233 if (gre_parse_header(skb, &tpi, NULL, htons(ETH_P_IP),
234 iph->ihl * 4) < 0)
235 return;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700236
237 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
238 ipv4_update_pmtu(skb, dev_net(skb->dev), info,
Maciej Żenczykowskid888f392018-09-25 20:56:26 -0700239 skb->dev->ifindex, IPPROTO_GRE);
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700240 return;
241 }
242 if (type == ICMP_REDIRECT) {
Maciej Żenczykowski1042caa2018-09-25 20:56:27 -0700243 ipv4_redirect(skb, dev_net(skb->dev), skb->dev->ifindex,
244 IPPROTO_GRE);
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700245 return;
246 }
247
248 ipgre_err(skb, info, &tpi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
William Tu84e54fe2017-08-22 09:40:28 -0700251static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi,
252 int gre_hdr_len)
253{
254 struct net *net = dev_net(skb->dev);
255 struct metadata_dst *tun_dst = NULL;
William Tu1d7e2ed2017-12-13 16:38:55 -0800256 struct erspan_base_hdr *ershdr;
William Tu84e54fe2017-08-22 09:40:28 -0700257 struct ip_tunnel_net *itn;
258 struct ip_tunnel *tunnel;
William Tu84e54fe2017-08-22 09:40:28 -0700259 const struct iphdr *iph;
William Tu3df192832018-02-05 13:35:34 -0800260 struct erspan_md2 *md2;
William Tu1d7e2ed2017-12-13 16:38:55 -0800261 int ver;
William Tu84e54fe2017-08-22 09:40:28 -0700262 int len;
263
264 itn = net_generic(net, erspan_net_id);
William Tu84e54fe2017-08-22 09:40:28 -0700265
266 iph = ip_hdr(skb);
William Tu1d7e2ed2017-12-13 16:38:55 -0800267 ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len);
William Tuc69de582018-01-25 13:20:09 -0800268 ver = ershdr->ver;
William Tu84e54fe2017-08-22 09:40:28 -0700269
William Tu84e54fe2017-08-22 09:40:28 -0700270 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex,
271 tpi->flags | TUNNEL_KEY,
272 iph->saddr, iph->daddr, tpi->key);
273
274 if (tunnel) {
William Tu1d7e2ed2017-12-13 16:38:55 -0800275 len = gre_hdr_len + erspan_hdr_len(ver);
276 if (unlikely(!pskb_may_pull(skb, len)))
William Tuae3e1332017-12-15 14:27:43 -0800277 return PACKET_REJECT;
William Tu1d7e2ed2017-12-13 16:38:55 -0800278
William Tu84e54fe2017-08-22 09:40:28 -0700279 if (__iptunnel_pull_header(skb,
William Tu1d7e2ed2017-12-13 16:38:55 -0800280 len,
William Tu84e54fe2017-08-22 09:40:28 -0700281 htons(ETH_P_TEB),
282 false, false) < 0)
283 goto drop;
284
William Tu1a66a832017-08-25 09:21:28 -0700285 if (tunnel->collect_md) {
Lorenzo Bianconi492b67e2019-04-06 17:16:52 +0200286 struct erspan_metadata *pkt_md, *md;
William Tu1a66a832017-08-25 09:21:28 -0700287 struct ip_tunnel_info *info;
Lorenzo Bianconi492b67e2019-04-06 17:16:52 +0200288 unsigned char *gh;
William Tu1a66a832017-08-25 09:21:28 -0700289 __be64 tun_id;
290 __be16 flags;
291
292 tpi->flags |= TUNNEL_KEY;
293 flags = tpi->flags;
294 tun_id = key32_to_tunnel_id(tpi->key);
295
296 tun_dst = ip_tun_rx_dst(skb, flags,
297 tun_id, sizeof(*md));
298 if (!tun_dst)
299 return PACKET_REJECT;
300
Lorenzo Bianconi492b67e2019-04-06 17:16:52 +0200301 /* skb can be uncloned in __iptunnel_pull_header, so
302 * old pkt_md is no longer valid and we need to reset
303 * it
304 */
305 gh = skb_network_header(skb) +
306 skb_network_header_len(skb);
307 pkt_md = (struct erspan_metadata *)(gh + gre_hdr_len +
308 sizeof(*ershdr));
William Tu1a66a832017-08-25 09:21:28 -0700309 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
William Tuf551c912017-12-13 16:38:56 -0800310 md->version = ver;
William Tu3df192832018-02-05 13:35:34 -0800311 md2 = &md->u.md2;
312 memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
313 ERSPAN_V2_MDSIZE);
William Tuf551c912017-12-13 16:38:56 -0800314
William Tu1a66a832017-08-25 09:21:28 -0700315 info = &tun_dst->u.tun_info;
316 info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
317 info->options_len = sizeof(*md);
William Tu1a66a832017-08-25 09:21:28 -0700318 }
319
William Tu84e54fe2017-08-22 09:40:28 -0700320 skb_reset_mac_header(skb);
321 ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
322 return PACKET_RCVD;
323 }
Haishuang Yan5a645062018-09-10 22:19:47 +0800324 return PACKET_REJECT;
325
William Tu84e54fe2017-08-22 09:40:28 -0700326drop:
327 kfree_skb(skb);
328 return PACKET_RCVD;
329}
330
Jiri Benc125372f2016-05-03 17:10:08 +0200331static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
332 struct ip_tunnel_net *itn, int hdr_len, bool raw_proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700334 struct metadata_dst *tun_dst = NULL;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000335 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct ip_tunnel *tunnel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700338 iph = ip_hdr(skb);
Pravin B Shelarbda7bb42013-06-17 17:49:38 -0700339 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
340 iph->saddr, iph->daddr, tpi->key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
stephen hemmingerd2083282012-09-24 18:12:23 +0000342 if (tunnel) {
wenxuc0d59da2019-11-20 10:59:39 +0800343 const struct iphdr *tnl_params;
344
Jiri Benc125372f2016-05-03 17:10:08 +0200345 if (__iptunnel_pull_header(skb, hdr_len, tpi->proto,
346 raw_proto, false) < 0)
Jiri Benc244a7972016-05-03 17:10:07 +0200347 goto drop;
348
Jiri Bence271c7b2016-05-11 15:53:57 +0200349 if (tunnel->dev->type != ARPHRD_NONE)
350 skb_pop_mac_header(skb);
351 else
352 skb_reset_mac_header(skb);
wenxuc0d59da2019-11-20 10:59:39 +0800353
354 tnl_params = &tunnel->parms.iph;
355 if (tunnel->collect_md || tnl_params->daddr == 0) {
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700356 __be16 flags;
357 __be64 tun_id;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700358
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700359 flags = tpi->flags & (TUNNEL_CSUM | TUNNEL_KEY);
Amir Vadaid817f432016-09-08 16:23:45 +0300360 tun_id = key32_to_tunnel_id(tpi->key);
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700361 tun_dst = ip_tun_rx_dst(skb, flags, tun_id, 0);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700362 if (!tun_dst)
363 return PACKET_REJECT;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700364 }
365
366 ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
Pravin B Shelarbda7bb42013-06-17 17:49:38 -0700367 return PACKET_RCVD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Jiri Benc125372f2016-05-03 17:10:08 +0200369 return PACKET_NEXT;
Jiri Benc244a7972016-05-03 17:10:07 +0200370
371drop:
372 kfree_skb(skb);
373 return PACKET_RCVD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Jiri Benc125372f2016-05-03 17:10:08 +0200376static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
377 int hdr_len)
378{
379 struct net *net = dev_net(skb->dev);
380 struct ip_tunnel_net *itn;
381 int res;
382
383 if (tpi->proto == htons(ETH_P_TEB))
384 itn = net_generic(net, gre_tap_net_id);
385 else
386 itn = net_generic(net, ipgre_net_id);
387
388 res = __ipgre_rcv(skb, tpi, itn, hdr_len, false);
389 if (res == PACKET_NEXT && tpi->proto == htons(ETH_P_TEB)) {
390 /* ipgre tunnels in collect metadata mode should receive
391 * also ETH_P_TEB traffic.
392 */
393 itn = net_generic(net, ipgre_net_id);
394 res = __ipgre_rcv(skb, tpi, itn, hdr_len, true);
395 }
396 return res;
397}
398
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700399static int gre_rcv(struct sk_buff *skb)
400{
401 struct tnl_ptk_info tpi;
402 bool csum_err = false;
Tom Herbert95f5c642016-04-29 17:12:16 -0700403 int hdr_len;
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700404
405#ifdef CONFIG_NET_IPGRE_BROADCAST
406 if (ipv4_is_multicast(ip_hdr(skb)->daddr)) {
407 /* Looped back packet, drop it! */
408 if (rt_is_output_route(skb_rtable(skb)))
409 goto drop;
410 }
411#endif
412
Eric Dumazete582615ad2016-06-15 06:24:00 -0700413 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP), 0);
Jiri Bencf132ae72016-05-03 15:00:21 +0200414 if (hdr_len < 0)
Tom Herbert95f5c642016-04-29 17:12:16 -0700415 goto drop;
416
William Tuf551c912017-12-13 16:38:56 -0800417 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
418 tpi.proto == htons(ETH_P_ERSPAN2))) {
William Tu84e54fe2017-08-22 09:40:28 -0700419 if (erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
420 return 0;
Haishuang Yandd8d5b82017-12-20 10:21:46 +0800421 goto out;
William Tu84e54fe2017-08-22 09:40:28 -0700422 }
423
Jiri Benc244a7972016-05-03 17:10:07 +0200424 if (ipgre_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700425 return 0;
426
Haishuang Yandd8d5b82017-12-20 10:21:46 +0800427out:
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700428 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
429drop:
430 kfree_skb(skb);
431 return 0;
432}
433
Pravin B Shelarc5441932013-03-25 14:49:35 +0000434static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
435 const struct iphdr *tnl_params,
436 __be16 proto)
437{
438 struct ip_tunnel *tunnel = netdev_priv(dev);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000439
Pravin B Shelarc5441932013-03-25 14:49:35 +0000440 if (tunnel->parms.o_flags & TUNNEL_SEQ)
441 tunnel->o_seqno++;
Eric Dumazetcef401d2013-01-25 20:34:37 +0000442
Pravin B Shelarc5441932013-03-25 14:49:35 +0000443 /* Push GRE header. */
Tom Herbert182a3522016-04-29 17:12:19 -0700444 gre_build_header(skb, tunnel->tun_hlen,
445 tunnel->parms.o_flags, proto, tunnel->parms.o_key,
446 htonl(tunnel->o_seqno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Nicolas Dichtelbf3d6a82013-05-27 23:48:15 +0000448 ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Alexander Duyckaed069d2016-04-14 15:33:37 -0400451static int gre_handle_offloads(struct sk_buff *skb, bool csum)
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -0700452{
Edward Cree6fa79662016-02-11 21:02:31 +0000453 return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -0700454}
455
William Tu862a03c2017-08-25 09:21:27 -0700456static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev,
457 __be16 proto)
458{
William Tu77a51962018-03-01 13:49:57 -0800459 struct ip_tunnel *tunnel = netdev_priv(dev);
William Tu862a03c2017-08-25 09:21:27 -0700460 struct ip_tunnel_info *tun_info;
461 const struct ip_tunnel_key *key;
William Tu862a03c2017-08-25 09:21:27 -0700462 int tunnel_hlen;
wenxu962924f2019-01-22 18:39:52 +0800463 __be16 flags;
William Tu862a03c2017-08-25 09:21:27 -0700464
465 tun_info = skb_tunnel_info(skb);
466 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
467 ip_tunnel_info_af(tun_info) != AF_INET))
468 goto err_free_skb;
469
470 key = &tun_info->key;
471 tunnel_hlen = gre_calc_hlen(key->tun_flags);
472
wenxu962924f2019-01-22 18:39:52 +0800473 if (skb_cow_head(skb, dev->needed_headroom))
474 goto err_free_skb;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700475
476 /* Push Tunnel header. */
Alexander Duyckaed069d2016-04-14 15:33:37 -0400477 if (gre_handle_offloads(skb, !!(tun_info->key.tun_flags & TUNNEL_CSUM)))
wenxu962924f2019-01-22 18:39:52 +0800478 goto err_free_skb;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700479
William Tu77a51962018-03-01 13:49:57 -0800480 flags = tun_info->key.tun_flags &
481 (TUNNEL_CSUM | TUNNEL_KEY | TUNNEL_SEQ);
David S. Millercba653212016-05-04 00:52:29 -0400482 gre_build_header(skb, tunnel_hlen, flags, proto,
William Tu77a51962018-03-01 13:49:57 -0800483 tunnel_id_to_key32(tun_info->key.tun_id),
Colin Ian King15746392018-03-21 19:34:58 +0000484 (flags & TUNNEL_SEQ) ? htonl(tunnel->o_seqno++) : 0);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700485
wenxu962924f2019-01-22 18:39:52 +0800486 ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen);
Pravin B Shelar039f5062015-12-24 14:34:54 -0800487
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700488 return;
489
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700490err_free_skb:
491 kfree_skb(skb);
492 dev->stats.tx_dropped++;
493}
494
Xin Long20704bd2019-01-14 18:10:06 +0800495static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev)
William Tu1a66a832017-08-25 09:21:28 -0700496{
497 struct ip_tunnel *tunnel = netdev_priv(dev);
498 struct ip_tunnel_info *tun_info;
499 const struct ip_tunnel_key *key;
500 struct erspan_metadata *md;
William Tu1a66a832017-08-25 09:21:28 -0700501 bool truncate = false;
wenxu962924f2019-01-22 18:39:52 +0800502 __be16 proto;
William Tu1a66a832017-08-25 09:21:28 -0700503 int tunnel_hlen;
William Tuf551c912017-12-13 16:38:56 -0800504 int version;
William Tu1baf5eb2018-04-27 14:16:32 -0700505 int nhoff;
William Tud5db21a2018-05-11 05:49:47 -0700506 int thoff;
William Tu1a66a832017-08-25 09:21:28 -0700507
508 tun_info = skb_tunnel_info(skb);
509 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
510 ip_tunnel_info_af(tun_info) != AF_INET))
511 goto err_free_skb;
512
513 key = &tun_info->key;
Pieter Jansen van Vuuren256c87c2018-06-26 21:39:36 -0700514 if (!(tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT))
wenxu962924f2019-01-22 18:39:52 +0800515 goto err_free_skb;
Xin Long2eb8d6d2019-10-28 23:19:35 +0800516 if (tun_info->options_len < sizeof(*md))
wenxu962924f2019-01-22 18:39:52 +0800517 goto err_free_skb;
Xin Long2eb8d6d2019-10-28 23:19:35 +0800518 md = ip_tunnel_info_opts(tun_info);
William Tu1a66a832017-08-25 09:21:28 -0700519
520 /* ERSPAN has fixed 8 byte GRE header */
William Tuf551c912017-12-13 16:38:56 -0800521 version = md->version;
522 tunnel_hlen = 8 + erspan_hdr_len(version);
William Tu1a66a832017-08-25 09:21:28 -0700523
wenxu962924f2019-01-22 18:39:52 +0800524 if (skb_cow_head(skb, dev->needed_headroom))
525 goto err_free_skb;
William Tu1a66a832017-08-25 09:21:28 -0700526
527 if (gre_handle_offloads(skb, false))
wenxu962924f2019-01-22 18:39:52 +0800528 goto err_free_skb;
William Tu1a66a832017-08-25 09:21:28 -0700529
William Tuf1929702017-10-05 12:07:12 -0700530 if (skb->len > dev->mtu + dev->hard_header_len) {
531 pskb_trim(skb, dev->mtu + dev->hard_header_len);
William Tu1a66a832017-08-25 09:21:28 -0700532 truncate = true;
533 }
534
William Tu1baf5eb2018-04-27 14:16:32 -0700535 nhoff = skb_network_header(skb) - skb_mac_header(skb);
536 if (skb->protocol == htons(ETH_P_IP) &&
537 (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
538 truncate = true;
539
William Tud5db21a2018-05-11 05:49:47 -0700540 thoff = skb_transport_header(skb) - skb_mac_header(skb);
541 if (skb->protocol == htons(ETH_P_IPV6) &&
542 (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff))
543 truncate = true;
544
William Tuf551c912017-12-13 16:38:56 -0800545 if (version == 1) {
William Tuc69de582018-01-25 13:20:09 -0800546 erspan_build_header(skb, ntohl(tunnel_id_to_key32(key->tun_id)),
William Tuf551c912017-12-13 16:38:56 -0800547 ntohl(md->u.index), truncate, true);
Xin Long20704bd2019-01-14 18:10:06 +0800548 proto = htons(ETH_P_ERSPAN);
William Tuf551c912017-12-13 16:38:56 -0800549 } else if (version == 2) {
William Tuc69de582018-01-25 13:20:09 -0800550 erspan_build_header_v2(skb,
551 ntohl(tunnel_id_to_key32(key->tun_id)),
552 md->u.md2.dir,
553 get_hwid(&md->u.md2),
554 truncate, true);
Xin Long20704bd2019-01-14 18:10:06 +0800555 proto = htons(ETH_P_ERSPAN2);
William Tuf551c912017-12-13 16:38:56 -0800556 } else {
wenxu962924f2019-01-22 18:39:52 +0800557 goto err_free_skb;
William Tuf551c912017-12-13 16:38:56 -0800558 }
William Tu1a66a832017-08-25 09:21:28 -0700559
560 gre_build_header(skb, 8, TUNNEL_SEQ,
Xin Long20704bd2019-01-14 18:10:06 +0800561 proto, 0, htonl(tunnel->o_seqno++));
William Tu1a66a832017-08-25 09:21:28 -0700562
wenxu962924f2019-01-22 18:39:52 +0800563 ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen);
William Tu1a66a832017-08-25 09:21:28 -0700564
William Tu1a66a832017-08-25 09:21:28 -0700565 return;
566
William Tu1a66a832017-08-25 09:21:28 -0700567err_free_skb:
568 kfree_skb(skb);
569 dev->stats.tx_dropped++;
570}
571
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700572static int gre_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
573{
574 struct ip_tunnel_info *info = skb_tunnel_info(skb);
wenxu962924f2019-01-22 18:39:52 +0800575 const struct ip_tunnel_key *key;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700576 struct rtable *rt;
577 struct flowi4 fl4;
578
579 if (ip_tunnel_info_af(info) != AF_INET)
580 return -EINVAL;
581
wenxu962924f2019-01-22 18:39:52 +0800582 key = &info->key;
583 ip_tunnel_init_flow(&fl4, IPPROTO_GRE, key->u.ipv4.dst, key->u.ipv4.src,
584 tunnel_id_to_key32(key->tun_id), key->tos, 0,
wenxu24ba1442019-02-24 11:36:20 +0800585 skb->mark, skb_get_hash(skb));
wenxu962924f2019-01-22 18:39:52 +0800586 rt = ip_route_output_key(dev_net(dev), &fl4);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700587 if (IS_ERR(rt))
588 return PTR_ERR(rt);
589
590 ip_rt_put(rt);
591 info->key.u.ipv4.src = fl4.saddr;
592 return 0;
593}
594
Pravin B Shelarc5441932013-03-25 14:49:35 +0000595static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
596 struct net_device *dev)
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800597{
Pravin B Shelarc5441932013-03-25 14:49:35 +0000598 struct ip_tunnel *tunnel = netdev_priv(dev);
599 const struct iphdr *tnl_params;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800600
Willem de Bruijncb9f1b72018-12-30 17:24:36 -0500601 if (!pskb_inet_may_pull(skb))
602 goto free_skb;
603
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700604 if (tunnel->collect_md) {
Jiri Benc20907142016-04-27 11:29:07 +0200605 gre_fb_xmit(skb, dev, skb->protocol);
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700606 return NETDEV_TX_OK;
607 }
608
Pravin B Shelarc5441932013-03-25 14:49:35 +0000609 if (dev->header_ops) {
610 /* Need space for new headers */
611 if (skb_cow_head(skb, dev->needed_headroom -
Chen Gang2bac7cb2013-04-22 20:45:42 +0000612 (tunnel->hlen + sizeof(struct iphdr))))
Pravin B Shelarc5441932013-03-25 14:49:35 +0000613 goto free_skb;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800614
Pravin B Shelarc5441932013-03-25 14:49:35 +0000615 tnl_params = (const struct iphdr *)skb->data;
Eric Dumazete985aad2010-09-27 03:57:11 +0000616
Pravin B Shelarc5441932013-03-25 14:49:35 +0000617 /* Pull skb since ip_tunnel_xmit() needs skb->data pointing
618 * to gre header.
619 */
620 skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
Timo Teräs8a0033a2014-12-15 09:24:13 +0200621 skb_reset_mac_header(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000622 } else {
623 if (skb_cow_head(skb, dev->needed_headroom))
624 goto free_skb;
Herbert Xue1a80002008-10-09 12:00:17 -0700625
Pravin B Shelarc5441932013-03-25 14:49:35 +0000626 tnl_params = &tunnel->parms.iph;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800627 }
628
Alexander Duyckaed069d2016-04-14 15:33:37 -0400629 if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
630 goto free_skb;
Timo Teräs8a0033a2014-12-15 09:24:13 +0200631
Pravin B Shelarc5441932013-03-25 14:49:35 +0000632 __gre_xmit(skb, dev, tnl_params, skb->protocol);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000633 return NETDEV_TX_OK;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800634
Pravin B Shelarc5441932013-03-25 14:49:35 +0000635free_skb:
Eric Dumazet3acfa1e2014-01-18 18:27:49 -0800636 kfree_skb(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000637 dev->stats.tx_dropped++;
638 return NETDEV_TX_OK;
Michal Schmidtee34c1e2007-12-13 09:46:32 -0800639}
640
William Tu84e54fe2017-08-22 09:40:28 -0700641static netdev_tx_t erspan_xmit(struct sk_buff *skb,
642 struct net_device *dev)
643{
644 struct ip_tunnel *tunnel = netdev_priv(dev);
645 bool truncate = false;
Xin Long20704bd2019-01-14 18:10:06 +0800646 __be16 proto;
William Tu84e54fe2017-08-22 09:40:28 -0700647
Willem de Bruijncb9f1b72018-12-30 17:24:36 -0500648 if (!pskb_inet_may_pull(skb))
649 goto free_skb;
650
William Tu1a66a832017-08-25 09:21:28 -0700651 if (tunnel->collect_md) {
Xin Long20704bd2019-01-14 18:10:06 +0800652 erspan_fb_xmit(skb, dev);
William Tu1a66a832017-08-25 09:21:28 -0700653 return NETDEV_TX_OK;
654 }
655
William Tu84e54fe2017-08-22 09:40:28 -0700656 if (gre_handle_offloads(skb, false))
657 goto free_skb;
658
659 if (skb_cow_head(skb, dev->needed_headroom))
660 goto free_skb;
661
William Tuf1929702017-10-05 12:07:12 -0700662 if (skb->len > dev->mtu + dev->hard_header_len) {
663 pskb_trim(skb, dev->mtu + dev->hard_header_len);
William Tu84e54fe2017-08-22 09:40:28 -0700664 truncate = true;
665 }
666
667 /* Push ERSPAN header */
Xin Long20704bd2019-01-14 18:10:06 +0800668 if (tunnel->erspan_ver == 1) {
William Tuc69de582018-01-25 13:20:09 -0800669 erspan_build_header(skb, ntohl(tunnel->parms.o_key),
670 tunnel->index,
William Tuf551c912017-12-13 16:38:56 -0800671 truncate, true);
Xin Long20704bd2019-01-14 18:10:06 +0800672 proto = htons(ETH_P_ERSPAN);
673 } else if (tunnel->erspan_ver == 2) {
William Tuc69de582018-01-25 13:20:09 -0800674 erspan_build_header_v2(skb, ntohl(tunnel->parms.o_key),
William Tuf551c912017-12-13 16:38:56 -0800675 tunnel->dir, tunnel->hwid,
676 truncate, true);
Xin Long20704bd2019-01-14 18:10:06 +0800677 proto = htons(ETH_P_ERSPAN2);
678 } else {
William Tu02f99df2018-05-16 17:24:32 -0700679 goto free_skb;
Xin Long20704bd2019-01-14 18:10:06 +0800680 }
William Tuf551c912017-12-13 16:38:56 -0800681
William Tu84e54fe2017-08-22 09:40:28 -0700682 tunnel->parms.o_flags &= ~TUNNEL_KEY;
Xin Long20704bd2019-01-14 18:10:06 +0800683 __gre_xmit(skb, dev, &tunnel->parms.iph, proto);
William Tu84e54fe2017-08-22 09:40:28 -0700684 return NETDEV_TX_OK;
685
686free_skb:
687 kfree_skb(skb);
688 dev->stats.tx_dropped++;
689 return NETDEV_TX_OK;
690}
691
Pravin B Shelarc5441932013-03-25 14:49:35 +0000692static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
693 struct net_device *dev)
694{
695 struct ip_tunnel *tunnel = netdev_priv(dev);
696
Willem de Bruijncb9f1b72018-12-30 17:24:36 -0500697 if (!pskb_inet_may_pull(skb))
698 goto free_skb;
699
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700700 if (tunnel->collect_md) {
Jiri Benc20907142016-04-27 11:29:07 +0200701 gre_fb_xmit(skb, dev, htons(ETH_P_TEB));
Pravin B Shelar2e15ea32015-08-07 23:51:42 -0700702 return NETDEV_TX_OK;
703 }
704
Alexander Duyckaed069d2016-04-14 15:33:37 -0400705 if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM)))
706 goto free_skb;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000707
708 if (skb_cow_head(skb, dev->needed_headroom))
709 goto free_skb;
710
711 __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB));
Pravin B Shelarc5441932013-03-25 14:49:35 +0000712 return NETDEV_TX_OK;
713
714free_skb:
Eric Dumazet3acfa1e2014-01-18 18:27:49 -0800715 kfree_skb(skb);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000716 dev->stats.tx_dropped++;
717 return NETDEV_TX_OK;
718}
719
Xin Longdd9d5982017-11-07 16:33:08 +0800720static void ipgre_link_update(struct net_device *dev, bool set_mtu)
721{
722 struct ip_tunnel *tunnel = netdev_priv(dev);
723 int len;
724
725 len = tunnel->tun_hlen;
726 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
727 len = tunnel->tun_hlen - len;
728 tunnel->hlen = tunnel->hlen + len;
729
730 dev->needed_headroom = dev->needed_headroom + len;
731 if (set_mtu)
732 dev->mtu = max_t(int, dev->mtu - len, 68);
733
734 if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
735 if (!(tunnel->parms.o_flags & TUNNEL_CSUM) ||
736 tunnel->encap.type == TUNNEL_ENCAP_NONE) {
737 dev->features |= NETIF_F_GSO_SOFTWARE;
738 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
Sabrina Dubroca1cc59542018-04-10 12:57:18 +0200739 } else {
740 dev->features &= ~NETIF_F_GSO_SOFTWARE;
741 dev->hw_features &= ~NETIF_F_GSO_SOFTWARE;
Xin Longdd9d5982017-11-07 16:33:08 +0800742 }
743 dev->features |= NETIF_F_LLTX;
Sabrina Dubroca1cc59542018-04-10 12:57:18 +0200744 } else {
745 dev->hw_features &= ~NETIF_F_GSO_SOFTWARE;
746 dev->features &= ~(NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE);
Xin Longdd9d5982017-11-07 16:33:08 +0800747 }
748}
749
Pravin B Shelarc5441932013-03-25 14:49:35 +0000750static int ipgre_tunnel_ioctl(struct net_device *dev,
751 struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct ip_tunnel_parm p;
Xin Longa0efab62017-11-07 16:33:09 +0800754 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Pravin B Shelarc5441932013-03-25 14:49:35 +0000756 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
757 return -EFAULT;
Xin Longa0efab62017-11-07 16:33:09 +0800758
Cong Wang6c734fb2013-06-29 12:02:59 +0800759 if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
760 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE ||
Xin Longa0efab62017-11-07 16:33:09 +0800761 p.iph.ihl != 5 || (p.iph.frag_off & htons(~IP_DF)) ||
762 ((p.i_flags | p.o_flags) & (GRE_VERSION | GRE_ROUTING)))
Cong Wang6c734fb2013-06-29 12:02:59 +0800763 return -EINVAL;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000764 }
Xin Longa0efab62017-11-07 16:33:09 +0800765
Pravin B Shelarc5441932013-03-25 14:49:35 +0000766 p.i_flags = gre_flags_to_tnl_flags(p.i_flags);
767 p.o_flags = gre_flags_to_tnl_flags(p.o_flags);
768
769 err = ip_tunnel_ioctl(dev, &p, cmd);
770 if (err)
771 return err;
772
Xin Longa0efab62017-11-07 16:33:09 +0800773 if (cmd == SIOCCHGTUNNEL) {
774 struct ip_tunnel *t = netdev_priv(dev);
775
776 t->parms.i_flags = p.i_flags;
777 t->parms.o_flags = p.o_flags;
778
779 if (strcmp(dev->rtnl_link_ops->kind, "erspan"))
780 ipgre_link_update(dev, true);
781 }
782
Tom Herbert95f5c642016-04-29 17:12:16 -0700783 p.i_flags = gre_tnl_flags_to_gre_flags(p.i_flags);
784 p.o_flags = gre_tnl_flags_to_gre_flags(p.o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000785
786 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
787 return -EFAULT;
Xin Longa0efab62017-11-07 16:33:09 +0800788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return 0;
790}
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792/* Nice toy. Unfortunately, useless in real life :-)
793 It allows to construct virtual multiprotocol broadcast "LAN"
794 over the Internet, provided multicast routing is tuned.
795
796
797 I have no idea was this bicycle invented before me,
798 so that I had to set ARPHRD_IPGRE to a random value.
799 I have an impression, that Cisco could make something similar,
800 but this feature is apparently missing in IOS<=11.2(8).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
803 with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
804
805 ping -t 255 224.66.66.66
806
807 If nobody answers, mbone does not work.
808
809 ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
810 ip addr add 10.66.66.<somewhat>/24 dev Universe
811 ifconfig Universe up
812 ifconfig Universe add fe80::<Your_real_addr>/10
813 ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
814 ftp 10.66.66.66
815 ...
816 ftp fec0:6666:6666::193.233.7.65
817 ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 */
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700819static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
820 unsigned short type,
Eric Dumazet15078502010-09-15 11:07:53 +0000821 const void *daddr, const void *saddr, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Patrick McHardy2941a482006-01-08 22:05:26 -0800823 struct ip_tunnel *t = netdev_priv(dev);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000824 struct iphdr *iph;
825 struct gre_base_hdr *greh;
826
Johannes Bergd58ff352017-06-16 14:29:23 +0200827 iph = skb_push(skb, t->hlen + sizeof(*iph));
Pravin B Shelarc5441932013-03-25 14:49:35 +0000828 greh = (struct gre_base_hdr *)(iph+1);
Tom Herbert95f5c642016-04-29 17:12:16 -0700829 greh->flags = gre_tnl_flags_to_gre_flags(t->parms.o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000830 greh->protocol = htons(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Pravin B Shelarc5441932013-03-25 14:49:35 +0000834 /* Set the source hardware address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (saddr)
836 memcpy(&iph->saddr, saddr, 4);
Timo Teräs6d55cb92010-03-03 04:01:13 +0000837 if (daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 memcpy(&iph->daddr, daddr, 4);
Timo Teräs6d55cb92010-03-03 04:01:13 +0000839 if (iph->daddr)
Timo Teräs77a482b2013-08-06 13:45:43 +0300840 return t->hlen + sizeof(*iph);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900841
Pravin B Shelarc5441932013-03-25 14:49:35 +0000842 return -(t->hlen + sizeof(*iph));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Timo Teras6a5f44d2007-10-23 20:31:53 -0700845static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
846{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000847 const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb);
Timo Teras6a5f44d2007-10-23 20:31:53 -0700848 memcpy(haddr, &iph->saddr, 4);
849 return 4;
850}
851
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700852static const struct header_ops ipgre_header_ops = {
853 .create = ipgre_header,
Timo Teras6a5f44d2007-10-23 20:31:53 -0700854 .parse = ipgre_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700855};
856
Timo Teras6a5f44d2007-10-23 20:31:53 -0700857#ifdef CONFIG_NET_IPGRE_BROADCAST
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858static int ipgre_open(struct net_device *dev)
859{
Patrick McHardy2941a482006-01-08 22:05:26 -0800860 struct ip_tunnel *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Joe Perchesf97c1e02007-12-16 13:45:43 -0800862 if (ipv4_is_multicast(t->parms.iph.daddr)) {
David S. Millercbb1e852011-05-04 12:33:34 -0700863 struct flowi4 fl4;
864 struct rtable *rt;
Eric Dumazete985aad2010-09-27 03:57:11 +0000865
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200866 rt = ip_route_output_gre(t->net, &fl4,
David S. Millercbb1e852011-05-04 12:33:34 -0700867 t->parms.iph.daddr,
868 t->parms.iph.saddr,
869 t->parms.o_key,
870 RT_TOS(t->parms.iph.tos),
871 t->parms.link);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800872 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return -EADDRNOTAVAIL;
Changli Gaod8d1f302010-06-10 23:31:35 -0700874 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 ip_rt_put(rt);
Ian Morris51456b22015-04-03 09:17:26 +0100876 if (!__in_dev_get_rtnl(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return -EADDRNOTAVAIL;
878 t->mlink = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -0700879 ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881 return 0;
882}
883
884static int ipgre_close(struct net_device *dev)
885{
Patrick McHardy2941a482006-01-08 22:05:26 -0800886 struct ip_tunnel *t = netdev_priv(dev);
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800887
Joe Perchesf97c1e02007-12-16 13:45:43 -0800888 if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
Denis V. Lunev7fee0ca2008-01-21 17:32:38 -0800889 struct in_device *in_dev;
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200890 in_dev = inetdev_by_index(t->net, t->mlink);
Eric Dumazet8723e1b2010-10-19 00:39:26 +0000891 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 ip_mc_dec_group(in_dev, t->parms.iph.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894 return 0;
895}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896#endif
897
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800898static const struct net_device_ops ipgre_netdev_ops = {
899 .ndo_init = ipgre_tunnel_init,
Pravin B Shelarc5441932013-03-25 14:49:35 +0000900 .ndo_uninit = ip_tunnel_uninit,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800901#ifdef CONFIG_NET_IPGRE_BROADCAST
902 .ndo_open = ipgre_open,
903 .ndo_stop = ipgre_close,
904#endif
Pravin B Shelarc5441932013-03-25 14:49:35 +0000905 .ndo_start_xmit = ipgre_xmit,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800906 .ndo_do_ioctl = ipgre_tunnel_ioctl,
Pravin B Shelarc5441932013-03-25 14:49:35 +0000907 .ndo_change_mtu = ip_tunnel_change_mtu,
908 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtel1e995842015-04-02 17:07:02 +0200909 .ndo_get_iflink = ip_tunnel_get_iflink,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800910};
911
Eric Dumazet6b78f162012-09-13 21:25:33 +0000912#define GRE_FEATURES (NETIF_F_SG | \
913 NETIF_F_FRAGLIST | \
914 NETIF_F_HIGHDMA | \
915 NETIF_F_HW_CSUM)
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917static void ipgre_tunnel_setup(struct net_device *dev)
918{
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -0800919 dev->netdev_ops = &ipgre_netdev_ops;
Nicolas Dichtel5a455272014-04-11 15:51:18 +0200920 dev->type = ARPHRD_IPGRE;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000921 ip_tunnel_setup(dev, ipgre_net_id);
922}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Pravin B Shelarc5441932013-03-25 14:49:35 +0000924static void __gre_tunnel_init(struct net_device *dev)
925{
926 struct ip_tunnel *tunnel;
927
928 tunnel = netdev_priv(dev);
Tom Herbert95f5c642016-04-29 17:12:16 -0700929 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000930 tunnel->parms.iph.protocol = IPPROTO_GRE;
931
Tom Herbert4565e992014-09-17 12:26:01 -0700932 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
933
Nicolas Dichtelb57708a2014-04-22 10:15:23 +0200934 dev->features |= GRE_FEATURES;
Eric Dumazet6b78f162012-09-13 21:25:33 +0000935 dev->hw_features |= GRE_FEATURES;
Pravin B Shelarc5441932013-03-25 14:49:35 +0000936
937 if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) {
Alexander Duycka0ca1532016-04-05 09:13:39 -0700938 /* TCP offload with GRE SEQ is not supported, nor
939 * can we support 2 levels of outer headers requiring
940 * an update.
941 */
942 if (!(tunnel->parms.o_flags & TUNNEL_CSUM) ||
943 (tunnel->encap.type == TUNNEL_ENCAP_NONE)) {
944 dev->features |= NETIF_F_GSO_SOFTWARE;
945 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
946 }
947
Pravin B Shelarc5441932013-03-25 14:49:35 +0000948 /* Can use a lockless transmit, unless we generate
949 * output sequences
950 */
951 dev->features |= NETIF_F_LLTX;
952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955static int ipgre_tunnel_init(struct net_device *dev)
956{
Pravin B Shelarc5441932013-03-25 14:49:35 +0000957 struct ip_tunnel *tunnel = netdev_priv(dev);
958 struct iphdr *iph = &tunnel->parms.iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Pravin B Shelarc5441932013-03-25 14:49:35 +0000960 __gre_tunnel_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Pravin B Shelarc5441932013-03-25 14:49:35 +0000962 memcpy(dev->dev_addr, &iph->saddr, 4);
963 memcpy(dev->broadcast, &iph->daddr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Pravin B Shelarc5441932013-03-25 14:49:35 +0000965 dev->flags = IFF_NOARP;
Eric Dumazet02875872014-10-05 18:38:35 -0700966 netif_keep_dst(dev);
Pravin B Shelarc5441932013-03-25 14:49:35 +0000967 dev->addr_len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Jiri Benca64b04d2016-04-27 11:29:06 +0200969 if (iph->daddr && !tunnel->collect_md) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970#ifdef CONFIG_NET_IPGRE_BROADCAST
Joe Perchesf97c1e02007-12-16 13:45:43 -0800971 if (ipv4_is_multicast(iph->daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (!iph->saddr)
973 return -EINVAL;
974 dev->flags = IFF_BROADCAST;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700975 dev->header_ops = &ipgre_header_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977#endif
Jiri Benca64b04d2016-04-27 11:29:06 +0200978 } else if (!tunnel->collect_md) {
Timo Teras6a5f44d2007-10-23 20:31:53 -0700979 dev->header_ops = &ipgre_header_ops;
Jiri Benca64b04d2016-04-27 11:29:06 +0200980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Pravin B Shelarc5441932013-03-25 14:49:35 +0000982 return ip_tunnel_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Pravin B Shelar9f57c672015-08-07 23:51:52 -0700985static const struct gre_protocol ipgre_protocol = {
986 .handler = gre_rcv,
987 .err_handler = gre_err,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988};
989
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000990static int __net_init ipgre_init_net(struct net *net)
Pavel Emelyanov59a4c752008-04-16 01:08:53 -0700991{
Pravin B Shelarc5441932013-03-25 14:49:35 +0000992 return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -0700993}
994
Eric Dumazet64bc1782017-09-19 16:27:09 -0700995static void __net_exit ipgre_exit_batch_net(struct list_head *list_net)
Pavel Emelyanov59a4c752008-04-16 01:08:53 -0700996{
Eric Dumazet64bc1782017-09-19 16:27:09 -0700997 ip_tunnel_delete_nets(list_net, ipgre_net_id, &ipgre_link_ops);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -0700998}
999
1000static struct pernet_operations ipgre_net_ops = {
1001 .init = ipgre_init_net,
Eric Dumazet64bc1782017-09-19 16:27:09 -07001002 .exit_batch = ipgre_exit_batch_net,
Eric W. Biedermancfb8fbf2009-11-29 15:46:13 +00001003 .id = &ipgre_net_id,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001004 .size = sizeof(struct ip_tunnel_net),
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001005};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001007static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1008 struct netlink_ext_ack *extack)
Herbert Xuc19e6542008-10-09 11:59:55 -07001009{
1010 __be16 flags;
1011
1012 if (!data)
1013 return 0;
1014
1015 flags = 0;
1016 if (data[IFLA_GRE_IFLAGS])
1017 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1018 if (data[IFLA_GRE_OFLAGS])
1019 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1020 if (flags & (GRE_VERSION|GRE_ROUTING))
1021 return -EINVAL;
1022
Jiri Benc946b6362016-04-27 14:08:01 +02001023 if (data[IFLA_GRE_COLLECT_METADATA] &&
1024 data[IFLA_GRE_ENCAP_TYPE] &&
1025 nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE)
1026 return -EINVAL;
1027
Herbert Xuc19e6542008-10-09 11:59:55 -07001028 return 0;
1029}
1030
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001031static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1032 struct netlink_ext_ack *extack)
Herbert Xue1a80002008-10-09 12:00:17 -07001033{
1034 __be32 daddr;
1035
1036 if (tb[IFLA_ADDRESS]) {
1037 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1038 return -EINVAL;
1039 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1040 return -EADDRNOTAVAIL;
1041 }
1042
1043 if (!data)
1044 goto out;
1045
1046 if (data[IFLA_GRE_REMOTE]) {
1047 memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4);
1048 if (!daddr)
1049 return -EINVAL;
1050 }
1051
1052out:
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001053 return ipgre_tunnel_validate(tb, data, extack);
Herbert Xue1a80002008-10-09 12:00:17 -07001054}
1055
William Tu84e54fe2017-08-22 09:40:28 -07001056static int erspan_validate(struct nlattr *tb[], struct nlattr *data[],
1057 struct netlink_ext_ack *extack)
1058{
1059 __be16 flags = 0;
1060 int ret;
1061
1062 if (!data)
1063 return 0;
1064
1065 ret = ipgre_tap_validate(tb, data, extack);
1066 if (ret)
1067 return ret;
1068
1069 /* ERSPAN should only have GRE sequence and key flag */
William Tu1a66a832017-08-25 09:21:28 -07001070 if (data[IFLA_GRE_OFLAGS])
1071 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1072 if (data[IFLA_GRE_IFLAGS])
1073 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1074 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1075 flags != (GRE_SEQ | GRE_KEY))
William Tu84e54fe2017-08-22 09:40:28 -07001076 return -EINVAL;
1077
1078 /* ERSPAN Session ID only has 10-bit. Since we reuse
1079 * 32-bit key field as ID, check it's range.
1080 */
1081 if (data[IFLA_GRE_IKEY] &&
1082 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1083 return -EINVAL;
1084
1085 if (data[IFLA_GRE_OKEY] &&
1086 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1087 return -EINVAL;
1088
1089 return 0;
1090}
1091
Philip Prindeville22a59be2016-06-14 15:53:02 -06001092static int ipgre_netlink_parms(struct net_device *dev,
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001093 struct nlattr *data[],
1094 struct nlattr *tb[],
Craig Gallek9830ad42017-04-19 12:30:54 -04001095 struct ip_tunnel_parm *parms,
1096 __u32 *fwmark)
Herbert Xuc19e6542008-10-09 11:59:55 -07001097{
Philip Prindeville22a59be2016-06-14 15:53:02 -06001098 struct ip_tunnel *t = netdev_priv(dev);
1099
Herbert Xu7bb82d92008-10-11 12:20:15 -07001100 memset(parms, 0, sizeof(*parms));
Herbert Xuc19e6542008-10-09 11:59:55 -07001101
1102 parms->iph.protocol = IPPROTO_GRE;
1103
1104 if (!data)
Philip Prindeville22a59be2016-06-14 15:53:02 -06001105 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001106
1107 if (data[IFLA_GRE_LINK])
1108 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1109
1110 if (data[IFLA_GRE_IFLAGS])
Pravin B Shelarc5441932013-03-25 14:49:35 +00001111 parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS]));
Herbert Xuc19e6542008-10-09 11:59:55 -07001112
1113 if (data[IFLA_GRE_OFLAGS])
Pravin B Shelarc5441932013-03-25 14:49:35 +00001114 parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS]));
Herbert Xuc19e6542008-10-09 11:59:55 -07001115
1116 if (data[IFLA_GRE_IKEY])
1117 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1118
1119 if (data[IFLA_GRE_OKEY])
1120 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1121
1122 if (data[IFLA_GRE_LOCAL])
Jiri Benc67b61f62015-03-29 16:59:26 +02001123 parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]);
Herbert Xuc19e6542008-10-09 11:59:55 -07001124
1125 if (data[IFLA_GRE_REMOTE])
Jiri Benc67b61f62015-03-29 16:59:26 +02001126 parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]);
Herbert Xuc19e6542008-10-09 11:59:55 -07001127
1128 if (data[IFLA_GRE_TTL])
1129 parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
1130
1131 if (data[IFLA_GRE_TOS])
1132 parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]);
1133
Philip Prindeville22a59be2016-06-14 15:53:02 -06001134 if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC])) {
1135 if (t->ignore_df)
1136 return -EINVAL;
Herbert Xuc19e6542008-10-09 11:59:55 -07001137 parms->iph.frag_off = htons(IP_DF);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001138 }
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001139
1140 if (data[IFLA_GRE_COLLECT_METADATA]) {
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001141 t->collect_md = true;
Jiri Bence271c7b2016-05-11 15:53:57 +02001142 if (dev->type == ARPHRD_IPGRE)
1143 dev->type = ARPHRD_NONE;
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001144 }
Philip Prindeville22a59be2016-06-14 15:53:02 -06001145
1146 if (data[IFLA_GRE_IGNORE_DF]) {
1147 if (nla_get_u8(data[IFLA_GRE_IGNORE_DF])
1148 && (parms->iph.frag_off & htons(IP_DF)))
1149 return -EINVAL;
1150 t->ignore_df = !!nla_get_u8(data[IFLA_GRE_IGNORE_DF]);
1151 }
1152
Craig Gallek9830ad42017-04-19 12:30:54 -04001153 if (data[IFLA_GRE_FWMARK])
1154 *fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
1155
Petr Machatae1f8f782020-03-13 13:39:36 +02001156 return 0;
1157}
1158
1159static int erspan_netlink_parms(struct net_device *dev,
1160 struct nlattr *data[],
1161 struct nlattr *tb[],
1162 struct ip_tunnel_parm *parms,
1163 __u32 *fwmark)
1164{
1165 struct ip_tunnel *t = netdev_priv(dev);
1166 int err;
1167
1168 err = ipgre_netlink_parms(dev, data, tb, parms, fwmark);
1169 if (err)
1170 return err;
Petr Machata32ca98f2020-03-16 19:53:00 +02001171 if (!data)
1172 return 0;
Petr Machatae1f8f782020-03-13 13:39:36 +02001173
William Tuf551c912017-12-13 16:38:56 -08001174 if (data[IFLA_GRE_ERSPAN_VER]) {
1175 t->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
William Tu84e54fe2017-08-22 09:40:28 -07001176
William Tuf551c912017-12-13 16:38:56 -08001177 if (t->erspan_ver != 1 && t->erspan_ver != 2)
William Tu84e54fe2017-08-22 09:40:28 -07001178 return -EINVAL;
1179 }
1180
William Tuf551c912017-12-13 16:38:56 -08001181 if (t->erspan_ver == 1) {
1182 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1183 t->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1184 if (t->index & ~INDEX_MASK)
1185 return -EINVAL;
1186 }
1187 } else if (t->erspan_ver == 2) {
1188 if (data[IFLA_GRE_ERSPAN_DIR]) {
1189 t->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1190 if (t->dir & ~(DIR_MASK >> DIR_OFFSET))
1191 return -EINVAL;
1192 }
1193 if (data[IFLA_GRE_ERSPAN_HWID]) {
1194 t->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1195 if (t->hwid & ~(HWID_MASK >> HWID_OFFSET))
1196 return -EINVAL;
1197 }
1198 }
1199
Philip Prindeville22a59be2016-06-14 15:53:02 -06001200 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001201}
1202
Tom Herbert4565e992014-09-17 12:26:01 -07001203/* This function returns true when ENCAP attributes are present in the nl msg */
1204static bool ipgre_netlink_encap_parms(struct nlattr *data[],
1205 struct ip_tunnel_encap *ipencap)
1206{
1207 bool ret = false;
1208
1209 memset(ipencap, 0, sizeof(*ipencap));
1210
1211 if (!data)
1212 return ret;
1213
1214 if (data[IFLA_GRE_ENCAP_TYPE]) {
1215 ret = true;
1216 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1217 }
1218
1219 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1220 ret = true;
1221 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1222 }
1223
1224 if (data[IFLA_GRE_ENCAP_SPORT]) {
1225 ret = true;
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001226 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
Tom Herbert4565e992014-09-17 12:26:01 -07001227 }
1228
1229 if (data[IFLA_GRE_ENCAP_DPORT]) {
1230 ret = true;
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001231 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
Tom Herbert4565e992014-09-17 12:26:01 -07001232 }
1233
1234 return ret;
1235}
1236
Pravin B Shelarc5441932013-03-25 14:49:35 +00001237static int gre_tap_init(struct net_device *dev)
Herbert Xue1a80002008-10-09 12:00:17 -07001238{
Pravin B Shelarc5441932013-03-25 14:49:35 +00001239 __gre_tunnel_init(dev);
stephen hemmingerbec94d4302014-12-27 10:01:42 -08001240 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Longd51711c2017-09-28 13:23:31 +08001241 netif_keep_dst(dev);
Herbert Xue1a80002008-10-09 12:00:17 -07001242
Pravin B Shelarc5441932013-03-25 14:49:35 +00001243 return ip_tunnel_init(dev);
Herbert Xue1a80002008-10-09 12:00:17 -07001244}
1245
Pravin B Shelarc5441932013-03-25 14:49:35 +00001246static const struct net_device_ops gre_tap_netdev_ops = {
1247 .ndo_init = gre_tap_init,
1248 .ndo_uninit = ip_tunnel_uninit,
1249 .ndo_start_xmit = gre_tap_xmit,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -08001250 .ndo_set_mac_address = eth_mac_addr,
1251 .ndo_validate_addr = eth_validate_addr,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001252 .ndo_change_mtu = ip_tunnel_change_mtu,
1253 .ndo_get_stats64 = ip_tunnel_get_stats64,
Nicolas Dichtel1e995842015-04-02 17:07:02 +02001254 .ndo_get_iflink = ip_tunnel_get_iflink,
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07001255 .ndo_fill_metadata_dst = gre_fill_metadata_dst,
Stephen Hemmingerb8c26a32008-11-20 20:34:29 -08001256};
1257
William Tu84e54fe2017-08-22 09:40:28 -07001258static int erspan_tunnel_init(struct net_device *dev)
1259{
1260 struct ip_tunnel *tunnel = netdev_priv(dev);
William Tu84e54fe2017-08-22 09:40:28 -07001261
1262 tunnel->tun_hlen = 8;
1263 tunnel->parms.iph.protocol = IPPROTO_GRE;
Xin Longc122fda2017-10-01 22:00:55 +08001264 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
William Tuf551c912017-12-13 16:38:56 -08001265 erspan_hdr_len(tunnel->erspan_ver);
William Tu84e54fe2017-08-22 09:40:28 -07001266
William Tu84e54fe2017-08-22 09:40:28 -07001267 dev->features |= GRE_FEATURES;
1268 dev->hw_features |= GRE_FEATURES;
1269 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Xin Longc84bed42017-10-01 22:00:56 +08001270 netif_keep_dst(dev);
William Tu84e54fe2017-08-22 09:40:28 -07001271
1272 return ip_tunnel_init(dev);
1273}
1274
1275static const struct net_device_ops erspan_netdev_ops = {
1276 .ndo_init = erspan_tunnel_init,
1277 .ndo_uninit = ip_tunnel_uninit,
1278 .ndo_start_xmit = erspan_xmit,
1279 .ndo_set_mac_address = eth_mac_addr,
1280 .ndo_validate_addr = eth_validate_addr,
1281 .ndo_change_mtu = ip_tunnel_change_mtu,
1282 .ndo_get_stats64 = ip_tunnel_get_stats64,
1283 .ndo_get_iflink = ip_tunnel_get_iflink,
1284 .ndo_fill_metadata_dst = gre_fill_metadata_dst,
1285};
1286
Herbert Xue1a80002008-10-09 12:00:17 -07001287static void ipgre_tap_setup(struct net_device *dev)
1288{
Herbert Xue1a80002008-10-09 12:00:17 -07001289 ether_setup(dev);
Xin Longcfddd4c2017-12-18 14:24:35 +08001290 dev->max_mtu = 0;
Jiri Bencd13b1612016-02-17 15:32:53 +01001291 dev->netdev_ops = &gre_tap_netdev_ops;
1292 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1293 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Pravin B Shelarc5441932013-03-25 14:49:35 +00001294 ip_tunnel_setup(dev, gre_tap_net_id);
Herbert Xue1a80002008-10-09 12:00:17 -07001295}
1296
Petr Machatae1f8f782020-03-13 13:39:36 +02001297static int
1298ipgre_newlink_encap_setup(struct net_device *dev, struct nlattr *data[])
Herbert Xuc19e6542008-10-09 11:59:55 -07001299{
Tom Herbert4565e992014-09-17 12:26:01 -07001300 struct ip_tunnel_encap ipencap;
1301
1302 if (ipgre_netlink_encap_parms(data, &ipencap)) {
1303 struct ip_tunnel *t = netdev_priv(dev);
Petr Machatae1f8f782020-03-13 13:39:36 +02001304 int err = ip_tunnel_encap_setup(t, &ipencap);
Tom Herbert4565e992014-09-17 12:26:01 -07001305
1306 if (err < 0)
1307 return err;
1308 }
Herbert Xuc19e6542008-10-09 11:59:55 -07001309
Petr Machatae1f8f782020-03-13 13:39:36 +02001310 return 0;
1311}
1312
1313static int ipgre_newlink(struct net *src_net, struct net_device *dev,
1314 struct nlattr *tb[], struct nlattr *data[],
1315 struct netlink_ext_ack *extack)
1316{
1317 struct ip_tunnel_parm p;
1318 __u32 fwmark = 0;
1319 int err;
1320
1321 err = ipgre_newlink_encap_setup(dev, data);
1322 if (err)
1323 return err;
1324
Craig Gallek9830ad42017-04-19 12:30:54 -04001325 err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001326 if (err < 0)
1327 return err;
Craig Gallek9830ad42017-04-19 12:30:54 -04001328 return ip_tunnel_newlink(dev, tb, &p, fwmark);
Herbert Xuc19e6542008-10-09 11:59:55 -07001329}
1330
Petr Machatae1f8f782020-03-13 13:39:36 +02001331static int erspan_newlink(struct net *src_net, struct net_device *dev,
1332 struct nlattr *tb[], struct nlattr *data[],
1333 struct netlink_ext_ack *extack)
1334{
1335 struct ip_tunnel_parm p;
1336 __u32 fwmark = 0;
1337 int err;
1338
1339 err = ipgre_newlink_encap_setup(dev, data);
1340 if (err)
1341 return err;
1342
1343 err = erspan_netlink_parms(dev, data, tb, &p, &fwmark);
1344 if (err)
1345 return err;
1346 return ip_tunnel_newlink(dev, tb, &p, fwmark);
1347}
1348
Herbert Xuc19e6542008-10-09 11:59:55 -07001349static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001350 struct nlattr *data[],
1351 struct netlink_ext_ack *extack)
Herbert Xuc19e6542008-10-09 11:59:55 -07001352{
Craig Gallek9830ad42017-04-19 12:30:54 -04001353 struct ip_tunnel *t = netdev_priv(dev);
Craig Gallek9830ad42017-04-19 12:30:54 -04001354 __u32 fwmark = t->fwmark;
Xin Longdd9d5982017-11-07 16:33:08 +08001355 struct ip_tunnel_parm p;
Philip Prindeville22a59be2016-06-14 15:53:02 -06001356 int err;
Tom Herbert4565e992014-09-17 12:26:01 -07001357
Petr Machatae1f8f782020-03-13 13:39:36 +02001358 err = ipgre_newlink_encap_setup(dev, data);
1359 if (err)
1360 return err;
Herbert Xuc19e6542008-10-09 11:59:55 -07001361
Craig Gallek9830ad42017-04-19 12:30:54 -04001362 err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
Philip Prindeville22a59be2016-06-14 15:53:02 -06001363 if (err < 0)
1364 return err;
Xin Longdd9d5982017-11-07 16:33:08 +08001365
1366 err = ip_tunnel_changelink(dev, tb, &p, fwmark);
1367 if (err < 0)
1368 return err;
1369
1370 t->parms.i_flags = p.i_flags;
1371 t->parms.o_flags = p.o_flags;
1372
Petr Machatae1f8f782020-03-13 13:39:36 +02001373 ipgre_link_update(dev, !tb[IFLA_MTU]);
1374
1375 return 0;
1376}
1377
1378static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],
1379 struct nlattr *data[],
1380 struct netlink_ext_ack *extack)
1381{
1382 struct ip_tunnel *t = netdev_priv(dev);
1383 __u32 fwmark = t->fwmark;
1384 struct ip_tunnel_parm p;
1385 int err;
1386
1387 err = ipgre_newlink_encap_setup(dev, data);
1388 if (err)
1389 return err;
1390
1391 err = erspan_netlink_parms(dev, data, tb, &p, &fwmark);
1392 if (err < 0)
1393 return err;
1394
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;
Xin Longdd9d5982017-11-07 16:33:08 +08001401
1402 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001403}
1404
1405static size_t ipgre_get_size(const struct net_device *dev)
1406{
1407 return
1408 /* IFLA_GRE_LINK */
1409 nla_total_size(4) +
1410 /* IFLA_GRE_IFLAGS */
1411 nla_total_size(2) +
1412 /* IFLA_GRE_OFLAGS */
1413 nla_total_size(2) +
1414 /* IFLA_GRE_IKEY */
1415 nla_total_size(4) +
1416 /* IFLA_GRE_OKEY */
1417 nla_total_size(4) +
1418 /* IFLA_GRE_LOCAL */
1419 nla_total_size(4) +
1420 /* IFLA_GRE_REMOTE */
1421 nla_total_size(4) +
1422 /* IFLA_GRE_TTL */
1423 nla_total_size(1) +
1424 /* IFLA_GRE_TOS */
1425 nla_total_size(1) +
1426 /* IFLA_GRE_PMTUDISC */
1427 nla_total_size(1) +
Tom Herbert4565e992014-09-17 12:26:01 -07001428 /* IFLA_GRE_ENCAP_TYPE */
1429 nla_total_size(2) +
1430 /* IFLA_GRE_ENCAP_FLAGS */
1431 nla_total_size(2) +
1432 /* IFLA_GRE_ENCAP_SPORT */
1433 nla_total_size(2) +
1434 /* IFLA_GRE_ENCAP_DPORT */
1435 nla_total_size(2) +
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001436 /* IFLA_GRE_COLLECT_METADATA */
1437 nla_total_size(0) +
Philip Prindeville22a59be2016-06-14 15:53:02 -06001438 /* IFLA_GRE_IGNORE_DF */
1439 nla_total_size(1) +
Craig Gallek9830ad42017-04-19 12:30:54 -04001440 /* IFLA_GRE_FWMARK */
1441 nla_total_size(4) +
William Tu84e54fe2017-08-22 09:40:28 -07001442 /* IFLA_GRE_ERSPAN_INDEX */
1443 nla_total_size(4) +
William Tuf551c912017-12-13 16:38:56 -08001444 /* IFLA_GRE_ERSPAN_VER */
1445 nla_total_size(1) +
1446 /* IFLA_GRE_ERSPAN_DIR */
1447 nla_total_size(1) +
1448 /* IFLA_GRE_ERSPAN_HWID */
1449 nla_total_size(2) +
Herbert Xuc19e6542008-10-09 11:59:55 -07001450 0;
1451}
1452
1453static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1454{
1455 struct ip_tunnel *t = netdev_priv(dev);
1456 struct ip_tunnel_parm *p = &t->parms;
Lorenzo Bianconifeaf5c72019-01-28 22:23:48 +01001457 __be16 o_flags = p->o_flags;
1458
Lorenzo Bianconi2bdf7002019-02-19 17:42:05 +01001459 if (t->erspan_ver == 1 || t->erspan_ver == 2) {
1460 if (!t->collect_md)
1461 o_flags |= TUNNEL_KEY;
1462
1463 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, t->erspan_ver))
1464 goto nla_put_failure;
1465
1466 if (t->erspan_ver == 1) {
1467 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, t->index))
1468 goto nla_put_failure;
1469 } else {
1470 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, t->dir))
1471 goto nla_put_failure;
1472 if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, t->hwid))
1473 goto nla_put_failure;
1474 }
1475 }
Herbert Xuc19e6542008-10-09 11:59:55 -07001476
David S. Millerf3756b72012-04-01 20:39:02 -04001477 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
Tom Herbert95f5c642016-04-29 17:12:16 -07001478 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1479 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1480 nla_put_be16(skb, IFLA_GRE_OFLAGS,
Lorenzo Bianconifeaf5c72019-01-28 22:23:48 +01001481 gre_tnl_flags_to_gre_flags(o_flags)) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001482 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1483 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
Jiri Benc930345e2015-03-29 16:59:25 +02001484 nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
1485 nla_put_in_addr(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
David S. Millerf3756b72012-04-01 20:39:02 -04001486 nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
1487 nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
1488 nla_put_u8(skb, IFLA_GRE_PMTUDISC,
Craig Gallek9830ad42017-04-19 12:30:54 -04001489 !!(p->iph.frag_off & htons(IP_DF))) ||
1490 nla_put_u32(skb, IFLA_GRE_FWMARK, t->fwmark))
David S. Millerf3756b72012-04-01 20:39:02 -04001491 goto nla_put_failure;
Tom Herbert4565e992014-09-17 12:26:01 -07001492
1493 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1494 t->encap.type) ||
Sabrina Dubroca3e97fa72015-02-06 17:22:22 +01001495 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1496 t->encap.sport) ||
1497 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1498 t->encap.dport) ||
Tom Herbert4565e992014-09-17 12:26:01 -07001499 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
Tom Herberte1b2cb62014-11-05 16:49:38 -08001500 t->encap.flags))
Tom Herbert4565e992014-09-17 12:26:01 -07001501 goto nla_put_failure;
1502
Philip Prindeville22a59be2016-06-14 15:53:02 -06001503 if (nla_put_u8(skb, IFLA_GRE_IGNORE_DF, t->ignore_df))
1504 goto nla_put_failure;
1505
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001506 if (t->collect_md) {
1507 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
1508 goto nla_put_failure;
1509 }
1510
Herbert Xuc19e6542008-10-09 11:59:55 -07001511 return 0;
1512
1513nla_put_failure:
1514 return -EMSGSIZE;
1515}
1516
William Tu84e54fe2017-08-22 09:40:28 -07001517static void erspan_setup(struct net_device *dev)
1518{
Xin Long84581bd2018-08-27 18:41:32 +08001519 struct ip_tunnel *t = netdev_priv(dev);
1520
William Tu84e54fe2017-08-22 09:40:28 -07001521 ether_setup(dev);
Haishuang Yan0e141f72019-09-27 14:58:20 +08001522 dev->max_mtu = 0;
William Tu84e54fe2017-08-22 09:40:28 -07001523 dev->netdev_ops = &erspan_netdev_ops;
1524 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1525 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1526 ip_tunnel_setup(dev, erspan_net_id);
Xin Long84581bd2018-08-27 18:41:32 +08001527 t->erspan_ver = 1;
William Tu84e54fe2017-08-22 09:40:28 -07001528}
1529
Herbert Xuc19e6542008-10-09 11:59:55 -07001530static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
1531 [IFLA_GRE_LINK] = { .type = NLA_U32 },
1532 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
1533 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
1534 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
1535 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001536 [IFLA_GRE_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
1537 [IFLA_GRE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
Herbert Xuc19e6542008-10-09 11:59:55 -07001538 [IFLA_GRE_TTL] = { .type = NLA_U8 },
1539 [IFLA_GRE_TOS] = { .type = NLA_U8 },
1540 [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
Tom Herbert4565e992014-09-17 12:26:01 -07001541 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
1542 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
1543 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
1544 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001545 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
Philip Prindeville22a59be2016-06-14 15:53:02 -06001546 [IFLA_GRE_IGNORE_DF] = { .type = NLA_U8 },
Craig Gallek9830ad42017-04-19 12:30:54 -04001547 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
William Tu84e54fe2017-08-22 09:40:28 -07001548 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
William Tuf551c912017-12-13 16:38:56 -08001549 [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
1550 [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
1551 [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
Herbert Xuc19e6542008-10-09 11:59:55 -07001552};
1553
1554static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
1555 .kind = "gre",
1556 .maxtype = IFLA_GRE_MAX,
1557 .policy = ipgre_policy,
1558 .priv_size = sizeof(struct ip_tunnel),
1559 .setup = ipgre_tunnel_setup,
1560 .validate = ipgre_tunnel_validate,
1561 .newlink = ipgre_newlink,
1562 .changelink = ipgre_changelink,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001563 .dellink = ip_tunnel_dellink,
Herbert Xuc19e6542008-10-09 11:59:55 -07001564 .get_size = ipgre_get_size,
1565 .fill_info = ipgre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001566 .get_link_net = ip_tunnel_get_link_net,
Herbert Xuc19e6542008-10-09 11:59:55 -07001567};
1568
Herbert Xue1a80002008-10-09 12:00:17 -07001569static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
1570 .kind = "gretap",
1571 .maxtype = IFLA_GRE_MAX,
1572 .policy = ipgre_policy,
1573 .priv_size = sizeof(struct ip_tunnel),
1574 .setup = ipgre_tap_setup,
1575 .validate = ipgre_tap_validate,
1576 .newlink = ipgre_newlink,
1577 .changelink = ipgre_changelink,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001578 .dellink = ip_tunnel_dellink,
Herbert Xue1a80002008-10-09 12:00:17 -07001579 .get_size = ipgre_get_size,
1580 .fill_info = ipgre_fill_info,
Nicolas Dichtel1728d4f2015-01-15 15:11:17 +01001581 .get_link_net = ip_tunnel_get_link_net,
Herbert Xue1a80002008-10-09 12:00:17 -07001582};
1583
William Tu84e54fe2017-08-22 09:40:28 -07001584static struct rtnl_link_ops erspan_link_ops __read_mostly = {
1585 .kind = "erspan",
1586 .maxtype = IFLA_GRE_MAX,
1587 .policy = ipgre_policy,
1588 .priv_size = sizeof(struct ip_tunnel),
1589 .setup = erspan_setup,
1590 .validate = erspan_validate,
Petr Machatae1f8f782020-03-13 13:39:36 +02001591 .newlink = erspan_newlink,
1592 .changelink = erspan_changelink,
William Tu84e54fe2017-08-22 09:40:28 -07001593 .dellink = ip_tunnel_dellink,
1594 .get_size = ipgre_get_size,
1595 .fill_info = ipgre_fill_info,
1596 .get_link_net = ip_tunnel_get_link_net,
1597};
1598
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001599struct net_device *gretap_fb_dev_create(struct net *net, const char *name,
1600 u8 name_assign_type)
1601{
1602 struct nlattr *tb[IFLA_MAX + 1];
1603 struct net_device *dev;
Nicolas Dichtel106da662016-06-13 10:31:04 +02001604 LIST_HEAD(list_kill);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001605 struct ip_tunnel *t;
1606 int err;
1607
1608 memset(&tb, 0, sizeof(tb));
1609
1610 dev = rtnl_create_link(net, name, name_assign_type,
David Ahernd0522f12018-11-06 12:51:14 -08001611 &ipgre_tap_ops, tb, NULL);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001612 if (IS_ERR(dev))
1613 return dev;
1614
1615 /* Configure flow based GRE device. */
1616 t = netdev_priv(dev);
1617 t->collect_md = true;
1618
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001619 err = ipgre_newlink(net, dev, tb, NULL, NULL);
Nicolas Dichtel106da662016-06-13 10:31:04 +02001620 if (err < 0) {
1621 free_netdev(dev);
1622 return ERR_PTR(err);
1623 }
David Wragg7e059152016-02-10 00:05:58 +00001624
1625 /* openvswitch users expect packet sizes to be unrestricted,
1626 * so set the largest MTU we can.
1627 */
1628 err = __ip_tunnel_change_mtu(dev, IP_MAX_MTU, false);
1629 if (err)
1630 goto out;
1631
Nicolas Dichtelda6f1da2016-06-13 10:31:06 +02001632 err = rtnl_configure_link(dev, NULL);
1633 if (err < 0)
1634 goto out;
1635
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001636 return dev;
1637out:
Nicolas Dichtel106da662016-06-13 10:31:04 +02001638 ip_tunnel_dellink(dev, &list_kill);
1639 unregister_netdevice_many(&list_kill);
Pravin B Shelarb2acd1d2015-08-07 23:51:47 -07001640 return ERR_PTR(err);
1641}
1642EXPORT_SYMBOL_GPL(gretap_fb_dev_create);
1643
Pravin B Shelarc5441932013-03-25 14:49:35 +00001644static int __net_init ipgre_tap_init_net(struct net *net)
1645{
Pravin B Shelar2e15ea32015-08-07 23:51:42 -07001646 return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, "gretap0");
Pravin B Shelarc5441932013-03-25 14:49:35 +00001647}
1648
Eric Dumazet64bc1782017-09-19 16:27:09 -07001649static void __net_exit ipgre_tap_exit_batch_net(struct list_head *list_net)
Pravin B Shelarc5441932013-03-25 14:49:35 +00001650{
Eric Dumazet64bc1782017-09-19 16:27:09 -07001651 ip_tunnel_delete_nets(list_net, gre_tap_net_id, &ipgre_tap_ops);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001652}
1653
1654static struct pernet_operations ipgre_tap_net_ops = {
1655 .init = ipgre_tap_init_net,
Eric Dumazet64bc1782017-09-19 16:27:09 -07001656 .exit_batch = ipgre_tap_exit_batch_net,
Pravin B Shelarc5441932013-03-25 14:49:35 +00001657 .id = &gre_tap_net_id,
1658 .size = sizeof(struct ip_tunnel_net),
1659};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
William Tu84e54fe2017-08-22 09:40:28 -07001661static int __net_init erspan_init_net(struct net *net)
1662{
1663 return ip_tunnel_init_net(net, erspan_net_id,
1664 &erspan_link_ops, "erspan0");
1665}
1666
Eric Dumazet64bc1782017-09-19 16:27:09 -07001667static void __net_exit erspan_exit_batch_net(struct list_head *net_list)
William Tu84e54fe2017-08-22 09:40:28 -07001668{
Eric Dumazet64bc1782017-09-19 16:27:09 -07001669 ip_tunnel_delete_nets(net_list, erspan_net_id, &erspan_link_ops);
William Tu84e54fe2017-08-22 09:40:28 -07001670}
1671
1672static struct pernet_operations erspan_net_ops = {
1673 .init = erspan_init_net,
Eric Dumazet64bc1782017-09-19 16:27:09 -07001674 .exit_batch = erspan_exit_batch_net,
William Tu84e54fe2017-08-22 09:40:28 -07001675 .id = &erspan_net_id,
1676 .size = sizeof(struct ip_tunnel_net),
1677};
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679static int __init ipgre_init(void)
1680{
1681 int err;
1682
Joe Perches058bd4d2012-03-11 18:36:11 +00001683 pr_info("GRE over IPv4 tunneling driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Eric W. Biedermancfb8fbf2009-11-29 15:46:13 +00001685 err = register_pernet_device(&ipgre_net_ops);
Pavel Emelyanov59a4c752008-04-16 01:08:53 -07001686 if (err < 0)
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001687 return err;
1688
Pravin B Shelarc5441932013-03-25 14:49:35 +00001689 err = register_pernet_device(&ipgre_tap_net_ops);
1690 if (err < 0)
William Tue3d03282017-08-22 17:04:05 -07001691 goto pnet_tap_failed;
Pravin B Shelarc5441932013-03-25 14:49:35 +00001692
William Tu84e54fe2017-08-22 09:40:28 -07001693 err = register_pernet_device(&erspan_net_ops);
1694 if (err < 0)
1695 goto pnet_erspan_failed;
1696
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001697 err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001698 if (err < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001699 pr_info("%s: can't add protocol\n", __func__);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001700 goto add_proto_failed;
1701 }
Pavel Emelyanov7daa0002008-04-16 01:10:05 -07001702
Herbert Xuc19e6542008-10-09 11:59:55 -07001703 err = rtnl_link_register(&ipgre_link_ops);
1704 if (err < 0)
1705 goto rtnl_link_failed;
1706
Herbert Xue1a80002008-10-09 12:00:17 -07001707 err = rtnl_link_register(&ipgre_tap_ops);
1708 if (err < 0)
1709 goto tap_ops_failed;
1710
William Tu84e54fe2017-08-22 09:40:28 -07001711 err = rtnl_link_register(&erspan_link_ops);
1712 if (err < 0)
1713 goto erspan_link_failed;
1714
Pravin B Shelarc5441932013-03-25 14:49:35 +00001715 return 0;
Herbert Xuc19e6542008-10-09 11:59:55 -07001716
William Tu84e54fe2017-08-22 09:40:28 -07001717erspan_link_failed:
1718 rtnl_link_unregister(&ipgre_tap_ops);
Herbert Xue1a80002008-10-09 12:00:17 -07001719tap_ops_failed:
1720 rtnl_link_unregister(&ipgre_link_ops);
Herbert Xuc19e6542008-10-09 11:59:55 -07001721rtnl_link_failed:
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001722 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001723add_proto_failed:
William Tu84e54fe2017-08-22 09:40:28 -07001724 unregister_pernet_device(&erspan_net_ops);
1725pnet_erspan_failed:
Pravin B Shelarc5441932013-03-25 14:49:35 +00001726 unregister_pernet_device(&ipgre_tap_net_ops);
William Tue3d03282017-08-22 17:04:05 -07001727pnet_tap_failed:
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001728 unregister_pernet_device(&ipgre_net_ops);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001729 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
Alexey Kuznetsovdb445752005-07-30 17:46:44 -07001732static void __exit ipgre_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Herbert Xue1a80002008-10-09 12:00:17 -07001734 rtnl_link_unregister(&ipgre_tap_ops);
Herbert Xuc19e6542008-10-09 11:59:55 -07001735 rtnl_link_unregister(&ipgre_link_ops);
William Tu84e54fe2017-08-22 09:40:28 -07001736 rtnl_link_unregister(&erspan_link_ops);
Pravin B Shelar9f57c672015-08-07 23:51:52 -07001737 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
Pravin B Shelarc5441932013-03-25 14:49:35 +00001738 unregister_pernet_device(&ipgre_tap_net_ops);
Alexey Dobriyanc2892f02010-02-16 07:57:44 +00001739 unregister_pernet_device(&ipgre_net_ops);
William Tu84e54fe2017-08-22 09:40:28 -07001740 unregister_pernet_device(&erspan_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
1743module_init(ipgre_init);
1744module_exit(ipgre_fini);
1745MODULE_LICENSE("GPL");
Patrick McHardy4d74f8b2008-10-10 12:11:06 -07001746MODULE_ALIAS_RTNL_LINK("gre");
1747MODULE_ALIAS_RTNL_LINK("gretap");
William Tu84e54fe2017-08-22 09:40:28 -07001748MODULE_ALIAS_RTNL_LINK("erspan");
Vasiliy Kulikov8909c9a2011-03-02 00:33:13 +03001749MODULE_ALIAS_NETDEV("gre0");
Pravin B Shelarc5441932013-03-25 14:49:35 +00001750MODULE_ALIAS_NETDEV("gretap0");
William Tu84e54fe2017-08-22 09:40:28 -07001751MODULE_ALIAS_NETDEV("erspan0");