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_put_ring(struct netlink_ring *ring, int nl_type,
11 struct sk_buff *nlskb)
13 struct netlink_diag_ring ndr;
15 ndr.ndr_block_size = ring->pg_vec_pages << PAGE_SHIFT;
16 ndr.ndr_block_nr = ring->pg_vec_len;
17 ndr.ndr_frame_size = ring->frame_size;
18 ndr.ndr_frame_nr = ring->frame_max + 1;
20 return nla_put(nlskb, nl_type, sizeof(ndr), &ndr);
23 static int sk_diag_put_rings_cfg(struct sock *sk, struct sk_buff *nlskb)
25 struct netlink_sock *nlk = nlk_sk(sk);
28 mutex_lock(&nlk->pg_vec_lock);
29 ret = sk_diag_put_ring(&nlk->rx_ring, NETLINK_DIAG_RX_RING, nlskb);
31 ret = sk_diag_put_ring(&nlk->tx_ring, NETLINK_DIAG_TX_RING,
33 mutex_unlock(&nlk->pg_vec_lock);
38 static int sk_diag_dump_groups(struct sock *sk, struct sk_buff *nlskb)
40 struct netlink_sock *nlk = nlk_sk(sk);
42 if (nlk->groups == NULL)
45 return nla_put(nlskb, NETLINK_DIAG_GROUPS, NLGRPSZ(nlk->ngroups),
49 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
50 struct netlink_diag_req *req,
51 u32 portid, u32 seq, u32 flags, int sk_ino)
54 struct netlink_diag_msg *rep;
55 struct netlink_sock *nlk = nlk_sk(sk);
57 nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
62 rep = nlmsg_data(nlh);
63 rep->ndiag_family = AF_NETLINK;
64 rep->ndiag_type = sk->sk_type;
65 rep->ndiag_protocol = sk->sk_protocol;
66 rep->ndiag_state = sk->sk_state;
68 rep->ndiag_ino = sk_ino;
69 rep->ndiag_portid = nlk->portid;
70 rep->ndiag_dst_portid = nlk->dst_portid;
71 rep->ndiag_dst_group = nlk->dst_group;
72 sock_diag_save_cookie(sk, rep->ndiag_cookie);
74 if ((req->ndiag_show & NDIAG_SHOW_GROUPS) &&
75 sk_diag_dump_groups(sk, skb))
78 if ((req->ndiag_show & NDIAG_SHOW_MEMINFO) &&
79 sock_diag_put_meminfo(sk, skb, NETLINK_DIAG_MEMINFO))
82 if ((req->ndiag_show & NDIAG_SHOW_RING_CFG) &&
83 sk_diag_put_rings_cfg(sk, skb))
86 return nlmsg_end(skb, nlh);
89 nlmsg_cancel(skb, nlh);
93 static int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
94 int protocol, int s_num)
96 struct netlink_table *tbl = &nl_table[protocol];
97 struct nl_portid_hash *hash = &tbl->hash;
98 struct net *net = sock_net(skb->sk);
99 struct netlink_diag_req *req;
101 int ret = 0, num = 0, i;
103 req = nlmsg_data(cb->nlh);
105 for (i = 0; i <= hash->mask; i++) {
106 sk_for_each(sk, &hash->table[i]) {
107 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) {
127 sk_for_each_bound(sk, &tbl->mc_list) {
130 if (!net_eq(sock_net(sk), net))
137 if (sk_diag_fill(sk, skb, req,
138 NETLINK_CB(cb->skb).portid,
141 sock_i_ino(sk)) < 0) {
149 cb->args[1] = protocol;
154 static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
156 struct netlink_diag_req *req;
157 int s_num = cb->args[0];
159 req = nlmsg_data(cb->nlh);
161 read_lock(&nl_table_lock);
163 if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
166 for (i = cb->args[1]; i < MAX_LINKS; i++) {
167 if (__netlink_diag_dump(skb, cb, i, s_num))
172 if (req->sdiag_protocol >= MAX_LINKS) {
173 read_unlock(&nl_table_lock);
177 __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
180 read_unlock(&nl_table_lock);
185 static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
187 int hdrlen = sizeof(struct netlink_diag_req);
188 struct net *net = sock_net(skb->sk);
190 if (nlmsg_len(h) < hdrlen)
193 if (h->nlmsg_flags & NLM_F_DUMP) {
194 struct netlink_dump_control c = {
195 .dump = netlink_diag_dump,
197 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
202 static const struct sock_diag_handler netlink_diag_handler = {
203 .family = AF_NETLINK,
204 .dump = netlink_diag_handler_dump,
207 static int __init netlink_diag_init(void)
209 return sock_diag_register(&netlink_diag_handler);
212 static void __exit netlink_diag_exit(void)
214 sock_diag_unregister(&netlink_diag_handler);
217 module_init(netlink_diag_init);
218 module_exit(netlink_diag_exit);
219 MODULE_LICENSE("GPL");
220 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */);