OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [unix/] [af_unix.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * NET4:        Implementation of BSD Unix domain sockets.
3
 *
4
 * Authors:     Alan Cox, <alan.cox@linux.org>
5
 *
6
 *              This program is free software; you can redistribute it and/or
7
 *              modify it under the terms of the GNU General Public License
8
 *              as published by the Free Software Foundation; either version
9
 *              2 of the License, or (at your option) any later version.
10
 *
11
 * Version:     $Id: af_unix.c,v 1.1.1.1 2004-04-15 01:16:03 phoenix Exp $
12
 *
13
 * Fixes:
14
 *              Linus Torvalds  :       Assorted bug cures.
15
 *              Niibe Yutaka    :       async I/O support.
16
 *              Carsten Paeth   :       PF_UNIX check, address fixes.
17
 *              Alan Cox        :       Limit size of allocated blocks.
18
 *              Alan Cox        :       Fixed the stupid socketpair bug.
19
 *              Alan Cox        :       BSD compatibility fine tuning.
20
 *              Alan Cox        :       Fixed a bug in connect when interrupted.
21
 *              Alan Cox        :       Sorted out a proper draft version of
22
 *                                      file descriptor passing hacked up from
23
 *                                      Mike Shaver's work.
24
 *              Marty Leisner   :       Fixes to fd passing
25
 *              Nick Nevin      :       recvmsg bugfix.
26
 *              Alan Cox        :       Started proper garbage collector
27
 *              Heiko EiBfeldt  :       Missing verify_area check
28
 *              Alan Cox        :       Started POSIXisms
29
 *              Andreas Schwab  :       Replace inode by dentry for proper
30
 *                                      reference counting
31
 *              Kirk Petersen   :       Made this a module
32
 *          Christoph Rohland   :       Elegant non-blocking accept/connect algorithm.
33
 *                                      Lots of bug fixes.
34
 *           Alexey Kuznetosv   :       Repaired (I hope) bugs introduces
35
 *                                      by above two patches.
36
 *           Andrea Arcangeli   :       If possible we block in connect(2)
37
 *                                      if the max backlog of the listen socket
38
 *                                      is been reached. This won't break
39
 *                                      old apps and it will avoid huge amount
40
 *                                      of socks hashed (this for unix_gc()
41
 *                                      performances reasons).
42
 *                                      Security fix that limits the max
43
 *                                      number of socks to 2*max_files and
44
 *                                      the number of skb queueable in the
45
 *                                      dgram receiver.
46
 *              Artur Skawina   :       Hash function optimizations
47
 *           Alexey Kuznetsov   :       Full scale SMP. Lot of bugs are introduced 8)
48
 *            Malcolm Beattie   :       Set peercred for socketpair
49
 *           Michal Ostrowski   :       Module initialization cleanup.
50
 *
51
 *
52
 * Known differences from reference BSD that was tested:
53
 *
54
 *      [TO FIX]
55
 *      ECONNREFUSED is not returned from one end of a connected() socket to the
56
 *              other the moment one end closes.
57
 *      fstat() doesn't return st_dev=NODEV, and give the blksize as high water mark
58
 *              and a fake inode identifier (nor the BSD first socket fstat twice bug).
59
 *      [NOT TO FIX]
60
 *      accept() returns a path name even if the connecting socket has closed
61
 *              in the meantime (BSD loses the path and gives up).
62
 *      accept() returns 0 length path for an unbound connector. BSD returns 16
63
 *              and a null first byte in the path (but not for gethost/peername - BSD bug ??)
64
 *      socketpair(...SOCK_RAW..) doesn't panic the kernel.
65
 *      BSD af_unix apparently has connect forgetting to block properly.
66
 *              (need to check this with the POSIX spec in detail)
67
 *
68
 * Differences from 2.0.0-11-... (ANK)
69
 *      Bug fixes and improvements.
70
 *              - client shutdown killed server socket.
71
 *              - removed all useless cli/sti pairs.
72
 *
73
 *      Semantic changes/extensions.
74
 *              - generic control message passing.
75
 *              - SCM_CREDENTIALS control message.
76
 *              - "Abstract" (not FS based) socket bindings.
77
 *                Abstract names are sequences of bytes (not zero terminated)
78
 *                started by 0, so that this name space does not intersect
79
 *                with BSD names.
80
 */
81
 
82
#include <linux/module.h>
83
#include <linux/config.h>
84
#include <linux/kernel.h>
85
#include <linux/major.h>
86
#include <linux/signal.h>
87
#include <linux/sched.h>
88
#include <linux/errno.h>
89
#include <linux/string.h>
90
#include <linux/stat.h>
91
#include <linux/socket.h>
92
#include <linux/un.h>
93
#include <linux/fcntl.h>
94
#include <linux/termios.h>
95
#include <linux/sockios.h>
96
#include <linux/net.h>
97
#include <linux/in.h>
98
#include <linux/fs.h>
99
#include <linux/slab.h>
100
#include <asm/uaccess.h>
101
#include <linux/skbuff.h>
102
#include <linux/netdevice.h>
103
#include <net/sock.h>
104
#include <linux/tcp.h>
105
#include <net/af_unix.h>
106
#include <linux/proc_fs.h>
107
#include <net/scm.h>
108
#include <linux/init.h>
109
#include <linux/poll.h>
110
#include <linux/smp_lock.h>
111
#include <linux/rtnetlink.h>
112
 
113
#include <asm/checksum.h>
114
 
115
int sysctl_unix_max_dgram_qlen = 10;
116
 
117
unix_socket *unix_socket_table[UNIX_HASH_SIZE+1];
118
rwlock_t unix_table_lock = RW_LOCK_UNLOCKED;
119
static atomic_t unix_nr_socks = ATOMIC_INIT(0);
120
 
121
#define unix_sockets_unbound    (unix_socket_table[UNIX_HASH_SIZE])
122
 
123
#define UNIX_ABSTRACT(sk)       ((sk)->protinfo.af_unix.addr->hash!=UNIX_HASH_SIZE)
124
 
125
/*
126
 *  SMP locking strategy:
127
 *    hash table is protected with rwlock unix_table_lock
128
 *    each socket state is protected by separate rwlock.
129
 */
130
 
131
static inline unsigned unix_hash_fold(unsigned hash)
132
{
133
        hash ^= hash>>16;
134
        hash ^= hash>>8;
135
        return hash&(UNIX_HASH_SIZE-1);
136
}
137
 
138
#define unix_peer(sk) ((sk)->pair)
139
 
140
static inline int unix_our_peer(unix_socket *sk, unix_socket *osk)
141
{
142
        return unix_peer(osk) == sk;
143
}
144
 
145
static inline int unix_may_send(unix_socket *sk, unix_socket *osk)
146
{
147
        return (unix_peer(osk) == NULL || unix_our_peer(sk, osk));
148
}
149
 
150
static inline unix_socket * unix_peer_get(unix_socket *s)
151
{
152
        unix_socket *peer;
153
 
154
        unix_state_rlock(s);
155
        peer = unix_peer(s);
156
        if (peer)
157
                sock_hold(peer);
158
        unix_state_runlock(s);
159
        return peer;
160
}
161
 
162
extern inline void unix_release_addr(struct unix_address *addr)
163
{
164
        if (atomic_dec_and_test(&addr->refcnt))
165
                kfree(addr);
166
}
167
 
168
/*
169
 *      Check unix socket name:
170
 *              - should be not zero length.
171
 *              - if started by not zero, should be NULL terminated (FS object)
172
 *              - if started by zero, it is abstract name.
173
 */
174
 
175
static int unix_mkname(struct sockaddr_un * sunaddr, int len, unsigned *hashp)
176
{
177
        if (len <= sizeof(short) || len > sizeof(*sunaddr))
178
                return -EINVAL;
179
        if (!sunaddr || sunaddr->sun_family != AF_UNIX)
180
                return -EINVAL;
181
        if (sunaddr->sun_path[0])
182
        {
183
                /*
184
                 *      This may look like an off by one error but it is
185
                 *      a bit more subtle. 108 is the longest valid AF_UNIX
186
                 *      path for a binding. sun_path[108] doesn't as such
187
                 *      exist. However in kernel space we are guaranteed that
188
                 *      it is a valid memory location in our kernel
189
                 *      address buffer.
190
                 */
191
                if (len > sizeof(*sunaddr))
192
                        len = sizeof(*sunaddr);
193
                ((char *)sunaddr)[len]=0;
194
                len = strlen(sunaddr->sun_path)+1+sizeof(short);
195
                return len;
196
        }
197
 
198
        *hashp = unix_hash_fold(csum_partial((char*)sunaddr, len, 0));
199
        return len;
200
}
201
 
202
static void __unix_remove_socket(unix_socket *sk)
203
{
204
        unix_socket **list = sk->protinfo.af_unix.list;
205
        if (list) {
206
                if (sk->next)
207
                        sk->next->prev = sk->prev;
208
                if (sk->prev)
209
                        sk->prev->next = sk->next;
210
                if (*list == sk)
211
                        *list = sk->next;
212
                sk->protinfo.af_unix.list = NULL;
213
                sk->prev = NULL;
214
                sk->next = NULL;
215
                __sock_put(sk);
216
        }
217
}
218
 
219
static void __unix_insert_socket(unix_socket **list, unix_socket *sk)
220
{
221
        BUG_TRAP(sk->protinfo.af_unix.list==NULL);
222
 
223
        sk->protinfo.af_unix.list = list;
224
        sk->prev = NULL;
225
        sk->next = *list;
226
        if (*list)
227
                (*list)->prev = sk;
228
        *list=sk;
229
        sock_hold(sk);
230
}
231
 
232
static inline void unix_remove_socket(unix_socket *sk)
233
{
234
        write_lock(&unix_table_lock);
235
        __unix_remove_socket(sk);
236
        write_unlock(&unix_table_lock);
237
}
238
 
239
static inline void unix_insert_socket(unix_socket **list, unix_socket *sk)
240
{
241
        write_lock(&unix_table_lock);
242
        __unix_insert_socket(list, sk);
243
        write_unlock(&unix_table_lock);
244
}
245
 
246
static unix_socket *__unix_find_socket_byname(struct sockaddr_un *sunname,
247
                                              int len, int type, unsigned hash)
248
{
249
        unix_socket *s;
250
 
251
        for (s=unix_socket_table[hash^type]; s; s=s->next) {
252
                if(s->protinfo.af_unix.addr->len==len &&
253
                   memcmp(s->protinfo.af_unix.addr->name, sunname, len) == 0)
254
                        return s;
255
        }
256
        return NULL;
257
}
258
 
259
static inline unix_socket *
260
unix_find_socket_byname(struct sockaddr_un *sunname,
261
                        int len, int type, unsigned hash)
262
{
263
        unix_socket *s;
264
 
265
        read_lock(&unix_table_lock);
266
        s = __unix_find_socket_byname(sunname, len, type, hash);
267
        if (s)
268
                sock_hold(s);
269
        read_unlock(&unix_table_lock);
270
        return s;
271
}
272
 
273
static unix_socket *unix_find_socket_byinode(struct inode *i)
274
{
275
        unix_socket *s;
276
 
277
        read_lock(&unix_table_lock);
278
        for (s=unix_socket_table[i->i_ino & (UNIX_HASH_SIZE-1)]; s; s=s->next)
279
        {
280
                struct dentry *dentry = s->protinfo.af_unix.dentry;
281
 
282
                if(dentry && dentry->d_inode == i)
283
                {
284
                        sock_hold(s);
285
                        break;
286
                }
287
        }
288
        read_unlock(&unix_table_lock);
289
        return s;
290
}
291
 
292
static inline int unix_writable(struct sock *sk)
293
{
294
        return ((atomic_read(&sk->wmem_alloc)<<2) <= sk->sndbuf);
295
}
296
 
297
static void unix_write_space(struct sock *sk)
298
{
299
        read_lock(&sk->callback_lock);
300
        if (unix_writable(sk)) {
301
                if (sk->sleep && waitqueue_active(sk->sleep))
302
                        wake_up_interruptible(sk->sleep);
303
                sk_wake_async(sk, 2, POLL_OUT);
304
        }
305
        read_unlock(&sk->callback_lock);
306
}
307
 
308
/* When dgram socket disconnects (or changes its peer), we clear its receive
309
 * queue of packets arrived from previous peer. First, it allows to do
310
 * flow control based only on wmem_alloc; second, sk connected to peer
311
 * may receive messages only from that peer. */
312
static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
313
{
314
        if (skb_queue_len(&sk->receive_queue)) {
315
                skb_queue_purge(&sk->receive_queue);
316
                wake_up_interruptible_all(&sk->protinfo.af_unix.peer_wait);
317
 
318
                /* If one link of bidirectional dgram pipe is disconnected,
319
                 * we signal error. Messages are lost. Do not make this,
320
                 * when peer was not connected to us.
321
                 */
322
                if (!other->dead && unix_peer(other) == sk) {
323
                        other->err = ECONNRESET;
324
                        other->error_report(other);
325
                }
326
        }
327
}
328
 
329
static void unix_sock_destructor(struct sock *sk)
330
{
331
        skb_queue_purge(&sk->receive_queue);
332
 
333
        BUG_TRAP(atomic_read(&sk->wmem_alloc) == 0);
334
        BUG_TRAP(sk->protinfo.af_unix.list==NULL);
335
        BUG_TRAP(sk->socket==NULL);
336
        if (sk->dead==0) {
337
                printk("Attempt to release alive unix socket: %p\n", sk);
338
                return;
339
        }
340
 
341
        if (sk->protinfo.af_unix.addr)
342
                unix_release_addr(sk->protinfo.af_unix.addr);
343
 
344
        atomic_dec(&unix_nr_socks);
345
#ifdef UNIX_REFCNT_DEBUG
346
        printk(KERN_DEBUG "UNIX %p is destroyed, %d are still alive.\n", sk, atomic_read(&unix_nr_socks));
347
#endif
348
        MOD_DEC_USE_COUNT;
349
}
350
 
351
static int unix_release_sock (unix_socket *sk, int embrion)
352
{
353
        struct dentry *dentry;
354
        struct vfsmount *mnt;
355
        unix_socket *skpair;
356
        struct sk_buff *skb;
357
        int state;
358
 
359
        unix_remove_socket(sk);
360
 
361
        /* Clear state */
362
        unix_state_wlock(sk);
363
        sock_orphan(sk);
364
        sk->shutdown = SHUTDOWN_MASK;
365
        dentry = sk->protinfo.af_unix.dentry;
366
        sk->protinfo.af_unix.dentry=NULL;
367
        mnt = sk->protinfo.af_unix.mnt;
368
        sk->protinfo.af_unix.mnt=NULL;
369
        state = sk->state;
370
        sk->state = TCP_CLOSE;
371
        unix_state_wunlock(sk);
372
 
373
        wake_up_interruptible_all(&sk->protinfo.af_unix.peer_wait);
374
 
375
        skpair=unix_peer(sk);
376
 
377
        if (skpair!=NULL) {
378
                if (sk->type==SOCK_STREAM) {
379
                        unix_state_wlock(skpair);
380
                        skpair->shutdown=SHUTDOWN_MASK; /* No more writes*/
381
                        if (!skb_queue_empty(&sk->receive_queue) || embrion)
382
                                skpair->err = ECONNRESET;
383
                        unix_state_wunlock(skpair);
384
                        skpair->state_change(skpair);
385
                        read_lock(&skpair->callback_lock);
386
                        sk_wake_async(skpair,1,POLL_HUP);
387
                        read_unlock(&skpair->callback_lock);
388
                }
389
                sock_put(skpair); /* It may now die */
390
                unix_peer(sk) = NULL;
391
        }
392
 
393
        /* Try to flush out this socket. Throw out buffers at least */
394
 
395
        while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
396
        {
397
                if (state==TCP_LISTEN)
398
                        unix_release_sock(skb->sk, 1);
399
                /* passed fds are erased in the kfree_skb hook        */
400
                kfree_skb(skb);
401
        }
402
 
403
        if (dentry) {
404
                dput(dentry);
405
                mntput(mnt);
406
        }
407
 
408
        sock_put(sk);
409
 
410
        /* ---- Socket is dead now and most probably destroyed ---- */
411
 
412
        /*
413
         * Fixme: BSD difference: In BSD all sockets connected to use get
414
         *        ECONNRESET and we die on the spot. In Linux we behave
415
         *        like files and pipes do and wait for the last
416
         *        dereference.
417
         *
418
         * Can't we simply set sock->err?
419
         *
420
         *        What the above comment does talk about? --ANK(980817)
421
         */
422
 
423
        if (atomic_read(&unix_tot_inflight))
424
                unix_gc();              /* Garbage collect fds */
425
 
426
        return 0;
427
}
428
 
