blob: 515dc7b6daea4396acbf96ae45730c311c2c4a7f [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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
Ilpo Järvinen172589c2007-08-28 15:50:33 -070012#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/fcntl.h>
16#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#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>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070030#include <net/netlink.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>
Pavel Emelyanovd3664772011-12-06 07:58:03 +000036#include <linux/sock_diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030038static const struct inet_diag_handler **inet_diag_table;
39
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030040struct inet_diag_entry {
Al Viro9f855292006-09-27 18:44:30 -070041 __be32 *saddr;
42 __be32 *daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 u16 sport;
44 u16 dport;
45 u16 family;
46 u16 userlocks;
47};
48
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +000049static struct sock *sdiagnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030051#define INET_DIAG_PUT(skb, attrtype, attrlen) \
Stephen Hemminger7c99c902005-06-23 12:20:36 -070052 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Herbert Xud523a322007-12-03 15:51:25 +110054static DEFINE_MUTEX(inet_diag_table_mutex);
55
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000056static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
Herbert Xud523a322007-12-03 15:51:25 +110057{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000058 if (!inet_diag_table[proto])
Herbert Xud523a322007-12-03 15:51:25 +110059 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000060 NETLINK_SOCK_DIAG, proto);
Herbert Xud523a322007-12-03 15:51:25 +110061
62 mutex_lock(&inet_diag_table_mutex);
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000063 if (!inet_diag_table[proto])
Herbert Xud523a322007-12-03 15:51:25 +110064 return ERR_PTR(-ENOENT);
65
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000066 return inet_diag_table[proto];
Herbert Xud523a322007-12-03 15:51:25 +110067}
68
69static inline void inet_diag_unlock_handler(
70 const struct inet_diag_handler *handler)
71{
72 mutex_unlock(&inet_diag_table_mutex);
73}
74
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -080075static int inet_csk_diag_fill(struct sock *sk,
Pavel Emelyanova029fe22011-12-06 07:59:32 +000076 struct sk_buff *skb, struct inet_diag_req *req,
77 u32 pid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -080078 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070080 const struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070081 const struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030082 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030084 void *info = NULL;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030085 struct inet_diag_meminfo *minfo = NULL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070086 unsigned char *b = skb_tail_pointer(skb);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030087 const struct inet_diag_handler *handler;
Pavel Emelyanova029fe22011-12-06 07:59:32 +000088 int ext = req->idiag_ext;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030089
Pavel Emelyanova029fe22011-12-06 07:59:32 +000090 handler = inet_diag_table[req->sdiag_protocol];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030091 BUG_ON(handler == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -030093 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 nlh->nlmsg_flags = nlmsg_flags;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 r = NLMSG_DATA(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -080097 BUG_ON(sk->sk_state == TCP_TIME_WAIT);
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -080098
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -080099 if (ext & (1 << (INET_DIAG_MEMINFO - 1)))
100 minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO, sizeof(*minfo));
101
102 if (ext & (1 << (INET_DIAG_INFO - 1)))
103 info = INET_DIAG_PUT(skb, INET_DIAG_INFO,
104 handler->idiag_info_size);
105
106 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) {
107 const size_t len = strlen(icsk->icsk_ca_ops->name);
108
109 strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1),
110 icsk->icsk_ca_ops->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800112
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300113 r->idiag_family = sk->sk_family;
114 r->idiag_state = sk->sk_state;
115 r->idiag_timer = 0;
116 r->idiag_retrans = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300118 r->id.idiag_if = sk->sk_bound_dev_if;
119 r->id.idiag_cookie[0] = (u32)(unsigned long)sk;
120 r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000122 r->id.idiag_sport = inet->inet_sport;
123 r->id.idiag_dport = inet->inet_dport;
124 r->id.idiag_src[0] = inet->inet_rcv_saddr;
125 r->id.idiag_dst[0] = inet->inet_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500127 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
128 * hence this needs to be included regardless of socket family.
129 */
130 if (ext & (1 << (INET_DIAG_TOS - 1)))
131 RTA_PUT_U8(skb, INET_DIAG_TOS, inet->tos);
132
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300133#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300134 if (r->idiag_family == AF_INET6) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000135 const struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000137 *(struct in6_addr *)r->id.idiag_src = np->rcv_saddr;
138 *(struct in6_addr *)r->id.idiag_dst = np->daddr;
Maciej Żenczykowski06236ac2011-11-07 14:23:11 +0000139 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
140 RTA_PUT_U8(skb, INET_DIAG_TCLASS, np->tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142#endif
143
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700144#define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700146 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300147 r->idiag_timer = 1;
148 r->idiag_retrans = icsk->icsk_retransmits;
149 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700150 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300151 r->idiag_timer = 4;
152 r->idiag_retrans = icsk->icsk_probes_out;
153 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300155 r->idiag_timer = 2;
156 r->idiag_retrans = icsk->icsk_probes_out;
157 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300159 r->idiag_timer = 0;
160 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162#undef EXPIRES_IN_MS
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300163
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300164 r->idiag_uid = sock_i_uid(sk);
165 r->idiag_inode = sock_i_ino(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (minfo) {
Eric Dumazet31e6d362009-06-17 19:05:41 -0700168 minfo->idiag_rmem = sk_rmem_alloc_get(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300169 minfo->idiag_wmem = sk->sk_wmem_queued;
170 minfo->idiag_fmem = sk->sk_forward_alloc;
Eric Dumazet31e6d362009-06-17 19:05:41 -0700171 minfo->idiag_tmem = sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300174 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300176 if (sk->sk_state < TCP_TIME_WAIT &&
177 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
178 icsk->icsk_ca_ops->get_info(sk, ext, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700180 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return skb->len;
182
Stephen Hemminger7c99c902005-06-23 12:20:36 -0700183rtattr_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700185 nlmsg_trim(skb, b);
Patrick McHardy26932562007-01-31 23:16:40 -0800186 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800189static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000190 struct sk_buff *skb, struct inet_diag_req *req,
191 u32 pid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800192 const struct nlmsghdr *unlh)
193{
194 long tmo;
195 struct inet_diag_msg *r;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700196 const unsigned char *previous_tail = skb_tail_pointer(skb);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800197 struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq,
198 unlh->nlmsg_type, sizeof(*r));
199
200 r = NLMSG_DATA(nlh);
201 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
202
203 nlh->nlmsg_flags = nlmsg_flags;
204
205 tmo = tw->tw_ttd - jiffies;
206 if (tmo < 0)
207 tmo = 0;
208
209 r->idiag_family = tw->tw_family;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800210 r->idiag_retrans = 0;
211 r->id.idiag_if = tw->tw_bound_dev_if;
212 r->id.idiag_cookie[0] = (u32)(unsigned long)tw;
213 r->id.idiag_cookie[1] = (u32)(((unsigned long)tw >> 31) >> 1);
214 r->id.idiag_sport = tw->tw_sport;
215 r->id.idiag_dport = tw->tw_dport;
216 r->id.idiag_src[0] = tw->tw_rcv_saddr;
217 r->id.idiag_dst[0] = tw->tw_daddr;
218 r->idiag_state = tw->tw_substate;
219 r->idiag_timer = 3;
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700220 r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800221 r->idiag_rqueue = 0;
222 r->idiag_wqueue = 0;
223 r->idiag_uid = 0;
224 r->idiag_inode = 0;
225#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
226 if (tw->tw_family == AF_INET6) {
227 const struct inet6_timewait_sock *tw6 =
228 inet6_twsk((struct sock *)tw);
229
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000230 *(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr;
231 *(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800232 }
233#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700234 nlh->nlmsg_len = skb_tail_pointer(skb) - previous_tail;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800235 return skb->len;
236nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700237 nlmsg_trim(skb, previous_tail);
Patrick McHardy26932562007-01-31 23:16:40 -0800238 return -EMSGSIZE;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800239}
240
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800241static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000242 struct inet_diag_req *r, u32 pid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800243 const struct nlmsghdr *unlh)
244{
245 if (sk->sk_state == TCP_TIME_WAIT)
246 return inet_twsk_diag_fill((struct inet_timewait_sock *)sk,
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000247 skb, r, pid, seq, nlmsg_flags,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800248 unlh);
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000249 return inet_csk_diag_fill(sk, skb, r, pid, seq, nlmsg_flags, unlh);
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800250}
251
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800252static int inet_diag_get_exact(struct sk_buff *in_skb,
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000253 const struct nlmsghdr *nlh,
254 struct inet_diag_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 int err;
257 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct sk_buff *rep;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300259 struct inet_hashinfo *hashinfo;
260 const struct inet_diag_handler *handler;
261
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000262 handler = inet_diag_lock_handler(req->sdiag_protocol);
Arnaldo Carvalho de Melo8cf8e5a2008-01-28 20:52:12 -0800263 if (IS_ERR(handler)) {
264 err = PTR_ERR(handler);
265 goto unlock;
266 }
Herbert Xud523a322007-12-03 15:51:25 +1100267
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300268 hashinfo = handler->idiag_hashinfo;
Herbert Xud523a322007-12-03 15:51:25 +1100269 err = -EINVAL;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300270
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000271 if (req->sdiag_family == AF_INET) {
Pavel Emelyanovc67499c2008-01-31 05:06:40 -0800272 sk = inet_lookup(&init_net, hashinfo, req->id.idiag_dst[0],
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300273 req->id.idiag_dport, req->id.idiag_src[0],
274 req->id.idiag_sport, req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300276#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000277 else if (req->sdiag_family == AF_INET6) {
Pavel Emelyanovd86e0da2008-01-31 05:07:21 -0800278 sk = inet6_lookup(&init_net, hashinfo,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300279 (struct in6_addr *)req->id.idiag_dst,
280 req->id.idiag_dport,
281 (struct in6_addr *)req->id.idiag_src,
282 req->id.idiag_sport,
283 req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285#endif
286 else {
Herbert Xud523a322007-12-03 15:51:25 +1100287 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
Herbert Xud523a322007-12-03 15:51:25 +1100290 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (sk == NULL)
Herbert Xud523a322007-12-03 15:51:25 +1100292 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 err = -ESTALE;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300295 if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE ||
296 req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) &&
297 ((u32)(unsigned long)sk != req->id.idiag_cookie[0] ||
298 (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 goto out;
300
301 err = -ENOMEM;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300302 rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
303 sizeof(struct inet_diag_meminfo) +
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300304 handler->idiag_info_size + 64)),
305 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!rep)
307 goto out;
308
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000309 err = sk_diag_fill(sk, rep, req,
Patrick McHardy26932562007-01-31 23:16:40 -0800310 NETLINK_CB(in_skb).pid,
311 nlh->nlmsg_seq, 0, nlh);
312 if (err < 0) {
313 WARN_ON(err == -EMSGSIZE);
314 kfree_skb(rep);
315 goto out;
316 }
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +0000317 err = netlink_unicast(sdiagnl, rep, NETLINK_CB(in_skb).pid,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300318 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (err > 0)
320 err = 0;
321
322out:
323 if (sk) {
324 if (sk->sk_state == TCP_TIME_WAIT)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700325 inet_twsk_put((struct inet_timewait_sock *)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 else
327 sock_put(sk);
328 }
Herbert Xud523a322007-12-03 15:51:25 +1100329unlock:
330 inet_diag_unlock_handler(handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return err;
332}
333
Al Viro9f855292006-09-27 18:44:30 -0700334static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 int words = bits >> 5;
337
338 bits &= 0x1f;
339
340 if (words) {
341 if (memcmp(a1, a2, words << 2))
342 return 0;
343 }
344 if (bits) {
Al Viro9f855292006-09-27 18:44:30 -0700345 __be32 w1, w2;
346 __be32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 w1 = a1[words];
349 w2 = a2[words];
350
351 mask = htonl((0xffffffff) << (32 - bits));
352
353 if ((w1 ^ w2) & mask)
354 return 0;
355 }
356
357 return 1;
358}
359
360
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300361static int inet_diag_bc_run(const void *bc, int len,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800362 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 while (len > 0) {
365 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300366 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300369 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300371 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 yes = 0;
373 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300374 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 yes = entry->sport >= op[1].no;
376 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300377 case INET_DIAG_BC_S_LE:
Roel Kluinb4ced2b2010-01-19 14:12:20 -0800378 yes = entry->sport <= op[1].no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300380 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 yes = entry->dport >= op[1].no;
382 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300383 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 yes = entry->dport <= op[1].no;
385 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300386 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
388 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300389 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300390 case INET_DIAG_BC_D_COND: {
391 struct inet_diag_hostcond *cond;
Al Viro9f855292006-09-27 18:44:30 -0700392 __be32 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300394 cond = (struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300396 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 entry->sport : entry->dport)) {
398 yes = 0;
399 break;
400 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (cond->prefix_len == 0)
403 break;
404
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300405 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 addr = entry->saddr;
407 else
408 addr = entry->daddr;
409
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800410 if (bitstring_match(addr, cond->addr,
411 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 break;
413 if (entry->family == AF_INET6 &&
414 cond->family == AF_INET) {
415 if (addr[0] == 0 && addr[1] == 0 &&
416 addr[2] == htonl(0xffff) &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300417 bitstring_match(addr + 3, cond->addr,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900418 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 break;
420 }
421 yes = 0;
422 break;
423 }
424 }
425
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800426 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 len -= op->yes;
428 bc += op->yes;
429 } else {
430 len -= op->no;
431 bc += op->no;
432 }
433 }
Eric Dumazeta02cec22010-09-22 20:43:57 +0000434 return len == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437static int valid_cc(const void *bc, int len, int cc)
438{
439 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300440 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 if (cc > len)
443 return 0;
444 if (cc == len)
445 return 1;
Eric Dumazeteeb14972011-06-17 16:25:39 -0400446 if (op->yes < 4 || op->yes & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return 0;
448 len -= op->yes;
449 bc += op->yes;
450 }
451 return 0;
452}
453
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300454static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Eric Dumazeteeb14972011-06-17 16:25:39 -0400456 const void *bc = bytecode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int len = bytecode_len;
458
459 while (len > 0) {
Eric Dumazeteeb14972011-06-17 16:25:39 -0400460 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462//printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
463 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300464 case INET_DIAG_BC_AUTO:
465 case INET_DIAG_BC_S_COND:
466 case INET_DIAG_BC_D_COND:
467 case INET_DIAG_BC_S_GE:
468 case INET_DIAG_BC_S_LE:
469 case INET_DIAG_BC_D_GE:
470 case INET_DIAG_BC_D_LE:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300471 case INET_DIAG_BC_JMP:
Eric Dumazeteeb14972011-06-17 16:25:39 -0400472 if (op->no < 4 || op->no > len + 4 || op->no & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return -EINVAL;
474 if (op->no < len &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300475 !valid_cc(bytecode, bytecode_len, len - op->no))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -EINVAL;
477 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300478 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 default:
481 return -EINVAL;
482 }
Eric Dumazeteeb14972011-06-17 16:25:39 -0400483 if (op->yes < 4 || op->yes > len + 4 || op->yes & 3)
484 return -EINVAL;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800485 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 len -= op->yes;
487 }
488 return len == 0 ? 0 : -EINVAL;
489}
490
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800491static int inet_csk_diag_dump(struct sock *sk,
492 struct sk_buff *skb,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000493 struct netlink_callback *cb,
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000494 struct inet_diag_req *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000495 const struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000497 if (bc != NULL) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300498 struct inet_diag_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct inet_sock *inet = inet_sk(sk);
500
501 entry.family = sk->sk_family;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300502#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (entry.family == AF_INET6) {
504 struct ipv6_pinfo *np = inet6_sk(sk);
505
506 entry.saddr = np->rcv_saddr.s6_addr32;
507 entry.daddr = np->daddr.s6_addr32;
508 } else
509#endif
510 {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000511 entry.saddr = &inet->inet_rcv_saddr;
512 entry.daddr = &inet->inet_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000514 entry.sport = inet->inet_num;
515 entry.dport = ntohs(inet->inet_dport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 entry.userlocks = sk->sk_userlocks;
517
Nelson Elhage22e76c82010-11-03 16:35:41 +0000518 if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return 0;
520 }
521
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000522 return inet_csk_diag_fill(sk, skb, r,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800523 NETLINK_CB(cb->skb).pid,
524 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800527static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
528 struct sk_buff *skb,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000529 struct netlink_callback *cb,
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000530 struct inet_diag_req *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000531 const struct nlattr *bc)
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800532{
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000533 if (bc != NULL) {
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800534 struct inet_diag_entry entry;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800535
536 entry.family = tw->tw_family;
537#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
538 if (tw->tw_family == AF_INET6) {
539 struct inet6_timewait_sock *tw6 =
540 inet6_twsk((struct sock *)tw);
541 entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
542 entry.daddr = tw6->tw_v6_daddr.s6_addr32;
543 } else
544#endif
545 {
546 entry.saddr = &tw->tw_rcv_saddr;
547 entry.daddr = &tw->tw_daddr;
548 }
549 entry.sport = tw->tw_num;
550 entry.dport = ntohs(tw->tw_dport);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900551 entry.userlocks = 0;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800552
Nelson Elhage22e76c82010-11-03 16:35:41 +0000553 if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry))
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800554 return 0;
555 }
556
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000557 return inet_twsk_diag_fill(tw, skb, r,
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800558 NETLINK_CB(cb->skb).pid,
559 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
560}
561
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300562static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800563 struct request_sock *req, u32 pid, u32 seq,
564 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700566 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700568 unsigned char *b = skb_tail_pointer(skb);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300569 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct nlmsghdr *nlh;
571 long tmo;
572
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300573 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 nlh->nlmsg_flags = NLM_F_MULTI;
575 r = NLMSG_DATA(nlh);
576
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300577 r->idiag_family = sk->sk_family;
578 r->idiag_state = TCP_SYN_RECV;
579 r->idiag_timer = 1;
580 r->idiag_retrans = req->retrans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300582 r->id.idiag_if = sk->sk_bound_dev_if;
583 r->id.idiag_cookie[0] = (u32)(unsigned long)req;
584 r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 tmo = req->expires - jiffies;
587 if (tmo < 0)
588 tmo = 0;
589
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000590 r->id.idiag_sport = inet->inet_sport;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300591 r->id.idiag_dport = ireq->rmt_port;
592 r->id.idiag_src[0] = ireq->loc_addr;
593 r->id.idiag_dst[0] = ireq->rmt_addr;
594 r->idiag_expires = jiffies_to_msecs(tmo);
595 r->idiag_rqueue = 0;
596 r->idiag_wqueue = 0;
597 r->idiag_uid = sock_i_uid(sk);
598 r->idiag_inode = 0;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300599#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300600 if (r->idiag_family == AF_INET6) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000601 *(struct in6_addr *)r->id.idiag_src = inet6_rsk(req)->loc_addr;
602 *(struct in6_addr *)r->id.idiag_dst = inet6_rsk(req)->rmt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700605 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 return skb->len;
608
609nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700610 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return -1;
612}
613
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300614static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000615 struct netlink_callback *cb,
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000616 struct inet_diag_req *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000617 const struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300619 struct inet_diag_entry entry;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700620 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo2ad69c52005-06-18 22:48:55 -0700621 struct listen_sock *lopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 struct inet_sock *inet = inet_sk(sk);
623 int j, s_j;
624 int reqnum, s_reqnum;
625 int err = 0;
626
627 s_j = cb->args[3];
628 s_reqnum = cb->args[4];
629
630 if (s_j > 0)
631 s_j--;
632
633 entry.family = sk->sk_family;
634
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700635 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700637 lopt = icsk->icsk_accept_queue.listen_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (!lopt || !lopt->qlen)
639 goto out;
640
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000641 if (bc != NULL) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000642 entry.sport = inet->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 entry.userlocks = sk->sk_userlocks;
644 }
645
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300646 for (j = s_j; j < lopt->nr_table_entries; j++) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700647 struct request_sock *req, *head = lopt->syn_table[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 reqnum = 0;
650 for (req = head; req; reqnum++, req = req->dl_next) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700651 struct inet_request_sock *ireq = inet_rsk(req);
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (reqnum < s_reqnum)
654 continue;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300655 if (r->id.idiag_dport != ireq->rmt_port &&
656 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 continue;
658
659 if (bc) {
660 entry.saddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300661#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800663 inet6_rsk(req)->loc_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700665 &ireq->loc_addr;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800666 entry.daddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300667#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800669 inet6_rsk(req)->rmt_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700671 &ireq->rmt_addr;
672 entry.dport = ntohs(ireq->rmt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Nelson Elhage22e76c82010-11-03 16:35:41 +0000674 if (!inet_diag_bc_run(nla_data(bc),
675 nla_len(bc), &entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 continue;
677 }
678
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300679 err = inet_diag_fill_req(skb, sk, req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 NETLINK_CB(cb->skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300681 cb->nlh->nlmsg_seq, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (err < 0) {
683 cb->args[3] = j + 1;
684 cb->args[4] = reqnum;
685 goto out;
686 }
687 }
688
689 s_reqnum = 0;
690 }
691
692out:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700693 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 return err;
696}
697
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000698static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
699 struct inet_diag_req *r, struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 int i, num;
702 int s_i, s_num;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300703 const struct inet_diag_handler *handler;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300704 struct inet_hashinfo *hashinfo;
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000705
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000706 handler = inet_diag_lock_handler(r->sdiag_protocol);
Arnaldo Carvalho de Melo8cf8e5a2008-01-28 20:52:12 -0800707 if (IS_ERR(handler))
708 goto unlock;
Herbert Xud523a322007-12-03 15:51:25 +1100709
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300710 hashinfo = handler->idiag_hashinfo;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 s_i = cb->args[1];
713 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (cb->args[0] == 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300716 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300718
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700719 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 struct sock *sk;
Eric Dumazetc25eb3b2008-11-23 17:22:55 -0800721 struct hlist_nulls_node *node;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800722 struct inet_listen_hashbucket *ilb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 num = 0;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800725 ilb = &hashinfo->listening_hash[i];
726 spin_lock_bh(&ilb->lock);
Eric Dumazetc25eb3b2008-11-23 17:22:55 -0800727 sk_nulls_for_each(sk, node, &ilb->head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct inet_sock *inet = inet_sk(sk);
729
730 if (num < s_num) {
731 num++;
732 continue;
733 }
734
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000735 if (r->sdiag_family != AF_UNSPEC &&
736 sk->sk_family != r->sdiag_family)
737 goto next_listen;
738
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000739 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300740 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 goto next_listen;
742
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300743 if (!(r->idiag_states & TCPF_LISTEN) ||
744 r->id.idiag_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 cb->args[3] > 0)
746 goto syn_recv;
747
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000748 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800749 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 goto done;
751 }
752
753syn_recv:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300754 if (!(r->idiag_states & TCPF_SYN_RECV))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 goto next_listen;
756
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000757 if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800758 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 goto done;
760 }
761
762next_listen:
763 cb->args[3] = 0;
764 cb->args[4] = 0;
765 ++num;
766 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800767 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 s_num = 0;
770 cb->args[3] = 0;
771 cb->args[4] = 0;
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773skip_listen_ht:
774 cb->args[0] = 1;
775 s_i = num = s_num = 0;
776 }
777
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300778 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
Herbert Xud523a322007-12-03 15:51:25 +1100779 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Eric Dumazetf373b532009-10-09 00:16:19 +0000781 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300782 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
David S. Miller7e3aab42008-11-21 16:39:19 -0800783 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 struct sock *sk;
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800785 struct hlist_nulls_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Andi Kleen6be547a2008-08-28 01:09:54 -0700787 num = 0;
788
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800789 if (hlist_nulls_empty(&head->chain) &&
790 hlist_nulls_empty(&head->twchain))
Andi Kleen6be547a2008-08-28 01:09:54 -0700791 continue;
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (i > s_i)
794 s_num = 0;
795
David S. Miller7e3aab42008-11-21 16:39:19 -0800796 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800797 sk_nulls_for_each(sk, node, &head->chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct inet_sock *inet = inet_sk(sk);
799
800 if (num < s_num)
801 goto next_normal;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300802 if (!(r->idiag_states & (1 << sk->sk_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 goto next_normal;
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000804 if (r->sdiag_family != AF_UNSPEC &&
805 sk->sk_family != r->sdiag_family)
806 goto next_normal;
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000807 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300808 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 goto next_normal;
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000810 if (r->id.idiag_dport != inet->inet_dport &&
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800811 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 goto next_normal;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000813 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
David S. Miller7e3aab42008-11-21 16:39:19 -0800814 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 goto done;
816 }
817next_normal:
818 ++num;
819 }
820
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300821 if (r->idiag_states & TCPF_TIME_WAIT) {
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800822 struct inet_timewait_sock *tw;
823
824 inet_twsk_for_each(tw, node,
Eric Dumazetdbca9b2752007-02-08 14:16:46 -0800825 &head->twchain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 if (num < s_num)
828 goto next_dying;
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000829 if (r->sdiag_family != AF_UNSPEC &&
830 tw->tw_family != r->sdiag_family)
831 goto next_dying;
Arnaldo Carvalho de Melo7dbf0752006-01-09 14:56:03 -0800832 if (r->id.idiag_sport != tw->tw_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300833 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 goto next_dying;
Arnaldo Carvalho de Melo7dbf0752006-01-09 14:56:03 -0800835 if (r->id.idiag_dport != tw->tw_dport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300836 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto next_dying;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000838 if (inet_twsk_diag_dump(tw, skb, cb, r, bc) < 0) {
David S. Miller7e3aab42008-11-21 16:39:19 -0800839 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 goto done;
841 }
842next_dying:
843 ++num;
844 }
845 }
David S. Miller7e3aab42008-11-21 16:39:19 -0800846 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
849done:
850 cb->args[1] = i;
851 cb->args[2] = num;
Herbert Xud523a322007-12-03 15:51:25 +1100852unlock:
853 inet_diag_unlock_handler(handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return skb->len;
855}
856
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000857static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
858{
859 struct nlattr *bc = NULL;
860 int hdrlen = sizeof(struct inet_diag_req);
861
862 if (nlmsg_attrlen(cb->nlh, hdrlen))
863 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
864
865 return __inet_diag_dump(skb, cb, (struct inet_diag_req *)NLMSG_DATA(cb->nlh), bc);
866}
867
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000868static inline int inet_diag_type2proto(int type)
869{
870 switch (type) {
871 case TCPDIAG_GETSOCK:
872 return IPPROTO_TCP;
873 case DCCPDIAG_GETSOCK:
874 return IPPROTO_DCCP;
875 default:
876 return 0;
877 }
878}
879
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000880static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *cb)
881{
882 struct inet_diag_req_compat *rc = NLMSG_DATA(cb->nlh);
883 struct inet_diag_req req;
884 struct nlattr *bc = NULL;
885 int hdrlen = sizeof(struct inet_diag_req_compat);
886
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000887 req.sdiag_family = AF_UNSPEC; /* compatibility */
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000888 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
889 req.idiag_ext = rc->idiag_ext;
890 req.idiag_states = rc->idiag_states;
891 req.id = rc->id;
892
893 if (nlmsg_attrlen(cb->nlh, hdrlen))
894 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
895
896 return __inet_diag_dump(skb, cb, &req, bc);
897}
898
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000899static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
900 const struct nlmsghdr *nlh)
901{
902 struct inet_diag_req_compat *rc = NLMSG_DATA(nlh);
903 struct inet_diag_req req;
904
905 req.sdiag_family = rc->idiag_family;
906 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
907 req.idiag_ext = rc->idiag_ext;
908 req.idiag_states = rc->idiag_states;
909 req.id = rc->id;
910
911 return inet_diag_get_exact(in_skb, nlh, &req);
912}
913
Pavel Emelyanov8d341722011-12-06 07:57:06 +0000914static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Pavel Emelyanov126fdc32011-12-06 07:58:21 +0000916 int hdrlen = sizeof(struct inet_diag_req_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Thomas Grafead592b2007-03-22 23:30:35 -0700918 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
919 nlmsg_len(nlh) < hdrlen)
920 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
David S. Millerb8f3ab42011-01-18 12:40:38 -0800922 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafead592b2007-03-22 23:30:35 -0700923 if (nlmsg_attrlen(nlh, hdrlen)) {
924 struct nlattr *attr;
925
926 attr = nlmsg_find_attr(nlh, hdrlen,
927 INET_DIAG_REQ_BYTECODE);
928 if (attr == NULL ||
929 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
930 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
931 return -EINVAL;
932 }
933
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +0000934 return netlink_dump_start(sdiagnl, skb, nlh,
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000935 inet_diag_dump_compat, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Thomas Grafead592b2007-03-22 23:30:35 -0700937
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000938 return inet_diag_get_exact_compat(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Pavel Emelyanovd3664772011-12-06 07:58:03 +0000941static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
942{
943 int hdrlen = sizeof(struct inet_diag_req);
944
945 if (nlmsg_len(h) < hdrlen)
946 return -EINVAL;
947
948 if (h->nlmsg_flags & NLM_F_DUMP) {
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000949 if (nlmsg_attrlen(h, hdrlen)) {
950 struct nlattr *attr;
951 attr = nlmsg_find_attr(h, hdrlen,
952 INET_DIAG_REQ_BYTECODE);
953 if (attr == NULL ||
954 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
955 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
956 return -EINVAL;
957 }
958
959 return netlink_dump_start(sdiagnl, skb, h,
960 inet_diag_dump, NULL, 0);
Pavel Emelyanovd3664772011-12-06 07:58:03 +0000961 }
962
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000963 return inet_diag_get_exact(skb, h, (struct inet_diag_req *)NLMSG_DATA(h));
Pavel Emelyanovd3664772011-12-06 07:58:03 +0000964}
965
966static struct sock_diag_handler inet_diag_handler = {
967 .family = AF_INET,
968 .dump = inet_diag_handler_dump,
969};
970
971static struct sock_diag_handler inet6_diag_handler = {
972 .family = AF_INET6,
973 .dump = inet_diag_handler_dump,
974};
975
976static struct sock_diag_handler *sock_diag_handlers[AF_MAX];
977static DEFINE_MUTEX(sock_diag_table_mutex);
978
979int sock_diag_register(struct sock_diag_handler *hndl)
980{
981 int err = 0;
982
983 if (hndl->family > AF_MAX)
984 return -EINVAL;
985
986 mutex_lock(&sock_diag_table_mutex);
987 if (sock_diag_handlers[hndl->family])
988 err = -EBUSY;
989 else
990 sock_diag_handlers[hndl->family] = hndl;
991 mutex_unlock(&sock_diag_table_mutex);
992
993 return err;
994}
995
996void sock_diag_unregister(struct sock_diag_handler *hnld)
997{
998 int family = hnld->family;
999
1000 if (family > AF_MAX)
1001 return;
1002
1003 mutex_lock(&sock_diag_table_mutex);
1004 BUG_ON(sock_diag_handlers[family] != hnld);
1005 sock_diag_handlers[family] = NULL;
1006 mutex_unlock(&sock_diag_table_mutex);
1007}
1008
1009static inline struct sock_diag_handler *sock_diag_lock_handler(int family)
1010{
1011 mutex_lock(&sock_diag_table_mutex);
1012 return sock_diag_handlers[family];
1013}
1014
1015static inline void sock_diag_unlock_handler(struct sock_diag_handler *h)
1016{
1017 mutex_unlock(&sock_diag_table_mutex);
1018}
1019
Pavel Emelyanov8d341722011-12-06 07:57:06 +00001020static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1021{
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001022 int err;
1023 struct sock_diag_req *req = NLMSG_DATA(nlh);
1024 struct sock_diag_handler *hndl;
1025
1026 if (nlmsg_len(nlh) < sizeof(*req))
1027 return -EINVAL;
1028
1029 hndl = sock_diag_lock_handler(req->sdiag_family);
1030 if (hndl == NULL)
1031 err = -ENOENT;
1032 else
1033 err = hndl->dump(skb, nlh);
1034 sock_diag_unlock_handler(hndl);
1035
1036 return err;
Pavel Emelyanov8d341722011-12-06 07:57:06 +00001037}
1038
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001039static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Pavel Emelyanov8d341722011-12-06 07:57:06 +00001041 switch (nlh->nlmsg_type) {
1042 case TCPDIAG_GETSOCK:
1043 case DCCPDIAG_GETSOCK:
1044 return inet_diag_rcv_msg_compat(skb, nlh);
1045 case SOCK_DIAG_BY_FAMILY:
1046 return __sock_diag_rcv_msg(skb, nlh);
1047 default:
1048 return -EINVAL;
1049 }
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001050}
1051
1052static DEFINE_MUTEX(sock_diag_mutex);
1053
1054static void sock_diag_rcv(struct sk_buff *skb)
1055{
1056 mutex_lock(&sock_diag_mutex);
1057 netlink_rcv_skb(skb, &sock_diag_rcv_msg);
1058 mutex_unlock(&sock_diag_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001061int inet_diag_register(const struct inet_diag_handler *h)
1062{
1063 const __u16 type = h->idiag_type;
1064 int err = -EINVAL;
1065
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001066 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001067 goto out;
1068
Herbert Xud523a322007-12-03 15:51:25 +11001069 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001070 err = -EEXIST;
1071 if (inet_diag_table[type] == NULL) {
1072 inet_diag_table[type] = h;
1073 err = 0;
1074 }
Herbert Xud523a322007-12-03 15:51:25 +11001075 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001076out:
1077 return err;
1078}
1079EXPORT_SYMBOL_GPL(inet_diag_register);
1080
1081void inet_diag_unregister(const struct inet_diag_handler *h)
1082{
1083 const __u16 type = h->idiag_type;
1084
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001085 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001086 return;
1087
Herbert Xud523a322007-12-03 15:51:25 +11001088 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001089 inet_diag_table[type] = NULL;
Herbert Xud523a322007-12-03 15:51:25 +11001090 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001091}
1092EXPORT_SYMBOL_GPL(inet_diag_unregister);
1093
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001094static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001096 const int inet_diag_table_size = (IPPROTO_MAX *
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001097 sizeof(struct inet_diag_handler *));
1098 int err = -ENOMEM;
1099
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001100 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001101 if (!inet_diag_table)
1102 goto out;
1103
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001104 sdiagnl = netlink_kernel_create(&init_net, NETLINK_SOCK_DIAG, 0,
1105 sock_diag_rcv, NULL, THIS_MODULE);
1106 if (sdiagnl == NULL)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001107 goto out_free_table;
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001108
1109 err = sock_diag_register(&inet_diag_handler);
1110 if (err)
1111 goto out_free_nl;
1112
1113 err = sock_diag_register(&inet6_diag_handler);
1114 if (err)
1115 goto out_free_inet;
1116
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001117out:
1118 return err;
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001119
1120out_free_inet:
1121 sock_diag_unregister(&inet_diag_handler);
1122out_free_nl:
1123 netlink_kernel_release(sdiagnl);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001124out_free_table:
1125 kfree(inet_diag_table);
1126 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001129static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001131 sock_diag_unregister(&inet6_diag_handler);
1132 sock_diag_unregister(&inet_diag_handler);
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001133 netlink_kernel_release(sdiagnl);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001134 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001137module_init(inet_diag_init);
1138module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139MODULE_LICENSE("GPL");
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001140MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);