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 u32 csum_idx_gen;
41 static struct tcf_hashinfo csum_hash_info;
43 static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
44 [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
47 static int tcf_csum_init(struct net *n, struct nlattr *nla, struct nlattr *est,
48 struct tc_action *a, int ovr, int bind)
50 struct nlattr *tb[TCA_CSUM_MAX + 1];
52 struct tcf_common *pc;
59 err = nla_parse_nested(tb, TCA_CSUM_MAX, nla, csum_policy);
63 if (tb[TCA_CSUM_PARMS] == NULL)
65 parm = nla_data(tb[TCA_CSUM_PARMS]);
67 pc = tcf_hash_check(parm->index, a, bind, &csum_hash_info);
69 pc = tcf_hash_create(parm->index, est, a, sizeof(*p), bind,
70 &csum_idx_gen, &csum_hash_info);
75 if (bind)/* dont override defaults */
77 tcf_hash_release(pc, bind, &csum_hash_info);
83 spin_lock_bh(&p->tcf_lock);
84 p->tcf_action = parm->action;
85 p->update_flags = parm->update_flags;
86 spin_unlock_bh(&p->tcf_lock);
88 if (ret == ACT_P_CREATED)
89 tcf_hash_insert(pc, &csum_hash_info);
94 static int tcf_csum_cleanup(struct tc_action *a, int bind)
96 struct tcf_csum *p = a->priv;
97 return tcf_hash_release(&p->common, bind, &csum_hash_info);
101 * tcf_csum_skb_nextlayer - Get next layer pointer
102 * @skb: sk_buff to use
103 * @ihl: previous summed headers length
104 * @ipl: complete packet length
105 * @jhl: next header length
107 * Check the expected next layer availability in the specified sk_buff.
108 * Return the next layer pointer if pass, NULL otherwise.
110 static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
111 unsigned int ihl, unsigned int ipl,
114 int ntkoff = skb_network_offset(skb);
117 if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
119 !skb_clone_writable(skb, hl + ntkoff) &&
120 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
123 return (void *)(skb_network_header(skb) + ihl);
126 static int tcf_csum_ipv4_icmp(struct sk_buff *skb,
127 unsigned int ihl, unsigned int ipl)
129 struct icmphdr *icmph;
131 icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
136 skb->csum = csum_partial(icmph, ipl - ihl, 0);
137 icmph->checksum = csum_fold(skb->csum);
139 skb->ip_summed = CHECKSUM_NONE;
144 static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
145 unsigned int ihl, unsigned int ipl)
147 struct igmphdr *igmph;
149 igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
154 skb->csum = csum_partial(igmph, ipl - ihl, 0);
155 igmph->csum = csum_fold(skb->csum);
157 skb->ip_summed = CHECKSUM_NONE;
162 static int tcf_csum_ipv6_icmp(struct sk_buff *skb,
163 unsigned int ihl, unsigned int ipl)
165 struct icmp6hdr *icmp6h;
166 const struct ipv6hdr *ip6h;
168 icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
172 ip6h = ipv6_hdr(skb);
173 icmp6h->icmp6_cksum = 0;
174 skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
175 icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
176 ipl - ihl, IPPROTO_ICMPV6,
179 skb->ip_summed = CHECKSUM_NONE;
184 static int tcf_csum_ipv4_tcp(struct sk_buff *skb,
185 unsigned int ihl, unsigned int ipl)
188 const struct iphdr *iph;
190 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
196 skb->csum = csum_partial(tcph, ipl - ihl, 0);
197 tcph->check = tcp_v4_check(ipl - ihl,
198 iph->saddr, iph->daddr, skb->csum);
200 skb->ip_summed = CHECKSUM_NONE;
205 static int tcf_csum_ipv6_tcp(struct sk_buff *skb,
206 unsigned int ihl, unsigned int ipl)
209 const struct ipv6hdr *ip6h;
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,
228 unsigned int ihl, unsigned int ipl, int udplite)
231 const struct iphdr *iph;
235 * Support both UDP and UDPLITE checksum algorithms, Don't use
236 * udph->len to get the real length without any protocol check,
237 * UDPLITE uses udph->len for another thing,
238 * Use iph->tot_len, or just ipl.
241 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
246 ul = ntohs(udph->len);
248 if (udplite || udph->check) {
254 skb->csum = csum_partial(udph, ipl - ihl, 0);
255 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
256 skb->csum = csum_partial(udph, ul, 0);
258 goto ignore_obscure_skb;
261 goto ignore_obscure_skb;
263 skb->csum = csum_partial(udph, ul, 0);
266 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
271 udph->check = CSUM_MANGLED_0;
274 skb->ip_summed = CHECKSUM_NONE;
280 static int tcf_csum_ipv6_udp(struct sk_buff *skb,
281 unsigned int ihl, unsigned int ipl, int udplite)
284 const struct ipv6hdr *ip6h;
288 * Support both UDP and UDPLITE checksum algorithms, Don't use
289 * udph->len to get the real length without any protocol check,
290 * UDPLITE uses udph->len for another thing,
291 * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
294 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
298 ip6h = ipv6_hdr(skb);
299 ul = ntohs(udph->len);
305 skb->csum = csum_partial(udph, ipl - ihl, 0);
307 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
308 skb->csum = csum_partial(udph, ul, 0);
311 goto ignore_obscure_skb;
314 goto ignore_obscure_skb;
316 skb->csum = csum_partial(udph, ul, 0);
319 udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
320 udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
324 udph->check = CSUM_MANGLED_0;
326 skb->ip_summed = CHECKSUM_NONE;
332 static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
334 const struct iphdr *iph;
337 ntkoff = skb_network_offset(skb);
339 if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
344 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
346 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
347 if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
348 ntohs(iph->tot_len)))
352 if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
353 if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
354 ntohs(iph->tot_len)))
358 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
359 if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4,
360 ntohs(iph->tot_len)))
364 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
365 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
366 ntohs(iph->tot_len), 0))
369 case IPPROTO_UDPLITE:
370 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
371 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
372 ntohs(iph->tot_len), 1))
377 if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
378 if (skb_cloned(skb) &&
379 !skb_clone_writable(skb, sizeof(*iph) + ntkoff) &&
380 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
383 ip_send_check(ip_hdr(skb));
392 static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh,
393 unsigned int ixhl, unsigned int *pl)
395 int off, len, optlen;
396 unsigned char *xh = (void *)ip6xh;
398 off = sizeof(*ip6xh);
407 optlen = xh[off + 1] + 2;
408 if (optlen != 6 || len < 6 || (off & 3) != 2)
409 /* wrong jumbo option length/alignment */
411 *pl = ntohl(*(__be32 *)(xh + off + 2));
414 optlen = xh[off + 1] + 2;
416 /* ignore obscure options */
428 static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
430 struct ipv6hdr *ip6h;
431 struct ipv6_opt_hdr *ip6xh;
432 unsigned int hl, ixhl;
437 ntkoff = skb_network_offset(skb);
441 if (!pskb_may_pull(skb, hl + ntkoff))
444 ip6h = ipv6_hdr(skb);
446 pl = ntohs(ip6h->payload_len);
447 nexthdr = ip6h->nexthdr;
451 case NEXTHDR_FRAGMENT:
453 case NEXTHDR_ROUTING:
456 if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
458 ip6xh = (void *)(skb_network_header(skb) + hl);
459 ixhl = ipv6_optlen(ip6xh);
460 if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
462 ip6xh = (void *)(skb_network_header(skb) + hl);
463 if ((nexthdr == NEXTHDR_HOP) &&
464 !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
466 nexthdr = ip6xh->nexthdr;
470 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
471 if (!tcf_csum_ipv6_icmp(skb,
472 hl, pl + sizeof(*ip6h)))
476 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
477 if (!tcf_csum_ipv6_tcp(skb,
478 hl, pl + sizeof(*ip6h)))
482 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
483 if (!tcf_csum_ipv6_udp(skb, hl,
484 pl + sizeof(*ip6h), 0))
487 case IPPROTO_UDPLITE:
488 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
489 if (!tcf_csum_ipv6_udp(skb, hl,
490 pl + sizeof(*ip6h), 1))
496 } while (pskb_may_pull(skb, hl + 1 + ntkoff));
506 static int tcf_csum(struct sk_buff *skb,
507 const struct tc_action *a, struct tcf_result *res)
509 struct tcf_csum *p = a->priv;
513 spin_lock(&p->tcf_lock);
514 p->tcf_tm.lastuse = jiffies;
515 bstats_update(&p->tcf_bstats, skb);
516 action = p->tcf_action;
517 update_flags = p->update_flags;
518 spin_unlock(&p->tcf_lock);
520 if (unlikely(action == TC_ACT_SHOT))
523 switch (skb->protocol) {
524 case cpu_to_be16(ETH_P_IP):
525 if (!tcf_csum_ipv4(skb, update_flags))
528 case cpu_to_be16(ETH_P_IPV6):
529 if (!tcf_csum_ipv6(skb, update_flags))
537 spin_lock(&p->tcf_lock);
538 p->tcf_qstats.drops++;
539 spin_unlock(&p->tcf_lock);
543 static int tcf_csum_dump(struct sk_buff *skb,
544 struct tc_action *a, int bind, int ref)
546 unsigned char *b = skb_tail_pointer(skb);
547 struct tcf_csum *p = a->priv;
548 struct tc_csum opt = {
549 .update_flags = p->update_flags,
550 .index = p->tcf_index,
551 .action = p->tcf_action,
552 .refcnt = p->tcf_refcnt - ref,
553 .bindcnt = p->tcf_bindcnt - bind,
557 if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
558 goto nla_put_failure;
559 t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
560 t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
561 t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
562 if (nla_put(skb, TCA_CSUM_TM, sizeof(t), &t))
563 goto nla_put_failure;
572 static struct tc_action_ops act_csum_ops = {
574 .hinfo = &csum_hash_info,
575 .type = TCA_ACT_CSUM,
576 .capab = TCA_CAP_NONE,
577 .owner = THIS_MODULE,
579 .dump = tcf_csum_dump,
580 .cleanup = tcf_csum_cleanup,
581 .init = tcf_csum_init,
584 MODULE_DESCRIPTION("Checksum updating actions");
585 MODULE_LICENSE("GPL");
587 static int __init csum_init_module(void)
589 int err = tcf_hashinfo_init(&csum_hash_info, CSUM_TAB_MASK);
593 return tcf_register_action(&act_csum_ops);
596 static void __exit csum_cleanup_module(void)
598 tcf_unregister_action(&act_csum_ops);
601 module_init(csum_init_module);
602 module_exit(csum_cleanup_module);