a029cdc2df6d0663bcefa7c8a693d35f6ad819a1
[linux-3.10.git] / net / tipc / msg.c
1 /*
2  * net/tipc/msg.c: TIPC message header routines
3  *
4  * Copyright (c) 2000-2006, Ericsson AB
5  * Copyright (c) 2005, 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 "addr.h"
39 #include "msg.h"
40
41 u32 tipc_msg_tot_importance(struct tipc_msg *m)
42 {
43         if (likely(msg_isdata(m))) {
44                 if (likely(msg_orignode(m) == tipc_own_addr))
45                         return msg_importance(m);
46                 return msg_importance(m) + 4;
47         }
48         if ((msg_user(m) == MSG_FRAGMENTER)  &&
49             (msg_type(m) == FIRST_FRAGMENT))
50                 return msg_importance(msg_get_wrapped(m));
51         return msg_importance(m);
52 }
53
54
55 void tipc_msg_init(struct tipc_msg *m, u32 user, u32 type,
56                             u32 hsize, u32 destnode)
57 {
58         memset(m, 0, hsize);
59         msg_set_version(m);
60         msg_set_user(m, user);
61         msg_set_hdr_sz(m, hsize);
62         msg_set_size(m, hsize);
63         msg_set_prevnode(m, tipc_own_addr);
64         msg_set_type(m, type);
65         if (!msg_short(m)) {
66                 msg_set_orignode(m, tipc_own_addr);
67                 msg_set_destnode(m, destnode);
68         }
69 }
70
71 /**
72  * tipc_msg_calc_data_size - determine total data size for message
73  */
74
75 int tipc_msg_calc_data_size(struct iovec const *msg_sect, u32 num_sect)
76 {
77         int dsz = 0;
78         int i;
79
80         for (i = 0; i < num_sect; i++)
81                 dsz += msg_sect[i].iov_len;
82         return dsz;
83 }
84
85 /**
86  * tipc_msg_build - create message using specified header and data
87  *
88  * Note: Caller must not hold any locks in case copy_from_user() is interrupted!
89  *
90  * Returns message data size or errno
91  */
92
93 int tipc_msg_build(struct tipc_msg *hdr,
94                             struct iovec const *msg_sect, u32 num_sect,
95                             int max_size, int usrmem, struct sk_buff** buf)
96 {
97         int dsz, sz, hsz, pos, res, cnt;
98
99         dsz = tipc_msg_calc_data_size(msg_sect, num_sect);
100         if (unlikely(dsz > TIPC_MAX_USER_MSG_SIZE)) {
101                 *buf = NULL;
102                 return -EINVAL;
103         }
104
105         pos = hsz = msg_hdr_sz(hdr);
106         sz = hsz + dsz;
107         msg_set_size(hdr, sz);
108         if (unlikely(sz > max_size)) {
109                 *buf = NULL;
110                 return dsz;
111         }
112
113         *buf = tipc_buf_acquire(sz);
114         if (!(*buf))
115                 return -ENOMEM;
116         skb_copy_to_linear_data(*buf, hdr, hsz);
117         for (res = 1, cnt = 0; res && (cnt < num_sect); cnt++) {
118                 if (likely(usrmem))
119                         res = !copy_from_user((*buf)->data + pos,
120                                               msg_sect[cnt].iov_base,
121                                               msg_sect[cnt].iov_len);
122                 else
123                         skb_copy_to_linear_data_offset(*buf, pos,
124                                                        msg_sect[cnt].iov_base,
125                                                        msg_sect[cnt].iov_len);
126                 pos += msg_sect[cnt].iov_len;
127         }
128         if (likely(res))
129                 return dsz;
130
131         buf_discard(*buf);
132         *buf = NULL;
133         return -EFAULT;
134 }
135
136 #ifdef CONFIG_TIPC_DEBUG
137
138 void tipc_msg_dbg(struct print_buf *buf, struct tipc_msg *msg, const char *str)
139 {
140         u32 usr = msg_user(msg);
141         tipc_printf(buf, KERN_DEBUG);
142         tipc_printf(buf, str);
143
144         switch (usr) {
145         case MSG_BUNDLER:
146                 tipc_printf(buf, "BNDL::");
147                 tipc_printf(buf, "MSGS(%u):", msg_msgcnt(msg));
148                 break;
149         case BCAST_PROTOCOL:
150                 tipc_printf(buf, "BCASTP::");
151                 break;
152         case MSG_FRAGMENTER:
153                 tipc_printf(buf, "FRAGM::");
154                 switch (msg_type(msg)) {
155                 case FIRST_FRAGMENT:
156                         tipc_printf(buf, "FIRST:");
157                         break;
158                 case FRAGMENT:
159                         tipc_printf(buf, "BODY:");
160                         break;
161                 case LAST_FRAGMENT:
162                         tipc_printf(buf, "LAST:");
163                         break;
164                 default:
165                         tipc_printf(buf, "UNKNOWN:%x",msg_type(msg));
166
167                 }
168                 tipc_printf(buf, "NO(%u/%u):",msg_long_msgno(msg),
169                             msg_fragm_no(msg));
170                 break;
171         case TIPC_LOW_IMPORTANCE:
172         case TIPC_MEDIUM_IMPORTANCE:
173         case TIPC_HIGH_IMPORTANCE:
174         case TIPC_CRITICAL_IMPORTANCE:
175                 tipc_printf(buf, "DAT%u:", msg_user(msg));
176                 if (msg_short(msg)) {
177                         tipc_printf(buf, "CON:");
178                         break;
179                 }
180                 switch (msg_type(msg)) {
181                 case TIPC_CONN_MSG:
182                         tipc_printf(buf, "CON:");
183                         break;
184                 case TIPC_MCAST_MSG:
185                         tipc_printf(buf, "MCST:");
186                         break;
187                 case TIPC_NAMED_MSG:
188                         tipc_printf(buf, "NAM:");
189                         break;
190                 case TIPC_DIRECT_MSG:
191                         tipc_printf(buf, "DIR:");
192                         break;
193                 default:
194                         tipc_printf(buf, "UNKNOWN TYPE %u",msg_type(msg));
195                 }
196                 if (msg_routed(msg) && !msg_non_seq(msg))
197                         tipc_printf(buf, "ROUT:");
198                 if (msg_reroute_cnt(msg))
199                         tipc_printf(buf, "REROUTED(%u):",
200                                     msg_reroute_cnt(msg));
201                 break;
202         case NAME_DISTRIBUTOR:
203                 tipc_printf(buf, "NMD::");
204                 switch (msg_type(msg)) {
205                 case PUBLICATION:
206                         tipc_printf(buf, "PUBL(%u):", (msg_size(msg) - msg_hdr_sz(msg)) / 20);  /* Items */
207                         break;
208                 case WITHDRAWAL:
209                         tipc_printf(buf, "WDRW:");
210                         break;
211                 default:
212                         tipc_printf(buf, "UNKNOWN:%x",msg_type(msg));
213                 }
214                 if (msg_routed(msg))
215                         tipc_printf(buf, "ROUT:");
216                 if (msg_reroute_cnt(msg))
217                         tipc_printf(buf, "REROUTED(%u):",
218                                     msg_reroute_cnt(msg));
219                 break;
220         case CONN_MANAGER:
221                 tipc_printf(buf, "CONN_MNG:");
222                 switch (msg_type(msg)) {
223                 case CONN_PROBE:
224                         tipc_printf(buf, "PROBE:");
225                         break;
226                 case CONN_PROBE_REPLY:
227                         tipc_printf(buf, "PROBE_REPLY:");
228                         break;
229                 case CONN_ACK:
230                         tipc_printf(buf, "CONN_ACK:");
231                         tipc_printf(buf, "ACK(%u):",msg_msgcnt(msg));
232                         break;
233                 default:
234                         tipc_printf(buf, "UNKNOWN TYPE:%x",msg_type(msg));
235                 }
236                 if (msg_routed(msg))
237                         tipc_printf(buf, "ROUT:");
238                 if (msg_reroute_cnt(msg))
239                         tipc_printf(buf, "REROUTED(%u):",msg_reroute_cnt(msg));
240                 break;
241         case LINK_PROTOCOL:
242                 tipc_printf(buf, "PROT:TIM(%u):",msg_timestamp(msg));
243                 switch (msg_type(msg)) {
244                 case STATE_MSG:
245                         tipc_printf(buf, "STATE:");
246                         tipc_printf(buf, "%s:",msg_probe(msg) ? "PRB" :"");
247                         tipc_printf(buf, "NXS(%u):",msg_next_sent(msg));
248                         tipc_printf(buf, "GAP(%u):",msg_seq_gap(msg));
249                         tipc_printf(buf, "LSTBC(%u):",msg_last_bcast(msg));
250                         break;
251                 case RESET_MSG:
252                         tipc_printf(buf, "RESET:");
253                         if (msg_size(msg) != msg_hdr_sz(msg))
254                                 tipc_printf(buf, "BEAR:%s:",msg_data(msg));
255                         break;
256                 case ACTIVATE_MSG:
257                         tipc_printf(buf, "ACTIVATE:");
258                         break;
259                 default:
260                         tipc_printf(buf, "UNKNOWN TYPE:%x",msg_type(msg));
261                 }
262                 tipc_printf(buf, "PLANE(%c):",msg_net_plane(msg));
263                 tipc_printf(buf, "SESS(%u):",msg_session(msg));
264                 break;
265         case CHANGEOVER_PROTOCOL:
266                 tipc_printf(buf, "TUNL:");
267                 switch (msg_type(msg)) {
268                 case DUPLICATE_MSG:
269                         tipc_printf(buf, "DUPL:");
270                         break;
271                 case ORIGINAL_MSG:
272                         tipc_printf(buf, "ORIG:");
273                         tipc_printf(buf, "EXP(%u)",msg_msgcnt(msg));
274                         break;
275                 default:
276                         tipc_printf(buf, "UNKNOWN TYPE:%x",msg_type(msg));
277                 }
278                 break;
279         case ROUTE_DISTRIBUTOR:
280                 tipc_printf(buf, "ROUTING_MNG:");
281                 switch (msg_type(msg)) {
282                 case EXT_ROUTING_TABLE:
283                         tipc_printf(buf, "EXT_TBL:");
284                         tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
285                         break;
286                 case LOCAL_ROUTING_TABLE:
287                         tipc_printf(buf, "LOCAL_TBL:");
288                         tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
289                         break;
290                 case SLAVE_ROUTING_TABLE:
291                         tipc_printf(buf, "DP_TBL:");
292                         tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
293                         break;
294                 case ROUTE_ADDITION:
295                         tipc_printf(buf, "ADD:");
296                         tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
297                         break;
298                 case ROUTE_REMOVAL:
299                         tipc_printf(buf, "REMOVE:");
300                         tipc_printf(buf, "TO:%x:",msg_remote_node(msg));
301                         break;
302                 default:
303                         tipc_printf(buf, "UNKNOWN TYPE:%x",msg_type(msg));
304                 }
305                 break;
306         case LINK_CONFIG:
307                 tipc_printf(buf, "CFG:");
308                 switch (msg_type(msg)) {
309                 case DSC_REQ_MSG:
310                         tipc_printf(buf, "DSC_REQ:");
311                         break;
312                 case DSC_RESP_MSG:
313                         tipc_printf(buf, "DSC_RESP:");
314                         break;
315                 default:
316                         tipc_printf(buf, "UNKNOWN TYPE:%x:",msg_type(msg));
317                         break;
318                 }
319                 break;
320         default:
321                 tipc_printf(buf, "UNKNOWN USER:");
322         }
323
324         switch (usr) {
325         case CONN_MANAGER:
326         case TIPC_LOW_IMPORTANCE:
327         case TIPC_MEDIUM_IMPORTANCE:
328         case TIPC_HIGH_IMPORTANCE:
329         case TIPC_CRITICAL_IMPORTANCE:
330                 switch (msg_errcode(msg)) {
331                 case TIPC_OK:
332                         break;
333                 case TIPC_ERR_NO_NAME:
334                         tipc_printf(buf, "NO_NAME:");
335                         break;
336                 case TIPC_ERR_NO_PORT:
337                         tipc_printf(buf, "NO_PORT:");
338                         break;
339                 case TIPC_ERR_NO_NODE:
340                         tipc_printf(buf, "NO_PROC:");
341                         break;
342                 case TIPC_ERR_OVERLOAD:
343                         tipc_printf(buf, "OVERLOAD:");
344                         break;
345                 case TIPC_CONN_SHUTDOWN:
346                         tipc_printf(buf, "SHUTDOWN:");
347                         break;
348                 default:
349                         tipc_printf(buf, "UNKNOWN ERROR(%x):",
350                                     msg_errcode(msg));
351                 }
352         default:{}
353         }
354
355         tipc_printf(buf, "HZ(%u):", msg_hdr_sz(msg));
356         tipc_printf(buf, "SZ(%u):", msg_size(msg));
357         tipc_printf(buf, "SQNO(%u):", msg_seqno(msg));
358
359         if (msg_non_seq(msg))
360                 tipc_printf(buf, "NOSEQ:");
361         else {
362                 tipc_printf(buf, "ACK(%u):", msg_ack(msg));
363         }
364         tipc_printf(buf, "BACK(%u):", msg_bcast_ack(msg));
365         tipc_printf(buf, "PRND(%x)", msg_prevnode(msg));
366
367         if (msg_isdata(msg)) {
368                 if (msg_named(msg)) {
369                         tipc_printf(buf, "NTYP(%u):", msg_nametype(msg));
370                         tipc_printf(buf, "NINST(%u)", msg_nameinst(msg));
371                 }
372         }
373
374         if ((usr != LINK_PROTOCOL) && (usr != LINK_CONFIG) &&
375             (usr != MSG_BUNDLER)) {
376                 if (!msg_short(msg)) {
377                         tipc_printf(buf, ":ORIG(%x:%u):",
378                                     msg_orignode(msg), msg_origport(msg));
379                         tipc_printf(buf, ":DEST(%x:%u):",
380                                     msg_destnode(msg), msg_destport(msg));
381                 } else {
382                         tipc_printf(buf, ":OPRT(%u):", msg_origport(msg));
383                         tipc_printf(buf, ":DPRT(%u):", msg_destport(msg));
384                 }
385                 if (msg_routed(msg) && !msg_non_seq(msg))
386                         tipc_printf(buf, ":TSEQN(%u)", msg_transp_seqno(msg));
387         }
388         if (msg_user(msg) == NAME_DISTRIBUTOR) {
389                 tipc_printf(buf, ":ONOD(%x):", msg_orignode(msg));
390                 tipc_printf(buf, ":DNOD(%x):", msg_destnode(msg));
391                 if (msg_routed(msg)) {
392                         tipc_printf(buf, ":CSEQN(%u)", msg_transp_seqno(msg));
393                 }
394         }
395
396         if (msg_user(msg) ==  LINK_CONFIG) {
397                 u32* raw = (u32*)msg;
398                 struct tipc_media_addr* orig = (struct tipc_media_addr*)&raw[5];
399                 tipc_printf(buf, ":REQL(%u):", msg_req_links(msg));
400                 tipc_printf(buf, ":DDOM(%x):", msg_dest_domain(msg));
401                 tipc_printf(buf, ":NETID(%u):", msg_bc_netid(msg));
402                 tipc_media_addr_printf(buf, orig);
403         }
404         if (msg_user(msg) == BCAST_PROTOCOL) {
405                 tipc_printf(buf, "BCNACK:AFTER(%u):", msg_bcgap_after(msg));
406                 tipc_printf(buf, "TO(%u):", msg_bcgap_to(msg));
407         }
408         tipc_printf(buf, "\n");
409         if ((usr == CHANGEOVER_PROTOCOL) && (msg_msgcnt(msg))) {
410                 tipc_msg_dbg(buf, msg_get_wrapped(msg), "      /");
411         }
412         if ((usr == MSG_FRAGMENTER) && (msg_type(msg) == FIRST_FRAGMENT)) {
413                 tipc_msg_dbg(buf, msg_get_wrapped(msg), "      /");
414         }
415 }
416
417 #endif