batman-adv: OGMv2 - add basic infrastructure
[deliverable/linux.git] / net / batman-adv / bat_v_elp.c
CommitLineData
d6f94d91
LL
1/* Copyright (C) 2011-2016 B.A.T.M.A.N. contributors:
2 *
3 * Linus Lüssing, Marek Lindner
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, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "bat_v_elp.h"
19#include "main.h"
20
21#include <linux/atomic.h>
22#include <linux/byteorder/generic.h>
23#include <linux/errno.h>
24#include <linux/etherdevice.h>
25#include <linux/fs.h>
26#include <linux/if_ether.h>
27#include <linux/jiffies.h>
28#include <linux/kernel.h>
29#include <linux/netdevice.h>
30#include <linux/random.h>
31#include <linux/rculist.h>
32#include <linux/rcupdate.h>
33#include <linux/skbuff.h>
34#include <linux/stddef.h>
35#include <linux/string.h>
36#include <linux/types.h>
37#include <linux/workqueue.h>
38
39#include "bat_algo.h"
40#include "hard-interface.h"
162bd64c
LL
41#include "hash.h"
42#include "originator.h"
d6f94d91 43#include "packet.h"
162bd64c 44#include "routing.h"
d6f94d91
LL
45#include "send.h"
46
47/**
48 * batadv_v_elp_start_timer - restart timer for ELP periodic work
49 * @hard_iface: the interface for which the timer has to be reset
50 */
51static void batadv_v_elp_start_timer(struct batadv_hard_iface *hard_iface)
52{
53 unsigned int msecs;
54
55 msecs = atomic_read(&hard_iface->bat_v.elp_interval) - BATADV_JITTER;
56 msecs += prandom_u32() % (2 * BATADV_JITTER);
57
58 queue_delayed_work(batadv_event_workqueue, &hard_iface->bat_v.elp_wq,
59 msecs_to_jiffies(msecs));
60}
61
62/**
63 * batadv_v_elp_periodic_work - ELP periodic task per interface
64 * @work: work queue item
65 *
66 * Emits broadcast ELP message in regular intervals.
67 */
68static void batadv_v_elp_periodic_work(struct work_struct *work)
69{
70 struct batadv_hard_iface *hard_iface;
71 struct batadv_hard_iface_bat_v *bat_v;
72 struct batadv_elp_packet *elp_packet;
73 struct batadv_priv *bat_priv;
74 struct sk_buff *skb;
75 u32 elp_interval;
76
77 bat_v = container_of(work, struct batadv_hard_iface_bat_v, elp_wq.work);
78 hard_iface = container_of(bat_v, struct batadv_hard_iface, bat_v);
79 bat_priv = netdev_priv(hard_iface->soft_iface);
80
81 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
82 goto out;
83
84 /* we are in the process of shutting this interface down */
85 if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
86 (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
87 goto out;
88
89 /* the interface was enabled but may not be ready yet */
90 if (hard_iface->if_status != BATADV_IF_ACTIVE)
91 goto restart_timer;
92
93 skb = skb_copy(hard_iface->bat_v.elp_skb, GFP_ATOMIC);
94 if (!skb)
95 goto restart_timer;
96
97 elp_packet = (struct batadv_elp_packet *)skb->data;
98 elp_packet->seqno = htonl(atomic_read(&hard_iface->bat_v.elp_seqno));
99 elp_interval = atomic_read(&hard_iface->bat_v.elp_interval);
100 elp_packet->elp_interval = htonl(elp_interval);
101
102 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
103 "Sending broadcast ELP packet on interface %s, seqno %u\n",
104 hard_iface->net_dev->name,
105 atomic_read(&hard_iface->bat_v.elp_seqno));
106
107 batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
108
109 atomic_inc(&hard_iface->bat_v.elp_seqno);
110
111restart_timer:
112 batadv_v_elp_start_timer(hard_iface);
113out:
114 return;
115}
116
117/**
118 * batadv_v_elp_iface_enable - setup the ELP interface private resources
119 * @hard_iface: interface for which the data has to be prepared
120 *
121 * Return: 0 on success or a -ENOMEM in case of failure.
122 */
123int batadv_v_elp_iface_enable(struct batadv_hard_iface *hard_iface)
124{
125 struct batadv_elp_packet *elp_packet;
126 unsigned char *elp_buff;
127 u32 random_seqno;
128 size_t size;
129 int res = -ENOMEM;
130
131 size = ETH_HLEN + NET_IP_ALIGN + BATADV_ELP_HLEN;
132 hard_iface->bat_v.elp_skb = dev_alloc_skb(size);
133 if (!hard_iface->bat_v.elp_skb)
134 goto out;
135
136 skb_reserve(hard_iface->bat_v.elp_skb, ETH_HLEN + NET_IP_ALIGN);
137 elp_buff = skb_push(hard_iface->bat_v.elp_skb, BATADV_ELP_HLEN);
138 elp_packet = (struct batadv_elp_packet *)elp_buff;
139 memset(elp_packet, 0, BATADV_ELP_HLEN);
140
141 elp_packet->packet_type = BATADV_ELP;
142 elp_packet->version = BATADV_COMPAT_VERSION;
143
144 /* randomize initial seqno to avoid collision */
145 get_random_bytes(&random_seqno, sizeof(random_seqno));
146 atomic_set(&hard_iface->bat_v.elp_seqno, random_seqno);
147 atomic_set(&hard_iface->bat_v.elp_interval, 500);
148
149 INIT_DELAYED_WORK(&hard_iface->bat_v.elp_wq,
150 batadv_v_elp_periodic_work);
151 batadv_v_elp_start_timer(hard_iface);
152 res = 0;
153
154out:
155 return res;
156}
157
158/**
159 * batadv_v_elp_iface_disable - release ELP interface private resources
160 * @hard_iface: interface for which the resources have to be released
161 */
162void batadv_v_elp_iface_disable(struct batadv_hard_iface *hard_iface)
163{
164 cancel_delayed_work_sync(&hard_iface->bat_v.elp_wq);
165
166 dev_kfree_skb(hard_iface->bat_v.elp_skb);
167 hard_iface->bat_v.elp_skb = NULL;
168}
169
170/**
171 * batadv_v_elp_primary_iface_set - change internal data to reflect the new
172 * primary interface
173 * @primary_iface: the new primary interface
174 */
175void batadv_v_elp_primary_iface_set(struct batadv_hard_iface *primary_iface)
176{
177 struct batadv_hard_iface *hard_iface;
178 struct batadv_elp_packet *elp_packet;
179 struct sk_buff *skb;
180
181 /* update orig field of every elp iface belonging to this mesh */
182 rcu_read_lock();
183 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
184 if (primary_iface->soft_iface != hard_iface->soft_iface)
185 continue;
186
187 if (!hard_iface->bat_v.elp_skb)
188 continue;
189
190 skb = hard_iface->bat_v.elp_skb;
191 elp_packet = (struct batadv_elp_packet *)skb->data;
192 ether_addr_copy(elp_packet->orig,
193 primary_iface->net_dev->dev_addr);
194 }
195 rcu_read_unlock();
196}
162bd64c
LL
197
198/**
199 * batadv_v_ogm_orig_get - retrieve and possibly create an originator node
200 * @bat_priv: the bat priv with all the soft interface information
201 * @addr: the address of the originator
202 *
203 * Return: the orig_node corresponding to the specified address. If such object
204 * does not exist it is allocated here. In case of allocation failure returns
205 * NULL.
206 */
207static struct batadv_orig_node *
208batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
209 const u8 *addr)
210{
211 struct batadv_orig_node *orig_node;
212 int hash_added;
213
214 orig_node = batadv_orig_hash_find(bat_priv, addr);
215 if (orig_node)
216 return orig_node;
217
218 orig_node = batadv_orig_node_new(bat_priv, addr);
219 if (!orig_node)
220 return NULL;
221
222 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
223 batadv_choose_orig, orig_node,
224 &orig_node->hash_entry);
225 if (hash_added != 0) {
226 /* orig_node->refcounter is initialised to 2 by
227 * batadv_orig_node_new()
228 */
229 batadv_orig_node_put(orig_node);
230 batadv_orig_node_put(orig_node);
231 orig_node = NULL;
232 }
233
234 return orig_node;
235}
236
237/**
238 * batadv_v_elp_neigh_update - update an ELP neighbour node
239 * @bat_priv: the bat priv with all the soft interface information
240 * @neigh_addr: the neighbour interface address
241 * @if_incoming: the interface the packet was received through
242 * @elp_packet: the received ELP packet
243 *
244 * Updates the ELP neighbour node state with the data received within the new
245 * ELP packet.
246 */
247static void batadv_v_elp_neigh_update(struct batadv_priv *bat_priv,
248 u8 *neigh_addr,
249 struct batadv_hard_iface *if_incoming,
250 struct batadv_elp_packet *elp_packet)
251
252{
253 struct batadv_neigh_node *neigh;
254 struct batadv_orig_node *orig_neigh;
255 struct batadv_hardif_neigh_node *hardif_neigh;
256 s32 seqno_diff;
257 s32 elp_latest_seqno;
258
259 orig_neigh = batadv_v_ogm_orig_get(bat_priv, elp_packet->orig);
260 if (!orig_neigh)
261 return;
262
263 neigh = batadv_neigh_node_new(orig_neigh, if_incoming, neigh_addr);
264 if (!neigh)
265 goto orig_free;
266
267 hardif_neigh = batadv_hardif_neigh_get(if_incoming, neigh_addr);
268 if (!hardif_neigh)
269 goto neigh_free;
270
271 elp_latest_seqno = hardif_neigh->bat_v.elp_latest_seqno;
272 seqno_diff = ntohl(elp_packet->seqno) - elp_latest_seqno;
273
274 /* known or older sequence numbers are ignored. However always adopt
275 * if the router seems to have been restarted.
276 */
277 if (seqno_diff < 1 && seqno_diff > -BATADV_ELP_MAX_AGE)
278 goto hardif_free;
279
280 neigh->last_seen = jiffies;
281 hardif_neigh->last_seen = jiffies;
282 hardif_neigh->bat_v.elp_latest_seqno = ntohl(elp_packet->seqno);
283 hardif_neigh->bat_v.elp_interval = ntohl(elp_packet->elp_interval);
284
285hardif_free:
286 if (hardif_neigh)
287 batadv_hardif_neigh_put(hardif_neigh);
288neigh_free:
289 if (neigh)
290 batadv_neigh_node_put(neigh);
291orig_free:
292 if (orig_neigh)
293 batadv_orig_node_put(orig_neigh);
294}
295
296/**
297 * batadv_v_elp_packet_recv - main ELP packet handler
298 * @skb: the received packet
299 * @if_incoming: the interface this packet was received through
300 *
301 * Return: NET_RX_SUCCESS and consumes the skb if the packet was peoperly
302 * processed or NET_RX_DROP in case of failure.
303 */
304int batadv_v_elp_packet_recv(struct sk_buff *skb,
305 struct batadv_hard_iface *if_incoming)
306{
307 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
308 struct batadv_elp_packet *elp_packet;
309 struct batadv_hard_iface *primary_if;
310 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
311 bool ret;
312
313 ret = batadv_check_management_packet(skb, if_incoming, BATADV_ELP_HLEN);
314 if (!ret)
315 return NET_RX_DROP;
316
317 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
318 return NET_RX_DROP;
319
320 /* did we receive a B.A.T.M.A.N. V ELP packet on an interface
321 * that does not have B.A.T.M.A.N. V ELP enabled ?
322 */
323 if (strcmp(bat_priv->bat_algo_ops->name, "BATMAN_V") != 0)
324 return NET_RX_DROP;
325
326 elp_packet = (struct batadv_elp_packet *)skb->data;
327
328 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
329 "Received ELP packet from %pM seqno %u ORIG: %pM\n",
330 ethhdr->h_source, ntohl(elp_packet->seqno),
331 elp_packet->orig);
332
333 primary_if = batadv_primary_if_get_selected(bat_priv);
334 if (!primary_if)
335 goto out;
336
337 batadv_v_elp_neigh_update(bat_priv, ethhdr->h_source, if_incoming,
338 elp_packet);
339
340out:
341 if (primary_if)
342 batadv_hardif_put(primary_if);
343 consume_skb(skb);
344 return NET_RX_SUCCESS;
345}
This page took 0.03579 seconds and 5 git commands to generate.