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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/sock_diag.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/unix_diag.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/uidgid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <net/af_unix.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <net/tcp_states.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	/* might or might not have unix_table_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	struct unix_address *addr = smp_load_acquire(&unix_sk(sk)->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	if (!addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	return nla_put(nlskb, UNIX_DIAG_NAME, addr->len - sizeof(short),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 		       addr->name->sun_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static int sk_diag_dump_vfs(struct sock *sk, struct sk_buff *nlskb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct dentry *dentry = unix_sk(sk)->path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	if (dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		struct unix_diag_vfs uv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 			.udiag_vfs_ino = d_backing_inode(dentry)->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 			.udiag_vfs_dev = dentry->d_sb->s_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		return nla_put(nlskb, UNIX_DIAG_VFS, sizeof(uv), &uv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static int sk_diag_dump_peer(struct sock *sk, struct sk_buff *nlskb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct sock *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	int ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	peer = unix_peer_get(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		unix_state_lock(peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		ino = sock_i_ino(peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		unix_state_unlock(peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		sock_put(peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return nla_put_u32(nlskb, UNIX_DIAG_PEER, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static int sk_diag_dump_icons(struct sock *sk, struct sk_buff *nlskb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct nlattr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	u32 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (sk->sk_state == TCP_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		spin_lock(&sk->sk_receive_queue.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		attr = nla_reserve(nlskb, UNIX_DIAG_ICONS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 				   sk->sk_receive_queue.qlen * sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		if (!attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		buf = nla_data(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		skb_queue_walk(&sk->sk_receive_queue, skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			struct sock *req, *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			req = skb->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			 * The state lock is outer for the same sk's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			 * queue lock. With the other's queue locked it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			 * OK to lock the state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			unix_state_lock_nested(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			peer = unix_sk(req)->peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			buf[i++] = (peer ? sock_i_ino(peer) : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			unix_state_unlock(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		spin_unlock(&sk->sk_receive_queue.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	spin_unlock(&sk->sk_receive_queue.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int sk_diag_show_rqlen(struct sock *sk, struct sk_buff *nlskb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	struct unix_diag_rqlen rql;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (sk->sk_state == TCP_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		rql.udiag_rqueue = sk->sk_receive_queue.qlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		rql.udiag_wqueue = sk->sk_max_ack_backlog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		rql.udiag_rqueue = (u32) unix_inq_len(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		rql.udiag_wqueue = (u32) unix_outq_len(sk);
^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) 	return nla_put(nlskb, UNIX_DIAG_RQLEN, sizeof(rql), &rql);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int sk_diag_dump_uid(struct sock *sk, struct sk_buff *nlskb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	uid_t uid = from_kuid_munged(sk_user_ns(nlskb->sk), sock_i_uid(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return nla_put(nlskb, UNIX_DIAG_UID, sizeof(uid_t), &uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		u32 portid, u32 seq, u32 flags, int sk_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct unix_diag_msg *rep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (!nlh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	rep = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	rep->udiag_family = AF_UNIX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	rep->udiag_type = sk->sk_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	rep->udiag_state = sk->sk_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	rep->pad = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	rep->udiag_ino = sk_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	sock_diag_save_cookie(sk, rep->udiag_cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if ((req->udiag_show & UDIAG_SHOW_NAME) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	    sk_diag_dump_name(sk, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		goto out_nlmsg_trim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if ((req->udiag_show & UDIAG_SHOW_VFS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	    sk_diag_dump_vfs(sk, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		goto out_nlmsg_trim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if ((req->udiag_show & UDIAG_SHOW_PEER) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	    sk_diag_dump_peer(sk, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		goto out_nlmsg_trim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if ((req->udiag_show & UDIAG_SHOW_ICONS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	    sk_diag_dump_icons(sk, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		goto out_nlmsg_trim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if ((req->udiag_show & UDIAG_SHOW_RQLEN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	    sk_diag_show_rqlen(sk, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		goto out_nlmsg_trim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if ((req->udiag_show & UDIAG_SHOW_MEMINFO) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	    sock_diag_put_meminfo(sk, skb, UNIX_DIAG_MEMINFO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		goto out_nlmsg_trim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	if (nla_put_u8(skb, UNIX_DIAG_SHUTDOWN, sk->sk_shutdown))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		goto out_nlmsg_trim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if ((req->udiag_show & UDIAG_SHOW_UID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	    sk_diag_dump_uid(sk, skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		goto out_nlmsg_trim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	nlmsg_end(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) out_nlmsg_trim:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	nlmsg_cancel(skb, nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		u32 portid, u32 seq, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	int sk_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	unix_state_lock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	sk_ino = sock_i_ino(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	unix_state_unlock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (!sk_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	return sk_diag_fill(sk, skb, req, portid, seq, flags, sk_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static int unix_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	struct unix_diag_req *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	int num, s_num, slot, s_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	req = nlmsg_data(cb->nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	s_slot = cb->args[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	num = s_num = cb->args[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	spin_lock(&unix_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	for (slot = s_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	     slot < ARRAY_SIZE(unix_socket_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	     s_num = 0, slot++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		sk_for_each(sk, &unix_socket_table[slot]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			if (!net_eq(sock_net(sk), net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			if (num < s_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 				goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			if (!(req->udiag_states & (1 << sk->sk_state)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 				goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			if (sk_diag_dump(sk, skb, req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 					 NETLINK_CB(cb->skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 					 cb->nlh->nlmsg_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 					 NLM_F_MULTI) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 				goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			num++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	spin_unlock(&unix_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	cb->args[0] = slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	cb->args[1] = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static struct sock *unix_lookup_by_ino(unsigned int ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	spin_lock(&unix_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	for (i = 0; i < ARRAY_SIZE(unix_socket_table); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		sk_for_each(sk, &unix_socket_table[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			if (ino == sock_i_ino(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 				sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 				spin_unlock(&unix_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				return sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	spin_unlock(&unix_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static int unix_diag_get_exact(struct sk_buff *in_skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			       const struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			       struct unix_diag_req *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	struct sk_buff *rep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	unsigned int extra_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	struct net *net = sock_net(in_skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (req->udiag_ino == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		goto out_nosk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	sk = unix_lookup_by_ino(req->udiag_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	if (sk == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		goto out_nosk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if (!net_eq(sock_net(sk), net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	err = sock_diag_check_cookie(sk, req->udiag_cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	extra_len = 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	rep = nlmsg_new(sizeof(struct unix_diag_msg) + extra_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (!rep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	err = sk_diag_fill(sk, rep, req, NETLINK_CB(in_skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			   nlh->nlmsg_seq, 0, req->udiag_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		nlmsg_free(rep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		extra_len += 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		if (extra_len >= PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			      MSG_DONTWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	if (sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) out_nosk:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static int unix_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	int hdrlen = sizeof(struct unix_diag_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (nlmsg_len(h) < hdrlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	if (h->nlmsg_flags & NLM_F_DUMP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		struct netlink_dump_control c = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			.dump = unix_diag_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		return netlink_dump_start(net->diag_nlsk, skb, h, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		return unix_diag_get_exact(skb, h, nlmsg_data(h));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static const struct sock_diag_handler unix_diag_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	.family = AF_UNIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	.dump = unix_diag_handler_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static int __init unix_diag_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	return sock_diag_register(&unix_diag_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static void __exit unix_diag_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	sock_diag_unregister(&unix_diag_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) module_init(unix_diag_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) module_exit(unix_diag_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 1 /* AF_LOCAL */);