429
static int unix_listen(struct socket *sock, int backlog)
430
{
431
        int err;
432
        struct sock *sk = sock->sk;
433
 
434
        err = -EOPNOTSUPP;
435
        if (sock->type!=SOCK_STREAM)
436
                goto out;                       /* Only stream sockets accept */
437
        err = -EINVAL;
438
        if (!sk->protinfo.af_unix.addr)
439
                goto out;                       /* No listens on an unbound socket */
440
        unix_state_wlock(sk);
441
        if (sk->state != TCP_CLOSE && sk->state != TCP_LISTEN)
442
                goto out_unlock;
443
        if (backlog > sk->max_ack_backlog)
444
                wake_up_interruptible_all(&sk->protinfo.af_unix.peer_wait);
445
        sk->max_ack_backlog=backlog;
446
        sk->state=TCP_LISTEN;
447
        /* set credentials so connect can copy them */
448
        sk->peercred.pid = current->pid;
449
        sk->peercred.uid = current->euid;
450
        sk->peercred.gid = current->egid;
451
        err = 0;
452
 
453
out_unlock:
454
        unix_state_wunlock(sk);
455
out:
456
        return err;
457
}
458
 
459
extern struct proto_ops unix_stream_ops;
460
extern struct proto_ops unix_dgram_ops;
461
 
462
static struct sock * unix_create1(struct socket *sock)
463
{
464
        struct sock *sk;
465
 
466
        if (atomic_read(&unix_nr_socks) >= 2*files_stat.max_files)
467
                return NULL;
468
 
469
        MOD_INC_USE_COUNT;
470
        sk = sk_alloc(PF_UNIX, GFP_KERNEL, 1);
471
        if (!sk) {
472
                MOD_DEC_USE_COUNT;
473
                return NULL;
474
        }
475
 
476
        atomic_inc(&unix_nr_socks);
477
 
478
        sock_init_data(sock,sk);
479
 
480
        sk->write_space         =       unix_write_space;
481
 
482
        sk->max_ack_backlog = sysctl_unix_max_dgram_qlen;
483
        sk->destruct = unix_sock_destructor;
484
        sk->protinfo.af_unix.dentry=NULL;
485
        sk->protinfo.af_unix.mnt=NULL;
486
        sk->protinfo.af_unix.lock = RW_LOCK_UNLOCKED;
487
        atomic_set(&sk->protinfo.af_unix.inflight, sock ? 0 : -1);
488
        init_MUTEX(&sk->protinfo.af_unix.readsem);/* single task reading lock */
489
        init_waitqueue_head(&sk->protinfo.af_unix.peer_wait);
490
        sk->protinfo.af_unix.list=NULL;
491
        unix_insert_socket(&unix_sockets_unbound, sk);
492
 
493
        return sk;
494
}
495
 
496
static int unix_create(struct socket *sock, int protocol)
497
{
498
        if (protocol && protocol != PF_UNIX)
499
                return -EPROTONOSUPPORT;
500
 
501
        sock->state = SS_UNCONNECTED;
502
 
503
        switch (sock->type) {
504
        case SOCK_STREAM:
505
                sock->ops = &unix_stream_ops;
506
                break;
507
                /*
508
                 *      Believe it or not BSD has AF_UNIX, SOCK_RAW though
509
                 *      nothing uses it.
510
                 */
511
        case SOCK_RAW:
512
                sock->type=SOCK_DGRAM;
513
        case SOCK_DGRAM:
514
                sock->ops = &unix_dgram_ops;
515
                break;
516
        default:
517
                return -ESOCKTNOSUPPORT;
518
        }
519
 
520
        return unix_create1(sock) ? 0 : -ENOMEM;
521
}
522
 
523
static int unix_release(struct socket *sock)
524
{
525
        unix_socket *sk = sock->sk;
526
 
527
        if (!sk)
528
                return 0;
529
 
530
        sock->sk = NULL;
531
 
532
        return unix_release_sock (sk, 0);
533
}
534
 
535
static int unix_autobind(struct socket *sock)
536
{
537
        struct sock *sk = sock->sk;
538
        static u32 ordernum = 1;
539
        struct unix_address * addr;
540
        int err;
541
 
542
        down(&sk->protinfo.af_unix.readsem);
543
 
544
        err = 0;
545
        if (sk->protinfo.af_unix.addr)
546
                goto out;
547
 
548
        err = -ENOMEM;
549
        addr = kmalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL);
550
        if (!addr)
551
                goto out;
552
 
553
        memset(addr, 0, sizeof(*addr) + sizeof(short) + 16);
554
        addr->name->sun_family = AF_UNIX;
555
        atomic_set(&addr->refcnt, 1);
556
 
557
retry:
558
        addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
559
        addr->hash = unix_hash_fold(csum_partial((void*)addr->name, addr->len, 0));
560
 
561
        write_lock(&unix_table_lock);
562
        ordernum = (ordernum+1)&0xFFFFF;
563
 
564
        if (__unix_find_socket_byname(addr->name, addr->len, sock->type,
565
                                      addr->hash)) {
566
                write_unlock(&unix_table_lock);
567
                /* Sanity yield. It is unusual case, but yet... */
568
                if (!(ordernum&0xFF))
569
                        yield();
570
                goto retry;
571
        }
572
        addr->hash ^= sk->type;
573
 
574
        __unix_remove_socket(sk);
575
        sk->protinfo.af_unix.addr = addr;
576
        __unix_insert_socket(&unix_socket_table[addr->hash], sk);
577
        write_unlock(&unix_table_lock);
578
        err = 0;
579
 
580
out:
581
        up(&sk->protinfo.af_unix.readsem);
582
        return err;
583
}
584
 
585
static unix_socket *unix_find_other(struct sockaddr_un *sunname, int len,
586
                                    int type, unsigned hash, int *error)
587
{
588
        unix_socket *u;
589
        struct nameidata nd;
590
        int err = 0;
591
 
592
        if (sunname->sun_path[0]) {
593
                if (path_init(sunname->sun_path,
594
                              LOOKUP_POSITIVE|LOOKUP_FOLLOW, &nd))
595
                        err = path_walk(sunname->sun_path, &nd);
596
                if (err)
597
                        goto fail;
598
                err = permission(nd.dentry->d_inode,MAY_WRITE);
599
                if (err)
600
                        goto put_fail;
601
 
602
                err = -ECONNREFUSED;
603
                if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
604
                        goto put_fail;
605
                u=unix_find_socket_byinode(nd.dentry->d_inode);
606
                if (!u)
607
                        goto put_fail;
608
 
609
                if (u->type == type)
610
                        UPDATE_ATIME(nd.dentry->d_inode);
611
 
612
                path_release(&nd);
613
 
614
                err=-EPROTOTYPE;
615
                if (u->type != type) {
616
                        sock_put(u);
617
                        goto fail;
618
                }
619
        } else {
620
                err = -ECONNREFUSED;
621
                u=unix_find_socket_byname(sunname, len, type, hash);
622
                if (u) {
623
                        struct dentry *dentry;
624
                        dentry = u->protinfo.af_unix.dentry;
625
                        if (dentry)
626
                                UPDATE_ATIME(dentry->d_inode);
627
                } else
628
                        goto fail;
629
        }
630
        return u;
631
 
632
put_fail:
633
        path_release(&nd);
634
fail:
635
        *error=err;
636
        return NULL;
637
}
638
 
639
 
640
static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
641
{
642
        struct sock *sk = sock->sk;
643
        struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
644
        struct dentry * dentry = NULL;
645
        struct nameidata nd;
646
        int err;
647
        unsigned hash;
648
        struct unix_address *addr;
649
        unix_socket **list;
650
 
651
        err = -EINVAL;
652
        if (sunaddr->sun_family != AF_UNIX)
653
                goto out;
654
 
655
        if (addr_len==sizeof(short)) {
656
                err = unix_autobind(sock);
657
                goto out;
658
        }
659
 
660
        err = unix_mkname(sunaddr, addr_len, &hash);
661
        if (err < 0)
662
                goto out;
663
        addr_len = err;
664
 
665
        down(&sk->protinfo.af_unix.readsem);
666
 
667
        err = -EINVAL;
668
        if (sk->protinfo.af_unix.addr)
669
                goto out_up;
670
 
671
        err = -ENOMEM;
672
        addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
673
        if (!addr)
674
                goto out_up;
675
 
676
        memcpy(addr->name, sunaddr, addr_len);
677
        addr->len = addr_len;
678
        addr->hash = hash^sk->type;
679
        atomic_set(&addr->refcnt, 1);
680
 
681
        if (sunaddr->sun_path[0]) {
682
                unsigned int mode;
683
                err = 0;
684
                /*
685
                 * Get the parent directory, calculate the hash for last
686
                 * component.
687
                 */
688
                if (path_init(sunaddr->sun_path, LOOKUP_PARENT, &nd))
689
                        err = path_walk(sunaddr->sun_path, &nd);
690
                if (err)
691
                        goto out_mknod_parent;
692
                /*
693
                 * Yucky last component or no last component at all?
694
                 * (foo/., foo/.., /////)
695
                 */
696
                err = -EEXIST;
697
                if (nd.last_type != LAST_NORM)
698
                        goto out_mknod;
699
                /*
700
                 * Lock the directory.
701
                 */
702
                down(&nd.dentry->d_inode->i_sem);
703
                /*
704
                 * Do the final lookup.
705
                 */
706
                dentry = lookup_hash(&nd.last, nd.dentry);
707
                err = PTR_ERR(dentry);
708
                if (IS_ERR(dentry))
709
                        goto out_mknod_unlock;
710
                err = -ENOENT;
711
                /*
712
                 * Special case - lookup gave negative, but... we had foo/bar/
713
                 * From the vfs_mknod() POV we just have a negative dentry -
714
                 * all is fine. Let's be bastards - you had / on the end, you've
715
                 * been asking for (non-existent) directory. -ENOENT for you.
716
                 */
717
                if (nd.last.name[nd.last.len] && !dentry->d_inode)
718
                        goto out_mknod_dput;
719
                /*
720
                 * All right, let's create it.
721
                 */
722
                mode = S_IFSOCK | (sock->inode->i_mode & ~current->fs->umask);
723
                err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
724
                if (err)
725
                        goto out_mknod_dput;
726
                up(&nd.dentry->d_inode->i_sem);
727
                dput(nd.dentry);
728
                nd.dentry = dentry;
729
 
730
                addr->hash = UNIX_HASH_SIZE;
731
        }
732
 
733
        write_lock(&unix_table_lock);
734
 
735
        if (!sunaddr->sun_path[0]) {
736
                err = -EADDRINUSE;
737
                if (__unix_find_socket_byname(sunaddr, addr_len,
738
                                              sk->type, hash)) {
739
                        unix_release_addr(addr);
740
                        goto out_unlock;
741
                }
742
 
743
                list = &unix_socket_table[addr->hash];
744
        } else {
745
                list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)];
746
                sk->protinfo.af_unix.dentry = nd.dentry;
747
                sk->protinfo.af_unix.mnt = nd.mnt;
748
        }
749
 
750
        err = 0;
751
        __unix_remove_socket(sk);
752
        sk->protinfo.af_unix.addr = addr;
753
        __unix_insert_socket(list, sk);
754
 
755
out_unlock:
756
        write_unlock(&unix_table_lock);
757
out_up:
758
        up(&sk->protinfo.af_unix.readsem);
759
out:
760
        return err;
761
 
762
out_mknod_dput:
763
        dput(dentry);
764
out_mknod_unlock:
765
        up(&nd.dentry->d_inode->i_sem);
766
out_mknod:
767
        path_release(&nd);
768
out_mknod_parent:
769
        if (err==-EEXIST)
770
                err=-EADDRINUSE;
771
        unix_release_addr(addr);
772
        goto out_up;
773
}
774
 
775
static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
776
                              int alen, int flags)
777
{
778
        struct sock *sk = sock->sk;
779
        struct sockaddr_un *sunaddr=(struct sockaddr_un*)addr;
780
        struct sock *other;
781
        unsigned hash;
782
        int err;
783
 
784
        if (addr->sa_family != AF_UNSPEC) {
785
                err = unix_mkname(sunaddr, alen, &hash);
786
                if (err < 0)
787
                        goto out;
788
                alen = err;
789
 
790
                if (sock->passcred && !sk->protinfo.af_unix.addr &&
791
                    (err = unix_autobind(sock)) != 0)
792
                        goto out;
793
 
794
                other=unix_find_other(sunaddr, alen, sock->type, hash, &err);
795
                if (!other)
796
                        goto out;
797
 
798
                unix_state_wlock(sk);
799
 
800
                err = -EPERM;
801
                if (!unix_may_send(sk, other))
802
                        goto out_unlock;
803
        } else {
804
                /*
805
                 *      1003.1g breaking connected state with AF_UNSPEC
806
                 */
807
                other = NULL;
808
                unix_state_wlock(sk);
809
        }
810
 
811
        /*
812
         * If it was connected, reconnect.
813
         */
814
        if (unix_peer(sk)) {
815
                struct sock *old_peer = unix_peer(sk);
816
                unix_peer(sk)=other;
817
                unix_state_wunlock(sk);
818
 
819
                if (other != old_peer)
820
                        unix_dgram_disconnected(sk, old_peer);
821
                sock_put(old_peer);
822
        } else {
823
                unix_peer(sk)=other;
824
                unix_state_wunlock(sk);
825
        }
826
        return 0;
827
 
828
out_unlock:
829
        unix_state_wunlock(sk);
830
        sock_put(other);
831
out:
832
        return err;
833
}
834
 
835
static long unix_wait_for_peer(unix_socket *other, long timeo)
836
{
837
        int sched;
838
        DECLARE_WAITQUEUE(wait, current);
839
 
840
        __set_current_state(TASK_INTERRUPTIBLE);
841
        add_wait_queue_exclusive(&other->protinfo.af_unix.peer_wait, &wait);
842
 
843
        sched = (!other->dead &&
844
                 !(other->shutdown&RCV_SHUTDOWN) &&
845
                 skb_queue_len(&other->receive_queue) > other->max_ack_backlog);
846
 
847
        unix_state_runlock(other);
848
 
849
        if (sched)
850
                timeo = schedule_timeout(timeo);
851
 
852
        __set_current_state(TASK_RUNNING);
853
        remove_wait_queue(&other->protinfo.af_unix.peer_wait, &wait);
854
        return timeo;
855
}
856
 
857
static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
858
                               int addr_len, int flags)
