Blame view

net/ipv6/fou6.c 5.07 KB
09c434b8a   Thomas Gleixner   treewide: Add SPD...
1
  // SPDX-License-Identifier: GPL-2.0-only
aa3463d65   Tom Herbert   fou: Add encap op...
2
3
4
5
6
7
  #include <linux/module.h>
  #include <linux/errno.h>
  #include <linux/socket.h>
  #include <linux/skbuff.h>
  #include <linux/ip.h>
  #include <linux/udp.h>
b8a51b38e   Stefano Brivio   fou, fou6: ICMP e...
8
  #include <linux/icmpv6.h>
aa3463d65   Tom Herbert   fou: Add encap op...
9
10
11
12
13
14
15
16
17
  #include <linux/types.h>
  #include <linux/kernel.h>
  #include <net/fou.h>
  #include <net/ip.h>
  #include <net/ip6_tunnel.h>
  #include <net/ip6_checksum.h>
  #include <net/protocol.h>
  #include <net/udp.h>
  #include <net/udp_tunnel.h>
9dc621afa   stephen hemminger   fou: make local f...
18
  #if IS_ENABLED(CONFIG_IPV6_FOU_TUNNEL)
aa3463d65   Tom Herbert   fou: Add encap op...
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  static void fou6_build_udp(struct sk_buff *skb, struct ip_tunnel_encap *e,
  			   struct flowi6 *fl6, u8 *protocol, __be16 sport)
  {
  	struct udphdr *uh;
  
  	skb_push(skb, sizeof(struct udphdr));
  	skb_reset_transport_header(skb);
  
  	uh = udp_hdr(skb);
  
  	uh->dest = e->dport;
  	uh->source = sport;
  	uh->len = htons(skb->len);
  	udp6_set_csum(!(e->flags & TUNNEL_ENCAP_FLAG_CSUM6), skb,
  		      &fl6->saddr, &fl6->daddr, skb->len);
  
  	*protocol = IPPROTO_UDP;
  }
9dc621afa   stephen hemminger   fou: make local f...
37
38
  static int fou6_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
  			     u8 *protocol, struct flowi6 *fl6)
aa3463d65   Tom Herbert   fou: Add encap op...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  {
  	__be16 sport;
  	int err;
  	int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM6 ?
  		SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  
  	err = __fou_build_header(skb, e, protocol, &sport, type);
  	if (err)
  		return err;
  
  	fou6_build_udp(skb, e, fl6, protocol, sport);
  
  	return 0;
  }
aa3463d65   Tom Herbert   fou: Add encap op...
53

9dc621afa   stephen hemminger   fou: make local f...
54
55
  static int gue6_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
  			     u8 *protocol, struct flowi6 *fl6)
aa3463d65   Tom Herbert   fou: Add encap op...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  {
  	__be16 sport;
  	int err;
  	int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM6 ?
  		SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  
  	err = __gue_build_header(skb, e, protocol, &sport, type);
  	if (err)
  		return err;
  
  	fou6_build_udp(skb, e, fl6, protocol, sport);
  
  	return 0;
  }
aa3463d65   Tom Herbert   fou: Add encap op...
70

b8a51b38e   Stefano Brivio   fou, fou6: ICMP e...
71
72
  static int gue6_err_proto_handler(int proto, struct sk_buff *skb,
  				  struct inet6_skb_parm *opt,
5de362df4   Paolo Abeni   fou6: fix proto e...
73
  				  u8 type, u8 code, int offset, __be32 info)
