blob: cb90d8a998821d607fb7fbf63f694f5d8c4beffc [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/socket.c: TIPC socket API
3 *
Per Liden9da1c8b2006-01-11 18:40:41 +01004 * Copyright (c) 2003-2006, Ericsson AB
Per Lidenb97bf3f2006-01-02 19:04:38 +01005 * Copyright (c) 2004-2005, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include <linux/module.h>
38#include <linux/types.h>
39#include <linux/net.h>
40#include <linux/socket.h>
41#include <linux/errno.h>
42#include <linux/mm.h>
43#include <linux/slab.h>
44#include <linux/poll.h>
45#include <linux/version.h>
46#include <linux/fcntl.h>
47#include <linux/version.h>
48#include <asm/semaphore.h>
49#include <asm/string.h>
50#include <asm/atomic.h>
51#include <net/sock.h>
52
53#include <linux/tipc.h>
Per Lidenea714cc2006-01-11 12:28:47 +010054#include <linux/tipc_config.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010055#include <net/tipc/tipc_msg.h>
56#include <net/tipc/tipc_port.h>
57
58#include "core.h"
59
60#define SS_LISTENING -1 /* socket is listening */
61#define SS_READY -2 /* socket is connectionless */
62
63#define OVERLOAD_LIMIT_BASE 5000
64
65struct tipc_sock {
66 struct sock sk;
67 struct tipc_port *p;
68 struct semaphore sem;
69};
70
71#define tipc_sk(sk) ((struct tipc_sock*)sk)
72
73static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
74static void wakeupdispatch(struct tipc_port *tport);
75
76static struct proto_ops packet_ops;
77static struct proto_ops stream_ops;
78static struct proto_ops msg_ops;
79
80static struct proto tipc_proto;
81
82static int sockets_enabled = 0;
83
84static atomic_t tipc_queue_size = ATOMIC_INIT(0);
85
86
87/*
88 * sock_lock(): Lock a port/socket pair. lock_sock() can
89 * not be used here, since the same lock must protect ports
90 * with non-socket interfaces.
91 * See net.c for description of locking policy.
92 */
93static inline void sock_lock(struct tipc_sock* tsock)
94{
95 spin_lock_bh(tsock->p->lock);
96}
97
98/*
99 * sock_unlock(): Unlock a port/socket pair
100 */
101static inline void sock_unlock(struct tipc_sock* tsock)
102{
103 spin_unlock_bh(tsock->p->lock);
104}
105
106/**
107 * pollmask - determine the current set of poll() events for a socket
108 * @sock: socket structure
109 *
110 * TIPC sets the returned events as follows:
111 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
112 * or if a connection-oriented socket is does not have an active connection
113 * (i.e. a read operation will not block).
114 * b) POLLOUT is set except when a socket's connection has been terminated
115 * (i.e. a write operation will not block).
116 * c) POLLHUP is set when a socket's connection has been terminated.
117 *
118 * IMPORTANT: The fact that a read or write operation will not block does NOT
119 * imply that the operation will succeed!
120 *
121 * Returns pollmask value
122 */
123
124static inline u32 pollmask(struct socket *sock)
125{
126 u32 mask;
127
128 if ((skb_queue_len(&sock->sk->sk_receive_queue) != 0) ||
129 (sock->state == SS_UNCONNECTED) ||
130 (sock->state == SS_DISCONNECTING))
131 mask = (POLLRDNORM | POLLIN);
132 else
133 mask = 0;
134
135 if (sock->state == SS_DISCONNECTING)
136 mask |= POLLHUP;
137 else
138 mask |= POLLOUT;
139
140 return mask;
141}
142
143
144/**
145 * advance_queue - discard first buffer in queue
146 * @tsock: TIPC socket
147 */
148
149static inline void advance_queue(struct tipc_sock *tsock)
150{
151 sock_lock(tsock);
152 buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue));
153 sock_unlock(tsock);
154 atomic_dec(&tipc_queue_size);
155}
156
157/**
158 * tipc_create - create a TIPC socket
159 * @sock: pre-allocated socket structure
160 * @protocol: protocol indicator (must be 0)
161 *
162 * This routine creates and attaches a 'struct sock' to the 'struct socket',
163 * then create and attaches a TIPC port to the 'struct sock' part.
164 *
165 * Returns 0 on success, errno otherwise
166 */
167static int tipc_create(struct socket *sock, int protocol)
168{
169 struct tipc_sock *tsock;
170 struct tipc_port *port;
171 struct sock *sk;
172 u32 ref;
173
174 if ((sock->type != SOCK_STREAM) &&
175 (sock->type != SOCK_SEQPACKET) &&
176 (sock->type != SOCK_DGRAM) &&
177 (sock->type != SOCK_RDM))
178 return -EPROTOTYPE;
179
180 if (unlikely(protocol != 0))
181 return -EPROTONOSUPPORT;
182
183 ref = tipc_createport_raw(0, &dispatch, &wakeupdispatch, TIPC_LOW_IMPORTANCE);
184 if (unlikely(!ref))
185 return -ENOMEM;
186
187 sock->state = SS_UNCONNECTED;
188
189 switch (sock->type) {
190 case SOCK_STREAM:
191 sock->ops = &stream_ops;
192 break;
193 case SOCK_SEQPACKET:
194 sock->ops = &packet_ops;
195 break;
196 case SOCK_DGRAM:
197 tipc_set_portunreliable(ref, 1);
198 /* fall through */
199 case SOCK_RDM:
200 tipc_set_portunreturnable(ref, 1);
201 sock->ops = &msg_ops;
202 sock->state = SS_READY;
203 break;
204 }
205
206 sk = sk_alloc(AF_TIPC, GFP_KERNEL, &tipc_proto, 1);
207 if (!sk) {
208 tipc_deleteport(ref);
209 return -ENOMEM;
210 }
211
212 sock_init_data(sock, sk);
213 init_waitqueue_head(sk->sk_sleep);
214 sk->sk_rcvtimeo = 8 * HZ; /* default connect timeout = 8s */
215
216 tsock = tipc_sk(sk);
217 port = tipc_get_port(ref);
218
219 tsock->p = port;
220 port->usr_handle = tsock;
221
222 init_MUTEX(&tsock->sem);
223
224 dbg("sock_create: %x\n",tsock);
225
226 atomic_inc(&tipc_user_count);
227
228 return 0;
229}
230
231/**
232 * release - destroy a TIPC socket
233 * @sock: socket to destroy
234 *
235 * This routine cleans up any messages that are still queued on the socket.
236 * For DGRAM and RDM socket types, all queued messages are rejected.
237 * For SEQPACKET and STREAM socket types, the first message is rejected
238 * and any others are discarded. (If the first message on a STREAM socket
239 * is partially-read, it is discarded and the next one is rejected instead.)
240 *
241 * NOTE: Rejected messages are not necessarily returned to the sender! They
242 * are returned or discarded according to the "destination droppable" setting
243 * specified for the message by the sender.
244 *
245 * Returns 0 on success, errno otherwise
246 */
247
248static int release(struct socket *sock)
249{
250 struct tipc_sock *tsock = tipc_sk(sock->sk);
251 struct sock *sk = sock->sk;
252 int res = TIPC_OK;
253 struct sk_buff *buf;
254
255 dbg("sock_delete: %x\n",tsock);
256 if (!tsock)
257 return 0;
258 down_interruptible(&tsock->sem);
259 if (!sock->sk) {
260 up(&tsock->sem);
261 return 0;
262 }
263
264 /* Reject unreceived messages, unless no longer connected */
265
266 while (sock->state != SS_DISCONNECTING) {
267 sock_lock(tsock);
268 buf = skb_dequeue(&sk->sk_receive_queue);
269 if (!buf)
270 tsock->p->usr_handle = 0;
271 sock_unlock(tsock);
272 if (!buf)
273 break;
274 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
275 buf_discard(buf);
276 else
277 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
278 atomic_dec(&tipc_queue_size);
279 }
280
281 /* Delete TIPC port */
282
283 res = tipc_deleteport(tsock->p->ref);
284 sock->sk = NULL;
285
286 /* Discard any remaining messages */
287
288 while ((buf = skb_dequeue(&sk->sk_receive_queue))) {
289 buf_discard(buf);
290 atomic_dec(&tipc_queue_size);
291 }
292
293 up(&tsock->sem);
294
295 sock_put(sk);
296
297 atomic_dec(&tipc_user_count);
298 return res;
299}
300
301/**
302 * bind - associate or disassocate TIPC name(s) with a socket
303 * @sock: socket structure
304 * @uaddr: socket address describing name(s) and desired operation
305 * @uaddr_len: size of socket address data structure
306 *
307 * Name and name sequence binding is indicated using a positive scope value;
308 * a negative scope value unbinds the specified name. Specifying no name
309 * (i.e. a socket address length of 0) unbinds all names from the socket.
310 *
311 * Returns 0 on success, errno otherwise
312 */
313
314static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
315{
316 struct tipc_sock *tsock = tipc_sk(sock->sk);
317 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
318 int res;
319
320 if (down_interruptible(&tsock->sem))
321 return -ERESTARTSYS;
322
323 if (unlikely(!uaddr_len)) {
324 res = tipc_withdraw(tsock->p->ref, 0, 0);
325 goto exit;
326 }
327
328 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
329 res = -EINVAL;
330 goto exit;
331 }
332
333 if (addr->family != AF_TIPC) {
334 res = -EAFNOSUPPORT;
335 goto exit;
336 }
337 if (addr->addrtype == TIPC_ADDR_NAME)
338 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
339 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
340 res = -EAFNOSUPPORT;
341 goto exit;
342 }
343
344 if (addr->scope > 0)
345 res = tipc_publish(tsock->p->ref, addr->scope,
346 &addr->addr.nameseq);
347 else
348 res = tipc_withdraw(tsock->p->ref, -addr->scope,
349 &addr->addr.nameseq);
350exit:
351 up(&tsock->sem);
352 return res;
353}
354
355/**
356 * get_name - get port ID of socket or peer socket
357 * @sock: socket structure
358 * @uaddr: area for returned socket address
359 * @uaddr_len: area for returned length of socket address
360 * @peer: 0 to obtain socket name, 1 to obtain peer socket name
361 *
362 * Returns 0 on success, errno otherwise
363 */
364
365static int get_name(struct socket *sock, struct sockaddr *uaddr,
366 int *uaddr_len, int peer)
367{
368 struct tipc_sock *tsock = tipc_sk(sock->sk);
369 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
370 u32 res;
371
372 if (down_interruptible(&tsock->sem))
373 return -ERESTARTSYS;
374
375 *uaddr_len = sizeof(*addr);
376 addr->addrtype = TIPC_ADDR_ID;
377 addr->family = AF_TIPC;
378 addr->scope = 0;
379 if (peer)
380 res = tipc_peer(tsock->p->ref, &addr->addr.id);
381 else
382 res = tipc_ownidentity(tsock->p->ref, &addr->addr.id);
383 addr->addr.name.domain = 0;
384
385 up(&tsock->sem);
386 return res;
387}
388
389/**
390 * poll - read and possibly block on pollmask
391 * @file: file structure associated with the socket
392 * @sock: socket for which to calculate the poll bits
393 * @wait: ???
394 *
395 * Returns the pollmask
396 */
397
398static unsigned int poll(struct file *file, struct socket *sock,
399 poll_table *wait)
400{
401 poll_wait(file, sock->sk->sk_sleep, wait);
402 /* NEED LOCK HERE? */
403 return pollmask(sock);
404}
405
406/**
407 * dest_name_check - verify user is permitted to send to specified port name
408 * @dest: destination address
409 * @m: descriptor for message to be sent
410 *
411 * Prevents restricted configuration commands from being issued by
412 * unauthorized users.
413 *
414 * Returns 0 if permission is granted, otherwise errno
415 */
416
417static inline int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
418{
419 struct tipc_cfg_msg_hdr hdr;
420
421 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
422 return 0;
423 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
424 return 0;
425
426 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
427 return -EACCES;
428
429 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
430 return -EFAULT;
431 if ((ntohs(hdr.tcm_type) & 0xC000) & (!capable(CAP_NET_ADMIN)))
432 return -EACCES;
433
434 return 0;
435}
436
437/**
438 * send_msg - send message in connectionless manner
439 * @iocb: (unused)
440 * @sock: socket structure
441 * @m: message to send
442 * @total_len: (unused)
443 *
444 * Message must have an destination specified explicitly.
445 * Used for SOCK_RDM and SOCK_DGRAM messages,
446 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
447 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
448 *
449 * Returns the number of bytes sent on success, or errno otherwise
450 */
451
452static int send_msg(struct kiocb *iocb, struct socket *sock,
453 struct msghdr *m, size_t total_len)
454{
455 struct tipc_sock *tsock = tipc_sk(sock->sk);
456 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
457 struct sk_buff *buf;
458 int needs_conn;
459 int res = -EINVAL;
460
461 if (unlikely(!dest))
462 return -EDESTADDRREQ;
463 if (unlikely(dest->family != AF_TIPC))
464 return -EINVAL;
465
466 needs_conn = (sock->state != SS_READY);
467 if (unlikely(needs_conn)) {
468 if (sock->state == SS_LISTENING)
469 return -EPIPE;
470 if (sock->state != SS_UNCONNECTED)
471 return -EISCONN;
472 if ((tsock->p->published) ||
473 ((sock->type == SOCK_STREAM) && (total_len != 0)))
474 return -EOPNOTSUPP;
475 }
476
477 if (down_interruptible(&tsock->sem))
478 return -ERESTARTSYS;
479
480 if (needs_conn) {
481
482 /* Abort any pending connection attempts (very unlikely) */
483
484 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
485 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
486 atomic_dec(&tipc_queue_size);
487 }
488
489 sock->state = SS_CONNECTING;
490 }
491
492 do {
493 if (dest->addrtype == TIPC_ADDR_NAME) {
494 if ((res = dest_name_check(dest, m)))
495 goto exit;
496 res = tipc_send2name(tsock->p->ref,
497 &dest->addr.name.name,
498 dest->addr.name.domain,
499 m->msg_iovlen,
500 m->msg_iov);
501 }
502 else if (dest->addrtype == TIPC_ADDR_ID) {
503 res = tipc_send2port(tsock->p->ref,
504 &dest->addr.id,
505 m->msg_iovlen,
506 m->msg_iov);
507 }
508 else if (dest->addrtype == TIPC_ADDR_MCAST) {
509 if (needs_conn) {
510 res = -EOPNOTSUPP;
511 goto exit;
512 }
513 if ((res = dest_name_check(dest, m)))
514 goto exit;
515 res = tipc_multicast(tsock->p->ref,
516 &dest->addr.nameseq,
517 0,
518 m->msg_iovlen,
519 m->msg_iov);
520 }
521 if (likely(res != -ELINKCONG)) {
522exit:
523 up(&tsock->sem);
524 return res;
525 }
526 if (m->msg_flags & MSG_DONTWAIT) {
527 res = -EWOULDBLOCK;
528 goto exit;
529 }
530 if (wait_event_interruptible(*sock->sk->sk_sleep,
531 !tsock->p->congested)) {
532 res = -ERESTARTSYS;
533 goto exit;
534 }
535 } while (1);
536}
537
538/**
539 * send_packet - send a connection-oriented message
540 * @iocb: (unused)
541 * @sock: socket structure
542 * @m: message to send
543 * @total_len: (unused)
544 *
545 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
546 *
547 * Returns the number of bytes sent on success, or errno otherwise
548 */
549
550static int send_packet(struct kiocb *iocb, struct socket *sock,
551 struct msghdr *m, size_t total_len)
552{
553 struct tipc_sock *tsock = tipc_sk(sock->sk);
554 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
555 int res;
556
557 /* Handle implied connection establishment */
558
559 if (unlikely(dest))
560 return send_msg(iocb, sock, m, total_len);
561
562 if (down_interruptible(&tsock->sem)) {
563 return -ERESTARTSYS;
564 }
565
566 if (unlikely(sock->state != SS_CONNECTED)) {
567 if (sock->state == SS_DISCONNECTING)
568 res = -EPIPE;
569 else
570 res = -ENOTCONN;
571 goto exit;
572 }
573
574 do {
575 res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov);
576 if (likely(res != -ELINKCONG)) {
577exit:
578 up(&tsock->sem);
579 return res;
580 }
581 if (m->msg_flags & MSG_DONTWAIT) {
582 res = -EWOULDBLOCK;
583 goto exit;
584 }
585 if (wait_event_interruptible(*sock->sk->sk_sleep,
586 !tsock->p->congested)) {
587 res = -ERESTARTSYS;
588 goto exit;
589 }
590 } while (1);
591}
592
593/**
594 * send_stream - send stream-oriented data
595 * @iocb: (unused)
596 * @sock: socket structure
597 * @m: data to send
598 * @total_len: total length of data to be sent
599 *
600 * Used for SOCK_STREAM data.
601 *
602 * Returns the number of bytes sent on success, or errno otherwise
603 */
604
605
606static int send_stream(struct kiocb *iocb, struct socket *sock,
607 struct msghdr *m, size_t total_len)
608{
609 struct msghdr my_msg;
610 struct iovec my_iov;
611 struct iovec *curr_iov;
612 int curr_iovlen;
613 char __user *curr_start;
614 int curr_left;
615 int bytes_to_send;
616 int res;
617
618 if (likely(total_len <= TIPC_MAX_USER_MSG_SIZE))
619 return send_packet(iocb, sock, m, total_len);
620
621 /* Can only send large data streams if already connected */
622
623 if (unlikely(sock->state != SS_CONNECTED)) {
624 if (sock->state == SS_DISCONNECTING)
625 return -EPIPE;
626 else
627 return -ENOTCONN;
628 }
629
630 /*
631 * Send each iovec entry using one or more messages
632 *
633 * Note: This algorithm is good for the most likely case
634 * (i.e. one large iovec entry), but could be improved to pass sets
635 * of small iovec entries into send_packet().
636 */
637
638 my_msg = *m;
639 curr_iov = my_msg.msg_iov;
640 curr_iovlen = my_msg.msg_iovlen;
641 my_msg.msg_iov = &my_iov;
642 my_msg.msg_iovlen = 1;
643
644 while (curr_iovlen--) {
645 curr_start = curr_iov->iov_base;
646 curr_left = curr_iov->iov_len;
647
648 while (curr_left) {
649 bytes_to_send = (curr_left < TIPC_MAX_USER_MSG_SIZE)
650 ? curr_left : TIPC_MAX_USER_MSG_SIZE;
651 my_iov.iov_base = curr_start;
652 my_iov.iov_len = bytes_to_send;
653 if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0)
654 return res;
655 curr_left -= bytes_to_send;
656 curr_start += bytes_to_send;
657 }
658
659 curr_iov++;
660 }
661
662 return total_len;
663}
664
665/**
666 * auto_connect - complete connection setup to a remote port
667 * @sock: socket structure
668 * @tsock: TIPC-specific socket structure
669 * @msg: peer's response message
670 *
671 * Returns 0 on success, errno otherwise
672 */
673
674static int auto_connect(struct socket *sock, struct tipc_sock *tsock,
675 struct tipc_msg *msg)
676{
677 struct tipc_portid peer;
678
679 if (msg_errcode(msg)) {
680 sock->state = SS_DISCONNECTING;
681 return -ECONNREFUSED;
682 }
683
684 peer.ref = msg_origport(msg);
685 peer.node = msg_orignode(msg);
686 tipc_connect2port(tsock->p->ref, &peer);
687 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
688 sock->state = SS_CONNECTED;
689 return 0;
690}
691
692/**
693 * set_orig_addr - capture sender's address for received message
694 * @m: descriptor for message info
695 * @msg: received message header
696 *
697 * Note: Address is not captured if not requested by receiver.
698 */
699
700static inline void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
701{
702 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
703
704 if (addr) {
705 addr->family = AF_TIPC;
706 addr->addrtype = TIPC_ADDR_ID;
707 addr->addr.id.ref = msg_origport(msg);
708 addr->addr.id.node = msg_orignode(msg);
709 addr->addr.name.domain = 0; /* could leave uninitialized */
710 addr->scope = 0; /* could leave uninitialized */
711 m->msg_namelen = sizeof(struct sockaddr_tipc);
712 }
713}
714
715/**
716 * anc_data_recv - optionally capture ancillary data for received message
717 * @m: descriptor for message info
718 * @msg: received message header
719 * @tport: TIPC port associated with message
720 *
721 * Note: Ancillary data is not captured if not requested by receiver.
722 *
723 * Returns 0 if successful, otherwise errno
724 */
725
726static inline int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
727 struct tipc_port *tport)
728{
729 u32 anc_data[3];
730 u32 err;
731 u32 dest_type;
732 int res;
733
734 if (likely(m->msg_controllen == 0))
735 return 0;
736
737 /* Optionally capture errored message object(s) */
738
739 err = msg ? msg_errcode(msg) : 0;
740 if (unlikely(err)) {
741 anc_data[0] = err;
742 anc_data[1] = msg_data_sz(msg);
743 if ((res = put_cmsg(m, SOL_SOCKET, TIPC_ERRINFO, 8, anc_data)))
744 return res;
745 if (anc_data[1] &&
746 (res = put_cmsg(m, SOL_SOCKET, TIPC_RETDATA, anc_data[1],
747 msg_data(msg))))
748 return res;
749 }
750
751 /* Optionally capture message destination object */
752
753 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
754 switch (dest_type) {
755 case TIPC_NAMED_MSG:
756 anc_data[0] = msg_nametype(msg);
757 anc_data[1] = msg_namelower(msg);
758 anc_data[2] = msg_namelower(msg);
759 break;
760 case TIPC_MCAST_MSG:
761 anc_data[0] = msg_nametype(msg);
762 anc_data[1] = msg_namelower(msg);
763 anc_data[2] = msg_nameupper(msg);
764 break;
765 case TIPC_CONN_MSG:
766 anc_data[0] = tport->conn_type;
767 anc_data[1] = tport->conn_instance;
768 anc_data[2] = tport->conn_instance;
769 break;
770 default:
771 anc_data[0] = 0;
772 }
773 if (anc_data[0] &&
774 (res = put_cmsg(m, SOL_SOCKET, TIPC_DESTNAME, 12, anc_data)))
775 return res;
776
777 return 0;
778}
779
780/**
781 * recv_msg - receive packet-oriented message
782 * @iocb: (unused)
783 * @m: descriptor for message info
784 * @buf_len: total size of user buffer area
785 * @flags: receive flags
786 *
787 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
788 * If the complete message doesn't fit in user area, truncate it.
789 *
790 * Returns size of returned message data, errno otherwise
791 */
792
793static int recv_msg(struct kiocb *iocb, struct socket *sock,
794 struct msghdr *m, size_t buf_len, int flags)
795{
796 struct tipc_sock *tsock = tipc_sk(sock->sk);
797 struct sk_buff *buf;
798 struct tipc_msg *msg;
799 unsigned int q_len;
800 unsigned int sz;
801 u32 err;
802 int res;
803
804 /* Currently doesn't support receiving into multiple iovec entries */
805
806 if (m->msg_iovlen != 1)
807 return -EOPNOTSUPP;
808
809 /* Catch invalid receive attempts */
810
811 if (unlikely(!buf_len))
812 return -EINVAL;
813
814 if (sock->type == SOCK_SEQPACKET) {
815 if (unlikely(sock->state == SS_UNCONNECTED))
816 return -ENOTCONN;
817 if (unlikely((sock->state == SS_DISCONNECTING) &&
818 (skb_queue_len(&sock->sk->sk_receive_queue) == 0)))
819 return -ENOTCONN;
820 }
821
822 /* Look for a message in receive queue; wait if necessary */
823
824 if (unlikely(down_interruptible(&tsock->sem)))
825 return -ERESTARTSYS;
826
827restart:
828 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
829 (flags & MSG_DONTWAIT))) {
830 res = -EWOULDBLOCK;
831 goto exit;
832 }
833
834 if ((res = wait_event_interruptible(
835 *sock->sk->sk_sleep,
836 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
837 (sock->state == SS_DISCONNECTING))) )) {
838 goto exit;
839 }
840
841 /* Catch attempt to receive on an already terminated connection */
842 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
843
844 if (!q_len) {
845 res = -ENOTCONN;
846 goto exit;
847 }
848
849 /* Get access to first message in receive queue */
850
851 buf = skb_peek(&sock->sk->sk_receive_queue);
852 msg = buf_msg(buf);
853 sz = msg_data_sz(msg);
854 err = msg_errcode(msg);
855
856 /* Complete connection setup for an implied connect */
857
858 if (unlikely(sock->state == SS_CONNECTING)) {
859 if ((res = auto_connect(sock, tsock, msg)))
860 goto exit;
861 }
862
863 /* Discard an empty non-errored message & try again */
864
865 if ((!sz) && (!err)) {
866 advance_queue(tsock);
867 goto restart;
868 }
869
870 /* Capture sender's address (optional) */
871
872 set_orig_addr(m, msg);
873
874 /* Capture ancillary data (optional) */
875
876 if ((res = anc_data_recv(m, msg, tsock->p)))
877 goto exit;
878
879 /* Capture message data (if valid) & compute return value (always) */
880
881 if (!err) {
882 if (unlikely(buf_len < sz)) {
883 sz = buf_len;
884 m->msg_flags |= MSG_TRUNC;
885 }
886 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
887 sz))) {
888 res = -EFAULT;
889 goto exit;
890 }
891 res = sz;
892 } else {
893 if ((sock->state == SS_READY) ||
894 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
895 res = 0;
896 else
897 res = -ECONNRESET;
898 }
899
900 /* Consume received message (optional) */
901
902 if (likely(!(flags & MSG_PEEK))) {
903 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
904 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
905 advance_queue(tsock);
906 }
907exit:
908 up(&tsock->sem);
909 return res;
910}
911
912/**
913 * recv_stream - receive stream-oriented data
914 * @iocb: (unused)
915 * @m: descriptor for message info
916 * @buf_len: total size of user buffer area
917 * @flags: receive flags
918 *
919 * Used for SOCK_STREAM messages only. If not enough data is available
920 * will optionally wait for more; never truncates data.
921 *
922 * Returns size of returned message data, errno otherwise
923 */
924
925static int recv_stream(struct kiocb *iocb, struct socket *sock,
926 struct msghdr *m, size_t buf_len, int flags)
927{
928 struct tipc_sock *tsock = tipc_sk(sock->sk);
929 struct sk_buff *buf;
930 struct tipc_msg *msg;
931 unsigned int q_len;
932 unsigned int sz;
933 int sz_to_copy;
934 int sz_copied = 0;
935 int needed;
936 char *crs = m->msg_iov->iov_base;
937 unsigned char *buf_crs;
938 u32 err;
939 int res;
940
941 /* Currently doesn't support receiving into multiple iovec entries */
942
943 if (m->msg_iovlen != 1)
944 return -EOPNOTSUPP;
945
946 /* Catch invalid receive attempts */
947
948 if (unlikely(!buf_len))
949 return -EINVAL;
950
951 if (unlikely(sock->state == SS_DISCONNECTING)) {
952 if (skb_queue_len(&sock->sk->sk_receive_queue) == 0)
953 return -ENOTCONN;
954 } else if (unlikely(sock->state != SS_CONNECTED))
955 return -ENOTCONN;
956
957 /* Look for a message in receive queue; wait if necessary */
958
959 if (unlikely(down_interruptible(&tsock->sem)))
960 return -ERESTARTSYS;
961
962restart:
963 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
964 (flags & MSG_DONTWAIT))) {
965 res = (sz_copied == 0) ? -EWOULDBLOCK : 0;
966 goto exit;
967 }
968
969 if ((res = wait_event_interruptible(
970 *sock->sk->sk_sleep,
971 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
972 (sock->state == SS_DISCONNECTING))) )) {
973 goto exit;
974 }
975
976 /* Catch attempt to receive on an already terminated connection */
977 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
978
979 if (!q_len) {
980 res = -ENOTCONN;
981 goto exit;
982 }
983
984 /* Get access to first message in receive queue */
985
986 buf = skb_peek(&sock->sk->sk_receive_queue);
987 msg = buf_msg(buf);
988 sz = msg_data_sz(msg);
989 err = msg_errcode(msg);
990
991 /* Discard an empty non-errored message & try again */
992
993 if ((!sz) && (!err)) {
994 advance_queue(tsock);
995 goto restart;
996 }
997
998 /* Optionally capture sender's address & ancillary data of first msg */
999
1000 if (sz_copied == 0) {
1001 set_orig_addr(m, msg);
1002 if ((res = anc_data_recv(m, msg, tsock->p)))
1003 goto exit;
1004 }
1005
1006 /* Capture message data (if valid) & compute return value (always) */
1007
1008 if (!err) {
1009 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
1010 sz = buf->tail - buf_crs;
1011
1012 needed = (buf_len - sz_copied);
1013 sz_to_copy = (sz <= needed) ? sz : needed;
1014 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1015 res = -EFAULT;
1016 goto exit;
1017 }
1018 sz_copied += sz_to_copy;
1019
1020 if (sz_to_copy < sz) {
1021 if (!(flags & MSG_PEEK))
1022 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1023 goto exit;
1024 }
1025
1026 crs += sz_to_copy;
1027 } else {
1028 if (sz_copied != 0)
1029 goto exit; /* can't add error msg to valid data */
1030
1031 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1032 res = 0;
1033 else
1034 res = -ECONNRESET;
1035 }
1036
1037 /* Consume received message (optional) */
1038
1039 if (likely(!(flags & MSG_PEEK))) {
1040 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1041 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
1042 advance_queue(tsock);
1043 }
1044
1045 /* Loop around if more data is required */
1046
1047 if ((sz_copied < buf_len) /* didn't get all requested data */
1048 && (flags & MSG_WAITALL) /* ... and need to wait for more */
1049 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
1050 && (!err) /* ... and haven't reached a FIN */
1051 )
1052 goto restart;
1053
1054exit:
1055 up(&tsock->sem);
1056 return res ? res : sz_copied;
1057}
1058
1059/**
1060 * queue_overloaded - test if queue overload condition exists
1061 * @queue_size: current size of queue
1062 * @base: nominal maximum size of queue
1063 * @msg: message to be added to queue
1064 *
1065 * Returns 1 if queue is currently overloaded, 0 otherwise
1066 */
1067
1068static int queue_overloaded(u32 queue_size, u32 base, struct tipc_msg *msg)
1069{
1070 u32 threshold;
1071 u32 imp = msg_importance(msg);
1072
1073 if (imp == TIPC_LOW_IMPORTANCE)
1074 threshold = base;
1075 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1076 threshold = base * 2;
1077 else if (imp == TIPC_HIGH_IMPORTANCE)
1078 threshold = base * 100;
1079 else
1080 return 0;
1081
1082 if (msg_connected(msg))
1083 threshold *= 4;
1084
1085 return (queue_size > threshold);
1086}
1087
1088/**
1089 * async_disconnect - wrapper function used to disconnect port
1090 * @portref: TIPC port reference (passed as pointer-sized value)
1091 */
1092
1093static void async_disconnect(unsigned long portref)
1094{
1095 tipc_disconnect((u32)portref);
1096}
1097
1098/**
1099 * dispatch - handle arriving message
1100 * @tport: TIPC port that received message
1101 * @buf: message
1102 *
1103 * Called with port locked. Must not take socket lock to avoid deadlock risk.
1104 *
1105 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1106 */
1107
1108static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1109{
1110 struct tipc_msg *msg = buf_msg(buf);
1111 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1112 struct socket *sock;
1113 u32 recv_q_len;
1114
1115 /* Reject message if socket is closing */
1116
1117 if (!tsock)
1118 return TIPC_ERR_NO_PORT;
1119
1120 /* Reject message if it is wrong sort of message for socket */
1121
1122 /*
1123 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1124 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1125 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1126 */
1127 sock = tsock->sk.sk_socket;
1128 if (sock->state == SS_READY) {
1129 if (msg_connected(msg)) {
1130 msg_dbg(msg, "dispatch filter 1\n");
1131 return TIPC_ERR_NO_PORT;
1132 }
1133 } else {
1134 if (msg_mcast(msg)) {
1135 msg_dbg(msg, "dispatch filter 2\n");
1136 return TIPC_ERR_NO_PORT;
1137 }
1138 if (sock->state == SS_CONNECTED) {
1139 if (!msg_connected(msg)) {
1140 msg_dbg(msg, "dispatch filter 3\n");
1141 return TIPC_ERR_NO_PORT;
1142 }
1143 }
1144 else if (sock->state == SS_CONNECTING) {
1145 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1146 msg_dbg(msg, "dispatch filter 4\n");
1147 return TIPC_ERR_NO_PORT;
1148 }
1149 }
1150 else if (sock->state == SS_LISTENING) {
1151 if (msg_connected(msg) || msg_errcode(msg)) {
1152 msg_dbg(msg, "dispatch filter 5\n");
1153 return TIPC_ERR_NO_PORT;
1154 }
1155 }
1156 else if (sock->state == SS_DISCONNECTING) {
1157 msg_dbg(msg, "dispatch filter 6\n");
1158 return TIPC_ERR_NO_PORT;
1159 }
1160 else /* (sock->state == SS_UNCONNECTED) */ {
1161 if (msg_connected(msg) || msg_errcode(msg)) {
1162 msg_dbg(msg, "dispatch filter 7\n");
1163 return TIPC_ERR_NO_PORT;
1164 }
1165 }
1166 }
1167
1168 /* Reject message if there isn't room to queue it */
1169
1170 if (unlikely((u32)atomic_read(&tipc_queue_size) >
1171 OVERLOAD_LIMIT_BASE)) {
1172 if (queue_overloaded(atomic_read(&tipc_queue_size),
1173 OVERLOAD_LIMIT_BASE, msg))
1174 return TIPC_ERR_OVERLOAD;
1175 }
1176 recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue);
1177 if (unlikely(recv_q_len > (OVERLOAD_LIMIT_BASE / 2))) {
1178 if (queue_overloaded(recv_q_len,
1179 OVERLOAD_LIMIT_BASE / 2, msg))
1180 return TIPC_ERR_OVERLOAD;
1181 }
1182
1183 /* Initiate connection termination for an incoming 'FIN' */
1184
1185 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1186 sock->state = SS_DISCONNECTING;
1187 /* Note: Use signal since port lock is already taken! */
1188 k_signal((Handler)async_disconnect, tport->ref);
1189 }
1190
1191 /* Enqueue message (finally!) */
1192
1193 msg_dbg(msg,"<DISP<: ");
1194 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1195 atomic_inc(&tipc_queue_size);
1196 skb_queue_tail(&sock->sk->sk_receive_queue, buf);
1197
1198 wake_up_interruptible(sock->sk->sk_sleep);
1199 return TIPC_OK;
1200}
1201
1202/**
1203 * wakeupdispatch - wake up port after congestion
1204 * @tport: port to wakeup
1205 *
1206 * Called with port lock on.
1207 */
1208
1209static void wakeupdispatch(struct tipc_port *tport)
1210{
1211 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1212
1213 wake_up_interruptible(tsock->sk.sk_sleep);
1214}
1215
1216/**
1217 * connect - establish a connection to another TIPC port
1218 * @sock: socket structure
1219 * @dest: socket address for destination port
1220 * @destlen: size of socket address data structure
1221 * @flags: (unused)
1222 *
1223 * Returns 0 on success, errno otherwise
1224 */
1225
1226static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1227 int flags)
1228{
1229 struct tipc_sock *tsock = tipc_sk(sock->sk);
1230 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1231 struct msghdr m = {0,};
1232 struct sk_buff *buf;
1233 struct tipc_msg *msg;
1234 int res;
1235
1236 /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
1237
1238 if (sock->state == SS_READY)
1239 return -EOPNOTSUPP;
1240
1241 /* MOVE THE REST OF THIS ERROR CHECKING TO send_msg()? */
1242 if (sock->state == SS_LISTENING)
1243 return -EOPNOTSUPP;
1244 if (sock->state == SS_CONNECTING)
1245 return -EALREADY;
1246 if (sock->state != SS_UNCONNECTED)
1247 return -EISCONN;
1248
1249 if ((dst->family != AF_TIPC) ||
1250 ((dst->addrtype != TIPC_ADDR_NAME) && (dst->addrtype != TIPC_ADDR_ID)))
1251 return -EINVAL;
1252
1253 /* Send a 'SYN-' to destination */
1254
1255 m.msg_name = dest;
1256 if ((res = send_msg(0, sock, &m, 0)) < 0) {
1257 sock->state = SS_DISCONNECTING;
1258 return res;
1259 }
1260
1261 if (down_interruptible(&tsock->sem))
1262 return -ERESTARTSYS;
1263
1264 /* Wait for destination's 'ACK' response */
1265
1266 res = wait_event_interruptible_timeout(*sock->sk->sk_sleep,
1267 skb_queue_len(&sock->sk->sk_receive_queue),
1268 sock->sk->sk_rcvtimeo);
1269 buf = skb_peek(&sock->sk->sk_receive_queue);
1270 if (res > 0) {
1271 msg = buf_msg(buf);
1272 res = auto_connect(sock, tsock, msg);
1273 if (!res) {
1274 if (dst->addrtype == TIPC_ADDR_NAME) {
1275 tsock->p->conn_type = dst->addr.name.name.type;
1276 tsock->p->conn_instance = dst->addr.name.name.instance;
1277 }
1278 if (!msg_data_sz(msg))
1279 advance_queue(tsock);
1280 }
1281 } else {
1282 if (res == 0) {
1283 res = -ETIMEDOUT;
1284 } else
1285 { /* leave "res" unchanged */ }
1286 sock->state = SS_DISCONNECTING;
1287 }
1288
1289 up(&tsock->sem);
1290 return res;
1291}
1292
1293/**
1294 * listen - allow socket to listen for incoming connections
1295 * @sock: socket structure
1296 * @len: (unused)
1297 *
1298 * Returns 0 on success, errno otherwise
1299 */
1300
1301static int listen(struct socket *sock, int len)
1302{
1303 /* REQUIRES SOCKET LOCKING OF SOME SORT? */
1304
1305 if (sock->state == SS_READY)
1306 return -EOPNOTSUPP;
1307 if (sock->state != SS_UNCONNECTED)
1308 return -EINVAL;
1309 sock->state = SS_LISTENING;
1310 return 0;
1311}
1312
1313/**
1314 * accept - wait for connection request
1315 * @sock: listening socket
1316 * @newsock: new socket that is to be connected
1317 * @flags: file-related flags associated with socket
1318 *
1319 * Returns 0 on success, errno otherwise
1320 */
1321
1322static int accept(struct socket *sock, struct socket *newsock, int flags)
1323{
1324 struct tipc_sock *tsock = tipc_sk(sock->sk);
1325 struct sk_buff *buf;
1326 int res = -EFAULT;
1327
1328 if (sock->state == SS_READY)
1329 return -EOPNOTSUPP;
1330 if (sock->state != SS_LISTENING)
1331 return -EINVAL;
1332
1333 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
1334 (flags & O_NONBLOCK)))
1335 return -EWOULDBLOCK;
1336
1337 if (down_interruptible(&tsock->sem))
1338 return -ERESTARTSYS;
1339
1340 if (wait_event_interruptible(*sock->sk->sk_sleep,
1341 skb_queue_len(&sock->sk->sk_receive_queue))) {
1342 res = -ERESTARTSYS;
1343 goto exit;
1344 }
1345 buf = skb_peek(&sock->sk->sk_receive_queue);
1346
1347 res = tipc_create(newsock, 0);
1348 if (!res) {
1349 struct tipc_sock *new_tsock = tipc_sk(newsock->sk);
1350 struct tipc_portid id;
1351 struct tipc_msg *msg = buf_msg(buf);
1352 u32 new_ref = new_tsock->p->ref;
1353
1354 id.ref = msg_origport(msg);
1355 id.node = msg_orignode(msg);
1356 tipc_connect2port(new_ref, &id);
1357 newsock->state = SS_CONNECTED;
1358
1359 tipc_set_portimportance(new_ref, msg_importance(msg));
1360 if (msg_named(msg)) {
1361 new_tsock->p->conn_type = msg_nametype(msg);
1362 new_tsock->p->conn_instance = msg_nameinst(msg);
1363 }
1364
1365 /*
1366 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1367 * Respond to 'SYN+' by queuing it on new socket.
1368 */
1369
1370 msg_dbg(msg,"<ACC<: ");
1371 if (!msg_data_sz(msg)) {
1372 struct msghdr m = {0,};
1373
1374 send_packet(0, newsock, &m, 0);
1375 advance_queue(tsock);
1376 } else {
1377 sock_lock(tsock);
1378 skb_dequeue(&sock->sk->sk_receive_queue);
1379 sock_unlock(tsock);
1380 skb_queue_head(&newsock->sk->sk_receive_queue, buf);
1381 }
1382 }
1383exit:
1384 up(&tsock->sem);
1385 return res;
1386}
1387
1388/**
1389 * shutdown - shutdown socket connection
1390 * @sock: socket structure
1391 * @how: direction to close (always treated as read + write)
1392 *
1393 * Terminates connection (if necessary), then purges socket's receive queue.
1394 *
1395 * Returns 0 on success, errno otherwise
1396 */
1397
1398static int shutdown(struct socket *sock, int how)
1399{
1400 struct tipc_sock* tsock = tipc_sk(sock->sk);
1401 struct sk_buff *buf;
1402 int res;
1403
1404 /* Could return -EINVAL for an invalid "how", but why bother? */
1405
1406 if (down_interruptible(&tsock->sem))
1407 return -ERESTARTSYS;
1408
1409 sock_lock(tsock);
1410
1411 switch (sock->state) {
1412 case SS_CONNECTED:
1413
1414 /* Send 'FIN+' or 'FIN-' message to peer */
1415
1416 sock_unlock(tsock);
1417restart:
1418 if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1419 atomic_dec(&tipc_queue_size);
1420 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1421 buf_discard(buf);
1422 goto restart;
1423 }
1424 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
1425 }
1426 else {
1427 tipc_shutdown(tsock->p->ref);
1428 }
1429 sock_lock(tsock);
1430
1431 /* fall through */
1432
1433 case SS_DISCONNECTING:
1434
1435 /* Discard any unreceived messages */
1436
1437 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1438 atomic_dec(&tipc_queue_size);
1439 buf_discard(buf);
1440 }
1441 tsock->p->conn_unacked = 0;
1442
1443 /* fall through */
1444
1445 case SS_CONNECTING:
1446 sock->state = SS_DISCONNECTING;
1447 res = 0;
1448 break;
1449
1450 default:
1451 res = -ENOTCONN;
1452 }
1453
1454 sock_unlock(tsock);
1455
1456 up(&tsock->sem);
1457 return res;
1458}
1459
1460/**
1461 * setsockopt - set socket option
1462 * @sock: socket structure
1463 * @lvl: option level
1464 * @opt: option identifier
1465 * @ov: pointer to new option value
1466 * @ol: length of option value
1467 *
1468 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1469 * (to ease compatibility).
1470 *
1471 * Returns 0 on success, errno otherwise
1472 */
1473
1474static int setsockopt(struct socket *sock, int lvl, int opt, char *ov, int ol)
1475{
1476 struct tipc_sock *tsock = tipc_sk(sock->sk);
1477 u32 value;
1478 int res;
1479
1480 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1481 return 0;
1482 if (lvl != SOL_TIPC)
1483 return -ENOPROTOOPT;
1484 if (ol < sizeof(value))
1485 return -EINVAL;
1486 if ((res = get_user(value, (u32 *)ov)))
1487 return res;
1488
1489 if (down_interruptible(&tsock->sem))
1490 return -ERESTARTSYS;
1491
1492 switch (opt) {
1493 case TIPC_IMPORTANCE:
1494 res = tipc_set_portimportance(tsock->p->ref, value);
1495 break;
1496 case TIPC_SRC_DROPPABLE:
1497 if (sock->type != SOCK_STREAM)
1498 res = tipc_set_portunreliable(tsock->p->ref, value);
1499 else
1500 res = -ENOPROTOOPT;
1501 break;
1502 case TIPC_DEST_DROPPABLE:
1503 res = tipc_set_portunreturnable(tsock->p->ref, value);
1504 break;
1505 case TIPC_CONN_TIMEOUT:
1506 sock->sk->sk_rcvtimeo = (value * HZ / 1000);
1507 break;
1508 default:
1509 res = -EINVAL;
1510 }
1511
1512 up(&tsock->sem);
1513 return res;
1514}
1515
1516/**
1517 * getsockopt - get socket option
1518 * @sock: socket structure
1519 * @lvl: option level
1520 * @opt: option identifier
1521 * @ov: receptacle for option value
1522 * @ol: receptacle for length of option value
1523 *
1524 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1525 * (to ease compatibility).
1526 *
1527 * Returns 0 on success, errno otherwise
1528 */
1529
1530static int getsockopt(struct socket *sock, int lvl, int opt, char *ov, int *ol)
1531{
1532 struct tipc_sock *tsock = tipc_sk(sock->sk);
1533 int len;
1534 u32 value;
1535 int res;
1536
1537 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1538 return put_user(0, ol);
1539 if (lvl != SOL_TIPC)
1540 return -ENOPROTOOPT;
1541 if ((res = get_user(len, ol)))
1542 return res;
1543
1544 if (down_interruptible(&tsock->sem))
1545 return -ERESTARTSYS;
1546
1547 switch (opt) {
1548 case TIPC_IMPORTANCE:
1549 res = tipc_portimportance(tsock->p->ref, &value);
1550 break;
1551 case TIPC_SRC_DROPPABLE:
1552 res = tipc_portunreliable(tsock->p->ref, &value);
1553 break;
1554 case TIPC_DEST_DROPPABLE:
1555 res = tipc_portunreturnable(tsock->p->ref, &value);
1556 break;
1557 case TIPC_CONN_TIMEOUT:
1558 value = (sock->sk->sk_rcvtimeo * 1000) / HZ;
1559 break;
1560 default:
1561 res = -EINVAL;
1562 }
1563
1564 if (res) {
1565 /* "get" failed */
1566 }
1567 else if (len < sizeof(value)) {
1568 res = -EINVAL;
1569 }
1570 else if ((res = copy_to_user(ov, &value, sizeof(value)))) {
1571 /* couldn't return value */
1572 }
1573 else {
1574 res = put_user(sizeof(value), ol);
1575 }
1576
1577 up(&tsock->sem);
1578 return res;
1579}
1580
1581/**
1582 * Placeholders for non-implemented functionality
1583 *
1584 * Returns error code (POSIX-compliant where defined)
1585 */
1586
1587static int ioctl(struct socket *s, u32 cmd, unsigned long arg)
1588{
1589 return -EINVAL;
1590}
1591
1592static int no_mmap(struct file *file, struct socket *sock,
1593 struct vm_area_struct *vma)
1594{
1595 return -EINVAL;
1596}
1597static ssize_t no_sendpage(struct socket *sock, struct page *page,
1598 int offset, size_t size, int flags)
1599{
1600 return -EINVAL;
1601}
1602
1603static int no_skpair(struct socket *s1, struct socket *s2)
1604{
1605 return -EOPNOTSUPP;
1606}
1607
1608/**
1609 * Protocol switches for the various types of TIPC sockets
1610 */
1611
1612static struct proto_ops msg_ops = {
1613 .owner = THIS_MODULE,
1614 .family = AF_TIPC,
1615 .release = release,
1616 .bind = bind,
1617 .connect = connect,
1618 .socketpair = no_skpair,
1619 .accept = accept,
1620 .getname = get_name,
1621 .poll = poll,
1622 .ioctl = ioctl,
1623 .listen = listen,
1624 .shutdown = shutdown,
1625 .setsockopt = setsockopt,
1626 .getsockopt = getsockopt,
1627 .sendmsg = send_msg,
1628 .recvmsg = recv_msg,
1629 .mmap = no_mmap,
1630 .sendpage = no_sendpage
1631};
1632
1633static struct proto_ops packet_ops = {
1634 .owner = THIS_MODULE,
1635 .family = AF_TIPC,
1636 .release = release,
1637 .bind = bind,
1638 .connect = connect,
1639 .socketpair = no_skpair,
1640 .accept = accept,
1641 .getname = get_name,
1642 .poll = poll,
1643 .ioctl = ioctl,
1644 .listen = listen,
1645 .shutdown = shutdown,
1646 .setsockopt = setsockopt,
1647 .getsockopt = getsockopt,
1648 .sendmsg = send_packet,
1649 .recvmsg = recv_msg,
1650 .mmap = no_mmap,
1651 .sendpage = no_sendpage
1652};
1653
1654static struct proto_ops stream_ops = {
1655 .owner = THIS_MODULE,
1656 .family = AF_TIPC,
1657 .release = release,
1658 .bind = bind,
1659 .connect = connect,
1660 .socketpair = no_skpair,
1661 .accept = accept,
1662 .getname = get_name,
1663 .poll = poll,
1664 .ioctl = ioctl,
1665 .listen = listen,
1666 .shutdown = shutdown,
1667 .setsockopt = setsockopt,
1668 .getsockopt = getsockopt,
1669 .sendmsg = send_stream,
1670 .recvmsg = recv_stream,
1671 .mmap = no_mmap,
1672 .sendpage = no_sendpage
1673};
1674
1675static struct net_proto_family tipc_family_ops = {
1676 .owner = THIS_MODULE,
1677 .family = AF_TIPC,
1678 .create = tipc_create
1679};
1680
1681static struct proto tipc_proto = {
1682 .name = "TIPC",
1683 .owner = THIS_MODULE,
1684 .obj_size = sizeof(struct tipc_sock)
1685};
1686
1687/**
1688 * socket_init - initialize TIPC socket interface
1689 *
1690 * Returns 0 on success, errno otherwise
1691 */
1692int socket_init(void)
1693{
1694 int res;
1695
1696 res = proto_register(&tipc_proto, 1);
1697 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001698 err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699 goto out;
1700 }
1701
1702 res = sock_register(&tipc_family_ops);
1703 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001704 err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705 proto_unregister(&tipc_proto);
1706 goto out;
1707 }
1708
1709 sockets_enabled = 1;
1710 out:
1711 return res;
1712}
1713
1714/**
1715 * sock_stop - stop TIPC socket interface
1716 */
1717void socket_stop(void)
1718{
1719 if (!sockets_enabled)
1720 return;
1721
1722 sockets_enabled = 0;
1723 sock_unregister(tipc_family_ops.family);
1724 proto_unregister(&tipc_proto);
1725}
1726