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, struct hard_iface *hard_iface,
36 const uint8_t *dst_addr)
38 struct ethhdr *ethhdr;
40 if (hard_iface->if_status != BATADV_IF_ACTIVE)
43 if (unlikely(!hard_iface->net_dev))
46 if (!(hard_iface->net_dev->flags & IFF_UP)) {
47 pr_warn("Interface %s is not up - can't send packet via that interface!\n",
48 hard_iface->net_dev->name);
52 /* push to the ethernet header. */
53 if (batadv_skb_head_push(skb, ETH_HLEN) < 0)
56 skb_reset_mac_header(skb);
58 ethhdr = (struct ethhdr *)skb_mac_header(skb);
59 memcpy(ethhdr->h_source, hard_iface->net_dev->dev_addr, ETH_ALEN);
60 memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN);
61 ethhdr->h_proto = __constant_htons(BATADV_ETH_P_BATMAN);
63 skb_set_network_header(skb, ETH_HLEN);
64 skb->priority = TC_PRIO_CONTROL;
65 skb->protocol = __constant_htons(BATADV_ETH_P_BATMAN);
67 skb->dev = hard_iface->net_dev;
69 /* dev_queue_xmit() returns a negative result on error. However on
70 * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
71 * (which is > 0). This will not be treated as an error.
73 return dev_queue_xmit(skb);
79 void batadv_schedule_bat_ogm(struct hard_iface *hard_iface)
81 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
83 if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
84 (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
87 /* the interface gets activated here to avoid race conditions between
88 * the moment of activating the interface in
89 * hardif_activate_interface() where the originator mac is set and
90 * outdated packets (especially uninitialized mac addresses) in the
93 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
94 hard_iface->if_status = BATADV_IF_ACTIVE;
96 bat_priv->bat_algo_ops->bat_ogm_schedule(hard_iface);
99 static void batadv_forw_packet_free(struct forw_packet *forw_packet)
101 if (forw_packet->skb)
102 kfree_skb(forw_packet->skb);
103 if (forw_packet->if_incoming)
104 batadv_hardif_free_ref(forw_packet->if_incoming);
108 static void _batadv_add_bcast_packet_to_list(struct bat_priv *bat_priv,
109 struct forw_packet *forw_packet,
110 unsigned long send_time)
112 INIT_HLIST_NODE(&forw_packet->list);
114 /* add new packet to packet list */
115 spin_lock_bh(&bat_priv->forw_bcast_list_lock);
116 hlist_add_head(&forw_packet->list, &bat_priv->forw_bcast_list);
117 spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
119 /* start timer for this packet */
120 INIT_DELAYED_WORK(&forw_packet->delayed_work,
121 batadv_send_outstanding_bcast_packet);
122 queue_delayed_work(batadv_event_workqueue, &forw_packet->delayed_work,
126 /* add a broadcast packet to the queue and setup timers. broadcast packets
127 * are sent multiple times to increase probability for being received.
129 * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
132 * The skb is not consumed, so the caller should make sure that the
135 int batadv_add_bcast_packet_to_list(struct bat_priv *bat_priv,
136 const struct sk_buff *skb,
139 struct hard_iface *primary_if = NULL;
140 struct forw_packet *forw_packet;
141 struct bcast_packet *bcast_packet;
142 struct sk_buff *newskb;
144 if (!batadv_atomic_dec_not_zero(&bat_priv->bcast_queue_left)) {
145 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
146 "bcast packet queue full\n");
150 primary_if = batadv_primary_if_get_selected(bat_priv);
154 forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);
159 newskb = skb_copy(skb, GFP_ATOMIC);
163 /* as we have a copy now, it is safe to decrease the TTL */
164 bcast_packet = (struct bcast_packet *)newskb->data;
165 bcast_packet->header.ttl--;
167 skb_reset_mac_header(newskb);
169 forw_packet->skb = newskb;
170 forw_packet->if_incoming = primary_if;
172 /* how often did we send the bcast packet ? */
173 forw_packet->num_packets = 0;
175 _batadv_add_bcast_packet_to_list(bat_priv, forw_packet, delay);
181 atomic_inc(&bat_priv->bcast_queue_left);
184 batadv_hardif_free_ref(primary_if);
185 return NETDEV_TX_BUSY;
188 static void batadv_send_outstanding_bcast_packet(struct work_struct *work)
190 struct hard_iface *hard_iface;
191 struct delayed_work *delayed_work =
192 container_of(work, struct delayed_work, work);
193 struct forw_packet *forw_packet =
194 container_of(delayed_work, struct forw_packet, delayed_work);
195 struct sk_buff *skb1;
196 struct net_device *soft_iface = forw_packet->if_incoming->soft_iface;
197 struct bat_priv *bat_priv = netdev_priv(soft_iface);
199 spin_lock_bh(&bat_priv->forw_bcast_list_lock);
200 hlist_del(&forw_packet->list);
201 spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
203 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
206 /* rebroadcast packet */
208 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
209 if (hard_iface->soft_iface != soft_iface)
212 /* send a copy of the saved skb */
213 skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
215 batadv_send_skb_packet(skb1, hard_iface,
216 batadv_broadcast_addr);
220 forw_packet->num_packets++;
222 /* if we still have some more bcasts to send */
223 if (forw_packet->num_packets < 3) {
224 _batadv_add_bcast_packet_to_list(bat_priv, forw_packet,
225 msecs_to_jiffies(5));
230 batadv_forw_packet_free(forw_packet);
231 atomic_inc(&bat_priv->bcast_queue_left);
234 void batadv_send_outstanding_bat_ogm_packet(struct work_struct *work)
236 struct delayed_work *delayed_work =
237 container_of(work, struct delayed_work, work);
238 struct forw_packet *forw_packet =
239 container_of(delayed_work, struct forw_packet, delayed_work);
240 struct bat_priv *bat_priv;
242 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
243 spin_lock_bh(&bat_priv->forw_bat_list_lock);
244 hlist_del(&forw_packet->list);
245 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
247 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
250 bat_priv->bat_algo_ops->bat_ogm_emit(forw_packet);
252 /* we have to have at least one packet in the queue
253 * to determine the queues wake up time unless we are
256 if (forw_packet->own)
257 batadv_schedule_bat_ogm(forw_packet->if_incoming);
260 /* don't count own packet */
261 if (!forw_packet->own)
262 atomic_inc(&bat_priv->batman_queue_left);
264 batadv_forw_packet_free(forw_packet);
267 void batadv_purge_outstanding_packets(struct bat_priv *bat_priv,
268 const struct hard_iface *hard_iface)
270 struct forw_packet *forw_packet;
271 struct hlist_node *tmp_node, *safe_tmp_node;
275 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
276 "purge_outstanding_packets(): %s\n",
277 hard_iface->net_dev->name);
279 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
280 "purge_outstanding_packets()\n");
282 /* free bcast list */
283 spin_lock_bh(&bat_priv->forw_bcast_list_lock);
284 hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
285 &bat_priv->forw_bcast_list, list) {
287 /* if purge_outstanding_packets() was called with an argument
288 * we delete only packets belonging to the given interface
291 (forw_packet->if_incoming != hard_iface))
294 spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
296 /* batadv_send_outstanding_bcast_packet() will lock the list to
297 * delete the item from the list
299 pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
300 spin_lock_bh(&bat_priv->forw_bcast_list_lock);
303 hlist_del(&forw_packet->list);
304 batadv_forw_packet_free(forw_packet);
307 spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
309 /* free batman packet list */
310 spin_lock_bh(&bat_priv->forw_bat_list_lock);
311 hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
312 &bat_priv->forw_bat_list, list) {
314 /* if purge_outstanding_packets() was called with an argument
315 * we delete only packets belonging to the given interface
318 (forw_packet->if_incoming != hard_iface))
321 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
323 /* send_outstanding_bat_packet() will lock the list to
324 * delete the item from the list
326 pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
327 spin_lock_bh(&bat_priv->forw_bat_list_lock);
330 hlist_del(&forw_packet->list);
331 batadv_forw_packet_free(forw_packet);
334 spin_unlock_bh(&bat_priv->forw_bat_list_lock);