1 #include <linux/types.h>
2 #include <linux/spinlock.h>
3 #include <linux/sock_diag.h>
4 #include <linux/unix_diag.h>
5 #include <linux/skbuff.h>
6 #include <net/netlink.h>
7 #include <net/af_unix.h>
8 #include <net/tcp_states.h>
10 #define UNIX_DIAG_PUT(skb, attrtype, attrlen) \
11 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
13 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
14 u32 pid, u32 seq, u32 flags, int sk_ino)
16 unsigned char *b = skb_tail_pointer(skb);
18 struct unix_diag_msg *rep;
20 nlh = NLMSG_PUT(skb, pid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep));
21 nlh->nlmsg_flags = flags;
23 rep = NLMSG_DATA(nlh);
25 rep->udiag_family = AF_UNIX;
26 rep->udiag_type = sk->sk_type;
27 rep->udiag_state = sk->sk_state;
28 rep->udiag_ino = sk_ino;
29 sock_diag_save_cookie(sk, rep->udiag_cookie);
31 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
39 static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
40 u32 pid, u32 seq, u32 flags)
45 sk_ino = sock_i_ino(sk);
46 unix_state_unlock(sk);
51 return sk_diag_fill(sk, skb, req, pid, seq, flags, sk_ino);
54 static int unix_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
56 struct unix_diag_req *req;
57 int num, s_num, slot, s_slot;
59 req = NLMSG_DATA(cb->nlh);
62 num = s_num = cb->args[1];
64 spin_lock(&unix_table_lock);
65 for (slot = s_slot; slot <= UNIX_HASH_SIZE; s_num = 0, slot++) {
67 struct hlist_node *node;
70 sk_for_each(sk, node, &unix_socket_table[slot]) {
73 if (!(req->udiag_states & (1 << sk->sk_state)))
75 if (sk_diag_dump(sk, skb, req,
76 NETLINK_CB(cb->skb).pid,
85 spin_unlock(&unix_table_lock);
92 static int unix_diag_get_exact(struct sk_buff *in_skb,
93 const struct nlmsghdr *nlh,
94 struct unix_diag_req *req)
99 static int unix_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
101 int hdrlen = sizeof(struct unix_diag_req);
103 if (nlmsg_len(h) < hdrlen)
106 if (h->nlmsg_flags & NLM_F_DUMP)
107 return netlink_dump_start(sock_diag_nlsk, skb, h,
108 unix_diag_dump, NULL, 0);
110 return unix_diag_get_exact(skb, h, (struct unix_diag_req *)NLMSG_DATA(h));
113 static struct sock_diag_handler unix_diag_handler = {
115 .dump = unix_diag_handler_dump,
118 static int __init unix_diag_init(void)
120 return sock_diag_register(&unix_diag_handler);
123 static void __exit unix_diag_exit(void)
125 sock_diag_unregister(&unix_diag_handler);
128 module_init(unix_diag_init);
129 module_exit(unix_diag_exit);
130 MODULE_LICENSE("GPL");
131 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 1 /* AF_LOCAL */);