blob: 1e269441065a6dd72966c48a2b5d84fb17832c8f [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
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>
32#include <net/ip6_checksum.h>
33#include <net/sctp/checksum.h>
34
35#include <net/act_api.h>
36
37#include <linux/tc_act/tc_csum.h>
38#include <net/tc_act/tc_csum.h>
39
40static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
41 [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
42};
43
44static unsigned int csum_net_id;
45static struct tc_action_ops act_csum_ops;
46
47static int tcf_csum_init(struct net *net, struct nlattr *nla,
48 struct nlattr *est, struct tc_action **a, int ovr,
49 int bind, bool rtnl_held,
50 struct netlink_ext_ack *extack)
51{
52 struct tc_action_net *tn = net_generic(net, csum_net_id);
53 struct tcf_csum_params *params_new;
54 struct nlattr *tb[TCA_CSUM_MAX + 1];
55 struct tc_csum *parm;
56 struct tcf_csum *p;
57 int ret = 0, err;
58 u32 index;
59
60 if (nla == NULL)
61 return -EINVAL;
62
63 err = nla_parse_nested(tb, TCA_CSUM_MAX, nla, csum_policy, NULL);
64 if (err < 0)
65 return err;
66
67 if (tb[TCA_CSUM_PARMS] == NULL)
68 return -EINVAL;
69 parm = nla_data(tb[TCA_CSUM_PARMS]);
70 index = parm->index;
71 err = tcf_idr_check_alloc(tn, &index, a, bind);
72 if (!err) {
73 ret = tcf_idr_create(tn, index, est, a,
74 &act_csum_ops, bind, true);
75 if (ret) {
76 tcf_idr_cleanup(tn, index);
77 return ret;
78 }
79 ret = ACT_P_CREATED;
80 } else if (err > 0) {
81 if (bind)/* dont override defaults */
82 return 0;
83 if (!ovr) {
84 tcf_idr_release(*a, bind);
85 return -EEXIST;
86 }
87 } else {
88 return err;
89 }
90
91 p = to_tcf_csum(*a);
92
93 params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
94 if (unlikely(!params_new)) {
95 tcf_idr_release(*a, bind);
96 return -ENOMEM;
97 }
98 params_new->update_flags = parm->update_flags;
99
100 spin_lock_bh(&p->tcf_lock);
101 p->tcf_action = parm->action;
102 rcu_swap_protected(p->params, params_new,
103 lockdep_is_held(&p->tcf_lock));
104 spin_unlock_bh(&p->tcf_lock);
105
106 if (params_new)
107 kfree_rcu(params_new, rcu);
108
109 if (ret == ACT_P_CREATED)
110 tcf_idr_insert(tn, *a);
111
112 return ret;
113}
114
115/**
116 * tcf_csum_skb_nextlayer - Get next layer pointer
117 * @skb: sk_buff to use
118 * @ihl: previous summed headers length
119 * @ipl: complete packet length
120 * @jhl: next header length
121 *
122 * Check the expected next layer availability in the specified sk_buff.
123 * Return the next layer pointer if pass, NULL otherwise.
124 */
125static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
126 unsigned int ihl, unsigned int ipl,
127 unsigned int jhl)
128{
129 int ntkoff = skb_network_offset(skb);
130 int hl = ihl + jhl;
131
132 if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
133 skb_try_make_writable(skb, hl + ntkoff))
134 return NULL;
135 else
136 return (void *)(skb_network_header(skb) + ihl);
137}
138
139static int tcf_csum_ipv4_icmp(struct sk_buff *skb, unsigned int ihl,
140 unsigned int ipl)
141{
142 struct icmphdr *icmph;
143
144 icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
145 if (icmph == NULL)
146 return 0;
147
148 icmph->checksum = 0;
149 skb->csum = csum_partial(icmph, ipl - ihl, 0);
150 icmph->checksum = csum_fold(skb->csum);
151
152 skb->ip_summed = CHECKSUM_NONE;
153
154 return 1;
155}
156
157static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
158 unsigned int ihl, unsigned int ipl)
159{
160 struct igmphdr *igmph;
161
162 igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
163 if (igmph == NULL)
164 return 0;
165
166 igmph->csum = 0;
167 skb->csum = csum_partial(igmph, ipl - ihl, 0);
168 igmph->csum = csum_fold(skb->csum);
169
170 skb->ip_summed = CHECKSUM_NONE;
171
172 return 1;
173}
174
175static int tcf_csum_ipv6_icmp(struct sk_buff *skb, unsigned int ihl,
176 unsigned int ipl)
177{
178 struct icmp6hdr *icmp6h;
179 const struct ipv6hdr *ip6h;
180
181 icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
182 if (icmp6h == NULL)
183 return 0;
184
185 ip6h = ipv6_hdr(skb);
186 icmp6h->icmp6_cksum = 0;
187 skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
188 icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
189 ipl - ihl, IPPROTO_ICMPV6,
190 skb->csum);
191
192 skb->ip_summed = CHECKSUM_NONE;
193
194 return 1;
195}
196
197static int tcf_csum_ipv4_tcp(struct sk_buff *skb, unsigned int ihl,
198 unsigned int ipl)
199{
200 struct tcphdr *tcph;
201 const struct iphdr *iph;
202
203 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
204 return 1;
205
206 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
207 if (tcph == NULL)
208 return 0;
209
210 iph = ip_hdr(skb);
211 tcph->check = 0;
212 skb->csum = csum_partial(tcph, ipl - ihl, 0);
213 tcph->check = tcp_v4_check(ipl - ihl,
214 iph->saddr, iph->daddr, skb->csum);
215
216 skb->ip_summed = CHECKSUM_NONE;
217
218 return 1;
219}
220
221static int tcf_csum_ipv6_tcp(struct sk_buff *skb, unsigned int ihl,
222 unsigned int ipl)
223{
224 struct tcphdr *tcph;
225 const struct ipv6hdr *ip6h;
226
227 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
228 return 1;
229
230 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
231 if (tcph == NULL)
232 return 0;
233
234 ip6h = ipv6_hdr(skb);
235 tcph->check = 0;
236 skb->csum = csum_partial(tcph, ipl - ihl, 0);
237 tcph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
238 ipl - ihl, IPPROTO_TCP,
239 skb->csum);
240
241 skb->ip_summed = CHECKSUM_NONE;
242
243 return 1;
244}
245
246static int tcf_csum_ipv4_udp(struct sk_buff *skb, unsigned int ihl,
247 unsigned int ipl, int udplite)
248{
249 struct udphdr *udph;
250 const struct iphdr *iph;
251 u16 ul;
252
253 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
254 return 1;
255
256 /*
257 * Support both UDP and UDPLITE checksum algorithms, Don't use
258 * udph->len to get the real length without any protocol check,
259 * UDPLITE uses udph->len for another thing,
260 * Use iph->tot_len, or just ipl.
261 */
262
263 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
264 if (udph == NULL)
265 return 0;
266
267 iph = ip_hdr(skb);
268 ul = ntohs(udph->len);
269
270 if (udplite || udph->check) {
271
272 udph->check = 0;
273
274 if (udplite) {
275 if (ul == 0)
276 skb->csum = csum_partial(udph, ipl - ihl, 0);
277 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
278 skb->csum = csum_partial(udph, ul, 0);
279 else
280 goto ignore_obscure_skb;
281 } else {
282 if (ul != ipl - ihl)
283 goto ignore_obscure_skb;
284
285 skb->csum = csum_partial(udph, ul, 0);
286 }
287
288 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
289 ul, iph->protocol,
290 skb->csum);
291
292 if (!udph->check)
293 udph->check = CSUM_MANGLED_0;
294 }
295
296 skb->ip_summed = CHECKSUM_NONE;
297
298ignore_obscure_skb:
299 return 1;
300}
301
302static int tcf_csum_ipv6_udp(struct sk_buff *skb, unsigned int ihl,
303 unsigned int ipl, int udplite)
304{
305 struct udphdr *udph;
306 const struct ipv6hdr *ip6h;
307 u16 ul;
308
309 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
310 return 1;
311
312 /*
313 * Support both UDP and UDPLITE checksum algorithms, Don't use
314 * udph->len to get the real length without any protocol check,
315 * UDPLITE uses udph->len for another thing,
316 * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
317 */
318
319 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
320 if (udph == NULL)
321 return 0;
322
323 ip6h = ipv6_hdr(skb);
324 ul = ntohs(udph->len);
325
326 udph->check = 0;
327
328 if (udplite) {
329 if (ul == 0)
330 skb->csum = csum_partial(udph, ipl - ihl, 0);
331
332 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
333 skb->csum = csum_partial(udph, ul, 0);
334
335 else
336 goto ignore_obscure_skb;
337 } else {
338 if (ul != ipl - ihl)
339 goto ignore_obscure_skb;
340
341 skb->csum = csum_partial(udph, ul, 0);
342 }
343
344 udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
345 udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
346 skb->csum);
347
348 if (!udph->check)
349 udph->check = CSUM_MANGLED_0;
350
351 skb->ip_summed = CHECKSUM_NONE;
352
353ignore_obscure_skb:
354 return 1;
355}
356
357static int tcf_csum_sctp(struct sk_buff *skb, unsigned int ihl,
358 unsigned int ipl)
359{
360 struct sctphdr *sctph;
361
362 if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
363 return 1;
364
365 sctph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*sctph));
366 if (!sctph)
367 return 0;
368
369 sctph->checksum = sctp_compute_cksum(skb,
370 skb_network_offset(skb) + ihl);
371 skb->ip_summed = CHECKSUM_NONE;
372 skb->csum_not_inet = 0;
373
374 return 1;
375}
376
377static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
378{
379 const struct iphdr *iph;
380 int ntkoff;
381
382 ntkoff = skb_network_offset(skb);
383
384 if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
385 goto fail;
386
387 iph = ip_hdr(skb);
388
389 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
390 case IPPROTO_ICMP:
391 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
392 if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
393 ntohs(iph->tot_len)))
394 goto fail;
395 break;
396 case IPPROTO_IGMP:
397 if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
398 if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
399 ntohs(iph->tot_len)))
400 goto fail;
401 break;
402 case IPPROTO_TCP:
403 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
404 if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4,
405 ntohs(iph->tot_len)))
406 goto fail;
407 break;
408 case IPPROTO_UDP:
409 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
410 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
411 ntohs(iph->tot_len), 0))
412 goto fail;
413 break;
414 case IPPROTO_UDPLITE:
415 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
416 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
417 ntohs(iph->tot_len), 1))
418 goto fail;
419 break;
420 case IPPROTO_SCTP:
421 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
422 !tcf_csum_sctp(skb, iph->ihl * 4, ntohs(iph->tot_len)))
423 goto fail;
424 break;
425 }
426
427 if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
428 if (skb_try_make_writable(skb, sizeof(*iph) + ntkoff))
429 goto fail;
430
431 ip_send_check(ip_hdr(skb));
432 }
433
434 return 1;
435
436fail:
437 return 0;
438}
439
440static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh, unsigned int ixhl,
441 unsigned int *pl)
442{
443 int off, len, optlen;
444 unsigned char *xh = (void *)ip6xh;
445
446 off = sizeof(*ip6xh);
447 len = ixhl - off;
448
449 while (len > 1) {
450 switch (xh[off]) {
451 case IPV6_TLV_PAD1:
452 optlen = 1;
453 break;
454 case IPV6_TLV_JUMBO:
455 optlen = xh[off + 1] + 2;
456 if (optlen != 6 || len < 6 || (off & 3) != 2)
457 /* wrong jumbo option length/alignment */
458 return 0;
459 *pl = ntohl(*(__be32 *)(xh + off + 2));
460 goto done;
461 default:
462 optlen = xh[off + 1] + 2;
463 if (optlen > len)
464 /* ignore obscure options */
465 goto done;
466 break;
467 }
468 off += optlen;
469 len -= optlen;
470 }
471
472done:
473 return 1;
474}
475
476static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
477{
478 struct ipv6hdr *ip6h;
479 struct ipv6_opt_hdr *ip6xh;
480 unsigned int hl, ixhl;
481 unsigned int pl;
482 int ntkoff;
483 u8 nexthdr;
484
485 ntkoff = skb_network_offset(skb);
486
487 hl = sizeof(*ip6h);
488
489 if (!pskb_may_pull(skb, hl + ntkoff))
490 goto fail;
491
492 ip6h = ipv6_hdr(skb);
493
494 pl = ntohs(ip6h->payload_len);
495 nexthdr = ip6h->nexthdr;
496
497 do {
498 switch (nexthdr) {
499 case NEXTHDR_FRAGMENT:
500 goto ignore_skb;
501 case NEXTHDR_ROUTING:
502 case NEXTHDR_HOP:
503 case NEXTHDR_DEST:
504 if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
505 goto fail;
506 ip6xh = (void *)(skb_network_header(skb) + hl);
507 ixhl = ipv6_optlen(ip6xh);
508 if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
509 goto fail;
510 ip6xh = (void *)(skb_network_header(skb) + hl);
511 if ((nexthdr == NEXTHDR_HOP) &&
512 !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
513 goto fail;
514 nexthdr = ip6xh->nexthdr;
515 hl += ixhl;
516 break;
517 case IPPROTO_ICMPV6:
518 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
519 if (!tcf_csum_ipv6_icmp(skb,
520 hl, pl + sizeof(*ip6h)))
521 goto fail;
522 goto done;
523 case IPPROTO_TCP:
524 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
525 if (!tcf_csum_ipv6_tcp(skb,
526 hl, pl + sizeof(*ip6h)))
527 goto fail;
528 goto done;
529 case IPPROTO_UDP:
530 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
531 if (!tcf_csum_ipv6_udp(skb, hl,
532 pl + sizeof(*ip6h), 0))
533 goto fail;
534 goto done;
535 case IPPROTO_UDPLITE:
536 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
537 if (!tcf_csum_ipv6_udp(skb, hl,
538 pl + sizeof(*ip6h), 1))
539 goto fail;
540 goto done;
541 case IPPROTO_SCTP:
542 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
543 !tcf_csum_sctp(skb, hl, pl + sizeof(*ip6h)))
544 goto fail;
545 goto done;
546 default:
547 goto ignore_skb;
548 }
549 } while (pskb_may_pull(skb, hl + 1 + ntkoff));
550
551done:
552ignore_skb:
553 return 1;
554
555fail:
556 return 0;
557}
558
559static int tcf_csum_act(struct sk_buff *skb, const struct tc_action *a,
560 struct tcf_result *res)
561{
562 struct tcf_csum *p = to_tcf_csum(a);
563 struct tcf_csum_params *params;
564 u32 update_flags;
565 int action;
566
567 params = rcu_dereference_bh(p->params);
568
569 tcf_lastuse_update(&p->tcf_tm);
570 bstats_cpu_update(this_cpu_ptr(p->common.cpu_bstats), skb);
571
572 action = READ_ONCE(p->tcf_action);
573 if (unlikely(action == TC_ACT_SHOT))
574 goto drop;
575
576 update_flags = params->update_flags;
577 switch (tc_skb_protocol(skb)) {
578 case cpu_to_be16(ETH_P_IP):
579 if (!tcf_csum_ipv4(skb, update_flags))
580 goto drop;
581 break;
582 case cpu_to_be16(ETH_P_IPV6):
583 if (!tcf_csum_ipv6(skb, update_flags))
584 goto drop;
585 break;
586 }
587
588 return action;
589
590drop:
591 qstats_drop_inc(this_cpu_ptr(p->common.cpu_qstats));
592 return TC_ACT_SHOT;
593}
594
595static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
596 int ref)
597{
598 unsigned char *b = skb_tail_pointer(skb);
599 struct tcf_csum *p = to_tcf_csum(a);
600 struct tcf_csum_params *params;
601 struct tc_csum opt = {
602 .index = p->tcf_index,
603 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
604 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
605 };
606 struct tcf_t t;
607
608 spin_lock_bh(&p->tcf_lock);
609 params = rcu_dereference_protected(p->params,
610 lockdep_is_held(&p->tcf_lock));
611 opt.action = p->tcf_action;
612 opt.update_flags = params->update_flags;
613
614 if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
615 goto nla_put_failure;
616
617 tcf_tm_dump(&t, &p->tcf_tm);
618 if (nla_put_64bit(skb, TCA_CSUM_TM, sizeof(t), &t, TCA_CSUM_PAD))
619 goto nla_put_failure;
620 spin_unlock_bh(&p->tcf_lock);
621
622 return skb->len;
623
624nla_put_failure:
625 spin_unlock_bh(&p->tcf_lock);
626 nlmsg_trim(skb, b);
627 return -1;
628}
629
630static void tcf_csum_cleanup(struct tc_action *a)
631{
632 struct tcf_csum *p = to_tcf_csum(a);
633 struct tcf_csum_params *params;
634
635 params = rcu_dereference_protected(p->params, 1);
636 if (params)
637 kfree_rcu(params, rcu);
638}
639
640static int tcf_csum_walker(struct net *net, struct sk_buff *skb,
641 struct netlink_callback *cb, int type,
642 const struct tc_action_ops *ops,
643 struct netlink_ext_ack *extack)
644{
645 struct tc_action_net *tn = net_generic(net, csum_net_id);
646
647 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
648}
649
650static int tcf_csum_search(struct net *net, struct tc_action **a, u32 index,
651 struct netlink_ext_ack *extack)
652{
653 struct tc_action_net *tn = net_generic(net, csum_net_id);
654
655 return tcf_idr_search(tn, a, index);
656}
657
658static size_t tcf_csum_get_fill_size(const struct tc_action *act)
659{
660 return nla_total_size(sizeof(struct tc_csum));
661}
662
663static struct tc_action_ops act_csum_ops = {
664 .kind = "csum",
665 .type = TCA_ACT_CSUM,
666 .owner = THIS_MODULE,
667 .act = tcf_csum_act,
668 .dump = tcf_csum_dump,
669 .init = tcf_csum_init,
670 .cleanup = tcf_csum_cleanup,
671 .walk = tcf_csum_walker,
672 .lookup = tcf_csum_search,
673 .get_fill_size = tcf_csum_get_fill_size,
674 .size = sizeof(struct tcf_csum),
675};
676
677static __net_init int csum_init_net(struct net *net)
678{
679 struct tc_action_net *tn = net_generic(net, csum_net_id);
680
681 return tc_action_net_init(net, tn, &act_csum_ops);
682}
683
684static void __net_exit csum_exit_net(struct list_head *net_list)
685{
686 tc_action_net_exit(net_list, csum_net_id);
687}
688
689static struct pernet_operations csum_net_ops = {
690 .init = csum_init_net,
691 .exit_batch = csum_exit_net,
692 .id = &csum_net_id,
693 .size = sizeof(struct tc_action_net),
694};
695
696MODULE_DESCRIPTION("Checksum updating actions");
697MODULE_LICENSE("GPL");
698
699static int __init csum_init_module(void)
700{
701 return tcf_register_action(&act_csum_ops, &csum_net_ops);
702}
703
704static void __exit csum_cleanup_module(void)
705{
706 tcf_unregister_action(&act_csum_ops, &csum_net_ops);
707}
708
709module_init(csum_init_module);
710module_exit(csum_cleanup_module);