162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * net/tipc/diag.c: TIPC socket diag
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (c) 2018, Ericsson AB
562306a36Sopenharmony_ci * All rights reserved.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
862306a36Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
1162306a36Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
1262306a36Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
1362306a36Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
1462306a36Sopenharmony_ci *    documentation and/or other materials provided with the distribution.
1562306a36Sopenharmony_ci * 3. Neither the names of the copyright holders nor the names of its
1662306a36Sopenharmony_ci *    contributors may be used to endorse or promote products derived from
1762306a36Sopenharmony_ci *    this software without specific prior written permission.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the
2062306a36Sopenharmony_ci * GNU General Public License ("GPL") version 2 as published by the Free
2162306a36Sopenharmony_ci * Software Foundation.
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "ASIS"
2462306a36Sopenharmony_ci * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE
2562306a36Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2662306a36Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2762306a36Sopenharmony_ci * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2862306a36Sopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2962306a36Sopenharmony_ci * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3062306a36Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3162306a36Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3262306a36Sopenharmony_ci * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3362306a36Sopenharmony_ci * POSSIBILITY OF SUCH DAMAGE.
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#include "core.h"
3762306a36Sopenharmony_ci#include "socket.h"
3862306a36Sopenharmony_ci#include <linux/sock_diag.h>
3962306a36Sopenharmony_ci#include <linux/tipc_sockets_diag.h>
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistatic u64 __tipc_diag_gen_cookie(struct sock *sk)
4262306a36Sopenharmony_ci{
4362306a36Sopenharmony_ci	u32 res[2];
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	sock_diag_save_cookie(sk, res);
4662306a36Sopenharmony_ci	return *((u64 *)res);
4762306a36Sopenharmony_ci}
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_cistatic int __tipc_add_sock_diag(struct sk_buff *skb,
5062306a36Sopenharmony_ci				struct netlink_callback *cb,
5162306a36Sopenharmony_ci				struct tipc_sock *tsk)
5262306a36Sopenharmony_ci{
5362306a36Sopenharmony_ci	struct tipc_sock_diag_req *req = nlmsg_data(cb->nlh);
5462306a36Sopenharmony_ci	struct nlmsghdr *nlh;
5562306a36Sopenharmony_ci	int err;
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	nlh = nlmsg_put_answer(skb, cb, SOCK_DIAG_BY_FAMILY, 0,
5862306a36Sopenharmony_ci			       NLM_F_MULTI);
5962306a36Sopenharmony_ci	if (!nlh)
6062306a36Sopenharmony_ci		return -EMSGSIZE;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	err = tipc_sk_fill_sock_diag(skb, cb, tsk, req->tidiag_states,
6362306a36Sopenharmony_ci				     __tipc_diag_gen_cookie);
6462306a36Sopenharmony_ci	if (err)
6562306a36Sopenharmony_ci		return err;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	nlmsg_end(skb, nlh);
6862306a36Sopenharmony_ci	return 0;
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistatic int tipc_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
7262306a36Sopenharmony_ci{
7362306a36Sopenharmony_ci	return tipc_nl_sk_walk(skb, cb, __tipc_add_sock_diag);
7462306a36Sopenharmony_ci}
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_cistatic int tipc_sock_diag_handler_dump(struct sk_buff *skb,
7762306a36Sopenharmony_ci				       struct nlmsghdr *h)
7862306a36Sopenharmony_ci{
7962306a36Sopenharmony_ci	int hdrlen = sizeof(struct tipc_sock_diag_req);
8062306a36Sopenharmony_ci	struct net *net = sock_net(skb->sk);
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	if (nlmsg_len(h) < hdrlen)
8362306a36Sopenharmony_ci		return -EINVAL;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	if (h->nlmsg_flags & NLM_F_DUMP) {
8662306a36Sopenharmony_ci		struct netlink_dump_control c = {
8762306a36Sopenharmony_ci			.start = tipc_dump_start,
8862306a36Sopenharmony_ci			.dump = tipc_diag_dump,
8962306a36Sopenharmony_ci			.done = tipc_dump_done,
9062306a36Sopenharmony_ci		};
9162306a36Sopenharmony_ci		netlink_dump_start(net->diag_nlsk, skb, h, &c);
9262306a36Sopenharmony_ci		return 0;
9362306a36Sopenharmony_ci	}
9462306a36Sopenharmony_ci	return -EOPNOTSUPP;
9562306a36Sopenharmony_ci}
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_cistatic const struct sock_diag_handler tipc_sock_diag_handler = {
9862306a36Sopenharmony_ci	.family = AF_TIPC,
9962306a36Sopenharmony_ci	.dump = tipc_sock_diag_handler_dump,
10062306a36Sopenharmony_ci};
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_cistatic int __init tipc_diag_init(void)
10362306a36Sopenharmony_ci{
10462306a36Sopenharmony_ci	return sock_diag_register(&tipc_sock_diag_handler);
10562306a36Sopenharmony_ci}
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_cistatic void __exit tipc_diag_exit(void)
10862306a36Sopenharmony_ci{
10962306a36Sopenharmony_ci	sock_diag_unregister(&tipc_sock_diag_handler);
11062306a36Sopenharmony_ci}
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_cimodule_init(tipc_diag_init);
11362306a36Sopenharmony_cimodule_exit(tipc_diag_exit);
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
11662306a36Sopenharmony_ciMODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, AF_TIPC);
117