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;
40 /* set skb to the first part and tmp_skb to the second part */
41 if (up->flags & UNI_FRAG_HEAD) {
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 */
55 /* move free entry to end */
58 list_move_tail(&tfp->list, head);
60 memcpy(skb_put(skb, tmp_skb->len), tmp_skb->data, tmp_skb->len);
65 static void frag_create_entry(struct list_head *head, struct sk_buff *skb)
67 struct frag_packet_list_entry *tfp;
68 struct unicast_frag_packet *up =
69 (struct unicast_frag_packet *)skb->data;
71 /* free and oldest packets stand at the end */
72 tfp = list_entry((head)->prev, typeof(*tfp), list);
75 tfp->seqno = ntohs(up->seqno);
77 list_move(&tfp->list, head);
81 static int frag_create_buffer(struct list_head *head)
84 struct frag_packet_list_entry *tfp;
86 for (i = 0; i < FRAG_BUFFER_SIZE; i++) {
87 tfp = kmalloc(sizeof(struct frag_packet_list_entry),
95 INIT_LIST_HEAD(&tfp->list);
96 list_add(&tfp->list, head);
102 static struct frag_packet_list_entry *frag_search_packet(struct list_head *head,
103 struct unicast_frag_packet *up)
105 struct frag_packet_list_entry *tfp;
106 struct unicast_frag_packet *tmp_up = NULL;
107 uint16_t search_seqno;
109 if (up->flags & UNI_FRAG_HEAD)
110 search_seqno = ntohs(up->seqno)+1;
112 search_seqno = ntohs(up->seqno)-1;
114 list_for_each_entry(tfp, head, list) {
119 if (tfp->seqno == ntohs(up->seqno))
122 tmp_up = (struct unicast_frag_packet *)tfp->skb->data;
124 if (tfp->seqno == search_seqno) {
126 if ((tmp_up->flags & UNI_FRAG_HEAD) !=
127 (up->flags & UNI_FRAG_HEAD))
136 list_move_tail(&tfp->list, head);
140 void frag_list_free(struct list_head *head)
142 struct frag_packet_list_entry *pf, *tmp_pf;
144 if (!list_empty(head)) {
146 list_for_each_entry_safe(pf, tmp_pf, head, list) {
155 /* frag_reassemble_skb():
156 * returns NET_RX_DROP if the operation failed - skb is left intact
157 * returns NET_RX_SUCCESS if the fragment was buffered (skb_new will be NULL)
158 * or the skb could be reassembled (skb_new will point to the new packet and
161 int frag_reassemble_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
162 struct sk_buff **new_skb)
165 struct orig_node *orig_node;
166 struct frag_packet_list_entry *tmp_frag_entry;
167 int ret = NET_RX_DROP;
168 struct unicast_frag_packet *unicast_packet =
169 (struct unicast_frag_packet *)skb->data;
172 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
173 orig_node = ((struct orig_node *)
174 hash_find(bat_priv->orig_hash, unicast_packet->orig));
177 pr_debug("couldn't find originator in orig_hash\n");
181 orig_node->last_frag_packet = jiffies;
183 if (list_empty(&orig_node->frag_list) &&
184 frag_create_buffer(&orig_node->frag_list)) {
185 pr_debug("couldn't create frag buffer\n");
189 tmp_frag_entry = frag_search_packet(&orig_node->frag_list,
192 if (!tmp_frag_entry) {
193 frag_create_entry(&orig_node->frag_list, skb);
194 ret = NET_RX_SUCCESS;
198 *new_skb = frag_merge_packet(&orig_node->frag_list, tmp_frag_entry,
200 /* if not, merge failed */
202 ret = NET_RX_SUCCESS;
204 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
209 static int frag_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
210 struct batman_if *batman_if, uint8_t dstaddr[])
212 struct unicast_packet tmp_uc, *unicast_packet;
213 struct sk_buff *frag_skb;
214 struct unicast_frag_packet *frag1, *frag2;
215 int uc_hdr_len = sizeof(struct unicast_packet);
216 int ucf_hdr_len = sizeof(struct unicast_frag_packet);
217 int data_len = skb->len;
219 if (!bat_priv->primary_if)
222 unicast_packet = (struct unicast_packet *) skb->data;
224 memcpy(&tmp_uc, unicast_packet, uc_hdr_len);
225 frag_skb = dev_alloc_skb(data_len - (data_len / 2) + ucf_hdr_len);
226 skb_split(skb, frag_skb, data_len / 2);
228 if (my_skb_head_push(skb, ucf_hdr_len - uc_hdr_len) < 0 ||
229 my_skb_head_push(frag_skb, ucf_hdr_len) < 0)
232 frag1 = (struct unicast_frag_packet *)skb->data;
233 frag2 = (struct unicast_frag_packet *)frag_skb->data;
235 memcpy(frag1, &tmp_uc, sizeof(struct unicast_packet));
238 frag1->version = COMPAT_VERSION;
239 frag1->packet_type = BAT_UNICAST_FRAG;
241 memcpy(frag1->orig, bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
242 memcpy(frag2, frag1, sizeof(struct unicast_frag_packet));
244 frag1->flags |= UNI_FRAG_HEAD;
245 frag2->flags &= ~UNI_FRAG_HEAD;
247 frag1->seqno = htons((uint16_t)atomic_inc_return(
248 &batman_if->frag_seqno));
249 frag2->seqno = htons((uint16_t)atomic_inc_return(
250 &batman_if->frag_seqno));
252 send_skb_packet(skb, batman_if, dstaddr);
253 send_skb_packet(frag_skb, batman_if, dstaddr);
254 return NET_RX_SUCCESS;
263 int unicast_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv)
265 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
266 struct unicast_packet *unicast_packet;
267 struct orig_node *orig_node;
268 struct batman_if *batman_if;
269 struct neigh_node *router;
270 int data_len = skb->len;
274 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
276 /* get routing information */
277 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
280 /* check for hna host */
282 orig_node = transtable_search(bat_priv, ethhdr->h_dest);
284 router = find_router(bat_priv, orig_node, NULL);
289 /* don't lock while sending the packets ... we therefore
290 * copy the required data before sending */
292 batman_if = router->if_incoming;
293 memcpy(dstaddr, router->addr, ETH_ALEN);
295 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
297 if (batman_if->if_status != IF_ACTIVE)
300 if (my_skb_head_push(skb, sizeof(struct unicast_packet)) < 0)
303 unicast_packet = (struct unicast_packet *)skb->data;
305 unicast_packet->version = COMPAT_VERSION;
306 /* batman packet type: unicast */
307 unicast_packet->packet_type = BAT_UNICAST;
308 /* set unicast ttl */
309 unicast_packet->ttl = TTL;
310 /* copy the destination for faster routing */
311 memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
313 if (atomic_read(&bat_priv->frag_enabled) &&
314 data_len + sizeof(struct unicast_packet) >
315 batman_if->net_dev->mtu) {
316 /* send frag skb decreases ttl */
317 unicast_packet->ttl++;
318 return frag_send_skb(skb, bat_priv, batman_if,
321 send_skb_packet(skb, batman_if, dstaddr);
325 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);