send.c 11.0 KB
Newer Older
1
/* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Marek Lindner, Simon Wunderlich
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 */

#include "main.h"
21
#include "distributed-arp-table.h"
22 23 24 25 26 27 28 29 30
#include "send.h"
#include "routing.h"
#include "translation-table.h"
#include "soft-interface.h"
#include "hard-interface.h"
#include "vis.h"
#include "gateway_common.h"
#include "originator.h"

A
Antonio Quartulli 已提交
31 32
#include <linux/if_ether.h>

33
static void batadv_send_outstanding_bcast_packet(struct work_struct *work);
34 35

/* send out an already prepared packet to the given address via the
36 37
 * specified batman interface
 */
38 39
int batadv_send_skb_packet(struct sk_buff *skb,
			   struct batadv_hard_iface *hard_iface,
40
			   const uint8_t *dst_addr)
41 42 43
{
	struct ethhdr *ethhdr;

44
	if (hard_iface->if_status != BATADV_IF_ACTIVE)
45 46
		goto send_skb_err;

47
	if (unlikely(!hard_iface->net_dev))
48 49
		goto send_skb_err;

50
	if (!(hard_iface->net_dev->flags & IFF_UP)) {
51 52
		pr_warn("Interface %s is not up - can't send packet via that interface!\n",
			hard_iface->net_dev->name);
53 54 55 56
		goto send_skb_err;
	}

	/* push to the ethernet header. */
57
	if (batadv_skb_head_push(skb, ETH_HLEN) < 0)
58 59 60 61
		goto send_skb_err;

	skb_reset_mac_header(skb);

62
	ethhdr = (struct ethhdr *)skb_mac_header(skb);
63
	memcpy(ethhdr->h_source, hard_iface->net_dev->dev_addr, ETH_ALEN);
64
	memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN);
A
Antonio Quartulli 已提交
65
	ethhdr->h_proto = __constant_htons(ETH_P_BATMAN);
66 67 68

	skb_set_network_header(skb, ETH_HLEN);
	skb->priority = TC_PRIO_CONTROL;
A
Antonio Quartulli 已提交
69
	skb->protocol = __constant_htons(ETH_P_BATMAN);
70

71
	skb->dev = hard_iface->net_dev;
72 73 74

	/* dev_queue_xmit() returns a negative result on error.	 However on
	 * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
75 76
	 * (which is > 0). This will not be treated as an error.
	 */
77 78 79 80 81 82
	return dev_queue_xmit(skb);
send_skb_err:
	kfree_skb(skb);
	return NET_XMIT_DROP;
}

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/**
 * batadv_send_skb_to_orig - Lookup next-hop and transmit skb.
 * @skb: Packet to be transmitted.
 * @orig_node: Final destination of the packet.
 * @recv_if: Interface used when receiving the packet (can be NULL).
 *
 * Looks up the best next-hop towards the passed originator and passes the
 * skb on for preparation of MAC header. If the packet originated from this
 * host, NULL can be passed as recv_if and no interface alternating is
 * attempted.
 *
 * Returns TRUE on success; FALSE otherwise.
 */
bool batadv_send_skb_to_orig(struct sk_buff *skb,
			     struct batadv_orig_node *orig_node,
			     struct batadv_hard_iface *recv_if)
{
	struct batadv_priv *bat_priv = orig_node->bat_priv;
	struct batadv_neigh_node *neigh_node;

	/* batadv_find_router() increases neigh_nodes refcount if found. */
	neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
	if (!neigh_node)
		return false;

	/* route it */
	batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);

	batadv_neigh_node_free_ref(neigh_node);

	return true;
}

116
void batadv_schedule_bat_ogm(struct batadv_hard_iface *hard_iface)
117
{
118
	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
119

120 121
	if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
	    (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
122 123
		return;

124
	/* the interface gets activated here to avoid race conditions between
125 126 127 128 129
	 * the moment of activating the interface in
	 * hardif_activate_interface() where the originator mac is set and
	 * outdated packets (especially uninitialized mac addresses) in the
	 * packet queue
	 */
130 131
	if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
		hard_iface->if_status = BATADV_IF_ACTIVE;
132

133
	bat_priv->bat_algo_ops->bat_ogm_schedule(hard_iface);
134 135
}

136
static void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet)
137 138 139
{
	if (forw_packet->skb)
		kfree_skb(forw_packet->skb);
140
	if (forw_packet->if_incoming)
141
		batadv_hardif_free_ref(forw_packet->if_incoming);
142 143 144
	kfree(forw_packet);
}

