Staging: batman-adv: add frag_ prefix to all fragmentation related functions
[firefly-linux-kernel-4.4.55.git] / drivers / staging / batman-adv / unicast.c
1 /*
2  * Copyright (C) 2010 B.A.T.M.A.N. contributors:
3  *
4  * Andreas Langer
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA
19  *
20  */
21
22 #include "main.h"
23 #include "unicast.h"
24 #include "send.h"
25 #include "soft-interface.h"
26 #include "hash.h"
27 #include "translation-table.h"
28 #include "routing.h"
29 #include "hard-interface.h"
30
31
32 struct sk_buff *frag_merge_packet(struct list_head *head,
33                                   struct frag_packet_list_entry *tfp,
34                                   struct sk_buff *skb)
35 {
36         struct unicast_frag_packet *up =
37                 (struct unicast_frag_packet *)skb->data;
38         struct sk_buff *tmp_skb;
39
40         /* set skb to the first part and tmp_skb to the second part */
41         if (up->flags & UNI_FRAG_HEAD) {
42                 tmp_skb = tfp->skb;
43         } else {
44                 tmp_skb = skb;
45                 skb = tfp->skb;
46         }
47
48         skb_pull(tmp_skb, sizeof(struct unicast_frag_packet));
49         if (pskb_expand_head(skb, 0, tmp_skb->len, GFP_ATOMIC) < 0) {
50                 /* free buffered skb, skb will be freed later */
51                 kfree_skb(tfp->skb);
52                 return NULL;
53         }
54
55         /* move free entry to end */
56         tfp->skb = NULL;
57         tfp->seqno = 0;
58         list_move_tail(&tfp->list, head);
59
60         memcpy(skb_put(skb, tmp_skb->len), tmp_skb->data, tmp_skb->len);
61         kfree_skb(tmp_skb);
62         return skb;
63 }
64
65 void frag_create_entry(struct list_head *head, struct sk_buff *skb)
66 {
67         struct frag_packet_list_entry *tfp;
68         struct unicast_frag_packet *up =
69                 (struct unicast_frag_packet *)skb->data;
70
71         /* free and oldest packets stand at the end */
72         tfp = list_entry((head)->prev, typeof(*tfp), list);
73         kfree_skb(tfp->skb);
74
75         tfp->seqno = ntohs(up->seqno);
76         tfp->skb = skb;
77         list_move(&tfp->list, head);
78         return;
79 }
80
81 int frag_create_buffer(struct list_head *head)
82 {
83         int i;
84         struct frag_packet_list_entry *tfp;
85
86         for (i = 0; i < FRAG_BUFFER_SIZE; i++) {
87                 tfp = kmalloc(sizeof(struct frag_packet_list_entry),
88                         GFP_ATOMIC);
89                 if (!tfp) {
90                         frag_list_free(head);
91                         return -ENOMEM;
92                 }
93                 tfp->skb = NULL;
94                 tfp->seqno = 0;
95                 INIT_LIST_HEAD(&tfp->list);
96                 list_add(&tfp->list, head);
97         }
98
99         return 0;
100 }
101
102 struct frag_packet_list_entry *frag_search_packet(struct list_head *head,
103                                                  struct unicast_frag_packet *up)
104 {
105         struct frag_packet_list_entry *tfp;
106         struct unicast_frag_packet *tmp_up = NULL;
107         uint16_t search_seqno;
108
109         if (up->flags & UNI_FRAG_HEAD)
110                 search_seqno = ntohs(up->seqno)+1;
111         else
112                 search_seqno = ntohs(up->seqno)-1;
113
114         list_for_each_entry(tfp, head, list) {
115
116                 if (!tfp->skb)
117                         continue;
118
119                 if (tfp->seqno == ntohs(up->seqno))
120                         goto mov_tail;
121
122                 tmp_up = (struct unicast_frag_packet *)tfp->skb->data;
123
124                 if (tfp->seqno == search_seqno) {
125
126                         if ((tmp_up->flags & UNI_FRAG_HEAD) !=
127                             (up->flags & UNI_FRAG_HEAD))
128                                 return tfp;
129                         else
130                                 goto mov_tail;
131                 }
132         }
133         return NULL;
134
135 mov_tail:
136         list_move_tail(&tfp->list, head);
137         return NULL;
138 }
139
140 void frag_list_free(struct list_head *head)
141 {
142         struct frag_packet_list_entry *pf, *tmp_pf;
143
144         if (!list_empty(head)) {
145
146                 list_for_each_entry_safe(pf, tmp_pf, head, list) {
147                         kfree_skb(pf->skb);
148                         list_del(&pf->list);
149                         kfree(pf);
150                 }
151         }
152         return;
153 }
154
155 static int frag_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
156                          struct batman_if *batman_if, uint8_t dstaddr[])
157 {
158         struct unicast_packet tmp_uc, *unicast_packet;
159         struct sk_buff *frag_skb;
160         struct unicast_frag_packet *frag1, *frag2;
161         int uc_hdr_len = sizeof(struct unicast_packet);
162         int ucf_hdr_len = sizeof(struct unicast_frag_packet);
163         int data_len = skb->len;
164
165         if (!bat_priv->primary_if)
166                 goto dropped;
167
168         unicast_packet = (struct unicast_packet *) skb->data;
169
170         memcpy(&tmp_uc, unicast_packet, uc_hdr_len);
171         frag_skb = dev_alloc_skb(data_len - (data_len / 2) + ucf_hdr_len);
172         skb_split(skb, frag_skb, data_len / 2);
173
174         if (my_skb_head_push(skb, ucf_hdr_len - uc_hdr_len) < 0 ||
175             my_skb_head_push(frag_skb, ucf_hdr_len) < 0)
176                 goto drop_frag;
177
178         frag1 = (struct unicast_frag_packet *)skb->data;
179         frag2 = (struct unicast_frag_packet *)frag_skb->data;
180
181         memcpy(frag1, &tmp_uc, sizeof(struct unicast_packet));
182
183         frag1->ttl--;
184         frag1->version = COMPAT_VERSION;
185         frag1->packet_type = BAT_UNICAST_FRAG;
186
187         memcpy(frag1->orig, bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
188         memcpy(frag2, frag1, sizeof(struct unicast_frag_packet));
189
190         frag1->flags |= UNI_FRAG_HEAD;
191         frag2->flags &= ~UNI_FRAG_HEAD;
192
193         frag1->seqno = htons((uint16_t)atomic_inc_return(
194                              &batman_if->frag_seqno));
195         frag2->seqno = htons((uint16_t)atomic_inc_return(
196                              &batman_if->frag_seqno));
197
198         send_skb_packet(skb, batman_if, dstaddr);
199         send_skb_packet(frag_skb, batman_if, dstaddr);
200         return NET_RX_SUCCESS;
201
202 drop_frag:
203         kfree_skb(frag_skb);
204 dropped:
205         kfree_skb(skb);
206         return NET_RX_DROP;
207 }
208
209 int unicast_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv)
210 {
211         struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
212         struct unicast_packet *unicast_packet;
213         struct orig_node *orig_node;
214         struct batman_if *batman_if;
215         struct neigh_node *router;
216         int data_len = skb->len;
217         uint8_t dstaddr[6];
218         unsigned long flags;
219
220         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
221
222         /* get routing information */
223         orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
224                                                    ethhdr->h_dest));
225
226         /* check for hna host */
227         if (!orig_node)
228                 orig_node = transtable_search(bat_priv, ethhdr->h_dest);
229
230         router = find_router(bat_priv, orig_node, NULL);
231
232         if (!router)
233                 goto unlock;
234
235         /* don't lock while sending the packets ... we therefore
236                 * copy the required data before sending */
237
238         batman_if = router->if_incoming;
239         memcpy(dstaddr, router->addr, ETH_ALEN);
240
241         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
242
243         if (batman_if->if_status != IF_ACTIVE)
244                 goto dropped;
245
246         if (my_skb_head_push(skb, sizeof(struct unicast_packet)) < 0)
247                 goto dropped;
248
249         unicast_packet = (struct unicast_packet *)skb->data;
250
251         unicast_packet->version = COMPAT_VERSION;
252         /* batman packet type: unicast */
253         unicast_packet->packet_type = BAT_UNICAST;
254         /* set unicast ttl */
255         unicast_packet->ttl = TTL;
256         /* copy the destination for faster routing */
257         memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
258
259         if (atomic_read(&bat_priv->frag_enabled) &&
260             data_len + sizeof(struct unicast_packet) >
261             batman_if->net_dev->mtu) {
262                 /* send frag skb decreases ttl */
263                 unicast_packet->ttl++;
264                 return frag_send_skb(skb, bat_priv, batman_if,
265                                      dstaddr);
266         }
267         send_skb_packet(skb, batman_if, dstaddr);
268         return 0;
269
270 unlock:
271         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
272 dropped:
273         kfree_skb(skb);
274         return 1;
275 }