1 /* 2 * net/tipc/msg.c: TIPC message header routines 3 * 4 * Copyright (c) 2000-2006, Ericsson AB 5 * Copyright (c) 2005, 2010-2011, Wind River Systems 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the names of the copyright holders nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * Alternatively, this software may be distributed under the terms of the 21 * GNU General Public License ("GPL") version 2 as published by the Free 22 * Software Foundation. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 #include "core.h" 38 #include "msg.h" 39 40 u32 tipc_msg_tot_importance(struct tipc_msg *m) 41 { 42 if (likely(msg_isdata(m))) { 43 if (likely(msg_orignode(m) == tipc_own_addr)) 44 return msg_importance(m); 45 return msg_importance(m) + 4; 46 } 47 if ((msg_user(m) == MSG_FRAGMENTER) && 48 (msg_type(m) == FIRST_FRAGMENT)) 49 return msg_importance(msg_get_wrapped(m)); 50 return msg_importance(m); 51 } 52 53 54 void tipc_msg_init(struct tipc_msg *m, u32 user, u32 type, 55 u32 hsize, u32 destnode) 56 { 57 memset(m, 0, hsize); 58 msg_set_version(m); 59 msg_set_user(m, user); 60 msg_set_hdr_sz(m, hsize); 61 msg_set_size(m, hsize); 62 msg_set_prevnode(m, tipc_own_addr); 63 msg_set_type(m, type); 64 if (!msg_short(m)) { 65 msg_set_orignode(m, tipc_own_addr); 66 msg_set_destnode(m, destnode); 67 } 68 } 69 70 /** 71 * tipc_msg_build - create message using specified header and data 72 * 73 * Note: Caller must not hold any locks in case copy_from_user() is interrupted! 74 * 75 * Returns message data size or errno 76 */ 77 78 int tipc_msg_build(struct tipc_msg *hdr, struct iovec const *msg_sect, 79 u32 num_sect, unsigned int total_len, 80 int max_size, int usrmem, struct sk_buff **buf) 81 { 82 int dsz, sz, hsz, pos, res, cnt; 83 84 dsz = total_len; 85 pos = hsz = msg_hdr_sz(hdr); 86 sz = hsz + dsz; 87 msg_set_size(hdr, sz); 88 if (unlikely(sz > max_size)) { 89 *buf = NULL; 90 return dsz; 91 } 92 93 *buf = tipc_buf_acquire(sz); 94 if (!(*buf)) 95 return -ENOMEM; 96 skb_copy_to_linear_data(*buf, hdr, hsz); 97 for (res = 1, cnt = 0; res && (cnt < num_sect); cnt++) { 98 if (likely(usrmem)) 99 res = !copy_from_user((*buf)->data + pos, 100 msg_sect[cnt].iov_base, 101 msg_sect[cnt].iov_len); 102 else 103 skb_copy_to_linear_data_offset(*buf, pos, 104 msg_sect[cnt].iov_base, 105 msg_sect[cnt].iov_len); 106 pos += msg_sect[cnt].iov_len; 107 } 108 if (likely(res)) 109 return dsz; 110 111 buf_discard(*buf); 112 *buf = NULL; 113 return -EFAULT; 114 } 115 116 #ifdef CONFIG_TIPC_DEBUG 117 118 void tipc_msg_dbg(struct print_buf *buf, struct tipc_msg *msg, const char *str) 119 { 120 u32 usr = msg_user(msg); 121 tipc_printf(buf, KERN_DEBUG); 122 tipc_printf(buf, str); 123 124 switch (usr) { 125 case MSG_BUNDLER: 126 tipc_printf(buf, "BNDL::"); 127 tipc_printf(buf, "MSGS(%u):", msg_msgcnt(msg)); 128 break; 129 case BCAST_PROTOCOL: 130 tipc_printf(buf, "BCASTP::"); 131 break; 132 case MSG_FRAGMENTER: 133 tipc_printf(buf, "FRAGM::"); 134 switch (msg_type(msg)) { 135 case FIRST_FRAGMENT: 136 tipc_printf(buf, "FIRST:"); 137 break; 138 case FRAGMENT: 139 tipc_printf(buf, "BODY:"); 140 break; 141 case LAST_FRAGMENT: 142 tipc_printf(buf, "LAST:"); 143 break; 144 default: 145 tipc_printf(buf, "UNKNOWN:%x", msg_type(msg)); 146 147 } 148 tipc_printf(buf, "NO(%u/%u):", msg_long_msgno(msg), 149 msg_fragm_no(msg)); 150 break; 151 case TIPC_LOW_IMPORTANCE: 152 case TIPC_MEDIUM_IMPORTANCE: 153 case TIPC_HIGH_IMPORTANCE: 154 case TIPC_CRITICAL_IMPORTANCE: 155 tipc_printf(buf, "DAT%u:", msg_user(msg)); 156 if (msg_short(msg)) { 157 tipc_printf(buf, "CON:"); 158 break; 159 } 160 switch (msg_type(msg)) { 161 case TIPC_CONN_MSG: 162 tipc_printf(buf, "CON:"); 163 break; 164 case TIPC_MCAST_MSG: 165 tipc_printf(buf, "MCST:"); 166 break; 167 case TIPC_NAMED_MSG: 168 tipc_printf(buf, "NAM:"); 169 break; 170 case TIPC_DIRECT_MSG: 171 tipc_printf(buf, "DIR:"); 172 break; 173 default: 174 tipc_printf(buf, "UNKNOWN TYPE %u", msg_type(msg)); 175 } 176 if (msg_reroute_cnt(msg)) 177 tipc_printf(buf, "REROUTED(%u):", 178 msg_reroute_cnt(msg)); 179 break; 180 case NAME_DISTRIBUTOR: 181 tipc_printf(buf, "NMD::"); 182 switch (msg_type(msg)) { 183 case PUBLICATION: 184 tipc_printf(buf, "PUBL(%u):", (msg_size(msg) - msg_hdr_sz(msg)) / 20); /* Items */ 185 break; 186 case WITHDRAWAL: 187 tipc_printf(buf, "WDRW:"); 188 break; 189 default: 190 tipc_printf(buf, "UNKNOWN:%x", msg_type(msg)); 191 } 192 if (msg_reroute_cnt(msg)) 193 tipc_printf(buf, "REROUTED(%u):", 194 msg_reroute_cnt(msg)); 195 break; 196 case CONN_MANAGER: 197 tipc_printf(buf, "CONN_MNG:"); 198 switch (msg_type(msg)) { 199 case CONN_PROBE: 200 tipc_printf(buf, "PROBE:"); 201 break; 202 case CONN_PROBE_REPLY: 203 tipc_printf(buf, "PROBE_REPLY:"); 204 break; 205 case CONN_ACK: 206 tipc_printf(buf, "CONN_ACK:"); 207 tipc_printf(buf, "ACK(%u):", msg_msgcnt(msg)); 208 break; 209 default: 210 tipc_printf(buf, "UNKNOWN TYPE:%x", msg_type(msg)); 211 } 212 if (msg_reroute_cnt(msg)) 213 tipc_printf(buf, "REROUTED(%u):", msg_reroute_cnt(msg)); 214 break; 215 case LINK_PROTOCOL: 216 switch (msg_type(msg)) { 217 case STATE_MSG: 218 tipc_printf(buf, "STATE:"); 219 tipc_printf(buf, "%s:", msg_probe(msg) ? "PRB" : ""); 220 tipc_printf(buf, "NXS(%u):", msg_next_sent(msg)); 221 tipc_printf(buf, "GAP(%u):", msg_seq_gap(msg)); 222 tipc_printf(buf, "LSTBC(%u):", msg_last_bcast(msg)); 223 break; 224 case RESET_MSG: 225 tipc_printf(buf, "RESET:"); 226 if (msg_size(msg) != msg_hdr_sz(msg)) 227 tipc_printf(buf, "BEAR:%s:", msg_data(msg)); 228 break; 229 case ACTIVATE_MSG: 230 tipc_printf(buf, "ACTIVATE:"); 231 break; 232 default: 233 tipc_printf(buf, "UNKNOWN TYPE:%x", msg_type(msg)); 234 } 235 tipc_printf(buf, "PLANE(%c):", msg_net_plane(msg)); 236 tipc_printf(buf, "SESS(%u):", msg_session(msg)); 237 break; 238 case CHANGEOVER_PROTOCOL: 239 tipc_printf(buf, "TUNL:"); 240 switch (msg_type(msg)) { 241 case DUPLICATE_MSG: 242 tipc_printf(buf, "DUPL:"); 243 break; 244 case ORIGINAL_MSG: 245 tipc_printf(buf, "ORIG:"); 246 tipc_printf(buf, "EXP(%u)", msg_msgcnt(msg)); 247 break; 248 default: 249 tipc_printf(buf, "UNKNOWN TYPE:%x", msg_type(msg)); 250 } 251 break; 252 case LINK_CONFIG: 253 tipc_printf(buf, "CFG:"); 254 switch (msg_type(msg)) { 255 case DSC_REQ_MSG: 256 tipc_printf(buf, "DSC_REQ:"); 257 break; 258 case DSC_RESP_MSG: 259 tipc_printf(buf, "DSC_RESP:"); 260 break; 261 default: 262 tipc_printf(buf, "UNKNOWN TYPE:%x:", msg_type(msg)); 263 break; 264 } 265 break; 266 default: 267 tipc_printf(buf, "UNKNOWN USER:"); 268 } 269 270 switch (usr) { 271 case CONN_MANAGER: 272 case TIPC_LOW_IMPORTANCE: 273 case TIPC_MEDIUM_IMPORTANCE: 274 case TIPC_HIGH_IMPORTANCE: 275 case TIPC_CRITICAL_IMPORTANCE: 276 switch (msg_errcode(msg)) { 277 case TIPC_OK: 278 break; 279 case TIPC_ERR_NO_NAME: 280 tipc_printf(buf, "NO_NAME:"); 281 break; 282 case TIPC_ERR_NO_PORT: 283 tipc_printf(buf, "NO_PORT:"); 284 break; 285 case TIPC_ERR_NO_NODE: 286 tipc_printf(buf, "NO_PROC:"); 287 break; 288 case TIPC_ERR_OVERLOAD: 289 tipc_printf(buf, "OVERLOAD:"); 290 break; 291 case TIPC_CONN_SHUTDOWN: 292 tipc_printf(buf, "SHUTDOWN:"); 293 break; 294 default: 295 tipc_printf(buf, "UNKNOWN ERROR(%x):", 296 msg_errcode(msg)); 297 } 298 default: 299 break; 300 } 301 302 tipc_printf(buf, "HZ(%u):", msg_hdr_sz(msg)); 303 tipc_printf(buf, "SZ(%u):", msg_size(msg)); 304 tipc_printf(buf, "SQNO(%u):", msg_seqno(msg)); 305 306 if (msg_non_seq(msg)) 307 tipc_printf(buf, "NOSEQ:"); 308 else 309 tipc_printf(buf, "ACK(%u):", msg_ack(msg)); 310 tipc_printf(buf, "BACK(%u):", msg_bcast_ack(msg)); 311 tipc_printf(buf, "PRND(%x)", msg_prevnode(msg)); 312 313 if (msg_isdata(msg)) { 314 if (msg_named(msg)) { 315 tipc_printf(buf, "NTYP(%u):", msg_nametype(msg)); 316 tipc_printf(buf, "NINST(%u)", msg_nameinst(msg)); 317 } 318 } 319 320 if ((usr != LINK_PROTOCOL) && (usr != LINK_CONFIG) && 321 (usr != MSG_BUNDLER)) { 322 if (!msg_short(msg)) { 323 tipc_printf(buf, ":ORIG(%x:%u):", 324 msg_orignode(msg), msg_origport(msg)); 325 tipc_printf(buf, ":DEST(%x:%u):", 326 msg_destnode(msg), msg_destport(msg)); 327 } else { 328 tipc_printf(buf, ":OPRT(%u):", msg_origport(msg)); 329 tipc_printf(buf, ":DPRT(%u):", msg_destport(msg)); 330 } 331 } 332 if (msg_user(msg) == NAME_DISTRIBUTOR) { 333 tipc_printf(buf, ":ONOD(%x):", msg_orignode(msg)); 334 tipc_printf(buf, ":DNOD(%x):", msg_destnode(msg)); 335 } 336 337 if (msg_user(msg) == LINK_CONFIG) { 338 u32 *raw = (u32 *)msg; 339 struct tipc_media_addr *orig = (struct tipc_media_addr *)&raw[5]; 340 tipc_printf(buf, ":DDOM(%x):", msg_dest_domain(msg)); 341 tipc_printf(buf, ":NETID(%u):", msg_bc_netid(msg)); 342 tipc_media_addr_printf(buf, orig); 343 } 344 if (msg_user(msg) == BCAST_PROTOCOL) { 345 tipc_printf(buf, "BCNACK:AFTER(%u):", msg_bcgap_after(msg)); 346 tipc_printf(buf, "TO(%u):", msg_bcgap_to(msg)); 347 } 348 tipc_printf(buf, "\n"); 349 if ((usr == CHANGEOVER_PROTOCOL) && (msg_msgcnt(msg))) 350 tipc_msg_dbg(buf, msg_get_wrapped(msg), " /"); 351 if ((usr == MSG_FRAGMENTER) && (msg_type(msg) == FIRST_FRAGMENT)) 352 tipc_msg_dbg(buf, msg_get_wrapped(msg), " /"); 353 } 354 355 #endif 356