859
{
860
        struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
861
        struct sock *sk = sock->sk;
862
        struct sock *newsk = NULL;
863
        unix_socket *other = NULL;
864
        struct sk_buff *skb = NULL;
865
        unsigned hash;
866
        int st;
867
        int err;
868
        long timeo;
869
 
870
        err = unix_mkname(sunaddr, addr_len, &hash);
871
        if (err < 0)
872
                goto out;
873
        addr_len = err;
874
 
875
        if (sock->passcred && !sk->protinfo.af_unix.addr &&
876
            (err = unix_autobind(sock)) != 0)
877
                goto out;
878
 
879
        timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
880
 
881
        /* First of all allocate resources.
882
           If we will make it after state is locked,
883
           we will have to recheck all again in any case.
884
         */
885
 
886
        err = -ENOMEM;
887
 
888
        /* create new sock for complete connection */
889
        newsk = unix_create1(NULL);
890
        if (newsk == NULL)
891
                goto out;
892
 
893
        /* Allocate skb for sending to listening sock */
894
        skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
895
        if (skb == NULL)
896
                goto out;
897
 
898
restart:
899
        /*  Find listening sock. */
900
        other=unix_find_other(sunaddr, addr_len, sk->type, hash, &err);
901
        if (!other)
902
                goto out;
903
 
904
        /* Latch state of peer */
905
        unix_state_rlock(other);
906
 
907
        /* Apparently VFS overslept socket death. Retry. */
908
        if (other->dead) {
909
                unix_state_runlock(other);
910
                sock_put(other);
911
                goto restart;
912
        }
913
 
914
        err = -ECONNREFUSED;
915
        if (other->state != TCP_LISTEN)
916
                goto out_unlock;
917
 
918
        if (skb_queue_len(&other->receive_queue) > other->max_ack_backlog) {
919
                err = -EAGAIN;
920
                if (!timeo)
921
                        goto out_unlock;
922
 
923
                timeo = unix_wait_for_peer(other, timeo);
924
 
925
                err = sock_intr_errno(timeo);
926
                if (signal_pending(current))
927
                        goto out;
928
                sock_put(other);
929
                goto restart;
930
        }
931
 
932
        /* Latch our state.
933
 
934
           It is tricky place. We need to grab write lock and cannot
935
           drop lock on peer. It is dangerous because deadlock is
936
           possible. Connect to self case and simultaneous
937
           attempt to connect are eliminated by checking socket
938
           state. other is TCP_LISTEN, if sk is TCP_LISTEN we
939
           check this before attempt to grab lock.
940
 
941
           Well, and we have to recheck the state after socket locked.
942
         */
943
        st = sk->state;
944
 
945
        switch (st) {
946
        case TCP_CLOSE:
947
                /* This is ok... continue with connect */
948
                break;
949
        case TCP_ESTABLISHED:
950
                /* Socket is already connected */
951
                err = -EISCONN;
952
                goto out_unlock;
953
        default:
954
                err = -EINVAL;
955
                goto out_unlock;
956
        }
957
 
958
        unix_state_wlock(sk);
959
 
960
        if (sk->state != st) {
961
                unix_state_wunlock(sk);
962
                unix_state_runlock(other);
963
                sock_put(other);
964
                goto restart;
965
        }
966
 
967
        /* The way is open! Fastly set all the necessary fields... */
968
 
969
        sock_hold(sk);
970
        unix_peer(newsk)=sk;
971
        newsk->state=TCP_ESTABLISHED;
972
        newsk->type=SOCK_STREAM;
973
        newsk->peercred.pid = current->pid;
974
        newsk->peercred.uid = current->euid;
975
        newsk->peercred.gid = current->egid;
976
        newsk->sleep = &newsk->protinfo.af_unix.peer_wait;
977
 
978
        /* copy address information from listening to new sock*/
979
        if (other->protinfo.af_unix.addr)
980
        {
981
                atomic_inc(&other->protinfo.af_unix.addr->refcnt);
982
                newsk->protinfo.af_unix.addr=other->protinfo.af_unix.addr;
983
        }
984
        if (other->protinfo.af_unix.dentry) {
985
                newsk->protinfo.af_unix.dentry=dget(other->protinfo.af_unix.dentry);
986
                newsk->protinfo.af_unix.mnt=mntget(other->protinfo.af_unix.mnt);
987
        }
988
 
989
        /* Set credentials */
990
        sk->peercred = other->peercred;
991
 
992
        sock_hold(newsk);
993
        unix_peer(sk)=newsk;
994
        sock->state=SS_CONNECTED;
995
        sk->state=TCP_ESTABLISHED;
996
 
997
        unix_state_wunlock(sk);
998
 
999
        /* take ten and and send info to listening sock */
1000
        spin_lock(&other->receive_queue.lock);
1001
        __skb_queue_tail(&other->receive_queue,skb);
1002
        /* Undo artificially decreased inflight after embrion
1003
         * is installed to listening socket. */
1004
        atomic_inc(&newsk->protinfo.af_unix.inflight);
1005
        spin_unlock(&other->receive_queue.lock);
1006
        unix_state_runlock(other);
1007
        other->data_ready(other, 0);
1008
        sock_put(other);
1009
        return 0;
1010
 
1011
out_unlock:
1012
        if (other)
1013
                unix_state_runlock(other);
1014
 
1015
out:
1016
        if (skb)
1017
                kfree_skb(skb);
1018
        if (newsk)
1019
                unix_release_sock(newsk, 0);
1020
        if (other)
1021
                sock_put(other);
1022
        return err;
1023
}
1024
 
1025
static int unix_socketpair(struct socket *socka, struct socket *sockb)
1026
{
1027
        struct sock *ska=socka->sk, *skb = sockb->sk;
1028
 
1029
        /* Join our sockets back to back */
1030
        sock_hold(ska);
1031
        sock_hold(skb);
1032
        unix_peer(ska)=skb;
1033
        unix_peer(skb)=ska;
1034
        ska->peercred.pid = skb->peercred.pid = current->pid;
1035
        ska->peercred.uid = skb->peercred.uid = current->euid;
1036
        ska->peercred.gid = skb->peercred.gid = current->egid;
1037
 
1038
        if (ska->type != SOCK_DGRAM)
1039
        {
1040
                ska->state=TCP_ESTABLISHED;
1041
                skb->state=TCP_ESTABLISHED;
1042
                socka->state=SS_CONNECTED;
1043
                sockb->state=SS_CONNECTED;
1044
        }
1045
        return 0;
1046
}
1047
 
1048
static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
1049
{
1050
        unix_socket *sk = sock->sk;
1051
        unix_socket *tsk;
1052
        struct sk_buff *skb;
1053
        int err;
1054
 
1055
        err = -EOPNOTSUPP;
1056
        if (sock->type!=SOCK_STREAM)
1057
                goto out;
1058
 
1059
        err = -EINVAL;
1060
        if (sk->state!=TCP_LISTEN)
1061
                goto out;
1062
 
1063
        /* If socket state is TCP_LISTEN it cannot change (for now...),
1064
         * so that no locks are necessary.
1065
         */
1066
 
1067
        skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
1068
        if (!skb) {
1069
                /* This means receive shutdown. */
1070
                if (err == 0)
1071
                        err = -EINVAL;
1072
                goto out;
1073
        }
1074
 
1075
        tsk = skb->sk;
1076
        skb_free_datagram(sk, skb);
1077
        wake_up_interruptible(&sk->protinfo.af_unix.peer_wait);
1078
 
1079
        /* attach accepted sock to socket */
1080
        unix_state_wlock(tsk);
1081
        newsock->state = SS_CONNECTED;
1082
        sock_graft(tsk, newsock);
1083
        unix_state_wunlock(tsk);
1084
        return 0;
1085
 
1086
out:
1087
        return err;
1088
}
1089
 
1090
 
1091
static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
1092
{
1093
        struct sock *sk = sock->sk;
1094
        struct sockaddr_un *sunaddr=(struct sockaddr_un *)uaddr;
1095
        int err = 0;
1096
 
1097
        if (peer) {
1098
                sk = unix_peer_get(sk);
1099
 
1100
                err = -ENOTCONN;
1101
                if (!sk)
1102
                        goto out;
1103
                err = 0;
1104
        } else {
1105
                sock_hold(sk);
1106
        }
1107
 
1108
        unix_state_rlock(sk);
1109
        if (!sk->protinfo.af_unix.addr) {
1110
                sunaddr->sun_family = AF_UNIX;
1111
                sunaddr->sun_path[0] = 0;
1112
                *uaddr_len = sizeof(short);
1113
        } else {
1114
                struct unix_address *addr = sk->protinfo.af_unix.addr;
1115
 
1116
                *uaddr_len = addr->len;
1117
                memcpy(sunaddr, addr->name, *uaddr_len);
1118
        }
1119
        unix_state_runlock(sk);
1120
        sock_put(sk);
1121
out:
1122
        return err;
1123
}
1124
 
1125
static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1126
{
1127
        int i;
1128
 
1129
        scm->fp = UNIXCB(skb).fp;
1130
        skb->destructor = sock_wfree;
1131
        UNIXCB(skb).fp = NULL;
1132
 
1133
        for (i=scm->fp->count-1; i>=0; i--)
1134
                unix_notinflight(scm->fp->fp[i]);
1135
}
1136
 
1137
static void unix_destruct_fds(struct sk_buff *skb)
1138
{
1139
        struct scm_cookie scm;
1140
        memset(&scm, 0, sizeof(scm));
1141
        unix_detach_fds(&scm, skb);
1142
 
1143
        /* Alas, it calls VFS */
1144
        /* So fscking what? fput() had been SMP-safe since the last Summer */
1145
        scm_destroy(&scm);
1146
        sock_wfree(skb);
1147
}
1148
 
1149
static void unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1150
{
1151
        int i;
1152
        for (i=scm->fp->count-1; i>=0; i--)
1153
                unix_inflight(scm->fp->fp[i]);
1154
        UNIXCB(skb).fp = scm->fp;
1155
        skb->destructor = unix_destruct_fds;
1156
        scm->fp = NULL;
1157
}
1158
 
1159
/*
1160
 *      Send AF_UNIX data.
1161
 */
1162
 
