Linux 4.19.168
[linux/fpc-iii.git] / net / sched / act_csum.c
blob24ad4ceaca27e823f529709158492564a259c776
1 /*
2 * Checksum updating actions
4 * Copyright (c) 2010 Gregoire Baron <baronchon@n7mm.org>
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.
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>
19 #include <linux/netlink.h>
20 #include <net/netlink.h>
21 #include <linux/rtnetlink.h>
23 #include <linux/skbuff.h>
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>
35 #include <net/act_api.h>
37 #include <linux/tc_act/tc_csum.h>
38 #include <net/tc_act/tc_csum.h>
40 static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
41 [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
44 static unsigned int csum_net_id;
45 static struct tc_action_ops act_csum_ops;
47 static 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)
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;
60 if (nla == NULL)
61 return -EINVAL;
63 err = nla_parse_nested(tb, TCA_CSUM_MAX, nla, csum_policy, NULL);
64 if (err < 0)
65 return err;
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;
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;
87 } else {
88 return err;
91 p = to_tcf_csum(*a);
93 params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
94 if (unlikely(!params_new)) {
95 tcf_idr_release(*a, bind);
96 return -ENOMEM;
98 params_new->update_flags = parm->update_flags;
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);
106 if (params_new)
107 kfree_rcu(params_new, rcu);
109 if (ret == ACT_P_CREATED)
110 tcf_idr_insert(tn, *a);
112 return ret;
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
122 * Check the expected next layer availability in the specified sk_buff.
123 * Return the next layer pointer if pass, NULL otherwise.
125 static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
126 unsigned int ihl, unsigned int ipl,
127 unsigned int jhl)
129 int ntkoff = skb_network_offset(skb);
130 int hl = ihl + jhl;
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);
139 static int tcf_csum_ipv4_icmp(struct sk_buff *skb, unsigned int ihl,
140 unsigned int ipl)
142 struct icmphdr *icmph;
144 icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
145 if (icmph == NULL)
146 return 0;
148 icmph->checksum = 0;
149 skb->csum = csum_partial(icmph, ipl - ihl, 0);
150 icmph->checksum = csum_fold(skb->csum);
152 skb->ip_summed = CHECKSUM_NONE;
154 return 1;
157 static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
158 unsigned int ihl, unsigned int ipl)
160 struct igmphdr *igmph;
162 igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
163 if (igmph == NULL)
164 return 0;
166 igmph->csum = 0;
167 skb->csum = csum_partial(igmph, ipl - ihl, 0);
168 igmph->csum = csum_fold(skb->csum);
170 skb->ip_summed = CHECKSUM_NONE;
172 return 1;
175 static int tcf_csum_ipv6_icmp(struct sk_buff *skb, unsigned int ihl,
176 unsigned int ipl)
178 struct icmp6hdr *icmp6h;
179 const struct ipv6hdr *ip6h;
181 icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
182 if (icmp6h == NULL)
183 return 0;
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);
192 skb->ip_summed = CHECKSUM_NONE;
194 return 1;
197 static int tcf_csum_ipv4_tcp(struct sk_buff *skb, unsigned int ihl,
198 unsigned int ipl)
200 struct tcphdr *tcph;
201 const struct iphdr *iph;
203 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
204 return 1;
206 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
207 if (tcph == NULL)
208 return 0;
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);
216 skb->ip_summed = CHECKSUM_NONE;
218 return 1;
221 static int tcf_csum_ipv6_tcp(struct sk_buff *skb, unsigned int ihl,
222 unsigned int ipl)
224 struct tcphdr *tcph;
225 const struct ipv6hdr *ip6h;
227 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
228 return 1;
230 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
231 if (tcph == NULL)
232 return 0;
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);
241 skb->ip_summed = CHECKSUM_NONE;
243 return 1;
246 static int tcf_csum_ipv4_udp(struct sk_buff *skb, unsigned int ihl,
247 unsigned int ipl, int udplite)
249 struct udphdr *udph;
250 const struct iphdr *iph;
251 u16 ul;
253 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
254 return 1;
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.
263 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
264 if (udph == NULL)
265 return 0;
267 iph = ip_hdr(skb);
268 ul = ntohs(udph->len);
270 if (udplite || udph->check) {
272 udph->check = 0;
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;
285 skb->csum = csum_partial(udph, ul, 0);
288 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
289 ul, iph->protocol,
290 skb->csum);
292 if (!udph->check)
293 udph->check = CSUM_MANGLED_0;
296 skb->ip_summed = CHECKSUM_NONE;
298 ignore_obscure_skb:
299 return 1;
302 static int tcf_csum_ipv6_udp(struct sk_buff *skb, unsigned int ihl,
303 unsigned int ipl, int udplite)
305 struct udphdr *udph;
306 const struct ipv6hdr *ip6h;
307 u16 ul;
309 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
310 return 1;
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.
319 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
320 if (udph == NULL)
321 return 0;
323 ip6h = ipv6_hdr(skb);
324 ul = ntohs(udph->len);
326 udph->check = 0;
328 if (udplite) {
329 if (ul == 0)
330 skb->csum = csum_partial(udph, ipl - ihl, 0);
332 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
333 skb->csum = csum_partial(udph, ul, 0);
335 else
336 goto ignore_obscure_skb;
337 } else {
338 if (ul != ipl - ihl)
339 goto ignore_obscure_skb;
341 skb->csum = csum_partial(udph, ul, 0);
344 udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
345 udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
346 skb->csum);
348 if (!udph->check)
349 udph->check = CSUM_MANGLED_0;
351 skb->ip_summed = CHECKSUM_NONE;
353 ignore_obscure_skb:
354 return 1;
357 static int tcf_csum_sctp(struct sk_buff *skb, unsigned int ihl,
358 unsigned int ipl)
360 struct sctphdr *sctph;
362 if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
363 return 1;
365 sctph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*sctph));
366 if (!sctph)
367 return 0;
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;
374 return 1;
377 static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
379 const struct iphdr *iph;
380 int ntkoff;
382 ntkoff = skb_network_offset(skb);
384 if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
385 goto fail;
387 iph = ip_hdr(skb);
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;
427 if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
428 if (skb_try_make_writable(skb, sizeof(*iph) + ntkoff))
429 goto fail;
431 ip_send_check(ip_hdr(skb));
434 return 1;
436 fail:
437 return 0;
440 static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh, unsigned int ixhl,
441 unsigned int *pl)
443 int off, len, optlen;
444 unsigned char *xh = (void *)ip6xh;
446 off = sizeof(*ip6xh);
447 len = ixhl - off;
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;
468 off += optlen;
469 len -= optlen;
472 done:
473 return 1;
476 static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
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;
485 ntkoff = skb_network_offset(skb);
487 hl = sizeof(*ip6h);
489 if (!pskb_may_pull(skb, hl + ntkoff))
490 goto fail;
492 ip6h = ipv6_hdr(skb);
494 pl = ntohs(ip6h->payload_len);
495 nexthdr = ip6h->nexthdr;
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;
549 } while (pskb_may_pull(skb, hl + 1 + ntkoff));
551 done:
552 ignore_skb:
553 return 1;
555 fail:
556 return 0;
559 static int tcf_csum_act(struct sk_buff *skb, const struct tc_action *a,
560 struct tcf_result *res)
562 struct tcf_csum *p = to_tcf_csum(a);
563 bool orig_vlan_tag_present = false;
564 unsigned int vlan_hdr_count = 0;
565 struct tcf_csum_params *params;
566 u32 update_flags;
567 __be16 protocol;
568 int action;
570 params = rcu_dereference_bh(p->params);
572 tcf_lastuse_update(&p->tcf_tm);
573 bstats_cpu_update(this_cpu_ptr(p->common.cpu_bstats), skb);
575 action = READ_ONCE(p->tcf_action);
576 if (unlikely(action == TC_ACT_SHOT))
577 goto drop;
579 update_flags = params->update_flags;
580 protocol = skb_protocol(skb, false);
581 again:
582 switch (protocol) {
583 case cpu_to_be16(ETH_P_IP):
584 if (!tcf_csum_ipv4(skb, update_flags))
585 goto drop;
586 break;
587 case cpu_to_be16(ETH_P_IPV6):
588 if (!tcf_csum_ipv6(skb, update_flags))
589 goto drop;
590 break;
591 case cpu_to_be16(ETH_P_8021AD): /* fall through */
592 case cpu_to_be16(ETH_P_8021Q):
593 if (skb_vlan_tag_present(skb) && !orig_vlan_tag_present) {
594 protocol = skb->protocol;
595 orig_vlan_tag_present = true;
596 } else {
597 struct vlan_hdr *vlan = (struct vlan_hdr *)skb->data;
599 protocol = vlan->h_vlan_encapsulated_proto;
600 skb_pull(skb, VLAN_HLEN);
601 skb_reset_network_header(skb);
602 vlan_hdr_count++;
604 goto again;
607 out:
608 /* Restore the skb for the pulled VLAN tags */
609 while (vlan_hdr_count--) {
610 skb_push(skb, VLAN_HLEN);
611 skb_reset_network_header(skb);
614 return action;
616 drop:
617 qstats_drop_inc(this_cpu_ptr(p->common.cpu_qstats));
618 action = TC_ACT_SHOT;
619 goto out;
622 static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
623 int ref)
625 unsigned char *b = skb_tail_pointer(skb);
626 struct tcf_csum *p = to_tcf_csum(a);
627 struct tcf_csum_params *params;
628 struct tc_csum opt = {
629 .index = p->tcf_index,
630 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
631 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
633 struct tcf_t t;
635 spin_lock_bh(&p->tcf_lock);
636 params = rcu_dereference_protected(p->params,
637 lockdep_is_held(&p->tcf_lock));
638 opt.action = p->tcf_action;
639 opt.update_flags = params->update_flags;
641 if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
642 goto nla_put_failure;
644 tcf_tm_dump(&t, &p->tcf_tm);
645 if (nla_put_64bit(skb, TCA_CSUM_TM, sizeof(t), &t, TCA_CSUM_PAD))
646 goto nla_put_failure;
647 spin_unlock_bh(&p->tcf_lock);
649 return skb->len;
651 nla_put_failure:
652 spin_unlock_bh(&p->tcf_lock);
653 nlmsg_trim(skb, b);
654 return -1;
657 static void tcf_csum_cleanup(struct tc_action *a)
659 struct tcf_csum *p = to_tcf_csum(a);
660 struct tcf_csum_params *params;
662 params = rcu_dereference_protected(p->params, 1);
663 if (params)
664 kfree_rcu(params, rcu);
667 static int tcf_csum_walker(struct net *net, struct sk_buff *skb,
668 struct netlink_callback *cb, int type,
669 const struct tc_action_ops *ops,
670 struct netlink_ext_ack *extack)
672 struct tc_action_net *tn = net_generic(net, csum_net_id);
674 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
677 static int tcf_csum_search(struct net *net, struct tc_action **a, u32 index,
678 struct netlink_ext_ack *extack)
680 struct tc_action_net *tn = net_generic(net, csum_net_id);
682 return tcf_idr_search(tn, a, index);
685 static size_t tcf_csum_get_fill_size(const struct tc_action *act)
687 return nla_total_size(sizeof(struct tc_csum));
690 static struct tc_action_ops act_csum_ops = {
691 .kind = "csum",
692 .type = TCA_ACT_CSUM,
693 .owner = THIS_MODULE,
694 .act = tcf_csum_act,
695 .dump = tcf_csum_dump,
696 .init = tcf_csum_init,
697 .cleanup = tcf_csum_cleanup,
698 .walk = tcf_csum_walker,
699 .lookup = tcf_csum_search,
700 .get_fill_size = tcf_csum_get_fill_size,
701 .size = sizeof(struct tcf_csum),
704 static __net_init int csum_init_net(struct net *net)
706 struct tc_action_net *tn = net_generic(net, csum_net_id);
708 return tc_action_net_init(net, tn, &act_csum_ops);
711 static void __net_exit csum_exit_net(struct list_head *net_list)
713 tc_action_net_exit(net_list, csum_net_id);
716 static struct pernet_operations csum_net_ops = {
717 .init = csum_init_net,
718 .exit_batch = csum_exit_net,
719 .id = &csum_net_id,
720 .size = sizeof(struct tc_action_net),
723 MODULE_DESCRIPTION("Checksum updating actions");
724 MODULE_LICENSE("GPL");
726 static int __init csum_init_module(void)
728 return tcf_register_action(&act_csum_ops, &csum_net_ops);
731 static void __exit csum_cleanup_module(void)
733 tcf_unregister_action(&act_csum_ops, &csum_net_ops);
736 module_init(csum_init_module);
737 module_exit(csum_cleanup_module);