Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* MPTCP socket monitoring support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2020 Red Hat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Paolo Abeni <pabeni@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/inet_diag.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <uapi/linux/mptcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "protocol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 			struct netlink_callback *cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 			const struct inet_diag_req_v2 *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 			struct nlattr *bc, bool net_admin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	if (!inet_diag_bc_sk(bc, sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	return inet_sk_diag_fill(sk, inet_csk(sk), skb, cb, req, NLM_F_MULTI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 				 net_admin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static int mptcp_diag_dump_one(struct netlink_callback *cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 			       const struct inet_diag_req_v2 *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct sk_buff *in_skb = cb->skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct mptcp_sock *msk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct sk_buff *rep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	int err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	net = sock_net(in_skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	msk = mptcp_token_get_sock(net, req->id.idiag_cookie[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	if (!msk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		goto out_nosk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	rep = nlmsg_new(nla_total_size(sizeof(struct inet_diag_msg)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 			inet_diag_msg_attrs_size() +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			nla_total_size(sizeof(struct mptcp_info)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			nla_total_size(sizeof(struct inet_diag_meminfo)) + 64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (!rep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	err = inet_sk_diag_fill(sk, inet_csk(sk), rep, cb, req, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 				netlink_net_capable(in_skb, CAP_NET_ADMIN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		WARN_ON(err == -EMSGSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		kfree_skb(rep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			      MSG_DONTWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) out_nosk:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static void mptcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			    const struct inet_diag_req_v2 *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct inet_diag_dump_data *cb_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct mptcp_sock *msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	struct nlattr *bc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	cb_data = cb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	bc = cb_data->inet_diag_nla_bc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	while ((msk = mptcp_token_iter_next(net, &cb->args[0], &cb->args[1])) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	       NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		struct inet_sock *inet = (struct inet_sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		struct sock *sk = (struct sock *)msk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		if (!(r->idiag_states & (1 << sk->sk_state)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		if (r->sdiag_family != AF_UNSPEC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		    sk->sk_family != r->sdiag_family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		if (r->id.idiag_sport != inet->inet_sport &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		    r->id.idiag_sport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		if (r->id.idiag_dport != inet->inet_dport &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		    r->id.idiag_dport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		ret = sk_diag_dump(sk, skb, cb, r, bc, net_admin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			/* will retry on the same position */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			cb->args[1]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static void mptcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 				void *_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct mptcp_sock *msk = mptcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct mptcp_info *info = _info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	u32 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	bool slow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	r->idiag_rqueue = sk_rmem_alloc_get(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	r->idiag_wqueue = sk_wmem_alloc_get(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	slow = lock_sock_fast(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	info->mptcpi_subflows = READ_ONCE(msk->pm.subflows);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	info->mptcpi_add_addr_signal = READ_ONCE(msk->pm.add_addr_signaled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	info->mptcpi_add_addr_accepted = READ_ONCE(msk->pm.add_addr_accepted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	info->mptcpi_subflows_max = READ_ONCE(msk->pm.subflows_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	val = READ_ONCE(msk->pm.add_addr_signal_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	info->mptcpi_add_addr_signal_max = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	val = READ_ONCE(msk->pm.add_addr_accept_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	info->mptcpi_add_addr_accepted_max = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		flags |= MPTCP_INFO_FLAG_FALLBACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (READ_ONCE(msk->can_ack))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		flags |= MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	info->mptcpi_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	info->mptcpi_token = READ_ONCE(msk->token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	info->mptcpi_write_seq = READ_ONCE(msk->write_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	info->mptcpi_snd_una = atomic64_read(&msk->snd_una);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	info->mptcpi_rcv_nxt = READ_ONCE(msk->ack_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	unlock_sock_fast(sk, slow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static const struct inet_diag_handler mptcp_diag_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	.dump		 = mptcp_diag_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	.dump_one	 = mptcp_diag_dump_one,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	.idiag_get_info  = mptcp_diag_get_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	.idiag_type	 = IPPROTO_MPTCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	.idiag_info_size = sizeof(struct mptcp_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static int __init mptcp_diag_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	return inet_diag_register(&mptcp_diag_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static void __exit mptcp_diag_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	inet_diag_unregister(&mptcp_diag_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) module_init(mptcp_diag_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) module_exit(mptcp_diag_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-262 /* AF_INET - IPPROTO_MPTCP */);