]>
Commit | Line | Data |
---|---|---|
b97bf3fd PL |
1 | /* |
2 | * net/tipc/msg.c: TIPC message header routines | |
c4307285 | 3 | * |
37e22164 | 4 | * Copyright (c) 2000-2006, 2014, Ericsson AB |
741de3e9 | 5 | * Copyright (c) 2005, 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" | |
b97bf3fd | 38 | #include "msg.h" |
5a379074 JPM |
39 | #include "addr.h" |
40 | #include "name_table.h" | |
b97bf3fd | 41 | |
8db1bae3 JPM |
42 | #define MAX_FORWARD_SIZE 1024 |
43 | ||
4f1688b2 | 44 | static unsigned int align(unsigned int i) |
23461e83 | 45 | { |
4f1688b2 | 46 | return (i + 3) & ~3u; |
23461e83 AS |
47 | } |
48 | ||
ae8509c4 PG |
49 | void tipc_msg_init(struct tipc_msg *m, u32 user, u32 type, u32 hsize, |
50 | u32 destnode) | |
23461e83 AS |
51 | { |
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); | |
15f4e2b3 AS |
59 | msg_set_orignode(m, tipc_own_addr); |
60 | msg_set_destnode(m, destnode); | |
23461e83 AS |
61 | } |
62 | ||
23461e83 AS |
63 | /** |
64 | * 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 | */ | |
26896904 | 70 | int tipc_msg_build(struct tipc_msg *hdr, struct iovec const *msg_sect, |
9446b87a | 71 | unsigned int len, int max_size, struct sk_buff **buf) |
23461e83 | 72 | { |
5c0a0fc8 YX |
73 | int dsz, sz, hsz; |
74 | unsigned char *to; | |
23461e83 | 75 | |
9446b87a | 76 | dsz = len; |
5c0a0fc8 | 77 | hsz = msg_hdr_sz(hdr); |
23461e83 AS |
78 | sz = hsz + dsz; |
79 | msg_set_size(hdr, sz); | |
80 | if (unlikely(sz > max_size)) { | |
81 | *buf = NULL; | |
82 | return dsz; | |
83 | } | |
84 | ||
31e3c3f6 | 85 | *buf = tipc_buf_acquire(sz); |
23461e83 AS |
86 | if (!(*buf)) |
87 | return -ENOMEM; | |
88 | skb_copy_to_linear_data(*buf, hdr, hsz); | |
5c0a0fc8 | 89 | to = (*buf)->data + hsz; |
9446b87a | 90 | if (len && memcpy_fromiovecend(to, msg_sect, 0, dsz)) { |
5c0a0fc8 YX |
91 | kfree_skb(*buf); |
92 | *buf = NULL; | |
93 | return -EFAULT; | |
23461e83 | 94 | } |
5c0a0fc8 | 95 | return dsz; |
23461e83 | 96 | } |
37e22164 JPM |
97 | |
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 | */ | |
103 | int 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; | |
145 | out_free: | |
146 | pr_warn_ratelimited("Unable to build fragment list\n"); | |
147 | kfree_skb(*buf); | |
148 | return 0; | |
149 | } | |
4f1688b2 | 150 | |
067608e9 JPM |
151 | |
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 | */ | |
162 | int 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; | |
247 | error: | |
248 | kfree_skb_list(*chain); | |
249 | *chain = NULL; | |
250 | return rc; | |
251 | } | |
252 | ||
4f1688b2 JPM |
253 | /** |
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 | */ | |
261 | bool 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 | */ | |
303 | bool 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 | } | |
8db1bae3 JPM |
335 | |
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 | */ | |
344 | bool 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 | ||
351 | if (skb_linearize(buf) || !msg_isdata(msg)) | |
352 | goto exit; | |
353 | if (msg_dest_droppable(msg) || msg_errcode(msg)) | |
354 | goto exit; | |
355 | ||
356 | memcpy(&ohdr, msg, msg_hdr_sz(msg)); | |
357 | msg_set_importance(msg, min_t(uint, ++imp, TIPC_CRITICAL_IMPORTANCE)); | |
358 | msg_set_errcode(msg, err); | |
359 | msg_set_origport(msg, msg_destport(&ohdr)); | |
360 | msg_set_destport(msg, msg_origport(&ohdr)); | |
361 | msg_set_prevnode(msg, tipc_own_addr); | |
362 | if (!msg_short(msg)) { | |
363 | msg_set_orignode(msg, msg_destnode(&ohdr)); | |
364 | msg_set_destnode(msg, msg_orignode(&ohdr)); | |
365 | } | |
366 | msg_set_size(msg, msg_hdr_sz(msg) + rdsz); | |
367 | skb_trim(buf, msg_size(msg)); | |
368 | skb_orphan(buf); | |
369 | *dnode = msg_orignode(&ohdr); | |
370 | return true; | |
371 | exit: | |
372 | kfree_skb(buf); | |
373 | return false; | |
374 | } | |
5a379074 JPM |
375 | |
376 | /** | |
377 | * tipc_msg_eval: determine fate of message that found no destination | |
378 | * @buf: the buffer containing the message. | |
379 | * @dnode: return value: next-hop node, if message to be forwarded | |
380 | * @err: error code to use, if message to be rejected | |
381 | * | |
382 | * Does not consume buffer | |
383 | * Returns 0 (TIPC_OK) if message ok and we can try again, -TIPC error | |
384 | * code if message to be rejected | |
385 | */ | |
386 | int tipc_msg_eval(struct sk_buff *buf, u32 *dnode) | |
387 | { | |
388 | struct tipc_msg *msg = buf_msg(buf); | |
389 | u32 dport; | |
390 | ||
391 | if (msg_type(msg) != TIPC_NAMED_MSG) | |
392 | return -TIPC_ERR_NO_PORT; | |
393 | if (skb_linearize(buf)) | |
394 | return -TIPC_ERR_NO_NAME; | |
395 | if (msg_data_sz(msg) > MAX_FORWARD_SIZE) | |
396 | return -TIPC_ERR_NO_NAME; | |
397 | if (msg_reroute_cnt(msg) > 0) | |
398 | return -TIPC_ERR_NO_NAME; | |
399 | ||
400 | *dnode = addr_domain(msg_lookup_scope(msg)); | |
401 | dport = tipc_nametbl_translate(msg_nametype(msg), | |
402 | msg_nameinst(msg), | |
403 | dnode); | |
404 | if (!dport) | |
405 | return -TIPC_ERR_NO_NAME; | |
406 | msg_incr_reroute_cnt(msg); | |
407 | msg_set_destnode(msg, *dnode); | |
408 | msg_set_destport(msg, dport); | |
409 | return TIPC_OK; | |
410 | } |