1 #include <linux/module.h>
4 #include <linux/netlink.h>
5 #include <linux/sock_diag.h>
6 #include <linux/netlink_diag.h>
7 #include <linux/rhashtable.h>
9 #include "af_netlink.h"
11 static int sk_diag_dump_groups(struct sock
*sk
, struct sk_buff
*nlskb
)
13 struct netlink_sock
*nlk
= nlk_sk(sk
);
15 if (nlk
->groups
== NULL
)
18 return nla_put(nlskb
, NETLINK_DIAG_GROUPS
, NLGRPSZ(nlk
->ngroups
),
22 static int sk_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
23 struct netlink_diag_req
*req
,
24 u32 portid
, u32 seq
, u32 flags
, int sk_ino
)
27 struct netlink_diag_msg
*rep
;
28 struct netlink_sock
*nlk
= nlk_sk(sk
);
30 nlh
= nlmsg_put(skb
, portid
, seq
, SOCK_DIAG_BY_FAMILY
, sizeof(*rep
),
35 rep
= nlmsg_data(nlh
);
36 rep
->ndiag_family
= AF_NETLINK
;
37 rep
->ndiag_type
= sk
->sk_type
;
38 rep
->ndiag_protocol
= sk
->sk_protocol
;
39 rep
->ndiag_state
= sk
->sk_state
;
41 rep
->ndiag_ino
= sk_ino
;
42 rep
->ndiag_portid
= nlk
->portid
;
43 rep
->ndiag_dst_portid
= nlk
->dst_portid
;
44 rep
->ndiag_dst_group
= nlk
->dst_group
;
45 sock_diag_save_cookie(sk
, rep
->ndiag_cookie
);
47 if ((req
->ndiag_show
& NDIAG_SHOW_GROUPS
) &&
48 sk_diag_dump_groups(sk
, skb
))
51 if ((req
->ndiag_show
& NDIAG_SHOW_MEMINFO
) &&
52 sock_diag_put_meminfo(sk
, skb
, NETLINK_DIAG_MEMINFO
))
59 nlmsg_cancel(skb
, nlh
);
63 static int __netlink_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
64 int protocol
, int s_num
)
66 struct rhashtable_iter
*hti
= (void *)cb
->args
[2];
67 struct netlink_table
*tbl
= &nl_table
[protocol
];
68 struct net
*net
= sock_net(skb
->sk
);
69 struct netlink_diag_req
*req
;
70 struct netlink_sock
*nlsk
;
75 req
= nlmsg_data(cb
->nlh
);
83 hti
= kmalloc(sizeof(*hti
), GFP_KERNEL
);
87 cb
->args
[2] = (long)hti
;
91 rhashtable_walk_enter(&tbl
->hash
, hti
);
93 ret
= rhashtable_walk_start(hti
);
99 while ((nlsk
= rhashtable_walk_next(hti
))) {
102 if (ret
== -EAGAIN
) {
109 sk
= (struct sock
*)nlsk
;
111 if (!net_eq(sock_net(sk
), net
))
114 if (sk_diag_fill(sk
, skb
, req
,
115 NETLINK_CB(cb
->skb
).portid
,
118 sock_i_ino(sk
)) < 0) {
125 rhashtable_walk_stop(hti
);
129 rhashtable_walk_exit(hti
);
133 read_lock(&nl_table_lock
);
134 sk_for_each_bound(sk
, &tbl
->mc_list
) {
137 if (!net_eq(sock_net(sk
), net
))
144 if (sk_diag_fill(sk
, skb
, req
,
145 NETLINK_CB(cb
->skb
).portid
,
148 sock_i_ino(sk
)) < 0) {
154 read_unlock(&nl_table_lock
);
162 static int netlink_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
164 struct netlink_diag_req
*req
;
165 int s_num
= cb
->args
[0];
168 req
= nlmsg_data(cb
->nlh
);
170 if (req
->sdiag_protocol
== NDIAG_PROTO_ALL
) {
173 for (i
= cb
->args
[1]; i
< MAX_LINKS
; i
++) {
174 err
= __netlink_diag_dump(skb
, cb
, i
, s_num
);
181 if (req
->sdiag_protocol
>= MAX_LINKS
)
184 err
= __netlink_diag_dump(skb
, cb
, req
->sdiag_protocol
, s_num
);
187 return err
< 0 ? err
: skb
->len
;
190 static int netlink_diag_dump_done(struct netlink_callback
*cb
)
192 struct rhashtable_iter
*hti
= (void *)cb
->args
[2];
194 if (cb
->args
[0] == 1)
195 rhashtable_walk_exit(hti
);
202 static int netlink_diag_handler_dump(struct sk_buff
*skb
, struct nlmsghdr
*h
)
204 int hdrlen
= sizeof(struct netlink_diag_req
);
205 struct net
*net
= sock_net(skb
->sk
);
207 if (nlmsg_len(h
) < hdrlen
)
210 if (h
->nlmsg_flags
& NLM_F_DUMP
) {
211 struct netlink_dump_control c
= {
212 .dump
= netlink_diag_dump
,
213 .done
= netlink_diag_dump_done
,
215 return netlink_dump_start(net
->diag_nlsk
, skb
, h
, &c
);
220 static const struct sock_diag_handler netlink_diag_handler
= {
221 .family
= AF_NETLINK
,
222 .dump
= netlink_diag_handler_dump
,
225 static int __init
netlink_diag_init(void)
227 return sock_diag_register(&netlink_diag_handler
);
230 static void __exit
netlink_diag_exit(void)
232 sock_diag_unregister(&netlink_diag_handler
);
235 module_init(netlink_diag_init
);
236 module_exit(netlink_diag_exit
);
237 MODULE_LICENSE("GPL");
238 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 16 /* AF_NETLINK */);