1 /* License: GPL */ 2 3 #include <linux/mutex.h> 4 #include <linux/socket.h> 5 #include <linux/skbuff.h> 6 #include <net/netlink.h> 7 #include <net/net_namespace.h> 8 #include <linux/module.h> 9 #include <net/sock.h> 10 #include <linux/kernel.h> 11 #include <linux/tcp.h> 12 #include <linux/workqueue.h> 13 14 #include <linux/inet_diag.h> 15 #include <linux/sock_diag.h> 16 17 static const struct sock_diag_handler *sock_diag_handlers[AF_MAX]; 18 static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh); 19 static DEFINE_MUTEX(sock_diag_table_mutex); 20 static struct workqueue_struct *broadcast_wq; 21 22 static u64 sock_gen_cookie(struct sock *sk) 23 { 24 while (1) { 25 u64 res = atomic64_read(&sk->sk_cookie); 26 27 if (res) 28 return res; 29 res = atomic64_inc_return(&sock_net(sk)->cookie_gen); 30 atomic64_cmpxchg(&sk->sk_cookie, 0, res); 31 } 32 } 33 34 int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie) 35 { 36 u64 res; 37 38 if (cookie[0] == INET_DIAG_NOCOOKIE && cookie[1] == INET_DIAG_NOCOOKIE) 39 return 0; 40 41 res = sock_gen_cookie(sk); 42 if ((u32)res != cookie[0] || (u32)(res >> 32) != cookie[1]) 43 return -ESTALE; 44 45 return 0; 46 } 47 EXPORT_SYMBOL_GPL(sock_diag_check_cookie); 48 49 void sock_diag_save_cookie(struct sock *sk, __u32 *cookie) 50 { 51 u64 res = sock_gen_cookie(sk); 52 53 cookie[0] = (u32)res; 54 cookie[1] = (u32)(res >> 32); 55 } 56 EXPORT_SYMBOL_GPL(sock_diag_save_cookie); 57 58 int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype) 59 { 60 u32 mem[SK_MEMINFO_VARS]; 61 62 mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk); 63 mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf; 64 mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk); 65 mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf; 66 mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc; 67 mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued; 68 mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc); 69 mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len; 70 71 return nla_put(skb, attrtype, sizeof(mem), &mem); 72 } 73 EXPORT_SYMBOL_GPL(sock_diag_put_meminfo); 74 75 int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk, 76 struct sk_buff *skb, int attrtype) 77 { 78 struct sock_fprog_kern *fprog; 79 struct sk_filter *filter; 80 struct nlattr *attr; 81 unsigned int flen; 82 int err = 0; 83 84 if (!may_report_filterinfo) { 85 nla_reserve(skb, attrtype, 0); 86 return 0; 87 } 88 89 rcu_read_lock(); 90 filter = rcu_dereference(sk->sk_filter); 91 if (!filter) 92 goto out; 93 94 fprog = filter->prog->orig_prog; 95 if (!fprog) 96 goto out; 97 98 flen = bpf_classic_proglen(fprog); 99 100 attr = nla_reserve(skb, attrtype, flen); 101 if (attr == NULL) { 102 err = -EMSGSIZE; 103 goto out; 104 } 105 106 memcpy(nla_data(attr), fprog->filter, flen); 107 out: 108 rcu_read_unlock(); 109 return err; 110 } 111 EXPORT_SYMBOL(sock_diag_put_filterinfo); 112 113 struct broadcast_sk { 114 struct sock *sk; 115 struct work_struct work; 116 }; 117 118 static size_t sock_diag_nlmsg_size(void) 119 { 120 return NLMSG_ALIGN(sizeof(struct inet_diag_msg) 121 + nla_total_size(sizeof(u8)) /* INET_DIAG_PROTOCOL */ 122 + nla_total_size(sizeof(struct tcp_info))); /* INET_DIAG_INFO */ 123 } 124 125 static void sock_diag_broadcast_destroy_work(struct work_struct *work) 126 { 127 struct broadcast_sk *bsk = 128 container_of(work, struct broadcast_sk, work); 129 struct sock *sk = bsk->sk; 130 const struct sock_diag_handler *hndl; 131 struct sk_buff *skb; 132 const enum sknetlink_groups group = sock_diag_destroy_group(sk); 133 int err = -1; 134 135 WARN_ON(group == SKNLGRP_NONE); 136 137 skb = nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL); 138 if (!skb) 139 goto out; 140 141 mutex_lock(&sock_diag_table_mutex); 142 hndl = sock_diag_handlers[sk->sk_family]; 143 if (hndl && hndl->get_info) 144 err = hndl->get_info(skb, sk); 145 mutex_unlock(&sock_diag_table_mutex); 146 147 if (!err) 148 nlmsg_multicast(sock_net(sk)->diag_nlsk, skb, 0, group, 149 GFP_KERNEL); 150 else 151 kfree_skb(skb); 152 out: 153 sk_destruct(sk); 154 kfree(bsk); 155 } 156 157 void sock_diag_broadcast_destroy(struct sock *sk) 158 { 159 /* Note, this function is often called from an interrupt context. */ 160 struct broadcast_sk *bsk = 161 kmalloc(sizeof(struct broadcast_sk), GFP_ATOMIC); 162 if (!bsk) 163 return sk_destruct(sk); 164 bsk->sk = sk; 165 INIT_WORK(&bsk->work, sock_diag_broadcast_destroy_work); 166 queue_work(broadcast_wq, &bsk->work); 167 } 168 169 void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) 170 { 171 mutex_lock(&sock_diag_table_mutex); 172 inet_rcv_compat = fn; 173 mutex_unlock(&sock_diag_table_mutex); 174 } 175 EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat); 176 177 void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) 178 { 179 mutex_lock(&sock_diag_table_mutex); 180 inet_rcv_compat = NULL; 181 mutex_unlock(&sock_diag_table_mutex); 182 } 183 EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat); 184 185 int sock_diag_register(const struct sock_diag_handler *hndl) 186 { 187 int err = 0; 188 189 if (hndl->family >= AF_MAX) 190 return -EINVAL; 191 192 mutex_lock(&sock_diag_table_mutex); 193 if (sock_diag_handlers[hndl->family]) 194 err = -EBUSY; 195 else 196 sock_diag_handlers[hndl->family] = hndl; 197 mutex_unlock(&sock_diag_table_mutex); 198 199 return err; 200 } 201 EXPORT_SYMBOL_GPL(sock_diag_register); 202 203 void sock_diag_unregister(const struct sock_diag_handler *hnld) 204 { 205 int family = hnld->family; 206 207 if (family >= AF_MAX) 208 return; 209 210 mutex_lock(&sock_diag_table_mutex); 211 BUG_ON(sock_diag_handlers[family] != hnld); 212 sock_diag_handlers[family] = NULL; 213 mutex_unlock(&sock_diag_table_mutex); 214 } 215 EXPORT_SYMBOL_GPL(sock_diag_unregister); 216 217 static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 218 { 219 int err; 220 struct sock_diag_req *req = nlmsg_data(nlh); 221 const struct sock_diag_handler *hndl; 222 223 if (nlmsg_len(nlh) < sizeof(*req)) 224 return -EINVAL; 225 226 if (req->sdiag_family >= AF_MAX) 227 return -EINVAL; 228 229 if (sock_diag_handlers[req->sdiag_family] == NULL) 230 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, 231 NETLINK_SOCK_DIAG, req->sdiag_family); 232 233 mutex_lock(&sock_diag_table_mutex); 234 hndl = sock_diag_handlers[req->sdiag_family]; 235 if (hndl == NULL) 236 err = -ENOENT; 237 else 238 err = hndl->dump(skb, nlh); 239 mutex_unlock(&sock_diag_table_mutex); 240 241 return err; 242 } 243 244 static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 245 { 246 int ret; 247 248 switch (nlh->nlmsg_type) { 249 case TCPDIAG_GETSOCK: 250 case DCCPDIAG_GETSOCK: 251 if (inet_rcv_compat == NULL) 252 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, 253 NETLINK_SOCK_DIAG, AF_INET); 254 255 mutex_lock(&sock_diag_table_mutex); 256 if (inet_rcv_compat != NULL) 257 ret = inet_rcv_compat(skb, nlh); 258 else 259 ret = -EOPNOTSUPP; 260 mutex_unlock(&sock_diag_table_mutex); 261 262 return ret; 263 case SOCK_DIAG_BY_FAMILY: 264 return __sock_diag_rcv_msg(skb, nlh); 265 default: 266 return -EINVAL; 267 } 268 } 269 270 static DEFINE_MUTEX(sock_diag_mutex); 271 272 static void sock_diag_rcv(struct sk_buff *skb) 273 { 274 mutex_lock(&sock_diag_mutex); 275 netlink_rcv_skb(skb, &sock_diag_rcv_msg); 276 mutex_unlock(&sock_diag_mutex); 277 } 278 279 static int sock_diag_bind(struct net *net, int group) 280 { 281 switch (group) { 282 case SKNLGRP_INET_TCP_DESTROY: 283 case SKNLGRP_INET_UDP_DESTROY: 284 if (!sock_diag_handlers[AF_INET]) 285 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, 286 NETLINK_SOCK_DIAG, AF_INET); 287 break; 288 case SKNLGRP_INET6_TCP_DESTROY: 289 case SKNLGRP_INET6_UDP_DESTROY: 290 if (!sock_diag_handlers[AF_INET6]) 291 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, 292 NETLINK_SOCK_DIAG, AF_INET); 293 break; 294 } 295 return 0; 296 } 297 298 static int __net_init diag_net_init(struct net *net) 299 { 300 struct netlink_kernel_cfg cfg = { 301 .groups = SKNLGRP_MAX, 302 .input = sock_diag_rcv, 303 .bind = sock_diag_bind, 304 .flags = NL_CFG_F_NONROOT_RECV, 305 }; 306 307 net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg); 308 return net->diag_nlsk == NULL ? -ENOMEM : 0; 309 } 310 311 static void __net_exit diag_net_exit(struct net *net) 312 { 313 netlink_kernel_release(net->diag_nlsk); 314 net->diag_nlsk = NULL; 315 } 316 317 static struct pernet_operations diag_net_ops = { 318 .init = diag_net_init, 319 .exit = diag_net_exit, 320 }; 321 322 static int __init sock_diag_init(void) 323 { 324 broadcast_wq = alloc_workqueue("sock_diag_events", 0, 0); 325 BUG_ON(!broadcast_wq); 326 return register_pernet_subsys(&diag_net_ops); 327 } 328 device_initcall(sock_diag_init); 329