Commit 278c7d7e authored by Maxime Chevallier's avatar Maxime Chevallier Committed by Greg Kroah-Hartman

packets: Always register packet sk in the same order

[ Upstream commit a4dc6a49 ]

When using fanouts with AF_PACKET, the demux functions such as
fanout_demux_cpu will return an index in the fanout socket array, which
corresponds to the selected socket.

The ordering of this array depends on the order the sockets were added
to a given fanout group, so for FANOUT_CPU this means sockets are bound
to cpus in the order they are configured, which is OK.

However, when stopping then restarting the interface these sockets are
bound to, the sockets are reassigned to the fanout group in the reverse
order, due to the fact that they were inserted at the head of the
interface's AF_PACKET socket list.

This means that traffic that was directed to the first socket in the
fanout group is now directed to the last one after an interface restart.

In the case of FANOUT_CPU, traffic from CPU0 will be directed to the
socket that used to receive traffic from the last CPU after an interface

This commit introduces a helper to add a socket at the tail of a list,
then uses it to register AF_PACKET sockets.

Note that this changes the order in which sockets are listed in /proc and
with sock_diag.

Fixes: dc99f600 ("packet: Add fanout support")
Signed-off-by: default avatarMaxime Chevallier <>
Acked-by: default avatarWillem de Bruijn <>
Signed-off-by: default avatarDavid S. Miller <>
Signed-off-by: default avatarGreg Kroah-Hartman <>
parent 970d4fb2
...@@ -710,6 +710,12 @@ static inline void sk_add_node_rcu(struct sock *sk, struct hlist_head *list) ...@@ -710,6 +710,12 @@ static inline void sk_add_node_rcu(struct sock *sk, struct hlist_head *list)
hlist_add_head_rcu(&sk->sk_node, list); hlist_add_head_rcu(&sk->sk_node, list);
} }
static inline void sk_add_node_tail_rcu(struct sock *sk, struct hlist_head *list)
hlist_add_tail_rcu(&sk->sk_node, list);
static inline void __sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list) static inline void __sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
{ {
hlist_nulls_add_head_rcu(&sk->sk_nulls_node, list); hlist_nulls_add_head_rcu(&sk->sk_nulls_node, list);
...@@ -3245,7 +3245,7 @@ static int packet_create(struct net *net, struct socket *sock, int protocol, ...@@ -3245,7 +3245,7 @@ static int packet_create(struct net *net, struct socket *sock, int protocol,
} }
mutex_lock(&net->packet.sklist_lock); mutex_lock(&net->packet.sklist_lock);
sk_add_node_rcu(sk, &net->packet.sklist); sk_add_node_tail_rcu(sk, &net->packet.sklist);
mutex_unlock(&net->packet.sklist_lock); mutex_unlock(&net->packet.sklist_lock);
preempt_disable(); preempt_disable();
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