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 | |
| 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) |
| 25 | { |
| 26 | struct nlmsghdr *nlh; |
| 27 | struct netlink_diag_msg *rep; |
| 28 | struct netlink_sock *nlk = nlk_sk(sk); |
| 29 | |
| 30 | nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep), |
| 31 | flags); |
| 32 | if (!nlh) |
| 33 | return -EMSGSIZE; |
| 34 | |
| 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; |
| 40 | |
| 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); |
| 46 | |
| 47 | if ((req->ndiag_show & NDIAG_SHOW_GROUPS) && |
| 48 | sk_diag_dump_groups(sk, skb)) |
| 49 | goto out_nlmsg_trim; |
| 50 | |
| 51 | if ((req->ndiag_show & NDIAG_SHOW_MEMINFO) && |
| 52 | sock_diag_put_meminfo(sk, skb, NETLINK_DIAG_MEMINFO)) |
| 53 | goto out_nlmsg_trim; |
| 54 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 55 | nlmsg_end(skb, nlh); |
| 56 | return 0; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 57 | |
| 58 | out_nlmsg_trim: |
| 59 | nlmsg_cancel(skb, nlh); |
| 60 | return -EMSGSIZE; |
| 61 | } |
| 62 | |
| 63 | static int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, |
| 64 | int protocol, int s_num) |
| 65 | { |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 66 | struct rhashtable_iter *hti = (void *)cb->args[2]; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 67 | struct netlink_table *tbl = &nl_table[protocol]; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 68 | struct net *net = sock_net(skb->sk); |
| 69 | struct netlink_diag_req *req; |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 70 | struct netlink_sock *nlsk; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 71 | struct sock *sk; |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 72 | int num = 2; |
| 73 | int ret = 0; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 74 | |
| 75 | req = nlmsg_data(cb->nlh); |
| 76 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 77 | if (s_num > 1) |
| 78 | goto mc_list; |
Thomas Graf | 88d6ed1 | 2015-01-02 23:00:16 +0100 | [diff] [blame] | 79 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 80 | num--; |
Thomas Graf | e341694 | 2014-08-02 11:47:45 +0200 | [diff] [blame] | 81 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 82 | if (!hti) { |
| 83 | hti = kmalloc(sizeof(*hti), GFP_KERNEL); |
| 84 | if (!hti) |
| 85 | return -ENOMEM; |
| 86 | |
| 87 | cb->args[2] = (long)hti; |
| 88 | } |
| 89 | |
| 90 | if (!s_num) |
| 91 | rhashtable_walk_enter(&tbl->hash, hti); |
| 92 | |
| 93 | ret = rhashtable_walk_start(hti); |
| 94 | if (ret == -EAGAIN) |
| 95 | ret = 0; |
| 96 | if (ret) |
| 97 | goto stop; |
| 98 | |
| 99 | while ((nlsk = rhashtable_walk_next(hti))) { |
| 100 | if (IS_ERR(nlsk)) { |
| 101 | ret = PTR_ERR(nlsk); |
| 102 | if (ret == -EAGAIN) { |
| 103 | ret = 0; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 104 | continue; |
| 105 | } |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 106 | break; |
| 107 | } |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 108 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 109 | sk = (struct sock *)nlsk; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 110 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 111 | if (!net_eq(sock_net(sk), net)) |
| 112 | continue; |
| 113 | |
| 114 | if (sk_diag_fill(sk, skb, req, |
| 115 | NETLINK_CB(cb->skb).portid, |
| 116 | cb->nlh->nlmsg_seq, |
| 117 | NLM_F_MULTI, |
| 118 | sock_i_ino(sk)) < 0) { |
| 119 | ret = 1; |
| 120 | break; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 124 | stop: |
| 125 | rhashtable_walk_stop(hti); |
| 126 | if (ret) |
| 127 | goto done; |
| 128 | |
| 129 | rhashtable_walk_exit(hti); |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 130 | num++; |
| 131 | |
| 132 | mc_list: |
| 133 | read_lock(&nl_table_lock); |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 134 | sk_for_each_bound(sk, &tbl->mc_list) { |
| 135 | if (sk_hashed(sk)) |
| 136 | continue; |
| 137 | if (!net_eq(sock_net(sk), net)) |
| 138 | continue; |
| 139 | if (num < s_num) { |
| 140 | num++; |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | if (sk_diag_fill(sk, skb, req, |
| 145 | NETLINK_CB(cb->skb).portid, |
| 146 | cb->nlh->nlmsg_seq, |
| 147 | NLM_F_MULTI, |
| 148 | sock_i_ino(sk)) < 0) { |
| 149 | ret = 1; |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 150 | break; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 151 | } |
| 152 | num++; |
| 153 | } |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 154 | read_unlock(&nl_table_lock); |
| 155 | |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 156 | done: |
| 157 | cb->args[0] = num; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 158 | |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 163 | { |
| 164 | struct netlink_diag_req *req; |
| 165 | int s_num = cb->args[0]; |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 166 | int err = 0; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 167 | |
| 168 | req = nlmsg_data(cb->nlh); |
| 169 | |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 170 | if (req->sdiag_protocol == NDIAG_PROTO_ALL) { |
| 171 | int i; |
| 172 | |
| 173 | for (i = cb->args[1]; i < MAX_LINKS; i++) { |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 174 | err = __netlink_diag_dump(skb, cb, i, s_num); |
| 175 | if (err) |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 176 | break; |
| 177 | s_num = 0; |
| 178 | } |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 179 | cb->args[1] = i; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 180 | } else { |
| 181 | if (req->sdiag_protocol >= MAX_LINKS) { |
| 182 | read_unlock(&nl_table_lock); |
Ying Xue | c5adde9 | 2015-01-12 14:52:23 +0800 | [diff] [blame] | 183 | rcu_read_unlock(); |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 184 | return -ENOENT; |
| 185 | } |
| 186 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 187 | err = __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num); |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 188 | } |
| 189 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 190 | return err < 0 ? err : skb->len; |
| 191 | } |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 192 | |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 193 | static int netlink_diag_dump_done(struct netlink_callback *cb) |
| 194 | { |
| 195 | struct rhashtable_iter *hti = (void *)cb->args[2]; |
| 196 | |
| 197 | if (cb->args[0] == 1) |
| 198 | rhashtable_walk_exit(hti); |
| 199 | |
| 200 | kfree(hti); |
| 201 | |
| 202 | return 0; |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h) |
| 206 | { |
| 207 | int hdrlen = sizeof(struct netlink_diag_req); |
| 208 | struct net *net = sock_net(skb->sk); |
| 209 | |
| 210 | if (nlmsg_len(h) < hdrlen) |
| 211 | return -EINVAL; |
| 212 | |
| 213 | if (h->nlmsg_flags & NLM_F_DUMP) { |
| 214 | struct netlink_dump_control c = { |
| 215 | .dump = netlink_diag_dump, |
Herbert Xu | ad20207 | 2016-08-19 16:21:37 +0800 | [diff] [blame] | 216 | .done = netlink_diag_dump_done, |
Andrey Vagin | eaaa313 | 2013-03-21 20:33:48 +0400 | [diff] [blame] | 217 | }; |
| 218 | return netlink_dump_start(net->diag_nlsk, skb, h, &c); |
| 219 | } else |
| 220 | return -EOPNOTSUPP; |
| 221 | } |
| 222 | |
| 223 | static const struct sock_diag_handler netlink_diag_handler = { |
| 224 | .family = AF_NETLINK, |
| 225 | .dump = netlink_diag_handler_dump, |
| 226 | }; |
| 227 | |
| 228 | static int __init netlink_diag_init(void) |
| 229 | { |
| 230 | return sock_diag_register(&netlink_diag_handler); |
| 231 | } |
| 232 | |
| 233 | static void __exit netlink_diag_exit(void) |
| 234 | { |
| 235 | sock_diag_unregister(&netlink_diag_handler); |
| 236 | } |
| 237 | |
| 238 | module_init(netlink_diag_init); |
| 239 | module_exit(netlink_diag_exit); |
| 240 | MODULE_LICENSE("GPL"); |
| 241 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */); |