b8a51b38e   Stefano Brivio   fou, fou6: ICMP e...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  {
  	const struct inet6_protocol *ipprot;
  
  	ipprot = rcu_dereference(inet6_protos[proto]);
  	if (ipprot && ipprot->err_handler) {
  		if (!ipprot->err_handler(skb, opt, type, code, offset, info))
  			return 0;
  	}
  
  	return -ENOENT;
  }
  
  static int gue6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  		    u8 type, u8 code, int offset, __be32 info)
  {
  	int transport_offset = skb_transport_offset(skb);
  	struct guehdr *guehdr;
26fc181e6   Eric Dumazet   fou, fou6: do not...
91
  	size_t len, optlen;
b8a51b38e   Stefano Brivio   fou, fou6: ICMP e...
92
  	int ret;
26fc181e6   Eric Dumazet   fou, fou6: do not...
93
  	len = sizeof(struct udphdr) + sizeof(struct guehdr);
5355ed638   Eric Dumazet   fou, fou6: avoid ...
94
  	if (!pskb_may_pull(skb, transport_offset + len))
b8a51b38e   Stefano Brivio   fou, fou6: ICMP e...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  		return -EINVAL;
  
  	guehdr = (struct guehdr *)&udp_hdr(skb)[1];
  
  	switch (guehdr->version) {
  	case 0: /* Full GUE header present */
  		break;
  	case 1: {
  		/* Direct encasulation of IPv4 or IPv6 */
  		skb_set_transport_header(skb, -(int)sizeof(struct icmp6hdr));
  
  		switch (((struct iphdr *)guehdr)->version) {
  		case 4:
  			ret = gue6_err_proto_handler(IPPROTO_IPIP, skb, opt,
  						     type, code, offset, info);
  			goto out;
  		case 6:
  			ret = gue6_err_proto_handler(IPPROTO_IPV6, skb, opt,
  						     type, code, offset, info);
  			goto out;
  		default:
  			ret = -EOPNOTSUPP;
  			goto out;
  		}
  	}
  	default: /* Undefined version */
  		return -EOPNOTSUPP;
  	}
  
  	if (guehdr->control)
  		return -ENOENT;
  
  	optlen = guehdr->hlen << 2;
5355ed638   Eric Dumazet   fou, fou6: avoid ...
128
  	if (!pskb_may_pull(skb, transport_offset + len + optlen))
26fc181e6   Eric Dumazet   fou, fou6: do not...
129
130
131
  		return -EINVAL;
  
  	guehdr = (struct guehdr *)&udp_hdr(skb)[1];
b8a51b38e   Stefano Brivio   fou, fou6: ICMP e...
132
133
  	if (validate_gue_flags(guehdr, optlen))
  		return -EINVAL;
44039e001   Stefano Brivio   fou6: Prevent unb...
134
135
136
137
138
139
140
  	/* Handling exceptions for direct UDP encapsulation in GUE would lead to
  	 * recursion. Besides, this kind of encapsulation can't even be
  	 * configured currently. Discard this.
  	 */
  	if (guehdr->proto_ctype == IPPROTO_UDP ||
  	    guehdr->proto_ctype == IPPROTO_UDPLITE)
  		return -EOPNOTSUPP;
b8a51b38e   Stefano Brivio   fou, fou6: ICMP e...
141
142
143
144
145
146
147
148
  	skb_set_transport_header(skb, -(int)sizeof(struct icmp6hdr));
  	ret = gue6_err_proto_handler(guehdr->proto_ctype, skb,
  				     opt, type, code, offset, info);
  
  out:
  	skb_set_transport_header(skb, transport_offset);
  	return ret;
  }
aa3463d65   Tom Herbert   fou: Add encap op...
149
150
151
  static const struct ip6_tnl_encap_ops fou_ip6tun_ops = {
  	.encap_hlen = fou_encap_hlen,
  	.build_header = fou6_build_header,
b8a51b38e   Stefano Brivio   fou, fou6: ICMP e...
152
  	.err_handler = gue6_err,
aa3463d65   Tom Herbert   fou: Add encap op...
153
154
155
156
157
  };
  
  static const struct ip6_tnl_encap_ops gue_ip6tun_ops = {
  	.encap_hlen = gue_encap_hlen,
  	.build_header = gue6_build_header,
b8a51b38e   Stefano Brivio   fou, fou6: ICMP e...
158
  	.err_handler = gue6_err,
aa3463d65   Tom Herbert   fou: Add encap op...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  };
  
  static int ip6_tnl_encap_add_fou_ops(void)
  {
  	int ret;
  
  	ret = ip6_tnl_encap_add_ops(&fou_ip6tun_ops, TUNNEL_ENCAP_FOU);
  	if (ret < 0) {
  		pr_err("can't add fou6 ops
  ");
  		return ret;
  	}
  
  	ret = ip6_tnl_encap_add_ops(&gue_ip6tun_ops, TUNNEL_ENCAP_GUE);
  	if (ret < 0) {
  		pr_err("can't add gue6 ops
  ");
  		ip6_tnl_encap_del_ops(&fou_ip6tun_ops, TUNNEL_ENCAP_FOU);
  		return ret;
  	}
  
  	return 0;
  }
  
  static void ip6_tnl_encap_del_fou_ops(void)
  {
  	ip6_tnl_encap_del_ops(&fou_ip6tun_ops, TUNNEL_ENCAP_FOU);
  	ip6_tnl_encap_del_ops(&gue_ip6tun_ops, TUNNEL_ENCAP_GUE);
  }
  
  #else
  
  static int ip6_tnl_encap_add_fou_ops(void)
  {
  	return 0;
  }
  
  static void ip6_tnl_encap_del_fou_ops(void)
  {
  }
  
  #endif
  
  static int __init fou6_init(void)
  {
  	int ret;
  
  	ret = ip6_tnl_encap_add_fou_ops();
  
  	return ret;
  }
  
  static void __exit fou6_fini(void)
  {
  	ip6_tnl_encap_del_fou_ops();
  }
  
  module_init(fou6_init);
  module_exit(fou6_fini);
  MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
  MODULE_LICENSE("GPL");
67c20de35   Rob Gill   net: Add MODULE_D...
220
  MODULE_DESCRIPTION("Foo over UDP (IPv6)");