blob: bd232d3bd022d516ad1ff228a69aaea7c194d752 [file] [log] [blame]
Grégoire Baroneb4d4062010-08-18 13:10:35 +00001/*
2 * Checksum updating actions
3 *
4 * Copyright (c) 2010 Gregoire Baron <baronchon@n7mm.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13#include <linux/types.h>
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/spinlock.h>
18
19#include <linux/netlink.h>
20#include <net/netlink.h>
21#include <linux/rtnetlink.h>
22
23#include <linux/skbuff.h>
24
25#include <net/ip.h>
26#include <net/ipv6.h>
27#include <net/icmp.h>
28#include <linux/icmpv6.h>
29#include <linux/igmp.h>
30#include <net/tcp.h>
31#include <net/udp.h>
Stephen Rothwell24362432010-08-22 20:31:14 -070032#include <net/ip6_checksum.h>
Davide Carattic008b332017-01-09 11:24:21 +010033#include <net/sctp/checksum.h>
Grégoire Baroneb4d4062010-08-18 13:10:35 +000034
35#include <net/act_api.h>
36
37#include <linux/tc_act/tc_csum.h>
38#include <net/tc_act/tc_csum.h>
39
Grégoire Baroneb4d4062010-08-18 13:10:35 +000040static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
41 [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
42};
43
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030044static unsigned int csum_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070045static struct tc_action_ops act_csum_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080046
47static int tcf_csum_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -070048 struct nlattr *est, struct tc_action **a, int ovr,
Vlad Buslov789871b2018-07-05 17:24:25 +030049 int bind, bool rtnl_held,
50 struct netlink_ext_ack *extack)
Grégoire Baroneb4d4062010-08-18 13:10:35 +000051{
WANG Congddf97cc2016-02-22 15:57:53 -080052 struct tc_action_net *tn = net_generic(net, csum_net_id);
Davide Caratti9c5f69b2018-01-22 18:14:32 +010053 struct tcf_csum_params *params_old, *params_new;
Grégoire Baroneb4d4062010-08-18 13:10:35 +000054 struct nlattr *tb[TCA_CSUM_MAX + 1];
55 struct tc_csum *parm;
Grégoire Baroneb4d4062010-08-18 13:10:35 +000056 struct tcf_csum *p;
57 int ret = 0, err;
58
59 if (nla == NULL)
60 return -EINVAL;
61
Johannes Bergfceb6432017-04-12 14:34:07 +020062 err = nla_parse_nested(tb, TCA_CSUM_MAX, nla, csum_policy, NULL);
Grégoire Baroneb4d4062010-08-18 13:10:35 +000063 if (err < 0)
64 return err;
65
66 if (tb[TCA_CSUM_PARMS] == NULL)
67 return -EINVAL;
68 parm = nla_data(tb[TCA_CSUM_PARMS]);
69
Vlad Buslov0190c1d2018-07-05 17:24:32 +030070 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
71 if (!err) {
Chris Mi65a206c2017-08-30 02:31:59 -040072 ret = tcf_idr_create(tn, parm->index, est, a,
Davide Carattif6052cf2018-01-22 18:14:31 +010073 &act_csum_ops, bind, true);
Vlad Buslov0190c1d2018-07-05 17:24:32 +030074 if (ret) {
75 tcf_idr_cleanup(tn, parm->index);
WANG Cong86062032014-02-11 17:07:31 -080076 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +030077 }
Grégoire Baroneb4d4062010-08-18 13:10:35 +000078 ret = ACT_P_CREATED;
Vlad Buslov0190c1d2018-07-05 17:24:32 +030079 } else if (err > 0) {
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050080 if (bind)/* dont override defaults */
81 return 0;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +030082 if (!ovr) {
83 tcf_idr_release(*a, bind);
Grégoire Baroneb4d4062010-08-18 13:10:35 +000084 return -EEXIST;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +030085 }
Vlad Buslov0190c1d2018-07-05 17:24:32 +030086 } else {
87 return err;
Grégoire Baroneb4d4062010-08-18 13:10:35 +000088 }
89
WANG Conga85a9702016-07-25 16:09:41 -070090 p = to_tcf_csum(*a);
Davide Caratti9c5f69b2018-01-22 18:14:32 +010091 ASSERT_RTNL();
92
93 params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
94 if (unlikely(!params_new)) {
Vlad Buslov4e8ddd72018-07-05 17:24:30 +030095 tcf_idr_release(*a, bind);
Davide Caratti9c5f69b2018-01-22 18:14:32 +010096 return -ENOMEM;
97 }
98 params_old = rtnl_dereference(p->params);
99
100 params_new->action = parm->action;
101 params_new->update_flags = parm->update_flags;
102 rcu_assign_pointer(p->params, params_new);
103 if (params_old)
104 kfree_rcu(params_old, rcu);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000105
106 if (ret == ACT_P_CREATED)
Chris Mi65a206c2017-08-30 02:31:59 -0400107 tcf_idr_insert(tn, *a);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000108
109 return ret;
110}
111
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000112/**
113 * tcf_csum_skb_nextlayer - Get next layer pointer
114 * @skb: sk_buff to use
115 * @ihl: previous summed headers length
116 * @ipl: complete packet length
117 * @jhl: next header length
118 *
119 * Check the expected next layer availability in the specified sk_buff.
120 * Return the next layer pointer if pass, NULL otherwise.
121 */
122static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
123 unsigned int ihl, unsigned int ipl,
124 unsigned int jhl)
125{
126 int ntkoff = skb_network_offset(skb);
127 int hl = ihl + jhl;
128
129 if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
Daniel Borkmann36976492016-02-19 23:05:25 +0100130 skb_try_make_writable(skb, hl + ntkoff))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000131 return NULL;
132 else
133 return (void *)(skb_network_header(skb) + ihl);
134}
135
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400136static int tcf_csum_ipv4_icmp(struct sk_buff *skb, unsigned int ihl,
137 unsigned int ipl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000138{
139 struct icmphdr *icmph;
140
141 icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
142 if (icmph == NULL)
143 return 0;
144
145 icmph->checksum = 0;
146 skb->csum = csum_partial(icmph, ipl - ihl, 0);
147 icmph->checksum = csum_fold(skb->csum);
148
149 skb->ip_summed = CHECKSUM_NONE;
150
151 return 1;
152}
153
154static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
155 unsigned int ihl, unsigned int ipl)
156{
157 struct igmphdr *igmph;
158
159 igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
160 if (igmph == NULL)
161 return 0;
162
163 igmph->csum = 0;
164 skb->csum = csum_partial(igmph, ipl - ihl, 0);
165 igmph->csum = csum_fold(skb->csum);
166
167 skb->ip_summed = CHECKSUM_NONE;
168
169 return 1;
170}
171
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400172static int tcf_csum_ipv6_icmp(struct sk_buff *skb, unsigned int ihl,
173 unsigned int ipl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000174{
175 struct icmp6hdr *icmp6h;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700176 const struct ipv6hdr *ip6h;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000177
178 icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
179 if (icmp6h == NULL)
180 return 0;
181
Eric Dumazetd14a4892013-04-12 11:07:47 -0700182 ip6h = ipv6_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000183 icmp6h->icmp6_cksum = 0;
184 skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
185 icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
186 ipl - ihl, IPPROTO_ICMPV6,
187 skb->csum);
188
189 skb->ip_summed = CHECKSUM_NONE;
190
191 return 1;
192}
193
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400194static int tcf_csum_ipv4_tcp(struct sk_buff *skb, unsigned int ihl,
195 unsigned int ipl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000196{
197 struct tcphdr *tcph;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700198 const struct iphdr *iph;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000199
Davide Carattiadd641e2017-03-23 10:39:40 +0100200 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
201 return 1;
202
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000203 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
204 if (tcph == NULL)
205 return 0;
206
Eric Dumazetd14a4892013-04-12 11:07:47 -0700207 iph = ip_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000208 tcph->check = 0;
209 skb->csum = csum_partial(tcph, ipl - ihl, 0);
210 tcph->check = tcp_v4_check(ipl - ihl,
211 iph->saddr, iph->daddr, skb->csum);
212
213 skb->ip_summed = CHECKSUM_NONE;
214
215 return 1;
216}
217
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400218static int tcf_csum_ipv6_tcp(struct sk_buff *skb, unsigned int ihl,
219 unsigned int ipl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000220{
221 struct tcphdr *tcph;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700222 const struct ipv6hdr *ip6h;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000223
Davide Carattiadd641e2017-03-23 10:39:40 +0100224 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
225 return 1;
226
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000227 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
228 if (tcph == NULL)
229 return 0;
230
Eric Dumazetd14a4892013-04-12 11:07:47 -0700231 ip6h = ipv6_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000232 tcph->check = 0;
233 skb->csum = csum_partial(tcph, ipl - ihl, 0);
234 tcph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
235 ipl - ihl, IPPROTO_TCP,
236 skb->csum);
237
238 skb->ip_summed = CHECKSUM_NONE;
239
240 return 1;
241}
242
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400243static int tcf_csum_ipv4_udp(struct sk_buff *skb, unsigned int ihl,
244 unsigned int ipl, int udplite)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000245{
246 struct udphdr *udph;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700247 const struct iphdr *iph;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000248 u16 ul;
249
Willem de Bruijn0c19f8462017-11-21 10:22:25 -0500250 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
251 return 1;
252
Changli Gao0eec32f2010-08-23 03:27:58 +0000253 /*
254 * Support both UDP and UDPLITE checksum algorithms, Don't use
255 * udph->len to get the real length without any protocol check,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000256 * UDPLITE uses udph->len for another thing,
257 * Use iph->tot_len, or just ipl.
258 */
259
260 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
261 if (udph == NULL)
262 return 0;
263
Eric Dumazetd14a4892013-04-12 11:07:47 -0700264 iph = ip_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000265 ul = ntohs(udph->len);
266
267 if (udplite || udph->check) {
268
269 udph->check = 0;
270
271 if (udplite) {
272 if (ul == 0)
273 skb->csum = csum_partial(udph, ipl - ihl, 0);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000274 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
275 skb->csum = csum_partial(udph, ul, 0);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000276 else
277 goto ignore_obscure_skb;
278 } else {
279 if (ul != ipl - ihl)
280 goto ignore_obscure_skb;
281
282 skb->csum = csum_partial(udph, ul, 0);
283 }
284
285 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
286 ul, iph->protocol,
287 skb->csum);
288
289 if (!udph->check)
290 udph->check = CSUM_MANGLED_0;
291 }
292
293 skb->ip_summed = CHECKSUM_NONE;
294
295ignore_obscure_skb:
296 return 1;
297}
298
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400299static int tcf_csum_ipv6_udp(struct sk_buff *skb, unsigned int ihl,
300 unsigned int ipl, int udplite)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000301{
302 struct udphdr *udph;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700303 const struct ipv6hdr *ip6h;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000304 u16 ul;
305
Willem de Bruijn0c19f8462017-11-21 10:22:25 -0500306 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
307 return 1;
308
Changli Gao0eec32f2010-08-23 03:27:58 +0000309 /*
310 * Support both UDP and UDPLITE checksum algorithms, Don't use
311 * udph->len to get the real length without any protocol check,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000312 * UDPLITE uses udph->len for another thing,
313 * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
314 */
315
316 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
317 if (udph == NULL)
318 return 0;
319
Eric Dumazetd14a4892013-04-12 11:07:47 -0700320 ip6h = ipv6_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000321 ul = ntohs(udph->len);
322
323 udph->check = 0;
324
325 if (udplite) {
326 if (ul == 0)
327 skb->csum = csum_partial(udph, ipl - ihl, 0);
328
329 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
330 skb->csum = csum_partial(udph, ul, 0);
331
332 else
333 goto ignore_obscure_skb;
334 } else {
335 if (ul != ipl - ihl)
336 goto ignore_obscure_skb;
337
338 skb->csum = csum_partial(udph, ul, 0);
339 }
340
341 udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
342 udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
343 skb->csum);
344
345 if (!udph->check)
346 udph->check = CSUM_MANGLED_0;
347
348 skb->ip_summed = CHECKSUM_NONE;
349
350ignore_obscure_skb:
351 return 1;
352}
353
Davide Carattic008b332017-01-09 11:24:21 +0100354static int tcf_csum_sctp(struct sk_buff *skb, unsigned int ihl,
355 unsigned int ipl)
356{
357 struct sctphdr *sctph;
358
Daniel Axtens1dd27cd2018-03-09 14:06:09 +1100359 if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
Davide Carattic008b332017-01-09 11:24:21 +0100360 return 1;
361
362 sctph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*sctph));
363 if (!sctph)
364 return 0;
365
366 sctph->checksum = sctp_compute_cksum(skb,
367 skb_network_offset(skb) + ihl);
368 skb->ip_summed = CHECKSUM_NONE;
Davide Carattidba00302017-05-18 15:44:40 +0200369 skb->csum_not_inet = 0;
Davide Carattic008b332017-01-09 11:24:21 +0100370
371 return 1;
372}
373
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000374static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
375{
Eric Dumazetd14a4892013-04-12 11:07:47 -0700376 const struct iphdr *iph;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000377 int ntkoff;
378
379 ntkoff = skb_network_offset(skb);
380
381 if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
382 goto fail;
383
384 iph = ip_hdr(skb);
385
386 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
387 case IPPROTO_ICMP:
388 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
Changli Gao0eec32f2010-08-23 03:27:58 +0000389 if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
390 ntohs(iph->tot_len)))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000391 goto fail;
392 break;
393 case IPPROTO_IGMP:
394 if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
Changli Gao0eec32f2010-08-23 03:27:58 +0000395 if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
396 ntohs(iph->tot_len)))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000397 goto fail;
398 break;
399 case IPPROTO_TCP:
400 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700401 if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4,
Changli Gao0eec32f2010-08-23 03:27:58 +0000402 ntohs(iph->tot_len)))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000403 goto fail;
404 break;
405 case IPPROTO_UDP:
406 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700407 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
Changli Gao0eec32f2010-08-23 03:27:58 +0000408 ntohs(iph->tot_len), 0))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000409 goto fail;
410 break;
411 case IPPROTO_UDPLITE:
412 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700413 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
Changli Gao0eec32f2010-08-23 03:27:58 +0000414 ntohs(iph->tot_len), 1))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000415 goto fail;
416 break;
Davide Carattic008b332017-01-09 11:24:21 +0100417 case IPPROTO_SCTP:
418 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
419 !tcf_csum_sctp(skb, iph->ihl * 4, ntohs(iph->tot_len)))
420 goto fail;
421 break;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000422 }
423
424 if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
Daniel Borkmann36976492016-02-19 23:05:25 +0100425 if (skb_try_make_writable(skb, sizeof(*iph) + ntkoff))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000426 goto fail;
427
Eric Dumazetd14a4892013-04-12 11:07:47 -0700428 ip_send_check(ip_hdr(skb));
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000429 }
430
431 return 1;
432
433fail:
434 return 0;
435}
436
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400437static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh, unsigned int ixhl,
438 unsigned int *pl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000439{
440 int off, len, optlen;
441 unsigned char *xh = (void *)ip6xh;
442
443 off = sizeof(*ip6xh);
444 len = ixhl - off;
445
446 while (len > 1) {
Changli Gao0eec32f2010-08-23 03:27:58 +0000447 switch (xh[off]) {
Eldad Zack1de5a712012-05-17 06:00:25 +0000448 case IPV6_TLV_PAD1:
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000449 optlen = 1;
450 break;
451 case IPV6_TLV_JUMBO:
452 optlen = xh[off + 1] + 2;
453 if (optlen != 6 || len < 6 || (off & 3) != 2)
454 /* wrong jumbo option length/alignment */
455 return 0;
456 *pl = ntohl(*(__be32 *)(xh + off + 2));
457 goto done;
458 default:
459 optlen = xh[off + 1] + 2;
460 if (optlen > len)
461 /* ignore obscure options */
462 goto done;
463 break;
464 }
465 off += optlen;
466 len -= optlen;
467 }
468
469done:
470 return 1;
471}
472
473static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
474{
475 struct ipv6hdr *ip6h;
476 struct ipv6_opt_hdr *ip6xh;
477 unsigned int hl, ixhl;
478 unsigned int pl;
479 int ntkoff;
480 u8 nexthdr;
481
482 ntkoff = skb_network_offset(skb);
483
484 hl = sizeof(*ip6h);
485
486 if (!pskb_may_pull(skb, hl + ntkoff))
487 goto fail;
488
489 ip6h = ipv6_hdr(skb);
490
491 pl = ntohs(ip6h->payload_len);
492 nexthdr = ip6h->nexthdr;
493
494 do {
495 switch (nexthdr) {
496 case NEXTHDR_FRAGMENT:
497 goto ignore_skb;
498 case NEXTHDR_ROUTING:
499 case NEXTHDR_HOP:
500 case NEXTHDR_DEST:
501 if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
502 goto fail;
503 ip6xh = (void *)(skb_network_header(skb) + hl);
504 ixhl = ipv6_optlen(ip6xh);
505 if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
506 goto fail;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700507 ip6xh = (void *)(skb_network_header(skb) + hl);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000508 if ((nexthdr == NEXTHDR_HOP) &&
509 !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
510 goto fail;
511 nexthdr = ip6xh->nexthdr;
512 hl += ixhl;
513 break;
514 case IPPROTO_ICMPV6:
515 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700516 if (!tcf_csum_ipv6_icmp(skb,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000517 hl, pl + sizeof(*ip6h)))
518 goto fail;
519 goto done;
520 case IPPROTO_TCP:
521 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700522 if (!tcf_csum_ipv6_tcp(skb,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000523 hl, pl + sizeof(*ip6h)))
524 goto fail;
525 goto done;
526 case IPPROTO_UDP:
527 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700528 if (!tcf_csum_ipv6_udp(skb, hl,
Changli Gao0eec32f2010-08-23 03:27:58 +0000529 pl + sizeof(*ip6h), 0))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000530 goto fail;
531 goto done;
532 case IPPROTO_UDPLITE:
533 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700534 if (!tcf_csum_ipv6_udp(skb, hl,
Changli Gao0eec32f2010-08-23 03:27:58 +0000535 pl + sizeof(*ip6h), 1))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000536 goto fail;
537 goto done;
Davide Carattic008b332017-01-09 11:24:21 +0100538 case IPPROTO_SCTP:
539 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
540 !tcf_csum_sctp(skb, hl, pl + sizeof(*ip6h)))
541 goto fail;
542 goto done;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000543 default:
544 goto ignore_skb;
545 }
546 } while (pskb_may_pull(skb, hl + 1 + ntkoff));
547
548done:
549ignore_skb:
550 return 1;
551
552fail:
553 return 0;
554}
555
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400556static int tcf_csum(struct sk_buff *skb, const struct tc_action *a,
557 struct tcf_result *res)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000558{
WANG Conga85a9702016-07-25 16:09:41 -0700559 struct tcf_csum *p = to_tcf_csum(a);
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100560 struct tcf_csum_params *params;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000561 u32 update_flags;
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100562 int action;
563
564 rcu_read_lock();
565 params = rcu_dereference(p->params);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000566
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400567 tcf_lastuse_update(&p->tcf_tm);
Davide Carattif6052cf2018-01-22 18:14:31 +0100568 bstats_cpu_update(this_cpu_ptr(p->common.cpu_bstats), skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000569
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100570 action = params->action;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000571 if (unlikely(action == TC_ACT_SHOT))
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100572 goto drop_stats;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000573
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100574 update_flags = params->update_flags;
Jiri Pirkod8b96052015-01-13 17:13:43 +0100575 switch (tc_skb_protocol(skb)) {
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000576 case cpu_to_be16(ETH_P_IP):
577 if (!tcf_csum_ipv4(skb, update_flags))
578 goto drop;
579 break;
580 case cpu_to_be16(ETH_P_IPV6):
581 if (!tcf_csum_ipv6(skb, update_flags))
582 goto drop;
583 break;
584 }
585
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100586unlock:
587 rcu_read_unlock();
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000588 return action;
589
590drop:
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100591 action = TC_ACT_SHOT;
592
593drop_stats:
Davide Carattif6052cf2018-01-22 18:14:31 +0100594 qstats_drop_inc(this_cpu_ptr(p->common.cpu_qstats));
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100595 goto unlock;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000596}
597
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400598static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
599 int ref)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000600{
601 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700602 struct tcf_csum *p = to_tcf_csum(a);
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100603 struct tcf_csum_params *params;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000604 struct tc_csum opt = {
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000605 .index = p->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300606 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
607 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000608 };
609 struct tcf_t t;
610
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100611 params = rtnl_dereference(p->params);
612 opt.action = params->action;
613 opt.update_flags = params->update_flags;
614
David S. Miller1b34ec42012-03-29 05:11:39 -0400615 if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
616 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400617
618 tcf_tm_dump(&t, &p->tcf_tm);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200619 if (nla_put_64bit(skb, TCA_CSUM_TM, sizeof(t), &t, TCA_CSUM_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400620 goto nla_put_failure;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000621
622 return skb->len;
623
624nla_put_failure:
625 nlmsg_trim(skb, b);
626 return -1;
627}
628
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100629static void tcf_csum_cleanup(struct tc_action *a)
630{
631 struct tcf_csum *p = to_tcf_csum(a);
632 struct tcf_csum_params *params;
633
634 params = rcu_dereference_protected(p->params, 1);
Davide Carattiaab378a2018-03-16 00:00:54 +0100635 if (params)
636 kfree_rcu(params, rcu);
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100637}
638
WANG Congddf97cc2016-02-22 15:57:53 -0800639static int tcf_csum_walker(struct net *net, struct sk_buff *skb,
640 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500641 const struct tc_action_ops *ops,
642 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800643{
644 struct tc_action_net *tn = net_generic(net, csum_net_id);
645
Alexander Aringb3620142018-02-15 10:54:59 -0500646 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800647}
648
Alexander Aring331a9292018-02-15 10:54:57 -0500649static int tcf_csum_search(struct net *net, struct tc_action **a, u32 index,
650 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800651{
652 struct tc_action_net *tn = net_generic(net, csum_net_id);
653
Chris Mi65a206c2017-08-30 02:31:59 -0400654 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800655}
656
Craig Dillabaugh29e6eee2018-05-01 10:17:43 -0400657static size_t tcf_csum_get_fill_size(const struct tc_action *act)
658{
659 return nla_total_size(sizeof(struct tc_csum));
660}
661
Vlad Buslovb4090742018-07-05 17:24:28 +0300662static int tcf_csum_delete(struct net *net, u32 index)
663{
664 struct tc_action_net *tn = net_generic(net, csum_net_id);
665
666 return tcf_idr_delete_index(tn, index);
667}
668
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000669static struct tc_action_ops act_csum_ops = {
Changli Gao0eec32f2010-08-23 03:27:58 +0000670 .kind = "csum",
Changli Gao0eec32f2010-08-23 03:27:58 +0000671 .type = TCA_ACT_CSUM,
Changli Gao0eec32f2010-08-23 03:27:58 +0000672 .owner = THIS_MODULE,
673 .act = tcf_csum,
674 .dump = tcf_csum_dump,
Changli Gao0eec32f2010-08-23 03:27:58 +0000675 .init = tcf_csum_init,
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100676 .cleanup = tcf_csum_cleanup,
WANG Congddf97cc2016-02-22 15:57:53 -0800677 .walk = tcf_csum_walker,
678 .lookup = tcf_csum_search,
Craig Dillabaugh29e6eee2018-05-01 10:17:43 -0400679 .get_fill_size = tcf_csum_get_fill_size,
Vlad Buslovb4090742018-07-05 17:24:28 +0300680 .delete = tcf_csum_delete,
WANG Conga85a9702016-07-25 16:09:41 -0700681 .size = sizeof(struct tcf_csum),
WANG Congddf97cc2016-02-22 15:57:53 -0800682};
683
684static __net_init int csum_init_net(struct net *net)
685{
686 struct tc_action_net *tn = net_generic(net, csum_net_id);
687
Cong Wangc7e460c2017-11-06 13:47:18 -0800688 return tc_action_net_init(tn, &act_csum_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800689}
690
Cong Wang039af9c2017-12-11 15:35:03 -0800691static void __net_exit csum_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800692{
Cong Wang039af9c2017-12-11 15:35:03 -0800693 tc_action_net_exit(net_list, csum_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800694}
695
696static struct pernet_operations csum_net_ops = {
697 .init = csum_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800698 .exit_batch = csum_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800699 .id = &csum_net_id,
700 .size = sizeof(struct tc_action_net),
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000701};
702
703MODULE_DESCRIPTION("Checksum updating actions");
704MODULE_LICENSE("GPL");
705
706static int __init csum_init_module(void)
707{
WANG Congddf97cc2016-02-22 15:57:53 -0800708 return tcf_register_action(&act_csum_ops, &csum_net_ops);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000709}
710
711static void __exit csum_cleanup_module(void)
712{
WANG Congddf97cc2016-02-22 15:57:53 -0800713 tcf_unregister_action(&act_csum_ops, &csum_net_ops);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000714}
715
716module_init(csum_init_module);
717module_exit(csum_cleanup_module);