]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/ipv4/udp_diag.c
udp_diag: Implement the dump-all functionality
[mv-sheeva.git] / net / ipv4 / udp_diag.c
1 /*
2  * udp_diag.c   Module for monitoring UDP transport protocols sockets.
3  *
4  * Authors:     Pavel Emelyanov, <xemul@parallels.com>
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License
8  *      as published by the Free Software Foundation; either version
9  *      2 of the License, or (at your option) any later version.
10  */
11
12
13 #include <linux/module.h>
14 #include <linux/inet_diag.h>
15 #include <linux/udp.h>
16 #include <net/udp.h>
17 #include <net/udplite.h>
18 #include <linux/inet_diag.h>
19 #include <linux/sock_diag.h>
20
21 static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
22                 struct netlink_callback *cb, struct inet_diag_req *req,
23                 struct nlattr *bc)
24 {
25         if (!inet_diag_bc_sk(bc, sk))
26                 return 0;
27
28         return inet_sk_diag_fill(sk, NULL, skb, req, NETLINK_CB(cb->skb).pid,
29                         cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
30 }
31
32 static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
33                 const struct nlmsghdr *nlh, struct inet_diag_req *req)
34 {
35         int err = -EINVAL;
36         struct sock *sk;
37         struct sk_buff *rep;
38
39         if (req->sdiag_family == AF_INET)
40                 sk = __udp4_lib_lookup(&init_net,
41                                 req->id.idiag_src[0], req->id.idiag_sport,
42                                 req->id.idiag_dst[0], req->id.idiag_dport,
43                                 req->id.idiag_if, tbl);
44         else if (req->sdiag_family == AF_INET6)
45                 sk = __udp6_lib_lookup(&init_net,
46                                 (struct in6_addr *)req->id.idiag_src,
47                                 req->id.idiag_sport,
48                                 (struct in6_addr *)req->id.idiag_dst,
49                                 req->id.idiag_dport,
50                                 req->id.idiag_if, tbl);
51         else
52                 goto out_nosk;
53
54         err = -ENOENT;
55         if (sk == NULL)
56                 goto out_nosk;
57
58         err = inet_diag_check_cookie(sk, req);
59         if (err)
60                 goto out;
61
62         err = -ENOMEM;
63         rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
64                                      sizeof(struct inet_diag_meminfo) +
65                                      64)), GFP_KERNEL);
66         if (!rep)
67                 goto out;
68
69         err = inet_sk_diag_fill(sk, NULL, rep, req,
70                            NETLINK_CB(in_skb).pid,
71                            nlh->nlmsg_seq, 0, nlh);
72         if (err < 0) {
73                 WARN_ON(err == -EMSGSIZE);
74                 kfree_skb(rep);
75                 goto out;
76         }
77         err = netlink_unicast(sock_diag_nlsk, rep, NETLINK_CB(in_skb).pid,
78                               MSG_DONTWAIT);
79         if (err > 0)
80                 err = 0;
81 out:
82         if (sk)
83                 sock_put(sk);
84 out_nosk:
85         return err;
86 }
87
88 static void udp_dump(struct udp_table *table, struct sk_buff *skb, struct netlink_callback *cb,
89                 struct inet_diag_req *r, struct nlattr *bc)
90 {
91         int num, s_num, slot, s_slot;
92
93         s_slot = cb->args[0];
94         num = s_num = cb->args[1];
95
96         for (slot = s_slot; slot <= table->mask; num = s_num = 0, slot++) {
97                 struct sock *sk;
98                 struct hlist_nulls_node *node;
99                 struct udp_hslot *hslot = &table->hash[slot];
100
101                 if (hlist_nulls_empty(&hslot->head))
102                         continue;
103
104                 spin_lock_bh(&hslot->lock);
105                 sk_nulls_for_each(sk, node, &hslot->head) {
106                         struct inet_sock *inet = inet_sk(sk);
107
108                         if (num < s_num)
109                                 goto next;
110                         if (!(r->idiag_states & (1 << sk->sk_state)))
111                                 goto next;
112                         if (r->sdiag_family != AF_UNSPEC &&
113                                         sk->sk_family != r->sdiag_family)
114                                 goto next;
115                         if (r->id.idiag_sport != inet->inet_sport &&
116                             r->id.idiag_sport)
117                                 goto next;
118                         if (r->id.idiag_dport != inet->inet_dport &&
119                             r->id.idiag_dport)
120                                 goto next;
121
122                         if (sk_diag_dump(sk, skb, cb, r, bc) < 0) {
123                                 spin_unlock_bh(&hslot->lock);
124                                 goto done;
125                         }
126 next:
127                         num++;
128                 }
129                 spin_unlock_bh(&hslot->lock);
130         }
131 done:
132         cb->args[0] = slot;
133         cb->args[1] = num;
134 }
135
136 static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
137                 struct inet_diag_req *r, struct nlattr *bc)
138 {
139         udp_dump(&udp_table, skb, cb, r, bc);
140 }
141
142 static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
143                 struct inet_diag_req *req)
144 {
145         return udp_dump_one(&udp_table, in_skb, nlh, req);
146 }
147
148 static const struct inet_diag_handler udp_diag_handler = {
149         .dump            = udp_diag_dump,
150         .dump_one        = udp_diag_dump_one,
151         .idiag_type      = IPPROTO_UDP,
152 };
153
154 static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
155                 struct inet_diag_req *r, struct nlattr *bc)
156 {
157         udp_dump(&udplite_table, skb, cb, r, bc);
158 }
159
160 static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
161                 struct inet_diag_req *req)
162 {
163         return udp_dump_one(&udplite_table, in_skb, nlh, req);
164 }
165
166 static const struct inet_diag_handler udplite_diag_handler = {
167         .dump            = udplite_diag_dump,
168         .dump_one        = udplite_diag_dump_one,
169         .idiag_type      = IPPROTO_UDPLITE,
170 };
171
172 static int __init udp_diag_init(void)
173 {
174         int err;
175
176         err = inet_diag_register(&udp_diag_handler);
177         if (err)
178                 goto out;
179         err = inet_diag_register(&udplite_diag_handler);
180         if (err)
181                 goto out_lite;
182 out:
183         return err;
184 out_lite:
185         inet_diag_unregister(&udp_diag_handler);
186         goto out;
187 }
188
189 static void __exit udp_diag_exit(void)
190 {
191         inet_diag_unregister(&udplite_diag_handler);
192         inet_diag_unregister(&udp_diag_handler);
193 }
194
195 module_init(udp_diag_init);
196 module_exit(udp_diag_exit);
197 MODULE_LICENSE("GPL");
198 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 17);
199 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 136);