blob: 14bb525e355eaa64889d01fee68aaff93618d688 [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>
Davide Carattif5c29d82019-03-20 15:00:01 +010036#include <net/pkt_cls.h>
Grégoire Baroneb4d4062010-08-18 13:10:35 +000037
38#include <linux/tc_act/tc_csum.h>
39#include <net/tc_act/tc_csum.h>
40
Grégoire Baroneb4d4062010-08-18 13:10:35 +000041static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
42 [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
43};
44
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030045static unsigned int csum_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070046static struct tc_action_ops act_csum_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080047
48static int tcf_csum_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -070049 struct nlattr *est, struct tc_action **a, int ovr,
Davide Caratti85d09662019-03-20 14:59:59 +010050 int bind, bool rtnl_held, struct tcf_proto *tp,
Vlad Buslov789871b2018-07-05 17:24:25 +030051 struct netlink_ext_ack *extack)
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);
Vlad Buslovb6a2b972018-08-10 20:51:42 +030054 struct tcf_csum_params *params_new;
Grégoire Baroneb4d4062010-08-18 13:10:35 +000055 struct nlattr *tb[TCA_CSUM_MAX + 1];
Davide Carattif5c29d82019-03-20 15:00:01 +010056 struct tcf_chain *goto_ch = NULL;
Grégoire Baroneb4d4062010-08-18 13:10:35 +000057 struct tc_csum *parm;
Grégoire Baroneb4d4062010-08-18 13:10:35 +000058 struct tcf_csum *p;
59 int ret = 0, err;
60
61 if (nla == NULL)
62 return -EINVAL;
63
Johannes Berg8cb08172019-04-26 14:07:28 +020064 err = nla_parse_nested_deprecated(tb, TCA_CSUM_MAX, nla, csum_policy,
65 NULL);
Grégoire Baroneb4d4062010-08-18 13:10:35 +000066 if (err < 0)
67 return err;
68
69 if (tb[TCA_CSUM_PARMS] == NULL)
70 return -EINVAL;
71 parm = nla_data(tb[TCA_CSUM_PARMS]);
72
Vlad Buslov0190c1d2018-07-05 17:24:32 +030073 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
74 if (!err) {
Chris Mi65a206c2017-08-30 02:31:59 -040075 ret = tcf_idr_create(tn, parm->index, est, a,
Davide Carattif6052cf2018-01-22 18:14:31 +010076 &act_csum_ops, bind, true);
Vlad Buslov0190c1d2018-07-05 17:24:32 +030077 if (ret) {
78 tcf_idr_cleanup(tn, parm->index);
WANG Cong86062032014-02-11 17:07:31 -080079 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +030080 }
Grégoire Baroneb4d4062010-08-18 13:10:35 +000081 ret = ACT_P_CREATED;
Vlad Buslov0190c1d2018-07-05 17:24:32 +030082 } else if (err > 0) {
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050083 if (bind)/* dont override defaults */
84 return 0;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +030085 if (!ovr) {
86 tcf_idr_release(*a, bind);
Grégoire Baroneb4d4062010-08-18 13:10:35 +000087 return -EEXIST;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +030088 }
Vlad Buslov0190c1d2018-07-05 17:24:32 +030089 } else {
90 return err;
Grégoire Baroneb4d4062010-08-18 13:10:35 +000091 }
92
Davide Carattif5c29d82019-03-20 15:00:01 +010093 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
94 if (err < 0)
95 goto release_idr;
96
WANG Conga85a9702016-07-25 16:09:41 -070097 p = to_tcf_csum(*a);
Davide Caratti9c5f69b2018-01-22 18:14:32 +010098
99 params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
100 if (unlikely(!params_new)) {
Davide Carattif5c29d82019-03-20 15:00:01 +0100101 err = -ENOMEM;
102 goto put_chain;
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100103 }
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100104 params_new->update_flags = parm->update_flags;
Vlad Buslovb6a2b972018-08-10 20:51:42 +0300105
Vlad Buslov653cd282018-08-14 21:46:16 +0300106 spin_lock_bh(&p->tcf_lock);
Davide Carattif5c29d82019-03-20 15:00:01 +0100107 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
Vlad Buslovb6a2b972018-08-10 20:51:42 +0300108 rcu_swap_protected(p->params, params_new,
109 lockdep_is_held(&p->tcf_lock));
Vlad Buslov653cd282018-08-14 21:46:16 +0300110 spin_unlock_bh(&p->tcf_lock);
Vlad Buslovb6a2b972018-08-10 20:51:42 +0300111
Davide Carattif5c29d82019-03-20 15:00:01 +0100112 if (goto_ch)
113 tcf_chain_put_by_act(goto_ch);
Vlad Buslovb6a2b972018-08-10 20:51:42 +0300114 if (params_new)
115 kfree_rcu(params_new, rcu);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000116
117 if (ret == ACT_P_CREATED)
Chris Mi65a206c2017-08-30 02:31:59 -0400118 tcf_idr_insert(tn, *a);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000119
120 return ret;
Davide Carattif5c29d82019-03-20 15:00:01 +0100121put_chain:
122 if (goto_ch)
123 tcf_chain_put_by_act(goto_ch);
124release_idr:
125 tcf_idr_release(*a, bind);
126 return err;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000127}
128
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000129/**
130 * tcf_csum_skb_nextlayer - Get next layer pointer
131 * @skb: sk_buff to use
132 * @ihl: previous summed headers length
133 * @ipl: complete packet length
134 * @jhl: next header length
135 *
136 * Check the expected next layer availability in the specified sk_buff.
137 * Return the next layer pointer if pass, NULL otherwise.
138 */
139static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
140 unsigned int ihl, unsigned int ipl,
141 unsigned int jhl)
142{
143 int ntkoff = skb_network_offset(skb);
144 int hl = ihl + jhl;
145
146 if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
Daniel Borkmann36976492016-02-19 23:05:25 +0100147 skb_try_make_writable(skb, hl + ntkoff))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000148 return NULL;
149 else
150 return (void *)(skb_network_header(skb) + ihl);
151}
152
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400153static int tcf_csum_ipv4_icmp(struct sk_buff *skb, unsigned int ihl,
154 unsigned int ipl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000155{
156 struct icmphdr *icmph;
157
158 icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
159 if (icmph == NULL)
160 return 0;
161
162 icmph->checksum = 0;
163 skb->csum = csum_partial(icmph, ipl - ihl, 0);
164 icmph->checksum = csum_fold(skb->csum);
165
166 skb->ip_summed = CHECKSUM_NONE;
167
168 return 1;
169}
170
171static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
172 unsigned int ihl, unsigned int ipl)
173{
174 struct igmphdr *igmph;
175
176 igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
177 if (igmph == NULL)
178 return 0;
179
180 igmph->csum = 0;
181 skb->csum = csum_partial(igmph, ipl - ihl, 0);
182 igmph->csum = csum_fold(skb->csum);
183
184 skb->ip_summed = CHECKSUM_NONE;
185
186 return 1;
187}
188
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400189static int tcf_csum_ipv6_icmp(struct sk_buff *skb, unsigned int ihl,
190 unsigned int ipl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000191{
192 struct icmp6hdr *icmp6h;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700193 const struct ipv6hdr *ip6h;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000194
195 icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
196 if (icmp6h == NULL)
197 return 0;
198
Eric Dumazetd14a4892013-04-12 11:07:47 -0700199 ip6h = ipv6_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000200 icmp6h->icmp6_cksum = 0;
201 skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
202 icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
203 ipl - ihl, IPPROTO_ICMPV6,
204 skb->csum);
205
206 skb->ip_summed = CHECKSUM_NONE;
207
208 return 1;
209}
210
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400211static int tcf_csum_ipv4_tcp(struct sk_buff *skb, unsigned int ihl,
212 unsigned int ipl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000213{
214 struct tcphdr *tcph;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700215 const struct iphdr *iph;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000216
Davide Carattiadd641e2017-03-23 10:39:40 +0100217 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
218 return 1;
219
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000220 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
221 if (tcph == NULL)
222 return 0;
223
Eric Dumazetd14a4892013-04-12 11:07:47 -0700224 iph = ip_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000225 tcph->check = 0;
226 skb->csum = csum_partial(tcph, ipl - ihl, 0);
227 tcph->check = tcp_v4_check(ipl - ihl,
228 iph->saddr, iph->daddr, skb->csum);
229
230 skb->ip_summed = CHECKSUM_NONE;
231
232 return 1;
233}
234
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400235static int tcf_csum_ipv6_tcp(struct sk_buff *skb, unsigned int ihl,
236 unsigned int ipl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000237{
238 struct tcphdr *tcph;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700239 const struct ipv6hdr *ip6h;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000240
Davide Carattiadd641e2017-03-23 10:39:40 +0100241 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
242 return 1;
243
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000244 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
245 if (tcph == NULL)
246 return 0;
247
Eric Dumazetd14a4892013-04-12 11:07:47 -0700248 ip6h = ipv6_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000249 tcph->check = 0;
250 skb->csum = csum_partial(tcph, ipl - ihl, 0);
251 tcph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
252 ipl - ihl, IPPROTO_TCP,
253 skb->csum);
254
255 skb->ip_summed = CHECKSUM_NONE;
256
257 return 1;
258}
259
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400260static int tcf_csum_ipv4_udp(struct sk_buff *skb, unsigned int ihl,
261 unsigned int ipl, int udplite)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000262{
263 struct udphdr *udph;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700264 const struct iphdr *iph;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000265 u16 ul;
266
Willem de Bruijn0c19f8462017-11-21 10:22:25 -0500267 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
268 return 1;
269
Changli Gao0eec32f2010-08-23 03:27:58 +0000270 /*
271 * Support both UDP and UDPLITE checksum algorithms, Don't use
272 * udph->len to get the real length without any protocol check,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000273 * UDPLITE uses udph->len for another thing,
274 * Use iph->tot_len, or just ipl.
275 */
276
277 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
278 if (udph == NULL)
279 return 0;
280
Eric Dumazetd14a4892013-04-12 11:07:47 -0700281 iph = ip_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000282 ul = ntohs(udph->len);
283
284 if (udplite || udph->check) {
285
286 udph->check = 0;
287
288 if (udplite) {
289 if (ul == 0)
290 skb->csum = csum_partial(udph, ipl - ihl, 0);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000291 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
292 skb->csum = csum_partial(udph, ul, 0);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000293 else
294 goto ignore_obscure_skb;
295 } else {
296 if (ul != ipl - ihl)
297 goto ignore_obscure_skb;
298
299 skb->csum = csum_partial(udph, ul, 0);
300 }
301
302 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
303 ul, iph->protocol,
304 skb->csum);
305
306 if (!udph->check)
307 udph->check = CSUM_MANGLED_0;
308 }
309
310 skb->ip_summed = CHECKSUM_NONE;
311
312ignore_obscure_skb:
313 return 1;
314}
315
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400316static int tcf_csum_ipv6_udp(struct sk_buff *skb, unsigned int ihl,
317 unsigned int ipl, int udplite)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000318{
319 struct udphdr *udph;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700320 const struct ipv6hdr *ip6h;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000321 u16 ul;
322
Willem de Bruijn0c19f8462017-11-21 10:22:25 -0500323 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
324 return 1;
325
Changli Gao0eec32f2010-08-23 03:27:58 +0000326 /*
327 * Support both UDP and UDPLITE checksum algorithms, Don't use
328 * udph->len to get the real length without any protocol check,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000329 * UDPLITE uses udph->len for another thing,
330 * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
331 */
332
333 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
334 if (udph == NULL)
335 return 0;
336
Eric Dumazetd14a4892013-04-12 11:07:47 -0700337 ip6h = ipv6_hdr(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000338 ul = ntohs(udph->len);
339
340 udph->check = 0;
341
342 if (udplite) {
343 if (ul == 0)
344 skb->csum = csum_partial(udph, ipl - ihl, 0);
345
346 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
347 skb->csum = csum_partial(udph, ul, 0);
348
349 else
350 goto ignore_obscure_skb;
351 } else {
352 if (ul != ipl - ihl)
353 goto ignore_obscure_skb;
354
355 skb->csum = csum_partial(udph, ul, 0);
356 }
357
358 udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
359 udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
360 skb->csum);
361
362 if (!udph->check)
363 udph->check = CSUM_MANGLED_0;
364
365 skb->ip_summed = CHECKSUM_NONE;
366
367ignore_obscure_skb:
368 return 1;
369}
370
Davide Carattic008b332017-01-09 11:24:21 +0100371static int tcf_csum_sctp(struct sk_buff *skb, unsigned int ihl,
372 unsigned int ipl)
373{
374 struct sctphdr *sctph;
375
Daniel Axtens1dd27cd2018-03-09 14:06:09 +1100376 if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
Davide Carattic008b332017-01-09 11:24:21 +0100377 return 1;
378
379 sctph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*sctph));
380 if (!sctph)
381 return 0;
382
383 sctph->checksum = sctp_compute_cksum(skb,
384 skb_network_offset(skb) + ihl);
385 skb->ip_summed = CHECKSUM_NONE;
Davide Carattidba00302017-05-18 15:44:40 +0200386 skb->csum_not_inet = 0;
Davide Carattic008b332017-01-09 11:24:21 +0100387
388 return 1;
389}
390
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000391static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
392{
Eric Dumazetd14a4892013-04-12 11:07:47 -0700393 const struct iphdr *iph;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000394 int ntkoff;
395
396 ntkoff = skb_network_offset(skb);
397
398 if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
399 goto fail;
400
401 iph = ip_hdr(skb);
402
403 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
404 case IPPROTO_ICMP:
405 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
Changli Gao0eec32f2010-08-23 03:27:58 +0000406 if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
407 ntohs(iph->tot_len)))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000408 goto fail;
409 break;
410 case IPPROTO_IGMP:
411 if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
Changli Gao0eec32f2010-08-23 03:27:58 +0000412 if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
413 ntohs(iph->tot_len)))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000414 goto fail;
415 break;
416 case IPPROTO_TCP:
417 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700418 if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4,
Changli Gao0eec32f2010-08-23 03:27:58 +0000419 ntohs(iph->tot_len)))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000420 goto fail;
421 break;
422 case IPPROTO_UDP:
423 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700424 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
Changli Gao0eec32f2010-08-23 03:27:58 +0000425 ntohs(iph->tot_len), 0))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000426 goto fail;
427 break;
428 case IPPROTO_UDPLITE:
429 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700430 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
Changli Gao0eec32f2010-08-23 03:27:58 +0000431 ntohs(iph->tot_len), 1))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000432 goto fail;
433 break;
Davide Carattic008b332017-01-09 11:24:21 +0100434 case IPPROTO_SCTP:
435 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
436 !tcf_csum_sctp(skb, iph->ihl * 4, ntohs(iph->tot_len)))
437 goto fail;
438 break;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000439 }
440
441 if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
Daniel Borkmann36976492016-02-19 23:05:25 +0100442 if (skb_try_make_writable(skb, sizeof(*iph) + ntkoff))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000443 goto fail;
444
Eric Dumazetd14a4892013-04-12 11:07:47 -0700445 ip_send_check(ip_hdr(skb));
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000446 }
447
448 return 1;
449
450fail:
451 return 0;
452}
453
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400454static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh, unsigned int ixhl,
455 unsigned int *pl)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000456{
457 int off, len, optlen;
458 unsigned char *xh = (void *)ip6xh;
459
460 off = sizeof(*ip6xh);
461 len = ixhl - off;
462
463 while (len > 1) {
Changli Gao0eec32f2010-08-23 03:27:58 +0000464 switch (xh[off]) {
Eldad Zack1de5a712012-05-17 06:00:25 +0000465 case IPV6_TLV_PAD1:
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000466 optlen = 1;
467 break;
468 case IPV6_TLV_JUMBO:
469 optlen = xh[off + 1] + 2;
470 if (optlen != 6 || len < 6 || (off & 3) != 2)
471 /* wrong jumbo option length/alignment */
472 return 0;
473 *pl = ntohl(*(__be32 *)(xh + off + 2));
474 goto done;
475 default:
476 optlen = xh[off + 1] + 2;
477 if (optlen > len)
478 /* ignore obscure options */
479 goto done;
480 break;
481 }
482 off += optlen;
483 len -= optlen;
484 }
485
486done:
487 return 1;
488}
489
490static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
491{
492 struct ipv6hdr *ip6h;
493 struct ipv6_opt_hdr *ip6xh;
494 unsigned int hl, ixhl;
495 unsigned int pl;
496 int ntkoff;
497 u8 nexthdr;
498
499 ntkoff = skb_network_offset(skb);
500
501 hl = sizeof(*ip6h);
502
503 if (!pskb_may_pull(skb, hl + ntkoff))
504 goto fail;
505
506 ip6h = ipv6_hdr(skb);
507
508 pl = ntohs(ip6h->payload_len);
509 nexthdr = ip6h->nexthdr;
510
511 do {
512 switch (nexthdr) {
513 case NEXTHDR_FRAGMENT:
514 goto ignore_skb;
515 case NEXTHDR_ROUTING:
516 case NEXTHDR_HOP:
517 case NEXTHDR_DEST:
518 if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
519 goto fail;
520 ip6xh = (void *)(skb_network_header(skb) + hl);
521 ixhl = ipv6_optlen(ip6xh);
522 if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
523 goto fail;
Eric Dumazetd14a4892013-04-12 11:07:47 -0700524 ip6xh = (void *)(skb_network_header(skb) + hl);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000525 if ((nexthdr == NEXTHDR_HOP) &&
526 !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
527 goto fail;
528 nexthdr = ip6xh->nexthdr;
529 hl += ixhl;
530 break;
531 case IPPROTO_ICMPV6:
532 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700533 if (!tcf_csum_ipv6_icmp(skb,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000534 hl, pl + sizeof(*ip6h)))
535 goto fail;
536 goto done;
537 case IPPROTO_TCP:
538 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700539 if (!tcf_csum_ipv6_tcp(skb,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000540 hl, pl + sizeof(*ip6h)))
541 goto fail;
542 goto done;
543 case IPPROTO_UDP:
544 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700545 if (!tcf_csum_ipv6_udp(skb, hl,
Changli Gao0eec32f2010-08-23 03:27:58 +0000546 pl + sizeof(*ip6h), 0))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000547 goto fail;
548 goto done;
549 case IPPROTO_UDPLITE:
550 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
Eric Dumazetd14a4892013-04-12 11:07:47 -0700551 if (!tcf_csum_ipv6_udp(skb, hl,
Changli Gao0eec32f2010-08-23 03:27:58 +0000552 pl + sizeof(*ip6h), 1))
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000553 goto fail;
554 goto done;
Davide Carattic008b332017-01-09 11:24:21 +0100555 case IPPROTO_SCTP:
556 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
557 !tcf_csum_sctp(skb, hl, pl + sizeof(*ip6h)))
558 goto fail;
559 goto done;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000560 default:
561 goto ignore_skb;
562 }
563 } while (pskb_may_pull(skb, hl + 1 + ntkoff));
564
565done:
566ignore_skb:
567 return 1;
568
569fail:
570 return 0;
571}
572
Jamal Hadi Salimc8315492018-08-12 09:34:51 -0400573static int tcf_csum_act(struct sk_buff *skb, const struct tc_action *a,
574 struct tcf_result *res)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000575{
WANG Conga85a9702016-07-25 16:09:41 -0700576 struct tcf_csum *p = to_tcf_csum(a);
Eli Britstein2ecba2d2019-02-26 09:57:34 +0000577 bool orig_vlan_tag_present = false;
578 unsigned int vlan_hdr_count = 0;
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100579 struct tcf_csum_params *params;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000580 u32 update_flags;
Eli Britstein2ecba2d2019-02-26 09:57:34 +0000581 __be16 protocol;
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100582 int action;
583
Paolo Abeni7fd4b282018-07-30 14:30:43 +0200584 params = rcu_dereference_bh(p->params);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000585
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400586 tcf_lastuse_update(&p->tcf_tm);
Davide Carattif6052cf2018-01-22 18:14:31 +0100587 bstats_cpu_update(this_cpu_ptr(p->common.cpu_bstats), skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000588
Davide Caratti11a245e2018-07-06 21:01:05 +0200589 action = READ_ONCE(p->tcf_action);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000590 if (unlikely(action == TC_ACT_SHOT))
Paolo Abeni7fd4b282018-07-30 14:30:43 +0200591 goto drop;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000592
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100593 update_flags = params->update_flags;
Eli Britstein2ecba2d2019-02-26 09:57:34 +0000594 protocol = tc_skb_protocol(skb);
595again:
596 switch (protocol) {
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000597 case cpu_to_be16(ETH_P_IP):
598 if (!tcf_csum_ipv4(skb, update_flags))
599 goto drop;
600 break;
601 case cpu_to_be16(ETH_P_IPV6):
602 if (!tcf_csum_ipv6(skb, update_flags))
603 goto drop;
604 break;
Eli Britstein2ecba2d2019-02-26 09:57:34 +0000605 case cpu_to_be16(ETH_P_8021AD): /* fall through */
606 case cpu_to_be16(ETH_P_8021Q):
607 if (skb_vlan_tag_present(skb) && !orig_vlan_tag_present) {
608 protocol = skb->protocol;
609 orig_vlan_tag_present = true;
610 } else {
611 struct vlan_hdr *vlan = (struct vlan_hdr *)skb->data;
612
613 protocol = vlan->h_vlan_encapsulated_proto;
614 skb_pull(skb, VLAN_HLEN);
615 skb_reset_network_header(skb);
616 vlan_hdr_count++;
617 }
618 goto again;
619 }
620
621out:
622 /* Restore the skb for the pulled VLAN tags */
623 while (vlan_hdr_count--) {
624 skb_push(skb, VLAN_HLEN);
625 skb_reset_network_header(skb);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000626 }
627
628 return action;
629
630drop:
Davide Carattif6052cf2018-01-22 18:14:31 +0100631 qstats_drop_inc(this_cpu_ptr(p->common.cpu_qstats));
Eli Britstein2ecba2d2019-02-26 09:57:34 +0000632 action = TC_ACT_SHOT;
633 goto out;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000634}
635
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400636static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
637 int ref)
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000638{
639 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700640 struct tcf_csum *p = to_tcf_csum(a);
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100641 struct tcf_csum_params *params;
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000642 struct tc_csum opt = {
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000643 .index = p->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300644 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
645 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000646 };
647 struct tcf_t t;
648
Vlad Buslov653cd282018-08-14 21:46:16 +0300649 spin_lock_bh(&p->tcf_lock);
Vlad Buslovb6a2b972018-08-10 20:51:42 +0300650 params = rcu_dereference_protected(p->params,
651 lockdep_is_held(&p->tcf_lock));
652 opt.action = p->tcf_action;
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100653 opt.update_flags = params->update_flags;
654
David S. Miller1b34ec42012-03-29 05:11:39 -0400655 if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
656 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400657
658 tcf_tm_dump(&t, &p->tcf_tm);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200659 if (nla_put_64bit(skb, TCA_CSUM_TM, sizeof(t), &t, TCA_CSUM_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400660 goto nla_put_failure;
Vlad Buslov653cd282018-08-14 21:46:16 +0300661 spin_unlock_bh(&p->tcf_lock);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000662
663 return skb->len;
664
665nla_put_failure:
Vlad Buslov653cd282018-08-14 21:46:16 +0300666 spin_unlock_bh(&p->tcf_lock);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000667 nlmsg_trim(skb, b);
668 return -1;
669}
670
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100671static void tcf_csum_cleanup(struct tc_action *a)
672{
673 struct tcf_csum *p = to_tcf_csum(a);
674 struct tcf_csum_params *params;
675
676 params = rcu_dereference_protected(p->params, 1);
Davide Carattiaab378a2018-03-16 00:00:54 +0100677 if (params)
678 kfree_rcu(params, rcu);
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100679}
680
WANG Congddf97cc2016-02-22 15:57:53 -0800681static int tcf_csum_walker(struct net *net, struct sk_buff *skb,
682 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500683 const struct tc_action_ops *ops,
684 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800685{
686 struct tc_action_net *tn = net_generic(net, csum_net_id);
687
Alexander Aringb3620142018-02-15 10:54:59 -0500688 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800689}
690
Cong Wangf061b482018-08-29 10:15:35 -0700691static int tcf_csum_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800692{
693 struct tc_action_net *tn = net_generic(net, csum_net_id);
694
Chris Mi65a206c2017-08-30 02:31:59 -0400695 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800696}
697
Craig Dillabaugh29e6eee2018-05-01 10:17:43 -0400698static size_t tcf_csum_get_fill_size(const struct tc_action *act)
699{
700 return nla_total_size(sizeof(struct tc_csum));
701}
702
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000703static struct tc_action_ops act_csum_ops = {
Changli Gao0eec32f2010-08-23 03:27:58 +0000704 .kind = "csum",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200705 .id = TCA_ID_CSUM,
Changli Gao0eec32f2010-08-23 03:27:58 +0000706 .owner = THIS_MODULE,
Jamal Hadi Salimc8315492018-08-12 09:34:51 -0400707 .act = tcf_csum_act,
Changli Gao0eec32f2010-08-23 03:27:58 +0000708 .dump = tcf_csum_dump,
Changli Gao0eec32f2010-08-23 03:27:58 +0000709 .init = tcf_csum_init,
Davide Caratti9c5f69b2018-01-22 18:14:32 +0100710 .cleanup = tcf_csum_cleanup,
WANG Congddf97cc2016-02-22 15:57:53 -0800711 .walk = tcf_csum_walker,
712 .lookup = tcf_csum_search,
Craig Dillabaugh29e6eee2018-05-01 10:17:43 -0400713 .get_fill_size = tcf_csum_get_fill_size,
WANG Conga85a9702016-07-25 16:09:41 -0700714 .size = sizeof(struct tcf_csum),
WANG Congddf97cc2016-02-22 15:57:53 -0800715};
716
717static __net_init int csum_init_net(struct net *net)
718{
719 struct tc_action_net *tn = net_generic(net, csum_net_id);
720
Cong Wangc7e460c2017-11-06 13:47:18 -0800721 return tc_action_net_init(tn, &act_csum_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800722}
723
Cong Wang039af9c2017-12-11 15:35:03 -0800724static void __net_exit csum_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800725{
Cong Wang039af9c2017-12-11 15:35:03 -0800726 tc_action_net_exit(net_list, csum_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800727}
728
729static struct pernet_operations csum_net_ops = {
730 .init = csum_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800731 .exit_batch = csum_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800732 .id = &csum_net_id,
733 .size = sizeof(struct tc_action_net),
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000734};
735
736MODULE_DESCRIPTION("Checksum updating actions");
737MODULE_LICENSE("GPL");
738
739static int __init csum_init_module(void)
740{
WANG Congddf97cc2016-02-22 15:57:53 -0800741 return tcf_register_action(&act_csum_ops, &csum_net_ops);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000742}
743
744static void __exit csum_cleanup_module(void)
745{
WANG Congddf97cc2016-02-22 15:57:53 -0800746 tcf_unregister_action(&act_csum_ops, &csum_net_ops);
Grégoire Baroneb4d4062010-08-18 13:10:35 +0000747}
748
749module_init(csum_init_module);
750module_exit(csum_cleanup_module);