145 146 147 148
static void
_batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
				 struct batadv_forw_packet *forw_packet,
				 unsigned long send_time)
149 150 151 152 153 154 155 156 157
{
	INIT_HLIST_NODE(&forw_packet->list);

	/* add new packet to packet list */
	spin_lock_bh(&bat_priv->forw_bcast_list_lock);
	hlist_add_head(&forw_packet->list, &bat_priv->forw_bcast_list);
	spin_unlock_bh(&bat_priv->forw_bcast_list_lock);

	/* start timer for this packet */
158
	queue_delayed_work(batadv_event_workqueue, &forw_packet->delayed_work,
159 160 161 162
			   send_time);
}

/* add a broadcast packet to the queue and setup timers. broadcast packets
163
 * are sent multiple times to increase probability for being received.
164 165 166 167 168
 *
 * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
 * errors.
 *
 * The skb is not consumed, so the caller should make sure that the
169 170
 * skb is freed.
 */
171
int batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
172 173
				    const struct sk_buff *skb,
				    unsigned long delay)
174
{
175 176
	struct batadv_hard_iface *primary_if = NULL;
	struct batadv_forw_packet *forw_packet;
177
	struct batadv_bcast_packet *bcast_packet;
178
	struct sk_buff *newskb;
179

180
	if (!batadv_atomic_dec_not_zero(&bat_priv->bcast_queue_left)) {
181 182
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
			   "bcast packet queue full\n");
183 184 185
		goto out;
	}

186
	primary_if = batadv_primary_if_get_selected(bat_priv);
187
	if (!primary_if)
188
		goto out_and_inc;
189

190
	forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);
191 192 193 194

	if (!forw_packet)
		goto out_and_inc;

195 196
	newskb = skb_copy(skb, GFP_ATOMIC);
	if (!newskb)
197 198 199
		goto packet_free;

	/* as we have a copy now, it is safe to decrease the TTL */
200
	bcast_packet = (struct batadv_bcast_packet *)newskb->data;
201
	bcast_packet->header.ttl--;
202

203
	skb_reset_mac_header(newskb);
204

205
	forw_packet->skb = newskb;
206
	forw_packet->if_incoming = primary_if;
207 208 209 210

	/* how often did we send the bcast packet ? */
	forw_packet->num_packets = 0;

211 212 213
	INIT_DELAYED_WORK(&forw_packet->delayed_work,
			  batadv_send_outstanding_bcast_packet);

214
	_batadv_add_bcast_packet_to_list(bat_priv, forw_packet, delay);
215 216 217 218 219 220 221
	return NETDEV_TX_OK;

packet_free:
	kfree(forw_packet);
out_and_inc:
	atomic_inc(&bat_priv->bcast_queue_left);
out:
222
	if (primary_if)
223
		batadv_hardif_free_ref(primary_if);
224 225 226
	return NETDEV_TX_BUSY;
}

227
static void batadv_send_outstanding_bcast_packet(struct work_struct *work)
228
{
229
	struct batadv_hard_iface *hard_iface;
230
	struct delayed_work *delayed_work;
231
	struct batadv_forw_packet *forw_packet;
232
	struct sk_buff *skb1;
233 234 235
	struct net_device *soft_iface;
	struct batadv_priv *bat_priv;

236
	delayed_work = container_of(work, struct delayed_work, work);
237 238 239 240
	forw_packet = container_of(delayed_work, struct batadv_forw_packet,
				   delayed_work);
	soft_iface = forw_packet->if_incoming->soft_iface;
	bat_priv = netdev_priv(soft_iface);
241 242 243 244 245

	spin_lock_bh(&bat_priv->forw_bcast_list_lock);
	hlist_del(&forw_packet->list);
	spin_unlock_bh(&bat_priv->forw_bcast_list_lock);

246
	if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
247 248
		goto out;

249 250 251
	if (batadv_dat_drop_broadcast_packet(bat_priv, forw_packet))
		goto out;

252 253
	/* rebroadcast packet */
	rcu_read_lock();
254
	list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
255
		if (hard_iface->soft_iface != soft_iface)
256 257 258 259 260
			continue;

		/* send a copy of the saved skb */
		skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
		if (skb1)
261
			batadv_send_skb_packet(skb1, hard_iface,
262
					       batadv_broadcast_addr);
263 264 265 266 267 268 269
	}
	rcu_read_unlock();

	forw_packet->num_packets++;

	/* if we still have some more bcasts to send */
	if (forw_packet->num_packets < 3) {
270 271
		_batadv_add_bcast_packet_to_list(bat_priv, forw_packet,
						 msecs_to_jiffies(5));
272 273 274 275
		return;
	}

