Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 2 | * inet_diag.c Module for monitoring INET transport protocols sockets. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * 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ärvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/fcntl.h> |
| 16 | #include <linux/random.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/cache.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/time.h> |
| 21 | |
| 22 | #include <net/icmp.h> |
| 23 | #include <net/tcp.h> |
| 24 | #include <net/ipv6.h> |
| 25 | #include <net/inet_common.h> |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame] | 26 | #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 Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 30 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #include <linux/inet.h> |
| 33 | #include <linux/stddef.h> |
| 34 | |
Arnaldo Carvalho de Melo | a8c2190 | 2005-08-12 12:56:38 -0300 | [diff] [blame] | 35 | #include <linux/inet_diag.h> |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 36 | #include <linux/sock_diag.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 38 | static const struct inet_diag_handler **inet_diag_table; |
| 39 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 40 | struct inet_diag_entry { |
Al Viro | 9f85529 | 2006-09-27 18:44:30 -0700 | [diff] [blame] | 41 | __be32 *saddr; |
| 42 | __be32 *daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | u16 sport; |
| 44 | u16 dport; |
| 45 | u16 family; |
| 46 | u16 userlocks; |
| 47 | }; |
| 48 | |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 49 | static struct sock *sdiagnl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 51 | #define INET_DIAG_PUT(skb, attrtype, attrlen) \ |
Stephen Hemminger | 7c99c90 | 2005-06-23 12:20:36 -0700 | [diff] [blame] | 52 | RTA_DATA(__RTA_PUT(skb, attrtype, attrlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 54 | static DEFINE_MUTEX(inet_diag_table_mutex); |
| 55 | |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame] | 56 | static const struct inet_diag_handler *inet_diag_lock_handler(int proto) |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 57 | { |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame] | 58 | if (!inet_diag_table[proto]) |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 59 | request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame] | 60 | NETLINK_SOCK_DIAG, proto); |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 61 | |
| 62 | mutex_lock(&inet_diag_table_mutex); |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame] | 63 | if (!inet_diag_table[proto]) |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 64 | return ERR_PTR(-ENOENT); |
| 65 | |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame] | 66 | return inet_diag_table[proto]; |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static 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 Melo | dff2c03 | 2006-01-09 14:56:56 -0800 | [diff] [blame] | 75 | static int inet_csk_diag_fill(struct sock *sk, |
Pavel Emelyanov | a029fe2 | 2011-12-06 07:59:32 +0000 | [diff] [blame^] | 76 | struct sk_buff *skb, struct inet_diag_req *req, |
| 77 | u32 pid, u32 seq, u16 nlmsg_flags, |
Arnaldo Carvalho de Melo | dff2c03 | 2006-01-09 14:56:56 -0800 | [diff] [blame] | 78 | const struct nlmsghdr *unlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 80 | const struct inet_sock *inet = inet_sk(sk); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 81 | const struct inet_connection_sock *icsk = inet_csk(sk); |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 82 | struct inet_diag_msg *r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | struct nlmsghdr *nlh; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 84 | void *info = NULL; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 85 | struct inet_diag_meminfo *minfo = NULL; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 86 | unsigned char *b = skb_tail_pointer(skb); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 87 | const struct inet_diag_handler *handler; |
Pavel Emelyanov | a029fe2 | 2011-12-06 07:59:32 +0000 | [diff] [blame^] | 88 | int ext = req->idiag_ext; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 89 | |
Pavel Emelyanov | a029fe2 | 2011-12-06 07:59:32 +0000 | [diff] [blame^] | 90 | handler = inet_diag_table[req->sdiag_protocol]; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 91 | BUG_ON(handler == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 93 | nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | nlh->nlmsg_flags = nlmsg_flags; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | r = NLMSG_DATA(nlh); |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 97 | BUG_ON(sk->sk_state == TCP_TIME_WAIT); |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 98 | |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 99 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 112 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 113 | r->idiag_family = sk->sk_family; |
| 114 | r->idiag_state = sk->sk_state; |
| 115 | r->idiag_timer = 0; |
| 116 | r->idiag_retrans = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 118 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 122 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Maciej Żenczykowski | 717b6d8 | 2011-11-22 16:03:10 -0500 | [diff] [blame] | 127 | /* 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 Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame] | 133 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 134 | if (r->idiag_family == AF_INET6) { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 135 | const struct ipv6_pinfo *np = inet6_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 137 | *(struct in6_addr *)r->id.idiag_src = np->rcv_saddr; |
| 138 | *(struct in6_addr *)r->id.idiag_dst = np->daddr; |
Maciej Żenczykowski | 06236ac | 2011-11-07 14:23:11 +0000 | [diff] [blame] | 139 | if (ext & (1 << (INET_DIAG_TCLASS - 1))) |
| 140 | RTA_PUT_U8(skb, INET_DIAG_TCLASS, np->tclass); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | #endif |
| 143 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 144 | #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 146 | if (icsk->icsk_pending == ICSK_TIME_RETRANS) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 147 | r->idiag_timer = 1; |
| 148 | r->idiag_retrans = icsk->icsk_retransmits; |
| 149 | r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 150 | } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 151 | r->idiag_timer = 4; |
| 152 | r->idiag_retrans = icsk->icsk_probes_out; |
| 153 | r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } else if (timer_pending(&sk->sk_timer)) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 155 | r->idiag_timer = 2; |
| 156 | r->idiag_retrans = icsk->icsk_probes_out; |
| 157 | r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } else { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 159 | r->idiag_timer = 0; |
| 160 | r->idiag_expires = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | #undef EXPIRES_IN_MS |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 163 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 164 | r->idiag_uid = sock_i_uid(sk); |
| 165 | r->idiag_inode = sock_i_ino(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | if (minfo) { |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 168 | minfo->idiag_rmem = sk_rmem_alloc_get(sk); |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 169 | minfo->idiag_wmem = sk->sk_wmem_queued; |
| 170 | minfo->idiag_fmem = sk->sk_forward_alloc; |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 171 | minfo->idiag_tmem = sk_wmem_alloc_get(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 174 | handler->idiag_get_info(sk, r, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 176 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 180 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | return skb->len; |
| 182 | |
Stephen Hemminger | 7c99c90 | 2005-06-23 12:20:36 -0700 | [diff] [blame] | 183 | rtattr_failure: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | nlmsg_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 185 | nlmsg_trim(skb, b); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 186 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 189 | static int inet_twsk_diag_fill(struct inet_timewait_sock *tw, |
Pavel Emelyanov | a029fe2 | 2011-12-06 07:59:32 +0000 | [diff] [blame^] | 190 | struct sk_buff *skb, struct inet_diag_req *req, |
| 191 | u32 pid, u32 seq, u16 nlmsg_flags, |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 192 | const struct nlmsghdr *unlh) |
| 193 | { |
| 194 | long tmo; |
| 195 | struct inet_diag_msg *r; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 196 | const unsigned char *previous_tail = skb_tail_pointer(skb); |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 197 | 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 Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 210 | 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ärvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 220 | r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ); |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 221 | 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 Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 230 | *(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 Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 232 | } |
| 233 | #endif |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 234 | nlh->nlmsg_len = skb_tail_pointer(skb) - previous_tail; |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 235 | return skb->len; |
| 236 | nlmsg_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 237 | nlmsg_trim(skb, previous_tail); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 238 | return -EMSGSIZE; |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Arnaldo Carvalho de Melo | dff2c03 | 2006-01-09 14:56:56 -0800 | [diff] [blame] | 241 | static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, |
Pavel Emelyanov | a029fe2 | 2011-12-06 07:59:32 +0000 | [diff] [blame^] | 242 | struct inet_diag_req *r, u32 pid, u32 seq, u16 nlmsg_flags, |
Arnaldo Carvalho de Melo | dff2c03 | 2006-01-09 14:56:56 -0800 | [diff] [blame] | 243 | 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 Emelyanov | a029fe2 | 2011-12-06 07:59:32 +0000 | [diff] [blame^] | 247 | skb, r, pid, seq, nlmsg_flags, |
Arnaldo Carvalho de Melo | dff2c03 | 2006-01-09 14:56:56 -0800 | [diff] [blame] | 248 | unlh); |
Pavel Emelyanov | a029fe2 | 2011-12-06 07:59:32 +0000 | [diff] [blame^] | 249 | return inet_csk_diag_fill(sk, skb, r, pid, seq, nlmsg_flags, unlh); |
Arnaldo Carvalho de Melo | dff2c03 | 2006-01-09 14:56:56 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 252 | static int inet_diag_get_exact(struct sk_buff *in_skb, |
Pavel Emelyanov | fe50ce2 | 2011-12-06 07:58:39 +0000 | [diff] [blame] | 253 | const struct nlmsghdr *nlh, |
| 254 | struct inet_diag_req *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
| 256 | int err; |
| 257 | struct sock *sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | struct sk_buff *rep; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 259 | struct inet_hashinfo *hashinfo; |
| 260 | const struct inet_diag_handler *handler; |
| 261 | |
Pavel Emelyanov | fe50ce2 | 2011-12-06 07:58:39 +0000 | [diff] [blame] | 262 | handler = inet_diag_lock_handler(req->sdiag_protocol); |
Arnaldo Carvalho de Melo | 8cf8e5a | 2008-01-28 20:52:12 -0800 | [diff] [blame] | 263 | if (IS_ERR(handler)) { |
| 264 | err = PTR_ERR(handler); |
| 265 | goto unlock; |
| 266 | } |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 267 | |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 268 | hashinfo = handler->idiag_hashinfo; |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 269 | err = -EINVAL; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 270 | |
Pavel Emelyanov | fe50ce2 | 2011-12-06 07:58:39 +0000 | [diff] [blame] | 271 | if (req->sdiag_family == AF_INET) { |
Pavel Emelyanov | c67499c | 2008-01-31 05:06:40 -0800 | [diff] [blame] | 272 | sk = inet_lookup(&init_net, hashinfo, req->id.idiag_dst[0], |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 273 | req->id.idiag_dport, req->id.idiag_src[0], |
| 274 | req->id.idiag_sport, req->id.idiag_if); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame] | 276 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Pavel Emelyanov | fe50ce2 | 2011-12-06 07:58:39 +0000 | [diff] [blame] | 277 | else if (req->sdiag_family == AF_INET6) { |
Pavel Emelyanov | d86e0da | 2008-01-31 05:07:21 -0800 | [diff] [blame] | 278 | sk = inet6_lookup(&init_net, hashinfo, |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 279 | (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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
| 285 | #endif |
| 286 | else { |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 287 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 290 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | if (sk == NULL) |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 292 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | err = -ESTALE; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 295 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | goto out; |
| 300 | |
| 301 | err = -ENOMEM; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 302 | rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) + |
| 303 | sizeof(struct inet_diag_meminfo) + |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 304 | handler->idiag_info_size + 64)), |
| 305 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | if (!rep) |
| 307 | goto out; |
| 308 | |
Pavel Emelyanov | a029fe2 | 2011-12-06 07:59:32 +0000 | [diff] [blame^] | 309 | err = sk_diag_fill(sk, rep, req, |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 310 | 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 Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 317 | err = netlink_unicast(sdiagnl, rep, NETLINK_CB(in_skb).pid, |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 318 | MSG_DONTWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | if (err > 0) |
| 320 | err = 0; |
| 321 | |
| 322 | out: |
| 323 | if (sk) { |
| 324 | if (sk->sk_state == TCP_TIME_WAIT) |
Arnaldo Carvalho de Melo | 8feaf0c0 | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 325 | inet_twsk_put((struct inet_timewait_sock *)sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | else |
| 327 | sock_put(sk); |
| 328 | } |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 329 | unlock: |
| 330 | inet_diag_unlock_handler(handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | return err; |
| 332 | } |
| 333 | |
Al Viro | 9f85529 | 2006-09-27 18:44:30 -0700 | [diff] [blame] | 334 | static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | { |
| 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 Viro | 9f85529 | 2006-09-27 18:44:30 -0700 | [diff] [blame] | 345 | __be32 w1, w2; |
| 346 | __be32 mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 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 Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 361 | static int inet_diag_bc_run(const void *bc, int len, |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 362 | const struct inet_diag_entry *entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
| 364 | while (len > 0) { |
| 365 | int yes = 1; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 366 | const struct inet_diag_bc_op *op = bc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
| 368 | switch (op->code) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 369 | case INET_DIAG_BC_NOP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 371 | case INET_DIAG_BC_JMP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | yes = 0; |
| 373 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 374 | case INET_DIAG_BC_S_GE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | yes = entry->sport >= op[1].no; |
| 376 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 377 | case INET_DIAG_BC_S_LE: |
Roel Kluin | b4ced2b | 2010-01-19 14:12:20 -0800 | [diff] [blame] | 378 | yes = entry->sport <= op[1].no; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 380 | case INET_DIAG_BC_D_GE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | yes = entry->dport >= op[1].no; |
| 382 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 383 | case INET_DIAG_BC_D_LE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | yes = entry->dport <= op[1].no; |
| 385 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 386 | case INET_DIAG_BC_AUTO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); |
| 388 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 389 | case INET_DIAG_BC_S_COND: |
Arnaldo Carvalho de Melo | a8c2190 | 2005-08-12 12:56:38 -0300 | [diff] [blame] | 390 | case INET_DIAG_BC_D_COND: { |
| 391 | struct inet_diag_hostcond *cond; |
Al Viro | 9f85529 | 2006-09-27 18:44:30 -0700 | [diff] [blame] | 392 | __be32 *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Arnaldo Carvalho de Melo | a8c2190 | 2005-08-12 12:56:38 -0300 | [diff] [blame] | 394 | cond = (struct inet_diag_hostcond *)(op + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (cond->port != -1 && |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 396 | cond->port != (op->code == INET_DIAG_BC_S_COND ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | entry->sport : entry->dport)) { |
| 398 | yes = 0; |
| 399 | break; |
| 400 | } |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | if (cond->prefix_len == 0) |
| 403 | break; |
| 404 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 405 | if (op->code == INET_DIAG_BC_S_COND) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | addr = entry->saddr; |
| 407 | else |
| 408 | addr = entry->daddr; |
| 409 | |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 410 | if (bitstring_match(addr, cond->addr, |
| 411 | cond->prefix_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | 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 Melo | a8c2190 | 2005-08-12 12:56:38 -0300 | [diff] [blame] | 417 | bitstring_match(addr + 3, cond->addr, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 418 | cond->prefix_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | break; |
| 420 | } |
| 421 | yes = 0; |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 426 | if (yes) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | len -= op->yes; |
| 428 | bc += op->yes; |
| 429 | } else { |
| 430 | len -= op->no; |
| 431 | bc += op->no; |
| 432 | } |
| 433 | } |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 434 | return len == 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static int valid_cc(const void *bc, int len, int cc) |
| 438 | { |
| 439 | while (len >= 0) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 440 | const struct inet_diag_bc_op *op = bc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
| 442 | if (cc > len) |
| 443 | return 0; |
| 444 | if (cc == len) |
| 445 | return 1; |
Eric Dumazet | eeb1497 | 2011-06-17 16:25:39 -0400 | [diff] [blame] | 446 | if (op->yes < 4 || op->yes & 3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | return 0; |
| 448 | len -= op->yes; |
| 449 | bc += op->yes; |
| 450 | } |
| 451 | return 0; |
| 452 | } |
| 453 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 454 | static int inet_diag_bc_audit(const void *bytecode, int bytecode_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
Eric Dumazet | eeb1497 | 2011-06-17 16:25:39 -0400 | [diff] [blame] | 456 | const void *bc = bytecode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | int len = bytecode_len; |
| 458 | |
| 459 | while (len > 0) { |
Eric Dumazet | eeb1497 | 2011-06-17 16:25:39 -0400 | [diff] [blame] | 460 | const struct inet_diag_bc_op *op = bc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
| 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 Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 464 | 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 Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 471 | case INET_DIAG_BC_JMP: |
Eric Dumazet | eeb1497 | 2011-06-17 16:25:39 -0400 | [diff] [blame] | 472 | if (op->no < 4 || op->no > len + 4 || op->no & 3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | return -EINVAL; |
| 474 | if (op->no < len && |
Arnaldo Carvalho de Melo | a8c2190 | 2005-08-12 12:56:38 -0300 | [diff] [blame] | 475 | !valid_cc(bytecode, bytecode_len, len - op->no)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | return -EINVAL; |
| 477 | break; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 478 | case INET_DIAG_BC_NOP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | break; |
| 480 | default: |
| 481 | return -EINVAL; |
| 482 | } |
Eric Dumazet | eeb1497 | 2011-06-17 16:25:39 -0400 | [diff] [blame] | 483 | if (op->yes < 4 || op->yes > len + 4 || op->yes & 3) |
| 484 | return -EINVAL; |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 485 | bc += op->yes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | len -= op->yes; |
| 487 | } |
| 488 | return len == 0 ? 0 : -EINVAL; |
| 489 | } |
| 490 | |
Arnaldo Carvalho de Melo | dff2c03 | 2006-01-09 14:56:56 -0800 | [diff] [blame] | 491 | static int inet_csk_diag_dump(struct sock *sk, |
| 492 | struct sk_buff *skb, |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 493 | struct netlink_callback *cb, |
Pavel Emelyanov | 25c4cd2 | 2011-12-06 07:58:58 +0000 | [diff] [blame] | 494 | struct inet_diag_req *r, |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 495 | const struct nlattr *bc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | { |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 497 | if (bc != NULL) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 498 | struct inet_diag_entry entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | struct inet_sock *inet = inet_sk(sk); |
| 500 | |
| 501 | entry.family = sk->sk_family; |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame] | 502 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | 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 Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 511 | entry.saddr = &inet->inet_rcv_saddr; |
| 512 | entry.daddr = &inet->inet_daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 514 | entry.sport = inet->inet_num; |
| 515 | entry.dport = ntohs(inet->inet_dport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | entry.userlocks = sk->sk_userlocks; |
| 517 | |
Nelson Elhage | 22e76c8 | 2010-11-03 16:35:41 +0000 | [diff] [blame] | 518 | if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | |
Pavel Emelyanov | a029fe2 | 2011-12-06 07:59:32 +0000 | [diff] [blame^] | 522 | return inet_csk_diag_fill(sk, skb, r, |
Arnaldo Carvalho de Melo | dff2c03 | 2006-01-09 14:56:56 -0800 | [diff] [blame] | 523 | NETLINK_CB(cb->skb).pid, |
| 524 | cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 527 | static int inet_twsk_diag_dump(struct inet_timewait_sock *tw, |
| 528 | struct sk_buff *skb, |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 529 | struct netlink_callback *cb, |
Pavel Emelyanov | 25c4cd2 | 2011-12-06 07:58:58 +0000 | [diff] [blame] | 530 | struct inet_diag_req *r, |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 531 | const struct nlattr *bc) |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 532 | { |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 533 | if (bc != NULL) { |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 534 | struct inet_diag_entry entry; |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 535 | |
| 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 Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 551 | entry.userlocks = 0; |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 552 | |
Nelson Elhage | 22e76c8 | 2010-11-03 16:35:41 +0000 | [diff] [blame] | 553 | if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry)) |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 554 | return 0; |
| 555 | } |
| 556 | |
Pavel Emelyanov | a029fe2 | 2011-12-06 07:59:32 +0000 | [diff] [blame^] | 557 | return inet_twsk_diag_fill(tw, skb, r, |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 558 | NETLINK_CB(cb->skb).pid, |
| 559 | cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); |
| 560 | } |
| 561 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 562 | static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk, |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 563 | struct request_sock *req, u32 pid, u32 seq, |
| 564 | const struct nlmsghdr *unlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | { |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 566 | const struct inet_request_sock *ireq = inet_rsk(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | struct inet_sock *inet = inet_sk(sk); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 568 | unsigned char *b = skb_tail_pointer(skb); |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 569 | struct inet_diag_msg *r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | struct nlmsghdr *nlh; |
| 571 | long tmo; |
| 572 | |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 573 | nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | nlh->nlmsg_flags = NLM_F_MULTI; |
| 575 | r = NLMSG_DATA(nlh); |
| 576 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 577 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 582 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | tmo = req->expires - jiffies; |
| 587 | if (tmo < 0) |
| 588 | tmo = 0; |
| 589 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 590 | r->id.idiag_sport = inet->inet_sport; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 591 | 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 Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame] | 599 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 600 | if (r->idiag_family == AF_INET6) { |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 601 | *(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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | #endif |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 605 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | return skb->len; |
| 608 | |
| 609 | nlmsg_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 610 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | return -1; |
| 612 | } |
| 613 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 614 | static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk, |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 615 | struct netlink_callback *cb, |
Pavel Emelyanov | 25c4cd2 | 2011-12-06 07:58:58 +0000 | [diff] [blame] | 616 | struct inet_diag_req *r, |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 617 | const struct nlattr *bc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 619 | struct inet_diag_entry entry; |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 620 | struct inet_connection_sock *icsk = inet_csk(sk); |
Arnaldo Carvalho de Melo | 2ad69c5 | 2005-06-18 22:48:55 -0700 | [diff] [blame] | 621 | struct listen_sock *lopt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | 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 Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 635 | read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 637 | lopt = icsk->icsk_accept_queue.listen_opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | if (!lopt || !lopt->qlen) |
| 639 | goto out; |
| 640 | |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 641 | if (bc != NULL) { |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 642 | entry.sport = inet->inet_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | entry.userlocks = sk->sk_userlocks; |
| 644 | } |
| 645 | |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 646 | for (j = s_j; j < lopt->nr_table_entries; j++) { |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 647 | struct request_sock *req, *head = lopt->syn_table[j]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
| 649 | reqnum = 0; |
| 650 | for (req = head; req; reqnum++, req = req->dl_next) { |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 651 | struct inet_request_sock *ireq = inet_rsk(req); |
| 652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | if (reqnum < s_reqnum) |
| 654 | continue; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 655 | if (r->id.idiag_dport != ireq->rmt_port && |
| 656 | r->id.idiag_dport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | continue; |
| 658 | |
| 659 | if (bc) { |
| 660 | entry.saddr = |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame] | 661 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | (entry.family == AF_INET6) ? |
Arnaldo Carvalho de Melo | ca304b6 | 2005-12-13 23:15:40 -0800 | [diff] [blame] | 663 | inet6_rsk(req)->loc_addr.s6_addr32 : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | #endif |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 665 | &ireq->loc_addr; |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 666 | entry.daddr = |
Arnaldo Carvalho de Melo | 505cbfc | 2005-08-12 09:19:38 -0300 | [diff] [blame] | 667 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | (entry.family == AF_INET6) ? |
Arnaldo Carvalho de Melo | ca304b6 | 2005-12-13 23:15:40 -0800 | [diff] [blame] | 669 | inet6_rsk(req)->rmt_addr.s6_addr32 : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | #endif |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 671 | &ireq->rmt_addr; |
| 672 | entry.dport = ntohs(ireq->rmt_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
Nelson Elhage | 22e76c8 | 2010-11-03 16:35:41 +0000 | [diff] [blame] | 674 | if (!inet_diag_bc_run(nla_data(bc), |
| 675 | nla_len(bc), &entry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | continue; |
| 677 | } |
| 678 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 679 | err = inet_diag_fill_req(skb, sk, req, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | NETLINK_CB(cb->skb).pid, |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 681 | cb->nlh->nlmsg_seq, cb->nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | 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 | |
| 692 | out: |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 693 | read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
| 695 | return err; |
| 696 | } |
| 697 | |
Pavel Emelyanov | 25c4cd2 | 2011-12-06 07:58:58 +0000 | [diff] [blame] | 698 | static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, |
| 699 | struct inet_diag_req *r, struct nlattr *bc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | { |
| 701 | int i, num; |
| 702 | int s_i, s_num; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 703 | const struct inet_diag_handler *handler; |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 704 | struct inet_hashinfo *hashinfo; |
Pavel Emelyanov | 37f352b | 2011-12-06 07:57:26 +0000 | [diff] [blame] | 705 | |
Pavel Emelyanov | 25c4cd2 | 2011-12-06 07:58:58 +0000 | [diff] [blame] | 706 | handler = inet_diag_lock_handler(r->sdiag_protocol); |
Arnaldo Carvalho de Melo | 8cf8e5a | 2008-01-28 20:52:12 -0800 | [diff] [blame] | 707 | if (IS_ERR(handler)) |
| 708 | goto unlock; |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 709 | |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 710 | hashinfo = handler->idiag_hashinfo; |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | s_i = cb->args[1]; |
| 713 | s_num = num = cb->args[2]; |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | if (cb->args[0] == 0) { |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 716 | if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | goto skip_listen_ht; |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 718 | |
Arnaldo Carvalho de Melo | 0f7ff92 | 2005-08-09 19:59:44 -0700 | [diff] [blame] | 719 | for (i = s_i; i < INET_LHTABLE_SIZE; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | struct sock *sk; |
Eric Dumazet | c25eb3b | 2008-11-23 17:22:55 -0800 | [diff] [blame] | 721 | struct hlist_nulls_node *node; |
Eric Dumazet | 5caea4e | 2008-11-20 00:40:07 -0800 | [diff] [blame] | 722 | struct inet_listen_hashbucket *ilb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
| 724 | num = 0; |
Eric Dumazet | 5caea4e | 2008-11-20 00:40:07 -0800 | [diff] [blame] | 725 | ilb = &hashinfo->listening_hash[i]; |
| 726 | spin_lock_bh(&ilb->lock); |
Eric Dumazet | c25eb3b | 2008-11-23 17:22:55 -0800 | [diff] [blame] | 727 | sk_nulls_for_each(sk, node, &ilb->head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | struct inet_sock *inet = inet_sk(sk); |
| 729 | |
| 730 | if (num < s_num) { |
| 731 | num++; |
| 732 | continue; |
| 733 | } |
| 734 | |
Pavel Emelyanov | d23deaa | 2011-12-06 07:59:15 +0000 | [diff] [blame] | 735 | if (r->sdiag_family != AF_UNSPEC && |
| 736 | sk->sk_family != r->sdiag_family) |
| 737 | goto next_listen; |
| 738 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 739 | if (r->id.idiag_sport != inet->inet_sport && |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 740 | r->id.idiag_sport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | goto next_listen; |
| 742 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 743 | if (!(r->idiag_states & TCPF_LISTEN) || |
| 744 | r->id.idiag_dport || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | cb->args[3] > 0) |
| 746 | goto syn_recv; |
| 747 | |
Pavel Emelyanov | 25c4cd2 | 2011-12-06 07:58:58 +0000 | [diff] [blame] | 748 | if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) { |
Eric Dumazet | 5caea4e | 2008-11-20 00:40:07 -0800 | [diff] [blame] | 749 | spin_unlock_bh(&ilb->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | goto done; |
| 751 | } |
| 752 | |
| 753 | syn_recv: |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 754 | if (!(r->idiag_states & TCPF_SYN_RECV)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | goto next_listen; |
| 756 | |
Pavel Emelyanov | 25c4cd2 | 2011-12-06 07:58:58 +0000 | [diff] [blame] | 757 | if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) { |
Eric Dumazet | 5caea4e | 2008-11-20 00:40:07 -0800 | [diff] [blame] | 758 | spin_unlock_bh(&ilb->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | goto done; |
| 760 | } |
| 761 | |
| 762 | next_listen: |
| 763 | cb->args[3] = 0; |
| 764 | cb->args[4] = 0; |
| 765 | ++num; |
| 766 | } |
Eric Dumazet | 5caea4e | 2008-11-20 00:40:07 -0800 | [diff] [blame] | 767 | spin_unlock_bh(&ilb->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
| 769 | s_num = 0; |
| 770 | cb->args[3] = 0; |
| 771 | cb->args[4] = 0; |
| 772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | skip_listen_ht: |
| 774 | cb->args[0] = 1; |
| 775 | s_i = num = s_num = 0; |
| 776 | } |
| 777 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 778 | if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV))) |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 779 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
Eric Dumazet | f373b53 | 2009-10-09 00:16:19 +0000 | [diff] [blame] | 781 | for (i = s_i; i <= hashinfo->ehash_mask; i++) { |
Arnaldo Carvalho de Melo | 540722f | 2005-08-10 05:54:28 -0300 | [diff] [blame] | 782 | struct inet_ehash_bucket *head = &hashinfo->ehash[i]; |
David S. Miller | 7e3aab4 | 2008-11-21 16:39:19 -0800 | [diff] [blame] | 783 | spinlock_t *lock = inet_ehash_lockp(hashinfo, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | struct sock *sk; |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 785 | struct hlist_nulls_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | |
Andi Kleen | 6be547a | 2008-08-28 01:09:54 -0700 | [diff] [blame] | 787 | num = 0; |
| 788 | |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 789 | if (hlist_nulls_empty(&head->chain) && |
| 790 | hlist_nulls_empty(&head->twchain)) |
Andi Kleen | 6be547a | 2008-08-28 01:09:54 -0700 | [diff] [blame] | 791 | continue; |
| 792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | if (i > s_i) |
| 794 | s_num = 0; |
| 795 | |
David S. Miller | 7e3aab4 | 2008-11-21 16:39:19 -0800 | [diff] [blame] | 796 | spin_lock_bh(lock); |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 797 | sk_nulls_for_each(sk, node, &head->chain) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | struct inet_sock *inet = inet_sk(sk); |
| 799 | |
| 800 | if (num < s_num) |
| 801 | goto next_normal; |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 802 | if (!(r->idiag_states & (1 << sk->sk_state))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | goto next_normal; |
Pavel Emelyanov | d23deaa | 2011-12-06 07:59:15 +0000 | [diff] [blame] | 804 | if (r->sdiag_family != AF_UNSPEC && |
| 805 | sk->sk_family != r->sdiag_family) |
| 806 | goto next_normal; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 807 | if (r->id.idiag_sport != inet->inet_sport && |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 808 | r->id.idiag_sport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | goto next_normal; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 810 | if (r->id.idiag_dport != inet->inet_dport && |
Arnaldo Carvalho de Melo | 4e852c0 | 2006-01-09 14:56:19 -0800 | [diff] [blame] | 811 | r->id.idiag_dport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | goto next_normal; |
Pavel Emelyanov | 25c4cd2 | 2011-12-06 07:58:58 +0000 | [diff] [blame] | 813 | if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) { |
David S. Miller | 7e3aab4 | 2008-11-21 16:39:19 -0800 | [diff] [blame] | 814 | spin_unlock_bh(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | goto done; |
| 816 | } |
| 817 | next_normal: |
| 818 | ++num; |
| 819 | } |
| 820 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 821 | if (r->idiag_states & TCPF_TIME_WAIT) { |
Arnaldo Carvalho de Melo | c7d58aa | 2006-01-09 14:56:38 -0800 | [diff] [blame] | 822 | struct inet_timewait_sock *tw; |
| 823 | |
| 824 | inet_twsk_for_each(tw, node, |
Eric Dumazet | dbca9b275 | 2007-02-08 14:16:46 -0800 | [diff] [blame] | 825 | &head->twchain) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | |
| 827 | if (num < s_num) |
| 828 | goto next_dying; |
Pavel Emelyanov | d23deaa | 2011-12-06 07:59:15 +0000 | [diff] [blame] | 829 | if (r->sdiag_family != AF_UNSPEC && |
| 830 | tw->tw_family != r->sdiag_family) |
| 831 | goto next_dying; |
Arnaldo Carvalho de Melo | 7dbf075 | 2006-01-09 14:56:03 -0800 | [diff] [blame] | 832 | if (r->id.idiag_sport != tw->tw_sport && |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 833 | r->id.idiag_sport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | goto next_dying; |
Arnaldo Carvalho de Melo | 7dbf075 | 2006-01-09 14:56:03 -0800 | [diff] [blame] | 835 | if (r->id.idiag_dport != tw->tw_dport && |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 836 | r->id.idiag_dport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | goto next_dying; |
Pavel Emelyanov | 25c4cd2 | 2011-12-06 07:58:58 +0000 | [diff] [blame] | 838 | if (inet_twsk_diag_dump(tw, skb, cb, r, bc) < 0) { |
David S. Miller | 7e3aab4 | 2008-11-21 16:39:19 -0800 | [diff] [blame] | 839 | spin_unlock_bh(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | goto done; |
| 841 | } |
| 842 | next_dying: |
| 843 | ++num; |
| 844 | } |
| 845 | } |
David S. Miller | 7e3aab4 | 2008-11-21 16:39:19 -0800 | [diff] [blame] | 846 | spin_unlock_bh(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | done: |
| 850 | cb->args[1] = i; |
| 851 | cb->args[2] = num; |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 852 | unlock: |
| 853 | inet_diag_unlock_handler(handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | return skb->len; |
| 855 | } |
| 856 | |
Pavel Emelyanov | 25c4cd2 | 2011-12-06 07:58:58 +0000 | [diff] [blame] | 857 | static 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 Emelyanov | a029fe2 | 2011-12-06 07:59:32 +0000 | [diff] [blame^] | 868 | static 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 Emelyanov | 25c4cd2 | 2011-12-06 07:58:58 +0000 | [diff] [blame] | 880 | static 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 Emelyanov | d23deaa | 2011-12-06 07:59:15 +0000 | [diff] [blame] | 887 | req.sdiag_family = AF_UNSPEC; /* compatibility */ |
Pavel Emelyanov | 25c4cd2 | 2011-12-06 07:58:58 +0000 | [diff] [blame] | 888 | 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 Emelyanov | fe50ce2 | 2011-12-06 07:58:39 +0000 | [diff] [blame] | 899 | static 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 Emelyanov | 8d34172 | 2011-12-06 07:57:06 +0000 | [diff] [blame] | 914 | static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | { |
Pavel Emelyanov | 126fdc3 | 2011-12-06 07:58:21 +0000 | [diff] [blame] | 916 | int hdrlen = sizeof(struct inet_diag_req_compat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | |
Thomas Graf | ead592b | 2007-03-22 23:30:35 -0700 | [diff] [blame] | 918 | if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || |
| 919 | nlmsg_len(nlh) < hdrlen) |
| 920 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | |
David S. Miller | b8f3ab4 | 2011-01-18 12:40:38 -0800 | [diff] [blame] | 922 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
Thomas Graf | ead592b | 2007-03-22 23:30:35 -0700 | [diff] [blame] | 923 | 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 Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 934 | return netlink_dump_start(sdiagnl, skb, nlh, |
Pavel Emelyanov | 25c4cd2 | 2011-12-06 07:58:58 +0000 | [diff] [blame] | 935 | inet_diag_dump_compat, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | } |
Thomas Graf | ead592b | 2007-03-22 23:30:35 -0700 | [diff] [blame] | 937 | |
Pavel Emelyanov | fe50ce2 | 2011-12-06 07:58:39 +0000 | [diff] [blame] | 938 | return inet_diag_get_exact_compat(skb, nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 941 | static 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 Emelyanov | 25c4cd2 | 2011-12-06 07:58:58 +0000 | [diff] [blame] | 949 | 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 Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Pavel Emelyanov | fe50ce2 | 2011-12-06 07:58:39 +0000 | [diff] [blame] | 963 | return inet_diag_get_exact(skb, h, (struct inet_diag_req *)NLMSG_DATA(h)); |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | static struct sock_diag_handler inet_diag_handler = { |
| 967 | .family = AF_INET, |
| 968 | .dump = inet_diag_handler_dump, |
| 969 | }; |
| 970 | |
| 971 | static struct sock_diag_handler inet6_diag_handler = { |
| 972 | .family = AF_INET6, |
| 973 | .dump = inet_diag_handler_dump, |
| 974 | }; |
| 975 | |
| 976 | static struct sock_diag_handler *sock_diag_handlers[AF_MAX]; |
| 977 | static DEFINE_MUTEX(sock_diag_table_mutex); |
| 978 | |
| 979 | int 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 | |
| 996 | void 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 | |
| 1009 | static 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 | |
| 1015 | static inline void sock_diag_unlock_handler(struct sock_diag_handler *h) |
| 1016 | { |
| 1017 | mutex_unlock(&sock_diag_table_mutex); |
| 1018 | } |
| 1019 | |
Pavel Emelyanov | 8d34172 | 2011-12-06 07:57:06 +0000 | [diff] [blame] | 1020 | static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 1021 | { |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 1022 | 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 Emelyanov | 8d34172 | 2011-12-06 07:57:06 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 1039 | static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | { |
Pavel Emelyanov | 8d34172 | 2011-12-06 07:57:06 +0000 | [diff] [blame] | 1041 | 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 Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | static DEFINE_MUTEX(sock_diag_mutex); |
| 1053 | |
| 1054 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 1061 | int inet_diag_register(const struct inet_diag_handler *h) |
| 1062 | { |
| 1063 | const __u16 type = h->idiag_type; |
| 1064 | int err = -EINVAL; |
| 1065 | |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame] | 1066 | if (type >= IPPROTO_MAX) |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 1067 | goto out; |
| 1068 | |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 1069 | mutex_lock(&inet_diag_table_mutex); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 1070 | err = -EEXIST; |
| 1071 | if (inet_diag_table[type] == NULL) { |
| 1072 | inet_diag_table[type] = h; |
| 1073 | err = 0; |
| 1074 | } |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 1075 | mutex_unlock(&inet_diag_table_mutex); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 1076 | out: |
| 1077 | return err; |
| 1078 | } |
| 1079 | EXPORT_SYMBOL_GPL(inet_diag_register); |
| 1080 | |
| 1081 | void inet_diag_unregister(const struct inet_diag_handler *h) |
| 1082 | { |
| 1083 | const __u16 type = h->idiag_type; |
| 1084 | |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame] | 1085 | if (type >= IPPROTO_MAX) |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 1086 | return; |
| 1087 | |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 1088 | mutex_lock(&inet_diag_table_mutex); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 1089 | inet_diag_table[type] = NULL; |
Herbert Xu | d523a32 | 2007-12-03 15:51:25 +1100 | [diff] [blame] | 1090 | mutex_unlock(&inet_diag_table_mutex); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 1091 | } |
| 1092 | EXPORT_SYMBOL_GPL(inet_diag_unregister); |
| 1093 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 1094 | static int __init inet_diag_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | { |
Pavel Emelyanov | f13c95f | 2011-12-06 08:05:24 +0000 | [diff] [blame] | 1096 | const int inet_diag_table_size = (IPPROTO_MAX * |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 1097 | sizeof(struct inet_diag_handler *)); |
| 1098 | int err = -ENOMEM; |
| 1099 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 1100 | inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 1101 | if (!inet_diag_table) |
| 1102 | goto out; |
| 1103 | |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 1104 | sdiagnl = netlink_kernel_create(&init_net, NETLINK_SOCK_DIAG, 0, |
| 1105 | sock_diag_rcv, NULL, THIS_MODULE); |
| 1106 | if (sdiagnl == NULL) |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 1107 | goto out_free_table; |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 1108 | |
| 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 Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 1117 | out: |
| 1118 | return err; |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 1119 | |
| 1120 | out_free_inet: |
| 1121 | sock_diag_unregister(&inet_diag_handler); |
| 1122 | out_free_nl: |
| 1123 | netlink_kernel_release(sdiagnl); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 1124 | out_free_table: |
| 1125 | kfree(inet_diag_table); |
| 1126 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 1129 | static void __exit inet_diag_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | { |
Pavel Emelyanov | d366477 | 2011-12-06 07:58:03 +0000 | [diff] [blame] | 1131 | sock_diag_unregister(&inet6_diag_handler); |
| 1132 | sock_diag_unregister(&inet_diag_handler); |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 1133 | netlink_kernel_release(sdiagnl); |
Arnaldo Carvalho de Melo | 4f5736c | 2005-08-12 09:27:49 -0300 | [diff] [blame] | 1134 | kfree(inet_diag_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Arnaldo Carvalho de Melo | 73c1f4a | 2005-08-12 12:51:49 -0300 | [diff] [blame] | 1137 | module_init(inet_diag_init); |
| 1138 | module_exit(inet_diag_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | MODULE_LICENSE("GPL"); |
Pavel Emelyanov | 7f1fb60 | 2011-12-06 07:56:43 +0000 | [diff] [blame] | 1140 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG); |