Commit a441dfdb authored by Patrick McHardy's avatar Patrick McHardy Committed by David S. Miller

[NETFILTER]: ip_nat: properly use RCU API for ip_nat_protos array

Replace preempt_{enable,disable} based RCU by proper use of the
RCU API and add missing rcu_read_lock/rcu_read_unlock calls in
paths used outside of packet processing context (nfnetlink_conntrack).
Signed-off-by: default avatarPatrick McHardy <kaber@trash.net>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent e92ad99c
...@@ -50,7 +50,7 @@ static struct ip_nat_protocol *ip_nat_protos[MAX_IP_NAT_PROTO]; ...@@ -50,7 +50,7 @@ static struct ip_nat_protocol *ip_nat_protos[MAX_IP_NAT_PROTO];
static inline struct ip_nat_protocol * static inline struct ip_nat_protocol *
__ip_nat_proto_find(u_int8_t protonum) __ip_nat_proto_find(u_int8_t protonum)
{ {
return ip_nat_protos[protonum]; return rcu_dereference(ip_nat_protos[protonum]);
} }
struct ip_nat_protocol * struct ip_nat_protocol *
...@@ -58,13 +58,11 @@ ip_nat_proto_find_get(u_int8_t protonum) ...@@ -58,13 +58,11 @@ ip_nat_proto_find_get(u_int8_t protonum)
{ {
struct ip_nat_protocol *p; struct ip_nat_protocol *p;
/* we need to disable preemption to make sure 'p' doesn't get rcu_read_lock();
* removed until we've grabbed the reference */
preempt_disable();
p = __ip_nat_proto_find(protonum); p = __ip_nat_proto_find(protonum);
if (!try_module_get(p->me)) if (!try_module_get(p->me))
p = &ip_nat_unknown_protocol; p = &ip_nat_unknown_protocol;
preempt_enable(); rcu_read_unlock();
return p; return p;
} }
...@@ -120,8 +118,8 @@ static int ...@@ -120,8 +118,8 @@ static int
in_range(const struct ip_conntrack_tuple *tuple, in_range(const struct ip_conntrack_tuple *tuple,
const struct ip_nat_range *range) const struct ip_nat_range *range)
{ {
struct ip_nat_protocol *proto = struct ip_nat_protocol *proto;
__ip_nat_proto_find(tuple->dst.protonum); int ret = 0;
/* If we are supposed to map IPs, then we must be in the /* If we are supposed to map IPs, then we must be in the
range specified, otherwise let this drag us onto a new src IP. */ range specified, otherwise let this drag us onto a new src IP. */
...@@ -131,12 +129,15 @@ in_range(const struct ip_conntrack_tuple *tuple, ...@@ -131,12 +129,15 @@ in_range(const struct ip_conntrack_tuple *tuple,
return 0; return 0;
} }
rcu_read_lock();
proto = __ip_nat_proto_find(tuple->dst.protonum);
if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED) if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)
|| proto->in_range(tuple, IP_NAT_MANIP_SRC, || proto->in_range(tuple, IP_NAT_MANIP_SRC,
&range->min, &range->max)) &range->min, &range->max))
return 1; ret = 1;
rcu_read_unlock();
return 0; return ret;
} }
static inline int static inline int
...@@ -260,27 +261,25 @@ get_unique_tuple(struct ip_conntrack_tuple *tuple, ...@@ -260,27 +261,25 @@ get_unique_tuple(struct ip_conntrack_tuple *tuple,
/* 3) The per-protocol part of the manip is made to map into /* 3) The per-protocol part of the manip is made to map into
the range to make a unique tuple. */ the range to make a unique tuple. */
proto = ip_nat_proto_find_get(orig_tuple->dst.protonum); rcu_read_lock();
proto = __ip_nat_proto_find(orig_tuple->dst.protonum);
/* Change protocol info to have some randomization */ /* Change protocol info to have some randomization */
if (range->flags & IP_NAT_RANGE_PROTO_RANDOM) { if (range->flags & IP_NAT_RANGE_PROTO_RANDOM) {
proto->unique_tuple(tuple, range, maniptype, conntrack); proto->unique_tuple(tuple, range, maniptype, conntrack);
ip_nat_proto_put(proto); goto out;
return;
} }
/* Only bother mapping if it's not already in range and unique */ /* Only bother mapping if it's not already in range and unique */
if ((!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED) if ((!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)
|| proto->in_range(tuple, maniptype, &range->min, &range->max)) || proto->in_range(tuple, maniptype, &range->min, &range->max))
&& !ip_nat_used_tuple(tuple, conntrack)) { && !ip_nat_used_tuple(tuple, conntrack))
ip_nat_proto_put(proto); goto out;
return;
}
/* Last change: get protocol to try to obtain unique tuple. */ /* Last change: get protocol to try to obtain unique tuple. */
proto->unique_tuple(tuple, range, maniptype, conntrack); proto->unique_tuple(tuple, range, maniptype, conntrack);
out:
ip_nat_proto_put(proto); rcu_read_unlock();
} }
unsigned int unsigned int
...@@ -360,12 +359,11 @@ manip_pkt(u_int16_t proto, ...@@ -360,12 +359,11 @@ manip_pkt(u_int16_t proto,
iph = (void *)(*pskb)->data + iphdroff; iph = (void *)(*pskb)->data + iphdroff;
/* Manipulate protcol part. */ /* Manipulate protcol part. */
p = ip_nat_proto_find_get(proto);
if (!p->manip_pkt(pskb, iphdroff, target, maniptype)) { /* rcu_read_lock()ed by nf_hook_slow */
ip_nat_proto_put(p); p = __ip_nat_proto_find(proto);
if (!p->manip_pkt(pskb, iphdroff, target, maniptype))
return 0; return 0;
}
ip_nat_proto_put(p);
iph = (void *)(*pskb)->data + iphdroff; iph = (void *)(*pskb)->data + iphdroff;
...@@ -515,7 +513,7 @@ int ip_nat_protocol_register(struct ip_nat_protocol *proto) ...@@ -515,7 +513,7 @@ int ip_nat_protocol_register(struct ip_nat_protocol *proto)
ret = -EBUSY; ret = -EBUSY;
goto out; goto out;
} }
ip_nat_protos[proto->protonum] = proto; rcu_assign_pointer(ip_nat_protos[proto->protonum], proto);
out: out:
write_unlock_bh(&ip_nat_lock); write_unlock_bh(&ip_nat_lock);
return ret; return ret;
...@@ -526,11 +524,10 @@ EXPORT_SYMBOL(ip_nat_protocol_register); ...@@ -526,11 +524,10 @@ EXPORT_SYMBOL(ip_nat_protocol_register);
void ip_nat_protocol_unregister(struct ip_nat_protocol *proto) void ip_nat_protocol_unregister(struct ip_nat_protocol *proto)
{ {
write_lock_bh(&ip_nat_lock); write_lock_bh(&ip_nat_lock);
ip_nat_protos[proto->protonum] = &ip_nat_unknown_protocol; rcu_assign_pointer(ip_nat_protos[proto->protonum],
&ip_nat_unknown_protocol);
write_unlock_bh(&ip_nat_lock); write_unlock_bh(&ip_nat_lock);
synchronize_rcu();
/* Someone could be still looking at the proto in a bh. */
synchronize_net();
} }
EXPORT_SYMBOL(ip_nat_protocol_unregister); EXPORT_SYMBOL(ip_nat_protocol_unregister);
...@@ -594,10 +591,10 @@ static int __init ip_nat_init(void) ...@@ -594,10 +591,10 @@ static int __init ip_nat_init(void)
/* Sew in builtin protocols. */ /* Sew in builtin protocols. */
write_lock_bh(&ip_nat_lock); write_lock_bh(&ip_nat_lock);
for (i = 0; i < MAX_IP_NAT_PROTO; i++) for (i = 0; i < MAX_IP_NAT_PROTO; i++)
ip_nat_protos[i] = &ip_nat_unknown_protocol; rcu_assign_pointer(ip_nat_protos[i], &ip_nat_unknown_protocol);
ip_nat_protos[IPPROTO_TCP] = &ip_nat_protocol_tcp; rcu_assign_pointer(ip_nat_protos[IPPROTO_TCP], &ip_nat_protocol_tcp);
ip_nat_protos[IPPROTO_UDP] = &ip_nat_protocol_udp; rcu_assign_pointer(ip_nat_protos[IPPROTO_UDP], &ip_nat_protocol_udp);
ip_nat_protos[IPPROTO_ICMP] = &ip_nat_protocol_icmp; rcu_assign_pointer(ip_nat_protos[IPPROTO_ICMP], &ip_nat_protocol_icmp);
write_unlock_bh(&ip_nat_lock); write_unlock_bh(&ip_nat_lock);
for (i = 0; i < ip_nat_htable_size; i++) { for (i = 0; i < ip_nat_htable_size; i++) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment