1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "translation-table.h"
24 #include "soft-interface.h"
25 #include "hard-interface.h"
27 #include "gateway_common.h"
28 #include "originator.h"
30 static void batadv_send_outstanding_bcast_packet(struct work_struct
*work
);
32 /* send out an already prepared packet to the given address via the
33 * specified batman interface
35 int batadv_send_skb_packet(struct sk_buff
*skb
,
36 struct batadv_hard_iface
*hard_iface
,
37 const uint8_t *dst_addr
)
39 struct ethhdr
*ethhdr
;
41 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
44 if (unlikely(!hard_iface
->net_dev
))
47 if (!(hard_iface
->net_dev
->flags
& IFF_UP
)) {
48 pr_warn("Interface %s is not up - can't send packet via that interface!\n",
49 hard_iface
->net_dev
->name
);
53 /* push to the ethernet header. */
54 if (batadv_skb_head_push(skb
, ETH_HLEN
) < 0)
57 skb_reset_mac_header(skb
);
59 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
60 memcpy(ethhdr
->h_source
, hard_iface
->net_dev
->dev_addr
, ETH_ALEN
);
61 memcpy(ethhdr
->h_dest
, dst_addr
, ETH_ALEN
);
62 ethhdr
->h_proto
= __constant_htons(BATADV_ETH_P_BATMAN
);
64 skb_set_network_header(skb
, ETH_HLEN
);
65 skb
->priority
= TC_PRIO_CONTROL
;
66 skb
->protocol
= __constant_htons(BATADV_ETH_P_BATMAN
);
68 skb
->dev
= hard_iface
->net_dev
;
70 /* dev_queue_xmit() returns a negative result on error. However on
71 * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
72 * (which is > 0). This will not be treated as an error.
74 return dev_queue_xmit(skb
);
80 void batadv_schedule_bat_ogm(struct batadv_hard_iface
*hard_iface
)
82 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
84 if ((hard_iface
->if_status
== BATADV_IF_NOT_IN_USE
) ||
85 (hard_iface
->if_status
== BATADV_IF_TO_BE_REMOVED
))
88 /* the interface gets activated here to avoid race conditions between
89 * the moment of activating the interface in
90 * hardif_activate_interface() where the originator mac is set and
91 * outdated packets (especially uninitialized mac addresses) in the
94 if (hard_iface
->if_status
== BATADV_IF_TO_BE_ACTIVATED
)
95 hard_iface
->if_status
= BATADV_IF_ACTIVE
;
97 bat_priv
->bat_algo_ops
->bat_ogm_schedule(hard_iface
);
100 static void batadv_forw_packet_free(struct batadv_forw_packet
*forw_packet
)
102 if (forw_packet
->skb
)
103 kfree_skb(forw_packet
->skb
);
104 if (forw_packet
->if_incoming
)
105 batadv_hardif_free_ref(forw_packet
->if_incoming
);
110 _batadv_add_bcast_packet_to_list(struct batadv_priv
*bat_priv
,
111 struct batadv_forw_packet
*forw_packet
,
112 unsigned long send_time
)
114 INIT_HLIST_NODE(&forw_packet
->list
);
116 /* add new packet to packet list */
117 spin_lock_bh(&bat_priv
->forw_bcast_list_lock
);
118 hlist_add_head(&forw_packet
->list
, &bat_priv
->forw_bcast_list
);
119 spin_unlock_bh(&bat_priv
->forw_bcast_list_lock
);
121 /* start timer for this packet */
122 INIT_DELAYED_WORK(&forw_packet
->delayed_work
,
123 batadv_send_outstanding_bcast_packet
);
124 queue_delayed_work(batadv_event_workqueue
, &forw_packet
->delayed_work
,
128 /* add a broadcast packet to the queue and setup timers. broadcast packets
129 * are sent multiple times to increase probability for being received.
131 * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
134 * The skb is not consumed, so the caller should make sure that the
137 int batadv_add_bcast_packet_to_list(struct batadv_priv
*bat_priv
,
138 const struct sk_buff
*skb
,
141 struct batadv_hard_iface
*primary_if
= NULL
;
142 struct batadv_forw_packet
*forw_packet
;
143 struct batadv_bcast_packet
*bcast_packet
;
144 struct sk_buff
*newskb
;
146 if (!batadv_atomic_dec_not_zero(&bat_priv
->bcast_queue_left
)) {
147 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
148 "bcast packet queue full\n");
152 primary_if
= batadv_primary_if_get_selected(bat_priv
);
156 forw_packet
= kmalloc(sizeof(*forw_packet
), GFP_ATOMIC
);
161 newskb
= skb_copy(skb
, GFP_ATOMIC
);
165 /* as we have a copy now, it is safe to decrease the TTL */
166 bcast_packet
= (struct batadv_bcast_packet
*)newskb
->data
;
167 bcast_packet
->header
.ttl
--;
169 skb_reset_mac_header(newskb
);
171 forw_packet
->skb
= newskb
;
172 forw_packet
->if_incoming
= primary_if
;
174 /* how often did we send the bcast packet ? */
175 forw_packet
->num_packets
= 0;
177 _batadv_add_bcast_packet_to_list(bat_priv
, forw_packet
, delay
);
183 atomic_inc(&bat_priv
->bcast_queue_left
);
186 batadv_hardif_free_ref(primary_if
);
187 return NETDEV_TX_BUSY
;
190 static void batadv_send_outstanding_bcast_packet(struct work_struct
*work
)
192 struct batadv_hard_iface
*hard_iface
;
193 struct delayed_work
*delayed_work
=
194 container_of(work
, struct delayed_work
, work
);
195 struct batadv_forw_packet
*forw_packet
;
196 struct sk_buff
*skb1
;
197 struct net_device
*soft_iface
;
198 struct batadv_priv
*bat_priv
;
200 forw_packet
= container_of(delayed_work
, struct batadv_forw_packet
,
202 soft_iface
= forw_packet
->if_incoming
->soft_iface
;
203 bat_priv
= netdev_priv(soft_iface
);
205 spin_lock_bh(&bat_priv
->forw_bcast_list_lock
);
206 hlist_del(&forw_packet
->list
);
207 spin_unlock_bh(&bat_priv
->forw_bcast_list_lock
);
209 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_DEACTIVATING
)
212 /* rebroadcast packet */
214 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
215 if (hard_iface
->soft_iface
!= soft_iface
)
218 /* send a copy of the saved skb */
219 skb1
= skb_clone(forw_packet
->skb
, GFP_ATOMIC
);
221 batadv_send_skb_packet(skb1
, hard_iface
,
222 batadv_broadcast_addr
);
226 forw_packet
->num_packets
++;
228 /* if we still have some more bcasts to send */
229 if (forw_packet
->num_packets
< 3) {
230 _batadv_add_bcast_packet_to_list(bat_priv
, forw_packet
,
231 msecs_to_jiffies(5));
236 batadv_forw_packet_free(forw_packet
);
237 atomic_inc(&bat_priv
->bcast_queue_left
);
240 void batadv_send_outstanding_bat_ogm_packet(struct work_struct
*work
)
242 struct delayed_work
*delayed_work
=
243 container_of(work
, struct delayed_work
, work
);
244 struct batadv_forw_packet
*forw_packet
;
245 struct batadv_priv
*bat_priv
;
247 forw_packet
= container_of(delayed_work
, struct batadv_forw_packet
,
249 bat_priv
= netdev_priv(forw_packet
->if_incoming
->soft_iface
);
250 spin_lock_bh(&bat_priv
->forw_bat_list_lock
);
251 hlist_del(&forw_packet
->list
);
252 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
254 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_DEACTIVATING
)
257 bat_priv
->bat_algo_ops
->bat_ogm_emit(forw_packet
);
259 /* we have to have at least one packet in the queue
260 * to determine the queues wake up time unless we are
263 if (forw_packet
->own
)
264 batadv_schedule_bat_ogm(forw_packet
->if_incoming
);
267 /* don't count own packet */
268 if (!forw_packet
->own
)
269 atomic_inc(&bat_priv
->batman_queue_left
);
271 batadv_forw_packet_free(forw_packet
);
275 batadv_purge_outstanding_packets(struct batadv_priv
*bat_priv
,
276 const struct batadv_hard_iface
*hard_iface
)
278 struct batadv_forw_packet
*forw_packet
;
279 struct hlist_node
*tmp_node
, *safe_tmp_node
;
283 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
284 "purge_outstanding_packets(): %s\n",
285 hard_iface
->net_dev
->name
);
287 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
288 "purge_outstanding_packets()\n");
290 /* free bcast list */
291 spin_lock_bh(&bat_priv
->forw_bcast_list_lock
);
292 hlist_for_each_entry_safe(forw_packet
, tmp_node
, safe_tmp_node
,
293 &bat_priv
->forw_bcast_list
, list
) {
295 /* if purge_outstanding_packets() was called with an argument
296 * we delete only packets belonging to the given interface
299 (forw_packet
->if_incoming
!= hard_iface
))
302 spin_unlock_bh(&bat_priv
->forw_bcast_list_lock
);
304 /* batadv_send_outstanding_bcast_packet() will lock the list to
305 * delete the item from the list
307 pending
= cancel_delayed_work_sync(&forw_packet
->delayed_work
);
308 spin_lock_bh(&bat_priv
->forw_bcast_list_lock
);
311 hlist_del(&forw_packet
->list
);
312 batadv_forw_packet_free(forw_packet
);
315 spin_unlock_bh(&bat_priv
->forw_bcast_list_lock
);
317 /* free batman packet list */
318 spin_lock_bh(&bat_priv
->forw_bat_list_lock
);
319 hlist_for_each_entry_safe(forw_packet
, tmp_node
, safe_tmp_node
,
320 &bat_priv
->forw_bat_list
, list
) {
322 /* if purge_outstanding_packets() was called with an argument
323 * we delete only packets belonging to the given interface
326 (forw_packet
->if_incoming
!= hard_iface
))
329 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
331 /* send_outstanding_bat_packet() will lock the list to
332 * delete the item from the list
334 pending
= cancel_delayed_work_sync(&forw_packet
->delayed_work
);
335 spin_lock_bh(&bat_priv
->forw_bat_list_lock
);
338 hlist_del(&forw_packet
->list
);
339 batadv_forw_packet_free(forw_packet
);
342 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);