GNU Linux-libre 4.14.330-gnu1
[releases.git] / net / netlink / diag.c
1 #include <linux/module.h>
2
3 #include <net/sock.h>
4 #include <linux/netlink.h>
5 #include <linux/sock_diag.h>
6 #include <linux/netlink_diag.h>
7 #include <linux/rhashtable.h>
8
9 #include "af_netlink.h"
10
11 static int sk_diag_dump_groups(struct sock *sk, struct sk_buff *nlskb)
12 {
13         struct netlink_sock *nlk = nlk_sk(sk);
14
15         if (nlk->groups == NULL)
16                 return 0;
17
18         return nla_put(nlskb, NETLINK_DIAG_GROUPS, NLGRPSZ(nlk->ngroups),
19                        nlk->groups);
20 }
21
22 static int sk_diag_put_flags(struct sock *sk, struct sk_buff *skb)
23 {
24         struct netlink_sock *nlk = nlk_sk(sk);
25         u32 flags = 0;
26
27         if (nlk->cb_running)
28                 flags |= NDIAG_FLAG_CB_RUNNING;
29         if (nlk->flags & NETLINK_F_RECV_PKTINFO)
30                 flags |= NDIAG_FLAG_PKTINFO;
31         if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
32                 flags |= NDIAG_FLAG_BROADCAST_ERROR;
33         if (nlk->flags & NETLINK_F_RECV_NO_ENOBUFS)
34                 flags |= NDIAG_FLAG_NO_ENOBUFS;
35         if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
36                 flags |= NDIAG_FLAG_LISTEN_ALL_NSID;
37         if (nlk->flags & NETLINK_F_CAP_ACK)
38                 flags |= NDIAG_FLAG_CAP_ACK;
39
40         return nla_put_u32(skb, NETLINK_DIAG_FLAGS, flags);
41 }
42
43 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
44                         struct netlink_diag_req *req,
45                         u32 portid, u32 seq, u32 flags, int sk_ino)
46 {
47         struct nlmsghdr *nlh;
48         struct netlink_diag_msg *rep;
49         struct netlink_sock *nlk = nlk_sk(sk);
50
51         nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
52                         flags);
53         if (!nlh)
54                 return -EMSGSIZE;
55
56         rep = nlmsg_data(nlh);
57         rep->ndiag_family       = AF_NETLINK;
58         rep->ndiag_type         = sk->sk_type;
59         rep->ndiag_protocol     = sk->sk_protocol;
60         rep->ndiag_state        = sk->sk_state;
61
62         rep->ndiag_ino          = sk_ino;
63         rep->ndiag_portid       = nlk->portid;
64         rep->ndiag_dst_portid   = nlk->dst_portid;
65         rep->ndiag_dst_group    = nlk->dst_group;
66         sock_diag_save_cookie(sk, rep->ndiag_cookie);
67
68         if ((req->ndiag_show & NDIAG_SHOW_GROUPS) &&
69             sk_diag_dump_groups(sk, skb))
70                 goto out_nlmsg_trim;
71
72         if ((req->ndiag_show & NDIAG_SHOW_MEMINFO) &&
73             sock_diag_put_meminfo(sk, skb, NETLINK_DIAG_MEMINFO))
74                 goto out_nlmsg_trim;
75
76         if ((req->ndiag_show & NDIAG_SHOW_FLAGS) &&
77             sk_diag_put_flags(sk, skb))
78                 goto out_nlmsg_trim;
79
80         nlmsg_end(skb, nlh);
81         return 0;
82
83 out_nlmsg_trim:
84         nlmsg_cancel(skb, nlh);
85         return -EMSGSIZE;
86 }
87
88 static int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
89                                 int protocol, int s_num)
90 {
91         struct rhashtable_iter *hti = (void *)cb->args[2];
92         struct netlink_table *tbl = &nl_table[protocol];
93         struct net *net = sock_net(skb->sk);
94         struct netlink_diag_req *req;
95         struct netlink_sock *nlsk;
96         unsigned long flags;
97         struct sock *sk;
98         int num = 2;
99         int ret = 0;
100
101         req = nlmsg_data(cb->nlh);
102
103         if (s_num > 1)
104                 goto mc_list;
105
106         num--;
107
108         if (!hti) {
109                 hti = kmalloc(sizeof(*hti), GFP_KERNEL);
110                 if (!hti)
111                         return -ENOMEM;
112
113                 cb->args[2] = (long)hti;
114         }
115
116         if (!s_num)
117                 rhashtable_walk_enter(&tbl->hash, hti);
118
119         ret = rhashtable_walk_start(hti);
120         if (ret == -EAGAIN)
121                 ret = 0;
122         if (ret)
123                 goto stop;
124
125         while ((nlsk = rhashtable_walk_next(hti))) {
126                 if (IS_ERR(nlsk)) {
127                         ret = PTR_ERR(nlsk);
128                         if (ret == -EAGAIN) {
129                                 ret = 0;
130                                 continue;
131                         }
132                         break;
133                 }
134
135                 sk = (struct sock *)nlsk;
136
137                 if (!net_eq(sock_net(sk), net))
138                         continue;
139
140                 if (sk_diag_fill(sk, skb, req,
141                                  NETLINK_CB(cb->skb).portid,
142                                  cb->nlh->nlmsg_seq,
143                                  NLM_F_MULTI,
144                                  sock_i_ino(sk)) < 0) {
145                         ret = 1;
146                         break;
147                 }
148         }
149
150 stop:
151         rhashtable_walk_stop(hti);
152         if (ret)
153                 goto done;
154
155         rhashtable_walk_exit(hti);
156         num++;
157
158 mc_list:
159         read_lock_irqsave(&nl_table_lock, flags);
160         sk_for_each_bound(sk, &tbl->mc_list) {
161                 if (sk_hashed(sk))
162                         continue;
163                 if (!net_eq(sock_net(sk), net))
164                         continue;
165                 if (num < s_num) {
166                         num++;
167                         continue;
168                 }
169
170                 if (sk_diag_fill(sk, skb, req,
171                                  NETLINK_CB(cb->skb).portid,
172                                  cb->nlh->nlmsg_seq,
173                                  NLM_F_MULTI,
174                                  __sock_i_ino(sk)) < 0) {
175                         ret = 1;
176                         break;
177                 }
178                 num++;
179         }
180         read_unlock_irqrestore(&nl_table_lock, flags);
181
182 done:
183         cb->args[0] = num;
184
185         return ret;
186 }
187
188 static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
189 {
190         struct netlink_diag_req *req;
191         int s_num = cb->args[0];
192         int err = 0;
193
194         req = nlmsg_data(cb->nlh);
195
196         if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
197                 int i;
198
199                 for (i = cb->args[1]; i < MAX_LINKS; i++) {
200                         err = __netlink_diag_dump(skb, cb, i, s_num);
201                         if (err)
202                                 break;
203                         s_num = 0;
204                 }
205                 cb->args[1] = i;
206         } else {
207                 if (req->sdiag_protocol >= MAX_LINKS)
208                         return -ENOENT;
209
210                 err = __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
211         }
212
213         return err < 0 ? err : skb->len;
214 }
215
216 static int netlink_diag_dump_done(struct netlink_callback *cb)
217 {
218         struct rhashtable_iter *hti = (void *)cb->args[2];
219
220         if (cb->args[0] == 1)
221                 rhashtable_walk_exit(hti);
222
223         kfree(hti);
224
225         return 0;
226 }
227
228 static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
229 {
230         int hdrlen = sizeof(struct netlink_diag_req);
231         struct net *net = sock_net(skb->sk);
232
233         if (nlmsg_len(h) < hdrlen)
234                 return -EINVAL;
235
236         if (h->nlmsg_flags & NLM_F_DUMP) {
237                 struct netlink_dump_control c = {
238                         .dump = netlink_diag_dump,
239                         .done = netlink_diag_dump_done,
240                 };
241                 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
242         } else
243                 return -EOPNOTSUPP;
244 }
245
246 static const struct sock_diag_handler netlink_diag_handler = {
247         .family = AF_NETLINK,
248         .dump = netlink_diag_handler_dump,
249 };
250
251 static int __init netlink_diag_init(void)
252 {
253         return sock_diag_register(&netlink_diag_handler);
254 }
255
256 static void __exit netlink_diag_exit(void)
257 {
258         sock_diag_unregister(&netlink_diag_handler);
259 }
260
261 module_init(netlink_diag_init);
262 module_exit(netlink_diag_exit);
263 MODULE_LICENSE("GPL");
264 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */);