Blame view

net/batman-adv/network-coding.c 58.6 KB
ac79cbb96   Sven Eckelmann   batman-adv: updat...
1
  /* Copyright (C) 2012-2017  B.A.T.M.A.N. contributors:
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
2
3
4
5
6
7
8
9
10
11
12
13
14
   *
   * Martin Hundebøll, Jeppe Ledet-Pedersen
   *
   * 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
ebf38fb7a   Antonio Quartulli   batman-adv: remov...
15
   * along with this program; if not, see <http://www.gnu.org/licenses/>.
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
16
   */
1e2c2a4fe   Sven Eckelmann   batman-adv: Add r...
17
18
19
20
  #include "network-coding.h"
  #include "main.h"
  
  #include <linux/atomic.h>
4635469f5   Linus Lüssing   batman-adv: Make ...
21
  #include <linux/bitops.h>
1e2c2a4fe   Sven Eckelmann   batman-adv: Add r...
22
23
  #include <linux/byteorder/generic.h>
  #include <linux/compiler.h>
d56b1705e   Martin Hundebøll   batman-adv: netwo...
24
  #include <linux/debugfs.h>
1e2c2a4fe   Sven Eckelmann   batman-adv: Add r...
25
26
27
28
29
30
31
32
33
  #include <linux/errno.h>
  #include <linux/etherdevice.h>
  #include <linux/fs.h>
  #include <linux/if_ether.h>
  #include <linux/if_packet.h>
  #include <linux/init.h>
  #include <linux/jhash.h>
  #include <linux/jiffies.h>
  #include <linux/kernel.h>
daf99b481   Sven Eckelmann   batman-adv: Conve...
34
  #include <linux/kref.h>
1e2c2a4fe   Sven Eckelmann   batman-adv: Add r...
35
36
37
38
39
40
41
42
43
44
45
  #include <linux/list.h>
  #include <linux/lockdep.h>
  #include <linux/netdevice.h>
  #include <linux/printk.h>
  #include <linux/random.h>
  #include <linux/rculist.h>
  #include <linux/rcupdate.h>
  #include <linux/seq_file.h>
  #include <linux/skbuff.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
1e2c2a4fe   Sven Eckelmann   batman-adv: Add r...
46
47
48
  #include <linux/stddef.h>
  #include <linux/string.h>
  #include <linux/workqueue.h>
d56b1705e   Martin Hundebøll   batman-adv: netwo...
49

1e2c2a4fe   Sven Eckelmann   batman-adv: Add r...
50
  #include "hard-interface.h"
953324776   Martin Hundebøll   batman-adv: netwo...
51
  #include "hash.h"
ba412080f   Sven Eckelmann   batman-adv: Conso...
52
  #include "log.h"
d56b1705e   Martin Hundebøll   batman-adv: netwo...
53
  #include "originator.h"
1e2c2a4fe   Sven Eckelmann   batman-adv: Add r...
54
  #include "packet.h"
2df5278b0   Martin Hundebøll   batman-adv: netwo...
55
  #include "routing.h"
1e2c2a4fe   Sven Eckelmann   batman-adv: Add r...
56
  #include "send.h"
1f8dce499   Markus Pargmann   batman-adv: split...
57
  #include "tvlv.h"
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
58

953324776   Martin Hundebøll   batman-adv: netwo...
59
  static struct lock_class_key batadv_nc_coding_hash_lock_class_key;
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
60
  static struct lock_class_key batadv_nc_decoding_hash_lock_class_key;
953324776   Martin Hundebøll   batman-adv: netwo...
61

d353d8d4d   Martin Hundebøll   batman-adv: netwo...
62
  static void batadv_nc_worker(struct work_struct *work);
2df5278b0   Martin Hundebøll   batman-adv: netwo...
63
64
  static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
  				       struct batadv_hard_iface *recv_if);
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
65
66
  
  /**
6c519bad7   Matthias Schiffer   batman-adv: set u...
67
   * batadv_nc_init - one-time initialization for network coding
672e79785   Sven Eckelmann   batman-adv: Fix k...
68
69
   *
   * Return: 0 on success or negative error number in case of failure
6c519bad7   Matthias Schiffer   batman-adv: set u...
70
71
72
73
74
75
76
77
78
79
80
81
82
   */
  int __init batadv_nc_init(void)
  {
  	int ret;
  
  	/* Register our packet type */
  	ret = batadv_recv_handler_register(BATADV_CODED,
  					   batadv_nc_recv_coded_packet);
  
  	return ret;
  }
  
  /**
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
83
84
85
86
87
88
89
90
91
92
   * batadv_nc_start_timer - initialise the nc periodic worker
   * @bat_priv: the bat priv with all the soft interface information
   */
  static void batadv_nc_start_timer(struct batadv_priv *bat_priv)
  {
  	queue_delayed_work(batadv_event_workqueue, &bat_priv->nc.work,
  			   msecs_to_jiffies(10));
  }
  
  /**
3f4841ffb   Marek Lindner   batman-adv: tvlv ...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
   * batadv_nc_tvlv_container_update - update the network coding tvlv container
   *  after network coding setting change
   * @bat_priv: the bat priv with all the soft interface information
   */
  static void batadv_nc_tvlv_container_update(struct batadv_priv *bat_priv)
  {
  	char nc_mode;
  
  	nc_mode = atomic_read(&bat_priv->network_coding);
  
  	switch (nc_mode) {
  	case 0:
  		batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
  		break;
  	case 1:
  		batadv_tvlv_container_register(bat_priv, BATADV_TVLV_NC, 1,
  					       NULL, 0);
  		break;
  	}
  }
  
  /**
   * batadv_nc_status_update - update the network coding tvlv container after
   *  network coding setting change
   * @net_dev: the soft interface net device
   */
  void batadv_nc_status_update(struct net_device *net_dev)
  {
  	struct batadv_priv *bat_priv = netdev_priv(net_dev);
f138694b1   Antonio Quartulli   batman-adv: add b...
122

3f4841ffb   Marek Lindner   batman-adv: tvlv ...
123
124
125
126
127
128
129
130
131
132
133
134
135
  	batadv_nc_tvlv_container_update(bat_priv);
  }
  
  /**
   * batadv_nc_tvlv_ogm_handler_v1 - process incoming nc tvlv container
   * @bat_priv: the bat priv with all the soft interface information
   * @orig: the orig_node of the ogm
   * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
   * @tvlv_value: tvlv buffer containing the gateway data
   * @tvlv_value_len: tvlv buffer length
   */
  static void batadv_nc_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  					  struct batadv_orig_node *orig,
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
136
137
  					  u8 flags,
  					  void *tvlv_value, u16 tvlv_value_len)
3f4841ffb   Marek Lindner   batman-adv: tvlv ...
138
139
  {
  	if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
4635469f5   Linus Lüssing   batman-adv: Make ...
140
  		clear_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities);
3f4841ffb   Marek Lindner   batman-adv: tvlv ...
141
  	else
4635469f5   Linus Lüssing   batman-adv: Make ...
142
  		set_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities);
3f4841ffb   Marek Lindner   batman-adv: tvlv ...
143
144
145
  }
  
  /**
6c519bad7   Matthias Schiffer   batman-adv: set u...
146
   * batadv_nc_mesh_init - initialise coding hash table and start house keeping
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
147
   * @bat_priv: the bat priv with all the soft interface information
672e79785   Sven Eckelmann   batman-adv: Fix k...
148
149
   *
   * Return: 0 on success or negative error number in case of failure
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
150
   */
6c519bad7   Matthias Schiffer   batman-adv: set u...
151
  int batadv_nc_mesh_init(struct batadv_priv *bat_priv)
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
152
  {
953324776   Martin Hundebøll   batman-adv: netwo...
153
  	bat_priv->nc.timestamp_fwd_flush = jiffies;
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
154
  	bat_priv->nc.timestamp_sniffed_purge = jiffies;
953324776   Martin Hundebøll   batman-adv: netwo...
155

612d2b4fe   Martin Hundebøll   batman-adv: netwo...
156
  	if (bat_priv->nc.coding_hash || bat_priv->nc.decoding_hash)
953324776   Martin Hundebøll   batman-adv: netwo...
157
158
159
160
161
162
163
164
  		return 0;
  
  	bat_priv->nc.coding_hash = batadv_hash_new(128);
  	if (!bat_priv->nc.coding_hash)
  		goto err;
  
  	batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
  				   &batadv_nc_coding_hash_lock_class_key);
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
165
166
167
  	bat_priv->nc.decoding_hash = batadv_hash_new(128);
  	if (!bat_priv->nc.decoding_hash)
  		goto err;
f44d54077   Martin Hundebøll   batman-adv: fix l...
168
  	batadv_hash_set_lock_class(bat_priv->nc.decoding_hash,
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
169
  				   &batadv_nc_decoding_hash_lock_class_key);
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
170
171
  	INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker);
  	batadv_nc_start_timer(bat_priv);
3f4841ffb   Marek Lindner   batman-adv: tvlv ...
172
173
174
175
  	batadv_tvlv_handler_register(bat_priv, batadv_nc_tvlv_ogm_handler_v1,
  				     NULL, BATADV_TVLV_NC, 1,
  				     BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  	batadv_nc_tvlv_container_update(bat_priv);
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
176
  	return 0;
953324776   Martin Hundebøll   batman-adv: netwo...
177
178
179
  
  err:
  	return -ENOMEM;
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
180
181
182
183
184
185
186
187
  }
  
  /**
   * batadv_nc_init_bat_priv - initialise the nc specific bat_priv variables
   * @bat_priv: the bat priv with all the soft interface information
   */
  void batadv_nc_init_bat_priv(struct batadv_priv *bat_priv)
  {
dab7b6219   Sven Eckelmann   batman-adv: Use s...
188
  	atomic_set(&bat_priv->network_coding, 0);
d56b1705e   Martin Hundebøll   batman-adv: netwo...
189
  	bat_priv->nc.min_tq = 200;
953324776   Martin Hundebøll   batman-adv: netwo...
190
  	bat_priv->nc.max_fwd_delay = 10;
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
191
  	bat_priv->nc.max_buffer_time = 200;
d56b1705e   Martin Hundebøll   batman-adv: netwo...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  }
  
  /**
   * batadv_nc_init_orig - initialise the nc fields of an orig_node
   * @orig_node: the orig_node which is going to be initialised
   */
  void batadv_nc_init_orig(struct batadv_orig_node *orig_node)
  {
  	INIT_LIST_HEAD(&orig_node->in_coding_list);
  	INIT_LIST_HEAD(&orig_node->out_coding_list);
  	spin_lock_init(&orig_node->in_coding_list_lock);
  	spin_lock_init(&orig_node->out_coding_list_lock);
  }
  
  /**
44e8e7e91   Sven Eckelmann   batman-adv: Avoid...
207
208
   * batadv_nc_node_release - release nc_node from lists and queue for free after
   *  rcu grace period
daf99b481   Sven Eckelmann   batman-adv: Conve...
209
   * @ref: kref pointer of the nc_node
d56b1705e   Martin Hundebøll   batman-adv: netwo...
210
   */
daf99b481   Sven Eckelmann   batman-adv: Conve...
211
  static void batadv_nc_node_release(struct kref *ref)
d56b1705e   Martin Hundebøll   batman-adv: netwo...
212
  {
daf99b481   Sven Eckelmann   batman-adv: Conve...
213
214
215
  	struct batadv_nc_node *nc_node;
  
  	nc_node = container_of(ref, struct batadv_nc_node, refcount);
5d9673109   Sven Eckelmann   batman-adv: Renam...
216
  	batadv_orig_node_put(nc_node->orig_node);
44e8e7e91   Sven Eckelmann   batman-adv: Avoid...
217
  	kfree_rcu(nc_node, rcu);
d56b1705e   Martin Hundebøll   batman-adv: netwo...
218
219
220
  }
  
  /**
27ad7545b   Sven Eckelmann   batman-adv: Renam...
221
   * batadv_nc_node_put - decrement the nc_node refcounter and possibly
44e8e7e91   Sven Eckelmann   batman-adv: Avoid...
222
   *  release it
daf99b481   Sven Eckelmann   batman-adv: Conve...
223
   * @nc_node: nc_node to be free'd
d56b1705e   Martin Hundebøll   batman-adv: netwo...
224
   */
27ad7545b   Sven Eckelmann   batman-adv: Renam...
225
  static void batadv_nc_node_put(struct batadv_nc_node *nc_node)
d56b1705e   Martin Hundebøll   batman-adv: netwo...
226
  {
daf99b481   Sven Eckelmann   batman-adv: Conve...
227
  	kref_put(&nc_node->refcount, batadv_nc_node_release);
d56b1705e   Martin Hundebøll   batman-adv: netwo...
228
229
230
  }
  
  /**
727e0cd59   Sven Eckelmann   batman-adv: Conve...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
   * batadv_nc_path_release - release nc_path from lists and queue for free after
   *  rcu grace period
   * @ref: kref pointer of the nc_path
   */
  static void batadv_nc_path_release(struct kref *ref)
  {
  	struct batadv_nc_path *nc_path;
  
  	nc_path = container_of(ref, struct batadv_nc_path, refcount);
  
  	kfree_rcu(nc_path, rcu);
  }
  
  /**
5fff28255   Sven Eckelmann   batman-adv: Renam...
245
   * batadv_nc_path_put - decrement the nc_path refcounter and possibly
727e0cd59   Sven Eckelmann   batman-adv: Conve...
246
247
   *  release it
   * @nc_path: nc_path to be free'd
953324776   Martin Hundebøll   batman-adv: netwo...
248
   */
5fff28255   Sven Eckelmann   batman-adv: Renam...
249
  static void batadv_nc_path_put(struct batadv_nc_path *nc_path)
953324776   Martin Hundebøll   batman-adv: netwo...
250
  {
727e0cd59   Sven Eckelmann   batman-adv: Conve...
251
  	kref_put(&nc_path->refcount, batadv_nc_path_release);
953324776   Martin Hundebøll   batman-adv: netwo...
252
253
254
255
256
  }
  
  /**
   * batadv_nc_packet_free - frees nc packet
   * @nc_packet: the nc packet to free
bd687fe41   Sven Eckelmann   batman-adv: use c...
257
   * @dropped: whether the packet is freed because is is dropped
953324776   Martin Hundebøll   batman-adv: netwo...
258
   */
bd687fe41   Sven Eckelmann   batman-adv: use c...
259
260
  static void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet,
  				  bool dropped)
953324776   Martin Hundebøll   batman-adv: netwo...
261
  {
bd687fe41   Sven Eckelmann   batman-adv: use c...
262
263
264
265
  	if (dropped)
  		kfree_skb(nc_packet->skb);
  	else
  		consume_skb(nc_packet->skb);
5fff28255   Sven Eckelmann   batman-adv: Renam...
266
  	batadv_nc_path_put(nc_packet->nc_path);
953324776   Martin Hundebøll   batman-adv: netwo...
267
268
269
270
  	kfree(nc_packet);
  }
  
  /**
d56b1705e   Martin Hundebøll   batman-adv: netwo...
271
272
273
274
   * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged
   * @bat_priv: the bat priv with all the soft interface information
   * @nc_node: the nc node to check
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
275
   * Return: true if the entry has to be purged now, false otherwise
d56b1705e   Martin Hundebøll   batman-adv: netwo...
276
277
278
279
280
281
282
283
284
285
286
   */
  static bool batadv_nc_to_purge_nc_node(struct batadv_priv *bat_priv,
  				       struct batadv_nc_node *nc_node)
  {
  	if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  		return true;
  
  	return batadv_has_timed_out(nc_node->last_seen, BATADV_NC_NODE_TIMEOUT);
  }
  
  /**
953324776   Martin Hundebøll   batman-adv: netwo...
287
288
289
290
   * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out
   * @bat_priv: the bat priv with all the soft interface information
   * @nc_path: the nc path to check
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
291
   * Return: true if the entry has to be purged now, false otherwise
953324776   Martin Hundebøll   batman-adv: netwo...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
   */
  static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv *bat_priv,
  					      struct batadv_nc_path *nc_path)
  {
  	if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  		return true;
  
  	/* purge the path when no packets has been added for 10 times the
  	 * max_fwd_delay time
  	 */
  	return batadv_has_timed_out(nc_path->last_valid,
  				    bat_priv->nc.max_fwd_delay * 10);
  }
  
  /**
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
307
308
309
310
   * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out
   * @bat_priv: the bat priv with all the soft interface information
   * @nc_path: the nc path to check
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
311
   * Return: true if the entry has to be purged now, false otherwise
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
312
313
314
315
316
317
318
319
320
321
322
   */
  static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv *bat_priv,
  						struct batadv_nc_path *nc_path)
  {
  	if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  		return true;
  
  	/* purge the path when no packets has been added for 10 times the
  	 * max_buffer time
  	 */
  	return batadv_has_timed_out(nc_path->last_valid,
fc1f86936   Marek Lindner   batman-adv: check...
323
  				    bat_priv->nc.max_buffer_time * 10);
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
324
325
326
  }
  
  /**
d56b1705e   Martin Hundebøll   batman-adv: netwo...
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
   * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
   *  entries
   * @bat_priv: the bat priv with all the soft interface information
   * @list: list of nc nodes
   * @lock: nc node list lock
   * @to_purge: function in charge to decide whether an entry has to be purged or
   *	      not. This function takes the nc node as argument and has to return
   *	      a boolean value: true if the entry has to be deleted, false
   *	      otherwise
   */
  static void
  batadv_nc_purge_orig_nc_nodes(struct batadv_priv *bat_priv,
  			      struct list_head *list,
  			      spinlock_t *lock,
  			      bool (*to_purge)(struct batadv_priv *,
  					       struct batadv_nc_node *))
  {
  	struct batadv_nc_node *nc_node, *nc_node_tmp;
  
  	/* For each nc_node in list */
  	spin_lock_bh(lock);
  	list_for_each_entry_safe(nc_node, nc_node_tmp, list, list) {
  		/* if an helper function has been passed as parameter,
  		 * ask it if the entry has to be purged or not
  		 */
  		if (to_purge && !to_purge(bat_priv, nc_node))
  			continue;
  
  		batadv_dbg(BATADV_DBG_NC, bat_priv,
  			   "Removing nc_node %pM -> %pM
  ",
  			   nc_node->addr, nc_node->orig_node->orig);
  		list_del_rcu(&nc_node->list);
27ad7545b   Sven Eckelmann   batman-adv: Renam...
360
  		batadv_nc_node_put(nc_node);
d56b1705e   Martin Hundebøll   batman-adv: netwo...
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
  	}
  	spin_unlock_bh(lock);
  }
  
  /**
   * batadv_nc_purge_orig - purges all nc node data attached of the given
   *  originator
   * @bat_priv: the bat priv with all the soft interface information
   * @orig_node: orig_node with the nc node entries to be purged
   * @to_purge: function in charge to decide whether an entry has to be purged or
   *	      not. This function takes the nc node as argument and has to return
   *	      a boolean value: true is the entry has to be deleted, false
   *	      otherwise
   */
  void batadv_nc_purge_orig(struct batadv_priv *bat_priv,
  			  struct batadv_orig_node *orig_node,
  			  bool (*to_purge)(struct batadv_priv *,
  					   struct batadv_nc_node *))
  {
  	/* Check ingoing nc_node's of this orig_node */
  	batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->in_coding_list,
  				      &orig_node->in_coding_list_lock,
  				      to_purge);
  
  	/* Check outgoing nc_node's of this orig_node */
  	batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->out_coding_list,
  				      &orig_node->out_coding_list_lock,
  				      to_purge);
  }
  
  /**
   * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they
   *  have timed out nc nodes
   * @bat_priv: the bat priv with all the soft interface information
   */
  static void batadv_nc_purge_orig_hash(struct batadv_priv *bat_priv)
  {
  	struct batadv_hashtable *hash = bat_priv->orig_hash;
  	struct hlist_head *head;
  	struct batadv_orig_node *orig_node;
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
401
  	u32 i;
d56b1705e   Martin Hundebøll   batman-adv: netwo...
402
403
404
405
406
407
408
409
410
411
412
413
414
415
  
  	if (!hash)
  		return;
  
  	/* For each orig_node */
  	for (i = 0; i < hash->size; i++) {
  		head = &hash->table[i];
  
  		rcu_read_lock();
  		hlist_for_each_entry_rcu(orig_node, head, hash_entry)
  			batadv_nc_purge_orig(bat_priv, orig_node,
  					     batadv_nc_to_purge_nc_node);
  		rcu_read_unlock();
  	}
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
416
417
418
  }
  
  /**
953324776   Martin Hundebøll   batman-adv: netwo...
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
   * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
   *  unused ones
   * @bat_priv: the bat priv with all the soft interface information
   * @hash: hash table containing the nc paths to check
   * @to_purge: function in charge to decide whether an entry has to be purged or
   *	      not. This function takes the nc node as argument and has to return
   *	      a boolean value: true is the entry has to be deleted, false
   *	      otherwise
   */
  static void batadv_nc_purge_paths(struct batadv_priv *bat_priv,
  				  struct batadv_hashtable *hash,
  				  bool (*to_purge)(struct batadv_priv *,
  						   struct batadv_nc_path *))
  {
  	struct hlist_head *head;
  	struct hlist_node *node_tmp;
  	struct batadv_nc_path *nc_path;
  	spinlock_t *lock; /* Protects lists in hash */
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
437
  	u32 i;
953324776   Martin Hundebøll   batman-adv: netwo...
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
  
  	for (i = 0; i < hash->size; i++) {
  		head = &hash->table[i];
  		lock = &hash->list_locks[i];
  
  		/* For each nc_path in this bin */
  		spin_lock_bh(lock);
  		hlist_for_each_entry_safe(nc_path, node_tmp, head, hash_entry) {
  			/* if an helper function has been passed as parameter,
  			 * ask it if the entry has to be purged or not
  			 */
  			if (to_purge && !to_purge(bat_priv, nc_path))
  				continue;
  
  			/* purging an non-empty nc_path should never happen, but
  			 * is observed under high CPU load. Delay the purging
  			 * until next iteration to allow the packet_list to be
  			 * emptied first.
  			 */
  			if (!unlikely(list_empty(&nc_path->packet_list))) {
  				net_ratelimited_function(printk,
  							 KERN_WARNING
  							 "Skipping free of non-empty nc_path (%pM -> %pM)!
  ",
  							 nc_path->prev_hop,
  							 nc_path->next_hop);
  				continue;
  			}
  
  			/* nc_path is unused, so remove it */
  			batadv_dbg(BATADV_DBG_NC, bat_priv,
  				   "Remove nc_path %pM -> %pM
  ",
  				   nc_path->prev_hop, nc_path->next_hop);
  			hlist_del_rcu(&nc_path->hash_entry);
5fff28255   Sven Eckelmann   batman-adv: Renam...
473
  			batadv_nc_path_put(nc_path);
953324776   Martin Hundebøll   batman-adv: netwo...
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
  		}
  		spin_unlock_bh(lock);
  	}
  }
  
  /**
   * batadv_nc_hash_key_gen - computes the nc_path hash key
   * @key: buffer to hold the final hash key
   * @src: source ethernet mac address going into the hash key
   * @dst: destination ethernet mac address going into the hash key
   */
  static void batadv_nc_hash_key_gen(struct batadv_nc_path *key, const char *src,
  				   const char *dst)
  {
  	memcpy(key->prev_hop, src, sizeof(key->prev_hop));
  	memcpy(key->next_hop, dst, sizeof(key->next_hop));
  }
  
  /**
   * batadv_nc_hash_choose - compute the hash value for an nc path
   * @data: data to hash
   * @size: size of the hash table
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
497
   * Return: the selected index in the hash table for the given data.
953324776   Martin Hundebøll   batman-adv: netwo...
498
   */
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
499
  static u32 batadv_nc_hash_choose(const void *data, u32 size)
