1 /*
2  * net/tipc/port.c: TIPC port code
3  *
4  * Copyright (c) 1992-2007, Ericsson AB
5  * Copyright (c) 2004-2008, 2010-2011, 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 "core.h"
38 #include "config.h"
39 #include "port.h"
40 #include "name_table.h"
41 
42 /* Connection management: */
43 #define PROBING_INTERVAL 3600000	/* [ms] => 1 h */
44 #define CONFIRMED 0
45 #define PROBING 1
46 
47 #define MAX_REJECT_SIZE 1024
48 
49 static struct sk_buff *msg_queue_head;
50 static struct sk_buff *msg_queue_tail;
51 
52 DEFINE_SPINLOCK(tipc_port_list_lock);
53 static DEFINE_SPINLOCK(queue_lock);
54 
55 static LIST_HEAD(ports);
56 static void port_handle_node_down(unsigned long ref);
57 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
58 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
59 static void port_timeout(unsigned long ref);
60 
61 
port_peernode(struct tipc_port * p_ptr)62 static u32 port_peernode(struct tipc_port *p_ptr)
63 {
64 	return msg_destnode(&p_ptr->phdr);
65 }
66 
port_peerport(struct tipc_port * p_ptr)67 static u32 port_peerport(struct tipc_port *p_ptr)
68 {
69 	return msg_destport(&p_ptr->phdr);
70 }
71 
72 /**
73  * tipc_multicast - send a multicast message to local and remote destinations
74  */
75 
tipc_multicast(u32 ref,struct tipc_name_seq const * seq,u32 num_sect,struct iovec const * msg_sect,unsigned int total_len)76 int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
77 		   u32 num_sect, struct iovec const *msg_sect,
78 		   unsigned int total_len)
79 {
80 	struct tipc_msg *hdr;
81 	struct sk_buff *buf;
82 	struct sk_buff *ibuf = NULL;
83 	struct tipc_port_list dports = {0, NULL, };
84 	struct tipc_port *oport = tipc_port_deref(ref);
85 	int ext_targets;
86 	int res;
87 
88 	if (unlikely(!oport))
89 		return -EINVAL;
90 
91 	/* Create multicast message */
92 
93 	hdr = &oport->phdr;
94 	msg_set_type(hdr, TIPC_MCAST_MSG);
95 	msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
96 	msg_set_destport(hdr, 0);
97 	msg_set_destnode(hdr, 0);
98 	msg_set_nametype(hdr, seq->type);
99 	msg_set_namelower(hdr, seq->lower);
100 	msg_set_nameupper(hdr, seq->upper);
101 	msg_set_hdr_sz(hdr, MCAST_H_SIZE);
102 	res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
103 			!oport->user_port, &buf);
104 	if (unlikely(!buf))
105 		return res;
106 
107 	/* Figure out where to send multicast message */
108 
109 	ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
110 						TIPC_NODE_SCOPE, &dports);
111 
112 	/* Send message to destinations (duplicate it only if necessary) */
113 
114 	if (ext_targets) {
115 		if (dports.count != 0) {
116 			ibuf = skb_copy(buf, GFP_ATOMIC);
117 			if (ibuf == NULL) {
118 				tipc_port_list_free(&dports);
119 				kfree_skb(buf);
120 				return -ENOMEM;
121 			}
122 		}
123 		res = tipc_bclink_send_msg(buf);
124 		if ((res < 0) && (dports.count != 0))
125 			kfree_skb(ibuf);
126 	} else {
127 		ibuf = buf;
128 	}
129 
130 	if (res >= 0) {
131 		if (ibuf)
132 			tipc_port_recv_mcast(ibuf, &dports);
133 	} else {
134 		tipc_port_list_free(&dports);
135 	}
136 	return res;
137 }
138 
139 /**
140  * tipc_port_recv_mcast - deliver multicast message to all destination ports
141  *
142  * If there is no port list, perform a lookup to create one
143  */
144 
tipc_port_recv_mcast(struct sk_buff * buf,struct tipc_port_list * dp)145 void tipc_port_recv_mcast(struct sk_buff *buf, struct tipc_port_list *dp)
146 {
147 	struct tipc_msg *msg;
148 	struct tipc_port_list dports = {0, NULL, };
149 	struct tipc_port_list *item = dp;
150 	int cnt = 0;
151 
152 	msg = buf_msg(buf);
153 
154 	/* Create destination port list, if one wasn't supplied */
155 
156 	if (dp == NULL) {
157 		tipc_nametbl_mc_translate(msg_nametype(msg),
158 				     msg_namelower(msg),
159 				     msg_nameupper(msg),
160 				     TIPC_CLUSTER_SCOPE,
161 				     &dports);
162 		item = dp = &dports;
163 	}
164 
165 	/* Deliver a copy of message to each destination port */
166 
167 	if (dp->count != 0) {
168 		msg_set_destnode(msg, tipc_own_addr);
169 		if (dp->count == 1) {
170 			msg_set_destport(msg, dp->ports[0]);
171 			tipc_port_recv_msg(buf);
172 			tipc_port_list_free(dp);
173 			return;
174 		}
175 		for (; cnt < dp->count; cnt++) {
176 			int index = cnt % PLSIZE;
177 			struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
178 
179 			if (b == NULL) {
180 				warn("Unable to deliver multicast message(s)\n");
181 				goto exit;
182 			}
183 			if ((index == 0) && (cnt != 0))
184 				item = item->next;
185 			msg_set_destport(buf_msg(b), item->ports[index]);
186 			tipc_port_recv_msg(b);
187 		}
188 	}
189 exit:
190 	kfree_skb(buf);
191 	tipc_port_list_free(dp);
192 }
193 
194 /**
195  * tipc_createport_raw - create a generic TIPC port
196  *
197  * Returns pointer to (locked) TIPC port, or NULL if unable to create it
198  */
199 
tipc_createport_raw(void * usr_handle,u32 (* dispatcher)(struct tipc_port *,struct sk_buff *),void (* wakeup)(struct tipc_port *),const u32 importance)200 struct tipc_port *tipc_createport_raw(void *usr_handle,
201 			u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
202 			void (*wakeup)(struct tipc_port *),
203 			const u32 importance)
204 {
205 	struct tipc_port *p_ptr;
206 	struct tipc_msg *msg;
207 	u32 ref;
208 
209 	p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
210 	if (!p_ptr) {
211 		warn("Port creation failed, no memory\n");
212 		return NULL;
213 	}
214 	ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
215 	if (!ref) {
216 		warn("Port creation failed, reference table exhausted\n");
217 		kfree(p_ptr);
218 		return NULL;
219 	}
220 
221 	p_ptr->usr_handle = usr_handle;
222 	p_ptr->max_pkt = MAX_PKT_DEFAULT;
223 	p_ptr->ref = ref;
224 	msg = &p_ptr->phdr;
225 	tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
226 	msg_set_origport(msg, ref);
227 	INIT_LIST_HEAD(&p_ptr->wait_list);
228 	INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
229 	p_ptr->dispatcher = dispatcher;
230 	p_ptr->wakeup = wakeup;
231 	p_ptr->user_port = NULL;
232 	k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
233 	spin_lock_bh(&tipc_port_list_lock);
234 	INIT_LIST_HEAD(&p_ptr->publications);
235 	INIT_LIST_HEAD(&p_ptr->port_list);
236 	list_add_tail(&p_ptr->port_list, &ports);
237 	spin_unlock_bh(&tipc_port_list_lock);
238 	return p_ptr;
239 }
240 
tipc_deleteport(u32 ref)241 int tipc_deleteport(u32 ref)
242 {
243 	struct tipc_port *p_ptr;
244 	struct sk_buff *buf = NULL;
245 
246 	tipc_withdraw(ref, 0, NULL);
247 	p_ptr = tipc_port_lock(ref);
248 	if (!p_ptr)
249 		return -EINVAL;
250 
251 	tipc_ref_discard(ref);
252 	tipc_port_unlock(p_ptr);
253 
254 	k_cancel_timer(&p_ptr->timer);
255 	if (p_ptr->connected) {
256 		buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
257 		tipc_nodesub_unsubscribe(&p_ptr->subscription);
258 	}
259 	kfree(p_ptr->user_port);
260 
261 	spin_lock_bh(&tipc_port_list_lock);
262 	list_del(&p_ptr->port_list);
263 	list_del(&p_ptr->wait_list);
264 	spin_unlock_bh(&tipc_port_list_lock);
265 	k_term_timer(&p_ptr->timer);
266 	kfree(p_ptr);
267 	tipc_net_route_msg(buf);
268 	return 0;
269 }
270 
port_unreliable(struct tipc_port * p_ptr)271 static int port_unreliable(struct tipc_port *p_ptr)
272 {
273 	return msg_src_droppable(&p_ptr->phdr);
274 }
275 
tipc_portunreliable(u32 ref,unsigned int * isunreliable)276 int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
277 {
278 	struct tipc_port *p_ptr;
279 
280 	p_ptr = tipc_port_lock(ref);
281 	if (!p_ptr)
282 		return -EINVAL;
283 	*isunreliable = port_unreliable(p_ptr);
284 	tipc_port_unlock(p_ptr);
285 	return 0;
286 }
287 
tipc_set_portunreliable(u32 ref,unsigned int isunreliable)288 int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
289 {
290 	struct tipc_port *p_ptr;
291 
292 	p_ptr = tipc_port_lock(ref);
293 	if (!p_ptr)
294 		return -EINVAL;
295 	msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
296 	tipc_port_unlock(p_ptr);
297 	return 0;
298 }
299 
port_unreturnable(struct tipc_port * p_ptr)300 static int port_unreturnable(struct tipc_port *p_ptr)
301 {
302 	return msg_dest_droppable(&p_ptr->phdr);
303 }
304 
tipc_portunreturnable(u32 ref,unsigned int * isunrejectable)305 int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
306 {
307 	struct tipc_port *p_ptr;
308 
309 	p_ptr = tipc_port_lock(ref);
310 	if (!p_ptr)
311 		return -EINVAL;
312 	*isunrejectable = port_unreturnable(p_ptr);
313 	tipc_port_unlock(p_ptr);
314 	return 0;
315 }
316 
tipc_set_portunreturnable(u32 ref,unsigned int isunrejectable)317 int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
318 {
319 	struct tipc_port *p_ptr;
320 
321 	p_ptr = tipc_port_lock(ref);
322 	if (!p_ptr)
323 		return -EINVAL;
324 	msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
325 	tipc_port_unlock(p_ptr);
326 	return 0;
327 }
328 
329 /*
330  * port_build_proto_msg(): create connection protocol message for port
331  *
332  * On entry the port must be locked and connected.
333  */
port_build_proto_msg(struct tipc_port * p_ptr,u32 type,u32 ack)334 static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
335 					    u32 type, u32 ack)
336 {
337 	struct sk_buff *buf;
338 	struct tipc_msg *msg;
339 
340 	buf = tipc_buf_acquire(INT_H_SIZE);
341 	if (buf) {
342 		msg = buf_msg(buf);
343 		tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
344 			      port_peernode(p_ptr));
345 		msg_set_destport(msg, port_peerport(p_ptr));
346 		msg_set_origport(msg, p_ptr->ref);
347 		msg_set_msgcnt(msg, ack);
348 	}
349 	return buf;
350 }
351 
tipc_reject_msg(struct sk_buff * buf,u32 err)352 int tipc_reject_msg(struct sk_buff *buf, u32 err)
353 {
354 	struct tipc_msg *msg = buf_msg(buf);
355 	struct sk_buff *rbuf;
356 	struct tipc_msg *rmsg;
357 	int hdr_sz;
358 	u32 imp;
359 	u32 data_sz = msg_data_sz(msg);
360 	u32 src_node;
361 	u32 rmsg_sz;
362 
363 	/* discard rejected message if it shouldn't be returned to sender */
364 
365 	if (WARN(!msg_isdata(msg),
366 		 "attempt to reject message with user=%u", msg_user(msg))) {
367 		dump_stack();
368 		goto exit;
369 	}
370 	if (msg_errcode(msg) || msg_dest_droppable(msg))
371 		goto exit;
372 
373 	/*
374 	 * construct returned message by copying rejected message header and
375 	 * data (or subset), then updating header fields that need adjusting
376 	 */
377 
378 	hdr_sz = msg_hdr_sz(msg);
379 	rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
380 
381 	rbuf = tipc_buf_acquire(rmsg_sz);
382 	if (rbuf == NULL)
383 		goto exit;
384 
385 	rmsg = buf_msg(rbuf);
386 	skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
387 
388 	if (msg_connected(rmsg)) {
389 		imp = msg_importance(rmsg);
390 		if (imp < TIPC_CRITICAL_IMPORTANCE)
391 			msg_set_importance(rmsg, ++imp);
392 	}
393 	msg_set_non_seq(rmsg, 0);
394 	msg_set_size(rmsg, rmsg_sz);
395 	msg_set_errcode(rmsg, err);
396 	msg_set_prevnode(rmsg, tipc_own_addr);
397 	msg_swap_words(rmsg, 4, 5);
398 	if (!msg_short(rmsg))
399 		msg_swap_words(rmsg, 6, 7);
400 
401 	/* send self-abort message when rejecting on a connected port */
402 	if (msg_connected(msg)) {
403 		struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
404 
405 		if (p_ptr) {
406 			struct sk_buff *abuf = NULL;
407 
408 			if (p_ptr->connected)
409 				abuf = port_build_self_abort_msg(p_ptr, err);
410 			tipc_port_unlock(p_ptr);
411 			tipc_net_route_msg(abuf);
412 		}
413 	}
414 
415 	/* send returned message & dispose of rejected message */
416 
417 	src_node = msg_prevnode(msg);
418 	if (src_node == tipc_own_addr)
419 		tipc_port_recv_msg(rbuf);
420 	else
421 		tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
422 exit:
423 	kfree_skb(buf);
424 	return data_sz;
425 }
426 
tipc_port_reject_sections(struct tipc_port * p_ptr,struct tipc_msg * hdr,struct iovec const * msg_sect,u32 num_sect,unsigned int total_len,int err)427 int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
428 			      struct iovec const *msg_sect, u32 num_sect,
429 			      unsigned int total_len, int err)
430 {
431 	struct sk_buff *buf;
432 	int res;
433 
434 	res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
435 			!p_ptr->user_port, &buf);
436 	if (!buf)
437 		return res;
438 
439 	return tipc_reject_msg(buf, err);
440 }
441 
port_timeout(unsigned long ref)442 static void port_timeout(unsigned long ref)
443 {
444 	struct tipc_port *p_ptr = tipc_port_lock(ref);
445 	struct sk_buff *buf = NULL;
446 
447 	if (!p_ptr)
448 		return;
449 
450 	if (!p_ptr->connected) {
451 		tipc_port_unlock(p_ptr);
452 		return;
453 	}
454 
455 	/* Last probe answered ? */
456 	if (p_ptr->probing_state == PROBING) {
457 		buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
458 	} else {
459 		buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
460 		p_ptr->probing_state = PROBING;
461 		k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
462 	}
463 	tipc_port_unlock(p_ptr);
464 	tipc_net_route_msg(buf);
465 }
466 
467 
port_handle_node_down(unsigned long ref)468 static void port_handle_node_down(unsigned long ref)
469 {
470 	struct tipc_port *p_ptr = tipc_port_lock(ref);
471 	struct sk_buff *buf = NULL;
472 
473 	if (!p_ptr)
474 		return;
475 	buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
476 	tipc_port_unlock(p_ptr);
477 	tipc_net_route_msg(buf);
478 }
479 
480 
port_build_self_abort_msg(struct tipc_port * p_ptr,u32 err)481 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
482 {
483 	struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
484 
485 	if (buf) {
486 		struct tipc_msg *msg = buf_msg(buf);
487 		msg_swap_words(msg, 4, 5);
488 		msg_swap_words(msg, 6, 7);
489 	}
490 	return buf;
491 }
492 
493 
port_build_peer_abort_msg(struct tipc_port * p_ptr,u32 err)494 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
495 {
496 	struct sk_buff *buf;
497 	struct tipc_msg *msg;
498 	u32 imp;
499 
500 	if (!p_ptr->connected)
501 		return NULL;
502 
503 	buf = tipc_buf_acquire(BASIC_H_SIZE);
504 	if (buf) {
505 		msg = buf_msg(buf);
506 		memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
507 		msg_set_hdr_sz(msg, BASIC_H_SIZE);
508 		msg_set_size(msg, BASIC_H_SIZE);
509 		imp = msg_importance(msg);
510 		if (imp < TIPC_CRITICAL_IMPORTANCE)
511 			msg_set_importance(msg, ++imp);
512 		msg_set_errcode(msg, err);
513 	}
514 	return buf;
515 }
516 
tipc_port_recv_proto_msg(struct sk_buff * buf)517 void tipc_port_recv_proto_msg(struct sk_buff *buf)
518 {
519 	struct tipc_msg *msg = buf_msg(buf);
520 	struct tipc_port *p_ptr;
521 	struct sk_buff *r_buf = NULL;
522 	u32 orignode = msg_orignode(msg);
523 	u32 origport = msg_origport(msg);
524 	u32 destport = msg_destport(msg);
525 	int wakeable;
526 
527 	/* Validate connection */
528 
529 	p_ptr = tipc_port_lock(destport);
530 	if (!p_ptr || !p_ptr->connected ||
531 	    (port_peernode(p_ptr) != orignode) ||
532 	    (port_peerport(p_ptr) != origport)) {
533 		r_buf = tipc_buf_acquire(BASIC_H_SIZE);
534 		if (r_buf) {
535 			msg = buf_msg(r_buf);
536 			tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
537 				      BASIC_H_SIZE, orignode);
538 			msg_set_errcode(msg, TIPC_ERR_NO_PORT);
539 			msg_set_origport(msg, destport);
540 			msg_set_destport(msg, origport);
541 		}
542 		if (p_ptr)
543 			tipc_port_unlock(p_ptr);
544 		goto exit;
545 	}
546 
547 	/* Process protocol message sent by peer */
548 
549 	switch (msg_type(msg)) {
550 	case CONN_ACK:
551 		wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
552 			p_ptr->wakeup;
553 		p_ptr->acked += msg_msgcnt(msg);
554 		if (!tipc_port_congested(p_ptr)) {
555 			p_ptr->congested = 0;
556 			if (wakeable)
557 				p_ptr->wakeup(p_ptr);
558 		}
559 		break;
560 	case CONN_PROBE:
561 		r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
562 		break;
563 	default:
564 		/* CONN_PROBE_REPLY or unrecognized - no action required */
565 		break;
566 	}
567 	p_ptr->probing_state = CONFIRMED;
568 	tipc_port_unlock(p_ptr);
569 exit:
570 	tipc_net_route_msg(r_buf);
571 	kfree_skb(buf);
572 }
573 
port_print(struct tipc_port * p_ptr,struct print_buf * buf,int full_id)574 static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
575 {
576 	struct publication *publ;
577 
578 	if (full_id)
579 		tipc_printf(buf, "<%u.%u.%u:%u>:",
580 			    tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
581 			    tipc_node(tipc_own_addr), p_ptr->ref);
582 	else
583 		tipc_printf(buf, "%-10u:", p_ptr->ref);
584 
585 	if (p_ptr->connected) {
586 		u32 dport = port_peerport(p_ptr);
587 		u32 destnode = port_peernode(p_ptr);
588 
589 		tipc_printf(buf, " connected to <%u.%u.%u:%u>",
590 			    tipc_zone(destnode), tipc_cluster(destnode),
591 			    tipc_node(destnode), dport);
592 		if (p_ptr->conn_type != 0)
593 			tipc_printf(buf, " via {%u,%u}",
594 				    p_ptr->conn_type,
595 				    p_ptr->conn_instance);
596 	} else if (p_ptr->published) {
597 		tipc_printf(buf, " bound to");
598 		list_for_each_entry(publ, &p_ptr->publications, pport_list) {
599 			if (publ->lower == publ->upper)
600 				tipc_printf(buf, " {%u,%u}", publ->type,
601 					    publ->lower);
602 			else
603 				tipc_printf(buf, " {%u,%u,%u}", publ->type,
604 					    publ->lower, publ->upper);
605 		}
606 	}
607 	tipc_printf(buf, "\n");
608 }
609 
610 #define MAX_PORT_QUERY 32768
611 
tipc_port_get_ports(void)612 struct sk_buff *tipc_port_get_ports(void)
613 {
614 	struct sk_buff *buf;
615 	struct tlv_desc *rep_tlv;
616 	struct print_buf pb;
617 	struct tipc_port *p_ptr;
618 	int str_len;
619 
620 	buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
621 	if (!buf)
622 		return NULL;
623 	rep_tlv = (struct tlv_desc *)buf->data;
624 
625 	tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
626 	spin_lock_bh(&tipc_port_list_lock);
627 	list_for_each_entry(p_ptr, &ports, port_list) {
628 		spin_lock_bh(p_ptr->lock);
629 		port_print(p_ptr, &pb, 0);
630 		spin_unlock_bh(p_ptr->lock);
631 	}
632 	spin_unlock_bh(&tipc_port_list_lock);
633 	str_len = tipc_printbuf_validate(&pb);
634 
635 	skb_put(buf, TLV_SPACE(str_len));
636 	TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
637 
638 	return buf;
639 }
640 
tipc_port_reinit(void)641 void tipc_port_reinit(void)
642 {
643 	struct tipc_port *p_ptr;
644 	struct tipc_msg *msg;
645 
646 	spin_lock_bh(&tipc_port_list_lock);
647 	list_for_each_entry(p_ptr, &ports, port_list) {
648 		msg = &p_ptr->phdr;
649 		if (msg_orignode(msg) == tipc_own_addr)
650 			break;
651 		msg_set_prevnode(msg, tipc_own_addr);
652 		msg_set_orignode(msg, tipc_own_addr);
653 	}
654 	spin_unlock_bh(&tipc_port_list_lock);
655 }
656 
657 
658 /*
659  *  port_dispatcher_sigh(): Signal handler for messages destinated
660  *                          to the tipc_port interface.
661  */
662 
port_dispatcher_sigh(void * dummy)663 static void port_dispatcher_sigh(void *dummy)
664 {
665 	struct sk_buff *buf;
666 
667 	spin_lock_bh(&queue_lock);
668 	buf = msg_queue_head;
669 	msg_queue_head = NULL;
670 	spin_unlock_bh(&queue_lock);
671 
672 	while (buf) {
673 		struct tipc_port *p_ptr;
674 		struct user_port *up_ptr;
675 		struct tipc_portid orig;
676 		struct tipc_name_seq dseq;
677 		void *usr_handle;
678 		int connected;
679 		int published;
680 		u32 message_type;
681 
682 		struct sk_buff *next = buf->next;
683 		struct tipc_msg *msg = buf_msg(buf);
684 		u32 dref = msg_destport(msg);
685 
686 		message_type = msg_type(msg);
687 		if (message_type > TIPC_DIRECT_MSG)
688 			goto reject;	/* Unsupported message type */
689 
690 		p_ptr = tipc_port_lock(dref);
691 		if (!p_ptr)
692 			goto reject;	/* Port deleted while msg in queue */
693 
694 		orig.ref = msg_origport(msg);
695 		orig.node = msg_orignode(msg);
696 		up_ptr = p_ptr->user_port;
697 		usr_handle = up_ptr->usr_handle;
698 		connected = p_ptr->connected;
699 		published = p_ptr->published;
700 
701 		if (unlikely(msg_errcode(msg)))
702 			goto err;
703 
704 		switch (message_type) {
705 
706 		case TIPC_CONN_MSG:{
707 				tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
708 				u32 peer_port = port_peerport(p_ptr);
709 				u32 peer_node = port_peernode(p_ptr);
710 				u32 dsz;
711 
712 				tipc_port_unlock(p_ptr);
713 				if (unlikely(!cb))
714 					goto reject;
715 				if (unlikely(!connected)) {
716 					if (tipc_connect2port(dref, &orig))
717 						goto reject;
718 				} else if ((msg_origport(msg) != peer_port) ||
719 					   (msg_orignode(msg) != peer_node))
720 					goto reject;
721 				dsz = msg_data_sz(msg);
722 				if (unlikely(dsz &&
723 					     (++p_ptr->conn_unacked >=
724 					      TIPC_FLOW_CONTROL_WIN)))
725 					tipc_acknowledge(dref,
726 							 p_ptr->conn_unacked);
727 				skb_pull(buf, msg_hdr_sz(msg));
728 				cb(usr_handle, dref, &buf, msg_data(msg), dsz);
729 				break;
730 			}
731 		case TIPC_DIRECT_MSG:{
732 				tipc_msg_event cb = up_ptr->msg_cb;
733 
734 				tipc_port_unlock(p_ptr);
735 				if (unlikely(!cb || connected))
736 					goto reject;
737 				skb_pull(buf, msg_hdr_sz(msg));
738 				cb(usr_handle, dref, &buf, msg_data(msg),
739 				   msg_data_sz(msg), msg_importance(msg),
740 				   &orig);
741 				break;
742 			}
743 		case TIPC_MCAST_MSG:
744 		case TIPC_NAMED_MSG:{
745 				tipc_named_msg_event cb = up_ptr->named_msg_cb;
746 
747 				tipc_port_unlock(p_ptr);
748 				if (unlikely(!cb || connected || !published))
749 					goto reject;
750 				dseq.type =  msg_nametype(msg);
751 				dseq.lower = msg_nameinst(msg);
752 				dseq.upper = (message_type == TIPC_NAMED_MSG)
753 					? dseq.lower : msg_nameupper(msg);
754 				skb_pull(buf, msg_hdr_sz(msg));
755 				cb(usr_handle, dref, &buf, msg_data(msg),
756 				   msg_data_sz(msg), msg_importance(msg),
757 				   &orig, &dseq);
758 				break;
759 			}
760 		}
761 		if (buf)
762 			kfree_skb(buf);
763 		buf = next;
764 		continue;
765 err:
766 		switch (message_type) {
767 
768 		case TIPC_CONN_MSG:{
769 				tipc_conn_shutdown_event cb =
770 					up_ptr->conn_err_cb;
771 				u32 peer_port = port_peerport(p_ptr);
772 				u32 peer_node = port_peernode(p_ptr);
773 
774 				tipc_port_unlock(p_ptr);
775 				if (!cb || !connected)
776 					break;
777 				if ((msg_origport(msg) != peer_port) ||
778 				    (msg_orignode(msg) != peer_node))
779 					break;
780 				tipc_disconnect(dref);
781 				skb_pull(buf, msg_hdr_sz(msg));
782 				cb(usr_handle, dref, &buf, msg_data(msg),
783 				   msg_data_sz(msg), msg_errcode(msg));
784 				break;
785 			}
786 		case TIPC_DIRECT_MSG:{
787 				tipc_msg_err_event cb = up_ptr->err_cb;
788 
789 				tipc_port_unlock(p_ptr);
790 				if (!cb || connected)
791 					break;
792 				skb_pull(buf, msg_hdr_sz(msg));
793 				cb(usr_handle, dref, &buf, msg_data(msg),
794 				   msg_data_sz(msg), msg_errcode(msg), &orig);
795 				break;
796 			}
797 		case TIPC_MCAST_MSG:
798 		case TIPC_NAMED_MSG:{
799 				tipc_named_msg_err_event cb =
800 					up_ptr->named_err_cb;
801 
802 				tipc_port_unlock(p_ptr);
803 				if (!cb || connected)
804 					break;
805 				dseq.type =  msg_nametype(msg);
806 				dseq.lower = msg_nameinst(msg);
807 				dseq.upper = (message_type == TIPC_NAMED_MSG)
808 					? dseq.lower : msg_nameupper(msg);
809 				skb_pull(buf, msg_hdr_sz(msg));
810 				cb(usr_handle, dref, &buf, msg_data(msg),
811 				   msg_data_sz(msg), msg_errcode(msg), &dseq);
812 				break;
813 			}
814 		}
815 		if (buf)
816 			kfree_skb(buf);
817 		buf = next;
818 		continue;
819 reject:
820 		tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
821 		buf = next;
822 	}
823 }
824 
825 /*
826  *  port_dispatcher(): Dispatcher for messages destinated
827  *  to the tipc_port interface. Called with port locked.
828  */
829 
port_dispatcher(struct tipc_port * dummy,struct sk_buff * buf)830 static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
831 {
832 	buf->next = NULL;
833 	spin_lock_bh(&queue_lock);
834 	if (msg_queue_head) {
835 		msg_queue_tail->next = buf;
836 		msg_queue_tail = buf;
837 	} else {
838 		msg_queue_tail = msg_queue_head = buf;
839 		tipc_k_signal((Handler)port_dispatcher_sigh, 0);
840 	}
841 	spin_unlock_bh(&queue_lock);
842 	return 0;
843 }
844 
845 /*
846  * Wake up port after congestion: Called with port locked,
847  *
848  */
849 
port_wakeup_sh(unsigned long ref)850 static void port_wakeup_sh(unsigned long ref)
851 {
852 	struct tipc_port *p_ptr;
853 	struct user_port *up_ptr;
854 	tipc_continue_event cb = NULL;
855 	void *uh = NULL;
856 
857 	p_ptr = tipc_port_lock(ref);
858 	if (p_ptr) {
859 		up_ptr = p_ptr->user_port;
860 		if (up_ptr) {
861 			cb = up_ptr->continue_event_cb;
862 			uh = up_ptr->usr_handle;
863 		}
864 		tipc_port_unlock(p_ptr);
865 	}
866 	if (cb)
867 		cb(uh, ref);
868 }
869 
870 
port_wakeup(struct tipc_port * p_ptr)871 static void port_wakeup(struct tipc_port *p_ptr)
872 {
873 	tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
874 }
875 
tipc_acknowledge(u32 ref,u32 ack)876 void tipc_acknowledge(u32 ref, u32 ack)
877 {
878 	struct tipc_port *p_ptr;
879 	struct sk_buff *buf = NULL;
880 
881 	p_ptr = tipc_port_lock(ref);
882 	if (!p_ptr)
883 		return;
884 	if (p_ptr->connected) {
885 		p_ptr->conn_unacked -= ack;
886 		buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
887 	}
888 	tipc_port_unlock(p_ptr);
889 	tipc_net_route_msg(buf);
890 }
891 
892 /*
893  * tipc_createport(): user level call.
894  */
895 
tipc_createport(void * usr_handle,unsigned int importance,tipc_msg_err_event error_cb,tipc_named_msg_err_event named_error_cb,tipc_conn_shutdown_event conn_error_cb,tipc_msg_event msg_cb,tipc_named_msg_event named_msg_cb,tipc_conn_msg_event conn_msg_cb,tipc_continue_event continue_event_cb,u32 * portref)896 int tipc_createport(void *usr_handle,
897 		    unsigned int importance,
898 		    tipc_msg_err_event error_cb,
899 		    tipc_named_msg_err_event named_error_cb,
900 		    tipc_conn_shutdown_event conn_error_cb,
901 		    tipc_msg_event msg_cb,
902 		    tipc_named_msg_event named_msg_cb,
903 		    tipc_conn_msg_event conn_msg_cb,
904 		    tipc_continue_event continue_event_cb,/* May be zero */
905 		    u32 *portref)
906 {
907 	struct user_port *up_ptr;
908 	struct tipc_port *p_ptr;
909 
910 	up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
911 	if (!up_ptr) {
912 		warn("Port creation failed, no memory\n");
913 		return -ENOMEM;
914 	}
915 	p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
916 						   port_wakeup, importance);
917 	if (!p_ptr) {
918 		kfree(up_ptr);
919 		return -ENOMEM;
920 	}
921 
922 	p_ptr->user_port = up_ptr;
923 	up_ptr->usr_handle = usr_handle;
924 	up_ptr->ref = p_ptr->ref;
925 	up_ptr->err_cb = error_cb;
926 	up_ptr->named_err_cb = named_error_cb;
927 	up_ptr->conn_err_cb = conn_error_cb;
928 	up_ptr->msg_cb = msg_cb;
929 	up_ptr->named_msg_cb = named_msg_cb;
930 	up_ptr->conn_msg_cb = conn_msg_cb;
931 	up_ptr->continue_event_cb = continue_event_cb;
932 	*portref = p_ptr->ref;
933 	tipc_port_unlock(p_ptr);
934 	return 0;
935 }
936 
tipc_portimportance(u32 ref,unsigned int * importance)937 int tipc_portimportance(u32 ref, unsigned int *importance)
938 {
939 	struct tipc_port *p_ptr;
940 
941 	p_ptr = tipc_port_lock(ref);
942 	if (!p_ptr)
943 		return -EINVAL;
944 	*importance = (unsigned int)msg_importance(&p_ptr->phdr);
945 	tipc_port_unlock(p_ptr);
946 	return 0;
947 }
948 
tipc_set_portimportance(u32 ref,unsigned int imp)949 int tipc_set_portimportance(u32 ref, unsigned int imp)
950 {
951 	struct tipc_port *p_ptr;
952 
953 	if (imp > TIPC_CRITICAL_IMPORTANCE)
954 		return -EINVAL;
955 
956 	p_ptr = tipc_port_lock(ref);
957 	if (!p_ptr)
958 		return -EINVAL;
959 	msg_set_importance(&p_ptr->phdr, (u32)imp);
960 	tipc_port_unlock(p_ptr);
961 	return 0;
962 }
963 
964 
tipc_publish(u32 ref,unsigned int scope,struct tipc_name_seq const * seq)965 int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
966 {
967 	struct tipc_port *p_ptr;
968 	struct publication *publ;
969 	u32 key;
970 	int res = -EINVAL;
971 
972 	p_ptr = tipc_port_lock(ref);
973 	if (!p_ptr)
974 		return -EINVAL;
975 
976 	if (p_ptr->connected)
977 		goto exit;
978 	if (seq->lower > seq->upper)
979 		goto exit;
980 	if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
981 		goto exit;
982 	key = ref + p_ptr->pub_count + 1;
983 	if (key == ref) {
984 		res = -EADDRINUSE;
985 		goto exit;
986 	}
987 	publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
988 				    scope, p_ptr->ref, key);
989 	if (publ) {
990 		list_add(&publ->pport_list, &p_ptr->publications);
991 		p_ptr->pub_count++;
992 		p_ptr->published = 1;
993 		res = 0;
994 	}
995 exit:
996 	tipc_port_unlock(p_ptr);
997 	return res;
998 }
999 
tipc_withdraw(u32 ref,unsigned int scope,struct tipc_name_seq const * seq)1000 int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1001 {
1002 	struct tipc_port *p_ptr;
1003 	struct publication *publ;
1004 	struct publication *tpubl;
1005 	int res = -EINVAL;
1006 
1007 	p_ptr = tipc_port_lock(ref);
1008 	if (!p_ptr)
1009 		return -EINVAL;
1010 	if (!seq) {
1011 		list_for_each_entry_safe(publ, tpubl,
1012 					 &p_ptr->publications, pport_list) {
1013 			tipc_nametbl_withdraw(publ->type, publ->lower,
1014 					      publ->ref, publ->key);
1015 		}
1016 		res = 0;
1017 	} else {
1018 		list_for_each_entry_safe(publ, tpubl,
1019 					 &p_ptr->publications, pport_list) {
1020 			if (publ->scope != scope)
1021 				continue;
1022 			if (publ->type != seq->type)
1023 				continue;
1024 			if (publ->lower != seq->lower)
1025 				continue;
1026 			if (publ->upper != seq->upper)
1027 				break;
1028 			tipc_nametbl_withdraw(publ->type, publ->lower,
1029 					      publ->ref, publ->key);
1030 			res = 0;
1031 			break;
1032 		}
1033 	}
1034 	if (list_empty(&p_ptr->publications))
1035 		p_ptr->published = 0;
1036 	tipc_port_unlock(p_ptr);
1037 	return res;
1038 }
1039 
tipc_connect2port(u32 ref,struct tipc_portid const * peer)1040 int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1041 {
1042 	struct tipc_port *p_ptr;
1043 	struct tipc_msg *msg;
1044 	int res = -EINVAL;
1045 
1046 	p_ptr = tipc_port_lock(ref);
1047 	if (!p_ptr)
1048 		return -EINVAL;
1049 	if (p_ptr->published || p_ptr->connected)
1050 		goto exit;
1051 	if (!peer->ref)
1052 		goto exit;
1053 
1054 	msg = &p_ptr->phdr;
1055 	msg_set_destnode(msg, peer->node);
1056 	msg_set_destport(msg, peer->ref);
1057 	msg_set_type(msg, TIPC_CONN_MSG);
1058 	msg_set_lookup_scope(msg, 0);
1059 	msg_set_hdr_sz(msg, SHORT_H_SIZE);
1060 
1061 	p_ptr->probing_interval = PROBING_INTERVAL;
1062 	p_ptr->probing_state = CONFIRMED;
1063 	p_ptr->connected = 1;
1064 	k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1065 
1066 	tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
1067 			  (void *)(unsigned long)ref,
1068 			  (net_ev_handler)port_handle_node_down);
1069 	res = 0;
1070 exit:
1071 	tipc_port_unlock(p_ptr);
1072 	p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
1073 	return res;
1074 }
1075 
1076 /**
1077  * tipc_disconnect_port - disconnect port from peer
1078  *
1079  * Port must be locked.
1080  */
1081 
tipc_disconnect_port(struct tipc_port * tp_ptr)1082 int tipc_disconnect_port(struct tipc_port *tp_ptr)
1083 {
1084 	int res;
1085 
1086 	if (tp_ptr->connected) {
1087 		tp_ptr->connected = 0;
1088 		/* let timer expire on it's own to avoid deadlock! */
1089 		tipc_nodesub_unsubscribe(
1090 			&((struct tipc_port *)tp_ptr)->subscription);
1091 		res = 0;
1092 	} else {
1093 		res = -ENOTCONN;
1094 	}
1095 	return res;
1096 }
1097 
1098 /*
1099  * tipc_disconnect(): Disconnect port form peer.
1100  *                    This is a node local operation.
1101  */
1102 
tipc_disconnect(u32 ref)1103 int tipc_disconnect(u32 ref)
1104 {
1105 	struct tipc_port *p_ptr;
1106 	int res;
1107 
1108 	p_ptr = tipc_port_lock(ref);
1109 	if (!p_ptr)
1110 		return -EINVAL;
1111 	res = tipc_disconnect_port((struct tipc_port *)p_ptr);
1112 	tipc_port_unlock(p_ptr);
1113 	return res;
1114 }
1115 
1116 /*
1117  * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1118  */
tipc_shutdown(u32 ref)1119 int tipc_shutdown(u32 ref)
1120 {
1121 	struct tipc_port *p_ptr;
1122 	struct sk_buff *buf = NULL;
1123 
1124 	p_ptr = tipc_port_lock(ref);
1125 	if (!p_ptr)
1126 		return -EINVAL;
1127 
1128 	buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
1129 	tipc_port_unlock(p_ptr);
1130 	tipc_net_route_msg(buf);
1131 	return tipc_disconnect(ref);
1132 }
1133 
1134 /**
1135  * tipc_port_recv_msg - receive message from lower layer and deliver to port user
1136  */
1137 
tipc_port_recv_msg(struct sk_buff * buf)1138 int tipc_port_recv_msg(struct sk_buff *buf)
1139 {
1140 	struct tipc_port *p_ptr;
1141 	struct tipc_msg *msg = buf_msg(buf);
1142 	u32 destport = msg_destport(msg);
1143 	u32 dsz = msg_data_sz(msg);
1144 	u32 err;
1145 
1146 	/* forward unresolved named message */
1147 	if (unlikely(!destport)) {
1148 		tipc_net_route_msg(buf);
1149 		return dsz;
1150 	}
1151 
1152 	/* validate destination & pass to port, otherwise reject message */
1153 	p_ptr = tipc_port_lock(destport);
1154 	if (likely(p_ptr)) {
1155 		if (likely(p_ptr->connected)) {
1156 			if ((unlikely(msg_origport(msg) !=
1157 				      tipc_peer_port(p_ptr))) ||
1158 			    (unlikely(msg_orignode(msg) !=
1159 				      tipc_peer_node(p_ptr))) ||
1160 			    (unlikely(!msg_connected(msg)))) {
1161 				err = TIPC_ERR_NO_PORT;
1162 				tipc_port_unlock(p_ptr);
1163 				goto reject;
1164 			}
1165 		}
1166 		err = p_ptr->dispatcher(p_ptr, buf);
1167 		tipc_port_unlock(p_ptr);
1168 		if (likely(!err))
1169 			return dsz;
1170 	} else {
1171 		err = TIPC_ERR_NO_PORT;
1172 	}
1173 reject:
1174 	return tipc_reject_msg(buf, err);
1175 }
1176 
1177 /*
1178  *  tipc_port_recv_sections(): Concatenate and deliver sectioned
1179  *                        message for this node.
1180  */
1181 
tipc_port_recv_sections(struct tipc_port * sender,unsigned int num_sect,struct iovec const * msg_sect,unsigned int total_len)1182 static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
1183 				   struct iovec const *msg_sect,
1184 				   unsigned int total_len)
1185 {
1186 	struct sk_buff *buf;
1187 	int res;
1188 
1189 	res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
1190 			MAX_MSG_SIZE, !sender->user_port, &buf);
1191 	if (likely(buf))
1192 		tipc_port_recv_msg(buf);
1193 	return res;
1194 }
1195 
1196 /**
1197  * tipc_send - send message sections on connection
1198  */
1199 
tipc_send(u32 ref,unsigned int num_sect,struct iovec const * msg_sect,unsigned int total_len)1200 int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
1201 	      unsigned int total_len)
1202 {
1203 	struct tipc_port *p_ptr;
1204 	u32 destnode;
1205 	int res;
1206 
1207 	p_ptr = tipc_port_deref(ref);
1208 	if (!p_ptr || !p_ptr->connected)
1209 		return -EINVAL;
1210 
1211 	p_ptr->congested = 1;
1212 	if (!tipc_port_congested(p_ptr)) {
1213 		destnode = port_peernode(p_ptr);
1214 		if (likely(destnode != tipc_own_addr))
1215 			res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1216 							   total_len, destnode);
1217 		else
1218 			res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1219 						      total_len);
1220 
1221 		if (likely(res != -ELINKCONG)) {
1222 			p_ptr->congested = 0;
1223 			if (res > 0)
1224 				p_ptr->sent++;
1225 			return res;
1226 		}
1227 	}
1228 	if (port_unreliable(p_ptr)) {
1229 		p_ptr->congested = 0;
1230 		return total_len;
1231 	}
1232 	return -ELINKCONG;
1233 }
1234 
1235 /**
1236  * tipc_send2name - send message sections to port name
1237  */
1238 
tipc_send2name(u32 ref,struct tipc_name const * name,unsigned int domain,unsigned int num_sect,struct iovec const * msg_sect,unsigned int total_len)1239 int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
1240 		   unsigned int num_sect, struct iovec const *msg_sect,
1241 		   unsigned int total_len)
1242 {
1243 	struct tipc_port *p_ptr;
1244 	struct tipc_msg *msg;
1245 	u32 destnode = domain;
1246 	u32 destport;
1247 	int res;
1248 
1249 	p_ptr = tipc_port_deref(ref);
1250 	if (!p_ptr || p_ptr->connected)
1251 		return -EINVAL;
1252 
1253 	msg = &p_ptr->phdr;
1254 	msg_set_type(msg, TIPC_NAMED_MSG);
1255 	msg_set_hdr_sz(msg, NAMED_H_SIZE);
1256 	msg_set_nametype(msg, name->type);
1257 	msg_set_nameinst(msg, name->instance);
1258 	msg_set_lookup_scope(msg, tipc_addr_scope(domain));
1259 	destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
1260 	msg_set_destnode(msg, destnode);
1261 	msg_set_destport(msg, destport);
1262 
1263 	if (likely(destport || destnode)) {
1264 		if (likely(destnode == tipc_own_addr))
1265 			res = tipc_port_recv_sections(p_ptr, num_sect,
1266 						      msg_sect, total_len);
1267 		else
1268 			res = tipc_link_send_sections_fast(p_ptr, msg_sect,
1269 							   num_sect, total_len,
1270 							   destnode);
1271 		if (likely(res != -ELINKCONG)) {
1272 			if (res > 0)
1273 				p_ptr->sent++;
1274 			return res;
1275 		}
1276 		if (port_unreliable(p_ptr)) {
1277 			return total_len;
1278 		}
1279 		return -ELINKCONG;
1280 	}
1281 	return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1282 					 total_len, TIPC_ERR_NO_NAME);
1283 }
1284 
1285 /**
1286  * tipc_send2port - send message sections to port identity
1287  */
1288 
tipc_send2port(u32 ref,struct tipc_portid const * dest,unsigned int num_sect,struct iovec const * msg_sect,unsigned int total_len)1289 int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1290 		   unsigned int num_sect, struct iovec const *msg_sect,
1291 		   unsigned int total_len)
1292 {
1293 	struct tipc_port *p_ptr;
1294 	struct tipc_msg *msg;
1295 	int res;
1296 
1297 	p_ptr = tipc_port_deref(ref);
1298 	if (!p_ptr || p_ptr->connected)
1299 		return -EINVAL;
1300 
1301 	msg = &p_ptr->phdr;
1302 	msg_set_type(msg, TIPC_DIRECT_MSG);
1303 	msg_set_lookup_scope(msg, 0);
1304 	msg_set_destnode(msg, dest->node);
1305 	msg_set_destport(msg, dest->ref);
1306 	msg_set_hdr_sz(msg, BASIC_H_SIZE);
1307 
1308 	if (dest->node == tipc_own_addr)
1309 		res =  tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
1310 					       total_len);
1311 	else
1312 		res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1313 						   total_len, dest->node);
1314 	if (likely(res != -ELINKCONG)) {
1315 		if (res > 0)
1316 			p_ptr->sent++;
1317 		return res;
1318 	}
1319 	if (port_unreliable(p_ptr)) {
1320 		return total_len;
1321 	}
1322 	return -ELINKCONG;
1323 }
1324 
1325 /**
1326  * tipc_send_buf2port - send message buffer to port identity
1327  */
1328 
tipc_send_buf2port(u32 ref,struct tipc_portid const * dest,struct sk_buff * buf,unsigned int dsz)1329 int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1330 	       struct sk_buff *buf, unsigned int dsz)
1331 {
1332 	struct tipc_port *p_ptr;
1333 	struct tipc_msg *msg;
1334 	int res;
1335 
1336 	p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
1337 	if (!p_ptr || p_ptr->connected)
1338 		return -EINVAL;
1339 
1340 	msg = &p_ptr->phdr;
1341 	msg_set_type(msg, TIPC_DIRECT_MSG);
1342 	msg_set_destnode(msg, dest->node);
1343 	msg_set_destport(msg, dest->ref);
1344 	msg_set_hdr_sz(msg, BASIC_H_SIZE);
1345 	msg_set_size(msg, BASIC_H_SIZE + dsz);
1346 	if (skb_cow(buf, BASIC_H_SIZE))
1347 		return -ENOMEM;
1348 
1349 	skb_push(buf, BASIC_H_SIZE);
1350 	skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
1351 
1352 	if (dest->node == tipc_own_addr)
1353 		res = tipc_port_recv_msg(buf);
1354 	else
1355 		res = tipc_send_buf_fast(buf, dest->node);
1356 	if (likely(res != -ELINKCONG)) {
1357 		if (res > 0)
1358 			p_ptr->sent++;
1359 		return res;
1360 	}
1361 	if (port_unreliable(p_ptr))
1362 		return dsz;
1363 	return -ELINKCONG;
1364 }
1365 
1366