fou: eliminate IPv4,v6 specific GRO functions
[deliverable/linux.git] / net / ipv4 / fou.c
1 #include <linux/module.h>
2 #include <linux/errno.h>
3 #include <linux/socket.h>
4 #include <linux/skbuff.h>
5 #include <linux/ip.h>
6 #include <linux/udp.h>
7 #include <linux/types.h>
8 #include <linux/kernel.h>
9 #include <net/genetlink.h>
10 #include <net/ip.h>
11 #include <net/protocol.h>
12 #include <net/udp.h>
13 #include <net/udp_tunnel.h>
14 #include <net/xfrm.h>
15 #include <uapi/linux/fou.h>
16 #include <uapi/linux/genetlink.h>
17
18 static DEFINE_SPINLOCK(fou_lock);
19 static LIST_HEAD(fou_list);
20
21 struct fou {
22 struct socket *sock;
23 u8 protocol;
24 u16 port;
25 struct udp_offload udp_offloads;
26 struct list_head list;
27 };
28
29 struct fou_cfg {
30 u8 protocol;
31 struct udp_port_cfg udp_config;
32 };
33
34 static inline struct fou *fou_from_sock(struct sock *sk)
35 {
36 return sk->sk_user_data;
37 }
38
39 static int fou_udp_encap_recv_deliver(struct sk_buff *skb,
40 u8 protocol, size_t len)
41 {
42 struct iphdr *iph = ip_hdr(skb);
43
44 /* Remove 'len' bytes from the packet (UDP header and
45 * FOU header if present), modify the protocol to the one
46 * we found, and then call rcv_encap.
47 */
48 iph->tot_len = htons(ntohs(iph->tot_len) - len);
49 __skb_pull(skb, len);
50 skb_postpull_rcsum(skb, udp_hdr(skb), len);
51 skb_reset_transport_header(skb);
52
53 return -protocol;
54 }
55
56 static int fou_udp_recv(struct sock *sk, struct sk_buff *skb)
57 {
58 struct fou *fou = fou_from_sock(sk);
59
60 if (!fou)
61 return 1;
62
63 return fou_udp_encap_recv_deliver(skb, fou->protocol,
64 sizeof(struct udphdr));
65 }
66
67 static struct sk_buff **fou_gro_receive(struct sk_buff **head,
68 struct sk_buff *skb)
69 {
70 const struct net_offload *ops;
71 struct sk_buff **pp = NULL;
72 u8 proto = NAPI_GRO_CB(skb)->proto;
73 const struct net_offload **offloads;
74
75 rcu_read_lock();
76 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
77 ops = rcu_dereference(offloads[proto]);
78 if (!ops || !ops->callbacks.gro_receive)
79 goto out_unlock;
80
81 pp = ops->callbacks.gro_receive(head, skb);
82
83 out_unlock:
84 rcu_read_unlock();
85
86 return pp;
87 }
88
89 static int fou_gro_complete(struct sk_buff *skb, int nhoff)
90 {
91 const struct net_offload *ops;
92 u8 proto = NAPI_GRO_CB(skb)->proto;
93 int err = -ENOSYS;
94 const struct net_offload **offloads;
95
96 rcu_read_lock();
97 offloads = NAPI_GRO_CB(skb)->is_ipv6 ? inet6_offloads : inet_offloads;
98 ops = rcu_dereference(offloads[proto]);
99 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
100 goto out_unlock;
101
102 err = ops->callbacks.gro_complete(skb, nhoff);
103
104 out_unlock:
105 rcu_read_unlock();
106
107 return err;
108 }
109
110 static int fou_add_to_port_list(struct fou *fou)
111 {
112 struct fou *fout;
113
114 spin_lock(&fou_lock);
115 list_for_each_entry(fout, &fou_list, list) {
116 if (fou->port == fout->port) {
117 spin_unlock(&fou_lock);
118 return -EALREADY;
119 }
120 }
121
122 list_add(&fou->list, &fou_list);
123 spin_unlock(&fou_lock);
124
125 return 0;
126 }
127
128 static void fou_release(struct fou *fou)
129 {
130 struct socket *sock = fou->sock;
131 struct sock *sk = sock->sk;
132
133 udp_del_offload(&fou->udp_offloads);
134
135 list_del(&fou->list);
136
137 /* Remove hooks into tunnel socket */
138 sk->sk_user_data = NULL;
139
140 sock_release(sock);
141
142 kfree(fou);
143 }
144
145 static int fou_create(struct net *net, struct fou_cfg *cfg,
146 struct socket **sockp)
147 {
148 struct fou *fou = NULL;
149 int err;
150 struct socket *sock = NULL;
151 struct sock *sk;
152
153 /* Open UDP socket */
154 err = udp_sock_create(net, &cfg->udp_config, &sock);
155 if (err < 0)
156 goto error;
157
158 /* Allocate FOU port structure */
159 fou = kzalloc(sizeof(*fou), GFP_KERNEL);
160 if (!fou) {
161 err = -ENOMEM;
162 goto error;
163 }
164
165 sk = sock->sk;
166
167 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
168 fou->protocol = cfg->protocol;
169 fou->port = cfg->udp_config.local_udp_port;
170 udp_sk(sk)->encap_rcv = fou_udp_recv;
171
172 udp_sk(sk)->encap_type = 1;
173 udp_encap_enable();
174
175 sk->sk_user_data = fou;
176 fou->sock = sock;
177
178 udp_set_convert_csum(sk, true);
179
180 sk->sk_allocation = GFP_ATOMIC;
181
182 fou->udp_offloads.callbacks.gro_receive = fou_gro_receive;
183 fou->udp_offloads.callbacks.gro_complete = fou_gro_complete;
184 fou->udp_offloads.port = cfg->udp_config.local_udp_port;
185 fou->udp_offloads.ipproto = cfg->protocol;
186
187 if (cfg->udp_config.family == AF_INET) {
188 err = udp_add_offload(&fou->udp_offloads);
189 if (err)
190 goto error;
191 }
192
193 err = fou_add_to_port_list(fou);
194 if (err)
195 goto error;
196
197 if (sockp)
198 *sockp = sock;
199
200 return 0;
201
202 error:
203 kfree(fou);
204 if (sock)
205 sock_release(sock);
206
207 return err;
208 }
209
210 static int fou_destroy(struct net *net, struct fou_cfg *cfg)
211 {
212 struct fou *fou;
213 u16 port = cfg->udp_config.local_udp_port;
214 int err = -EINVAL;
215
216 spin_lock(&fou_lock);
217 list_for_each_entry(fou, &fou_list, list) {
218 if (fou->port == port) {
219 udp_del_offload(&fou->udp_offloads);
220 fou_release(fou);
221 err = 0;
222 break;
223 }
224 }
225 spin_unlock(&fou_lock);
226
227 return err;
228 }
229
230 static struct genl_family fou_nl_family = {
231 .id = GENL_ID_GENERATE,
232 .hdrsize = 0,
233 .name = FOU_GENL_NAME,
234 .version = FOU_GENL_VERSION,
235 .maxattr = FOU_ATTR_MAX,
236 .netnsok = true,
237 };
238
239 static struct nla_policy fou_nl_policy[FOU_ATTR_MAX + 1] = {
240 [FOU_ATTR_PORT] = { .type = NLA_U16, },
241 [FOU_ATTR_AF] = { .type = NLA_U8, },
242 [FOU_ATTR_IPPROTO] = { .type = NLA_U8, },
243 };
244
245 static int parse_nl_config(struct genl_info *info,
246 struct fou_cfg *cfg)
247 {
248 memset(cfg, 0, sizeof(*cfg));
249
250 cfg->udp_config.family = AF_INET;
251
252 if (info->attrs[FOU_ATTR_AF]) {
253 u8 family = nla_get_u8(info->attrs[FOU_ATTR_AF]);
254
255 if (family != AF_INET && family != AF_INET6)
256 return -EINVAL;
257
258 cfg->udp_config.family = family;
259 }
260
261 if (info->attrs[FOU_ATTR_PORT]) {
262 u16 port = nla_get_u16(info->attrs[FOU_ATTR_PORT]);
263
264 cfg->udp_config.local_udp_port = port;
265 }
266
267 if (info->attrs[FOU_ATTR_IPPROTO])
268 cfg->protocol = nla_get_u8(info->attrs[FOU_ATTR_IPPROTO]);
269
270 return 0;
271 }
272
273 static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
274 {
275 struct fou_cfg cfg;
276 int err;
277
278 err = parse_nl_config(info, &cfg);
279 if (err)
280 return err;
281
282 return fou_create(&init_net, &cfg, NULL);
283 }
284
285 static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
286 {
287 struct fou_cfg cfg;
288
289 parse_nl_config(info, &cfg);
290
291 return fou_destroy(&init_net, &cfg);
292 }
293
294 static const struct genl_ops fou_nl_ops[] = {
295 {
296 .cmd = FOU_CMD_ADD,
297 .doit = fou_nl_cmd_add_port,
298 .policy = fou_nl_policy,
299 .flags = GENL_ADMIN_PERM,
300 },
301 {
302 .cmd = FOU_CMD_DEL,
303 .doit = fou_nl_cmd_rm_port,
304 .policy = fou_nl_policy,
305 .flags = GENL_ADMIN_PERM,
306 },
307 };
308
309 static int __init fou_init(void)
310 {
311 int ret;
312
313 ret = genl_register_family_with_ops(&fou_nl_family,
314 fou_nl_ops);
315
316 return ret;
317 }
318
319 static void __exit fou_fini(void)
320 {
321 struct fou *fou, *next;
322
323 genl_unregister_family(&fou_nl_family);
324
325 /* Close all the FOU sockets */
326
327 spin_lock(&fou_lock);
328 list_for_each_entry_safe(fou, next, &fou_list, list)
329 fou_release(fou);
330 spin_unlock(&fou_lock);
331 }
332
333 module_init(fou_init);
334 module_exit(fou_fini);
335 MODULE_AUTHOR("Tom Herbert <therbert@google.com>");
336 MODULE_LICENSE("GPL");
This page took 0.038651 seconds and 5 git commands to generate.