2 * Copyright (C) 2010 B.A.T.M.A.N. contributors:
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.
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.
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
25 #include "soft-interface.h"
27 #include "translation-table.h"
29 #include "hard-interface.h"
32 static struct sk_buff *frag_merge_packet(struct list_head *head,
33 struct frag_packet_list_entry *tfp,
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;
43 /* set skb to the first part and tmp_skb to the second part */
44 if (up->flags & UNI_FRAG_HEAD) {
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 */
58 /* move free entry to end */
61 list_move_tail(&tfp->list, head);
63 memcpy(skb_put(skb, tmp_skb->len), tmp_skb->data, tmp_skb->len);
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;
73 static void frag_create_entry(struct list_head *head, struct sk_buff *skb)
75 struct frag_packet_list_entry *tfp;
76 struct unicast_frag_packet *up =
77 (struct unicast_frag_packet *)skb->data;
79 /* free and oldest packets stand at the end */
80 tfp = list_entry((head)->prev, typeof(*tfp), list);
83 tfp->seqno = ntohs(up->seqno);
85 list_move(&tfp->list, head);
89 static int frag_create_buffer(struct list_head *head)
92 struct frag_packet_list_entry *tfp;
94 for (i = 0; i < FRAG_BUFFER_SIZE; i++) {
95 tfp = kmalloc(sizeof(struct frag_packet_list_entry),
103 INIT_LIST_HEAD(&tfp->list);
104 list_add(&tfp->list, head);
110 static struct frag_packet_list_entry *frag_search_packet(struct list_head *head,
111 struct unicast_frag_packet *up)
113 struct frag_packet_list_entry *tfp;
114 struct unicast_frag_packet *tmp_up = NULL;
115 uint16_t search_seqno;
117 if (up->flags & UNI_FRAG_HEAD)
118 search_seqno = ntohs(up->seqno)+1;
120 search_seqno = ntohs(up->seqno)-1;
122 list_for_each_entry(tfp, head, list) {
127 if (tfp->seqno == ntohs(up->seqno))
130 tmp_up = (struct unicast_frag_packet *)tfp->skb->data;
132 if (tfp->seqno == search_seqno) {
134 if ((tmp_up->flags & UNI_FRAG_HEAD) !=
135 (up->flags & UNI_FRAG_HEAD))
144 list_move_tail(&tfp->list, head);
148 void frag_list_free(struct list_head *head)
150 struct frag_packet_list_entry *pf, *tmp_pf;
152 if (!list_empty(head)) {
154 list_for_each_entry_safe(pf, tmp_pf, head, list) {
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
169 int frag_reassemble_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
170 struct sk_buff **new_skb)
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;
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));
185 pr_debug("couldn't find originator in orig_hash\n");
189 orig_node->last_frag_packet = jiffies;
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");
197 tmp_frag_entry = frag_search_packet(&orig_node->frag_list,
200 if (!tmp_frag_entry) {
201 frag_create_entry(&orig_node->frag_list, skb);
202 ret = NET_RX_SUCCESS;
206 *new_skb = frag_merge_packet(&orig_node->frag_list, tmp_frag_entry,
208 /* if not, merge failed */
210 ret = NET_RX_SUCCESS;
212 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
217 int frag_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
218 struct batman_if *batman_if, uint8_t dstaddr[])
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;
227 if (!bat_priv->primary_if)
230 unicast_packet = (struct unicast_packet *) skb->data;
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);
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)
240 frag1 = (struct unicast_frag_packet *)skb->data;
241 frag2 = (struct unicast_frag_packet *)frag_skb->data;
243 memcpy(frag1, &tmp_uc, sizeof(struct unicast_packet));
246 frag1->version = COMPAT_VERSION;
247 frag1->packet_type = BAT_UNICAST_FRAG;
249 memcpy(frag1->orig, bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
250 memcpy(frag2, frag1, sizeof(struct unicast_frag_packet));
252 frag1->flags |= UNI_FRAG_HEAD;
253 frag2->flags &= ~UNI_FRAG_HEAD;
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));
260 send_skb_packet(skb, batman_if, dstaddr);
261 send_skb_packet(frag_skb, batman_if, dstaddr);
262 return NET_RX_SUCCESS;
271 int unicast_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv)
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;
282 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
284 /* get routing information */
285 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
288 /* check for hna host */
290 orig_node = transtable_search(bat_priv, ethhdr->h_dest);
292 router = find_router(bat_priv, orig_node, NULL);
297 /* don't lock while sending the packets ... we therefore
298 * copy the required data before sending */
300 batman_if = router->if_incoming;
301 memcpy(dstaddr, router->addr, ETH_ALEN);
303 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
305 if (batman_if->if_status != IF_ACTIVE)
308 if (my_skb_head_push(skb, sizeof(struct unicast_packet)) < 0)
311 unicast_packet = (struct unicast_packet *)skb->data;
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);
321 if (atomic_read(&bat_priv->frag_enabled) &&
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,
329 send_skb_packet(skb, batman_if, dstaddr);
333 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);