blob: e978ccd4402cbc68ba1c46e20909a047978df1c2 [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
40#define CSUM_TAB_MASK 15
Grégoire Baroneb4d4062010-08-18 13:10:35 +000041
42static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
43 [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
44};
45
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030046static unsigned int csum_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070047static struct tc_action_ops act_csum_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080048
49static int tcf_csum_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -070050 struct nlattr *est, struct tc_action **a, int ovr,
WANG Congddf97cc2016-02-22 15:57:53 -080051 int bind)
Grégoire Baroneb4d4062010-08-18 13:10:35 +000052{
WANG Congddf97cc2016-02-22 15:57:53 -080053 struct tc_action_net *tn = net_generic(net, csum_net_id);
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
Eric Dumazetcc7ec452011-01-19 19:26:56 +000062 err = nla_parse_nested(tb, TCA_CSUM_MAX, nla, csum_policy);
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
WANG Congddf97cc2016-02-22 15:57:53 -080070 if (!tcf_hash_check(tn, parm->index, a, bind)) {
71 ret = tcf_hash_create(tn, parm->index, est, a,
WANG Conga85a9702016-07-25 16:09:41 -070072 &act_csum_ops, bind, false);
WANG Cong86062032014-02-11 17:07:31 -080073 if (ret)
74 return ret;
Grégoire Baroneb4d4062010-08-18 13:10:35 +000075 ret = ACT_P_CREATED;
76 } else {
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050077 if (bind)/* dont override defaults */
78 return 0;
WANG Conga85a9702016-07-25 16:09:41 -070079 tcf_hash_release(*a, bind);
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050080 if (!ovr)
Grégoire Baroneb4d4062010-08-18 13:10:35 +000081 return -EEXIST;
Grégoire Baroneb4d4062010-08-18 13:10:35 +000082 }
83
WANG Conga85a9702016-07-25 16:09:41 -070084 p = to_tcf_csum(*a);
Grégoire Baroneb4d4062010-08-18 13:10:35 +000085 spin_lock_bh(&p->tcf_lock);
86 p->tcf_action = parm->action;
87 p->update_flags = parm->update_flags;
88 spin_unlock_bh(&p->tcf_lock);
89
90 if (ret == ACT_P_CREATED)
WANG Conga85a9702016-07-25 16:09:41 -070091 tcf_hash_insert(tn, *a);
Grégoire Baroneb4d4062010-08-18 13:10:35 +000092
93 return ret;
94}
95
Grégoire Baroneb4d4062010-08-18 13:10:35 +000096/**
97 * tcf_csum_skb_nextlayer - Get next layer pointer
98 * @skb: sk_buff to use
99 * @ihl: previous summed headers length
100 * @ipl: complete packet length
101 * @jhl: next header length
102 *
103 * Check the expected next layer availability in the specified sk_buff.
104 * Return the next layer pointer if pass, NULL otherwise.
105 */
106static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
107 unsigned int ihl, unsigned int ipl,
108 unsigned int jhl)
109{
110 int ntkoff = skb_network_offset(skb);
111 int hl = ihl + jhl;
112
113 if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
Daniel Borkmann36976492016-02-19 23:05:25 +0100114 skb_try_make_writable(skb, hl + ntkoff))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000115 return NULL;
116 else
117 return (void *)(skb_network_header(skb) + ihl);
118}
119
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400120static int tcf_csum_ipv4_icmp(struct sk_buff *skb, unsigned int ihl,
121 unsigned int ipl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000122{
123 struct icmphdr *icmph;
124
125 icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
126 if (icmph == NULL)
127 return 0;
128
129 icmph->checksum = 0;
130 skb->csum = csum_partial(icmph, ipl - ihl, 0);
131 icmph->checksum = csum_fold(skb->csum);
132
133 skb->ip_summed = CHECKSUM_NONE;
134
135 return 1;
136}
137
138static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
139 unsigned int ihl, unsigned int ipl)
140{
141 struct igmphdr *igmph;
142
143 igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
144 if (igmph == NULL)
145 return 0;
146
147 igmph->csum = 0;
148 skb->csum = csum_partial(igmph, ipl - ihl, 0);
149 igmph->csum = csum_fold(skb->csum);
150
151 skb->ip_summed = CHECKSUM_NONE;
152
153 return 1;
154}
155
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400156static int tcf_csum_ipv6_icmp(struct sk_buff *skb, unsigned int ihl,
157 unsigned int ipl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000158{
159 struct icmp6hdr *icmp6h;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700160 const struct ipv6hdr *ip6h;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000161
162 icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
163 if (icmp6h == NULL)
164 return 0;
165
Eric Dumazetd14a4892013-04-12 11:07:47 -0700166 ip6h = ipv6_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000167 icmp6h->icmp6_cksum = 0;
168 skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
169 icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
170 ipl - ihl, IPPROTO_ICMPV6,
171 skb->csum);
172
173 skb->ip_summed = CHECKSUM_NONE;
174
175 return 1;
176}
177
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400178static int tcf_csum_ipv4_tcp(struct sk_buff *skb, unsigned int ihl,
179 unsigned int ipl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000180{
181 struct tcphdr *tcph;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700182 const struct iphdr *iph;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000183
184 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
185 if (tcph == NULL)
186 return 0;
187
Eric Dumazetd14a4892013-04-12 11:07:47 -0700188 iph = ip_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000189 tcph->check = 0;
190 skb->csum = csum_partial(tcph, ipl - ihl, 0);
191 tcph->check = tcp_v4_check(ipl - ihl,
192 iph->saddr, iph->daddr, skb->csum);
193
194 skb->ip_summed = CHECKSUM_NONE;
195
196 return 1;
197}
198
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400199static int tcf_csum_ipv6_tcp(struct sk_buff *skb, unsigned int ihl,
200 unsigned int ipl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000201{
202 struct tcphdr *tcph;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700203 const struct ipv6hdr *ip6h;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000204
205 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
206 if (tcph == NULL)
207 return 0;
208
Eric Dumazetd14a4892013-04-12 11:07:47 -0700209 ip6h = ipv6_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000210 tcph->check = 0;
211 skb->csum = csum_partial(tcph, ipl - ihl, 0);
212 tcph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
213 ipl - ihl, IPPROTO_TCP,
214 skb->csum);
215
216 skb->ip_summed = CHECKSUM_NONE;
217
218 return 1;
219}
220
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400221static int tcf_csum_ipv4_udp(struct sk_buff *skb, unsigned int ihl,
222 unsigned int ipl, int udplite)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000223{
224 struct udphdr *udph;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700225 const struct iphdr *iph;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000226 u16 ul;
227
Changli Gao0eec32f2010-08-23 03:27:58 +0000228 /*
229 * Support both UDP and UDPLITE checksum algorithms, Don't use
230 * udph->len to get the real length without any protocol check,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000231 * UDPLITE uses udph->len for another thing,
232 * Use iph->tot_len, or just ipl.
233 */
234
235 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
236 if (udph == NULL)
237 return 0;
238
Eric Dumazetd14a4892013-04-12 11:07:47 -0700239 iph = ip_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000240 ul = ntohs(udph->len);
241
242 if (udplite || udph->check) {
243
244 udph->check = 0;
245
246 if (udplite) {
247 if (ul == 0)
248 skb->csum = csum_partial(udph, ipl - ihl, 0);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000249 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
250 skb->csum = csum_partial(udph, ul, 0);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000251 else
252 goto ignore_obscure_skb;
253 } else {
254 if (ul != ipl - ihl)
255 goto ignore_obscure_skb;
256
257 skb->csum = csum_partial(udph, ul, 0);
258 }
259
260 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
261 ul, iph->protocol,
262 skb->csum);
263
264 if (!udph->check)
265 udph->check = CSUM_MANGLED_0;
266 }
267
268 skb->ip_summed = CHECKSUM_NONE;
269
270ignore_obscure_skb:
271 return 1;
272}
273
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400274static int tcf_csum_ipv6_udp(struct sk_buff *skb, unsigned int ihl,
275 unsigned int ipl, int udplite)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000276{
277 struct udphdr *udph;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700278 const struct ipv6hdr *ip6h;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000279 u16 ul;
280
Changli Gao0eec32f2010-08-23 03:27:58 +0000281 /*
282 * Support both UDP and UDPLITE checksum algorithms, Don't use
283 * udph->len to get the real length without any protocol check,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000284 * UDPLITE uses udph->len for another thing,
285 * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
286 */
287
288 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
289 if (udph == NULL)
290 return 0;
291
Eric Dumazetd14a4892013-04-12 11:07:47 -0700292 ip6h = ipv6_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000293 ul = ntohs(udph->len);
294
295 udph->check = 0;
296
297 if (udplite) {
298 if (ul == 0)
299 skb->csum = csum_partial(udph, ipl - ihl, 0);
300
301 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
302 skb->csum = csum_partial(udph, ul, 0);
303
304 else
305 goto ignore_obscure_skb;
306 } else {
307 if (ul != ipl - ihl)
308 goto ignore_obscure_skb;
309
310 skb->csum = csum_partial(udph, ul, 0);
311 }
312
313 udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
314 udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
315 skb->csum);
316
317 if (!udph->check)
318 udph->check = CSUM_MANGLED_0;
319
320 skb->ip_summed = CHECKSUM_NONE;
321
322ignore_obscure_skb:
323 return 1;
324}
325
Davide Carattic008b332017-01-09 11:24:21 +0100326static int tcf_csum_sctp(struct sk_buff *skb, unsigned int ihl,
327 unsigned int ipl)
328{
329 struct sctphdr *sctph;
330
331 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_SCTP)
332 return 1;
333
334 sctph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*sctph));
335 if (!sctph)
336 return 0;
337
338 sctph->checksum = sctp_compute_cksum(skb,
339 skb_network_offset(skb) + ihl);
340 skb->ip_summed = CHECKSUM_NONE;
341
342 return 1;
343}
344
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000345static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
346{
Eric Dumazetd14a4892013-04-12 11:07:47 -0700347 const struct iphdr *iph;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000348 int ntkoff;
349
350 ntkoff = skb_network_offset(skb);
351
352 if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
353 goto fail;
354
355 iph = ip_hdr(skb);
356
357 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
358 case IPPROTO_ICMP:
359 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
Changli Gao0eec32f2010-08-23 03:27:58 +0000360 if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
361 ntohs(iph->tot_len)))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000362 goto fail;
363 break;
364 case IPPROTO_IGMP:
365 if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
Changli Gao0eec32f2010-08-23 03:27:58 +0000366 if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
367 ntohs(iph->tot_len)))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000368 goto fail;
369 break;
370 case IPPROTO_TCP:
371 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700372 if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4,
Changli Gao0eec32f2010-08-23 03:27:58 +0000373 ntohs(iph->tot_len)))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000374 goto fail;
375 break;
376 case IPPROTO_UDP:
377 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700378 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
Changli Gao0eec32f2010-08-23 03:27:58 +0000379 ntohs(iph->tot_len), 0))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000380 goto fail;
381 break;
382 case IPPROTO_UDPLITE:
383 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700384 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
Changli Gao0eec32f2010-08-23 03:27:58 +0000385 ntohs(iph->tot_len), 1))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000386 goto fail;
387 break;
Davide Carattic008b332017-01-09 11:24:21 +0100388 case IPPROTO_SCTP:
389 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
390 !tcf_csum_sctp(skb, iph->ihl * 4, ntohs(iph->tot_len)))
391 goto fail;
392 break;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000393 }
394
395 if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
Daniel Borkmann36976492016-02-19 23:05:25 +0100396 if (skb_try_make_writable(skb, sizeof(*iph) + ntkoff))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000397 goto fail;
398
Eric Dumazetd14a4892013-04-12 11:07:47 -0700399 ip_send_check(ip_hdr(skb));
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000400 }
401
402 return 1;
403
404fail:
405 return 0;
406}
407
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400408static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh, unsigned int ixhl,
409 unsigned int *pl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000410{
411 int off, len, optlen;
412 unsigned char *xh = (void *)ip6xh;
413
414 off = sizeof(*ip6xh);
415 len = ixhl - off;
416
417 while (len > 1) {
Changli Gao0eec32f2010-08-23 03:27:58 +0000418 switch (xh[off]) {
Eldad Zack1de5a712012-05-17 06:00:25 +0000419 case IPV6_TLV_PAD1:
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000420 optlen = 1;
421 break;
422 case IPV6_TLV_JUMBO:
423 optlen = xh[off + 1] + 2;
424 if (optlen != 6 || len < 6 || (off & 3) != 2)
425 /* wrong jumbo option length/alignment */
426 return 0;
427 *pl = ntohl(*(__be32 *)(xh + off + 2));
428 goto done;
429 default:
430 optlen = xh[off + 1] + 2;
431 if (optlen > len)
432 /* ignore obscure options */
433 goto done;
434 break;
435 }
436 off += optlen;
437 len -= optlen;
438 }
439
440done:
441 return 1;
442}
443
444static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
445{
446 struct ipv6hdr *ip6h;
447 struct ipv6_opt_hdr *ip6xh;
448 unsigned int hl, ixhl;
449 unsigned int pl;
450 int ntkoff;
451 u8 nexthdr;
452
453 ntkoff = skb_network_offset(skb);
454
455 hl = sizeof(*ip6h);
456
457 if (!pskb_may_pull(skb, hl + ntkoff))
458 goto fail;
459
460 ip6h = ipv6_hdr(skb);
461
462 pl = ntohs(ip6h->payload_len);
463 nexthdr = ip6h->nexthdr;
464
465 do {
466 switch (nexthdr) {
467 case NEXTHDR_FRAGMENT:
468 goto ignore_skb;
469 case NEXTHDR_ROUTING:
470 case NEXTHDR_HOP:
471 case NEXTHDR_DEST:
472 if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
473 goto fail;
474 ip6xh = (void *)(skb_network_header(skb) + hl);
475 ixhl = ipv6_optlen(ip6xh);
476 if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
477 goto fail;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700478 ip6xh = (void *)(skb_network_header(skb) + hl);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000479 if ((nexthdr == NEXTHDR_HOP) &&
480 !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
481 goto fail;
482 nexthdr = ip6xh->nexthdr;
483 hl += ixhl;
484 break;
485 case IPPROTO_ICMPV6:
486 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700487 if (!tcf_csum_ipv6_icmp(skb,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000488 hl, pl + sizeof(*ip6h)))
489 goto fail;
490 goto done;
491 case IPPROTO_TCP:
492 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700493 if (!tcf_csum_ipv6_tcp(skb,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000494 hl, pl + sizeof(*ip6h)))
495 goto fail;
496 goto done;
497 case IPPROTO_UDP:
498 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700499 if (!tcf_csum_ipv6_udp(skb, hl,
Changli Gao0eec32f2010-08-23 03:27:58 +0000500 pl + sizeof(*ip6h), 0))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000501 goto fail;
502 goto done;
503 case IPPROTO_UDPLITE:
504 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700505 if (!tcf_csum_ipv6_udp(skb, hl,
Changli Gao0eec32f2010-08-23 03:27:58 +0000506 pl + sizeof(*ip6h), 1))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000507 goto fail;
508 goto done;
Davide Carattic008b332017-01-09 11:24:21 +0100509 case IPPROTO_SCTP:
510 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
511 !tcf_csum_sctp(skb, hl, pl + sizeof(*ip6h)))
512 goto fail;
513 goto done;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000514 default:
515 goto ignore_skb;
516 }
517 } while (pskb_may_pull(skb, hl + 1 + ntkoff));
518
519done:
520ignore_skb:
521 return 1;
522
523fail:
524 return 0;
525}
526
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400527static int tcf_csum(struct sk_buff *skb, const struct tc_action *a,
528 struct tcf_result *res)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000529{
WANG Conga85a9702016-07-25 16:09:41 -0700530 struct tcf_csum *p = to_tcf_csum(a);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000531 int action;
532 u32 update_flags;
533
534 spin_lock(&p->tcf_lock);
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400535 tcf_lastuse_update(&p->tcf_tm);
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000536 bstats_update(&p->tcf_bstats, skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000537 action = p->tcf_action;
538 update_flags = p->update_flags;
539 spin_unlock(&p->tcf_lock);
540
541 if (unlikely(action == TC_ACT_SHOT))
542 goto drop;
543
Jiri Pirkod8b96052015-01-13 17:13:43 +0100544 switch (tc_skb_protocol(skb)) {
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000545 case cpu_to_be16(ETH_P_IP):
546 if (!tcf_csum_ipv4(skb, update_flags))
547 goto drop;
548 break;
549 case cpu_to_be16(ETH_P_IPV6):
550 if (!tcf_csum_ipv6(skb, update_flags))
551 goto drop;
552 break;
553 }
554
555 return action;
556
557drop:
558 spin_lock(&p->tcf_lock);
559 p->tcf_qstats.drops++;
560 spin_unlock(&p->tcf_lock);
561 return TC_ACT_SHOT;
562}
563
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400564static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
565 int ref)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000566{
567 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700568 struct tcf_csum *p = to_tcf_csum(a);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000569 struct tc_csum opt = {
570 .update_flags = p->update_flags,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000571 .index = p->tcf_index,
572 .action = p->tcf_action,
573 .refcnt = p->tcf_refcnt - ref,
574 .bindcnt = p->tcf_bindcnt - bind,
575 };
576 struct tcf_t t;
577
David S. Miller1b34ec42012-03-29 05:11:39 -0400578 if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
579 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400580
581 tcf_tm_dump(&t, &p->tcf_tm);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200582 if (nla_put_64bit(skb, TCA_CSUM_TM, sizeof(t), &t, TCA_CSUM_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400583 goto nla_put_failure;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000584
585 return skb->len;
586
587nla_put_failure:
588 nlmsg_trim(skb, b);
589 return -1;
590}
591
WANG Congddf97cc2016-02-22 15:57:53 -0800592static int tcf_csum_walker(struct net *net, struct sk_buff *skb,
593 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700594 const struct tc_action_ops *ops)
WANG Congddf97cc2016-02-22 15:57:53 -0800595{
596 struct tc_action_net *tn = net_generic(net, csum_net_id);
597
WANG Conga85a9702016-07-25 16:09:41 -0700598 return tcf_generic_walker(tn, skb, cb, type, ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800599}
600
WANG Conga85a9702016-07-25 16:09:41 -0700601static int tcf_csum_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800602{
603 struct tc_action_net *tn = net_generic(net, csum_net_id);
604
605 return tcf_hash_search(tn, a, index);
606}
607
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000608static struct tc_action_ops act_csum_ops = {
Changli Gao0eec32f2010-08-23 03:27:58 +0000609 .kind = "csum",
Changli Gao0eec32f2010-08-23 03:27:58 +0000610 .type = TCA_ACT_CSUM,
Changli Gao0eec32f2010-08-23 03:27:58 +0000611 .owner = THIS_MODULE,
612 .act = tcf_csum,
613 .dump = tcf_csum_dump,
Changli Gao0eec32f2010-08-23 03:27:58 +0000614 .init = tcf_csum_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800615 .walk = tcf_csum_walker,
616 .lookup = tcf_csum_search,
WANG Conga85a9702016-07-25 16:09:41 -0700617 .size = sizeof(struct tcf_csum),
WANG Congddf97cc2016-02-22 15:57:53 -0800618};
619
620static __net_init int csum_init_net(struct net *net)
621{
622 struct tc_action_net *tn = net_generic(net, csum_net_id);
623
624 return tc_action_net_init(tn, &act_csum_ops, CSUM_TAB_MASK);
625}
626
627static void __net_exit csum_exit_net(struct net *net)
628{
629 struct tc_action_net *tn = net_generic(net, csum_net_id);
630
631 tc_action_net_exit(tn);
632}
633
634static struct pernet_operations csum_net_ops = {
635 .init = csum_init_net,
636 .exit = csum_exit_net,
637 .id = &csum_net_id,
638 .size = sizeof(struct tc_action_net),
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000639};
640
641MODULE_DESCRIPTION("Checksum updating actions");
642MODULE_LICENSE("GPL");
643
644static int __init csum_init_module(void)
645{
WANG Congddf97cc2016-02-22 15:57:53 -0800646 return tcf_register_action(&act_csum_ops, &csum_net_ops);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000647}
648
649static void __exit csum_cleanup_module(void)
650{
WANG Congddf97cc2016-02-22 15:57:53 -0800651 tcf_unregister_action(&act_csum_ops, &csum_net_ops);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000652}
653
654module_init(csum_init_module);
655module_exit(csum_cleanup_module);