]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/unix/diag.c
unix_diag: Dumping all sockets core
[karo-tx-linux.git] / net / unix / diag.c
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>
9
10 #define UNIX_DIAG_PUT(skb, attrtype, attrlen) \
11         RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
12
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)
15 {
16         unsigned char *b = skb_tail_pointer(skb);
17         struct nlmsghdr *nlh;
18         struct unix_diag_msg *rep;
19
20         nlh = NLMSG_PUT(skb, pid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep));
21         nlh->nlmsg_flags = flags;
22
23         rep = NLMSG_DATA(nlh);
24
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);
30
31         nlh->nlmsg_len = skb_tail_pointer(skb) - b;
32         return skb->len;
33
34 nlmsg_failure:
35         nlmsg_trim(skb, b);
36         return -EMSGSIZE;
37 }
38
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)
41 {
42         int sk_ino;
43
44         unix_state_lock(sk);
45         sk_ino = sock_i_ino(sk);
46         unix_state_unlock(sk);
47
48         if (!sk_ino)
49                 return 0;
50
51         return sk_diag_fill(sk, skb, req, pid, seq, flags, sk_ino);
52 }
53
54 static int unix_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
55 {
56         struct unix_diag_req *req;
57         int num, s_num, slot, s_slot;
58
59         req = NLMSG_DATA(cb->nlh);
60
61         s_slot = cb->args[0];
62         num = s_num = cb->args[1];
63
64         spin_lock(&unix_table_lock);
65         for (slot = s_slot; slot <= UNIX_HASH_SIZE; s_num = 0, slot++) {
66                 struct sock *sk;
67                 struct hlist_node *node;
68
69                 num = 0;
70                 sk_for_each(sk, node, &unix_socket_table[slot]) {
71                         if (num < s_num)
72                                 goto next;
73                         if (!(req->udiag_states & (1 << sk->sk_state)))
74                                 goto next;
75                         if (sk_diag_dump(sk, skb, req,
76                                                 NETLINK_CB(cb->skb).pid,
77                                                 cb->nlh->nlmsg_seq,
78                                                 NLM_F_MULTI) < 0)
79                                 goto done;
80 next:
81                         num++;
82                 }
83         }
84 done:
85         spin_unlock(&unix_table_lock);
86         cb->args[0] = slot;
87         cb->args[1] = num;
88
89         return skb->len;
90 }
91
92 static int unix_diag_get_exact(struct sk_buff *in_skb,
93                                const struct nlmsghdr *nlh,
94                                struct unix_diag_req *req)
95 {
96         return -EAFNOSUPPORT;
97 }
98
99 static int unix_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
100 {
101         int hdrlen = sizeof(struct unix_diag_req);
102
103         if (nlmsg_len(h) < hdrlen)
104                 return -EINVAL;
105
106         if (h->nlmsg_flags & NLM_F_DUMP)
107                 return netlink_dump_start(sock_diag_nlsk, skb, h,
108                                           unix_diag_dump, NULL, 0);
109         else
110                 return unix_diag_get_exact(skb, h, (struct unix_diag_req *)NLMSG_DATA(h));
111 }
112
113 static struct sock_diag_handler unix_diag_handler = {
114         .family = AF_UNIX,
115         .dump = unix_diag_handler_dump,
116 };
117
118 static int __init unix_diag_init(void)
119 {
120         return sock_diag_register(&unix_diag_handler);
121 }
122
123 static void __exit unix_diag_exit(void)
124 {
125         sock_diag_unregister(&unix_diag_handler);
126 }
127
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 */);