82
static void softif_neigh_free_ref(struct kref *refcount)
84
struct softif_neigh *softif_neigh;
86
softif_neigh = container_of(refcount, struct softif_neigh, refcount);
90
static void softif_neigh_free_rcu(struct rcu_head *rcu)
92
struct softif_neigh *softif_neigh;
94
softif_neigh = container_of(rcu, struct softif_neigh, rcu);
95
kref_put(&softif_neigh->refcount, softif_neigh_free_ref);
98
void softif_neigh_purge(struct bat_priv *bat_priv)
100
struct softif_neigh *softif_neigh, *softif_neigh_tmp;
75
static void softif_neigh_free_ref(struct softif_neigh *softif_neigh)
77
if (atomic_dec_and_test(&softif_neigh->refcount))
78
kfree_rcu(softif_neigh, rcu);
81
static void softif_neigh_vid_free_rcu(struct rcu_head *rcu)
83
struct softif_neigh_vid *softif_neigh_vid;
84
struct softif_neigh *softif_neigh;
101
85
struct hlist_node *node, *node_tmp;
86
struct bat_priv *bat_priv;
88
softif_neigh_vid = container_of(rcu, struct softif_neigh_vid, rcu);
89
bat_priv = softif_neigh_vid->bat_priv;
103
91
spin_lock_bh(&bat_priv->softif_neigh_lock);
105
92
hlist_for_each_entry_safe(softif_neigh, node, node_tmp,
106
&bat_priv->softif_neigh_list, list) {
108
if ((!time_after(jiffies, softif_neigh->last_seen +
109
msecs_to_jiffies(SOFTIF_NEIGH_TIMEOUT))) &&
110
(atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE))
93
&softif_neigh_vid->softif_neigh_list, list) {
113
94
hlist_del_rcu(&softif_neigh->list);
115
if (bat_priv->softif_neigh == softif_neigh) {
116
bat_dbg(DBG_ROUTES, bat_priv,
117
"Current mesh exit point '%pM' vanished "
119
softif_neigh->addr, softif_neigh->vid);
120
softif_neigh_tmp = bat_priv->softif_neigh;
121
bat_priv->softif_neigh = NULL;
122
kref_put(&softif_neigh_tmp->refcount,
123
softif_neigh_free_ref);
126
call_rcu(&softif_neigh->rcu, softif_neigh_free_rcu);
95
softif_neigh_free_ref(softif_neigh);
129
97
spin_unlock_bh(&bat_priv->softif_neigh_lock);
99
kfree(softif_neigh_vid);
102
static void softif_neigh_vid_free_ref(struct softif_neigh_vid *softif_neigh_vid)
104
if (atomic_dec_and_test(&softif_neigh_vid->refcount))
105
call_rcu(&softif_neigh_vid->rcu, softif_neigh_vid_free_rcu);
108
static struct softif_neigh_vid *softif_neigh_vid_get(struct bat_priv *bat_priv,
111
struct softif_neigh_vid *softif_neigh_vid;
112
struct hlist_node *node;
115
hlist_for_each_entry_rcu(softif_neigh_vid, node,
116
&bat_priv->softif_neigh_vids, list) {
117
if (softif_neigh_vid->vid != vid)
120
if (!atomic_inc_not_zero(&softif_neigh_vid->refcount))
126
softif_neigh_vid = kzalloc(sizeof(struct softif_neigh_vid),
128
if (!softif_neigh_vid)
131
softif_neigh_vid->vid = vid;
132
softif_neigh_vid->bat_priv = bat_priv;
134
/* initialize with 2 - caller decrements counter by one */
135
atomic_set(&softif_neigh_vid->refcount, 2);
136
INIT_HLIST_HEAD(&softif_neigh_vid->softif_neigh_list);
137
INIT_HLIST_NODE(&softif_neigh_vid->list);
138
spin_lock_bh(&bat_priv->softif_neigh_vid_lock);
139
hlist_add_head_rcu(&softif_neigh_vid->list,
140
&bat_priv->softif_neigh_vids);
141
spin_unlock_bh(&bat_priv->softif_neigh_vid_lock);
145
return softif_neigh_vid;
132
148
static struct softif_neigh *softif_neigh_get(struct bat_priv *bat_priv,
133
149
uint8_t *addr, short vid)
135
struct softif_neigh *softif_neigh;
151
struct softif_neigh_vid *softif_neigh_vid;
152
struct softif_neigh *softif_neigh = NULL;
136
153
struct hlist_node *node;
155
softif_neigh_vid = softif_neigh_vid_get(bat_priv, vid);
156
if (!softif_neigh_vid)
139
160
hlist_for_each_entry_rcu(softif_neigh, node,
140
&bat_priv->softif_neigh_list, list) {
141
if (memcmp(softif_neigh->addr, addr, ETH_ALEN) != 0)
161
&softif_neigh_vid->softif_neigh_list,
163
if (!compare_eth(softif_neigh->addr, addr))
144
if (softif_neigh->vid != vid)
166
if (!atomic_inc_not_zero(&softif_neigh->refcount))
147
169
softif_neigh->last_seen = jiffies;
151
173
softif_neigh = kzalloc(sizeof(struct softif_neigh), GFP_ATOMIC);
152
174
if (!softif_neigh)
155
177
memcpy(softif_neigh->addr, addr, ETH_ALEN);
156
softif_neigh->vid = vid;
157
178
softif_neigh->last_seen = jiffies;
158
kref_init(&softif_neigh->refcount);
179
/* initialize with 2 - caller decrements counter by one */
180
atomic_set(&softif_neigh->refcount, 2);
160
182
INIT_HLIST_NODE(&softif_neigh->list);
161
183
spin_lock_bh(&bat_priv->softif_neigh_lock);
162
hlist_add_head_rcu(&softif_neigh->list, &bat_priv->softif_neigh_list);
163
spin_unlock_bh(&bat_priv->softif_neigh_lock);
166
kref_get(&softif_neigh->refcount);
184
hlist_add_head_rcu(&softif_neigh->list,
185
&softif_neigh_vid->softif_neigh_list);
186
spin_unlock_bh(&bat_priv->softif_neigh_lock);
191
if (softif_neigh_vid)
192
softif_neigh_vid_free_ref(softif_neigh_vid);
196
static struct softif_neigh *softif_neigh_get_selected(
197
struct softif_neigh_vid *softif_neigh_vid)
199
struct softif_neigh *softif_neigh;
202
softif_neigh = rcu_dereference(softif_neigh_vid->softif_neigh);
204
if (softif_neigh && !atomic_inc_not_zero(&softif_neigh->refcount))
211
static struct softif_neigh *softif_neigh_vid_get_selected(
212
struct bat_priv *bat_priv,
215
struct softif_neigh_vid *softif_neigh_vid;
216
struct softif_neigh *softif_neigh = NULL;
218
softif_neigh_vid = softif_neigh_vid_get(bat_priv, vid);
219
if (!softif_neigh_vid)
222
softif_neigh = softif_neigh_get_selected(softif_neigh_vid);
224
if (softif_neigh_vid)
225
softif_neigh_vid_free_ref(softif_neigh_vid);
229
static void softif_neigh_vid_select(struct bat_priv *bat_priv,
230
struct softif_neigh *new_neigh,
233
struct softif_neigh_vid *softif_neigh_vid;
234
struct softif_neigh *curr_neigh;
236
softif_neigh_vid = softif_neigh_vid_get(bat_priv, vid);
237
if (!softif_neigh_vid)
240
spin_lock_bh(&bat_priv->softif_neigh_lock);
242
if (new_neigh && !atomic_inc_not_zero(&new_neigh->refcount))
245
curr_neigh = softif_neigh_vid->softif_neigh;
246
rcu_assign_pointer(softif_neigh_vid->softif_neigh, new_neigh);
248
if ((curr_neigh) && (!new_neigh))
249
bat_dbg(DBG_ROUTES, bat_priv,
250
"Removing mesh exit point on vid: %d (prev: %pM).\n",
251
vid, curr_neigh->addr);
252
else if ((curr_neigh) && (new_neigh))
253
bat_dbg(DBG_ROUTES, bat_priv,
254
"Changing mesh exit point on vid: %d from %pM "
255
"to %pM.\n", vid, curr_neigh->addr, new_neigh->addr);
256
else if ((!curr_neigh) && (new_neigh))
257
bat_dbg(DBG_ROUTES, bat_priv,
258
"Setting mesh exit point on vid: %d to %pM.\n",
259
vid, new_neigh->addr);
262
softif_neigh_free_ref(curr_neigh);
264
spin_unlock_bh(&bat_priv->softif_neigh_lock);
267
if (softif_neigh_vid)
268
softif_neigh_vid_free_ref(softif_neigh_vid);
271
static void softif_neigh_vid_deselect(struct bat_priv *bat_priv,
272
struct softif_neigh_vid *softif_neigh_vid)
274
struct softif_neigh *curr_neigh;
275
struct softif_neigh *softif_neigh = NULL, *softif_neigh_tmp;
276
struct hard_iface *primary_if = NULL;
277
struct hlist_node *node;
279
primary_if = primary_if_get_selected(bat_priv);
283
/* find new softif_neigh immediately to avoid temporary loops */
285
curr_neigh = rcu_dereference(softif_neigh_vid->softif_neigh);
287
hlist_for_each_entry_rcu(softif_neigh_tmp, node,
288
&softif_neigh_vid->softif_neigh_list,
290
if (softif_neigh_tmp == curr_neigh)
293
/* we got a neighbor but its mac is 'bigger' than ours */
294
if (memcmp(primary_if->net_dev->dev_addr,
295
softif_neigh_tmp->addr, ETH_ALEN) < 0)
298
if (!atomic_inc_not_zero(&softif_neigh_tmp->refcount))
301
softif_neigh = softif_neigh_tmp;
308
softif_neigh_vid_select(bat_priv, softif_neigh, softif_neigh_vid->vid);
311
hardif_free_ref(primary_if);
313
softif_neigh_free_ref(softif_neigh);
172
316
int softif_neigh_seq_print_text(struct seq_file *seq, void *offset)
174
318
struct net_device *net_dev = (struct net_device *)seq->private;
175
319
struct bat_priv *bat_priv = netdev_priv(net_dev);
320
struct softif_neigh_vid *softif_neigh_vid;
176
321
struct softif_neigh *softif_neigh;
177
struct hlist_node *node;
178
size_t buf_size, pos;
181
if (!bat_priv->primary_if) {
182
return seq_printf(seq, "BATMAN mesh %s disabled - "
183
"please specify interfaces to enable it\n",
322
struct hard_iface *primary_if;
323
struct hlist_node *node, *node_tmp;
324
struct softif_neigh *curr_softif_neigh;
325
int ret = 0, last_seen_secs, last_seen_msecs;
327
primary_if = primary_if_get_selected(bat_priv);
329
ret = seq_printf(seq, "BATMAN mesh %s disabled - "
330
"please specify interfaces to enable it\n",
335
if (primary_if->if_status != IF_ACTIVE) {
336
ret = seq_printf(seq, "BATMAN mesh %s "
337
"disabled - primary interface not active\n",
187
342
seq_printf(seq, "Softif neighbor list (%s)\n", net_dev->name);
190
/* Estimate length for: " xx:xx:xx:xx:xx:xx\n" */
192
hlist_for_each_entry_rcu(softif_neigh, node,
193
&bat_priv->softif_neigh_list, list)
197
buff = kmalloc(buf_size, GFP_ATOMIC);
205
hlist_for_each_entry_rcu(softif_neigh, node,
206
&bat_priv->softif_neigh_list, list) {
207
pos += snprintf(buff + pos, 31, "%s %pM (vid: %d)\n",
208
bat_priv->softif_neigh == softif_neigh
209
? "=>" : " ", softif_neigh->addr,
214
seq_printf(seq, "%s", buff);
345
hlist_for_each_entry_rcu(softif_neigh_vid, node,
346
&bat_priv->softif_neigh_vids, list) {
347
seq_printf(seq, " %-15s %s on vid: %d\n",
348
"Originator", "last-seen", softif_neigh_vid->vid);
350
curr_softif_neigh = softif_neigh_get_selected(softif_neigh_vid);
352
hlist_for_each_entry_rcu(softif_neigh, node_tmp,
353
&softif_neigh_vid->softif_neigh_list,
355
last_seen_secs = jiffies_to_msecs(jiffies -
356
softif_neigh->last_seen) / 1000;
357
last_seen_msecs = jiffies_to_msecs(jiffies -
358
softif_neigh->last_seen) % 1000;
359
seq_printf(seq, "%s %pM %3i.%03is\n",
360
curr_softif_neigh == softif_neigh
361
? "=>" : " ", softif_neigh->addr,
362
last_seen_secs, last_seen_msecs);
365
if (curr_softif_neigh)
366
softif_neigh_free_ref(curr_softif_neigh);
368
seq_printf(seq, "\n");
374
hardif_free_ref(primary_if);
378
void softif_neigh_purge(struct bat_priv *bat_priv)
380
struct softif_neigh *softif_neigh, *curr_softif_neigh;
381
struct softif_neigh_vid *softif_neigh_vid;
382
struct hlist_node *node, *node_tmp, *node_tmp2;
386
hlist_for_each_entry_rcu(softif_neigh_vid, node,
387
&bat_priv->softif_neigh_vids, list) {
388
if (!atomic_inc_not_zero(&softif_neigh_vid->refcount))
391
curr_softif_neigh = softif_neigh_get_selected(softif_neigh_vid);
394
spin_lock_bh(&bat_priv->softif_neigh_lock);
395
hlist_for_each_entry_safe(softif_neigh, node_tmp, node_tmp2,
396
&softif_neigh_vid->softif_neigh_list,
398
if ((!time_after(jiffies, softif_neigh->last_seen +
399
msecs_to_jiffies(SOFTIF_NEIGH_TIMEOUT))) &&
400
(atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE))
403
if (curr_softif_neigh == softif_neigh) {
404
bat_dbg(DBG_ROUTES, bat_priv,
405
"Current mesh exit point on vid: %d "
407
softif_neigh_vid->vid,
412
hlist_del_rcu(&softif_neigh->list);
413
softif_neigh_free_ref(softif_neigh);
415
spin_unlock_bh(&bat_priv->softif_neigh_lock);
417
/* soft_neigh_vid_deselect() needs to acquire the
418
* softif_neigh_lock */
420
softif_neigh_vid_deselect(bat_priv, softif_neigh_vid);
422
if (curr_softif_neigh)
423
softif_neigh_free_ref(curr_softif_neigh);
425
softif_neigh_vid_free_ref(softif_neigh_vid);
429
spin_lock_bh(&bat_priv->softif_neigh_vid_lock);
430
hlist_for_each_entry_safe(softif_neigh_vid, node, node_tmp,
431
&bat_priv->softif_neigh_vids, list) {
432
if (!hlist_empty(&softif_neigh_vid->softif_neigh_list))
435
hlist_del_rcu(&softif_neigh_vid->list);
436
softif_neigh_vid_free_ref(softif_neigh_vid);
438
spin_unlock_bh(&bat_priv->softif_neigh_vid_lock);
219
442
static void softif_batman_recv(struct sk_buff *skb, struct net_device *dev,
231
456
batman_packet = (struct batman_packet *)(skb->data + ETH_HLEN);
233
458
if (batman_packet->version != COMPAT_VERSION)
236
461
if (batman_packet->packet_type != BAT_PACKET)
239
464
if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
242
467
if (is_my_mac(batman_packet->orig))
245
470
softif_neigh = softif_neigh_get(bat_priv, batman_packet->orig, vid);
247
471
if (!softif_neigh)
250
if (bat_priv->softif_neigh == softif_neigh)
474
curr_softif_neigh = softif_neigh_vid_get_selected(bat_priv, vid);
475
if (curr_softif_neigh == softif_neigh)
478
primary_if = primary_if_get_selected(bat_priv);
253
482
/* we got a neighbor but its mac is 'bigger' than ours */
254
if (memcmp(bat_priv->primary_if->net_dev->dev_addr,
483
if (memcmp(primary_if->net_dev->dev_addr,
255
484
softif_neigh->addr, ETH_ALEN) < 0)
487
/* close own batX device and use softif_neigh as exit node */
488
if (!curr_softif_neigh) {
489
softif_neigh_vid_select(bat_priv, softif_neigh, vid);
258
493
/* switch to new 'smallest neighbor' */
259
if ((bat_priv->softif_neigh) &&
260
(memcmp(softif_neigh->addr, bat_priv->softif_neigh->addr,
262
bat_dbg(DBG_ROUTES, bat_priv,
263
"Changing mesh exit point from %pM (vid: %d) "
264
"to %pM (vid: %d).\n",
265
bat_priv->softif_neigh->addr,
266
bat_priv->softif_neigh->vid,
267
softif_neigh->addr, softif_neigh->vid);
268
softif_neigh_tmp = bat_priv->softif_neigh;
269
bat_priv->softif_neigh = softif_neigh;
270
kref_put(&softif_neigh_tmp->refcount, softif_neigh_free_ref);
271
/* we need to hold the additional reference */
275
/* close own batX device and use softif_neigh as exit node */
276
if ((!bat_priv->softif_neigh) &&
277
(memcmp(softif_neigh->addr,
278
bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN) < 0)) {
279
bat_dbg(DBG_ROUTES, bat_priv,
280
"Setting mesh exit point to %pM (vid: %d).\n",
281
softif_neigh->addr, softif_neigh->vid);
282
bat_priv->softif_neigh = softif_neigh;
283
/* we need to hold the additional reference */
494
if (memcmp(softif_neigh->addr, curr_softif_neigh->addr, ETH_ALEN) < 0)
495
softif_neigh_vid_select(bat_priv, softif_neigh, vid);
288
kref_put(&softif_neigh->refcount, softif_neigh_free_ref);
500
softif_neigh_free_ref(softif_neigh);
501
if (curr_softif_neigh)
502
softif_neigh_free_ref(curr_softif_neigh);
504
hardif_free_ref(primary_if);