xref: /openbmc/linux/net/mptcp/diag.c (revision f0931824)
1 // SPDX-License-Identifier: GPL-2.0
2 /* MPTCP socket monitoring support
3  *
4  * Copyright (c) 2019 Red Hat
5  *
6  * Author: Davide Caratti <dcaratti@redhat.com>
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/net.h>
11 #include <linux/inet_diag.h>
12 #include <net/netlink.h>
13 #include <uapi/linux/mptcp.h>
14 #include "protocol.h"
15 
16 static int subflow_get_info(struct sock *sk, struct sk_buff *skb)
17 {
18 	struct mptcp_subflow_context *sf;
19 	struct nlattr *start;
20 	u32 flags = 0;
21 	bool slow;
22 	int err;
23 
24 	if (inet_sk_state_load(sk) == TCP_LISTEN)
25 		return 0;
26 
27 	start = nla_nest_start_noflag(skb, INET_ULP_INFO_MPTCP);
28 	if (!start)
29 		return -EMSGSIZE;
30 
31 	slow = lock_sock_fast(sk);
32 	rcu_read_lock();
33 	sf = rcu_dereference(inet_csk(sk)->icsk_ulp_data);
34 	if (!sf) {
35 		err = 0;
36 		goto nla_failure;
37 	}
38 
39 	if (sf->mp_capable)
40 		flags |= MPTCP_SUBFLOW_FLAG_MCAP_REM;
41 	if (sf->request_mptcp)
42 		flags |= MPTCP_SUBFLOW_FLAG_MCAP_LOC;
43 	if (sf->mp_join)
44 		flags |= MPTCP_SUBFLOW_FLAG_JOIN_REM;
45 	if (sf->request_join)
46 		flags |= MPTCP_SUBFLOW_FLAG_JOIN_LOC;
47 	if (sf->backup)
48 		flags |= MPTCP_SUBFLOW_FLAG_BKUP_REM;
49 	if (sf->request_bkup)
50 		flags |= MPTCP_SUBFLOW_FLAG_BKUP_LOC;
51 	if (sf->fully_established)
52 		flags |= MPTCP_SUBFLOW_FLAG_FULLY_ESTABLISHED;
53 	if (sf->conn_finished)
54 		flags |= MPTCP_SUBFLOW_FLAG_CONNECTED;
55 	if (sf->map_valid)
56 		flags |= MPTCP_SUBFLOW_FLAG_MAPVALID;
57 
58 	if (nla_put_u32(skb, MPTCP_SUBFLOW_ATTR_TOKEN_REM, sf->remote_token) ||
59 	    nla_put_u32(skb, MPTCP_SUBFLOW_ATTR_TOKEN_LOC, sf->token) ||
60 	    nla_put_u32(skb, MPTCP_SUBFLOW_ATTR_RELWRITE_SEQ,
61 			sf->rel_write_seq) ||
62 	    nla_put_u64_64bit(skb, MPTCP_SUBFLOW_ATTR_MAP_SEQ, sf->map_seq,
63 			      MPTCP_SUBFLOW_ATTR_PAD) ||
64 	    nla_put_u32(skb, MPTCP_SUBFLOW_ATTR_MAP_SFSEQ,
65 			sf->map_subflow_seq) ||
66 	    nla_put_u32(skb, MPTCP_SUBFLOW_ATTR_SSN_OFFSET, sf->ssn_offset) ||
67 	    nla_put_u16(skb, MPTCP_SUBFLOW_ATTR_MAP_DATALEN,
68 			sf->map_data_len) ||
69 	    nla_put_u32(skb, MPTCP_SUBFLOW_ATTR_FLAGS, flags) ||
70 	    nla_put_u8(skb, MPTCP_SUBFLOW_ATTR_ID_REM, sf->remote_id) ||
71 	    nla_put_u8(skb, MPTCP_SUBFLOW_ATTR_ID_LOC, subflow_get_local_id(sf))) {
72 		err = -EMSGSIZE;
73 		goto nla_failure;
74 	}
75 
76 	rcu_read_unlock();
77 	unlock_sock_fast(sk, slow);
78 	nla_nest_end(skb, start);
79 	return 0;
80 
81 nla_failure:
82 	rcu_read_unlock();
83 	unlock_sock_fast(sk, slow);
84 	nla_nest_cancel(skb, start);
85 	return err;
86 }
87 
88 static size_t subflow_get_info_size(const struct sock *sk)
89 {
90 	size_t size = 0;
91 
92 	size += nla_total_size(0) +	/* INET_ULP_INFO_MPTCP */
93 		nla_total_size(4) +	/* MPTCP_SUBFLOW_ATTR_TOKEN_REM */
94 		nla_total_size(4) +	/* MPTCP_SUBFLOW_ATTR_TOKEN_LOC */
95 		nla_total_size(4) +	/* MPTCP_SUBFLOW_ATTR_RELWRITE_SEQ */
96 		nla_total_size_64bit(8) +	/* MPTCP_SUBFLOW_ATTR_MAP_SEQ */
97 		nla_total_size(4) +	/* MPTCP_SUBFLOW_ATTR_MAP_SFSEQ */
98 		nla_total_size(2) +	/* MPTCP_SUBFLOW_ATTR_SSN_OFFSET */
99 		nla_total_size(2) +	/* MPTCP_SUBFLOW_ATTR_MAP_DATALEN */
100 		nla_total_size(4) +	/* MPTCP_SUBFLOW_ATTR_FLAGS */
101 		nla_total_size(1) +	/* MPTCP_SUBFLOW_ATTR_ID_REM */
102 		nla_total_size(1) +	/* MPTCP_SUBFLOW_ATTR_ID_LOC */
103 		0;
104 	return size;
105 }
106 
107 void mptcp_diag_subflow_init(struct tcp_ulp_ops *ops)
108 {
109 	ops->get_info = subflow_get_info;
110 	ops->get_info_size = subflow_get_info_size;
111 }
112