~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to net/batman-adv/hard-interface.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
 
2
 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
3
3
 *
4
4
 * Marek Lindner, Simon Wunderlich
5
5
 *
31
31
 
32
32
#include <linux/if_arp.h>
33
33
 
34
 
/* protect update critical side of if_list - but not the content */
35
 
static DEFINE_SPINLOCK(if_list_lock);
36
 
 
37
 
static void hardif_free_rcu(struct rcu_head *rcu)
 
34
 
 
35
static int batman_skb_recv(struct sk_buff *skb,
 
36
                           struct net_device *dev,
 
37
                           struct packet_type *ptype,
 
38
                           struct net_device *orig_dev);
 
39
 
 
40
void hardif_free_rcu(struct rcu_head *rcu)
38
41
{
39
 
        struct batman_if *batman_if;
 
42
        struct hard_iface *hard_iface;
40
43
 
41
 
        batman_if = container_of(rcu, struct batman_if, rcu);
42
 
        dev_put(batman_if->net_dev);
43
 
        kref_put(&batman_if->refcount, hardif_free_ref);
 
44
        hard_iface = container_of(rcu, struct hard_iface, rcu);
 
45
        dev_put(hard_iface->net_dev);
 
46
        kfree(hard_iface);
44
47
}
45
48
 
46
 
struct batman_if *get_batman_if_by_netdev(struct net_device *net_dev)
 
49
struct hard_iface *hardif_get_by_netdev(struct net_device *net_dev)
47
50
{
48
 
        struct batman_if *batman_if;
 
51
        struct hard_iface *hard_iface;
49
52
 
50
53
        rcu_read_lock();
51
 
        list_for_each_entry_rcu(batman_if, &if_list, list) {
52
 
                if (batman_if->net_dev == net_dev)
 
54
        list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
 
55
                if (hard_iface->net_dev == net_dev &&
 
56
                    atomic_inc_not_zero(&hard_iface->refcount))
53
57
                        goto out;
54
58
        }
55
59
 
56
 
        batman_if = NULL;
 
60
        hard_iface = NULL;
57
61
 
58
62
out:
59
 
        if (batman_if)
60
 
                kref_get(&batman_if->refcount);
61
 
 
62
63
        rcu_read_unlock();
63
 
        return batman_if;
 
64
        return hard_iface;
64
65
}
65
66
 
66
67
static int is_valid_iface(struct net_device *net_dev)
75
76
                return 0;
76
77
 
77
78
        /* no batman over batman */
78
 
#ifdef HAVE_NET_DEVICE_OPS
79
 
        if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
80
 
                return 0;
81
 
#else
82
 
        if (net_dev->hard_start_xmit == interface_tx)
83
 
                return 0;
84
 
#endif
 
79
        if (softif_is_valid(net_dev))
 
80
                return 0;
85
81
 
86
82
        /* Device is being bridged */
87
83
        /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
90
86
        return 1;
91
87
}
92
88
 
93
 
static struct batman_if *get_active_batman_if(struct net_device *soft_iface)
 
89
static struct hard_iface *hardif_get_active(struct net_device *soft_iface)
94
90
{
95
 
        struct batman_if *batman_if;
 
91
        struct hard_iface *hard_iface;
96
92
 
97
93
        rcu_read_lock();
98
 
        list_for_each_entry_rcu(batman_if, &if_list, list) {
99
 
                if (batman_if->soft_iface != soft_iface)
 
94
        list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
 
95
                if (hard_iface->soft_iface != soft_iface)
100
96
                        continue;
101
97
 
102
 
                if (batman_if->if_status == IF_ACTIVE)
 
98
                if (hard_iface->if_status == IF_ACTIVE &&
 
99
                    atomic_inc_not_zero(&hard_iface->refcount))
103
100
                        goto out;
104
101
        }
105
102
 
106
 
        batman_if = NULL;
 
103
        hard_iface = NULL;
107
104
 
108
105
out:
109
 
        if (batman_if)
110
 
                kref_get(&batman_if->refcount);
111
 
 
112
106
        rcu_read_unlock();
113
 
        return batman_if;
 
107
        return hard_iface;
114
108
}
115
109
 
116
 
static void update_primary_addr(struct bat_priv *bat_priv)
 
