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>
34 #include <net/act_api.h>
36 #include <linux/tc_act/tc_csum.h>
37 #include <net/tc_act/tc_csum.h>
39 #define CSUM_TAB_MASK 15
40 static struct tcf_common *tcf_csum_ht[CSUM_TAB_MASK + 1];
41 static u32 csum_idx_gen;
42 static DEFINE_RWLOCK(csum_lock);
44 static struct tcf_hashinfo csum_hash_info = {
46 .hmask = CSUM_TAB_MASK,
50 static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
51 [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
54 static int tcf_csum_init(struct nlattr *nla, struct nlattr *est,
55 struct tc_action *a, int ovr, int bind)
57 struct nlattr *tb[TCA_CSUM_MAX + 1];
59 struct tcf_common *pc;
66 err = nla_parse_nested(tb, TCA_CSUM_MAX, nla,csum_policy);
70 if (tb[TCA_CSUM_PARMS] == NULL)
72 parm = nla_data(tb[TCA_CSUM_PARMS]);
74 pc = tcf_hash_check(parm->index, a, bind, &csum_hash_info);
76 pc = tcf_hash_create(parm->index, est, a, sizeof(*p), bind, &csum_idx_gen, &csum_hash_info);
84 tcf_hash_release(pc, bind, &csum_hash_info);
89 spin_lock_bh(&p->tcf_lock);
90 p->tcf_action = parm->action;
91 p->update_flags = parm->update_flags;
92 spin_unlock_bh(&p->tcf_lock);
94 if (ret == ACT_P_CREATED)
95 tcf_hash_insert(pc, &csum_hash_info);
100 static int tcf_csum_cleanup(struct tc_action *a, int bind)
102 struct tcf_csum *p = a->priv;
103 return tcf_hash_release(&p->common, bind, &csum_hash_info);
107 * tcf_csum_skb_nextlayer - Get next layer pointer
108 * @skb: sk_buff to use
109 * @ihl: previous summed headers length
110 * @ipl: complete packet length
111 * @jhl: next header length
113 * Check the expected next layer availability in the specified sk_buff.
114 * Return the next layer pointer if pass, NULL otherwise.
116 static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
117 unsigned int ihl, unsigned int ipl,
120 int ntkoff = skb_network_offset(skb);
123 if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
125 !skb_clone_writable(skb, hl + ntkoff) &&
126 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
129 return (void *)(skb_network_header(skb) + ihl);
132 static int tcf_csum_ipv4_icmp(struct sk_buff *skb,
133 unsigned int ihl, unsigned int ipl)
135 struct icmphdr *icmph;
137 icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
142 skb->csum = csum_partial(icmph, ipl - ihl, 0);
143 icmph->checksum = csum_fold(skb->csum);
145 skb->ip_summed = CHECKSUM_NONE;
150 static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
151 unsigned int ihl, unsigned int ipl)
153 struct igmphdr *igmph;
155 igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
160 skb->csum = csum_partial(igmph, ipl - ihl, 0);
161 igmph->csum = csum_fold(skb->csum);
163 skb->ip_summed = CHECKSUM_NONE;
168 static int tcf_csum_ipv6_icmp(struct sk_buff *skb, struct ipv6hdr *ip6h,
169 unsigned int ihl, unsigned int ipl)
171 struct icmp6hdr *icmp6h;
173 icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
177 icmp6h->icmp6_cksum = 0;
178 skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
179 icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
180 ipl - ihl, IPPROTO_ICMPV6,
183 skb->ip_summed = CHECKSUM_NONE;
188 static int tcf_csum_ipv4_tcp(struct sk_buff *skb, struct iphdr *iph,
189 unsigned int ihl, unsigned int ipl)
193 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
198 skb->csum = csum_partial(tcph, ipl - ihl, 0);
199 tcph->check = tcp_v4_check(ipl - ihl,
200 iph->saddr, iph->daddr, skb->csum);
202 skb->ip_summed = CHECKSUM_NONE;
207 static int tcf_csum_ipv6_tcp(struct sk_buff *skb, struct ipv6hdr *ip6h,
208 unsigned int ihl, unsigned int ipl)
212 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
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, struct iphdr *iph,
228 unsigned int ihl, unsigned int ipl, int udplite)
233 /* Support both UDP and UDPLITE checksum algorithms,
234 * Don't use udph->len to get the real length without any protocol check,
235 * UDPLITE uses udph->len for another thing,
236 * Use iph->tot_len, or just ipl.
239 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
243 ul = ntohs(udph->len);
245 if (udplite || udph->check) {
251 skb->csum = csum_partial(udph, ipl - ihl, 0);
253 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
254 skb->csum = csum_partial(udph, ul, 0);
257 goto ignore_obscure_skb;
260 goto ignore_obscure_skb;
262 skb->csum = csum_partial(udph, ul, 0);
265 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
270 udph->check = CSUM_MANGLED_0;
273 skb->ip_summed = CHECKSUM_NONE;
279 static int tcf_csum_ipv6_udp(struct sk_buff *skb, struct ipv6hdr *ip6h,
280 unsigned int ihl, unsigned int ipl, int udplite)
285 /* Support both UDP and UDPLITE checksum algorithms,
286 * Don't use udph->len to get the real length without any protocol check,
287 * UDPLITE uses udph->len for another thing,
288 * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
291 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
295 ul = ntohs(udph->len);
301 skb->csum = csum_partial(udph, ipl - ihl, 0);
303 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
304 skb->csum = csum_partial(udph, ul, 0);
307 goto ignore_obscure_skb;
310 goto ignore_obscure_skb;
312 skb->csum = csum_partial(udph, ul, 0);
315 udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
316 udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
320 udph->check = CSUM_MANGLED_0;
322 skb->ip_summed = CHECKSUM_NONE;
328 static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
333 ntkoff = skb_network_offset(skb);
335 if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
340 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
342 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
343 if (!tcf_csum_ipv4_icmp(skb,
344 iph->ihl * 4, ntohs(iph->tot_len)))
348 if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
349 if (!tcf_csum_ipv4_igmp(skb,
350 iph->ihl * 4, ntohs(iph->tot_len)))
354 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
355 if (!tcf_csum_ipv4_tcp(skb, iph,
356 iph->ihl * 4, ntohs(iph->tot_len)))
360 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
361 if (!tcf_csum_ipv4_udp(skb, iph,
362 iph->ihl * 4, ntohs(iph->tot_len), 0))
365 case IPPROTO_UDPLITE:
366 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
367 if (!tcf_csum_ipv4_udp(skb, iph,
368 iph->ihl * 4, ntohs(iph->tot_len), 1))
373 if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
374 if (skb_cloned(skb) &&
375 !skb_clone_writable(skb, sizeof(*iph) + ntkoff) &&
376 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
388 static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh,
389 unsigned int ixhl, unsigned int *pl)
391 int off, len, optlen;
392 unsigned char *xh = (void *)ip6xh;
394 off = sizeof(*ip6xh);
404 optlen = xh[off + 1] + 2;
405 if (optlen != 6 || len < 6 || (off & 3) != 2)
406 /* wrong jumbo option length/alignment */
408 *pl = ntohl(*(__be32 *)(xh + off + 2));
411 optlen = xh[off + 1] + 2;
413 /* ignore obscure options */
425 static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
427 struct ipv6hdr *ip6h;
428 struct ipv6_opt_hdr *ip6xh;
429 unsigned int hl, ixhl;
434 ntkoff = skb_network_offset(skb);
438 if (!pskb_may_pull(skb, hl + ntkoff))
441 ip6h = ipv6_hdr(skb);
443 pl = ntohs(ip6h->payload_len);
444 nexthdr = ip6h->nexthdr;
448 case NEXTHDR_FRAGMENT:
450 case NEXTHDR_ROUTING:
453 if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
455 ip6xh = (void *)(skb_network_header(skb) + hl);
456 ixhl = ipv6_optlen(ip6xh);
457 if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
459 if ((nexthdr == NEXTHDR_HOP) &&
460 !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
462 nexthdr = ip6xh->nexthdr;
466 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
467 if (!tcf_csum_ipv6_icmp(skb, ip6h,
468 hl, pl + sizeof(*ip6h)))
472 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
473 if (!tcf_csum_ipv6_tcp(skb, ip6h,
474 hl, pl + sizeof(*ip6h)))
478 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
479 if (!tcf_csum_ipv6_udp(skb, ip6h,
480 hl, pl + sizeof(*ip6h), 0))
483 case IPPROTO_UDPLITE:
484 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
485 if (!tcf_csum_ipv6_udp(skb, ip6h,
486 hl, pl + sizeof(*ip6h), 1))
492 } while (pskb_may_pull(skb, hl + 1 + ntkoff));
502 static int tcf_csum(struct sk_buff *skb,
503 struct tc_action *a, struct tcf_result *res)
505 struct tcf_csum *p = a->priv;
509 spin_lock(&p->tcf_lock);
510 p->tcf_tm.lastuse = jiffies;
511 p->tcf_bstats.bytes += qdisc_pkt_len(skb);
512 p->tcf_bstats.packets++;
513 action = p->tcf_action;
514 update_flags = p->update_flags;
515 spin_unlock(&p->tcf_lock);
517 if (unlikely(action == TC_ACT_SHOT))
520 switch (skb->protocol) {
521 case cpu_to_be16(ETH_P_IP):
522 if (!tcf_csum_ipv4(skb, update_flags))
525 case cpu_to_be16(ETH_P_IPV6):
526 if (!tcf_csum_ipv6(skb, update_flags))
534 spin_lock(&p->tcf_lock);
535 p->tcf_qstats.drops++;
536 spin_unlock(&p->tcf_lock);
540 static int tcf_csum_dump(struct sk_buff *skb,
541 struct tc_action *a, int bind, int ref)
543 unsigned char *b = skb_tail_pointer(skb);
544 struct tcf_csum *p = a->priv;
545 struct tc_csum opt = {
546 .update_flags = p->update_flags,
548 .index = p->tcf_index,
549 .action = p->tcf_action,
550 .refcnt = p->tcf_refcnt - ref,
551 .bindcnt = p->tcf_bindcnt - bind,
555 NLA_PUT(skb, TCA_CSUM_PARMS, sizeof(opt), &opt);
556 t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
557 t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
558 t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
559 NLA_PUT(skb, TCA_CSUM_TM, sizeof(t), &t);
568 static struct tc_action_ops act_csum_ops = {
570 .hinfo = &csum_hash_info,
571 .type = TCA_ACT_CSUM,
572 .capab = TCA_CAP_NONE,
573 .owner = THIS_MODULE,
575 .dump = tcf_csum_dump,
576 .cleanup = tcf_csum_cleanup,
577 .lookup = tcf_hash_search,
578 .init = tcf_csum_init,
579 .walk = tcf_generic_walker
582 MODULE_DESCRIPTION("Checksum updating actions");
583 MODULE_LICENSE("GPL");
585 static int __init csum_init_module(void)
587 return tcf_register_action(&act_csum_ops);
590 static void __exit csum_cleanup_module(void)
592 tcf_unregister_action(&act_csum_ops);
595 module_init(csum_init_module);
596 module_exit(csum_cleanup_module);