]>
Commit | Line | Data |
---|---|---|
8ef874bf PE |
1 | #include <linux/mutex.h> |
2 | #include <linux/socket.h> | |
3 | #include <linux/skbuff.h> | |
4 | #include <net/netlink.h> | |
5 | #include <net/net_namespace.h> | |
6 | #include <linux/module.h> | |
5d2e5f27 PE |
7 | #include <linux/rtnetlink.h> |
8 | #include <net/sock.h> | |
8ef874bf PE |
9 | |
10 | #include <linux/inet_diag.h> | |
11 | #include <linux/sock_diag.h> | |
12 | ||
8dcf01fc | 13 | static const struct sock_diag_handler *sock_diag_handlers[AF_MAX]; |
8ef874bf PE |
14 | static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh); |
15 | static DEFINE_MUTEX(sock_diag_table_mutex); | |
16 | ||
f65c1b53 PE |
17 | int sock_diag_check_cookie(void *sk, __u32 *cookie) |
18 | { | |
19 | if ((cookie[0] != INET_DIAG_NOCOOKIE || | |
20 | cookie[1] != INET_DIAG_NOCOOKIE) && | |
21 | ((u32)(unsigned long)sk != cookie[0] || | |
22 | (u32)((((unsigned long)sk) >> 31) >> 1) != cookie[1])) | |
23 | return -ESTALE; | |
24 | else | |
25 | return 0; | |
26 | } | |
27 | EXPORT_SYMBOL_GPL(sock_diag_check_cookie); | |
28 | ||
29 | void sock_diag_save_cookie(void *sk, __u32 *cookie) | |
30 | { | |
31 | cookie[0] = (u32)(unsigned long)sk; | |
32 | cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1); | |
33 | } | |
34 | EXPORT_SYMBOL_GPL(sock_diag_save_cookie); | |
35 | ||
5d2e5f27 PE |
36 | int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype) |
37 | { | |
38 | __u32 *mem; | |
39 | ||
40 | mem = RTA_DATA(__RTA_PUT(skb, attrtype, SK_MEMINFO_VARS * sizeof(__u32))); | |
41 | ||
42 | mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk); | |
43 | mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf; | |
44 | mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk); | |
45 | mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf; | |
46 | mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc; | |
47 | mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued; | |
48 | mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc); | |
d594e987 | 49 | mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len; |
5d2e5f27 PE |
50 | |
51 | return 0; | |
52 | ||
53 | rtattr_failure: | |
54 | return -EMSGSIZE; | |
55 | } | |
56 | EXPORT_SYMBOL_GPL(sock_diag_put_meminfo); | |
57 | ||
8ef874bf PE |
58 | void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) |
59 | { | |
60 | mutex_lock(&sock_diag_table_mutex); | |
61 | inet_rcv_compat = fn; | |
62 | mutex_unlock(&sock_diag_table_mutex); | |
63 | } | |
64 | EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat); | |
65 | ||
66 | void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) | |
67 | { | |
68 | mutex_lock(&sock_diag_table_mutex); | |
69 | inet_rcv_compat = NULL; | |
70 | mutex_unlock(&sock_diag_table_mutex); | |
71 | } | |
72 | EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat); | |
73 | ||
8dcf01fc | 74 | int sock_diag_register(const struct sock_diag_handler *hndl) |
8ef874bf PE |
75 | { |
76 | int err = 0; | |
77 | ||
6f8e4ad0 | 78 | if (hndl->family >= AF_MAX) |
8ef874bf PE |
79 | return -EINVAL; |
80 | ||
81 | mutex_lock(&sock_diag_table_mutex); | |
82 | if (sock_diag_handlers[hndl->family]) | |
83 | err = -EBUSY; | |
84 | else | |
85 | sock_diag_handlers[hndl->family] = hndl; | |
86 | mutex_unlock(&sock_diag_table_mutex); | |
87 | ||
88 | return err; | |
89 | } | |
90 | EXPORT_SYMBOL_GPL(sock_diag_register); | |
91 | ||
8dcf01fc | 92 | void sock_diag_unregister(const struct sock_diag_handler *hnld) |
8ef874bf PE |
93 | { |
94 | int family = hnld->family; | |
95 | ||
6f8e4ad0 | 96 | if (family >= AF_MAX) |
8ef874bf PE |
97 | return; |
98 | ||
99 | mutex_lock(&sock_diag_table_mutex); | |
100 | BUG_ON(sock_diag_handlers[family] != hnld); | |
101 | sock_diag_handlers[family] = NULL; | |
102 | mutex_unlock(&sock_diag_table_mutex); | |
103 | } | |
104 | EXPORT_SYMBOL_GPL(sock_diag_unregister); | |
105 | ||
8dcf01fc | 106 | static const inline struct sock_diag_handler *sock_diag_lock_handler(int family) |
8ef874bf PE |
107 | { |
108 | if (sock_diag_handlers[family] == NULL) | |
109 | request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, | |
aec8dc62 | 110 | NETLINK_SOCK_DIAG, family); |
8ef874bf PE |
111 | |
112 | mutex_lock(&sock_diag_table_mutex); | |
113 | return sock_diag_handlers[family]; | |
114 | } | |
115 | ||
8dcf01fc | 116 | static inline void sock_diag_unlock_handler(const struct sock_diag_handler *h) |
8ef874bf PE |
117 | { |
118 | mutex_unlock(&sock_diag_table_mutex); | |
119 | } | |
120 | ||
121 | static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) | |
122 | { | |
123 | int err; | |
124 | struct sock_diag_req *req = NLMSG_DATA(nlh); | |
8dcf01fc | 125 | const struct sock_diag_handler *hndl; |
8ef874bf PE |
126 | |
127 | if (nlmsg_len(nlh) < sizeof(*req)) | |
128 | return -EINVAL; | |
129 | ||
130 | hndl = sock_diag_lock_handler(req->sdiag_family); | |
131 | if (hndl == NULL) | |
132 | err = -ENOENT; | |
133 | else | |
134 | err = hndl->dump(skb, nlh); | |
135 | sock_diag_unlock_handler(hndl); | |
136 | ||
137 | return err; | |
138 | } | |
139 | ||
140 | static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) | |
141 | { | |
142 | int ret; | |
143 | ||
144 | switch (nlh->nlmsg_type) { | |
145 | case TCPDIAG_GETSOCK: | |
146 | case DCCPDIAG_GETSOCK: | |
147 | if (inet_rcv_compat == NULL) | |
148 | request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, | |
aec8dc62 | 149 | NETLINK_SOCK_DIAG, AF_INET); |
8ef874bf PE |
150 | |
151 | mutex_lock(&sock_diag_table_mutex); | |
152 | if (inet_rcv_compat != NULL) | |
153 | ret = inet_rcv_compat(skb, nlh); | |
154 | else | |
155 | ret = -EOPNOTSUPP; | |
156 | mutex_unlock(&sock_diag_table_mutex); | |
157 | ||
158 | return ret; | |
159 | case SOCK_DIAG_BY_FAMILY: | |
160 | return __sock_diag_rcv_msg(skb, nlh); | |
161 | default: | |
162 | return -EINVAL; | |
163 | } | |
164 | } | |
165 | ||
166 | static DEFINE_MUTEX(sock_diag_mutex); | |
167 | ||
168 | static void sock_diag_rcv(struct sk_buff *skb) | |
169 | { | |
170 | mutex_lock(&sock_diag_mutex); | |
171 | netlink_rcv_skb(skb, &sock_diag_rcv_msg); | |
172 | mutex_unlock(&sock_diag_mutex); | |
173 | } | |
174 | ||
175 | struct sock *sock_diag_nlsk; | |
176 | EXPORT_SYMBOL_GPL(sock_diag_nlsk); | |
177 | ||
178 | static int __init sock_diag_init(void) | |
179 | { | |
180 | sock_diag_nlsk = netlink_kernel_create(&init_net, NETLINK_SOCK_DIAG, 0, | |
181 | sock_diag_rcv, NULL, THIS_MODULE); | |
182 | return sock_diag_nlsk == NULL ? -ENOMEM : 0; | |
183 | } | |
184 | ||
185 | static void __exit sock_diag_exit(void) | |
186 | { | |
187 | netlink_kernel_release(sock_diag_nlsk); | |
188 | } | |
189 | ||
190 | module_init(sock_diag_init); | |
191 | module_exit(sock_diag_exit); | |
192 | MODULE_LICENSE("GPL"); | |
193 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG); |