1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2013 Nicira, Inc.
4 */
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/skbuff.h>
11 #include <linux/netdevice.h>
12 #include <linux/in.h>
13 #include <linux/if_arp.h>
14 #include <linux/init.h>
15 #include <linux/in6.h>
16 #include <linux/inetdevice.h>
17 #include <linux/netfilter_ipv4.h>
18 #include <linux/etherdevice.h>
19 #include <linux/if_ether.h>
20 #include <linux/if_vlan.h>
21 #include <linux/static_key.h>
22
23 #include <net/ip.h>
24 #include <net/icmp.h>
25 #include <net/protocol.h>
26 #include <net/ip_tunnels.h>
27 #include <net/ip6_tunnel.h>
28 #include <net/ip6_checksum.h>
29 #include <net/arp.h>
30 #include <net/checksum.h>
31 #include <net/dsfield.h>
32 #include <net/inet_ecn.h>
33 #include <net/xfrm.h>
34 #include <net/net_namespace.h>
35 #include <net/netns/generic.h>
36 #include <net/rtnetlink.h>
37 #include <net/dst_metadata.h>
38 #include <net/geneve.h>
39 #include <net/vxlan.h>
40 #include <net/erspan.h>
41
42 const struct ip_tunnel_encap_ops __rcu *
43 iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
44 EXPORT_SYMBOL(iptun_encaps);
45
46 const struct ip6_tnl_encap_ops __rcu *
47 ip6tun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
48 EXPORT_SYMBOL(ip6tun_encaps);
49
iptunnel_xmit(struct sock * sk,struct rtable * rt,struct sk_buff * skb,__be32 src,__be32 dst,__u8 proto,__u8 tos,__u8 ttl,__be16 df,bool xnet,u16 ipcb_flags)50 void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
51 __be32 src, __be32 dst, __u8 proto,
52 __u8 tos, __u8 ttl, __be16 df, bool xnet,
53 u16 ipcb_flags)
54 {
55 int pkt_len = skb->len - skb_inner_network_offset(skb);
56 struct net *net = dev_net(rt->dst.dev);
57 struct net_device *dev = skb->dev;
58 struct iphdr *iph;
59 int err;
60
61 if (unlikely(dev_recursion_level() > IP_TUNNEL_RECURSION_LIMIT)) {
62 if (dev) {
63 net_crit_ratelimited("Dead loop on virtual device %s (net %llu), fix it urgently!\n",
64 dev->name, dev_net(dev)->net_cookie);
65 DEV_STATS_INC(dev, tx_errors);
66 }
67 ip_rt_put(rt);
68 kfree_skb_reason(skb, SKB_DROP_REASON_RECURSION_LIMIT);
69 return;
70 }
71
72 dev_xmit_recursion_inc();
73
74 skb_scrub_packet(skb, xnet);
75
76 skb_clear_hash_if_not_l4(skb);
77 skb_dst_set(skb, &rt->dst);
78 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
79 IPCB(skb)->flags = ipcb_flags;
80
81 /* Push down and install the IP header. */
82 skb_push(skb, sizeof(struct iphdr));
83 skb_reset_network_header(skb);
84
85 iph = ip_hdr(skb);
86
87 iph->version = 4;
88 iph->ihl = sizeof(struct iphdr) >> 2;
89 iph->frag_off = ip_mtu_locked(&rt->dst) ? 0 : df;
90 iph->protocol = proto;
91 iph->tos = tos;
92 iph->daddr = dst;
93 iph->saddr = src;
94 iph->ttl = ttl;
95 __ip_select_ident(net, iph, skb_shinfo(skb)->gso_segs ?: 1);
96
97 err = ip_local_out(net, sk, skb);
98
99 if (dev) {
100 if (unlikely(net_xmit_eval(err)))
101 pkt_len = 0;
102 iptunnel_xmit_stats(dev, pkt_len);
103 }
104
105 dev_xmit_recursion_dec();
106 }
107 EXPORT_SYMBOL_GPL(iptunnel_xmit);
108
__iptunnel_pull_header(struct sk_buff * skb,int hdr_len,__be16 inner_proto,bool raw_proto,bool xnet)109 int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
110 __be16 inner_proto, bool raw_proto, bool xnet)
111 {
112 if (unlikely(!pskb_may_pull(skb, hdr_len)))
113 return -ENOMEM;
114
115 skb_pull_rcsum(skb, hdr_len);
116
117 if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
118 struct ethhdr *eh;
119
120 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
121 return -ENOMEM;
122
123 eh = (struct ethhdr *)skb->data;
124 if (likely(eth_proto_is_802_3(eh->h_proto)))
125 skb->protocol = eh->h_proto;
126 else
127 skb->protocol = htons(ETH_P_802_2);
128
129 } else {
130 skb->protocol = inner_proto;
131 }
132
133 skb_clear_hash_if_not_l4(skb);
134 __vlan_hwaccel_clear_tag(skb);
135 skb_set_queue_mapping(skb, 0);
136 skb_scrub_packet(skb, xnet);
137
138 return iptunnel_pull_offloads(skb);
139 }
140 EXPORT_SYMBOL_GPL(__iptunnel_pull_header);
141
iptunnel_metadata_reply(struct metadata_dst * md,gfp_t flags)142 struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
143 gfp_t flags)
144 {
145 IP_TUNNEL_DECLARE_FLAGS(tun_flags) = { };
146 struct metadata_dst *res;
147 struct ip_tunnel_info *dst, *src;
148
149 if (!md || md->type != METADATA_IP_TUNNEL ||
150 md->u.tun_info.mode & IP_TUNNEL_INFO_TX)
151 return NULL;
152
153 src = &md->u.tun_info;
154 res = metadata_dst_alloc(src->options_len, METADATA_IP_TUNNEL, flags);
155 if (!res)
156 return NULL;
157
158 dst = &res->u.tun_info;
159 dst->key.tun_id = src->key.tun_id;
160 if (src->mode & IP_TUNNEL_INFO_IPV6)
161 memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src,
162 sizeof(struct in6_addr));
163 else
164 dst->key.u.ipv4.dst = src->key.u.ipv4.src;
165 ip_tunnel_flags_copy(dst->key.tun_flags, src->key.tun_flags);
166 dst->mode = src->mode | IP_TUNNEL_INFO_TX;
167 ip_tunnel_info_opts_set(dst, ip_tunnel_info_opts(src),
168 src->options_len, tun_flags);
169
170 return res;
171 }
172 EXPORT_SYMBOL_GPL(iptunnel_metadata_reply);
173
iptunnel_handle_offloads(struct sk_buff * skb,int gso_type_mask)174 int iptunnel_handle_offloads(struct sk_buff *skb,
175 int gso_type_mask)
176 {
177 int err;
178
179 if (likely(!skb->encapsulation)) {
180 skb_reset_inner_headers(skb);
181 skb->encapsulation = 1;
182 }
183
184 if (skb_is_gso(skb)) {
185 err = skb_header_unclone(skb, GFP_ATOMIC);
186 if (unlikely(err))
187 return err;
188 skb_shinfo(skb)->gso_type |= gso_type_mask;
189 return 0;
190 }
191
192 if (skb->ip_summed != CHECKSUM_PARTIAL) {
193 skb->ip_summed = CHECKSUM_NONE;
194 /* We clear encapsulation here to prevent badly-written
195 * drivers potentially deciding to offload an inner checksum
196 * if we set CHECKSUM_PARTIAL on the outer header.
197 * This should go away when the drivers are all fixed.
198 */
199 skb->encapsulation = 0;
200 }
201
202 return 0;
203 }
204 EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
205
206 /**
207 * iptunnel_pmtud_build_icmp() - Build ICMP error message for PMTUD
208 * @skb: Original packet with L2 header
209 * @mtu: MTU value for ICMP error
210 *
211 * Return: length on success, negative error code if message couldn't be built.
212 */
iptunnel_pmtud_build_icmp(struct sk_buff * skb,int mtu)213 static int iptunnel_pmtud_build_icmp(struct sk_buff *skb, int mtu)
214 {
215 const struct iphdr *iph;
216 struct icmphdr *icmph;
217 struct iphdr *niph;
218 struct ethhdr eh;
219 int len, err;
220
221 if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct iphdr)))
222 return -EINVAL;
223
224 if (skb_is_gso(skb))
225 skb_gso_reset(skb);
226
227 skb_copy_bits(skb, skb_mac_offset(skb), &eh, ETH_HLEN);
228 pskb_pull(skb, ETH_HLEN);
229
230 err = pskb_trim(skb, 576 - sizeof(*niph) - sizeof(*icmph));
231 if (err)
232 return err;
233
234 len = skb->len + sizeof(*icmph);
235 err = skb_cow(skb, sizeof(*niph) + sizeof(*icmph) + ETH_HLEN);
236 if (err)
237 return err;
238 iph = ip_hdr(skb);
239 icmph = skb_push(skb, sizeof(*icmph));
240 *icmph = (struct icmphdr) {
241 .type = ICMP_DEST_UNREACH,
242 .code = ICMP_FRAG_NEEDED,
243 .checksum = 0,
244 .un.frag.__unused = 0,
245 .un.frag.mtu = htons(mtu),
246 };
247 icmph->checksum = csum_fold(skb_checksum(skb, 0, len, 0));
248 skb_reset_transport_header(skb);
249
250 niph = skb_push(skb, sizeof(*niph));
251 *niph = (struct iphdr) {
252 .ihl = sizeof(*niph) / 4u,
253 .version = 4,
254 .tos = 0,
255 .tot_len = htons(len + sizeof(*niph)),
256 .id = 0,
257 .frag_off = htons(IP_DF),
258 .ttl = iph->ttl,
259 .protocol = IPPROTO_ICMP,
260 .saddr = iph->daddr,
261 .daddr = iph->saddr,
262 };
263 ip_send_check(niph);
264 skb_reset_network_header(skb);
265
266 skb->ip_summed = CHECKSUM_NONE;
267
268 eth_header(skb, skb->dev, ntohs(eh.h_proto), eh.h_source, eh.h_dest, 0);
269 skb_reset_mac_header(skb);
270
271 if (skb_valid_dst(skb))
272 skb_dst_drop(skb);
273
274 return skb->len;
275 }
276
277 /**
278 * iptunnel_pmtud_check_icmp() - Trigger ICMP reply if needed and allowed
279 * @skb: Buffer being sent by encapsulation, L2 headers expected
280 * @mtu: Network MTU for path
281 *
282 * Return: 0 for no ICMP reply, length if built, negative value on error.
283 */
iptunnel_pmtud_check_icmp(struct sk_buff * skb,int mtu)284 static int iptunnel_pmtud_check_icmp(struct sk_buff *skb, int mtu)
285 {
286 const struct iphdr *iph = ip_hdr(skb);
287
288 if (mtu < 576 || iph->frag_off != htons(IP_DF))
289 return 0;
290
291 if (ipv4_is_lbcast(iph->daddr) || ipv4_is_multicast(iph->daddr) ||
292 ipv4_is_zeronet(iph->saddr) || ipv4_is_loopback(iph->saddr) ||
293 ipv4_is_lbcast(iph->saddr) || ipv4_is_multicast(iph->saddr))
294 return 0;
295
296 if (iph->protocol == IPPROTO_ICMP) {
297 const struct icmphdr *icmph;
298
299 if (!pskb_network_may_pull(skb, iph->ihl * 4 +
300 offsetofend(struct icmphdr, type)))
301 return 0;
302 iph = ip_hdr(skb);
303 icmph = (void *)iph + iph->ihl * 4;
304 if (icmp_is_err(icmph->type))
305 return 0;
306 }
307 return iptunnel_pmtud_build_icmp(skb, mtu);
308 }
309
310 #if IS_ENABLED(CONFIG_IPV6)
311 /**
312 * iptunnel_pmtud_build_icmpv6() - Build ICMPv6 error message for PMTUD
313 * @skb: Original packet with L2 header
314 * @mtu: MTU value for ICMPv6 error
315 *
316 * Return: length on success, negative error code if message couldn't be built.
317 */
iptunnel_pmtud_build_icmpv6(struct sk_buff * skb,int mtu)318 static int iptunnel_pmtud_build_icmpv6(struct sk_buff *skb, int mtu)
319 {
320 const struct ipv6hdr *ip6h;
321 struct icmp6hdr *icmp6h;
322 struct ipv6hdr *nip6h;
323 struct ethhdr eh;
324 int len, err;
325 __wsum csum;
326
327 if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct ipv6hdr)))
328 return -EINVAL;
329
330 if (skb_is_gso(skb))
331 skb_gso_reset(skb);
332
333 skb_copy_bits(skb, skb_mac_offset(skb), &eh, ETH_HLEN);
334 pskb_pull(skb, ETH_HLEN);
335
336 err = pskb_trim(skb, IPV6_MIN_MTU - sizeof(*nip6h) - sizeof(*icmp6h));
337 if (err)
338 return err;
339
340 len = skb->len + sizeof(*icmp6h);
341 err = skb_cow(skb, sizeof(*nip6h) + sizeof(*icmp6h) + ETH_HLEN);
342 if (err)
343 return err;
344
345 ip6h = ipv6_hdr(skb);
346 icmp6h = skb_push(skb, sizeof(*icmp6h));
347 *icmp6h = (struct icmp6hdr) {
348 .icmp6_type = ICMPV6_PKT_TOOBIG,
349 .icmp6_code = 0,
350 .icmp6_cksum = 0,
351 .icmp6_mtu = htonl(mtu),
352 };
353 skb_reset_transport_header(skb);
354
355 nip6h = skb_push(skb, sizeof(*nip6h));
356 *nip6h = (struct ipv6hdr) {
357 .priority = 0,
358 .version = 6,
359 .flow_lbl = { 0 },
360 .payload_len = htons(len),
361 .nexthdr = IPPROTO_ICMPV6,
362 .hop_limit = ip6h->hop_limit,
363 .saddr = ip6h->daddr,
364 .daddr = ip6h->saddr,
365 };
366 skb_reset_network_header(skb);
367
368 csum = skb_checksum(skb, skb_transport_offset(skb), len, 0);
369 icmp6h->icmp6_cksum = csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr, len,
370 IPPROTO_ICMPV6, csum);
371
372 skb->ip_summed = CHECKSUM_NONE;
373
374 eth_header(skb, skb->dev, ntohs(eh.h_proto), eh.h_source, eh.h_dest, 0);
375 skb_reset_mac_header(skb);
376
377 if (skb_valid_dst(skb))
378 skb_dst_drop(skb);
379
380 return skb->len;
381 }
382
383 /**
384 * iptunnel_pmtud_check_icmpv6() - Trigger ICMPv6 reply if needed and allowed
385 * @skb: Buffer being sent by encapsulation, L2 headers expected
386 * @mtu: Network MTU for path
387 *
388 * Return: 0 for no ICMPv6 reply, length if built, negative value on error.
389 */
iptunnel_pmtud_check_icmpv6(struct sk_buff * skb,int mtu)390 static int iptunnel_pmtud_check_icmpv6(struct sk_buff *skb, int mtu)
391 {
392 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
393 int stype = ipv6_addr_type(&ip6h->saddr);
394 u8 proto = ip6h->nexthdr;
395 __be16 frag_off;
396 int offset;
397
398 if (mtu < IPV6_MIN_MTU)
399 return 0;
400
401 if (stype == IPV6_ADDR_ANY || stype == IPV6_ADDR_MULTICAST ||
402 stype == IPV6_ADDR_LOOPBACK)
403 return 0;
404
405 offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &proto,
406 &frag_off);
407 if (offset < 0 || (frag_off & htons(~0x7)))
408 return 0;
409
410 if (proto == IPPROTO_ICMPV6) {
411 struct icmp6hdr *icmp6h;
412
413 if (!pskb_may_pull(skb, skb_network_header(skb) +
414 offset + 1 - skb->data))
415 return 0;
416
417 icmp6h = (struct icmp6hdr *)(skb_network_header(skb) + offset);
418 if (icmpv6_is_err(icmp6h->icmp6_type) ||
419 icmp6h->icmp6_type == NDISC_REDIRECT)
420 return 0;
421 }
422
423 return iptunnel_pmtud_build_icmpv6(skb, mtu);
424 }
425 #endif /* IS_ENABLED(CONFIG_IPV6) */
426
427 /**
428 * skb_tunnel_check_pmtu() - Check, update PMTU and trigger ICMP reply as needed
429 * @skb: Buffer being sent by encapsulation, L2 headers expected
430 * @encap_dst: Destination for tunnel encapsulation (outer IP)
431 * @headroom: Encapsulation header size, bytes
432 * @reply: Build matching ICMP or ICMPv6 message as a result
433 *
434 * L2 tunnel implementations that can carry IP and can be directly bridged
435 * (currently UDP tunnels) can't always rely on IP forwarding paths to handle
436 * PMTU discovery. In the bridged case, ICMP or ICMPv6 messages need to be built
437 * based on payload and sent back by the encapsulation itself.
438 *
439 * For routable interfaces, we just need to update the PMTU for the destination.
440 *
441 * Return: 0 if ICMP error not needed, length if built, negative value on error
442 */
skb_tunnel_check_pmtu(struct sk_buff * skb,struct dst_entry * encap_dst,int headroom,bool reply)443 int skb_tunnel_check_pmtu(struct sk_buff *skb, struct dst_entry *encap_dst,
444 int headroom, bool reply)
445 {
446 u32 mtu = dst_mtu(encap_dst) - headroom;
447
448 if ((skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu)) ||
449 (!skb_is_gso(skb) && (skb->len - skb_network_offset(skb)) <= mtu))
450 return 0;
451
452 skb_dst_update_pmtu_no_confirm(skb, mtu);
453
454 if (!reply)
455 return 0;
456
457 if (skb->protocol == htons(ETH_P_IP))
458 return iptunnel_pmtud_check_icmp(skb, mtu);
459
460 #if IS_ENABLED(CONFIG_IPV6)
461 if (skb->protocol == htons(ETH_P_IPV6))
462 return iptunnel_pmtud_check_icmpv6(skb, mtu);
463 #endif
464 return 0;
465 }
466 EXPORT_SYMBOL(skb_tunnel_check_pmtu);
467
468 static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = {
469 [LWTUNNEL_IP_UNSPEC] = { .strict_start_type = LWTUNNEL_IP_OPTS },
470 [LWTUNNEL_IP_ID] = { .type = NLA_U64 },
471 [LWTUNNEL_IP_DST] = { .type = NLA_U32 },
472 [LWTUNNEL_IP_SRC] = { .type = NLA_U32 },
473 [LWTUNNEL_IP_TTL] = { .type = NLA_U8 },
474 [LWTUNNEL_IP_TOS] = { .type = NLA_U8 },
475 [LWTUNNEL_IP_FLAGS] = { .type = NLA_U16 },
476 [LWTUNNEL_IP_OPTS] = { .type = NLA_NESTED },
477 };
478
479 static const struct nla_policy ip_opts_policy[LWTUNNEL_IP_OPTS_MAX + 1] = {
480 [LWTUNNEL_IP_OPTS_GENEVE] = { .type = NLA_NESTED },
481 [LWTUNNEL_IP_OPTS_VXLAN] = { .type = NLA_NESTED },
482 [LWTUNNEL_IP_OPTS_ERSPAN] = { .type = NLA_NESTED },
483 };
484
485 static const struct nla_policy
486 geneve_opt_policy[LWTUNNEL_IP_OPT_GENEVE_MAX + 1] = {
487 [LWTUNNEL_IP_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
488 [LWTUNNEL_IP_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
489 [LWTUNNEL_IP_OPT_GENEVE_DATA] = { .type = NLA_BINARY, .len = 127 },
490 };
491
492 static const struct nla_policy
493 vxlan_opt_policy[LWTUNNEL_IP_OPT_VXLAN_MAX + 1] = {
494 [LWTUNNEL_IP_OPT_VXLAN_GBP] = { .type = NLA_U32 },
495 };
496
497 static const struct nla_policy
498 erspan_opt_policy[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1] = {
499 [LWTUNNEL_IP_OPT_ERSPAN_VER] = { .type = NLA_U8 },
500 [LWTUNNEL_IP_OPT_ERSPAN_INDEX] = { .type = NLA_U32 },
501 [LWTUNNEL_IP_OPT_ERSPAN_DIR] = { .type = NLA_U8 },
502 [LWTUNNEL_IP_OPT_ERSPAN_HWID] = { .type = NLA_U8 },
503 };
504
ip_tun_parse_opts_geneve(struct nlattr * attr,struct ip_tunnel_info * info,int opts_len,struct netlink_ext_ack * extack)505 static int ip_tun_parse_opts_geneve(struct nlattr *attr,
506 struct ip_tunnel_info *info, int opts_len,
507 struct netlink_ext_ack *extack)
508 {
509 struct nlattr *tb[LWTUNNEL_IP_OPT_GENEVE_MAX + 1];
510 int data_len, err;
511
512 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_GENEVE_MAX, attr,
513 geneve_opt_policy, extack);
514 if (err)
515 return err;
516
517 if (!tb[LWTUNNEL_IP_OPT_GENEVE_CLASS] ||
518 !tb[LWTUNNEL_IP_OPT_GENEVE_TYPE] ||
519 !tb[LWTUNNEL_IP_OPT_GENEVE_DATA])
520 return -EINVAL;
521
522 attr = tb[LWTUNNEL_IP_OPT_GENEVE_DATA];
523 data_len = nla_len(attr);
524 if (data_len % 4)
525 return -EINVAL;
526
527 if (info) {
528 struct geneve_opt *opt = ip_tunnel_info_opts(info) + opts_len;
529
530 memcpy(opt->opt_data, nla_data(attr), data_len);
531 opt->length = data_len / 4;
532 attr = tb[LWTUNNEL_IP_OPT_GENEVE_CLASS];
533 opt->opt_class = nla_get_be16(attr);
534 attr = tb[LWTUNNEL_IP_OPT_GENEVE_TYPE];
535 opt->type = nla_get_u8(attr);
536 __set_bit(IP_TUNNEL_GENEVE_OPT_BIT, info->key.tun_flags);
537 }
538
539 return sizeof(struct geneve_opt) + data_len;
540 }
541
ip_tun_parse_opts_vxlan(struct nlattr * attr,struct ip_tunnel_info * info,int opts_len,struct netlink_ext_ack * extack)542 static int ip_tun_parse_opts_vxlan(struct nlattr *attr,
543 struct ip_tunnel_info *info, int opts_len,
544 struct netlink_ext_ack *extack)
545 {
546 struct nlattr *tb[LWTUNNEL_IP_OPT_VXLAN_MAX + 1];
547 int err;
548
549 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_VXLAN_MAX, attr,
550 vxlan_opt_policy, extack);
551 if (err)
552 return err;
553
554 if (!tb[LWTUNNEL_IP_OPT_VXLAN_GBP])
555 return -EINVAL;
556
557 if (info) {
558 struct vxlan_metadata *md =
559 ip_tunnel_info_opts(info) + opts_len;
560
561 attr = tb[LWTUNNEL_IP_OPT_VXLAN_GBP];
562 md->gbp = nla_get_u32(attr);
563 md->gbp &= VXLAN_GBP_MASK;
564 __set_bit(IP_TUNNEL_VXLAN_OPT_BIT, info->key.tun_flags);
565 }
566
567 return sizeof(struct vxlan_metadata);
568 }
569
ip_tun_parse_opts_erspan(struct nlattr * attr,struct ip_tunnel_info * info,int opts_len,struct netlink_ext_ack * extack)570 static int ip_tun_parse_opts_erspan(struct nlattr *attr,
571 struct ip_tunnel_info *info, int opts_len,
572 struct netlink_ext_ack *extack)
573 {
574 struct nlattr *tb[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1];
575 int err;
576 u8 ver;
577
578 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_ERSPAN_MAX, attr,
579 erspan_opt_policy, extack);
580 if (err)
581 return err;
582
583 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_VER])
584 return -EINVAL;
585
586 ver = nla_get_u8(tb[LWTUNNEL_IP_OPT_ERSPAN_VER]);
587 if (ver == 1) {
588 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX])
589 return -EINVAL;
590 } else if (ver == 2) {
591 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_DIR] ||
592 !tb[LWTUNNEL_IP_OPT_ERSPAN_HWID])
593 return -EINVAL;
594 } else {
595 return -EINVAL;
596 }
597
598 if (info) {
599 struct erspan_metadata *md =
600 ip_tunnel_info_opts(info) + opts_len;
601
602 md->version = ver;
603 if (ver == 1) {
604 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX];
605 md->u.index = nla_get_be32(attr);
606 } else {
607 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_DIR];
608 md->u.md2.dir = nla_get_u8(attr);
609 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_HWID];
610 set_hwid(&md->u.md2, nla_get_u8(attr));
611 }
612
613 __set_bit(IP_TUNNEL_ERSPAN_OPT_BIT, info->key.tun_flags);
614 }
615
616 return sizeof(struct erspan_metadata);
617 }
618
ip_tun_parse_opts(struct nlattr * attr,struct ip_tunnel_info * info,struct netlink_ext_ack * extack)619 static int ip_tun_parse_opts(struct nlattr *attr, struct ip_tunnel_info *info,
620 struct netlink_ext_ack *extack)
621 {
622 int err, rem, opt_len, opts_len = 0;
623 struct nlattr *nla;
624 u32 type = 0;
625
626 if (!attr)
627 return 0;
628
629 err = nla_validate(nla_data(attr), nla_len(attr), LWTUNNEL_IP_OPTS_MAX,
630 ip_opts_policy, extack);
631 if (err)
632 return err;
633
634 nla_for_each_attr(nla, nla_data(attr), nla_len(attr), rem) {
635 switch (nla_type(nla)) {
636 case LWTUNNEL_IP_OPTS_GENEVE:
637 if (type && type != IP_TUNNEL_GENEVE_OPT_BIT)
638 return -EINVAL;
639 opt_len = ip_tun_parse_opts_geneve(nla, info, opts_len,
640 extack);
641 if (opt_len < 0)
642 return opt_len;
643 opts_len += opt_len;
644 if (opts_len > IP_TUNNEL_OPTS_MAX)
645 return -EINVAL;
646 type = IP_TUNNEL_GENEVE_OPT_BIT;
647 break;
648 case LWTUNNEL_IP_OPTS_VXLAN:
649 if (type)
650 return -EINVAL;
651 opt_len = ip_tun_parse_opts_vxlan(nla, info, opts_len,
652 extack);
653 if (opt_len < 0)
654 return opt_len;
655 opts_len += opt_len;
656 type = IP_TUNNEL_VXLAN_OPT_BIT;
657 break;
658 case LWTUNNEL_IP_OPTS_ERSPAN:
659 if (type)
660 return -EINVAL;
661 opt_len = ip_tun_parse_opts_erspan(nla, info, opts_len,
662 extack);
663 if (opt_len < 0)
664 return opt_len;
665 opts_len += opt_len;
666 type = IP_TUNNEL_ERSPAN_OPT_BIT;
667 break;
668 default:
669 return -EINVAL;
670 }
671 }
672
673 return opts_len;
674 }
675
ip_tun_get_optlen(struct nlattr * attr,struct netlink_ext_ack * extack)676 static int ip_tun_get_optlen(struct nlattr *attr,
677 struct netlink_ext_ack *extack)
678 {
679 return ip_tun_parse_opts(attr, NULL, extack);
680 }
681
ip_tun_set_opts(struct nlattr * attr,struct ip_tunnel_info * info,int opts_len,struct netlink_ext_ack * extack)682 static int ip_tun_set_opts(struct nlattr *attr, struct ip_tunnel_info *info,
683 int opts_len, struct netlink_ext_ack *extack)
684 {
685 /* `options_len` is the __counted_by() annotation of the `options`
686 * flexible array, it must be initialized before parsing writes
687 * into it.
688 */
689 info->options_len = opts_len;
690
691 return ip_tun_parse_opts(attr, info, extack);
692 }
693
ip_tun_build_state(struct net * net,struct nlattr * attr,unsigned int family,const void * cfg,struct lwtunnel_state ** ts,struct netlink_ext_ack * extack)694 static int ip_tun_build_state(struct net *net, struct nlattr *attr,
695 unsigned int family, const void *cfg,
696 struct lwtunnel_state **ts,
697 struct netlink_ext_ack *extack)
698 {
699 struct nlattr *tb[LWTUNNEL_IP_MAX + 1];
700 struct lwtunnel_state *new_state;
701 struct ip_tunnel_info *tun_info;
702 int err, opt_len;
703
704 err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_MAX, attr,
705 ip_tun_policy, extack);
706 if (err < 0)
707 return err;
708
709 opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP_OPTS], extack);
710 if (opt_len < 0)
711 return opt_len;
712
713 new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
714 if (!new_state)
715 return -ENOMEM;
716
717 new_state->type = LWTUNNEL_ENCAP_IP;
718
719 tun_info = lwt_tun_info(new_state);
720
721 err = ip_tun_set_opts(tb[LWTUNNEL_IP_OPTS], tun_info, opt_len,
722 extack);
723 if (err < 0) {
724 lwtstate_free(new_state);
725 return err;
726 }
727
728 #ifdef CONFIG_DST_CACHE
729 err = dst_cache_init(&tun_info->dst_cache, GFP_KERNEL);
730 if (err) {
731 lwtstate_free(new_state);
732 return err;
733 }
734 #endif
735
736 if (tb[LWTUNNEL_IP_ID])
737 tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]);
738
739 if (tb[LWTUNNEL_IP_DST])
740 tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]);
741
742 if (tb[LWTUNNEL_IP_SRC])
743 tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]);
744
745 if (tb[LWTUNNEL_IP_TTL])
746 tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]);
747
748 if (tb[LWTUNNEL_IP_TOS])
749 tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]);
750
751 if (tb[LWTUNNEL_IP_FLAGS]) {
752 IP_TUNNEL_DECLARE_FLAGS(flags);
753
754 ip_tunnel_flags_from_be16(flags,
755 nla_get_be16(tb[LWTUNNEL_IP_FLAGS]));
756 ip_tunnel_clear_options_present(flags);
757
758 ip_tunnel_flags_or(tun_info->key.tun_flags,
759 tun_info->key.tun_flags, flags);
760 }
761
762 tun_info->mode = IP_TUNNEL_INFO_TX;
763
764 *ts = new_state;
765
766 return 0;
767 }
768
ip_tun_destroy_state(struct lwtunnel_state * lwtstate)769 static void ip_tun_destroy_state(struct lwtunnel_state *lwtstate)
770 {
771 #ifdef CONFIG_DST_CACHE
772 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
773
774 dst_cache_destroy(&tun_info->dst_cache);
775 #endif
776 }
777
ip_tun_fill_encap_opts_geneve(struct sk_buff * skb,struct ip_tunnel_info * tun_info)778 static int ip_tun_fill_encap_opts_geneve(struct sk_buff *skb,
779 struct ip_tunnel_info *tun_info)
780 {
781 struct geneve_opt *opt;
782 struct nlattr *nest;
783 int offset = 0;
784
785 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_GENEVE);
786 if (!nest)
787 return -ENOMEM;
788
789 while (tun_info->options_len > offset) {
790 opt = ip_tunnel_info_opts(tun_info) + offset;
791 if (nla_put_be16(skb, LWTUNNEL_IP_OPT_GENEVE_CLASS,
792 opt->opt_class) ||
793 nla_put_u8(skb, LWTUNNEL_IP_OPT_GENEVE_TYPE, opt->type) ||
794 nla_put(skb, LWTUNNEL_IP_OPT_GENEVE_DATA, opt->length * 4,
795 opt->opt_data)) {
796 nla_nest_cancel(skb, nest);
797 return -ENOMEM;
798 }
799 offset += sizeof(*opt) + opt->length * 4;
800 }
801
802 nla_nest_end(skb, nest);
803 return 0;
804 }
805
ip_tun_fill_encap_opts_vxlan(struct sk_buff * skb,struct ip_tunnel_info * tun_info)806 static int ip_tun_fill_encap_opts_vxlan(struct sk_buff *skb,
807 struct ip_tunnel_info *tun_info)
808 {
809 struct vxlan_metadata *md;
810 struct nlattr *nest;
811
812 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_VXLAN);
813 if (!nest)
814 return -ENOMEM;
815
816 md = ip_tunnel_info_opts(tun_info);
817 if (nla_put_u32(skb, LWTUNNEL_IP_OPT_VXLAN_GBP, md->gbp)) {
818 nla_nest_cancel(skb, nest);
819 return -ENOMEM;
820 }
821
822 nla_nest_end(skb, nest);
823 return 0;
824 }
825
ip_tun_fill_encap_opts_erspan(struct sk_buff * skb,struct ip_tunnel_info * tun_info)826 static int ip_tun_fill_encap_opts_erspan(struct sk_buff *skb,
827 struct ip_tunnel_info *tun_info)
828 {
829 struct erspan_metadata *md;
830 struct nlattr *nest;
831
832 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_ERSPAN);
833 if (!nest)
834 return -ENOMEM;
835
836 md = ip_tunnel_info_opts(tun_info);
837 if (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_VER, md->version))
838 goto err;
839
840 if (md->version == 1 &&
841 nla_put_be32(skb, LWTUNNEL_IP_OPT_ERSPAN_INDEX, md->u.index))
842 goto err;
843
844 if (md->version == 2 &&
845 (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_DIR, md->u.md2.dir) ||
846 nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_HWID,
847 get_hwid(&md->u.md2))))
848 goto err;
849
850 nla_nest_end(skb, nest);
851 return 0;
852 err:
853 nla_nest_cancel(skb, nest);
854 return -ENOMEM;
855 }
856
ip_tun_fill_encap_opts(struct sk_buff * skb,int type,struct ip_tunnel_info * tun_info)857 static int ip_tun_fill_encap_opts(struct sk_buff *skb, int type,
858 struct ip_tunnel_info *tun_info)
859 {
860 struct nlattr *nest;
861 int err = 0;
862
863 if (!ip_tunnel_is_options_present(tun_info->key.tun_flags))
864 return 0;
865
866 nest = nla_nest_start_noflag(skb, type);
867 if (!nest)
868 return -ENOMEM;
869
870 if (test_bit(IP_TUNNEL_GENEVE_OPT_BIT, tun_info->key.tun_flags))
871 err = ip_tun_fill_encap_opts_geneve(skb, tun_info);
872 else if (test_bit(IP_TUNNEL_VXLAN_OPT_BIT, tun_info->key.tun_flags))
873 err = ip_tun_fill_encap_opts_vxlan(skb, tun_info);
874 else if (test_bit(IP_TUNNEL_ERSPAN_OPT_BIT, tun_info->key.tun_flags))
875 err = ip_tun_fill_encap_opts_erspan(skb, tun_info);
876
877 if (err) {
878 nla_nest_cancel(skb, nest);
879 return err;
880 }
881
882 nla_nest_end(skb, nest);
883 return 0;
884 }
885
ip_tun_fill_encap_info(struct sk_buff * skb,struct lwtunnel_state * lwtstate)886 static int ip_tun_fill_encap_info(struct sk_buff *skb,
887 struct lwtunnel_state *lwtstate)
888 {
889 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
890
891 if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id,
892 LWTUNNEL_IP_PAD) ||
893 nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) ||
894 nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) ||
895 nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) ||
896 nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) ||
897 nla_put_be16(skb, LWTUNNEL_IP_FLAGS,
898 ip_tunnel_flags_to_be16(tun_info->key.tun_flags)) ||
899 ip_tun_fill_encap_opts(skb, LWTUNNEL_IP_OPTS, tun_info))
900 return -ENOMEM;
901
902 return 0;
903 }
904
ip_tun_opts_nlsize(struct ip_tunnel_info * info)905 static int ip_tun_opts_nlsize(struct ip_tunnel_info *info)
906 {
907 int opt_len;
908
909 if (!ip_tunnel_is_options_present(info->key.tun_flags))
910 return 0;
911
912 opt_len = nla_total_size(0); /* LWTUNNEL_IP_OPTS */
913 if (test_bit(IP_TUNNEL_GENEVE_OPT_BIT, info->key.tun_flags)) {
914 struct geneve_opt *opt;
915 int offset = 0;
916
917 opt_len += nla_total_size(0); /* LWTUNNEL_IP_OPTS_GENEVE */
918 while (info->options_len > offset) {
919 opt = ip_tunnel_info_opts(info) + offset;
920 opt_len += nla_total_size(2) /* OPT_GENEVE_CLASS */
921 + nla_total_size(1) /* OPT_GENEVE_TYPE */
922 + nla_total_size(opt->length * 4);
923 /* OPT_GENEVE_DATA */
924 offset += sizeof(*opt) + opt->length * 4;
925 }
926 } else if (test_bit(IP_TUNNEL_VXLAN_OPT_BIT, info->key.tun_flags)) {
927 opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_VXLAN */
928 + nla_total_size(4); /* OPT_VXLAN_GBP */
929 } else if (test_bit(IP_TUNNEL_ERSPAN_OPT_BIT, info->key.tun_flags)) {
930 struct erspan_metadata *md = ip_tunnel_info_opts(info);
931
932 opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_ERSPAN */
933 + nla_total_size(1) /* OPT_ERSPAN_VER */
934 + (md->version == 1 ? nla_total_size(4)
935 /* OPT_ERSPAN_INDEX (v1) */
936 : nla_total_size(1) +
937 nla_total_size(1));
938 /* OPT_ERSPAN_DIR + HWID (v2) */
939 }
940
941 return opt_len;
942 }
943
ip_tun_encap_nlsize(struct lwtunnel_state * lwtstate)944 static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
945 {
946 return nla_total_size_64bit(8) /* LWTUNNEL_IP_ID */
947 + nla_total_size(4) /* LWTUNNEL_IP_DST */
948 + nla_total_size(4) /* LWTUNNEL_IP_SRC */
949 + nla_total_size(1) /* LWTUNNEL_IP_TOS */
950 + nla_total_size(1) /* LWTUNNEL_IP_TTL */
951 + nla_total_size(2) /* LWTUNNEL_IP_FLAGS */
952 + ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
953 /* LWTUNNEL_IP_OPTS */
954 }
955
ip_tun_cmp_encap(struct lwtunnel_state * a,struct lwtunnel_state * b)956 static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
957 {
958 struct ip_tunnel_info *info_a = lwt_tun_info(a);
959 struct ip_tunnel_info *info_b = lwt_tun_info(b);
960
961 return memcmp(info_a, info_b, sizeof(info_a->key)) ||
962 info_a->mode != info_b->mode ||
963 info_a->options_len != info_b->options_len ||
964 memcmp(ip_tunnel_info_opts(info_a),
965 ip_tunnel_info_opts(info_b), info_a->options_len);
966 }
967
968 static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
969 .build_state = ip_tun_build_state,
970 .destroy_state = ip_tun_destroy_state,
971 .fill_encap = ip_tun_fill_encap_info,
972 .get_encap_size = ip_tun_encap_nlsize,
973 .cmp_encap = ip_tun_cmp_encap,
974 .owner = THIS_MODULE,
975 };
976
977 static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = {
978 [LWTUNNEL_IP6_UNSPEC] = { .strict_start_type = LWTUNNEL_IP6_OPTS },
979 [LWTUNNEL_IP6_ID] = { .type = NLA_U64 },
980 [LWTUNNEL_IP6_DST] = { .len = sizeof(struct in6_addr) },
981 [LWTUNNEL_IP6_SRC] = { .len = sizeof(struct in6_addr) },
982 [LWTUNNEL_IP6_HOPLIMIT] = { .type = NLA_U8 },
983 [LWTUNNEL_IP6_TC] = { .type = NLA_U8 },
984 [LWTUNNEL_IP6_FLAGS] = { .type = NLA_U16 },
985 [LWTUNNEL_IP6_OPTS] = { .type = NLA_NESTED },
986 };
987
ip6_tun_build_state(struct net * net,struct nlattr * attr,unsigned int family,const void * cfg,struct lwtunnel_state ** ts,struct netlink_ext_ack * extack)988 static int ip6_tun_build_state(struct net *net, struct nlattr *attr,
989 unsigned int family, const void *cfg,
990 struct lwtunnel_state **ts,
991 struct netlink_ext_ack *extack)
992 {
993 struct nlattr *tb[LWTUNNEL_IP6_MAX + 1];
994 struct lwtunnel_state *new_state;
995 struct ip_tunnel_info *tun_info;
996 int err, opt_len;
997
998 err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP6_MAX, attr,
999 ip6_tun_policy, extack);
1000 if (err < 0)
1001 return err;
1002
1003 opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP6_OPTS], extack);
1004 if (opt_len < 0)
1005 return opt_len;
1006
1007 new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
1008 if (!new_state)
1009 return -ENOMEM;
1010
1011 new_state->type = LWTUNNEL_ENCAP_IP6;
1012
1013 tun_info = lwt_tun_info(new_state);
1014
1015 err = ip_tun_set_opts(tb[LWTUNNEL_IP6_OPTS], tun_info, opt_len,
1016 extack);
1017 if (err < 0) {
1018 lwtstate_free(new_state);
1019 return err;
1020 }
1021
1022 if (tb[LWTUNNEL_IP6_ID])
1023 tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]);
1024
1025 if (tb[LWTUNNEL_IP6_DST])
1026 tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]);
1027
1028 if (tb[LWTUNNEL_IP6_SRC])
1029 tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]);
1030
1031 if (tb[LWTUNNEL_IP6_HOPLIMIT])
1032 tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]);
1033
1034 if (tb[LWTUNNEL_IP6_TC])
1035 tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]);
1036
1037 if (tb[LWTUNNEL_IP6_FLAGS]) {
1038 IP_TUNNEL_DECLARE_FLAGS(flags);
1039 __be16 data;
1040
1041 data = nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]);
1042 ip_tunnel_flags_from_be16(flags, data);
1043 ip_tunnel_clear_options_present(flags);
1044
1045 ip_tunnel_flags_or(tun_info->key.tun_flags,
1046 tun_info->key.tun_flags, flags);
1047 }
1048
1049 tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6;
1050
1051 *ts = new_state;
1052
1053 return 0;
1054 }
1055
ip6_tun_fill_encap_info(struct sk_buff * skb,struct lwtunnel_state * lwtstate)1056 static int ip6_tun_fill_encap_info(struct sk_buff *skb,
1057 struct lwtunnel_state *lwtstate)
1058 {
1059 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
1060
1061 if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id,
1062 LWTUNNEL_IP6_PAD) ||
1063 nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) ||
1064 nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) ||
1065 nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) ||
1066 nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) ||
1067 nla_put_be16(skb, LWTUNNEL_IP6_FLAGS,
1068 ip_tunnel_flags_to_be16(tun_info->key.tun_flags)) ||
1069 ip_tun_fill_encap_opts(skb, LWTUNNEL_IP6_OPTS, tun_info))
1070 return -ENOMEM;
1071
1072 return 0;
1073 }
1074
ip6_tun_encap_nlsize(struct lwtunnel_state * lwtstate)1075 static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
1076 {
1077 return nla_total_size_64bit(8) /* LWTUNNEL_IP6_ID */
1078 + nla_total_size(16) /* LWTUNNEL_IP6_DST */
1079 + nla_total_size(16) /* LWTUNNEL_IP6_SRC */
1080 + nla_total_size(1) /* LWTUNNEL_IP6_HOPLIMIT */
1081 + nla_total_size(1) /* LWTUNNEL_IP6_TC */
1082 + nla_total_size(2) /* LWTUNNEL_IP6_FLAGS */
1083 + ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
1084 /* LWTUNNEL_IP6_OPTS */
1085 }
1086
1087 static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
1088 .build_state = ip6_tun_build_state,
1089 .fill_encap = ip6_tun_fill_encap_info,
1090 .get_encap_size = ip6_tun_encap_nlsize,
1091 .cmp_encap = ip_tun_cmp_encap,
1092 .owner = THIS_MODULE,
1093 };
1094
ip_tunnel_core_init(void)1095 void __init ip_tunnel_core_init(void)
1096 {
1097 /* If you land here, make sure whether increasing ip_tunnel_info's
1098 * options_len is a reasonable choice with its usage in front ends
1099 * (f.e., it's part of flow keys, etc).
1100 */
1101 BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255);
1102
1103 lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
1104 lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
1105 }
1106
1107 DEFINE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt);
1108 EXPORT_SYMBOL(ip_tunnel_metadata_cnt);
1109
ip_tunnel_need_metadata(void)1110 void ip_tunnel_need_metadata(void)
1111 {
1112 static_branch_inc(&ip_tunnel_metadata_cnt);
1113 }
1114 EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata);
1115
ip_tunnel_unneed_metadata(void)1116 void ip_tunnel_unneed_metadata(void)
1117 {
1118 static_branch_dec(&ip_tunnel_metadata_cnt);
1119 }
1120 EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);
1121
1122 /* Returns either the correct skb->protocol value, or 0 if invalid. */
ip_tunnel_parse_protocol(const struct sk_buff * skb)1123 __be16 ip_tunnel_parse_protocol(const struct sk_buff *skb)
1124 {
1125 if (skb_network_header(skb) >= skb->head &&
1126 (skb_network_header(skb) + sizeof(struct iphdr)) <= skb_tail_pointer(skb) &&
1127 ip_hdr(skb)->version == 4)
1128 return htons(ETH_P_IP);
1129 if (skb_network_header(skb) >= skb->head &&
1130 (skb_network_header(skb) + sizeof(struct ipv6hdr)) <= skb_tail_pointer(skb) &&
1131 ipv6_hdr(skb)->version == 6)
1132 return htons(ETH_P_IPV6);
1133 return 0;
1134 }
1135 EXPORT_SYMBOL(ip_tunnel_parse_protocol);
1136
1137 const struct header_ops ip_tunnel_header_ops = { .parse_protocol = ip_tunnel_parse_protocol };
1138 EXPORT_SYMBOL(ip_tunnel_header_ops);
1139
1140 /* This function returns true when ENCAP attributes are present in the nl msg */
ip_tunnel_netlink_encap_parms(struct nlattr * data[],struct ip_tunnel_encap * encap)1141 bool ip_tunnel_netlink_encap_parms(struct nlattr *data[],
1142 struct ip_tunnel_encap *encap)
1143 {
1144 bool ret = false;
1145
1146 memset(encap, 0, sizeof(*encap));
1147
1148 if (!data)
1149 return ret;
1150
1151 if (data[IFLA_IPTUN_ENCAP_TYPE]) {
1152 ret = true;
1153 encap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
1154 }
1155
1156 if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
1157 ret = true;
1158 encap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
1159 }
1160
1161 if (data[IFLA_IPTUN_ENCAP_SPORT]) {
1162 ret = true;
1163 encap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
1164 }
1165
1166 if (data[IFLA_IPTUN_ENCAP_DPORT]) {
1167 ret = true;
1168 encap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
1169 }
1170
1171 return ret;
1172 }
1173 EXPORT_SYMBOL_GPL(ip_tunnel_netlink_encap_parms);
1174
ip_tunnel_netlink_parms(struct nlattr * data[],struct ip_tunnel_parm_kern * parms)1175 void ip_tunnel_netlink_parms(struct nlattr *data[],
1176 struct ip_tunnel_parm_kern *parms)
1177 {
1178 if (data[IFLA_IPTUN_LINK])
1179 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1180
1181 if (data[IFLA_IPTUN_LOCAL])
1182 parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
1183
1184 if (data[IFLA_IPTUN_REMOTE])
1185 parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
1186
1187 if (data[IFLA_IPTUN_TTL]) {
1188 parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
1189 if (parms->iph.ttl)
1190 parms->iph.frag_off = htons(IP_DF);
1191 }
1192
1193 if (data[IFLA_IPTUN_TOS])
1194 parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
1195
1196 if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
1197 parms->iph.frag_off = htons(IP_DF);
1198
1199 if (data[IFLA_IPTUN_FLAGS]) {
1200 __be16 flags;
1201
1202 flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
1203 ip_tunnel_flags_from_be16(parms->i_flags, flags);
1204 }
1205
1206 if (data[IFLA_IPTUN_PROTO])
1207 parms->iph.protocol = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1208 }
1209 EXPORT_SYMBOL_GPL(ip_tunnel_netlink_parms);
1210