Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 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> |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 7 | #include <linux/rhashtable.h> |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 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 | |
Andrey Vagin | 457c79e | 2017-04-03 18:13:32 -0700 | [diff] [blame] | 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 | |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 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 | |
Andrey Vagin | 457c79e | 2017-04-03 18:13:32 -0700 | [diff] [blame] | 76 | if ((req->ndiag_show & NDIAG_SHOW_FLAGS) && |
| 77 | sk_diag_put_flags(sk, skb)) |
| 78 | goto out_nlmsg_trim; |
| 79 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 80 | nlmsg_end(skb, nlh); |
| 81 | return 0; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 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 | { |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 91 | struct rhashtable_iter *hti = (void *)cb->args[2]; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 92 | struct netlink_table *tbl = &nl_table[protocol]; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 93 | struct net *net = sock_net(skb->sk); |
| 94 | struct netlink_diag_req *req; |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 95 | struct netlink_sock *nlsk; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 96 | struct sock *sk; |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 97 | int num = 2; |
| 98 | int ret = 0; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 99 | |
| 100 | req = nlmsg_data(cb->nlh); |
| 101 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 102 | if (s_num > 1) |
| 103 | goto mc_list; |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 104 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 105 | num--; |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 106 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 107 | if (!hti) { |
| 108 | hti = kmalloc(sizeof(*hti), GFP_KERNEL); |
| 109 | if (!hti) |
| 110 | return -ENOMEM; |
| 111 | |
| 112 | cb->args[2] = (long)hti; |
| 113 | } |
| 114 | |
| 115 | if (!s_num) |
| 116 | rhashtable_walk_enter(&tbl->hash, hti); |
| 117 | |
Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame^] | 118 | rhashtable_walk_start(hti); |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 119 | |
| 120 | while ((nlsk = rhashtable_walk_next(hti))) { |
| 121 | if (IS_ERR(nlsk)) { |
| 122 | ret = PTR_ERR(nlsk); |
| 123 | if (ret == -EAGAIN) { |
| 124 | ret = 0; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 125 | continue; |
| 126 | } |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 127 | break; |
| 128 | } |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 129 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 130 | sk = (struct sock *)nlsk; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 131 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 132 | if (!net_eq(sock_net(sk), net)) |
| 133 | continue; |
| 134 | |
| 135 | if (sk_diag_fill(sk, skb, req, |
| 136 | NETLINK_CB(cb->skb).portid, |
| 137 | cb->nlh->nlmsg_seq, |
| 138 | NLM_F_MULTI, |
| 139 | sock_i_ino(sk)) < 0) { |
| 140 | ret = 1; |
| 141 | break; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 145 | rhashtable_walk_stop(hti); |
Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame^] | 146 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 147 | if (ret) |
| 148 | goto done; |
| 149 | |
| 150 | rhashtable_walk_exit(hti); |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 151 | num++; |
| 152 | |
| 153 | mc_list: |
| 154 | read_lock(&nl_table_lock); |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 155 | sk_for_each_bound(sk, &tbl->mc_list) { |
| 156 | if (sk_hashed(sk)) |
| 157 | continue; |
| 158 | if (!net_eq(sock_net(sk), net)) |
| 159 | continue; |
| 160 | if (num < s_num) { |
| 161 | num++; |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | if (sk_diag_fill(sk, skb, req, |
| 166 | NETLINK_CB(cb->skb).portid, |
| 167 | cb->nlh->nlmsg_seq, |
| 168 | NLM_F_MULTI, |
| 169 | sock_i_ino(sk)) < 0) { |
| 170 | ret = 1; |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 171 | break; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 172 | } |
| 173 | num++; |
| 174 | } |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 175 | read_unlock(&nl_table_lock); |
| 176 | |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 177 | done: |
| 178 | cb->args[0] = num; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 179 | |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 184 | { |
| 185 | struct netlink_diag_req *req; |
| 186 | int s_num = cb->args[0]; |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 187 | int err = 0; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 188 | |
| 189 | req = nlmsg_data(cb->nlh); |
| 190 | |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 191 | if (req->sdiag_protocol == NDIAG_PROTO_ALL) { |
| 192 | int i; |
| 193 | |
| 194 | for (i = cb->args[1]; i < MAX_LINKS; i++) { |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 195 | err = __netlink_diag_dump(skb, cb, i, s_num); |
| 196 | if (err) |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 197 | break; |
| 198 | s_num = 0; |
| 199 | } |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 200 | cb->args[1] = i; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 201 | } else { |
Eric Dumazet | 93636d1 | 2016-11-02 20:21:20 -0700 | [diff] [blame] | 202 | if (req->sdiag_protocol >= MAX_LINKS) |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 203 | return -ENOENT; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 204 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 205 | err = __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num); |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 206 | } |
| 207 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 208 | return err < 0 ? err : skb->len; |
| 209 | } |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 210 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 211 | static int netlink_diag_dump_done(struct netlink_callback *cb) |
| 212 | { |
| 213 | struct rhashtable_iter *hti = (void *)cb->args[2]; |
| 214 | |
| 215 | if (cb->args[0] == 1) |
| 216 | rhashtable_walk_exit(hti); |
| 217 | |
| 218 | kfree(hti); |
| 219 | |
| 220 | return 0; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h) |
| 224 | { |
| 225 | int hdrlen = sizeof(struct netlink_diag_req); |
| 226 | struct net *net = sock_net(skb->sk); |
| 227 | |
| 228 | if (nlmsg_len(h) < hdrlen) |
| 229 | return -EINVAL; |
| 230 | |
| 231 | if (h->nlmsg_flags & NLM_F_DUMP) { |
| 232 | struct netlink_dump_control c = { |
| 233 | .dump = netlink_diag_dump, |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 234 | .done = netlink_diag_dump_done, |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 235 | }; |
| 236 | return netlink_dump_start(net->diag_nlsk, skb, h, &c); |
| 237 | } else |
| 238 | return -EOPNOTSUPP; |
| 239 | } |
| 240 | |
| 241 | static const struct sock_diag_handler netlink_diag_handler = { |
| 242 | .family = AF_NETLINK, |
| 243 | .dump = netlink_diag_handler_dump, |
| 244 | }; |
| 245 | |
| 246 | static int __init netlink_diag_init(void) |
| 247 | { |
| 248 | return sock_diag_register(&netlink_diag_handler); |
| 249 | } |
| 250 | |
| 251 | static void __exit netlink_diag_exit(void) |
| 252 | { |
| 253 | sock_diag_unregister(&netlink_diag_handler); |
| 254 | } |
| 255 | |
| 256 | module_init(netlink_diag_init); |
| 257 | module_exit(netlink_diag_exit); |
| 258 | MODULE_LICENSE("GPL"); |
| 259 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */); |