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