110
static void primary_if_update_addr(struct bat_priv *bat_priv)
117
111
{
118
112
        struct vis_packet *vis_packet;
 
113
        struct hard_iface *primary_if;
 
114
 
 
115
        primary_if = primary_if_get_selected(bat_priv);
 
116
        if (!primary_if)
 
117
                goto out;
119
118
 
120
119
        vis_packet = (struct vis_packet *)
121
120
                                bat_priv->my_vis_info->skb_packet->data;
122
 
        memcpy(vis_packet->vis_orig,
123
 
               bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
 
121
        memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
124
122
        memcpy(vis_packet->sender_orig,
125
 
               bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
 
123
               primary_if->net_dev->dev_addr, ETH_ALEN);
 
124
 
 
125
out:
 
126
        if (primary_if)
 
127
                hardif_free_ref(primary_if);
126
128
}
127
129
 
128
 
static void set_primary_if(struct bat_priv *bat_priv,
129
 
                           struct batman_if *batman_if)
 
130
static void primary_if_select(struct bat_priv *bat_priv,
 
131
                              struct hard_iface *new_hard_iface)
130
132
{
 
133
        struct hard_iface *curr_hard_iface;
131
134
        struct batman_packet *batman_packet;
132
 
        struct batman_if *old_if;
133
 
 
134
 
        if (batman_if)
135
 
                kref_get(&batman_if->refcount);
136
 
 
137
 
        old_if = bat_priv->primary_if;
138
 
        bat_priv->primary_if = batman_if;
139
 
 
140
 
        if (old_if)
141
 
                kref_put(&old_if->refcount, hardif_free_ref);
142
 
 
143
 
        if (!bat_priv->primary_if)
 
135
 
 
136
        ASSERT_RTNL();
 
137
 
 
138
        if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
 
139
                new_hard_iface = NULL;
 
140
 
 
141
        curr_hard_iface = bat_priv->primary_if;
 
142
        rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
 
143
 
 
144
        if (curr_hard_iface)
 
145
                hardif_free_ref(curr_hard_iface);
 
146
 
 
147
        if (!new_hard_iface)
144
148
                return;
145
149
 
146
 
        batman_packet = (struct batman_packet *)(batman_if->packet_buff);
 
150
        batman_packet = (struct batman_packet *)(new_hard_iface->packet_buff);
147
151
        batman_packet->flags = PRIMARIES_FIRST_HOP;
148
152
        batman_packet->ttl = TTL;
149
153
 
150
 
        update_primary_addr(bat_priv);
 
154
        primary_if_update_addr(bat_priv);
151
155
 
152
156
        /***
153
 
         * hacky trick to make sure that we send the HNA information via
 
157
         * hacky trick to make sure that we send the TT information via
154
158
         * our new primary interface
155
159
         */
156
 
        atomic_set(&bat_priv->hna_local_changed, 1);
 
160
        atomic_set(&bat_priv->tt_local_changed, 1);
157
161
}
158
162
 
159
 
static bool hardif_is_iface_up(struct batman_if *batman_if)
 
163
static bool hardif_is_iface_up(struct hard_iface *hard_iface)
160
164
{
161
 
        if (batman_if->net_dev->flags & IFF_UP)
 
165
        if (hard_iface->net_dev->flags & IFF_UP)
162
166
                return true;
163
167
 
164
168
        return false;
165
169
}
166
170
 
167
 
static void update_mac_addresses(struct batman_if *batman_if)
 
171
static void update_mac_addresses(struct hard_iface *hard_iface)
168
172
{
169
 
        memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
170
 
               batman_if->net_dev->dev_addr, ETH_ALEN);
171
 
        memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
172
 
               batman_if->net_dev->dev_addr, ETH_ALEN);
 
173
        memcpy(((struct batman_packet *)(hard_iface->packet_buff))->orig,
 
174
               hard_iface->net_dev->dev_addr, ETH_ALEN);
 
175
        memcpy(((struct batman_packet *)(hard_iface->packet_buff))->prev_sender,
 
176
               hard_iface->net_dev->dev_addr, ETH_ALEN);
173
177
}
174
178
 
175
179
static void check_known_mac_addr(struct net_device *net_dev)
176
180
{
177
 
        struct batman_if *batman_if;
 
181
        struct hard_iface *hard_iface;
178
182
 
179
183
        rcu_read_lock();
180
 
        list_for_each_entry_rcu(batman_if, &if_list, list) {
181
 
                if ((batman_if->if_status != IF_ACTIVE) &&
182
 
                    (batman_if->if_status != IF_TO_BE_ACTIVATED))
183
 
                        continue;
184
 
 
185
 
                if (batman_if->net_dev == net_dev)
186
 
                        continue;
187
 
 
188
 
                if (!compare_orig(batman_if->net_dev->dev_addr,
189
 
                                  net_dev->dev_addr))
 
184
        list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
 
185
                if ((hard_iface->if_status != IF_ACTIVE) &&
 
186
                    (hard_iface->if_status != IF_TO_BE_ACTIVATED))
 
187
                        continue;
 
188
 
 
189
                if (hard_iface->net_dev == net_dev)
 
190
                        continue;
 
191
 
 
192
                if (!compare_eth(hard_iface->net_dev->dev_addr,
 
193
                                 net_dev->dev_addr))
190
194
                        continue;
191
195
 
192
196
                pr_warning("The newly added mac address (%pM) already exists "
193
197
                           "on: %s\n", net_dev->dev_addr,
194
 
                           batman_if->net_dev->name);
 
198
                           hard_iface->net_dev->name);
195
199
                pr_warning("It is strongly recommended to keep mac addresses "
196
200
                           "unique to avoid problems!\n");
197
201
        }
