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