out:
276
	batadv_forw_packet_free(forw_packet);
277 278 279
	atomic_inc(&bat_priv->bcast_queue_left);
}

280
void batadv_send_outstanding_bat_ogm_packet(struct work_struct *work)
281
{
282
	struct delayed_work *delayed_work;
283 284
	struct batadv_forw_packet *forw_packet;
	struct batadv_priv *bat_priv;
285

286
	delayed_work = container_of(work, struct delayed_work, work);
287 288
	forw_packet = container_of(delayed_work, struct batadv_forw_packet,
				   delayed_work);
289 290 291 292 293
	bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
	spin_lock_bh(&bat_priv->forw_bat_list_lock);
	hlist_del(&forw_packet->list);
	spin_unlock_bh(&bat_priv->forw_bat_list_lock);

294
	if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
295 296
		goto out;

297
	bat_priv->bat_algo_ops->bat_ogm_emit(forw_packet);
298

299
	/* we have to have at least one packet in the queue
300 301 302 303
	 * to determine the queues wake up time unless we are
	 * shutting down
	 */
	if (forw_packet->own)
304
		batadv_schedule_bat_ogm(forw_packet->if_incoming);
305 306 307 308 309 310

out:
	/* don't count own packet */
	if (!forw_packet->own)
		atomic_inc(&bat_priv->batman_queue_left);

311
	batadv_forw_packet_free(forw_packet);
312 313
}

314 315 316
void
batadv_purge_outstanding_packets(struct batadv_priv *bat_priv,
				 const struct batadv_hard_iface *hard_iface)
317
{
318
	struct batadv_forw_packet *forw_packet;
319
	struct hlist_node *tmp_node, *safe_tmp_node;
320
	bool pending;
321

322
	if (hard_iface)
323
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
324 325
			   "purge_outstanding_packets(): %s\n",
			   hard_iface->net_dev->name);
326
	else
327
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
328
			   "purge_outstanding_packets()\n");
329 330 331 332 333

	/* free bcast list */
	spin_lock_bh(&bat_priv->forw_bcast_list_lock);
	hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
				  &bat_priv->forw_bcast_list, list) {
334
		/* if purge_outstanding_packets() was called with an argument
335 336
		 * we delete only packets belonging to the given interface
		 */
337 338
		if ((hard_iface) &&
		    (forw_packet->if_incoming != hard_iface))
339 340 341 342
			continue;

		spin_unlock_bh(&bat_priv->forw_bcast_list_lock);

343
		/* batadv_send_outstanding_bcast_packet() will lock the list to
344 345
		 * delete the item from the list
		 */
346
		pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
347
		spin_lock_bh(&bat_priv->forw_bcast_list_lock);
348 349 350

		if (pending) {
			hlist_del(&forw_packet->list);
351
			batadv_forw_packet_free(forw_packet);
352
		}
353 354 355 356 357 358 359
	}
	spin_unlock_bh(&bat_priv->forw_bcast_list_lock);

	/* free batman packet list */
	spin_lock_bh(&bat_priv->forw_bat_list_lock);
	hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
				  &bat_priv->forw_bat_list, list) {
360
		/* if purge_outstanding_packets() was called with an argument
361 362
		 * we delete only packets belonging to the given interface
		 */
363 364
		if ((hard_iface) &&
		    (forw_packet->if_incoming != hard_iface))
365 366 367 368
			continue;

		spin_unlock_bh(&bat_priv->forw_bat_list_lock);

369
		/* send_outstanding_bat_packet() will lock the list to
370 371
		 * delete the item from the list
		 */
372
		pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
373
		spin_lock_bh(&bat_priv->forw_bat_list_lock);
374 375 376

		if (pending) {
			hlist_del(&forw_packet->list);
377
			batadv_forw_packet_free(forw_packet);
378
		}
379 380 381
	}
	spin_unlock_bh(&bat_priv->forw_bat_list_lock);
}