]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
e905a9ed | 2 | * Linux NET3: GRE over IP protocol decoder. |
1da177e4 LT |
3 | * |
4 | * Authors: Alexey Kuznetsov (kuznet@ms2.inr.ac.ru) | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | */ | |
12 | ||
afd46503 JP |
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
14 | ||
4fc268d2 | 15 | #include <linux/capability.h> |
1da177e4 LT |
16 | #include <linux/module.h> |
17 | #include <linux/types.h> | |
1da177e4 | 18 | #include <linux/kernel.h> |
5a0e3ad6 | 19 | #include <linux/slab.h> |
7c0f6ba6 | 20 | #include <linux/uaccess.h> |
1da177e4 LT |
21 | #include <linux/skbuff.h> |
22 | #include <linux/netdevice.h> | |
23 | #include <linux/in.h> | |
24 | #include <linux/tcp.h> | |
25 | #include <linux/udp.h> | |
26 | #include <linux/if_arp.h> | |
2e15ea39 | 27 | #include <linux/if_vlan.h> |
1da177e4 LT |
28 | #include <linux/init.h> |
29 | #include <linux/in6.h> | |
30 | #include <linux/inetdevice.h> | |
31 | #include <linux/igmp.h> | |
32 | #include <linux/netfilter_ipv4.h> | |
e1a80002 | 33 | #include <linux/etherdevice.h> |
46f25dff | 34 | #include <linux/if_ether.h> |
1da177e4 LT |
35 | |
36 | #include <net/sock.h> | |
37 | #include <net/ip.h> | |
38 | #include <net/icmp.h> | |
39 | #include <net/protocol.h> | |
c5441932 | 40 | #include <net/ip_tunnels.h> |
1da177e4 LT |
41 | #include <net/arp.h> |
42 | #include <net/checksum.h> | |
43 | #include <net/dsfield.h> | |
44 | #include <net/inet_ecn.h> | |
45 | #include <net/xfrm.h> | |
59a4c759 PE |
46 | #include <net/net_namespace.h> |
47 | #include <net/netns/generic.h> | |
c19e654d | 48 | #include <net/rtnetlink.h> |
00959ade | 49 | #include <net/gre.h> |
2e15ea39 | 50 | #include <net/dst_metadata.h> |
84e54fe0 | 51 | #include <net/erspan.h> |
1da177e4 | 52 | |
1da177e4 LT |
53 | /* |
54 | Problems & solutions | |
55 | -------------------- | |
56 | ||
57 | 1. The most important issue is detecting local dead loops. | |
58 | They would cause complete host lockup in transmit, which | |
59 | would be "resolved" by stack overflow or, if queueing is enabled, | |
60 | with infinite looping in net_bh. | |
61 | ||
62 | We cannot track such dead loops during route installation, | |
63 | it is infeasible task. The most general solutions would be | |
64 | to keep skb->encapsulation counter (sort of local ttl), | |
6d0722a2 | 65 | and silently drop packet when it expires. It is a good |
bff52857 | 66 | solution, but it supposes maintaining new variable in ALL |
1da177e4 LT |
67 | skb, even if no tunneling is used. |
68 | ||
6d0722a2 ED |
69 | Current solution: xmit_recursion breaks dead loops. This is a percpu |
70 | counter, since when we enter the first ndo_xmit(), cpu migration is | |
71 | forbidden. We force an exit if this counter reaches RECURSION_LIMIT | |
1da177e4 LT |
72 | |
73 | 2. Networking dead loops would not kill routers, but would really | |
74 | kill network. IP hop limit plays role of "t->recursion" in this case, | |
75 | if we copy it from packet being encapsulated to upper header. | |
76 | It is very good solution, but it introduces two problems: | |
77 | ||
78 | - Routing protocols, using packets with ttl=1 (OSPF, RIP2), | |
79 | do not work over tunnels. | |
80 | - traceroute does not work. I planned to relay ICMP from tunnel, | |
81 | so that this problem would be solved and traceroute output | |
82 | would even more informative. This idea appeared to be wrong: | |
83 | only Linux complies to rfc1812 now (yes, guys, Linux is the only | |
84 | true router now :-)), all routers (at least, in neighbourhood of mine) | |
85 | return only 8 bytes of payload. It is the end. | |
86 | ||
87 | Hence, if we want that OSPF worked or traceroute said something reasonable, | |
88 | we should search for another solution. | |
89 | ||
90 | One of them is to parse packet trying to detect inner encapsulation | |
91 | made by our node. It is difficult or even impossible, especially, | |
bff52857 | 92 | taking into account fragmentation. TO be short, ttl is not solution at all. |
1da177e4 LT |
93 | |
94 | Current solution: The solution was UNEXPECTEDLY SIMPLE. | |
95 | We force DF flag on tunnels with preconfigured hop limit, | |
96 | that is ALL. :-) Well, it does not remove the problem completely, | |
97 | but exponential growth of network traffic is changed to linear | |
98 | (branches, that exceed pmtu are pruned) and tunnel mtu | |
bff52857 | 99 | rapidly degrades to value <68, where looping stops. |
1da177e4 LT |
100 | Yes, it is not good if there exists a router in the loop, |
101 | which does not force DF, even when encapsulating packets have DF set. | |
102 | But it is not our problem! Nobody could accuse us, we made | |
103 | all that we could make. Even if it is your gated who injected | |
104 | fatal route to network, even if it were you who configured | |
105 | fatal static route: you are innocent. :-) | |
106 | ||
1da177e4 LT |
107 | Alexey Kuznetsov. |
108 | */ | |
109 | ||
eccc1bb8 | 110 | static bool log_ecn_error = true; |
111 | module_param(log_ecn_error, bool, 0644); | |
112 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |
113 | ||
c19e654d | 114 | static struct rtnl_link_ops ipgre_link_ops __read_mostly; |
1da177e4 | 115 | static int ipgre_tunnel_init(struct net_device *dev); |
1a66a836 | 116 | static void erspan_build_header(struct sk_buff *skb, |
a3222dc9 WT |
117 | __be32 id, u32 index, |
118 | bool truncate, bool is_ipv4); | |
eb8ce741 | 119 | |
c7d03a00 AD |
120 | static unsigned int ipgre_net_id __read_mostly; |
121 | static unsigned int gre_tap_net_id __read_mostly; | |
84e54fe0 | 122 | static unsigned int erspan_net_id __read_mostly; |
1da177e4 | 123 | |
9f57c67c PS |
124 | static void ipgre_err(struct sk_buff *skb, u32 info, |
125 | const struct tnl_ptk_info *tpi) | |
1da177e4 | 126 | { |
1da177e4 | 127 | |
c5441932 PS |
128 | /* All the routers (except for Linux) return only |
129 | 8 bytes of packet payload. It means, that precise relaying of | |
130 | ICMP in the real Internet is absolutely infeasible. | |
1da177e4 | 131 | |
c5441932 PS |
132 | Moreover, Cisco "wise men" put GRE key to the third word |
133 | in GRE header. It makes impossible maintaining even soft | |
134 | state for keyed GRE tunnels with enabled checksum. Tell | |
135 | them "thank you". | |
1da177e4 | 136 | |
c5441932 PS |
137 | Well, I wonder, rfc1812 was written by Cisco employee, |
138 | what the hell these idiots break standards established | |
139 | by themselves??? | |
140 | */ | |
141 | struct net *net = dev_net(skb->dev); | |
142 | struct ip_tunnel_net *itn; | |
96f5a846 | 143 | const struct iphdr *iph; |
88c7664f ACM |
144 | const int type = icmp_hdr(skb)->type; |
145 | const int code = icmp_hdr(skb)->code; | |
20e1954f | 146 | unsigned int data_len = 0; |
1da177e4 | 147 | struct ip_tunnel *t; |
1da177e4 | 148 | |
1da177e4 LT |
149 | switch (type) { |
150 | default: | |
151 | case ICMP_PARAMETERPROB: | |
9f57c67c | 152 | return; |
1da177e4 LT |
153 | |
154 | case ICMP_DEST_UNREACH: | |
155 | switch (code) { | |
156 | case ICMP_SR_FAILED: | |
157 | case ICMP_PORT_UNREACH: | |
158 | /* Impossible event. */ | |
9f57c67c | 159 | return; |
1da177e4 LT |
160 | default: |
161 | /* All others are translated to HOST_UNREACH. | |
162 | rfc2003 contains "deep thoughts" about NET_UNREACH, | |
163 | I believe they are just ether pollution. --ANK | |
164 | */ | |
165 | break; | |
166 | } | |
167 | break; | |
9f57c67c | 168 | |
1da177e4 LT |
169 | case ICMP_TIME_EXCEEDED: |
170 | if (code != ICMP_EXC_TTL) | |
9f57c67c | 171 | return; |
20e1954f | 172 | data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */ |
1da177e4 | 173 | break; |
55be7a9c DM |
174 | |
175 | case ICMP_REDIRECT: | |
176 | break; | |
1da177e4 LT |
177 | } |
178 | ||
bda7bb46 | 179 | if (tpi->proto == htons(ETH_P_TEB)) |
c5441932 PS |
180 | itn = net_generic(net, gre_tap_net_id); |
181 | else | |
182 | itn = net_generic(net, ipgre_net_id); | |
183 | ||
c0c0c50f | 184 | iph = (const struct iphdr *)(icmp_hdr(skb) + 1); |
bda7bb46 PS |
185 | t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags, |
186 | iph->daddr, iph->saddr, tpi->key); | |
d2083287 | 187 | |
51456b29 | 188 | if (!t) |
9f57c67c | 189 | return; |
36393395 | 190 | |
9b8c6d7b ED |
191 | #if IS_ENABLED(CONFIG_IPV6) |
192 | if (tpi->proto == htons(ETH_P_IPV6) && | |
20e1954f ED |
193 | !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4 + tpi->hdr_len, |
194 | type, data_len)) | |
9b8c6d7b ED |
195 | return; |
196 | #endif | |
197 | ||
36393395 | 198 | if (t->parms.iph.daddr == 0 || |
f97c1e0c | 199 | ipv4_is_multicast(t->parms.iph.daddr)) |
9f57c67c | 200 | return; |
1da177e4 LT |
201 | |
202 | if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED) | |
9f57c67c | 203 | return; |
1da177e4 | 204 | |
da6185d8 | 205 | if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO)) |
1da177e4 LT |
206 | t->err_count++; |
207 | else | |
208 | t->err_count = 1; | |
209 | t->err_time = jiffies; | |
9f57c67c PS |
210 | } |
211 | ||
212 | static 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 | ||
e582615a | 228 | const struct iphdr *iph = (struct iphdr *)skb->data; |
9f57c67c PS |
229 | const int type = icmp_hdr(skb)->type; |
230 | const int code = icmp_hdr(skb)->code; | |
231 | struct tnl_ptk_info tpi; | |
232 | bool csum_err = false; | |
233 | ||
e582615a ED |
234 | if (gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP), |
235 | iph->ihl * 4) < 0) { | |
9f57c67c PS |
236 | if (!csum_err) /* ignore csum errors. */ |
237 | return; | |
238 | } | |
239 | ||
240 | if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) { | |
241 | ipv4_update_pmtu(skb, dev_net(skb->dev), info, | |
242 | skb->dev->ifindex, 0, IPPROTO_GRE, 0); | |
243 | return; | |
244 | } | |
245 | if (type == ICMP_REDIRECT) { | |
246 | ipv4_redirect(skb, dev_net(skb->dev), skb->dev->ifindex, 0, | |
247 | IPPROTO_GRE, 0); | |
248 | return; | |
249 | } | |
250 | ||
251 | ipgre_err(skb, info, &tpi); | |
1da177e4 LT |
252 | } |
253 | ||
84e54fe0 WT |
254 | static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi, |
255 | int gre_hdr_len) | |
256 | { | |
257 | struct net *net = dev_net(skb->dev); | |
258 | struct metadata_dst *tun_dst = NULL; | |
1d7e2ed2 WT |
259 | struct erspan_base_hdr *ershdr; |
260 | struct erspan_metadata *pkt_md; | |
84e54fe0 WT |
261 | struct ip_tunnel_net *itn; |
262 | struct ip_tunnel *tunnel; | |
84e54fe0 | 263 | const struct iphdr *iph; |
1d7e2ed2 | 264 | int ver; |
84e54fe0 WT |
265 | int len; |
266 | ||
267 | itn = net_generic(net, erspan_net_id); | |
84e54fe0 WT |
268 | len = gre_hdr_len + sizeof(*ershdr); |
269 | ||
1d7e2ed2 | 270 | /* Check based hdr len */ |
84e54fe0 | 271 | if (unlikely(!pskb_may_pull(skb, len))) |
c05fad57 | 272 | return PACKET_REJECT; |
84e54fe0 WT |
273 | |
274 | iph = ip_hdr(skb); | |
1d7e2ed2 WT |
275 | ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len); |
276 | ver = (ntohs(ershdr->ver_vlan) & VER_MASK) >> VER_OFFSET; | |
84e54fe0 WT |
277 | |
278 | /* The original GRE header does not have key field, | |
279 | * Use ERSPAN 10-bit session ID as key. | |
280 | */ | |
935a9749 | 281 | tpi->key = cpu_to_be32(ntohs(ershdr->session_id) & ID_MASK); |
1d7e2ed2 | 282 | pkt_md = (struct erspan_metadata *)(ershdr + 1); |
84e54fe0 WT |
283 | tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, |
284 | tpi->flags | TUNNEL_KEY, | |
285 | iph->saddr, iph->daddr, tpi->key); | |
286 | ||
287 | if (tunnel) { | |
1d7e2ed2 WT |
288 | len = gre_hdr_len + erspan_hdr_len(ver); |
289 | if (unlikely(!pskb_may_pull(skb, len))) | |
290 | return -ENOMEM; | |
291 | ||
84e54fe0 | 292 | if (__iptunnel_pull_header(skb, |
1d7e2ed2 | 293 | len, |
84e54fe0 WT |
294 | htons(ETH_P_TEB), |
295 | false, false) < 0) | |
296 | goto drop; | |
297 | ||
1a66a836 WT |
298 | if (tunnel->collect_md) { |
299 | struct ip_tunnel_info *info; | |
300 | struct erspan_metadata *md; | |
301 | __be64 tun_id; | |
302 | __be16 flags; | |
303 | ||
304 | tpi->flags |= TUNNEL_KEY; | |
305 | flags = tpi->flags; | |
306 | tun_id = key32_to_tunnel_id(tpi->key); | |
307 | ||
308 | tun_dst = ip_tun_rx_dst(skb, flags, | |
309 | tun_id, sizeof(*md)); | |
310 | if (!tun_dst) | |
311 | return PACKET_REJECT; | |
312 | ||
313 | md = ip_tunnel_info_opts(&tun_dst->u.tun_info); | |
314 | if (!md) | |
315 | return PACKET_REJECT; | |
316 | ||
1d7e2ed2 | 317 | memcpy(md, pkt_md, sizeof(*md)); |
f551c91d WT |
318 | md->version = ver; |
319 | ||
1a66a836 WT |
320 | info = &tun_dst->u.tun_info; |
321 | info->key.tun_flags |= TUNNEL_ERSPAN_OPT; | |
322 | info->options_len = sizeof(*md); | |
323 | } else { | |
f551c91d WT |
324 | tunnel->erspan_ver = ver; |
325 | if (ver == 1) { | |
326 | tunnel->index = ntohl(pkt_md->u.index); | |
327 | } else { | |
328 | u16 md2_flags; | |
329 | u16 dir, hwid; | |
330 | ||
331 | md2_flags = ntohs(pkt_md->u.md2.flags); | |
332 | dir = (md2_flags & DIR_MASK) >> DIR_OFFSET; | |
333 | hwid = (md2_flags & HWID_MASK) >> HWID_OFFSET; | |
334 | tunnel->dir = dir; | |
335 | tunnel->hwid = hwid; | |
336 | } | |
337 | ||
1a66a836 WT |
338 | } |
339 | ||
84e54fe0 WT |
340 | skb_reset_mac_header(skb); |
341 | ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); | |
342 | return PACKET_RCVD; | |
343 | } | |
344 | drop: | |
345 | kfree_skb(skb); | |
346 | return PACKET_RCVD; | |
347 | } | |
348 | ||
125372fa JB |
349 | static 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) | |
1da177e4 | 351 | { |
2e15ea39 | 352 | struct metadata_dst *tun_dst = NULL; |
b71d1d42 | 353 | const struct iphdr *iph; |
1da177e4 | 354 | struct ip_tunnel *tunnel; |
1da177e4 | 355 | |
c5441932 | 356 | iph = ip_hdr(skb); |
bda7bb46 PS |
357 | tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags, |
358 | iph->saddr, iph->daddr, tpi->key); | |
e1a80002 | 359 | |
d2083287 | 360 | if (tunnel) { |
125372fa JB |
361 | if (__iptunnel_pull_header(skb, hdr_len, tpi->proto, |
362 | raw_proto, false) < 0) | |
244a797b JB |
363 | goto drop; |
364 | ||
e271c7b4 JB |
365 | if (tunnel->dev->type != ARPHRD_NONE) |
366 | skb_pop_mac_header(skb); | |
367 | else | |
368 | skb_reset_mac_header(skb); | |
2e15ea39 | 369 | if (tunnel->collect_md) { |
c29a70d2 PS |
370 | __be16 flags; |
371 | __be64 tun_id; | |
2e15ea39 | 372 | |
c29a70d2 | 373 | flags = tpi->flags & (TUNNEL_CSUM | TUNNEL_KEY); |
d817f432 | 374 | tun_id = key32_to_tunnel_id(tpi->key); |
c29a70d2 | 375 | tun_dst = ip_tun_rx_dst(skb, flags, tun_id, 0); |
2e15ea39 PS |
376 | if (!tun_dst) |
377 | return PACKET_REJECT; | |
2e15ea39 PS |
378 | } |
379 | ||
380 | ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); | |
bda7bb46 | 381 | return PACKET_RCVD; |
1da177e4 | 382 | } |
125372fa | 383 | return PACKET_NEXT; |
244a797b JB |
384 | |
385 | drop: | |
386 | kfree_skb(skb); | |
387 | return PACKET_RCVD; | |
1da177e4 LT |
388 | } |
389 | ||
125372fa JB |
390 | static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi, |
391 | int hdr_len) | |
392 | { | |
393 | struct net *net = dev_net(skb->dev); | |
394 | struct ip_tunnel_net *itn; | |
395 | int res; | |
396 | ||
397 | if (tpi->proto == htons(ETH_P_TEB)) | |
398 | itn = net_generic(net, gre_tap_net_id); | |
399 | else | |
400 | itn = net_generic(net, ipgre_net_id); | |
401 | ||
402 | res = __ipgre_rcv(skb, tpi, itn, hdr_len, false); | |
403 | if (res == PACKET_NEXT && tpi->proto == htons(ETH_P_TEB)) { | |
404 | /* ipgre tunnels in collect metadata mode should receive | |
405 | * also ETH_P_TEB traffic. | |
406 | */ | |
407 | itn = net_generic(net, ipgre_net_id); | |
408 | res = __ipgre_rcv(skb, tpi, itn, hdr_len, true); | |
409 | } | |
410 | return res; | |
411 | } | |
412 | ||
9f57c67c PS |
413 | static int gre_rcv(struct sk_buff *skb) |
414 | { | |
415 | struct tnl_ptk_info tpi; | |
416 | bool csum_err = false; | |
95f5c64c | 417 | int hdr_len; |
9f57c67c PS |
418 | |
419 | #ifdef CONFIG_NET_IPGRE_BROADCAST | |
420 | if (ipv4_is_multicast(ip_hdr(skb)->daddr)) { | |
421 | /* Looped back packet, drop it! */ | |
422 | if (rt_is_output_route(skb_rtable(skb))) | |
423 | goto drop; | |
424 | } | |
425 | #endif | |
426 | ||
e582615a | 427 | hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP), 0); |
f132ae7c | 428 | if (hdr_len < 0) |
95f5c64c TH |
429 | goto drop; |
430 | ||
f551c91d WT |
431 | if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) || |
432 | tpi.proto == htons(ETH_P_ERSPAN2))) { | |
84e54fe0 WT |
433 | if (erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD) |
434 | return 0; | |
435 | } | |
436 | ||
244a797b | 437 | if (ipgre_rcv(skb, &tpi, hdr_len) == PACKET_RCVD) |
9f57c67c PS |
438 | return 0; |
439 | ||
440 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); | |
441 | drop: | |
442 | kfree_skb(skb); | |
443 | return 0; | |
444 | } | |
445 | ||
c5441932 PS |
446 | static void __gre_xmit(struct sk_buff *skb, struct net_device *dev, |
447 | const struct iphdr *tnl_params, | |
448 | __be16 proto) | |
449 | { | |
450 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
1da177e4 | 451 | |
c5441932 PS |
452 | if (tunnel->parms.o_flags & TUNNEL_SEQ) |
453 | tunnel->o_seqno++; | |
1da177e4 | 454 | |
c5441932 | 455 | /* Push GRE header. */ |
182a352d TH |
456 | gre_build_header(skb, tunnel->tun_hlen, |
457 | tunnel->parms.o_flags, proto, tunnel->parms.o_key, | |
458 | htonl(tunnel->o_seqno)); | |
54bc9bac | 459 | |
bf3d6a8f | 460 | ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol); |
c5441932 | 461 | } |
1da177e4 | 462 | |
aed069df | 463 | static int gre_handle_offloads(struct sk_buff *skb, bool csum) |
b2acd1dc | 464 | { |
6fa79666 | 465 | return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE); |
b2acd1dc PS |
466 | } |
467 | ||
fc4099f1 PS |
468 | static struct rtable *gre_get_rt(struct sk_buff *skb, |
469 | struct net_device *dev, | |
470 | struct flowi4 *fl, | |
471 | const struct ip_tunnel_key *key) | |
472 | { | |
473 | struct net *net = dev_net(dev); | |
474 | ||
475 | memset(fl, 0, sizeof(*fl)); | |
476 | fl->daddr = key->u.ipv4.dst; | |
477 | fl->saddr = key->u.ipv4.src; | |
478 | fl->flowi4_tos = RT_TOS(key->tos); | |
479 | fl->flowi4_mark = skb->mark; | |
480 | fl->flowi4_proto = IPPROTO_GRE; | |
481 | ||
482 | return ip_route_output_key(net, fl); | |
483 | } | |
484 | ||
862a03c3 WT |
485 | static struct rtable *prepare_fb_xmit(struct sk_buff *skb, |
486 | struct net_device *dev, | |
487 | struct flowi4 *fl, | |
488 | int tunnel_hlen) | |
2e15ea39 PS |
489 | { |
490 | struct ip_tunnel_info *tun_info; | |
2e15ea39 | 491 | const struct ip_tunnel_key *key; |
db3c6139 | 492 | struct rtable *rt = NULL; |
2e15ea39 | 493 | int min_headroom; |
db3c6139 | 494 | bool use_cache; |
2e15ea39 PS |
495 | int err; |
496 | ||
61adedf3 | 497 | tun_info = skb_tunnel_info(skb); |
2e15ea39 | 498 | key = &tun_info->key; |
db3c6139 | 499 | use_cache = ip_tunnel_dst_cache_usable(skb, tun_info); |
862a03c3 | 500 | |
db3c6139 | 501 | if (use_cache) |
862a03c3 | 502 | rt = dst_cache_get_ip4(&tun_info->dst_cache, &fl->saddr); |
3c1cb4d2 | 503 | if (!rt) { |
862a03c3 | 504 | rt = gre_get_rt(skb, dev, fl, key); |
3c1cb4d2 | 505 | if (IS_ERR(rt)) |
862a03c3 | 506 | goto err_free_skb; |
db3c6139 | 507 | if (use_cache) |
3c1cb4d2 | 508 | dst_cache_set_ip4(&tun_info->dst_cache, &rt->dst, |
862a03c3 | 509 | fl->saddr); |
3c1cb4d2 | 510 | } |
2e15ea39 | 511 | |
2e15ea39 PS |
512 | min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len |
513 | + tunnel_hlen + sizeof(struct iphdr); | |
514 | if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) { | |
515 | int head_delta = SKB_DATA_ALIGN(min_headroom - | |
516 | skb_headroom(skb) + | |
517 | 16); | |
518 | err = pskb_expand_head(skb, max_t(int, head_delta, 0), | |
519 | 0, GFP_ATOMIC); | |
520 | if (unlikely(err)) | |
521 | goto err_free_rt; | |
522 | } | |
862a03c3 WT |
523 | return rt; |
524 | ||
525 | err_free_rt: | |
526 | ip_rt_put(rt); | |
527 | err_free_skb: | |
528 | kfree_skb(skb); | |
529 | dev->stats.tx_dropped++; | |
530 | return NULL; | |
531 | } | |
532 | ||
533 | static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev, | |
534 | __be16 proto) | |
535 | { | |
536 | struct ip_tunnel_info *tun_info; | |
537 | const struct ip_tunnel_key *key; | |
538 | struct rtable *rt = NULL; | |
539 | struct flowi4 fl; | |
540 | int tunnel_hlen; | |
541 | __be16 df, flags; | |
542 | ||
543 | tun_info = skb_tunnel_info(skb); | |
544 | if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) || | |
545 | ip_tunnel_info_af(tun_info) != AF_INET)) | |
546 | goto err_free_skb; | |
547 | ||
548 | key = &tun_info->key; | |
549 | tunnel_hlen = gre_calc_hlen(key->tun_flags); | |
550 | ||
551 | rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen); | |
552 | if (!rt) | |
553 | return; | |
2e15ea39 PS |
554 | |
555 | /* Push Tunnel header. */ | |
aed069df | 556 | if (gre_handle_offloads(skb, !!(tun_info->key.tun_flags & TUNNEL_CSUM))) |
2e15ea39 | 557 | goto err_free_rt; |
2e15ea39 PS |
558 | |
559 | flags = tun_info->key.tun_flags & (TUNNEL_CSUM | TUNNEL_KEY); | |
cba65321 | 560 | gre_build_header(skb, tunnel_hlen, flags, proto, |
d817f432 | 561 | tunnel_id_to_key32(tun_info->key.tun_id), 0); |
2e15ea39 PS |
562 | |
563 | df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; | |
039f5062 PS |
564 | |
565 | iptunnel_xmit(skb->sk, rt, skb, fl.saddr, key->u.ipv4.dst, IPPROTO_GRE, | |
566 | key->tos, key->ttl, df, false); | |
2e15ea39 PS |
567 | return; |
568 | ||
569 | err_free_rt: | |
570 | ip_rt_put(rt); | |
571 | err_free_skb: | |
572 | kfree_skb(skb); | |
573 | dev->stats.tx_dropped++; | |
574 | } | |
575 | ||
1a66a836 WT |
576 | static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev, |
577 | __be16 proto) | |
578 | { | |
579 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
580 | struct ip_tunnel_info *tun_info; | |
581 | const struct ip_tunnel_key *key; | |
582 | struct erspan_metadata *md; | |
583 | struct rtable *rt = NULL; | |
584 | bool truncate = false; | |
585 | struct flowi4 fl; | |
586 | int tunnel_hlen; | |
f551c91d | 587 | int version; |
1a66a836 WT |
588 | __be16 df; |
589 | ||
590 | tun_info = skb_tunnel_info(skb); | |
591 | if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) || | |
592 | ip_tunnel_info_af(tun_info) != AF_INET)) | |
593 | goto err_free_skb; | |
594 | ||
595 | key = &tun_info->key; | |
f551c91d WT |
596 | md = ip_tunnel_info_opts(tun_info); |
597 | if (!md) | |
598 | goto err_free_rt; | |
1a66a836 WT |
599 | |
600 | /* ERSPAN has fixed 8 byte GRE header */ | |
f551c91d WT |
601 | version = md->version; |
602 | tunnel_hlen = 8 + erspan_hdr_len(version); | |
1a66a836 WT |
603 | |
604 | rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen); | |
605 | if (!rt) | |
606 | return; | |
607 | ||
608 | if (gre_handle_offloads(skb, false)) | |
609 | goto err_free_rt; | |
610 | ||
f192970d WT |
611 | if (skb->len > dev->mtu + dev->hard_header_len) { |
612 | pskb_trim(skb, dev->mtu + dev->hard_header_len); | |
1a66a836 WT |
613 | truncate = true; |
614 | } | |
615 | ||
f551c91d WT |
616 | if (version == 1) { |
617 | erspan_build_header(skb, tunnel_id_to_key32(key->tun_id), | |
618 | ntohl(md->u.index), truncate, true); | |
619 | } else if (version == 2) { | |
620 | u16 md2_flags; | |
621 | u8 direction; | |
622 | u16 hwid; | |
1a66a836 | 623 | |
f551c91d WT |
624 | md2_flags = ntohs(md->u.md2.flags); |
625 | direction = (md2_flags & DIR_MASK) >> DIR_OFFSET; | |
626 | hwid = (md2_flags & HWID_MASK) >> HWID_OFFSET; | |
627 | ||
628 | erspan_build_header_v2(skb, tunnel_id_to_key32(key->tun_id), | |
629 | direction, hwid, truncate, true); | |
630 | } else { | |
631 | goto err_free_rt; | |
632 | } | |
1a66a836 WT |
633 | |
634 | gre_build_header(skb, 8, TUNNEL_SEQ, | |
635 | htons(ETH_P_ERSPAN), 0, htonl(tunnel->o_seqno++)); | |
636 | ||
637 | df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; | |
638 | ||
639 | iptunnel_xmit(skb->sk, rt, skb, fl.saddr, key->u.ipv4.dst, IPPROTO_GRE, | |
640 | key->tos, key->ttl, df, false); | |
641 | return; | |
642 | ||
643 | err_free_rt: | |
644 | ip_rt_put(rt); | |
645 | err_free_skb: | |
646 | kfree_skb(skb); | |
647 | dev->stats.tx_dropped++; | |
648 | } | |
649 | ||
fc4099f1 PS |
650 | static int gre_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) |
651 | { | |
652 | struct ip_tunnel_info *info = skb_tunnel_info(skb); | |
653 | struct rtable *rt; | |
654 | struct flowi4 fl4; | |
655 | ||
656 | if (ip_tunnel_info_af(info) != AF_INET) | |
657 | return -EINVAL; | |
658 | ||
659 | rt = gre_get_rt(skb, dev, &fl4, &info->key); | |
660 | if (IS_ERR(rt)) | |
661 | return PTR_ERR(rt); | |
662 | ||
663 | ip_rt_put(rt); | |
664 | info->key.u.ipv4.src = fl4.saddr; | |
665 | return 0; | |
666 | } | |
667 | ||
c5441932 PS |
668 | static netdev_tx_t ipgre_xmit(struct sk_buff *skb, |
669 | struct net_device *dev) | |
670 | { | |
671 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
672 | const struct iphdr *tnl_params; | |
1da177e4 | 673 | |
2e15ea39 | 674 | if (tunnel->collect_md) { |
2090714e | 675 | gre_fb_xmit(skb, dev, skb->protocol); |
2e15ea39 PS |
676 | return NETDEV_TX_OK; |
677 | } | |
678 | ||
c5441932 PS |
679 | if (dev->header_ops) { |
680 | /* Need space for new headers */ | |
681 | if (skb_cow_head(skb, dev->needed_headroom - | |
2bac7cb3 | 682 | (tunnel->hlen + sizeof(struct iphdr)))) |
c5441932 | 683 | goto free_skb; |
1da177e4 | 684 | |
c5441932 | 685 | tnl_params = (const struct iphdr *)skb->data; |
1da177e4 | 686 | |
c5441932 PS |
687 | /* Pull skb since ip_tunnel_xmit() needs skb->data pointing |
688 | * to gre header. | |
689 | */ | |
690 | skb_pull(skb, tunnel->hlen + sizeof(struct iphdr)); | |
8a0033a9 | 691 | skb_reset_mac_header(skb); |
c5441932 PS |
692 | } else { |
693 | if (skb_cow_head(skb, dev->needed_headroom)) | |
694 | goto free_skb; | |
1da177e4 | 695 | |
c5441932 | 696 | tnl_params = &tunnel->parms.iph; |
1da177e4 LT |
697 | } |
698 | ||
aed069df AD |
699 | if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM))) |
700 | goto free_skb; | |
8a0033a9 | 701 | |
c5441932 | 702 | __gre_xmit(skb, dev, tnl_params, skb->protocol); |
6ed10654 | 703 | return NETDEV_TX_OK; |
1da177e4 | 704 | |
c5441932 | 705 | free_skb: |
3acfa1e7 | 706 | kfree_skb(skb); |
c5441932 | 707 | dev->stats.tx_dropped++; |
6ed10654 | 708 | return NETDEV_TX_OK; |
1da177e4 LT |
709 | } |
710 | ||
84e54fe0 WT |
711 | static netdev_tx_t erspan_xmit(struct sk_buff *skb, |
712 | struct net_device *dev) | |
713 | { | |
714 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
715 | bool truncate = false; | |
716 | ||
1a66a836 WT |
717 | if (tunnel->collect_md) { |
718 | erspan_fb_xmit(skb, dev, skb->protocol); | |
719 | return NETDEV_TX_OK; | |
720 | } | |
721 | ||
84e54fe0 WT |
722 | if (gre_handle_offloads(skb, false)) |
723 | goto free_skb; | |
724 | ||
725 | if (skb_cow_head(skb, dev->needed_headroom)) | |
726 | goto free_skb; | |
727 | ||
f192970d WT |
728 | if (skb->len > dev->mtu + dev->hard_header_len) { |
729 | pskb_trim(skb, dev->mtu + dev->hard_header_len); | |
84e54fe0 WT |
730 | truncate = true; |
731 | } | |
732 | ||
733 | /* Push ERSPAN header */ | |
f551c91d WT |
734 | if (tunnel->erspan_ver == 1) |
735 | erspan_build_header(skb, tunnel->parms.o_key, tunnel->index, | |
736 | truncate, true); | |
737 | else | |
738 | erspan_build_header_v2(skb, tunnel->parms.o_key, | |
739 | tunnel->dir, tunnel->hwid, | |
740 | truncate, true); | |
741 | ||
84e54fe0 WT |
742 | tunnel->parms.o_flags &= ~TUNNEL_KEY; |
743 | __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_ERSPAN)); | |
744 | return NETDEV_TX_OK; | |
745 | ||
746 | free_skb: | |
747 | kfree_skb(skb); | |
748 | dev->stats.tx_dropped++; | |
749 | return NETDEV_TX_OK; | |
750 | } | |
751 | ||
c5441932 PS |
752 | static netdev_tx_t gre_tap_xmit(struct sk_buff *skb, |
753 | struct net_device *dev) | |
ee34c1eb | 754 | { |
c5441932 | 755 | struct ip_tunnel *tunnel = netdev_priv(dev); |
ee34c1eb | 756 | |
2e15ea39 | 757 | if (tunnel->collect_md) { |
2090714e | 758 | gre_fb_xmit(skb, dev, htons(ETH_P_TEB)); |
2e15ea39 PS |
759 | return NETDEV_TX_OK; |
760 | } | |
761 | ||
aed069df AD |
762 | if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM))) |
763 | goto free_skb; | |
ee34c1eb | 764 | |
c5441932 PS |
765 | if (skb_cow_head(skb, dev->needed_headroom)) |
766 | goto free_skb; | |
42aa9162 | 767 | |
c5441932 | 768 | __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB)); |
c5441932 | 769 | return NETDEV_TX_OK; |
ee34c1eb | 770 | |
c5441932 | 771 | free_skb: |
3acfa1e7 | 772 | kfree_skb(skb); |
c5441932 PS |
773 | dev->stats.tx_dropped++; |
774 | return NETDEV_TX_OK; | |
ee34c1eb MS |
775 | } |
776 | ||
dd9d598c XL |
777 | static void ipgre_link_update(struct net_device *dev, bool set_mtu) |
778 | { | |
779 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
780 | int len; | |
781 | ||
782 | len = tunnel->tun_hlen; | |
783 | tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags); | |
784 | len = tunnel->tun_hlen - len; | |
785 | tunnel->hlen = tunnel->hlen + len; | |
786 | ||
787 | dev->needed_headroom = dev->needed_headroom + len; | |
788 | if (set_mtu) | |
789 | dev->mtu = max_t(int, dev->mtu - len, 68); | |
790 | ||
791 | if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) { | |
792 | if (!(tunnel->parms.o_flags & TUNNEL_CSUM) || | |
793 | tunnel->encap.type == TUNNEL_ENCAP_NONE) { | |
794 | dev->features |= NETIF_F_GSO_SOFTWARE; | |
795 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; | |
796 | } | |
797 | dev->features |= NETIF_F_LLTX; | |
798 | } | |
799 | } | |
800 | ||
c5441932 PS |
801 | static int ipgre_tunnel_ioctl(struct net_device *dev, |
802 | struct ifreq *ifr, int cmd) | |
1da177e4 | 803 | { |
1da177e4 | 804 | struct ip_tunnel_parm p; |
a0efab67 | 805 | int err; |
1da177e4 | 806 | |
c5441932 PS |
807 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
808 | return -EFAULT; | |
a0efab67 | 809 | |
6c734fb8 CW |
810 | if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) { |
811 | if (p.iph.version != 4 || p.iph.protocol != IPPROTO_GRE || | |
a0efab67 XL |
812 | p.iph.ihl != 5 || (p.iph.frag_off & htons(~IP_DF)) || |
813 | ((p.i_flags | p.o_flags) & (GRE_VERSION | GRE_ROUTING))) | |
6c734fb8 | 814 | return -EINVAL; |
1da177e4 | 815 | } |
a0efab67 | 816 | |
c5441932 PS |
817 | p.i_flags = gre_flags_to_tnl_flags(p.i_flags); |
818 | p.o_flags = gre_flags_to_tnl_flags(p.o_flags); | |
1da177e4 | 819 | |
c5441932 PS |
820 | err = ip_tunnel_ioctl(dev, &p, cmd); |
821 | if (err) | |
822 | return err; | |
1da177e4 | 823 | |
a0efab67 XL |
824 | if (cmd == SIOCCHGTUNNEL) { |
825 | struct ip_tunnel *t = netdev_priv(dev); | |
826 | ||
827 | t->parms.i_flags = p.i_flags; | |
828 | t->parms.o_flags = p.o_flags; | |
829 | ||
830 | if (strcmp(dev->rtnl_link_ops->kind, "erspan")) | |
831 | ipgre_link_update(dev, true); | |
832 | } | |
833 | ||
95f5c64c TH |
834 | p.i_flags = gre_tnl_flags_to_gre_flags(p.i_flags); |
835 | p.o_flags = gre_tnl_flags_to_gre_flags(p.o_flags); | |
c5441932 PS |
836 | |
837 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | |
838 | return -EFAULT; | |
a0efab67 | 839 | |
1da177e4 LT |
840 | return 0; |
841 | } | |
842 | ||
1da177e4 LT |
843 | /* Nice toy. Unfortunately, useless in real life :-) |
844 | It allows to construct virtual multiprotocol broadcast "LAN" | |
845 | over the Internet, provided multicast routing is tuned. | |
846 | ||
847 | ||
848 | I have no idea was this bicycle invented before me, | |
849 | so that I had to set ARPHRD_IPGRE to a random value. | |
850 | I have an impression, that Cisco could make something similar, | |
851 | but this feature is apparently missing in IOS<=11.2(8). | |
e905a9ed | 852 | |
1da177e4 LT |
853 | I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks |
854 | with broadcast 224.66.66.66. If you have access to mbone, play with me :-) | |
855 | ||
856 | ping -t 255 224.66.66.66 | |
857 | ||
858 | If nobody answers, mbone does not work. | |
859 | ||
860 | ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255 | |
861 | ip addr add 10.66.66.<somewhat>/24 dev Universe | |
862 | ifconfig Universe up | |
863 | ifconfig Universe add fe80::<Your_real_addr>/10 | |
864 | ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96 | |
865 | ftp 10.66.66.66 | |
866 | ... | |
867 | ftp fec0:6666:6666::193.233.7.65 | |
868 | ... | |
1da177e4 | 869 | */ |
3b04ddde SH |
870 | static int ipgre_header(struct sk_buff *skb, struct net_device *dev, |
871 | unsigned short type, | |
1507850b | 872 | const void *daddr, const void *saddr, unsigned int len) |
1da177e4 | 873 | { |
2941a486 | 874 | struct ip_tunnel *t = netdev_priv(dev); |
c5441932 PS |
875 | struct iphdr *iph; |
876 | struct gre_base_hdr *greh; | |
1da177e4 | 877 | |
d58ff351 | 878 | iph = skb_push(skb, t->hlen + sizeof(*iph)); |
c5441932 | 879 | greh = (struct gre_base_hdr *)(iph+1); |
95f5c64c | 880 | greh->flags = gre_tnl_flags_to_gre_flags(t->parms.o_flags); |
c5441932 | 881 | greh->protocol = htons(type); |
1da177e4 | 882 | |
c5441932 | 883 | memcpy(iph, &t->parms.iph, sizeof(struct iphdr)); |
e905a9ed | 884 | |
c5441932 | 885 | /* Set the source hardware address. */ |
1da177e4 LT |
886 | if (saddr) |
887 | memcpy(&iph->saddr, saddr, 4); | |
6d55cb91 | 888 | if (daddr) |
1da177e4 | 889 | memcpy(&iph->daddr, daddr, 4); |
6d55cb91 | 890 | if (iph->daddr) |
77a482bd | 891 | return t->hlen + sizeof(*iph); |
e905a9ed | 892 | |
c5441932 | 893 | return -(t->hlen + sizeof(*iph)); |
1da177e4 LT |
894 | } |
895 | ||
6a5f44d7 TT |
896 | static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr) |
897 | { | |
b71d1d42 | 898 | const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb); |
6a5f44d7 TT |
899 | memcpy(haddr, &iph->saddr, 4); |
900 | return 4; | |
901 | } | |
902 | ||
3b04ddde SH |
903 | static const struct header_ops ipgre_header_ops = { |
904 | .create = ipgre_header, | |
6a5f44d7 | 905 | .parse = ipgre_header_parse, |
3b04ddde SH |
906 | }; |
907 | ||
6a5f44d7 | 908 | #ifdef CONFIG_NET_IPGRE_BROADCAST |
1da177e4 LT |
909 | static int ipgre_open(struct net_device *dev) |
910 | { | |
2941a486 | 911 | struct ip_tunnel *t = netdev_priv(dev); |
1da177e4 | 912 | |
f97c1e0c | 913 | if (ipv4_is_multicast(t->parms.iph.daddr)) { |
cbb1e85f DM |
914 | struct flowi4 fl4; |
915 | struct rtable *rt; | |
916 | ||
b57708ad | 917 | rt = ip_route_output_gre(t->net, &fl4, |
cbb1e85f DM |
918 | t->parms.iph.daddr, |
919 | t->parms.iph.saddr, | |
920 | t->parms.o_key, | |
921 | RT_TOS(t->parms.iph.tos), | |
922 | t->parms.link); | |
b23dd4fe | 923 | if (IS_ERR(rt)) |
1da177e4 | 924 | return -EADDRNOTAVAIL; |
d8d1f30b | 925 | dev = rt->dst.dev; |
1da177e4 | 926 | ip_rt_put(rt); |
51456b29 | 927 | if (!__in_dev_get_rtnl(dev)) |
1da177e4 LT |
928 | return -EADDRNOTAVAIL; |
929 | t->mlink = dev->ifindex; | |
e5ed6399 | 930 | ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr); |
1da177e4 LT |
931 | } |
932 | return 0; | |
933 | } | |
934 | ||
935 | static int ipgre_close(struct net_device *dev) | |
936 | { | |
2941a486 | 937 | struct ip_tunnel *t = netdev_priv(dev); |
b8c26a33 | 938 | |
f97c1e0c | 939 | if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) { |
7fee0ca2 | 940 | struct in_device *in_dev; |
b57708ad | 941 | in_dev = inetdev_by_index(t->net, t->mlink); |
8723e1b4 | 942 | if (in_dev) |
1da177e4 | 943 | ip_mc_dec_group(in_dev, t->parms.iph.daddr); |
1da177e4 LT |
944 | } |
945 | return 0; | |
946 | } | |
1da177e4 LT |
947 | #endif |
948 | ||
b8c26a33 SH |
949 | static const struct net_device_ops ipgre_netdev_ops = { |
950 | .ndo_init = ipgre_tunnel_init, | |
c5441932 | 951 | .ndo_uninit = ip_tunnel_uninit, |
b8c26a33 SH |
952 | #ifdef CONFIG_NET_IPGRE_BROADCAST |
953 | .ndo_open = ipgre_open, | |
954 | .ndo_stop = ipgre_close, | |
955 | #endif | |
c5441932 | 956 | .ndo_start_xmit = ipgre_xmit, |
b8c26a33 | 957 | .ndo_do_ioctl = ipgre_tunnel_ioctl, |
c5441932 PS |
958 | .ndo_change_mtu = ip_tunnel_change_mtu, |
959 | .ndo_get_stats64 = ip_tunnel_get_stats64, | |
1e99584b | 960 | .ndo_get_iflink = ip_tunnel_get_iflink, |
b8c26a33 SH |
961 | }; |
962 | ||
6b78f16e ED |
963 | #define GRE_FEATURES (NETIF_F_SG | \ |
964 | NETIF_F_FRAGLIST | \ | |
965 | NETIF_F_HIGHDMA | \ | |
966 | NETIF_F_HW_CSUM) | |
967 | ||
1da177e4 LT |
968 | static void ipgre_tunnel_setup(struct net_device *dev) |
969 | { | |
b8c26a33 | 970 | dev->netdev_ops = &ipgre_netdev_ops; |
5a455275 | 971 | dev->type = ARPHRD_IPGRE; |
c5441932 PS |
972 | ip_tunnel_setup(dev, ipgre_net_id); |
973 | } | |
1da177e4 | 974 | |
c5441932 PS |
975 | static void __gre_tunnel_init(struct net_device *dev) |
976 | { | |
977 | struct ip_tunnel *tunnel; | |
4565e991 | 978 | int t_hlen; |
c5441932 PS |
979 | |
980 | tunnel = netdev_priv(dev); | |
95f5c64c | 981 | tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags); |
c5441932 PS |
982 | tunnel->parms.iph.protocol = IPPROTO_GRE; |
983 | ||
4565e991 TH |
984 | tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen; |
985 | ||
986 | t_hlen = tunnel->hlen + sizeof(struct iphdr); | |
987 | ||
988 | dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4; | |
989 | dev->mtu = ETH_DATA_LEN - t_hlen - 4; | |
6b78f16e | 990 | |
b57708ad | 991 | dev->features |= GRE_FEATURES; |
6b78f16e | 992 | dev->hw_features |= GRE_FEATURES; |
c5441932 PS |
993 | |
994 | if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) { | |
a0ca153f AD |
995 | /* TCP offload with GRE SEQ is not supported, nor |
996 | * can we support 2 levels of outer headers requiring | |
997 | * an update. | |
998 | */ | |
999 | if (!(tunnel->parms.o_flags & TUNNEL_CSUM) || | |
1000 | (tunnel->encap.type == TUNNEL_ENCAP_NONE)) { | |
1001 | dev->features |= NETIF_F_GSO_SOFTWARE; | |
1002 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; | |
1003 | } | |
1004 | ||
c5441932 PS |
1005 | /* Can use a lockless transmit, unless we generate |
1006 | * output sequences | |
1007 | */ | |
1008 | dev->features |= NETIF_F_LLTX; | |
1009 | } | |
1da177e4 LT |
1010 | } |
1011 | ||
1012 | static int ipgre_tunnel_init(struct net_device *dev) | |
1013 | { | |
c5441932 PS |
1014 | struct ip_tunnel *tunnel = netdev_priv(dev); |
1015 | struct iphdr *iph = &tunnel->parms.iph; | |
1da177e4 | 1016 | |
c5441932 | 1017 | __gre_tunnel_init(dev); |
1da177e4 | 1018 | |
c5441932 PS |
1019 | memcpy(dev->dev_addr, &iph->saddr, 4); |
1020 | memcpy(dev->broadcast, &iph->daddr, 4); | |
1da177e4 | 1021 | |
c5441932 | 1022 | dev->flags = IFF_NOARP; |
02875878 | 1023 | netif_keep_dst(dev); |
c5441932 | 1024 | dev->addr_len = 4; |
1da177e4 | 1025 | |
a64b04d8 | 1026 | if (iph->daddr && !tunnel->collect_md) { |
1da177e4 | 1027 | #ifdef CONFIG_NET_IPGRE_BROADCAST |
f97c1e0c | 1028 | if (ipv4_is_multicast(iph->daddr)) { |
1da177e4 LT |
1029 | if (!iph->saddr) |
1030 | return -EINVAL; | |
1031 | dev->flags = IFF_BROADCAST; | |
3b04ddde | 1032 | dev->header_ops = &ipgre_header_ops; |
1da177e4 LT |
1033 | } |
1034 | #endif | |
a64b04d8 | 1035 | } else if (!tunnel->collect_md) { |
6a5f44d7 | 1036 | dev->header_ops = &ipgre_header_ops; |
a64b04d8 | 1037 | } |
1da177e4 | 1038 | |
c5441932 | 1039 | return ip_tunnel_init(dev); |
1da177e4 LT |
1040 | } |
1041 | ||
9f57c67c PS |
1042 | static const struct gre_protocol ipgre_protocol = { |
1043 | .handler = gre_rcv, | |
1044 | .err_handler = gre_err, | |
1da177e4 LT |
1045 | }; |
1046 | ||
2c8c1e72 | 1047 | static int __net_init ipgre_init_net(struct net *net) |
59a4c759 | 1048 | { |
c5441932 | 1049 | return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL); |
59a4c759 PE |
1050 | } |
1051 | ||
64bc1781 | 1052 | static void __net_exit ipgre_exit_batch_net(struct list_head *list_net) |
59a4c759 | 1053 | { |
64bc1781 | 1054 | ip_tunnel_delete_nets(list_net, ipgre_net_id, &ipgre_link_ops); |
59a4c759 PE |
1055 | } |
1056 | ||
1057 | static struct pernet_operations ipgre_net_ops = { | |
1058 | .init = ipgre_init_net, | |
64bc1781 | 1059 | .exit_batch = ipgre_exit_batch_net, |
cfb8fbf2 | 1060 | .id = &ipgre_net_id, |
c5441932 | 1061 | .size = sizeof(struct ip_tunnel_net), |
59a4c759 | 1062 | }; |
1da177e4 | 1063 | |
a8b8a889 MS |
1064 | static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[], |
1065 | struct netlink_ext_ack *extack) | |
c19e654d HX |
1066 | { |
1067 | __be16 flags; | |
1068 | ||
1069 | if (!data) | |
1070 | return 0; | |
1071 | ||
1072 | flags = 0; | |
1073 | if (data[IFLA_GRE_IFLAGS]) | |
1074 | flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); | |
1075 | if (data[IFLA_GRE_OFLAGS]) | |
1076 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); | |
1077 | if (flags & (GRE_VERSION|GRE_ROUTING)) | |
1078 | return -EINVAL; | |
1079 | ||
946b636f JB |
1080 | if (data[IFLA_GRE_COLLECT_METADATA] && |
1081 | data[IFLA_GRE_ENCAP_TYPE] && | |
1082 | nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE) | |
1083 | return -EINVAL; | |
1084 | ||
c19e654d HX |
1085 | return 0; |
1086 | } | |
1087 | ||
a8b8a889 MS |
1088 | static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[], |
1089 | struct netlink_ext_ack *extack) | |
e1a80002 HX |
1090 | { |
1091 | __be32 daddr; | |
1092 | ||
1093 | if (tb[IFLA_ADDRESS]) { | |
1094 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
1095 | return -EINVAL; | |
1096 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
1097 | return -EADDRNOTAVAIL; | |
1098 | } | |
1099 | ||
1100 | if (!data) | |
1101 | goto out; | |
1102 | ||
1103 | if (data[IFLA_GRE_REMOTE]) { | |
1104 | memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4); | |
1105 | if (!daddr) | |
1106 | return -EINVAL; | |
1107 | } | |
1108 | ||
1109 | out: | |
a8b8a889 | 1110 | return ipgre_tunnel_validate(tb, data, extack); |
e1a80002 HX |
1111 | } |
1112 | ||
84e54fe0 WT |
1113 | static int erspan_validate(struct nlattr *tb[], struct nlattr *data[], |
1114 | struct netlink_ext_ack *extack) | |
1115 | { | |
1116 | __be16 flags = 0; | |
1117 | int ret; | |
1118 | ||
1119 | if (!data) | |
1120 | return 0; | |
1121 | ||
1122 | ret = ipgre_tap_validate(tb, data, extack); | |
1123 | if (ret) | |
1124 | return ret; | |
1125 | ||
1126 | /* ERSPAN should only have GRE sequence and key flag */ | |
1a66a836 WT |
1127 | if (data[IFLA_GRE_OFLAGS]) |
1128 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); | |
1129 | if (data[IFLA_GRE_IFLAGS]) | |
1130 | flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); | |
1131 | if (!data[IFLA_GRE_COLLECT_METADATA] && | |
1132 | flags != (GRE_SEQ | GRE_KEY)) | |
84e54fe0 WT |
1133 | return -EINVAL; |
1134 | ||
1135 | /* ERSPAN Session ID only has 10-bit. Since we reuse | |
1136 | * 32-bit key field as ID, check it's range. | |
1137 | */ | |
1138 | if (data[IFLA_GRE_IKEY] && | |
1139 | (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK)) | |
1140 | return -EINVAL; | |
1141 | ||
1142 | if (data[IFLA_GRE_OKEY] && | |
1143 | (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK)) | |
1144 | return -EINVAL; | |
1145 | ||
1146 | return 0; | |
1147 | } | |
1148 | ||
22a59be8 | 1149 | static int ipgre_netlink_parms(struct net_device *dev, |
2e15ea39 PS |
1150 | struct nlattr *data[], |
1151 | struct nlattr *tb[], | |
9830ad4c CG |
1152 | struct ip_tunnel_parm *parms, |
1153 | __u32 *fwmark) | |
c19e654d | 1154 | { |
22a59be8 PP |
1155 | struct ip_tunnel *t = netdev_priv(dev); |
1156 | ||
7bb82d92 | 1157 | memset(parms, 0, sizeof(*parms)); |
c19e654d HX |
1158 | |
1159 | parms->iph.protocol = IPPROTO_GRE; | |
1160 | ||
1161 | if (!data) | |
22a59be8 | 1162 | return 0; |
c19e654d HX |
1163 | |
1164 | if (data[IFLA_GRE_LINK]) | |
1165 | parms->link = nla_get_u32(data[IFLA_GRE_LINK]); | |
1166 | ||
1167 | if (data[IFLA_GRE_IFLAGS]) | |
c5441932 | 1168 | parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS])); |
c19e654d HX |
1169 | |
1170 | if (data[IFLA_GRE_OFLAGS]) | |
c5441932 | 1171 | parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS])); |
c19e654d HX |
1172 | |
1173 | if (data[IFLA_GRE_IKEY]) | |
1174 | parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); | |
1175 | ||
1176 | if (data[IFLA_GRE_OKEY]) | |
1177 | parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); | |
1178 | ||
1179 | if (data[IFLA_GRE_LOCAL]) | |
67b61f6c | 1180 | parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]); |
c19e654d HX |
1181 | |
1182 | if (data[IFLA_GRE_REMOTE]) | |
67b61f6c | 1183 | parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]); |
c19e654d HX |
1184 | |
1185 | if (data[IFLA_GRE_TTL]) | |
1186 | parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]); | |
1187 | ||
1188 | if (data[IFLA_GRE_TOS]) | |
1189 | parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]); | |
1190 | ||
22a59be8 PP |
1191 | if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC])) { |
1192 | if (t->ignore_df) | |
1193 | return -EINVAL; | |
c19e654d | 1194 | parms->iph.frag_off = htons(IP_DF); |
22a59be8 | 1195 | } |
2e15ea39 PS |
1196 | |
1197 | if (data[IFLA_GRE_COLLECT_METADATA]) { | |
2e15ea39 | 1198 | t->collect_md = true; |
e271c7b4 JB |
1199 | if (dev->type == ARPHRD_IPGRE) |
1200 | dev->type = ARPHRD_NONE; | |
2e15ea39 | 1201 | } |
22a59be8 PP |
1202 | |
1203 | if (data[IFLA_GRE_IGNORE_DF]) { | |
1204 | if (nla_get_u8(data[IFLA_GRE_IGNORE_DF]) | |
1205 | && (parms->iph.frag_off & htons(IP_DF))) | |
1206 | return -EINVAL; | |
1207 | t->ignore_df = !!nla_get_u8(data[IFLA_GRE_IGNORE_DF]); | |
1208 | } | |
1209 | ||
9830ad4c CG |
1210 | if (data[IFLA_GRE_FWMARK]) |
1211 | *fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]); | |
1212 | ||
f551c91d WT |
1213 | if (data[IFLA_GRE_ERSPAN_VER]) { |
1214 | t->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]); | |
84e54fe0 | 1215 | |
f551c91d | 1216 | if (t->erspan_ver != 1 && t->erspan_ver != 2) |
84e54fe0 WT |
1217 | return -EINVAL; |
1218 | } | |
1219 | ||
f551c91d WT |
1220 | if (t->erspan_ver == 1) { |
1221 | if (data[IFLA_GRE_ERSPAN_INDEX]) { | |
1222 | t->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]); | |
1223 | if (t->index & ~INDEX_MASK) | |
1224 | return -EINVAL; | |
1225 | } | |
1226 | } else if (t->erspan_ver == 2) { | |
1227 | if (data[IFLA_GRE_ERSPAN_DIR]) { | |
1228 | t->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]); | |
1229 | if (t->dir & ~(DIR_MASK >> DIR_OFFSET)) | |
1230 | return -EINVAL; | |
1231 | } | |
1232 | if (data[IFLA_GRE_ERSPAN_HWID]) { | |
1233 | t->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]); | |
1234 | if (t->hwid & ~(HWID_MASK >> HWID_OFFSET)) | |
1235 | return -EINVAL; | |
1236 | } | |
1237 | } | |
1238 | ||
22a59be8 | 1239 | return 0; |
c19e654d HX |
1240 | } |
1241 | ||
4565e991 TH |
1242 | /* This function returns true when ENCAP attributes are present in the nl msg */ |
1243 | static bool ipgre_netlink_encap_parms(struct nlattr *data[], | |
1244 | struct ip_tunnel_encap *ipencap) | |
1245 | { | |
1246 | bool ret = false; | |
1247 | ||
1248 | memset(ipencap, 0, sizeof(*ipencap)); | |
1249 | ||
1250 | if (!data) | |
1251 | return ret; | |
1252 | ||
1253 | if (data[IFLA_GRE_ENCAP_TYPE]) { | |
1254 | ret = true; | |
1255 | ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]); | |
1256 | } | |
1257 | ||
1258 | if (data[IFLA_GRE_ENCAP_FLAGS]) { | |
1259 | ret = true; | |
1260 | ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]); | |
1261 | } | |
1262 | ||
1263 | if (data[IFLA_GRE_ENCAP_SPORT]) { | |
1264 | ret = true; | |
3e97fa70 | 1265 | ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]); |
4565e991 TH |
1266 | } |
1267 | ||
1268 | if (data[IFLA_GRE_ENCAP_DPORT]) { | |
1269 | ret = true; | |
3e97fa70 | 1270 | ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]); |
4565e991 TH |
1271 | } |
1272 | ||
1273 | return ret; | |
1274 | } | |
1275 | ||
c5441932 | 1276 | static int gre_tap_init(struct net_device *dev) |
e1a80002 | 1277 | { |
c5441932 | 1278 | __gre_tunnel_init(dev); |
bec94d43 | 1279 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
d51711c0 | 1280 | netif_keep_dst(dev); |
e1a80002 | 1281 | |
c5441932 | 1282 | return ip_tunnel_init(dev); |
e1a80002 HX |
1283 | } |
1284 | ||
c5441932 PS |
1285 | static const struct net_device_ops gre_tap_netdev_ops = { |
1286 | .ndo_init = gre_tap_init, | |
1287 | .ndo_uninit = ip_tunnel_uninit, | |
1288 | .ndo_start_xmit = gre_tap_xmit, | |
b8c26a33 SH |
1289 | .ndo_set_mac_address = eth_mac_addr, |
1290 | .ndo_validate_addr = eth_validate_addr, | |
c5441932 PS |
1291 | .ndo_change_mtu = ip_tunnel_change_mtu, |
1292 | .ndo_get_stats64 = ip_tunnel_get_stats64, | |
1e99584b | 1293 | .ndo_get_iflink = ip_tunnel_get_iflink, |
fc4099f1 | 1294 | .ndo_fill_metadata_dst = gre_fill_metadata_dst, |
b8c26a33 SH |
1295 | }; |
1296 | ||
84e54fe0 WT |
1297 | static int erspan_tunnel_init(struct net_device *dev) |
1298 | { | |
1299 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
1300 | int t_hlen; | |
1301 | ||
1302 | tunnel->tun_hlen = 8; | |
1303 | tunnel->parms.iph.protocol = IPPROTO_GRE; | |
c122fda2 | 1304 | tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen + |
f551c91d | 1305 | erspan_hdr_len(tunnel->erspan_ver); |
c122fda2 | 1306 | t_hlen = tunnel->hlen + sizeof(struct iphdr); |
84e54fe0 WT |
1307 | |
1308 | dev->needed_headroom = LL_MAX_HEADER + t_hlen + 4; | |
1309 | dev->mtu = ETH_DATA_LEN - t_hlen - 4; | |
1310 | dev->features |= GRE_FEATURES; | |
1311 | dev->hw_features |= GRE_FEATURES; | |
1312 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; | |
c84bed44 | 1313 | netif_keep_dst(dev); |
84e54fe0 WT |
1314 | |
1315 | return ip_tunnel_init(dev); | |
1316 | } | |
1317 | ||
1318 | static const struct net_device_ops erspan_netdev_ops = { | |
1319 | .ndo_init = erspan_tunnel_init, | |
1320 | .ndo_uninit = ip_tunnel_uninit, | |
1321 | .ndo_start_xmit = erspan_xmit, | |
1322 | .ndo_set_mac_address = eth_mac_addr, | |
1323 | .ndo_validate_addr = eth_validate_addr, | |
1324 | .ndo_change_mtu = ip_tunnel_change_mtu, | |
1325 | .ndo_get_stats64 = ip_tunnel_get_stats64, | |
1326 | .ndo_get_iflink = ip_tunnel_get_iflink, | |
1327 | .ndo_fill_metadata_dst = gre_fill_metadata_dst, | |
1328 | }; | |
1329 | ||
e1a80002 HX |
1330 | static void ipgre_tap_setup(struct net_device *dev) |
1331 | { | |
e1a80002 | 1332 | ether_setup(dev); |
d13b161c JB |
1333 | dev->netdev_ops = &gre_tap_netdev_ops; |
1334 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; | |
1335 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; | |
c5441932 | 1336 | ip_tunnel_setup(dev, gre_tap_net_id); |
e1a80002 HX |
1337 | } |
1338 | ||
c5441932 | 1339 | static int ipgre_newlink(struct net *src_net, struct net_device *dev, |
7a3f4a18 MS |
1340 | struct nlattr *tb[], struct nlattr *data[], |
1341 | struct netlink_ext_ack *extack) | |
c19e654d | 1342 | { |
c5441932 | 1343 | struct ip_tunnel_parm p; |
4565e991 | 1344 | struct ip_tunnel_encap ipencap; |
9830ad4c | 1345 | __u32 fwmark = 0; |
22a59be8 | 1346 | int err; |
4565e991 TH |
1347 | |
1348 | if (ipgre_netlink_encap_parms(data, &ipencap)) { | |
1349 | struct ip_tunnel *t = netdev_priv(dev); | |
22a59be8 | 1350 | err = ip_tunnel_encap_setup(t, &ipencap); |
4565e991 TH |
1351 | |
1352 | if (err < 0) | |
1353 | return err; | |
1354 | } | |
c19e654d | 1355 | |
9830ad4c | 1356 | err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark); |
22a59be8 PP |
1357 | if (err < 0) |
1358 | return err; | |
9830ad4c | 1359 | return ip_tunnel_newlink(dev, tb, &p, fwmark); |
c19e654d HX |
1360 | } |
1361 | ||
1362 | static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[], | |
ad744b22 MS |
1363 | struct nlattr *data[], |
1364 | struct netlink_ext_ack *extack) | |
c19e654d | 1365 | { |
9830ad4c | 1366 | struct ip_tunnel *t = netdev_priv(dev); |
4565e991 | 1367 | struct ip_tunnel_encap ipencap; |
9830ad4c | 1368 | __u32 fwmark = t->fwmark; |
dd9d598c | 1369 | struct ip_tunnel_parm p; |
22a59be8 | 1370 | int err; |
4565e991 TH |
1371 | |
1372 | if (ipgre_netlink_encap_parms(data, &ipencap)) { | |
22a59be8 | 1373 | err = ip_tunnel_encap_setup(t, &ipencap); |
4565e991 TH |
1374 | |
1375 | if (err < 0) | |
1376 | return err; | |
1377 | } | |
c19e654d | 1378 | |
9830ad4c | 1379 | err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark); |
22a59be8 PP |
1380 | if (err < 0) |
1381 | return err; | |
dd9d598c XL |
1382 | |
1383 | err = ip_tunnel_changelink(dev, tb, &p, fwmark); | |
1384 | if (err < 0) | |
1385 | return err; | |
1386 | ||
1387 | t->parms.i_flags = p.i_flags; | |
1388 | t->parms.o_flags = p.o_flags; | |
1389 | ||
1390 | if (strcmp(dev->rtnl_link_ops->kind, "erspan")) | |
1391 | ipgre_link_update(dev, !tb[IFLA_MTU]); | |
1392 | ||
1393 | return 0; | |
c19e654d HX |
1394 | } |
1395 | ||
1396 | static size_t ipgre_get_size(const struct net_device *dev) | |
1397 | { | |
1398 | return | |
1399 | /* IFLA_GRE_LINK */ | |
1400 | nla_total_size(4) + | |
1401 | /* IFLA_GRE_IFLAGS */ | |
1402 | nla_total_size(2) + | |
1403 | /* IFLA_GRE_OFLAGS */ | |
1404 | nla_total_size(2) + | |
1405 | /* IFLA_GRE_IKEY */ | |
1406 | nla_total_size(4) + | |
1407 | /* IFLA_GRE_OKEY */ | |
1408 | nla_total_size(4) + | |
1409 | /* IFLA_GRE_LOCAL */ | |
1410 | nla_total_size(4) + | |
1411 | /* IFLA_GRE_REMOTE */ | |
1412 | nla_total_size(4) + | |
1413 | /* IFLA_GRE_TTL */ | |
1414 | nla_total_size(1) + | |
1415 | /* IFLA_GRE_TOS */ | |
1416 | nla_total_size(1) + | |
1417 | /* IFLA_GRE_PMTUDISC */ | |
1418 | nla_total_size(1) + | |
4565e991 TH |
1419 | /* IFLA_GRE_ENCAP_TYPE */ |
1420 | nla_total_size(2) + | |
1421 | /* IFLA_GRE_ENCAP_FLAGS */ | |
1422 | nla_total_size(2) + | |
1423 | /* IFLA_GRE_ENCAP_SPORT */ | |
1424 | nla_total_size(2) + | |
1425 | /* IFLA_GRE_ENCAP_DPORT */ | |
1426 | nla_total_size(2) + | |
2e15ea39 PS |
1427 | /* IFLA_GRE_COLLECT_METADATA */ |
1428 | nla_total_size(0) + | |
22a59be8 PP |
1429 | /* IFLA_GRE_IGNORE_DF */ |
1430 | nla_total_size(1) + | |
9830ad4c CG |
1431 | /* IFLA_GRE_FWMARK */ |
1432 | nla_total_size(4) + | |
84e54fe0 WT |
1433 | /* IFLA_GRE_ERSPAN_INDEX */ |
1434 | nla_total_size(4) + | |
f551c91d WT |
1435 | /* IFLA_GRE_ERSPAN_VER */ |
1436 | nla_total_size(1) + | |
1437 | /* IFLA_GRE_ERSPAN_DIR */ | |
1438 | nla_total_size(1) + | |
1439 | /* IFLA_GRE_ERSPAN_HWID */ | |
1440 | nla_total_size(2) + | |
c19e654d HX |
1441 | 0; |
1442 | } | |
1443 | ||
1444 | static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
1445 | { | |
1446 | struct ip_tunnel *t = netdev_priv(dev); | |
1447 | struct ip_tunnel_parm *p = &t->parms; | |
1448 | ||
f3756b79 | 1449 | if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || |
95f5c64c TH |
1450 | nla_put_be16(skb, IFLA_GRE_IFLAGS, |
1451 | gre_tnl_flags_to_gre_flags(p->i_flags)) || | |
1452 | nla_put_be16(skb, IFLA_GRE_OFLAGS, | |
1453 | gre_tnl_flags_to_gre_flags(p->o_flags)) || | |
f3756b79 DM |
1454 | nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || |
1455 | nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || | |
930345ea JB |
1456 | nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) || |
1457 | nla_put_in_addr(skb, IFLA_GRE_REMOTE, p->iph.daddr) || | |
f3756b79 DM |
1458 | nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) || |
1459 | nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) || | |
1460 | nla_put_u8(skb, IFLA_GRE_PMTUDISC, | |
9830ad4c CG |
1461 | !!(p->iph.frag_off & htons(IP_DF))) || |
1462 | nla_put_u32(skb, IFLA_GRE_FWMARK, t->fwmark)) | |
f3756b79 | 1463 | goto nla_put_failure; |
4565e991 TH |
1464 | |
1465 | if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE, | |
1466 | t->encap.type) || | |
3e97fa70 SD |
1467 | nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT, |
1468 | t->encap.sport) || | |
1469 | nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT, | |
1470 | t->encap.dport) || | |
4565e991 | 1471 | nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS, |
e1b2cb65 | 1472 | t->encap.flags)) |
4565e991 TH |
1473 | goto nla_put_failure; |
1474 | ||
22a59be8 PP |
1475 | if (nla_put_u8(skb, IFLA_GRE_IGNORE_DF, t->ignore_df)) |
1476 | goto nla_put_failure; | |
1477 | ||
2e15ea39 PS |
1478 | if (t->collect_md) { |
1479 | if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA)) | |
1480 | goto nla_put_failure; | |
1481 | } | |
1482 | ||
f551c91d WT |
1483 | if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, t->erspan_ver)) |
1484 | goto nla_put_failure; | |
1485 | ||
1486 | if (t->erspan_ver == 1) { | |
84e54fe0 WT |
1487 | if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, t->index)) |
1488 | goto nla_put_failure; | |
f551c91d WT |
1489 | } else if (t->erspan_ver == 2) { |
1490 | if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, t->dir)) | |
1491 | goto nla_put_failure; | |
1492 | if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, t->hwid)) | |
1493 | goto nla_put_failure; | |
1494 | } | |
84e54fe0 | 1495 | |
c19e654d HX |
1496 | return 0; |
1497 | ||
1498 | nla_put_failure: | |
1499 | return -EMSGSIZE; | |
1500 | } | |
1501 | ||
84e54fe0 WT |
1502 | static void erspan_setup(struct net_device *dev) |
1503 | { | |
1504 | ether_setup(dev); | |
1505 | dev->netdev_ops = &erspan_netdev_ops; | |
1506 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; | |
1507 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; | |
1508 | ip_tunnel_setup(dev, erspan_net_id); | |
1509 | } | |
1510 | ||
c19e654d HX |
1511 | static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = { |
1512 | [IFLA_GRE_LINK] = { .type = NLA_U32 }, | |
1513 | [IFLA_GRE_IFLAGS] = { .type = NLA_U16 }, | |
1514 | [IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, | |
1515 | [IFLA_GRE_IKEY] = { .type = NLA_U32 }, | |
1516 | [IFLA_GRE_OKEY] = { .type = NLA_U32 }, | |
4d74f8ba PM |
1517 | [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, |
1518 | [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, | |
c19e654d HX |
1519 | [IFLA_GRE_TTL] = { .type = NLA_U8 }, |
1520 | [IFLA_GRE_TOS] = { .type = NLA_U8 }, | |
1521 | [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 }, | |
4565e991 TH |
1522 | [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 }, |
1523 | [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 }, | |
1524 | [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 }, | |
1525 | [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 }, | |
2e15ea39 | 1526 | [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG }, |
22a59be8 | 1527 | [IFLA_GRE_IGNORE_DF] = { .type = NLA_U8 }, |
9830ad4c | 1528 | [IFLA_GRE_FWMARK] = { .type = NLA_U32 }, |
84e54fe0 | 1529 | [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 }, |
f551c91d WT |
1530 | [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 }, |
1531 | [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 }, | |
1532 | [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 }, | |
c19e654d HX |
1533 | }; |
1534 | ||
1535 | static struct rtnl_link_ops ipgre_link_ops __read_mostly = { | |
1536 | .kind = "gre", | |
1537 | .maxtype = IFLA_GRE_MAX, | |
1538 | .policy = ipgre_policy, | |
1539 | .priv_size = sizeof(struct ip_tunnel), | |
1540 | .setup = ipgre_tunnel_setup, | |
1541 | .validate = ipgre_tunnel_validate, | |
1542 | .newlink = ipgre_newlink, | |
1543 | .changelink = ipgre_changelink, | |
c5441932 | 1544 | .dellink = ip_tunnel_dellink, |
c19e654d HX |
1545 | .get_size = ipgre_get_size, |
1546 | .fill_info = ipgre_fill_info, | |
1728d4fa | 1547 | .get_link_net = ip_tunnel_get_link_net, |
c19e654d HX |
1548 | }; |
1549 | ||
e1a80002 HX |
1550 | static struct rtnl_link_ops ipgre_tap_ops __read_mostly = { |
1551 | .kind = "gretap", | |
1552 | .maxtype = IFLA_GRE_MAX, | |
1553 | .policy = ipgre_policy, | |
1554 | .priv_size = sizeof(struct ip_tunnel), | |
1555 | .setup = ipgre_tap_setup, | |
1556 | .validate = ipgre_tap_validate, | |
1557 | .newlink = ipgre_newlink, | |
1558 | .changelink = ipgre_changelink, | |
c5441932 | 1559 | .dellink = ip_tunnel_dellink, |
e1a80002 HX |
1560 | .get_size = ipgre_get_size, |
1561 | .fill_info = ipgre_fill_info, | |
1728d4fa | 1562 | .get_link_net = ip_tunnel_get_link_net, |
e1a80002 HX |
1563 | }; |
1564 | ||
84e54fe0 WT |
1565 | static struct rtnl_link_ops erspan_link_ops __read_mostly = { |
1566 | .kind = "erspan", | |
1567 | .maxtype = IFLA_GRE_MAX, | |
1568 | .policy = ipgre_policy, | |
1569 | .priv_size = sizeof(struct ip_tunnel), | |
1570 | .setup = erspan_setup, | |
1571 | .validate = erspan_validate, | |
1572 | .newlink = ipgre_newlink, | |
1573 | .changelink = ipgre_changelink, | |
1574 | .dellink = ip_tunnel_dellink, | |
1575 | .get_size = ipgre_get_size, | |
1576 | .fill_info = ipgre_fill_info, | |
1577 | .get_link_net = ip_tunnel_get_link_net, | |
1578 | }; | |
1579 | ||
b2acd1dc PS |
1580 | struct net_device *gretap_fb_dev_create(struct net *net, const char *name, |
1581 | u8 name_assign_type) | |
1582 | { | |
1583 | struct nlattr *tb[IFLA_MAX + 1]; | |
1584 | struct net_device *dev; | |
106da663 | 1585 | LIST_HEAD(list_kill); |
b2acd1dc PS |
1586 | struct ip_tunnel *t; |
1587 | int err; | |
1588 | ||
1589 | memset(&tb, 0, sizeof(tb)); | |
1590 | ||
1591 | dev = rtnl_create_link(net, name, name_assign_type, | |
1592 | &ipgre_tap_ops, tb); | |
1593 | if (IS_ERR(dev)) | |
1594 | return dev; | |
1595 | ||
1596 | /* Configure flow based GRE device. */ | |
1597 | t = netdev_priv(dev); | |
1598 | t->collect_md = true; | |
1599 | ||
7a3f4a18 | 1600 | err = ipgre_newlink(net, dev, tb, NULL, NULL); |
106da663 ND |
1601 | if (err < 0) { |
1602 | free_netdev(dev); | |
1603 | return ERR_PTR(err); | |
1604 | } | |
7e059158 DW |
1605 | |
1606 | /* openvswitch users expect packet sizes to be unrestricted, | |
1607 | * so set the largest MTU we can. | |
1608 | */ | |
1609 | err = __ip_tunnel_change_mtu(dev, IP_MAX_MTU, false); | |
1610 | if (err) | |
1611 | goto out; | |
1612 | ||
da6f1da8 ND |
1613 | err = rtnl_configure_link(dev, NULL); |
1614 | if (err < 0) | |
1615 | goto out; | |
1616 | ||
b2acd1dc PS |
1617 | return dev; |
1618 | out: | |
106da663 ND |
1619 | ip_tunnel_dellink(dev, &list_kill); |
1620 | unregister_netdevice_many(&list_kill); | |
b2acd1dc PS |
1621 | return ERR_PTR(err); |
1622 | } | |
1623 | EXPORT_SYMBOL_GPL(gretap_fb_dev_create); | |
1624 | ||
c5441932 PS |
1625 | static int __net_init ipgre_tap_init_net(struct net *net) |
1626 | { | |
2e15ea39 | 1627 | return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, "gretap0"); |
c5441932 PS |
1628 | } |
1629 | ||
64bc1781 | 1630 | static void __net_exit ipgre_tap_exit_batch_net(struct list_head *list_net) |
c5441932 | 1631 | { |
64bc1781 | 1632 | ip_tunnel_delete_nets(list_net, gre_tap_net_id, &ipgre_tap_ops); |
c5441932 PS |
1633 | } |
1634 | ||
1635 | static struct pernet_operations ipgre_tap_net_ops = { | |
1636 | .init = ipgre_tap_init_net, | |
64bc1781 | 1637 | .exit_batch = ipgre_tap_exit_batch_net, |
c5441932 PS |
1638 | .id = &gre_tap_net_id, |
1639 | .size = sizeof(struct ip_tunnel_net), | |
1640 | }; | |
1da177e4 | 1641 | |
84e54fe0 WT |
1642 | static int __net_init erspan_init_net(struct net *net) |
1643 | { | |
1644 | return ip_tunnel_init_net(net, erspan_net_id, | |
1645 | &erspan_link_ops, "erspan0"); | |
1646 | } | |
1647 | ||
64bc1781 | 1648 | static void __net_exit erspan_exit_batch_net(struct list_head *net_list) |
84e54fe0 | 1649 | { |
64bc1781 | 1650 | ip_tunnel_delete_nets(net_list, erspan_net_id, &erspan_link_ops); |
84e54fe0 WT |
1651 | } |
1652 | ||
1653 | static struct pernet_operations erspan_net_ops = { | |
1654 | .init = erspan_init_net, | |
64bc1781 | 1655 | .exit_batch = erspan_exit_batch_net, |
84e54fe0 WT |
1656 | .id = &erspan_net_id, |
1657 | .size = sizeof(struct ip_tunnel_net), | |
1658 | }; | |
1659 | ||
1da177e4 LT |
1660 | static int __init ipgre_init(void) |
1661 | { | |
1662 | int err; | |
1663 | ||
058bd4d2 | 1664 | pr_info("GRE over IPv4 tunneling driver\n"); |
1da177e4 | 1665 | |
cfb8fbf2 | 1666 | err = register_pernet_device(&ipgre_net_ops); |
59a4c759 | 1667 | if (err < 0) |
c2892f02 AD |
1668 | return err; |
1669 | ||
c5441932 PS |
1670 | err = register_pernet_device(&ipgre_tap_net_ops); |
1671 | if (err < 0) | |
e3d0328c | 1672 | goto pnet_tap_failed; |
c5441932 | 1673 | |
84e54fe0 WT |
1674 | err = register_pernet_device(&erspan_net_ops); |
1675 | if (err < 0) | |
1676 | goto pnet_erspan_failed; | |
1677 | ||
9f57c67c | 1678 | err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO); |
c2892f02 | 1679 | if (err < 0) { |
058bd4d2 | 1680 | pr_info("%s: can't add protocol\n", __func__); |
c2892f02 AD |
1681 | goto add_proto_failed; |
1682 | } | |
7daa0004 | 1683 | |
c19e654d HX |
1684 | err = rtnl_link_register(&ipgre_link_ops); |
1685 | if (err < 0) | |
1686 | goto rtnl_link_failed; | |
1687 | ||
e1a80002 HX |
1688 | err = rtnl_link_register(&ipgre_tap_ops); |
1689 | if (err < 0) | |
1690 | goto tap_ops_failed; | |
1691 | ||
84e54fe0 WT |
1692 | err = rtnl_link_register(&erspan_link_ops); |
1693 | if (err < 0) | |
1694 | goto erspan_link_failed; | |
1695 | ||
c5441932 | 1696 | return 0; |
c19e654d | 1697 | |
84e54fe0 WT |
1698 | erspan_link_failed: |
1699 | rtnl_link_unregister(&ipgre_tap_ops); | |
e1a80002 HX |
1700 | tap_ops_failed: |
1701 | rtnl_link_unregister(&ipgre_link_ops); | |
c19e654d | 1702 | rtnl_link_failed: |
9f57c67c | 1703 | gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO); |
c2892f02 | 1704 | add_proto_failed: |
84e54fe0 WT |
1705 | unregister_pernet_device(&erspan_net_ops); |
1706 | pnet_erspan_failed: | |
c5441932 | 1707 | unregister_pernet_device(&ipgre_tap_net_ops); |
e3d0328c | 1708 | pnet_tap_failed: |
c2892f02 | 1709 | unregister_pernet_device(&ipgre_net_ops); |
c5441932 | 1710 | return err; |
1da177e4 LT |
1711 | } |
1712 | ||
db44575f | 1713 | static void __exit ipgre_fini(void) |
1da177e4 | 1714 | { |
e1a80002 | 1715 | rtnl_link_unregister(&ipgre_tap_ops); |
c19e654d | 1716 | rtnl_link_unregister(&ipgre_link_ops); |
84e54fe0 | 1717 | rtnl_link_unregister(&erspan_link_ops); |
9f57c67c | 1718 | gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO); |
c5441932 | 1719 | unregister_pernet_device(&ipgre_tap_net_ops); |
c2892f02 | 1720 | unregister_pernet_device(&ipgre_net_ops); |
84e54fe0 | 1721 | unregister_pernet_device(&erspan_net_ops); |
1da177e4 LT |
1722 | } |
1723 | ||
1724 | module_init(ipgre_init); | |
1725 | module_exit(ipgre_fini); | |
1726 | MODULE_LICENSE("GPL"); | |
4d74f8ba PM |
1727 | MODULE_ALIAS_RTNL_LINK("gre"); |
1728 | MODULE_ALIAS_RTNL_LINK("gretap"); | |
84e54fe0 | 1729 | MODULE_ALIAS_RTNL_LINK("erspan"); |
8909c9ad | 1730 | MODULE_ALIAS_NETDEV("gre0"); |
c5441932 | 1731 | MODULE_ALIAS_NETDEV("gretap0"); |
84e54fe0 | 1732 | MODULE_ALIAS_NETDEV("erspan0"); |