net/sched: add ACT_CSUM action to update packets checksums
Subsystems (3)
PATCH 1/1 net/sched: add ACT_CSUM action to update packets checksums
net/sched: add ACT_CSUM action to update packets checksums
ACT_CSUM can be called just after ACT_PEDIT in order to re-compute some altered checksums in IPv4 and IPv6 packets. The following checksums are supported by this patch:
- IPv4: IPv4 header, ICMP, IGMP, TCP, UDP & UDPLite
- IPv6: ICMPv6, TCP, UDP & UDPLite
It's possible to request in the same action to update different kind of checksums, if the packets flow mix TCP, UDP and UDPLite, ...
An example of usage is done in the associated iproute2 patch.
--- include/linux/tc_act/Kbuild | 1 + include/linux/tc_act/tc_csum.h | 32 ++ include/net/tc_act/tc_csum.h | 15 + net/sched/Kconfig | 10 + net/sched/Makefile | 1 + net/sched/act_csum.c | 700 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 759 insertions(+), 0 deletions(-) create mode 100644 include/linux/tc_act/tc_csum.h create mode 100644 include/net/tc_act/tc_csum.h create mode 100644 net/sched/act_csum.c
include/linux/tc_act/Kbuild +1 -0
@@ -4,3 +4,4 @@ header-y += tc_mirred.h header-y += tc_pedit.h header-y += tc_nat.h header-y += tc_skbedit.h +header-y += tc_csum.h
include/linux/tc_act/tc_csum.h new file +32 -0
@@ -0,0 +1,32 @@ +#ifndef __LINUX_TC_CSUM_H +#define __LINUX_TC_CSUM_H + +#include <linux/types.h> +#include <linux/pkt_cls.h> + +#define TCA_ACT_CSUM 16 + +enum { +TCA_CSUM_UNSPEC , +TCA_CSUM_PARMS , +TCA_CSUM_TM , + __TCA_CSUM_MAX +}; +#define TCA_CSUM_MAX (__TCA_CSUM_MAX - 1) + +enum { +TCA_CSUM_UPDATE_FLAG_IPV4HDR =1 , +TCA_CSUM_UPDATE_FLAG_ICMP =2 , +TCA_CSUM_UPDATE_FLAG_IGMP =4 , +TCA_CSUM_UPDATE_FLAG_TCP =8 , +TCA_CSUM_UPDATE_FLAG_UDP =16 , +TCA_CSUM_UPDATE_FLAG_UDPLITE =32 +}; + +struct tc_csum { +tc_gen ; + +__u32 update_flags; +}; + +#endif /* __LINUX_TC_CSUM_H */
include/net/tc_act/tc_csum.h new file +15 -0
@@ -0,0 +1,15 @@ +#ifndef __NET_TC_CSUM_H +#define __NET_TC_CSUM_H + +#include <linux/types.h> +#include <net/act_api.h> + +struct tcf_csum { +struct tcf_common common; + +u32 update_flags; +}; +#define to_tcf_csum (pc) \ + container_of(pc,struct tcf_csum,common) + +#endif /* __NET_TC_CSUM_H */
net/sched/Kconfig +10 -0
@@ -518,6 +518,16 @@ config NET_ACT_SKBEDIT To compile this code as a module, choose M here: the module will be called act_skbedit. +config NET_ACT_CSUM + tristate "Checksum Updating" + depends on NET_CLS_ACT + ---help--- + Say Y here to update some common checksum after some direct + packet alterations. + + To compile this code as a module, choose M here: the + module will be called act_csum. + config NET_CLS_IND bool "Incoming device classification" depends on NET_CLS_U32 || NET_CLS_FW
net/sched/Makefile +1 -0
@@ -15,6 +15,7 @@ obj-$(CONFIG_NET_ACT_NAT) += act_nat.o obj-$(CONFIG_NET_ACT_PEDIT) += act_pedit.o obj-$(CONFIG_NET_ACT_SIMP) += act_simple.o obj-$(CONFIG_NET_ACT_SKBEDIT) += act_skbedit.o +obj-$(CONFIG_NET_ACT_CSUM) += act_csum.o obj-$(CONFIG_NET_SCH_FIFO) += sch_fifo.o obj-$(CONFIG_NET_SCH_CBQ) += sch_cbq.o obj-$(CONFIG_NET_SCH_HTB) += sch_htb.o
net/sched/act_csum.c new file +700 -0
700 lines not on this page · raw message
Quoted 10 lines · Grégoire Baron
net/sched: add
ACT_CSUMaction to update packets checksums
ACT_CSUMcan be called just afterACT_PEDITin order to re-compute some altered checksums in IPv4 and IPv6 packets. The following checksums are supported by this patch:
- IPv4: IPv4 header, ICMP, IGMP, TCP, UDP & UDPLite
- IPv6: ICMPv6, TCP, UDP & UDPLite
It's possible to request in the same action to update different kind of checksums, if the packets flow mix TCP, UDP and UDPLite, ...
Quoted 3 lines
An example of usage is done in the associated iproute2 patch.
Signed-off-by Gregoire Baron <[email protected]>
Excellent work! Ive always wanted to do this albeit slightly differently.
I think it would be nice to factor a lot of the code repeated everywhere into some boilerplate function that gets invoked by all; users. Example, code such as:
+ struct icmphdr *icmph; + int hl; + int ntkoff;
+
+ ntkoff = skb_network_offset(skb);
+
+ hl = ihl + sizeof(*icmph);
+
+ if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
+ (skb_cloned(skb) &&
+ !skb_clone_writable(skb, hl + ntkoff) &&
+ pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
+ goto fail;
+
+ icmph = (void *)(skb_network_header(skb) + ihl);
+
+ icmph->checksum = 0;
cheers,
jamal
Quoted 2 lines
Excellent work! Ive always wanted to do this albeit slightly differently.
Thanks!
Quoted 3 lines
I think it would be nice to factor a lot of the code repeated everywhere into some boilerplate function that gets invoked by all; users.
You're rigth. I will correct that, maybe using a macro which is specialised to get the protocol structure ...
However, this macro could call a 'goto' instruction. Is it really a good idea? Tell me. Are you sure a function is appropriate?
Regards,
Grégoire
Quoted 4 lines · Grégoire Baron
You're rigth. I will correct that, maybe using a macro which is specialised to get the protocol structure ...
However, this macro could call a 'goto' instruction. Is it really a good idea? Tell me. Are you sure a function is appropriate?
Do you even need the goto fail? It seems you could short-circuit and return 0 (at least thats what the goto seems to do)
But i really dont want to complicate this; In the minimal i think you can make code like this
====
struct icmphdr *icmph;
+ int hl;
+ int ntkoff;
+
+ ntkoff = skb_network_offset(skb);
+
+ hl = ihl + sizeof(*icmph);
+
+ if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
+ (skb_cloned(skb) &&
+ !skb_clone_writable(skb, hl + ntkoff) &&
+ pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
+ goto fail;
===
reusable - return 0 if fail else return 1 etc etc
BTW, another comment: icmp and igmp have unused parameter iph passed to them - probably cut and paste.
cheers,
jamal
Thank you for your help!
I've just sent a new version (v2) of this patch.
Regards,
Grégoire
Quoted 2 lines · Grégoire Baron
Thank you for your help!
I've just sent a new version (v2) of this patch.
No need for OOB msg - just Cc patch next time.
cheers,
jamal
Quoted 10 lines · Grégoire Baron
net/sched: add
ACT_CSUMaction to update packets checksums
ACT_CSUMcan be called just afterACT_PEDITin order to re-compute some altered checksums in IPv4 and IPv6 packets. The following checksums are supported by this patch:
- IPv4: IPv4 header, ICMP, IGMP, TCP, UDP & UDPLite
- IPv6: ICMPv6, TCP, UDP & UDPLite
It's possible to request in the same action to update different kind of checksums, if the packets flow mix TCP, UDP and UDPLite, ...
Quoted 3 lines
An example of usage is done in the associated iproute2 patch.
Signed-off-by Gregoire Baron <[email protected]>
Impressive :)
One style note :
Quoted 8 lines
net/sched/act_csum.c new file +8
-0+ +switch (iph->frag_off &htons (IP_OFFSET ) ?0 : iph->protocol) { +case IPPROTO_ICMP : + { +if (update_flags &TCA_CSUM_UPDATE_FLAG_ICMP ) +if (!tcf_csum_ipv4_icmp (skb, iph, + iph->ihl *4 ,ntohs (iph->tot_len))) + goto fail;
net/sched/act_csum.c line 418
Quoted 10 lines
net/sched/act_csum.c new file +10
-0+break ; + } +case IPPROTO_IGMP : + { +if (update_flags &TCA_CSUM_UPDATE_FLAG_IGMP ) +if (!tcf_csum_ipv4_igmp (skb, iph, + iph->ihl *4 ,ntohs (iph->tot_len))) + goto fail; +break ; + }
You add extra block delimiters (thus two lines) per switch cases, while not necessary. Please remove them.
And one note about tcf_csum_dump()
static int tcf_csum_dump(struct sk_buff *skb,
struct tc_action *a, int bind, int ref)
{
unsigned char *b = skb_tail_pointer(skb);
struct tcf_csum *p = a->priv;
struct tc_csum *opt;
struct tcf_t t;
int s;
s = sizeof(*opt);
/* netlink spinlocks held above us - must use ATOMIC */
opt = kzalloc(s, GFP_ATOMIC);
if (unlikely(!opt))
return -ENOBUFS;Please dont use kzalloc() here for such a small variable (24 bytes), use an automatic one (on stack)
struct tc_csum parms = {
.update_flags = p->update_flags,
.index = p->tcf_index,
.action = p->tcf_action,
.refcnt = p->tcf_refcnt - ref,
.bindcnt = p->tcf_bindcnt - bind,
};(Using such a construct make sure holes are zero filled, thus we dont leak kernel memory to user)
NLA_PUT(skb, TCA_CSUM_PARMS, sizeof(parms), &parms);
Hmm, I can see existing code have some leaks, I'll post a separate patch...