GNU Linux-libre 4.4.288-gnu1
[releases.git] / net / tipc / msg.c
1 /*
2  * net/tipc/msg.c: TIPC message header routines
3  *
4  * Copyright (c) 2000-2006, 2014-2015, 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 <net/sock.h>
38 #include "core.h"
39 #include "msg.h"
40 #include "addr.h"
41 #include "name_table.h"
42
43 #define MAX_FORWARD_SIZE 1024
44
45 static unsigned int align(unsigned int i)
46 {
47         return (i + 3) & ~3u;
48 }
49
50 /**
51  * tipc_buf_acquire - creates a TIPC message buffer
52  * @size: message size (including TIPC header)
53  *
54  * Returns a new buffer with data pointers set to the specified size.
55  *
56  * NOTE: Headroom is reserved to allow prepending of a data link header.
57  *       There may also be unrequested tailroom present at the buffer's end.
58  */
59 struct sk_buff *tipc_buf_acquire(u32 size)
60 {
61         struct sk_buff *skb;
62         unsigned int buf_size = (BUF_HEADROOM + size + 3) & ~3u;
63
64         skb = alloc_skb_fclone(buf_size, GFP_ATOMIC);
65         if (skb) {
66                 skb_reserve(skb, BUF_HEADROOM);
67                 skb_put(skb, size);
68                 skb->next = NULL;
69         }
70         return skb;
71 }
72
73 void tipc_msg_init(u32 own_node, struct tipc_msg *m, u32 user, u32 type,
74                    u32 hsize, u32 dnode)
75 {
76         memset(m, 0, hsize);
77         msg_set_version(m);
78         msg_set_user(m, user);
79         msg_set_hdr_sz(m, hsize);
80         msg_set_size(m, hsize);
81         msg_set_prevnode(m, own_node);
82         msg_set_type(m, type);
83         if (hsize > SHORT_H_SIZE) {
84                 msg_set_orignode(m, own_node);
85                 msg_set_destnode(m, dnode);
86         }
87 }
88
89 struct sk_buff *tipc_msg_create(uint user, uint type,
90                                 uint hdr_sz, uint data_sz, u32 dnode,
91                                 u32 onode, u32 dport, u32 oport, int errcode)
92 {
93         struct tipc_msg *msg;
94         struct sk_buff *buf;
95
96         buf = tipc_buf_acquire(hdr_sz + data_sz);
97         if (unlikely(!buf))
98                 return NULL;
99
100         msg = buf_msg(buf);
101         tipc_msg_init(onode, msg, user, type, hdr_sz, dnode);
102         msg_set_size(msg, hdr_sz + data_sz);
103         msg_set_origport(msg, oport);
104         msg_set_destport(msg, dport);
105         msg_set_errcode(msg, errcode);
106         if (hdr_sz > SHORT_H_SIZE) {
107                 msg_set_orignode(msg, onode);
108                 msg_set_destnode(msg, dnode);
109         }
110         return buf;
111 }
112
113 /* tipc_buf_append(): Append a buffer to the fragment list of another buffer
114  * @*headbuf: in:  NULL for first frag, otherwise value returned from prev call
115  *            out: set when successful non-complete reassembly, otherwise NULL
116  * @*buf:     in:  the buffer to append. Always defined
117  *            out: head buf after successful complete reassembly, otherwise NULL
118  * Returns 1 when reassembly complete, otherwise 0
119  */
120 int tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf)
121 {
122         struct sk_buff *head = *headbuf;
123         struct sk_buff *frag = *buf;
124         struct sk_buff *tail = NULL;
125         struct tipc_msg *msg;
126         u32 fragid;
127         int delta;
128         bool headstolen;
129
130         if (!frag)
131                 goto err;
132
133         msg = buf_msg(frag);
134         fragid = msg_type(msg);
135         frag->next = NULL;
136         skb_pull(frag, msg_hdr_sz(msg));
137
138         if (fragid == FIRST_FRAGMENT) {
139                 if (unlikely(head))
140                         goto err;
141                 *buf = NULL;
142                 if (skb_has_frag_list(frag) && __skb_linearize(frag))
143                         goto err;
144                 frag = skb_unshare(frag, GFP_ATOMIC);
145                 if (unlikely(!frag))
146                         goto err;
147                 head = *headbuf = frag;
148                 TIPC_SKB_CB(head)->tail = NULL;
149                 return 0;
150         }
151
152         if (!head)
153                 goto err;
154
155         if (skb_try_coalesce(head, frag, &headstolen, &delta)) {
156                 kfree_skb_partial(frag, headstolen);
157         } else {
158                 tail = TIPC_SKB_CB(head)->tail;
159                 if (!skb_has_frag_list(head))
160                         skb_shinfo(head)->frag_list = frag;
161                 else
162                         tail->next = frag;
163                 head->truesize += frag->truesize;
164                 head->data_len += frag->len;
165                 head->len += frag->len;
166                 TIPC_SKB_CB(head)->tail = frag;
167         }
168
169         if (fragid == LAST_FRAGMENT) {
170                 TIPC_SKB_CB(head)->validated = false;
171                 if (unlikely(!tipc_msg_validate(head)))
172                         goto err;
173                 *buf = head;
174                 TIPC_SKB_CB(head)->tail = NULL;
175                 *headbuf = NULL;
176                 return 1;
177         }
178         *buf = NULL;
179         return 0;
180 err:
181         kfree_skb(*buf);
182         kfree_skb(*headbuf);
183         *buf = *headbuf = NULL;
184         return 0;
185 }
186
187 /* tipc_msg_validate - validate basic format of received message
188  *
189  * This routine ensures a TIPC message has an acceptable header, and at least
190  * as much data as the header indicates it should.  The routine also ensures
191  * that the entire message header is stored in the main fragment of the message
192  * buffer, to simplify future access to message header fields.
193  *
194  * Note: Having extra info present in the message header or data areas is OK.
195  * TIPC will ignore the excess, under the assumption that it is optional info
196  * introduced by a later release of the protocol.
197  */
198 bool tipc_msg_validate(struct sk_buff *skb)
199 {
200         struct tipc_msg *msg;
201         int msz, hsz;
202
203         if (unlikely(TIPC_SKB_CB(skb)->validated))
204                 return true;
205         if (unlikely(!pskb_may_pull(skb, MIN_H_SIZE)))
206                 return false;
207
208         hsz = msg_hdr_sz(buf_msg(skb));
209         if (unlikely(hsz < MIN_H_SIZE) || (hsz > MAX_H_SIZE))
210                 return false;
211         if (unlikely(!pskb_may_pull(skb, hsz)))
212                 return false;
213
214         msg = buf_msg(skb);
215         if (unlikely(msg_version(msg) != TIPC_VERSION))
216                 return false;
217
218         msz = msg_size(msg);
219         if (unlikely(msz < hsz))
220                 return false;
221         if (unlikely((msz - hsz) > TIPC_MAX_USER_MSG_SIZE))
222                 return false;
223         if (unlikely(skb->len < msz))
224                 return false;
225
226         TIPC_SKB_CB(skb)->validated = true;
227         return true;
228 }
229
230 /**
231  * tipc_msg_build - create buffer chain containing specified header and data
232  * @mhdr: Message header, to be prepended to data
233  * @m: User message
234  * @dsz: Total length of user data
235  * @pktmax: Max packet size that can be used
236  * @list: Buffer or chain of buffers to be returned to caller
237  *
238  * Returns message data size or errno: -ENOMEM, -EFAULT
239  */
240 int tipc_msg_build(struct tipc_msg *mhdr, struct msghdr *m,
241                    int offset, int dsz, int pktmax, struct sk_buff_head *list)
242 {
243         int mhsz = msg_hdr_sz(mhdr);
244         int msz = mhsz + dsz;
245         int pktno = 1;
246         int pktsz;
247         int pktrem = pktmax;
248         int drem = dsz;
249         struct tipc_msg pkthdr;
250         struct sk_buff *skb;
251         char *pktpos;
252         int rc;
253
254         msg_set_size(mhdr, msz);
255
256         /* No fragmentation needed? */
257         if (likely(msz <= pktmax)) {
258                 skb = tipc_buf_acquire(msz);
259                 if (unlikely(!skb))
260                         return -ENOMEM;
261                 skb_orphan(skb);
262                 __skb_queue_tail(list, skb);
263                 skb_copy_to_linear_data(skb, mhdr, mhsz);
264                 pktpos = skb->data + mhsz;
265                 if (copy_from_iter(pktpos, dsz, &m->msg_iter) == dsz)
266                         return dsz;
267                 rc = -EFAULT;
268                 goto error;
269         }
270
271         /* Prepare reusable fragment header */
272         tipc_msg_init(msg_prevnode(mhdr), &pkthdr, MSG_FRAGMENTER,
273                       FIRST_FRAGMENT, INT_H_SIZE, msg_destnode(mhdr));
274         msg_set_size(&pkthdr, pktmax);
275         msg_set_fragm_no(&pkthdr, pktno);
276         msg_set_importance(&pkthdr, msg_importance(mhdr));
277
278         /* Prepare first fragment */
279         skb = tipc_buf_acquire(pktmax);
280         if (!skb)
281                 return -ENOMEM;
282         skb_orphan(skb);
283         __skb_queue_tail(list, skb);
284         pktpos = skb->data;
285         skb_copy_to_linear_data(skb, &pkthdr, INT_H_SIZE);
286         pktpos += INT_H_SIZE;
287         pktrem -= INT_H_SIZE;
288         skb_copy_to_linear_data_offset(skb, INT_H_SIZE, mhdr, mhsz);
289         pktpos += mhsz;
290         pktrem -= mhsz;
291
292         do {
293                 if (drem < pktrem)
294                         pktrem = drem;
295
296                 if (copy_from_iter(pktpos, pktrem, &m->msg_iter) != pktrem) {
297                         rc = -EFAULT;
298                         goto error;
299                 }
300                 drem -= pktrem;
301
302                 if (!drem)
303                         break;
304
305                 /* Prepare new fragment: */
306                 if (drem < (pktmax - INT_H_SIZE))
307                         pktsz = drem + INT_H_SIZE;
308                 else
309                         pktsz = pktmax;
310                 skb = tipc_buf_acquire(pktsz);
311                 if (!skb) {
312                         rc = -ENOMEM;
313                         goto error;
314                 }
315                 skb_orphan(skb);
316                 __skb_queue_tail(list, skb);
317                 msg_set_type(&pkthdr, FRAGMENT);
318                 msg_set_size(&pkthdr, pktsz);
319                 msg_set_fragm_no(&pkthdr, ++pktno);
320                 skb_copy_to_linear_data(skb, &pkthdr, INT_H_SIZE);
321                 pktpos = skb->data + INT_H_SIZE;
322                 pktrem = pktsz - INT_H_SIZE;
323
324         } while (1);
325         msg_set_type(buf_msg(skb), LAST_FRAGMENT);
326         return dsz;
327 error:
328         __skb_queue_purge(list);
329         __skb_queue_head_init(list);
330         return rc;
331 }
332
333 /**
334  * tipc_msg_bundle(): Append contents of a buffer to tail of an existing one
335  * @skb: the buffer to append to ("bundle")
336  * @msg:  message to be appended
337  * @mtu:  max allowable size for the bundle buffer
338  * Consumes buffer if successful
339  * Returns true if bundling could be performed, otherwise false
340  */
341 bool tipc_msg_bundle(struct sk_buff *skb, struct tipc_msg *msg, u32 mtu)
342 {
343         struct tipc_msg *bmsg;
344         unsigned int bsz;
345         unsigned int msz = msg_size(msg);
346         u32 start, pad;
347         u32 max = mtu - INT_H_SIZE;
348
349         if (likely(msg_user(msg) == MSG_FRAGMENTER))
350                 return false;
351         if (!skb)
352                 return false;
353         bmsg = buf_msg(skb);
354         bsz = msg_size(bmsg);
355         start = align(bsz);
356         pad = start - bsz;
357
358         if (unlikely(msg_user(msg) == TUNNEL_PROTOCOL))
359                 return false;
360         if (unlikely(msg_user(msg) == BCAST_PROTOCOL))
361                 return false;
362         if (unlikely(msg_user(bmsg) != MSG_BUNDLER))
363                 return false;
364         if (unlikely(skb_tailroom(skb) < (pad + msz)))
365                 return false;
366         if (unlikely(max < (start + msz)))
367                 return false;
368         if ((msg_importance(msg) < TIPC_SYSTEM_IMPORTANCE) &&
369             (msg_importance(bmsg) == TIPC_SYSTEM_IMPORTANCE))
370                 return false;
371
372         skb_put(skb, pad + msz);
373         skb_copy_to_linear_data_offset(skb, start, msg, msz);
374         msg_set_size(bmsg, start + msz);
375         msg_set_msgcnt(bmsg, msg_msgcnt(bmsg) + 1);
376         return true;
377 }
378
379 /**
380  *  tipc_msg_extract(): extract bundled inner packet from buffer
381  *  @skb: buffer to be extracted from.
382  *  @iskb: extracted inner buffer, to be returned
383  *  @pos: position in outer message of msg to be extracted.
384  *        Returns position of next msg
385  *  Consumes outer buffer when last packet extracted
386  *  Returns true when when there is an extracted buffer, otherwise false
387  */
388 bool tipc_msg_extract(struct sk_buff *skb, struct sk_buff **iskb, int *pos)
389 {
390         struct tipc_msg *msg;
391         int imsz, offset;
392
393         *iskb = NULL;
394         if (unlikely(skb_linearize(skb)))
395                 goto none;
396
397         msg = buf_msg(skb);
398         offset = msg_hdr_sz(msg) + *pos;
399         if (unlikely(offset > (msg_size(msg) - MIN_H_SIZE)))
400                 goto none;
401
402         *iskb = skb_clone(skb, GFP_ATOMIC);
403         if (unlikely(!*iskb))
404                 goto none;
405         skb_pull(*iskb, offset);
406         imsz = msg_size(buf_msg(*iskb));
407         skb_trim(*iskb, imsz);
408         if (unlikely(!tipc_msg_validate(*iskb)))
409                 goto none;
410         *pos += align(imsz);
411         return true;
412 none:
413         kfree_skb(skb);
414         kfree_skb(*iskb);
415         *iskb = NULL;
416         return false;
417 }
418
419 /**
420  * tipc_msg_make_bundle(): Create bundle buf and append message to its tail
421  * @list: the buffer chain, where head is the buffer to replace/append
422  * @skb: buffer to be created, appended to and returned in case of success
423  * @msg: message to be appended
424  * @mtu: max allowable size for the bundle buffer, inclusive header
425  * @dnode: destination node for message. (Not always present in header)
426  * Returns true if success, otherwise false
427  */
428 bool tipc_msg_make_bundle(struct sk_buff **skb,  struct tipc_msg *msg,
429                           u32 mtu, u32 dnode)
430 {
431         struct sk_buff *_skb;
432         struct tipc_msg *bmsg;
433         u32 msz = msg_size(msg);
434         u32 max = mtu - INT_H_SIZE;
435
436         if (msg_user(msg) == MSG_FRAGMENTER)
437                 return false;
438         if (msg_user(msg) == TUNNEL_PROTOCOL)
439                 return false;
440         if (msg_user(msg) == BCAST_PROTOCOL)
441                 return false;
442         if (msz > (max / 2))
443                 return false;
444
445         _skb = tipc_buf_acquire(max);
446         if (!_skb)
447                 return false;
448
449         skb_trim(_skb, INT_H_SIZE);
450         bmsg = buf_msg(_skb);
451         tipc_msg_init(msg_prevnode(msg), bmsg, MSG_BUNDLER, 0,
452                       INT_H_SIZE, dnode);
453         if (msg_isdata(msg))
454                 msg_set_importance(bmsg, TIPC_CRITICAL_IMPORTANCE);
455         else
456                 msg_set_importance(bmsg, TIPC_SYSTEM_IMPORTANCE);
457         msg_set_seqno(bmsg, msg_seqno(msg));
458         msg_set_ack(bmsg, msg_ack(msg));
459         msg_set_bcast_ack(bmsg, msg_bcast_ack(msg));
460         tipc_msg_bundle(_skb, msg, mtu);
461         *skb = _skb;
462         return true;
463 }
464
465 /**
466  * tipc_msg_reverse(): swap source and destination addresses and add error code
467  * @own_node: originating node id for reversed message
468  * @skb:  buffer containing message to be reversed; may be replaced.
469  * @err:  error code to be set in message, if any
470  * Consumes buffer at failure
471  * Returns true if success, otherwise false
472  */
473 bool tipc_msg_reverse(u32 own_node,  struct sk_buff **skb, int err)
474 {
475         struct sk_buff *_skb = *skb;
476         struct tipc_msg *hdr = buf_msg(_skb);
477         struct tipc_msg ohdr;
478         int dlen = min_t(uint, msg_data_sz(hdr), MAX_FORWARD_SIZE);
479
480         if (skb_linearize(_skb))
481                 goto exit;
482         hdr = buf_msg(_skb);
483         if (msg_dest_droppable(hdr))
484                 goto exit;
485         if (msg_errcode(hdr))
486                 goto exit;
487
488         /* Take a copy of original header before altering message */
489         memcpy(&ohdr, hdr, msg_hdr_sz(hdr));
490
491         /* Never return SHORT header; expand by replacing buffer if necessary */
492         if (msg_short(hdr)) {
493                 *skb = tipc_buf_acquire(BASIC_H_SIZE + dlen);
494                 if (!*skb)
495                         goto exit;
496                 memcpy((*skb)->data + BASIC_H_SIZE, msg_data(hdr), dlen);
497                 kfree_skb(_skb);
498                 _skb = *skb;
499                 hdr = buf_msg(_skb);
500                 memcpy(hdr, &ohdr, BASIC_H_SIZE);
501                 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
502         }
503
504         /* Now reverse the concerned fields */
505         msg_set_errcode(hdr, err);
506         msg_set_origport(hdr, msg_destport(&ohdr));
507         msg_set_destport(hdr, msg_origport(&ohdr));
508         msg_set_destnode(hdr, msg_prevnode(&ohdr));
509         msg_set_prevnode(hdr, own_node);
510         msg_set_orignode(hdr, own_node);
511         msg_set_size(hdr, msg_hdr_sz(hdr) + dlen);
512         skb_trim(_skb, msg_size(hdr));
513         skb_orphan(_skb);
514         return true;
515 exit:
516         kfree_skb(_skb);
517         *skb = NULL;
518         return false;
519 }
520
521 /**
522  * tipc_msg_lookup_dest(): try to find new destination for named message
523  * @skb: the buffer containing the message.
524  * @err: error code to be used by caller if lookup fails
525  * Does not consume buffer
526  * Returns true if a destination is found, false otherwise
527  */
528 bool tipc_msg_lookup_dest(struct net *net, struct sk_buff *skb, int *err)
529 {
530         struct tipc_msg *msg = buf_msg(skb);
531         u32 dport, dnode;
532         u32 onode = tipc_own_addr(net);
533
534         if (!msg_isdata(msg))
535                 return false;
536         if (!msg_named(msg))
537                 return false;
538         if (msg_errcode(msg))
539                 return false;
540         *err = TIPC_ERR_NO_NAME;
541         if (skb_linearize(skb))
542                 return false;
543         msg = buf_msg(skb);
544         if (msg_reroute_cnt(msg))
545                 return false;
546         dnode = addr_domain(net, msg_lookup_scope(msg));
547         dport = tipc_nametbl_translate(net, msg_nametype(msg),
548                                        msg_nameinst(msg), &dnode);
549         if (!dport)
550                 return false;
551         msg_incr_reroute_cnt(msg);
552         if (dnode != onode)
553                 msg_set_prevnode(msg, onode);
554         msg_set_destnode(msg, dnode);
555         msg_set_destport(msg, dport);
556         *err = TIPC_OK;
557         return true;
558 }
559
560 /* tipc_msg_reassemble() - clone a buffer chain of fragments and
561  *                         reassemble the clones into one message
562  */
563 bool tipc_msg_reassemble(struct sk_buff_head *list, struct sk_buff_head *rcvq)
564 {
565         struct sk_buff *skb, *_skb;
566         struct sk_buff *frag = NULL;
567         struct sk_buff *head = NULL;
568         int hdr_len;
569
570         /* Copy header if single buffer */
571         if (skb_queue_len(list) == 1) {
572                 skb = skb_peek(list);
573                 hdr_len = skb_headroom(skb) + msg_hdr_sz(buf_msg(skb));
574                 _skb = __pskb_copy(skb, hdr_len, GFP_ATOMIC);
575                 if (!_skb)
576                         return false;
577                 __skb_queue_tail(rcvq, _skb);
578                 return true;
579         }
580
581         /* Clone all fragments and reassemble */
582         skb_queue_walk(list, skb) {
583                 frag = skb_clone(skb, GFP_ATOMIC);
584                 if (!frag)
585                         goto error;
586                 frag->next = NULL;
587                 if (tipc_buf_append(&head, &frag))
588                         break;
589                 if (!head)
590                         goto error;
591         }
592         __skb_queue_tail(rcvq, frag);
593         return true;
594 error:
595         pr_warn("Failed do clone local mcast rcv buffer\n");
596         kfree_skb(head);
597         return false;
598 }
599
600 /* tipc_skb_queue_sorted(); sort pkt into list according to sequence number
601  * @list: list to be appended to
602  * @seqno: sequence number of buffer to add
603  * @skb: buffer to add
604  */
605 void __tipc_skb_queue_sorted(struct sk_buff_head *list, u16 seqno,
606                              struct sk_buff *skb)
607 {
608         struct sk_buff *_skb, *tmp;
609
610         if (skb_queue_empty(list) || less(seqno, buf_seqno(skb_peek(list)))) {
611                 __skb_queue_head(list, skb);
612                 return;
613         }
614
615         if (more(seqno, buf_seqno(skb_peek_tail(list)))) {
616                 __skb_queue_tail(list, skb);
617                 return;
618         }
619
620         skb_queue_walk_safe(list, _skb, tmp) {
621                 if (more(seqno, buf_seqno(_skb)))
622                         continue;
623                 if (seqno == buf_seqno(_skb))
624                         break;
625                 __skb_queue_before(list, _skb, skb);
626                 return;
627         }
628         kfree_skb(skb);
629 }