]>
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" |
b97bf3fd | 39 | |
8db1bae3 JPM |
40 | #define MAX_FORWARD_SIZE 1024 |
41 | ||
4f1688b2 | 42 | static unsigned int align(unsigned int i) |
23461e83 | 43 | { |
4f1688b2 | 44 | return (i + 3) & ~3u; |
23461e83 AS |
45 | } |
46 | ||
ae8509c4 PG |
47 | void tipc_msg_init(struct tipc_msg *m, u32 user, u32 type, u32 hsize, |
48 | u32 destnode) | |
23461e83 AS |
49 | { |
50 | memset(m, 0, hsize); | |
51 | msg_set_version(m); | |
52 | msg_set_user(m, user); | |
53 | msg_set_hdr_sz(m, hsize); | |
54 | msg_set_size(m, hsize); | |
55 | msg_set_prevnode(m, tipc_own_addr); | |
56 | msg_set_type(m, type); | |
15f4e2b3 AS |
57 | msg_set_orignode(m, tipc_own_addr); |
58 | msg_set_destnode(m, destnode); | |
23461e83 AS |
59 | } |
60 | ||
23461e83 AS |
61 | /** |
62 | * tipc_msg_build - create message using specified header and data | |
63 | * | |
64 | * Note: Caller must not hold any locks in case copy_from_user() is interrupted! | |
65 | * | |
66 | * Returns message data size or errno | |
67 | */ | |
26896904 | 68 | int tipc_msg_build(struct tipc_msg *hdr, struct iovec const *msg_sect, |
9446b87a | 69 | unsigned int len, int max_size, struct sk_buff **buf) |
23461e83 | 70 | { |
5c0a0fc8 YX |
71 | int dsz, sz, hsz; |
72 | unsigned char *to; | |
23461e83 | 73 | |
9446b87a | 74 | dsz = len; |
5c0a0fc8 | 75 | hsz = msg_hdr_sz(hdr); |
23461e83 AS |
76 | sz = hsz + dsz; |
77 | msg_set_size(hdr, sz); | |
78 | if (unlikely(sz > max_size)) { | |
79 | *buf = NULL; | |
80 | return dsz; | |
81 | } | |
82 | ||
31e3c3f6 | 83 | *buf = tipc_buf_acquire(sz); |
23461e83 AS |
84 | if (!(*buf)) |
85 | return -ENOMEM; | |
86 | skb_copy_to_linear_data(*buf, hdr, hsz); | |
5c0a0fc8 | 87 | to = (*buf)->data + hsz; |
9446b87a | 88 | if (len && memcpy_fromiovecend(to, msg_sect, 0, dsz)) { |
5c0a0fc8 YX |
89 | kfree_skb(*buf); |
90 | *buf = NULL; | |
91 | return -EFAULT; | |
23461e83 | 92 | } |
5c0a0fc8 | 93 | return dsz; |
23461e83 | 94 | } |
37e22164 JPM |
95 | |
96 | /* tipc_buf_append(): Append a buffer to the fragment list of another buffer | |
97 | * Let first buffer become head buffer | |
98 | * Returns 1 and sets *buf to headbuf if chain is complete, otherwise 0 | |
99 | * Leaves headbuf pointer at NULL if failure | |
100 | */ | |
101 | int tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf) | |
102 | { | |
103 | struct sk_buff *head = *headbuf; | |
104 | struct sk_buff *frag = *buf; | |
105 | struct sk_buff *tail; | |
106 | struct tipc_msg *msg = buf_msg(frag); | |
107 | u32 fragid = msg_type(msg); | |
108 | bool headstolen; | |
109 | int delta; | |
110 | ||
111 | skb_pull(frag, msg_hdr_sz(msg)); | |
112 | ||
113 | if (fragid == FIRST_FRAGMENT) { | |
114 | if (head || skb_unclone(frag, GFP_ATOMIC)) | |
115 | goto out_free; | |
116 | head = *headbuf = frag; | |
117 | skb_frag_list_init(head); | |
118 | return 0; | |
119 | } | |
120 | if (!head) | |
121 | goto out_free; | |
122 | tail = TIPC_SKB_CB(head)->tail; | |
123 | if (skb_try_coalesce(head, frag, &headstolen, &delta)) { | |
124 | kfree_skb_partial(frag, headstolen); | |
125 | } else { | |
126 | if (!skb_has_frag_list(head)) | |
127 | skb_shinfo(head)->frag_list = frag; | |
128 | else | |
129 | tail->next = frag; | |
130 | head->truesize += frag->truesize; | |
131 | head->data_len += frag->len; | |
132 | head->len += frag->len; | |
133 | TIPC_SKB_CB(head)->tail = frag; | |
134 | } | |
135 | if (fragid == LAST_FRAGMENT) { | |
136 | *buf = head; | |
137 | TIPC_SKB_CB(head)->tail = NULL; | |
138 | *headbuf = NULL; | |
139 | return 1; | |
140 | } | |
141 | *buf = NULL; | |
142 | return 0; | |
143 | out_free: | |
144 | pr_warn_ratelimited("Unable to build fragment list\n"); | |
145 | kfree_skb(*buf); | |
146 | return 0; | |
147 | } | |
4f1688b2 | 148 | |
067608e9 JPM |
149 | |
150 | /** | |
151 | * tipc_msg_build2 - create buffer chain containing specified header and data | |
152 | * @mhdr: Message header, to be prepended to data | |
153 | * @iov: User data | |
154 | * @offset: Posision in iov to start copying from | |
155 | * @dsz: Total length of user data | |
156 | * @pktmax: Max packet size that can be used | |
157 | * @chain: Buffer or chain of buffers to be returned to caller | |
158 | * Returns message data size or errno: -ENOMEM, -EFAULT | |
159 | */ | |
160 | int tipc_msg_build2(struct tipc_msg *mhdr, struct iovec const *iov, | |
161 | int offset, int dsz, int pktmax , struct sk_buff **chain) | |
162 | { | |
163 | int mhsz = msg_hdr_sz(mhdr); | |
164 | int msz = mhsz + dsz; | |
165 | int pktno = 1; | |
166 | int pktsz; | |
167 | int pktrem = pktmax; | |
168 | int drem = dsz; | |
169 | struct tipc_msg pkthdr; | |
170 | struct sk_buff *buf, *prev; | |
171 | char *pktpos; | |
172 | int rc; | |
173 | ||
174 | msg_set_size(mhdr, msz); | |
175 | ||
176 | /* No fragmentation needed? */ | |
177 | if (likely(msz <= pktmax)) { | |
178 | buf = tipc_buf_acquire(msz); | |
179 | *chain = buf; | |
180 | if (unlikely(!buf)) | |
181 | return -ENOMEM; | |
182 | skb_copy_to_linear_data(buf, mhdr, mhsz); | |
183 | pktpos = buf->data + mhsz; | |
184 | if (!dsz || !memcpy_fromiovecend(pktpos, iov, offset, dsz)) | |
185 | return dsz; | |
186 | rc = -EFAULT; | |
187 | goto error; | |
188 | } | |
189 | ||
190 | /* Prepare reusable fragment header */ | |
191 | tipc_msg_init(&pkthdr, MSG_FRAGMENTER, FIRST_FRAGMENT, | |
192 | INT_H_SIZE, msg_destnode(mhdr)); | |
193 | msg_set_size(&pkthdr, pktmax); | |
194 | msg_set_fragm_no(&pkthdr, pktno); | |
195 | ||
196 | /* Prepare first fragment */ | |
197 | *chain = buf = tipc_buf_acquire(pktmax); | |
198 | if (!buf) | |
199 | return -ENOMEM; | |
200 | pktpos = buf->data; | |
201 | skb_copy_to_linear_data(buf, &pkthdr, INT_H_SIZE); | |
202 | pktpos += INT_H_SIZE; | |
203 | pktrem -= INT_H_SIZE; | |
204 | skb_copy_to_linear_data_offset(buf, INT_H_SIZE, mhdr, mhsz); | |
205 | pktpos += mhsz; | |
206 | pktrem -= mhsz; | |
207 | ||
208 | do { | |
209 | if (drem < pktrem) | |
210 | pktrem = drem; | |
211 | ||
212 | if (memcpy_fromiovecend(pktpos, iov, offset, pktrem)) { | |
213 | rc = -EFAULT; | |
214 | goto error; | |
215 | } | |
216 | drem -= pktrem; | |
217 | offset += pktrem; | |
218 | ||
219 | if (!drem) | |
220 | break; | |
221 | ||
222 | /* Prepare new fragment: */ | |
223 | if (drem < (pktmax - INT_H_SIZE)) | |
224 | pktsz = drem + INT_H_SIZE; | |
225 | else | |
226 | pktsz = pktmax; | |
227 | prev = buf; | |
228 | buf = tipc_buf_acquire(pktsz); | |
229 | if (!buf) { | |
230 | rc = -ENOMEM; | |
231 | goto error; | |
232 | } | |
233 | prev->next = buf; | |
234 | msg_set_type(&pkthdr, FRAGMENT); | |
235 | msg_set_size(&pkthdr, pktsz); | |
236 | msg_set_fragm_no(&pkthdr, ++pktno); | |
237 | skb_copy_to_linear_data(buf, &pkthdr, INT_H_SIZE); | |
238 | pktpos = buf->data + INT_H_SIZE; | |
239 | pktrem = pktsz - INT_H_SIZE; | |
240 | ||
241 | } while (1); | |
242 | ||
243 | msg_set_type(buf_msg(buf), LAST_FRAGMENT); | |
244 | return dsz; | |
245 | error: | |
246 | kfree_skb_list(*chain); | |
247 | *chain = NULL; | |
248 | return rc; | |
249 | } | |
250 | ||
4f1688b2 JPM |
251 | /** |
252 | * tipc_msg_bundle(): Append contents of a buffer to tail of an existing one | |
253 | * @bbuf: the existing buffer ("bundle") | |
254 | * @buf: buffer to be appended | |
255 | * @mtu: max allowable size for the bundle buffer | |
256 | * Consumes buffer if successful | |
257 | * Returns true if bundling could be performed, otherwise false | |
258 | */ | |
259 | bool tipc_msg_bundle(struct sk_buff *bbuf, struct sk_buff *buf, u32 mtu) | |
260 | { | |
261 | struct tipc_msg *bmsg = buf_msg(bbuf); | |
262 | struct tipc_msg *msg = buf_msg(buf); | |
263 | unsigned int bsz = msg_size(bmsg); | |
264 | unsigned int msz = msg_size(msg); | |
265 | u32 start = align(bsz); | |
266 | u32 max = mtu - INT_H_SIZE; | |
267 | u32 pad = start - bsz; | |
268 | ||
269 | if (likely(msg_user(msg) == MSG_FRAGMENTER)) | |
270 | return false; | |
271 | if (unlikely(msg_user(msg) == CHANGEOVER_PROTOCOL)) | |
272 | return false; | |
273 | if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) | |
274 | return false; | |
275 | if (likely(msg_user(bmsg) != MSG_BUNDLER)) | |
276 | return false; | |
277 | if (likely(msg_type(bmsg) != BUNDLE_OPEN)) | |
278 | return false; | |
279 | if (unlikely(skb_tailroom(bbuf) < (pad + msz))) | |
280 | return false; | |
281 | if (unlikely(max < (start + msz))) | |
282 | return false; | |
283 | ||
284 | skb_put(bbuf, pad + msz); | |
285 | skb_copy_to_linear_data_offset(bbuf, start, buf->data, msz); | |
286 | msg_set_size(bmsg, start + msz); | |
287 | msg_set_msgcnt(bmsg, msg_msgcnt(bmsg) + 1); | |
288 | bbuf->next = buf->next; | |
289 | kfree_skb(buf); | |
290 | return true; | |
291 | } | |
292 | ||
293 | /** | |
294 | * tipc_msg_make_bundle(): Create bundle buf and append message to its tail | |
295 | * @buf: buffer to be appended and replaced | |
296 | * @mtu: max allowable size for the bundle buffer, inclusive header | |
297 | * @dnode: destination node for message. (Not always present in header) | |
298 | * Replaces buffer if successful | |
299 | * Returns true if sucess, otherwise false | |
300 | */ | |
301 | bool tipc_msg_make_bundle(struct sk_buff **buf, u32 mtu, u32 dnode) | |
302 | { | |
303 | struct sk_buff *bbuf; | |
304 | struct tipc_msg *bmsg; | |
305 | struct tipc_msg *msg = buf_msg(*buf); | |
306 | u32 msz = msg_size(msg); | |
307 | u32 max = mtu - INT_H_SIZE; | |
308 | ||
309 | if (msg_user(msg) == MSG_FRAGMENTER) | |
310 | return false; | |
311 | if (msg_user(msg) == CHANGEOVER_PROTOCOL) | |
312 | return false; | |
313 | if (msg_user(msg) == BCAST_PROTOCOL) | |
314 | return false; | |
315 | if (msz > (max / 2)) | |
316 | return false; | |
317 | ||
318 | bbuf = tipc_buf_acquire(max); | |
319 | if (!bbuf) | |
320 | return false; | |
321 | ||
322 | skb_trim(bbuf, INT_H_SIZE); | |
323 | bmsg = buf_msg(bbuf); | |
324 | tipc_msg_init(bmsg, MSG_BUNDLER, BUNDLE_OPEN, INT_H_SIZE, dnode); | |
325 | msg_set_seqno(bmsg, msg_seqno(msg)); | |
326 | msg_set_ack(bmsg, msg_ack(msg)); | |
327 | msg_set_bcast_ack(bmsg, msg_bcast_ack(msg)); | |
328 | bbuf->next = (*buf)->next; | |
329 | tipc_msg_bundle(bbuf, *buf, mtu); | |
330 | *buf = bbuf; | |
331 | return true; | |
332 | } | |
8db1bae3 JPM |
333 | |
334 | /** | |
335 | * tipc_msg_reverse(): swap source and destination addresses and add error code | |
336 | * @buf: buffer containing message to be reversed | |
337 | * @dnode: return value: node where to send message after reversal | |
338 | * @err: error code to be set in message | |
339 | * Consumes buffer if failure | |
340 | * Returns true if success, otherwise false | |
341 | */ | |
342 | bool tipc_msg_reverse(struct sk_buff *buf, u32 *dnode, int err) | |
343 | { | |
344 | struct tipc_msg *msg = buf_msg(buf); | |
345 | uint imp = msg_importance(msg); | |
346 | struct tipc_msg ohdr; | |
347 | uint rdsz = min_t(uint, msg_data_sz(msg), MAX_FORWARD_SIZE); | |
348 | ||
349 | if (skb_linearize(buf) || !msg_isdata(msg)) | |
350 | goto exit; | |
351 | if (msg_dest_droppable(msg) || msg_errcode(msg)) | |
352 | goto exit; | |
353 | ||
354 | memcpy(&ohdr, msg, msg_hdr_sz(msg)); | |
355 | msg_set_importance(msg, min_t(uint, ++imp, TIPC_CRITICAL_IMPORTANCE)); | |
356 | msg_set_errcode(msg, err); | |
357 | msg_set_origport(msg, msg_destport(&ohdr)); | |
358 | msg_set_destport(msg, msg_origport(&ohdr)); | |
359 | msg_set_prevnode(msg, tipc_own_addr); | |
360 | if (!msg_short(msg)) { | |
361 | msg_set_orignode(msg, msg_destnode(&ohdr)); | |
362 | msg_set_destnode(msg, msg_orignode(&ohdr)); | |
363 | } | |
364 | msg_set_size(msg, msg_hdr_sz(msg) + rdsz); | |
365 | skb_trim(buf, msg_size(msg)); | |
366 | skb_orphan(buf); | |
367 | *dnode = msg_orignode(&ohdr); | |
368 | return true; | |
369 | exit: | |
370 | kfree_skb(buf); | |
371 | return false; | |
372 | } |