Branch data Line data Source code
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 "protocol.h"
14 : :
15 : 240 : static int subflow_get_info(struct sock *sk, struct sk_buff *skb, bool net_admin)
16 : : {
17 : 240 : struct mptcp_subflow_context *sf;
18 : 240 : struct nlattr *start;
19 : 240 : u32 flags = 0;
20 : 240 : bool slow;
21 : 240 : int err;
22 : :
23 [ - + ]: 240 : if (inet_sk_state_load(sk) == TCP_LISTEN)
24 : : return 0;
25 : :
26 : 240 : start = nla_nest_start_noflag(skb, INET_ULP_INFO_MPTCP);
27 [ + - ]: 240 : if (!start)
28 : : return -EMSGSIZE;
29 : :
30 : 240 : slow = lock_sock_fast(sk);
31 : 240 : rcu_read_lock();
32 [ + - - + : 240 : sf = rcu_dereference(inet_csk(sk)->icsk_ulp_data);
- - - - -
- ]
33 [ - + ]: 240 : if (!sf) {
34 : 0 : err = 0;
35 : 0 : goto nla_failure;
36 : : }
37 : :
38 [ + + ]: 240 : if (sf->mp_capable)
39 : 72 : flags |= MPTCP_SUBFLOW_FLAG_MCAP_REM;
40 [ + + ]: 240 : if (sf->request_mptcp)
41 : 18 : flags |= MPTCP_SUBFLOW_FLAG_MCAP_LOC;
42 [ + + ]: 240 : if (sf->mp_join)
43 : 168 : flags |= MPTCP_SUBFLOW_FLAG_JOIN_REM;
44 [ + + ]: 240 : if (sf->request_join)
45 : 66 : flags |= MPTCP_SUBFLOW_FLAG_JOIN_LOC;
46 [ - + ]: 240 : if (sf->backup)
47 : 0 : flags |= MPTCP_SUBFLOW_FLAG_BKUP_REM;
48 [ - + ]: 240 : if (sf->request_bkup)
49 : 0 : flags |= MPTCP_SUBFLOW_FLAG_BKUP_LOC;
50 [ + + + - ]: 240 : if (READ_ONCE(sf->fully_established))
51 : 240 : flags |= MPTCP_SUBFLOW_FLAG_FULLY_ESTABLISHED;
52 [ + - ]: 240 : if (sf->conn_finished)
53 : 240 : flags |= MPTCP_SUBFLOW_FLAG_CONNECTED;
54 [ - + ]: 240 : if (sf->map_valid)
55 : 0 : flags |= MPTCP_SUBFLOW_FLAG_MAPVALID;
56 : :
57 [ + - + - ]: 408 : if (nla_put_u32(skb, MPTCP_SUBFLOW_ATTR_TOKEN_REM, sf->remote_token) ||
58 [ + - ]: 408 : nla_put_u32(skb, MPTCP_SUBFLOW_ATTR_TOKEN_LOC, sf->token) ||
59 [ + - ]: 240 : nla_put_u32(skb, MPTCP_SUBFLOW_ATTR_FLAGS, flags) ||
60 [ + + ]: 408 : nla_put_u8(skb, MPTCP_SUBFLOW_ATTR_ID_REM, sf->remote_id) ||
61 [ - + ]: 72 : nla_put_u8(skb, MPTCP_SUBFLOW_ATTR_ID_LOC, subflow_get_local_id(sf))) {
62 : 0 : err = -EMSGSIZE;
63 : 0 : goto nla_failure;
64 : : }
65 : :
66 : : /* Only export seq related counters to user with CAP_NET_ADMIN */
67 [ + - + - ]: 408 : if (net_admin &&
68 [ + - ]: 240 : (nla_put_u32(skb, MPTCP_SUBFLOW_ATTR_RELWRITE_SEQ,
69 [ + - ]: 240 : sf->rel_write_seq) ||
70 [ + - ]: 240 : nla_put_u64_64bit(skb, MPTCP_SUBFLOW_ATTR_MAP_SEQ, sf->map_seq,
71 [ + - ]: 240 : MPTCP_SUBFLOW_ATTR_PAD) ||
72 [ + - ]: 240 : nla_put_u32(skb, MPTCP_SUBFLOW_ATTR_MAP_SFSEQ,
73 [ + - ]: 240 : sf->map_subflow_seq) ||
74 [ + + ]: 408 : nla_put_u32(skb, MPTCP_SUBFLOW_ATTR_SSN_OFFSET, sf->ssn_offset) ||
75 [ - + ]: 240 : nla_put_u16(skb, MPTCP_SUBFLOW_ATTR_MAP_DATALEN,
76 : 240 : sf->map_data_len))) {
77 : 0 : err = -EMSGSIZE;
78 : 0 : goto nla_failure;
79 : : }
80 : :
81 : 240 : rcu_read_unlock();
82 : 240 : unlock_sock_fast(sk, slow);
83 : 240 : nla_nest_end(skb, start);
84 : 240 : return 0;
85 : :
86 : 0 : nla_failure:
87 : 0 : rcu_read_unlock();
88 : 0 : unlock_sock_fast(sk, slow);
89 : 0 : nla_nest_cancel(skb, start);
90 : 0 : return err;
91 : : }
92 : :
93 : 0 : static size_t subflow_get_info_size(const struct sock *sk, bool net_admin)
94 : : {
95 : 0 : size_t size = 0;
96 : :
97 : 0 : size += nla_total_size(0) + /* INET_ULP_INFO_MPTCP */
98 : 0 : nla_total_size(4) + /* MPTCP_SUBFLOW_ATTR_TOKEN_REM */
99 : 0 : nla_total_size(4) + /* MPTCP_SUBFLOW_ATTR_TOKEN_LOC */
100 : 0 : nla_total_size(4) + /* MPTCP_SUBFLOW_ATTR_FLAGS */
101 : 0 : nla_total_size(1) + /* MPTCP_SUBFLOW_ATTR_ID_REM */
102 : 0 : nla_total_size(1) + /* MPTCP_SUBFLOW_ATTR_ID_LOC */
103 : : 0;
104 : :
105 [ # # ]: 0 : if (net_admin)
106 : 0 : size += nla_total_size(4) + /* MPTCP_SUBFLOW_ATTR_RELWRITE_SEQ */
107 : 0 : nla_total_size_64bit(8) + /* MPTCP_SUBFLOW_ATTR_MAP_SEQ */
108 : 0 : nla_total_size(4) + /* MPTCP_SUBFLOW_ATTR_MAP_SFSEQ */
109 : 0 : nla_total_size(4) + /* MPTCP_SUBFLOW_ATTR_SSN_OFFSET */
110 : 0 : nla_total_size(2) + /* MPTCP_SUBFLOW_ATTR_MAP_DATALEN */
111 : : 0;
112 : :
113 : 0 : return size;
114 : : }
115 : :
116 : 4 : void mptcp_diag_subflow_init(struct tcp_ulp_ops *ops)
117 : : {
118 : 4 : ops->get_info = subflow_get_info;
119 : 4 : ops->get_info_size = subflow_get_info_size;
120 : 4 : }
|