201
205
int hardif_min_mtu(struct net_device *soft_iface)
202
206
{
203
207
        struct bat_priv *bat_priv = netdev_priv(soft_iface);
204
 
        struct batman_if *batman_if;
 
208
        struct hard_iface *hard_iface;
205
209
        /* allow big frames if all devices are capable to do so
206
210
         * (have MTU > 1500 + BAT_HEADER_LEN) */
207
211
        int min_mtu = ETH_DATA_LEN;
210
214
                goto out;
211
215
 
212
216
        rcu_read_lock();
213
 
        list_for_each_entry_rcu(batman_if, &if_list, list) {
214
 
                if ((batman_if->if_status != IF_ACTIVE) &&
215
 
                    (batman_if->if_status != IF_TO_BE_ACTIVATED))
216
 
                        continue;
217
 
 
218
 
                if (batman_if->soft_iface != soft_iface)
219
 
                        continue;
220
 
 
221
 
                min_mtu = min_t(int, batman_if->net_dev->mtu - BAT_HEADER_LEN,
 
217
        list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
 
218
                if ((hard_iface->if_status != IF_ACTIVE) &&
 
219
                    (hard_iface->if_status != IF_TO_BE_ACTIVATED))
 
220
                        continue;
 
221
 
 
222
                if (hard_iface->soft_iface != soft_iface)
 
223
                        continue;
 
224
 
 
225
                min_mtu = min_t(int, hard_iface->net_dev->mtu - BAT_HEADER_LEN,
222
226
                                min_mtu);
223
227
        }
224
228
        rcu_read_unlock();
236
240
                soft_iface->mtu = min_mtu;
237
241
}
238
242
 
239
 
static void hardif_activate_interface(struct batman_if *batman_if)
 
243
static void hardif_activate_interface(struct hard_iface *hard_iface)
240
244
{
241
245
        struct bat_priv *bat_priv;
242
 
 
243
 
        if (batman_if->if_status != IF_INACTIVE)
244
 
                return;
245
 
 
246
 
        bat_priv = netdev_priv(batman_if->soft_iface);
247
 
 
248
 
        update_mac_addresses(batman_if);
249
 
        batman_if->if_status = IF_TO_BE_ACTIVATED;
 
246
        struct hard_iface *primary_if = NULL;
 
247
 
 
248
        if (hard_iface->if_status != IF_INACTIVE)
 
249
                goto out;
 
250
 
 
251
        bat_priv = netdev_priv(hard_iface->soft_iface);
 
252
 
 
253
        update_mac_addresses(hard_iface);
 
254
        hard_iface->if_status = IF_TO_BE_ACTIVATED;
250
255
 
251
256
        /**
252
257
         * the first active interface becomes our primary interface or
253
258
         * the next active interface after the old primay interface was removed
254
259
         */
255
 
        if (!bat_priv->primary_if)
256
 
                set_primary_if(bat_priv, batman_if);
257
 
 
258
 
        bat_info(batman_if->soft_iface, "Interface activated: %s\n",
259
 
                 batman_if->net_dev->name);
260
 
 
261
 
        update_min_mtu(batman_if->soft_iface);
262
 
        return;
 
260
        primary_if = primary_if_get_selected(bat_priv);
 
261
        if (!primary_if)
 
262
                primary_if_select(bat_priv, hard_iface);
 
263
 
 
264
        bat_info(hard_iface->soft_iface, "Interface activated: %s\n",
 
265
                 hard_iface->net_dev->name);
 
266
 
 
267
        update_min_mtu(hard_iface->soft_iface);
 
268
 
 
269
out:
 
270
        if (primary_if)
 
271
                hardif_free_ref(primary_if);
263
272
}
264
273
 
265
 
static void hardif_deactivate_interface(struct batman_if *batman_if)
 
274
static void hardif_deactivate_interface(struct hard_iface *hard_iface)
266
275
{
267
 
        if ((batman_if->if_status != IF_ACTIVE) &&
268
 
           (batman_if->if_status != IF_TO_BE_ACTIVATED))
 
276
        if ((hard_iface->if_status != IF_ACTIVE) &&
 
277
            (hard_iface->if_status != IF_TO_BE_ACTIVATED))
269
278
                return;
270
279
 
271
 
        batman_if->if_status = IF_INACTIVE;
272
 
 
273
 
        bat_info(batman_if->soft_iface, "Interface deactivated: %s\n",
274
 
                 batman_if->net_dev->name);
275
 
 
276
 
        update_min_mtu(batman_if->soft_iface);
 
280
        hard_iface->if_status = IF_INACTIVE;
 
281
 
 
282
        bat_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
 
283
                 hard_iface->net_dev->name);
 
284
 
 
285
        update_min_mtu(hard_iface->soft_iface);
277
286
}
278
287
 
