[PATCH can-next v2 2/3] af_can: store socket pointers in struct netns_can
Filippo Storniolo <[email protected]>
| Newsgroups | org.kernel.vger.linux-can |
|---|---|
| Message-ID | <[email protected]> |
AF_CAN sockets need to be stored in the netns_can structure in order to be retrieved by the CAN diagnostic module when a netlink request message is issued by the userspace. On socket creation (`can_create()`), add the pointer to the new socket to `netns_can::sk_list`. During socket release (`isotp_release()`, `raw_release()`, `j1939_release()`, `bcm_release()`), remove the corresponding pointer from this list. Since this is a prerequisite of the CAN diagnostic module, deletes and insert operations are conditioned by IS_ENABLED(CONFIG_CAN_DIAG). Signed-off-by: Filippo Storniolo <[email protected]> --- include/linux/can/core.h | 11 +++++++++++ include/net/netns/can.h | 6 ++++++ net/can/af_can.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ net/can/bcm.c | 2 ++ net/can/isotp.c | 2 ++ net/can/j1939/socket.c | 2 ++ net/can/raw.c | 2 ++ 7 files changed, 75 insertions(+) diff --git a/include/linux/can/core.h b/include/linux/can/core.h index 3287232e3cad..5132f5c89b7c 100644 --- a/include/linux/can/core.h +++ b/include/linux/can/core.h @@ -61,4 +61,15 @@ extern int can_send(struct sk_buff *skb, int loop); void can_set_skb_uid(struct sk_buff *skb); void can_sock_destruct(struct sock *sk); +/* function prototypes for the CAN diag module */ +#if IS_ENABLED(CONFIG_CAN_DIAG) +void lock_can_diag_mutex(struct net *net); +void unlock_can_diag_mutex(struct net *net); +void can_add_sock_sklist(struct sock *sk); +void can_remove_sock_sklist(struct sock *sk); +#else +#define can_add_sock_sklist(sk) +#define can_remove_sock_sklist(sk) +#endif + #endif /* !_CAN_CORE_H */ diff --git a/include/net/netns/can.h b/include/net/netns/can.h index 48b79f7e6236..bcafff5e6669 100644 --- a/include/net/netns/can.h +++ b/include/net/netns/can.h @@ -36,6 +36,12 @@ struct netns_can { /* CAN GW per-net gateway jobs */ struct hlist_head cgw_list; + +#if IS_ENABLED(CONFIG_CAN_DIAG) + /* CAN diag support */ + struct mutex sklist_lock; + struct hlist_head sklist; +#endif }; #endif /* __NETNS_CAN_H__ */ diff --git a/net/can/af_can.c b/net/can/af_can.c index 65af25946985..1ea36411f219 100644 --- a/net/can/af_can.c +++ b/net/can/af_can.c @@ -111,6 +111,44 @@ static inline void can_put_proto(const struct can_proto *cp) module_put(cp->prot->owner); } +#if IS_ENABLED(CONFIG_CAN_DIAG) +void lock_can_diag_mutex(struct net *net) +{ + mutex_lock(&net->can.sklist_lock); +} +EXPORT_SYMBOL(lock_can_diag_mutex); + +void unlock_can_diag_mutex(struct net *net) +{ + mutex_unlock(&net->can.sklist_lock); +} +EXPORT_SYMBOL(unlock_can_diag_mutex); + +void can_add_sock_sklist(struct sock *sk) +{ + struct net *net; + + net = sock_net(sk); + + lock_can_diag_mutex(net); + sk_add_node(sk, &net->can.sklist); + unlock_can_diag_mutex(net); +} +EXPORT_SYMBOL(can_add_sock_sklist); + +void can_remove_sock_sklist(struct sock *sk) +{ + struct net *net; + + net = sock_net(sk); + + lock_can_diag_mutex(net); + sk_del_node_init(sk); + unlock_can_diag_mutex(net); +} +EXPORT_SYMBOL(can_remove_sock_sklist); +#endif + static int can_create(struct net *net, struct socket *sock, int protocol, int kern) { @@ -174,6 +212,8 @@ static int can_create(struct net *net, struct socket *sock, int protocol, sock_put(sk); sock->sk = NULL; } else { + can_add_sock_sklist(sk); + sock_prot_inuse_add(net, sk->sk_prot, 1); } @@ -799,6 +839,12 @@ EXPORT_SYMBOL(can_proto_unregister); static int can_pernet_init(struct net *net) { spin_lock_init(&net->can.rcvlists_lock); + +#if IS_ENABLED(CONFIG_CAN_DIAG) + mutex_init(&net->can.sklist_lock); + INIT_HLIST_HEAD(&net->can.sklist); +#endif + net->can.rx_alldev_list = kzalloc_obj(*net->can.rx_alldev_list); if (!net->can.rx_alldev_list) goto out; @@ -842,6 +888,10 @@ static void can_pernet_exit(struct net *net) kfree(net->can.rx_alldev_list); kfree(net->can.pkg_stats); kfree(net->can.rcv_lists_stats); + +#if IS_ENABLED(CONFIG_CAN_DIAG) + WARN_ON_ONCE(!hlist_empty(&net->can.sklist)); +#endif } /* af_can module init/exit functions */ diff --git a/net/can/bcm.c b/net/can/bcm.c index 3d637a1e0ac1..a90b7aea2869 100644 --- a/net/can/bcm.c +++ b/net/can/bcm.c @@ -1921,6 +1921,8 @@ static int bcm_release(struct socket *sock) if (!sk) return 0; + can_remove_sock_sklist(sk); + net = sock_net(sk); bo = bcm_sk(sk); diff --git a/net/can/isotp.c b/net/can/isotp.c index 155530aedce2..25098c5546d2 100644 --- a/net/can/isotp.c +++ b/net/can/isotp.c @@ -1455,6 +1455,8 @@ static int isotp_release(struct socket *sock) so = isotp_sk(sk); net = sock_net(sk); + can_remove_sock_sklist(sk); + /* best-effort: wait for a running pdu to finish, but don't block on * it forever - give up after the first signal */ diff --git a/net/can/j1939/socket.c b/net/can/j1939/socket.c index ccd43ff5519c..cbfd0b888768 100644 --- a/net/can/j1939/socket.c +++ b/net/can/j1939/socket.c @@ -641,6 +641,8 @@ static int j1939_sk_release(struct socket *sock) if (!sk) return 0; + can_remove_sock_sklist(sk); + lock_sock(sk); jsk = j1939_sk(sk); diff --git a/net/can/raw.c b/net/can/raw.c index 82d9c0499c95..7784b8fd8d19 100644 --- a/net/can/raw.c +++ b/net/can/raw.c @@ -418,6 +418,8 @@ static int raw_release(struct socket *sock) ro = raw_sk(sk); net = sock_net(sk); + can_remove_sock_sklist(sk); + spin_lock(&raw_notifier_lock); while (raw_busy_notifier == ro) { spin_unlock(&raw_notifier_lock); -- 2.55.0