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