279
 
int hardif_enable_interface(struct batman_if *batman_if, char *iface_name)
 
288
int hardif_enable_interface(struct hard_iface *hard_iface, char *iface_name)
280
289
{
281
290
        struct bat_priv *bat_priv;
282
291
        struct batman_packet *batman_packet;
283
 
 
284
 
        if (batman_if->if_status != IF_NOT_IN_USE)
285
 
                goto out;
286
 
 
287
 
        batman_if->soft_iface = dev_get_by_name(&init_net, iface_name);
288
 
 
289
 
        if (!batman_if->soft_iface) {
290
 
                batman_if->soft_iface = softif_create(iface_name);
291
 
 
292
 
                if (!batman_if->soft_iface)
 
292
        struct net_device *soft_iface;
 
293
        int ret;
 
294
 
 
295
        if (hard_iface->if_status != IF_NOT_IN_USE)
 
296
                goto out;
 
297
 
 
298
        if (!atomic_inc_not_zero(&hard_iface->refcount))
 
299
                goto out;
 
300
 
 
301
        soft_iface = dev_get_by_name(&init_net, iface_name);
 
302
 
 
303
        if (!soft_iface) {
 
304
                soft_iface = softif_create(iface_name);
 
305
 
 
306
                if (!soft_iface) {
 
307
                        ret = -ENOMEM;
293
308
                        goto err;
 
309
                }
294
310
 
295
311
                /* dev_get_by_name() increases the reference counter for us */
296
 
                dev_hold(batman_if->soft_iface);
297
 
        }
298
 
 
299
 
        bat_priv = netdev_priv(batman_if->soft_iface);
300
 
        batman_if->packet_len = BAT_PACKET_LEN;
301
 
        batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
302
 
 
303
 
        if (!batman_if->packet_buff) {
304
 
                bat_err(batman_if->soft_iface, "Can't add interface packet "
305
 
                        "(%s): out of memory\n", batman_if->net_dev->name);
306
 
                goto err;
307
 
        }
308
 
 
309
 
        batman_packet = (struct batman_packet *)(batman_if->packet_buff);
 
312
                dev_hold(soft_iface);
 
313
        }
 
314
 
 
315
        if (!softif_is_valid(soft_iface)) {
 
316
                pr_err("Can't create batman mesh interface %s: "
 
317
                       "already exists as regular interface\n",
 
318
                       soft_iface->name);
 
319
                dev_put(soft_iface);
 
320
                ret = -EINVAL;
 
321
                goto err;
 
322
        }
 
323
 
 
324
        hard_iface->soft_iface = soft_iface;
 
325
        bat_priv = netdev_priv(hard_iface->soft_iface);
 
326
        hard_iface->packet_len = BAT_PACKET_LEN;
 
327
        hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC);
 
328
 
 
329
        if (!hard_iface->packet_buff) {
 
330
                bat_err(hard_iface->soft_iface, "Can't add interface packet "
 
331
                        "(%s): out of memory\n", hard_iface->net_dev->name);
 
332
                ret = -ENOMEM;
 
333
                goto err;
 
334
        }
 
335
 
 
336
        batman_packet = (struct batman_packet *)(hard_iface->packet_buff);
310
337
        batman_packet->packet_type = BAT_PACKET;
311
338
        batman_packet->version = COMPAT_VERSION;
312
339
        batman_packet->flags = 0;
313
340
        batman_packet->ttl = 2;
314
341
        batman_packet->tq = TQ_MAX_VALUE;
315
 
        batman_packet->num_hna = 0;
 
342
        batman_packet->num_tt = 0;
316
343
 
317
 
        batman_if->if_num = bat_priv->num_ifaces;
 
344
        hard_iface->if_num = bat_priv->num_ifaces;
318
345
        bat_priv->num_ifaces++;
319
 
        batman_if->if_status = IF_INACTIVE;
320
 
        orig_hash_add_if(batman_if, bat_priv->num_ifaces);
321
 
 
322
 
        batman_if->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
323
 
        batman_if->batman_adv_ptype.func = batman_skb_recv;
324
 
        batman_if->batman_adv_ptype.dev = batman_if->net_dev;
325
 
        kref_get(&batman_if->refcount);
326
 
        dev_add_pack(&batman_if->batman_adv_ptype);