953324776   Martin Hundebøll   batman-adv: netwo...
500
501
  {
  	const struct batadv_nc_path *nc_path = data;
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
502
  	u32 hash = 0;
953324776   Martin Hundebøll   batman-adv: netwo...
503

36fd61cb8   Sven Eckelmann   batman-adv: Use c...
504
505
  	hash = jhash(&nc_path->prev_hop, sizeof(nc_path->prev_hop), hash);
  	hash = jhash(&nc_path->next_hop, sizeof(nc_path->next_hop), hash);
953324776   Martin Hundebøll   batman-adv: netwo...
506
507
508
509
510
511
512
513
514
515
  
  	return hash % size;
  }
  
  /**
   * batadv_nc_hash_compare - comparing function used in the network coding hash
   *  tables
   * @node: node in the local table
   * @data2: second object to compare the node to
   *
4b426b108   Sven Eckelmann   batman-adv: Use b...
516
   * Return: true if the two entry are the same, false otherwise
953324776   Martin Hundebøll   batman-adv: netwo...
517
   */
4b426b108   Sven Eckelmann   batman-adv: Use b...
518
519
  static bool batadv_nc_hash_compare(const struct hlist_node *node,
  				   const void *data2)
953324776   Martin Hundebøll   batman-adv: netwo...
520
521
522
523
524
525
526
  {
  	const struct batadv_nc_path *nc_path1, *nc_path2;
  
  	nc_path1 = container_of(node, struct batadv_nc_path, hash_entry);
  	nc_path2 = data2;
  
  	/* Return 1 if the two keys are identical */
676970e55   Antonio Quartulli   batman-adv: use b...
527
  	if (!batadv_compare_eth(nc_path1->prev_hop, nc_path2->prev_hop))
4b426b108   Sven Eckelmann   batman-adv: Use b...
528
  		return false;
953324776   Martin Hundebøll   batman-adv: netwo...
529

676970e55   Antonio Quartulli   batman-adv: use b...
530
  	if (!batadv_compare_eth(nc_path1->next_hop, nc_path2->next_hop))
4b426b108   Sven Eckelmann   batman-adv: Use b...
531
  		return false;
953324776   Martin Hundebøll   batman-adv: netwo...
532

4b426b108   Sven Eckelmann   batman-adv: Use b...
533
  	return true;
953324776   Martin Hundebøll   batman-adv: netwo...
534
535
536
537
538
539
540
  }
  
  /**
   * batadv_nc_hash_find - search for an existing nc path and return it
   * @hash: hash table containing the nc path
   * @data: search key
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
541
   * Return: the nc_path if found, NULL otherwise.
953324776   Martin Hundebøll   batman-adv: netwo...
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
   */
  static struct batadv_nc_path *
  batadv_nc_hash_find(struct batadv_hashtable *hash,
  		    void *data)
  {
  	struct hlist_head *head;
  	struct batadv_nc_path *nc_path, *nc_path_tmp = NULL;
  	int index;
  
  	if (!hash)
  		return NULL;
  
  	index = batadv_nc_hash_choose(data, hash->size);
  	head = &hash->table[index];
  
  	rcu_read_lock();
  	hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
  		if (!batadv_nc_hash_compare(&nc_path->hash_entry, data))
  			continue;
727e0cd59   Sven Eckelmann   batman-adv: Conve...
561
  		if (!kref_get_unless_zero(&nc_path->refcount))
953324776   Martin Hundebøll   batman-adv: netwo...
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
  			continue;
  
  		nc_path_tmp = nc_path;
  		break;
  	}
  	rcu_read_unlock();
  
  	return nc_path_tmp;
  }
  
  /**
   * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
   * @nc_packet: the nc packet to send
   */
  static void batadv_nc_send_packet(struct batadv_nc_packet *nc_packet)
  {
95d392784   Antonio Quartulli   batman-adv: keep ...
578
  	batadv_send_unicast_skb(nc_packet->skb, nc_packet->neigh_node);
953324776   Martin Hundebøll   batman-adv: netwo...
579
  	nc_packet->skb = NULL;
bd687fe41   Sven Eckelmann   batman-adv: use c...
580
  	batadv_nc_packet_free(nc_packet, false);
953324776   Martin Hundebøll   batman-adv: netwo...
581
582
583
  }
  
  /**
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
584
585
586
587
588
589
590
591
592
   * batadv_nc_sniffed_purge - Checks timestamp of given sniffed nc_packet.
   * @bat_priv: the bat priv with all the soft interface information
   * @nc_path: the nc path the packet belongs to
   * @nc_packet: the nc packet to be checked
   *
   * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
   * timeout. If so, the packet is no longer kept and the entry deleted from the
   * queue. Has to be called with the appropriate locks.
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
593
   * Return: false as soon as the entry in the fifo queue has not been timed out
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
594
595
596
597
598
599
600
601
   * yet and true otherwise.
   */
  static bool batadv_nc_sniffed_purge(struct batadv_priv *bat_priv,
  				    struct batadv_nc_path *nc_path,
  				    struct batadv_nc_packet *nc_packet)
  {
  	unsigned long timeout = bat_priv->nc.max_buffer_time;
  	bool res = false;
2c72d655b   Sven Eckelmann   batman-adv: Annot...
602
  	lockdep_assert_held(&nc_path->packet_list_lock);
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
603
604
605
606
607
608
609
610
611
  	/* Packets are added to tail, so the remaining packets did not time
  	 * out and we can stop processing the current queue
  	 */
  	if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
  	    !batadv_has_timed_out(nc_packet->timestamp, timeout))
  		goto out;
  
  	/* purge nc packet */
  	list_del(&nc_packet->list);
bd687fe41   Sven Eckelmann   batman-adv: use c...
612
  	batadv_nc_packet_free(nc_packet, true);
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
613
614
615
616
617
618
619
620
  
  	res = true;
  
  out:
  	return res;
  }
  
  /**
953324776   Martin Hundebøll   batman-adv: netwo...
621
622
623
624
625
626
627
628
629
   * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet.
   * @bat_priv: the bat priv with all the soft interface information
   * @nc_path: the nc path the packet belongs to
   * @nc_packet: the nc packet to be checked
   *
   * Checks whether the given nc packet has hit its forward timeout. If so, the
   * packet is no longer delayed, immediately sent and the entry deleted from the
   * queue. Has to be called with the appropriate locks.
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
630
   * Return: false as soon as the entry in the fifo queue has not been timed out
953324776   Martin Hundebøll   batman-adv: netwo...
631
632
633
634
635
636
637
   * yet and true otherwise.
   */
  static bool batadv_nc_fwd_flush(struct batadv_priv *bat_priv,
  				struct batadv_nc_path *nc_path,
  				struct batadv_nc_packet *nc_packet)
  {
  	unsigned long timeout = bat_priv->nc.max_fwd_delay;
2c72d655b   Sven Eckelmann   batman-adv: Annot...
638
  	lockdep_assert_held(&nc_path->packet_list_lock);
953324776   Martin Hundebøll   batman-adv: netwo...
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
  	/* Packets are added to tail, so the remaining packets did not time
  	 * out and we can stop processing the current queue
  	 */
  	if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
  	    !batadv_has_timed_out(nc_packet->timestamp, timeout))
  		return false;
  
  	/* Send packet */
  	batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  	batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  			   nc_packet->skb->len + ETH_HLEN);
  	list_del(&nc_packet->list);
  	batadv_nc_send_packet(nc_packet);
  
  	return true;
  }
  
  /**
   * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out
   *  nc packets
   * @bat_priv: the bat priv with all the soft interface information
   * @hash: to be processed hash table
   * @process_fn: Function called to process given nc packet. Should return true
   *	        to encourage this function to proceed with the next packet.
   *	        Otherwise the rest of the current queue is skipped.
   */
  static void
  batadv_nc_process_nc_paths(struct batadv_priv *bat_priv,
  			   struct batadv_hashtable *hash,
  			   bool (*process_fn)(struct batadv_priv *,
  					      struct batadv_nc_path *,
  					      struct batadv_nc_packet *))
  {
  	struct hlist_head *head;
  	struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
  	struct batadv_nc_path *nc_path;
  	bool ret;
  	int i;
  
  	if (!hash)
  		return;
  
  	/* Loop hash table bins */
  	for (i = 0; i < hash->size; i++) {
  		head = &hash->table[i];
  
  		/* Loop coding paths */
  		rcu_read_lock();
  		hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
  			/* Loop packets */
  			spin_lock_bh(&nc_path->packet_list_lock);
  			list_for_each_entry_safe(nc_packet, nc_packet_tmp,
  						 &nc_path->packet_list, list) {
  				ret = process_fn(bat_priv, nc_path, nc_packet);
  				if (!ret)
  					break;
  			}
  			spin_unlock_bh(&nc_path->packet_list_lock);
  		}
  		rcu_read_unlock();
  	}
  }
  
  /**
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
703
704
705
706
707
708
709
710
   * batadv_nc_worker - periodic task for house keeping related to network coding
   * @work: kernel work struct
   */
  static void batadv_nc_worker(struct work_struct *work)
  {
  	struct delayed_work *delayed_work;
  	struct batadv_priv_nc *priv_nc;
  	struct batadv_priv *bat_priv;
953324776   Martin Hundebøll   batman-adv: netwo...
711
  	unsigned long timeout;
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
712

4ba4bc0f7   Geliang Tang   batman-adv: use t...
713
  	delayed_work = to_delayed_work(work);
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
714
715
  	priv_nc = container_of(delayed_work, struct batadv_priv_nc, work);
  	bat_priv = container_of(priv_nc, struct batadv_priv, nc);
d56b1705e   Martin Hundebøll   batman-adv: netwo...
716
  	batadv_nc_purge_orig_hash(bat_priv);
953324776   Martin Hundebøll   batman-adv: netwo...
717
718
  	batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash,
  			      batadv_nc_to_purge_nc_path_coding);
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
719
720
  	batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash,
  			      batadv_nc_to_purge_nc_path_decoding);
953324776   Martin Hundebøll   batman-adv: netwo...
721
722
723
724
725
726
727
728
  
  	timeout = bat_priv->nc.max_fwd_delay;
  
  	if (batadv_has_timed_out(bat_priv->nc.timestamp_fwd_flush, timeout)) {
  		batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.coding_hash,
  					   batadv_nc_fwd_flush);
  		bat_priv->nc.timestamp_fwd_flush = jiffies;
  	}
d56b1705e   Martin Hundebøll   batman-adv: netwo...
729

612d2b4fe   Martin Hundebøll   batman-adv: netwo...
730
731
732
733
734
735
  	if (batadv_has_timed_out(bat_priv->nc.timestamp_sniffed_purge,
  				 bat_priv->nc.max_buffer_time)) {
  		batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.decoding_hash,
  					   batadv_nc_sniffed_purge);
  		bat_priv->nc.timestamp_sniffed_purge = jiffies;
  	}
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
736
737
738
739
740
  	/* Schedule a new check */
  	batadv_nc_start_timer(bat_priv);
  }
  
  /**
d56b1705e   Martin Hundebøll   batman-adv: netwo...
741
742
743
744
745
746
   * batadv_can_nc_with_orig - checks whether the given orig node is suitable for
   *  coding or not
   * @bat_priv: the bat priv with all the soft interface information
   * @orig_node: neighboring orig node which may be used as nc candidate
   * @ogm_packet: incoming ogm packet also used for the checks
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
747
   * Return: true if:
d56b1705e   Martin Hundebøll   batman-adv: netwo...
748
749
750
751
752
753
754
755
756
   *  1) The OGM must have the most recent sequence number.
   *  2) The TTL must be decremented by one and only one.
   *  3) The OGM must be received from the first hop from orig_node.
   *  4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
   */
  static bool batadv_can_nc_with_orig(struct batadv_priv *bat_priv,
  				    struct batadv_orig_node *orig_node,
  				    struct batadv_ogm_packet *ogm_packet)
  {
7351a4822   Simon Wunderlich   batman-adv: split...
757
  	struct batadv_orig_ifinfo *orig_ifinfo;
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
758
759
  	u32 last_real_seqno;
  	u8 last_ttl;
7351a4822   Simon Wunderlich   batman-adv: split...
760
761
762
  
  	orig_ifinfo = batadv_orig_ifinfo_get(orig_node, BATADV_IF_DEFAULT);
  	if (!orig_ifinfo)
d56b1705e   Martin Hundebøll   batman-adv: netwo...
763
  		return false;
7351a4822   Simon Wunderlich   batman-adv: split...
764
765
766
  
  	last_ttl = orig_ifinfo->last_ttl;
  	last_real_seqno = orig_ifinfo->last_real_seqno;
35f94779c   Sven Eckelmann   batman-adv: Renam...
767
  	batadv_orig_ifinfo_put(orig_ifinfo);
7351a4822   Simon Wunderlich   batman-adv: split...
768
769
770
771
  
  	if (last_real_seqno != ntohl(ogm_packet->seqno))
  		return false;
  	if (last_ttl != ogm_packet->ttl + 1)
d56b1705e   Martin Hundebøll   batman-adv: netwo...
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
  		return false;
  	if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender))
  		return false;
  	if (ogm_packet->tq < bat_priv->nc.min_tq)
  		return false;
  
  	return true;
  }
  
  /**
   * batadv_nc_find_nc_node - search for an existing nc node and return it
   * @orig_node: orig node originating the ogm packet
   * @orig_neigh_node: neighboring orig node from which we received the ogm packet
   *  (can be equal to orig_node)
   * @in_coding: traverse incoming or outgoing network coding list
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
788
   * Return: the nc_node if found, NULL otherwise.
d56b1705e   Martin Hundebøll   batman-adv: netwo...
789
   */
6fc77a548   Sven Eckelmann   batman-adv: Fix f...
790
791
792
793
  static struct batadv_nc_node *
  batadv_nc_find_nc_node(struct batadv_orig_node *orig_node,
  		       struct batadv_orig_node *orig_neigh_node,
  		       bool in_coding)
d56b1705e   Martin Hundebøll   batman-adv: netwo...
794
795
796
797
798
799
800
801
802
803
804
805
806
807
  {
  	struct batadv_nc_node *nc_node, *nc_node_out = NULL;
  	struct list_head *list;
  
  	if (in_coding)
  		list = &orig_neigh_node->in_coding_list;
  	else
  		list = &orig_neigh_node->out_coding_list;
  
  	/* Traverse list of nc_nodes to orig_node */
  	rcu_read_lock();
  	list_for_each_entry_rcu(nc_node, list, list) {
  		if (!batadv_compare_eth(nc_node->addr, orig_node->orig))
  			continue;
daf99b481   Sven Eckelmann   batman-adv: Conve...
808
  		if (!kref_get_unless_zero(&nc_node->refcount))
d56b1705e   Martin Hundebøll   batman-adv: netwo...
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
  			continue;
  
  		/* Found a match */
  		nc_node_out = nc_node;
  		break;
  	}
  	rcu_read_unlock();
  
  	return nc_node_out;
  }
  
  /**
   * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
   *  not found
   * @bat_priv: the bat priv with all the soft interface information
   * @orig_node: orig node originating the ogm packet
   * @orig_neigh_node: neighboring orig node from which we received the ogm packet
   *  (can be equal to orig_node)
   * @in_coding: traverse incoming or outgoing network coding list
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
829
   * Return: the nc_node if found or created, NULL in case of an error.
d56b1705e   Martin Hundebøll   batman-adv: netwo...
830
   */
6fc77a548   Sven Eckelmann   batman-adv: Fix f...
831
832
833
834
835
  static struct batadv_nc_node *
  batadv_nc_get_nc_node(struct batadv_priv *bat_priv,
  		      struct batadv_orig_node *orig_node,
  		      struct batadv_orig_node *orig_neigh_node,
  		      bool in_coding)
d56b1705e   Martin Hundebøll   batman-adv: netwo...
836
837
838
839
  {
  	struct batadv_nc_node *nc_node;
  	spinlock_t *lock; /* Used to lock list selected by "int in_coding" */
  	struct list_head *list;
18733cfe4   Sven Eckelmann   batman-adv: Preve...
840
841
842
843
844
845
846
847
848
849
  	/* Select ingoing or outgoing coding node */
  	if (in_coding) {
  		lock = &orig_neigh_node->in_coding_list_lock;
  		list = &orig_neigh_node->in_coding_list;
  	} else {
  		lock = &orig_neigh_node->out_coding_list_lock;
  		list = &orig_neigh_node->out_coding_list;
  	}
  
  	spin_lock_bh(lock);
d56b1705e   Martin Hundebøll   batman-adv: netwo...
850
851
852
853
854
  	/* Check if nc_node is already added */
  	nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding);
  
  	/* Node found */
  	if (nc_node)
18733cfe4   Sven Eckelmann   batman-adv: Preve...
855
  		goto unlock;
d56b1705e   Martin Hundebøll   batman-adv: netwo...
856
857
858
  
  	nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC);
  	if (!nc_node)
18733cfe4   Sven Eckelmann   batman-adv: Preve...
859
  		goto unlock;
d56b1705e   Martin Hundebøll   batman-adv: netwo...
860

d56b1705e   Martin Hundebøll   batman-adv: netwo...
861
862
  	/* Initialize nc_node */
  	INIT_LIST_HEAD(&nc_node->list);
daf99b481   Sven Eckelmann   batman-adv: Conve...
863
  	kref_init(&nc_node->refcount);
55db2d590   Sven Eckelmann   batman-adv: Place...
864
865
866
  	ether_addr_copy(nc_node->addr, orig_node->orig);
  	kref_get(&orig_neigh_node->refcount);
  	nc_node->orig_node = orig_neigh_node;
d56b1705e   Martin Hundebøll   batman-adv: netwo...
867

d56b1705e   Martin Hundebøll   batman-adv: netwo...
868
869
870
871
872
  	batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM
  ",
  		   nc_node->addr, nc_node->orig_node->orig);
  
  	/* Add nc_node to orig_node */
da7a26af4   Sven Eckelmann   batman-adv: Place...
873
  	kref_get(&nc_node->refcount);
d56b1705e   Martin Hundebøll   batman-adv: netwo...
874
  	list_add_tail_rcu(&nc_node->list, list);
18733cfe4   Sven Eckelmann   batman-adv: Preve...
875
876
  
  unlock:
d56b1705e   Martin Hundebøll   batman-adv: netwo...
877
878
879
  	spin_unlock_bh(lock);
  
  	return nc_node;
d56b1705e   Martin Hundebøll   batman-adv: netwo...
880
881
882
  }
  
  /**
34473822d   Sven Eckelmann   batman-adv: Fix k...
883
884
   * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node
   *  structs (best called on incoming OGMs)
d56b1705e   Martin Hundebøll   batman-adv: netwo...
885
886
887
888
889
890
891
892
893
894
895
896
897
   * @bat_priv: the bat priv with all the soft interface information
   * @orig_node: orig node originating the ogm packet
   * @orig_neigh_node: neighboring orig node from which we received the ogm packet
   *  (can be equal to orig_node)
   * @ogm_packet: incoming ogm packet
   * @is_single_hop_neigh: orig_node is a single hop neighbor
   */
  void batadv_nc_update_nc_node(struct batadv_priv *bat_priv,
  			      struct batadv_orig_node *orig_node,
  			      struct batadv_orig_node *orig_neigh_node,
  			      struct batadv_ogm_packet *ogm_packet,
  			      int is_single_hop_neigh)
  {
4f248cff9   Sven Eckelmann   batman-adv: Remov...
898
899
  	struct batadv_nc_node *in_nc_node = NULL;
  	struct batadv_nc_node *out_nc_node = NULL;
d56b1705e   Martin Hundebøll   batman-adv: netwo...
900
901
902
903
  
  	/* Check if network coding is enabled */
  	if (!atomic_read(&bat_priv->network_coding))
  		goto out;
3f4841ffb   Marek Lindner   batman-adv: tvlv ...
904
  	/* check if orig node is network coding enabled */
4635469f5   Linus Lüssing   batman-adv: Make ...
905
  	if (!test_bit(BATADV_ORIG_CAPA_HAS_NC, &orig_node->capabilities))
3f4841ffb   Marek Lindner   batman-adv: tvlv ...
906
  		goto out;
d56b1705e   Martin Hundebøll   batman-adv: netwo...
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
  	/* accept ogms from 'good' neighbors and single hop neighbors */
  	if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) &&
  	    !is_single_hop_neigh)
  		goto out;
  
  	/* Add orig_node as in_nc_node on hop */
  	in_nc_node = batadv_nc_get_nc_node(bat_priv, orig_node,
  					   orig_neigh_node, true);
  	if (!in_nc_node)
  		goto out;
  
  	in_nc_node->last_seen = jiffies;
  
  	/* Add hop as out_nc_node on orig_node */
  	out_nc_node = batadv_nc_get_nc_node(bat_priv, orig_neigh_node,
  					    orig_node, false);
  	if (!out_nc_node)
  		goto out;
  
  	out_nc_node->last_seen = jiffies;
  
  out:
  	if (in_nc_node)
27ad7545b   Sven Eckelmann   batman-adv: Renam...
930
  		batadv_nc_node_put(in_nc_node);
d56b1705e   Martin Hundebøll   batman-adv: netwo...
931
  	if (out_nc_node)
27ad7545b   Sven Eckelmann   batman-adv: Renam...
932
  		batadv_nc_node_put(out_nc_node);
d56b1705e   Martin Hundebøll   batman-adv: netwo...
933
934
935
  }
  
  /**
953324776   Martin Hundebøll   batman-adv: netwo...
936
937
938
939
940
941
   * batadv_nc_get_path - get existing nc_path or allocate a new one
   * @bat_priv: the bat priv with all the soft interface information
   * @hash: hash table containing the nc path
   * @src: ethernet source address - first half of the nc path search key
   * @dst: ethernet destination address - second half of the nc path search key
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
942
   * Return: pointer to nc_path if the path was found or created, returns NULL
953324776   Martin Hundebøll   batman-adv: netwo...
943
944
945
946
   * on error.
   */
  static struct batadv_nc_path *batadv_nc_get_path(struct batadv_priv *bat_priv,
  						 struct batadv_hashtable *hash,
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
947
948
  						 u8 *src,
  						 u8 *dst)
953324776   Martin Hundebøll   batman-adv: netwo...
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
  {
  	int hash_added;
  	struct batadv_nc_path *nc_path, nc_path_key;
  
  	batadv_nc_hash_key_gen(&nc_path_key, src, dst);
  
  	/* Search for existing nc_path */
  	nc_path = batadv_nc_hash_find(hash, (void *)&nc_path_key);
  
  	if (nc_path) {
  		/* Set timestamp to delay removal of nc_path */
  		nc_path->last_valid = jiffies;
  		return nc_path;
  	}
  
  	/* No existing nc_path was found; create a new */
  	nc_path = kzalloc(sizeof(*nc_path), GFP_ATOMIC);
  
  	if (!nc_path)
  		return NULL;
  
  	/* Initialize nc_path */
  	INIT_LIST_HEAD(&nc_path->packet_list);
  	spin_lock_init(&nc_path->packet_list_lock);
727e0cd59   Sven Eckelmann   batman-adv: Conve...
973
  	kref_init(&nc_path->refcount);
953324776   Martin Hundebøll   batman-adv: netwo...
974
  	nc_path->last_valid = jiffies;
8fdd01530   Antonio Quartulli   batman-adv: prefe...
975
976
  	ether_addr_copy(nc_path->next_hop, dst);
  	ether_addr_copy(nc_path->prev_hop, src);
953324776   Martin Hundebøll   batman-adv: netwo...
977
978
979
980
981
982
983
  
  	batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_path %pM -> %pM
  ",
  		   nc_path->prev_hop,
  		   nc_path->next_hop);
  
  	/* Add nc_path to hash table */
f489eab5b   Sven Eckelmann   batman-adv: Place...
984
  	kref_get(&nc_path->refcount);
953324776   Martin Hundebøll   batman-adv: netwo...
985
986
987
988
989
990
991
992
993
994
995
996
997
  	hash_added = batadv_hash_add(hash, batadv_nc_hash_compare,
  				     batadv_nc_hash_choose, &nc_path_key,
  				     &nc_path->hash_entry);
  
  	if (hash_added < 0) {
  		kfree(nc_path);
  		return NULL;
  	}
  
  	return nc_path;
  }
  
  /**
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
998
999
1000
   * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
   *  selection of a receiver with slightly lower TQ than the other
   * @tq: to be weighted tq value
672e79785   Sven Eckelmann   batman-adv: Fix k...
1001
1002
   *
   * Return: scaled tq value
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1003
   */
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
1004
  static u8 batadv_nc_random_weight_tq(u8 tq)
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1005
  {
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
1006
  	u8 rand_val, rand_tq;
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
  
  	get_random_bytes(&rand_val, sizeof(rand_val));
  
  	/* randomize the estimated packet loss (max TQ - estimated TQ) */
  	rand_tq = rand_val * (BATADV_TQ_MAX_VALUE - tq);
  
  	/* normalize the randomized packet loss */
  	rand_tq /= BATADV_TQ_MAX_VALUE;
  
  	/* convert to (randomized) estimated tq again */
  	return BATADV_TQ_MAX_VALUE - rand_tq;
  }
  
  /**
   * batadv_nc_memxor - XOR destination with source
   * @dst: byte array to XOR into
   * @src: byte array to XOR from
   * @len: length of destination array
   */
  static void batadv_nc_memxor(char *dst, const char *src, unsigned int len)
  {
  	unsigned int i;
  
  	for (i = 0; i < len; ++i)
  		dst[i] ^= src[i];
  }
  
  /**
   * batadv_nc_code_packets - code a received unicast_packet with an nc packet
   *  into a coded_packet and send it
   * @bat_priv: the bat priv with all the soft interface information
   * @skb: data skb to forward
   * @ethhdr: pointer to the ethernet header inside the skb
   * @nc_packet: structure containing the packet to the skb can be coded with
   * @neigh_node: next hop to forward packet to
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
1043
   * Return: true if both packets are consumed, false otherwise.
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1044
1045
1046
1047
1048
1049
1050
   */
  static bool batadv_nc_code_packets(struct batadv_priv *bat_priv,
  				   struct sk_buff *skb,
  				   struct ethhdr *ethhdr,
  				   struct batadv_nc_packet *nc_packet,
  				   struct batadv_neigh_node *neigh_node)
  {
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
1051
  	u8 tq_weighted_neigh, tq_weighted_coding, tq_tmp;
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1052
1053
1054
1055
  	struct sk_buff *skb_dest, *skb_src;
  	struct batadv_unicast_packet *packet1;
  	struct batadv_unicast_packet *packet2;
  	struct batadv_coded_packet *coded_packet;
95d392784   Antonio Quartulli   batman-adv: keep ...
1056
1057
  	struct batadv_neigh_node *neigh_tmp, *router_neigh, *first_dest;
  	struct batadv_neigh_node *router_coding = NULL, *second_dest;
89652331c   Simon Wunderlich   batman-adv: split...
1058
1059
  	struct batadv_neigh_ifinfo *router_neigh_ifinfo = NULL;
  	struct batadv_neigh_ifinfo *router_coding_ifinfo = NULL;
95d392784   Antonio Quartulli   batman-adv: keep ...
1060
  	u8 *first_source, *second_source;
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1061
1062
1063
1064
1065
1066
1067
  	__be32 packet_id1, packet_id2;
  	size_t count;
  	bool res = false;
  	int coding_len;
  	int unicast_size = sizeof(*packet1);
  	int coded_size = sizeof(*coded_packet);
  	int header_add = coded_size - unicast_size;
7351a4822   Simon Wunderlich   batman-adv: split...
1068
1069
1070
1071
1072
  	/* TODO: do we need to consider the outgoing interface for
  	 * coded packets?
  	 */
  	router_neigh = batadv_orig_router_get(neigh_node->orig_node,
  					      BATADV_IF_DEFAULT);
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1073
1074
  	if (!router_neigh)
  		goto out;
89652331c   Simon Wunderlich   batman-adv: split...
1075
1076
1077
1078
  	router_neigh_ifinfo = batadv_neigh_ifinfo_get(router_neigh,
  						      BATADV_IF_DEFAULT);
  	if (!router_neigh_ifinfo)
  		goto out;
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1079
  	neigh_tmp = nc_packet->neigh_node;
7351a4822   Simon Wunderlich   batman-adv: split...
1080
1081
  	router_coding = batadv_orig_router_get(neigh_tmp->orig_node,
  					       BATADV_IF_DEFAULT);
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1082
1083
  	if (!router_coding)
  		goto out;
89652331c   Simon Wunderlich   batman-adv: split...
1084
1085
1086
1087
1088
1089
1090
1091
1092
  	router_coding_ifinfo = batadv_neigh_ifinfo_get(router_coding,
  						       BATADV_IF_DEFAULT);
  	if (!router_coding_ifinfo)
  		goto out;
  
  	tq_tmp = router_neigh_ifinfo->bat_iv.tq_avg;
  	tq_weighted_neigh = batadv_nc_random_weight_tq(tq_tmp);
  	tq_tmp = router_coding_ifinfo->bat_iv.tq_avg;
  	tq_weighted_coding = batadv_nc_random_weight_tq(tq_tmp);
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1093
1094
1095
1096
1097
1098
  
  	/* Select one destination for the MAC-header dst-field based on
  	 * weighted TQ-values.
  	 */
  	if (tq_weighted_neigh >= tq_weighted_coding) {
  		/* Destination from nc_packet is selected for MAC-header */
95d392784   Antonio Quartulli   batman-adv: keep ...
1099
  		first_dest = nc_packet->neigh_node;
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1100
  		first_source = nc_packet->nc_path->prev_hop;
95d392784   Antonio Quartulli   batman-adv: keep ...
1101
  		second_dest = neigh_node;
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1102
1103
1104
1105
1106
1107
1108
1109
  		second_source = ethhdr->h_source;
  		packet1 = (struct batadv_unicast_packet *)nc_packet->skb->data;
  		packet2 = (struct batadv_unicast_packet *)skb->data;
  		packet_id1 = nc_packet->packet_id;
  		packet_id2 = batadv_skb_crc32(skb,
  					      skb->data + sizeof(*packet2));
  	} else {
  		/* Destination for skb is selected for MAC-header */
95d392784   Antonio Quartulli   batman-adv: keep ...
1110
  		first_dest = neigh_node;
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1111
  		first_source = ethhdr->h_source;
95d392784   Antonio Quartulli   batman-adv: keep ...
1112
  		second_dest = nc_packet->neigh_node;
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
  		second_source = nc_packet->nc_path->prev_hop;
  		packet1 = (struct batadv_unicast_packet *)skb->data;
  		packet2 = (struct batadv_unicast_packet *)nc_packet->skb->data;
  		packet_id1 = batadv_skb_crc32(skb,
  					      skb->data + sizeof(*packet1));
  		packet_id2 = nc_packet->packet_id;
  	}
  
  	/* Instead of zero padding the smallest data buffer, we
  	 * code into the largest.
  	 */
  	if (skb->len <= nc_packet->skb->len) {
  		skb_dest = nc_packet->skb;
  		skb_src = skb;
  	} else {
  		skb_dest = skb;
  		skb_src = nc_packet->skb;
  	}
  
  	/* coding_len is used when decoding the packet shorter packet */
  	coding_len = skb_src->len - unicast_size;
  
  	if (skb_linearize(skb_dest) < 0 || skb_linearize(skb_src) < 0)
  		goto out;
  
  	skb_push(skb_dest, header_add);
  
  	coded_packet = (struct batadv_coded_packet *)skb_dest->data;
  	skb_reset_mac_header(skb_dest);
a40d9b075   Simon Wunderlich   batman-adv: fix h...
1142
1143
1144
  	coded_packet->packet_type = BATADV_CODED;
  	coded_packet->version = BATADV_COMPAT_VERSION;
  	coded_packet->ttl = packet1->ttl;
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1145
1146
  
  	/* Info about first unicast packet */
8fdd01530   Antonio Quartulli   batman-adv: prefe...
1147
1148
  	ether_addr_copy(coded_packet->first_source, first_source);
  	ether_addr_copy(coded_packet->first_orig_dest, packet1->dest);
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1149
1150
1151
1152
  	coded_packet->first_crc = packet_id1;
  	coded_packet->first_ttvn = packet1->ttvn;
  
  	/* Info about second unicast packet */
95d392784   Antonio Quartulli   batman-adv: keep ...
1153
  	ether_addr_copy(coded_packet->second_dest, second_dest->addr);
8fdd01530   Antonio Quartulli   batman-adv: prefe...
1154
1155
  	ether_addr_copy(coded_packet->second_source, second_source);
  	ether_addr_copy(coded_packet->second_orig_dest, packet2->dest);
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1156
  	coded_packet->second_crc = packet_id2;
a40d9b075   Simon Wunderlich   batman-adv: fix h...
1157
  	coded_packet->second_ttl = packet2->ttl;
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
  	coded_packet->second_ttvn = packet2->ttvn;
  	coded_packet->coded_len = htons(coding_len);
  
  	/* This is where the magic happens: Code skb_src into skb_dest */
  	batadv_nc_memxor(skb_dest->data + coded_size,
  			 skb_src->data + unicast_size, coding_len);
  
  	/* Update counters accordingly */
  	if (BATADV_SKB_CB(skb_src)->decoded &&
  	    BATADV_SKB_CB(skb_dest)->decoded) {
  		/* Both packets are recoded */
  		count = skb_src->len + ETH_HLEN;
  		count += skb_dest->len + ETH_HLEN;
  		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE, 2);
  		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, count);
  	} else if (!BATADV_SKB_CB(skb_src)->decoded &&
  		   !BATADV_SKB_CB(skb_dest)->decoded) {
  		/* Both packets are newly coded */
  		count = skb_src->len + ETH_HLEN;
  		count += skb_dest->len + ETH_HLEN;
  		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE, 2);
  		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, count);
  	} else if (BATADV_SKB_CB(skb_src)->decoded &&
  		   !BATADV_SKB_CB(skb_dest)->decoded) {
  		/* skb_src recoded and skb_dest is newly coded */
  		batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
  		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
  				   skb_src->len + ETH_HLEN);
  		batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
  		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
  				   skb_dest->len + ETH_HLEN);
  	} else if (!BATADV_SKB_CB(skb_src)->decoded &&
  		   BATADV_SKB_CB(skb_dest)->decoded) {
  		/* skb_src is newly coded and skb_dest is recoded */
  		batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
  		batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
  				   skb_src->len + ETH_HLEN);
  		batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
  		batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
  				   skb_dest->len + ETH_HLEN);
  	}
  
  	/* skb_src is now coded into skb_dest, so free it */
bd687fe41   Sven Eckelmann   batman-adv: use c...
1201
  	consume_skb(skb_src);
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1202
1203
1204
  
  	/* avoid duplicate free of skb from nc_packet */
  	nc_packet->skb = NULL;
bd687fe41   Sven Eckelmann   batman-adv: use c...
1205
  	batadv_nc_packet_free(nc_packet, false);
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1206
1207
  
  	/* Send the coded packet and return true */
95d392784   Antonio Quartulli   batman-adv: keep ...
1208
  	batadv_send_unicast_skb(skb_dest, first_dest);
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1209
1210
1211
  	res = true;
  out:
  	if (router_neigh)
25bb25099   Sven Eckelmann   batman-adv: Renam...
1212
  		batadv_neigh_node_put(router_neigh);
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1213
  	if (router_coding)
25bb25099   Sven Eckelmann   batman-adv: Renam...
1214
  		batadv_neigh_node_put(router_coding);
89652331c   Simon Wunderlich   batman-adv: split...
1215
  	if (router_neigh_ifinfo)
044fa3ae1   Sven Eckelmann   batman-adv: Renam...
1216
  		batadv_neigh_ifinfo_put(router_neigh_ifinfo);
89652331c   Simon Wunderlich   batman-adv: split...
1217
  	if (router_coding_ifinfo)
044fa3ae1   Sven Eckelmann   batman-adv: Renam...
1218
  		batadv_neigh_ifinfo_put(router_coding_ifinfo);
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
  	return res;
  }
  
  /**
   * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
   * @skb: data skb to forward
   * @dst: destination mac address of the other skb to code with
   * @src: source mac address of skb
   *
   * Whenever we network code a packet we have to check whether we received it in
   * a network coded form. If so, we may not be able to use it for coding because
   * some neighbors may also have received (overheard) the packet in the network
   * coded form without being able to decode it. It is hard to know which of the
   * neighboring nodes was able to decode the packet, therefore we can only
   * re-code the packet if the source of the previous encoded packet is involved.
   * Since the source encoded the packet we can be certain it has all necessary
   * decode information.
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
1237
   * Return: true if coding of a decoded packet is allowed.
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1238
   */
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
1239
  static bool batadv_nc_skb_coding_possible(struct sk_buff *skb, u8 *dst, u8 *src)
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1240
1241
1242
  {
  	if (BATADV_SKB_CB(skb)->decoded && !batadv_compare_eth(dst, src))
  		return false;
24820df14   Antonio Quartulli   batman-adv: check...
1243
  	return true;
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
  }
  
  /**
   * batadv_nc_path_search - Find the coding path matching in_nc_node and
   *  out_nc_node to retrieve a buffered packet that can be used for coding.
   * @bat_priv: the bat priv with all the soft interface information
   * @in_nc_node: pointer to skb next hop's neighbor nc node
   * @out_nc_node: pointer to skb source's neighbor nc node
   * @skb: data skb to forward
   * @eth_dst: next hop mac address of skb
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
1255
   * Return: true if coding of a decoded skb is allowed.
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1256
1257
1258
1259
1260
1261
   */
  static struct batadv_nc_packet *
  batadv_nc_path_search(struct batadv_priv *bat_priv,
  		      struct batadv_nc_node *in_nc_node,
  		      struct batadv_nc_node *out_nc_node,
  		      struct sk_buff *skb,
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
1262
  		      u8 *eth_dst)
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
  {
  	struct batadv_nc_path *nc_path, nc_path_key;
  	struct batadv_nc_packet *nc_packet_out = NULL;
  	struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
  	struct batadv_hashtable *hash = bat_priv->nc.coding_hash;
  	int idx;
  
  	if (!hash)
  		return NULL;
  
  	/* Create almost path key */
  	batadv_nc_hash_key_gen(&nc_path_key, in_nc_node->addr,
  			       out_nc_node->addr);
  	idx = batadv_nc_hash_choose(&nc_path_key, hash->size);
  
  	/* Check for coding opportunities in this nc_path */
  	rcu_read_lock();
  	hlist_for_each_entry_rcu(nc_path, &hash->table[idx], hash_entry) {
  		if (!batadv_compare_eth(nc_path->prev_hop, in_nc_node->addr))
  			continue;
  
  		if (!batadv_compare_eth(nc_path->next_hop, out_nc_node->addr))
  			continue;
  
  		spin_lock_bh(&nc_path->packet_list_lock);
  		if (list_empty(&nc_path->packet_list)) {
  			spin_unlock_bh(&nc_path->packet_list_lock);
  			continue;
  		}
  
  		list_for_each_entry_safe(nc_packet, nc_packet_tmp,
  					 &nc_path->packet_list, list) {
  			if (!batadv_nc_skb_coding_possible(nc_packet->skb,
  							   eth_dst,
  							   in_nc_node->addr))
  				continue;
  
  			/* Coding opportunity is found! */
  			list_del(&nc_packet->list);
  			nc_packet_out = nc_packet;
  			break;
  		}
  
  		spin_unlock_bh(&nc_path->packet_list_lock);
  		break;
  	}
  	rcu_read_unlock();
  
  	return nc_packet_out;
  }
  
  /**
   * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
   *  skb's sender (may be equal to the originator).
   * @bat_priv: the bat priv with all the soft interface information
   * @skb: data skb to forward
   * @eth_dst: next hop mac address of skb
   * @eth_src: source mac address of skb
   * @in_nc_node: pointer to skb next hop's neighbor nc node
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
1323
   * Return: an nc packet if a suitable coding packet was found, NULL otherwise.
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1324
1325
1326
1327
   */
  static struct batadv_nc_packet *
  batadv_nc_skb_src_search(struct batadv_priv *bat_priv,
  			 struct sk_buff *skb,
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
1328
1329
  			 u8 *eth_dst,
  			 u8 *eth_src,
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
  			 struct batadv_nc_node *in_nc_node)
  {
  	struct batadv_orig_node *orig_node;
  	struct batadv_nc_node *out_nc_node;
  	struct batadv_nc_packet *nc_packet = NULL;
  
  	orig_node = batadv_orig_hash_find(bat_priv, eth_src);
  	if (!orig_node)
  		return NULL;
  
  	rcu_read_lock();
  	list_for_each_entry_rcu(out_nc_node,
  				&orig_node->out_coding_list, list) {
  		/* Check if the skb is decoded and if recoding is possible */
  		if (!batadv_nc_skb_coding_possible(skb,
  						   out_nc_node->addr, eth_src))
  			continue;
  
  		/* Search for an opportunity in this nc_path */
  		nc_packet = batadv_nc_path_search(bat_priv, in_nc_node,
  						  out_nc_node, skb, eth_dst);
  		if (nc_packet)
  			break;
  	}
  	rcu_read_unlock();
5d9673109   Sven Eckelmann   batman-adv: Renam...
1355
  	batadv_orig_node_put(orig_node);
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1356
1357
1358
1359
  	return nc_packet;
  }
  
  /**
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1360
1361
1362
1363
1364
1365
1366
1367
   * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the
   *  unicast skb before it is stored for use in later decoding
   * @bat_priv: the bat priv with all the soft interface information
   * @skb: data skb to store
   * @eth_dst_new: new destination mac address of skb
   */
  static void batadv_nc_skb_store_before_coding(struct batadv_priv *bat_priv,
  					      struct sk_buff *skb,
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
1368
  					      u8 *eth_dst_new)
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1369
1370
1371
1372
  {
  	struct ethhdr *ethhdr;
  
  	/* Copy skb header to change the mac header */
bad93e9d4   Octavian Purdila   net: add __pskb_c...
1373
  	skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1374
1375
1376
1377
  	if (!skb)
  		return;
  
  	/* Set the mac header as if we actually sent the packet uncoded */
7ed4be952   Antonio Quartulli   batman-adv: use e...
1378
  	ethhdr = eth_hdr(skb);
8fdd01530   Antonio Quartulli   batman-adv: prefe...
1379
1380
  	ether_addr_copy(ethhdr->h_source, ethhdr->h_dest);
  	ether_addr_copy(ethhdr->h_dest, eth_dst_new);
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
  
  	/* Set data pointer to MAC header to mimic packets from our tx path */
  	skb_push(skb, ETH_HLEN);
  
  	/* Add the packet to the decoding packet pool */
  	batadv_nc_skb_store_for_decoding(bat_priv, skb);
  
  	/* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
  	 * our ref
  	 */
bd687fe41   Sven Eckelmann   batman-adv: use c...
1391
  	consume_skb(skb);
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1392
1393
1394
  }
  
  /**
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
   * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
   * @skb: data skb to forward
   * @neigh_node: next hop to forward packet to
   * @ethhdr: pointer to the ethernet header inside the skb
   *
   * Loops through list of neighboring nodes the next hop has a good connection to
   * (receives OGMs with a sufficient quality). We need to find a neighbor of our
   * next hop that potentially sent a packet which our next hop also received
   * (overheard) and has stored for later decoding.
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
1405
   * Return: true if the skb was consumed (encoded packet sent) or false otherwise
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
   */
  static bool batadv_nc_skb_dst_search(struct sk_buff *skb,
  				     struct batadv_neigh_node *neigh_node,
  				     struct ethhdr *ethhdr)
  {
  	struct net_device *netdev = neigh_node->if_incoming->soft_iface;
  	struct batadv_priv *bat_priv = netdev_priv(netdev);
  	struct batadv_orig_node *orig_node = neigh_node->orig_node;
  	struct batadv_nc_node *nc_node;
  	struct batadv_nc_packet *nc_packet = NULL;
  
  	rcu_read_lock();
  	list_for_each_entry_rcu(nc_node, &orig_node->in_coding_list, list) {
  		/* Search for coding opportunity with this in_nc_node */
  		nc_packet = batadv_nc_skb_src_search(bat_priv, skb,
  						     neigh_node->addr,
  						     ethhdr->h_source, nc_node);
  
  		/* Opportunity was found, so stop searching */
  		if (nc_packet)
  			break;
  	}
  	rcu_read_unlock();
  
  	if (!nc_packet)
  		return false;
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1432
1433
1434
1435
1436
  	/* Save packets for later decoding */
  	batadv_nc_skb_store_before_coding(bat_priv, skb,
  					  neigh_node->addr);
  	batadv_nc_skb_store_before_coding(bat_priv, nc_packet->skb,
  					  nc_packet->neigh_node->addr);
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
  	/* Code and send packets */
  	if (batadv_nc_code_packets(bat_priv, skb, ethhdr, nc_packet,
  				   neigh_node))
  		return true;
  
  	/* out of mem ? Coding failed - we have to free the buffered packet
  	 * to avoid memleaks. The skb passed as argument will be dealt with
  	 * by the calling function.
  	 */
  	batadv_nc_send_packet(nc_packet);
  	return false;
  }
  
  /**
953324776   Martin Hundebøll   batman-adv: netwo...
1451
1452
1453
1454
1455
1456
   * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
   * @skb: skb to add to path
   * @nc_path: path to add skb to
   * @neigh_node: next hop to forward packet to
   * @packet_id: checksum to identify packet
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
1457
   * Return: true if the packet was buffered or false in case of an error.
953324776   Martin Hundebøll   batman-adv: netwo...
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
   */
  static bool batadv_nc_skb_add_to_path(struct sk_buff *skb,
  				      struct batadv_nc_path *nc_path,
  				      struct batadv_neigh_node *neigh_node,
  				      __be32 packet_id)
  {
  	struct batadv_nc_packet *nc_packet;
  
  	nc_packet = kzalloc(sizeof(*nc_packet), GFP_ATOMIC);
  	if (!nc_packet)
  		return false;
  
  	/* Initialize nc_packet */
  	nc_packet->timestamp = jiffies;
  	nc_packet->packet_id = packet_id;
  	nc_packet->skb = skb;
  	nc_packet->neigh_node = neigh_node;
  	nc_packet->nc_path = nc_path;
  
  	/* Add coding packet to list */
  	spin_lock_bh(&nc_path->packet_list_lock);
  	list_add_tail(&nc_packet->list, &nc_path->packet_list);
  	spin_unlock_bh(&nc_path->packet_list_lock);
  
  	return true;
  }
  
  /**
   * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
   *  buffer
   * @skb: data skb to forward
   * @neigh_node: next hop to forward packet to
953324776   Martin Hundebøll   batman-adv: netwo...
1490
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
1491
   * Return: true if the skb was consumed (encoded packet sent) or false otherwise
953324776   Martin Hundebøll   batman-adv: netwo...
1492
1493
   */
  bool batadv_nc_skb_forward(struct sk_buff *skb,
e91ecfc64   Martin Hundebøll   batman-adv: Move ...
1494
  			   struct batadv_neigh_node *neigh_node)
953324776   Martin Hundebøll   batman-adv: netwo...
1495
1496
1497
1498
1499
  {
  	const struct net_device *netdev = neigh_node->if_incoming->soft_iface;
  	struct batadv_priv *bat_priv = netdev_priv(netdev);
  	struct batadv_unicast_packet *packet;
  	struct batadv_nc_path *nc_path;
e91ecfc64   Martin Hundebøll   batman-adv: Move ...
1500
  	struct ethhdr *ethhdr = eth_hdr(skb);
953324776   Martin Hundebøll   batman-adv: netwo...
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
  	__be32 packet_id;
  	u8 *payload;
  
  	/* Check if network coding is enabled */
  	if (!atomic_read(&bat_priv->network_coding))
  		goto out;
  
  	/* We only handle unicast packets */
  	payload = skb_network_header(skb);
  	packet = (struct batadv_unicast_packet *)payload;
a40d9b075   Simon Wunderlich   batman-adv: fix h...
1511
  	if (packet->packet_type != BATADV_UNICAST)
953324776   Martin Hundebøll   batman-adv: netwo...
1512
  		goto out;
3c12de9a5   Martin Hundebøll   batman-adv: netwo...
1513
1514
1515
  	/* Try to find a coding opportunity and send the skb if one is found */
  	if (batadv_nc_skb_dst_search(skb, neigh_node, ethhdr))
  		return true;
953324776   Martin Hundebøll   batman-adv: netwo...
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
  	/* Find or create a nc_path for this src-dst pair */
  	nc_path = batadv_nc_get_path(bat_priv,
  				     bat_priv->nc.coding_hash,
  				     ethhdr->h_source,
  				     neigh_node->addr);
  
  	if (!nc_path)
  		goto out;
  
  	/* Add skb to nc_path */
  	packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
  	if (!batadv_nc_skb_add_to_path(skb, nc_path, neigh_node, packet_id))
  		goto free_nc_path;
  
  	/* Packet is consumed */
  	return true;
  
  free_nc_path:
5fff28255   Sven Eckelmann   batman-adv: Renam...
1534
  	batadv_nc_path_put(nc_path);
953324776   Martin Hundebøll   batman-adv: netwo...
1535
1536
1537
1538
1539
1540
  out:
  	/* Packet is not consumed */
  	return false;
  }
  
  /**
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
   * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used
   *  when decoding coded packets
   * @bat_priv: the bat priv with all the soft interface information
   * @skb: data skb to store
   */
  void batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv,
  				      struct sk_buff *skb)
  {
  	struct batadv_unicast_packet *packet;
  	struct batadv_nc_path *nc_path;
7ed4be952   Antonio Quartulli   batman-adv: use e...
1551
  	struct ethhdr *ethhdr = eth_hdr(skb);
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
  	__be32 packet_id;
  	u8 *payload;
  
  	/* Check if network coding is enabled */
  	if (!atomic_read(&bat_priv->network_coding))
  		goto out;
  
  	/* Check for supported packet type */
  	payload = skb_network_header(skb);
  	packet = (struct batadv_unicast_packet *)payload;
a40d9b075   Simon Wunderlich   batman-adv: fix h...
1562
  	if (packet->packet_type != BATADV_UNICAST)
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
  		goto out;
  
  	/* Find existing nc_path or create a new */
  	nc_path = batadv_nc_get_path(bat_priv,
  				     bat_priv->nc.decoding_hash,
  				     ethhdr->h_source,
  				     ethhdr->h_dest);
  
  	if (!nc_path)
  		goto out;
  
  	/* Clone skb and adjust skb->data to point at batman header */
  	skb = skb_clone(skb, GFP_ATOMIC);
  	if (unlikely(!skb))
  		goto free_nc_path;
  
  	if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  		goto free_skb;
  
  	if (unlikely(!skb_pull_rcsum(skb, ETH_HLEN)))
  		goto free_skb;
  
  	/* Add skb to nc_path */
  	packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
  	if (!batadv_nc_skb_add_to_path(skb, nc_path, NULL, packet_id))
  		goto free_skb;
  
  	batadv_inc_counter(bat_priv, BATADV_CNT_NC_BUFFER);
  	return;
  
  free_skb:
  	kfree_skb(skb);
  free_nc_path:
5fff28255   Sven Eckelmann   batman-adv: Renam...
1596
  	batadv_nc_path_put(nc_path);
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
  out:
  	return;
  }
  
  /**
   * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet
   *  should be saved in the decoding buffer and, if so, store it there
   * @bat_priv: the bat priv with all the soft interface information
   * @skb: unicast skb to store
   */
  void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv,
  					 struct sk_buff *skb)
  {
7ed4be952   Antonio Quartulli   batman-adv: use e...
1610
  	struct ethhdr *ethhdr = eth_hdr(skb);
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1611

6e0895c2e   David S. Miller   Merge git://git.k...
1612
  	if (batadv_is_my_mac(bat_priv, ethhdr->h_dest))
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1613
1614
1615
1616
1617
1618
1619
1620
1621
  		return;
  
  	/* Set data pointer to MAC header to mimic packets from our tx path */
  	skb_push(skb, ETH_HLEN);
  
  	batadv_nc_skb_store_for_decoding(bat_priv, skb);
  }
  
  /**
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1622
1623
   * batadv_nc_skb_decode_packet - decode given skb using the decode data stored
   *  in nc_packet
6e0895c2e   David S. Miller   Merge git://git.k...
1624
   * @bat_priv: the bat priv with all the soft interface information
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1625
1626
1627
   * @skb: unicast skb to decode
   * @nc_packet: decode data needed to decode the skb
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
1628
   * Return: pointer to decoded unicast packet if the packet was decoded or NULL
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1629
1630
1631
   * in case of an error.
   */
  static struct batadv_unicast_packet *
6e0895c2e   David S. Miller   Merge git://git.k...
1632
  batadv_nc_skb_decode_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1633
1634
1635
1636
1637
1638
1639
  			    struct batadv_nc_packet *nc_packet)
  {
  	const int h_size = sizeof(struct batadv_unicast_packet);
  	const int h_diff = sizeof(struct batadv_coded_packet) - h_size;
  	struct batadv_unicast_packet *unicast_packet;
  	struct batadv_coded_packet coded_packet_tmp;
  	struct ethhdr *ethhdr, ethhdr_tmp;
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
1640
  	u8 *orig_dest, ttl, ttvn;
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1641
  	unsigned int coding_len;
7da19971a   Marek Lindner   batman-adv: check...
1642
  	int err;
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
  
  	/* Save headers temporarily */
  	memcpy(&coded_packet_tmp, skb->data, sizeof(coded_packet_tmp));
  	memcpy(&ethhdr_tmp, skb_mac_header(skb), sizeof(ethhdr_tmp));
  
  	if (skb_cow(skb, 0) < 0)
  		return NULL;
  
  	if (unlikely(!skb_pull_rcsum(skb, h_diff)))
  		return NULL;
  
  	/* Data points to batman header, so set mac header 14 bytes before
  	 * and network to data
  	 */
  	skb_set_mac_header(skb, -ETH_HLEN);
  	skb_reset_network_header(skb);
  
  	/* Reconstruct original mac header */
7ed4be952   Antonio Quartulli   batman-adv: use e...
1661
  	ethhdr = eth_hdr(skb);
abae9479c   Fengguang Wu   batman-adv: fix c...
1662
  	*ethhdr = ethhdr_tmp;
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1663
1664
1665
1666
  
  	/* Select the correct unicast header information based on the location
  	 * of our mac address in the coded_packet header
  	 */
6e0895c2e   David S. Miller   Merge git://git.k...
1667
  	if (batadv_is_my_mac(bat_priv, coded_packet_tmp.second_dest)) {
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1668
1669
1670
1671
  		/* If we are the second destination the packet was overheard,
  		 * so the Ethernet address must be copied to h_dest and
  		 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
  		 */
8fdd01530   Antonio Quartulli   batman-adv: prefe...
1672
  		ether_addr_copy(ethhdr->h_dest, coded_packet_tmp.second_dest);
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1673
1674
1675
1676
1677
1678
1679
  		skb->pkt_type = PACKET_HOST;
  
  		orig_dest = coded_packet_tmp.second_orig_dest;
  		ttl = coded_packet_tmp.second_ttl;
  		ttvn = coded_packet_tmp.second_ttvn;
  	} else {
  		orig_dest = coded_packet_tmp.first_orig_dest;
a40d9b075   Simon Wunderlich   batman-adv: fix h...
1680
  		ttl = coded_packet_tmp.ttl;
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
  		ttvn = coded_packet_tmp.first_ttvn;
  	}
  
  	coding_len = ntohs(coded_packet_tmp.coded_len);
  
  	if (coding_len > skb->len)
  		return NULL;
  
  	/* Here the magic is reversed:
  	 *   extract the missing packet from the received coded packet
  	 */
  	batadv_nc_memxor(skb->data + h_size,
  			 nc_packet->skb->data + h_size,
  			 coding_len);
  
  	/* Resize decoded skb if decoded with larger packet */
7da19971a   Marek Lindner   batman-adv: check...
1697
1698
1699
1700
1701
  	if (nc_packet->skb->len > coding_len + h_size) {
  		err = pskb_trim_rcsum(skb, coding_len + h_size);
  		if (err)
  			return NULL;
  	}
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1702
1703
1704
  
  	/* Create decoded unicast packet */
  	unicast_packet = (struct batadv_unicast_packet *)skb->data;
a40d9b075   Simon Wunderlich   batman-adv: fix h...
1705
1706
1707
  	unicast_packet->packet_type = BATADV_UNICAST;
  	unicast_packet->version = BATADV_COMPAT_VERSION;
  	unicast_packet->ttl = ttl;
8fdd01530   Antonio Quartulli   batman-adv: prefe...
1708
  	ether_addr_copy(unicast_packet->dest, orig_dest);
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1709
  	unicast_packet->ttvn = ttvn;
bd687fe41   Sven Eckelmann   batman-adv: use c...
1710
  	batadv_nc_packet_free(nc_packet, false);
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
  	return unicast_packet;
  }
  
  /**
   * batadv_nc_find_decoding_packet - search through buffered decoding data to
   *  find the data needed to decode the coded packet
   * @bat_priv: the bat priv with all the soft interface information
   * @ethhdr: pointer to the ethernet header inside the coded packet
   * @coded: coded packet we try to find decode data for
   *
62fe710f6   Sven Eckelmann   batman-adv: Fix k...
1721
   * Return: pointer to nc packet if the needed data was found or NULL otherwise.
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1722
1723
1724
1725
1726
1727
1728
1729
1730
   */
  static struct batadv_nc_packet *
  batadv_nc_find_decoding_packet(struct batadv_priv *bat_priv,
  			       struct ethhdr *ethhdr,
  			       struct batadv_coded_packet *coded)
  {
  	struct batadv_hashtable *hash = bat_priv->nc.decoding_hash;
  	struct batadv_nc_packet *tmp_nc_packet, *nc_packet = NULL;
  	struct batadv_nc_path *nc_path, nc_path_key;
6b5e971a2   Sven Eckelmann   batman-adv: Repla...
1731
  	u8 *dest, *source;
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1732
1733
1734
1735
1736
1737
1738
1739
  	__be32 packet_id;
  	int index;
  
  	if (!hash)
  		return NULL;
  
  	/* Select the correct packet id based on the location of our mac-addr */
  	dest = ethhdr->h_source;
6e0895c2e   David S. Miller   Merge git://git.k...
1740
  	if (!batadv_is_my_mac(bat_priv, coded->second_dest)) {
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
  		source = coded->second_source;
  		packet_id = coded->second_crc;
  	} else {
  		source = coded->first_source;
  		packet_id = coded->first_crc;
  	}
  
  	batadv_nc_hash_key_gen(&nc_path_key, source, dest);
  	index = batadv_nc_hash_choose(&nc_path_key, hash->size);
  
  	/* Search for matching coding path */
  	rcu_read_lock();
  	hlist_for_each_entry_rcu(nc_path, &hash->table[index], hash_entry) {
  		/* Find matching nc_packet */
  		spin_lock_bh(&nc_path->packet_list_lock);
  		list_for_each_entry(tmp_nc_packet,
  				    &nc_path->packet_list, list) {
  			if (packet_id == tmp_nc_packet->packet_id) {
  				list_del(&tmp_nc_packet->list);
  
  				nc_packet = tmp_nc_packet;
  				break;
  			}
  		}
  		spin_unlock_bh(&nc_path->packet_list_lock);
  
  		if (nc_packet)
  			break;
  	}
  	rcu_read_unlock();
  
  	if (!nc_packet)
  		batadv_dbg(BATADV_DBG_NC, bat_priv,
  			   "No decoding packet found for %u
  ", packet_id);
  
  	return nc_packet;
  }
  
  /**
   * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the
   *  resulting unicast packet
   * @skb: incoming coded packet
   * @recv_if: pointer to interface this packet was received on
672e79785   Sven Eckelmann   batman-adv: Fix k...
1785
1786
1787
   *
   * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
   * otherwise.
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
   */
  static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
  				       struct batadv_hard_iface *recv_if)
  {
  	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  	struct batadv_unicast_packet *unicast_packet;
  	struct batadv_coded_packet *coded_packet;
  	struct batadv_nc_packet *nc_packet;
  	struct ethhdr *ethhdr;
  	int hdr_size = sizeof(*coded_packet);
  
  	/* Check if network coding is enabled */
  	if (!atomic_read(&bat_priv->network_coding))
b91a2543b   Sven Eckelmann   batman-adv: Consu...
1801
  		goto free_skb;
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1802
1803
1804
  
  	/* Make sure we can access (and remove) header */
  	if (unlikely(!pskb_may_pull(skb, hdr_size)))
b91a2543b   Sven Eckelmann   batman-adv: Consu...
1805
  		goto free_skb;
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1806
1807
  
  	coded_packet = (struct batadv_coded_packet *)skb->data;
7ed4be952   Antonio Quartulli   batman-adv: use e...
1808
  	ethhdr = eth_hdr(skb);
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1809
1810
  
  	/* Verify frame is destined for us */
6e0895c2e   David S. Miller   Merge git://git.k...
1811
1812
  	if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest) &&
  	    !batadv_is_my_mac(bat_priv, coded_packet->second_dest))
b91a2543b   Sven Eckelmann   batman-adv: Consu...
1813
  		goto free_skb;
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1814
1815
  
  	/* Update stat counter */
6e0895c2e   David S. Miller   Merge git://git.k...
1816
  	if (batadv_is_my_mac(bat_priv, coded_packet->second_dest))
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1817
1818
1819
1820
1821
1822
  		batadv_inc_counter(bat_priv, BATADV_CNT_NC_SNIFFED);
  
  	nc_packet = batadv_nc_find_decoding_packet(bat_priv, ethhdr,
  						   coded_packet);
  	if (!nc_packet) {
  		batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
b91a2543b   Sven Eckelmann   batman-adv: Consu...
1823
  		goto free_skb;
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
  	}
  
  	/* Make skb's linear, because decoding accesses the entire buffer */
  	if (skb_linearize(skb) < 0)
  		goto free_nc_packet;
  
  	if (skb_linearize(nc_packet->skb) < 0)
  		goto free_nc_packet;
  
  	/* Decode the packet */
6e0895c2e   David S. Miller   Merge git://git.k...
1834
  	unicast_packet = batadv_nc_skb_decode_packet(bat_priv, skb, nc_packet);
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
  	if (!unicast_packet) {
  		batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
  		goto free_nc_packet;
  	}
  
  	/* Mark packet as decoded to do correct recoding when forwarding */
  	BATADV_SKB_CB(skb)->decoded = true;
  	batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE);
  	batadv_add_counter(bat_priv, BATADV_CNT_NC_DECODE_BYTES,
  			   skb->len + ETH_HLEN);
  	return batadv_recv_unicast_packet(skb, recv_if);
  
  free_nc_packet:
bd687fe41   Sven Eckelmann   batman-adv: use c...
1848
  	batadv_nc_packet_free(nc_packet, true);
b91a2543b   Sven Eckelmann   batman-adv: Consu...
1849
1850
  free_skb:
  	kfree_skb(skb);
2df5278b0   Martin Hundebøll   batman-adv: netwo...
1851
1852
1853
1854
  	return NET_RX_DROP;
  }
  
  /**
6c519bad7   Matthias Schiffer   batman-adv: set u...
1855
   * batadv_nc_mesh_free - clean up network coding memory
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
1856
1857
   * @bat_priv: the bat priv with all the soft interface information
   */
6c519bad7   Matthias Schiffer   batman-adv: set u...
1858
  void batadv_nc_mesh_free(struct batadv_priv *bat_priv)
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
1859
  {
3f4841ffb   Marek Lindner   batman-adv: tvlv ...
1860
1861
  	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
  	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_NC, 1);
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
1862
  	cancel_delayed_work_sync(&bat_priv->nc.work);
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1863

953324776   Martin Hundebøll   batman-adv: netwo...
1864
1865
  	batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
  	batadv_hash_destroy(bat_priv->nc.coding_hash);
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1866
1867
  	batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, NULL);
  	batadv_hash_destroy(bat_priv->nc.decoding_hash);
d353d8d4d   Martin Hundebøll   batman-adv: netwo...
1868
  }
d56b1705e   Martin Hundebøll   batman-adv: netwo...
1869

dc1cbd145   Sven Eckelmann   batman-adv: Allow...
1870
  #ifdef CONFIG_BATMAN_ADV_DEBUGFS
d56b1705e   Martin Hundebøll   batman-adv: netwo...
1871
1872
1873
1874
  /**
   * batadv_nc_nodes_seq_print_text - print the nc node information
   * @seq: seq file to print on
   * @offset: not used
672e79785   Sven Eckelmann   batman-adv: Fix k...
1875
1876
   *
   * Return: always 0
d56b1705e   Martin Hundebøll   batman-adv: netwo...
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
   */
  int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset)
  {
  	struct net_device *net_dev = (struct net_device *)seq->private;
  	struct batadv_priv *bat_priv = netdev_priv(net_dev);
  	struct batadv_hashtable *hash = bat_priv->orig_hash;
  	struct batadv_hard_iface *primary_if;
  	struct hlist_head *head;
  	struct batadv_orig_node *orig_node;
  	struct batadv_nc_node *nc_node;
  	int i;
  
  	primary_if = batadv_seq_print_text_primary_if_get(seq);
  	if (!primary_if)
  		goto out;
  
  	/* Traverse list of originators */
  	for (i = 0; i < hash->size; i++) {
  		head = &hash->table[i];
  
  		/* For each orig_node in this bin */
  		rcu_read_lock();
  		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
aa27c3126   Marek Lindner   batman-adv: do no...
1900
1901
1902
1903
1904
1905
  			/* no need to print the orig node if it does not have
  			 * network coding neighbors
  			 */
  			if (list_empty(&orig_node->in_coding_list) &&
  			    list_empty(&orig_node->out_coding_list))
  				continue;
d56b1705e   Martin Hundebøll   batman-adv: netwo...
1906
1907
  			seq_printf(seq, "Node:      %pM
  ", orig_node->orig);
0c8146535   Antonio Quartulli   batman-adv: use s...
1908
  			seq_puts(seq, " Ingoing:  ");
d56b1705e   Martin Hundebøll   batman-adv: netwo...
1909
1910
1911
1912
1913
1914
  			/* For each in_nc_node to this orig_node */
  			list_for_each_entry_rcu(nc_node,
  						&orig_node->in_coding_list,
  						list)
  				seq_printf(seq, "%pM ",
  					   nc_node->addr);
912eeed9f   Markus Elfring   batman-adv: Combi...
1915
1916
  			seq_puts(seq, "
   Outgoing: ");
d56b1705e   Martin Hundebøll   batman-adv: netwo...
1917
1918
1919
1920
1921
1922
  			/* For out_nc_node to this orig_node */
  			list_for_each_entry_rcu(nc_node,
  						&orig_node->out_coding_list,
  						list)
  				seq_printf(seq, "%pM ",
  					   nc_node->addr);
0c8146535   Antonio Quartulli   batman-adv: use s...
1923
1924
1925
  			seq_puts(seq, "
  
  ");
d56b1705e   Martin Hundebøll   batman-adv: netwo...
1926
1927
1928
1929
1930
1931
  		}
  		rcu_read_unlock();
  	}
  
  out:
  	if (primary_if)
82047ad7f   Sven Eckelmann   batman-adv: Renam...
1932
  		batadv_hardif_put(primary_if);
d56b1705e   Martin Hundebøll   batman-adv: netwo...
1933
1934
1935
1936
1937
1938
  	return 0;
  }
  
  /**
   * batadv_nc_init_debugfs - create nc folder and related files in debugfs
   * @bat_priv: the bat priv with all the soft interface information
672e79785   Sven Eckelmann   batman-adv: Fix k...
1939
1940
   *
   * Return: 0 on success or negative error number in case of failure
d56b1705e   Martin Hundebøll   batman-adv: netwo...
1941
1942
1943
1944
1945
1946
1947
1948
   */
  int batadv_nc_init_debugfs(struct batadv_priv *bat_priv)
  {
  	struct dentry *nc_dir, *file;
  
  	nc_dir = debugfs_create_dir("nc", bat_priv->debug_dir);
  	if (!nc_dir)
  		goto out;
507b37cf7   Sven Eckelmann   batman-adv: Use o...
1949
  	file = debugfs_create_u8("min_tq", 0644, nc_dir, &bat_priv->nc.min_tq);
d56b1705e   Martin Hundebøll   batman-adv: netwo...
1950
1951
  	if (!file)
  		goto out;
507b37cf7   Sven Eckelmann   batman-adv: Use o...
1952
  	file = debugfs_create_u32("max_fwd_delay", 0644, nc_dir,
953324776   Martin Hundebøll   batman-adv: netwo...
1953
1954
1955
  				  &bat_priv->nc.max_fwd_delay);
  	if (!file)
  		goto out;
507b37cf7   Sven Eckelmann   batman-adv: Use o...
1956
  	file = debugfs_create_u32("max_buffer_time", 0644, nc_dir,
612d2b4fe   Martin Hundebøll   batman-adv: netwo...
1957
1958
1959
  				  &bat_priv->nc.max_buffer_time);
  	if (!file)
  		goto out;
d56b1705e   Martin Hundebøll   batman-adv: netwo...
1960
1961
1962
1963
1964
  	return 0;
  
  out:
  	return -ENOMEM;
  }
dc1cbd145   Sven Eckelmann   batman-adv: Allow...
1965
  #endif