Staging: batman-adv: Unify sysfs file names with their bat_priv atomics
[cascardo/linux.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 static 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         struct unicast_packet *unicast_packet;
40         int hdr_len = sizeof(struct unicast_packet),
41             uni_diff = sizeof(struct unicast_frag_packet) - hdr_len;
42
43         /* set skb to the first part and tmp_skb to the second part */
44         if (up->flags & UNI_FRAG_HEAD) {
45                 tmp_skb = tfp->skb;
46         } else {
47                 tmp_skb = skb;
48                 skb = tfp->skb;
49         }
50
51         skb_pull(tmp_skb, sizeof(struct unicast_frag_packet));
52         if (pskb_expand_head(skb, 0, tmp_skb->len, GFP_ATOMIC) < 0) {
53                 /* free buffered skb, skb will be freed later */
54                 kfree_skb(tfp->skb);
55                 return NULL;
56         }
57
58         /* move free entry to end */
59         tfp->skb = NULL;
60         tfp->seqno = 0;
61         list_move_tail(&tfp->list, head);
62
63         memcpy(skb_put(skb, tmp_skb->len), tmp_skb->data, tmp_skb->len);
64         kfree_skb(tmp_skb);
65
66         memmove(skb->data + uni_diff, skb->data, hdr_len);
67         unicast_packet = (struct unicast_packet *) skb_pull(skb, uni_diff);
68         unicast_packet->packet_type = BAT_UNICAST;
69
70         return skb;
71 }
72
73 static void frag_create_entry(struct list_head *head, struct sk_buff *skb)
74 {
75         struct frag_packet_list_entry *tfp;
76         struct unicast_frag_packet *up =
77                 (struct unicast_frag_packet *)skb->data;
78
79         /* free and oldest packets stand at the end */
80         tfp = list_entry((head)->prev, typeof(*tfp), list);
81         kfree_skb(tfp->skb);
82
83         tfp->seqno = ntohs(up->seqno);
84         tfp->skb = skb;
85         list_move(&tfp->list, head);
86         return;
87 }
88
89 static int frag_create_buffer(struct list_head *head)
90 {
91         int i;
92         struct frag_packet_list_entry *tfp;
93
94         for (i = 0; i < FRAG_BUFFER_SIZE; i++) {
95                 tfp = kmalloc(sizeof(struct frag_packet_list_entry),
96                         GFP_ATOMIC);
97                 if (!tfp) {
98                         frag_list_free(head);
99                         return -ENOMEM;
100                 }
101                 tfp->skb = NULL;
102                 tfp->seqno = 0;
103                 INIT_LIST_HEAD(&tfp->list);
104                 list_add(&tfp->list, head);
105         }
106
107         return 0;
108 }
109
110 static struct frag_packet_list_entry *frag_search_packet(struct list_head *head,
111                                                  struct unicast_frag_packet *up)
112 {
113         struct frag_packet_list_entry *tfp;
114         struct unicast_frag_packet *tmp_up = NULL;
115         uint16_t search_seqno;
116
117         if (up->flags & UNI_FRAG_HEAD)
118                 search_seqno = ntohs(up->seqno)+1;
119         else
120                 search_seqno = ntohs(up->seqno)-1;
121
122         list_for_each_entry(tfp, head, list) {
123
124                 if (!tfp->skb)
125                         continue;
126
127                 if (tfp->seqno == ntohs(up->seqno))
128                         goto mov_tail;
129
130                 tmp_up = (struct unicast_frag_packet *)tfp->skb->data;
131
132                 if (tfp->seqno == search_seqno) {
133
134                         if ((tmp_up->flags & UNI_FRAG_HEAD) !=
135                             (up->flags & UNI_FRAG_HEAD))
136                                 return tfp;
137                         else
138                                 goto mov_tail;
139                 }
140         }
141         return NULL;
142
143 mov_tail:
144         list_move_tail(&tfp->list, head);
145         return NULL;
146 }
147
148 void frag_list_free(struct list_head *head)
149 {
150         struct frag_packet_list_entry *pf, *tmp_pf;
151
152         if (!list_empty(head)) {
153
154                 list_for_each_entry_safe(pf, tmp_pf, head, list) {
155                         kfree_skb(pf->skb);
156                         list_del(&pf->list);
157                         kfree(pf);
158                 }
159         }
160         return;
161 }
162
163 /* frag_reassemble_skb():
164  * returns NET_RX_DROP if the operation failed - skb is left intact
165  * returns NET_RX_SUCCESS if the fragment was buffered (skb_new will be NULL)
166  * or the skb could be reassembled (skb_new will point to the new packet and
167  * skb was freed)
168  */
169 int frag_reassemble_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
170                         struct sk_buff **new_skb)
171 {
172         unsigned long flags;
173         struct orig_node *orig_node;
174         struct frag_packet_list_entry *tmp_frag_entry;
175         int ret = NET_RX_DROP;
176         struct unicast_frag_packet *unicast_packet =
177                 (struct unicast_frag_packet *)skb->data;
178
179         *new_skb = NULL;
180         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
181         orig_node = ((struct orig_node *)
182                     hash_find(bat_priv->orig_hash, unicast_packet->orig));
183
184         if (!orig_node) {
185                 pr_debug("couldn't find originator in orig_hash\n");
186                 goto out;
187         }
188
189         orig_node->last_frag_packet = jiffies;
190
191         if (list_empty(&orig_node->frag_list) &&
192             frag_create_buffer(&orig_node->frag_list)) {
193                 pr_debug("couldn't create frag buffer\n");
194                 goto out;
195         }
196
197         tmp_frag_entry = frag_search_packet(&orig_node->frag_list,
198                                             unicast_packet);
199
200         if (!tmp_frag_entry) {
201                 frag_create_entry(&orig_node->frag_list, skb);
202                 ret = NET_RX_SUCCESS;
203                 goto out;
204         }
205
206         *new_skb = frag_merge_packet(&orig_node->frag_list, tmp_frag_entry,
207                                      skb);
208         /* if not, merge failed */
209         if (*new_skb)
210                 ret = NET_RX_SUCCESS;
211 out:
212         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
213
214         return ret;
215 }
216
217 int frag_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
218                   struct batman_if *batman_if, uint8_t dstaddr[])
219 {
220         struct unicast_packet tmp_uc, *unicast_packet;
221         struct sk_buff *frag_skb;
222         struct unicast_frag_packet *frag1, *frag2;
223         int uc_hdr_len = sizeof(struct unicast_packet);
224         int ucf_hdr_len = sizeof(struct unicast_frag_packet);
225         int data_len = skb->len;
226
227         if (!bat_priv->primary_if)
228                 goto dropped;
229
230         unicast_packet = (struct unicast_packet *) skb->data;
231
232         memcpy(&tmp_uc, unicast_packet, uc_hdr_len);
233         frag_skb = dev_alloc_skb(data_len - (data_len / 2) + ucf_hdr_len);
234         skb_split(skb, frag_skb, data_len / 2);
235
236         if (my_skb_head_push(skb, ucf_hdr_len - uc_hdr_len) < 0 ||
237             my_skb_head_push(frag_skb, ucf_hdr_len) < 0)
238                 goto drop_frag;
239
240         frag1 = (struct unicast_frag_packet *)skb->data;
241         frag2 = (struct unicast_frag_packet *)frag_skb->data;
242
243         memcpy(frag1, &tmp_uc, sizeof(struct unicast_packet));
244
245         frag1->ttl--;
246         frag1->version = COMPAT_VERSION;
247         frag1->packet_type = BAT_UNICAST_FRAG;
248
249         memcpy(frag1->orig, bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
250         memcpy(frag2, frag1, sizeof(struct unicast_frag_packet));
251
252         frag1->flags |= UNI_FRAG_HEAD;
253         frag2->flags &= ~UNI_FRAG_HEAD;
254
255         frag1->seqno = htons((uint16_t)atomic_inc_return(
256                              &batman_if->frag_seqno));
257         frag2->seqno = htons((uint16_t)atomic_inc_return(
258                              &batman_if->frag_seqno));
259
260         send_skb_packet(skb, batman_if, dstaddr);
261         send_skb_packet(frag_skb, batman_if, dstaddr);
262         return NET_RX_SUCCESS;
263
264 drop_frag:
265         kfree_skb(frag_skb);
266 dropped:
267         kfree_skb(skb);
268         return NET_RX_DROP;
269 }
270
271 int unicast_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv)
272 {
273         struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
274         struct unicast_packet *unicast_packet;
275         struct orig_node *orig_node;
276         struct batman_if *batman_if;
277         struct neigh_node *router;
278         int data_len = skb->len;
279         uint8_t dstaddr[6];
280         unsigned long flags;
281
282         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
283
284         /* get routing information */
285         orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
286                                                    ethhdr->h_dest));
287
288         /* check for hna host */
289         if (!orig_node)
290                 orig_node = transtable_search(bat_priv, ethhdr->h_dest);
291
292         router = find_router(bat_priv, orig_node, NULL);
293
294         if (!router)
295                 goto unlock;
296
297         /* don't lock while sending the packets ... we therefore
298                 * copy the required data before sending */
299
300         batman_if = router->if_incoming;
301         memcpy(dstaddr, router->addr, ETH_ALEN);
302
303         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
304
305         if (batman_if->if_status != IF_ACTIVE)
306                 goto dropped;
307
308         if (my_skb_head_push(skb, sizeof(struct unicast_packet)) < 0)
309                 goto dropped;
310
311         unicast_packet = (struct unicast_packet *)skb->data;
312
313         unicast_packet->version = COMPAT_VERSION;
314         /* batman packet type: unicast */
315         unicast_packet->packet_type = BAT_UNICAST;
316         /* set unicast ttl */
317         unicast_packet->ttl = TTL;
318         /* copy the destination for faster routing */
319         memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
320
321         if (atomic_read(&bat_priv->fragmentation) &&
322             data_len + sizeof(struct unicast_packet) >
323             batman_if->net_dev->mtu) {
324                 /* send frag skb decreases ttl */
325                 unicast_packet->ttl++;
326                 return frag_send_skb(skb, bat_priv, batman_if,
327                                      dstaddr);
328         }
329         send_skb_packet(skb, batman_if, dstaddr);
330         return 0;
331
332 unlock:
333         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
334 dropped:
335         kfree_skb(skb);
336         return 1;
337 }