[TIPC]: Minor cleanup of message header code
[safe/jmp/linux-2.6] / net / tipc / msg.h
1 /*
2  * net/tipc/msg.h: Include file for TIPC message header routines
3  *
4  * Copyright (c) 2000-2007, Ericsson AB
5  * Copyright (c) 2005-2007, 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 #ifndef _TIPC_MSG_H
38 #define _TIPC_MSG_H
39
40 #include "core.h"
41
42 #define TIPC_VERSION              2
43
44 #define SHORT_H_SIZE              24    /* Connected, in-cluster messages */
45 #define DIR_MSG_H_SIZE            32    /* Directly addressed messages */
46 #define LONG_H_SIZE               40    /* Named messages */
47 #define MCAST_H_SIZE              44    /* Multicast messages */
48 #define INT_H_SIZE                40    /* Internal messages */
49 #define MIN_H_SIZE                24    /* Smallest legal TIPC header size */
50 #define MAX_H_SIZE                60    /* Largest possible TIPC header size */
51
52 #define MAX_MSG_SIZE (MAX_H_SIZE + TIPC_MAX_USER_MSG_SIZE)
53
54
55 /*
56                 TIPC user data message header format, version 2
57
58         - Fundamental definitions available to privileged TIPC users
59           are located in tipc_msg.h.
60         - Remaining definitions available to TIPC internal users appear below.
61 */
62
63
64 static inline void msg_set_word(struct tipc_msg *m, u32 w, u32 val)
65 {
66         m->hdr[w] = htonl(val);
67 }
68
69 static inline void msg_set_bits(struct tipc_msg *m, u32 w,
70                                 u32 pos, u32 mask, u32 val)
71 {
72         val = (val & mask) << pos;
73         m->hdr[w] &= ~htonl(mask << pos);
74         m->hdr[w] |= htonl(val);
75 }
76
77 /*
78  * Word 0
79  */
80
81 static inline u32 msg_version(struct tipc_msg *m)
82 {
83         return msg_bits(m, 0, 29, 7);
84 }
85
86 static inline void msg_set_version(struct tipc_msg *m)
87 {
88         msg_set_bits(m, 0, 29, 0xf, TIPC_VERSION);
89 }
90
91 static inline u32 msg_user(struct tipc_msg *m)
92 {
93         return msg_bits(m, 0, 25, 0xf);
94 }
95
96 static inline u32 msg_isdata(struct tipc_msg *m)
97 {
98         return (msg_user(m) <= TIPC_CRITICAL_IMPORTANCE);
99 }
100
101 static inline void msg_set_user(struct tipc_msg *m, u32 n)
102 {
103         msg_set_bits(m, 0, 25, 0xf, n);
104 }
105
106 static inline void msg_set_importance(struct tipc_msg *m, u32 i)
107 {
108         msg_set_user(m, i);
109 }
110
111 static inline void msg_set_hdr_sz(struct tipc_msg *m,u32 n)
112 {
113         msg_set_bits(m, 0, 21, 0xf, n>>2);
114 }
115
116 static inline int msg_non_seq(struct tipc_msg *m)
117 {
118         return msg_bits(m, 0, 20, 1);
119 }
120
121 static inline void msg_set_non_seq(struct tipc_msg *m)
122 {
123         msg_set_bits(m, 0, 20, 1, 1);
124 }
125
126 static inline int msg_dest_droppable(struct tipc_msg *m)
127 {
128         return msg_bits(m, 0, 19, 1);
129 }
130
131 static inline void msg_set_dest_droppable(struct tipc_msg *m, u32 d)
132 {
133         msg_set_bits(m, 0, 19, 1, d);
134 }
135
136 static inline int msg_src_droppable(struct tipc_msg *m)
137 {
138         return msg_bits(m, 0, 18, 1);
139 }
140
141 static inline void msg_set_src_droppable(struct tipc_msg *m, u32 d)
142 {
143         msg_set_bits(m, 0, 18, 1, d);
144 }
145
146 static inline void msg_set_size(struct tipc_msg *m, u32 sz)
147 {
148         m->hdr[0] = htonl((msg_word(m, 0) & ~0x1ffff) | sz);
149 }
150
151
152 /*
153  * Word 1
154  */
155
156 static inline void msg_set_type(struct tipc_msg *m, u32 n)
157 {
158         msg_set_bits(m, 1, 29, 0x7, n);
159 }
160
161 static inline void msg_set_errcode(struct tipc_msg *m, u32 err)
162 {
163         msg_set_bits(m, 1, 25, 0xf, err);
164 }
165
166 static inline u32 msg_reroute_cnt(struct tipc_msg *m)
167 {
168         return msg_bits(m, 1, 21, 0xf);
169 }
170
171 static inline void msg_incr_reroute_cnt(struct tipc_msg *m)
172 {
173         msg_set_bits(m, 1, 21, 0xf, msg_reroute_cnt(m) + 1);
174 }
175
176 static inline void msg_reset_reroute_cnt(struct tipc_msg *m)
177 {
178         msg_set_bits(m, 1, 21, 0xf, 0);
179 }
180
181 static inline u32 msg_lookup_scope(struct tipc_msg *m)
182 {
183         return msg_bits(m, 1, 19, 0x3);
184 }
185
186 static inline void msg_set_lookup_scope(struct tipc_msg *m, u32 n)
187 {
188         msg_set_bits(m, 1, 19, 0x3, n);
189 }
190
191 static inline u32 msg_bcast_ack(struct tipc_msg *m)
192 {
193         return msg_bits(m, 1, 0, 0xffff);
194 }
195
196 static inline void msg_set_bcast_ack(struct tipc_msg *m, u32 n)
197 {
198         msg_set_bits(m, 1, 0, 0xffff, n);
199 }
200
201
202 /*
203  * Word 2
204  */
205
206 static inline u32 msg_ack(struct tipc_msg *m)
207 {
208         return msg_bits(m, 2, 16, 0xffff);
209 }
210
211 static inline void msg_set_ack(struct tipc_msg *m, u32 n)
212 {
213         msg_set_bits(m, 2, 16, 0xffff, n);
214 }
215
216 static inline u32 msg_seqno(struct tipc_msg *m)
217 {
218         return msg_bits(m, 2, 0, 0xffff);
219 }
220
221 static inline void msg_set_seqno(struct tipc_msg *m, u32 n)
222 {
223         msg_set_bits(m, 2, 0, 0xffff, n);
224 }
225
226
227 /*
228  * Words 3-10
229  */
230
231
232 static inline void msg_set_prevnode(struct tipc_msg *m, u32 a)
233 {
234         msg_set_word(m, 3, a);
235 }
236
237 static inline void msg_set_origport(struct tipc_msg *m, u32 p)
238 {
239         msg_set_word(m, 4, p);
240 }
241
242 static inline void msg_set_destport(struct tipc_msg *m, u32 p)
243 {
244         msg_set_word(m, 5, p);
245 }
246
247 static inline void msg_set_mc_netid(struct tipc_msg *m, u32 p)
248 {
249         msg_set_word(m, 5, p);
250 }
251
252 static inline void msg_set_orignode(struct tipc_msg *m, u32 a)
253 {
254         msg_set_word(m, 6, a);
255 }
256
257 static inline void msg_set_destnode(struct tipc_msg *m, u32 a)
258 {
259         msg_set_word(m, 7, a);
260 }
261
262 static inline int msg_is_dest(struct tipc_msg *m, u32 d)
263 {
264         return(msg_short(m) || (msg_destnode(m) == d));
265 }
266
267 static inline u32 msg_routed(struct tipc_msg *m)
268 {
269         if (likely(msg_short(m)))
270                 return 0;
271         return(msg_destnode(m) ^ msg_orignode(m)) >> 11;
272 }
273
274 static inline void msg_set_nametype(struct tipc_msg *m, u32 n)
275 {
276         msg_set_word(m, 8, n);
277 }
278
279 static inline u32 msg_transp_seqno(struct tipc_msg *m)
280 {
281         return msg_word(m, 8);
282 }
283
284 static inline void msg_set_timestamp(struct tipc_msg *m, u32 n)
285 {
286         msg_set_word(m, 8, n);
287 }
288
289 static inline u32 msg_timestamp(struct tipc_msg *m)
290 {
291         return msg_word(m, 8);
292 }
293
294 static inline void msg_set_transp_seqno(struct tipc_msg *m, u32 n)
295 {
296         msg_set_word(m, 8, n);
297 }
298
299 static inline void msg_set_namelower(struct tipc_msg *m, u32 n)
300 {
301         msg_set_word(m, 9, n);
302 }
303
304 static inline void msg_set_nameinst(struct tipc_msg *m, u32 n)
305 {
306         msg_set_namelower(m, n);
307 }
308
309 static inline void msg_set_nameupper(struct tipc_msg *m, u32 n)
310 {
311         msg_set_word(m, 10, n);
312 }
313
314 static inline struct tipc_msg *msg_get_wrapped(struct tipc_msg *m)
315 {
316         return (struct tipc_msg *)msg_data(m);
317 }
318
319
320 /*
321                 TIPC internal message header format, version 2
322
323        1 0 9 8 7 6 5 4|3 2 1 0 9 8 7 6|5 4 3 2 1 0 9 8|7 6 5 4 3 2 1 0
324       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
325    w0:|vers |msg usr|hdr sz |n|resrv|            packet size          |
326       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
327    w1:|m typ|rsv=0|   sequence gap    |       broadcast ack no        |
328       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
329    w2:| link level ack no/bc_gap_from |     seq no / bcast_gap_to     |
330       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
331    w3:|                       previous node                           |
332       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
333    w4:|  next sent broadcast/fragm no | next sent pkt/ fragm msg no   |
334       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
335    w5:|          session no           |rsv=0|r|berid|link prio|netpl|p|
336       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
337    w6:|                      originating node                         |
338       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
339    w7:|                      destination node                         |
340       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
341    w8:|                   transport sequence number                   |
342       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
343    w9:|   msg count / bcast tag       |       link tolerance          |
344       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
345       \                                                               \
346       /                     User Specific Data                        /
347       \                                                               \
348       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
349
350       NB: CONN_MANAGER use data message format. LINK_CONFIG has own format.
351 */
352
353 /*
354  * Internal users
355  */
356
357 #define  BCAST_PROTOCOL       5
358 #define  MSG_BUNDLER          6
359 #define  LINK_PROTOCOL        7
360 #define  CONN_MANAGER         8
361 #define  ROUTE_DISTRIBUTOR    9
362 #define  CHANGEOVER_PROTOCOL  10
363 #define  NAME_DISTRIBUTOR     11
364 #define  MSG_FRAGMENTER       12
365 #define  LINK_CONFIG          13
366 #define  DSC_H_SIZE           40
367
368 /*
369  *  Connection management protocol messages
370  */
371
372 #define CONN_PROBE        0
373 #define CONN_PROBE_REPLY  1
374 #define CONN_ACK          2
375
376 /*
377  * Name distributor messages
378  */
379
380 #define PUBLICATION       0
381 #define WITHDRAWAL        1
382
383
384 /*
385  * Word 1
386  */
387
388 static inline u32 msg_seq_gap(struct tipc_msg *m)
389 {
390         return msg_bits(m, 1, 16, 0xff);
391 }
392
393 static inline void msg_set_seq_gap(struct tipc_msg *m, u32 n)
394 {
395         msg_set_bits(m, 1, 16, 0xff, n);
396 }
397
398 static inline u32 msg_req_links(struct tipc_msg *m)
399 {
400         return msg_bits(m, 1, 16, 0xfff);
401 }
402
403 static inline void msg_set_req_links(struct tipc_msg *m, u32 n)
404 {
405         msg_set_bits(m, 1, 16, 0xfff, n);
406 }
407
408
409 /*
410  * Word 2
411  */
412
413 static inline u32 msg_dest_domain(struct tipc_msg *m)
414 {
415         return msg_word(m, 2);
416 }
417
418 static inline void msg_set_dest_domain(struct tipc_msg *m, u32 n)
419 {
420         msg_set_word(m, 2, n);
421 }
422
423 static inline u32 msg_bcgap_after(struct tipc_msg *m)
424 {
425         return msg_bits(m, 2, 16, 0xffff);
426 }
427
428 static inline void msg_set_bcgap_after(struct tipc_msg *m, u32 n)
429 {
430         msg_set_bits(m, 2, 16, 0xffff, n);
431 }
432
433 static inline u32 msg_bcgap_to(struct tipc_msg *m)
434 {
435         return msg_bits(m, 2, 0, 0xffff);
436 }
437
438 static inline void msg_set_bcgap_to(struct tipc_msg *m, u32 n)
439 {
440         msg_set_bits(m, 2, 0, 0xffff, n);
441 }
442
443
444 /*
445  * Word 4
446  */
447
448 static inline u32 msg_last_bcast(struct tipc_msg *m)
449 {
450         return msg_bits(m, 4, 16, 0xffff);
451 }
452
453 static inline void msg_set_last_bcast(struct tipc_msg *m, u32 n)
454 {
455         msg_set_bits(m, 4, 16, 0xffff, n);
456 }
457
458
459 static inline u32 msg_fragm_no(struct tipc_msg *m)
460 {
461         return msg_bits(m, 4, 16, 0xffff);
462 }
463
464 static inline void msg_set_fragm_no(struct tipc_msg *m, u32 n)
465 {
466         msg_set_bits(m, 4, 16, 0xffff, n);
467 }
468
469
470 static inline u32 msg_next_sent(struct tipc_msg *m)
471 {
472         return msg_bits(m, 4, 0, 0xffff);
473 }
474
475 static inline void msg_set_next_sent(struct tipc_msg *m, u32 n)
476 {
477         msg_set_bits(m, 4, 0, 0xffff, n);
478 }
479
480
481 static inline u32 msg_long_msgno(struct tipc_msg *m)
482 {
483         return msg_bits(m, 4, 0, 0xffff);
484 }
485
486 static inline void msg_set_long_msgno(struct tipc_msg *m, u32 n)
487 {
488         msg_set_bits(m, 4, 0, 0xffff, n);
489 }
490
491 static inline u32 msg_bc_netid(struct tipc_msg *m)
492 {
493         return msg_word(m, 4);
494 }
495
496 static inline void msg_set_bc_netid(struct tipc_msg *m, u32 id)
497 {
498         msg_set_word(m, 4, id);
499 }
500
501 static inline u32 msg_link_selector(struct tipc_msg *m)
502 {
503         return msg_bits(m, 4, 0, 1);
504 }
505
506 static inline void msg_set_link_selector(struct tipc_msg *m, u32 n)
507 {
508         msg_set_bits(m, 4, 0, 1, (n & 1));
509 }
510
511 /*
512  * Word 5
513  */
514
515 static inline u32 msg_session(struct tipc_msg *m)
516 {
517         return msg_bits(m, 5, 16, 0xffff);
518 }
519
520 static inline void msg_set_session(struct tipc_msg *m, u32 n)
521 {
522         msg_set_bits(m, 5, 16, 0xffff, n);
523 }
524
525 static inline u32 msg_probe(struct tipc_msg *m)
526 {
527         return msg_bits(m, 5, 0, 1);
528 }
529
530 static inline void msg_set_probe(struct tipc_msg *m, u32 val)
531 {
532         msg_set_bits(m, 5, 0, 1, (val & 1));
533 }
534
535 static inline char msg_net_plane(struct tipc_msg *m)
536 {
537         return msg_bits(m, 5, 1, 7) + 'A';
538 }
539
540 static inline void msg_set_net_plane(struct tipc_msg *m, char n)
541 {
542         msg_set_bits(m, 5, 1, 7, (n - 'A'));
543 }
544
545 static inline u32 msg_linkprio(struct tipc_msg *m)
546 {
547         return msg_bits(m, 5, 4, 0x1f);
548 }
549
550 static inline void msg_set_linkprio(struct tipc_msg *m, u32 n)
551 {
552         msg_set_bits(m, 5, 4, 0x1f, n);
553 }
554
555 static inline u32 msg_bearer_id(struct tipc_msg *m)
556 {
557         return msg_bits(m, 5, 9, 0x7);
558 }
559
560 static inline void msg_set_bearer_id(struct tipc_msg *m, u32 n)
561 {
562         msg_set_bits(m, 5, 9, 0x7, n);
563 }
564
565 static inline u32 msg_redundant_link(struct tipc_msg *m)
566 {
567         return msg_bits(m, 5, 12, 0x1);
568 }
569
570 static inline void msg_set_redundant_link(struct tipc_msg *m)
571 {
572         msg_set_bits(m, 5, 12, 0x1, 1);
573 }
574
575 static inline void msg_clear_redundant_link(struct tipc_msg *m)
576 {
577         msg_set_bits(m, 5, 12, 0x1, 0);
578 }
579
580
581 /*
582  * Word 9
583  */
584
585 static inline u32 msg_msgcnt(struct tipc_msg *m)
586 {
587         return msg_bits(m, 9, 16, 0xffff);
588 }
589
590 static inline void msg_set_msgcnt(struct tipc_msg *m, u32 n)
591 {
592         msg_set_bits(m, 9, 16, 0xffff, n);
593 }
594
595 static inline u32 msg_bcast_tag(struct tipc_msg *m)
596 {
597         return msg_bits(m, 9, 16, 0xffff);
598 }
599
600 static inline void msg_set_bcast_tag(struct tipc_msg *m, u32 n)
601 {
602         msg_set_bits(m, 9, 16, 0xffff, n);
603 }
604
605 static inline u32 msg_max_pkt(struct tipc_msg *m)
606 {
607         return (msg_bits(m, 9, 16, 0xffff) * 4);
608 }
609
610 static inline void msg_set_max_pkt(struct tipc_msg *m, u32 n)
611 {
612         msg_set_bits(m, 9, 16, 0xffff, (n / 4));
613 }
614
615 static inline u32 msg_link_tolerance(struct tipc_msg *m)
616 {
617         return msg_bits(m, 9, 0, 0xffff);
618 }
619
620 static inline void msg_set_link_tolerance(struct tipc_msg *m, u32 n)
621 {
622         msg_set_bits(m, 9, 0, 0xffff, n);
623 }
624
625 /*
626  * Routing table message data
627  */
628
629
630 static inline u32 msg_remote_node(struct tipc_msg *m)
631 {
632         return msg_word(m, msg_hdr_sz(m)/4);
633 }
634
635 static inline void msg_set_remote_node(struct tipc_msg *m, u32 a)
636 {
637         msg_set_word(m, msg_hdr_sz(m)/4, a);
638 }
639
640 static inline void msg_set_dataoctet(struct tipc_msg *m, u32 pos)
641 {
642         msg_data(m)[pos + 4] = 1;
643 }
644
645 /*
646  * Segmentation message types
647  */
648
649 #define FIRST_FRAGMENT     0
650 #define FRAGMENT           1
651 #define LAST_FRAGMENT      2
652
653 /*
654  * Link management protocol message types
655  */
656
657 #define STATE_MSG       0
658 #define RESET_MSG       1
659 #define ACTIVATE_MSG    2
660
661 /*
662  * Changeover tunnel message types
663  */
664 #define DUPLICATE_MSG    0
665 #define ORIGINAL_MSG     1
666
667 /*
668  * Routing table message types
669  */
670 #define EXT_ROUTING_TABLE    0
671 #define LOCAL_ROUTING_TABLE  1
672 #define SLAVE_ROUTING_TABLE  2
673 #define ROUTE_ADDITION       3
674 #define ROUTE_REMOVAL        4
675
676 /*
677  * Config protocol message types
678  */
679
680 #define DSC_REQ_MSG          0
681 #define DSC_RESP_MSG         1
682
683 static inline u32 msg_tot_importance(struct tipc_msg *m)
684 {
685         if (likely(msg_isdata(m))) {
686                 if (likely(msg_orignode(m) == tipc_own_addr))
687                         return msg_importance(m);
688                 return msg_importance(m) + 4;
689         }
690         if ((msg_user(m) == MSG_FRAGMENTER)  &&
691             (msg_type(m) == FIRST_FRAGMENT))
692                 return msg_importance(msg_get_wrapped(m));
693         return msg_importance(m);
694 }
695
696
697 static inline void msg_init(struct tipc_msg *m, u32 user, u32 type,
698                             u32 err, u32 hsize, u32 destnode)
699 {
700         memset(m, 0, hsize);
701         msg_set_version(m);
702         msg_set_user(m, user);
703         msg_set_hdr_sz(m, hsize);
704         msg_set_size(m, hsize);
705         msg_set_prevnode(m, tipc_own_addr);
706         msg_set_type(m, type);
707         msg_set_errcode(m, err);
708         if (!msg_short(m)) {
709                 msg_set_orignode(m, tipc_own_addr);
710                 msg_set_destnode(m, destnode);
711         }
712 }
713
714 /**
715  * msg_calc_data_size - determine total data size for message
716  */
717
718 static inline int msg_calc_data_size(struct iovec const *msg_sect, u32 num_sect)
719 {
720         int dsz = 0;
721         int i;
722
723         for (i = 0; i < num_sect; i++)
724                 dsz += msg_sect[i].iov_len;
725         return dsz;
726 }
727
728 /**
729  * msg_build - create message using specified header and data
730  *
731  * Note: Caller must not hold any locks in case copy_from_user() is interrupted!
732  *
733  * Returns message data size or errno
734  */
735
736 static inline int msg_build(struct tipc_msg *hdr,
737                             struct iovec const *msg_sect, u32 num_sect,
738                             int max_size, int usrmem, struct sk_buff** buf)
739 {
740         int dsz, sz, hsz, pos, res, cnt;
741
742         dsz = msg_calc_data_size(msg_sect, num_sect);
743         if (unlikely(dsz > TIPC_MAX_USER_MSG_SIZE)) {
744                 *buf = NULL;
745                 return -EINVAL;
746         }
747
748         pos = hsz = msg_hdr_sz(hdr);
749         sz = hsz + dsz;
750         msg_set_size(hdr, sz);
751         if (unlikely(sz > max_size)) {
752                 *buf = NULL;
753                 return dsz;
754         }
755
756         *buf = buf_acquire(sz);
757         if (!(*buf))
758                 return -ENOMEM;
759         skb_copy_to_linear_data(*buf, hdr, hsz);
760         for (res = 1, cnt = 0; res && (cnt < num_sect); cnt++) {
761                 if (likely(usrmem))
762                         res = !copy_from_user((*buf)->data + pos,
763                                               msg_sect[cnt].iov_base,
764                                               msg_sect[cnt].iov_len);
765                 else
766                         skb_copy_to_linear_data_offset(*buf, pos,
767                                                        msg_sect[cnt].iov_base,
768                                                        msg_sect[cnt].iov_len);
769                 pos += msg_sect[cnt].iov_len;
770         }
771         if (likely(res))
772                 return dsz;
773
774         buf_discard(*buf);
775         *buf = NULL;
776         return -EFAULT;
777 }
778
779 static inline void msg_set_media_addr(struct tipc_msg *m, struct tipc_media_addr *a)
780 {
781         memcpy(&((int *)m)[5], a, sizeof(*a));
782 }
783
784 static inline void msg_get_media_addr(struct tipc_msg *m, struct tipc_media_addr *a)
785 {
786         memcpy(a, &((int*)m)[5], sizeof(*a));
787 }
788
789 #endif