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 static const struct nla_policy csum_policy
[TCA_CSUM_MAX
+ 1] = {
41 [TCA_CSUM_PARMS
] = { .len
= sizeof(struct tc_csum
), },
44 static unsigned int csum_net_id
;
45 static struct tc_action_ops act_csum_ops
;
47 static int tcf_csum_init(struct net
*net
, struct nlattr
*nla
,
48 struct nlattr
*est
, struct tc_action
**a
, int ovr
,
49 int bind
, bool rtnl_held
,
50 struct netlink_ext_ack
*extack
)
52 struct tc_action_net
*tn
= net_generic(net
, csum_net_id
);
53 struct tcf_csum_params
*params_new
;
54 struct nlattr
*tb
[TCA_CSUM_MAX
+ 1];
63 err
= nla_parse_nested(tb
, TCA_CSUM_MAX
, nla
, csum_policy
, NULL
);
67 if (tb
[TCA_CSUM_PARMS
] == NULL
)
69 parm
= nla_data(tb
[TCA_CSUM_PARMS
]);
71 err
= tcf_idr_check_alloc(tn
, &index
, a
, bind
);
73 ret
= tcf_idr_create(tn
, index
, est
, a
,
74 &act_csum_ops
, bind
, true);
76 tcf_idr_cleanup(tn
, index
);
81 if (bind
)/* dont override defaults */
84 tcf_idr_release(*a
, bind
);
93 params_new
= kzalloc(sizeof(*params_new
), GFP_KERNEL
);
94 if (unlikely(!params_new
)) {
95 tcf_idr_release(*a
, bind
);
98 params_new
->update_flags
= parm
->update_flags
;
100 spin_lock_bh(&p
->tcf_lock
);
101 p
->tcf_action
= parm
->action
;
102 rcu_swap_protected(p
->params
, params_new
,
103 lockdep_is_held(&p
->tcf_lock
));
104 spin_unlock_bh(&p
->tcf_lock
);
107 kfree_rcu(params_new
, rcu
);
109 if (ret
== ACT_P_CREATED
)
110 tcf_idr_insert(tn
, *a
);
116 * tcf_csum_skb_nextlayer - Get next layer pointer
117 * @skb: sk_buff to use
118 * @ihl: previous summed headers length
119 * @ipl: complete packet length
120 * @jhl: next header length
122 * Check the expected next layer availability in the specified sk_buff.
123 * Return the next layer pointer if pass, NULL otherwise.
125 static void *tcf_csum_skb_nextlayer(struct sk_buff
*skb
,
126 unsigned int ihl
, unsigned int ipl
,
129 int ntkoff
= skb_network_offset(skb
);
132 if (!pskb_may_pull(skb
, ipl
+ ntkoff
) || (ipl
< hl
) ||
133 skb_try_make_writable(skb
, hl
+ ntkoff
))
136 return (void *)(skb_network_header(skb
) + ihl
);
139 static int tcf_csum_ipv4_icmp(struct sk_buff
*skb
, unsigned int ihl
,
142 struct icmphdr
*icmph
;
144 icmph
= tcf_csum_skb_nextlayer(skb
, ihl
, ipl
, sizeof(*icmph
));
149 skb
->csum
= csum_partial(icmph
, ipl
- ihl
, 0);
150 icmph
->checksum
= csum_fold(skb
->csum
);
152 skb
->ip_summed
= CHECKSUM_NONE
;
157 static int tcf_csum_ipv4_igmp(struct sk_buff
*skb
,
158 unsigned int ihl
, unsigned int ipl
)
160 struct igmphdr
*igmph
;
162 igmph
= tcf_csum_skb_nextlayer(skb
, ihl
, ipl
, sizeof(*igmph
));
167 skb
->csum
= csum_partial(igmph
, ipl
- ihl
, 0);
168 igmph
->csum
= csum_fold(skb
->csum
);
170 skb
->ip_summed
= CHECKSUM_NONE
;
175 static int tcf_csum_ipv6_icmp(struct sk_buff
*skb
, unsigned int ihl
,
178 struct icmp6hdr
*icmp6h
;
179 const struct ipv6hdr
*ip6h
;
181 icmp6h
= tcf_csum_skb_nextlayer(skb
, ihl
, ipl
, sizeof(*icmp6h
));
185 ip6h
= ipv6_hdr(skb
);
186 icmp6h
->icmp6_cksum
= 0;
187 skb
->csum
= csum_partial(icmp6h
, ipl
- ihl
, 0);
188 icmp6h
->icmp6_cksum
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
189 ipl
- ihl
, IPPROTO_ICMPV6
,
192 skb
->ip_summed
= CHECKSUM_NONE
;
197 static int tcf_csum_ipv4_tcp(struct sk_buff
*skb
, unsigned int ihl
,
201 const struct iphdr
*iph
;
203 if (skb_is_gso(skb
) && skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV4
)
206 tcph
= tcf_csum_skb_nextlayer(skb
, ihl
, ipl
, sizeof(*tcph
));
212 skb
->csum
= csum_partial(tcph
, ipl
- ihl
, 0);
213 tcph
->check
= tcp_v4_check(ipl
- ihl
,
214 iph
->saddr
, iph
->daddr
, skb
->csum
);
216 skb
->ip_summed
= CHECKSUM_NONE
;
221 static int tcf_csum_ipv6_tcp(struct sk_buff
*skb
, unsigned int ihl
,
225 const struct ipv6hdr
*ip6h
;
227 if (skb_is_gso(skb
) && skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV6
)
230 tcph
= tcf_csum_skb_nextlayer(skb
, ihl
, ipl
, sizeof(*tcph
));
234 ip6h
= ipv6_hdr(skb
);
236 skb
->csum
= csum_partial(tcph
, ipl
- ihl
, 0);
237 tcph
->check
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
,
238 ipl
- ihl
, IPPROTO_TCP
,
241 skb
->ip_summed
= CHECKSUM_NONE
;
246 static int tcf_csum_ipv4_udp(struct sk_buff
*skb
, unsigned int ihl
,
247 unsigned int ipl
, int udplite
)
250 const struct iphdr
*iph
;
253 if (skb_is_gso(skb
) && skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP
)
257 * Support both UDP and UDPLITE checksum algorithms, Don't use
258 * udph->len to get the real length without any protocol check,
259 * UDPLITE uses udph->len for another thing,
260 * Use iph->tot_len, or just ipl.
263 udph
= tcf_csum_skb_nextlayer(skb
, ihl
, ipl
, sizeof(*udph
));
268 ul
= ntohs(udph
->len
);
270 if (udplite
|| udph
->check
) {
276 skb
->csum
= csum_partial(udph
, ipl
- ihl
, 0);
277 else if ((ul
>= sizeof(*udph
)) && (ul
<= ipl
- ihl
))
278 skb
->csum
= csum_partial(udph
, ul
, 0);
280 goto ignore_obscure_skb
;
283 goto ignore_obscure_skb
;
285 skb
->csum
= csum_partial(udph
, ul
, 0);
288 udph
->check
= csum_tcpudp_magic(iph
->saddr
, iph
->daddr
,
293 udph
->check
= CSUM_MANGLED_0
;
296 skb
->ip_summed
= CHECKSUM_NONE
;
302 static int tcf_csum_ipv6_udp(struct sk_buff
*skb
, unsigned int ihl
,
303 unsigned int ipl
, int udplite
)
306 const struct ipv6hdr
*ip6h
;
309 if (skb_is_gso(skb
) && skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP
)
313 * Support both UDP and UDPLITE checksum algorithms, Don't use
314 * udph->len to get the real length without any protocol check,
315 * UDPLITE uses udph->len for another thing,
316 * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
319 udph
= tcf_csum_skb_nextlayer(skb
, ihl
, ipl
, sizeof(*udph
));
323 ip6h
= ipv6_hdr(skb
);
324 ul
= ntohs(udph
->len
);
330 skb
->csum
= csum_partial(udph
, ipl
- ihl
, 0);
332 else if ((ul
>= sizeof(*udph
)) && (ul
<= ipl
- ihl
))
333 skb
->csum
= csum_partial(udph
, ul
, 0);
336 goto ignore_obscure_skb
;
339 goto ignore_obscure_skb
;
341 skb
->csum
= csum_partial(udph
, ul
, 0);
344 udph
->check
= csum_ipv6_magic(&ip6h
->saddr
, &ip6h
->daddr
, ul
,
345 udplite
? IPPROTO_UDPLITE
: IPPROTO_UDP
,
349 udph
->check
= CSUM_MANGLED_0
;
351 skb
->ip_summed
= CHECKSUM_NONE
;
357 static int tcf_csum_sctp(struct sk_buff
*skb
, unsigned int ihl
,
360 struct sctphdr
*sctph
;
362 if (skb_is_gso(skb
) && skb_is_gso_sctp(skb
))
365 sctph
= tcf_csum_skb_nextlayer(skb
, ihl
, ipl
, sizeof(*sctph
));
369 sctph
->checksum
= sctp_compute_cksum(skb
,
370 skb_network_offset(skb
) + ihl
);
371 skb
->ip_summed
= CHECKSUM_NONE
;
372 skb
->csum_not_inet
= 0;
377 static int tcf_csum_ipv4(struct sk_buff
*skb
, u32 update_flags
)
379 const struct iphdr
*iph
;
382 ntkoff
= skb_network_offset(skb
);
384 if (!pskb_may_pull(skb
, sizeof(*iph
) + ntkoff
))
389 switch (iph
->frag_off
& htons(IP_OFFSET
) ? 0 : iph
->protocol
) {
391 if (update_flags
& TCA_CSUM_UPDATE_FLAG_ICMP
)
392 if (!tcf_csum_ipv4_icmp(skb
, iph
->ihl
* 4,
393 ntohs(iph
->tot_len
)))
397 if (update_flags
& TCA_CSUM_UPDATE_FLAG_IGMP
)
398 if (!tcf_csum_ipv4_igmp(skb
, iph
->ihl
* 4,
399 ntohs(iph
->tot_len
)))
403 if (update_flags
& TCA_CSUM_UPDATE_FLAG_TCP
)
404 if (!tcf_csum_ipv4_tcp(skb
, iph
->ihl
* 4,
405 ntohs(iph
->tot_len
)))
409 if (update_flags
& TCA_CSUM_UPDATE_FLAG_UDP
)
410 if (!tcf_csum_ipv4_udp(skb
, iph
->ihl
* 4,
411 ntohs(iph
->tot_len
), 0))
414 case IPPROTO_UDPLITE
:
415 if (update_flags
& TCA_CSUM_UPDATE_FLAG_UDPLITE
)
416 if (!tcf_csum_ipv4_udp(skb
, iph
->ihl
* 4,
417 ntohs(iph
->tot_len
), 1))
421 if ((update_flags
& TCA_CSUM_UPDATE_FLAG_SCTP
) &&
422 !tcf_csum_sctp(skb
, iph
->ihl
* 4, ntohs(iph
->tot_len
)))
427 if (update_flags
& TCA_CSUM_UPDATE_FLAG_IPV4HDR
) {
428 if (skb_try_make_writable(skb
, sizeof(*iph
) + ntkoff
))
431 ip_send_check(ip_hdr(skb
));
440 static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr
*ip6xh
, unsigned int ixhl
,
443 int off
, len
, optlen
;
444 unsigned char *xh
= (void *)ip6xh
;
446 off
= sizeof(*ip6xh
);
455 optlen
= xh
[off
+ 1] + 2;
456 if (optlen
!= 6 || len
< 6 || (off
& 3) != 2)
457 /* wrong jumbo option length/alignment */
459 *pl
= ntohl(*(__be32
*)(xh
+ off
+ 2));
462 optlen
= xh
[off
+ 1] + 2;
464 /* ignore obscure options */
476 static int tcf_csum_ipv6(struct sk_buff
*skb
, u32 update_flags
)
478 struct ipv6hdr
*ip6h
;
479 struct ipv6_opt_hdr
*ip6xh
;
480 unsigned int hl
, ixhl
;
485 ntkoff
= skb_network_offset(skb
);
489 if (!pskb_may_pull(skb
, hl
+ ntkoff
))
492 ip6h
= ipv6_hdr(skb
);
494 pl
= ntohs(ip6h
->payload_len
);
495 nexthdr
= ip6h
->nexthdr
;
499 case NEXTHDR_FRAGMENT
:
501 case NEXTHDR_ROUTING
:
504 if (!pskb_may_pull(skb
, hl
+ sizeof(*ip6xh
) + ntkoff
))
506 ip6xh
= (void *)(skb_network_header(skb
) + hl
);
507 ixhl
= ipv6_optlen(ip6xh
);
508 if (!pskb_may_pull(skb
, hl
+ ixhl
+ ntkoff
))
510 ip6xh
= (void *)(skb_network_header(skb
) + hl
);
511 if ((nexthdr
== NEXTHDR_HOP
) &&
512 !(tcf_csum_ipv6_hopopts(ip6xh
, ixhl
, &pl
)))
514 nexthdr
= ip6xh
->nexthdr
;
518 if (update_flags
& TCA_CSUM_UPDATE_FLAG_ICMP
)
519 if (!tcf_csum_ipv6_icmp(skb
,
520 hl
, pl
+ sizeof(*ip6h
)))
524 if (update_flags
& TCA_CSUM_UPDATE_FLAG_TCP
)
525 if (!tcf_csum_ipv6_tcp(skb
,
526 hl
, pl
+ sizeof(*ip6h
)))
530 if (update_flags
& TCA_CSUM_UPDATE_FLAG_UDP
)
531 if (!tcf_csum_ipv6_udp(skb
, hl
,
532 pl
+ sizeof(*ip6h
), 0))
535 case IPPROTO_UDPLITE
:
536 if (update_flags
& TCA_CSUM_UPDATE_FLAG_UDPLITE
)
537 if (!tcf_csum_ipv6_udp(skb
, hl
,
538 pl
+ sizeof(*ip6h
), 1))
542 if ((update_flags
& TCA_CSUM_UPDATE_FLAG_SCTP
) &&
543 !tcf_csum_sctp(skb
, hl
, pl
+ sizeof(*ip6h
)))
549 } while (pskb_may_pull(skb
, hl
+ 1 + ntkoff
));
559 static int tcf_csum_act(struct sk_buff
*skb
, const struct tc_action
*a
,
560 struct tcf_result
*res
)
562 struct tcf_csum
*p
= to_tcf_csum(a
);
563 bool orig_vlan_tag_present
= false;
564 unsigned int vlan_hdr_count
= 0;
565 struct tcf_csum_params
*params
;
570 params
= rcu_dereference_bh(p
->params
);
572 tcf_lastuse_update(&p
->tcf_tm
);
573 bstats_cpu_update(this_cpu_ptr(p
->common
.cpu_bstats
), skb
);
575 action
= READ_ONCE(p
->tcf_action
);
576 if (unlikely(action
== TC_ACT_SHOT
))
579 update_flags
= params
->update_flags
;
580 protocol
= skb_protocol(skb
, false);
583 case cpu_to_be16(ETH_P_IP
):
584 if (!tcf_csum_ipv4(skb
, update_flags
))
587 case cpu_to_be16(ETH_P_IPV6
):
588 if (!tcf_csum_ipv6(skb
, update_flags
))
591 case cpu_to_be16(ETH_P_8021AD
): /* fall through */
592 case cpu_to_be16(ETH_P_8021Q
):
593 if (skb_vlan_tag_present(skb
) && !orig_vlan_tag_present
) {
594 protocol
= skb
->protocol
;
595 orig_vlan_tag_present
= true;
597 struct vlan_hdr
*vlan
= (struct vlan_hdr
*)skb
->data
;
599 protocol
= vlan
->h_vlan_encapsulated_proto
;
600 skb_pull(skb
, VLAN_HLEN
);
601 skb_reset_network_header(skb
);
608 /* Restore the skb for the pulled VLAN tags */
609 while (vlan_hdr_count
--) {
610 skb_push(skb
, VLAN_HLEN
);
611 skb_reset_network_header(skb
);
617 qstats_drop_inc(this_cpu_ptr(p
->common
.cpu_qstats
));
618 action
= TC_ACT_SHOT
;
622 static int tcf_csum_dump(struct sk_buff
*skb
, struct tc_action
*a
, int bind
,
625 unsigned char *b
= skb_tail_pointer(skb
);
626 struct tcf_csum
*p
= to_tcf_csum(a
);
627 struct tcf_csum_params
*params
;
628 struct tc_csum opt
= {
629 .index
= p
->tcf_index
,
630 .refcnt
= refcount_read(&p
->tcf_refcnt
) - ref
,
631 .bindcnt
= atomic_read(&p
->tcf_bindcnt
) - bind
,
635 spin_lock_bh(&p
->tcf_lock
);
636 params
= rcu_dereference_protected(p
->params
,
637 lockdep_is_held(&p
->tcf_lock
));
638 opt
.action
= p
->tcf_action
;
639 opt
.update_flags
= params
->update_flags
;
641 if (nla_put(skb
, TCA_CSUM_PARMS
, sizeof(opt
), &opt
))
642 goto nla_put_failure
;
644 tcf_tm_dump(&t
, &p
->tcf_tm
);
645 if (nla_put_64bit(skb
, TCA_CSUM_TM
, sizeof(t
), &t
, TCA_CSUM_PAD
))
646 goto nla_put_failure
;
647 spin_unlock_bh(&p
->tcf_lock
);
652 spin_unlock_bh(&p
->tcf_lock
);
657 static void tcf_csum_cleanup(struct tc_action
*a
)
659 struct tcf_csum
*p
= to_tcf_csum(a
);
660 struct tcf_csum_params
*params
;
662 params
= rcu_dereference_protected(p
->params
, 1);
664 kfree_rcu(params
, rcu
);
667 static int tcf_csum_walker(struct net
*net
, struct sk_buff
*skb
,
668 struct netlink_callback
*cb
, int type
,
669 const struct tc_action_ops
*ops
,
670 struct netlink_ext_ack
*extack
)
672 struct tc_action_net
*tn
= net_generic(net
, csum_net_id
);
674 return tcf_generic_walker(tn
, skb
, cb
, type
, ops
, extack
);
677 static int tcf_csum_search(struct net
*net
, struct tc_action
**a
, u32 index
,
678 struct netlink_ext_ack
*extack
)
680 struct tc_action_net
*tn
= net_generic(net
, csum_net_id
);
682 return tcf_idr_search(tn
, a
, index
);
685 static size_t tcf_csum_get_fill_size(const struct tc_action
*act
)
687 return nla_total_size(sizeof(struct tc_csum
));
690 static struct tc_action_ops act_csum_ops
= {
692 .type
= TCA_ACT_CSUM
,
693 .owner
= THIS_MODULE
,
695 .dump
= tcf_csum_dump
,
696 .init
= tcf_csum_init
,
697 .cleanup
= tcf_csum_cleanup
,
698 .walk
= tcf_csum_walker
,
699 .lookup
= tcf_csum_search
,
700 .get_fill_size
= tcf_csum_get_fill_size
,
701 .size
= sizeof(struct tcf_csum
),
704 static __net_init
int csum_init_net(struct net
*net
)
706 struct tc_action_net
*tn
= net_generic(net
, csum_net_id
);
708 return tc_action_net_init(net
, tn
, &act_csum_ops
);
711 static void __net_exit
csum_exit_net(struct list_head
*net_list
)
713 tc_action_net_exit(net_list
, csum_net_id
);
716 static struct pernet_operations csum_net_ops
= {
717 .init
= csum_init_net
,
718 .exit_batch
= csum_exit_net
,
720 .size
= sizeof(struct tc_action_net
),
723 MODULE_DESCRIPTION("Checksum updating actions");
724 MODULE_LICENSE("GPL");
726 static int __init
csum_init_module(void)
728 return tcf_register_action(&act_csum_ops
, &csum_net_ops
);
731 static void __exit
csum_cleanup_module(void)
733 tcf_unregister_action(&act_csum_ops
, &csum_net_ops
);
736 module_init(csum_init_module
);
737 module_exit(csum_cleanup_module
);