]>
Commit | Line | Data |
---|---|---|
e23775f2 PS |
1 | /* |
2 | * Linux NET3: GRE over IP protocol decoder. | |
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 | ||
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
14 | ||
b66081f3 | 15 | #ifndef USE_UPSTREAM_TUNNEL |
e23775f2 PS |
16 | #include <linux/capability.h> |
17 | #include <linux/module.h> | |
18 | #include <linux/types.h> | |
19 | #include <linux/kernel.h> | |
20 | #include <linux/kconfig.h> | |
21 | #include <linux/slab.h> | |
436d36db | 22 | #include <linux/uaccess.h> |
e23775f2 PS |
23 | #include <linux/skbuff.h> |
24 | #include <linux/netdevice.h> | |
25 | #include <linux/netdev_features.h> | |
26 | #include <linux/in.h> | |
27 | #include <linux/tcp.h> | |
28 | #include <linux/udp.h> | |
29 | #include <linux/if_arp.h> | |
30 | #include <linux/mroute.h> | |
31 | #include <linux/if_vlan.h> | |
32 | #include <linux/init.h> | |
33 | #include <linux/in6.h> | |
34 | #include <linux/inetdevice.h> | |
35 | #include <linux/igmp.h> | |
36 | #include <linux/netfilter_ipv4.h> | |
37 | #include <linux/etherdevice.h> | |
38 | #include <linux/if_ether.h> | |
39 | ||
40 | #include <net/sock.h> | |
41 | #include <net/ip.h> | |
42 | #include <net/icmp.h> | |
43 | #include <net/protocol.h> | |
44 | #include <net/ip_tunnels.h> | |
45 | #include <net/arp.h> | |
46 | #include <net/checksum.h> | |
47 | #include <net/dsfield.h> | |
48 | #include <net/inet_ecn.h> | |
49 | #include <net/xfrm.h> | |
50 | #include <net/net_namespace.h> | |
51 | #include <net/netns/generic.h> | |
52 | #include <net/rtnetlink.h> | |
53 | #include <net/gre.h> | |
54 | #include <net/dst_metadata.h> | |
8e53509c | 55 | #include <net/erspan.h> |
e23775f2 | 56 | |
e23775f2 PS |
57 | #if IS_ENABLED(CONFIG_IPV6) |
58 | #include <net/ipv6.h> | |
59 | #include <net/ip6_fib.h> | |
60 | #include <net/ip6_route.h> | |
61 | #endif | |
62 | ||
63 | #include "gso.h" | |
64 | #include "vport-netdev.h" | |
65 | ||
66 | static int gre_tap_net_id __read_mostly; | |
8e53509c | 67 | static unsigned int erspan_net_id __read_mostly; |
29a29457 WT |
68 | static void erspan_build_header(struct sk_buff *skb, |
69 | __be32 id, u32 index, | |
70 | bool truncate, bool is_ipv4); | |
8e53509c | 71 | |
8de03c61 | 72 | static bool ip_gre_loaded = false; |
e23775f2 PS |
73 | |
74 | #define ip_gre_calc_hlen rpl_ip_gre_calc_hlen | |
75 | static int ip_gre_calc_hlen(__be16 o_flags) | |
76 | { | |
77 | int addend = 4; | |
78 | ||
79 | if (o_flags & TUNNEL_CSUM) | |
80 | addend += 4; | |
81 | if (o_flags & TUNNEL_KEY) | |
82 | addend += 4; | |
83 | if (o_flags & TUNNEL_SEQ) | |
84 | addend += 4; | |
85 | return addend; | |
86 | } | |
87 | ||
8e53509c WT |
88 | /* Returns the least-significant 32 bits of a __be64. */ |
89 | static __be32 tunnel_id_to_key(__be64 x) | |
e23775f2 | 90 | { |
8e53509c WT |
91 | #ifdef __BIG_ENDIAN |
92 | return (__force __be32)x; | |
93 | #else | |
94 | return (__force __be32)((__force u64)x >> 32); | |
95 | #endif | |
96 | } | |
e23775f2 | 97 | |
8e53509c WT |
98 | static struct dst_ops md_dst_ops = { |
99 | .family = AF_UNSPEC, | |
100 | }; | |
101 | ||
102 | #ifndef DST_METADATA | |
103 | #define DST_METADATA 0x0080 | |
104 | #endif | |
105 | ||
106 | static void rpl__metadata_dst_init(struct metadata_dst *md_dst, | |
107 | enum metadata_type type, u8 optslen) | |
108 | ||
e23775f2 | 109 | { |
8e53509c WT |
110 | struct dst_entry *dst; |
111 | ||
112 | dst = &md_dst->dst; | |
113 | dst_init(dst, &md_dst_ops, NULL, 1, DST_OBSOLETE_NONE, | |
114 | DST_METADATA | DST_NOCOUNT); | |
115 | ||
116 | #if 0 | |
117 | /* unused in OVS */ | |
118 | dst->input = dst_md_discard; | |
119 | dst->output = dst_md_discard_out; | |
e23775f2 | 120 | #endif |
8e53509c WT |
121 | memset(dst + 1, 0, sizeof(*md_dst) + optslen - sizeof(*dst)); |
122 | md_dst->type = type; | |
e23775f2 PS |
123 | } |
124 | ||
8e53509c WT |
125 | static struct metadata_dst *erspan_rpl_metadata_dst_alloc(u8 optslen, enum metadata_type type, |
126 | gfp_t flags) | |
e23775f2 | 127 | { |
8e53509c WT |
128 | struct metadata_dst *md_dst; |
129 | ||
130 | md_dst = kmalloc(sizeof(*md_dst) + optslen, flags); | |
131 | if (!md_dst) | |
132 | return NULL; | |
133 | ||
134 | rpl__metadata_dst_init(md_dst, type, optslen); | |
135 | ||
136 | return md_dst; | |
137 | } | |
138 | static inline struct metadata_dst *rpl_tun_rx_dst(int md_size) | |
139 | { | |
140 | struct metadata_dst *tun_dst; | |
141 | ||
142 | tun_dst = erspan_rpl_metadata_dst_alloc(md_size, METADATA_IP_TUNNEL, GFP_ATOMIC); | |
143 | if (!tun_dst) | |
144 | return NULL; | |
145 | ||
146 | tun_dst->u.tun_info.options_len = 0; | |
147 | tun_dst->u.tun_info.mode = 0; | |
148 | return tun_dst; | |
149 | } | |
150 | static inline struct metadata_dst *rpl__ip_tun_set_dst(__be32 saddr, | |
151 | __be32 daddr, | |
152 | __u8 tos, __u8 ttl, | |
153 | __be16 tp_dst, | |
154 | __be16 flags, | |
155 | __be64 tunnel_id, | |
156 | int md_size) | |
157 | { | |
158 | struct metadata_dst *tun_dst; | |
159 | ||
160 | tun_dst = rpl_tun_rx_dst(md_size); | |
161 | if (!tun_dst) | |
162 | return NULL; | |
163 | ||
164 | ip_tunnel_key_init(&tun_dst->u.tun_info.key, | |
165 | saddr, daddr, tos, ttl, | |
166 | 0, 0, tp_dst, tunnel_id, flags); | |
167 | return tun_dst; | |
e23775f2 PS |
168 | } |
169 | ||
8e53509c WT |
170 | static inline struct metadata_dst *rpl_ip_tun_rx_dst(struct sk_buff *skb, |
171 | __be16 flags, | |
172 | __be64 tunnel_id, | |
173 | int md_size) | |
174 | { | |
175 | const struct iphdr *iph = ip_hdr(skb); | |
176 | ||
177 | return rpl__ip_tun_set_dst(iph->saddr, iph->daddr, iph->tos, iph->ttl, | |
178 | 0, flags, tunnel_id, md_size); | |
179 | } | |
180 | ||
181 | static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi, | |
182 | int gre_hdr_len) | |
e23775f2 PS |
183 | { |
184 | struct net *net = dev_net(skb->dev); | |
e987c48a | 185 | struct metadata_dst *tun_dst = NULL; |
2829d394 WT |
186 | struct erspan_base_hdr *ershdr; |
187 | struct erspan_metadata *pkt_md; | |
e23775f2 | 188 | struct ip_tunnel_net *itn; |
e23775f2 | 189 | struct ip_tunnel *tunnel; |
8e53509c | 190 | const struct iphdr *iph; |
c387d817 | 191 | struct erspan_md2 *md2; |
2829d394 | 192 | int ver; |
8e53509c | 193 | int len; |
e23775f2 | 194 | |
8e53509c | 195 | itn = net_generic(net, erspan_net_id); |
8e53509c | 196 | len = gre_hdr_len + sizeof(*ershdr); |
e23775f2 | 197 | |
2829d394 | 198 | /* Check based hdr len */ |
8e53509c | 199 | if (unlikely(!pskb_may_pull(skb, len))) |
e987c48a | 200 | return PACKET_REJECT; |
e23775f2 PS |
201 | |
202 | iph = ip_hdr(skb); | |
2829d394 | 203 | ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len); |
e987c48a | 204 | ver = ershdr->ver; |
8e53509c WT |
205 | |
206 | /* The original GRE header does not have key field, | |
207 | * Use ERSPAN 10-bit session ID as key. | |
208 | */ | |
209 | tpi->key = cpu_to_be32(get_session_id(ershdr)); | |
8e53509c WT |
210 | tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, |
211 | tpi->flags, | |
6690de40 | 212 | iph->saddr, iph->daddr, tpi->key); |
8e53509c | 213 | |
e23775f2 | 214 | if (tunnel) { |
2829d394 WT |
215 | len = gre_hdr_len + erspan_hdr_len(ver); |
216 | if (unlikely(!pskb_may_pull(skb, len))) | |
bf3eff8d | 217 | return PACKET_REJECT; |
2829d394 | 218 | |
e289f5e0 WT |
219 | ershdr = (struct erspan_base_hdr *)skb->data; |
220 | pkt_md = (struct erspan_metadata *)(ershdr + 1); | |
221 | ||
8e53509c | 222 | if (__iptunnel_pull_header(skb, |
2829d394 | 223 | len, |
8e53509c WT |
224 | htons(ETH_P_TEB), |
225 | false, false) < 0) | |
226 | goto drop; | |
227 | ||
228 | if (tunnel->collect_md) { | |
229 | struct ip_tunnel_info *info; | |
230 | struct erspan_metadata *md; | |
231 | __be64 tun_id; | |
232 | __be16 flags; | |
233 | ||
234 | tpi->flags |= TUNNEL_KEY; | |
235 | flags = tpi->flags; | |
236 | tun_id = key32_to_tunnel_id(tpi->key); | |
237 | ||
238 | tun_dst = rpl_ip_tun_rx_dst(skb, flags, tun_id, sizeof(*md)); | |
239 | if (!tun_dst) | |
e23775f2 | 240 | return PACKET_REJECT; |
8e53509c WT |
241 | |
242 | md = ip_tunnel_info_opts(&tun_dst->u.tun_info); | |
243 | md->version = ver; | |
244 | md2 = &md->u.md2; | |
245 | memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE : | |
246 | ERSPAN_V2_MDSIZE); | |
247 | ||
248 | info = &tun_dst->u.tun_info; | |
249 | info->key.tun_flags |= TUNNEL_ERSPAN_OPT; | |
250 | info->options_len = sizeof(*md); | |
251 | } | |
252 | ||
253 | skb_reset_mac_header(skb); | |
254 | ovs_ip_tunnel_rcv(tunnel->dev, skb, tun_dst); | |
255 | kfree(tun_dst); | |
256 | return PACKET_RCVD; | |
257 | } | |
258 | drop: | |
259 | kfree_skb(skb); | |
260 | return PACKET_RCVD; | |
261 | } | |
262 | ||
263 | ||
264 | static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi, | |
265 | struct ip_tunnel_net *itn, int hdr_len, bool raw_proto) | |
266 | { | |
267 | struct metadata_dst tun_dst; | |
268 | const struct iphdr *iph; | |
269 | struct ip_tunnel *tunnel; | |
270 | ||
271 | iph = ip_hdr(skb); | |
272 | tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags, | |
273 | iph->saddr, iph->daddr, tpi->key); | |
274 | ||
275 | if (tunnel) { | |
276 | if (__iptunnel_pull_header(skb, hdr_len, tpi->proto, | |
277 | raw_proto, false) < 0) | |
278 | goto drop; | |
279 | ||
280 | if (tunnel->dev->type != ARPHRD_NONE) | |
281 | skb_pop_mac_header(skb); | |
282 | else | |
283 | skb_reset_mac_header(skb); | |
284 | if (tunnel->collect_md) { | |
285 | __be16 flags; | |
286 | __be64 tun_id; | |
287 | ||
288 | flags = tpi->flags & (TUNNEL_CSUM | TUNNEL_KEY); | |
289 | tun_id = key32_to_tunnel_id(tpi->key); | |
290 | ovs_ip_tun_rx_dst(&tun_dst, skb, flags, tun_id, 0); | |
e23775f2 PS |
291 | } |
292 | ||
293 | ovs_ip_tunnel_rcv(tunnel->dev, skb, &tun_dst); | |
294 | return PACKET_RCVD; | |
295 | } | |
8e53509c WT |
296 | return PACKET_NEXT; |
297 | ||
298 | drop: | |
299 | kfree_skb(skb); | |
300 | return PACKET_RCVD; | |
301 | } | |
302 | ||
303 | ||
304 | static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi, | |
305 | int hdr_len) | |
306 | { | |
307 | struct net *net = dev_net(skb->dev); | |
308 | struct ip_tunnel_net *itn; | |
309 | int res; | |
310 | ||
311 | if (tpi->proto == htons(ETH_P_TEB)) | |
312 | itn = net_generic(net, gre_tap_net_id); | |
ba787100 HY |
313 | else if (tpi->proto == htons(ETH_P_ERSPAN) || |
314 | tpi->proto == htons(ETH_P_ERSPAN2)) | |
315 | itn = net_generic(net, erspan_net_id); | |
8e53509c | 316 | else |
d5822f42 | 317 | return PACKET_RCVD; |
8e53509c WT |
318 | |
319 | res = __ipgre_rcv(skb, tpi, itn, hdr_len, false); | |
d5822f42 | 320 | |
8e53509c | 321 | return res; |
e23775f2 PS |
322 | } |
323 | ||
8e53509c WT |
324 | static void __gre_xmit(struct sk_buff *skb, struct net_device *dev, |
325 | const struct iphdr *tnl_params, | |
326 | __be16 proto) | |
e23775f2 | 327 | { |
8e53509c WT |
328 | struct ip_tunnel *tunnel = netdev_priv(dev); |
329 | struct tnl_ptk_info tpi; | |
330 | ||
331 | tpi.flags = tunnel->parms.o_flags; | |
332 | tpi.proto = proto; | |
333 | tpi.key = tunnel->parms.o_key; | |
334 | if (tunnel->parms.o_flags & TUNNEL_SEQ) | |
335 | tunnel->o_seqno++; | |
336 | tpi.seq = htonl(tunnel->o_seqno); | |
337 | ||
338 | /* Push GRE header. */ | |
339 | gre_build_header(skb, &tpi, tunnel->hlen); | |
340 | ||
341 | ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol); | |
342 | } | |
343 | ||
8e53509c WT |
344 | static int gre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *unused_tpi) |
345 | { | |
346 | struct tnl_ptk_info tpi; | |
347 | bool csum_err = false; | |
348 | int hdr_len; | |
349 | ||
350 | hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP), 0); | |
351 | if (hdr_len < 0) | |
352 | goto drop; | |
353 | ||
e4120b10 WT |
354 | if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) || |
355 | tpi.proto == htons(ETH_P_ERSPAN2))) { | |
8e53509c WT |
356 | if (erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD) |
357 | return 0; | |
4a8a3521 | 358 | goto drop; |
8e53509c WT |
359 | } |
360 | ||
361 | if (ipgre_rcv(skb, &tpi, hdr_len) == PACKET_RCVD) | |
362 | return 0; | |
363 | drop: | |
364 | ||
365 | kfree_skb(skb); | |
366 | return 0; | |
367 | } | |
e23775f2 | 368 | |
cf5789aa | 369 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0) |
436d36db | 370 | #include "gso.h" |
cf5789aa | 371 | /* gre_handle_offloads() has different return type on older kernsl. */ |
e23775f2 PS |
372 | static void gre_nop_fix(struct sk_buff *skb) { } |
373 | ||
374 | static void gre_csum_fix(struct sk_buff *skb) | |
375 | { | |
376 | struct gre_base_hdr *greh; | |
377 | __be32 *options; | |
378 | int gre_offset = skb_transport_offset(skb); | |
379 | ||
380 | greh = (struct gre_base_hdr *)skb_transport_header(skb); | |
381 | options = ((__be32 *)greh + 1); | |
382 | ||
383 | *options = 0; | |
384 | *(__sum16 *)options = csum_fold(skb_checksum(skb, gre_offset, | |
385 | skb->len - gre_offset, 0)); | |
386 | } | |
387 | ||
a620d468 | 388 | #define gre_handle_offloads rpl_gre_handle_offloads |
cf5789aa | 389 | static int rpl_gre_handle_offloads(struct sk_buff *skb, bool gre_csum) |
e23775f2 PS |
390 | { |
391 | int type = gre_csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE; | |
392 | gso_fix_segment_t fix_segment; | |
393 | ||
394 | if (gre_csum) | |
395 | fix_segment = gre_csum_fix; | |
396 | else | |
397 | fix_segment = gre_nop_fix; | |
398 | ||
2a94b571 | 399 | return ovs_iptunnel_handle_offloads(skb, type, fix_segment); |
e23775f2 PS |
400 | } |
401 | #else | |
436d36db GR |
402 | static int gre_handle_offloads(struct sk_buff *skb, bool csum) |
403 | { | |
404 | return iptunnel_handle_offloads(skb, csum, | |
405 | csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE); | |
406 | } | |
407 | #endif | |
e23775f2 PS |
408 | |
409 | static bool is_gre_gso(struct sk_buff *skb) | |
410 | { | |
411 | return skb_shinfo(skb)->gso_type & | |
412 | (SKB_GSO_GRE | SKB_GSO_GRE_CSUM); | |
413 | } | |
414 | ||
e23775f2 PS |
415 | static void build_header(struct sk_buff *skb, int hdr_len, __be16 flags, |
416 | __be16 proto, __be32 key, __be32 seq) | |
417 | { | |
418 | struct gre_base_hdr *greh; | |
419 | ||
420 | skb_push(skb, hdr_len); | |
421 | ||
422 | skb_reset_transport_header(skb); | |
423 | greh = (struct gre_base_hdr *)skb->data; | |
424 | greh->flags = tnl_flags_to_gre_flags(flags); | |
425 | greh->protocol = proto; | |
426 | ||
427 | if (flags & (TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_SEQ)) { | |
428 | __be32 *ptr = (__be32 *)(((u8 *)greh) + hdr_len - 4); | |
429 | ||
430 | if (flags & TUNNEL_SEQ) { | |
431 | *ptr = seq; | |
432 | ptr--; | |
433 | } | |
434 | if (flags & TUNNEL_KEY) { | |
435 | *ptr = key; | |
436 | ptr--; | |
437 | } | |
438 | if (flags & TUNNEL_CSUM && !is_gre_gso(skb)) { | |
439 | *ptr = 0; | |
440 | *(__sum16 *)ptr = csum_fold(skb_checksum(skb, 0, | |
441 | skb->len, 0)); | |
442 | } | |
443 | } | |
444 | ovs_skb_set_inner_protocol(skb, proto); | |
445 | } | |
446 | ||
aad7cb91 PS |
447 | static struct rtable *gre_get_rt(struct sk_buff *skb, |
448 | struct net_device *dev, | |
449 | struct flowi4 *fl, | |
450 | const struct ip_tunnel_key *key) | |
451 | { | |
452 | struct net *net = dev_net(dev); | |
453 | ||
454 | memset(fl, 0, sizeof(*fl)); | |
455 | fl->daddr = key->u.ipv4.dst; | |
456 | fl->saddr = key->u.ipv4.src; | |
457 | fl->flowi4_tos = RT_TOS(key->tos); | |
458 | fl->flowi4_mark = skb->mark; | |
459 | fl->flowi4_proto = IPPROTO_GRE; | |
460 | ||
461 | return ip_route_output_key(net, fl); | |
462 | } | |
e23775f2 | 463 | |
d8433b88 WT |
464 | static struct rtable *prepare_fb_xmit(struct sk_buff *skb, |
465 | struct net_device *dev, | |
466 | struct flowi4 *fl, | |
467 | int tunnel_hlen) | |
468 | { | |
469 | struct ip_tunnel_info *tun_info; | |
470 | const struct ip_tunnel_key *key; | |
471 | struct rtable *rt = NULL; | |
472 | int min_headroom; | |
473 | bool use_cache; | |
474 | int err; | |
475 | ||
476 | tun_info = skb_tunnel_info(skb); | |
477 | key = &tun_info->key; | |
478 | use_cache = ip_tunnel_dst_cache_usable(skb, tun_info); | |
479 | ||
480 | if (use_cache) | |
481 | rt = dst_cache_get_ip4(&tun_info->dst_cache, &fl->saddr); | |
482 | if (!rt) { | |
483 | rt = gre_get_rt(skb, dev, fl, key); | |
484 | if (IS_ERR(rt)) | |
485 | goto err_free_skb; | |
486 | if (use_cache) | |
487 | dst_cache_set_ip4(&tun_info->dst_cache, &rt->dst, | |
488 | fl->saddr); | |
489 | } | |
490 | ||
491 | min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len | |
492 | + tunnel_hlen + sizeof(struct iphdr); | |
493 | if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) { | |
494 | int head_delta = SKB_DATA_ALIGN(min_headroom - | |
495 | skb_headroom(skb) + | |
496 | 16); | |
497 | err = pskb_expand_head(skb, max_t(int, head_delta, 0), | |
498 | 0, GFP_ATOMIC); | |
499 | if (unlikely(err)) | |
500 | goto err_free_rt; | |
501 | } | |
502 | return rt; | |
503 | ||
504 | err_free_rt: | |
505 | ip_rt_put(rt); | |
506 | err_free_skb: | |
507 | kfree_skb(skb); | |
508 | dev->stats.tx_dropped++; | |
509 | return NULL; | |
510 | } | |
511 | ||
e23775f2 PS |
512 | netdev_tx_t rpl_gre_fb_xmit(struct sk_buff *skb) |
513 | { | |
514 | struct net_device *dev = skb->dev; | |
e23775f2 PS |
515 | struct ip_tunnel_info *tun_info; |
516 | const struct ip_tunnel_key *key; | |
517 | struct flowi4 fl; | |
518 | struct rtable *rt; | |
519 | int min_headroom; | |
520 | int tunnel_hlen; | |
521 | __be16 df, flags; | |
522 | int err; | |
523 | ||
524 | tun_info = skb_tunnel_info(skb); | |
525 | if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) || | |
526 | ip_tunnel_info_af(tun_info) != AF_INET)) | |
527 | goto err_free_skb; | |
528 | ||
529 | key = &tun_info->key; | |
aad7cb91 PS |
530 | |
531 | rt = gre_get_rt(skb, dev, &fl, key); | |
e23775f2 PS |
532 | if (IS_ERR(rt)) |
533 | goto err_free_skb; | |
534 | ||
535 | tunnel_hlen = ip_gre_calc_hlen(key->tun_flags); | |
536 | ||
8063e095 | 537 | min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len |
e23775f2 PS |
538 | + tunnel_hlen + sizeof(struct iphdr) |
539 | + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0); | |
540 | if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) { | |
541 | int head_delta = SKB_DATA_ALIGN(min_headroom - | |
542 | skb_headroom(skb) + | |
543 | 16); | |
544 | err = pskb_expand_head(skb, max_t(int, head_delta, 0), | |
545 | 0, GFP_ATOMIC); | |
546 | if (unlikely(err)) | |
547 | goto err_free_rt; | |
548 | } | |
549 | ||
56174c2b GR |
550 | if (skb_vlan_tag_present(skb)) { |
551 | skb = __vlan_hwaccel_push_inside(skb); | |
552 | if (unlikely(!skb)) { | |
553 | err = -ENOMEM; | |
554 | goto err_free_rt; | |
555 | } | |
e23775f2 PS |
556 | } |
557 | ||
558 | /* Push Tunnel header. */ | |
436d36db | 559 | err = gre_handle_offloads(skb, !!(tun_info->key.tun_flags & TUNNEL_CSUM)); |
cf5789aa | 560 | if (err) |
e23775f2 | 561 | goto err_free_rt; |
e23775f2 PS |
562 | |
563 | flags = tun_info->key.tun_flags & (TUNNEL_CSUM | TUNNEL_KEY); | |
564 | build_header(skb, tunnel_hlen, flags, htons(ETH_P_TEB), | |
565 | tunnel_id_to_key(tun_info->key.tun_id), 0); | |
566 | ||
567 | df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; | |
43dd2fce PS |
568 | iptunnel_xmit(skb->sk, rt, skb, fl.saddr, key->u.ipv4.dst, IPPROTO_GRE, |
569 | key->tos, key->ttl, df, false); | |
e23775f2 PS |
570 | return NETDEV_TX_OK; |
571 | ||
572 | err_free_rt: | |
573 | ip_rt_put(rt); | |
574 | err_free_skb: | |
575 | kfree_skb(skb); | |
576 | dev->stats.tx_dropped++; | |
577 | return NETDEV_TX_OK; | |
578 | } | |
579 | EXPORT_SYMBOL(rpl_gre_fb_xmit); | |
580 | ||
8e53509c WT |
581 | static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev, |
582 | __be16 proto) | |
583 | { | |
584 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
585 | struct ip_tunnel_info *tun_info; | |
586 | const struct ip_tunnel_key *key; | |
587 | struct erspan_metadata *md; | |
588 | struct rtable *rt = NULL; | |
589 | struct tnl_ptk_info tpi; | |
590 | bool truncate = false; | |
591 | struct flowi4 fl; | |
592 | int tunnel_hlen; | |
593 | int version; | |
594 | __be16 df; | |
c7d95a7c | 595 | int nhoff; |
f1bc8ecd | 596 | int thoff; |
8e53509c WT |
597 | |
598 | tun_info = skb_tunnel_info(skb); | |
599 | if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) || | |
600 | ip_tunnel_info_af(tun_info) != AF_INET)) | |
601 | goto err_free_skb; | |
602 | ||
603 | key = &tun_info->key; | |
604 | md = ip_tunnel_info_opts(tun_info); | |
605 | if (!md) | |
606 | goto err_free_rt; | |
607 | ||
608 | /* ERSPAN has fixed 8 byte GRE header */ | |
609 | version = md->version; | |
610 | tunnel_hlen = 8 + erspan_hdr_len(version); | |
611 | ||
612 | rt = prepare_fb_xmit(skb, dev, &fl, tunnel_hlen); | |
613 | if (!rt) | |
614 | return; | |
615 | ||
616 | if (gre_handle_offloads(skb, false)) | |
617 | goto err_free_rt; | |
618 | ||
619 | if (skb->len > dev->mtu + dev->hard_header_len) { | |
620 | pskb_trim(skb, dev->mtu + dev->hard_header_len); | |
621 | truncate = true; | |
622 | } | |
623 | ||
c7d95a7c WT |
624 | nhoff = skb_network_header(skb) - skb_mac_header(skb); |
625 | if (skb->protocol == htons(ETH_P_IP) && | |
626 | (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff)) | |
627 | truncate = true; | |
628 | ||
f1bc8ecd WT |
629 | thoff = skb_transport_header(skb) - skb_mac_header(skb); |
630 | if (skb->protocol == htons(ETH_P_IPV6) && | |
631 | (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff)) | |
632 | truncate = true; | |
633 | ||
8e53509c WT |
634 | if (version == 1) { |
635 | erspan_build_header(skb, ntohl(tunnel_id_to_key32(key->tun_id)), | |
636 | ntohl(md->u.index), truncate, true); | |
637 | tpi.hdr_len = ERSPAN_V1_MDSIZE; | |
638 | tpi.proto = htons(ETH_P_ERSPAN); | |
639 | } else if (version == 2) { | |
640 | erspan_build_header_v2(skb, | |
641 | ntohl(tunnel_id_to_key32(key->tun_id)), | |
642 | md->u.md2.dir, | |
643 | get_hwid(&md->u.md2), | |
644 | truncate, true); | |
645 | tpi.hdr_len = ERSPAN_V2_MDSIZE; | |
646 | tpi.proto = htons(ETH_P_ERSPAN2); | |
647 | } else { | |
648 | goto err_free_rt; | |
649 | } | |
650 | ||
651 | tpi.flags = TUNNEL_SEQ; | |
652 | tpi.key = tunnel_id_to_key32(key->tun_id); | |
653 | tpi.seq = htonl(tunnel->o_seqno++); | |
654 | ||
655 | gre_build_header(skb, &tpi, 8); | |
656 | ||
657 | df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0; | |
658 | ||
659 | iptunnel_xmit(skb->sk, rt, skb, fl.saddr, key->u.ipv4.dst, IPPROTO_GRE, | |
660 | key->tos, key->ttl, df, false); | |
661 | return; | |
662 | ||
663 | err_free_rt: | |
664 | ip_rt_put(rt); | |
665 | err_free_skb: | |
666 | kfree_skb(skb); | |
667 | dev->stats.tx_dropped++; | |
668 | } | |
669 | ||
e23775f2 PS |
670 | #define GRE_FEATURES (NETIF_F_SG | \ |
671 | NETIF_F_FRAGLIST | \ | |
672 | NETIF_F_HIGHDMA | \ | |
673 | NETIF_F_HW_CSUM | \ | |
674 | NETIF_F_NETNS_LOCAL) | |
675 | ||
676 | static void __gre_tunnel_init(struct net_device *dev) | |
677 | { | |
678 | struct ip_tunnel *tunnel; | |
e23775f2 PS |
679 | |
680 | tunnel = netdev_priv(dev); | |
e23775f2 | 681 | tunnel->tun_hlen = ip_gre_calc_hlen(tunnel->parms.o_flags); |
8e53509c | 682 | tunnel->parms.iph.protocol = IPPROTO_GRE; |
e23775f2 PS |
683 | |
684 | tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen; | |
685 | ||
e23775f2 | 686 | dev->features |= GRE_FEATURES; |
e23775f2 | 687 | dev->hw_features |= GRE_FEATURES; |
e23775f2 PS |
688 | |
689 | if (!(tunnel->parms.o_flags & TUNNEL_SEQ)) { | |
8e53509c WT |
690 | /* TCP offload with GRE SEQ is not supported, nor |
691 | * can we support 2 levels of outer headers requiring | |
692 | * an update. | |
693 | */ | |
694 | if (!(tunnel->parms.o_flags & TUNNEL_CSUM) || | |
695 | (tunnel->encap.type == TUNNEL_ENCAP_NONE)) { | |
696 | dev->features |= NETIF_F_GSO_SOFTWARE; | |
697 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; | |
698 | } | |
699 | ||
e23775f2 PS |
700 | /* Can use a lockless transmit, unless we generate |
701 | * output sequences | |
702 | */ | |
703 | dev->features |= NETIF_F_LLTX; | |
704 | } | |
705 | } | |
706 | ||
828677b2 GR |
707 | static int __gre_rcv(struct sk_buff *skb) |
708 | { | |
709 | return gre_rcv(skb, NULL); | |
710 | } | |
711 | ||
712 | void __gre_err(struct sk_buff *skb, u32 info) | |
713 | { | |
714 | pr_warn("%s: GRE receive error\n", __func__); | |
715 | } | |
716 | ||
717 | static const struct gre_protocol ipgre_protocol = { | |
718 | .handler = __gre_rcv, | |
719 | .err_handler = __gre_err, | |
720 | }; | |
e23775f2 | 721 | |
436d36db GR |
722 | #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS |
723 | static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[], | |
724 | struct netlink_ext_ack *extack) | |
725 | #else | |
e23775f2 | 726 | static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[]) |
436d36db | 727 | #endif |
e23775f2 PS |
728 | { |
729 | __be16 flags; | |
730 | ||
731 | if (!data) | |
732 | return 0; | |
733 | ||
734 | flags = 0; | |
735 | if (data[IFLA_GRE_IFLAGS]) | |
736 | flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); | |
737 | if (data[IFLA_GRE_OFLAGS]) | |
738 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); | |
739 | if (flags & (GRE_VERSION|GRE_ROUTING)) | |
740 | return -EINVAL; | |
741 | ||
742 | return 0; | |
743 | } | |
744 | ||
436d36db GR |
745 | #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS |
746 | static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[], | |
747 | struct netlink_ext_ack *extack) | |
748 | #else | |
e23775f2 | 749 | static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[]) |
436d36db | 750 | #endif |
e23775f2 PS |
751 | { |
752 | __be32 daddr; | |
753 | ||
754 | if (tb[IFLA_ADDRESS]) { | |
755 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
756 | return -EINVAL; | |
757 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
758 | return -EADDRNOTAVAIL; | |
759 | } | |
760 | ||
761 | if (!data) | |
762 | goto out; | |
763 | ||
764 | if (data[IFLA_GRE_REMOTE]) { | |
765 | memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4); | |
766 | if (!daddr) | |
767 | return -EINVAL; | |
768 | } | |
769 | ||
770 | out: | |
436d36db GR |
771 | #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS |
772 | return ipgre_tunnel_validate(tb, data, NULL); | |
773 | #else | |
e23775f2 | 774 | return ipgre_tunnel_validate(tb, data); |
436d36db | 775 | #endif |
e23775f2 PS |
776 | } |
777 | ||
8e53509c | 778 | enum { |
e1ededf4 | 779 | #ifndef HAVE_IFLA_GRE_ENCAP_DPORT |
8e53509c WT |
780 | IFLA_GRE_ENCAP_TYPE = IFLA_GRE_FLAGS + 1, |
781 | IFLA_GRE_ENCAP_FLAGS, | |
782 | IFLA_GRE_ENCAP_SPORT, | |
783 | IFLA_GRE_ENCAP_DPORT, | |
784 | #endif | |
e1ededf4 | 785 | #ifndef HAVE_IFLA_GRE_COLLECT_METADATA |
8e53509c WT |
786 | IFLA_GRE_COLLECT_METADATA = IFLA_GRE_ENCAP_DPORT + 1, |
787 | #endif | |
e1ededf4 | 788 | #ifndef HAVE_IFLA_GRE_IGNORE_DF |
8e53509c WT |
789 | IFLA_GRE_IGNORE_DF = IFLA_GRE_COLLECT_METADATA + 1, |
790 | #endif | |
e1ededf4 | 791 | #ifndef HAVE_IFLA_GRE_FWMARK |
8e53509c WT |
792 | IFLA_GRE_FWMARK = IFLA_GRE_IGNORE_DF + 1, |
793 | #endif | |
e1ededf4 | 794 | #ifndef HAVE_IFLA_GRE_ERSPAN_INDEX |
8e53509c WT |
795 | IFLA_GRE_ERSPAN_INDEX = IFLA_GRE_FWMARK + 1, |
796 | #endif | |
e1ededf4 | 797 | #ifndef HAVE_IFLA_GRE_ERSPAN_HWID |
e4120b10 WT |
798 | IFLA_GRE_ERSPAN_VER = IFLA_GRE_ERSPAN_INDEX + 1, |
799 | IFLA_GRE_ERSPAN_DIR, | |
800 | IFLA_GRE_ERSPAN_HWID, | |
801 | #endif | |
8e53509c WT |
802 | }; |
803 | ||
e4120b10 | 804 | #define RPL_IFLA_GRE_MAX (IFLA_GRE_ERSPAN_HWID + 1) |
8e53509c | 805 | |
436d36db GR |
806 | #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS |
807 | static int erspan_validate(struct nlattr *tb[], struct nlattr *data[], | |
808 | struct netlink_ext_ack *extack) | |
809 | #else | |
8e53509c | 810 | static int erspan_validate(struct nlattr *tb[], struct nlattr *data[]) |
436d36db | 811 | #endif |
8e53509c WT |
812 | { |
813 | __be16 flags = 0; | |
814 | int ret; | |
815 | ||
816 | if (!data) | |
817 | return 0; | |
818 | ||
436d36db GR |
819 | #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS |
820 | ret = ipgre_tap_validate(tb, data, NULL); | |
821 | #else | |
8e53509c | 822 | ret = ipgre_tap_validate(tb, data); |
436d36db | 823 | #endif |
8e53509c WT |
824 | if (ret) |
825 | return ret; | |
826 | ||
827 | /* ERSPAN should only have GRE sequence and key flag */ | |
5bff8644 WT |
828 | if (data[IFLA_GRE_OFLAGS]) |
829 | flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]); | |
830 | if (data[IFLA_GRE_IFLAGS]) | |
831 | flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]); | |
832 | if (!data[IFLA_GRE_COLLECT_METADATA] && | |
833 | flags != (GRE_SEQ | GRE_KEY)) | |
8e53509c WT |
834 | return -EINVAL; |
835 | ||
836 | /* ERSPAN Session ID only has 10-bit. Since we reuse | |
837 | * 32-bit key field as ID, check it's range. | |
838 | */ | |
8e53509c WT |
839 | if (data[IFLA_GRE_OKEY] && |
840 | (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK)) | |
841 | return -EINVAL; | |
842 | ||
843 | return 0; | |
844 | } | |
845 | ||
846 | static int ipgre_netlink_parms(struct net_device *dev, | |
847 | struct nlattr *data[], | |
848 | struct nlattr *tb[], | |
849 | struct ip_tunnel_parm *parms) | |
e23775f2 | 850 | { |
8e53509c WT |
851 | struct ip_tunnel *t = netdev_priv(dev); |
852 | ||
e23775f2 PS |
853 | memset(parms, 0, sizeof(*parms)); |
854 | ||
855 | parms->iph.protocol = IPPROTO_GRE; | |
8e53509c WT |
856 | |
857 | if (!data) | |
858 | return 0; | |
859 | ||
860 | if (data[IFLA_GRE_LINK]) | |
861 | parms->link = nla_get_u32(data[IFLA_GRE_LINK]); | |
862 | ||
863 | if (data[IFLA_GRE_IFLAGS]) | |
864 | parms->i_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_IFLAGS])); | |
865 | ||
866 | if (data[IFLA_GRE_OFLAGS]) | |
867 | parms->o_flags = gre_flags_to_tnl_flags(nla_get_be16(data[IFLA_GRE_OFLAGS])); | |
868 | ||
869 | if (data[IFLA_GRE_IKEY]) | |
870 | parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]); | |
871 | ||
872 | if (data[IFLA_GRE_OKEY]) | |
873 | parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); | |
874 | ||
875 | if (data[IFLA_GRE_LOCAL]) | |
876 | parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]); | |
877 | ||
878 | if (data[IFLA_GRE_REMOTE]) | |
879 | parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]); | |
880 | ||
881 | if (data[IFLA_GRE_TTL]) | |
882 | parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]); | |
883 | ||
884 | if (data[IFLA_GRE_TOS]) | |
885 | parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]); | |
886 | ||
887 | if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC])) { | |
888 | if (t->ignore_df) | |
889 | return -EINVAL; | |
890 | parms->iph.frag_off = htons(IP_DF); | |
891 | } | |
892 | ||
893 | if (data[IFLA_GRE_COLLECT_METADATA]) { | |
894 | t->collect_md = true; | |
895 | if (dev->type == ARPHRD_IPGRE) | |
896 | dev->type = ARPHRD_NONE; | |
897 | } | |
898 | ||
899 | if (data[IFLA_GRE_IGNORE_DF]) { | |
900 | if (nla_get_u8(data[IFLA_GRE_IGNORE_DF]) | |
901 | && (parms->iph.frag_off & htons(IP_DF))) | |
902 | return -EINVAL; | |
903 | t->ignore_df = !!nla_get_u8(data[IFLA_GRE_IGNORE_DF]); | |
904 | } | |
905 | ||
906 | if (data[IFLA_GRE_ERSPAN_INDEX]) { | |
907 | t->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]); | |
908 | ||
909 | if (t->index & ~INDEX_MASK) | |
910 | return -EINVAL; | |
911 | } | |
912 | ||
913 | return 0; | |
e23775f2 PS |
914 | } |
915 | ||
916 | static int gre_tap_init(struct net_device *dev) | |
917 | { | |
918 | __gre_tunnel_init(dev); | |
919 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; | |
920 | ||
921 | return ip_tunnel_init(dev); | |
922 | } | |
923 | ||
924 | static netdev_tx_t gre_dev_xmit(struct sk_buff *skb, struct net_device *dev) | |
925 | { | |
926 | /* Drop All packets coming from networking stack. OVS-CB is | |
927 | * not initialized for these packets. | |
928 | */ | |
929 | ||
930 | dev_kfree_skb(skb); | |
931 | dev->stats.tx_dropped++; | |
932 | return NETDEV_TX_OK; | |
933 | } | |
934 | ||
8e53509c WT |
935 | static netdev_tx_t erspan_xmit(struct sk_buff *skb, |
936 | struct net_device *dev) | |
937 | { | |
938 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
939 | bool truncate = false; | |
940 | ||
5bff8644 WT |
941 | if (tunnel->collect_md) { |
942 | erspan_fb_xmit(skb, dev, skb->protocol); | |
943 | return NETDEV_TX_OK; | |
944 | } | |
945 | ||
8e53509c WT |
946 | if (gre_handle_offloads(skb, false)) |
947 | goto free_skb; | |
948 | ||
949 | if (skb_cow_head(skb, dev->needed_headroom)) | |
950 | goto free_skb; | |
951 | ||
4f53627e WT |
952 | if (skb->len > dev->mtu + dev->hard_header_len) { |
953 | pskb_trim(skb, dev->mtu + dev->hard_header_len); | |
8e53509c WT |
954 | truncate = true; |
955 | } | |
956 | ||
957 | /* Push ERSPAN header */ | |
e4120b10 | 958 | if (tunnel->erspan_ver == 1) |
e6aa9dd8 WT |
959 | erspan_build_header(skb, ntohl(tunnel->parms.o_key), |
960 | tunnel->index, | |
e4120b10 | 961 | truncate, true); |
5e720da5 | 962 | else if (tunnel->erspan_ver == 2) |
e6aa9dd8 | 963 | erspan_build_header_v2(skb, ntohl(tunnel->parms.o_key), |
e4120b10 WT |
964 | tunnel->dir, tunnel->hwid, |
965 | truncate, true); | |
5e720da5 WT |
966 | else |
967 | goto free_skb; | |
e4120b10 | 968 | |
8e53509c WT |
969 | tunnel->parms.o_flags &= ~TUNNEL_KEY; |
970 | __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_ERSPAN)); | |
971 | return NETDEV_TX_OK; | |
972 | ||
973 | free_skb: | |
974 | kfree_skb(skb); | |
975 | dev->stats.tx_dropped++; | |
976 | return NETDEV_TX_OK; | |
977 | } | |
978 | ||
979 | static netdev_tx_t __erspan_fb_xmit(struct sk_buff *skb) | |
980 | { | |
981 | erspan_fb_xmit(skb, skb->dev, skb->protocol); | |
982 | return NETDEV_TX_OK; | |
983 | } | |
984 | ||
aad7cb91 PS |
985 | int ovs_gre_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) |
986 | { | |
987 | struct ip_tunnel_info *info = skb_tunnel_info(skb); | |
988 | struct rtable *rt; | |
989 | struct flowi4 fl4; | |
990 | ||
991 | if (ip_tunnel_info_af(info) != AF_INET) | |
992 | return -EINVAL; | |
993 | ||
994 | rt = gre_get_rt(skb, dev, &fl4, &info->key); | |
995 | if (IS_ERR(rt)) | |
996 | return PTR_ERR(rt); | |
997 | ||
998 | ip_rt_put(rt); | |
999 | info->key.u.ipv4.src = fl4.saddr; | |
1000 | return 0; | |
1001 | } | |
1002 | EXPORT_SYMBOL_GPL(ovs_gre_fill_metadata_dst); | |
1003 | ||
8e53509c WT |
1004 | static int erspan_tunnel_init(struct net_device *dev) |
1005 | { | |
1006 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
8e53509c WT |
1007 | |
1008 | tunnel->tun_hlen = 8; | |
1009 | tunnel->parms.iph.protocol = IPPROTO_GRE; | |
cd71b0d7 | 1010 | tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen + |
e4120b10 | 1011 | erspan_hdr_len(tunnel->erspan_ver); |
8e53509c | 1012 | |
8e53509c WT |
1013 | dev->features |= GRE_FEATURES; |
1014 | dev->hw_features |= GRE_FEATURES; | |
1015 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; | |
2ce53c0a | 1016 | netif_keep_dst(dev); |
8e53509c WT |
1017 | |
1018 | return ip_tunnel_init(dev); | |
1019 | } | |
1020 | ||
1021 | static int ipgre_header(struct sk_buff *skb, struct net_device *dev, | |
1022 | unsigned short type, | |
1023 | const void *daddr, const void *saddr, unsigned int len) | |
1024 | { | |
1025 | struct ip_tunnel *t = netdev_priv(dev); | |
1026 | struct iphdr *iph; | |
1027 | struct gre_base_hdr *greh; | |
1028 | ||
1029 | iph = (struct iphdr *)__skb_push(skb, t->hlen + sizeof(*iph)); | |
1030 | greh = (struct gre_base_hdr *)(iph+1); | |
1031 | greh->flags = gre_tnl_flags_to_gre_flags(t->parms.o_flags); | |
1032 | greh->protocol = htons(type); | |
1033 | ||
1034 | memcpy(iph, &t->parms.iph, sizeof(struct iphdr)); | |
1035 | ||
1036 | /* Set the source hardware address. */ | |
1037 | if (saddr) | |
1038 | memcpy(&iph->saddr, saddr, 4); | |
1039 | if (daddr) | |
1040 | memcpy(&iph->daddr, daddr, 4); | |
1041 | if (iph->daddr) | |
1042 | return t->hlen + sizeof(*iph); | |
1043 | ||
1044 | return -(t->hlen + sizeof(*iph)); | |
1045 | } | |
1046 | ||
1047 | static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr) | |
1048 | { | |
1049 | const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb); | |
1050 | memcpy(haddr, &iph->saddr, 4); | |
1051 | return 4; | |
1052 | } | |
1053 | ||
1054 | static const struct header_ops ipgre_header_ops = { | |
1055 | .create = ipgre_header, | |
1056 | .parse = ipgre_header_parse, | |
1057 | }; | |
1058 | ||
1059 | static int ipgre_tunnel_init(struct net_device *dev) | |
1060 | { | |
1061 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
1062 | struct iphdr *iph = &tunnel->parms.iph; | |
1063 | ||
1064 | __gre_tunnel_init(dev); | |
1065 | ||
1066 | memcpy(dev->dev_addr, &iph->saddr, 4); | |
1067 | memcpy(dev->broadcast, &iph->daddr, 4); | |
1068 | ||
1069 | dev->flags = IFF_NOARP; | |
1070 | netif_keep_dst(dev); | |
1071 | dev->addr_len = 4; | |
1072 | ||
1073 | if (!tunnel->collect_md) { | |
1074 | dev->header_ops = &ipgre_header_ops; | |
1075 | } | |
1076 | ||
1077 | return ip_tunnel_init(dev); | |
1078 | } | |
1079 | ||
1080 | static netdev_tx_t ipgre_xmit(struct sk_buff *skb, | |
1081 | struct net_device *dev) | |
1082 | { | |
1083 | struct ip_tunnel *tunnel = netdev_priv(dev); | |
1084 | const struct iphdr *tnl_params; | |
1085 | ||
1086 | if (tunnel->collect_md) { | |
1087 | gre_fb_xmit(skb); | |
1088 | return NETDEV_TX_OK; | |
1089 | } | |
1090 | ||
1091 | if (dev->header_ops) { | |
1092 | /* Need space for new headers */ | |
1093 | if (skb_cow_head(skb, dev->needed_headroom - | |
1094 | (tunnel->hlen + sizeof(struct iphdr)))) | |
1095 | goto free_skb; | |
1096 | ||
1097 | tnl_params = (const struct iphdr *)skb->data; | |
1098 | ||
1099 | /* Pull skb since ip_tunnel_xmit() needs skb->data pointing | |
1100 | * to gre header. | |
1101 | */ | |
1102 | skb_pull(skb, tunnel->hlen + sizeof(struct iphdr)); | |
1103 | skb_reset_mac_header(skb); | |
1104 | } else { | |
1105 | if (skb_cow_head(skb, dev->needed_headroom)) | |
1106 | goto free_skb; | |
1107 | ||
1108 | tnl_params = &tunnel->parms.iph; | |
1109 | } | |
1110 | ||
1111 | if (gre_handle_offloads(skb, !!(tunnel->parms.o_flags & TUNNEL_CSUM))) | |
1112 | goto free_skb; | |
1113 | ||
1114 | __gre_xmit(skb, dev, tnl_params, skb->protocol); | |
1115 | return NETDEV_TX_OK; | |
1116 | ||
1117 | free_skb: | |
1118 | kfree_skb(skb); | |
1119 | dev->stats.tx_dropped++; | |
1120 | return NETDEV_TX_OK; | |
1121 | } | |
1122 | ||
1123 | static const struct net_device_ops ipgre_netdev_ops = { | |
1124 | .ndo_init = ipgre_tunnel_init, | |
1125 | .ndo_uninit = rpl_ip_tunnel_uninit, | |
1126 | .ndo_start_xmit = ipgre_xmit, | |
2fe15e8c YHW |
1127 | #ifdef HAVE_RHEL7_MAX_MTU |
1128 | .ndo_size = sizeof(struct net_device_ops), | |
1129 | .extended.ndo_change_mtu = ip_tunnel_change_mtu, | |
1130 | #else | |
8e53509c | 1131 | .ndo_change_mtu = ip_tunnel_change_mtu, |
2fe15e8c | 1132 | #endif |
8e53509c WT |
1133 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
1134 | #ifdef HAVE_GET_LINK_NET | |
1135 | .ndo_get_iflink = ip_tunnel_get_iflink, | |
1136 | #endif | |
1137 | }; | |
1138 | ||
e23775f2 PS |
1139 | static const struct net_device_ops gre_tap_netdev_ops = { |
1140 | .ndo_init = gre_tap_init, | |
8e53509c | 1141 | .ndo_uninit = rpl_ip_tunnel_uninit, |
e23775f2 PS |
1142 | .ndo_start_xmit = gre_dev_xmit, |
1143 | .ndo_set_mac_address = eth_mac_addr, | |
1144 | .ndo_validate_addr = eth_validate_addr, | |
39ca3383 | 1145 | #ifdef HAVE_RHEL7_MAX_MTU |
3ab8a26e | 1146 | .ndo_size = sizeof(struct net_device_ops), |
39ca3383 YHW |
1147 | .extended.ndo_change_mtu = ip_tunnel_change_mtu, |
1148 | #else | |
e23775f2 | 1149 | .ndo_change_mtu = ip_tunnel_change_mtu, |
39ca3383 | 1150 | #endif |
436d36db | 1151 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
e23775f2 | 1152 | #ifdef HAVE_NDO_GET_IFLINK |
8e53509c | 1153 | .ndo_get_iflink = rpl_ip_tunnel_get_iflink, |
e23775f2 | 1154 | #endif |
aad7cb91 PS |
1155 | #ifdef HAVE_NDO_FILL_METADATA_DST |
1156 | .ndo_fill_metadata_dst = gre_fill_metadata_dst, | |
1157 | #endif | |
e23775f2 PS |
1158 | }; |
1159 | ||
8e53509c WT |
1160 | static const struct net_device_ops erspan_netdev_ops = { |
1161 | .ndo_init = erspan_tunnel_init, | |
1162 | .ndo_uninit = rpl_ip_tunnel_uninit, | |
1163 | .ndo_start_xmit = erspan_xmit, | |
1164 | .ndo_set_mac_address = eth_mac_addr, | |
1165 | .ndo_validate_addr = eth_validate_addr, | |
2fe15e8c YHW |
1166 | #ifdef HAVE_RHEL7_MAX_MTU |
1167 | .ndo_size = sizeof(struct net_device_ops), | |
1168 | .extended.ndo_change_mtu = ip_tunnel_change_mtu, | |
1169 | #else | |
8e53509c | 1170 | .ndo_change_mtu = ip_tunnel_change_mtu, |
2fe15e8c | 1171 | #endif |
436d36db | 1172 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
8e53509c WT |
1173 | #ifdef HAVE_NDO_GET_IFLINK |
1174 | .ndo_get_iflink = rpl_ip_tunnel_get_iflink, | |
1175 | #endif | |
1176 | #ifdef HAVE_NDO_FILL_METADATA_DST | |
1177 | .ndo_fill_metadata_dst = gre_fill_metadata_dst, | |
1178 | #endif | |
1179 | }; | |
1180 | ||
e23775f2 PS |
1181 | static void ipgre_tap_setup(struct net_device *dev) |
1182 | { | |
1183 | ether_setup(dev); | |
e987c48a | 1184 | #ifdef HAVE_NET_DEVICE_MAX_MTU |
4a8a3521 | 1185 | dev->max_mtu = 0; |
e987c48a | 1186 | #endif |
e23775f2 PS |
1187 | dev->netdev_ops = &gre_tap_netdev_ops; |
1188 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; | |
1189 | ip_tunnel_setup(dev, gre_tap_net_id); | |
1190 | } | |
1191 | ||
8e53509c WT |
1192 | static void erspan_setup(struct net_device *dev) |
1193 | { | |
bc92e8a4 XL |
1194 | struct ip_tunnel *t = netdev_priv(dev); |
1195 | ||
8e53509c WT |
1196 | eth_hw_addr_random(dev); |
1197 | ether_setup(dev); | |
1198 | dev->netdev_ops = &erspan_netdev_ops; | |
1199 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; | |
1200 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; | |
1201 | ip_tunnel_setup(dev, erspan_net_id); | |
bc92e8a4 | 1202 | t->erspan_ver = 1; |
8e53509c WT |
1203 | } |
1204 | ||
436d36db GR |
1205 | #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS |
1206 | static int ipgre_newlink(struct net *src_net, struct net_device *dev, | |
1207 | struct nlattr *tb[], struct nlattr *data[], | |
1208 | struct netlink_ext_ack *extack) | |
1209 | #else | |
e23775f2 PS |
1210 | static int ipgre_newlink(struct net *src_net, struct net_device *dev, |
1211 | struct nlattr *tb[], struct nlattr *data[]) | |
436d36db | 1212 | #endif |
e23775f2 PS |
1213 | { |
1214 | struct ip_tunnel_parm p; | |
1215 | int err; | |
1216 | ||
1217 | ipgre_netlink_parms(dev, data, tb, &p); | |
1218 | err = ip_tunnel_newlink(dev, tb, &p); | |
1219 | return err; | |
1220 | ||
1221 | } | |
1222 | ||
1223 | static size_t ipgre_get_size(const struct net_device *dev) | |
1224 | { | |
1225 | return | |
1226 | /* IFLA_GRE_LINK */ | |
1227 | nla_total_size(4) + | |
1228 | /* IFLA_GRE_IFLAGS */ | |
1229 | nla_total_size(2) + | |
1230 | /* IFLA_GRE_OFLAGS */ | |
1231 | nla_total_size(2) + | |
1232 | /* IFLA_GRE_IKEY */ | |
1233 | nla_total_size(4) + | |
1234 | /* IFLA_GRE_OKEY */ | |
1235 | nla_total_size(4) + | |
1236 | /* IFLA_GRE_LOCAL */ | |
1237 | nla_total_size(4) + | |
1238 | /* IFLA_GRE_REMOTE */ | |
1239 | nla_total_size(4) + | |
1240 | /* IFLA_GRE_TTL */ | |
1241 | nla_total_size(1) + | |
1242 | /* IFLA_GRE_TOS */ | |
1243 | nla_total_size(1) + | |
1244 | /* IFLA_GRE_PMTUDISC */ | |
1245 | nla_total_size(1) + | |
1246 | /* IFLA_GRE_ENCAP_TYPE */ | |
1247 | nla_total_size(2) + | |
1248 | /* IFLA_GRE_ENCAP_FLAGS */ | |
1249 | nla_total_size(2) + | |
1250 | /* IFLA_GRE_ENCAP_SPORT */ | |
1251 | nla_total_size(2) + | |
1252 | /* IFLA_GRE_ENCAP_DPORT */ | |
1253 | nla_total_size(2) + | |
1254 | /* IFLA_GRE_COLLECT_METADATA */ | |
1255 | nla_total_size(0) + | |
8e53509c WT |
1256 | /* IFLA_GRE_ERSPAN_INDEX */ |
1257 | nla_total_size(4) + | |
e4120b10 WT |
1258 | /* IFLA_GRE_ERSPAN_VER */ |
1259 | nla_total_size(1) + | |
1260 | /* IFLA_GRE_ERSPAN_DIR */ | |
1261 | nla_total_size(1) + | |
1262 | /* IFLA_GRE_ERSPAN_HWID */ | |
1263 | nla_total_size(2) + | |
e23775f2 PS |
1264 | 0; |
1265 | } | |
1266 | ||
1267 | static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
1268 | { | |
1269 | struct ip_tunnel *t = netdev_priv(dev); | |
1270 | struct ip_tunnel_parm *p = &t->parms; | |
1271 | ||
1272 | if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) || | |
1273 | nla_put_be16(skb, IFLA_GRE_IFLAGS, tnl_flags_to_gre_flags(p->i_flags)) || | |
1274 | nla_put_be16(skb, IFLA_GRE_OFLAGS, tnl_flags_to_gre_flags(p->o_flags)) || | |
1275 | nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || | |
1276 | nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || | |
1277 | nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) || | |
1278 | nla_put_in_addr(skb, IFLA_GRE_REMOTE, p->iph.daddr) || | |
1279 | nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) || | |
1280 | nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) || | |
1281 | nla_put_u8(skb, IFLA_GRE_PMTUDISC, | |
1282 | !!(p->iph.frag_off & htons(IP_DF)))) | |
1283 | goto nla_put_failure; | |
1284 | ||
e4120b10 WT |
1285 | if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, t->erspan_ver)) |
1286 | goto nla_put_failure; | |
1287 | ||
1288 | if (t->erspan_ver == 1) { | |
1289 | if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, t->index)) | |
1290 | goto nla_put_failure; | |
1291 | } else if (t->erspan_ver == 2) { | |
1292 | if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, t->dir)) | |
1293 | goto nla_put_failure; | |
1294 | if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, t->hwid)) | |
8e53509c | 1295 | goto nla_put_failure; |
e4120b10 | 1296 | } |
8e53509c | 1297 | |
e23775f2 PS |
1298 | return 0; |
1299 | ||
1300 | nla_put_failure: | |
1301 | return -EMSGSIZE; | |
1302 | } | |
1303 | ||
8e53509c | 1304 | static const struct nla_policy ipgre_policy[RPL_IFLA_GRE_MAX + 1] = { |
e23775f2 PS |
1305 | [IFLA_GRE_LINK] = { .type = NLA_U32 }, |
1306 | [IFLA_GRE_IFLAGS] = { .type = NLA_U16 }, | |
1307 | [IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, | |
1308 | [IFLA_GRE_IKEY] = { .type = NLA_U32 }, | |
1309 | [IFLA_GRE_OKEY] = { .type = NLA_U32 }, | |
1310 | [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, | |
1311 | [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, | |
1312 | [IFLA_GRE_TTL] = { .type = NLA_U8 }, | |
1313 | [IFLA_GRE_TOS] = { .type = NLA_U8 }, | |
1314 | [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 }, | |
8e53509c | 1315 | [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 }, |
e4120b10 WT |
1316 | [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 }, |
1317 | [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 }, | |
1318 | [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 }, | |
8e53509c WT |
1319 | }; |
1320 | ||
e23775f2 PS |
1321 | static struct rtnl_link_ops ipgre_tap_ops __read_mostly = { |
1322 | .kind = "ovs_gretap", | |
8e53509c | 1323 | .maxtype = RPL_IFLA_GRE_MAX, |
e23775f2 PS |
1324 | .policy = ipgre_policy, |
1325 | .priv_size = sizeof(struct ip_tunnel), | |
1326 | .setup = ipgre_tap_setup, | |
1327 | .validate = ipgre_tap_validate, | |
1328 | .newlink = ipgre_newlink, | |
1329 | .dellink = ip_tunnel_dellink, | |
1330 | .get_size = ipgre_get_size, | |
1331 | .fill_info = ipgre_fill_info, | |
1332 | #ifdef HAVE_GET_LINK_NET | |
1333 | .get_link_net = ip_tunnel_get_link_net, | |
1334 | #endif | |
1335 | }; | |
1336 | ||
8e53509c WT |
1337 | static struct rtnl_link_ops erspan_link_ops __read_mostly = { |
1338 | .kind = "erspan", | |
1339 | .maxtype = RPL_IFLA_GRE_MAX, | |
1340 | .policy = ipgre_policy, | |
1341 | .priv_size = sizeof(struct ip_tunnel), | |
1342 | .setup = erspan_setup, | |
1343 | .validate = erspan_validate, | |
1344 | .newlink = ipgre_newlink, | |
1345 | .dellink = ip_tunnel_dellink, | |
1346 | .get_size = ipgre_get_size, | |
1347 | .fill_info = ipgre_fill_info, | |
1348 | #ifdef HAVE_GET_LINK_NET | |
1349 | .get_link_net = ip_tunnel_get_link_net, | |
1350 | #endif | |
1351 | }; | |
1352 | ||
e23775f2 PS |
1353 | struct net_device *rpl_gretap_fb_dev_create(struct net *net, const char *name, |
1354 | u8 name_assign_type) | |
1355 | { | |
1356 | struct nlattr *tb[IFLA_MAX + 1]; | |
1357 | struct net_device *dev; | |
7cecb9c9 | 1358 | LIST_HEAD(list_kill); |
e23775f2 PS |
1359 | struct ip_tunnel *t; |
1360 | int err; | |
1361 | ||
1362 | memset(&tb, 0, sizeof(tb)); | |
1363 | ||
1364 | dev = rtnl_create_link(net, (char *)name, name_assign_type, | |
1365 | &ipgre_tap_ops, tb); | |
1366 | if (IS_ERR(dev)) | |
1367 | return dev; | |
1368 | ||
1369 | t = netdev_priv(dev); | |
1370 | t->collect_md = true; | |
1371 | /* Configure flow based GRE device. */ | |
436d36db GR |
1372 | #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS |
1373 | err = ipgre_newlink(net, dev, tb, NULL, NULL); | |
1374 | #else | |
e23775f2 | 1375 | err = ipgre_newlink(net, dev, tb, NULL); |
436d36db | 1376 | #endif |
7cecb9c9 PS |
1377 | if (err < 0) { |
1378 | free_netdev(dev); | |
1379 | return ERR_PTR(err); | |
1380 | } | |
06f1a61a DW |
1381 | |
1382 | /* openvswitch users expect packet sizes to be unrestricted, | |
1383 | * so set the largest MTU we can. | |
1384 | */ | |
1385 | err = __ip_tunnel_change_mtu(dev, IP_MAX_MTU, false); | |
1386 | if (err) | |
1387 | goto out; | |
1388 | ||
e23775f2 PS |
1389 | return dev; |
1390 | out: | |
7cecb9c9 PS |
1391 | ip_tunnel_dellink(dev, &list_kill); |
1392 | unregister_netdevice_many(&list_kill); | |
e23775f2 PS |
1393 | return ERR_PTR(err); |
1394 | } | |
1395 | EXPORT_SYMBOL_GPL(rpl_gretap_fb_dev_create); | |
1396 | ||
8e53509c WT |
1397 | static int __net_init erspan_init_net(struct net *net) |
1398 | { | |
1399 | return ip_tunnel_init_net(net, erspan_net_id, | |
1400 | &erspan_link_ops, NULL); | |
1401 | } | |
1402 | ||
1403 | static void __net_exit erspan_exit_net(struct net *net) | |
1404 | { | |
1405 | struct ip_tunnel_net *itn = net_generic(net, erspan_net_id); | |
1406 | ||
1407 | ip_tunnel_delete_net(itn, &erspan_link_ops); | |
1408 | } | |
1409 | ||
1410 | static struct pernet_operations erspan_net_ops = { | |
1411 | .init = erspan_init_net, | |
1412 | .exit = erspan_exit_net, | |
1413 | .id = &erspan_net_id, | |
1414 | .size = sizeof(struct ip_tunnel_net), | |
1415 | }; | |
1416 | ||
e23775f2 PS |
1417 | static int __net_init ipgre_tap_init_net(struct net *net) |
1418 | { | |
37ed6372 | 1419 | return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, "ovs-gretap0"); |
e23775f2 PS |
1420 | } |
1421 | ||
1422 | static void __net_exit ipgre_tap_exit_net(struct net *net) | |
1423 | { | |
1424 | struct ip_tunnel_net *itn = net_generic(net, gre_tap_net_id); | |
1425 | ||
1426 | ip_tunnel_delete_net(itn, &ipgre_tap_ops); | |
1427 | } | |
1428 | ||
1429 | static struct pernet_operations ipgre_tap_net_ops = { | |
1430 | .init = ipgre_tap_init_net, | |
1431 | .exit = ipgre_tap_exit_net, | |
1432 | .id = &gre_tap_net_id, | |
1433 | .size = sizeof(struct ip_tunnel_net), | |
1434 | }; | |
1435 | ||
8e53509c WT |
1436 | static struct net_device *erspan_fb_dev_create(struct net *net, |
1437 | const char *name, | |
1438 | u8 name_assign_type) | |
1439 | { | |
1440 | struct nlattr *tb[IFLA_MAX + 1]; | |
1441 | struct net_device *dev; | |
1442 | LIST_HEAD(list_kill); | |
1443 | struct ip_tunnel *t; | |
1444 | int err; | |
1445 | ||
1446 | memset(&tb, 0, sizeof(tb)); | |
1447 | ||
1448 | dev = rtnl_create_link(net, (char *)name, name_assign_type, | |
1449 | &erspan_link_ops, tb); | |
1450 | if (IS_ERR(dev)) | |
1451 | return dev; | |
1452 | ||
1453 | t = netdev_priv(dev); | |
1454 | t->collect_md = true; | |
1455 | /* Configure flow based GRE device. */ | |
436d36db GR |
1456 | #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS |
1457 | err = ipgre_newlink(net, dev, tb, NULL, NULL); | |
1458 | #else | |
8e53509c | 1459 | err = ipgre_newlink(net, dev, tb, NULL); |
436d36db | 1460 | #endif |
8e53509c WT |
1461 | if (err < 0) { |
1462 | free_netdev(dev); | |
1463 | return ERR_PTR(err); | |
1464 | } | |
1465 | ||
1466 | /* openvswitch users expect packet sizes to be unrestricted, | |
1467 | * so set the largest MTU we can. | |
1468 | */ | |
1469 | err = __ip_tunnel_change_mtu(dev, IP_MAX_MTU, false); | |
1470 | if (err) | |
1471 | goto out; | |
1472 | ||
1473 | return dev; | |
1474 | out: | |
1475 | ip_tunnel_dellink(dev, &list_kill); | |
1476 | unregister_netdevice_many(&list_kill); | |
1477 | return ERR_PTR(err); | |
1478 | } | |
1479 | ||
1480 | static struct vport_ops ovs_erspan_vport_ops; | |
1481 | ||
1482 | static struct vport *erspan_tnl_create(const struct vport_parms *parms) | |
1483 | { | |
1484 | struct net *net = ovs_dp_get_net(parms->dp); | |
1485 | struct net_device *dev; | |
1486 | struct vport *vport; | |
1487 | int err; | |
1488 | ||
1489 | vport = ovs_vport_alloc(0, &ovs_erspan_vport_ops, parms); | |
1490 | if (IS_ERR(vport)) | |
1491 | return vport; | |
1492 | ||
1493 | rtnl_lock(); | |
1494 | dev = erspan_fb_dev_create(net, parms->name, NET_NAME_USER); | |
1495 | if (IS_ERR(dev)) { | |
1496 | rtnl_unlock(); | |
1497 | ovs_vport_free(vport); | |
1498 | return ERR_CAST(dev); | |
1499 | } | |
1500 | ||
1501 | err = dev_change_flags(dev, dev->flags | IFF_UP); | |
1502 | if (err < 0) { | |
1503 | rtnl_delete_link(dev); | |
1504 | rtnl_unlock(); | |
1505 | ovs_vport_free(vport); | |
1506 | return ERR_PTR(err); | |
1507 | } | |
1508 | ||
1509 | rtnl_unlock(); | |
1510 | return vport; | |
1511 | } | |
1512 | ||
1513 | static struct vport *erspan_create(const struct vport_parms *parms) | |
1514 | { | |
1515 | struct vport *vport; | |
1516 | ||
1517 | vport = erspan_tnl_create(parms); | |
1518 | if (IS_ERR(vport)) | |
1519 | return vport; | |
1520 | ||
1521 | return ovs_netdev_link(vport, parms->name); | |
1522 | } | |
1523 | ||
8e53509c WT |
1524 | static struct vport_ops ovs_erspan_vport_ops = { |
1525 | .type = OVS_VPORT_TYPE_ERSPAN, | |
1526 | .create = erspan_create, | |
1527 | .send = __erspan_fb_xmit, | |
1528 | #ifndef USE_UPSTREAM_TUNNEL | |
1529 | .fill_metadata_dst = gre_fill_metadata_dst, | |
1530 | #endif | |
1531 | .destroy = ovs_netdev_tunnel_destroy, | |
1532 | }; | |
1533 | ||
1534 | static struct vport_ops ovs_ipgre_vport_ops; | |
1535 | ||
1536 | static struct vport *ipgre_tnl_create(const struct vport_parms *parms) | |
1537 | { | |
1538 | struct net *net = ovs_dp_get_net(parms->dp); | |
1539 | struct net_device *dev; | |
1540 | struct vport *vport; | |
1541 | int err; | |
1542 | ||
1543 | vport = ovs_vport_alloc(0, &ovs_ipgre_vport_ops, parms); | |
1544 | if (IS_ERR(vport)) | |
1545 | return vport; | |
1546 | ||
1547 | rtnl_lock(); | |
1548 | dev = gretap_fb_dev_create(net, parms->name, NET_NAME_USER); | |
1549 | if (IS_ERR(dev)) { | |
1550 | rtnl_unlock(); | |
1551 | ovs_vport_free(vport); | |
1552 | return ERR_CAST(dev); | |
1553 | } | |
1554 | ||
1555 | err = dev_change_flags(dev, dev->flags | IFF_UP); | |
1556 | if (err < 0) { | |
1557 | rtnl_delete_link(dev); | |
1558 | rtnl_unlock(); | |
1559 | ovs_vport_free(vport); | |
1560 | return ERR_PTR(err); | |
1561 | } | |
1562 | ||
1563 | rtnl_unlock(); | |
1564 | return vport; | |
1565 | } | |
1566 | ||
1567 | static struct vport *ipgre_create(const struct vport_parms *parms) | |
1568 | { | |
1569 | struct vport *vport; | |
1570 | ||
1571 | vport = ipgre_tnl_create(parms); | |
1572 | if (IS_ERR(vport)) | |
1573 | return vport; | |
1574 | ||
1575 | return ovs_netdev_link(vport, parms->name); | |
1576 | } | |
1577 | ||
1578 | static struct vport_ops ovs_ipgre_vport_ops = { | |
1579 | .type = OVS_VPORT_TYPE_GRE, | |
1580 | .create = ipgre_create, | |
1581 | .send = gre_fb_xmit, | |
1582 | #ifndef USE_UPSTREAM_TUNNEL | |
1583 | .fill_metadata_dst = gre_fill_metadata_dst, | |
1584 | #endif | |
1585 | .destroy = ovs_netdev_tunnel_destroy, | |
1586 | }; | |
1587 | ||
e23775f2 PS |
1588 | int rpl_ipgre_init(void) |
1589 | { | |
1590 | int err; | |
1591 | ||
1592 | err = register_pernet_device(&ipgre_tap_net_ops); | |
8de03c61 WT |
1593 | if (err < 0) { |
1594 | if (err == -EEXIST) | |
1595 | goto ip_gre_loaded; | |
1596 | else | |
1597 | goto pnet_tap_failed; | |
1598 | } | |
e23775f2 | 1599 | |
8e53509c | 1600 | err = register_pernet_device(&erspan_net_ops); |
8de03c61 WT |
1601 | if (err < 0) { |
1602 | if (err == -EEXIST) | |
1603 | goto ip_gre_loaded; | |
1604 | else | |
1605 | goto pnet_erspan_failed; | |
1606 | } | |
8e53509c | 1607 | |
828677b2 GR |
1608 | err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO); |
1609 | if (err < 0) { | |
1610 | pr_info("%s: can't add protocol\n", __func__); | |
8de03c61 WT |
1611 | if (err == -EBUSY) { |
1612 | goto ip_gre_loaded; | |
1613 | } else { | |
1614 | goto add_proto_failed; | |
1615 | } | |
828677b2 | 1616 | } |
e23775f2 | 1617 | |
e23775f2 | 1618 | pr_info("GRE over IPv4 tunneling driver\n"); |
8de03c61 WT |
1619 | ovs_vport_ops_register(&ovs_ipgre_vport_ops); |
1620 | ovs_vport_ops_register(&ovs_erspan_vport_ops); | |
1621 | return 0; | |
1622 | ||
1623 | ip_gre_loaded: | |
1624 | /* Since GRE only allows single receiver to be registerd, | |
1625 | * we skip here so only gre transmit works, see: | |
1626 | * | |
1627 | * commit 9f57c67c379d88a10e8ad676426fee5ae7341b14 | |
1628 | * Author: Pravin B Shelar <pshelar@nicira.com> | |
1629 | * Date: Fri Aug 7 23:51:52 2015 -0700 | |
1630 | * gre: Remove support for sharing GRE protocol hook | |
1631 | * | |
1632 | * OVS GRE receive part is disabled. | |
1633 | */ | |
1634 | pr_info("GRE TX only over IPv4 tunneling driver\n"); | |
1635 | ip_gre_loaded = true; | |
8e53509c WT |
1636 | ovs_vport_ops_register(&ovs_ipgre_vport_ops); |
1637 | ovs_vport_ops_register(&ovs_erspan_vport_ops); | |
e23775f2 PS |
1638 | return 0; |
1639 | ||
e23775f2 | 1640 | add_proto_failed: |
8e53509c WT |
1641 | unregister_pernet_device(&erspan_net_ops); |
1642 | pnet_erspan_failed: | |
e23775f2 | 1643 | unregister_pernet_device(&ipgre_tap_net_ops); |
f3c31839 | 1644 | pnet_tap_failed: |
258b27d3 | 1645 | pr_err("Error while initializing GRE %d\n", err); |
e23775f2 PS |
1646 | return err; |
1647 | } | |
1648 | ||
1649 | void rpl_ipgre_fini(void) | |
1650 | { | |
8e53509c WT |
1651 | ovs_vport_ops_unregister(&ovs_erspan_vport_ops); |
1652 | ovs_vport_ops_unregister(&ovs_ipgre_vport_ops); | |
8de03c61 WT |
1653 | |
1654 | if (!ip_gre_loaded) { | |
1655 | gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO); | |
8de03c61 WT |
1656 | unregister_pernet_device(&erspan_net_ops); |
1657 | unregister_pernet_device(&ipgre_tap_net_ops); | |
1658 | } | |
e23775f2 PS |
1659 | } |
1660 | ||
1661 | #endif |