blob: 6ec958401f7802a03610c5384a1c2180255798de [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/msg.c: TIPC message header routines
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy37e22162014-05-14 05:39:12 -04004 * Copyright (c) 2000-2006, 2014, Ericsson AB
Allan Stephens741de3e2011-01-25 13:33:31 -05005 * Copyright (c) 2005, 2010-2011, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "msg.h"
Jon Paul Maloy5a379072014-06-25 20:41:36 -050039#include "addr.h"
40#include "name_table.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -050042#define MAX_FORWARD_SIZE 1024
43
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -050044static unsigned int align(unsigned int i)
Allan Stephens23461e82010-05-11 14:30:18 +000045{
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -050046 return (i + 3) & ~3u;
Allan Stephens23461e82010-05-11 14:30:18 +000047}
48
Paul Gortmakerae8509c2013-06-17 10:54:47 -040049void tipc_msg_init(struct tipc_msg *m, u32 user, u32 type, u32 hsize,
50 u32 destnode)
Allan Stephens23461e82010-05-11 14:30:18 +000051{
52 memset(m, 0, hsize);
53 msg_set_version(m);
54 msg_set_user(m, user);
55 msg_set_hdr_sz(m, hsize);
56 msg_set_size(m, hsize);
57 msg_set_prevnode(m, tipc_own_addr);
58 msg_set_type(m, type);
Allan Stephens15f4e2b2011-05-31 14:35:18 -040059 msg_set_orignode(m, tipc_own_addr);
60 msg_set_destnode(m, destnode);
Allan Stephens23461e82010-05-11 14:30:18 +000061}
62
63/**
Allan Stephens23461e82010-05-11 14:30:18 +000064 * tipc_msg_build - create message using specified header and data
65 *
66 * Note: Caller must not hold any locks in case copy_from_user() is interrupted!
67 *
68 * Returns message data size or errno
69 */
Allan Stephens26896902011-04-21 10:42:07 -050070int tipc_msg_build(struct tipc_msg *hdr, struct iovec const *msg_sect,
Ying Xue9446b872013-10-18 07:23:15 +020071 unsigned int len, int max_size, struct sk_buff **buf)
Allan Stephens23461e82010-05-11 14:30:18 +000072{
Ying Xue5c0a0fc2013-10-18 07:23:14 +020073 int dsz, sz, hsz;
74 unsigned char *to;
Allan Stephens23461e82010-05-11 14:30:18 +000075
Ying Xue9446b872013-10-18 07:23:15 +020076 dsz = len;
Ying Xue5c0a0fc2013-10-18 07:23:14 +020077 hsz = msg_hdr_sz(hdr);
Allan Stephens23461e82010-05-11 14:30:18 +000078 sz = hsz + dsz;
79 msg_set_size(hdr, sz);
80 if (unlikely(sz > max_size)) {
81 *buf = NULL;
82 return dsz;
83 }
84
stephen hemminger31e3c3f2010-10-13 13:20:35 +000085 *buf = tipc_buf_acquire(sz);
Allan Stephens23461e82010-05-11 14:30:18 +000086 if (!(*buf))
87 return -ENOMEM;
88 skb_copy_to_linear_data(*buf, hdr, hsz);
Ying Xue5c0a0fc2013-10-18 07:23:14 +020089 to = (*buf)->data + hsz;
Ying Xue9446b872013-10-18 07:23:15 +020090 if (len && memcpy_fromiovecend(to, msg_sect, 0, dsz)) {
Ying Xue5c0a0fc2013-10-18 07:23:14 +020091 kfree_skb(*buf);
92 *buf = NULL;
93 return -EFAULT;
Allan Stephens23461e82010-05-11 14:30:18 +000094 }
Ying Xue5c0a0fc2013-10-18 07:23:14 +020095 return dsz;
Allan Stephens23461e82010-05-11 14:30:18 +000096}
Jon Paul Maloy37e22162014-05-14 05:39:12 -040097
98/* tipc_buf_append(): Append a buffer to the fragment list of another buffer
99 * Let first buffer become head buffer
100 * Returns 1 and sets *buf to headbuf if chain is complete, otherwise 0
101 * Leaves headbuf pointer at NULL if failure
102 */
103int tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf)
104{
105 struct sk_buff *head = *headbuf;
106 struct sk_buff *frag = *buf;
107 struct sk_buff *tail;
108 struct tipc_msg *msg = buf_msg(frag);
109 u32 fragid = msg_type(msg);
110 bool headstolen;
111 int delta;
112
113 skb_pull(frag, msg_hdr_sz(msg));
114
115 if (fragid == FIRST_FRAGMENT) {
116 if (head || skb_unclone(frag, GFP_ATOMIC))
117 goto out_free;
118 head = *headbuf = frag;
119 skb_frag_list_init(head);
120 return 0;
121 }
122 if (!head)
123 goto out_free;
124 tail = TIPC_SKB_CB(head)->tail;
125 if (skb_try_coalesce(head, frag, &headstolen, &delta)) {
126 kfree_skb_partial(frag, headstolen);
127 } else {
128 if (!skb_has_frag_list(head))
129 skb_shinfo(head)->frag_list = frag;
130 else
131 tail->next = frag;
132 head->truesize += frag->truesize;
133 head->data_len += frag->len;
134 head->len += frag->len;
135 TIPC_SKB_CB(head)->tail = frag;
136 }
137 if (fragid == LAST_FRAGMENT) {
138 *buf = head;
139 TIPC_SKB_CB(head)->tail = NULL;
140 *headbuf = NULL;
141 return 1;
142 }
143 *buf = NULL;
144 return 0;
145out_free:
146 pr_warn_ratelimited("Unable to build fragment list\n");
147 kfree_skb(*buf);
148 return 0;
149}
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500150
Jon Paul Maloy067608e2014-06-25 20:41:34 -0500151
152/**
153 * tipc_msg_build2 - create buffer chain containing specified header and data
154 * @mhdr: Message header, to be prepended to data
155 * @iov: User data
156 * @offset: Posision in iov to start copying from
157 * @dsz: Total length of user data
158 * @pktmax: Max packet size that can be used
159 * @chain: Buffer or chain of buffers to be returned to caller
160 * Returns message data size or errno: -ENOMEM, -EFAULT
161 */
162int tipc_msg_build2(struct tipc_msg *mhdr, struct iovec const *iov,
163 int offset, int dsz, int pktmax , struct sk_buff **chain)
164{
165 int mhsz = msg_hdr_sz(mhdr);
166 int msz = mhsz + dsz;
167 int pktno = 1;
168 int pktsz;
169 int pktrem = pktmax;
170 int drem = dsz;
171 struct tipc_msg pkthdr;
172 struct sk_buff *buf, *prev;
173 char *pktpos;
174 int rc;
175
176 msg_set_size(mhdr, msz);
177
178 /* No fragmentation needed? */
179 if (likely(msz <= pktmax)) {
180 buf = tipc_buf_acquire(msz);
181 *chain = buf;
182 if (unlikely(!buf))
183 return -ENOMEM;
184 skb_copy_to_linear_data(buf, mhdr, mhsz);
185 pktpos = buf->data + mhsz;
186 if (!dsz || !memcpy_fromiovecend(pktpos, iov, offset, dsz))
187 return dsz;
188 rc = -EFAULT;
189 goto error;
190 }
191
192 /* Prepare reusable fragment header */
193 tipc_msg_init(&pkthdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
194 INT_H_SIZE, msg_destnode(mhdr));
195 msg_set_size(&pkthdr, pktmax);
196 msg_set_fragm_no(&pkthdr, pktno);
197
198 /* Prepare first fragment */
199 *chain = buf = tipc_buf_acquire(pktmax);
200 if (!buf)
201 return -ENOMEM;
202 pktpos = buf->data;
203 skb_copy_to_linear_data(buf, &pkthdr, INT_H_SIZE);
204 pktpos += INT_H_SIZE;
205 pktrem -= INT_H_SIZE;
206 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, mhdr, mhsz);
207 pktpos += mhsz;
208 pktrem -= mhsz;
209
210 do {
211 if (drem < pktrem)
212 pktrem = drem;
213
214 if (memcpy_fromiovecend(pktpos, iov, offset, pktrem)) {
215 rc = -EFAULT;
216 goto error;
217 }
218 drem -= pktrem;
219 offset += pktrem;
220
221 if (!drem)
222 break;
223
224 /* Prepare new fragment: */
225 if (drem < (pktmax - INT_H_SIZE))
226 pktsz = drem + INT_H_SIZE;
227 else
228 pktsz = pktmax;
229 prev = buf;
230 buf = tipc_buf_acquire(pktsz);
231 if (!buf) {
232 rc = -ENOMEM;
233 goto error;
234 }
235 prev->next = buf;
236 msg_set_type(&pkthdr, FRAGMENT);
237 msg_set_size(&pkthdr, pktsz);
238 msg_set_fragm_no(&pkthdr, ++pktno);
239 skb_copy_to_linear_data(buf, &pkthdr, INT_H_SIZE);
240 pktpos = buf->data + INT_H_SIZE;
241 pktrem = pktsz - INT_H_SIZE;
242
243 } while (1);
244
245 msg_set_type(buf_msg(buf), LAST_FRAGMENT);
246 return dsz;
247error:
248 kfree_skb_list(*chain);
249 *chain = NULL;
250 return rc;
251}
252
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500253/**
254 * tipc_msg_bundle(): Append contents of a buffer to tail of an existing one
255 * @bbuf: the existing buffer ("bundle")
256 * @buf: buffer to be appended
257 * @mtu: max allowable size for the bundle buffer
258 * Consumes buffer if successful
259 * Returns true if bundling could be performed, otherwise false
260 */
261bool tipc_msg_bundle(struct sk_buff *bbuf, struct sk_buff *buf, u32 mtu)
262{
263 struct tipc_msg *bmsg = buf_msg(bbuf);
264 struct tipc_msg *msg = buf_msg(buf);
265 unsigned int bsz = msg_size(bmsg);
266 unsigned int msz = msg_size(msg);
267 u32 start = align(bsz);
268 u32 max = mtu - INT_H_SIZE;
269 u32 pad = start - bsz;
270
271 if (likely(msg_user(msg) == MSG_FRAGMENTER))
272 return false;
273 if (unlikely(msg_user(msg) == CHANGEOVER_PROTOCOL))
274 return false;
275 if (unlikely(msg_user(msg) == BCAST_PROTOCOL))
276 return false;
277 if (likely(msg_user(bmsg) != MSG_BUNDLER))
278 return false;
279 if (likely(msg_type(bmsg) != BUNDLE_OPEN))
280 return false;
281 if (unlikely(skb_tailroom(bbuf) < (pad + msz)))
282 return false;
283 if (unlikely(max < (start + msz)))
284 return false;
285
286 skb_put(bbuf, pad + msz);
287 skb_copy_to_linear_data_offset(bbuf, start, buf->data, msz);
288 msg_set_size(bmsg, start + msz);
289 msg_set_msgcnt(bmsg, msg_msgcnt(bmsg) + 1);
290 bbuf->next = buf->next;
291 kfree_skb(buf);
292 return true;
293}
294
295/**
296 * tipc_msg_make_bundle(): Create bundle buf and append message to its tail
297 * @buf: buffer to be appended and replaced
298 * @mtu: max allowable size for the bundle buffer, inclusive header
299 * @dnode: destination node for message. (Not always present in header)
300 * Replaces buffer if successful
301 * Returns true if sucess, otherwise false
302 */
303bool tipc_msg_make_bundle(struct sk_buff **buf, u32 mtu, u32 dnode)
304{
305 struct sk_buff *bbuf;
306 struct tipc_msg *bmsg;
307 struct tipc_msg *msg = buf_msg(*buf);
308 u32 msz = msg_size(msg);
309 u32 max = mtu - INT_H_SIZE;
310
311 if (msg_user(msg) == MSG_FRAGMENTER)
312 return false;
313 if (msg_user(msg) == CHANGEOVER_PROTOCOL)
314 return false;
315 if (msg_user(msg) == BCAST_PROTOCOL)
316 return false;
317 if (msz > (max / 2))
318 return false;
319
320 bbuf = tipc_buf_acquire(max);
321 if (!bbuf)
322 return false;
323
324 skb_trim(bbuf, INT_H_SIZE);
325 bmsg = buf_msg(bbuf);
326 tipc_msg_init(bmsg, MSG_BUNDLER, BUNDLE_OPEN, INT_H_SIZE, dnode);
327 msg_set_seqno(bmsg, msg_seqno(msg));
328 msg_set_ack(bmsg, msg_ack(msg));
329 msg_set_bcast_ack(bmsg, msg_bcast_ack(msg));
330 bbuf->next = (*buf)->next;
331 tipc_msg_bundle(bbuf, *buf, mtu);
332 *buf = bbuf;
333 return true;
334}
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500335
336/**
337 * tipc_msg_reverse(): swap source and destination addresses and add error code
338 * @buf: buffer containing message to be reversed
339 * @dnode: return value: node where to send message after reversal
340 * @err: error code to be set in message
341 * Consumes buffer if failure
342 * Returns true if success, otherwise false
343 */
344bool tipc_msg_reverse(struct sk_buff *buf, u32 *dnode, int err)
345{
346 struct tipc_msg *msg = buf_msg(buf);
347 uint imp = msg_importance(msg);
348 struct tipc_msg ohdr;
349 uint rdsz = min_t(uint, msg_data_sz(msg), MAX_FORWARD_SIZE);
350
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500351 if (skb_linearize(buf))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500352 goto exit;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500353 if (msg_dest_droppable(msg))
354 goto exit;
355 if (msg_errcode(msg))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500356 goto exit;
357
358 memcpy(&ohdr, msg, msg_hdr_sz(msg));
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500359 imp = min_t(uint, imp + 1, TIPC_CRITICAL_IMPORTANCE);
360 if (msg_isdata(msg))
361 msg_set_importance(msg, imp);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500362 msg_set_errcode(msg, err);
363 msg_set_origport(msg, msg_destport(&ohdr));
364 msg_set_destport(msg, msg_origport(&ohdr));
365 msg_set_prevnode(msg, tipc_own_addr);
366 if (!msg_short(msg)) {
367 msg_set_orignode(msg, msg_destnode(&ohdr));
368 msg_set_destnode(msg, msg_orignode(&ohdr));
369 }
370 msg_set_size(msg, msg_hdr_sz(msg) + rdsz);
371 skb_trim(buf, msg_size(msg));
372 skb_orphan(buf);
373 *dnode = msg_orignode(&ohdr);
374 return true;
375exit:
376 kfree_skb(buf);
377 return false;
378}
Jon Paul Maloy5a379072014-06-25 20:41:36 -0500379
380/**
381 * tipc_msg_eval: determine fate of message that found no destination
382 * @buf: the buffer containing the message.
383 * @dnode: return value: next-hop node, if message to be forwarded
384 * @err: error code to use, if message to be rejected
385 *
386 * Does not consume buffer
387 * Returns 0 (TIPC_OK) if message ok and we can try again, -TIPC error
388 * code if message to be rejected
389 */
390int tipc_msg_eval(struct sk_buff *buf, u32 *dnode)
391{
392 struct tipc_msg *msg = buf_msg(buf);
393 u32 dport;
394
395 if (msg_type(msg) != TIPC_NAMED_MSG)
396 return -TIPC_ERR_NO_PORT;
397 if (skb_linearize(buf))
398 return -TIPC_ERR_NO_NAME;
399 if (msg_data_sz(msg) > MAX_FORWARD_SIZE)
400 return -TIPC_ERR_NO_NAME;
401 if (msg_reroute_cnt(msg) > 0)
402 return -TIPC_ERR_NO_NAME;
403
404 *dnode = addr_domain(msg_lookup_scope(msg));
405 dport = tipc_nametbl_translate(msg_nametype(msg),
406 msg_nameinst(msg),
407 dnode);
408 if (!dport)
409 return -TIPC_ERR_NO_NAME;
410 msg_incr_reroute_cnt(msg);
411 msg_set_destnode(msg, *dnode);
412 msg_set_destport(msg, dport);
413 return TIPC_OK;
414}