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