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>
37 #include <linux/tc_act/tc_csum.h>
38 #include <net/tc_act/tc_csum.h>
40 #define CSUM_TAB_MASK 15
42 static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
43 [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
46 static unsigned int csum_net_id;
47 static struct tc_action_ops act_csum_ops;
49 static int tcf_csum_init(struct net *net, struct nlattr *nla,
50 struct nlattr *est, struct tc_action **a, int ovr,
53 struct tc_action_net *tn = net_generic(net, csum_net_id);
54 struct nlattr *tb[TCA_CSUM_MAX + 1];
62 err = nla_parse_nested(tb, TCA_CSUM_MAX, nla, csum_policy, NULL);
66 if (tb[TCA_CSUM_PARMS] == NULL)
68 parm = nla_data(tb[TCA_CSUM_PARMS]);
70 if (!tcf_hash_check(tn, parm->index, a, bind)) {
71 ret = tcf_hash_create(tn, parm->index, est, a,
72 &act_csum_ops, bind, false);
77 if (bind)/* dont override defaults */
79 tcf_hash_release(*a, bind);
85 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);
90 if (ret == ACT_P_CREATED)
91 tcf_hash_insert(tn, *a);
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
103 * Check the expected next layer availability in the specified sk_buff.
104 * Return the next layer pointer if pass, NULL otherwise.
106 static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
107 unsigned int ihl, unsigned int ipl,
110 int ntkoff = skb_network_offset(skb);
113 if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
114 skb_try_make_writable(skb, hl + ntkoff))
117 return (void *)(skb_network_header(skb) + ihl);
120 static int tcf_csum_ipv4_icmp(struct sk_buff *skb, unsigned int ihl,
123 struct icmphdr *icmph;
125 icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
130 skb->csum = csum_partial(icmph, ipl - ihl, 0);
131 icmph->checksum = csum_fold(skb->csum);
133 skb->ip_summed = CHECKSUM_NONE;
138 static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
139 unsigned int ihl, unsigned int ipl)
141 struct igmphdr *igmph;
143 igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
148 skb->csum = csum_partial(igmph, ipl - ihl, 0);
149 igmph->csum = csum_fold(skb->csum);
151 skb->ip_summed = CHECKSUM_NONE;
156 static int tcf_csum_ipv6_icmp(struct sk_buff *skb, unsigned int ihl,
159 struct icmp6hdr *icmp6h;
160 const struct ipv6hdr *ip6h;
162 icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
166 ip6h = ipv6_hdr(skb);
167 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,
173 skb->ip_summed = CHECKSUM_NONE;
178 static int tcf_csum_ipv4_tcp(struct sk_buff *skb, unsigned int ihl,
182 const struct iphdr *iph;
184 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
187 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
193 skb->csum = csum_partial(tcph, ipl - ihl, 0);
194 tcph->check = tcp_v4_check(ipl - ihl,
195 iph->saddr, iph->daddr, skb->csum);
197 skb->ip_summed = CHECKSUM_NONE;
202 static int tcf_csum_ipv6_tcp(struct sk_buff *skb, unsigned int ihl,
206 const struct ipv6hdr *ip6h;
208 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
211 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
215 ip6h = ipv6_hdr(skb);
217 skb->csum = csum_partial(tcph, ipl - ihl, 0);
218 tcph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
219 ipl - ihl, IPPROTO_TCP,
222 skb->ip_summed = CHECKSUM_NONE;
227 static int tcf_csum_ipv4_udp(struct sk_buff *skb, unsigned int ihl,
228 unsigned int ipl, int udplite)
231 const struct iphdr *iph;
234 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
238 * Support both UDP and UDPLITE checksum algorithms, Don't use
239 * udph->len to get the real length without any protocol check,
240 * UDPLITE uses udph->len for another thing,
241 * Use iph->tot_len, or just ipl.
244 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
249 ul = ntohs(udph->len);
251 if (udplite || udph->check) {
257 skb->csum = csum_partial(udph, ipl - ihl, 0);
258 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
259 skb->csum = csum_partial(udph, ul, 0);
261 goto ignore_obscure_skb;
264 goto ignore_obscure_skb;
266 skb->csum = csum_partial(udph, ul, 0);
269 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
274 udph->check = CSUM_MANGLED_0;
277 skb->ip_summed = CHECKSUM_NONE;
283 static int tcf_csum_ipv6_udp(struct sk_buff *skb, unsigned int ihl,
284 unsigned int ipl, int udplite)
287 const struct ipv6hdr *ip6h;
290 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
294 * Support both UDP and UDPLITE checksum algorithms, Don't use
295 * udph->len to get the real length without any protocol check,
296 * UDPLITE uses udph->len for another thing,
297 * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
300 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
304 ip6h = ipv6_hdr(skb);
305 ul = ntohs(udph->len);
311 skb->csum = csum_partial(udph, ipl - ihl, 0);
313 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
314 skb->csum = csum_partial(udph, ul, 0);
317 goto ignore_obscure_skb;
320 goto ignore_obscure_skb;
322 skb->csum = csum_partial(udph, ul, 0);
325 udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
326 udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
330 udph->check = CSUM_MANGLED_0;
332 skb->ip_summed = CHECKSUM_NONE;
338 static int tcf_csum_sctp(struct sk_buff *skb, unsigned int ihl,
341 struct sctphdr *sctph;
343 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_SCTP)
346 sctph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*sctph));
350 sctph->checksum = sctp_compute_cksum(skb,
351 skb_network_offset(skb) + ihl);
352 skb->ip_summed = CHECKSUM_NONE;
353 skb->csum_not_inet = 0;
358 static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
360 const struct iphdr *iph;
363 ntkoff = skb_network_offset(skb);
365 if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
370 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
372 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
373 if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
374 ntohs(iph->tot_len)))
378 if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
379 if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
380 ntohs(iph->tot_len)))
384 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
385 if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4,
386 ntohs(iph->tot_len)))
390 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
391 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
392 ntohs(iph->tot_len), 0))
395 case IPPROTO_UDPLITE:
396 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
397 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
398 ntohs(iph->tot_len), 1))
402 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
403 !tcf_csum_sctp(skb, iph->ihl * 4, ntohs(iph->tot_len)))
408 if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
409 if (skb_try_make_writable(skb, sizeof(*iph) + ntkoff))
412 ip_send_check(ip_hdr(skb));
421 static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh, unsigned int ixhl,
424 int off, len, optlen;
425 unsigned char *xh = (void *)ip6xh;
427 off = sizeof(*ip6xh);
436 optlen = xh[off + 1] + 2;
437 if (optlen != 6 || len < 6 || (off & 3) != 2)
438 /* wrong jumbo option length/alignment */
440 *pl = ntohl(*(__be32 *)(xh + off + 2));
443 optlen = xh[off + 1] + 2;
445 /* ignore obscure options */
457 static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
459 struct ipv6hdr *ip6h;
460 struct ipv6_opt_hdr *ip6xh;
461 unsigned int hl, ixhl;
466 ntkoff = skb_network_offset(skb);
470 if (!pskb_may_pull(skb, hl + ntkoff))
473 ip6h = ipv6_hdr(skb);
475 pl = ntohs(ip6h->payload_len);
476 nexthdr = ip6h->nexthdr;
480 case NEXTHDR_FRAGMENT:
482 case NEXTHDR_ROUTING:
485 if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
487 ip6xh = (void *)(skb_network_header(skb) + hl);
488 ixhl = ipv6_optlen(ip6xh);
489 if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
491 ip6xh = (void *)(skb_network_header(skb) + hl);
492 if ((nexthdr == NEXTHDR_HOP) &&
493 !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
495 nexthdr = ip6xh->nexthdr;
499 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
500 if (!tcf_csum_ipv6_icmp(skb,
501 hl, pl + sizeof(*ip6h)))
505 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
506 if (!tcf_csum_ipv6_tcp(skb,
507 hl, pl + sizeof(*ip6h)))
511 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
512 if (!tcf_csum_ipv6_udp(skb, hl,
513 pl + sizeof(*ip6h), 0))
516 case IPPROTO_UDPLITE:
517 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
518 if (!tcf_csum_ipv6_udp(skb, hl,
519 pl + sizeof(*ip6h), 1))
523 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
524 !tcf_csum_sctp(skb, hl, pl + sizeof(*ip6h)))
530 } while (pskb_may_pull(skb, hl + 1 + ntkoff));
540 static int tcf_csum(struct sk_buff *skb, const struct tc_action *a,
541 struct tcf_result *res)
543 struct tcf_csum *p = to_tcf_csum(a);
547 spin_lock(&p->tcf_lock);
548 tcf_lastuse_update(&p->tcf_tm);
549 bstats_update(&p->tcf_bstats, skb);
550 action = p->tcf_action;
551 update_flags = p->update_flags;
552 spin_unlock(&p->tcf_lock);
554 if (unlikely(action == TC_ACT_SHOT))
557 switch (tc_skb_protocol(skb)) {
558 case cpu_to_be16(ETH_P_IP):
559 if (!tcf_csum_ipv4(skb, update_flags))
562 case cpu_to_be16(ETH_P_IPV6):
563 if (!tcf_csum_ipv6(skb, update_flags))
571 spin_lock(&p->tcf_lock);
572 p->tcf_qstats.drops++;
573 spin_unlock(&p->tcf_lock);
577 static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
580 unsigned char *b = skb_tail_pointer(skb);
581 struct tcf_csum *p = to_tcf_csum(a);
582 struct tc_csum opt = {
583 .update_flags = p->update_flags,
584 .index = p->tcf_index,
585 .action = p->tcf_action,
586 .refcnt = p->tcf_refcnt - ref,
587 .bindcnt = p->tcf_bindcnt - bind,
591 if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
592 goto nla_put_failure;
594 tcf_tm_dump(&t, &p->tcf_tm);
595 if (nla_put_64bit(skb, TCA_CSUM_TM, sizeof(t), &t, TCA_CSUM_PAD))
596 goto nla_put_failure;
605 static int tcf_csum_walker(struct net *net, struct sk_buff *skb,
606 struct netlink_callback *cb, int type,
607 const struct tc_action_ops *ops)
609 struct tc_action_net *tn = net_generic(net, csum_net_id);
611 return tcf_generic_walker(tn, skb, cb, type, ops);
614 static int tcf_csum_search(struct net *net, struct tc_action **a, u32 index)
616 struct tc_action_net *tn = net_generic(net, csum_net_id);
618 return tcf_hash_search(tn, a, index);
621 static struct tc_action_ops act_csum_ops = {
623 .type = TCA_ACT_CSUM,
624 .owner = THIS_MODULE,
626 .dump = tcf_csum_dump,
627 .init = tcf_csum_init,
628 .walk = tcf_csum_walker,
629 .lookup = tcf_csum_search,
630 .size = sizeof(struct tcf_csum),
633 static __net_init int csum_init_net(struct net *net)
635 struct tc_action_net *tn = net_generic(net, csum_net_id);
637 return tc_action_net_init(tn, &act_csum_ops, CSUM_TAB_MASK);
640 static void __net_exit csum_exit_net(struct net *net)
642 struct tc_action_net *tn = net_generic(net, csum_net_id);
644 tc_action_net_exit(tn);
647 static struct pernet_operations csum_net_ops = {
648 .init = csum_init_net,
649 .exit = csum_exit_net,
651 .size = sizeof(struct tc_action_net),
654 MODULE_DESCRIPTION("Checksum updating actions");
655 MODULE_LICENSE("GPL");
657 static int __init csum_init_module(void)
659 return tcf_register_action(&act_csum_ops, &csum_net_ops);
662 static void __exit csum_cleanup_module(void)
664 tcf_unregister_action(&act_csum_ops, &csum_net_ops);
667 module_init(csum_init_module);
668 module_exit(csum_cleanup_module);