327
 
 
328
 
        atomic_set(&batman_if->seqno, 1);
329
 
        atomic_set(&batman_if->frag_seqno, 1);
330
 
        bat_info(batman_if->soft_iface, "Adding interface: %s\n",
331
 
                 batman_if->net_dev->name);
332
 
 
333
 
        if (atomic_read(&bat_priv->fragmentation) && batman_if->net_dev->mtu <
 
346
        hard_iface->if_status = IF_INACTIVE;
 
347
        orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
 
348
 
 
349
        hard_iface->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
 
350
        hard_iface->batman_adv_ptype.func = batman_skb_recv;
 
351
        hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
 
352
        dev_add_pack(&hard_iface->batman_adv_ptype);
 
353
 
 
354
        atomic_set(&hard_iface->seqno, 1);
 
355
        atomic_set(&hard_iface->frag_seqno, 1);
 
356
        bat_info(hard_iface->soft_iface, "Adding interface: %s\n",
 
357
                 hard_iface->net_dev->name);
 
358
 
 
359
        if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
334
360
                ETH_DATA_LEN + BAT_HEADER_LEN)
335
 
                bat_info(batman_if->soft_iface,
 
361
                bat_info(hard_iface->soft_iface,
336
362
                        "The MTU of interface %s is too small (%i) to handle "
337
363
                        "the transport of batman-adv packets. Packets going "
338
364
                        "over this interface will be fragmented on layer2 "
339
365
                        "which could impact the performance. Setting the MTU "
340
366
                        "to %zi would solve the problem.\n",
341
 
                        batman_if->net_dev->name, batman_if->net_dev->mtu,
 
367
                        hard_iface->net_dev->name, hard_iface->net_dev->mtu,
342
368
                        ETH_DATA_LEN + BAT_HEADER_LEN);
343
369
 
344
 
        if (!atomic_read(&bat_priv->fragmentation) && batman_if->net_dev->mtu <
 
370
        if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
345
371
                ETH_DATA_LEN + BAT_HEADER_LEN)
346
 
                bat_info(batman_if->soft_iface,
 
372
                bat_info(hard_iface->soft_iface,
347
373
                        "The MTU of interface %s is too small (%i) to handle "
348
374
                        "the transport of batman-adv packets. If you experience"
349
375
                        " problems getting traffic through try increasing the "
350
376
                        "MTU to %zi.\n",
351
 
                        batman_if->net_dev->name, batman_if->net_dev->mtu,
 
377
                        hard_iface->net_dev->name, hard_iface->net_dev->mtu,
352
378
                        ETH_DATA_LEN + BAT_HEADER_LEN);
353
379
 
354
 
        if (hardif_is_iface_up(batman_if))
355
 
                hardif_activate_interface(batman_if);
 
380
        if (hardif_is_iface_up(hard_iface))
 
381
                hardif_activate_interface(hard_iface);
356
382
        else
357
 
                bat_err(batman_if->soft_iface, "Not using interface %s "
 
383
                bat_err(hard_iface->soft_iface, "Not using interface %s "
358
384
                        "(retrying later): interface not active\n",
359
 
                        batman_if->net_dev->name);
 
385
                        hard_iface->net_dev->name);
360
386
 
361
387
        /* begin scheduling originator messages on that interface */
362
 
        schedule_own_packet(batman_if);
 
388
        schedule_own_packet(hard_iface);
363
389
 
364
390
out:
365
391
        return 0;
366
392
 
367
393
err:
368
 
        return -ENOMEM;
 
394
        hardif_free_ref(hard_iface);
 
395
        return ret;
369
396
}
370
397
 
371
 
void hardif_disable_interface(struct batman_if *batman_if)
 
398
void hardif_disable_interface(struct hard_iface *hard_iface)
372
399
{
373
 
        struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
374
 
 
375
 
        if (batman_if->if_status == IF_ACTIVE)
376
 
                hardif_deactivate_interface(batman_if);
377
 
 
378
 
        if (batman_if->if_status != IF_INACTIVE)
379
 
                return;
380
 
 
381
 
        bat_info(batman_if->soft_iface, "Removing interface: %s\n",
382
 
                 batman_if->net_dev->name);
383
 
        dev_remove_pack(&batman_if->batman_adv_ptype);
384
 
        kref_put(&batman_if->refcount, hardif_free_ref);
 
400
        struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
 
401
        struct hard_iface *primary_if = NULL;
 
402
 
 
403
        if (hard_iface->if_status == IF_ACTIVE)
 
404
                hardif_deactivate_interface(hard_iface);
 
405
 
 
406
        if (hard_iface->if_status != IF_INACTIVE)
 
407
                goto out;
 
408
 
 
409
        bat_info(hard_iface->soft_iface, "Removing interface: %s\n",
 
410
                 hard_iface->net_dev->name);
 
411
        dev_remove_pack(&hard_iface->batman_adv_ptype);
385
412
 
386
413
        bat_priv->num_ifaces--;
387
 
        orig_hash_del_if(batman_if, bat_priv->num_ifaces);
388
 
 
389
 
        if (batman_if == bat_priv->primary_if) {
390
 
                struct batman_if *new_if;
391
 
 
392
 
                new_if = get_active_batman_if(batman_if->soft_iface);
393
 
                set_primary_if(bat_priv, new_if);
 
414
        orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
 
415
 
 
416
        primary_if = primary_if_get_selected(bat_priv);
 
417
        if (hard_iface == primary_if) {
 
418
                struct hard_iface *new_if;
 
419
 
 
420
                new_if = hardif_get_active(hard_iface->soft_iface);
 
421
                primary_if_select(bat_priv, new_if);
394
422
 
395
423
                if (new_if)
396
 
                        kref_put(&new_if->refcount, hardif_free_ref);
 
424
                        hardif_free_ref(new_if);
397
425
        }
398
426
 
399
 
        kfree(batman_if->packet_buff);
400
 
        batman_if->packet_buff = NULL;
401
 
        batman_if->if_status = IF_NOT_IN_USE;
 
427
        kfree(hard_iface->packet_buff);
 
428
        hard_iface->packet_buff = NULL;
 
429
        hard_iface->if_status = IF_NOT_IN_USE;
402
430
 
403
 
        /* delete all references to this batman_if */
 
431
        /* delete all references to this hard_iface */
404
432
        purge_orig_ref(bat_priv);
405
 
        purge_outstanding_packets(bat_priv, batman_if);
406
 
        dev_put(batman_if->soft_iface);
 
433
        purge_outstanding_packets(bat_priv, hard_iface);
 
434
        dev_put(hard_iface->soft_iface);
407
435
 
408
436
        /* nobody uses this interface anymore */
409
437
        if (!bat_priv->num_ifaces)
410
 
                softif_destroy(batman_if->soft_iface);
411
 
 
412
 
        batman_if->soft_iface = NULL;
 
438
                softif_destroy(hard_iface->soft_iface);
 
439
 
 
440
        hard_iface->soft_iface = NULL;
 
441
        hardif_free_ref(hard_iface);
 
442
 
 
443
out:
 
444
        if (primary_if)
 
445
                hardif_free_ref(primary_if);
413
446
}
414
447
 
415
 
static struct batman_if *hardif_add_interface(struct net_device *net_dev)
 
448
static struct hard_iface *hardif_add_interface(struct net_device *net_dev)
416
449
{
417
 
        struct batman_if *batman_if;
 
450
        struct hard_iface *hard_iface;
418
451
        int ret;
419
452
 
 
453
        ASSERT_RTNL();
 
454
 
420
455
        ret = is_valid_iface(net_dev);
421
456
        if (ret != 1)
422
457
                goto out;
423
458
 
424
459
        dev_hold(net_dev);
425
460
 
426
 
        batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
427
 
        if (!batman_if) {
 
461
        hard_iface = kmalloc(sizeof(struct hard_iface), GFP_ATOMIC);
 
462
        if (!hard_iface) {
428
463
                pr_err("Can't add interface (%s): out of memory\n",
429
464
                       net_dev->name);
430
465
                goto release_dev;
431
466
        }
432
467
 
433
 
        ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
 
468
        ret = sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
434
469
        if (ret)
435
470
                goto free_if;
436
471
 
437
 
        batman_if->if_num = -1;
438
 
        batman_if->net_dev = net_dev;
439
 
        batman_if->soft_iface = NULL;
440
 
        batman_if->if_status = IF_NOT_IN_USE;
441
 
        INIT_LIST_HEAD(&batman_if->list);
442
 
        kref_init(&batman_if->refcount);
443
 
 
444
 
        check_known_mac_addr(batman_if->net_dev);
445
 
 
446
 
        spin_lock(&if_list_lock);
447
 
        list_add_tail_rcu(&batman_if->list, &if_list);
448
 
        spin_unlock(&if_list_lock);
449
 
 
 
472
        hard_iface->if_num = -1;
 
473
        hard_iface->net_dev = net_dev;
 
474
        hard_iface->soft_iface = NULL;
 
475
        hard_iface->if_status = IF_NOT_IN_USE;
 
476
        INIT_LIST_HEAD(&hard_iface->list);
450
477
        /* extra reference for return */
451
 
        kref_get(&batman_if->refcount);
452
 
        return batman_if;
 
478
        atomic_set(&hard_iface->refcount, 2);
 
479
 
 
480
        check_known_mac_addr(hard_iface->net_dev);
 
481
        list_add_tail_rcu(&hard_iface->list, &hardif_list);
 
482
 
 
483
        return hard_iface;
453
484
 
454
485
free_if:
455
 
        kfree(batman_if);
 
486
        kfree(hard_iface);
456
487
release_dev:
457
488
        dev_put(net_dev);
458
489
out:
459
490
        return NULL;
460
491
}
461
492
 
462
 
static void hardif_remove_interface(struct batman_if *batman_if)
 
493
static void hardif_remove_interface(struct hard_iface *hard_iface)
463
494
{
 
495
        ASSERT_RTNL();
 
496
 
464
497
        /* first deactivate interface */
465
 
        if (batman_if->if_status != IF_NOT_IN_USE)
466
 
                hardif_disable_interface(batman_if);
 
498
        if (hard_iface->if_status != IF_NOT_IN_USE)
 
499
                hardif_disable_interface(hard_iface);
467
500
 
468
 
        if (batman_if->if_status != IF_NOT_IN_USE)
 
501
        if (hard_iface->if_status != IF_NOT_IN_USE)
469
502
                return;
470
503
 
471
 
        batman_if->if_status = IF_TO_BE_REMOVED;
472
 
        sysfs_del_hardif(&batman_if->hardif_obj);
473
 
        call_rcu(&batman_if->rcu, hardif_free_rcu);
 
504
        hard_iface->if_status = IF_TO_BE_REMOVED;
 
505
        sysfs_del_hardif(&hard_iface->hardif_obj);
 
506
        hardif_free_ref(hard_iface);
474
507
}
475
508
 
476
509
void hardif_remove_interfaces(void)
477
510
{
478
 
        struct batman_if *batman_if, *batman_if_tmp;
479
 
        struct list_head if_queue;
480
 
 
481
 
        INIT_LIST_HEAD(&if_queue);
482
 
 
483
 
        spin_lock(&if_list_lock);
484
 
        list_for_each_entry_safe(batman_if, batman_if_tmp, &if_list, list) {
485
 
                list_del_rcu(&batman_if->list);
486
 
                list_add_tail(&batman_if->list, &if_queue);
487
 
        }
488
 
        spin_unlock(&if_list_lock);
 
511
        struct hard_iface *hard_iface, *hard_iface_tmp;
489
512
 
490
513
        rtnl_lock();
491
 
        list_for_each_entry_safe(batman_if, batman_if_tmp, &if_queue, list) {
492
 
                hardif_remove_interface(batman_if);
 
514
        list_for_each_entry_safe(hard_iface, hard_iface_tmp,
 
515
                                 &hardif_list, list) {
 
516
                list_del_rcu(&hard_iface->list);
 
517
                hardif_remove_interface(hard_iface);
493
518
        }
494
519
        rtnl_unlock();
495
520
}
498
523
                         unsigned long event, void *ptr)
499
524
{
500
525
        struct net_device *net_dev = (struct net_device *)ptr;
501
 
        struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
 
526
        struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
 
527
        struct hard_iface *primary_if = NULL;
502
528
        struct bat_priv *bat_priv;
503
529
 
504
 
        if (!batman_if && event == NETDEV_REGISTER)
505
 
                batman_if = hardif_add_interface(net_dev);
 
530
        if (!hard_iface && event == NETDEV_REGISTER)
 
531
                hard_iface = hardif_add_interface(net_dev);
506
532
 
507
 
        if (!batman_if)
 
533
        if (!hard_iface)
508
534
                goto out;
509
535
 
510
536
        switch (event) {
511
537
        case NETDEV_UP:
512
 
                hardif_activate_interface(batman_if);
 
538
                hardif_activate_interface(hard_iface);
513
539
                break;
514
540
        case NETDEV_GOING_DOWN:
515
541
        case NETDEV_DOWN:
516
 
                hardif_deactivate_interface(batman_if);
 
542
                hardif_deactivate_interface(hard_iface);
517
543
                break;
518
544
        case NETDEV_UNREGISTER:
519
 
                spin_lock(&if_list_lock);
520
 
                list_del_rcu(&batman_if->list);
521
 
                spin_unlock(&if_list_lock);
 
545
                list_del_rcu(&hard_iface->list);
522
546
 
523
 
                hardif_remove_interface(batman_if);
 
547
                hardif_remove_interface(hard_iface);
524
548
                break;
525
549
        case NETDEV_CHANGEMTU:
526
 
                if (batman_if->soft_iface)
527
 
                        update_min_mtu(batman_if->soft_iface);
 
550
                if (hard_iface->soft_iface)
 
551
                        update_min_mtu(hard_iface->soft_iface);
528
552
                break;
529
553
        case NETDEV_CHANGEADDR:
530
 
                if (batman_if->if_status == IF_NOT_IN_USE)
531
 
                        goto hardif_put;
532
 
 
533
 
                check_known_mac_addr(batman_if->net_dev);
534
 
                update_mac_addresses(batman_if);
535
 
 
536
 
                bat_priv = netdev_priv(batman_if->soft_iface);
537
 
                if (batman_if == bat_priv->primary_if)
538
 
                        update_primary_addr(bat_priv);
 
554
                if (hard_iface->if_status == IF_NOT_IN_USE)
 
555
                        goto hardif_put;
 
556
 
 
557
                check_known_mac_addr(hard_iface->net_dev);
 
558
                update_mac_addresses(hard_iface);
 
559
 
 
560
                bat_priv = netdev_priv(hard_iface->soft_iface);
 
561
                primary_if = primary_if_get_selected(bat_priv);
 
562
                if (!primary_if)
 
563
                        goto hardif_put;
 
564
 
 
565
                if (hard_iface == primary_if)
 
566
                        primary_if_update_addr(bat_priv);
539
567
                break;
540
568
        default:
541
569
                break;
542
570
        };
543
571
 
544
572
hardif_put:
545
 
        kref_put(&batman_if->refcount, hardif_free_ref);
 
573
        hardif_free_ref(hard_iface);
546
574
out:
 
575
        if (primary_if)
 
576
                hardif_free_ref(primary_if);
547
577
        return NOTIFY_DONE;
548
578
}
549
579
 
550
580
/* receive a packet with the batman ethertype coming on a hard
551
581
 * interface */
552
 
int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
553
 
        struct packet_type *ptype, struct net_device *orig_dev)
 
582
static int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
 
583
                           struct packet_type *ptype,
 
584
                           struct net_device *orig_dev)
554
585
{
555
586
        struct bat_priv *bat_priv;
556
587
        struct batman_packet *batman_packet;
557
 
        struct batman_if *batman_if;
 
588
        struct hard_iface *hard_iface;
558
589
        int ret;
559
590
 
560
 
        batman_if = container_of(ptype, struct batman_if, batman_adv_ptype);
 
591
        hard_iface = container_of(ptype, struct hard_iface, batman_adv_ptype);
561
592
        skb = skb_share_check(skb, GFP_ATOMIC);
562
593
 
563
594
        /* skb was released by skb_share_check() */
573
604
                                || !skb_mac_header(skb)))
