~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/net/netlink/af_netlink.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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.
 11  * 
 12  */
 13 
 14 #include <linux/config.h>
 15 #include <linux/module.h>
 16 
 17 #include <linux/kernel.h>
 18 #include <linux/init.h>
 19 #include <linux/major.h>
 20 #include <linux/signal.h>
 21 #include <linux/sched.h>
 22 #include <linux/errno.h>
 23 #include <linux/string.h>
 24 #include <linux/stat.h>
 25 #include <linux/socket.h>
 26 #include <linux/un.h>
 27 #include <linux/fcntl.h>
 28 #include <linux/termios.h>
 29 #include <linux/sockios.h>
 30 #include <linux/net.h>
 31 #include <linux/fs.h>
 32 #include <linux/malloc.h>
 33 #include <asm/uaccess.h>
 34 #include <linux/skbuff.h>
 35 #include <linux/netdevice.h>
 36 #include <linux/netlink.h>
 37 #include <linux/proc_fs.h>
 38 #include <linux/smp_lock.h>
 39 #include <net/sock.h>
 40 #include <net/scm.h>
 41 
 42 #define Nprintk(a...)
 43 
 44 #if defined(CONFIG_NETLINK_DEV) || defined(CONFIG_NETLINK_DEV_MODULE)
 45 #define NL_EMULATE_DEV
 46 #endif
 47 
 48 #define BUG_TRAP(x) if (!(x)) { printk("Assertion (" #x ") failed at " __FILE__ "(%d):" __FUNCTION__ "\n", __LINE__); }
 49 
 50 struct netlink_opt
 51 {
 52         u32                     pid;
 53         unsigned                groups;
 54         u32                     dst_pid;
 55         unsigned                dst_groups;
 56         unsigned long           state;
 57         int                     (*handler)(int unit, struct sk_buff *skb);
 58         wait_queue_head_t       wait;
 59         struct netlink_callback *cb;
 60         spinlock_t              cb_lock;
 61         void                    (*data_ready)(struct sock *sk, int bytes);
 62 };
 63 
 64 static struct sock *nl_table[MAX_LINKS];
 65 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
 66 
 67 #ifdef NL_EMULATE_DEV
 68 static struct socket *netlink_kernel[MAX_LINKS];
 69 #endif
 70 
 71 static int netlink_dump(struct sock *sk);
 72 static void netlink_destroy_callback(struct netlink_callback *cb);
 73 
 74 atomic_t netlink_sock_nr;
 75 
 76 static rwlock_t nl_table_lock = RW_LOCK_UNLOCKED;
 77 static atomic_t nl_table_users = ATOMIC_INIT(0);
 78 
 79 static void netlink_sock_destruct(struct sock *sk)
 80 {
 81         skb_queue_purge(&sk->receive_queue);
 82 
 83         if (!sk->dead) {
 84                 printk("Freeing alive netlink socket %p\n", sk);
 85                 return;
 86         }
 87         BUG_TRAP(atomic_read(&sk->rmem_alloc)==0);
 88         BUG_TRAP(atomic_read(&sk->wmem_alloc)==0);
 89         BUG_TRAP(sk->protinfo.af_netlink->cb==NULL);
 90 
 91         kfree(sk->protinfo.af_netlink);
 92 
 93         atomic_dec(&netlink_sock_nr);
 94 #ifdef NETLINK_REFCNT_DEBUG
 95         printk(KERN_DEBUG "NETLINK %p released, %d are still alive\n", sk, atomic_read(&netlink_sock_nr));
 96 #endif
 97 }
 98 
 99 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
