~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/core/sock_diag.c

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.75 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp