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