100  * Look, when several writers sleep and reader wakes them up, all but one
101  * immediately hit write lock and grab all the cpus. Exclusive sleep solves
102  * this, _but_ remember, it adds useless work on UP machines.
103  */
104 
105 static void netlink_table_grab(void)
106 {
107         write_lock_bh(&nl_table_lock);
108 
109         if (atomic_read(&nl_table_users)) {
110                 DECLARE_WAITQUEUE(wait, current);
111 
112                 add_wait_queue_exclusive(&nl_table_wait, &wait);
113                 for(;;) {
114                         set_current_state(TASK_UNINTERRUPTIBLE);
115                         if (atomic_read(&nl_table_users) == 0)
116                                 break;
117                         write_unlock_bh(&nl_table_lock);
118                         schedule();
119                         write_lock_bh(&nl_table_lock);
120                 }
121 
122                 __set_current_state(TASK_RUNNING);
123                 remove_wait_queue(&nl_table_wait, &wait);
124         }
125 }
126 
127 static __inline__ void netlink_table_ungrab(void)
128 {
129         write_unlock_bh(&nl_table_lock);
130         wake_up(&nl_table_wait);
131 }
132 
133 static __inline__ void
134 netlink_lock_table(void)
135 {
136         /* read_lock() synchronizes us to netlink_table_grab */
137 
138         read_lock(&nl_table_lock);
139         atomic_inc(&nl_table_users);
140         read_unlock(&nl_table_lock);
141 }
142 
143 static __inline__ void
144 netlink_unlock_table(void)
145 {
146         if (atomic_dec_and_test(&nl_table_users))
147                 wake_up(&nl_table_wait);
148 }
149 
150 static __inline__ struct sock *netlink_lookup(int protocol, u32 pid)
151 {
152         struct sock *sk;
153 
154         read_lock(&nl_table_lock);
155         for (sk=nl_table[protocol]; sk; sk=sk->next) {
156                 if (sk->protinfo.af_netlink->pid == pid) {
157                         sock_hold(sk);
158                         read_unlock(&nl_table_lock);
159                         return sk;
160                 }
161         }
162 
163         read_unlock(&nl_table_lock);
164         return NULL;
165 }
166 
167 extern struct proto_ops netlink_ops;
168 
169 static int netlink_insert(struct sock *sk, u32 pid)
170 {
171         int err = -EADDRINUSE;
172         struct sock *osk;
173 
174         netlink_table_grab();
175         for (osk=nl_table[sk->protocol]; osk; osk=osk->next) {
176                 if (osk->protinfo.af_netlink->pid == pid)
177                         break;
178         }
179         if (osk == NULL) {
180                 err = -EBUSY;
181                 if (sk->protinfo.af_netlink->pid == 0) {
182                         sk->protinfo.af_netlink->pid = pid;
183                         sk->next = nl_table[sk->protocol];
184                         nl_table[sk->protocol] = sk;
185                         sock_hold(sk);
186                         err = 0;
187                 }
188         }
189         netlink_table_ungrab();
190         return err;
191 }
192 
193 static void netlink_remove(struct sock *sk)
194 {
195         struct sock **skp;
196 
197         netlink_table_grab();
198         for (skp = &nl_table[sk->protocol]; *skp; skp = &((*skp)->next)) {
199                 if (*skp == sk) {
200                         *skp = sk->next;
201                         __sock_put(sk);
202                         break;
203                 }
204         }
205         netlink_table_ungrab();
206 }
207 
208 static int netlink_create(struct socket *sock, int protocol)
209 {
210         struct sock *sk;
211 
212         sock->state = SS_UNCONNECTED;
213 
214         if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
215                 return -ESOCKTNOSUPPORT;
216 
217         if (protocol<0 || protocol >= MAX_LINKS)
218                 return -EPROTONOSUPPORT;
219 
220         sock->ops = &netlink_ops;
221 
222         sk = sk_alloc(PF_NETLINK, GFP_KERNEL, 1);
223         if (!sk)
224                 return -ENOMEM;
225 
226         sock_init_data(sock,sk);
227 
228         sk->protinfo.af_netlink = kmalloc(sizeof(struct netlink_opt), GFP_KERNEL);
229         if (sk->protinfo.af_netlink == NULL) {
230                 sk_free(sk);
231                 return -ENOMEM;
232         }
233         memset(sk->protinfo.af_netlink, 0, sizeof(struct netlink_opt));
234 
235         spin_lock_init(&sk->protinfo.af_netlink->cb_lock);
236         init_waitqueue_head(&sk->protinfo.af_netlink->wait);
237         sk->destruct = netlink_sock_destruct;
238         atomic_inc(&netlink_sock_nr);
239 
240         sk->protocol=protocol;
241         return 0;
242 }
243 
244 static int netlink_release(struct socket *sock)
245 {
246         struct sock *sk = sock->sk;
247 
248         if (!sk)
249                 return 0;
250 
251         netlink_remove(sk);
252 
253         spin_lock(&sk->protinfo.af_netlink->cb_lock);
254         if (sk->protinfo.af_netlink->cb) {
255                 sk->protinfo.af_netlink->cb->done(sk->protinfo.af_netlink->cb);
256                 netlink_destroy_callback(sk->protinfo.af_netlink->cb);
257                 sk->protinfo.af_netlink->cb = NULL;
258                 __sock_put(sk);
259         }
260         spin_unlock(&sk->protinfo.af_netlink->cb_lock);
261 
262         /* OK. Socket is unlinked, and, therefore,
263            no new packets will arrive */
264 
265         sock_orphan(sk);
266         sock->sk = NULL;
267         wake_up_interruptible_all(&sk->protinfo.af_netlink->wait);
268 
269         skb_queue_purge(&sk->write_queue);
270 
271         sock_put(sk);
272         return 0;
273 }
274 
275 static int netlink_autobind(struct socket *sock)
276 {
277         struct sock *sk = sock->sk;
278         struct sock *osk;
279         s32 pid = current->pid;
280         int err;
281 
282 retry:
283         netlink_table_grab();
284         for (osk=nl_table[sk->protocol]; osk; osk=osk->next) {
285                 if (osk->protinfo.af_netlink->pid == pid) {
286                         /* Bind collision, search negative pid values. */
287                         if (pid > 0)
288                                 pid = -4096;
289                         pid--;
290                         netlink_table_ungrab();
291                         goto retry;
292                 }
293         }
294         netlink_table_ungrab();
295 
296         err = netlink_insert(sk, pid);
297         if (err == -EADDRINUSE)
298                 goto retry;
299         sk->protinfo.af_netlink->groups = 0;
300         return 0;
301 }
302 
303 static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
304 {
305         struct sock *sk = sock->sk;
306         int err;
307         struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
308         
309         if (nladdr->nl_family != AF_NETLINK)
310                 return -EINVAL;
311 
312         /* Only superuser is allowed to listen multicasts */
313         if (nladdr->nl_groups && !capable(CAP_NET_ADMIN))
314                 return -EPERM;
315 
316         if (sk->protinfo.af_netlink->pid) {
317                 if (nladdr->nl_pid != sk->protinfo.af_netlink->pid)
318                         return -EINVAL;
319                 sk->protinfo.af_netlink->groups = nladdr->nl_groups;
320                 return 0;
321         }
322 
323         if (nladdr->nl_pid == 0) {
324                 err = netlink_autobind(sock);
325                 if (err == 0)
326                         sk->protinfo.af_netlink->groups = nladdr->nl_groups;
327                 return err;
328         }
329 
330         err = netlink_insert(sk, nladdr->nl_pid);
331         if (err == 0)
332                 sk->protinfo.af_netlink->groups = nladdr->nl_groups;
333         return err;
334 }
335 
336 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
337                            int alen, int flags)
338 {
339         int err = 0;
340         struct sock *sk = sock->sk;
341         struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr;
342 
343         if (addr->sa_family == AF_UNSPEC) {
344                 sk->protinfo.af_netlink->dst_pid = 0;
345                 sk->protinfo.af_netlink->dst_groups = 0;
346                 return 0;
347         }
348         if (addr->sa_family != AF_NETLINK)
349                 return -EINVAL;
350 
351         /* Only superuser is allowed to send multicasts */
352         if (nladdr->nl_groups && !capable(CAP_NET_ADMIN))
353                 return -EPERM;
354 
355         if (!sk->protinfo.af_netlink->pid)
356                 err = netlink_autobind(sock);
357 
358         if (err == 0) {
359                 sk->protinfo.af_netlink->dst_pid = nladdr->nl_pid;
360                 sk->protinfo.af_netlink->dst_groups = nladdr->nl_groups;
361         }
362 
363         return 0;
364 }
365 
366 static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
367 {
368         struct sock *sk = sock->sk;
369         struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr;
370         
371         nladdr->nl_family = AF_NETLINK;
372         *addr_len = sizeof(*nladdr);
373 
374         if (peer) {
375                 nladdr->nl_pid = sk->protinfo.af_netlink->dst_pid;
376                 nladdr->nl_groups = sk->protinfo.af_netlink->dst_groups;
377         } else {
378                 nladdr->nl_pid = sk->protinfo.af_netlink->pid;
379                 nladdr->nl_groups = sk->protinfo.af_netlink->groups;
380         }
381         return 0;
382 }
383 
384 static void netlink_overrun(struct sock *sk)
385 {
386         if (!test_and_set_bit(0, &sk->protinfo.af_netlink->state)) {
387                 sk->err = ENOBUFS;
388                 sk->error_report(sk);
389         }
390 }
391 
392 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock)
393 {
394         struct sock *sk;
395         int len = skb->len;
396         int protocol = ssk->protocol;
397         long timeo;
398         DECLARE_WAITQUEUE(wait, current);
399 
400         timeo = sock_sndtimeo(ssk, nonblock);
401 
402 retry:
403         sk = netlink_lookup(protocol, pid);
404         if (sk == NULL)
405                 goto no_dst;
406 
407 #ifdef NL_EMULATE_DEV
408         if (sk->protinfo.af_netlink->handler) {
409                 skb_orphan(skb);
410                 len = sk->protinfo.af_netlink->handler(protocol, skb);
411                 sock_put(sk);
412                 return len;
413         }
414 #endif
415 
416         if (atomic_read(&sk->rmem_alloc) > sk->rcvbuf ||
417             test_bit(0, &sk->protinfo.af_netlink->state)) {
418                 if (!timeo) {
419                         if (ssk->protinfo.af_netlink->pid == 0)
420                                 netlink_overrun(sk);
421                         sock_put(sk);
422                         kfree_skb(skb);
423                         return -EAGAIN;
424                 }
425 
426                 __set_current_state(TASK_INTERRUPTIBLE);
427                 add_wait_queue(&sk->protinfo.af_netlink->wait, &wait);
428 
429                 if ((atomic_read(&sk->rmem_alloc) > sk->rcvbuf ||
430                     test_bit(0, &sk->protinfo.af_netlink->state)) &&
431                     !sk->dead)
432                         timeo = schedule_timeout(timeo);
433 
434                 __set_current_state(TASK_RUNNING);
435                 remove_wait_queue(&sk->protinfo.af_netlink->wait, &wait);
436                 sock_put(sk);
437 
438                 if (signal_pending(current)) {
439                         kfree_skb(skb);
440                         return sock_intr_errno(timeo);
441                 }
442                 goto retry;
443         }
444 
445         skb_orphan(skb);
446         skb_set_owner_r(skb, sk);
447         skb_queue_tail(&sk->receive_queue, skb);
448         sk->data_ready(sk, len);
449         sock_put(sk);
450         return len;
451 
452 no_dst:
453         kfree_skb(skb);
454         return -ECONNREFUSED;
455 }
456 
457 static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
458 {
459 #ifdef NL_EMULATE_DEV
460         if (sk->protinfo.af_netlink->handler) {
461                 skb_orphan(skb);
462                 sk->protinfo.af_netlink->handler(sk->protocol, skb);
463                 return 0;
464         } else
465 #endif
466         if (atomic_read(&sk->rmem_alloc) <= sk->rcvbuf &&
467             !test_bit(0, &sk->protinfo.af_netlink->state)) {
468                 skb_orphan(skb);
469                 skb_set_owner_r(skb, sk);
470                 skb_queue_tail(&sk->receive_queue, skb);
471                 sk->data_ready(sk, skb->len);
472                 return 0;
473         }
474         return -1;
475 }
476 
477 void netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid,
478                        u32 group, int allocation)
479 {
480         struct sock *sk;
481         struct sk_buff *skb2 = NULL;
482         int protocol = ssk->protocol;
483         int failure = 0;
484 
485         /* While we sleep in clone, do not allow to change socket list */
486 
487         netlink_lock_table();
488 
489         for (sk = nl_table[protocol]; sk; sk = sk->next) {
490                 if (ssk == sk)
491                         continue;
492 
493                 if (sk->protinfo.af_netlink->pid == pid ||
494                     !(sk->protinfo.af_netlink->groups&group))
495                         continue;
496 
497                 if (failure) {
498                         netlink_overrun(sk);
499                         continue;
500                 }
501 
502                 sock_hold(sk);
503                 if (skb2 == NULL) {
504                         if (atomic_read(&skb->users) != 1) {
505                                 skb2 = skb_clone(skb, allocation);
506                         } else {
507                                 skb2 = skb;
508                                 atomic_inc(&skb->users);
509                         }
510                 }
511                 if (skb2 == NULL) {
512                         netlink_overrun(sk);
513                         /* Clone failed. Notify ALL listeners. */
514                         failure = 1;
515                 } else if (netlink_broadcast_deliver(sk, skb2)) {
516                         netlink_overrun(sk);
517                 } else
518                         skb2 = NULL;
519                 sock_put(sk);
520         }
521 
522         netlink_unlock_table();
523 
524         if (skb2)
525                 kfree_skb(skb2);
526         kfree_skb(skb);
527 }
528 
529 void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code)
530 {
531         struct sock *sk;
532         int protocol = ssk->protocol;
533 
534         read_lock(&nl_table_lock);
535         for (sk = nl_table[protocol]; sk; sk = sk->next) {
536                 if (ssk == sk)
537                         continue;
538 
539                 if (sk->protinfo.af_netlink->pid == pid ||
540                     !(sk->protinfo.af_netlink->groups&group))
541                         continue;
542 
543                 sk->err = code;
544                 sk->error_report(sk);
545         }
546         read_unlock(&nl_table_lock);
547 }
548 
549 static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, int len,
550                            struct scm_cookie *scm)
551 {
552         struct sock *sk = sock->sk;
553         struct sockaddr_nl *addr=msg->msg_name;
554         u32 dst_pid;
555         u32 dst_groups;
556         struct sk_buff *skb;
557         int err;
558 
559         if (msg->msg_flags&MSG_OOB)
560                 return -EOPNOTSUPP;
561 
562         if (msg->msg_namelen) {
563                 if (addr->nl_family != AF_NETLINK)
564                         return -EINVAL;
565                 dst_pid = addr->nl_pid;
566                 dst_groups = addr->nl_groups;
567                 if (dst_groups && !capable(CAP_NET_ADMIN))
568                         return -EPERM;
569         } else {
570                 dst_pid = sk->protinfo.af_netlink->dst_pid;
571                 dst_groups = sk->protinfo.af_netlink->dst_groups;
572         }
573 
574         if (!sk->protinfo.af_netlink->pid) {
575                 err = netlink_autobind(sock);
576                 if (err)
577                         goto out;
578         }
579 
580         err = -EMSGSIZE;
581         if ((unsigned)len > sk->sndbuf-32)
582                 goto out;
583         err = -ENOBUFS;
584         skb = alloc_skb(len, GFP_KERNEL);
585         if (skb==NULL)
586                 goto out;
587 
588         NETLINK_CB(skb).pid = sk->protinfo.af_netlink->pid;
589         NETLINK_CB(skb).groups = sk->protinfo.af_netlink->groups;
590         NETLINK_CB(skb).dst_pid = dst_pid;
591         NETLINK_CB(skb).dst_groups = dst_groups;
592         memcpy(NETLINK_CREDS(skb), &scm->creds, sizeof(struct ucred));
593 
594         /* What can I do? Netlink is asynchronous, so that
595            we will have to save current capabilities to
596            check them, when this message will be delivered
597            to corresponding kernel module.   --ANK (980802)
598          */
599         NETLINK_CB(skb).eff_cap = current->cap_effective;
600 
601         err = -EFAULT;
602         if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) {
603                 kfree_skb(skb);
604                 goto out;
605         }
606 
607         if (dst_groups) {
608                 atomic_inc(&skb->users);
609                 netlink_broadcast(sk, skb, dst_pid, dst_groups, GFP_KERNEL);
610         }
611         err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT);
612 
613 out:
614         return err;
615 }
616 
617 static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, int len,
618                            int flags, struct scm_cookie *scm)
619 {
620         struct sock *sk = sock->sk;
621         int noblock = flags&MSG_DONTWAIT;
622         int copied;
623         struct sk_buff *skb;
624         int err;
625 
626         if (flags&MSG_OOB)
627                 return -EOPNOTSUPP;
628 
629         copied = 0;
630 
631         skb = skb_recv_datagram(sk,flags,noblock,&err);
632         if (skb==NULL)
633                 goto out;
634 
635         msg->msg_namelen = 0;
636 
637         copied = skb->len;
638         if (len < copied) {
639                 msg->msg_flags |= MSG_TRUNC;
640                 copied = len;
641         }
642 
643         skb->h.raw = skb->data;
644         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
645 
646         if (msg->msg_name) {
647                 struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name;
648                 addr->nl_family = AF_NETLINK;
649                 addr->nl_pid    = NETLINK_CB(skb).pid;
650                 addr->nl_groups = NETLINK_CB(skb).dst_groups;
651                 msg->msg_namelen = sizeof(*addr);
652         }
653 
654         scm->creds = *NETLINK_CREDS(skb);
655         skb_free_datagram(sk, skb);
656 
657         if (sk->protinfo.af_netlink->cb
658             && atomic_read(&sk->rmem_alloc) <= sk->rcvbuf/2)
659                 netlink_dump(sk);
660 
661 out:
662         if (skb_queue_len(&sk->receive_queue) <= sk->rcvbuf/2) {
663                 if (skb_queue_len(&sk->receive_queue) == 0)
664                         clear_bit(0, &sk->protinfo.af_netlink->state);
665                 if (!test_bit(0, &sk->protinfo.af_netlink->state))
666                         wake_up_interruptible(&sk->protinfo.af_netlink->wait);
667         }
668         return err ? : copied;
669 }
670 
671 void netlink_data_ready(struct sock *sk, int len)
672 {
673         if (sk->protinfo.af_netlink->data_ready)
674                 sk->protinfo.af_netlink->data_ready(sk, len);
675 
676         if (skb_queue_len(&sk->receive_queue) <= sk->rcvbuf/2) {
677                 if (skb_queue_len(&sk->receive_queue) == 0)
678                         clear_bit(0, &sk->protinfo.af_netlink->state);
679                 if (!test_bit(0, &sk->protinfo.af_netlink->state))
680                         wake_up_interruptible(&sk->protinfo.af_netlink->wait);
681         }
682 }
683 
684 /*
685  *      We export these functions to other modules. They provide a 
686  *      complete set of kernel non-blocking support for message
687  *      queueing.
688  */
689 
690 struct sock *
691 netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len))
692 {
693         struct socket *sock;
694         struct sock *sk;
695 
696         if (unit<0 || unit>=MAX_LINKS)
697                 return NULL;
698 
699         if (!(sock = sock_alloc())) 
700                 return NULL;
701 
702         sock->type = SOCK_RAW;
703 
704         if (netlink_create(sock, unit) < 0) {
705                 sock_release(sock);
706                 return NULL;
707         }
708         sk = sock->sk;
709         sk->data_ready = netlink_data_ready;
710         if (input)
711                 sk->protinfo.af_netlink->data_ready = input;
712 
713         netlink_insert(sk, 0);
714         return sk;
715 }
716 
717 static void netlink_destroy_callback(struct netlink_callback *cb)
718 {
719         if (cb->skb)
720                 kfree_skb(cb->skb);
721         kfree(cb);
722 }
723 
724 /*
725  * It looks a bit ugly.
726  * It would be better to create kernel thread.
727  */
728 
729 static int netlink_dump(struct sock *sk)
730 {
731         struct netlink_callback *cb;
732         struct sk_buff *skb;
733         struct nlmsghdr *nlh;
734         int len;
735         
736         skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL);
737         if (!skb)
738                 return -ENOBUFS;
739 
740         spin_lock(&sk->protinfo.af_netlink->cb_lock);
741 
742         cb = sk->protinfo.af_netlink->cb;
743         if (cb == NULL) {
744                 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
745                 kfree_skb(skb);
746                 return -EINVAL;
747         }
748 
749         len = cb->dump(skb, cb);
750 
751         if (len > 0) {
752                 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
753                 skb_queue_tail(&sk->receive_queue, skb);
754                 sk->data_ready(sk, len);
755                 return 0;
756         }
757 
758         nlh = __nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, NLMSG_DONE, sizeof(int));
759         nlh->nlmsg_flags |= NLM_F_MULTI;
760         memcpy(NLMSG_DATA(nlh), &len, sizeof(len));
761         skb_queue_tail(&sk->receive_queue, skb);
762         sk->data_ready(sk, skb->len);
763 
764         cb->done(cb);
765         sk->protinfo.af_netlink->cb = NULL;
766         spin_unlock(&sk->protinfo.af_netlink->cb_lock);
767 
768         netlink_destroy_callback(cb);
769         sock_put(sk);
770         return 0;
771 }
772 
773 int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
774                        struct nlmsghdr *nlh,
775                        int (*dump)(struct sk_buff *skb, struct netlink_callback*),
776                        int (*done)(struct netlink_callback*))
777 {
778         struct netlink_callback *cb;
779         struct sock *sk;
780 
781         cb = kmalloc(sizeof(*cb), GFP_KERNEL);
782         if (cb == NULL)
783                 return -ENOBUFS;
784 
785         memset(cb, 0, sizeof(*cb));
786         cb->dump = dump;
787         cb->done = done;
788         cb->nlh = nlh;
789         atomic_inc(&skb->users);
790         cb->skb = skb;
791 
792         sk = netlink_lookup(ssk->protocol, NETLINK_CB(skb).pid);
793         if (sk == NULL) {
794                 netlink_destroy_callback(cb);
795                 return -ECONNREFUSED;
796         }
797         /* A dump is in progress... */
798         spin_lock(&sk->protinfo.af_netlink->cb_lock);
799         if (sk->protinfo.af_netlink->cb) {
800                 spin_unlock(&sk->protinfo.af_netlink->cb_lock);
801                 netlink_destroy_callback(cb);
802                 sock_put(sk);
803                 return -EBUSY;
804         }
805         sk->protinfo.af_netlink->cb = cb;
806         spin_unlock(&sk->protinfo.af_netlink->cb_lock);
807 
808         netlink_dump(sk);
809         return 0;
810 }
811 
812 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
813 {
814         struct sk_buff *skb;
815         struct nlmsghdr *rep;
816         struct nlmsgerr *errmsg;
817         int size;
818 
819         if (err == 0)
820                 size = NLMSG_SPACE(sizeof(struct nlmsgerr));
821         else
822                 size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len));
823 
824         skb = alloc_skb(size, GFP_KERNEL);
825         if (!skb)
826                 return;
827 
828         rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
829                           NLMSG_ERROR, sizeof(struct nlmsgerr));
830         errmsg = NLMSG_DATA(rep);
831         errmsg->error = err;
832         memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr));
833         netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
834 }
835 
836 
837 #ifdef NL_EMULATE_DEV
838 
839 static rwlock_t nl_emu_lock = RW_LOCK_UNLOCKED;
840 
841 /*
842  *      Backward compatibility.
843  */     
844  
845 int netlink_attach(int unit, int (*function)(int, struct sk_buff *skb))
846 {
847         struct sock *sk = netlink_kernel_create(unit, NULL);
848         if (sk == NULL)
849                 return -ENOBUFS;
850         sk->protinfo.af_netlink->handler = function;
851         write_lock_bh(&nl_emu_lock);
852         netlink_kernel[unit] = sk->socket;
853         write_unlock_bh(&nl_emu_lock);
854         return 0;
855 }
856 
857 void netlink_detach(int unit)
858 {
859         struct socket *sock;
860 
861         write_lock_bh(&nl_emu_lock);
862         sock = netlink_kernel[unit];
863         netlink_kernel[unit] = NULL;
864         write_unlock_bh(&nl_emu_lock);
865 
866         sock_release(sock);
867 }
868 
869 int netlink_post(int unit, struct sk_buff *skb)
870 {
871         struct socket *sock;
872 
873         read_lock(&nl_emu_lock);
874         sock = netlink_kernel[unit];
875         if (sock) {
876                 struct sock *sk = sock->sk;
877                 memset(skb->cb, 0, sizeof(skb->cb));
878                 sock_hold(sk);
879                 read_unlock(&nl_emu_lock);
880 
881                 netlink_broadcast(sk, skb, 0, ~0, GFP_ATOMIC);
882 
883                 sock_put(sk);
884                 return 0;
885         }
886         read_unlock(&nl_emu_lock);
887         return -EUNATCH;
888 }
889 
890 #endif
891 
892 
893 #ifdef CONFIG_PROC_FS
894 static int netlink_read_proc(char *buffer, char **start, off_t offset,
895                              int length, int *eof, void *data)
896 {
897         off_t pos=0;
898         off_t begin=0;
899         int len=0;
900         int i;
901         struct sock *s;
902         
903         len+= sprintf(buffer,"sk       Eth Pid    Groups   "
904                       "Rmem     Wmem     Dump     Locks\n");
905         
906         for (i=0; i<MAX_LINKS; i++) {
907                 read_lock(&nl_table_lock);
908                 for (s = nl_table[i]; s; s = s->next) {
909                         len+=sprintf(buffer+len,"%p %-3d %-6d %08x %-8d %-8d %p %d",
910                                      s,
911                                      s->protocol,
912                                      s->protinfo.af_netlink->pid,
913                                      s->protinfo.af_netlink->groups,
914                                      atomic_read(&s->rmem_alloc),
915                                      atomic_read(&s->wmem_alloc),
916                                      s->protinfo.af_netlink->cb,
917                                      atomic_read(&s->refcnt)
918                                      );
919 
920                         buffer[len++]='\n';
921                 
922                         pos=begin+len;
923                         if(pos<offset) {
924                                 len=0;
925                                 begin=pos;
926                         }
927                         if(pos>offset+length) {
928                                 read_unlock(&nl_table_lock);
929                                 goto done;
930                         }
931                 }
932                 read_unlock(&nl_table_lock);
933         }
934         *eof = 1;
935 
936 done:
937         *start=buffer+(offset-begin);
938         len-=(offset-begin);
939         if(len>length)
940                 len=length;
941         if(len<0)
942                 len=0;
943         return len;
944 }
945 #endif
946 
947 struct proto_ops netlink_ops = {
948         family:         PF_NETLINK,
949 
950         release:        netlink_release,
951         bind:           netlink_bind,
952         connect:        netlink_connect,
953         socketpair:     sock_no_socketpair,
954         accept:         sock_no_accept,
955         getname:        netlink_getname,
956         poll:           datagram_poll,
957         ioctl:          sock_no_ioctl,
958         listen:         sock_no_listen,
959         shutdown:       sock_no_shutdown,
960         setsockopt:     sock_no_setsockopt,
961         getsockopt:     sock_no_getsockopt,
962         sendmsg:        netlink_sendmsg,
963         recvmsg:        netlink_recvmsg,
964         mmap:           sock_no_mmap,
965 };
966 
967 struct net_proto_family netlink_family_ops = {
968         PF_NETLINK,
969         netlink_create
970 };
971 
972 static int __init netlink_proto_init(void)
973 {
974         struct sk_buff *dummy_skb;
975 
976         if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb)) {
977                 printk(KERN_CRIT "netlink_init: panic\n");
978                 return -1;
979         }
980         sock_register(&netlink_family_ops);
981 #ifdef CONFIG_PROC_FS
982         create_proc_read_entry("net/netlink", 0, 0, netlink_read_proc, NULL);
983 #endif
984         return 0;
985 }
986 
987 module_init(netlink_proto_init);
988 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.