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)
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>
28 #include <linux/icmpv6.h>
29 #include <linux/igmp.h>
32 #include <net/ip6_checksum.h>
33 #include <net/sctp/checksum.h>
35 #include <net/act_api.h>
36 #include <net/pkt_cls.h>
38 #include <linux/tc_act/tc_csum.h>
39 #include <net/tc_act/tc_csum.h>
41 static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
42 [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
45 static unsigned int csum_net_id;
46 static struct tc_action_ops act_csum_ops;
48 static int tcf_csum_init(struct net *net, struct nlattr *nla,
49 struct nlattr *est, struct tc_action **a, int ovr,
50 int bind, bool rtnl_held, struct tcf_proto *tp,
51 struct netlink_ext_ack *extack)
53 struct tc_action_net *tn = net_generic(net, csum_net_id);
54 struct tcf_csum_params *params_new;
55 struct nlattr *tb[TCA_CSUM_MAX + 1];
56 struct tcf_chain *goto_ch = NULL;
64 err = nla_parse_nested(tb, TCA_CSUM_MAX, nla, csum_policy, NULL);
68 if (tb[TCA_CSUM_PARMS] == NULL)
70 parm = nla_data(tb[TCA_CSUM_PARMS]);
72 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
74 ret = tcf_idr_create(tn, parm->index, est, a,
75 &act_csum_ops, bind, true);
77 tcf_idr_cleanup(tn, parm->index);
82 if (bind)/* dont override defaults */
85 tcf_idr_release(*a, bind);
92 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
98 params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
99 if (unlikely(!params_new)) {
103 params_new->update_flags = parm->update_flags;
105 spin_lock_bh(&p->tcf_lock);
106 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
107 rcu_swap_protected(p->params, params_new,
108 lockdep_is_held(&p->tcf_lock));
109 spin_unlock_bh(&p->tcf_lock);
112 tcf_chain_put_by_act(goto_ch);
114 kfree_rcu(params_new, rcu);
116 if (ret == ACT_P_CREATED)
117 tcf_idr_insert(tn, *a);
122 tcf_chain_put_by_act(goto_ch);
124 tcf_idr_release(*a, bind);
129 * tcf_csum_skb_nextlayer - Get next layer pointer
130 * @skb: sk_buff to use
131 * @ihl: previous summed headers length
132 * @ipl: complete packet length
133 * @jhl: next header length
135 * Check the expected next layer availability in the specified sk_buff.
136 * Return the next layer pointer if pass, NULL otherwise.
138 static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
139 unsigned int ihl, unsigned int ipl,
142 int ntkoff = skb_network_offset(skb);
145 if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
146 skb_try_make_writable(skb, hl + ntkoff))
149 return (void *)(skb_network_header(skb) + ihl);
152 static int tcf_csum_ipv4_icmp(struct sk_buff *skb, unsigned int ihl,
155 struct icmphdr *icmph;
157 icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
162 skb->csum = csum_partial(icmph, ipl - ihl, 0);
163 icmph->checksum = csum_fold(skb->csum);
165 skb->ip_summed = CHECKSUM_NONE;
170 static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
171 unsigned int ihl, unsigned int ipl)
173 struct igmphdr *igmph;
175 igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
180 skb->csum = csum_partial(igmph, ipl - ihl, 0);
181 igmph->csum = csum_fold(skb->csum);
183 skb->ip_summed = CHECKSUM_NONE;
188 static int tcf_csum_ipv6_icmp(struct sk_buff *skb, unsigned int ihl,
191 struct icmp6hdr *icmp6h;
192 const struct ipv6hdr *ip6h;
194 icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
198 ip6h = ipv6_hdr(skb);
199 icmp6h->icmp6_cksum = 0;
200 skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
201 icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
202 ipl - ihl, IPPROTO_ICMPV6,
205 skb->ip_summed = CHECKSUM_NONE;
210 static int tcf_csum_ipv4_tcp(struct sk_buff *skb, unsigned int ihl,
214 const struct iphdr *iph;
216 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
219 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
225 skb->csum = csum_partial(tcph, ipl - ihl, 0);
226 tcph->check = tcp_v4_check(ipl - ihl,
227 iph->saddr, iph->daddr, skb->csum);
229 skb->ip_summed = CHECKSUM_NONE;
234 static int tcf_csum_ipv6_tcp(struct sk_buff *skb, unsigned int ihl,
238 const struct ipv6hdr *ip6h;
240 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
243 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
247 ip6h = ipv6_hdr(skb);
249 skb->csum = csum_partial(tcph, ipl - ihl, 0);
250 tcph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
251 ipl - ihl, IPPROTO_TCP,
254 skb->ip_summed = CHECKSUM_NONE;
259 static int tcf_csum_ipv4_udp(struct sk_buff *skb, unsigned int ihl,
260 unsigned int ipl, int udplite)
263 const struct iphdr *iph;
266 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
270 * Support both UDP and UDPLITE checksum algorithms, Don't use
271 * udph->len to get the real length without any protocol check,
272 * UDPLITE uses udph->len for another thing,
273 * Use iph->tot_len, or just ipl.
276 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
281 ul = ntohs(udph->len);
283 if (udplite || udph->check) {
289 skb->csum = csum_partial(udph, ipl - ihl, 0);
290 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
291 skb->csum = csum_partial(udph, ul, 0);
293 goto ignore_obscure_skb;
296 goto ignore_obscure_skb;
298 skb->csum = csum_partial(udph, ul, 0);
301 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
306 udph->check = CSUM_MANGLED_0;
309 skb->ip_summed = CHECKSUM_NONE;
315 static int tcf_csum_ipv6_udp(struct sk_buff *skb, unsigned int ihl,
316 unsigned int ipl, int udplite)
319 const struct ipv6hdr *ip6h;
322 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
326 * Support both UDP and UDPLITE checksum algorithms, Don't use
327 * udph->len to get the real length without any protocol check,
328 * UDPLITE uses udph->len for another thing,
329 * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
332 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
336 ip6h = ipv6_hdr(skb);
337 ul = ntohs(udph->len);
343 skb->csum = csum_partial(udph, ipl - ihl, 0);
345 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
346 skb->csum = csum_partial(udph, ul, 0);
349 goto ignore_obscure_skb;
352 goto ignore_obscure_skb;
354 skb->csum = csum_partial(udph, ul, 0);
357 udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
358 udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
362 udph->check = CSUM_MANGLED_0;
364 skb->ip_summed = CHECKSUM_NONE;
370 static int tcf_csum_sctp(struct sk_buff *skb, unsigned int ihl,
373 struct sctphdr *sctph;
375 if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
378 sctph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*sctph));
382 sctph->checksum = sctp_compute_cksum(skb,
383 skb_network_offset(skb) + ihl);
384 skb->ip_summed = CHECKSUM_NONE;
385 skb->csum_not_inet = 0;
390 static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
392 const struct iphdr *iph;
395 ntkoff = skb_network_offset(skb);
397 if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
402 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
404 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
405 if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
406 ntohs(iph->tot_len)))
410 if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
411 if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
412 ntohs(iph->tot_len)))
416 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
417 if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4,
418 ntohs(iph->tot_len)))
422 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
423 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
424 ntohs(iph->tot_len), 0))
427 case IPPROTO_UDPLITE:
428 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
429 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
430 ntohs(iph->tot_len), 1))
434 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
435 !tcf_csum_sctp(skb, iph->ihl * 4, ntohs(iph->tot_len)))
440 if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
441 if (skb_try_make_writable(skb, sizeof(*iph) + ntkoff))
444 ip_send_check(ip_hdr(skb));
453 static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh, unsigned int ixhl,
456 int off, len, optlen;
457 unsigned char *xh = (void *)ip6xh;
459 off = sizeof(*ip6xh);
468 optlen = xh[off + 1] + 2;
469 if (optlen != 6 || len < 6 || (off & 3) != 2)
470 /* wrong jumbo option length/alignment */
472 *pl = ntohl(*(__be32 *)(xh + off + 2));
475 optlen = xh[off + 1] + 2;
477 /* ignore obscure options */
489 static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
491 struct ipv6hdr *ip6h;
492 struct ipv6_opt_hdr *ip6xh;
493 unsigned int hl, ixhl;
498 ntkoff = skb_network_offset(skb);
502 if (!pskb_may_pull(skb, hl + ntkoff))
505 ip6h = ipv6_hdr(skb);
507 pl = ntohs(ip6h->payload_len);
508 nexthdr = ip6h->nexthdr;
512 case NEXTHDR_FRAGMENT:
514 case NEXTHDR_ROUTING:
517 if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
519 ip6xh = (void *)(skb_network_header(skb) + hl);
520 ixhl = ipv6_optlen(ip6xh);
521 if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
523 ip6xh = (void *)(skb_network_header(skb) + hl);
524 if ((nexthdr == NEXTHDR_HOP) &&
525 !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
527 nexthdr = ip6xh->nexthdr;
531 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
532 if (!tcf_csum_ipv6_icmp(skb,
533 hl, pl + sizeof(*ip6h)))
537 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
538 if (!tcf_csum_ipv6_tcp(skb,
539 hl, pl + sizeof(*ip6h)))
543 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
544 if (!tcf_csum_ipv6_udp(skb, hl,
545 pl + sizeof(*ip6h), 0))
548 case IPPROTO_UDPLITE:
549 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
550 if (!tcf_csum_ipv6_udp(skb, hl,
551 pl + sizeof(*ip6h), 1))
555 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
556 !tcf_csum_sctp(skb, hl, pl + sizeof(*ip6h)))
562 } while (pskb_may_pull(skb, hl + 1 + ntkoff));
572 static int tcf_csum_act(struct sk_buff *skb, const struct tc_action *a,
573 struct tcf_result *res)
575 struct tcf_csum *p = to_tcf_csum(a);
576 bool orig_vlan_tag_present = false;
577 unsigned int vlan_hdr_count = 0;
578 struct tcf_csum_params *params;
583 params = rcu_dereference_bh(p->params);
585 tcf_lastuse_update(&p->tcf_tm);
586 bstats_cpu_update(this_cpu_ptr(p->common.cpu_bstats), skb);
588 action = READ_ONCE(p->tcf_action);
589 if (unlikely(action == TC_ACT_SHOT))
592 update_flags = params->update_flags;
593 protocol = tc_skb_protocol(skb);
596 case cpu_to_be16(ETH_P_IP):
597 if (!tcf_csum_ipv4(skb, update_flags))
600 case cpu_to_be16(ETH_P_IPV6):
601 if (!tcf_csum_ipv6(skb, update_flags))
604 case cpu_to_be16(ETH_P_8021AD): /* fall through */
605 case cpu_to_be16(ETH_P_8021Q):
606 if (skb_vlan_tag_present(skb) && !orig_vlan_tag_present) {
607 protocol = skb->protocol;
608 orig_vlan_tag_present = true;
610 struct vlan_hdr *vlan = (struct vlan_hdr *)skb->data;
612 protocol = vlan->h_vlan_encapsulated_proto;
613 skb_pull(skb, VLAN_HLEN);
614 skb_reset_network_header(skb);
621 /* Restore the skb for the pulled VLAN tags */
622 while (vlan_hdr_count--) {
623 skb_push(skb, VLAN_HLEN);
624 skb_reset_network_header(skb);
630 qstats_drop_inc(this_cpu_ptr(p->common.cpu_qstats));
631 action = TC_ACT_SHOT;
635 static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
638 unsigned char *b = skb_tail_pointer(skb);
639 struct tcf_csum *p = to_tcf_csum(a);
640 struct tcf_csum_params *params;
641 struct tc_csum opt = {
642 .index = p->tcf_index,
643 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
644 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
648 spin_lock_bh(&p->tcf_lock);
649 params = rcu_dereference_protected(p->params,
650 lockdep_is_held(&p->tcf_lock));
651 opt.action = p->tcf_action;
652 opt.update_flags = params->update_flags;
654 if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
655 goto nla_put_failure;
657 tcf_tm_dump(&t, &p->tcf_tm);
658 if (nla_put_64bit(skb, TCA_CSUM_TM, sizeof(t), &t, TCA_CSUM_PAD))
659 goto nla_put_failure;
660 spin_unlock_bh(&p->tcf_lock);
665 spin_unlock_bh(&p->tcf_lock);
670 static void tcf_csum_cleanup(struct tc_action *a)
672 struct tcf_csum *p = to_tcf_csum(a);
673 struct tcf_csum_params *params;
675 params = rcu_dereference_protected(p->params, 1);
677 kfree_rcu(params, rcu);
680 static int tcf_csum_walker(struct net *net, struct sk_buff *skb,
681 struct netlink_callback *cb, int type,
682 const struct tc_action_ops *ops,
683 struct netlink_ext_ack *extack)
685 struct tc_action_net *tn = net_generic(net, csum_net_id);
687 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
690 static int tcf_csum_search(struct net *net, struct tc_action **a, u32 index)
692 struct tc_action_net *tn = net_generic(net, csum_net_id);
694 return tcf_idr_search(tn, a, index);
697 static size_t tcf_csum_get_fill_size(const struct tc_action *act)
699 return nla_total_size(sizeof(struct tc_csum));
702 static struct tc_action_ops act_csum_ops = {
705 .owner = THIS_MODULE,
707 .dump = tcf_csum_dump,
708 .init = tcf_csum_init,
709 .cleanup = tcf_csum_cleanup,
710 .walk = tcf_csum_walker,
711 .lookup = tcf_csum_search,
712 .get_fill_size = tcf_csum_get_fill_size,
713 .size = sizeof(struct tcf_csum),
716 static __net_init int csum_init_net(struct net *net)
718 struct tc_action_net *tn = net_generic(net, csum_net_id);
720 return tc_action_net_init(tn, &act_csum_ops);
723 static void __net_exit csum_exit_net(struct list_head *net_list)
725 tc_action_net_exit(net_list, csum_net_id);
728 static struct pernet_operations csum_net_ops = {
729 .init = csum_init_net,
730 .exit_batch = csum_exit_net,
732 .size = sizeof(struct tc_action_net),
735 MODULE_DESCRIPTION("Checksum updating actions");
736 MODULE_LICENSE("GPL");
738 static int __init csum_init_module(void)
740 return tcf_register_action(&act_csum_ops, &csum_net_ops);
743 static void __exit csum_cleanup_module(void)
745 tcf_unregister_action(&act_csum_ops, &csum_net_ops);
748 module_init(csum_init_module);
749 module_exit(csum_cleanup_module);