Merge remote branch 'nouveau/for-airlied' of nouveau-2.6
[firefly-linux-kernel-4.4.55.git] / drivers / staging / batman-adv / hard-interface.c
1 /*
2  * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA
19  *
20  */
21
22 #include "main.h"
23 #include "hard-interface.h"
24 #include "log.h"
25 #include "soft-interface.h"
26 #include "send.h"
27 #include "translation-table.h"
28 #include "routing.h"
29 #include "hash.h"
30 #include "compat.h"
31
32 #define MIN(x, y) ((x) < (y) ? (x) : (y))
33
34 static char avail_ifs;
35 static char active_ifs;
36
37 static void hardif_free_interface(struct rcu_head *rcu);
38
39 static struct batman_if *get_batman_if_by_name(char *name)
40 {
41         struct batman_if *batman_if;
42
43         rcu_read_lock();
44         list_for_each_entry_rcu(batman_if, &if_list, list) {
45                 if (strncmp(batman_if->dev, name, IFNAMSIZ) == 0)
46                         goto out;
47         }
48
49         batman_if = NULL;
50
51 out:
52         rcu_read_unlock();
53         return batman_if;
54 }
55
56 int hardif_min_mtu(void)
57 {
58         struct batman_if *batman_if;
59         /* allow big frames if all devices are capable to do so
60          * (have MTU > 1500 + BAT_HEADER_LEN) */
61         int min_mtu = ETH_DATA_LEN;
62
63         rcu_read_lock();
64         list_for_each_entry_rcu(batman_if, &if_list, list) {
65                 if ((batman_if->if_active == IF_ACTIVE) ||
66                     (batman_if->if_active == IF_TO_BE_ACTIVATED))
67                         min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
68                                       min_mtu);
69         }
70         rcu_read_unlock();
71
72         return min_mtu;
73 }
74
75 static void check_known_mac_addr(uint8_t *addr)
76 {
77         struct batman_if *batman_if;
78         char mac_string[ETH_STR_LEN];
79
80         rcu_read_lock();
81         list_for_each_entry_rcu(batman_if, &if_list, list) {
82                 if ((batman_if->if_active != IF_ACTIVE) &&
83                     (batman_if->if_active != IF_TO_BE_ACTIVATED))
84                         continue;
85
86                 if (!compare_orig(batman_if->net_dev->dev_addr, addr))
87                         continue;
88
89                 addr_to_string(mac_string, addr);
90                 debug_log(LOG_TYPE_WARN, "The newly added mac address (%s) already exists on: %s\n",
91                           mac_string, batman_if->dev);
92                 debug_log(LOG_TYPE_WARN, "It is strongly recommended to keep mac addresses unique to avoid problems!\n");
93         }
94         rcu_read_unlock();
95 }
96
97 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
98 void update_min_mtu(void)
99 {
100         int min_mtu;
101
102         min_mtu = hardif_min_mtu();
103         if (soft_device->mtu != min_mtu)
104                 soft_device->mtu = min_mtu;
105 }
106
107 /* checks if the interface is up. (returns 1 if it is) */
108 static int hardif_is_interface_up(char *dev)
109 {
110         struct net_device *net_dev;
111
112         /**
113          * if we already have an interface in our interface list and
114          * the current interface is not the primary interface and
115          * the primary interface is not up and
116          * the primary interface has never been up - don't activate any
117          * secondary interface !
118          */
119
120         rcu_read_lock();
121         if ((!list_empty(&if_list)) &&
122             strncmp(((struct batman_if *)if_list.next)->dev, dev, IFNAMSIZ) &&
123             !(((struct batman_if *)if_list.next)->if_active == IF_ACTIVE) &&
124             !(((struct batman_if *)if_list.next)->if_active == IF_TO_BE_ACTIVATED) &&
125             (!main_if_was_up())) {
126                 rcu_read_unlock();
127                 goto end;
128         }
129         rcu_read_unlock();
130
131 #ifdef __NET_NET_NAMESPACE_H
132         net_dev = dev_get_by_name(&init_net, dev);
133 #else
134         net_dev = dev_get_by_name(dev);
135 #endif
136         if (!net_dev)
137                 goto end;
138
139         if (!(net_dev->flags & IFF_UP))
140                 goto failure;
141
142         dev_put(net_dev);
143         return 1;
144
145 failure:
146         dev_put(net_dev);
147 end:
148         return 0;
149 }
150
151 /* deactivates the interface. */
152 void hardif_deactivate_interface(struct batman_if *batman_if)
153 {
154         if (batman_if->if_active != IF_ACTIVE)
155                 return;
156
157         if (batman_if->raw_sock)
158                 sock_release(batman_if->raw_sock);
159
160         /**
161          * batman_if->net_dev has been acquired by dev_get_by_name() in
162          * proc_interfaces_write() and has to be unreferenced.
163          */
164
165         if (batman_if->net_dev)
166                 dev_put(batman_if->net_dev);
167
168         batman_if->raw_sock = NULL;
169         batman_if->net_dev = NULL;
170
171         batman_if->if_active = IF_INACTIVE;
172         active_ifs--;
173
174         debug_log(LOG_TYPE_NOTICE, "Interface deactivated: %s\n",
175                   batman_if->dev);
176 }
177
178 /* (re)activate given interface. */
179 static void hardif_activate_interface(struct batman_if *batman_if)
180 {
181         struct sockaddr_ll bind_addr;
182         int retval;
183
184         if (batman_if->if_active != IF_INACTIVE)
185                 return;
186
187 #ifdef __NET_NET_NAMESPACE_H
188         batman_if->net_dev = dev_get_by_name(&init_net, batman_if->dev);
189 #else
190         batman_if->net_dev = dev_get_by_name(batman_if->dev);
191 #endif
192         if (!batman_if->net_dev)
193                 goto dev_err;
194
195         retval = sock_create_kern(PF_PACKET, SOCK_RAW,
196                                   __constant_htons(ETH_P_BATMAN),
197                                   &batman_if->raw_sock);
198
199         if (retval < 0) {
200                 debug_log(LOG_TYPE_WARN, "Can't create raw socket: %i\n",
201                           retval);
202                 goto sock_err;
203         }
204
205         bind_addr.sll_family = AF_PACKET;
206         bind_addr.sll_ifindex = batman_if->net_dev->ifindex;
207         bind_addr.sll_protocol = 0;     /* is set by the kernel */
208
209         retval = kernel_bind(batman_if->raw_sock,
210                              (struct sockaddr *)&bind_addr, sizeof(bind_addr));
211
212         if (retval < 0) {
213                 debug_log(LOG_TYPE_WARN, "Can't create bind raw socket: %i\n",
214                           retval);
215                 goto bind_err;
216         }
217
218         check_known_mac_addr(batman_if->net_dev->dev_addr);
219
220         batman_if->raw_sock->sk->sk_user_data =
221                 batman_if->raw_sock->sk->sk_data_ready;
222         batman_if->raw_sock->sk->sk_data_ready = batman_data_ready;
223
224         addr_to_string(batman_if->addr_str, batman_if->net_dev->dev_addr);
225
226         memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
227                batman_if->net_dev->dev_addr, ETH_ALEN);
228         memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
229                batman_if->net_dev->dev_addr, ETH_ALEN);
230
231         batman_if->if_active = IF_TO_BE_ACTIVATED;
232         active_ifs++;
233
234         /* save the mac address if it is our primary interface */
235         if (batman_if->if_num == 0)
236                 set_main_if_addr(batman_if->net_dev->dev_addr);
237
238         debug_log(LOG_TYPE_NOTICE, "Interface activated: %s\n",
239                   batman_if->dev);
240
241         return;
242
243 bind_err:
244         sock_release(batman_if->raw_sock);
245 sock_err:
246         dev_put(batman_if->net_dev);
247 dev_err:
248         batman_if->raw_sock = NULL;
249         batman_if->net_dev = NULL;
250 }
251
252 static void hardif_free_interface(struct rcu_head *rcu)
253 {
254         struct batman_if *batman_if = container_of(rcu, struct batman_if, rcu);
255
256         kfree(batman_if->packet_buff);
257         kfree(batman_if->dev);
258         kfree(batman_if);
259 }
260
261 /**
262  * called by
263  *  - echo '' > /proc/.../interfaces
264  *  - modprobe -r batman-adv-core
265  */
266 /* removes and frees all interfaces */
267 void hardif_remove_interfaces(void)
268 {
269         struct batman_if *batman_if = NULL;
270
271         avail_ifs = 0;
272
273         /* no lock needed - we don't delete somewhere else */
274         list_for_each_entry(batman_if, &if_list, list) {
275
276                 list_del_rcu(&batman_if->list);
277
278                 /* first deactivate interface */
279                 if (batman_if->if_active != IF_INACTIVE)
280                         hardif_deactivate_interface(batman_if);
281
282                 call_rcu(&batman_if->rcu, hardif_free_interface);
283         }
284 }
285
286 static int resize_orig(struct orig_node *orig_node, int if_num)
287 {
288         void *data_ptr;
289
290         data_ptr = kmalloc((if_num + 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS,
291                            GFP_ATOMIC);
292         if (!data_ptr) {
293                 debug_log(LOG_TYPE_WARN, "Can't resize orig: out of memory\n");
294                 return -1;
295         }
296
297         memcpy(data_ptr, orig_node->bcast_own,
298                if_num * sizeof(TYPE_OF_WORD) * NUM_WORDS);
299         kfree(orig_node->bcast_own);
300         orig_node->bcast_own = data_ptr;
301
302         data_ptr = kmalloc((if_num + 1) * sizeof(uint8_t), GFP_ATOMIC);
303         if (!data_ptr) {
304                 debug_log(LOG_TYPE_WARN, "Can't resize orig: out of memory\n");
305                 return -1;
306         }
307
308         memcpy(data_ptr, orig_node->bcast_own_sum, if_num * sizeof(uint8_t));
309         kfree(orig_node->bcast_own_sum);
310         orig_node->bcast_own_sum = data_ptr;
311
312         return 0;
313 }
314
315
316 /* adds an interface the interface list and activate it, if possible */
317 int hardif_add_interface(char *dev, int if_num)
318 {
319         struct batman_if *batman_if;
320         struct batman_packet *batman_packet;
321         struct orig_node *orig_node;
322         struct hash_it_t *hashit = NULL;
323
324         batman_if = kmalloc(sizeof(struct batman_if), GFP_KERNEL);
325
326         if (!batman_if) {
327                 debug_log(LOG_TYPE_WARN, "Can't add interface (%s): out of memory\n", dev);
328                 return -1;
329         }
330
331         batman_if->raw_sock = NULL;
332         batman_if->net_dev = NULL;
333
334         if ((if_num == 0) && (num_hna > 0))
335                 batman_if->packet_len = BAT_PACKET_LEN + num_hna * ETH_ALEN;
336         else
337                 batman_if->packet_len = BAT_PACKET_LEN;
338
339         batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_KERNEL);
340
341         if (!batman_if->packet_buff) {
342                 debug_log(LOG_TYPE_WARN, "Can't add interface packet (%s): out of memory\n", dev);
343                 goto out;
344         }
345
346         batman_if->if_num = if_num;
347         batman_if->dev = dev;
348         batman_if->if_active = IF_INACTIVE;
349         INIT_RCU_HEAD(&batman_if->rcu);
350
351         debug_log(LOG_TYPE_NOTICE, "Adding interface: %s\n", dev);
352         avail_ifs++;
353
354         INIT_LIST_HEAD(&batman_if->list);
355
356         batman_packet = (struct batman_packet *)(batman_if->packet_buff);
357         batman_packet->packet_type = BAT_PACKET;
358         batman_packet->version = COMPAT_VERSION;
359         batman_packet->flags = 0x00;
360         batman_packet->ttl = (batman_if->if_num > 0 ? 2 : TTL);
361         batman_packet->flags = 0;
362         batman_packet->tq = TQ_MAX_VALUE;
363         batman_packet->num_hna = 0;
364
365         if (batman_if->packet_len != BAT_PACKET_LEN) {
366                 unsigned char *hna_buff;
367                 int hna_len;
368
369                 hna_buff = batman_if->packet_buff + BAT_PACKET_LEN;
370                 hna_len = batman_if->packet_len - BAT_PACKET_LEN;
371                 batman_packet->num_hna = hna_local_fill_buffer(hna_buff,
372                                                                hna_len);
373         }
374
375         atomic_set(&batman_if->seqno, 1);
376
377         /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
378          * if_num */
379         spin_lock(&orig_hash_lock);
380
381         while (NULL != (hashit = hash_iterate(orig_hash, hashit))) {
382                 orig_node = hashit->bucket->data;
383                 if (resize_orig(orig_node, if_num) == -1) {
384                         spin_unlock(&orig_hash_lock);
385                         goto out;
386                 }
387         }
388
389         spin_unlock(&orig_hash_lock);
390
391         if (!hardif_is_interface_up(batman_if->dev))
392                 debug_log(LOG_TYPE_WARN, "Not using interface %s (retrying later): interface not active\n", batman_if->dev);
393         else
394                 hardif_activate_interface(batman_if);
395
396         list_add_tail_rcu(&batman_if->list, &if_list);
397
398         /* begin sending originator messages on that interface */
399         schedule_own_packet(batman_if);
400         return 1;
401
402 out:
403         if (batman_if->packet_buff)
404                 kfree(batman_if->packet_buff);
405         kfree(batman_if);
406         kfree(dev);
407         return -1;
408 }
409
410 char hardif_get_active_if_num(void)
411 {
412         return active_ifs;
413 }
414
415 static int hard_if_event(struct notifier_block *this,
416                             unsigned long event, void *ptr)
417 {
418         struct net_device *dev = (struct net_device *)ptr;
419         struct batman_if *batman_if = get_batman_if_by_name(dev->name);
420
421         if (!batman_if)
422                 goto out;
423
424         switch (event) {
425         case NETDEV_GOING_DOWN:
426         case NETDEV_DOWN:
427         case NETDEV_UNREGISTER:
428                 hardif_deactivate_interface(batman_if);
429                 break;
430         case NETDEV_UP:
431                 hardif_activate_interface(batman_if);
432                 if ((atomic_read(&module_state) == MODULE_INACTIVE) &&
433                     (hardif_get_active_if_num() > 0)) {
434                         activate_module();
435                 }
436                 break;
437         /* NETDEV_CHANGEADDR - mac address change - what are we doing here ? */
438         default:
439                 /* debug_log(LOG_TYPE_CRIT, "hard_if_event: %s %i\n", dev->name, event); */
440                 break;
441         };
442
443         update_min_mtu();
444
445 out:
446         return NOTIFY_DONE;
447 }
448
449 struct notifier_block hard_if_notifier = {
450         .notifier_call = hard_if_event,
451 };