1163
static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, int len,
1164
                              struct scm_cookie *scm)
1165
{
1166
        struct sock *sk = sock->sk;
1167
        struct sockaddr_un *sunaddr=msg->msg_name;
1168
        unix_socket *other = NULL;
1169
        int namelen = 0; /* fake GCC */
1170
        int err;
1171
        unsigned hash;
1172
        struct sk_buff *skb;
1173
        long timeo;
1174
 
1175
        err = -EOPNOTSUPP;
1176
        if (msg->msg_flags&MSG_OOB)
1177
                goto out;
1178
 
1179
        if (msg->msg_namelen) {
1180
                err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
1181
                if (err < 0)
1182
                        goto out;
1183
                namelen = err;
1184
        } else {
1185
                sunaddr = NULL;
1186
                err = -ENOTCONN;
1187
                other = unix_peer_get(sk);
1188
                if (!other)
1189
                        goto out;
1190
        }
1191
 
1192
        if (sock->passcred && !sk->protinfo.af_unix.addr &&
1193
            (err = unix_autobind(sock)) != 0)
1194
                goto out;
1195
 
1196
        err = -EMSGSIZE;
1197
        if ((unsigned)len > sk->sndbuf - 32)
1198
                goto out;
1199
 
1200
        skb = sock_alloc_send_skb(sk, len, msg->msg_flags&MSG_DONTWAIT, &err);
1201
        if (skb==NULL)
1202
                goto out;
1203
 
1204
        memcpy(UNIXCREDS(skb), &scm->creds, sizeof(struct ucred));
1205
        if (scm->fp)
1206
                unix_attach_fds(scm, skb);
1207
 
1208
        skb->h.raw = skb->data;
1209
        err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
1210
        if (err)
1211
                goto out_free;
1212
 
1213
        timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1214
 
1215
restart:
1216
        if (!other) {
1217
                err = -ECONNRESET;
1218
                if (sunaddr == NULL)
1219
                        goto out_free;
1220
 
1221
                other = unix_find_other(sunaddr, namelen, sk->type, hash, &err);
1222
                if (other==NULL)
1223
                        goto out_free;
1224
        }
1225
 
1226
        unix_state_rlock(other);
1227
        err = -EPERM;
1228
        if (!unix_may_send(sk, other))
1229
                goto out_unlock;
1230
 
1231
        if (other->dead) {
1232
                /*
1233
                 *      Check with 1003.1g - what should
1234
                 *      datagram error
1235
                 */
1236
                unix_state_runlock(other);
1237
                sock_put(other);
1238
 
1239
                err = 0;
1240
                unix_state_wlock(sk);
1241
                if (unix_peer(sk) == other) {
1242
                        unix_peer(sk)=NULL;
1243
                        unix_state_wunlock(sk);
1244
 
1245
                        unix_dgram_disconnected(sk, other);
1246
                        sock_put(other);
1247
                        err = -ECONNREFUSED;
1248
                } else {
1249
                        unix_state_wunlock(sk);
1250
                }
1251
 
1252
                other = NULL;
1253
                if (err)
1254
                        goto out_free;
1255
                goto restart;
1256
        }
1257
 
1258
        err = -EPIPE;
1259
        if (other->shutdown&RCV_SHUTDOWN)
1260
                goto out_unlock;
1261
 
1262
        if (unix_peer(other) != sk &&
1263
            skb_queue_len(&other->receive_queue) > other->max_ack_backlog) {
1264
                if (!timeo) {
1265
                        err = -EAGAIN;
1266
                        goto out_unlock;
1267
                }
1268
 
1269
                timeo = unix_wait_for_peer(other, timeo);
1270
 
1271
                err = sock_intr_errno(timeo);
1272
                if (signal_pending(current))
1273
                        goto out_free;
1274
 
1275
                goto restart;
1276
        }
1277
 
1278
        skb_queue_tail(&other->receive_queue, skb);
1279
        unix_state_runlock(other);
1280
        other->data_ready(other, len);
1281
        sock_put(other);
1282
        return len;
1283
 
1284
out_unlock:
1285
        unix_state_runlock(other);
1286
out_free:
1287
        kfree_skb(skb);
1288
out:
1289
        if (other)
1290
                sock_put(other);
1291
        return err;
1292
}
1293
 
1294
 
1295
static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, int len,
1296
                               struct scm_cookie *scm)
1297
{
1298
        struct sock *sk = sock->sk;
1299
        unix_socket *other = NULL;
1300
        struct sockaddr_un *sunaddr=msg->msg_name;
1301
        int err,size;
1302
        struct sk_buff *skb;
1303
        int sent=0;
1304
 
1305
        err = -EOPNOTSUPP;
1306
        if (msg->msg_flags&MSG_OOB)
1307
                goto out_err;
1308
 
1309
        if (msg->msg_namelen) {
1310
                err = (sk->state==TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP);
1311
                goto out_err;
1312
        } else {
1313
                sunaddr = NULL;
1314
                err = -ENOTCONN;
1315
                other = unix_peer_get(sk);
1316
                if (!other)
1317
                        goto out_err;
1318
        }
1319
 
1320
        if (sk->shutdown&SEND_SHUTDOWN)
1321
                goto pipe_err;
1322
 
1323
        while(sent < len)
1324
        {
1325
                /*
1326
                 *      Optimisation for the fact that under 0.01% of X messages typically
1327
                 *      need breaking up.
1328
                 */
1329
 
1330
                size=len-sent;
1331
 
1332
                /* Keep two messages in the pipe so it schedules better */
1333
                if (size > sk->sndbuf/2 - 64)
1334
                        size = sk->sndbuf/2 - 64;
1335
 
1336
                if (size > SKB_MAX_ALLOC)
1337
                        size = SKB_MAX_ALLOC;
1338
 
1339
                /*
1340
                 *      Grab a buffer
1341
                 */
1342
 
1343
                skb=sock_alloc_send_skb(sk,size,msg->msg_flags&MSG_DONTWAIT, &err);
1344
 
1345
                if (skb==NULL)
1346
                        goto out_err;
1347
 
1348
                /*
1349
                 *      If you pass two values to the sock_alloc_send_skb
1350
                 *      it tries to grab the large buffer with GFP_NOFS
1351
                 *      (which can fail easily), and if it fails grab the
1352
                 *      fallback size buffer which is under a page and will
1353
                 *      succeed. [Alan]
1354
                 */
1355
                size = min_t(int, size, skb_tailroom(skb));
1356
 
1357
                memcpy(UNIXCREDS(skb), &scm->creds, sizeof(struct ucred));
1358
                if (scm->fp)
1359
                        unix_attach_fds(scm, skb);
1360
 
1361
                if ((err = memcpy_fromiovec(skb_put(skb,size), msg->msg_iov, size)) != 0) {
1362
                        kfree_skb(skb);
1363
                        goto out_err;
1364
                }
1365
 
1366
                unix_state_rlock(other);
1367
 
1368
                if (other->dead || (other->shutdown & RCV_SHUTDOWN))
1369
                        goto pipe_err_free;
1370
 
1371
                skb_queue_tail(&other->receive_queue, skb);
1372
                unix_state_runlock(other);
1373
                other->data_ready(other, size);
1374
                sent+=size;
1375
        }
1376
        sock_put(other);
1377
        return sent;
1378
 
1379
pipe_err_free:
1380
        unix_state_runlock(other);
1381
        kfree_skb(skb);
1382
pipe_err:
1383
        if (sent==0 && !(msg->msg_flags&MSG_NOSIGNAL))
1384
                send_sig(SIGPIPE,current,0);
1385
        err = -EPIPE;
1386
out_err:
1387
        if (other)
1388
                sock_put(other);
1389
        return sent ? : err;
1390
}
1391
 
1392
static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
1393
{
1394
        msg->msg_namelen = 0;
1395
        if (sk->protinfo.af_unix.addr) {
1396
                msg->msg_namelen=sk->protinfo.af_unix.addr->len;
1397
                memcpy(msg->msg_name,
1398
                       sk->protinfo.af_unix.addr->name,
1399
                       sk->protinfo.af_unix.addr->len);
1400
        }
1401
}
1402
 
1403
static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg, int size,
1404
                              int flags, struct scm_cookie *scm)