574
605
                goto err_free;
575
606
 
576
 
        if (!batman_if->soft_iface)
 
607
        if (!hard_iface->soft_iface)
577
608
                goto err_free;
578
609
 
579
 
        bat_priv = netdev_priv(batman_if->soft_iface);
 
610
        bat_priv = netdev_priv(hard_iface->soft_iface);
580
611
 
581
612
        if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
582
613
                goto err_free;
583
614
 
584
615
        /* discard frames on not active interfaces */
585
 
        if (batman_if->if_status != IF_ACTIVE)
 
616
        if (hard_iface->if_status != IF_ACTIVE)
586
617
                goto err_free;
587
618
 
588
619
        batman_packet = (struct batman_packet *)skb->data;
600
631
        switch (batman_packet->packet_type) {
601
632
                /* batman originator packet */
602
633
        case BAT_PACKET:
603
 
                ret = recv_bat_packet(skb, batman_if);
 
634
                ret = recv_bat_packet(skb, hard_iface);
604
635
                break;
605
636
 
606
637
                /* batman icmp packet */
607
638
        case BAT_ICMP:
608
 
                ret = recv_icmp_packet(skb, batman_if);
 
639
                ret = recv_icmp_packet(skb, hard_iface);
609
640
                break;
610
641
 
611
642
                /* unicast packet */
612
643
        case BAT_UNICAST:
613
 
                ret = recv_unicast_packet(skb, batman_if);
 
644
                ret = recv_unicast_packet(skb, hard_iface);
614
645
                break;
615
646
 
616
647
                /* fragmented unicast packet */
617
648
        case BAT_UNICAST_FRAG:
618
 
                ret = recv_ucast_frag_packet(skb, batman_if);
 
649
                ret = recv_ucast_frag_packet(skb, hard_iface);
619
650
                break;
620
651
 
621
652
                /* broadcast packet */
622
653
        case BAT_BCAST:
623
 
                ret = recv_bcast_packet(skb, batman_if);
 
654
                ret = recv_bcast_packet(skb, hard_iface);
624
655
                break;
625
656
 
626
657
                /* vis packet */
627
658
        case BAT_VIS:
628
 
                ret = recv_vis_packet(skb, batman_if);
 
659
                ret = recv_vis_packet(skb, hard_iface);
629
660
                break;
630
661
        default:
631
662
                ret = NET_RX_DROP;