1 #include <linux/module.h>
4 #include <linux/netlink.h>
5 #include <linux/sock_diag.h>
6 #include <linux/netlink_diag.h>
8 #include "af_netlink.h"
10 static int sk_diag_dump_groups(struct sock *sk, struct sk_buff *nlskb)
12 struct netlink_sock *nlk = nlk_sk(sk);
14 if (nlk->groups == NULL)
17 return nla_put(nlskb, NETLINK_DIAG_GROUPS, NLGRPSZ(nlk->ngroups),
21 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
22 struct netlink_diag_req *req,
23 u32 portid, u32 seq, u32 flags, int sk_ino)
26 struct netlink_diag_msg *rep;
27 struct netlink_sock *nlk = nlk_sk(sk);
29 nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
34 rep = nlmsg_data(nlh);
35 rep->ndiag_family = AF_NETLINK;
36 rep->ndiag_type = sk->sk_type;
37 rep->ndiag_protocol = sk->sk_protocol;
38 rep->ndiag_state = sk->sk_state;
40 rep->ndiag_ino = sk_ino;
41 rep->ndiag_portid = nlk->portid;
42 rep->ndiag_dst_portid = nlk->dst_portid;
43 rep->ndiag_dst_group = nlk->dst_group;
44 sock_diag_save_cookie(sk, rep->ndiag_cookie);
46 if ((req->ndiag_show & NDIAG_SHOW_GROUPS) &&
47 sk_diag_dump_groups(sk, skb))
50 if ((req->ndiag_show & NDIAG_SHOW_MEMINFO) &&
51 sock_diag_put_meminfo(sk, skb, NETLINK_DIAG_MEMINFO))
54 return nlmsg_end(skb, nlh);
57 nlmsg_cancel(skb, nlh);
61 static int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
62 int protocol, int s_num)
64 struct netlink_table *tbl = &nl_table[protocol];
65 struct nl_portid_hash *hash = &tbl->hash;
66 struct net *net = sock_net(skb->sk);
67 struct netlink_diag_req *req;
69 int ret = 0, num = 0, i;
71 req = nlmsg_data(cb->nlh);
73 for (i = 0; i <= hash->mask; i++) {
74 sk_for_each(sk, &hash->table[i]) {
75 if (!net_eq(sock_net(sk), net))
82 if (sk_diag_fill(sk, skb, req,
83 NETLINK_CB(cb->skb).portid,
86 sock_i_ino(sk)) < 0) {
95 sk_for_each_bound(sk, &tbl->mc_list) {
98 if (!net_eq(sock_net(sk), net))
105 if (sk_diag_fill(sk, skb, req,
106 NETLINK_CB(cb->skb).portid,
109 sock_i_ino(sk)) < 0) {
117 cb->args[1] = protocol;
122 static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
124 struct netlink_diag_req *req;
125 int s_num = cb->args[0];
127 req = nlmsg_data(cb->nlh);
129 read_lock(&nl_table_lock);
131 if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
134 for (i = cb->args[1]; i < MAX_LINKS; i++) {
135 if (__netlink_diag_dump(skb, cb, i, s_num))
140 if (req->sdiag_protocol >= MAX_LINKS) {
141 read_unlock(&nl_table_lock);
145 __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
148 read_unlock(&nl_table_lock);
153 static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
155 int hdrlen = sizeof(struct netlink_diag_req);
156 struct net *net = sock_net(skb->sk);
158 if (nlmsg_len(h) < hdrlen)
161 if (h->nlmsg_flags & NLM_F_DUMP) {
162 struct netlink_dump_control c = {
163 .dump = netlink_diag_dump,
165 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
170 static const struct sock_diag_handler netlink_diag_handler = {
171 .family = AF_NETLINK,
172 .dump = netlink_diag_handler_dump,
175 static int __init netlink_diag_init(void)
177 return sock_diag_register(&netlink_diag_handler);
180 static void __exit netlink_diag_exit(void)
182 sock_diag_unregister(&netlink_diag_handler);
185 module_init(netlink_diag_init);
186 module_exit(netlink_diag_exit);
187 MODULE_LICENSE("GPL");
188 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */);