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