1405
{
1406
        struct sock *sk = sock->sk;
1407
        int noblock = flags & MSG_DONTWAIT;
1408
        struct sk_buff *skb;
1409
        int err;
1410
 
1411
        err = -EOPNOTSUPP;
1412
        if (flags&MSG_OOB)
1413
                goto out;
1414
 
1415
        msg->msg_namelen = 0;
1416
 
1417
        skb = skb_recv_datagram(sk, flags, noblock, &err);
1418
        if (!skb)
1419
                goto out;
1420
 
1421
        wake_up_interruptible(&sk->protinfo.af_unix.peer_wait);
1422
 
1423
        if (msg->msg_name)
1424
                unix_copy_addr(msg, skb->sk);
1425
 
1426
        if (size > skb->len)
1427
                size = skb->len;
1428
        else if (size < skb->len)
1429
                msg->msg_flags |= MSG_TRUNC;
1430
 
1431
        err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size);
1432
        if (err)
1433
                goto out_free;
1434
 
1435
        scm->creds = *UNIXCREDS(skb);
1436
 
1437
        if (!(flags & MSG_PEEK))
1438
        {
1439
                if (UNIXCB(skb).fp)
1440
                        unix_detach_fds(scm, skb);
1441
        }
1442
        else
1443
        {
1444
                /* It is questionable: on PEEK we could:
1445
                   - do not return fds - good, but too simple 8)
1446
                   - return fds, and do not return them on read (old strategy,
1447
                     apparently wrong)
1448
                   - clone fds (I choosed it for now, it is the most universal
1449
                     solution)
1450
 
1451
                   POSIX 1003.1g does not actually define this clearly
1452
                   at all. POSIX 1003.1g doesn't define a lot of things
1453
                   clearly however!
1454
 
1455
                */
1456
                if (UNIXCB(skb).fp)
1457
                        scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1458
        }
1459
        err = size;
1460
 
1461
out_free:
1462
        skb_free_datagram(sk,skb);
1463
out:
1464
        return err;
1465
}
1466
 
1467
/*
1468
 *      Sleep until data has arrive. But check for races..
1469
 */
1470
 
1471
static long unix_stream_data_wait(unix_socket * sk, long timeo)
1472
{
1473
        DECLARE_WAITQUEUE(wait, current);
1474
 
1475
        unix_state_rlock(sk);
1476
 
1477
        add_wait_queue(sk->sleep, &wait);
1478
 
1479
        for (;;) {
1480
                set_current_state(TASK_INTERRUPTIBLE);
1481
 
1482
                if (skb_queue_len(&sk->receive_queue) ||
1483
                    sk->err ||
1484
                    (sk->shutdown & RCV_SHUTDOWN) ||
1485
                    signal_pending(current) ||
1486
                    !timeo)
1487
                        break;
1488
 
1489
                set_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
1490
                unix_state_runlock(sk);
1491
                timeo = schedule_timeout(timeo);
1492
                unix_state_rlock(sk);
1493
                clear_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
1494
        }
1495
 
1496
        __set_current_state(TASK_RUNNING);
1497
        remove_wait_queue(sk->sleep, &wait);
1498
        unix_state_runlock(sk);
1499
        return timeo;
1500
}
1501
 
1502
 
1503
 
1504
static int unix_stream_recvmsg(struct socket *sock, struct msghdr *msg, int size,
1505
                               int flags, struct scm_cookie *scm)
1506
{
1507
        struct sock *sk = sock->sk;
1508
        struct sockaddr_un *sunaddr=msg->msg_name;
1509
        int copied = 0;
1510
        int check_creds = 0;
1511
        int target;
1512
        int err = 0;
1513
        long timeo;
1514
 
1515
        err = -EINVAL;
1516
        if (sk->state != TCP_ESTABLISHED)
1517
                goto out;
1518
 
1519
        err = -EOPNOTSUPP;
1520
        if (flags&MSG_OOB)
1521
                goto out;
1522
 
1523
        target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
1524
        timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
1525
 
1526
        msg->msg_namelen = 0;
1527
 
1528
        /* Lock the socket to prevent queue disordering
1529
         * while sleeps in memcpy_tomsg
1530
         */
1531
 
1532
        down(&sk->protinfo.af_unix.readsem);
1533
 
1534
        do
1535
        {
1536
                int chunk;
1537
                struct sk_buff *skb;
1538
 
1539
                skb=skb_dequeue(&sk->receive_queue);
1540
                if (skb==NULL)
1541
                {
1542
                        if (copied >= target)
1543
                                break;
1544
 
1545
                        /*
1546
                         *      POSIX 1003.1g mandates this order.
1547
                         */
1548
 
1549
                        if ((err = sock_error(sk)) != 0)
1550
                                break;
1551
                        if (sk->shutdown & RCV_SHUTDOWN)
1552
                                break;
1553
                        err = -EAGAIN;
1554
                        if (!timeo)
1555
                                break;
1556
                        up(&sk->protinfo.af_unix.readsem);
1557
 
1558
                        timeo = unix_stream_data_wait(sk, timeo);
1559
 
1560
                        if (signal_pending(current)) {
1561
                                err = sock_intr_errno(timeo);
1562
                                goto out;
1563
                        }
1564
                        down(&sk->protinfo.af_unix.readsem);
1565
                        continue;
1566
                }
1567
 
1568
                if (check_creds) {
1569
                        /* Never glue messages from different writers */
1570
                        if (memcmp(UNIXCREDS(skb), &scm->creds, sizeof(scm->creds)) != 0) {
1571
                                skb_queue_head(&sk->receive_queue, skb);
1572
                                break;
1573
                        }
1574
                } else {
1575
                        /* Copy credentials */
1576
                        scm->creds = *UNIXCREDS(skb);
1577
                        check_creds = 1;
1578
                }
1579
 
1580
                /* Copy address just once */
1581
                if (sunaddr)
1582
                {
1583
                        unix_copy_addr(msg, skb->sk);
1584
                        sunaddr = NULL;
1585
                }
1586
 
1587
                chunk = min_t(unsigned int, skb->len, size);
1588
                if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1589
                        skb_queue_head(&sk->receive_queue, skb);
1590
                        if (copied == 0)
1591
                                copied = -EFAULT;
1592
                        break;
1593
                }
1594
                copied += chunk;
1595
                size -= chunk;
1596
 
1597
                /* Mark read part of skb as used */
1598
                if (!(flags & MSG_PEEK))
1599
                {
1600
                        skb_pull(skb, chunk);
1601
 
1602
                        if (UNIXCB(skb).fp)
1603
                                unix_detach_fds(scm, skb);
1604
 
1605
                        /* put the skb back if we didn't use it up.. */
1606
                        if (skb->len)
1607
                        {
1608
                                skb_queue_head(&sk->receive_queue, skb);
1609
                                break;
1610
                        }
1611
 
1612
                        kfree_skb(skb);
1613
 
1614
                        if (scm->fp)
1615
                                break;
1616
                }
1617
                else
1618
                {
1619
                        /* It is questionable, see note in unix_dgram_recvmsg.
1620
                         */
1621
                        if (UNIXCB(skb).fp)
1622
                                scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1623
 
1624
                        /* put message back and return */
1625
                        skb_queue_head(&sk->receive_queue, skb);
1626
                        break;
1627
                }
1628
        } while (size);
1629
 
1630
        up(&sk->protinfo.af_unix.readsem);
1631
out:
1632
        return copied ? : err;
1633
}
1634
 
1635
static int unix_shutdown(struct socket *sock, int mode)
1636
{
1637
        struct sock *sk = sock->sk;
1638
        unix_socket *other;
1639
 
1640
        mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN);
1641
 
1642
        if (mode) {
1643
                unix_state_wlock(sk);
1644
                sk->shutdown |= mode;
1645
                other=unix_peer(sk);
1646
                if (other)
1647
                        sock_hold(other);
1648
                unix_state_wunlock(sk);
1649
                sk->state_change(sk);
1650
 
1651
                if (other && sk->type == SOCK_STREAM) {
1652
                        int peer_mode = 0;
1653
 
1654
                        if (mode&RCV_SHUTDOWN)
1655
                                peer_mode |= SEND_SHUTDOWN;
1656
                        if (mode&SEND_SHUTDOWN)
1657
                                peer_mode |= RCV_SHUTDOWN;
1658
                        unix_state_wlock(other);
1659
                        other->shutdown |= peer_mode;
1660
                        unix_state_wunlock(other);
1661
                        other->state_change(other);
1662
                        read_lock(&other->callback_lock);
1663
                        if (peer_mode == SHUTDOWN_MASK)
1664
                                sk_wake_async(other,1,POLL_HUP);
1665
                        else if (peer_mode & RCV_SHUTDOWN)
1666
                                sk_wake_async(other,1,POLL_IN);
1667
                        read_unlock(&other->callback_lock);
1668
                }
1669
                if (other)
1670
                        sock_put(other);
1671
        }
