blob: 3bd510941da0b2665709ced910b43a274562f329 [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
14#include <linux/config.h>
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/fcntl.h>
18#include <linux/random.h>
19#include <linux/cache.h>
20#include <linux/init.h>
21#include <linux/time.h>
22
23#include <net/icmp.h>
24#include <net/tcp.h>
25#include <net/ipv6.h>
26#include <net/inet_common.h>
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -030027#include <net/inet_connection_sock.h>
28#include <net/inet_hashtables.h>
29#include <net/inet_timewait_sock.h>
30#include <net/inet6_hashtables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <linux/inet.h>
33#include <linux/stddef.h>
34
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -030035#include <linux/inet_diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030037static const struct inet_diag_handler **inet_diag_table;
38
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030039struct inet_diag_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 u32 *saddr;
41 u32 *daddr;
42 u16 sport;
43 u16 dport;
44 u16 family;
45 u16 userlocks;
46};
47
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030048static struct sock *idiagnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030050#define INET_DIAG_PUT(skb, attrtype, attrlen) \
Stephen Hemminger7c99c902005-06-23 12:20:36 -070051 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030053static int inet_diag_fill(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -030054 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);
73 if (sk->sk_state != TCP_TIME_WAIT) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030074 if (ext & (1 << (INET_DIAG_MEMINFO - 1)))
75 minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO,
76 sizeof(*minfo));
77 if (ext & (1 << (INET_DIAG_INFO - 1)))
78 info = INET_DIAG_PUT(skb, INET_DIAG_INFO,
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030079 handler->idiag_info_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030081 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030082 size_t len = strlen(icsk->icsk_ca_ops->name);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030083 strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1),
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -030084 icsk->icsk_ca_ops->name);
Stephen Hemminger056ede62005-06-23 12:21:28 -070085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030087 r->idiag_family = sk->sk_family;
88 r->idiag_state = sk->sk_state;
89 r->idiag_timer = 0;
90 r->idiag_retrans = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030092 r->id.idiag_if = sk->sk_bound_dev_if;
93 r->id.idiag_cookie[0] = (u32)(unsigned long)sk;
94 r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030096 if (r->idiag_state == TCP_TIME_WAIT) {
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070097 const struct inet_timewait_sock *tw = inet_twsk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 long tmo = tw->tw_ttd - jiffies;
99 if (tmo < 0)
100 tmo = 0;
101
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300102 r->id.idiag_sport = tw->tw_sport;
103 r->id.idiag_dport = tw->tw_dport;
104 r->id.idiag_src[0] = tw->tw_rcv_saddr;
105 r->id.idiag_dst[0] = tw->tw_daddr;
106 r->idiag_state = tw->tw_substate;
107 r->idiag_timer = 3;
108 r->idiag_expires = (tmo * 1000 + HZ - 1) / HZ;
109 r->idiag_rqueue = 0;
110 r->idiag_wqueue = 0;
111 r->idiag_uid = 0;
112 r->idiag_inode = 0;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300113#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300114 if (r->idiag_family == AF_INET6) {
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700115 const struct tcp6_timewait_sock *tcp6tw = tcp6_twsk(sk);
116
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300117 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700118 &tcp6tw->tw_v6_rcv_saddr);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300119 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700120 &tcp6tw->tw_v6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122#endif
123 nlh->nlmsg_len = skb->tail - b;
124 return skb->len;
125 }
126
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300127 r->id.idiag_sport = inet->sport;
128 r->id.idiag_dport = inet->dport;
129 r->id.idiag_src[0] = inet->rcv_saddr;
130 r->id.idiag_dst[0] = inet->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300132#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300133 if (r->idiag_family == AF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct ipv6_pinfo *np = inet6_sk(sk);
135
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300136 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 &np->rcv_saddr);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300138 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 &np->daddr);
140 }
141#endif
142
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300143#define EXPIRES_IN_MS(tmo) ((tmo - jiffies) * 1000 + HZ - 1) / HZ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700145 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300146 r->idiag_timer = 1;
147 r->idiag_retrans = icsk->icsk_retransmits;
148 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700149 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300150 r->idiag_timer = 4;
151 r->idiag_retrans = icsk->icsk_probes_out;
152 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300154 r->idiag_timer = 2;
155 r->idiag_retrans = icsk->icsk_probes_out;
156 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300158 r->idiag_timer = 0;
159 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161#undef EXPIRES_IN_MS
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300162
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300163 r->idiag_uid = sock_i_uid(sk);
164 r->idiag_inode = sock_i_ino(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 if (minfo) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300167 minfo->idiag_rmem = atomic_read(&sk->sk_rmem_alloc);
168 minfo->idiag_wmem = sk->sk_wmem_queued;
169 minfo->idiag_fmem = sk->sk_forward_alloc;
170 minfo->idiag_tmem = atomic_read(&sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300173 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300175 if (sk->sk_state < TCP_TIME_WAIT &&
176 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
177 icsk->icsk_ca_ops->get_info(sk, ext, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 nlh->nlmsg_len = skb->tail - b;
180 return skb->len;
181
Stephen Hemminger7c99c902005-06-23 12:20:36 -0700182rtattr_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183nlmsg_failure:
184 skb_trim(skb, b - skb->data);
185 return -1;
186}
187
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300188static int inet_diag_get_exact(struct sk_buff *in_skb, const struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 int err;
191 struct sock *sk;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300192 struct inet_diag_req *req = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct sk_buff *rep;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300194 struct inet_hashinfo *hashinfo;
195 const struct inet_diag_handler *handler;
196
197 handler = inet_diag_table[nlh->nlmsg_type];
198 BUG_ON(handler == NULL);
199 hashinfo = handler->idiag_hashinfo;
200
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300201 if (req->idiag_family == AF_INET) {
202 sk = inet_lookup(hashinfo, req->id.idiag_dst[0],
203 req->id.idiag_dport, req->id.idiag_src[0],
204 req->id.idiag_sport, req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300206#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300207 else if (req->idiag_family == AF_INET6) {
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300208 sk = inet6_lookup(hashinfo,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300209 (struct in6_addr *)req->id.idiag_dst,
210 req->id.idiag_dport,
211 (struct in6_addr *)req->id.idiag_src,
212 req->id.idiag_sport,
213 req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215#endif
216 else {
217 return -EINVAL;
218 }
219
220 if (sk == NULL)
221 return -ENOENT;
222
223 err = -ESTALE;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300224 if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE ||
225 req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) &&
226 ((u32)(unsigned long)sk != req->id.idiag_cookie[0] ||
227 (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto out;
229
230 err = -ENOMEM;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300231 rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
232 sizeof(struct inet_diag_meminfo) +
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300233 handler->idiag_info_size + 64)),
234 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (!rep)
236 goto out;
237
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300238 if (inet_diag_fill(rep, sk, req->idiag_ext,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 NETLINK_CB(in_skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300240 nlh->nlmsg_seq, 0, nlh) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 BUG();
242
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300243 err = netlink_unicast(idiagnl, rep, NETLINK_CB(in_skb).pid,
244 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (err > 0)
246 err = 0;
247
248out:
249 if (sk) {
250 if (sk->sk_state == TCP_TIME_WAIT)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700251 inet_twsk_put((struct inet_timewait_sock *)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 else
253 sock_put(sk);
254 }
255 return err;
256}
257
258static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
259{
260 int words = bits >> 5;
261
262 bits &= 0x1f;
263
264 if (words) {
265 if (memcmp(a1, a2, words << 2))
266 return 0;
267 }
268 if (bits) {
269 __u32 w1, w2;
270 __u32 mask;
271
272 w1 = a1[words];
273 w2 = a2[words];
274
275 mask = htonl((0xffffffff) << (32 - bits));
276
277 if ((w1 ^ w2) & mask)
278 return 0;
279 }
280
281 return 1;
282}
283
284
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300285static int inet_diag_bc_run(const void *bc, int len,
286 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 while (len > 0) {
289 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300290 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300293 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300295 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 yes = 0;
297 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300298 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 yes = entry->sport >= op[1].no;
300 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300301 case INET_DIAG_BC_S_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 yes = entry->dport <= op[1].no;
303 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300304 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 yes = entry->dport >= op[1].no;
306 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300307 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 yes = entry->dport <= op[1].no;
309 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300310 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
312 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300313 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300314 case INET_DIAG_BC_D_COND: {
315 struct inet_diag_hostcond *cond;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 u32 *addr;
317
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300318 cond = (struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300320 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 entry->sport : entry->dport)) {
322 yes = 0;
323 break;
324 }
325
326 if (cond->prefix_len == 0)
327 break;
328
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300329 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 addr = entry->saddr;
331 else
332 addr = entry->daddr;
333
334 if (bitstring_match(addr, cond->addr, cond->prefix_len))
335 break;
336 if (entry->family == AF_INET6 &&
337 cond->family == AF_INET) {
338 if (addr[0] == 0 && addr[1] == 0 &&
339 addr[2] == htonl(0xffff) &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300340 bitstring_match(addr + 3, cond->addr,
341 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 break;
343 }
344 yes = 0;
345 break;
346 }
347 }
348
349 if (yes) {
350 len -= op->yes;
351 bc += op->yes;
352 } else {
353 len -= op->no;
354 bc += op->no;
355 }
356 }
357 return (len == 0);
358}
359
360static int valid_cc(const void *bc, int len, int cc)
361{
362 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300363 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 if (cc > len)
366 return 0;
367 if (cc == len)
368 return 1;
369 if (op->yes < 4)
370 return 0;
371 len -= op->yes;
372 bc += op->yes;
373 }
374 return 0;
375}
376
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300377static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 const unsigned char *bc = bytecode;
380 int len = bytecode_len;
381
382 while (len > 0) {
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300383 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385//printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
386 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300387 case INET_DIAG_BC_AUTO:
388 case INET_DIAG_BC_S_COND:
389 case INET_DIAG_BC_D_COND:
390 case INET_DIAG_BC_S_GE:
391 case INET_DIAG_BC_S_LE:
392 case INET_DIAG_BC_D_GE:
393 case INET_DIAG_BC_D_LE:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300394 if (op->yes < 4 || op->yes > len + 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return -EINVAL;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300396 case INET_DIAG_BC_JMP:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300397 if (op->no < 4 || op->no > len + 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -EINVAL;
399 if (op->no < len &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300400 !valid_cc(bytecode, bytecode_len, len - op->no))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return -EINVAL;
402 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300403 case INET_DIAG_BC_NOP:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300404 if (op->yes < 4 || op->yes > len + 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return -EINVAL;
406 break;
407 default:
408 return -EINVAL;
409 }
410 bc += op->yes;
411 len -= op->yes;
412 }
413 return len == 0 ? 0 : -EINVAL;
414}
415
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300416static int inet_diag_dump_sock(struct sk_buff *skb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 struct netlink_callback *cb)
418{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300419 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300422 struct inet_diag_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct rtattr *bc = (struct rtattr *)(r + 1);
424 struct inet_sock *inet = inet_sk(sk);
425
426 entry.family = sk->sk_family;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300427#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (entry.family == AF_INET6) {
429 struct ipv6_pinfo *np = inet6_sk(sk);
430
431 entry.saddr = np->rcv_saddr.s6_addr32;
432 entry.daddr = np->daddr.s6_addr32;
433 } else
434#endif
435 {
436 entry.saddr = &inet->rcv_saddr;
437 entry.daddr = &inet->daddr;
438 }
439 entry.sport = inet->num;
440 entry.dport = ntohs(inet->dport);
441 entry.userlocks = sk->sk_userlocks;
442
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300443 if (!inet_diag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return 0;
445 }
446
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300447 return inet_diag_fill(skb, sk, r->idiag_ext, NETLINK_CB(cb->skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300448 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300451static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700452 struct request_sock *req,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300453 u32 pid, u32 seq,
454 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700456 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct inet_sock *inet = inet_sk(sk);
458 unsigned char *b = skb->tail;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300459 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct nlmsghdr *nlh;
461 long tmo;
462
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300463 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 nlh->nlmsg_flags = NLM_F_MULTI;
465 r = NLMSG_DATA(nlh);
466
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300467 r->idiag_family = sk->sk_family;
468 r->idiag_state = TCP_SYN_RECV;
469 r->idiag_timer = 1;
470 r->idiag_retrans = req->retrans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300472 r->id.idiag_if = sk->sk_bound_dev_if;
473 r->id.idiag_cookie[0] = (u32)(unsigned long)req;
474 r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 tmo = req->expires - jiffies;
477 if (tmo < 0)
478 tmo = 0;
479
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300480 r->id.idiag_sport = inet->sport;
481 r->id.idiag_dport = ireq->rmt_port;
482 r->id.idiag_src[0] = ireq->loc_addr;
483 r->id.idiag_dst[0] = ireq->rmt_addr;
484 r->idiag_expires = jiffies_to_msecs(tmo);
485 r->idiag_rqueue = 0;
486 r->idiag_wqueue = 0;
487 r->idiag_uid = sock_i_uid(sk);
488 r->idiag_inode = 0;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300489#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300490 if (r->idiag_family == AF_INET6) {
491 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700492 &tcp6_rsk(req)->loc_addr);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300493 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700494 &tcp6_rsk(req)->rmt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496#endif
497 nlh->nlmsg_len = skb->tail - b;
498
499 return skb->len;
500
501nlmsg_failure:
502 skb_trim(skb, b - skb->data);
503 return -1;
504}
505
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300506static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct netlink_callback *cb)
508{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300509 struct inet_diag_entry entry;
510 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700511 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo2ad69c52005-06-18 22:48:55 -0700512 struct listen_sock *lopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct rtattr *bc = NULL;
514 struct inet_sock *inet = inet_sk(sk);
515 int j, s_j;
516 int reqnum, s_reqnum;
517 int err = 0;
518
519 s_j = cb->args[3];
520 s_reqnum = cb->args[4];
521
522 if (s_j > 0)
523 s_j--;
524
525 entry.family = sk->sk_family;
526
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700527 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700529 lopt = icsk->icsk_accept_queue.listen_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (!lopt || !lopt->qlen)
531 goto out;
532
533 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
534 bc = (struct rtattr *)(r + 1);
535 entry.sport = inet->num;
536 entry.userlocks = sk->sk_userlocks;
537 }
538
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300539 for (j = s_j; j < lopt->nr_table_entries; j++) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700540 struct request_sock *req, *head = lopt->syn_table[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 reqnum = 0;
543 for (req = head; req; reqnum++, req = req->dl_next) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700544 struct inet_request_sock *ireq = inet_rsk(req);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (reqnum < s_reqnum)
547 continue;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300548 if (r->id.idiag_dport != ireq->rmt_port &&
549 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 continue;
551
552 if (bc) {
553 entry.saddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300554#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700556 tcp6_rsk(req)->loc_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700558 &ireq->loc_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 entry.daddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300560#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700562 tcp6_rsk(req)->rmt_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700564 &ireq->rmt_addr;
565 entry.dport = ntohs(ireq->rmt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300567 if (!inet_diag_bc_run(RTA_DATA(bc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 RTA_PAYLOAD(bc), &entry))
569 continue;
570 }
571
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300572 err = inet_diag_fill_req(skb, sk, req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 NETLINK_CB(cb->skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300574 cb->nlh->nlmsg_seq, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (err < 0) {
576 cb->args[3] = j + 1;
577 cb->args[4] = reqnum;
578 goto out;
579 }
580 }
581
582 s_reqnum = 0;
583 }
584
585out:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700586 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 return err;
589}
590
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300591static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 int i, num;
594 int s_i, s_num;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300595 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300596 const struct inet_diag_handler *handler;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300597 struct inet_hashinfo *hashinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300599 handler = inet_diag_table[cb->nlh->nlmsg_type];
600 BUG_ON(handler == NULL);
601 hashinfo = handler->idiag_hashinfo;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 s_i = cb->args[1];
604 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (cb->args[0] == 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300607 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300609
610 inet_listen_lock(hashinfo);
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700611 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct sock *sk;
613 struct hlist_node *node;
614
615 num = 0;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300616 sk_for_each(sk, node, &hashinfo->listening_hash[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct inet_sock *inet = inet_sk(sk);
618
619 if (num < s_num) {
620 num++;
621 continue;
622 }
623
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300624 if (r->id.idiag_sport != inet->sport &&
625 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 goto next_listen;
627
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300628 if (!(r->idiag_states & TCPF_LISTEN) ||
629 r->id.idiag_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 cb->args[3] > 0)
631 goto syn_recv;
632
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300633 if (inet_diag_dump_sock(skb, sk, cb) < 0) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300634 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 goto done;
636 }
637
638syn_recv:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300639 if (!(r->idiag_states & TCPF_SYN_RECV))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 goto next_listen;
641
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300642 if (inet_diag_dump_reqs(skb, sk, cb) < 0) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300643 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 goto done;
645 }
646
647next_listen:
648 cb->args[3] = 0;
649 cb->args[4] = 0;
650 ++num;
651 }
652
653 s_num = 0;
654 cb->args[3] = 0;
655 cb->args[4] = 0;
656 }
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300657 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658skip_listen_ht:
659 cb->args[0] = 1;
660 s_i = num = s_num = 0;
661 }
662
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300663 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return skb->len;
665
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300666 for (i = s_i; i < hashinfo->ehash_size; i++) {
667 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct sock *sk;
669 struct hlist_node *node;
670
671 if (i > s_i)
672 s_num = 0;
673
674 read_lock_bh(&head->lock);
675
676 num = 0;
677 sk_for_each(sk, node, &head->chain) {
678 struct inet_sock *inet = inet_sk(sk);
679
680 if (num < s_num)
681 goto next_normal;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300682 if (!(r->idiag_states & (1 << sk->sk_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 goto next_normal;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300684 if (r->id.idiag_sport != inet->sport &&
685 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 goto next_normal;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300687 if (r->id.idiag_dport != inet->dport && r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 goto next_normal;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300689 if (inet_diag_dump_sock(skb, sk, cb) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 read_unlock_bh(&head->lock);
691 goto done;
692 }
693next_normal:
694 ++num;
695 }
696
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300697 if (r->idiag_states & TCPF_TIME_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 sk_for_each(sk, node,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300699 &hashinfo->ehash[i + hashinfo->ehash_size].chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct inet_sock *inet = inet_sk(sk);
701
702 if (num < s_num)
703 goto next_dying;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300704 if (r->id.idiag_sport != inet->sport &&
705 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 goto next_dying;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300707 if (r->id.idiag_dport != inet->dport &&
708 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 goto next_dying;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300710 if (inet_diag_dump_sock(skb, sk, cb) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 read_unlock_bh(&head->lock);
712 goto done;
713 }
714next_dying:
715 ++num;
716 }
717 }
718 read_unlock_bh(&head->lock);
719 }
720
721done:
722 cb->args[1] = i;
723 cb->args[2] = num;
724 return skb->len;
725}
726
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300727static int inet_diag_dump_done(struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 return 0;
730}
731
732
733static __inline__ int
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300734inet_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
737 return 0;
738
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300739 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 goto err_inval;
741
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300742 if (inet_diag_table[nlh->nlmsg_type] == NULL)
743 return -ENOENT;
744
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300745 if (NLMSG_LENGTH(sizeof(struct inet_diag_req)) > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 goto err_inval;
747
748 if (nlh->nlmsg_flags&NLM_F_DUMP) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300749 if (nlh->nlmsg_len >
750 (4 + NLMSG_SPACE(sizeof(struct inet_diag_req)))) {
751 struct rtattr *rta = (void *)(NLMSG_DATA(nlh) +
752 sizeof(struct inet_diag_req));
753 if (rta->rta_type != INET_DIAG_REQ_BYTECODE ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 rta->rta_len < 8 ||
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300755 rta->rta_len >
756 (nlh->nlmsg_len -
757 NLMSG_SPACE(sizeof(struct inet_diag_req))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 goto err_inval;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300759 if (inet_diag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 goto err_inval;
761 }
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300762 return netlink_dump_start(idiagnl, skb, nlh,
763 inet_diag_dump,
764 inet_diag_dump_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300766 return inet_diag_get_exact(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
769err_inval:
770 return -EINVAL;
771}
772
773
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300774static inline void inet_diag_rcv_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 int err;
777 struct nlmsghdr * nlh;
778
779 if (skb->len >= NLMSG_SPACE(0)) {
780 nlh = (struct nlmsghdr *)skb->data;
781 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
782 return;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300783 err = inet_diag_rcv_msg(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (err || nlh->nlmsg_flags & NLM_F_ACK)
785 netlink_ack(skb, nlh, err);
786 }
787}
788
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300789static void inet_diag_rcv(struct sock *sk, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700792 unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700794 while (qlen-- && (skb = skb_dequeue(&sk->sk_receive_queue))) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300795 inet_diag_rcv_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 kfree_skb(skb);
797 }
798}
799
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300800static void tcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300801 void *_info)
802{
803 const struct tcp_sock *tp = tcp_sk(sk);
804 struct tcp_info *info = _info;
805
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300806 r->idiag_rqueue = tp->rcv_nxt - tp->copied_seq;
807 r->idiag_wqueue = tp->write_seq - tp->snd_una;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300808 if (info != NULL)
809 tcp_get_info(sk, info);
810}
811
812static struct inet_diag_handler tcp_diag_handler = {
813 .idiag_hashinfo = &tcp_hashinfo,
814 .idiag_get_info = tcp_diag_get_info,
815 .idiag_type = TCPDIAG_GETSOCK,
816 .idiag_info_size = sizeof(struct tcp_info),
817};
818
819static DEFINE_SPINLOCK(inet_diag_register_lock);
820
821int inet_diag_register(const struct inet_diag_handler *h)
822{
823 const __u16 type = h->idiag_type;
824 int err = -EINVAL;
825
826 if (type >= INET_DIAG_GETSOCK_MAX)
827 goto out;
828
829 spin_lock(&inet_diag_register_lock);
830 err = -EEXIST;
831 if (inet_diag_table[type] == NULL) {
832 inet_diag_table[type] = h;
833 err = 0;
834 }
835 spin_unlock(&inet_diag_register_lock);
836out:
837 return err;
838}
839EXPORT_SYMBOL_GPL(inet_diag_register);
840
841void inet_diag_unregister(const struct inet_diag_handler *h)
842{
843 const __u16 type = h->idiag_type;
844
845 if (type >= INET_DIAG_GETSOCK_MAX)
846 return;
847
848 spin_lock(&inet_diag_register_lock);
849 inet_diag_table[type] = NULL;
850 spin_unlock(&inet_diag_register_lock);
851
852 synchronize_rcu();
853}
854EXPORT_SYMBOL_GPL(inet_diag_unregister);
855
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300856static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300858 const int inet_diag_table_size = (INET_DIAG_GETSOCK_MAX *
859 sizeof(struct inet_diag_handler *));
860 int err = -ENOMEM;
861
862 inet_diag_table = kmalloc(inet_diag_table_size, GFP_KERNEL);
863 if (!inet_diag_table)
864 goto out;
865
866 memset(inet_diag_table, 0, inet_diag_table_size);
867
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300868 idiagnl = netlink_kernel_create(NETLINK_INET_DIAG, inet_diag_rcv,
869 THIS_MODULE);
870 if (idiagnl == NULL)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300871 goto out_free_table;
872
873 err = inet_diag_register(&tcp_diag_handler);
874 if (err)
875 goto out_sock_release;
876out:
877 return err;
878out_sock_release:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300879 sock_release(idiagnl->sk_socket);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300880out_free_table:
881 kfree(inet_diag_table);
882 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300885static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300887 sock_release(idiagnl->sk_socket);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300888 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300891module_init(inet_diag_init);
892module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893MODULE_LICENSE("GPL");