blob: 3b4b2daa3b3e1b40315a2afa657b13d67517ebb0 [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
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"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000026#include "vis.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027#include "gateway_common.h"
28#include "originator.h"
29
Sven Eckelmannbb079c82012-05-16 20:23:14 +020030static void batadv_send_outstanding_bcast_packet(struct work_struct *work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032/* send out an already prepared packet to the given address via the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020033 * specified batman interface
34 */
Sven Eckelmann56303d32012-06-05 22:31:31 +020035int batadv_send_skb_packet(struct sk_buff *skb,
36 struct batadv_hard_iface *hard_iface,
Sven Eckelmann9455e342012-05-12 02:09:37 +020037 const uint8_t *dst_addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038{
39 struct ethhdr *ethhdr;
40
Sven Eckelmanne9a4f292012-06-03 22:19:19 +020041 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000042 goto send_skb_err;
43
Marek Lindnere6c10f42011-02-18 12:33:20 +000044 if (unlikely(!hard_iface->net_dev))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045 goto send_skb_err;
46
Marek Lindnere6c10f42011-02-18 12:33:20 +000047 if (!(hard_iface->net_dev->flags & IFF_UP)) {
Sven Eckelmann67969582012-03-26 16:22:45 +020048 pr_warn("Interface %s is not up - can't send packet via that interface!\n",
49 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000050 goto send_skb_err;
51 }
52
53 /* push to the ethernet header. */
Sven Eckelmann04b482a2012-05-12 02:09:38 +020054 if (batadv_skb_head_push(skb, ETH_HLEN) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000055 goto send_skb_err;
56
57 skb_reset_mac_header(skb);
58
Sven Eckelmann40e0c4f2012-03-07 09:07:48 +010059 ethhdr = (struct ethhdr *)skb_mac_header(skb);
Marek Lindnere6c10f42011-02-18 12:33:20 +000060 memcpy(ethhdr->h_source, hard_iface->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061 memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN);
Sven Eckelmann7e071c72012-06-03 22:19:13 +020062 ethhdr->h_proto = __constant_htons(BATADV_ETH_P_BATMAN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063
64 skb_set_network_header(skb, ETH_HLEN);
65 skb->priority = TC_PRIO_CONTROL;
Sven Eckelmann7e071c72012-06-03 22:19:13 +020066 skb->protocol = __constant_htons(BATADV_ETH_P_BATMAN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000067
Marek Lindnere6c10f42011-02-18 12:33:20 +000068 skb->dev = hard_iface->net_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000069
70 /* dev_queue_xmit() returns a negative result on error. However on
71 * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020072 * (which is > 0). This will not be treated as an error.
73 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000074 return dev_queue_xmit(skb);
75send_skb_err:
76 kfree_skb(skb);
77 return NET_XMIT_DROP;
78}
79
Sven Eckelmann56303d32012-06-05 22:31:31 +020080void batadv_schedule_bat_ogm(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081{
Sven Eckelmann56303d32012-06-05 22:31:31 +020082 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000083
Sven Eckelmanne9a4f292012-06-03 22:19:19 +020084 if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
85 (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000086 return;
87
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020088 /* the interface gets activated here to avoid race conditions between
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000089 * 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 */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +020094 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
95 hard_iface->if_status = BATADV_IF_ACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000096
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +080097 bat_priv->bat_algo_ops->bat_ogm_schedule(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000098}
99
Sven Eckelmann56303d32012-06-05 22:31:31 +0200100static void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000101{
102 if (forw_packet->skb)
103 kfree_skb(forw_packet->skb);
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200104 if (forw_packet->if_incoming)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200105 batadv_hardif_free_ref(forw_packet->if_incoming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000106 kfree(forw_packet);
107}
108
Sven Eckelmann56303d32012-06-05 22:31:31 +0200109static 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)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113{
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,
Sven Eckelmannbb079c82012-05-16 20:23:14 +0200123 batadv_send_outstanding_bcast_packet);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200124 queue_delayed_work(batadv_event_workqueue, &forw_packet->delayed_work,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125 send_time);
126}
127
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000128/* add a broadcast packet to the queue and setup timers. broadcast packets
Antonio Quartulli015758d2011-07-09 17:52:13 +0200129 * are sent multiple times to increase probability for being received.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000130 *
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
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200135 * skb is freed.
136 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200137int batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
Sven Eckelmann9455e342012-05-12 02:09:37 +0200138 const struct sk_buff *skb,
139 unsigned long delay)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000140{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200141 struct batadv_hard_iface *primary_if = NULL;
142 struct batadv_forw_packet *forw_packet;
Sven Eckelmann96412692012-06-05 22:31:30 +0200143 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmann747e4222011-05-14 23:14:50 +0200144 struct sk_buff *newskb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000145
Sven Eckelmann3e348192012-05-16 20:23:22 +0200146 if (!batadv_atomic_dec_not_zero(&bat_priv->bcast_queue_left)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200147 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
148 "bcast packet queue full\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000149 goto out;
150 }
151
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200152 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200153 if (!primary_if)
Marek Lindnerca06c6e2011-05-14 20:01:22 +0200154 goto out_and_inc;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155
Sven Eckelmann704509b2011-05-14 23:14:54 +0200156 forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000157
158 if (!forw_packet)
159 goto out_and_inc;
160
Sven Eckelmann747e4222011-05-14 23:14:50 +0200161 newskb = skb_copy(skb, GFP_ATOMIC);
162 if (!newskb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163 goto packet_free;
164
165 /* as we have a copy now, it is safe to decrease the TTL */
Sven Eckelmann96412692012-06-05 22:31:30 +0200166 bcast_packet = (struct batadv_bcast_packet *)newskb->data;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100167 bcast_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168
Sven Eckelmann747e4222011-05-14 23:14:50 +0200169 skb_reset_mac_header(newskb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000170
Sven Eckelmann747e4222011-05-14 23:14:50 +0200171 forw_packet->skb = newskb;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200172 forw_packet->if_incoming = primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000173
174 /* how often did we send the bcast packet ? */
175 forw_packet->num_packets = 0;
176
Sven Eckelmannbb079c82012-05-16 20:23:14 +0200177 _batadv_add_bcast_packet_to_list(bat_priv, forw_packet, delay);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000178 return NETDEV_TX_OK;
179
180packet_free:
181 kfree(forw_packet);
182out_and_inc:
183 atomic_inc(&bat_priv->bcast_queue_left);
184out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200185 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200186 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000187 return NETDEV_TX_BUSY;
188}
189
Sven Eckelmannbb079c82012-05-16 20:23:14 +0200190static void batadv_send_outstanding_bcast_packet(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200192 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193 struct delayed_work *delayed_work =
194 container_of(work, struct delayed_work, work);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200195 struct batadv_forw_packet *forw_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000196 struct sk_buff *skb1;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200197 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);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000204
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
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200209 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210 goto out;
211
212 /* rebroadcast packet */
213 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200214 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000215 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216 continue;
217
218 /* send a copy of the saved skb */
219 skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
220 if (skb1)
Sven Eckelmann9455e342012-05-12 02:09:37 +0200221 batadv_send_skb_packet(skb1, hard_iface,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200222 batadv_broadcast_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000223 }
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) {
Sven Eckelmannbb079c82012-05-16 20:23:14 +0200230 _batadv_add_bcast_packet_to_list(bat_priv, forw_packet,
231 msecs_to_jiffies(5));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232 return;
233 }
234
235out:
Sven Eckelmannbb079c82012-05-16 20:23:14 +0200236 batadv_forw_packet_free(forw_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237 atomic_inc(&bat_priv->bcast_queue_left);
238}
239
Sven Eckelmann9455e342012-05-12 02:09:37 +0200240void batadv_send_outstanding_bat_ogm_packet(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241{
242 struct delayed_work *delayed_work =
243 container_of(work, struct delayed_work, work);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200244 struct batadv_forw_packet *forw_packet;
245 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246
Sven Eckelmann56303d32012-06-05 22:31:31 +0200247 forw_packet = container_of(delayed_work, struct batadv_forw_packet,
248 delayed_work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249 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
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200254 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255 goto out;
256
Marek Lindner01c42242011-11-28 21:31:55 +0800257 bat_priv->bat_algo_ops->bat_ogm_emit(forw_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000258
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200259 /* we have to have at least one packet in the queue
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000260 * to determine the queues wake up time unless we are
261 * shutting down
262 */
263 if (forw_packet->own)
Sven Eckelmann9455e342012-05-12 02:09:37 +0200264 batadv_schedule_bat_ogm(forw_packet->if_incoming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000265
266out:
267 /* don't count own packet */
268 if (!forw_packet->own)
269 atomic_inc(&bat_priv->batman_queue_left);
270
Sven Eckelmannbb079c82012-05-16 20:23:14 +0200271 batadv_forw_packet_free(forw_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272}
273
Sven Eckelmann56303d32012-06-05 22:31:31 +0200274void
275batadv_purge_outstanding_packets(struct batadv_priv *bat_priv,
276 const struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200278 struct batadv_forw_packet *forw_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000279 struct hlist_node *tmp_node, *safe_tmp_node;
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200280 bool pending;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281
Marek Lindnere6c10f42011-02-18 12:33:20 +0000282 if (hard_iface)
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200283 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200284 "purge_outstanding_packets(): %s\n",
285 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000286 else
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200287 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200288 "purge_outstanding_packets()\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289
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
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200295 /* if purge_outstanding_packets() was called with an argument
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296 * we delete only packets belonging to the given interface
297 */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000298 if ((hard_iface) &&
299 (forw_packet->if_incoming != hard_iface))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000300 continue;
301
302 spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
303
Sven Eckelmannbb079c82012-05-16 20:23:14 +0200304 /* batadv_send_outstanding_bcast_packet() will lock the list to
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305 * delete the item from the list
306 */
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200307 pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000308 spin_lock_bh(&bat_priv->forw_bcast_list_lock);
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200309
310 if (pending) {
311 hlist_del(&forw_packet->list);
Sven Eckelmannbb079c82012-05-16 20:23:14 +0200312 batadv_forw_packet_free(forw_packet);
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200313 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000314 }
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
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200322 /* if purge_outstanding_packets() was called with an argument
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323 * we delete only packets belonging to the given interface
324 */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000325 if ((hard_iface) &&
326 (forw_packet->if_incoming != hard_iface))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327 continue;
328
329 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
330
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200331 /* send_outstanding_bat_packet() will lock the list to
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332 * delete the item from the list
333 */
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200334 pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335 spin_lock_bh(&bat_priv->forw_bat_list_lock);
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200336
337 if (pending) {
338 hlist_del(&forw_packet->list);
Sven Eckelmannbb079c82012-05-16 20:23:14 +0200339 batadv_forw_packet_free(forw_packet);
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200340 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341 }
342 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
343}