blob: 492858e6faf0140fee625d8f966093c57c141773 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03002 * inet_diag.c Module for monitoring INET transport protocols sockets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03004 * Version: $Id: inet_diag.c,v 1.3 2002/02/01 22:01:04 davem Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/types.h>
16#include <linux/fcntl.h>
17#include <linux/random.h>
18#include <linux/cache.h>
19#include <linux/init.h>
20#include <linux/time.h>
21
22#include <net/icmp.h>
23#include <net/tcp.h>
24#include <net/ipv6.h>
25#include <net/inet_common.h>
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -030026#include <net/inet_connection_sock.h>
27#include <net/inet_hashtables.h>
28#include <net/inet_timewait_sock.h>
29#include <net/inet6_hashtables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <linux/inet.h>
32#include <linux/stddef.h>
33
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -030034#include <linux/inet_diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030036static const struct inet_diag_handler **inet_diag_table;
37
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030038struct inet_diag_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 u32 *saddr;
40 u32 *daddr;
41 u16 sport;
42 u16 dport;
43 u16 family;
44 u16 userlocks;
45};
46
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030047static struct sock *idiagnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030049#define INET_DIAG_PUT(skb, attrtype, attrlen) \
Stephen Hemminger7c99c902005-06-23 12:20:36 -070050 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -080052static int inet_csk_diag_fill(struct sock *sk,
53 struct sk_buff *skb,
54 int ext, u32 pid, u32 seq, u16 nlmsg_flags,
55 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070057 const struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070058 const struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030059 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030061 void *info = NULL;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030062 struct inet_diag_meminfo *minfo = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 unsigned char *b = skb->tail;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030064 const struct inet_diag_handler *handler;
65
66 handler = inet_diag_table[unlh->nlmsg_type];
67 BUG_ON(handler == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -030069 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 nlh->nlmsg_flags = nlmsg_flags;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 r = NLMSG_DATA(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -080073 BUG_ON(sk->sk_state == TCP_TIME_WAIT);
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -080074
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -080075 if (ext & (1 << (INET_DIAG_MEMINFO - 1)))
76 minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO, sizeof(*minfo));
77
78 if (ext & (1 << (INET_DIAG_INFO - 1)))
79 info = INET_DIAG_PUT(skb, INET_DIAG_INFO,
80 handler->idiag_info_size);
81
82 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) {
83 const size_t len = strlen(icsk->icsk_ca_ops->name);
84
85 strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1),
86 icsk->icsk_ca_ops->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -080088
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030089 r->idiag_family = sk->sk_family;
90 r->idiag_state = sk->sk_state;
91 r->idiag_timer = 0;
92 r->idiag_retrans = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030094 r->id.idiag_if = sk->sk_bound_dev_if;
95 r->id.idiag_cookie[0] = (u32)(unsigned long)sk;
96 r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030098 r->id.idiag_sport = inet->sport;
99 r->id.idiag_dport = inet->dport;
100 r->id.idiag_src[0] = inet->rcv_saddr;
101 r->id.idiag_dst[0] = inet->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300103#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300104 if (r->idiag_family == AF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct ipv6_pinfo *np = inet6_sk(sk);
106
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300107 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 &np->rcv_saddr);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300109 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 &np->daddr);
111 }
112#endif
113
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300114#define EXPIRES_IN_MS(tmo) ((tmo - jiffies) * 1000 + HZ - 1) / HZ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700116 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300117 r->idiag_timer = 1;
118 r->idiag_retrans = icsk->icsk_retransmits;
119 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700120 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300121 r->idiag_timer = 4;
122 r->idiag_retrans = icsk->icsk_probes_out;
123 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300125 r->idiag_timer = 2;
126 r->idiag_retrans = icsk->icsk_probes_out;
127 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300129 r->idiag_timer = 0;
130 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132#undef EXPIRES_IN_MS
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300133
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300134 r->idiag_uid = sock_i_uid(sk);
135 r->idiag_inode = sock_i_ino(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 if (minfo) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300138 minfo->idiag_rmem = atomic_read(&sk->sk_rmem_alloc);
139 minfo->idiag_wmem = sk->sk_wmem_queued;
140 minfo->idiag_fmem = sk->sk_forward_alloc;
141 minfo->idiag_tmem = atomic_read(&sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300144 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300146 if (sk->sk_state < TCP_TIME_WAIT &&
147 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
148 icsk->icsk_ca_ops->get_info(sk, ext, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 nlh->nlmsg_len = skb->tail - b;
151 return skb->len;
152
Stephen Hemminger7c99c902005-06-23 12:20:36 -0700153rtattr_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154nlmsg_failure:
155 skb_trim(skb, b - skb->data);
156 return -1;
157}
158
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800159static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
160 struct sk_buff *skb, int ext, u32 pid,
161 u32 seq, u16 nlmsg_flags,
162 const struct nlmsghdr *unlh)
163{
164 long tmo;
165 struct inet_diag_msg *r;
166 const unsigned char *previous_tail = skb->tail;
167 struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq,
168 unlh->nlmsg_type, sizeof(*r));
169
170 r = NLMSG_DATA(nlh);
171 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
172
173 nlh->nlmsg_flags = nlmsg_flags;
174
175 tmo = tw->tw_ttd - jiffies;
176 if (tmo < 0)
177 tmo = 0;
178
179 r->idiag_family = tw->tw_family;
180 r->idiag_state = tw->tw_state;
181 r->idiag_timer = 0;
182 r->idiag_retrans = 0;
183 r->id.idiag_if = tw->tw_bound_dev_if;
184 r->id.idiag_cookie[0] = (u32)(unsigned long)tw;
185 r->id.idiag_cookie[1] = (u32)(((unsigned long)tw >> 31) >> 1);
186 r->id.idiag_sport = tw->tw_sport;
187 r->id.idiag_dport = tw->tw_dport;
188 r->id.idiag_src[0] = tw->tw_rcv_saddr;
189 r->id.idiag_dst[0] = tw->tw_daddr;
190 r->idiag_state = tw->tw_substate;
191 r->idiag_timer = 3;
192 r->idiag_expires = (tmo * 1000 + HZ - 1) / HZ;
193 r->idiag_rqueue = 0;
194 r->idiag_wqueue = 0;
195 r->idiag_uid = 0;
196 r->idiag_inode = 0;
197#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
198 if (tw->tw_family == AF_INET6) {
199 const struct inet6_timewait_sock *tw6 =
200 inet6_twsk((struct sock *)tw);
201
202 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
203 &tw6->tw_v6_rcv_saddr);
204 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
205 &tw6->tw_v6_daddr);
206 }
207#endif
208 nlh->nlmsg_len = skb->tail - previous_tail;
209 return skb->len;
210nlmsg_failure:
211 skb_trim(skb, previous_tail - skb->data);
212 return -1;
213}
214
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800215static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
216 int ext, u32 pid, u32 seq, u16 nlmsg_flags,
217 const struct nlmsghdr *unlh)
218{
219 if (sk->sk_state == TCP_TIME_WAIT)
220 return inet_twsk_diag_fill((struct inet_timewait_sock *)sk,
221 skb, ext, pid, seq, nlmsg_flags,
222 unlh);
223 return inet_csk_diag_fill(sk, skb, ext, pid, seq, nlmsg_flags, unlh);
224}
225
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800226static int inet_diag_get_exact(struct sk_buff *in_skb,
227 const struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 int err;
230 struct sock *sk;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300231 struct inet_diag_req *req = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 struct sk_buff *rep;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300233 struct inet_hashinfo *hashinfo;
234 const struct inet_diag_handler *handler;
235
236 handler = inet_diag_table[nlh->nlmsg_type];
237 BUG_ON(handler == NULL);
238 hashinfo = handler->idiag_hashinfo;
239
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300240 if (req->idiag_family == AF_INET) {
241 sk = inet_lookup(hashinfo, req->id.idiag_dst[0],
242 req->id.idiag_dport, req->id.idiag_src[0],
243 req->id.idiag_sport, req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300245#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300246 else if (req->idiag_family == AF_INET6) {
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300247 sk = inet6_lookup(hashinfo,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300248 (struct in6_addr *)req->id.idiag_dst,
249 req->id.idiag_dport,
250 (struct in6_addr *)req->id.idiag_src,
251 req->id.idiag_sport,
252 req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254#endif
255 else {
256 return -EINVAL;
257 }
258
259 if (sk == NULL)
260 return -ENOENT;
261
262 err = -ESTALE;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300263 if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE ||
264 req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) &&
265 ((u32)(unsigned long)sk != req->id.idiag_cookie[0] ||
266 (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 goto out;
268
269 err = -ENOMEM;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300270 rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
271 sizeof(struct inet_diag_meminfo) +
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300272 handler->idiag_info_size + 64)),
273 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (!rep)
275 goto out;
276
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800277 if (sk_diag_fill(sk, rep, req->idiag_ext,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 NETLINK_CB(in_skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300279 nlh->nlmsg_seq, 0, nlh) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 BUG();
281
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300282 err = netlink_unicast(idiagnl, rep, NETLINK_CB(in_skb).pid,
283 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (err > 0)
285 err = 0;
286
287out:
288 if (sk) {
289 if (sk->sk_state == TCP_TIME_WAIT)
Arnaldo Carvalho de Melo8feaf0c2005-08-09 20:09:30 -0700290 inet_twsk_put((struct inet_timewait_sock *)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 else
292 sock_put(sk);
293 }
294 return err;
295}
296
297static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
298{
299 int words = bits >> 5;
300
301 bits &= 0x1f;
302
303 if (words) {
304 if (memcmp(a1, a2, words << 2))
305 return 0;
306 }
307 if (bits) {
308 __u32 w1, w2;
309 __u32 mask;
310
311 w1 = a1[words];
312 w2 = a2[words];
313
314 mask = htonl((0xffffffff) << (32 - bits));
315
316 if ((w1 ^ w2) & mask)
317 return 0;
318 }
319
320 return 1;
321}
322
323
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300324static int inet_diag_bc_run(const void *bc, int len,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800325 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 while (len > 0) {
328 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300329 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300332 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300334 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 yes = 0;
336 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300337 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 yes = entry->sport >= op[1].no;
339 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300340 case INET_DIAG_BC_S_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 yes = entry->dport <= op[1].no;
342 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300343 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 yes = entry->dport >= op[1].no;
345 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300346 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 yes = entry->dport <= op[1].no;
348 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300349 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
351 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300352 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300353 case INET_DIAG_BC_D_COND: {
354 struct inet_diag_hostcond *cond;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 u32 *addr;
356
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300357 cond = (struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300359 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 entry->sport : entry->dport)) {
361 yes = 0;
362 break;
363 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (cond->prefix_len == 0)
366 break;
367
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300368 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 addr = entry->saddr;
370 else
371 addr = entry->daddr;
372
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800373 if (bitstring_match(addr, cond->addr,
374 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 break;
376 if (entry->family == AF_INET6 &&
377 cond->family == AF_INET) {
378 if (addr[0] == 0 && addr[1] == 0 &&
379 addr[2] == htonl(0xffff) &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300380 bitstring_match(addr + 3, cond->addr,
381 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 break;
383 }
384 yes = 0;
385 break;
386 }
387 }
388
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800389 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 len -= op->yes;
391 bc += op->yes;
392 } else {
393 len -= op->no;
394 bc += op->no;
395 }
396 }
397 return (len == 0);
398}
399
400static int valid_cc(const void *bc, int len, int cc)
401{
402 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300403 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (cc > len)
406 return 0;
407 if (cc == len)
408 return 1;
409 if (op->yes < 4)
410 return 0;
411 len -= op->yes;
412 bc += op->yes;
413 }
414 return 0;
415}
416
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300417static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 const unsigned char *bc = bytecode;
420 int len = bytecode_len;
421
422 while (len > 0) {
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300423 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425//printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
426 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300427 case INET_DIAG_BC_AUTO:
428 case INET_DIAG_BC_S_COND:
429 case INET_DIAG_BC_D_COND:
430 case INET_DIAG_BC_S_GE:
431 case INET_DIAG_BC_S_LE:
432 case INET_DIAG_BC_D_GE:
433 case INET_DIAG_BC_D_LE:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300434 if (op->yes < 4 || op->yes > len + 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return -EINVAL;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300436 case INET_DIAG_BC_JMP:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300437 if (op->no < 4 || op->no > len + 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return -EINVAL;
439 if (op->no < len &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300440 !valid_cc(bytecode, bytecode_len, len - op->no))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return -EINVAL;
442 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300443 case INET_DIAG_BC_NOP:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300444 if (op->yes < 4 || op->yes > len + 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return -EINVAL;
446 break;
447 default:
448 return -EINVAL;
449 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800450 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 len -= op->yes;
452 }
453 return len == 0 ? 0 : -EINVAL;
454}
455
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800456static int inet_csk_diag_dump(struct sock *sk,
457 struct sk_buff *skb,
458 struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300460 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300463 struct inet_diag_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct rtattr *bc = (struct rtattr *)(r + 1);
465 struct inet_sock *inet = inet_sk(sk);
466
467 entry.family = sk->sk_family;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300468#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (entry.family == AF_INET6) {
470 struct ipv6_pinfo *np = inet6_sk(sk);
471
472 entry.saddr = np->rcv_saddr.s6_addr32;
473 entry.daddr = np->daddr.s6_addr32;
474 } else
475#endif
476 {
477 entry.saddr = &inet->rcv_saddr;
478 entry.daddr = &inet->daddr;
479 }
480 entry.sport = inet->num;
481 entry.dport = ntohs(inet->dport);
482 entry.userlocks = sk->sk_userlocks;
483
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300484 if (!inet_diag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 0;
486 }
487
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800488 return inet_csk_diag_fill(sk, skb, r->idiag_ext,
489 NETLINK_CB(cb->skb).pid,
490 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800493static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
494 struct sk_buff *skb,
495 struct netlink_callback *cb)
496{
497 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
498
499 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
500 struct inet_diag_entry entry;
501 struct rtattr *bc = (struct rtattr *)(r + 1);
502
503 entry.family = tw->tw_family;
504#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
505 if (tw->tw_family == AF_INET6) {
506 struct inet6_timewait_sock *tw6 =
507 inet6_twsk((struct sock *)tw);
508 entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
509 entry.daddr = tw6->tw_v6_daddr.s6_addr32;
510 } else
511#endif
512 {
513 entry.saddr = &tw->tw_rcv_saddr;
514 entry.daddr = &tw->tw_daddr;
515 }
516 entry.sport = tw->tw_num;
517 entry.dport = ntohs(tw->tw_dport);
518 entry.userlocks = 0;
519
520 if (!inet_diag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
521 return 0;
522 }
523
524 return inet_twsk_diag_fill(tw, skb, r->idiag_ext,
525 NETLINK_CB(cb->skb).pid,
526 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
527}
528
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300529static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800530 struct request_sock *req, u32 pid, u32 seq,
531 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700533 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct inet_sock *inet = inet_sk(sk);
535 unsigned char *b = skb->tail;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300536 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct nlmsghdr *nlh;
538 long tmo;
539
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300540 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 nlh->nlmsg_flags = NLM_F_MULTI;
542 r = NLMSG_DATA(nlh);
543
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300544 r->idiag_family = sk->sk_family;
545 r->idiag_state = TCP_SYN_RECV;
546 r->idiag_timer = 1;
547 r->idiag_retrans = req->retrans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300549 r->id.idiag_if = sk->sk_bound_dev_if;
550 r->id.idiag_cookie[0] = (u32)(unsigned long)req;
551 r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 tmo = req->expires - jiffies;
554 if (tmo < 0)
555 tmo = 0;
556
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300557 r->id.idiag_sport = inet->sport;
558 r->id.idiag_dport = ireq->rmt_port;
559 r->id.idiag_src[0] = ireq->loc_addr;
560 r->id.idiag_dst[0] = ireq->rmt_addr;
561 r->idiag_expires = jiffies_to_msecs(tmo);
562 r->idiag_rqueue = 0;
563 r->idiag_wqueue = 0;
564 r->idiag_uid = sock_i_uid(sk);
565 r->idiag_inode = 0;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300566#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300567 if (r->idiag_family == AF_INET6) {
568 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800569 &inet6_rsk(req)->loc_addr);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300570 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800571 &inet6_rsk(req)->rmt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573#endif
574 nlh->nlmsg_len = skb->tail - b;
575
576 return skb->len;
577
578nlmsg_failure:
579 skb_trim(skb, b - skb->data);
580 return -1;
581}
582
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300583static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800584 struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300586 struct inet_diag_entry entry;
587 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700588 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo2ad69c52005-06-18 22:48:55 -0700589 struct listen_sock *lopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct rtattr *bc = NULL;
591 struct inet_sock *inet = inet_sk(sk);
592 int j, s_j;
593 int reqnum, s_reqnum;
594 int err = 0;
595
596 s_j = cb->args[3];
597 s_reqnum = cb->args[4];
598
599 if (s_j > 0)
600 s_j--;
601
602 entry.family = sk->sk_family;
603
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700604 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700606 lopt = icsk->icsk_accept_queue.listen_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!lopt || !lopt->qlen)
608 goto out;
609
610 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
611 bc = (struct rtattr *)(r + 1);
612 entry.sport = inet->num;
613 entry.userlocks = sk->sk_userlocks;
614 }
615
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300616 for (j = s_j; j < lopt->nr_table_entries; j++) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700617 struct request_sock *req, *head = lopt->syn_table[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 reqnum = 0;
620 for (req = head; req; reqnum++, req = req->dl_next) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700621 struct inet_request_sock *ireq = inet_rsk(req);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (reqnum < s_reqnum)
624 continue;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300625 if (r->id.idiag_dport != ireq->rmt_port &&
626 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 continue;
628
629 if (bc) {
630 entry.saddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300631#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800633 inet6_rsk(req)->loc_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700635 &ireq->loc_addr;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800636 entry.daddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300637#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800639 inet6_rsk(req)->rmt_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700641 &ireq->rmt_addr;
642 entry.dport = ntohs(ireq->rmt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300644 if (!inet_diag_bc_run(RTA_DATA(bc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 RTA_PAYLOAD(bc), &entry))
646 continue;
647 }
648
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300649 err = inet_diag_fill_req(skb, sk, req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 NETLINK_CB(cb->skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300651 cb->nlh->nlmsg_seq, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (err < 0) {
653 cb->args[3] = j + 1;
654 cb->args[4] = reqnum;
655 goto out;
656 }
657 }
658
659 s_reqnum = 0;
660 }
661
662out:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700663 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 return err;
666}
667
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300668static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 int i, num;
671 int s_i, s_num;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300672 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300673 const struct inet_diag_handler *handler;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300674 struct inet_hashinfo *hashinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300676 handler = inet_diag_table[cb->nlh->nlmsg_type];
677 BUG_ON(handler == NULL);
678 hashinfo = handler->idiag_hashinfo;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 s_i = cb->args[1];
681 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (cb->args[0] == 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300684 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300686
687 inet_listen_lock(hashinfo);
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700688 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct sock *sk;
690 struct hlist_node *node;
691
692 num = 0;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300693 sk_for_each(sk, node, &hashinfo->listening_hash[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct inet_sock *inet = inet_sk(sk);
695
696 if (num < s_num) {
697 num++;
698 continue;
699 }
700
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300701 if (r->id.idiag_sport != inet->sport &&
702 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 goto next_listen;
704
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300705 if (!(r->idiag_states & TCPF_LISTEN) ||
706 r->id.idiag_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 cb->args[3] > 0)
708 goto syn_recv;
709
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800710 if (inet_csk_diag_dump(sk, skb, cb) < 0) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300711 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 goto done;
713 }
714
715syn_recv:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300716 if (!(r->idiag_states & TCPF_SYN_RECV))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goto next_listen;
718
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300719 if (inet_diag_dump_reqs(skb, sk, cb) < 0) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300720 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 goto done;
722 }
723
724next_listen:
725 cb->args[3] = 0;
726 cb->args[4] = 0;
727 ++num;
728 }
729
730 s_num = 0;
731 cb->args[3] = 0;
732 cb->args[4] = 0;
733 }
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300734 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735skip_listen_ht:
736 cb->args[0] = 1;
737 s_i = num = s_num = 0;
738 }
739
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300740 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return skb->len;
742
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300743 for (i = s_i; i < hashinfo->ehash_size; i++) {
744 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct sock *sk;
746 struct hlist_node *node;
747
748 if (i > s_i)
749 s_num = 0;
750
751 read_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 num = 0;
753 sk_for_each(sk, node, &head->chain) {
754 struct inet_sock *inet = inet_sk(sk);
755
756 if (num < s_num)
757 goto next_normal;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300758 if (!(r->idiag_states & (1 << sk->sk_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 goto next_normal;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300760 if (r->id.idiag_sport != inet->sport &&
761 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 goto next_normal;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800763 if (r->id.idiag_dport != inet->dport &&
764 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto next_normal;
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800766 if (inet_csk_diag_dump(sk, skb, cb) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 read_unlock_bh(&head->lock);
768 goto done;
769 }
770next_normal:
771 ++num;
772 }
773
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300774 if (r->idiag_states & TCPF_TIME_WAIT) {
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800775 struct inet_timewait_sock *tw;
776
777 inet_twsk_for_each(tw, node,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300778 &hashinfo->ehash[i + hashinfo->ehash_size].chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (num < s_num)
781 goto next_dying;
Arnaldo Carvalho de Melo7dbf0752006-01-09 14:56:03 -0800782 if (r->id.idiag_sport != tw->tw_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300783 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 goto next_dying;
Arnaldo Carvalho de Melo7dbf0752006-01-09 14:56:03 -0800785 if (r->id.idiag_dport != tw->tw_dport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300786 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto next_dying;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800788 if (inet_twsk_diag_dump(tw, skb, cb) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 read_unlock_bh(&head->lock);
790 goto done;
791 }
792next_dying:
793 ++num;
794 }
795 }
796 read_unlock_bh(&head->lock);
797 }
798
799done:
800 cb->args[1] = i;
801 cb->args[2] = num;
802 return skb->len;
803}
804
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800805static inline int inet_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
808 return 0;
809
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300810 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto err_inval;
812
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300813 if (inet_diag_table[nlh->nlmsg_type] == NULL)
814 return -ENOENT;
815
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300816 if (NLMSG_LENGTH(sizeof(struct inet_diag_req)) > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 goto err_inval;
818
819 if (nlh->nlmsg_flags&NLM_F_DUMP) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300820 if (nlh->nlmsg_len >
821 (4 + NLMSG_SPACE(sizeof(struct inet_diag_req)))) {
822 struct rtattr *rta = (void *)(NLMSG_DATA(nlh) +
823 sizeof(struct inet_diag_req));
824 if (rta->rta_type != INET_DIAG_REQ_BYTECODE ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 rta->rta_len < 8 ||
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300826 rta->rta_len >
827 (nlh->nlmsg_len -
828 NLMSG_SPACE(sizeof(struct inet_diag_req))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 goto err_inval;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300830 if (inet_diag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto err_inval;
832 }
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300833 return netlink_dump_start(idiagnl, skb, nlh,
Thomas Grafa8f74b22005-11-10 02:25:52 +0100834 inet_diag_dump, NULL);
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800835 } else
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300836 return inet_diag_get_exact(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838err_inval:
839 return -EINVAL;
840}
841
842
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300843static inline void inet_diag_rcv_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (skb->len >= NLMSG_SPACE(0)) {
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800846 int err;
847 struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
848
849 if (nlh->nlmsg_len < sizeof(*nlh) ||
850 skb->len < nlh->nlmsg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300852 err = inet_diag_rcv_msg(skb, nlh);
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800853 if (err || nlh->nlmsg_flags & NLM_F_ACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 netlink_ack(skb, nlh, err);
855 }
856}
857
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300858static void inet_diag_rcv(struct sock *sk, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700861 unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700863 while (qlen-- && (skb = skb_dequeue(&sk->sk_receive_queue))) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300864 inet_diag_rcv_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 kfree_skb(skb);
866 }
867}
868
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300869static DEFINE_SPINLOCK(inet_diag_register_lock);
870
871int inet_diag_register(const struct inet_diag_handler *h)
872{
873 const __u16 type = h->idiag_type;
874 int err = -EINVAL;
875
876 if (type >= INET_DIAG_GETSOCK_MAX)
877 goto out;
878
879 spin_lock(&inet_diag_register_lock);
880 err = -EEXIST;
881 if (inet_diag_table[type] == NULL) {
882 inet_diag_table[type] = h;
883 err = 0;
884 }
885 spin_unlock(&inet_diag_register_lock);
886out:
887 return err;
888}
889EXPORT_SYMBOL_GPL(inet_diag_register);
890
891void inet_diag_unregister(const struct inet_diag_handler *h)
892{
893 const __u16 type = h->idiag_type;
894
895 if (type >= INET_DIAG_GETSOCK_MAX)
896 return;
897
898 spin_lock(&inet_diag_register_lock);
899 inet_diag_table[type] = NULL;
900 spin_unlock(&inet_diag_register_lock);
901
902 synchronize_rcu();
903}
904EXPORT_SYMBOL_GPL(inet_diag_unregister);
905
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300906static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300908 const int inet_diag_table_size = (INET_DIAG_GETSOCK_MAX *
909 sizeof(struct inet_diag_handler *));
910 int err = -ENOMEM;
911
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700912 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300913 if (!inet_diag_table)
914 goto out;
915
Patrick McHardy06628602005-08-15 12:33:26 -0700916 idiagnl = netlink_kernel_create(NETLINK_INET_DIAG, 0, inet_diag_rcv,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300917 THIS_MODULE);
918 if (idiagnl == NULL)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300919 goto out_free_table;
Arnaldo Carvalho de Melo17b085e2005-08-12 12:59:17 -0300920 err = 0;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300921out:
922 return err;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300923out_free_table:
924 kfree(inet_diag_table);
925 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300928static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300930 sock_release(idiagnl->sk_socket);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300931 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300934module_init(inet_diag_init);
935module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936MODULE_LICENSE("GPL");