1672
        return 0;
1673
}
1674
 
1675
static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1676
{
1677
        struct sock *sk = sock->sk;
1678
        long amount=0;
1679
        int err;
1680
 
1681
        switch(cmd)
1682
        {
1683
                case SIOCOUTQ:
1684
                        amount = atomic_read(&sk->wmem_alloc);
1685
                        err = put_user(amount, (int *)arg);
1686
                        break;
1687
                case SIOCINQ:
1688
                {
1689
                        struct sk_buff *skb;
1690
                        if (sk->state==TCP_LISTEN) {
1691
                                err = -EINVAL;
1692
                                break;
1693
                        }
1694
 
1695
                        spin_lock(&sk->receive_queue.lock);
1696
                        if((skb=skb_peek(&sk->receive_queue))!=NULL)
1697
                                amount=skb->len;
1698
                        spin_unlock(&sk->receive_queue.lock);
1699
                        err = put_user(amount, (int *)arg);
1700
                        break;
1701
                }
1702
 
1703
                default:
1704
                        err = dev_ioctl(cmd, (void *)arg);
1705
                        break;
1706
        }
1707
        return err;
1708
}
1709
 
1710
static unsigned int unix_poll(struct file * file, struct socket *sock, poll_table *wait)
1711
{
1712
        struct sock *sk = sock->sk;
1713
        unsigned int mask;
1714
 
1715
        poll_wait(file, sk->sleep, wait);
1716
        mask = 0;
1717
 
1718
        /* exceptional events? */
1719
        if (sk->err)
1720
                mask |= POLLERR;
1721
        if (sk->shutdown == SHUTDOWN_MASK)
1722
                mask |= POLLHUP;
1723
 
1724
        /* readable? */
1725
        if (!skb_queue_empty(&sk->receive_queue) || (sk->shutdown&RCV_SHUTDOWN))
1726
                mask |= POLLIN | POLLRDNORM;
1727
 
1728
        /* Connection-based need to check for termination and startup */
1729
        if (sk->type == SOCK_STREAM && sk->state==TCP_CLOSE)
1730
                mask |= POLLHUP;
1731
 
1732
        /*
1733
         * we set writable also when the other side has shut down the
1734
         * connection. This prevents stuck sockets.
1735
         */
1736
        if (unix_writable(sk))
1737
                mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1738
 
1739
        return mask;
1740
}
1741
 
1742
 
1743
#ifdef CONFIG_PROC_FS
1744
static int unix_read_proc(char *buffer, char **start, off_t offset,
1745
                          int length, int *eof, void *data)
1746
{
1747
        off_t pos=0;
1748
        off_t begin=0;
1749
        int len=0;
1750
        int i;
1751
        unix_socket *s;
1752
 
1753
        len+= sprintf(buffer,"Num       RefCount Protocol Flags    Type St "
1754
            "Inode Path\n");
1755
 
1756
        read_lock(&unix_table_lock);
1757
        forall_unix_sockets (i,s)
1758
        {
1759
                unix_state_rlock(s);
1760
 
1761
                len+=sprintf(buffer+len,"%p: %08X %08X %08X %04X %02X %5lu",
1762
                        s,
1763
                        atomic_read(&s->refcnt),
1764
                        0,
1765
                        s->state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
1766
                        s->type,
1767
                        s->socket ?
1768
                        (s->state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
1769
                        (s->state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
1770
                        sock_i_ino(s));
1771
 
1772
                if (s->protinfo.af_unix.addr)
1773
                {
1774
                        buffer[len++] = ' ';
1775
                        memcpy(buffer+len, s->protinfo.af_unix.addr->name->sun_path,
1776
                               s->protinfo.af_unix.addr->len-sizeof(short));
1777
                        if (!UNIX_ABSTRACT(s))
1778
                                len--;
1779
                        else
1780
                                buffer[len] = '@';
1781
                        len += s->protinfo.af_unix.addr->len - sizeof(short);
1782
                }
1783
                unix_state_runlock(s);
1784
 
1785
                buffer[len++]='\n';
1786
 
1787
                pos = begin + len;
1788
                if(pos<offset)
1789
                {
1790
                        len=0;
1791
                        begin=pos;
1792
                }
1793
                if(pos>offset+length)
1794
                        goto done;
1795
        }
1796
        *eof = 1;
1797
done:
1798
        read_unlock(&unix_table_lock);
1799
        *start=buffer+(offset-begin);
1800
        len-=(offset-begin);
1801
        if(len>length)
1802
                len=length;
1803
        if (len < 0)
1804
                len = 0;
1805
        return len;
1806
}
1807
#endif
1808
 
1809
struct proto_ops unix_stream_ops = {
1810
        family:         PF_UNIX,
1811
 
1812
        release:        unix_release,
1813
        bind:           unix_bind,
1814
        connect:        unix_stream_connect,
1815
        socketpair:     unix_socketpair,
1816
        accept:         unix_accept,
1817
        getname:        unix_getname,
1818
        poll:           unix_poll,
1819
        ioctl:          unix_ioctl,
1820
        listen:         unix_listen,
1821
        shutdown:       unix_shutdown,
1822
        setsockopt:     sock_no_setsockopt,
1823
        getsockopt:     sock_no_getsockopt,
1824
        sendmsg:        unix_stream_sendmsg,
1825
        recvmsg:        unix_stream_recvmsg,
1826
        mmap:           sock_no_mmap,
1827
        sendpage:       sock_no_sendpage,
1828
};
1829
 
1830
struct proto_ops unix_dgram_ops = {
1831
        family:         PF_UNIX,
1832
 
1833
        release:        unix_release,
1834
        bind:           unix_bind,
1835
        connect:        unix_dgram_connect,
1836
        socketpair:     unix_socketpair,
1837
        accept:         sock_no_accept,
1838
        getname:        unix_getname,
1839
        poll:           datagram_poll,
1840
        ioctl:          unix_ioctl,
1841
        listen:         sock_no_listen,
1842
        shutdown:       unix_shutdown,
1843
        setsockopt:     sock_no_setsockopt,
1844
        getsockopt:     sock_no_getsockopt,
1845
        sendmsg:        unix_dgram_sendmsg,
1846
        recvmsg:        unix_dgram_recvmsg,
1847
        mmap:           sock_no_mmap,
1848
        sendpage:       sock_no_sendpage,
1849
};
1850
 
1851
struct net_proto_family unix_family_ops = {
1852
        family:         PF_UNIX,
1853
        create:         unix_create
1854
};
1855
 
1856
#ifdef CONFIG_SYSCTL
1857
extern void unix_sysctl_register(void);
1858
extern void unix_sysctl_unregister(void);
1859
#else
1860
static inline void unix_sysctl_register(void) {}
1861
static inline void unix_sysctl_unregister(void) {}
1862
#endif
1863
 
1864
static char banner[] __initdata = KERN_INFO "NET4: Unix domain sockets 1.0/SMP for Linux NET4.0.\n";
1865
 
1866
static int __init af_unix_init(void)
1867
{
1868
        struct sk_buff *dummy_skb;
1869
 
1870
        printk(banner);
1871
        if (sizeof(struct unix_skb_parms) > sizeof(dummy_skb->cb))
1872
        {
1873
                printk(KERN_CRIT "unix_proto_init: panic\n");
1874
                return -1;
1875
        }
1876
        sock_register(&unix_family_ops);
1877
#ifdef CONFIG_PROC_FS
1878
        create_proc_read_entry("net/unix", 0, 0, unix_read_proc, NULL);
1879
#endif
1880
        unix_sysctl_register();
1881
        return 0;
1882
}
1883
 
1884
static void __exit af_unix_exit(void)
1885
{
1886
        sock_unregister(PF_UNIX);
1887
        unix_sysctl_unregister();
1888
        remove_proc_entry("net/unix", 0);
1889
}
1890
 
1891
module_init(af_unix_init);
1892
module_exit(af_unix_exit);
1893
 
1894
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.