]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/batman-adv/unicast.c
f951abc1afe6badceeab74dc0aab711fd95eea22
[net-next-2.6.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 *merge_frag_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 create_frag_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 void create_frag_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                 tfp->skb = NULL;
90                 tfp->seqno = 0;
91                 INIT_LIST_HEAD(&tfp->list);
92                 list_add(&tfp->list, head);
93         }
94
95         return;
96 }
97
98 struct frag_packet_list_entry *search_frag_packet(struct list_head *head,
99                                                  struct unicast_frag_packet *up)
100 {
101         struct frag_packet_list_entry *tfp;
102         struct unicast_frag_packet *tmp_up = NULL;
103         uint16_t search_seqno;
104
105         if (up->flags & UNI_FRAG_HEAD)
106                 search_seqno = ntohs(up->seqno)+1;
107         else
108                 search_seqno = ntohs(up->seqno)-1;
109
110         list_for_each_entry(tfp, head, list) {
111
112                 if (!tfp->skb)
113                         continue;
114
115                 if (tfp->seqno == ntohs(up->seqno))
116                         goto mov_tail;
117
118                 tmp_up = (struct unicast_frag_packet *)tfp->skb->data;
119
120                 if (tfp->seqno == search_seqno) {
121
122                         if ((tmp_up->flags & UNI_FRAG_HEAD) !=
123                             (up->flags & UNI_FRAG_HEAD))
124                                 return tfp;
125                         else
126                                 goto mov_tail;
127                 }
128         }
129         return NULL;
130
131 mov_tail:
132         list_move_tail(&tfp->list, head);
133         return NULL;
134 }
135
136 void frag_list_free(struct list_head *head)
137 {
138         struct frag_packet_list_entry *pf, *tmp_pf;
139
140         if (!list_empty(head)) {
141
142                 list_for_each_entry_safe(pf, tmp_pf, head, list) {
143                         kfree_skb(pf->skb);
144                         list_del(&pf->list);
145                         kfree(pf);
146                 }
147         }
148         return;
149 }
150
151 static int unicast_send_frag_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
152                           struct batman_if *batman_if, uint8_t dstaddr[],
153                           struct orig_node *orig_node)
154 {
155         struct unicast_frag_packet *ucast_frag1, *ucast_frag2;
156         int hdr_len = sizeof(struct unicast_frag_packet);
157         struct sk_buff *frag_skb;
158         int data_len = skb->len;
159
160         if (!bat_priv->primary_if)
161                 goto dropped;
162
163         frag_skb = dev_alloc_skb(data_len - (data_len / 2) + hdr_len);
164         skb_split(skb, frag_skb, data_len / 2);
165
166         if (my_skb_head_push(frag_skb, hdr_len) < 0 ||
167             my_skb_head_push(skb, hdr_len) < 0)
168                 goto drop_frag;
169
170         ucast_frag1 = (struct unicast_frag_packet *)skb->data;
171         ucast_frag2 = (struct unicast_frag_packet *)frag_skb->data;
172
173         ucast_frag1->version = COMPAT_VERSION;
174         ucast_frag1->packet_type = BAT_UNICAST_FRAG;
175         ucast_frag1->ttl = TTL;
176         memcpy(ucast_frag1->orig,
177                bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
178         memcpy(ucast_frag1->dest, orig_node->orig, ETH_ALEN);
179
180         memcpy(ucast_frag2, ucast_frag1, sizeof(struct unicast_frag_packet));
181
182         ucast_frag1->flags |= UNI_FRAG_HEAD;
183         ucast_frag2->flags &= ~UNI_FRAG_HEAD;
184
185         ucast_frag1->seqno = htons((uint16_t)atomic_inc_return(
186                                                 &batman_if->frag_seqno));
187
188         ucast_frag2->seqno = htons((uint16_t)atomic_inc_return(
189                                                 &batman_if->frag_seqno));
190
191         send_skb_packet(skb, batman_if, dstaddr);
192         send_skb_packet(frag_skb, batman_if, dstaddr);
193         return 0;
194
195 drop_frag:
196         kfree_skb(frag_skb);
197 dropped:
198         kfree_skb(skb);
199         return 1;
200 }
201
202 int unicast_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv)
203 {
204         struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
205         struct unicast_packet *unicast_packet;
206         struct orig_node *orig_node;
207         struct batman_if *batman_if;
208         struct neigh_node *router;
209         int data_len = skb->len;
210         uint8_t dstaddr[6];
211         unsigned long flags;
212
213         spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
214
215         /* get routing information */
216         orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
217                                                    ethhdr->h_dest));
218
219         /* check for hna host */
220         if (!orig_node)
221                 orig_node = transtable_search(bat_priv, ethhdr->h_dest);
222
223         router = find_router(orig_node, NULL);
224
225         if (!router)
226                 goto unlock;
227
228         /* don't lock while sending the packets ... we therefore
229                 * copy the required data before sending */
230
231         batman_if = router->if_incoming;
232         memcpy(dstaddr, router->addr, ETH_ALEN);
233
234         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
235
236         if (batman_if->if_status != IF_ACTIVE)
237                 goto dropped;
238
239         if (atomic_read(&bat_priv->frag_enabled) &&
240             data_len + sizeof(struct unicast_packet) > batman_if->net_dev->mtu)
241                 return unicast_send_frag_skb(skb, bat_priv, batman_if,
242                                              dstaddr, orig_node);
243
244         if (my_skb_head_push(skb, sizeof(struct unicast_packet)) < 0)
245                 goto dropped;
246
247         unicast_packet = (struct unicast_packet *)skb->data;
248
249         unicast_packet->version = COMPAT_VERSION;
250         /* batman packet type: unicast */
251         unicast_packet->packet_type = BAT_UNICAST;
252         /* set unicast ttl */
253         unicast_packet->ttl = TTL;
254         /* copy the destination for faster routing */
255         memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
256
257         send_skb_packet(skb, batman_if, dstaddr);
258         return 0;
259
260 unlock:
261         spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
262 dropped:
263         kfree_skb(skb);
264         return 1;
265 }