batman-adv: make batadv_test_bit() return 0 or 1 only
[deliverable/linux.git] / net / batman-adv / send.c
CommitLineData
9cfc7bd6 1/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
c6c8fea2
SE
2 *
3 * Marek Lindner, Simon Wunderlich
4 *
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.
8 *
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.
13 *
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
17 * 02110-1301, USA
c6c8fea2
SE
18 */
19
20#include "main.h"
21#include "send.h"
22#include "routing.h"
23#include "translation-table.h"
24#include "soft-interface.h"
25#include "hard-interface.h"
c6c8fea2 26#include "vis.h"
c6c8fea2
SE
27#include "gateway_common.h"
28#include "originator.h"
29
bb079c82 30static void batadv_send_outstanding_bcast_packet(struct work_struct *work);
c6c8fea2 31
c6c8fea2 32/* send out an already prepared packet to the given address via the
9cfc7bd6
SE
33 * specified batman interface
34 */
56303d34
SE
35int batadv_send_skb_packet(struct sk_buff *skb,
36 struct batadv_hard_iface *hard_iface,
9455e34c 37 const uint8_t *dst_addr)
c6c8fea2
SE
38{
39 struct ethhdr *ethhdr;
40
e9a4f295 41 if (hard_iface->if_status != BATADV_IF_ACTIVE)
c6c8fea2
SE
42 goto send_skb_err;
43
e6c10f43 44 if (unlikely(!hard_iface->net_dev))
c6c8fea2
SE
45 goto send_skb_err;
46
e6c10f43 47 if (!(hard_iface->net_dev->flags & IFF_UP)) {
67969581
SE
48 pr_warn("Interface %s is not up - can't send packet via that interface!\n",
49 hard_iface->net_dev->name);
c6c8fea2
SE
50 goto send_skb_err;
51 }
52
53 /* push to the ethernet header. */
04b482a2 54 if (batadv_skb_head_push(skb, ETH_HLEN) < 0)
c6c8fea2
SE
55 goto send_skb_err;
56
57 skb_reset_mac_header(skb);
58
40e0c4f5 59 ethhdr = (struct ethhdr *)skb_mac_header(skb);
e6c10f43 60 memcpy(ethhdr->h_source, hard_iface->net_dev->dev_addr, ETH_ALEN);
c6c8fea2 61 memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN);
7e071c79 62 ethhdr->h_proto = __constant_htons(BATADV_ETH_P_BATMAN);
c6c8fea2
SE
63
64 skb_set_network_header(skb, ETH_HLEN);
65 skb->priority = TC_PRIO_CONTROL;
7e071c79 66 skb->protocol = __constant_htons(BATADV_ETH_P_BATMAN);
c6c8fea2 67
e6c10f43 68 skb->dev = hard_iface->net_dev;
c6c8fea2
SE
69
70 /* dev_queue_xmit() returns a negative result on error. However on
71 * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
9cfc7bd6
SE
72 * (which is > 0). This will not be treated as an error.
73 */
c6c8fea2
SE
74 return dev_queue_xmit(skb);
75send_skb_err:
76 kfree_skb(skb);
77 return NET_XMIT_DROP;
78}
79
56303d34 80void batadv_schedule_bat_ogm(struct batadv_hard_iface *hard_iface)
c6c8fea2 81{
56303d34 82 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
c6c8fea2 83
e9a4f295
SE
84 if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
85 (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
c6c8fea2
SE
86 return;
87
9cfc7bd6 88 /* the interface gets activated here to avoid race conditions between
c6c8fea2
SE
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
92 * packet queue
93 */
e9a4f295
SE
94 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
95 hard_iface->if_status = BATADV_IF_ACTIVE;
c6c8fea2 96
be9aa4c1 97 bat_priv->bat_algo_ops->bat_ogm_schedule(hard_iface);
c6c8fea2
SE
98}
99
56303d34 100static void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet)
c6c8fea2
SE
101{
102 if (forw_packet->skb)
103 kfree_skb(forw_packet->skb);
6d5808d4 104 if (forw_packet->if_incoming)
e5d89254 105 batadv_hardif_free_ref(forw_packet->if_incoming);
c6c8fea2
SE
106 kfree(forw_packet);
107}
108
56303d34
SE
109static void
110_batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
111 struct batadv_forw_packet *forw_packet,
112 unsigned long send_time)
c6c8fea2
SE
113{
114 INIT_HLIST_NODE(&forw_packet->list);
115
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);
120
121 /* start timer for this packet */
122 INIT_DELAYED_WORK(&forw_packet->delayed_work,
bb079c82 123 batadv_send_outstanding_bcast_packet);
3193e8fd 124 queue_delayed_work(batadv_event_workqueue, &forw_packet->delayed_work,
c6c8fea2
SE
125 send_time);
126}
127
c6c8fea2 128/* add a broadcast packet to the queue and setup timers. broadcast packets
015758d0 129 * are sent multiple times to increase probability for being received.
c6c8fea2
SE
130 *
131 * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
132 * errors.
133 *
134 * The skb is not consumed, so the caller should make sure that the
9cfc7bd6
SE
135 * skb is freed.
136 */
56303d34 137int batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
9455e34c
SE
138 const struct sk_buff *skb,
139 unsigned long delay)
c6c8fea2 140{
56303d34
SE
141 struct batadv_hard_iface *primary_if = NULL;
142 struct batadv_forw_packet *forw_packet;
96412690 143 struct batadv_bcast_packet *bcast_packet;
747e4221 144 struct sk_buff *newskb;
c6c8fea2 145
3e34819e 146 if (!batadv_atomic_dec_not_zero(&bat_priv->bcast_queue_left)) {
39c75a51
SE
147 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
148 "bcast packet queue full\n");
c6c8fea2
SE
149 goto out;
150 }
151
e5d89254 152 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22 153 if (!primary_if)
ca06c6eb 154 goto out_and_inc;
c6c8fea2 155
704509b8 156 forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);
c6c8fea2
SE
157
158 if (!forw_packet)
159 goto out_and_inc;
160
747e4221
SE
161 newskb = skb_copy(skb, GFP_ATOMIC);
162 if (!newskb)
c6c8fea2
SE
163 goto packet_free;
164
165 /* as we have a copy now, it is safe to decrease the TTL */
96412690 166 bcast_packet = (struct batadv_bcast_packet *)newskb->data;
76543d14 167 bcast_packet->header.ttl--;
c6c8fea2 168
747e4221 169 skb_reset_mac_header(newskb);
c6c8fea2 170
747e4221 171 forw_packet->skb = newskb;
32ae9b22 172 forw_packet->if_incoming = primary_if;
c6c8fea2
SE
173
174 /* how often did we send the bcast packet ? */
175 forw_packet->num_packets = 0;
176
bb079c82 177 _batadv_add_bcast_packet_to_list(bat_priv, forw_packet, delay);
c6c8fea2
SE
178 return NETDEV_TX_OK;
179
180packet_free:
181 kfree(forw_packet);
182out_and_inc:
183 atomic_inc(&bat_priv->bcast_queue_left);
184out:
32ae9b22 185 if (primary_if)
e5d89254 186 batadv_hardif_free_ref(primary_if);
c6c8fea2
SE
187 return NETDEV_TX_BUSY;
188}
189
bb079c82 190static void batadv_send_outstanding_bcast_packet(struct work_struct *work)
c6c8fea2 191{
56303d34 192 struct batadv_hard_iface *hard_iface;
c6c8fea2
SE
193 struct delayed_work *delayed_work =
194 container_of(work, struct delayed_work, work);
56303d34 195 struct batadv_forw_packet *forw_packet;
c6c8fea2 196 struct sk_buff *skb1;
56303d34
SE
197 struct net_device *soft_iface;
198 struct batadv_priv *bat_priv;
199
200 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
201 delayed_work);
202 soft_iface = forw_packet->if_incoming->soft_iface;
203 bat_priv = netdev_priv(soft_iface);
c6c8fea2
SE
204
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);
208
39c75a51 209 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
c6c8fea2
SE
210 goto out;
211
212 /* rebroadcast packet */
213 rcu_read_lock();
3193e8fd 214 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
e6c10f43 215 if (hard_iface->soft_iface != soft_iface)
c6c8fea2
SE
216 continue;
217
218 /* send a copy of the saved skb */
219 skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
220 if (skb1)
9455e34c 221 batadv_send_skb_packet(skb1, hard_iface,
3193e8fd 222 batadv_broadcast_addr);
c6c8fea2
SE
223 }
224 rcu_read_unlock();
225
226 forw_packet->num_packets++;
227
228 /* if we still have some more bcasts to send */
229 if (forw_packet->num_packets < 3) {
bb079c82
SE
230 _batadv_add_bcast_packet_to_list(bat_priv, forw_packet,
231 msecs_to_jiffies(5));
c6c8fea2
SE
232 return;
233 }
234
235out:
bb079c82 236 batadv_forw_packet_free(forw_packet);
c6c8fea2
SE
237 atomic_inc(&bat_priv->bcast_queue_left);
238}
239
9455e34c 240void batadv_send_outstanding_bat_ogm_packet(struct work_struct *work)
c6c8fea2
SE
241{
242 struct delayed_work *delayed_work =
243 container_of(work, struct delayed_work, work);
56303d34
SE
244 struct batadv_forw_packet *forw_packet;
245 struct batadv_priv *bat_priv;
c6c8fea2 246
56303d34
SE
247 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
248 delayed_work);
c6c8fea2
SE
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);
253
39c75a51 254 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
c6c8fea2
SE
255 goto out;
256
01c4224b 257 bat_priv->bat_algo_ops->bat_ogm_emit(forw_packet);
c6c8fea2 258
9cfc7bd6 259 /* we have to have at least one packet in the queue
c6c8fea2
SE
260 * to determine the queues wake up time unless we are
261 * shutting down
262 */
263 if (forw_packet->own)
9455e34c 264 batadv_schedule_bat_ogm(forw_packet->if_incoming);
c6c8fea2
SE
265
266out:
267 /* don't count own packet */
268 if (!forw_packet->own)
269 atomic_inc(&bat_priv->batman_queue_left);
270
bb079c82 271 batadv_forw_packet_free(forw_packet);
c6c8fea2
SE
272}
273
56303d34
SE
274void
275batadv_purge_outstanding_packets(struct batadv_priv *bat_priv,
276 const struct batadv_hard_iface *hard_iface)
c6c8fea2 277{
56303d34 278 struct batadv_forw_packet *forw_packet;
c6c8fea2 279 struct hlist_node *tmp_node, *safe_tmp_node;
6d5808d4 280 bool pending;
c6c8fea2 281
e6c10f43 282 if (hard_iface)
39c75a51 283 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
284 "purge_outstanding_packets(): %s\n",
285 hard_iface->net_dev->name);
c6c8fea2 286 else
39c75a51 287 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 288 "purge_outstanding_packets()\n");
c6c8fea2
SE
289
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) {
294
9cfc7bd6 295 /* if purge_outstanding_packets() was called with an argument
c6c8fea2
SE
296 * we delete only packets belonging to the given interface
297 */
e6c10f43
ML
298 if ((hard_iface) &&
299 (forw_packet->if_incoming != hard_iface))
c6c8fea2
SE
300 continue;
301
302 spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
303
bb079c82 304 /* batadv_send_outstanding_bcast_packet() will lock the list to
c6c8fea2
SE
305 * delete the item from the list
306 */
6d5808d4 307 pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
c6c8fea2 308 spin_lock_bh(&bat_priv->forw_bcast_list_lock);
6d5808d4
SE
309
310 if (pending) {
311 hlist_del(&forw_packet->list);
bb079c82 312 batadv_forw_packet_free(forw_packet);
6d5808d4 313 }
c6c8fea2
SE
314 }
315 spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
316
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) {
321
9cfc7bd6 322 /* if purge_outstanding_packets() was called with an argument
c6c8fea2
SE
323 * we delete only packets belonging to the given interface
324 */
e6c10f43
ML
325 if ((hard_iface) &&
326 (forw_packet->if_incoming != hard_iface))
c6c8fea2
SE
327 continue;
328
329 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
330
9cfc7bd6 331 /* send_outstanding_bat_packet() will lock the list to
c6c8fea2
SE
332 * delete the item from the list
333 */
6d5808d4 334 pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
c6c8fea2 335 spin_lock_bh(&bat_priv->forw_bat_list_lock);
6d5808d4
SE
336
337 if (pending) {
338 hlist_del(&forw_packet->list);
bb079c82 339 batadv_forw_packet_free(forw_packet);
6d5808d4 340 }
c6c8fea2
SE
341 }
342 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
343}
This page took 0.173605 seconds and 5 git commands to generate.