]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/tipc/port.c
tipc: align tipc function names with common naming practice in the network
[mirror_ubuntu-bionic-kernel.git] / net / tipc / port.c
1 /*
2 * net/tipc/port.c: TIPC port code
3 *
4 * Copyright (c) 1992-2007, 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 "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 DEFINE_SPINLOCK(tipc_port_list_lock);
50
51 static LIST_HEAD(ports);
52 static void port_handle_node_down(unsigned long ref);
53 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
54 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
55 static void port_timeout(unsigned long ref);
56
57
58 static u32 port_peernode(struct tipc_port *p_ptr)
59 {
60 return msg_destnode(&p_ptr->phdr);
61 }
62
63 static u32 port_peerport(struct tipc_port *p_ptr)
64 {
65 return msg_destport(&p_ptr->phdr);
66 }
67
68 /**
69 * tipc_port_peer_msg - verify message was sent by connected port's peer
70 *
71 * Handles cases where the node's network address has changed from
72 * the default of <0.0.0> to its configured setting.
73 */
74 int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
75 {
76 u32 peernode;
77 u32 orignode;
78
79 if (msg_origport(msg) != port_peerport(p_ptr))
80 return 0;
81
82 orignode = msg_orignode(msg);
83 peernode = port_peernode(p_ptr);
84 return (orignode == peernode) ||
85 (!orignode && (peernode == tipc_own_addr)) ||
86 (!peernode && (orignode == tipc_own_addr));
87 }
88
89 /**
90 * tipc_port_mcast_xmit - send a multicast message to local and remote
91 * destinations
92 */
93 int tipc_port_mcast_xmit(u32 ref, struct tipc_name_seq const *seq,
94 struct iovec const *msg_sect, unsigned int len)
95 {
96 struct tipc_msg *hdr;
97 struct sk_buff *buf;
98 struct sk_buff *ibuf = NULL;
99 struct tipc_port_list dports = {0, NULL, };
100 struct tipc_port *oport = tipc_port_deref(ref);
101 int ext_targets;
102 int res;
103
104 if (unlikely(!oport))
105 return -EINVAL;
106
107 /* Create multicast message */
108 hdr = &oport->phdr;
109 msg_set_type(hdr, TIPC_MCAST_MSG);
110 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
111 msg_set_destport(hdr, 0);
112 msg_set_destnode(hdr, 0);
113 msg_set_nametype(hdr, seq->type);
114 msg_set_namelower(hdr, seq->lower);
115 msg_set_nameupper(hdr, seq->upper);
116 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
117 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
118 if (unlikely(!buf))
119 return res;
120
121 /* Figure out where to send multicast message */
122 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
123 TIPC_NODE_SCOPE, &dports);
124
125 /* Send message to destinations (duplicate it only if necessary) */
126 if (ext_targets) {
127 if (dports.count != 0) {
128 ibuf = skb_copy(buf, GFP_ATOMIC);
129 if (ibuf == NULL) {
130 tipc_port_list_free(&dports);
131 kfree_skb(buf);
132 return -ENOMEM;
133 }
134 }
135 res = tipc_bclink_xmit(buf);
136 if ((res < 0) && (dports.count != 0))
137 kfree_skb(ibuf);
138 } else {
139 ibuf = buf;
140 }
141
142 if (res >= 0) {
143 if (ibuf)
144 tipc_port_mcast_rcv(ibuf, &dports);
145 } else {
146 tipc_port_list_free(&dports);
147 }
148 return res;
149 }
150
151 /**
152 * tipc_port_mcast_rcv - deliver multicast message to all destination ports
153 *
154 * If there is no port list, perform a lookup to create one
155 */
156 void tipc_port_mcast_rcv(struct sk_buff *buf, struct tipc_port_list *dp)
157 {
158 struct tipc_msg *msg;
159 struct tipc_port_list dports = {0, NULL, };
160 struct tipc_port_list *item = dp;
161 int cnt = 0;
162
163 msg = buf_msg(buf);
164
165 /* Create destination port list, if one wasn't supplied */
166 if (dp == NULL) {
167 tipc_nametbl_mc_translate(msg_nametype(msg),
168 msg_namelower(msg),
169 msg_nameupper(msg),
170 TIPC_CLUSTER_SCOPE,
171 &dports);
172 item = dp = &dports;
173 }
174
175 /* Deliver a copy of message to each destination port */
176 if (dp->count != 0) {
177 msg_set_destnode(msg, tipc_own_addr);
178 if (dp->count == 1) {
179 msg_set_destport(msg, dp->ports[0]);
180 tipc_port_rcv(buf);
181 tipc_port_list_free(dp);
182 return;
183 }
184 for (; cnt < dp->count; cnt++) {
185 int index = cnt % PLSIZE;
186 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
187
188 if (b == NULL) {
189 pr_warn("Unable to deliver multicast message(s)\n");
190 goto exit;
191 }
192 if ((index == 0) && (cnt != 0))
193 item = item->next;
194 msg_set_destport(buf_msg(b), item->ports[index]);
195 tipc_port_rcv(b);
196 }
197 }
198 exit:
199 kfree_skb(buf);
200 tipc_port_list_free(dp);
201 }
202
203 /**
204 * tipc_createport - create a generic TIPC port
205 *
206 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
207 */
208 struct tipc_port *tipc_createport(struct sock *sk,
209 u32 (*dispatcher)(struct tipc_port *,
210 struct sk_buff *),
211 void (*wakeup)(struct tipc_port *),
212 const u32 importance)
213 {
214 struct tipc_port *p_ptr;
215 struct tipc_msg *msg;
216 u32 ref;
217
218 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
219 if (!p_ptr) {
220 pr_warn("Port creation failed, no memory\n");
221 return NULL;
222 }
223 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
224 if (!ref) {
225 pr_warn("Port creation failed, ref. table exhausted\n");
226 kfree(p_ptr);
227 return NULL;
228 }
229
230 p_ptr->sk = sk;
231 p_ptr->max_pkt = MAX_PKT_DEFAULT;
232 p_ptr->ref = ref;
233 INIT_LIST_HEAD(&p_ptr->wait_list);
234 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
235 p_ptr->dispatcher = dispatcher;
236 p_ptr->wakeup = wakeup;
237 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
238 INIT_LIST_HEAD(&p_ptr->publications);
239 INIT_LIST_HEAD(&p_ptr->port_list);
240
241 /*
242 * Must hold port list lock while initializing message header template
243 * to ensure a change to node's own network address doesn't result
244 * in template containing out-dated network address information
245 */
246 spin_lock_bh(&tipc_port_list_lock);
247 msg = &p_ptr->phdr;
248 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
249 msg_set_origport(msg, ref);
250 list_add_tail(&p_ptr->port_list, &ports);
251 spin_unlock_bh(&tipc_port_list_lock);
252 return p_ptr;
253 }
254
255 int tipc_deleteport(struct tipc_port *p_ptr)
256 {
257 struct sk_buff *buf = NULL;
258
259 tipc_withdraw(p_ptr, 0, NULL);
260
261 spin_lock_bh(p_ptr->lock);
262 tipc_ref_discard(p_ptr->ref);
263 spin_unlock_bh(p_ptr->lock);
264
265 k_cancel_timer(&p_ptr->timer);
266 if (p_ptr->connected) {
267 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
268 tipc_nodesub_unsubscribe(&p_ptr->subscription);
269 }
270
271 spin_lock_bh(&tipc_port_list_lock);
272 list_del(&p_ptr->port_list);
273 list_del(&p_ptr->wait_list);
274 spin_unlock_bh(&tipc_port_list_lock);
275 k_term_timer(&p_ptr->timer);
276 kfree(p_ptr);
277 tipc_net_route_msg(buf);
278 return 0;
279 }
280
281 static int port_unreliable(struct tipc_port *p_ptr)
282 {
283 return msg_src_droppable(&p_ptr->phdr);
284 }
285
286 int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
287 {
288 struct tipc_port *p_ptr;
289
290 p_ptr = tipc_port_lock(ref);
291 if (!p_ptr)
292 return -EINVAL;
293 *isunreliable = port_unreliable(p_ptr);
294 tipc_port_unlock(p_ptr);
295 return 0;
296 }
297
298 int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
299 {
300 struct tipc_port *p_ptr;
301
302 p_ptr = tipc_port_lock(ref);
303 if (!p_ptr)
304 return -EINVAL;
305 msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
306 tipc_port_unlock(p_ptr);
307 return 0;
308 }
309
310 static int port_unreturnable(struct tipc_port *p_ptr)
311 {
312 return msg_dest_droppable(&p_ptr->phdr);
313 }
314
315 int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
316 {
317 struct tipc_port *p_ptr;
318
319 p_ptr = tipc_port_lock(ref);
320 if (!p_ptr)
321 return -EINVAL;
322 *isunrejectable = port_unreturnable(p_ptr);
323 tipc_port_unlock(p_ptr);
324 return 0;
325 }
326
327 int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
328 {
329 struct tipc_port *p_ptr;
330
331 p_ptr = tipc_port_lock(ref);
332 if (!p_ptr)
333 return -EINVAL;
334 msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
335 tipc_port_unlock(p_ptr);
336 return 0;
337 }
338
339 /*
340 * port_build_proto_msg(): create connection protocol message for port
341 *
342 * On entry the port must be locked and connected.
343 */
344 static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
345 u32 type, u32 ack)
346 {
347 struct sk_buff *buf;
348 struct tipc_msg *msg;
349
350 buf = tipc_buf_acquire(INT_H_SIZE);
351 if (buf) {
352 msg = buf_msg(buf);
353 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
354 port_peernode(p_ptr));
355 msg_set_destport(msg, port_peerport(p_ptr));
356 msg_set_origport(msg, p_ptr->ref);
357 msg_set_msgcnt(msg, ack);
358 }
359 return buf;
360 }
361
362 int tipc_reject_msg(struct sk_buff *buf, u32 err)
363 {
364 struct tipc_msg *msg = buf_msg(buf);
365 struct sk_buff *rbuf;
366 struct tipc_msg *rmsg;
367 int hdr_sz;
368 u32 imp;
369 u32 data_sz = msg_data_sz(msg);
370 u32 src_node;
371 u32 rmsg_sz;
372
373 /* discard rejected message if it shouldn't be returned to sender */
374 if (WARN(!msg_isdata(msg),
375 "attempt to reject message with user=%u", msg_user(msg))) {
376 dump_stack();
377 goto exit;
378 }
379 if (msg_errcode(msg) || msg_dest_droppable(msg))
380 goto exit;
381
382 /*
383 * construct returned message by copying rejected message header and
384 * data (or subset), then updating header fields that need adjusting
385 */
386 hdr_sz = msg_hdr_sz(msg);
387 rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
388
389 rbuf = tipc_buf_acquire(rmsg_sz);
390 if (rbuf == NULL)
391 goto exit;
392
393 rmsg = buf_msg(rbuf);
394 skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
395
396 if (msg_connected(rmsg)) {
397 imp = msg_importance(rmsg);
398 if (imp < TIPC_CRITICAL_IMPORTANCE)
399 msg_set_importance(rmsg, ++imp);
400 }
401 msg_set_non_seq(rmsg, 0);
402 msg_set_size(rmsg, rmsg_sz);
403 msg_set_errcode(rmsg, err);
404 msg_set_prevnode(rmsg, tipc_own_addr);
405 msg_swap_words(rmsg, 4, 5);
406 if (!msg_short(rmsg))
407 msg_swap_words(rmsg, 6, 7);
408
409 /* send self-abort message when rejecting on a connected port */
410 if (msg_connected(msg)) {
411 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
412
413 if (p_ptr) {
414 struct sk_buff *abuf = NULL;
415
416 if (p_ptr->connected)
417 abuf = port_build_self_abort_msg(p_ptr, err);
418 tipc_port_unlock(p_ptr);
419 tipc_net_route_msg(abuf);
420 }
421 }
422
423 /* send returned message & dispose of rejected message */
424 src_node = msg_prevnode(msg);
425 if (in_own_node(src_node))
426 tipc_port_rcv(rbuf);
427 else
428 tipc_link_xmit(rbuf, src_node, msg_link_selector(rmsg));
429 exit:
430 kfree_skb(buf);
431 return data_sz;
432 }
433
434 int tipc_port_iovec_reject(struct tipc_port *p_ptr, struct tipc_msg *hdr,
435 struct iovec const *msg_sect, unsigned int len,
436 int err)
437 {
438 struct sk_buff *buf;
439 int res;
440
441 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
442 if (!buf)
443 return res;
444
445 return tipc_reject_msg(buf, err);
446 }
447
448 static void port_timeout(unsigned long ref)
449 {
450 struct tipc_port *p_ptr = tipc_port_lock(ref);
451 struct sk_buff *buf = NULL;
452
453 if (!p_ptr)
454 return;
455
456 if (!p_ptr->connected) {
457 tipc_port_unlock(p_ptr);
458 return;
459 }
460
461 /* Last probe answered ? */
462 if (p_ptr->probing_state == PROBING) {
463 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
464 } else {
465 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
466 p_ptr->probing_state = PROBING;
467 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
468 }
469 tipc_port_unlock(p_ptr);
470 tipc_net_route_msg(buf);
471 }
472
473
474 static void port_handle_node_down(unsigned long ref)
475 {
476 struct tipc_port *p_ptr = tipc_port_lock(ref);
477 struct sk_buff *buf = NULL;
478
479 if (!p_ptr)
480 return;
481 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
482 tipc_port_unlock(p_ptr);
483 tipc_net_route_msg(buf);
484 }
485
486
487 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
488 {
489 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
490
491 if (buf) {
492 struct tipc_msg *msg = buf_msg(buf);
493 msg_swap_words(msg, 4, 5);
494 msg_swap_words(msg, 6, 7);
495 }
496 return buf;
497 }
498
499
500 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
501 {
502 struct sk_buff *buf;
503 struct tipc_msg *msg;
504 u32 imp;
505
506 if (!p_ptr->connected)
507 return NULL;
508
509 buf = tipc_buf_acquire(BASIC_H_SIZE);
510 if (buf) {
511 msg = buf_msg(buf);
512 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
513 msg_set_hdr_sz(msg, BASIC_H_SIZE);
514 msg_set_size(msg, BASIC_H_SIZE);
515 imp = msg_importance(msg);
516 if (imp < TIPC_CRITICAL_IMPORTANCE)
517 msg_set_importance(msg, ++imp);
518 msg_set_errcode(msg, err);
519 }
520 return buf;
521 }
522
523 void tipc_port_proto_rcv(struct sk_buff *buf)
524 {
525 struct tipc_msg *msg = buf_msg(buf);
526 struct tipc_port *p_ptr;
527 struct sk_buff *r_buf = NULL;
528 u32 destport = msg_destport(msg);
529 int wakeable;
530
531 /* Validate connection */
532 p_ptr = tipc_port_lock(destport);
533 if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
534 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
535 if (r_buf) {
536 msg = buf_msg(r_buf);
537 tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
538 BASIC_H_SIZE, msg_orignode(msg));
539 msg_set_errcode(msg, TIPC_ERR_NO_PORT);
540 msg_set_origport(msg, destport);
541 msg_set_destport(msg, msg_origport(msg));
542 }
543 if (p_ptr)
544 tipc_port_unlock(p_ptr);
545 goto exit;
546 }
547
548 /* Process protocol message sent by peer */
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
574 static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
575 {
576 struct publication *publ;
577 int ret;
578
579 if (full_id)
580 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
581 tipc_zone(tipc_own_addr),
582 tipc_cluster(tipc_own_addr),
583 tipc_node(tipc_own_addr), p_ptr->ref);
584 else
585 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
586
587 if (p_ptr->connected) {
588 u32 dport = port_peerport(p_ptr);
589 u32 destnode = port_peernode(p_ptr);
590
591 ret += tipc_snprintf(buf + ret, len - ret,
592 " connected to <%u.%u.%u:%u>",
593 tipc_zone(destnode),
594 tipc_cluster(destnode),
595 tipc_node(destnode), dport);
596 if (p_ptr->conn_type != 0)
597 ret += tipc_snprintf(buf + ret, len - ret,
598 " via {%u,%u}", p_ptr->conn_type,
599 p_ptr->conn_instance);
600 } else if (p_ptr->published) {
601 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
602 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
603 if (publ->lower == publ->upper)
604 ret += tipc_snprintf(buf + ret, len - ret,
605 " {%u,%u}", publ->type,
606 publ->lower);
607 else
608 ret += tipc_snprintf(buf + ret, len - ret,
609 " {%u,%u,%u}", publ->type,
610 publ->lower, publ->upper);
611 }
612 }
613 ret += tipc_snprintf(buf + ret, len - ret, "\n");
614 return ret;
615 }
616
617 struct sk_buff *tipc_port_get_ports(void)
618 {
619 struct sk_buff *buf;
620 struct tlv_desc *rep_tlv;
621 char *pb;
622 int pb_len;
623 struct tipc_port *p_ptr;
624 int str_len = 0;
625
626 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
627 if (!buf)
628 return NULL;
629 rep_tlv = (struct tlv_desc *)buf->data;
630 pb = TLV_DATA(rep_tlv);
631 pb_len = ULTRA_STRING_MAX_LEN;
632
633 spin_lock_bh(&tipc_port_list_lock);
634 list_for_each_entry(p_ptr, &ports, port_list) {
635 spin_lock_bh(p_ptr->lock);
636 str_len += port_print(p_ptr, pb, pb_len, 0);
637 spin_unlock_bh(p_ptr->lock);
638 }
639 spin_unlock_bh(&tipc_port_list_lock);
640 str_len += 1; /* for "\0" */
641 skb_put(buf, TLV_SPACE(str_len));
642 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
643
644 return buf;
645 }
646
647 void tipc_port_reinit(void)
648 {
649 struct tipc_port *p_ptr;
650 struct tipc_msg *msg;
651
652 spin_lock_bh(&tipc_port_list_lock);
653 list_for_each_entry(p_ptr, &ports, port_list) {
654 msg = &p_ptr->phdr;
655 msg_set_prevnode(msg, tipc_own_addr);
656 msg_set_orignode(msg, tipc_own_addr);
657 }
658 spin_unlock_bh(&tipc_port_list_lock);
659 }
660
661 void tipc_acknowledge(u32 ref, u32 ack)
662 {
663 struct tipc_port *p_ptr;
664 struct sk_buff *buf = NULL;
665
666 p_ptr = tipc_port_lock(ref);
667 if (!p_ptr)
668 return;
669 if (p_ptr->connected) {
670 p_ptr->conn_unacked -= ack;
671 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
672 }
673 tipc_port_unlock(p_ptr);
674 tipc_net_route_msg(buf);
675 }
676
677 int tipc_portimportance(u32 ref, unsigned int *importance)
678 {
679 struct tipc_port *p_ptr;
680
681 p_ptr = tipc_port_lock(ref);
682 if (!p_ptr)
683 return -EINVAL;
684 *importance = (unsigned int)msg_importance(&p_ptr->phdr);
685 tipc_port_unlock(p_ptr);
686 return 0;
687 }
688
689 int tipc_set_portimportance(u32 ref, unsigned int imp)
690 {
691 struct tipc_port *p_ptr;
692
693 if (imp > TIPC_CRITICAL_IMPORTANCE)
694 return -EINVAL;
695
696 p_ptr = tipc_port_lock(ref);
697 if (!p_ptr)
698 return -EINVAL;
699 msg_set_importance(&p_ptr->phdr, (u32)imp);
700 tipc_port_unlock(p_ptr);
701 return 0;
702 }
703
704
705 int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
706 struct tipc_name_seq const *seq)
707 {
708 struct publication *publ;
709 u32 key;
710
711 if (p_ptr->connected)
712 return -EINVAL;
713 key = p_ptr->ref + p_ptr->pub_count + 1;
714 if (key == p_ptr->ref)
715 return -EADDRINUSE;
716
717 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
718 scope, p_ptr->ref, key);
719 if (publ) {
720 list_add(&publ->pport_list, &p_ptr->publications);
721 p_ptr->pub_count++;
722 p_ptr->published = 1;
723 return 0;
724 }
725 return -EINVAL;
726 }
727
728 int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
729 struct tipc_name_seq const *seq)
730 {
731 struct publication *publ;
732 struct publication *tpubl;
733 int res = -EINVAL;
734
735 if (!seq) {
736 list_for_each_entry_safe(publ, tpubl,
737 &p_ptr->publications, pport_list) {
738 tipc_nametbl_withdraw(publ->type, publ->lower,
739 publ->ref, publ->key);
740 }
741 res = 0;
742 } else {
743 list_for_each_entry_safe(publ, tpubl,
744 &p_ptr->publications, pport_list) {
745 if (publ->scope != scope)
746 continue;
747 if (publ->type != seq->type)
748 continue;
749 if (publ->lower != seq->lower)
750 continue;
751 if (publ->upper != seq->upper)
752 break;
753 tipc_nametbl_withdraw(publ->type, publ->lower,
754 publ->ref, publ->key);
755 res = 0;
756 break;
757 }
758 }
759 if (list_empty(&p_ptr->publications))
760 p_ptr->published = 0;
761 return res;
762 }
763
764 int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
765 {
766 struct tipc_port *p_ptr;
767 int res;
768
769 p_ptr = tipc_port_lock(ref);
770 if (!p_ptr)
771 return -EINVAL;
772 res = __tipc_port_connect(ref, p_ptr, peer);
773 tipc_port_unlock(p_ptr);
774 return res;
775 }
776
777 /*
778 * __tipc_port_connect - connect to a remote peer
779 *
780 * Port must be locked.
781 */
782 int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
783 struct tipc_portid const *peer)
784 {
785 struct tipc_msg *msg;
786 int res = -EINVAL;
787
788 if (p_ptr->published || p_ptr->connected)
789 goto exit;
790 if (!peer->ref)
791 goto exit;
792
793 msg = &p_ptr->phdr;
794 msg_set_destnode(msg, peer->node);
795 msg_set_destport(msg, peer->ref);
796 msg_set_type(msg, TIPC_CONN_MSG);
797 msg_set_lookup_scope(msg, 0);
798 msg_set_hdr_sz(msg, SHORT_H_SIZE);
799
800 p_ptr->probing_interval = PROBING_INTERVAL;
801 p_ptr->probing_state = CONFIRMED;
802 p_ptr->connected = 1;
803 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
804
805 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
806 (void *)(unsigned long)ref,
807 (net_ev_handler)port_handle_node_down);
808 res = 0;
809 exit:
810 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
811 return res;
812 }
813
814 /*
815 * __tipc_disconnect - disconnect port from peer
816 *
817 * Port must be locked.
818 */
819 int __tipc_port_disconnect(struct tipc_port *tp_ptr)
820 {
821 if (tp_ptr->connected) {
822 tp_ptr->connected = 0;
823 /* let timer expire on it's own to avoid deadlock! */
824 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
825 return 0;
826 }
827
828 return -ENOTCONN;
829 }
830
831 /*
832 * tipc_port_disconnect(): Disconnect port form peer.
833 * This is a node local operation.
834 */
835 int tipc_port_disconnect(u32 ref)
836 {
837 struct tipc_port *p_ptr;
838 int res;
839
840 p_ptr = tipc_port_lock(ref);
841 if (!p_ptr)
842 return -EINVAL;
843 res = __tipc_port_disconnect(p_ptr);
844 tipc_port_unlock(p_ptr);
845 return res;
846 }
847
848 /*
849 * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
850 */
851 int tipc_port_shutdown(u32 ref)
852 {
853 struct tipc_port *p_ptr;
854 struct sk_buff *buf = NULL;
855
856 p_ptr = tipc_port_lock(ref);
857 if (!p_ptr)
858 return -EINVAL;
859
860 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
861 tipc_port_unlock(p_ptr);
862 tipc_net_route_msg(buf);
863 return tipc_port_disconnect(ref);
864 }
865
866 /**
867 * tipc_port_rcv - receive message from lower layer and deliver to port user
868 */
869 int tipc_port_rcv(struct sk_buff *buf)
870 {
871 struct tipc_port *p_ptr;
872 struct tipc_msg *msg = buf_msg(buf);
873 u32 destport = msg_destport(msg);
874 u32 dsz = msg_data_sz(msg);
875 u32 err;
876
877 /* forward unresolved named message */
878 if (unlikely(!destport)) {
879 tipc_net_route_msg(buf);
880 return dsz;
881 }
882
883 /* validate destination & pass to port, otherwise reject message */
884 p_ptr = tipc_port_lock(destport);
885 if (likely(p_ptr)) {
886 err = p_ptr->dispatcher(p_ptr, buf);
887 tipc_port_unlock(p_ptr);
888 if (likely(!err))
889 return dsz;
890 } else {
891 err = TIPC_ERR_NO_PORT;
892 }
893
894 return tipc_reject_msg(buf, err);
895 }
896
897 /*
898 * tipc_port_iovec_rcv: Concatenate and deliver sectioned
899 * message for this node.
900 */
901 static int tipc_port_iovec_rcv(struct tipc_port *sender,
902 struct iovec const *msg_sect,
903 unsigned int len)
904 {
905 struct sk_buff *buf;
906 int res;
907
908 res = tipc_msg_build(&sender->phdr, msg_sect, len, MAX_MSG_SIZE, &buf);
909 if (likely(buf))
910 tipc_port_rcv(buf);
911 return res;
912 }
913
914 /**
915 * tipc_send - send message sections on connection
916 */
917 int tipc_send(u32 ref, struct iovec const *msg_sect, unsigned int len)
918 {
919 struct tipc_port *p_ptr;
920 u32 destnode;
921 int res;
922
923 p_ptr = tipc_port_deref(ref);
924 if (!p_ptr || !p_ptr->connected)
925 return -EINVAL;
926
927 p_ptr->congested = 1;
928 if (!tipc_port_congested(p_ptr)) {
929 destnode = port_peernode(p_ptr);
930 if (likely(!in_own_node(destnode)))
931 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
932 destnode);
933 else
934 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
935
936 if (likely(res != -ELINKCONG)) {
937 p_ptr->congested = 0;
938 if (res > 0)
939 p_ptr->sent++;
940 return res;
941 }
942 }
943 if (port_unreliable(p_ptr)) {
944 p_ptr->congested = 0;
945 return len;
946 }
947 return -ELINKCONG;
948 }
949
950 /**
951 * tipc_send2name - send message sections to port name
952 */
953 int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
954 struct iovec const *msg_sect, unsigned int len)
955 {
956 struct tipc_port *p_ptr;
957 struct tipc_msg *msg;
958 u32 destnode = domain;
959 u32 destport;
960 int res;
961
962 p_ptr = tipc_port_deref(ref);
963 if (!p_ptr || p_ptr->connected)
964 return -EINVAL;
965
966 msg = &p_ptr->phdr;
967 msg_set_type(msg, TIPC_NAMED_MSG);
968 msg_set_hdr_sz(msg, NAMED_H_SIZE);
969 msg_set_nametype(msg, name->type);
970 msg_set_nameinst(msg, name->instance);
971 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
972 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
973 msg_set_destnode(msg, destnode);
974 msg_set_destport(msg, destport);
975
976 if (likely(destport || destnode)) {
977 if (likely(in_own_node(destnode)))
978 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
979 else if (tipc_own_addr)
980 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
981 destnode);
982 else
983 res = tipc_port_iovec_reject(p_ptr, msg, msg_sect,
984 len, TIPC_ERR_NO_NODE);
985 if (likely(res != -ELINKCONG)) {
986 if (res > 0)
987 p_ptr->sent++;
988 return res;
989 }
990 if (port_unreliable(p_ptr)) {
991 return len;
992 }
993 return -ELINKCONG;
994 }
995 return tipc_port_iovec_reject(p_ptr, msg, msg_sect, len,
996 TIPC_ERR_NO_NAME);
997 }
998
999 /**
1000 * tipc_send2port - send message sections to port identity
1001 */
1002 int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1003 struct iovec const *msg_sect, unsigned int len)
1004 {
1005 struct tipc_port *p_ptr;
1006 struct tipc_msg *msg;
1007 int res;
1008
1009 p_ptr = tipc_port_deref(ref);
1010 if (!p_ptr || p_ptr->connected)
1011 return -EINVAL;
1012
1013 msg = &p_ptr->phdr;
1014 msg_set_type(msg, TIPC_DIRECT_MSG);
1015 msg_set_lookup_scope(msg, 0);
1016 msg_set_destnode(msg, dest->node);
1017 msg_set_destport(msg, dest->ref);
1018 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1019
1020 if (in_own_node(dest->node))
1021 res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
1022 else if (tipc_own_addr)
1023 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
1024 dest->node);
1025 else
1026 res = tipc_port_iovec_reject(p_ptr, msg, msg_sect, len,
1027 TIPC_ERR_NO_NODE);
1028 if (likely(res != -ELINKCONG)) {
1029 if (res > 0)
1030 p_ptr->sent++;
1031 return res;
1032 }
1033 if (port_unreliable(p_ptr)) {
1034 return len;
1035 }
1036 return -ELINKCONG;
1037 }