xref: /openbmc/linux/net/tipc/diag.c (revision 867e6d38)
1 /*
2  * net/tipc/diag.c: TIPC socket diag
3  *
4  * Copyright (c) 2018, Ericsson AB
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the names of the copyright holders nor the names of its
16  *    contributors may be used to endorse or promote products derived from
17  *    this software without specific prior written permission.
18  *
19  * Alternatively, this software may be distributed under the terms of the
20  * GNU General Public License ("GPL") version 2 as published by the Free
21  * Software Foundation.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "ASIS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include "core.h"
37 #include "socket.h"
38 #include <linux/sock_diag.h>
39 #include <linux/tipc_sockets_diag.h>
40 
41 static u64 __tipc_diag_gen_cookie(struct sock *sk)
42 {
43 	u32 res[2];
44 
45 	sock_diag_save_cookie(sk, res);
46 	return *((u64 *)res);
47 }
48 
49 static int __tipc_add_sock_diag(struct sk_buff *skb,
50 				struct netlink_callback *cb,
51 				struct tipc_sock *tsk)
52 {
53 	struct tipc_sock_diag_req *req = nlmsg_data(cb->nlh);
54 	struct nlmsghdr *nlh;
55 	int err;
56 
57 	nlh = nlmsg_put_answer(skb, cb, SOCK_DIAG_BY_FAMILY, 0,
58 			       NLM_F_MULTI);
59 	if (!nlh)
60 		return -EMSGSIZE;
61 
62 	err = tipc_sk_fill_sock_diag(skb, cb, tsk, req->tidiag_states,
63 				     __tipc_diag_gen_cookie);
64 	if (err)
65 		return err;
66 
67 	nlmsg_end(skb, nlh);
68 	return 0;
69 }
70 
71 static int tipc_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
72 {
73 	return tipc_nl_sk_walk(skb, cb, __tipc_add_sock_diag);
74 }
75 
76 static int tipc_sock_diag_handler_dump(struct sk_buff *skb,
77 				       struct nlmsghdr *h)
78 {
79 	int hdrlen = sizeof(struct tipc_sock_diag_req);
80 	struct net *net = sock_net(skb->sk);
81 
82 	if (nlmsg_len(h) < hdrlen)
83 		return -EINVAL;
84 
85 	if (h->nlmsg_flags & NLM_F_DUMP) {
86 		struct netlink_dump_control c = {
87 			.start = tipc_dump_start,
88 			.dump = tipc_diag_dump,
89 			.done = tipc_dump_done,
90 		};
91 		netlink_dump_start(net->diag_nlsk, skb, h, &c);
92 		return 0;
93 	}
94 	return -EOPNOTSUPP;
95 }
96 
97 static const struct sock_diag_handler tipc_sock_diag_handler = {
98 	.family = AF_TIPC,
99 	.dump = tipc_sock_diag_handler_dump,
100 };
101 
102 static int __init tipc_diag_init(void)
103 {
104 	return sock_diag_register(&tipc_sock_diag_handler);
105 }
106 
107 static void __exit tipc_diag_exit(void)
108 {
109 	sock_diag_unregister(&tipc_sock_diag_handler);
110 }
111 
112 module_init(tipc_diag_init);
113 module_exit(tipc_diag_exit);
114 
115 MODULE_LICENSE("Dual BSD/GPL");
116 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, AF_TIPC);
117