Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NETLINK Kernel-user communication protocol. |
| 3 | * |
| 4 | * Authors: Alan Cox <alan@redhat.com> |
| 5 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith |
| 13 | * added netlink_proto_exit |
| 14 | * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br> |
| 15 | * use nlk_sk, as sk->protinfo is on a diet 8) |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 16 | * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org> |
| 17 | * - inc module use count of module that owns |
| 18 | * the kernel socket in case userspace opens |
| 19 | * socket of same protocol |
| 20 | * - remove all module support, since netlink is |
| 21 | * mandatory if CONFIG_NET=y these days |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 26 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/kernel.h> |
| 28 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/signal.h> |
| 30 | #include <linux/sched.h> |
| 31 | #include <linux/errno.h> |
| 32 | #include <linux/string.h> |
| 33 | #include <linux/stat.h> |
| 34 | #include <linux/socket.h> |
| 35 | #include <linux/un.h> |
| 36 | #include <linux/fcntl.h> |
| 37 | #include <linux/termios.h> |
| 38 | #include <linux/sockios.h> |
| 39 | #include <linux/net.h> |
| 40 | #include <linux/fs.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <asm/uaccess.h> |
| 43 | #include <linux/skbuff.h> |
| 44 | #include <linux/netdevice.h> |
| 45 | #include <linux/rtnetlink.h> |
| 46 | #include <linux/proc_fs.h> |
| 47 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/notifier.h> |
| 49 | #include <linux/security.h> |
| 50 | #include <linux/jhash.h> |
| 51 | #include <linux/jiffies.h> |
| 52 | #include <linux/random.h> |
| 53 | #include <linux/bitops.h> |
| 54 | #include <linux/mm.h> |
| 55 | #include <linux/types.h> |
Andrew Morton | 54e0f52 | 2005-04-30 07:07:04 +0100 | [diff] [blame] | 56 | #include <linux/audit.h> |
Steve Grubb | e7c3497 | 2006-04-03 09:08:13 -0400 | [diff] [blame] | 57 | #include <linux/selinux.h> |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 58 | #include <linux/mutex.h> |
Andrew Morton | 54e0f52 | 2005-04-30 07:07:04 +0100 | [diff] [blame] | 59 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 60 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <net/sock.h> |
| 62 | #include <net/scm.h> |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 63 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 65 | #define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8) |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 66 | #define NLGRPLONGS(x) (NLGRPSZ(x)/sizeof(unsigned long)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | struct netlink_sock { |
| 69 | /* struct sock has to be the first member of netlink_sock */ |
| 70 | struct sock sk; |
| 71 | u32 pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | u32 dst_pid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 73 | u32 dst_group; |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 74 | u32 flags; |
| 75 | u32 subscriptions; |
| 76 | u32 ngroups; |
| 77 | unsigned long *groups; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | unsigned long state; |
| 79 | wait_queue_head_t wait; |
| 80 | struct netlink_callback *cb; |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 81 | struct mutex *cb_mutex; |
| 82 | struct mutex cb_def_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | void (*data_ready)(struct sock *sk, int bytes); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 84 | struct module *module; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 87 | #define NETLINK_KERNEL_SOCKET 0x1 |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 88 | #define NETLINK_RECV_PKTINFO 0x2 |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | static inline struct netlink_sock *nlk_sk(struct sock *sk) |
| 91 | { |
Denis Cheng | 32b21e0 | 2007-08-28 15:41:11 -0700 | [diff] [blame] | 92 | return container_of(sk, struct netlink_sock, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Denis V. Lunev | aed8156 | 2007-10-10 21:14:32 -0700 | [diff] [blame^] | 95 | static inline int netlink_is_kernel(struct sock *sk) |
| 96 | { |
| 97 | return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET; |
| 98 | } |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | struct nl_pid_hash { |
| 101 | struct hlist_head *table; |
| 102 | unsigned long rehash_time; |
| 103 | |
| 104 | unsigned int mask; |
| 105 | unsigned int shift; |
| 106 | |
| 107 | unsigned int entries; |
| 108 | unsigned int max_shift; |
| 109 | |
| 110 | u32 rnd; |
| 111 | }; |
| 112 | |
| 113 | struct netlink_table { |
| 114 | struct nl_pid_hash hash; |
| 115 | struct hlist_head mc_list; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 116 | unsigned long *listeners; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | unsigned int nl_nonroot; |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 118 | unsigned int groups; |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 119 | struct mutex *cb_mutex; |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 120 | struct module *module; |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 121 | int registered; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | static struct netlink_table *nl_table; |
| 125 | |
| 126 | static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait); |
| 127 | |
| 128 | static int netlink_dump(struct sock *sk); |
| 129 | static void netlink_destroy_callback(struct netlink_callback *cb); |
Adrian Bunk | 42bad1d | 2007-04-26 00:57:41 -0700 | [diff] [blame] | 130 | static void netlink_queue_skip(struct nlmsghdr *nlh, struct sk_buff *skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | static DEFINE_RWLOCK(nl_table_lock); |
| 133 | static atomic_t nl_table_users = ATOMIC_INIT(0); |
| 134 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 135 | static ATOMIC_NOTIFIER_HEAD(netlink_chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 137 | static u32 netlink_group_mask(u32 group) |
| 138 | { |
| 139 | return group ? 1 << (group - 1) : 0; |
| 140 | } |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid) |
| 143 | { |
| 144 | return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask]; |
| 145 | } |
| 146 | |
| 147 | static void netlink_sock_destruct(struct sock *sk) |
| 148 | { |
Herbert Xu | 3f660d6 | 2007-05-03 03:17:14 -0700 | [diff] [blame] | 149 | struct netlink_sock *nlk = nlk_sk(sk); |
| 150 | |
Herbert Xu | 3f660d6 | 2007-05-03 03:17:14 -0700 | [diff] [blame] | 151 | if (nlk->cb) { |
| 152 | if (nlk->cb->done) |
| 153 | nlk->cb->done(nlk->cb); |
| 154 | netlink_destroy_callback(nlk->cb); |
| 155 | } |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | skb_queue_purge(&sk->sk_receive_queue); |
| 158 | |
| 159 | if (!sock_flag(sk, SOCK_DEAD)) { |
| 160 | printk("Freeing alive netlink socket %p\n", sk); |
| 161 | return; |
| 162 | } |
| 163 | BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc)); |
| 164 | BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc)); |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 165 | BUG_TRAP(!nlk_sk(sk)->groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP. |
| 169 | * Look, when several writers sleep and reader wakes them up, all but one |
| 170 | * immediately hit write lock and grab all the cpus. Exclusive sleep solves |
| 171 | * this, _but_ remember, it adds useless work on UP machines. |
| 172 | */ |
| 173 | |
| 174 | static void netlink_table_grab(void) |
| 175 | { |
Arjan van de Ven | 6abd219 | 2006-07-03 00:24:07 -0700 | [diff] [blame] | 176 | write_lock_irq(&nl_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
| 178 | if (atomic_read(&nl_table_users)) { |
| 179 | DECLARE_WAITQUEUE(wait, current); |
| 180 | |
| 181 | add_wait_queue_exclusive(&nl_table_wait, &wait); |
| 182 | for(;;) { |
| 183 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 184 | if (atomic_read(&nl_table_users) == 0) |
| 185 | break; |
Arjan van de Ven | 6abd219 | 2006-07-03 00:24:07 -0700 | [diff] [blame] | 186 | write_unlock_irq(&nl_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | schedule(); |
Arjan van de Ven | 6abd219 | 2006-07-03 00:24:07 -0700 | [diff] [blame] | 188 | write_lock_irq(&nl_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | __set_current_state(TASK_RUNNING); |
| 192 | remove_wait_queue(&nl_table_wait, &wait); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static __inline__ void netlink_table_ungrab(void) |
| 197 | { |
Arjan van de Ven | 6abd219 | 2006-07-03 00:24:07 -0700 | [diff] [blame] | 198 | write_unlock_irq(&nl_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | wake_up(&nl_table_wait); |
| 200 | } |
| 201 | |
| 202 | static __inline__ void |
| 203 | netlink_lock_table(void) |
| 204 | { |
| 205 | /* read_lock() synchronizes us to netlink_table_grab */ |
| 206 | |
| 207 | read_lock(&nl_table_lock); |
| 208 | atomic_inc(&nl_table_users); |
| 209 | read_unlock(&nl_table_lock); |
| 210 | } |
| 211 | |
| 212 | static __inline__ void |
| 213 | netlink_unlock_table(void) |
| 214 | { |
| 215 | if (atomic_dec_and_test(&nl_table_users)) |
| 216 | wake_up(&nl_table_wait); |
| 217 | } |
| 218 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 219 | static __inline__ struct sock *netlink_lookup(struct net *net, int protocol, u32 pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
| 221 | struct nl_pid_hash *hash = &nl_table[protocol].hash; |
| 222 | struct hlist_head *head; |
| 223 | struct sock *sk; |
| 224 | struct hlist_node *node; |
| 225 | |
| 226 | read_lock(&nl_table_lock); |
| 227 | head = nl_pid_hashfn(hash, pid); |
| 228 | sk_for_each(sk, node, head) { |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 229 | if ((sk->sk_net == net) && (nlk_sk(sk)->pid == pid)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | sock_hold(sk); |
| 231 | goto found; |
| 232 | } |
| 233 | } |
| 234 | sk = NULL; |
| 235 | found: |
| 236 | read_unlock(&nl_table_lock); |
| 237 | return sk; |
| 238 | } |
| 239 | |
| 240 | static inline struct hlist_head *nl_pid_hash_alloc(size_t size) |
| 241 | { |
| 242 | if (size <= PAGE_SIZE) |
| 243 | return kmalloc(size, GFP_ATOMIC); |
| 244 | else |
| 245 | return (struct hlist_head *) |
| 246 | __get_free_pages(GFP_ATOMIC, get_order(size)); |
| 247 | } |
| 248 | |
| 249 | static inline void nl_pid_hash_free(struct hlist_head *table, size_t size) |
| 250 | { |
| 251 | if (size <= PAGE_SIZE) |
| 252 | kfree(table); |
| 253 | else |
| 254 | free_pages((unsigned long)table, get_order(size)); |
| 255 | } |
| 256 | |
| 257 | static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow) |
| 258 | { |
| 259 | unsigned int omask, mask, shift; |
| 260 | size_t osize, size; |
| 261 | struct hlist_head *otable, *table; |
| 262 | int i; |
| 263 | |
| 264 | omask = mask = hash->mask; |
| 265 | osize = size = (mask + 1) * sizeof(*table); |
| 266 | shift = hash->shift; |
| 267 | |
| 268 | if (grow) { |
| 269 | if (++shift > hash->max_shift) |
| 270 | return 0; |
| 271 | mask = mask * 2 + 1; |
| 272 | size *= 2; |
| 273 | } |
| 274 | |
| 275 | table = nl_pid_hash_alloc(size); |
| 276 | if (!table) |
| 277 | return 0; |
| 278 | |
| 279 | memset(table, 0, size); |
| 280 | otable = hash->table; |
| 281 | hash->table = table; |
| 282 | hash->mask = mask; |
| 283 | hash->shift = shift; |
| 284 | get_random_bytes(&hash->rnd, sizeof(hash->rnd)); |
| 285 | |
| 286 | for (i = 0; i <= omask; i++) { |
| 287 | struct sock *sk; |
| 288 | struct hlist_node *node, *tmp; |
| 289 | |
| 290 | sk_for_each_safe(sk, node, tmp, &otable[i]) |
| 291 | __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid)); |
| 292 | } |
| 293 | |
| 294 | nl_pid_hash_free(otable, osize); |
| 295 | hash->rehash_time = jiffies + 10 * 60 * HZ; |
| 296 | return 1; |
| 297 | } |
| 298 | |
| 299 | static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len) |
| 300 | { |
| 301 | int avg = hash->entries >> hash->shift; |
| 302 | |
| 303 | if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1)) |
| 304 | return 1; |
| 305 | |
| 306 | if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) { |
| 307 | nl_pid_hash_rehash(hash, 0); |
| 308 | return 1; |
| 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 314 | static const struct proto_ops netlink_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 316 | static void |
| 317 | netlink_update_listeners(struct sock *sk) |
| 318 | { |
| 319 | struct netlink_table *tbl = &nl_table[sk->sk_protocol]; |
| 320 | struct hlist_node *node; |
| 321 | unsigned long mask; |
| 322 | unsigned int i; |
| 323 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 324 | for (i = 0; i < NLGRPLONGS(tbl->groups); i++) { |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 325 | mask = 0; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 326 | sk_for_each_bound(sk, node, &tbl->mc_list) { |
| 327 | if (i < NLGRPLONGS(nlk_sk(sk)->ngroups)) |
| 328 | mask |= nlk_sk(sk)->groups[i]; |
| 329 | } |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 330 | tbl->listeners[i] = mask; |
| 331 | } |
| 332 | /* this function is only called with the netlink table "grabbed", which |
| 333 | * makes sure updates are visible before bind or setsockopt return. */ |
| 334 | } |
| 335 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 336 | static int netlink_insert(struct sock *sk, struct net *net, u32 pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
| 338 | struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash; |
| 339 | struct hlist_head *head; |
| 340 | int err = -EADDRINUSE; |
| 341 | struct sock *osk; |
| 342 | struct hlist_node *node; |
| 343 | int len; |
| 344 | |
| 345 | netlink_table_grab(); |
| 346 | head = nl_pid_hashfn(hash, pid); |
| 347 | len = 0; |
| 348 | sk_for_each(osk, node, head) { |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 349 | if ((osk->sk_net == net) && (nlk_sk(osk)->pid == pid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | break; |
| 351 | len++; |
| 352 | } |
| 353 | if (node) |
| 354 | goto err; |
| 355 | |
| 356 | err = -EBUSY; |
| 357 | if (nlk_sk(sk)->pid) |
| 358 | goto err; |
| 359 | |
| 360 | err = -ENOMEM; |
| 361 | if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX)) |
| 362 | goto err; |
| 363 | |
| 364 | if (len && nl_pid_hash_dilute(hash, len)) |
| 365 | head = nl_pid_hashfn(hash, pid); |
| 366 | hash->entries++; |
| 367 | nlk_sk(sk)->pid = pid; |
| 368 | sk_add_node(sk, head); |
| 369 | err = 0; |
| 370 | |
| 371 | err: |
| 372 | netlink_table_ungrab(); |
| 373 | return err; |
| 374 | } |
| 375 | |
| 376 | static void netlink_remove(struct sock *sk) |
| 377 | { |
| 378 | netlink_table_grab(); |
David S. Miller | d470e3b | 2005-06-26 15:31:51 -0700 | [diff] [blame] | 379 | if (sk_del_node_init(sk)) |
| 380 | nl_table[sk->sk_protocol].hash.entries--; |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 381 | if (nlk_sk(sk)->subscriptions) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | __sk_del_bind_node(sk); |
| 383 | netlink_table_ungrab(); |
| 384 | } |
| 385 | |
| 386 | static struct proto netlink_proto = { |
| 387 | .name = "NETLINK", |
| 388 | .owner = THIS_MODULE, |
| 389 | .obj_size = sizeof(struct netlink_sock), |
| 390 | }; |
| 391 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 392 | static int __netlink_create(struct net *net, struct socket *sock, |
| 393 | struct mutex *cb_mutex, int protocol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
| 395 | struct sock *sk; |
| 396 | struct netlink_sock *nlk; |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 397 | |
| 398 | sock->ops = &netlink_ops; |
| 399 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 400 | sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto, 1); |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 401 | if (!sk) |
| 402 | return -ENOMEM; |
| 403 | |
| 404 | sock_init_data(sock, sk); |
| 405 | |
| 406 | nlk = nlk_sk(sk); |
Patrick McHardy | ffa4d72 | 2007-04-25 14:01:17 -0700 | [diff] [blame] | 407 | if (cb_mutex) |
| 408 | nlk->cb_mutex = cb_mutex; |
| 409 | else { |
| 410 | nlk->cb_mutex = &nlk->cb_def_mutex; |
| 411 | mutex_init(nlk->cb_mutex); |
| 412 | } |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 413 | init_waitqueue_head(&nlk->wait); |
| 414 | |
| 415 | sk->sk_destruct = netlink_sock_destruct; |
| 416 | sk->sk_protocol = protocol; |
| 417 | return 0; |
| 418 | } |
| 419 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 420 | static int netlink_create(struct net *net, struct socket *sock, int protocol) |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 421 | { |
| 422 | struct module *module = NULL; |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 423 | struct mutex *cb_mutex; |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 424 | struct netlink_sock *nlk; |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 425 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | sock->state = SS_UNCONNECTED; |
| 428 | |
| 429 | if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM) |
| 430 | return -ESOCKTNOSUPPORT; |
| 431 | |
| 432 | if (protocol<0 || protocol >= MAX_LINKS) |
| 433 | return -EPROTONOSUPPORT; |
| 434 | |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 435 | netlink_lock_table(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 436 | #ifdef CONFIG_KMOD |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 437 | if (!nl_table[protocol].registered) { |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 438 | netlink_unlock_table(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 439 | request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 440 | netlink_lock_table(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 441 | } |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 442 | #endif |
| 443 | if (nl_table[protocol].registered && |
| 444 | try_module_get(nl_table[protocol].module)) |
| 445 | module = nl_table[protocol].module; |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 446 | cb_mutex = nl_table[protocol].cb_mutex; |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 447 | netlink_unlock_table(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 448 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 449 | if ((err = __netlink_create(net, sock, cb_mutex, protocol)) < 0) |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 450 | goto out_module; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 452 | nlk = nlk_sk(sock->sk); |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 453 | nlk->module = module; |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 454 | out: |
| 455 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 457 | out_module: |
| 458 | module_put(module); |
| 459 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static int netlink_release(struct socket *sock) |
| 463 | { |
| 464 | struct sock *sk = sock->sk; |
| 465 | struct netlink_sock *nlk; |
| 466 | |
| 467 | if (!sk) |
| 468 | return 0; |
| 469 | |
| 470 | netlink_remove(sk); |
Denis Lunev | ac57b3a | 2007-04-18 17:05:58 -0700 | [diff] [blame] | 471 | sock_orphan(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | nlk = nlk_sk(sk); |
| 473 | |
Herbert Xu | 3f660d6 | 2007-05-03 03:17:14 -0700 | [diff] [blame] | 474 | /* |
| 475 | * OK. Socket is unlinked, any packets that arrive now |
| 476 | * will be purged. |
| 477 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | sock->sk = NULL; |
| 480 | wake_up_interruptible_all(&nlk->wait); |
| 481 | |
| 482 | skb_queue_purge(&sk->sk_write_queue); |
| 483 | |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 484 | if (nlk->pid && !nlk->subscriptions) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | struct netlink_notify n = { |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 486 | .net = sk->sk_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | .protocol = sk->sk_protocol, |
| 488 | .pid = nlk->pid, |
| 489 | }; |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 490 | atomic_notifier_call_chain(&netlink_chain, |
| 491 | NETLINK_URELEASE, &n); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 492 | } |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 493 | |
Mariusz Kozlowski | 5e7c001 | 2007-01-02 15:24:30 -0800 | [diff] [blame] | 494 | module_put(nlk->module); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 495 | |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 496 | netlink_table_grab(); |
Denis V. Lunev | aed8156 | 2007-10-10 21:14:32 -0700 | [diff] [blame^] | 497 | if (netlink_is_kernel(sk)) { |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 498 | kfree(nl_table[sk->sk_protocol].listeners); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 499 | nl_table[sk->sk_protocol].module = NULL; |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 500 | nl_table[sk->sk_protocol].registered = 0; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 501 | } else if (nlk->subscriptions) |
| 502 | netlink_update_listeners(sk); |
| 503 | netlink_table_ungrab(); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 504 | |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 505 | kfree(nlk->groups); |
| 506 | nlk->groups = NULL; |
| 507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | sock_put(sk); |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static int netlink_autobind(struct socket *sock) |
| 513 | { |
| 514 | struct sock *sk = sock->sk; |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 515 | struct net *net = sk->sk_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash; |
| 517 | struct hlist_head *head; |
| 518 | struct sock *osk; |
| 519 | struct hlist_node *node; |
Herbert Xu | c27bd49 | 2005-11-22 14:41:50 -0800 | [diff] [blame] | 520 | s32 pid = current->tgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | int err; |
| 522 | static s32 rover = -4097; |
| 523 | |
| 524 | retry: |
| 525 | cond_resched(); |
| 526 | netlink_table_grab(); |
| 527 | head = nl_pid_hashfn(hash, pid); |
| 528 | sk_for_each(osk, node, head) { |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 529 | if ((osk->sk_net != net)) |
| 530 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | if (nlk_sk(osk)->pid == pid) { |
| 532 | /* Bind collision, search negative pid values. */ |
| 533 | pid = rover--; |
| 534 | if (rover > -4097) |
| 535 | rover = -4097; |
| 536 | netlink_table_ungrab(); |
| 537 | goto retry; |
| 538 | } |
| 539 | } |
| 540 | netlink_table_ungrab(); |
| 541 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 542 | err = netlink_insert(sk, net, pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | if (err == -EADDRINUSE) |
| 544 | goto retry; |
David S. Miller | d470e3b | 2005-06-26 15:31:51 -0700 | [diff] [blame] | 545 | |
| 546 | /* If 2 threads race to autobind, that is fine. */ |
| 547 | if (err == -EBUSY) |
| 548 | err = 0; |
| 549 | |
| 550 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 553 | static inline int netlink_capable(struct socket *sock, unsigned int flag) |
| 554 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) || |
| 556 | capable(CAP_NET_ADMIN); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 559 | static void |
| 560 | netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions) |
| 561 | { |
| 562 | struct netlink_sock *nlk = nlk_sk(sk); |
| 563 | |
| 564 | if (nlk->subscriptions && !subscriptions) |
| 565 | __sk_del_bind_node(sk); |
| 566 | else if (!nlk->subscriptions && subscriptions) |
| 567 | sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list); |
| 568 | nlk->subscriptions = subscriptions; |
| 569 | } |
| 570 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 571 | static int netlink_realloc_groups(struct sock *sk) |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 572 | { |
| 573 | struct netlink_sock *nlk = nlk_sk(sk); |
| 574 | unsigned int groups; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 575 | unsigned long *new_groups; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 576 | int err = 0; |
| 577 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 578 | netlink_table_grab(); |
| 579 | |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 580 | groups = nl_table[sk->sk_protocol].groups; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 581 | if (!nl_table[sk->sk_protocol].registered) { |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 582 | err = -ENOENT; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 583 | goto out_unlock; |
| 584 | } |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 585 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 586 | if (nlk->ngroups >= groups) |
| 587 | goto out_unlock; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 588 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 589 | new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC); |
| 590 | if (new_groups == NULL) { |
| 591 | err = -ENOMEM; |
| 592 | goto out_unlock; |
| 593 | } |
| 594 | memset((char*)new_groups + NLGRPSZ(nlk->ngroups), 0, |
| 595 | NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups)); |
| 596 | |
| 597 | nlk->groups = new_groups; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 598 | nlk->ngroups = groups; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 599 | out_unlock: |
| 600 | netlink_table_ungrab(); |
| 601 | return err; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len) |
| 605 | { |
| 606 | struct sock *sk = sock->sk; |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 607 | struct net *net = sk->sk_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | struct netlink_sock *nlk = nlk_sk(sk); |
| 609 | struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr; |
| 610 | int err; |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | if (nladdr->nl_family != AF_NETLINK) |
| 613 | return -EINVAL; |
| 614 | |
| 615 | /* Only superuser is allowed to listen multicasts */ |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 616 | if (nladdr->nl_groups) { |
| 617 | if (!netlink_capable(sock, NL_NONROOT_RECV)) |
| 618 | return -EPERM; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 619 | err = netlink_realloc_groups(sk); |
| 620 | if (err) |
| 621 | return err; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 622 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
| 624 | if (nlk->pid) { |
| 625 | if (nladdr->nl_pid != nlk->pid) |
| 626 | return -EINVAL; |
| 627 | } else { |
| 628 | err = nladdr->nl_pid ? |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 629 | netlink_insert(sk, net, nladdr->nl_pid) : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | netlink_autobind(sock); |
| 631 | if (err) |
| 632 | return err; |
| 633 | } |
| 634 | |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 635 | if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | return 0; |
| 637 | |
| 638 | netlink_table_grab(); |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 639 | netlink_update_subscriptions(sk, nlk->subscriptions + |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 640 | hweight32(nladdr->nl_groups) - |
| 641 | hweight32(nlk->groups[0])); |
| 642 | nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 643 | netlink_update_listeners(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | netlink_table_ungrab(); |
| 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | static int netlink_connect(struct socket *sock, struct sockaddr *addr, |
| 650 | int alen, int flags) |
| 651 | { |
| 652 | int err = 0; |
| 653 | struct sock *sk = sock->sk; |
| 654 | struct netlink_sock *nlk = nlk_sk(sk); |
| 655 | struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr; |
| 656 | |
| 657 | if (addr->sa_family == AF_UNSPEC) { |
| 658 | sk->sk_state = NETLINK_UNCONNECTED; |
| 659 | nlk->dst_pid = 0; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 660 | nlk->dst_group = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | return 0; |
| 662 | } |
| 663 | if (addr->sa_family != AF_NETLINK) |
| 664 | return -EINVAL; |
| 665 | |
| 666 | /* Only superuser is allowed to send multicasts */ |
| 667 | if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND)) |
| 668 | return -EPERM; |
| 669 | |
| 670 | if (!nlk->pid) |
| 671 | err = netlink_autobind(sock); |
| 672 | |
| 673 | if (err == 0) { |
| 674 | sk->sk_state = NETLINK_CONNECTED; |
| 675 | nlk->dst_pid = nladdr->nl_pid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 676 | nlk->dst_group = ffs(nladdr->nl_groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | return err; |
| 680 | } |
| 681 | |
| 682 | static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer) |
| 683 | { |
| 684 | struct sock *sk = sock->sk; |
| 685 | struct netlink_sock *nlk = nlk_sk(sk); |
| 686 | struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr; |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | nladdr->nl_family = AF_NETLINK; |
| 689 | nladdr->nl_pad = 0; |
| 690 | *addr_len = sizeof(*nladdr); |
| 691 | |
| 692 | if (peer) { |
| 693 | nladdr->nl_pid = nlk->dst_pid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 694 | nladdr->nl_groups = netlink_group_mask(nlk->dst_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } else { |
| 696 | nladdr->nl_pid = nlk->pid; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 697 | nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | static void netlink_overrun(struct sock *sk) |
| 703 | { |
| 704 | if (!test_and_set_bit(0, &nlk_sk(sk)->state)) { |
| 705 | sk->sk_err = ENOBUFS; |
| 706 | sk->sk_error_report(sk); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid) |
| 711 | { |
| 712 | int protocol = ssk->sk_protocol; |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 713 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | struct sock *sock; |
| 715 | struct netlink_sock *nlk; |
| 716 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 717 | net = ssk->sk_net; |
| 718 | sock = netlink_lookup(net, protocol, pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | if (!sock) |
| 720 | return ERR_PTR(-ECONNREFUSED); |
| 721 | |
| 722 | /* Don't bother queuing skb if kernel socket has no input function */ |
| 723 | nlk = nlk_sk(sock); |
Denis V. Lunev | aed8156 | 2007-10-10 21:14:32 -0700 | [diff] [blame^] | 724 | if ((netlink_is_kernel(sock) && !nlk->data_ready) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | (sock->sk_state == NETLINK_CONNECTED && |
| 726 | nlk->dst_pid != nlk_sk(ssk)->pid)) { |
| 727 | sock_put(sock); |
| 728 | return ERR_PTR(-ECONNREFUSED); |
| 729 | } |
| 730 | return sock; |
| 731 | } |
| 732 | |
| 733 | struct sock *netlink_getsockbyfilp(struct file *filp) |
| 734 | { |
Josef Sipek | 6db5fc5 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 735 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | struct sock *sock; |
| 737 | |
| 738 | if (!S_ISSOCK(inode->i_mode)) |
| 739 | return ERR_PTR(-ENOTSOCK); |
| 740 | |
| 741 | sock = SOCKET_I(inode)->sk; |
| 742 | if (sock->sk_family != AF_NETLINK) |
| 743 | return ERR_PTR(-EINVAL); |
| 744 | |
| 745 | sock_hold(sock); |
| 746 | return sock; |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * Attach a skb to a netlink socket. |
| 751 | * The caller must hold a reference to the destination socket. On error, the |
| 752 | * reference is dropped. The skb is not send to the destination, just all |
| 753 | * all error checks are performed and memory in the queue is reserved. |
| 754 | * Return values: |
| 755 | * < 0: error. skb freed, reference to sock dropped. |
| 756 | * 0: continue |
| 757 | * 1: repeat lookup - reference dropped while waiting for socket memory. |
| 758 | */ |
Alexey Kuznetsov | a70ea99 | 2006-02-09 16:40:11 -0800 | [diff] [blame] | 759 | int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock, |
| 760 | long timeo, struct sock *ssk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | { |
| 762 | struct netlink_sock *nlk; |
| 763 | |
| 764 | nlk = nlk_sk(sk); |
| 765 | |
| 766 | if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || |
| 767 | test_bit(0, &nlk->state)) { |
| 768 | DECLARE_WAITQUEUE(wait, current); |
| 769 | if (!timeo) { |
Denis V. Lunev | aed8156 | 2007-10-10 21:14:32 -0700 | [diff] [blame^] | 770 | if (!ssk || netlink_is_kernel(ssk)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | netlink_overrun(sk); |
| 772 | sock_put(sk); |
| 773 | kfree_skb(skb); |
| 774 | return -EAGAIN; |
| 775 | } |
| 776 | |
| 777 | __set_current_state(TASK_INTERRUPTIBLE); |
| 778 | add_wait_queue(&nlk->wait, &wait); |
| 779 | |
| 780 | if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || |
| 781 | test_bit(0, &nlk->state)) && |
| 782 | !sock_flag(sk, SOCK_DEAD)) |
| 783 | timeo = schedule_timeout(timeo); |
| 784 | |
| 785 | __set_current_state(TASK_RUNNING); |
| 786 | remove_wait_queue(&nlk->wait, &wait); |
| 787 | sock_put(sk); |
| 788 | |
| 789 | if (signal_pending(current)) { |
| 790 | kfree_skb(skb); |
| 791 | return sock_intr_errno(timeo); |
| 792 | } |
| 793 | return 1; |
| 794 | } |
| 795 | skb_set_owner_r(skb, sk); |
| 796 | return 0; |
| 797 | } |
| 798 | |
Denis V. Lunev | 7ee015e | 2007-10-10 21:14:03 -0700 | [diff] [blame] | 799 | int netlink_sendskb(struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | int len = skb->len; |
| 802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | skb_queue_tail(&sk->sk_receive_queue, skb); |
| 804 | sk->sk_data_ready(sk, len); |
| 805 | sock_put(sk); |
| 806 | return len; |
| 807 | } |
| 808 | |
| 809 | void netlink_detachskb(struct sock *sk, struct sk_buff *skb) |
| 810 | { |
| 811 | kfree_skb(skb); |
| 812 | sock_put(sk); |
| 813 | } |
| 814 | |
Victor Fusco | 37da647 | 2005-07-18 13:35:43 -0700 | [diff] [blame] | 815 | static inline struct sk_buff *netlink_trim(struct sk_buff *skb, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 816 | gfp_t allocation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | { |
| 818 | int delta; |
| 819 | |
| 820 | skb_orphan(skb); |
| 821 | |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 822 | delta = skb->end - skb->tail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | if (delta * 2 < skb->truesize) |
| 824 | return skb; |
| 825 | |
| 826 | if (skb_shared(skb)) { |
| 827 | struct sk_buff *nskb = skb_clone(skb, allocation); |
| 828 | if (!nskb) |
| 829 | return skb; |
| 830 | kfree_skb(skb); |
| 831 | skb = nskb; |
| 832 | } |
| 833 | |
| 834 | if (!pskb_expand_head(skb, 0, -delta, allocation)) |
| 835 | skb->truesize -= delta; |
| 836 | |
| 837 | return skb; |
| 838 | } |
| 839 | |
| 840 | int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock) |
| 841 | { |
| 842 | struct sock *sk; |
| 843 | int err; |
| 844 | long timeo; |
| 845 | |
| 846 | skb = netlink_trim(skb, gfp_any()); |
| 847 | |
| 848 | timeo = sock_sndtimeo(ssk, nonblock); |
| 849 | retry: |
| 850 | sk = netlink_getsockbypid(ssk, pid); |
| 851 | if (IS_ERR(sk)) { |
| 852 | kfree_skb(skb); |
| 853 | return PTR_ERR(sk); |
| 854 | } |
Alexey Kuznetsov | a70ea99 | 2006-02-09 16:40:11 -0800 | [diff] [blame] | 855 | err = netlink_attachskb(sk, skb, nonblock, timeo, ssk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | if (err == 1) |
| 857 | goto retry; |
| 858 | if (err) |
| 859 | return err; |
| 860 | |
Denis V. Lunev | 7ee015e | 2007-10-10 21:14:03 -0700 | [diff] [blame] | 861 | return netlink_sendskb(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 864 | int netlink_has_listeners(struct sock *sk, unsigned int group) |
| 865 | { |
| 866 | int res = 0; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 867 | unsigned long *listeners; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 868 | |
Denis V. Lunev | aed8156 | 2007-10-10 21:14:32 -0700 | [diff] [blame^] | 869 | BUG_ON(!netlink_is_kernel(sk)); |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 870 | |
| 871 | rcu_read_lock(); |
| 872 | listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners); |
| 873 | |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 874 | if (group - 1 < nl_table[sk->sk_protocol].groups) |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 875 | res = test_bit(group - 1, listeners); |
| 876 | |
| 877 | rcu_read_unlock(); |
| 878 | |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 879 | return res; |
| 880 | } |
| 881 | EXPORT_SYMBOL_GPL(netlink_has_listeners); |
| 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb) |
| 884 | { |
| 885 | struct netlink_sock *nlk = nlk_sk(sk); |
| 886 | |
| 887 | if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf && |
| 888 | !test_bit(0, &nlk->state)) { |
| 889 | skb_set_owner_r(skb, sk); |
| 890 | skb_queue_tail(&sk->sk_receive_queue, skb); |
| 891 | sk->sk_data_ready(sk, skb->len); |
| 892 | return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf; |
| 893 | } |
| 894 | return -1; |
| 895 | } |
| 896 | |
| 897 | struct netlink_broadcast_data { |
| 898 | struct sock *exclude_sk; |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 899 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | u32 pid; |
| 901 | u32 group; |
| 902 | int failure; |
| 903 | int congested; |
| 904 | int delivered; |
Al Viro | 7d877f3 | 2005-10-21 03:20:43 -0400 | [diff] [blame] | 905 | gfp_t allocation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | struct sk_buff *skb, *skb2; |
| 907 | }; |
| 908 | |
| 909 | static inline int do_one_broadcast(struct sock *sk, |
| 910 | struct netlink_broadcast_data *p) |
| 911 | { |
| 912 | struct netlink_sock *nlk = nlk_sk(sk); |
| 913 | int val; |
| 914 | |
| 915 | if (p->exclude_sk == sk) |
| 916 | goto out; |
| 917 | |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 918 | if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups || |
| 919 | !test_bit(p->group - 1, nlk->groups)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | goto out; |
| 921 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 922 | if ((sk->sk_net != p->net)) |
| 923 | goto out; |
| 924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | if (p->failure) { |
| 926 | netlink_overrun(sk); |
| 927 | goto out; |
| 928 | } |
| 929 | |
| 930 | sock_hold(sk); |
| 931 | if (p->skb2 == NULL) { |
Tommy S. Christensen | 68acc02 | 2005-05-19 13:06:35 -0700 | [diff] [blame] | 932 | if (skb_shared(p->skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | p->skb2 = skb_clone(p->skb, p->allocation); |
| 934 | } else { |
Tommy S. Christensen | 68acc02 | 2005-05-19 13:06:35 -0700 | [diff] [blame] | 935 | p->skb2 = skb_get(p->skb); |
| 936 | /* |
| 937 | * skb ownership may have been set when |
| 938 | * delivered to a previous socket. |
| 939 | */ |
| 940 | skb_orphan(p->skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | if (p->skb2 == NULL) { |
| 944 | netlink_overrun(sk); |
| 945 | /* Clone failed. Notify ALL listeners. */ |
| 946 | p->failure = 1; |
| 947 | } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) { |
| 948 | netlink_overrun(sk); |
| 949 | } else { |
| 950 | p->congested |= val; |
| 951 | p->delivered = 1; |
| 952 | p->skb2 = NULL; |
| 953 | } |
| 954 | sock_put(sk); |
| 955 | |
| 956 | out: |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 961 | u32 group, gfp_t allocation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | { |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 963 | struct net *net = ssk->sk_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | struct netlink_broadcast_data info; |
| 965 | struct hlist_node *node; |
| 966 | struct sock *sk; |
| 967 | |
| 968 | skb = netlink_trim(skb, allocation); |
| 969 | |
| 970 | info.exclude_sk = ssk; |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 971 | info.net = net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | info.pid = pid; |
| 973 | info.group = group; |
| 974 | info.failure = 0; |
| 975 | info.congested = 0; |
| 976 | info.delivered = 0; |
| 977 | info.allocation = allocation; |
| 978 | info.skb = skb; |
| 979 | info.skb2 = NULL; |
| 980 | |
| 981 | /* While we sleep in clone, do not allow to change socket list */ |
| 982 | |
| 983 | netlink_lock_table(); |
| 984 | |
| 985 | sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list) |
| 986 | do_one_broadcast(sk, &info); |
| 987 | |
Tommy S. Christensen | aa1c6a6 | 2005-05-19 13:07:32 -0700 | [diff] [blame] | 988 | kfree_skb(skb); |
| 989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | netlink_unlock_table(); |
| 991 | |
| 992 | if (info.skb2) |
| 993 | kfree_skb(info.skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | if (info.delivered) { |
| 996 | if (info.congested && (allocation & __GFP_WAIT)) |
| 997 | yield(); |
| 998 | return 0; |
| 999 | } |
| 1000 | if (info.failure) |
| 1001 | return -ENOBUFS; |
| 1002 | return -ESRCH; |
| 1003 | } |
| 1004 | |
| 1005 | struct netlink_set_err_data { |
| 1006 | struct sock *exclude_sk; |
| 1007 | u32 pid; |
| 1008 | u32 group; |
| 1009 | int code; |
| 1010 | }; |
| 1011 | |
| 1012 | static inline int do_one_set_err(struct sock *sk, |
| 1013 | struct netlink_set_err_data *p) |
| 1014 | { |
| 1015 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1016 | |
| 1017 | if (sk == p->exclude_sk) |
| 1018 | goto out; |
| 1019 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1020 | if (sk->sk_net != p->exclude_sk->sk_net) |
| 1021 | goto out; |
| 1022 | |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1023 | if (nlk->pid == p->pid || p->group - 1 >= nlk->ngroups || |
| 1024 | !test_bit(p->group - 1, nlk->groups)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | goto out; |
| 1026 | |
| 1027 | sk->sk_err = p->code; |
| 1028 | sk->sk_error_report(sk); |
| 1029 | out: |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code) |
| 1034 | { |
| 1035 | struct netlink_set_err_data info; |
| 1036 | struct hlist_node *node; |
| 1037 | struct sock *sk; |
| 1038 | |
| 1039 | info.exclude_sk = ssk; |
| 1040 | info.pid = pid; |
| 1041 | info.group = group; |
| 1042 | info.code = code; |
| 1043 | |
| 1044 | read_lock(&nl_table_lock); |
| 1045 | |
| 1046 | sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list) |
| 1047 | do_one_set_err(sk, &info); |
| 1048 | |
| 1049 | read_unlock(&nl_table_lock); |
| 1050 | } |
| 1051 | |
Johannes Berg | 84659eb | 2007-07-18 15:47:05 -0700 | [diff] [blame] | 1052 | /* must be called with netlink table grabbed */ |
| 1053 | static void netlink_update_socket_mc(struct netlink_sock *nlk, |
| 1054 | unsigned int group, |
| 1055 | int is_new) |
| 1056 | { |
| 1057 | int old, new = !!is_new, subscriptions; |
| 1058 | |
| 1059 | old = test_bit(group - 1, nlk->groups); |
| 1060 | subscriptions = nlk->subscriptions - old + new; |
| 1061 | if (new) |
| 1062 | __set_bit(group - 1, nlk->groups); |
| 1063 | else |
| 1064 | __clear_bit(group - 1, nlk->groups); |
| 1065 | netlink_update_subscriptions(&nlk->sk, subscriptions); |
| 1066 | netlink_update_listeners(&nlk->sk); |
| 1067 | } |
| 1068 | |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1069 | static int netlink_setsockopt(struct socket *sock, int level, int optname, |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1070 | char __user *optval, int optlen) |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1071 | { |
| 1072 | struct sock *sk = sock->sk; |
| 1073 | struct netlink_sock *nlk = nlk_sk(sk); |
Johannes Berg | eb49653 | 2007-07-18 02:07:51 -0700 | [diff] [blame] | 1074 | unsigned int val = 0; |
| 1075 | int err; |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1076 | |
| 1077 | if (level != SOL_NETLINK) |
| 1078 | return -ENOPROTOOPT; |
| 1079 | |
| 1080 | if (optlen >= sizeof(int) && |
Johannes Berg | eb49653 | 2007-07-18 02:07:51 -0700 | [diff] [blame] | 1081 | get_user(val, (unsigned int __user *)optval)) |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1082 | return -EFAULT; |
| 1083 | |
| 1084 | switch (optname) { |
| 1085 | case NETLINK_PKTINFO: |
| 1086 | if (val) |
| 1087 | nlk->flags |= NETLINK_RECV_PKTINFO; |
| 1088 | else |
| 1089 | nlk->flags &= ~NETLINK_RECV_PKTINFO; |
| 1090 | err = 0; |
| 1091 | break; |
| 1092 | case NETLINK_ADD_MEMBERSHIP: |
| 1093 | case NETLINK_DROP_MEMBERSHIP: { |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1094 | if (!netlink_capable(sock, NL_NONROOT_RECV)) |
| 1095 | return -EPERM; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1096 | err = netlink_realloc_groups(sk); |
| 1097 | if (err) |
| 1098 | return err; |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1099 | if (!val || val - 1 >= nlk->ngroups) |
| 1100 | return -EINVAL; |
| 1101 | netlink_table_grab(); |
Johannes Berg | 84659eb | 2007-07-18 15:47:05 -0700 | [diff] [blame] | 1102 | netlink_update_socket_mc(nlk, val, |
| 1103 | optname == NETLINK_ADD_MEMBERSHIP); |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1104 | netlink_table_ungrab(); |
| 1105 | err = 0; |
| 1106 | break; |
| 1107 | } |
| 1108 | default: |
| 1109 | err = -ENOPROTOOPT; |
| 1110 | } |
| 1111 | return err; |
| 1112 | } |
| 1113 | |
| 1114 | static int netlink_getsockopt(struct socket *sock, int level, int optname, |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1115 | char __user *optval, int __user *optlen) |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1116 | { |
| 1117 | struct sock *sk = sock->sk; |
| 1118 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1119 | int len, val, err; |
| 1120 | |
| 1121 | if (level != SOL_NETLINK) |
| 1122 | return -ENOPROTOOPT; |
| 1123 | |
| 1124 | if (get_user(len, optlen)) |
| 1125 | return -EFAULT; |
| 1126 | if (len < 0) |
| 1127 | return -EINVAL; |
| 1128 | |
| 1129 | switch (optname) { |
| 1130 | case NETLINK_PKTINFO: |
| 1131 | if (len < sizeof(int)) |
| 1132 | return -EINVAL; |
| 1133 | len = sizeof(int); |
| 1134 | val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0; |
Heiko Carstens | a27b58f | 2006-10-30 15:06:12 -0800 | [diff] [blame] | 1135 | if (put_user(len, optlen) || |
| 1136 | put_user(val, optval)) |
| 1137 | return -EFAULT; |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1138 | err = 0; |
| 1139 | break; |
| 1140 | default: |
| 1141 | err = -ENOPROTOOPT; |
| 1142 | } |
| 1143 | return err; |
| 1144 | } |
| 1145 | |
| 1146 | static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) |
| 1147 | { |
| 1148 | struct nl_pktinfo info; |
| 1149 | |
| 1150 | info.group = NETLINK_CB(skb).dst_group; |
| 1151 | put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info); |
| 1152 | } |
| 1153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | static inline void netlink_rcv_wake(struct sock *sk) |
| 1155 | { |
| 1156 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1157 | |
David S. Miller | b03efcf | 2005-07-08 14:57:23 -0700 | [diff] [blame] | 1158 | if (skb_queue_empty(&sk->sk_receive_queue)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | clear_bit(0, &nlk->state); |
| 1160 | if (!test_bit(0, &nlk->state)) |
| 1161 | wake_up_interruptible(&nlk->wait); |
| 1162 | } |
| 1163 | |
| 1164 | static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock, |
| 1165 | struct msghdr *msg, size_t len) |
| 1166 | { |
| 1167 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); |
| 1168 | struct sock *sk = sock->sk; |
| 1169 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1170 | struct sockaddr_nl *addr=msg->msg_name; |
| 1171 | u32 dst_pid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 1172 | u32 dst_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | struct sk_buff *skb; |
| 1174 | int err; |
| 1175 | struct scm_cookie scm; |
| 1176 | |
| 1177 | if (msg->msg_flags&MSG_OOB) |
| 1178 | return -EOPNOTSUPP; |
| 1179 | |
| 1180 | if (NULL == siocb->scm) |
| 1181 | siocb->scm = &scm; |
| 1182 | err = scm_send(sock, msg, siocb->scm); |
| 1183 | if (err < 0) |
| 1184 | return err; |
| 1185 | |
| 1186 | if (msg->msg_namelen) { |
| 1187 | if (addr->nl_family != AF_NETLINK) |
| 1188 | return -EINVAL; |
| 1189 | dst_pid = addr->nl_pid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 1190 | dst_group = ffs(addr->nl_groups); |
| 1191 | if (dst_group && !netlink_capable(sock, NL_NONROOT_SEND)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | return -EPERM; |
| 1193 | } else { |
| 1194 | dst_pid = nlk->dst_pid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 1195 | dst_group = nlk->dst_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | if (!nlk->pid) { |
| 1199 | err = netlink_autobind(sock); |
| 1200 | if (err) |
| 1201 | goto out; |
| 1202 | } |
| 1203 | |
| 1204 | err = -EMSGSIZE; |
| 1205 | if (len > sk->sk_sndbuf - 32) |
| 1206 | goto out; |
| 1207 | err = -ENOBUFS; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1208 | skb = alloc_skb(len, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | if (skb==NULL) |
| 1210 | goto out; |
| 1211 | |
| 1212 | NETLINK_CB(skb).pid = nlk->pid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 1213 | NETLINK_CB(skb).dst_group = dst_group; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 1214 | NETLINK_CB(skb).loginuid = audit_get_loginuid(current->audit_context); |
Steve Grubb | e7c3497 | 2006-04-03 09:08:13 -0400 | [diff] [blame] | 1215 | selinux_get_task_sid(current, &(NETLINK_CB(skb).sid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred)); |
| 1217 | |
| 1218 | /* What can I do? Netlink is asynchronous, so that |
| 1219 | we will have to save current capabilities to |
| 1220 | check them, when this message will be delivered |
| 1221 | to corresponding kernel module. --ANK (980802) |
| 1222 | */ |
| 1223 | |
| 1224 | err = -EFAULT; |
| 1225 | if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) { |
| 1226 | kfree_skb(skb); |
| 1227 | goto out; |
| 1228 | } |
| 1229 | |
| 1230 | err = security_netlink_send(sk, skb); |
| 1231 | if (err) { |
| 1232 | kfree_skb(skb); |
| 1233 | goto out; |
| 1234 | } |
| 1235 | |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 1236 | if (dst_group) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | atomic_inc(&skb->users); |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 1238 | netlink_broadcast(sk, skb, dst_pid, dst_group, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
| 1240 | err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT); |
| 1241 | |
| 1242 | out: |
| 1243 | return err; |
| 1244 | } |
| 1245 | |
| 1246 | static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock, |
| 1247 | struct msghdr *msg, size_t len, |
| 1248 | int flags) |
| 1249 | { |
| 1250 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); |
| 1251 | struct scm_cookie scm; |
| 1252 | struct sock *sk = sock->sk; |
| 1253 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1254 | int noblock = flags&MSG_DONTWAIT; |
| 1255 | size_t copied; |
| 1256 | struct sk_buff *skb; |
| 1257 | int err; |
| 1258 | |
| 1259 | if (flags&MSG_OOB) |
| 1260 | return -EOPNOTSUPP; |
| 1261 | |
| 1262 | copied = 0; |
| 1263 | |
| 1264 | skb = skb_recv_datagram(sk,flags,noblock,&err); |
| 1265 | if (skb==NULL) |
| 1266 | goto out; |
| 1267 | |
| 1268 | msg->msg_namelen = 0; |
| 1269 | |
| 1270 | copied = skb->len; |
| 1271 | if (len < copied) { |
| 1272 | msg->msg_flags |= MSG_TRUNC; |
| 1273 | copied = len; |
| 1274 | } |
| 1275 | |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 1276 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 1278 | |
| 1279 | if (msg->msg_name) { |
| 1280 | struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name; |
| 1281 | addr->nl_family = AF_NETLINK; |
| 1282 | addr->nl_pad = 0; |
| 1283 | addr->nl_pid = NETLINK_CB(skb).pid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 1284 | addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | msg->msg_namelen = sizeof(*addr); |
| 1286 | } |
| 1287 | |
Patrick McHardy | cc9a06c | 2006-03-12 20:34:27 -0800 | [diff] [blame] | 1288 | if (nlk->flags & NETLINK_RECV_PKTINFO) |
| 1289 | netlink_cmsg_recv_pktinfo(msg, skb); |
| 1290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | if (NULL == siocb->scm) { |
| 1292 | memset(&scm, 0, sizeof(scm)); |
| 1293 | siocb->scm = &scm; |
| 1294 | } |
| 1295 | siocb->scm->creds = *NETLINK_CREDS(skb); |
Patrick McHardy | 188ccb5 | 2007-05-03 03:27:01 -0700 | [diff] [blame] | 1296 | if (flags & MSG_TRUNC) |
| 1297 | copied = skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | skb_free_datagram(sk, skb); |
| 1299 | |
| 1300 | if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) |
| 1301 | netlink_dump(sk); |
| 1302 | |
| 1303 | scm_recv(sock, msg, siocb->scm, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | out: |
| 1305 | netlink_rcv_wake(sk); |
| 1306 | return err ? : copied; |
| 1307 | } |
| 1308 | |
| 1309 | static void netlink_data_ready(struct sock *sk, int len) |
| 1310 | { |
| 1311 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1312 | |
| 1313 | if (nlk->data_ready) |
| 1314 | nlk->data_ready(sk, len); |
| 1315 | netlink_rcv_wake(sk); |
| 1316 | } |
| 1317 | |
| 1318 | /* |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1319 | * We export these functions to other modules. They provide a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | * complete set of kernel non-blocking support for message |
| 1321 | * queueing. |
| 1322 | */ |
| 1323 | |
| 1324 | struct sock * |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1325 | netlink_kernel_create(struct net *net, int unit, unsigned int groups, |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1326 | void (*input)(struct sock *sk, int len), |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 1327 | struct mutex *cb_mutex, struct module *module) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | { |
| 1329 | struct socket *sock; |
| 1330 | struct sock *sk; |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1331 | struct netlink_sock *nlk; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1332 | unsigned long *listeners = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | |
Akinobu Mita | fab2caf | 2006-08-29 02:15:24 -0700 | [diff] [blame] | 1334 | BUG_ON(!nl_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
| 1336 | if (unit<0 || unit>=MAX_LINKS) |
| 1337 | return NULL; |
| 1338 | |
| 1339 | if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock)) |
| 1340 | return NULL; |
| 1341 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1342 | if (__netlink_create(net, sock, cb_mutex, unit) < 0) |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1343 | goto out_sock_release; |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1344 | |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1345 | if (groups < 32) |
| 1346 | groups = 32; |
| 1347 | |
| 1348 | listeners = kzalloc(NLGRPSZ(groups), GFP_KERNEL); |
| 1349 | if (!listeners) |
| 1350 | goto out_sock_release; |
| 1351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | sk = sock->sk; |
| 1353 | sk->sk_data_ready = netlink_data_ready; |
| 1354 | if (input) |
| 1355 | nlk_sk(sk)->data_ready = input; |
| 1356 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1357 | if (netlink_insert(sk, net, 0)) |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1358 | goto out_sock_release; |
| 1359 | |
| 1360 | nlk = nlk_sk(sk); |
| 1361 | nlk->flags |= NETLINK_KERNEL_SOCKET; |
| 1362 | |
| 1363 | netlink_table_grab(); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1364 | if (!nl_table[unit].registered) { |
| 1365 | nl_table[unit].groups = groups; |
| 1366 | nl_table[unit].listeners = listeners; |
| 1367 | nl_table[unit].cb_mutex = cb_mutex; |
| 1368 | nl_table[unit].module = module; |
| 1369 | nl_table[unit].registered = 1; |
| 1370 | } |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1371 | netlink_table_ungrab(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1372 | |
| 1373 | return sk; |
| 1374 | |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1375 | out_sock_release: |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1376 | kfree(listeners); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1377 | sock_release(sock); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1378 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1381 | /** |
| 1382 | * netlink_change_ngroups - change number of multicast groups |
| 1383 | * |
| 1384 | * This changes the number of multicast groups that are available |
| 1385 | * on a certain netlink family. Note that it is not possible to |
Johannes Berg | 84659eb | 2007-07-18 15:47:05 -0700 | [diff] [blame] | 1386 | * change the number of groups to below 32. Also note that it does |
| 1387 | * not implicitly call netlink_clear_multicast_users() when the |
| 1388 | * number of groups is reduced. |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1389 | * |
| 1390 | * @sk: The kernel netlink socket, as returned by netlink_kernel_create(). |
| 1391 | * @groups: The new number of groups. |
| 1392 | */ |
| 1393 | int netlink_change_ngroups(struct sock *sk, unsigned int groups) |
| 1394 | { |
| 1395 | unsigned long *listeners, *old = NULL; |
| 1396 | struct netlink_table *tbl = &nl_table[sk->sk_protocol]; |
| 1397 | int err = 0; |
| 1398 | |
| 1399 | if (groups < 32) |
| 1400 | groups = 32; |
| 1401 | |
| 1402 | netlink_table_grab(); |
| 1403 | if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) { |
| 1404 | listeners = kzalloc(NLGRPSZ(groups), GFP_ATOMIC); |
| 1405 | if (!listeners) { |
| 1406 | err = -ENOMEM; |
| 1407 | goto out_ungrab; |
| 1408 | } |
| 1409 | old = tbl->listeners; |
| 1410 | memcpy(listeners, old, NLGRPSZ(tbl->groups)); |
| 1411 | rcu_assign_pointer(tbl->listeners, listeners); |
| 1412 | } |
| 1413 | tbl->groups = groups; |
| 1414 | |
| 1415 | out_ungrab: |
| 1416 | netlink_table_ungrab(); |
| 1417 | synchronize_rcu(); |
| 1418 | kfree(old); |
| 1419 | return err; |
| 1420 | } |
| 1421 | EXPORT_SYMBOL(netlink_change_ngroups); |
| 1422 | |
Johannes Berg | 84659eb | 2007-07-18 15:47:05 -0700 | [diff] [blame] | 1423 | /** |
| 1424 | * netlink_clear_multicast_users - kick off multicast listeners |
| 1425 | * |
| 1426 | * This function removes all listeners from the given group. |
| 1427 | * @ksk: The kernel netlink socket, as returned by |
| 1428 | * netlink_kernel_create(). |
| 1429 | * @group: The multicast group to clear. |
| 1430 | */ |
| 1431 | void netlink_clear_multicast_users(struct sock *ksk, unsigned int group) |
| 1432 | { |
| 1433 | struct sock *sk; |
| 1434 | struct hlist_node *node; |
| 1435 | struct netlink_table *tbl = &nl_table[ksk->sk_protocol]; |
| 1436 | |
| 1437 | netlink_table_grab(); |
| 1438 | |
| 1439 | sk_for_each_bound(sk, node, &tbl->mc_list) |
| 1440 | netlink_update_socket_mc(nlk_sk(sk), group, 0); |
| 1441 | |
| 1442 | netlink_table_ungrab(); |
| 1443 | } |
| 1444 | EXPORT_SYMBOL(netlink_clear_multicast_users); |
| 1445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | void netlink_set_nonroot(int protocol, unsigned int flags) |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1447 | { |
| 1448 | if ((unsigned int)protocol < MAX_LINKS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | nl_table[protocol].nl_nonroot = flags; |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
| 1452 | static void netlink_destroy_callback(struct netlink_callback *cb) |
| 1453 | { |
| 1454 | if (cb->skb) |
| 1455 | kfree_skb(cb->skb); |
| 1456 | kfree(cb); |
| 1457 | } |
| 1458 | |
| 1459 | /* |
| 1460 | * It looks a bit ugly. |
| 1461 | * It would be better to create kernel thread. |
| 1462 | */ |
| 1463 | |
| 1464 | static int netlink_dump(struct sock *sk) |
| 1465 | { |
| 1466 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1467 | struct netlink_callback *cb; |
| 1468 | struct sk_buff *skb; |
| 1469 | struct nlmsghdr *nlh; |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 1470 | int len, err = -ENOBUFS; |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL); |
| 1473 | if (!skb) |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 1474 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 1476 | mutex_lock(nlk->cb_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | |
| 1478 | cb = nlk->cb; |
| 1479 | if (cb == NULL) { |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 1480 | err = -EINVAL; |
| 1481 | goto errout_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | len = cb->dump(skb, cb); |
| 1485 | |
| 1486 | if (len > 0) { |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 1487 | mutex_unlock(nlk->cb_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | skb_queue_tail(&sk->sk_receive_queue, skb); |
| 1489 | sk->sk_data_ready(sk, len); |
| 1490 | return 0; |
| 1491 | } |
| 1492 | |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 1493 | nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI); |
| 1494 | if (!nlh) |
| 1495 | goto errout_skb; |
| 1496 | |
| 1497 | memcpy(nlmsg_data(nlh), &len, sizeof(len)); |
| 1498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | skb_queue_tail(&sk->sk_receive_queue, skb); |
| 1500 | sk->sk_data_ready(sk, skb->len); |
| 1501 | |
Thomas Graf | a8f74b2 | 2005-11-10 02:25:52 +0100 | [diff] [blame] | 1502 | if (cb->done) |
| 1503 | cb->done(cb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | nlk->cb = NULL; |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 1505 | mutex_unlock(nlk->cb_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | |
| 1507 | netlink_destroy_callback(cb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | return 0; |
Thomas Graf | 1797754 | 2005-06-18 22:53:48 -0700 | [diff] [blame] | 1509 | |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 1510 | errout_skb: |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 1511 | mutex_unlock(nlk->cb_mutex); |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 1512 | kfree_skb(skb); |
| 1513 | errout: |
| 1514 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | int netlink_dump_start(struct sock *ssk, struct sk_buff *skb, |
| 1518 | struct nlmsghdr *nlh, |
| 1519 | int (*dump)(struct sk_buff *skb, struct netlink_callback*), |
| 1520 | int (*done)(struct netlink_callback*)) |
| 1521 | { |
| 1522 | struct netlink_callback *cb; |
| 1523 | struct sock *sk; |
| 1524 | struct netlink_sock *nlk; |
| 1525 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 1526 | cb = kzalloc(sizeof(*cb), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | if (cb == NULL) |
| 1528 | return -ENOBUFS; |
| 1529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | cb->dump = dump; |
| 1531 | cb->done = done; |
| 1532 | cb->nlh = nlh; |
| 1533 | atomic_inc(&skb->users); |
| 1534 | cb->skb = skb; |
| 1535 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1536 | sk = netlink_lookup(ssk->sk_net, ssk->sk_protocol, NETLINK_CB(skb).pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | if (sk == NULL) { |
| 1538 | netlink_destroy_callback(cb); |
| 1539 | return -ECONNREFUSED; |
| 1540 | } |
| 1541 | nlk = nlk_sk(sk); |
Herbert Xu | 3f660d6 | 2007-05-03 03:17:14 -0700 | [diff] [blame] | 1542 | /* A dump is in progress... */ |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 1543 | mutex_lock(nlk->cb_mutex); |
Herbert Xu | 3f660d6 | 2007-05-03 03:17:14 -0700 | [diff] [blame] | 1544 | if (nlk->cb) { |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 1545 | mutex_unlock(nlk->cb_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | netlink_destroy_callback(cb); |
| 1547 | sock_put(sk); |
| 1548 | return -EBUSY; |
| 1549 | } |
| 1550 | nlk->cb = cb; |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 1551 | mutex_unlock(nlk->cb_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | |
| 1553 | netlink_dump(sk); |
| 1554 | sock_put(sk); |
Thomas Graf | c702e80 | 2007-03-22 23:30:55 -0700 | [diff] [blame] | 1555 | |
| 1556 | /* We successfully started a dump, by returning -EINTR we |
| 1557 | * signal the queue mangement to interrupt processing of |
| 1558 | * any netlink messages so userspace gets a chance to read |
| 1559 | * the results. */ |
| 1560 | return -EINTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err) |
| 1564 | { |
| 1565 | struct sk_buff *skb; |
| 1566 | struct nlmsghdr *rep; |
| 1567 | struct nlmsgerr *errmsg; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1568 | size_t payload = sizeof(*errmsg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1570 | /* error messages get the original request appened */ |
| 1571 | if (err) |
| 1572 | payload += nlmsg_len(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 1574 | skb = nlmsg_new(payload, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | if (!skb) { |
| 1576 | struct sock *sk; |
| 1577 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1578 | sk = netlink_lookup(in_skb->sk->sk_net, |
| 1579 | in_skb->sk->sk_protocol, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | NETLINK_CB(in_skb).pid); |
| 1581 | if (sk) { |
| 1582 | sk->sk_err = ENOBUFS; |
| 1583 | sk->sk_error_report(sk); |
| 1584 | sock_put(sk); |
| 1585 | } |
| 1586 | return; |
| 1587 | } |
| 1588 | |
| 1589 | rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, |
Thomas Graf | 1797754 | 2005-06-18 22:53:48 -0700 | [diff] [blame] | 1590 | NLMSG_ERROR, sizeof(struct nlmsgerr), 0); |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 1591 | errmsg = nlmsg_data(rep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | errmsg->error = err; |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 1593 | memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT); |
| 1595 | } |
| 1596 | |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1597 | static int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *, |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 1598 | struct nlmsghdr *)) |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1599 | { |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1600 | struct nlmsghdr *nlh; |
| 1601 | int err; |
| 1602 | |
| 1603 | while (skb->len >= nlmsg_total_size(0)) { |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 1604 | nlh = nlmsg_hdr(skb); |
Thomas Graf | d35b685 | 2007-03-22 23:28:46 -0700 | [diff] [blame] | 1605 | err = 0; |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1606 | |
Martin Murray | ad8e4b75 | 2006-01-10 13:02:29 -0800 | [diff] [blame] | 1607 | if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len) |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1608 | return 0; |
| 1609 | |
Thomas Graf | d35b685 | 2007-03-22 23:28:46 -0700 | [diff] [blame] | 1610 | /* Only requests are handled by the kernel */ |
| 1611 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) |
| 1612 | goto skip; |
| 1613 | |
Thomas Graf | 45e7ae7 | 2007-03-22 23:29:10 -0700 | [diff] [blame] | 1614 | /* Skip control messages */ |
| 1615 | if (nlh->nlmsg_type < NLMSG_MIN_TYPE) |
| 1616 | goto skip; |
| 1617 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 1618 | err = cb(skb, nlh); |
| 1619 | if (err == -EINTR) { |
| 1620 | /* Not an error, but we interrupt processing */ |
| 1621 | netlink_queue_skip(nlh, skb); |
| 1622 | return err; |
Thomas Graf | d35b685 | 2007-03-22 23:28:46 -0700 | [diff] [blame] | 1623 | } |
| 1624 | skip: |
| 1625 | if (nlh->nlmsg_flags & NLM_F_ACK || err) |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1626 | netlink_ack(skb, nlh, err); |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1627 | |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 1628 | netlink_queue_skip(nlh, skb); |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | return 0; |
| 1632 | } |
| 1633 | |
| 1634 | /** |
| 1635 | * nelink_run_queue - Process netlink receive queue. |
| 1636 | * @sk: Netlink socket containing the queue |
Herbert Xu | 0cfad07 | 2007-09-16 16:24:44 -0700 | [diff] [blame] | 1637 | * @qlen: Initial queue length |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1638 | * @cb: Callback function invoked for each netlink message found |
| 1639 | * |
| 1640 | * Processes as much as there was in the queue upon entry and invokes |
| 1641 | * a callback function for each netlink message found. The callback |
| 1642 | * function may refuse a message by returning a negative error code |
| 1643 | * but setting the error pointer to 0 in which case this function |
| 1644 | * returns with a qlen != 0. |
| 1645 | * |
| 1646 | * qlen must be initialized to 0 before the initial entry, afterwards |
Herbert Xu | 0cfad07 | 2007-09-16 16:24:44 -0700 | [diff] [blame] | 1647 | * the function may be called repeatedly until the returned qlen is 0. |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 1648 | * |
| 1649 | * The callback function may return -EINTR to signal that processing |
| 1650 | * of netlink messages shall be interrupted. In this case the message |
| 1651 | * currently being processed will NOT be requeued onto the receive |
| 1652 | * queue. |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1653 | */ |
Herbert Xu | 0cfad07 | 2007-09-16 16:24:44 -0700 | [diff] [blame] | 1654 | unsigned int netlink_run_queue(struct sock *sk, unsigned int qlen, |
| 1655 | int (*cb)(struct sk_buff *, struct nlmsghdr *)) |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1656 | { |
| 1657 | struct sk_buff *skb; |
| 1658 | |
Herbert Xu | 0cfad07 | 2007-09-16 16:24:44 -0700 | [diff] [blame] | 1659 | if (!qlen || qlen > skb_queue_len(&sk->sk_receive_queue)) |
| 1660 | qlen = skb_queue_len(&sk->sk_receive_queue); |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1661 | |
Herbert Xu | 0cfad07 | 2007-09-16 16:24:44 -0700 | [diff] [blame] | 1662 | for (; qlen; qlen--) { |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1663 | skb = skb_dequeue(&sk->sk_receive_queue); |
| 1664 | if (netlink_rcv_skb(skb, cb)) { |
| 1665 | if (skb->len) |
| 1666 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 1667 | else { |
| 1668 | kfree_skb(skb); |
Herbert Xu | 0cfad07 | 2007-09-16 16:24:44 -0700 | [diff] [blame] | 1669 | qlen--; |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1670 | } |
| 1671 | break; |
| 1672 | } |
| 1673 | |
| 1674 | kfree_skb(skb); |
| 1675 | } |
Herbert Xu | 0cfad07 | 2007-09-16 16:24:44 -0700 | [diff] [blame] | 1676 | |
| 1677 | return qlen; |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | /** |
| 1681 | * netlink_queue_skip - Skip netlink message while processing queue. |
| 1682 | * @nlh: Netlink message to be skipped |
| 1683 | * @skb: Socket buffer containing the netlink messages. |
| 1684 | * |
| 1685 | * Pulls the given netlink message off the socket buffer so the next |
| 1686 | * call to netlink_queue_run() will not reconsider the message. |
| 1687 | */ |
Adrian Bunk | 42bad1d | 2007-04-26 00:57:41 -0700 | [diff] [blame] | 1688 | static void netlink_queue_skip(struct nlmsghdr *nlh, struct sk_buff *skb) |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 1689 | { |
| 1690 | int msglen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 1691 | |
| 1692 | if (msglen > skb->len) |
| 1693 | msglen = skb->len; |
| 1694 | |
| 1695 | skb_pull(skb, msglen); |
| 1696 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 1698 | /** |
| 1699 | * nlmsg_notify - send a notification netlink message |
| 1700 | * @sk: netlink socket to use |
| 1701 | * @skb: notification message |
| 1702 | * @pid: destination netlink pid for reports or 0 |
| 1703 | * @group: destination multicast group or 0 |
| 1704 | * @report: 1 to report back, 0 to disable |
| 1705 | * @flags: allocation flags |
| 1706 | */ |
| 1707 | int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 pid, |
| 1708 | unsigned int group, int report, gfp_t flags) |
| 1709 | { |
| 1710 | int err = 0; |
| 1711 | |
| 1712 | if (group) { |
| 1713 | int exclude_pid = 0; |
| 1714 | |
| 1715 | if (report) { |
| 1716 | atomic_inc(&skb->users); |
| 1717 | exclude_pid = pid; |
| 1718 | } |
| 1719 | |
| 1720 | /* errors reported via destination sk->sk_err */ |
| 1721 | nlmsg_multicast(sk, skb, exclude_pid, group, flags); |
| 1722 | } |
| 1723 | |
| 1724 | if (report) |
| 1725 | err = nlmsg_unicast(sk, skb, pid); |
| 1726 | |
| 1727 | return err; |
| 1728 | } |
| 1729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | #ifdef CONFIG_PROC_FS |
| 1731 | struct nl_seq_iter { |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1732 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | int link; |
| 1734 | int hash_idx; |
| 1735 | }; |
| 1736 | |
| 1737 | static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos) |
| 1738 | { |
| 1739 | struct nl_seq_iter *iter = seq->private; |
| 1740 | int i, j; |
| 1741 | struct sock *s; |
| 1742 | struct hlist_node *node; |
| 1743 | loff_t off = 0; |
| 1744 | |
| 1745 | for (i=0; i<MAX_LINKS; i++) { |
| 1746 | struct nl_pid_hash *hash = &nl_table[i].hash; |
| 1747 | |
| 1748 | for (j = 0; j <= hash->mask; j++) { |
| 1749 | sk_for_each(s, node, &hash->table[j]) { |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1750 | if (iter->net != s->sk_net) |
| 1751 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | if (off == pos) { |
| 1753 | iter->link = i; |
| 1754 | iter->hash_idx = j; |
| 1755 | return s; |
| 1756 | } |
| 1757 | ++off; |
| 1758 | } |
| 1759 | } |
| 1760 | } |
| 1761 | return NULL; |
| 1762 | } |
| 1763 | |
| 1764 | static void *netlink_seq_start(struct seq_file *seq, loff_t *pos) |
| 1765 | { |
| 1766 | read_lock(&nl_table_lock); |
| 1767 | return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 1768 | } |
| 1769 | |
| 1770 | static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1771 | { |
| 1772 | struct sock *s; |
| 1773 | struct nl_seq_iter *iter; |
| 1774 | int i, j; |
| 1775 | |
| 1776 | ++*pos; |
| 1777 | |
| 1778 | if (v == SEQ_START_TOKEN) |
| 1779 | return netlink_seq_socket_idx(seq, 0); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1780 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1781 | iter = seq->private; |
| 1782 | s = v; |
| 1783 | do { |
| 1784 | s = sk_next(s); |
| 1785 | } while (s && (iter->net != s->sk_net)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | if (s) |
| 1787 | return s; |
| 1788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | i = iter->link; |
| 1790 | j = iter->hash_idx + 1; |
| 1791 | |
| 1792 | do { |
| 1793 | struct nl_pid_hash *hash = &nl_table[i].hash; |
| 1794 | |
| 1795 | for (; j <= hash->mask; j++) { |
| 1796 | s = sk_head(&hash->table[j]); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1797 | while (s && (iter->net != s->sk_net)) |
| 1798 | s = sk_next(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | if (s) { |
| 1800 | iter->link = i; |
| 1801 | iter->hash_idx = j; |
| 1802 | return s; |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | j = 0; |
| 1807 | } while (++i < MAX_LINKS); |
| 1808 | |
| 1809 | return NULL; |
| 1810 | } |
| 1811 | |
| 1812 | static void netlink_seq_stop(struct seq_file *seq, void *v) |
| 1813 | { |
| 1814 | read_unlock(&nl_table_lock); |
| 1815 | } |
| 1816 | |
| 1817 | |
| 1818 | static int netlink_seq_show(struct seq_file *seq, void *v) |
| 1819 | { |
| 1820 | if (v == SEQ_START_TOKEN) |
| 1821 | seq_puts(seq, |
| 1822 | "sk Eth Pid Groups " |
| 1823 | "Rmem Wmem Dump Locks\n"); |
| 1824 | else { |
| 1825 | struct sock *s = v; |
| 1826 | struct netlink_sock *nlk = nlk_sk(s); |
| 1827 | |
| 1828 | seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n", |
| 1829 | s, |
| 1830 | s->sk_protocol, |
| 1831 | nlk->pid, |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1832 | nlk->groups ? (u32)nlk->groups[0] : 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | atomic_read(&s->sk_rmem_alloc), |
| 1834 | atomic_read(&s->sk_wmem_alloc), |
| 1835 | nlk->cb, |
| 1836 | atomic_read(&s->sk_refcnt) |
| 1837 | ); |
| 1838 | |
| 1839 | } |
| 1840 | return 0; |
| 1841 | } |
| 1842 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 1843 | static const struct seq_operations netlink_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | .start = netlink_seq_start, |
| 1845 | .next = netlink_seq_next, |
| 1846 | .stop = netlink_seq_stop, |
| 1847 | .show = netlink_seq_show, |
| 1848 | }; |
| 1849 | |
| 1850 | |
| 1851 | static int netlink_seq_open(struct inode *inode, struct file *file) |
| 1852 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | struct nl_seq_iter *iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | |
Pavel Emelyanov | cf7732e | 2007-10-10 02:29:29 -0700 | [diff] [blame] | 1855 | iter = __seq_open_private(file, &netlink_seq_ops, sizeof(*iter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | if (!iter) |
| 1857 | return -ENOMEM; |
| 1858 | |
Eric W. Biederman | 077130c | 2007-09-13 09:18:57 +0200 | [diff] [blame] | 1859 | iter->net = get_proc_net(inode); |
| 1860 | if (!iter->net) { |
| 1861 | seq_release_private(inode, file); |
| 1862 | return -ENXIO; |
| 1863 | } |
Pavel Emelyanov | cf7732e | 2007-10-10 02:29:29 -0700 | [diff] [blame] | 1864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | return 0; |
| 1866 | } |
| 1867 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1868 | static int netlink_seq_release(struct inode *inode, struct file *file) |
| 1869 | { |
| 1870 | struct seq_file *seq = file->private_data; |
| 1871 | struct nl_seq_iter *iter = seq->private; |
| 1872 | put_net(iter->net); |
| 1873 | return seq_release_private(inode, file); |
| 1874 | } |
| 1875 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 1876 | static const struct file_operations netlink_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | .owner = THIS_MODULE, |
| 1878 | .open = netlink_seq_open, |
| 1879 | .read = seq_read, |
| 1880 | .llseek = seq_lseek, |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1881 | .release = netlink_seq_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | }; |
| 1883 | |
| 1884 | #endif |
| 1885 | |
| 1886 | int netlink_register_notifier(struct notifier_block *nb) |
| 1887 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1888 | return atomic_notifier_chain_register(&netlink_chain, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | int netlink_unregister_notifier(struct notifier_block *nb) |
| 1892 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1893 | return atomic_notifier_chain_unregister(&netlink_chain, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | } |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1895 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1896 | static const struct proto_ops netlink_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | .family = PF_NETLINK, |
| 1898 | .owner = THIS_MODULE, |
| 1899 | .release = netlink_release, |
| 1900 | .bind = netlink_bind, |
| 1901 | .connect = netlink_connect, |
| 1902 | .socketpair = sock_no_socketpair, |
| 1903 | .accept = sock_no_accept, |
| 1904 | .getname = netlink_getname, |
| 1905 | .poll = datagram_poll, |
| 1906 | .ioctl = sock_no_ioctl, |
| 1907 | .listen = sock_no_listen, |
| 1908 | .shutdown = sock_no_shutdown, |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1909 | .setsockopt = netlink_setsockopt, |
| 1910 | .getsockopt = netlink_getsockopt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | .sendmsg = netlink_sendmsg, |
| 1912 | .recvmsg = netlink_recvmsg, |
| 1913 | .mmap = sock_no_mmap, |
| 1914 | .sendpage = sock_no_sendpage, |
| 1915 | }; |
| 1916 | |
| 1917 | static struct net_proto_family netlink_family_ops = { |
| 1918 | .family = PF_NETLINK, |
| 1919 | .create = netlink_create, |
| 1920 | .owner = THIS_MODULE, /* for consistency 8) */ |
| 1921 | }; |
| 1922 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 1923 | static int __net_init netlink_net_init(struct net *net) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1924 | { |
| 1925 | #ifdef CONFIG_PROC_FS |
| 1926 | if (!proc_net_fops_create(net, "netlink", 0, &netlink_seq_fops)) |
| 1927 | return -ENOMEM; |
| 1928 | #endif |
| 1929 | return 0; |
| 1930 | } |
| 1931 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 1932 | static void __net_exit netlink_net_exit(struct net *net) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1933 | { |
| 1934 | #ifdef CONFIG_PROC_FS |
| 1935 | proc_net_remove(net, "netlink"); |
| 1936 | #endif |
| 1937 | } |
| 1938 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 1939 | static struct pernet_operations __net_initdata netlink_net_ops = { |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1940 | .init = netlink_net_init, |
| 1941 | .exit = netlink_net_exit, |
| 1942 | }; |
| 1943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | static int __init netlink_proto_init(void) |
| 1945 | { |
| 1946 | struct sk_buff *dummy_skb; |
| 1947 | int i; |
Denis Cheng | 26ff5dd | 2007-09-16 16:36:02 -0700 | [diff] [blame] | 1948 | unsigned long limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | unsigned int order; |
| 1950 | int err = proto_register(&netlink_proto, 0); |
| 1951 | |
| 1952 | if (err != 0) |
| 1953 | goto out; |
| 1954 | |
YOSHIFUJI Hideaki | ef047f5 | 2006-09-01 00:29:06 -0700 | [diff] [blame] | 1955 | BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 1957 | nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL); |
Akinobu Mita | fab2caf | 2006-08-29 02:15:24 -0700 | [diff] [blame] | 1958 | if (!nl_table) |
| 1959 | goto panic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | if (num_physpages >= (128 * 1024)) |
Denis Cheng | 26ff5dd | 2007-09-16 16:36:02 -0700 | [diff] [blame] | 1962 | limit = num_physpages >> (21 - PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | else |
Denis Cheng | 26ff5dd | 2007-09-16 16:36:02 -0700 | [diff] [blame] | 1964 | limit = num_physpages >> (23 - PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | |
Denis Cheng | 26ff5dd | 2007-09-16 16:36:02 -0700 | [diff] [blame] | 1966 | order = get_bitmask_order(limit) - 1 + PAGE_SHIFT; |
| 1967 | limit = (1UL << order) / sizeof(struct hlist_head); |
| 1968 | order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | |
| 1970 | for (i = 0; i < MAX_LINKS; i++) { |
| 1971 | struct nl_pid_hash *hash = &nl_table[i].hash; |
| 1972 | |
| 1973 | hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table)); |
| 1974 | if (!hash->table) { |
| 1975 | while (i-- > 0) |
| 1976 | nl_pid_hash_free(nl_table[i].hash.table, |
| 1977 | 1 * sizeof(*hash->table)); |
| 1978 | kfree(nl_table); |
Akinobu Mita | fab2caf | 2006-08-29 02:15:24 -0700 | [diff] [blame] | 1979 | goto panic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | } |
| 1981 | memset(hash->table, 0, 1 * sizeof(*hash->table)); |
| 1982 | hash->max_shift = order; |
| 1983 | hash->shift = 0; |
| 1984 | hash->mask = 0; |
| 1985 | hash->rehash_time = jiffies; |
| 1986 | } |
| 1987 | |
| 1988 | sock_register(&netlink_family_ops); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1989 | register_pernet_subsys(&netlink_net_ops); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1990 | /* The netlink device handler may be needed early. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | rtnetlink_init(); |
| 1992 | out: |
| 1993 | return err; |
Akinobu Mita | fab2caf | 2006-08-29 02:15:24 -0700 | [diff] [blame] | 1994 | panic: |
| 1995 | panic("netlink_init: Cannot allocate nl_table\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | core_initcall(netlink_proto_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | |
| 2000 | EXPORT_SYMBOL(netlink_ack); |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2001 | EXPORT_SYMBOL(netlink_run_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | EXPORT_SYMBOL(netlink_broadcast); |
| 2003 | EXPORT_SYMBOL(netlink_dump_start); |
| 2004 | EXPORT_SYMBOL(netlink_kernel_create); |
| 2005 | EXPORT_SYMBOL(netlink_register_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | EXPORT_SYMBOL(netlink_set_nonroot); |
| 2007 | EXPORT_SYMBOL(netlink_unicast); |
| 2008 | EXPORT_SYMBOL(netlink_unregister_notifier); |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2009 | EXPORT_SYMBOL(nlmsg_notify); |