]> asedeno.scripts.mit.edu Git - linux.git/blob - net/tipc/socket.c
e732b1fe7eab9945388cc16fbb9b0df88c1ea36e
[linux.git] / net / tipc / socket.c
1 /*
2  * net/tipc/socket.c: TIPC socket API
3  *
4  * Copyright (c) 2001-2007, 2012-2015, Ericsson AB
5  * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 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.
19  *
20  * 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
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #include <linux/rhashtable.h>
38 #include "core.h"
39 #include "name_table.h"
40 #include "node.h"
41 #include "link.h"
42 #include "name_distr.h"
43 #include "socket.h"
44 #include "bcast.h"
45 #include "netlink.h"
46
47 #define CONN_TIMEOUT_DEFAULT    8000    /* default connect timeout = 8s */
48 #define CONN_PROBING_INTERVAL   msecs_to_jiffies(3600000)  /* [ms] => 1 h */
49 #define TIPC_FWD_MSG            1
50 #define TIPC_MAX_PORT           0xffffffff
51 #define TIPC_MIN_PORT           1
52
53 enum {
54         TIPC_LISTEN = TCP_LISTEN,
55         TIPC_ESTABLISHED = TCP_ESTABLISHED,
56         TIPC_OPEN = TCP_CLOSE,
57         TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
58 };
59
60 /**
61  * struct tipc_sock - TIPC socket structure
62  * @sk: socket - interacts with 'port' and with user via the socket API
63  * @conn_type: TIPC type used when connection was established
64  * @conn_instance: TIPC instance used when connection was established
65  * @published: non-zero if port has one or more associated names
66  * @max_pkt: maximum packet size "hint" used when building messages sent by port
67  * @portid: unique port identity in TIPC socket hash table
68  * @phdr: preformatted message header used when sending messages
69  * @publications: list of publications for port
70  * @pub_count: total # of publications port has made during its lifetime
71  * @probing_state:
72  * @conn_timeout: the time we can wait for an unresponded setup request
73  * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
74  * @link_cong: non-zero if owner must sleep because of link congestion
75  * @sent_unacked: # messages sent by socket, and not yet acked by peer
76  * @rcv_unacked: # messages read by user, but not yet acked back to peer
77  * @peer: 'connected' peer for dgram/rdm
78  * @node: hash table node
79  * @rcu: rcu struct for tipc_sock
80  */
81 struct tipc_sock {
82         struct sock sk;
83         u32 conn_type;
84         u32 conn_instance;
85         int published;
86         u32 max_pkt;
87         u32 portid;
88         struct tipc_msg phdr;
89         struct list_head sock_list;
90         struct list_head publications;
91         u32 pub_count;
92         uint conn_timeout;
93         atomic_t dupl_rcvcnt;
94         bool probe_unacked;
95         bool link_cong;
96         u16 snt_unacked;
97         u16 snd_win;
98         u16 peer_caps;
99         u16 rcv_unacked;
100         u16 rcv_win;
101         struct sockaddr_tipc peer;
102         struct rhash_head node;
103         struct rcu_head rcu;
104 };
105
106 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
107 static void tipc_data_ready(struct sock *sk);
108 static void tipc_write_space(struct sock *sk);
109 static void tipc_sock_destruct(struct sock *sk);
110 static int tipc_release(struct socket *sock);
111 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
112 static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
113 static void tipc_sk_timeout(unsigned long data);
114 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
115                            struct tipc_name_seq const *seq);
116 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
117                             struct tipc_name_seq const *seq);
118 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
119 static int tipc_sk_insert(struct tipc_sock *tsk);
120 static void tipc_sk_remove(struct tipc_sock *tsk);
121 static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
122                               size_t dsz);
123 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
124
125 static const struct proto_ops packet_ops;
126 static const struct proto_ops stream_ops;
127 static const struct proto_ops msg_ops;
128 static struct proto tipc_proto;
129
130 static const struct rhashtable_params tsk_rht_params;
131
132 /*
133  * Revised TIPC socket locking policy:
134  *
135  * Most socket operations take the standard socket lock when they start
136  * and hold it until they finish (or until they need to sleep).  Acquiring
137  * this lock grants the owner exclusive access to the fields of the socket
138  * data structures, with the exception of the backlog queue.  A few socket
139  * operations can be done without taking the socket lock because they only
140  * read socket information that never changes during the life of the socket.
141  *
142  * Socket operations may acquire the lock for the associated TIPC port if they
143  * need to perform an operation on the port.  If any routine needs to acquire
144  * both the socket lock and the port lock it must take the socket lock first
145  * to avoid the risk of deadlock.
146  *
147  * The dispatcher handling incoming messages cannot grab the socket lock in
148  * the standard fashion, since invoked it runs at the BH level and cannot block.
149  * Instead, it checks to see if the socket lock is currently owned by someone,
150  * and either handles the message itself or adds it to the socket's backlog
151  * queue; in the latter case the queued message is processed once the process
152  * owning the socket lock releases it.
153  *
154  * NOTE: Releasing the socket lock while an operation is sleeping overcomes
155  * the problem of a blocked socket operation preventing any other operations
156  * from occurring.  However, applications must be careful if they have
157  * multiple threads trying to send (or receive) on the same socket, as these
158  * operations might interfere with each other.  For example, doing a connect
159  * and a receive at the same time might allow the receive to consume the
160  * ACK message meant for the connect.  While additional work could be done
161  * to try and overcome this, it doesn't seem to be worthwhile at the present.
162  *
163  * NOTE: Releasing the socket lock while an operation is sleeping also ensures
164  * that another operation that must be performed in a non-blocking manner is
165  * not delayed for very long because the lock has already been taken.
166  *
167  * NOTE: This code assumes that certain fields of a port/socket pair are
168  * constant over its lifetime; such fields can be examined without taking
169  * the socket lock and/or port lock, and do not need to be re-read even
170  * after resuming processing after waiting.  These fields include:
171  *   - socket type
172  *   - pointer to socket sk structure (aka tipc_sock structure)
173  *   - pointer to port structure
174  *   - port reference
175  */
176
177 static u32 tsk_own_node(struct tipc_sock *tsk)
178 {
179         return msg_prevnode(&tsk->phdr);
180 }
181
182 static u32 tsk_peer_node(struct tipc_sock *tsk)
183 {
184         return msg_destnode(&tsk->phdr);
185 }
186
187 static u32 tsk_peer_port(struct tipc_sock *tsk)
188 {
189         return msg_destport(&tsk->phdr);
190 }
191
192 static  bool tsk_unreliable(struct tipc_sock *tsk)
193 {
194         return msg_src_droppable(&tsk->phdr) != 0;
195 }
196
197 static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
198 {
199         msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
200 }
201
202 static bool tsk_unreturnable(struct tipc_sock *tsk)
203 {
204         return msg_dest_droppable(&tsk->phdr) != 0;
205 }
206
207 static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
208 {
209         msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
210 }
211
212 static int tsk_importance(struct tipc_sock *tsk)
213 {
214         return msg_importance(&tsk->phdr);
215 }
216
217 static int tsk_set_importance(struct tipc_sock *tsk, int imp)
218 {
219         if (imp > TIPC_CRITICAL_IMPORTANCE)
220                 return -EINVAL;
221         msg_set_importance(&tsk->phdr, (u32)imp);
222         return 0;
223 }
224
225 static struct tipc_sock *tipc_sk(const struct sock *sk)
226 {
227         return container_of(sk, struct tipc_sock, sk);
228 }
229
230 static bool tsk_conn_cong(struct tipc_sock *tsk)
231 {
232         return tsk->snt_unacked >= tsk->snd_win;
233 }
234
235 /* tsk_blocks(): translate a buffer size in bytes to number of
236  * advertisable blocks, taking into account the ratio truesize(len)/len
237  * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
238  */
239 static u16 tsk_adv_blocks(int len)
240 {
241         return len / FLOWCTL_BLK_SZ / 4;
242 }
243
244 /* tsk_inc(): increment counter for sent or received data
245  * - If block based flow control is not supported by peer we
246  *   fall back to message based ditto, incrementing the counter
247  */
248 static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
249 {
250         if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
251                 return ((msglen / FLOWCTL_BLK_SZ) + 1);
252         return 1;
253 }
254
255 /**
256  * tsk_advance_rx_queue - discard first buffer in socket receive queue
257  *
258  * Caller must hold socket lock
259  */
260 static void tsk_advance_rx_queue(struct sock *sk)
261 {
262         kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
263 }
264
265 /* tipc_sk_respond() : send response message back to sender
266  */
267 static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
268 {
269         u32 selector;
270         u32 dnode;
271         u32 onode = tipc_own_addr(sock_net(sk));
272
273         if (!tipc_msg_reverse(onode, &skb, err))
274                 return;
275
276         dnode = msg_destnode(buf_msg(skb));
277         selector = msg_origport(buf_msg(skb));
278         tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
279 }
280
281 /**
282  * tsk_rej_rx_queue - reject all buffers in socket receive queue
283  *
284  * Caller must hold socket lock
285  */
286 static void tsk_rej_rx_queue(struct sock *sk)
287 {
288         struct sk_buff *skb;
289
290         while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
291                 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
292 }
293
294 static bool tipc_sk_connected(struct sock *sk)
295 {
296         return sk->sk_socket->state == SS_CONNECTED;
297 }
298
299 /* tipc_sk_type_connectionless - check if the socket is datagram socket
300  * @sk: socket
301  *
302  * Returns true if connection less, false otherwise
303  */
304 static bool tipc_sk_type_connectionless(struct sock *sk)
305 {
306         return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
307 }
308
309 /* tsk_peer_msg - verify if message was sent by connected port's peer
310  *
311  * Handles cases where the node's network address has changed from
312  * the default of <0.0.0> to its configured setting.
313  */
314 static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
315 {
316         struct sock *sk = &tsk->sk;
317         struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
318         u32 peer_port = tsk_peer_port(tsk);
319         u32 orig_node;
320         u32 peer_node;
321
322         if (unlikely(!tipc_sk_connected(sk)))
323                 return false;
324
325         if (unlikely(msg_origport(msg) != peer_port))
326                 return false;
327
328         orig_node = msg_orignode(msg);
329         peer_node = tsk_peer_node(tsk);
330
331         if (likely(orig_node == peer_node))
332                 return true;
333
334         if (!orig_node && (peer_node == tn->own_addr))
335                 return true;
336
337         if (!peer_node && (orig_node == tn->own_addr))
338                 return true;
339
340         return false;
341 }
342
343 /* tipc_set_sk_state - set the sk_state of the socket
344  * @sk: socket
345  *
346  * Caller must hold socket lock
347  *
348  * Returns 0 on success, errno otherwise
349  */
350 static int tipc_set_sk_state(struct sock *sk, int state)
351 {
352         int oldstate = sk->sk_socket->state;
353         int oldsk_state = sk->sk_state;
354         int res = -EINVAL;
355
356         switch (state) {
357         case TIPC_OPEN:
358                 res = 0;
359                 break;
360         case TIPC_LISTEN:
361                 if (oldsk_state == TIPC_OPEN)
362                         res = 0;
363                 break;
364         case TIPC_ESTABLISHED:
365                 if (oldstate == SS_CONNECTING ||
366                     oldsk_state == TIPC_OPEN)
367                         res = 0;
368                 break;
369         case TIPC_DISCONNECTING:
370                 if (oldstate == SS_CONNECTING ||
371                     oldsk_state == TIPC_ESTABLISHED)
372                         res = 0;
373                 break;
374         }
375
376         if (!res)
377                 sk->sk_state = state;
378
379         return res;
380 }
381
382 /**
383  * tipc_sk_create - create a TIPC socket
384  * @net: network namespace (must be default network)
385  * @sock: pre-allocated socket structure
386  * @protocol: protocol indicator (must be 0)
387  * @kern: caused by kernel or by userspace?
388  *
389  * This routine creates additional data structures used by the TIPC socket,
390  * initializes them, and links them together.
391  *
392  * Returns 0 on success, errno otherwise
393  */
394 static int tipc_sk_create(struct net *net, struct socket *sock,
395                           int protocol, int kern)
396 {
397         struct tipc_net *tn;
398         const struct proto_ops *ops;
399         struct sock *sk;
400         struct tipc_sock *tsk;
401         struct tipc_msg *msg;
402
403         /* Validate arguments */
404         if (unlikely(protocol != 0))
405                 return -EPROTONOSUPPORT;
406
407         switch (sock->type) {
408         case SOCK_STREAM:
409                 ops = &stream_ops;
410                 break;
411         case SOCK_SEQPACKET:
412                 ops = &packet_ops;
413                 break;
414         case SOCK_DGRAM:
415         case SOCK_RDM:
416                 ops = &msg_ops;
417                 break;
418         default:
419                 return -EPROTOTYPE;
420         }
421
422         /* Allocate socket's protocol area */
423         sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
424         if (sk == NULL)
425                 return -ENOMEM;
426
427         tsk = tipc_sk(sk);
428         tsk->max_pkt = MAX_PKT_DEFAULT;
429         INIT_LIST_HEAD(&tsk->publications);
430         msg = &tsk->phdr;
431         tn = net_generic(sock_net(sk), tipc_net_id);
432         tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
433                       NAMED_H_SIZE, 0);
434
435         /* Finish initializing socket data structures */
436         sock->ops = ops;
437         sock_init_data(sock, sk);
438         tipc_set_sk_state(sk, TIPC_OPEN);
439         if (tipc_sk_insert(tsk)) {
440                 pr_warn("Socket create failed; port number exhausted\n");
441                 return -EINVAL;
442         }
443         msg_set_origport(msg, tsk->portid);
444         setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
445         sk->sk_shutdown = 0;
446         sk->sk_backlog_rcv = tipc_backlog_rcv;
447         sk->sk_rcvbuf = sysctl_tipc_rmem[1];
448         sk->sk_data_ready = tipc_data_ready;
449         sk->sk_write_space = tipc_write_space;
450         sk->sk_destruct = tipc_sock_destruct;
451         tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
452         atomic_set(&tsk->dupl_rcvcnt, 0);
453
454         /* Start out with safe limits until we receive an advertised window */
455         tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
456         tsk->rcv_win = tsk->snd_win;
457
458         if (tipc_sk_type_connectionless(sk)) {
459                 tsk_set_unreturnable(tsk, true);
460                 if (sock->type == SOCK_DGRAM)
461                         tsk_set_unreliable(tsk, true);
462         }
463
464         return 0;
465 }
466
467 static void tipc_sk_callback(struct rcu_head *head)
468 {
469         struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
470
471         sock_put(&tsk->sk);
472 }
473
474 /* Caller should hold socket lock for the socket. */
475 static void __tipc_shutdown(struct socket *sock, int error)
476 {
477         struct sock *sk = sock->sk;
478         struct tipc_sock *tsk = tipc_sk(sk);
479         struct net *net = sock_net(sk);
480         u32 dnode = tsk_peer_node(tsk);
481         struct sk_buff *skb;
482
483         /* Reject all unreceived messages, except on an active connection
484          * (which disconnects locally & sends a 'FIN+' to peer).
485          */
486         while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
487                 if (TIPC_SKB_CB(skb)->bytes_read) {
488                         kfree_skb(skb);
489                 } else {
490                         if (!tipc_sk_type_connectionless(sk) &&
491                             sk->sk_state != TIPC_DISCONNECTING) {
492                                 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
493                                 tipc_node_remove_conn(net, dnode, tsk->portid);
494                         }
495                         tipc_sk_respond(sk, skb, error);
496                 }
497         }
498         if (sk->sk_state != TIPC_DISCONNECTING) {
499                 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
500                                       TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
501                                       tsk_own_node(tsk), tsk_peer_port(tsk),
502                                       tsk->portid, error);
503                 if (skb)
504                         tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
505                 if (!tipc_sk_type_connectionless(sk)) {
506                         tipc_node_remove_conn(net, dnode, tsk->portid);
507                         tipc_set_sk_state(sk, TIPC_DISCONNECTING);
508                 }
509         }
510 }
511
512 /**
513  * tipc_release - destroy a TIPC socket
514  * @sock: socket to destroy
515  *
516  * This routine cleans up any messages that are still queued on the socket.
517  * For DGRAM and RDM socket types, all queued messages are rejected.
518  * For SEQPACKET and STREAM socket types, the first message is rejected
519  * and any others are discarded.  (If the first message on a STREAM socket
520  * is partially-read, it is discarded and the next one is rejected instead.)
521  *
522  * NOTE: Rejected messages are not necessarily returned to the sender!  They
523  * are returned or discarded according to the "destination droppable" setting
524  * specified for the message by the sender.
525  *
526  * Returns 0 on success, errno otherwise
527  */
528 static int tipc_release(struct socket *sock)
529 {
530         struct sock *sk = sock->sk;
531         struct tipc_sock *tsk;
532
533         /*
534          * Exit if socket isn't fully initialized (occurs when a failed accept()
535          * releases a pre-allocated child socket that was never used)
536          */
537         if (sk == NULL)
538                 return 0;
539
540         tsk = tipc_sk(sk);
541         lock_sock(sk);
542
543         __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
544         sk->sk_shutdown = SHUTDOWN_MASK;
545         tipc_sk_withdraw(tsk, 0, NULL);
546         sk_stop_timer(sk, &sk->sk_timer);
547         tipc_sk_remove(tsk);
548
549         /* Reject any messages that accumulated in backlog queue */
550         release_sock(sk);
551
552         call_rcu(&tsk->rcu, tipc_sk_callback);
553         sock->sk = NULL;
554
555         return 0;
556 }
557
558 /**
559  * tipc_bind - associate or disassocate TIPC name(s) with a socket
560  * @sock: socket structure
561  * @uaddr: socket address describing name(s) and desired operation
562  * @uaddr_len: size of socket address data structure
563  *
564  * Name and name sequence binding is indicated using a positive scope value;
565  * a negative scope value unbinds the specified name.  Specifying no name
566  * (i.e. a socket address length of 0) unbinds all names from the socket.
567  *
568  * Returns 0 on success, errno otherwise
569  *
570  * NOTE: This routine doesn't need to take the socket lock since it doesn't
571  *       access any non-constant socket information.
572  */
573 static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
574                      int uaddr_len)
575 {
576         struct sock *sk = sock->sk;
577         struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
578         struct tipc_sock *tsk = tipc_sk(sk);
579         int res = -EINVAL;
580
581         lock_sock(sk);
582         if (unlikely(!uaddr_len)) {
583                 res = tipc_sk_withdraw(tsk, 0, NULL);
584                 goto exit;
585         }
586
587         if (uaddr_len < sizeof(struct sockaddr_tipc)) {
588                 res = -EINVAL;
589                 goto exit;
590         }
591         if (addr->family != AF_TIPC) {
592                 res = -EAFNOSUPPORT;
593                 goto exit;
594         }
595
596         if (addr->addrtype == TIPC_ADDR_NAME)
597                 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
598         else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
599                 res = -EAFNOSUPPORT;
600                 goto exit;
601         }
602
603         if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
604             (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
605             (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
606                 res = -EACCES;
607                 goto exit;
608         }
609
610         res = (addr->scope > 0) ?
611                 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
612                 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
613 exit:
614         release_sock(sk);
615         return res;
616 }
617
618 /**
619  * tipc_getname - get port ID of socket or peer socket
620  * @sock: socket structure
621  * @uaddr: area for returned socket address
622  * @uaddr_len: area for returned length of socket address
623  * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
624  *
625  * Returns 0 on success, errno otherwise
626  *
627  * NOTE: This routine doesn't need to take the socket lock since it only
628  *       accesses socket information that is unchanging (or which changes in
629  *       a completely predictable manner).
630  */
631 static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
632                         int *uaddr_len, int peer)
633 {
634         struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
635         struct sock *sk = sock->sk;
636         struct tipc_sock *tsk = tipc_sk(sk);
637         struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
638
639         memset(addr, 0, sizeof(*addr));
640         if (peer) {
641                 if ((sock->state != SS_CONNECTED) &&
642                     ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
643                         return -ENOTCONN;
644                 addr->addr.id.ref = tsk_peer_port(tsk);
645                 addr->addr.id.node = tsk_peer_node(tsk);
646         } else {
647                 addr->addr.id.ref = tsk->portid;
648                 addr->addr.id.node = tn->own_addr;
649         }
650
651         *uaddr_len = sizeof(*addr);
652         addr->addrtype = TIPC_ADDR_ID;
653         addr->family = AF_TIPC;
654         addr->scope = 0;
655         addr->addr.name.domain = 0;
656
657         return 0;
658 }
659
660 /**
661  * tipc_poll - read and possibly block on pollmask
662  * @file: file structure associated with the socket
663  * @sock: socket for which to calculate the poll bits
664  * @wait: ???
665  *
666  * Returns pollmask value
667  *
668  * COMMENTARY:
669  * It appears that the usual socket locking mechanisms are not useful here
670  * since the pollmask info is potentially out-of-date the moment this routine
671  * exits.  TCP and other protocols seem to rely on higher level poll routines
672  * to handle any preventable race conditions, so TIPC will do the same ...
673  *
674  * IMPORTANT: The fact that a read or write operation is indicated does NOT
675  * imply that the operation will succeed, merely that it should be performed
676  * and will not block.
677  */
678 static unsigned int tipc_poll(struct file *file, struct socket *sock,
679                               poll_table *wait)
680 {
681         struct sock *sk = sock->sk;
682         struct tipc_sock *tsk = tipc_sk(sk);
683         u32 mask = 0;
684
685         sock_poll_wait(file, sk_sleep(sk), wait);
686
687         if (sk->sk_shutdown & RCV_SHUTDOWN)
688                 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
689         if (sk->sk_shutdown == SHUTDOWN_MASK)
690                 mask |= POLLHUP;
691
692         switch ((int)sock->state) {
693         case SS_CONNECTED:
694                 if (!tsk->link_cong && !tsk_conn_cong(tsk))
695                         mask |= POLLOUT;
696                 /* fall thru' */
697         case SS_CONNECTING:
698                 if (!skb_queue_empty(&sk->sk_receive_queue))
699                         mask |= (POLLIN | POLLRDNORM);
700                 break;
701         default:
702                 switch (sk->sk_state) {
703                 case TIPC_OPEN:
704                         if (!tsk->link_cong)
705                                 mask |= POLLOUT;
706                         if (tipc_sk_type_connectionless(sk) &&
707                             (!skb_queue_empty(&sk->sk_receive_queue)))
708                                 mask |= (POLLIN | POLLRDNORM);
709                         break;
710                 case TIPC_DISCONNECTING:
711                         mask = (POLLIN | POLLRDNORM | POLLHUP);
712                         break;
713                 case TIPC_LISTEN:
714                         if (!skb_queue_empty(&sk->sk_receive_queue))
715                                 mask |= (POLLIN | POLLRDNORM);
716                         break;
717                 }
718         }
719
720         return mask;
721 }
722
723 /**
724  * tipc_sendmcast - send multicast message
725  * @sock: socket structure
726  * @seq: destination address
727  * @msg: message to send
728  * @dsz: total length of message data
729  * @timeo: timeout to wait for wakeup
730  *
731  * Called from function tipc_sendmsg(), which has done all sanity checks
732  * Returns the number of bytes sent on success, or errno
733  */
734 static int tipc_sendmcast(struct  socket *sock, struct tipc_name_seq *seq,
735                           struct msghdr *msg, size_t dsz, long timeo)
736 {
737         struct sock *sk = sock->sk;
738         struct tipc_sock *tsk = tipc_sk(sk);
739         struct net *net = sock_net(sk);
740         struct tipc_msg *mhdr = &tsk->phdr;
741         struct sk_buff_head pktchain;
742         struct iov_iter save = msg->msg_iter;
743         uint mtu;
744         int rc;
745
746         if (!timeo && tsk->link_cong)
747                 return -ELINKCONG;
748
749         msg_set_type(mhdr, TIPC_MCAST_MSG);
750         msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
751         msg_set_destport(mhdr, 0);
752         msg_set_destnode(mhdr, 0);
753         msg_set_nametype(mhdr, seq->type);
754         msg_set_namelower(mhdr, seq->lower);
755         msg_set_nameupper(mhdr, seq->upper);
756         msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
757
758         skb_queue_head_init(&pktchain);
759
760 new_mtu:
761         mtu = tipc_bcast_get_mtu(net);
762         rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &pktchain);
763         if (unlikely(rc < 0))
764                 return rc;
765
766         do {
767                 rc = tipc_bcast_xmit(net, &pktchain);
768                 if (likely(!rc))
769                         return dsz;
770
771                 if (rc == -ELINKCONG) {
772                         tsk->link_cong = 1;
773                         rc = tipc_wait_for_sndmsg(sock, &timeo);
774                         if (!rc)
775                                 continue;
776                 }
777                 __skb_queue_purge(&pktchain);
778                 if (rc == -EMSGSIZE) {
779                         msg->msg_iter = save;
780                         goto new_mtu;
781                 }
782                 break;
783         } while (1);
784         return rc;
785 }
786
787 /**
788  * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
789  * @arrvq: queue with arriving messages, to be cloned after destination lookup
790  * @inputq: queue with cloned messages, delivered to socket after dest lookup
791  *
792  * Multi-threaded: parallel calls with reference to same queues may occur
793  */
794 void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
795                        struct sk_buff_head *inputq)
796 {
797         struct tipc_msg *msg;
798         struct tipc_plist dports;
799         u32 portid;
800         u32 scope = TIPC_CLUSTER_SCOPE;
801         struct sk_buff_head tmpq;
802         uint hsz;
803         struct sk_buff *skb, *_skb;
804
805         __skb_queue_head_init(&tmpq);
806         tipc_plist_init(&dports);
807
808         skb = tipc_skb_peek(arrvq, &inputq->lock);
809         for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
810                 msg = buf_msg(skb);
811                 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
812
813                 if (in_own_node(net, msg_orignode(msg)))
814                         scope = TIPC_NODE_SCOPE;
815
816                 /* Create destination port list and message clones: */
817                 tipc_nametbl_mc_translate(net,
818                                           msg_nametype(msg), msg_namelower(msg),
819                                           msg_nameupper(msg), scope, &dports);
820                 portid = tipc_plist_pop(&dports);
821                 for (; portid; portid = tipc_plist_pop(&dports)) {
822                         _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
823                         if (_skb) {
824                                 msg_set_destport(buf_msg(_skb), portid);
825                                 __skb_queue_tail(&tmpq, _skb);
826                                 continue;
827                         }
828                         pr_warn("Failed to clone mcast rcv buffer\n");
829                 }
830                 /* Append to inputq if not already done by other thread */
831                 spin_lock_bh(&inputq->lock);
832                 if (skb_peek(arrvq) == skb) {
833                         skb_queue_splice_tail_init(&tmpq, inputq);
834                         kfree_skb(__skb_dequeue(arrvq));
835                 }
836                 spin_unlock_bh(&inputq->lock);
837                 __skb_queue_purge(&tmpq);
838                 kfree_skb(skb);
839         }
840         tipc_sk_rcv(net, inputq);
841 }
842
843 /**
844  * tipc_sk_proto_rcv - receive a connection mng protocol message
845  * @tsk: receiving socket
846  * @skb: pointer to message buffer.
847  */
848 static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
849                               struct sk_buff_head *xmitq)
850 {
851         struct sock *sk = &tsk->sk;
852         u32 onode = tsk_own_node(tsk);
853         struct tipc_msg *hdr = buf_msg(skb);
854         int mtyp = msg_type(hdr);
855         bool conn_cong;
856
857         /* Ignore if connection cannot be validated: */
858         if (!tsk_peer_msg(tsk, hdr))
859                 goto exit;
860
861         tsk->probe_unacked = false;
862
863         if (mtyp == CONN_PROBE) {
864                 msg_set_type(hdr, CONN_PROBE_REPLY);
865                 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
866                         __skb_queue_tail(xmitq, skb);
867                 return;
868         } else if (mtyp == CONN_ACK) {
869                 conn_cong = tsk_conn_cong(tsk);
870                 tsk->snt_unacked -= msg_conn_ack(hdr);
871                 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
872                         tsk->snd_win = msg_adv_win(hdr);
873                 if (conn_cong)
874                         sk->sk_write_space(sk);
875         } else if (mtyp != CONN_PROBE_REPLY) {
876                 pr_warn("Received unknown CONN_PROTO msg\n");
877         }
878 exit:
879         kfree_skb(skb);
880 }
881
882 static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
883 {
884         struct sock *sk = sock->sk;
885         struct tipc_sock *tsk = tipc_sk(sk);
886         DEFINE_WAIT(wait);
887         int done;
888
889         do {
890                 int err = sock_error(sk);
891                 if (err)
892                         return err;
893                 if (sk->sk_shutdown & SEND_SHUTDOWN)
894                         return -EPIPE;
895                 if (!*timeo_p)
896                         return -EAGAIN;
897                 if (signal_pending(current))
898                         return sock_intr_errno(*timeo_p);
899
900                 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
901                 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
902                 finish_wait(sk_sleep(sk), &wait);
903         } while (!done);
904         return 0;
905 }
906
907 /**
908  * tipc_sendmsg - send message in connectionless manner
909  * @sock: socket structure
910  * @m: message to send
911  * @dsz: amount of user data to be sent
912  *
913  * Message must have an destination specified explicitly.
914  * Used for SOCK_RDM and SOCK_DGRAM messages,
915  * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
916  * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
917  *
918  * Returns the number of bytes sent on success, or errno otherwise
919  */
920 static int tipc_sendmsg(struct socket *sock,
921                         struct msghdr *m, size_t dsz)
922 {
923         struct sock *sk = sock->sk;
924         int ret;
925
926         lock_sock(sk);
927         ret = __tipc_sendmsg(sock, m, dsz);
928         release_sock(sk);
929
930         return ret;
931 }
932
933 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
934 {
935         DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
936         struct sock *sk = sock->sk;
937         struct tipc_sock *tsk = tipc_sk(sk);
938         struct net *net = sock_net(sk);
939         struct tipc_msg *mhdr = &tsk->phdr;
940         u32 dnode, dport;
941         struct sk_buff_head pktchain;
942         bool is_connectionless = tipc_sk_type_connectionless(sk);
943         struct sk_buff *skb;
944         struct tipc_name_seq *seq;
945         struct iov_iter save;
946         u32 mtu;
947         long timeo;
948         int rc;
949
950         if (dsz > TIPC_MAX_USER_MSG_SIZE)
951                 return -EMSGSIZE;
952         if (unlikely(!dest)) {
953                 if (is_connectionless && tsk->peer.family == AF_TIPC)
954                         dest = &tsk->peer;
955                 else
956                         return -EDESTADDRREQ;
957         } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
958                    dest->family != AF_TIPC) {
959                 return -EINVAL;
960         }
961         if (!is_connectionless) {
962                 if (sk->sk_state == TIPC_LISTEN)
963                         return -EPIPE;
964                 if (sk->sk_state != TIPC_OPEN)
965                         return -EISCONN;
966                 if (tsk->published)
967                         return -EOPNOTSUPP;
968                 if (dest->addrtype == TIPC_ADDR_NAME) {
969                         tsk->conn_type = dest->addr.name.name.type;
970                         tsk->conn_instance = dest->addr.name.name.instance;
971                 }
972         }
973         seq = &dest->addr.nameseq;
974         timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
975
976         if (dest->addrtype == TIPC_ADDR_MCAST) {
977                 return tipc_sendmcast(sock, seq, m, dsz, timeo);
978         } else if (dest->addrtype == TIPC_ADDR_NAME) {
979                 u32 type = dest->addr.name.name.type;
980                 u32 inst = dest->addr.name.name.instance;
981                 u32 domain = dest->addr.name.domain;
982
983                 dnode = domain;
984                 msg_set_type(mhdr, TIPC_NAMED_MSG);
985                 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
986                 msg_set_nametype(mhdr, type);
987                 msg_set_nameinst(mhdr, inst);
988                 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
989                 dport = tipc_nametbl_translate(net, type, inst, &dnode);
990                 msg_set_destnode(mhdr, dnode);
991                 msg_set_destport(mhdr, dport);
992                 if (unlikely(!dport && !dnode))
993                         return -EHOSTUNREACH;
994         } else if (dest->addrtype == TIPC_ADDR_ID) {
995                 dnode = dest->addr.id.node;
996                 msg_set_type(mhdr, TIPC_DIRECT_MSG);
997                 msg_set_lookup_scope(mhdr, 0);
998                 msg_set_destnode(mhdr, dnode);
999                 msg_set_destport(mhdr, dest->addr.id.ref);
1000                 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
1001         }
1002
1003         skb_queue_head_init(&pktchain);
1004         save = m->msg_iter;
1005 new_mtu:
1006         mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
1007         rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &pktchain);
1008         if (rc < 0)
1009                 return rc;
1010
1011         do {
1012                 skb = skb_peek(&pktchain);
1013                 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
1014                 rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid);
1015                 if (likely(!rc)) {
1016                         if (!is_connectionless)
1017                                 sock->state = SS_CONNECTING;
1018                         return dsz;
1019                 }
1020                 if (rc == -ELINKCONG) {
1021                         tsk->link_cong = 1;
1022                         rc = tipc_wait_for_sndmsg(sock, &timeo);
1023                         if (!rc)
1024                                 continue;
1025                 }
1026                 __skb_queue_purge(&pktchain);
1027                 if (rc == -EMSGSIZE) {
1028                         m->msg_iter = save;
1029                         goto new_mtu;
1030                 }
1031                 break;
1032         } while (1);
1033
1034         return rc;
1035 }
1036
1037 static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
1038 {
1039         struct sock *sk = sock->sk;
1040         struct tipc_sock *tsk = tipc_sk(sk);
1041         DEFINE_WAIT(wait);
1042         int done;
1043
1044         do {
1045                 int err = sock_error(sk);
1046                 if (err)
1047                         return err;
1048                 if (sk->sk_state == TIPC_DISCONNECTING)
1049                         return -EPIPE;
1050                 else if (sock->state != SS_CONNECTED)
1051                         return -ENOTCONN;
1052                 if (!*timeo_p)
1053                         return -EAGAIN;
1054                 if (signal_pending(current))
1055                         return sock_intr_errno(*timeo_p);
1056
1057                 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1058                 done = sk_wait_event(sk, timeo_p,
1059                                      (!tsk->link_cong &&
1060                                       !tsk_conn_cong(tsk)) ||
1061                                       !tipc_sk_connected(sk));
1062                 finish_wait(sk_sleep(sk), &wait);
1063         } while (!done);
1064         return 0;
1065 }
1066
1067 /**
1068  * tipc_send_stream - send stream-oriented data
1069  * @sock: socket structure
1070  * @m: data to send
1071  * @dsz: total length of data to be transmitted
1072  *
1073  * Used for SOCK_STREAM data.
1074  *
1075  * Returns the number of bytes sent on success (or partial success),
1076  * or errno if no data sent
1077  */
1078 static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1079 {
1080         struct sock *sk = sock->sk;
1081         int ret;
1082
1083         lock_sock(sk);
1084         ret = __tipc_send_stream(sock, m, dsz);
1085         release_sock(sk);
1086
1087         return ret;
1088 }
1089
1090 static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1091 {
1092         struct sock *sk = sock->sk;
1093         struct net *net = sock_net(sk);
1094         struct tipc_sock *tsk = tipc_sk(sk);
1095         struct tipc_msg *mhdr = &tsk->phdr;
1096         struct sk_buff_head pktchain;
1097         DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1098         u32 portid = tsk->portid;
1099         int rc = -EINVAL;
1100         long timeo;
1101         u32 dnode;
1102         uint mtu, send, sent = 0;
1103         struct iov_iter save;
1104         int hlen = MIN_H_SIZE;
1105
1106         /* Handle implied connection establishment */
1107         if (unlikely(dest)) {
1108                 rc = __tipc_sendmsg(sock, m, dsz);
1109                 hlen = msg_hdr_sz(mhdr);
1110                 if (dsz && (dsz == rc))
1111                         tsk->snt_unacked = tsk_inc(tsk, dsz + hlen);
1112                 return rc;
1113         }
1114         if (dsz > (uint)INT_MAX)
1115                 return -EMSGSIZE;
1116
1117         if (unlikely(sock->state != SS_CONNECTED)) {
1118                 if (sk->sk_state == TIPC_DISCONNECTING)
1119                         return -EPIPE;
1120                 else
1121                         return -ENOTCONN;
1122         }
1123
1124         timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1125         if (!timeo && tsk->link_cong)
1126                 return -ELINKCONG;
1127
1128         dnode = tsk_peer_node(tsk);
1129         skb_queue_head_init(&pktchain);
1130
1131 next:
1132         save = m->msg_iter;
1133         mtu = tsk->max_pkt;
1134         send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
1135         rc = tipc_msg_build(mhdr, m, sent, send, mtu, &pktchain);
1136         if (unlikely(rc < 0))
1137                 return rc;
1138
1139         do {
1140                 if (likely(!tsk_conn_cong(tsk))) {
1141                         rc = tipc_node_xmit(net, &pktchain, dnode, portid);
1142                         if (likely(!rc)) {
1143                                 tsk->snt_unacked += tsk_inc(tsk, send + hlen);
1144                                 sent += send;
1145                                 if (sent == dsz)
1146                                         return dsz;
1147                                 goto next;
1148                         }
1149                         if (rc == -EMSGSIZE) {
1150                                 __skb_queue_purge(&pktchain);
1151                                 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1152                                                                  portid);
1153                                 m->msg_iter = save;
1154                                 goto next;
1155                         }
1156                         if (rc != -ELINKCONG)
1157                                 break;
1158
1159                         tsk->link_cong = 1;
1160                 }
1161                 rc = tipc_wait_for_sndpkt(sock, &timeo);
1162         } while (!rc);
1163
1164         __skb_queue_purge(&pktchain);
1165         return sent ? sent : rc;
1166 }
1167
1168 /**
1169  * tipc_send_packet - send a connection-oriented message
1170  * @sock: socket structure
1171  * @m: message to send
1172  * @dsz: length of data to be transmitted
1173  *
1174  * Used for SOCK_SEQPACKET messages.
1175  *
1176  * Returns the number of bytes sent on success, or errno otherwise
1177  */
1178 static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
1179 {
1180         if (dsz > TIPC_MAX_USER_MSG_SIZE)
1181                 return -EMSGSIZE;
1182
1183         return tipc_send_stream(sock, m, dsz);
1184 }
1185
1186 /* tipc_sk_finish_conn - complete the setup of a connection
1187  */
1188 static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
1189                                 u32 peer_node)
1190 {
1191         struct sock *sk = &tsk->sk;
1192         struct net *net = sock_net(sk);
1193         struct tipc_msg *msg = &tsk->phdr;
1194
1195         msg_set_destnode(msg, peer_node);
1196         msg_set_destport(msg, peer_port);
1197         msg_set_type(msg, TIPC_CONN_MSG);
1198         msg_set_lookup_scope(msg, 0);
1199         msg_set_hdr_sz(msg, SHORT_H_SIZE);
1200
1201         sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
1202         tipc_set_sk_state(sk, TIPC_ESTABLISHED);
1203         tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1204         tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
1205         tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
1206         if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1207                 return;
1208
1209         /* Fall back to message based flow control */
1210         tsk->rcv_win = FLOWCTL_MSG_WIN;
1211         tsk->snd_win = FLOWCTL_MSG_WIN;
1212 }
1213
1214 /**
1215  * set_orig_addr - capture sender's address for received message
1216  * @m: descriptor for message info
1217  * @msg: received message header
1218  *
1219  * Note: Address is not captured if not requested by receiver.
1220  */
1221 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
1222 {
1223         DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
1224
1225         if (addr) {
1226                 addr->family = AF_TIPC;
1227                 addr->addrtype = TIPC_ADDR_ID;
1228                 memset(&addr->addr, 0, sizeof(addr->addr));
1229                 addr->addr.id.ref = msg_origport(msg);
1230                 addr->addr.id.node = msg_orignode(msg);
1231                 addr->addr.name.domain = 0;     /* could leave uninitialized */
1232                 addr->scope = 0;                /* could leave uninitialized */
1233                 m->msg_namelen = sizeof(struct sockaddr_tipc);
1234         }
1235 }
1236
1237 /**
1238  * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
1239  * @m: descriptor for message info
1240  * @msg: received message header
1241  * @tsk: TIPC port associated with message
1242  *
1243  * Note: Ancillary data is not captured if not requested by receiver.
1244  *
1245  * Returns 0 if successful, otherwise errno
1246  */
1247 static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1248                                  struct tipc_sock *tsk)
1249 {
1250         u32 anc_data[3];
1251         u32 err;
1252         u32 dest_type;
1253         int has_name;
1254         int res;
1255
1256         if (likely(m->msg_controllen == 0))
1257                 return 0;
1258
1259         /* Optionally capture errored message object(s) */
1260         err = msg ? msg_errcode(msg) : 0;
1261         if (unlikely(err)) {
1262                 anc_data[0] = err;
1263                 anc_data[1] = msg_data_sz(msg);
1264                 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1265                 if (res)
1266                         return res;
1267                 if (anc_data[1]) {
1268                         res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1269                                        msg_data(msg));
1270                         if (res)
1271                                 return res;
1272                 }
1273         }
1274
1275         /* Optionally capture message destination object */
1276         dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1277         switch (dest_type) {
1278         case TIPC_NAMED_MSG:
1279                 has_name = 1;
1280                 anc_data[0] = msg_nametype(msg);
1281                 anc_data[1] = msg_namelower(msg);
1282                 anc_data[2] = msg_namelower(msg);
1283                 break;
1284         case TIPC_MCAST_MSG:
1285                 has_name = 1;
1286                 anc_data[0] = msg_nametype(msg);
1287                 anc_data[1] = msg_namelower(msg);
1288                 anc_data[2] = msg_nameupper(msg);
1289                 break;
1290         case TIPC_CONN_MSG:
1291                 has_name = (tsk->conn_type != 0);
1292                 anc_data[0] = tsk->conn_type;
1293                 anc_data[1] = tsk->conn_instance;
1294                 anc_data[2] = tsk->conn_instance;
1295                 break;
1296         default:
1297                 has_name = 0;
1298         }
1299         if (has_name) {
1300                 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1301                 if (res)
1302                         return res;
1303         }
1304
1305         return 0;
1306 }
1307
1308 static void tipc_sk_send_ack(struct tipc_sock *tsk)
1309 {
1310         struct sock *sk = &tsk->sk;
1311         struct net *net = sock_net(sk);
1312         struct sk_buff *skb = NULL;
1313         struct tipc_msg *msg;
1314         u32 peer_port = tsk_peer_port(tsk);
1315         u32 dnode = tsk_peer_node(tsk);
1316
1317         if (!tipc_sk_connected(sk))
1318                 return;
1319         skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1320                               dnode, tsk_own_node(tsk), peer_port,
1321                               tsk->portid, TIPC_OK);
1322         if (!skb)
1323                 return;
1324         msg = buf_msg(skb);
1325         msg_set_conn_ack(msg, tsk->rcv_unacked);
1326         tsk->rcv_unacked = 0;
1327
1328         /* Adjust to and advertize the correct window limit */
1329         if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1330                 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1331                 msg_set_adv_win(msg, tsk->rcv_win);
1332         }
1333         tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
1334 }
1335
1336 static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1337 {
1338         struct sock *sk = sock->sk;
1339         DEFINE_WAIT(wait);
1340         long timeo = *timeop;
1341         int err;
1342
1343         for (;;) {
1344                 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1345                 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1346                         if (sk->sk_shutdown & RCV_SHUTDOWN) {
1347                                 err = -ENOTCONN;
1348                                 break;
1349                         }
1350                         release_sock(sk);
1351                         timeo = schedule_timeout(timeo);
1352                         lock_sock(sk);
1353                 }
1354                 err = 0;
1355                 if (!skb_queue_empty(&sk->sk_receive_queue))
1356                         break;
1357                 err = -EAGAIN;
1358                 if (!timeo)
1359                         break;
1360                 err = sock_intr_errno(timeo);
1361                 if (signal_pending(current))
1362                         break;
1363         }
1364         finish_wait(sk_sleep(sk), &wait);
1365         *timeop = timeo;
1366         return err;
1367 }
1368
1369 /**
1370  * tipc_recvmsg - receive packet-oriented message
1371  * @m: descriptor for message info
1372  * @buf_len: total size of user buffer area
1373  * @flags: receive flags
1374  *
1375  * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1376  * If the complete message doesn't fit in user area, truncate it.
1377  *
1378  * Returns size of returned message data, errno otherwise
1379  */
1380 static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1381                         int flags)
1382 {
1383         struct sock *sk = sock->sk;
1384         struct tipc_sock *tsk = tipc_sk(sk);
1385         struct sk_buff *buf;
1386         struct tipc_msg *msg;
1387         bool is_connectionless = tipc_sk_type_connectionless(sk);
1388         long timeo;
1389         unsigned int sz;
1390         u32 err;
1391         int res, hlen;
1392
1393         /* Catch invalid receive requests */
1394         if (unlikely(!buf_len))
1395                 return -EINVAL;
1396
1397         lock_sock(sk);
1398
1399         if (!is_connectionless && unlikely(sk->sk_state == TIPC_OPEN)) {
1400                 res = -ENOTCONN;
1401                 goto exit;
1402         }
1403
1404         timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1405 restart:
1406
1407         /* Look for a message in receive queue; wait if necessary */
1408         res = tipc_wait_for_rcvmsg(sock, &timeo);
1409         if (res)
1410                 goto exit;
1411
1412         /* Look at first message in receive queue */
1413         buf = skb_peek(&sk->sk_receive_queue);
1414         msg = buf_msg(buf);
1415         sz = msg_data_sz(msg);
1416         hlen = msg_hdr_sz(msg);
1417         err = msg_errcode(msg);
1418
1419         /* Discard an empty non-errored message & try again */
1420         if ((!sz) && (!err)) {
1421                 tsk_advance_rx_queue(sk);
1422                 goto restart;
1423         }
1424
1425         /* Capture sender's address (optional) */
1426         set_orig_addr(m, msg);
1427
1428         /* Capture ancillary data (optional) */
1429         res = tipc_sk_anc_data_recv(m, msg, tsk);
1430         if (res)
1431                 goto exit;
1432
1433         /* Capture message data (if valid) & compute return value (always) */
1434         if (!err) {
1435                 if (unlikely(buf_len < sz)) {
1436                         sz = buf_len;
1437                         m->msg_flags |= MSG_TRUNC;
1438                 }
1439                 res = skb_copy_datagram_msg(buf, hlen, m, sz);
1440                 if (res)
1441                         goto exit;
1442                 res = sz;
1443         } else {
1444                 if (is_connectionless || err == TIPC_CONN_SHUTDOWN ||
1445                     m->msg_control)
1446                         res = 0;
1447                 else
1448                         res = -ECONNRESET;
1449         }
1450
1451         if (unlikely(flags & MSG_PEEK))
1452                 goto exit;
1453
1454         if (likely(!is_connectionless)) {
1455                 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1456                 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1457                         tipc_sk_send_ack(tsk);
1458         }
1459         tsk_advance_rx_queue(sk);
1460 exit:
1461         release_sock(sk);
1462         return res;
1463 }
1464
1465 /**
1466  * tipc_recv_stream - receive stream-oriented data
1467  * @m: descriptor for message info
1468  * @buf_len: total size of user buffer area
1469  * @flags: receive flags
1470  *
1471  * Used for SOCK_STREAM messages only.  If not enough data is available
1472  * will optionally wait for more; never truncates data.
1473  *
1474  * Returns size of returned message data, errno otherwise
1475  */
1476 static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1477                             size_t buf_len, int flags)
1478 {
1479         struct sock *sk = sock->sk;
1480         struct tipc_sock *tsk = tipc_sk(sk);
1481         struct sk_buff *buf;
1482         struct tipc_msg *msg;
1483         long timeo;
1484         unsigned int sz;
1485         int target;
1486         int sz_copied = 0;
1487         u32 err;
1488         int res = 0, hlen;
1489
1490         /* Catch invalid receive attempts */
1491         if (unlikely(!buf_len))
1492                 return -EINVAL;
1493
1494         lock_sock(sk);
1495
1496         if (unlikely(sk->sk_state == TIPC_OPEN)) {
1497                 res = -ENOTCONN;
1498                 goto exit;
1499         }
1500
1501         target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
1502         timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1503
1504 restart:
1505         /* Look for a message in receive queue; wait if necessary */
1506         res = tipc_wait_for_rcvmsg(sock, &timeo);
1507         if (res)
1508                 goto exit;
1509
1510         /* Look at first message in receive queue */
1511         buf = skb_peek(&sk->sk_receive_queue);
1512         msg = buf_msg(buf);
1513         sz = msg_data_sz(msg);
1514         hlen = msg_hdr_sz(msg);
1515         err = msg_errcode(msg);
1516
1517         /* Discard an empty non-errored message & try again */
1518         if ((!sz) && (!err)) {
1519                 tsk_advance_rx_queue(sk);
1520                 goto restart;
1521         }
1522
1523         /* Optionally capture sender's address & ancillary data of first msg */
1524         if (sz_copied == 0) {
1525                 set_orig_addr(m, msg);
1526                 res = tipc_sk_anc_data_recv(m, msg, tsk);
1527                 if (res)
1528                         goto exit;
1529         }
1530
1531         /* Capture message data (if valid) & compute return value (always) */
1532         if (!err) {
1533                 u32 offset = TIPC_SKB_CB(buf)->bytes_read;
1534                 u32 needed;
1535                 int sz_to_copy;
1536
1537                 sz -= offset;
1538                 needed = (buf_len - sz_copied);
1539                 sz_to_copy = min(sz, needed);
1540
1541                 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
1542                 if (res)
1543                         goto exit;
1544
1545                 sz_copied += sz_to_copy;
1546
1547                 if (sz_to_copy < sz) {
1548                         if (!(flags & MSG_PEEK))
1549                                 TIPC_SKB_CB(buf)->bytes_read =
1550                                         offset + sz_to_copy;
1551                         goto exit;
1552                 }
1553         } else {
1554                 if (sz_copied != 0)
1555                         goto exit; /* can't add error msg to valid data */
1556
1557                 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1558                         res = 0;
1559                 else
1560                         res = -ECONNRESET;
1561         }
1562
1563         if (unlikely(flags & MSG_PEEK))
1564                 goto exit;
1565
1566         tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1567         if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1568                 tipc_sk_send_ack(tsk);
1569         tsk_advance_rx_queue(sk);
1570
1571         /* Loop around if more data is required */
1572         if ((sz_copied < buf_len) &&    /* didn't get all requested data */
1573             (!skb_queue_empty(&sk->sk_receive_queue) ||
1574             (sz_copied < target)) &&    /* and more is ready or required */
1575             (!err))                     /* and haven't reached a FIN */
1576                 goto restart;
1577
1578 exit:
1579         release_sock(sk);
1580         return sz_copied ? sz_copied : res;
1581 }
1582
1583 /**
1584  * tipc_write_space - wake up thread if port congestion is released
1585  * @sk: socket
1586  */
1587 static void tipc_write_space(struct sock *sk)
1588 {
1589         struct socket_wq *wq;
1590
1591         rcu_read_lock();
1592         wq = rcu_dereference(sk->sk_wq);
1593         if (skwq_has_sleeper(wq))
1594                 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1595                                                 POLLWRNORM | POLLWRBAND);
1596         rcu_read_unlock();
1597 }
1598
1599 /**
1600  * tipc_data_ready - wake up threads to indicate messages have been received
1601  * @sk: socket
1602  * @len: the length of messages
1603  */
1604 static void tipc_data_ready(struct sock *sk)
1605 {
1606         struct socket_wq *wq;
1607
1608         rcu_read_lock();
1609         wq = rcu_dereference(sk->sk_wq);
1610         if (skwq_has_sleeper(wq))
1611                 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1612                                                 POLLRDNORM | POLLRDBAND);
1613         rcu_read_unlock();
1614 }
1615
1616 static void tipc_sock_destruct(struct sock *sk)
1617 {
1618         __skb_queue_purge(&sk->sk_receive_queue);
1619 }
1620
1621 /**
1622  * filter_connect - Handle all incoming messages for a connection-based socket
1623  * @tsk: TIPC socket
1624  * @skb: pointer to message buffer. Set to NULL if buffer is consumed
1625  *
1626  * Returns true if everything ok, false otherwise
1627  */
1628 static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
1629 {
1630         struct sock *sk = &tsk->sk;
1631         struct net *net = sock_net(sk);
1632         struct socket *sock = sk->sk_socket;
1633         struct tipc_msg *hdr = buf_msg(skb);
1634
1635         if (unlikely(msg_mcast(hdr)))
1636                 return false;
1637
1638         switch ((int)sock->state) {
1639         case SS_CONNECTED:
1640
1641                 /* Accept only connection-based messages sent by peer */
1642                 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1643                         return false;
1644
1645                 if (unlikely(msg_errcode(hdr))) {
1646                         tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1647                         /* Let timer expire on it's own */
1648                         tipc_node_remove_conn(net, tsk_peer_node(tsk),
1649                                               tsk->portid);
1650                         sk->sk_state_change(sk);
1651                 }
1652                 return true;
1653
1654         case SS_CONNECTING:
1655
1656                 /* Accept only ACK or NACK message */
1657                 if (unlikely(!msg_connected(hdr)))
1658                         return false;
1659
1660                 if (unlikely(msg_errcode(hdr))) {
1661                         tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1662                         sk->sk_err = ECONNREFUSED;
1663                         return true;
1664                 }
1665
1666                 if (unlikely(!msg_isdata(hdr))) {
1667                         tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1668                         sk->sk_err = EINVAL;
1669                         return true;
1670                 }
1671
1672                 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1673                 msg_set_importance(&tsk->phdr, msg_importance(hdr));
1674                 sock->state = SS_CONNECTED;
1675
1676                 /* If 'ACK+' message, add to socket receive queue */
1677                 if (msg_data_sz(hdr))
1678                         return true;
1679
1680                 /* If empty 'ACK-' message, wake up sleeping connect() */
1681                 if (waitqueue_active(sk_sleep(sk)))
1682                         wake_up_interruptible(sk_sleep(sk));
1683
1684                 /* 'ACK-' message is neither accepted nor rejected: */
1685                 msg_set_dest_droppable(hdr, 1);
1686                 return false;
1687         }
1688
1689         switch (sk->sk_state) {
1690         case TIPC_OPEN:
1691         case TIPC_DISCONNECTING:
1692                 break;
1693         case TIPC_LISTEN:
1694                 /* Accept only SYN message */
1695                 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1696                         return true;
1697                 break;
1698         default:
1699                 pr_err("Unknown sk_state %u\n", sk->sk_state);
1700         }
1701
1702         return false;
1703 }
1704
1705 /**
1706  * rcvbuf_limit - get proper overload limit of socket receive queue
1707  * @sk: socket
1708  * @skb: message
1709  *
1710  * For connection oriented messages, irrespective of importance,
1711  * default queue limit is 2 MB.
1712  *
1713  * For connectionless messages, queue limits are based on message
1714  * importance as follows:
1715  *
1716  * TIPC_LOW_IMPORTANCE       (2 MB)
1717  * TIPC_MEDIUM_IMPORTANCE    (4 MB)
1718  * TIPC_HIGH_IMPORTANCE      (8 MB)
1719  * TIPC_CRITICAL_IMPORTANCE  (16 MB)
1720  *
1721  * Returns overload limit according to corresponding message importance
1722  */
1723 static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
1724 {
1725         struct tipc_sock *tsk = tipc_sk(sk);
1726         struct tipc_msg *hdr = buf_msg(skb);
1727
1728         if (unlikely(!msg_connected(hdr)))
1729                 return sk->sk_rcvbuf << msg_importance(hdr);
1730
1731         if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1732                 return sk->sk_rcvbuf;
1733
1734         return FLOWCTL_MSG_LIM;
1735 }
1736
1737 /**
1738  * filter_rcv - validate incoming message
1739  * @sk: socket
1740  * @skb: pointer to message.
1741  *
1742  * Enqueues message on receive queue if acceptable; optionally handles
1743  * disconnect indication for a connected socket.
1744  *
1745  * Called with socket lock already taken
1746  *
1747  * Returns true if message was added to socket receive queue, otherwise false
1748  */
1749 static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1750                        struct sk_buff_head *xmitq)
1751 {
1752         struct tipc_sock *tsk = tipc_sk(sk);
1753         struct tipc_msg *hdr = buf_msg(skb);
1754         unsigned int limit = rcvbuf_limit(sk, skb);
1755         int err = TIPC_OK;
1756         int usr = msg_user(hdr);
1757
1758         if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
1759                 tipc_sk_proto_rcv(tsk, skb, xmitq);
1760                 return false;
1761         }
1762
1763         if (unlikely(usr == SOCK_WAKEUP)) {
1764                 kfree_skb(skb);
1765                 tsk->link_cong = 0;
1766                 sk->sk_write_space(sk);
1767                 return false;
1768         }
1769
1770         /* Drop if illegal message type */
1771         if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1772                 kfree_skb(skb);
1773                 return false;
1774         }
1775
1776         /* Reject if wrong message type for current socket state */
1777         if (tipc_sk_type_connectionless(sk)) {
1778                 if (msg_connected(hdr)) {
1779                         err = TIPC_ERR_NO_PORT;
1780                         goto reject;
1781                 }
1782         } else if (unlikely(!filter_connect(tsk, skb))) {
1783                 err = TIPC_ERR_NO_PORT;
1784                 goto reject;
1785         }
1786
1787         /* Reject message if there isn't room to queue it */
1788         if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1789                 err = TIPC_ERR_OVERLOAD;
1790                 goto reject;
1791         }
1792
1793         /* Enqueue message */
1794         TIPC_SKB_CB(skb)->bytes_read = 0;
1795         __skb_queue_tail(&sk->sk_receive_queue, skb);
1796         skb_set_owner_r(skb, sk);
1797
1798         sk->sk_data_ready(sk);
1799         return true;
1800
1801 reject:
1802         if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1803                 __skb_queue_tail(xmitq, skb);
1804         return false;
1805 }
1806
1807 /**
1808  * tipc_backlog_rcv - handle incoming message from backlog queue
1809  * @sk: socket
1810  * @skb: message
1811  *
1812  * Caller must hold socket lock
1813  *
1814  * Returns 0
1815  */
1816 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
1817 {
1818         unsigned int truesize = skb->truesize;
1819         struct sk_buff_head xmitq;
1820         u32 dnode, selector;
1821
1822         __skb_queue_head_init(&xmitq);
1823
1824         if (likely(filter_rcv(sk, skb, &xmitq))) {
1825                 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
1826                 return 0;
1827         }
1828
1829         if (skb_queue_empty(&xmitq))
1830                 return 0;
1831
1832         /* Send response/rejected message */
1833         skb = __skb_dequeue(&xmitq);
1834         dnode = msg_destnode(buf_msg(skb));
1835         selector = msg_origport(buf_msg(skb));
1836         tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
1837         return 0;
1838 }
1839
1840 /**
1841  * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1842  *                   inputq and try adding them to socket or backlog queue
1843  * @inputq: list of incoming buffers with potentially different destinations
1844  * @sk: socket where the buffers should be enqueued
1845  * @dport: port number for the socket
1846  *
1847  * Caller must hold socket lock
1848  */
1849 static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
1850                             u32 dport, struct sk_buff_head *xmitq)
1851 {
1852         unsigned long time_limit = jiffies + 2;
1853         struct sk_buff *skb;
1854         unsigned int lim;
1855         atomic_t *dcnt;
1856         u32 onode;
1857
1858         while (skb_queue_len(inputq)) {
1859                 if (unlikely(time_after_eq(jiffies, time_limit)))
1860                         return;
1861
1862                 skb = tipc_skb_dequeue(inputq, dport);
1863                 if (unlikely(!skb))
1864                         return;
1865
1866                 /* Add message directly to receive queue if possible */
1867                 if (!sock_owned_by_user(sk)) {
1868                         filter_rcv(sk, skb, xmitq);
1869                         continue;
1870                 }
1871
1872                 /* Try backlog, compensating for double-counted bytes */
1873                 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
1874                 if (!sk->sk_backlog.len)
1875                         atomic_set(dcnt, 0);
1876                 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1877                 if (likely(!sk_add_backlog(sk, skb, lim)))
1878                         continue;
1879
1880                 /* Overload => reject message back to sender */
1881                 onode = tipc_own_addr(sock_net(sk));
1882                 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1883                         __skb_queue_tail(xmitq, skb);
1884                 break;
1885         }
1886 }
1887
1888 /**
1889  * tipc_sk_rcv - handle a chain of incoming buffers
1890  * @inputq: buffer list containing the buffers
1891  * Consumes all buffers in list until inputq is empty
1892  * Note: may be called in multiple threads referring to the same queue
1893  */
1894 void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
1895 {
1896         struct sk_buff_head xmitq;
1897         u32 dnode, dport = 0;
1898         int err;
1899         struct tipc_sock *tsk;
1900         struct sock *sk;
1901         struct sk_buff *skb;
1902
1903         __skb_queue_head_init(&xmitq);
1904         while (skb_queue_len(inputq)) {
1905                 dport = tipc_skb_peek_port(inputq, dport);
1906                 tsk = tipc_sk_lookup(net, dport);
1907
1908                 if (likely(tsk)) {
1909                         sk = &tsk->sk;
1910                         if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
1911                                 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
1912                                 spin_unlock_bh(&sk->sk_lock.slock);
1913                         }
1914                         /* Send pending response/rejected messages, if any */
1915                         while ((skb = __skb_dequeue(&xmitq))) {
1916                                 dnode = msg_destnode(buf_msg(skb));
1917                                 tipc_node_xmit_skb(net, skb, dnode, dport);
1918                         }
1919                         sock_put(sk);
1920                         continue;
1921                 }
1922
1923                 /* No destination socket => dequeue skb if still there */
1924                 skb = tipc_skb_dequeue(inputq, dport);
1925                 if (!skb)
1926                         return;
1927
1928                 /* Try secondary lookup if unresolved named message */
1929                 err = TIPC_ERR_NO_PORT;
1930                 if (tipc_msg_lookup_dest(net, skb, &err))
1931                         goto xmit;
1932
1933                 /* Prepare for message rejection */
1934                 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
1935                         continue;
1936 xmit:
1937                 dnode = msg_destnode(buf_msg(skb));
1938                 tipc_node_xmit_skb(net, skb, dnode, dport);
1939         }
1940 }
1941
1942 static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1943 {
1944         struct sock *sk = sock->sk;
1945         DEFINE_WAIT(wait);
1946         int done;
1947
1948         do {
1949                 int err = sock_error(sk);
1950                 if (err)
1951                         return err;
1952                 if (!*timeo_p)
1953                         return -ETIMEDOUT;
1954                 if (signal_pending(current))
1955                         return sock_intr_errno(*timeo_p);
1956
1957                 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1958                 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1959                 finish_wait(sk_sleep(sk), &wait);
1960         } while (!done);
1961         return 0;
1962 }
1963
1964 /**
1965  * tipc_connect - establish a connection to another TIPC port
1966  * @sock: socket structure
1967  * @dest: socket address for destination port
1968  * @destlen: size of socket address data structure
1969  * @flags: file-related flags associated with socket
1970  *
1971  * Returns 0 on success, errno otherwise
1972  */
1973 static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1974                         int destlen, int flags)
1975 {
1976         struct sock *sk = sock->sk;
1977         struct tipc_sock *tsk = tipc_sk(sk);
1978         struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1979         struct msghdr m = {NULL,};
1980         long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
1981         socket_state previous;
1982         int res = 0;
1983
1984         lock_sock(sk);
1985
1986         /* DGRAM/RDM connect(), just save the destaddr */
1987         if (tipc_sk_type_connectionless(sk)) {
1988                 if (dst->family == AF_UNSPEC) {
1989                         memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
1990                 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1991                         res = -EINVAL;
1992                 } else {
1993                         memcpy(&tsk->peer, dest, destlen);
1994                 }
1995                 goto exit;
1996         }
1997
1998         /*
1999          * Reject connection attempt using multicast address
2000          *
2001          * Note: send_msg() validates the rest of the address fields,
2002          *       so there's no need to do it here
2003          */
2004         if (dst->addrtype == TIPC_ADDR_MCAST) {
2005                 res = -EINVAL;
2006                 goto exit;
2007         }
2008
2009         previous = sock->state;
2010
2011         switch (sk->sk_state) {
2012         case TIPC_OPEN:
2013                 /* Send a 'SYN-' to destination */
2014                 m.msg_name = dest;
2015                 m.msg_namelen = destlen;
2016
2017                 /* If connect is in non-blocking case, set MSG_DONTWAIT to
2018                  * indicate send_msg() is never blocked.
2019                  */
2020                 if (!timeout)
2021                         m.msg_flags = MSG_DONTWAIT;
2022
2023                 res = __tipc_sendmsg(sock, &m, 0);
2024                 if ((res < 0) && (res != -EWOULDBLOCK))
2025                         goto exit;
2026
2027                 /* Just entered SS_CONNECTING state; the only
2028                  * difference is that return value in non-blocking
2029                  * case is EINPROGRESS, rather than EALREADY.
2030                  */
2031                 res = -EINPROGRESS;
2032                 break;
2033         }
2034
2035         switch (sock->state) {
2036         case SS_CONNECTING:
2037                 if (previous == SS_CONNECTING)
2038                         res = -EALREADY;
2039                 if (!timeout)
2040                         goto exit;
2041                 timeout = msecs_to_jiffies(timeout);
2042                 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
2043                 res = tipc_wait_for_connect(sock, &timeout);
2044                 break;
2045         case SS_CONNECTED:
2046                 res = -EISCONN;
2047                 break;
2048         default:
2049                 res = -EINVAL;
2050                 break;
2051         }
2052 exit:
2053         release_sock(sk);
2054         return res;
2055 }
2056
2057 /**
2058  * tipc_listen - allow socket to listen for incoming connections
2059  * @sock: socket structure
2060  * @len: (unused)
2061  *
2062  * Returns 0 on success, errno otherwise
2063  */
2064 static int tipc_listen(struct socket *sock, int len)
2065 {
2066         struct sock *sk = sock->sk;
2067         int res;
2068
2069         lock_sock(sk);
2070         res = tipc_set_sk_state(sk, TIPC_LISTEN);
2071         release_sock(sk);
2072
2073         return res;
2074 }
2075
2076 static int tipc_wait_for_accept(struct socket *sock, long timeo)
2077 {
2078         struct sock *sk = sock->sk;
2079         DEFINE_WAIT(wait);
2080         int err;
2081
2082         /* True wake-one mechanism for incoming connections: only
2083          * one process gets woken up, not the 'whole herd'.
2084          * Since we do not 'race & poll' for established sockets
2085          * anymore, the common case will execute the loop only once.
2086         */
2087         for (;;) {
2088                 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2089                                           TASK_INTERRUPTIBLE);
2090                 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
2091                         release_sock(sk);
2092                         timeo = schedule_timeout(timeo);
2093                         lock_sock(sk);
2094                 }
2095                 err = 0;
2096                 if (!skb_queue_empty(&sk->sk_receive_queue))
2097                         break;
2098                 err = -EAGAIN;
2099                 if (!timeo)
2100                         break;
2101                 err = sock_intr_errno(timeo);
2102                 if (signal_pending(current))
2103                         break;
2104         }
2105         finish_wait(sk_sleep(sk), &wait);
2106         return err;
2107 }
2108
2109 /**
2110  * tipc_accept - wait for connection request
2111  * @sock: listening socket
2112  * @newsock: new socket that is to be connected
2113  * @flags: file-related flags associated with socket
2114  *
2115  * Returns 0 on success, errno otherwise
2116  */
2117 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
2118 {
2119         struct sock *new_sk, *sk = sock->sk;
2120         struct sk_buff *buf;
2121         struct tipc_sock *new_tsock;
2122         struct tipc_msg *msg;
2123         long timeo;
2124         int res;
2125
2126         lock_sock(sk);
2127
2128         if (sk->sk_state != TIPC_LISTEN) {
2129                 res = -EINVAL;
2130                 goto exit;
2131         }
2132         timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2133         res = tipc_wait_for_accept(sock, timeo);
2134         if (res)
2135                 goto exit;
2136
2137         buf = skb_peek(&sk->sk_receive_queue);
2138
2139         res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 0);
2140         if (res)
2141                 goto exit;
2142         security_sk_clone(sock->sk, new_sock->sk);
2143
2144         new_sk = new_sock->sk;
2145         new_tsock = tipc_sk(new_sk);
2146         msg = buf_msg(buf);
2147
2148         /* we lock on new_sk; but lockdep sees the lock on sk */
2149         lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
2150
2151         /*
2152          * Reject any stray messages received by new socket
2153          * before the socket lock was taken (very, very unlikely)
2154          */
2155         tsk_rej_rx_queue(new_sk);
2156
2157         /* Connect new socket to it's peer */
2158         tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
2159         new_sock->state = SS_CONNECTED;
2160
2161         tsk_set_importance(new_tsock, msg_importance(msg));
2162         if (msg_named(msg)) {
2163                 new_tsock->conn_type = msg_nametype(msg);
2164                 new_tsock->conn_instance = msg_nameinst(msg);
2165         }
2166
2167         /*
2168          * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2169          * Respond to 'SYN+' by queuing it on new socket.
2170          */
2171         if (!msg_data_sz(msg)) {
2172                 struct msghdr m = {NULL,};
2173
2174                 tsk_advance_rx_queue(sk);
2175                 __tipc_send_stream(new_sock, &m, 0);
2176         } else {
2177                 __skb_dequeue(&sk->sk_receive_queue);
2178                 __skb_queue_head(&new_sk->sk_receive_queue, buf);
2179                 skb_set_owner_r(buf, new_sk);
2180         }
2181         release_sock(new_sk);
2182 exit:
2183         release_sock(sk);
2184         return res;
2185 }
2186
2187 /**
2188  * tipc_shutdown - shutdown socket connection
2189  * @sock: socket structure
2190  * @how: direction to close (must be SHUT_RDWR)
2191  *
2192  * Terminates connection (if necessary), then purges socket's receive queue.
2193  *
2194  * Returns 0 on success, errno otherwise
2195  */
2196 static int tipc_shutdown(struct socket *sock, int how)
2197 {
2198         struct sock *sk = sock->sk;
2199         int res;
2200
2201         if (how != SHUT_RDWR)
2202                 return -EINVAL;
2203
2204         lock_sock(sk);
2205
2206         __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2207         sk->sk_shutdown = SEND_SHUTDOWN;
2208
2209         if (sk->sk_state == TIPC_DISCONNECTING) {
2210                 /* Discard any unreceived messages */
2211                 __skb_queue_purge(&sk->sk_receive_queue);
2212
2213                 /* Wake up anyone sleeping in poll */
2214                 sk->sk_state_change(sk);
2215                 res = 0;
2216         } else {
2217                 res = -ENOTCONN;
2218         }
2219
2220         release_sock(sk);
2221         return res;
2222 }
2223
2224 static void tipc_sk_timeout(unsigned long data)
2225 {
2226         struct tipc_sock *tsk = (struct tipc_sock *)data;
2227         struct sock *sk = &tsk->sk;
2228         struct sk_buff *skb = NULL;
2229         u32 peer_port, peer_node;
2230         u32 own_node = tsk_own_node(tsk);
2231
2232         bh_lock_sock(sk);
2233         if (!tipc_sk_connected(sk)) {
2234                 bh_unlock_sock(sk);
2235                 goto exit;
2236         }
2237         peer_port = tsk_peer_port(tsk);
2238         peer_node = tsk_peer_node(tsk);
2239
2240         if (tsk->probe_unacked) {
2241                 if (!sock_owned_by_user(sk)) {
2242                         tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2243                         tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2244                                               tsk_peer_port(tsk));
2245                         sk->sk_state_change(sk);
2246                 } else {
2247                         /* Try again later */
2248                         sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2249                 }
2250
2251                 bh_unlock_sock(sk);
2252                 goto exit;
2253         }
2254
2255         skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2256                               INT_H_SIZE, 0, peer_node, own_node,
2257                               peer_port, tsk->portid, TIPC_OK);
2258         tsk->probe_unacked = true;
2259         sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
2260         bh_unlock_sock(sk);
2261         if (skb)
2262                 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
2263 exit:
2264         sock_put(sk);
2265 }
2266
2267 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
2268                            struct tipc_name_seq const *seq)
2269 {
2270         struct sock *sk = &tsk->sk;
2271         struct net *net = sock_net(sk);
2272         struct publication *publ;
2273         u32 key;
2274
2275         if (tipc_sk_connected(sk))
2276                 return -EINVAL;
2277         key = tsk->portid + tsk->pub_count + 1;
2278         if (key == tsk->portid)
2279                 return -EADDRINUSE;
2280
2281         publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
2282                                     scope, tsk->portid, key);
2283         if (unlikely(!publ))
2284                 return -EINVAL;
2285
2286         list_add(&publ->pport_list, &tsk->publications);
2287         tsk->pub_count++;
2288         tsk->published = 1;
2289         return 0;
2290 }
2291
2292 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
2293                             struct tipc_name_seq const *seq)
2294 {
2295         struct net *net = sock_net(&tsk->sk);
2296         struct publication *publ;
2297         struct publication *safe;
2298         int rc = -EINVAL;
2299
2300         list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
2301                 if (seq) {
2302                         if (publ->scope != scope)
2303                                 continue;
2304                         if (publ->type != seq->type)
2305                                 continue;
2306                         if (publ->lower != seq->lower)
2307                                 continue;
2308                         if (publ->upper != seq->upper)
2309                                 break;
2310                         tipc_nametbl_withdraw(net, publ->type, publ->lower,
2311                                               publ->ref, publ->key);
2312                         rc = 0;
2313                         break;
2314                 }
2315                 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2316                                       publ->ref, publ->key);
2317                 rc = 0;
2318         }
2319         if (list_empty(&tsk->publications))
2320                 tsk->published = 0;
2321         return rc;
2322 }
2323
2324 /* tipc_sk_reinit: set non-zero address in all existing sockets
2325  *                 when we go from standalone to network mode.
2326  */
2327 void tipc_sk_reinit(struct net *net)
2328 {
2329         struct tipc_net *tn = net_generic(net, tipc_net_id);
2330         const struct bucket_table *tbl;
2331         struct rhash_head *pos;
2332         struct tipc_sock *tsk;
2333         struct tipc_msg *msg;
2334         int i;
2335
2336         rcu_read_lock();
2337         tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2338         for (i = 0; i < tbl->size; i++) {
2339                 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2340                         spin_lock_bh(&tsk->sk.sk_lock.slock);
2341                         msg = &tsk->phdr;
2342                         msg_set_prevnode(msg, tn->own_addr);
2343                         msg_set_orignode(msg, tn->own_addr);
2344                         spin_unlock_bh(&tsk->sk.sk_lock.slock);
2345                 }
2346         }
2347         rcu_read_unlock();
2348 }
2349
2350 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
2351 {
2352         struct tipc_net *tn = net_generic(net, tipc_net_id);
2353         struct tipc_sock *tsk;
2354
2355         rcu_read_lock();
2356         tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
2357         if (tsk)
2358                 sock_hold(&tsk->sk);
2359         rcu_read_unlock();
2360
2361         return tsk;
2362 }
2363
2364 static int tipc_sk_insert(struct tipc_sock *tsk)
2365 {
2366         struct sock *sk = &tsk->sk;
2367         struct net *net = sock_net(sk);
2368         struct tipc_net *tn = net_generic(net, tipc_net_id);
2369         u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2370         u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2371
2372         while (remaining--) {
2373                 portid++;
2374                 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2375                         portid = TIPC_MIN_PORT;
2376                 tsk->portid = portid;
2377                 sock_hold(&tsk->sk);
2378                 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2379                                                    tsk_rht_params))
2380                         return 0;
2381                 sock_put(&tsk->sk);
2382         }
2383
2384         return -1;
2385 }
2386
2387 static void tipc_sk_remove(struct tipc_sock *tsk)
2388 {
2389         struct sock *sk = &tsk->sk;
2390         struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
2391
2392         if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
2393                 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2394                 __sock_put(sk);
2395         }
2396 }
2397
2398 static const struct rhashtable_params tsk_rht_params = {
2399         .nelem_hint = 192,
2400         .head_offset = offsetof(struct tipc_sock, node),
2401         .key_offset = offsetof(struct tipc_sock, portid),
2402         .key_len = sizeof(u32), /* portid */
2403         .max_size = 1048576,
2404         .min_size = 256,
2405         .automatic_shrinking = true,
2406 };
2407
2408 int tipc_sk_rht_init(struct net *net)
2409 {
2410         struct tipc_net *tn = net_generic(net, tipc_net_id);
2411
2412         return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
2413 }
2414
2415 void tipc_sk_rht_destroy(struct net *net)
2416 {
2417         struct tipc_net *tn = net_generic(net, tipc_net_id);
2418
2419         /* Wait for socket readers to complete */
2420         synchronize_net();
2421
2422         rhashtable_destroy(&tn->sk_rht);
2423 }
2424
2425 /**
2426  * tipc_setsockopt - set socket option
2427  * @sock: socket structure
2428  * @lvl: option level
2429  * @opt: option identifier
2430  * @ov: pointer to new option value
2431  * @ol: length of option value
2432  *
2433  * For stream sockets only, accepts and ignores all IPPROTO_TCP options
2434  * (to ease compatibility).
2435  *
2436  * Returns 0 on success, errno otherwise
2437  */
2438 static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2439                            char __user *ov, unsigned int ol)
2440 {
2441         struct sock *sk = sock->sk;
2442         struct tipc_sock *tsk = tipc_sk(sk);
2443         u32 value;
2444         int res;
2445
2446         if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2447                 return 0;
2448         if (lvl != SOL_TIPC)
2449                 return -ENOPROTOOPT;
2450         if (ol < sizeof(value))
2451                 return -EINVAL;
2452         res = get_user(value, (u32 __user *)ov);
2453         if (res)
2454                 return res;
2455
2456         lock_sock(sk);
2457
2458         switch (opt) {
2459         case TIPC_IMPORTANCE:
2460                 res = tsk_set_importance(tsk, value);
2461                 break;
2462         case TIPC_SRC_DROPPABLE:
2463                 if (sock->type != SOCK_STREAM)
2464                         tsk_set_unreliable(tsk, value);
2465                 else
2466                         res = -ENOPROTOOPT;
2467                 break;
2468         case TIPC_DEST_DROPPABLE:
2469                 tsk_set_unreturnable(tsk, value);
2470                 break;
2471         case TIPC_CONN_TIMEOUT:
2472                 tipc_sk(sk)->conn_timeout = value;
2473                 /* no need to set "res", since already 0 at this point */
2474                 break;
2475         default:
2476                 res = -EINVAL;
2477         }
2478
2479         release_sock(sk);
2480
2481         return res;
2482 }
2483
2484 /**
2485  * tipc_getsockopt - get socket option
2486  * @sock: socket structure
2487  * @lvl: option level
2488  * @opt: option identifier
2489  * @ov: receptacle for option value
2490  * @ol: receptacle for length of option value
2491  *
2492  * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
2493  * (to ease compatibility).
2494  *
2495  * Returns 0 on success, errno otherwise
2496  */
2497 static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2498                            char __user *ov, int __user *ol)
2499 {
2500         struct sock *sk = sock->sk;
2501         struct tipc_sock *tsk = tipc_sk(sk);
2502         int len;
2503         u32 value;
2504         int res;
2505
2506         if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2507                 return put_user(0, ol);
2508         if (lvl != SOL_TIPC)
2509                 return -ENOPROTOOPT;
2510         res = get_user(len, ol);
2511         if (res)
2512                 return res;
2513
2514         lock_sock(sk);
2515
2516         switch (opt) {
2517         case TIPC_IMPORTANCE:
2518                 value = tsk_importance(tsk);
2519                 break;
2520         case TIPC_SRC_DROPPABLE:
2521                 value = tsk_unreliable(tsk);
2522                 break;
2523         case TIPC_DEST_DROPPABLE:
2524                 value = tsk_unreturnable(tsk);
2525                 break;
2526         case TIPC_CONN_TIMEOUT:
2527                 value = tsk->conn_timeout;
2528                 /* no need to set "res", since already 0 at this point */
2529                 break;
2530         case TIPC_NODE_RECVQ_DEPTH:
2531                 value = 0; /* was tipc_queue_size, now obsolete */
2532                 break;
2533         case TIPC_SOCK_RECVQ_DEPTH:
2534                 value = skb_queue_len(&sk->sk_receive_queue);
2535                 break;
2536         default:
2537                 res = -EINVAL;
2538         }
2539
2540         release_sock(sk);
2541
2542         if (res)
2543                 return res;     /* "get" failed */
2544
2545         if (len < sizeof(value))
2546                 return -EINVAL;
2547
2548         if (copy_to_user(ov, &value, sizeof(value)))
2549                 return -EFAULT;
2550
2551         return put_user(sizeof(value), ol);
2552 }
2553
2554 static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2555 {
2556         struct sock *sk = sock->sk;
2557         struct tipc_sioc_ln_req lnr;
2558         void __user *argp = (void __user *)arg;
2559
2560         switch (cmd) {
2561         case SIOCGETLINKNAME:
2562                 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2563                         return -EFAULT;
2564                 if (!tipc_node_get_linkname(sock_net(sk),
2565                                             lnr.bearer_id & 0xffff, lnr.peer,
2566                                             lnr.linkname, TIPC_MAX_LINK_NAME)) {
2567                         if (copy_to_user(argp, &lnr, sizeof(lnr)))
2568                                 return -EFAULT;
2569                         return 0;
2570                 }
2571                 return -EADDRNOTAVAIL;
2572         default:
2573                 return -ENOIOCTLCMD;
2574         }
2575 }
2576
2577 /* Protocol switches for the various types of TIPC sockets */
2578
2579 static const struct proto_ops msg_ops = {
2580         .owner          = THIS_MODULE,
2581         .family         = AF_TIPC,
2582         .release        = tipc_release,
2583         .bind           = tipc_bind,
2584         .connect        = tipc_connect,
2585         .socketpair     = sock_no_socketpair,
2586         .accept         = sock_no_accept,
2587         .getname        = tipc_getname,
2588         .poll           = tipc_poll,
2589         .ioctl          = tipc_ioctl,
2590         .listen         = sock_no_listen,
2591         .shutdown       = tipc_shutdown,
2592         .setsockopt     = tipc_setsockopt,
2593         .getsockopt     = tipc_getsockopt,
2594         .sendmsg        = tipc_sendmsg,
2595         .recvmsg        = tipc_recvmsg,
2596         .mmap           = sock_no_mmap,
2597         .sendpage       = sock_no_sendpage
2598 };
2599
2600 static const struct proto_ops packet_ops = {
2601         .owner          = THIS_MODULE,
2602         .family         = AF_TIPC,
2603         .release        = tipc_release,
2604         .bind           = tipc_bind,
2605         .connect        = tipc_connect,
2606         .socketpair     = sock_no_socketpair,
2607         .accept         = tipc_accept,
2608         .getname        = tipc_getname,
2609         .poll           = tipc_poll,
2610         .ioctl          = tipc_ioctl,
2611         .listen         = tipc_listen,
2612         .shutdown       = tipc_shutdown,
2613         .setsockopt     = tipc_setsockopt,
2614         .getsockopt     = tipc_getsockopt,
2615         .sendmsg        = tipc_send_packet,
2616         .recvmsg        = tipc_recvmsg,
2617         .mmap           = sock_no_mmap,
2618         .sendpage       = sock_no_sendpage
2619 };
2620
2621 static const struct proto_ops stream_ops = {
2622         .owner          = THIS_MODULE,
2623         .family         = AF_TIPC,
2624         .release        = tipc_release,
2625         .bind           = tipc_bind,
2626         .connect        = tipc_connect,
2627         .socketpair     = sock_no_socketpair,
2628         .accept         = tipc_accept,
2629         .getname        = tipc_getname,
2630         .poll           = tipc_poll,
2631         .ioctl          = tipc_ioctl,
2632         .listen         = tipc_listen,
2633         .shutdown       = tipc_shutdown,
2634         .setsockopt     = tipc_setsockopt,
2635         .getsockopt     = tipc_getsockopt,
2636         .sendmsg        = tipc_send_stream,
2637         .recvmsg        = tipc_recv_stream,
2638         .mmap           = sock_no_mmap,
2639         .sendpage       = sock_no_sendpage
2640 };
2641
2642 static const struct net_proto_family tipc_family_ops = {
2643         .owner          = THIS_MODULE,
2644         .family         = AF_TIPC,
2645         .create         = tipc_sk_create
2646 };
2647
2648 static struct proto tipc_proto = {
2649         .name           = "TIPC",
2650         .owner          = THIS_MODULE,
2651         .obj_size       = sizeof(struct tipc_sock),
2652         .sysctl_rmem    = sysctl_tipc_rmem
2653 };
2654
2655 /**
2656  * tipc_socket_init - initialize TIPC socket interface
2657  *
2658  * Returns 0 on success, errno otherwise
2659  */
2660 int tipc_socket_init(void)
2661 {
2662         int res;
2663
2664         res = proto_register(&tipc_proto, 1);
2665         if (res) {
2666                 pr_err("Failed to register TIPC protocol type\n");
2667                 goto out;
2668         }
2669
2670         res = sock_register(&tipc_family_ops);
2671         if (res) {
2672                 pr_err("Failed to register TIPC socket type\n");
2673                 proto_unregister(&tipc_proto);
2674                 goto out;
2675         }
2676  out:
2677         return res;
2678 }
2679
2680 /**
2681  * tipc_socket_stop - stop TIPC socket interface
2682  */
2683 void tipc_socket_stop(void)
2684 {
2685         sock_unregister(tipc_family_ops.family);
2686         proto_unregister(&tipc_proto);
2687 }
2688
2689 /* Caller should hold socket lock for the passed tipc socket. */
2690 static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
2691 {
2692         u32 peer_node;
2693         u32 peer_port;
2694         struct nlattr *nest;
2695
2696         peer_node = tsk_peer_node(tsk);
2697         peer_port = tsk_peer_port(tsk);
2698
2699         nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2700
2701         if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2702                 goto msg_full;
2703         if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2704                 goto msg_full;
2705
2706         if (tsk->conn_type != 0) {
2707                 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2708                         goto msg_full;
2709                 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2710                         goto msg_full;
2711                 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2712                         goto msg_full;
2713         }
2714         nla_nest_end(skb, nest);
2715
2716         return 0;
2717
2718 msg_full:
2719         nla_nest_cancel(skb, nest);
2720
2721         return -EMSGSIZE;
2722 }
2723
2724 /* Caller should hold socket lock for the passed tipc socket. */
2725 static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2726                             struct tipc_sock *tsk)
2727 {
2728         int err;
2729         void *hdr;
2730         struct nlattr *attrs;
2731         struct net *net = sock_net(skb->sk);
2732         struct tipc_net *tn = net_generic(net, tipc_net_id);
2733         struct sock *sk = &tsk->sk;
2734
2735         hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2736                           &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2737         if (!hdr)
2738                 goto msg_cancel;
2739
2740         attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2741         if (!attrs)
2742                 goto genlmsg_cancel;
2743         if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
2744                 goto attr_msg_cancel;
2745         if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
2746                 goto attr_msg_cancel;
2747
2748         if (tipc_sk_connected(sk)) {
2749                 err = __tipc_nl_add_sk_con(skb, tsk);
2750                 if (err)
2751                         goto attr_msg_cancel;
2752         } else if (!list_empty(&tsk->publications)) {
2753                 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2754                         goto attr_msg_cancel;
2755         }
2756         nla_nest_end(skb, attrs);
2757         genlmsg_end(skb, hdr);
2758
2759         return 0;
2760
2761 attr_msg_cancel:
2762         nla_nest_cancel(skb, attrs);
2763 genlmsg_cancel:
2764         genlmsg_cancel(skb, hdr);
2765 msg_cancel:
2766         return -EMSGSIZE;
2767 }
2768
2769 int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2770 {
2771         int err;
2772         struct tipc_sock *tsk;
2773         const struct bucket_table *tbl;
2774         struct rhash_head *pos;
2775         struct net *net = sock_net(skb->sk);
2776         struct tipc_net *tn = net_generic(net, tipc_net_id);
2777         u32 tbl_id = cb->args[0];
2778         u32 prev_portid = cb->args[1];
2779
2780         rcu_read_lock();
2781         tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2782         for (; tbl_id < tbl->size; tbl_id++) {
2783                 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
2784                         spin_lock_bh(&tsk->sk.sk_lock.slock);
2785                         if (prev_portid && prev_portid != tsk->portid) {
2786                                 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2787                                 continue;
2788                         }
2789
2790                         err = __tipc_nl_add_sk(skb, cb, tsk);
2791                         if (err) {
2792                                 prev_portid = tsk->portid;
2793                                 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2794                                 goto out;
2795                         }
2796                         prev_portid = 0;
2797                         spin_unlock_bh(&tsk->sk.sk_lock.slock);
2798                 }
2799         }
2800 out:
2801         rcu_read_unlock();
2802         cb->args[0] = tbl_id;
2803         cb->args[1] = prev_portid;
2804
2805         return skb->len;
2806 }
2807
2808 /* Caller should hold socket lock for the passed tipc socket. */
2809 static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2810                                  struct netlink_callback *cb,
2811                                  struct publication *publ)
2812 {
2813         void *hdr;
2814         struct nlattr *attrs;
2815
2816         hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2817                           &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2818         if (!hdr)
2819                 goto msg_cancel;
2820
2821         attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2822         if (!attrs)
2823                 goto genlmsg_cancel;
2824
2825         if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2826                 goto attr_msg_cancel;
2827         if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2828                 goto attr_msg_cancel;
2829         if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2830                 goto attr_msg_cancel;
2831         if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2832                 goto attr_msg_cancel;
2833
2834         nla_nest_end(skb, attrs);
2835         genlmsg_end(skb, hdr);
2836
2837         return 0;
2838
2839 attr_msg_cancel:
2840         nla_nest_cancel(skb, attrs);
2841 genlmsg_cancel:
2842         genlmsg_cancel(skb, hdr);
2843 msg_cancel:
2844         return -EMSGSIZE;
2845 }
2846
2847 /* Caller should hold socket lock for the passed tipc socket. */
2848 static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2849                                   struct netlink_callback *cb,
2850                                   struct tipc_sock *tsk, u32 *last_publ)
2851 {
2852         int err;
2853         struct publication *p;
2854
2855         if (*last_publ) {
2856                 list_for_each_entry(p, &tsk->publications, pport_list) {
2857                         if (p->key == *last_publ)
2858                                 break;
2859                 }
2860                 if (p->key != *last_publ) {
2861                         /* We never set seq or call nl_dump_check_consistent()
2862                          * this means that setting prev_seq here will cause the
2863                          * consistence check to fail in the netlink callback
2864                          * handler. Resulting in the last NLMSG_DONE message
2865                          * having the NLM_F_DUMP_INTR flag set.
2866                          */
2867                         cb->prev_seq = 1;
2868                         *last_publ = 0;
2869                         return -EPIPE;
2870                 }
2871         } else {
2872                 p = list_first_entry(&tsk->publications, struct publication,
2873                                      pport_list);
2874         }
2875
2876         list_for_each_entry_from(p, &tsk->publications, pport_list) {
2877                 err = __tipc_nl_add_sk_publ(skb, cb, p);
2878                 if (err) {
2879                         *last_publ = p->key;
2880                         return err;
2881                 }
2882         }
2883         *last_publ = 0;
2884
2885         return 0;
2886 }
2887
2888 int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2889 {
2890         int err;
2891         u32 tsk_portid = cb->args[0];
2892         u32 last_publ = cb->args[1];
2893         u32 done = cb->args[2];
2894         struct net *net = sock_net(skb->sk);
2895         struct tipc_sock *tsk;
2896
2897         if (!tsk_portid) {
2898                 struct nlattr **attrs;
2899                 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2900
2901                 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2902                 if (err)
2903                         return err;
2904
2905                 if (!attrs[TIPC_NLA_SOCK])
2906                         return -EINVAL;
2907
2908                 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2909                                        attrs[TIPC_NLA_SOCK],
2910                                        tipc_nl_sock_policy);
2911                 if (err)
2912                         return err;
2913
2914                 if (!sock[TIPC_NLA_SOCK_REF])
2915                         return -EINVAL;
2916
2917                 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
2918         }
2919
2920         if (done)
2921                 return 0;
2922
2923         tsk = tipc_sk_lookup(net, tsk_portid);
2924         if (!tsk)
2925                 return -EINVAL;
2926
2927         lock_sock(&tsk->sk);
2928         err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2929         if (!err)
2930                 done = 1;
2931         release_sock(&tsk->sk);
2932         sock_put(&tsk->sk);
2933
2934         cb->args[0] = tsk_portid;
2935         cb->args[1] = last_publ;
2936         cb->args[2] = done;
2937
2938         return skb->len;
2939 }