Commit f65c1b53 authored by Pavel Emelyanov's avatar Pavel Emelyanov Committed by David S. Miller

sock_diag: Generalize requests cookies managements

The sk address is used as a cookie between dump/get_exact calls.
It will be required for unix socket sdumping, so move it from
inet_diag to sock_diag.
Signed-off-by: default avatarPavel Emelyanov <xemul@parallels.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent aec8dc62
...@@ -168,7 +168,6 @@ int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, ...@@ -168,7 +168,6 @@ int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
struct inet_diag_req *req); struct inet_diag_req *req);
int inet_diag_bc_sk(const struct nlattr *_bc, struct sock *sk); int inet_diag_bc_sk(const struct nlattr *_bc, struct sock *sk);
int inet_diag_check_cookie(struct sock *sk, struct inet_diag_req *req);
extern int inet_diag_register(const struct inet_diag_handler *handler); extern int inet_diag_register(const struct inet_diag_handler *handler);
extern void inet_diag_unregister(const struct inet_diag_handler *handler); extern void inet_diag_unregister(const struct inet_diag_handler *handler);
......
...@@ -22,5 +22,8 @@ void sock_diag_unregister(struct sock_diag_handler *h); ...@@ -22,5 +22,8 @@ void sock_diag_unregister(struct sock_diag_handler *h);
void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)); void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)); void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
int sock_diag_check_cookie(void *sk, __u32 *cookie);
void sock_diag_save_cookie(void *sk, __u32 *cookie);
extern struct sock *sock_diag_nlsk; extern struct sock *sock_diag_nlsk;
#endif #endif
...@@ -12,6 +12,25 @@ static struct sock_diag_handler *sock_diag_handlers[AF_MAX]; ...@@ -12,6 +12,25 @@ static struct sock_diag_handler *sock_diag_handlers[AF_MAX];
static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh); static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
static DEFINE_MUTEX(sock_diag_table_mutex); static DEFINE_MUTEX(sock_diag_table_mutex);
int sock_diag_check_cookie(void *sk, __u32 *cookie)
{
if ((cookie[0] != INET_DIAG_NOCOOKIE ||
cookie[1] != INET_DIAG_NOCOOKIE) &&
((u32)(unsigned long)sk != cookie[0] ||
(u32)((((unsigned long)sk) >> 31) >> 1) != cookie[1]))
return -ESTALE;
else
return 0;
}
EXPORT_SYMBOL_GPL(sock_diag_check_cookie);
void sock_diag_save_cookie(void *sk, __u32 *cookie)
{
cookie[0] = (u32)(unsigned long)sk;
cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
}
EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
{ {
mutex_lock(&sock_diag_table_mutex); mutex_lock(&sock_diag_table_mutex);
......
...@@ -102,8 +102,7 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, ...@@ -102,8 +102,7 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
r->idiag_retrans = 0; r->idiag_retrans = 0;
r->id.idiag_if = sk->sk_bound_dev_if; r->id.idiag_if = sk->sk_bound_dev_if;
r->id.idiag_cookie[0] = (u32)(unsigned long)sk; sock_diag_save_cookie(sk, r->id.idiag_cookie);
r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
r->id.idiag_sport = inet->inet_sport; r->id.idiag_sport = inet->inet_sport;
r->id.idiag_dport = inet->inet_dport; r->id.idiag_dport = inet->inet_dport;
...@@ -221,8 +220,7 @@ static int inet_twsk_diag_fill(struct inet_timewait_sock *tw, ...@@ -221,8 +220,7 @@ static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
r->idiag_family = tw->tw_family; r->idiag_family = tw->tw_family;
r->idiag_retrans = 0; r->idiag_retrans = 0;
r->id.idiag_if = tw->tw_bound_dev_if; r->id.idiag_if = tw->tw_bound_dev_if;
r->id.idiag_cookie[0] = (u32)(unsigned long)tw; sock_diag_save_cookie(tw, r->id.idiag_cookie);
r->id.idiag_cookie[1] = (u32)(((unsigned long)tw >> 31) >> 1);
r->id.idiag_sport = tw->tw_sport; r->id.idiag_sport = tw->tw_sport;
r->id.idiag_dport = tw->tw_dport; r->id.idiag_dport = tw->tw_dport;
r->id.idiag_src[0] = tw->tw_rcv_saddr; r->id.idiag_src[0] = tw->tw_rcv_saddr;
...@@ -261,18 +259,6 @@ static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, ...@@ -261,18 +259,6 @@ static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
return inet_csk_diag_fill(sk, skb, r, pid, seq, nlmsg_flags, unlh); return inet_csk_diag_fill(sk, skb, r, pid, seq, nlmsg_flags, unlh);
} }
int inet_diag_check_cookie(struct sock *sk, struct inet_diag_req *req)
{
if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE ||
req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) &&
((u32)(unsigned long)sk != req->id.idiag_cookie[0] ||
(u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1]))
return -ESTALE;
else
return 0;
}
EXPORT_SYMBOL_GPL(inet_diag_check_cookie);
int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb, int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb,
const struct nlmsghdr *nlh, struct inet_diag_req *req) const struct nlmsghdr *nlh, struct inet_diag_req *req)
{ {
...@@ -304,7 +290,7 @@ int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_s ...@@ -304,7 +290,7 @@ int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_s
if (sk == NULL) if (sk == NULL)
goto out_nosk; goto out_nosk;
err = inet_diag_check_cookie(sk, req); err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
if (err) if (err)
goto out; goto out;
...@@ -617,8 +603,7 @@ static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk, ...@@ -617,8 +603,7 @@ static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
r->idiag_retrans = req->retrans; r->idiag_retrans = req->retrans;
r->id.idiag_if = sk->sk_bound_dev_if; r->id.idiag_if = sk->sk_bound_dev_if;
r->id.idiag_cookie[0] = (u32)(unsigned long)req; sock_diag_save_cookie(req, r->id.idiag_cookie);
r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
tmo = req->expires - jiffies; tmo = req->expires - jiffies;
if (tmo < 0) if (tmo < 0)
......
...@@ -57,7 +57,7 @@ static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb, ...@@ -57,7 +57,7 @@ static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
if (sk == NULL) if (sk == NULL)
goto out_nosk; goto out_nosk;
err = inet_diag_check_cookie(sk, req); err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
if (err) if (err)
goto out; goto out;
......
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