1 From: Pablo Neira Ayuso <pablo@netfilter.org>
2 Date: Mon, 27 Nov 2017 21:55:14 +0100
3 Subject: [PATCH] netfilter: move checksum indirection to struct nf_ipv6_ops
5 We cannot make a direct call to nf_ip6_checksum() because that would
6 result in autoloading the 'ipv6' module because of symbol dependencies.
7 Therefore, define checksum indirection in nf_ipv6_ops where this really
10 For IPv4, we can indeed make a direct function call, which is faster,
11 given IPv4 is built-in in the networking code by default. Still,
12 CONFIG_INET=n and CONFIG_NETFILTER=y is possible, so define empty inline
13 stub for IPv4 in such case.
15 Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
17 create mode 100644 net/netfilter/utils.c
19 --- a/include/linux/netfilter.h
20 +++ b/include/linux/netfilter.h
21 @@ -311,8 +311,6 @@ struct nf_queue_entry;
24 unsigned short family;
25 - __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
26 - unsigned int dataoff, u_int8_t protocol);
27 __sum16 (*checksum_partial)(struct sk_buff *skb,
30 @@ -333,20 +331,9 @@ static inline const struct nf_afinfo *nf
31 return rcu_dereference(nf_afinfo[family]);
34 -static inline __sum16
35 -nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
36 - u_int8_t protocol, unsigned short family)
38 - const struct nf_afinfo *afinfo;
42 - afinfo = nf_get_afinfo(family);
44 - csum = afinfo->checksum(skb, hook, dataoff, protocol);
48 +__sum16 nf_checksum(struct sk_buff *skb, unsigned int hook,
49 + unsigned int dataoff, u_int8_t protocol,
50 + unsigned short family);
53 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
54 --- a/include/linux/netfilter_ipv4.h
55 +++ b/include/linux/netfilter_ipv4.h
57 #include <uapi/linux/netfilter_ipv4.h>
59 int ip_route_me_harder(struct net *net, struct sk_buff *skb, unsigned addr_type);
62 __sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
63 unsigned int dataoff, u_int8_t protocol);
65 +static inline __sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
66 + unsigned int dataoff, u_int8_t protocol)
70 +#endif /* CONFIG_INET */
72 #endif /*__LINUX_IP_NETFILTER_H*/
73 --- a/include/linux/netfilter_ipv6.h
74 +++ b/include/linux/netfilter_ipv6.h
75 @@ -19,6 +19,8 @@ struct nf_ipv6_ops {
76 void (*route_input)(struct sk_buff *skb);
77 int (*fragment)(struct net *net, struct sock *sk, struct sk_buff *skb,
78 int (*output)(struct net *, struct sock *, struct sk_buff *));
79 + __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
80 + unsigned int dataoff, u_int8_t protocol);
83 #ifdef CONFIG_NETFILTER
84 --- a/net/bridge/netfilter/nf_tables_bridge.c
85 +++ b/net/bridge/netfilter/nf_tables_bridge.c
86 @@ -106,12 +106,6 @@ static int nf_br_reroute(struct net *net
90 -static __sum16 nf_br_checksum(struct sk_buff *skb, unsigned int hook,
91 - unsigned int dataoff, u_int8_t protocol)
96 static __sum16 nf_br_checksum_partial(struct sk_buff *skb, unsigned int hook,
97 unsigned int dataoff, unsigned int len,
99 @@ -127,7 +121,6 @@ static int nf_br_route(struct net *net,
101 static const struct nf_afinfo nf_br_afinfo = {
103 - .checksum = nf_br_checksum,
104 .checksum_partial = nf_br_checksum_partial,
105 .route = nf_br_route,
106 .saveroute = nf_br_saveroute,
107 --- a/net/ipv4/netfilter.c
108 +++ b/net/ipv4/netfilter.c
109 @@ -188,7 +188,6 @@ static int nf_ip_route(struct net *net,
111 static const struct nf_afinfo nf_ip_afinfo = {
113 - .checksum = nf_ip_checksum,
114 .checksum_partial = nf_ip_checksum_partial,
115 .route = nf_ip_route,
116 .saveroute = nf_ip_saveroute,
117 --- a/net/ipv6/netfilter.c
118 +++ b/net/ipv6/netfilter.c
119 @@ -194,12 +194,12 @@ static __sum16 nf_ip6_checksum_partial(s
120 static const struct nf_ipv6_ops ipv6ops = {
121 .chk_addr = ipv6_chk_addr,
122 .route_input = ip6_route_input,
123 - .fragment = ip6_fragment
124 + .fragment = ip6_fragment,
125 + .checksum = nf_ip6_checksum,
128 static const struct nf_afinfo nf_ip6_afinfo = {
130 - .checksum = nf_ip6_checksum,
131 .checksum_partial = nf_ip6_checksum_partial,
132 .route = nf_ip6_route,
133 .saveroute = nf_ip6_saveroute,
134 --- a/net/netfilter/Makefile
135 +++ b/net/netfilter/Makefile
137 # SPDX-License-Identifier: GPL-2.0
138 -netfilter-objs := core.o nf_log.o nf_queue.o nf_sockopt.o
139 +netfilter-objs := core.o nf_log.o nf_queue.o nf_sockopt.o utils.o
141 nf_conntrack-y := nf_conntrack_core.o nf_conntrack_standalone.o nf_conntrack_expect.o nf_conntrack_helper.o nf_conntrack_proto.o nf_conntrack_l3proto_generic.o nf_conntrack_proto_generic.o nf_conntrack_proto_tcp.o nf_conntrack_proto_udp.o nf_conntrack_extend.o nf_conntrack_acct.o nf_conntrack_seqadj.o
142 nf_conntrack-$(CONFIG_NF_CONNTRACK_TIMEOUT) += nf_conntrack_timeout.o
144 +++ b/net/netfilter/utils.c
146 +#include <linux/kernel.h>
147 +#include <linux/netfilter.h>
148 +#include <linux/netfilter_ipv4.h>
149 +#include <linux/netfilter_ipv6.h>
151 +__sum16 nf_checksum(struct sk_buff *skb, unsigned int hook,
152 + unsigned int dataoff, u_int8_t protocol,
153 + unsigned short family)
155 + const struct nf_ipv6_ops *v6ops;
160 + csum = nf_ip_checksum(skb, hook, dataoff, protocol);
163 + v6ops = rcu_dereference(nf_ipv6_ops);
165 + csum = v6ops->checksum(skb, hook, dataoff, protocol);
171 +EXPORT_SYMBOL_GPL(nf_checksum);