Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 1 | #include <linux/mutex.h> |
| 2 | #include <linux/socket.h> |
| 3 | #include <linux/skbuff.h> |
| 4 | #include <net/netlink.h> |
| 5 | #include <net/net_namespace.h> |
| 6 | #include <linux/module.h> |
Pavel Emelyanov | 5d2e5f2 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 7 | #include <net/sock.h> |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 8 | |
| 9 | #include <linux/inet_diag.h> |
| 10 | #include <linux/sock_diag.h> |
| 11 | |
Shan Wei | 8dcf01f | 2012-04-24 18:21:07 +0000 | [diff] [blame] | 12 | static const struct sock_diag_handler *sock_diag_handlers[AF_MAX]; |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 13 | static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh); |
| 14 | static DEFINE_MUTEX(sock_diag_table_mutex); |
| 15 | |
Pavel Emelyanov | f65c1b5 | 2011-12-15 02:43:44 +0000 | [diff] [blame] | 16 | int sock_diag_check_cookie(void *sk, __u32 *cookie) |
| 17 | { |
| 18 | if ((cookie[0] != INET_DIAG_NOCOOKIE || |
| 19 | cookie[1] != INET_DIAG_NOCOOKIE) && |
| 20 | ((u32)(unsigned long)sk != cookie[0] || |
| 21 | (u32)((((unsigned long)sk) >> 31) >> 1) != cookie[1])) |
| 22 | return -ESTALE; |
| 23 | else |
| 24 | return 0; |
| 25 | } |
| 26 | EXPORT_SYMBOL_GPL(sock_diag_check_cookie); |
| 27 | |
| 28 | void sock_diag_save_cookie(void *sk, __u32 *cookie) |
| 29 | { |
| 30 | cookie[0] = (u32)(unsigned long)sk; |
| 31 | cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1); |
| 32 | } |
| 33 | EXPORT_SYMBOL_GPL(sock_diag_save_cookie); |
| 34 | |
Pavel Emelyanov | 5d2e5f2 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 35 | int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype) |
| 36 | { |
Thomas Graf | 7b46866 | 2012-06-26 23:36:11 +0000 | [diff] [blame] | 37 | u32 mem[SK_MEMINFO_VARS]; |
Pavel Emelyanov | 5d2e5f2 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 38 | |
| 39 | mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk); |
| 40 | mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf; |
| 41 | mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk); |
| 42 | mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf; |
| 43 | mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc; |
| 44 | mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued; |
| 45 | mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc); |
Eric Dumazet | d594e98 | 2012-06-04 03:50:35 +0000 | [diff] [blame] | 46 | mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len; |
Pavel Emelyanov | 5d2e5f2 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 47 | |
Thomas Graf | 7b46866 | 2012-06-26 23:36:11 +0000 | [diff] [blame] | 48 | return nla_put(skb, attrtype, sizeof(mem), &mem); |
Pavel Emelyanov | 5d2e5f2 | 2011-12-30 00:53:13 +0000 | [diff] [blame] | 49 | } |
| 50 | EXPORT_SYMBOL_GPL(sock_diag_put_meminfo); |
| 51 | |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 52 | void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) |
| 53 | { |
| 54 | mutex_lock(&sock_diag_table_mutex); |
| 55 | inet_rcv_compat = fn; |
| 56 | mutex_unlock(&sock_diag_table_mutex); |
| 57 | } |
| 58 | EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat); |
| 59 | |
| 60 | void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) |
| 61 | { |
| 62 | mutex_lock(&sock_diag_table_mutex); |
| 63 | inet_rcv_compat = NULL; |
| 64 | mutex_unlock(&sock_diag_table_mutex); |
| 65 | } |
| 66 | EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat); |
| 67 | |
Shan Wei | 8dcf01f | 2012-04-24 18:21:07 +0000 | [diff] [blame] | 68 | int sock_diag_register(const struct sock_diag_handler *hndl) |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 69 | { |
| 70 | int err = 0; |
| 71 | |
Dan Carpenter | 6f8e4ad | 2011-12-07 20:49:38 +0000 | [diff] [blame] | 72 | if (hndl->family >= AF_MAX) |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 73 | return -EINVAL; |
| 74 | |
| 75 | mutex_lock(&sock_diag_table_mutex); |
| 76 | if (sock_diag_handlers[hndl->family]) |
| 77 | err = -EBUSY; |
| 78 | else |
| 79 | sock_diag_handlers[hndl->family] = hndl; |
| 80 | mutex_unlock(&sock_diag_table_mutex); |
| 81 | |
| 82 | return err; |
| 83 | } |
| 84 | EXPORT_SYMBOL_GPL(sock_diag_register); |
| 85 | |
Shan Wei | 8dcf01f | 2012-04-24 18:21:07 +0000 | [diff] [blame] | 86 | void sock_diag_unregister(const struct sock_diag_handler *hnld) |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 87 | { |
| 88 | int family = hnld->family; |
| 89 | |
Dan Carpenter | 6f8e4ad | 2011-12-07 20:49:38 +0000 | [diff] [blame] | 90 | if (family >= AF_MAX) |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 91 | return; |
| 92 | |
| 93 | mutex_lock(&sock_diag_table_mutex); |
| 94 | BUG_ON(sock_diag_handlers[family] != hnld); |
| 95 | sock_diag_handlers[family] = NULL; |
| 96 | mutex_unlock(&sock_diag_table_mutex); |
| 97 | } |
| 98 | EXPORT_SYMBOL_GPL(sock_diag_unregister); |
| 99 | |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 100 | static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 101 | { |
| 102 | int err; |
Thomas Graf | 7b46866 | 2012-06-26 23:36:11 +0000 | [diff] [blame] | 103 | struct sock_diag_req *req = nlmsg_data(nlh); |
Shan Wei | 8dcf01f | 2012-04-24 18:21:07 +0000 | [diff] [blame] | 104 | const struct sock_diag_handler *hndl; |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 105 | |
| 106 | if (nlmsg_len(nlh) < sizeof(*req)) |
| 107 | return -EINVAL; |
| 108 | |
Mathias Krause | 6e601a5 | 2013-02-23 01:13:47 +0000 | [diff] [blame] | 109 | if (req->sdiag_family >= AF_MAX) |
| 110 | return -EINVAL; |
| 111 | |
Mathias Krause | 8e90455 | 2013-02-23 01:13:48 +0000 | [diff] [blame] | 112 | if (sock_diag_handlers[req->sdiag_family] == NULL) |
| 113 | request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, |
| 114 | NETLINK_SOCK_DIAG, req->sdiag_family); |
| 115 | |
| 116 | mutex_lock(&sock_diag_table_mutex); |
| 117 | hndl = sock_diag_handlers[req->sdiag_family]; |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 118 | if (hndl == NULL) |
| 119 | err = -ENOENT; |
| 120 | else |
| 121 | err = hndl->dump(skb, nlh); |
Mathias Krause | 8e90455 | 2013-02-23 01:13:48 +0000 | [diff] [blame] | 122 | mutex_unlock(&sock_diag_table_mutex); |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 123 | |
| 124 | return err; |
| 125 | } |
| 126 | |
| 127 | static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 128 | { |
| 129 | int ret; |
| 130 | |
| 131 | switch (nlh->nlmsg_type) { |
| 132 | case TCPDIAG_GETSOCK: |
| 133 | case DCCPDIAG_GETSOCK: |
| 134 | if (inet_rcv_compat == NULL) |
| 135 | request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, |
Pavel Emelyanov | aec8dc6 | 2011-12-15 02:43:27 +0000 | [diff] [blame] | 136 | NETLINK_SOCK_DIAG, AF_INET); |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 137 | |
| 138 | mutex_lock(&sock_diag_table_mutex); |
| 139 | if (inet_rcv_compat != NULL) |
| 140 | ret = inet_rcv_compat(skb, nlh); |
| 141 | else |
| 142 | ret = -EOPNOTSUPP; |
| 143 | mutex_unlock(&sock_diag_table_mutex); |
| 144 | |
| 145 | return ret; |
| 146 | case SOCK_DIAG_BY_FAMILY: |
| 147 | return __sock_diag_rcv_msg(skb, nlh); |
| 148 | default: |
| 149 | return -EINVAL; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static DEFINE_MUTEX(sock_diag_mutex); |
| 154 | |
| 155 | static void sock_diag_rcv(struct sk_buff *skb) |
| 156 | { |
| 157 | mutex_lock(&sock_diag_mutex); |
| 158 | netlink_rcv_skb(skb, &sock_diag_rcv_msg); |
| 159 | mutex_unlock(&sock_diag_mutex); |
| 160 | } |
| 161 | |
Andrey Vagin | 51d7ccc | 2012-07-16 04:28:49 +0000 | [diff] [blame] | 162 | static int __net_init diag_net_init(struct net *net) |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 163 | { |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 164 | struct netlink_kernel_cfg cfg = { |
| 165 | .input = sock_diag_rcv, |
| 166 | }; |
| 167 | |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 168 | net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg); |
Andrey Vagin | 51d7ccc | 2012-07-16 04:28:49 +0000 | [diff] [blame] | 169 | return net->diag_nlsk == NULL ? -ENOMEM : 0; |
| 170 | } |
| 171 | |
| 172 | static void __net_exit diag_net_exit(struct net *net) |
| 173 | { |
| 174 | netlink_kernel_release(net->diag_nlsk); |
| 175 | net->diag_nlsk = NULL; |
| 176 | } |
| 177 | |
| 178 | static struct pernet_operations diag_net_ops = { |
| 179 | .init = diag_net_init, |
| 180 | .exit = diag_net_exit, |
| 181 | }; |
| 182 | |
| 183 | static int __init sock_diag_init(void) |
| 184 | { |
| 185 | return register_pernet_subsys(&diag_net_ops); |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static void __exit sock_diag_exit(void) |
| 189 | { |
Andrey Vagin | 51d7ccc | 2012-07-16 04:28:49 +0000 | [diff] [blame] | 190 | unregister_pernet_subsys(&diag_net_ops); |
Pavel Emelyanov | 8ef874b | 2011-12-06 07:59:52 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | module_init(sock_diag_init); |
| 194 | module_exit(sock_diag_exit); |
| 195 | MODULE_LICENSE("GPL"); |
| 196 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG); |