2 * Checksum updating actions
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_hashinfo csum_hash_info;
42 static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
43 [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
46 static int tcf_csum_init(struct net *n, struct nlattr *nla, struct nlattr *est,
47 struct tc_action *a, int ovr, int bind)
49 struct nlattr *tb[TCA_CSUM_MAX + 1];
57 err = nla_parse_nested(tb, TCA_CSUM_MAX, nla, csum_policy);
61 if (tb[TCA_CSUM_PARMS] == NULL)
63 parm = nla_data(tb[TCA_CSUM_PARMS]);
65 if (!tcf_hash_check(parm->index, a, bind)) {
66 ret = tcf_hash_create(parm->index, est, a, sizeof(*p), bind);
71 if (bind)/* dont override defaults */
73 tcf_hash_release(a, bind);
79 spin_lock_bh(&p->tcf_lock);
80 p->tcf_action = parm->action;
81 p->update_flags = parm->update_flags;
82 spin_unlock_bh(&p->tcf_lock);
84 if (ret == ACT_P_CREATED)
91 * tcf_csum_skb_nextlayer - Get next layer pointer
92 * @skb: sk_buff to use
93 * @ihl: previous summed headers length
94 * @ipl: complete packet length
95 * @jhl: next header length
97 * Check the expected next layer availability in the specified sk_buff.
98 * Return the next layer pointer if pass, NULL otherwise.
100 static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
101 unsigned int ihl, unsigned int ipl,
104 int ntkoff = skb_network_offset(skb);
107 if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
109 !skb_clone_writable(skb, hl + ntkoff) &&
110 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
113 return (void *)(skb_network_header(skb) + ihl);
116 static int tcf_csum_ipv4_icmp(struct sk_buff *skb,
117 unsigned int ihl, unsigned int ipl)
119 struct icmphdr *icmph;
121 icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
126 skb->csum = csum_partial(icmph, ipl - ihl, 0);
127 icmph->checksum = csum_fold(skb->csum);
129 skb->ip_summed = CHECKSUM_NONE;
134 static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
135 unsigned int ihl, unsigned int ipl)
137 struct igmphdr *igmph;
139 igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
144 skb->csum = csum_partial(igmph, ipl - ihl, 0);
145 igmph->csum = csum_fold(skb->csum);
147 skb->ip_summed = CHECKSUM_NONE;
152 static int tcf_csum_ipv6_icmp(struct sk_buff *skb,
153 unsigned int ihl, unsigned int ipl)
155 struct icmp6hdr *icmp6h;
156 const struct ipv6hdr *ip6h;
158 icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
162 ip6h = ipv6_hdr(skb);
163 icmp6h->icmp6_cksum = 0;
164 skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
165 icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
166 ipl - ihl, IPPROTO_ICMPV6,
169 skb->ip_summed = CHECKSUM_NONE;
174 static int tcf_csum_ipv4_tcp(struct sk_buff *skb,
175 unsigned int ihl, unsigned int ipl)
178 const struct iphdr *iph;
180 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
186 skb->csum = csum_partial(tcph, ipl - ihl, 0);
187 tcph->check = tcp_v4_check(ipl - ihl,
188 iph->saddr, iph->daddr, skb->csum);
190 skb->ip_summed = CHECKSUM_NONE;
195 static int tcf_csum_ipv6_tcp(struct sk_buff *skb,
196 unsigned int ihl, unsigned int ipl)
199 const struct ipv6hdr *ip6h;
201 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
205 ip6h = ipv6_hdr(skb);
207 skb->csum = csum_partial(tcph, ipl - ihl, 0);
208 tcph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
209 ipl - ihl, IPPROTO_TCP,
212 skb->ip_summed = CHECKSUM_NONE;
217 static int tcf_csum_ipv4_udp(struct sk_buff *skb,
218 unsigned int ihl, unsigned int ipl, int udplite)
221 const struct iphdr *iph;
225 * Support both UDP and UDPLITE checksum algorithms, Don't use
226 * udph->len to get the real length without any protocol check,
227 * UDPLITE uses udph->len for another thing,
228 * Use iph->tot_len, or just ipl.
231 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
236 ul = ntohs(udph->len);
238 if (udplite || udph->check) {
244 skb->csum = csum_partial(udph, ipl - ihl, 0);
245 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
246 skb->csum = csum_partial(udph, ul, 0);
248 goto ignore_obscure_skb;
251 goto ignore_obscure_skb;
253 skb->csum = csum_partial(udph, ul, 0);
256 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
261 udph->check = CSUM_MANGLED_0;
264 skb->ip_summed = CHECKSUM_NONE;
270 static int tcf_csum_ipv6_udp(struct sk_buff *skb,
271 unsigned int ihl, unsigned int ipl, int udplite)
274 const struct ipv6hdr *ip6h;
278 * Support both UDP and UDPLITE checksum algorithms, Don't use
279 * udph->len to get the real length without any protocol check,
280 * UDPLITE uses udph->len for another thing,
281 * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
284 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
288 ip6h = ipv6_hdr(skb);
289 ul = ntohs(udph->len);
295 skb->csum = csum_partial(udph, ipl - ihl, 0);
297 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
298 skb->csum = csum_partial(udph, ul, 0);
301 goto ignore_obscure_skb;
304 goto ignore_obscure_skb;
306 skb->csum = csum_partial(udph, ul, 0);
309 udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
310 udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
314 udph->check = CSUM_MANGLED_0;
316 skb->ip_summed = CHECKSUM_NONE;
322 static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
324 const struct iphdr *iph;
327 ntkoff = skb_network_offset(skb);
329 if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
334 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
336 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
337 if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
338 ntohs(iph->tot_len)))
342 if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
343 if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
344 ntohs(iph->tot_len)))
348 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
349 if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4,
350 ntohs(iph->tot_len)))
354 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
355 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
356 ntohs(iph->tot_len), 0))
359 case IPPROTO_UDPLITE:
360 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
361 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
362 ntohs(iph->tot_len), 1))
367 if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
368 if (skb_cloned(skb) &&
369 !skb_clone_writable(skb, sizeof(*iph) + ntkoff) &&
370 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
373 ip_send_check(ip_hdr(skb));
382 static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh,
383 unsigned int ixhl, unsigned int *pl)
385 int off, len, optlen;
386 unsigned char *xh = (void *)ip6xh;
388 off = sizeof(*ip6xh);
397 optlen = xh[off + 1] + 2;
398 if (optlen != 6 || len < 6 || (off & 3) != 2)
399 /* wrong jumbo option length/alignment */
401 *pl = ntohl(*(__be32 *)(xh + off + 2));
404 optlen = xh[off + 1] + 2;
406 /* ignore obscure options */
418 static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
420 struct ipv6hdr *ip6h;
421 struct ipv6_opt_hdr *ip6xh;
422 unsigned int hl, ixhl;
427 ntkoff = skb_network_offset(skb);
431 if (!pskb_may_pull(skb, hl + ntkoff))
434 ip6h = ipv6_hdr(skb);
436 pl = ntohs(ip6h->payload_len);
437 nexthdr = ip6h->nexthdr;
441 case NEXTHDR_FRAGMENT:
443 case NEXTHDR_ROUTING:
446 if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
448 ip6xh = (void *)(skb_network_header(skb) + hl);
449 ixhl = ipv6_optlen(ip6xh);
450 if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
452 ip6xh = (void *)(skb_network_header(skb) + hl);
453 if ((nexthdr == NEXTHDR_HOP) &&
454 !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
456 nexthdr = ip6xh->nexthdr;
460 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
461 if (!tcf_csum_ipv6_icmp(skb,
462 hl, pl + sizeof(*ip6h)))
466 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
467 if (!tcf_csum_ipv6_tcp(skb,
468 hl, pl + sizeof(*ip6h)))
472 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
473 if (!tcf_csum_ipv6_udp(skb, hl,
474 pl + sizeof(*ip6h), 0))
477 case IPPROTO_UDPLITE:
478 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
479 if (!tcf_csum_ipv6_udp(skb, hl,
480 pl + sizeof(*ip6h), 1))
486 } while (pskb_may_pull(skb, hl + 1 + ntkoff));
496 static int tcf_csum(struct sk_buff *skb,
497 const struct tc_action *a, struct tcf_result *res)
499 struct tcf_csum *p = a->priv;
503 spin_lock(&p->tcf_lock);
504 p->tcf_tm.lastuse = jiffies;
505 bstats_update(&p->tcf_bstats, skb);
506 action = p->tcf_action;
507 update_flags = p->update_flags;
508 spin_unlock(&p->tcf_lock);
510 if (unlikely(action == TC_ACT_SHOT))
513 switch (skb->protocol) {
514 case cpu_to_be16(ETH_P_IP):
515 if (!tcf_csum_ipv4(skb, update_flags))
518 case cpu_to_be16(ETH_P_IPV6):
519 if (!tcf_csum_ipv6(skb, update_flags))
527 spin_lock(&p->tcf_lock);
528 p->tcf_qstats.drops++;
529 spin_unlock(&p->tcf_lock);
533 static int tcf_csum_dump(struct sk_buff *skb,
534 struct tc_action *a, int bind, int ref)
536 unsigned char *b = skb_tail_pointer(skb);
537 struct tcf_csum *p = a->priv;
538 struct tc_csum opt = {
539 .update_flags = p->update_flags,
540 .index = p->tcf_index,
541 .action = p->tcf_action,
542 .refcnt = p->tcf_refcnt - ref,
543 .bindcnt = p->tcf_bindcnt - bind,
547 if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
548 goto nla_put_failure;
549 t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
550 t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
551 t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
552 if (nla_put(skb, TCA_CSUM_TM, sizeof(t), &t))
553 goto nla_put_failure;
562 static struct tc_action_ops act_csum_ops = {
564 .hinfo = &csum_hash_info,
565 .type = TCA_ACT_CSUM,
566 .owner = THIS_MODULE,
568 .dump = tcf_csum_dump,
569 .init = tcf_csum_init,
572 MODULE_DESCRIPTION("Checksum updating actions");
573 MODULE_LICENSE("GPL");
575 static int __init csum_init_module(void)
577 int err = tcf_hashinfo_init(&csum_hash_info, CSUM_TAB_MASK);
581 return tcf_register_action(&act_csum_ops);
584 static void __exit csum_cleanup_module(void)
586 tcf_unregister_action(&act_csum_ops);
589 module_init(csum_init_module);
590 module_exit(csum_cleanup_module);