Sven Eckelmann | 0046b04 | 2016-01-01 00:01:03 +0100 | [diff] [blame] | 1 | /* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 2 | * |
| 3 | * Marek Lindner, Simon Wunderlich |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of version 2 of the GNU General Public |
| 7 | * License as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
Antonio Quartulli | ebf38fb | 2013-11-03 20:40:48 +0100 | [diff] [blame] | 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 18 | #include "hard-interface.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 19 | #include "main.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 20 | |
Sven Eckelmann | 7a659d5 | 2016-01-16 10:29:54 +0100 | [diff] [blame] | 21 | #include <linux/atomic.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 22 | #include <linux/bug.h> |
| 23 | #include <linux/byteorder/generic.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/fs.h> |
Sven Eckelmann | fcafa5e | 2016-05-15 11:07:42 +0200 | [diff] [blame] | 26 | #include <linux/if.h> |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 27 | #include <linux/if_arp.h> |
Antonio Quartulli | af5d4f7 | 2012-11-26 00:38:50 +0100 | [diff] [blame] | 28 | #include <linux/if_ether.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 29 | #include <linux/kernel.h> |
Sven Eckelmann | 7a659d5 | 2016-01-16 10:29:54 +0100 | [diff] [blame] | 30 | #include <linux/kref.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 31 | #include <linux/list.h> |
| 32 | #include <linux/netdevice.h> |
| 33 | #include <linux/printk.h> |
| 34 | #include <linux/rculist.h> |
| 35 | #include <linux/rtnetlink.h> |
| 36 | #include <linux/slab.h> |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 37 | #include <linux/spinlock.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 38 | #include <linux/workqueue.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 39 | |
Marek Lindner | 7db682d | 2016-05-10 22:31:59 +0800 | [diff] [blame] | 40 | #include "bat_algo.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 41 | #include "bridge_loop_avoidance.h" |
| 42 | #include "debugfs.h" |
| 43 | #include "distributed-arp-table.h" |
| 44 | #include "gateway_client.h" |
Sven Eckelmann | ba41208 | 2016-05-15 23:48:31 +0200 | [diff] [blame^] | 45 | #include "log.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 46 | #include "originator.h" |
| 47 | #include "packet.h" |
| 48 | #include "send.h" |
| 49 | #include "soft-interface.h" |
| 50 | #include "sysfs.h" |
| 51 | #include "translation-table.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 52 | |
Sven Eckelmann | 140ed8e | 2016-01-05 12:06:26 +0100 | [diff] [blame] | 53 | /** |
| 54 | * batadv_hardif_release - release hard interface from lists and queue for |
| 55 | * free after rcu grace period |
Sven Eckelmann | 7a659d5 | 2016-01-16 10:29:54 +0100 | [diff] [blame] | 56 | * @ref: kref pointer of the hard interface |
Sven Eckelmann | 140ed8e | 2016-01-05 12:06:26 +0100 | [diff] [blame] | 57 | */ |
Sven Eckelmann | 7a659d5 | 2016-01-16 10:29:54 +0100 | [diff] [blame] | 58 | void batadv_hardif_release(struct kref *ref) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 59 | { |
Sven Eckelmann | 7a659d5 | 2016-01-16 10:29:54 +0100 | [diff] [blame] | 60 | struct batadv_hard_iface *hard_iface; |
| 61 | |
| 62 | hard_iface = container_of(ref, struct batadv_hard_iface, refcount); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 63 | dev_put(hard_iface->net_dev); |
Sven Eckelmann | 140ed8e | 2016-01-05 12:06:26 +0100 | [diff] [blame] | 64 | |
| 65 | kfree_rcu(hard_iface, rcu); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 68 | struct batadv_hard_iface * |
| 69 | batadv_hardif_get_by_netdev(const struct net_device *net_dev) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 70 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 71 | struct batadv_hard_iface *hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 72 | |
| 73 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 74 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 75 | if (hard_iface->net_dev == net_dev && |
Sven Eckelmann | 7a659d5 | 2016-01-16 10:29:54 +0100 | [diff] [blame] | 76 | kref_get_unless_zero(&hard_iface->refcount)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 77 | goto out; |
| 78 | } |
| 79 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 80 | hard_iface = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 81 | |
| 82 | out: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 83 | rcu_read_unlock(); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 84 | return hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Antonio Quartulli | b7eddd0 | 2012-09-09 10:46:46 +0200 | [diff] [blame] | 87 | /** |
Andrew Lunn | 1bc4e2b | 2016-02-11 22:15:57 +0100 | [diff] [blame] | 88 | * batadv_mutual_parents - check if two devices are each others parent |
| 89 | * @dev1: 1st net_device |
| 90 | * @dev2: 2nd net_device |
| 91 | * |
| 92 | * veth devices come in pairs and each is the parent of the other! |
| 93 | * |
| 94 | * Return: true if the devices are each others parent, otherwise false |
| 95 | */ |
| 96 | static bool batadv_mutual_parents(const struct net_device *dev1, |
| 97 | const struct net_device *dev2) |
| 98 | { |
| 99 | int dev1_parent_iflink = dev_get_iflink(dev1); |
| 100 | int dev2_parent_iflink = dev_get_iflink(dev2); |
| 101 | |
| 102 | if (!dev1_parent_iflink || !dev2_parent_iflink) |
| 103 | return false; |
| 104 | |
| 105 | return (dev1_parent_iflink == dev2->ifindex) && |
| 106 | (dev2_parent_iflink == dev1->ifindex); |
| 107 | } |
| 108 | |
| 109 | /** |
Antonio Quartulli | b7eddd0 | 2012-09-09 10:46:46 +0200 | [diff] [blame] | 110 | * batadv_is_on_batman_iface - check if a device is a batman iface descendant |
| 111 | * @net_dev: the device to check |
| 112 | * |
| 113 | * If the user creates any virtual device on top of a batman-adv interface, it |
| 114 | * is important to prevent this new interface to be used to create a new mesh |
| 115 | * network (this behaviour would lead to a batman-over-batman configuration). |
| 116 | * This function recursively checks all the fathers of the device passed as |
| 117 | * argument looking for a batman-adv soft interface. |
| 118 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 119 | * Return: true if the device is descendant of a batman-adv mesh interface (or |
Antonio Quartulli | b7eddd0 | 2012-09-09 10:46:46 +0200 | [diff] [blame] | 120 | * if it is a batman-adv interface itself), false otherwise |
| 121 | */ |
| 122 | static bool batadv_is_on_batman_iface(const struct net_device *net_dev) |
| 123 | { |
| 124 | struct net_device *parent_dev; |
Andrew Lunn | 2cd45a0 | 2016-04-21 12:57:27 +0200 | [diff] [blame] | 125 | struct net *net = dev_net(net_dev); |
Antonio Quartulli | b7eddd0 | 2012-09-09 10:46:46 +0200 | [diff] [blame] | 126 | bool ret; |
| 127 | |
| 128 | /* check if this is a batman-adv mesh interface */ |
| 129 | if (batadv_softif_is_valid(net_dev)) |
| 130 | return true; |
| 131 | |
| 132 | /* no more parents..stop recursion */ |
Nicolas Dichtel | a54acb3 | 2015-04-02 17:07:00 +0200 | [diff] [blame] | 133 | if (dev_get_iflink(net_dev) == 0 || |
| 134 | dev_get_iflink(net_dev) == net_dev->ifindex) |
Antonio Quartulli | b7eddd0 | 2012-09-09 10:46:46 +0200 | [diff] [blame] | 135 | return false; |
| 136 | |
| 137 | /* recurse over the parent device */ |
Andrew Lunn | 2cd45a0 | 2016-04-21 12:57:27 +0200 | [diff] [blame] | 138 | parent_dev = __dev_get_by_index(net, dev_get_iflink(net_dev)); |
Antonio Quartulli | b7eddd0 | 2012-09-09 10:46:46 +0200 | [diff] [blame] | 139 | /* if we got a NULL parent_dev there is something broken.. */ |
| 140 | if (WARN(!parent_dev, "Cannot find parent device")) |
| 141 | return false; |
| 142 | |
Andrew Lunn | 1bc4e2b | 2016-02-11 22:15:57 +0100 | [diff] [blame] | 143 | if (batadv_mutual_parents(net_dev, parent_dev)) |
| 144 | return false; |
| 145 | |
Antonio Quartulli | b7eddd0 | 2012-09-09 10:46:46 +0200 | [diff] [blame] | 146 | ret = batadv_is_on_batman_iface(parent_dev); |
| 147 | |
Antonio Quartulli | b7eddd0 | 2012-09-09 10:46:46 +0200 | [diff] [blame] | 148 | return ret; |
| 149 | } |
| 150 | |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 151 | static bool batadv_is_valid_iface(const struct net_device *net_dev) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 152 | { |
| 153 | if (net_dev->flags & IFF_LOOPBACK) |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 154 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 155 | |
| 156 | if (net_dev->type != ARPHRD_ETHER) |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 157 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 158 | |
| 159 | if (net_dev->addr_len != ETH_ALEN) |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 160 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 161 | |
| 162 | /* no batman over batman */ |
Antonio Quartulli | b7eddd0 | 2012-09-09 10:46:46 +0200 | [diff] [blame] | 163 | if (batadv_is_on_batman_iface(net_dev)) |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 164 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 165 | |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 166 | return true; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Matthias Schiffer | de68d10 | 2013-03-09 23:14:22 +0100 | [diff] [blame] | 169 | /** |
| 170 | * batadv_is_wifi_netdev - check if the given net_device struct is a wifi |
| 171 | * interface |
| 172 | * @net_device: the device to check |
| 173 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 174 | * Return: true if the net device is a 802.11 wireless device, false otherwise. |
Matthias Schiffer | de68d10 | 2013-03-09 23:14:22 +0100 | [diff] [blame] | 175 | */ |
Antonio Quartulli | 0c69aec | 2013-10-13 02:50:18 +0200 | [diff] [blame] | 176 | bool batadv_is_wifi_netdev(struct net_device *net_device) |
Matthias Schiffer | de68d10 | 2013-03-09 23:14:22 +0100 | [diff] [blame] | 177 | { |
Antonio Quartulli | 0c69aec | 2013-10-13 02:50:18 +0200 | [diff] [blame] | 178 | if (!net_device) |
| 179 | return false; |
| 180 | |
Matthias Schiffer | de68d10 | 2013-03-09 23:14:22 +0100 | [diff] [blame] | 181 | #ifdef CONFIG_WIRELESS_EXT |
| 182 | /* pre-cfg80211 drivers have to implement WEXT, so it is possible to |
| 183 | * check for wireless_handlers != NULL |
| 184 | */ |
| 185 | if (net_device->wireless_handlers) |
| 186 | return true; |
| 187 | #endif |
| 188 | |
| 189 | /* cfg80211 drivers have to set ieee80211_ptr */ |
| 190 | if (net_device->ieee80211_ptr) |
| 191 | return true; |
| 192 | |
| 193 | return false; |
| 194 | } |
| 195 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 196 | static struct batadv_hard_iface * |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 197 | batadv_hardif_get_active(const struct net_device *soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 198 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 199 | struct batadv_hard_iface *hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 200 | |
| 201 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 202 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 203 | if (hard_iface->soft_iface != soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 204 | continue; |
| 205 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 206 | if (hard_iface->if_status == BATADV_IF_ACTIVE && |
Sven Eckelmann | 7a659d5 | 2016-01-16 10:29:54 +0100 | [diff] [blame] | 207 | kref_get_unless_zero(&hard_iface->refcount)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 208 | goto out; |
| 209 | } |
| 210 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 211 | hard_iface = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 212 | |
| 213 | out: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 214 | rcu_read_unlock(); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 215 | return hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 218 | static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv, |
| 219 | struct batadv_hard_iface *oldif) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 220 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 221 | struct batadv_hard_iface *primary_if; |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 222 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 223 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 224 | if (!primary_if) |
| 225 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 226 | |
Antonio Quartulli | 785ea11 | 2011-11-23 11:35:44 +0100 | [diff] [blame] | 227 | batadv_dat_init_own_addr(bat_priv, primary_if); |
Sven Eckelmann | 08adf15 | 2012-05-12 13:38:47 +0200 | [diff] [blame] | 228 | batadv_bla_update_orig_address(bat_priv, primary_if, oldif); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 229 | out: |
| 230 | if (primary_if) |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 231 | batadv_hardif_put(primary_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 234 | static void batadv_primary_if_select(struct batadv_priv *bat_priv, |
| 235 | struct batadv_hard_iface *new_hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 236 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 237 | struct batadv_hard_iface *curr_hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 238 | |
Sven Eckelmann | c3caf51 | 2011-05-03 11:51:38 +0200 | [diff] [blame] | 239 | ASSERT_RTNL(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 240 | |
Sven Eckelmann | 17a8691 | 2016-04-11 13:06:40 +0200 | [diff] [blame] | 241 | if (new_hard_iface) |
| 242 | kref_get(&new_hard_iface->refcount); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 243 | |
Sven Eckelmann | 728cbc6 | 2011-05-15 00:50:21 +0200 | [diff] [blame] | 244 | curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 245 | rcu_assign_pointer(bat_priv->primary_if, new_hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 246 | |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 247 | if (!new_hard_iface) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 248 | goto out; |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 249 | |
Marek Lindner | cd8b78e | 2012-02-07 17:20:49 +0800 | [diff] [blame] | 250 | bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface); |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 251 | batadv_primary_if_update_addr(bat_priv, curr_hard_iface); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 252 | |
| 253 | out: |
| 254 | if (curr_hard_iface) |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 255 | batadv_hardif_put(curr_hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 258 | static bool |
| 259 | batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 260 | { |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 261 | if (hard_iface->net_dev->flags & IFF_UP) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 262 | return true; |
| 263 | |
| 264 | return false; |
| 265 | } |
| 266 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 267 | static void batadv_check_known_mac_addr(const struct net_device *net_dev) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 268 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 269 | const struct batadv_hard_iface *hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 270 | |
| 271 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 272 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 273 | if ((hard_iface->if_status != BATADV_IF_ACTIVE) && |
| 274 | (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 275 | continue; |
| 276 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 277 | if (hard_iface->net_dev == net_dev) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 278 | continue; |
| 279 | |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 280 | if (!batadv_compare_eth(hard_iface->net_dev->dev_addr, |
| 281 | net_dev->dev_addr)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 282 | continue; |
| 283 | |
Sven Eckelmann | 6796958 | 2012-03-26 16:22:45 +0200 | [diff] [blame] | 284 | pr_warn("The newly added mac address (%pM) already exists on: %s\n", |
| 285 | net_dev->dev_addr, hard_iface->net_dev->name); |
| 286 | pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 287 | } |
| 288 | rcu_read_unlock(); |
| 289 | } |
| 290 | |
Sven Eckelmann | 7bca68c | 2015-08-07 19:28:42 +0200 | [diff] [blame] | 291 | /** |
| 292 | * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom |
| 293 | * @soft_iface: netdev struct of the mesh interface |
| 294 | */ |
| 295 | static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface) |
| 296 | { |
| 297 | const struct batadv_hard_iface *hard_iface; |
| 298 | unsigned short lower_header_len = ETH_HLEN; |
| 299 | unsigned short lower_headroom = 0; |
| 300 | unsigned short lower_tailroom = 0; |
| 301 | unsigned short needed_headroom; |
| 302 | |
| 303 | rcu_read_lock(); |
| 304 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
| 305 | if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) |
| 306 | continue; |
| 307 | |
| 308 | if (hard_iface->soft_iface != soft_iface) |
| 309 | continue; |
| 310 | |
| 311 | lower_header_len = max_t(unsigned short, lower_header_len, |
| 312 | hard_iface->net_dev->hard_header_len); |
| 313 | |
| 314 | lower_headroom = max_t(unsigned short, lower_headroom, |
| 315 | hard_iface->net_dev->needed_headroom); |
| 316 | |
| 317 | lower_tailroom = max_t(unsigned short, lower_tailroom, |
| 318 | hard_iface->net_dev->needed_tailroom); |
| 319 | } |
| 320 | rcu_read_unlock(); |
| 321 | |
| 322 | needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN); |
| 323 | needed_headroom += batadv_max_header_len(); |
| 324 | |
| 325 | soft_iface->needed_headroom = needed_headroom; |
| 326 | soft_iface->needed_tailroom = lower_tailroom; |
| 327 | } |
| 328 | |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 329 | int batadv_hardif_min_mtu(struct net_device *soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 330 | { |
Marek Lindner | a19d3d8 | 2013-05-27 15:33:25 +0800 | [diff] [blame] | 331 | struct batadv_priv *bat_priv = netdev_priv(soft_iface); |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 332 | const struct batadv_hard_iface *hard_iface; |
Antonio Quartulli | 930cd6e | 2014-01-21 11:22:05 +0100 | [diff] [blame] | 333 | int min_mtu = INT_MAX; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 334 | |
| 335 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 336 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 337 | if ((hard_iface->if_status != BATADV_IF_ACTIVE) && |
| 338 | (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 339 | continue; |
| 340 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 341 | if (hard_iface->soft_iface != soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 342 | continue; |
| 343 | |
Marek Lindner | a19d3d8 | 2013-05-27 15:33:25 +0800 | [diff] [blame] | 344 | min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 345 | } |
| 346 | rcu_read_unlock(); |
Marek Lindner | a19d3d8 | 2013-05-27 15:33:25 +0800 | [diff] [blame] | 347 | |
Marek Lindner | a19d3d8 | 2013-05-27 15:33:25 +0800 | [diff] [blame] | 348 | if (atomic_read(&bat_priv->fragmentation) == 0) |
| 349 | goto out; |
| 350 | |
| 351 | /* with fragmentation enabled the maximum size of internally generated |
| 352 | * packets such as translation table exchanges or tvlv containers, etc |
| 353 | * has to be calculated |
| 354 | */ |
| 355 | min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE); |
| 356 | min_mtu -= sizeof(struct batadv_frag_packet); |
| 357 | min_mtu *= BATADV_FRAG_MAX_FRAGMENTS; |
Marek Lindner | a19d3d8 | 2013-05-27 15:33:25 +0800 | [diff] [blame] | 358 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 359 | out: |
Antonio Quartulli | 930cd6e | 2014-01-21 11:22:05 +0100 | [diff] [blame] | 360 | /* report to the other components the maximum amount of bytes that |
| 361 | * batman-adv can send over the wire (without considering the payload |
| 362 | * overhead). For example, this value is used by TT to compute the |
| 363 | * maximum local table table size |
| 364 | */ |
| 365 | atomic_set(&bat_priv->packet_size_max, min_mtu); |
| 366 | |
| 367 | /* the real soft-interface MTU is computed by removing the payload |
| 368 | * overhead from the maximum amount of bytes that was just computed. |
| 369 | * |
| 370 | * However batman-adv does not support MTUs bigger than ETH_DATA_LEN |
| 371 | */ |
| 372 | return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* adjusts the MTU if a new interface with a smaller MTU appeared. */ |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 376 | void batadv_update_min_mtu(struct net_device *soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 377 | { |
Marek Lindner | a19d3d8 | 2013-05-27 15:33:25 +0800 | [diff] [blame] | 378 | soft_iface->mtu = batadv_hardif_min_mtu(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 379 | |
Marek Lindner | a19d3d8 | 2013-05-27 15:33:25 +0800 | [diff] [blame] | 380 | /* Check if the local translate table should be cleaned up to match a |
| 381 | * new (and smaller) MTU. |
| 382 | */ |
| 383 | batadv_tt_local_resize_to_mtu(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 386 | static void |
| 387 | batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 388 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 389 | struct batadv_priv *bat_priv; |
| 390 | struct batadv_hard_iface *primary_if = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 391 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 392 | if (hard_iface->if_status != BATADV_IF_INACTIVE) |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 393 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 394 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 395 | bat_priv = netdev_priv(hard_iface->soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 396 | |
Marek Lindner | c322939 | 2012-03-11 06:17:50 +0800 | [diff] [blame] | 397 | bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface); |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 398 | hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 399 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 400 | /* the first active interface becomes our primary interface or |
Antonio Quartulli | 015758d | 2011-07-09 17:52:13 +0200 | [diff] [blame] | 401 | * the next active interface after the old primary interface was removed |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 402 | */ |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 403 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 404 | if (!primary_if) |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 405 | batadv_primary_if_select(bat_priv, hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 406 | |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 407 | batadv_info(hard_iface->soft_iface, "Interface activated: %s\n", |
| 408 | hard_iface->net_dev->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 409 | |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 410 | batadv_update_min_mtu(hard_iface->soft_iface); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 411 | |
Antonio Quartulli | b6cf5d4 | 2016-04-14 09:37:05 +0800 | [diff] [blame] | 412 | if (bat_priv->bat_algo_ops->bat_iface_activate) |
| 413 | bat_priv->bat_algo_ops->bat_iface_activate(hard_iface); |
| 414 | |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 415 | out: |
| 416 | if (primary_if) |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 417 | batadv_hardif_put(primary_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 420 | static void |
| 421 | batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 422 | { |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 423 | if ((hard_iface->if_status != BATADV_IF_ACTIVE) && |
| 424 | (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 425 | return; |
| 426 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 427 | hard_iface->if_status = BATADV_IF_INACTIVE; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 428 | |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 429 | batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n", |
| 430 | hard_iface->net_dev->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 431 | |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 432 | batadv_update_min_mtu(hard_iface->soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Antonio Quartulli | cb4b0d4 | 2013-02-16 14:42:39 +0100 | [diff] [blame] | 435 | /** |
| 436 | * batadv_master_del_slave - remove hard_iface from the current master interface |
| 437 | * @slave: the interface enslaved in another master |
| 438 | * @master: the master from which slave has to be removed |
| 439 | * |
| 440 | * Invoke ndo_del_slave on master passing slave as argument. In this way slave |
| 441 | * is free'd and master can correctly change its internal state. |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 442 | * |
| 443 | * Return: 0 on success, a negative value representing the error otherwise |
Antonio Quartulli | cb4b0d4 | 2013-02-16 14:42:39 +0100 | [diff] [blame] | 444 | */ |
| 445 | static int batadv_master_del_slave(struct batadv_hard_iface *slave, |
| 446 | struct net_device *master) |
| 447 | { |
| 448 | int ret; |
| 449 | |
| 450 | if (!master) |
| 451 | return 0; |
| 452 | |
| 453 | ret = -EBUSY; |
| 454 | if (master->netdev_ops->ndo_del_slave) |
| 455 | ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev); |
| 456 | |
| 457 | return ret; |
| 458 | } |
| 459 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 460 | int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface, |
Andrew Lunn | 2cd45a0 | 2016-04-21 12:57:27 +0200 | [diff] [blame] | 461 | struct net *net, const char *iface_name) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 462 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 463 | struct batadv_priv *bat_priv; |
Antonio Quartulli | cb4b0d4 | 2013-02-16 14:42:39 +0100 | [diff] [blame] | 464 | struct net_device *soft_iface, *master; |
Antonio Quartulli | 293e933 | 2013-05-19 12:55:16 +0200 | [diff] [blame] | 465 | __be16 ethertype = htons(ETH_P_BATMAN); |
Marek Lindner | 411d6ed | 2013-05-08 13:31:59 +0800 | [diff] [blame] | 466 | int max_header_len = batadv_max_header_len(); |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 467 | int ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 468 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 469 | if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 470 | goto out; |
| 471 | |
Sven Eckelmann | 17a8691 | 2016-04-11 13:06:40 +0200 | [diff] [blame] | 472 | kref_get(&hard_iface->refcount); |
Marek Lindner | ed75ccb | 2011-02-10 14:33:51 +0000 | [diff] [blame] | 473 | |
Andrew Lunn | 2cd45a0 | 2016-04-21 12:57:27 +0200 | [diff] [blame] | 474 | soft_iface = dev_get_by_name(net, iface_name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 475 | |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 476 | if (!soft_iface) { |
Andrew Lunn | 2cd45a0 | 2016-04-21 12:57:27 +0200 | [diff] [blame] | 477 | soft_iface = batadv_softif_create(net, iface_name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 478 | |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 479 | if (!soft_iface) { |
| 480 | ret = -ENOMEM; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 481 | goto err; |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 482 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 483 | |
| 484 | /* dev_get_by_name() increases the reference counter for us */ |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 485 | dev_hold(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Sven Eckelmann | 04b482a | 2012-05-12 02:09:38 +0200 | [diff] [blame] | 488 | if (!batadv_softif_is_valid(soft_iface)) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 489 | pr_err("Can't create batman mesh interface %s: already exists as regular interface\n", |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 490 | soft_iface->name); |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 491 | ret = -EINVAL; |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 492 | goto err_dev; |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Antonio Quartulli | cb4b0d4 | 2013-02-16 14:42:39 +0100 | [diff] [blame] | 495 | /* check if the interface is enslaved in another virtual one and |
| 496 | * in that case unlink it first |
| 497 | */ |
| 498 | master = netdev_master_upper_dev_get(hard_iface->net_dev); |
| 499 | ret = batadv_master_del_slave(hard_iface, master); |
| 500 | if (ret) |
| 501 | goto err_dev; |
| 502 | |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 503 | hard_iface->soft_iface = soft_iface; |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 504 | bat_priv = netdev_priv(hard_iface->soft_iface); |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 505 | |
Jiri Pirko | 6dffb04 | 2015-12-03 12:12:10 +0100 | [diff] [blame] | 506 | ret = netdev_master_upper_dev_link(hard_iface->net_dev, |
Jiri Pirko | 29bf24a | 2015-12-03 12:12:11 +0100 | [diff] [blame] | 507 | soft_iface, NULL, NULL); |
Sven Eckelmann | 3dbd550 | 2013-02-11 17:10:27 +0800 | [diff] [blame] | 508 | if (ret) |
| 509 | goto err_dev; |
| 510 | |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 511 | ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 512 | if (ret < 0) |
Sven Eckelmann | 3dbd550 | 2013-02-11 17:10:27 +0800 | [diff] [blame] | 513 | goto err_upper; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 514 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 515 | hard_iface->if_num = bat_priv->num_ifaces; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 516 | bat_priv->num_ifaces++; |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 517 | hard_iface->if_status = BATADV_IF_INACTIVE; |
Simon Wunderlich | 6244630 | 2012-07-01 22:51:55 +0200 | [diff] [blame] | 518 | ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces); |
| 519 | if (ret < 0) { |
| 520 | bat_priv->bat_algo_ops->bat_iface_disable(hard_iface); |
| 521 | bat_priv->num_ifaces--; |
| 522 | hard_iface->if_status = BATADV_IF_NOT_IN_USE; |
Sven Eckelmann | 3dbd550 | 2013-02-11 17:10:27 +0800 | [diff] [blame] | 523 | goto err_upper; |
Simon Wunderlich | 6244630 | 2012-07-01 22:51:55 +0200 | [diff] [blame] | 524 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 525 | |
Sven Eckelmann | d7d6de9 | 2016-03-05 16:09:18 +0100 | [diff] [blame] | 526 | kref_get(&hard_iface->refcount); |
Sven Eckelmann | 7e071c7 | 2012-06-03 22:19:13 +0200 | [diff] [blame] | 527 | hard_iface->batman_adv_ptype.type = ethertype; |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 528 | hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv; |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 529 | hard_iface->batman_adv_ptype.dev = hard_iface->net_dev; |
| 530 | dev_add_pack(&hard_iface->batman_adv_ptype); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 531 | |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 532 | batadv_info(hard_iface->soft_iface, "Adding interface: %s\n", |
| 533 | hard_iface->net_dev->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 534 | |
Sven Eckelmann | 0aca236 | 2012-06-19 20:26:30 +0200 | [diff] [blame] | 535 | if (atomic_read(&bat_priv->fragmentation) && |
Marek Lindner | 411d6ed | 2013-05-08 13:31:59 +0800 | [diff] [blame] | 536 | hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len) |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 537 | batadv_info(hard_iface->soft_iface, |
Marek Lindner | 411d6ed | 2013-05-08 13:31:59 +0800 | [diff] [blame] | 538 | "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. Packets going over this interface will be fragmented on layer2 which could impact the performance. Setting the MTU to %i would solve the problem.\n", |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 539 | hard_iface->net_dev->name, hard_iface->net_dev->mtu, |
Marek Lindner | 411d6ed | 2013-05-08 13:31:59 +0800 | [diff] [blame] | 540 | ETH_DATA_LEN + max_header_len); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 541 | |
Sven Eckelmann | 0aca236 | 2012-06-19 20:26:30 +0200 | [diff] [blame] | 542 | if (!atomic_read(&bat_priv->fragmentation) && |
Marek Lindner | 411d6ed | 2013-05-08 13:31:59 +0800 | [diff] [blame] | 543 | hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len) |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 544 | batadv_info(hard_iface->soft_iface, |
Marek Lindner | 411d6ed | 2013-05-08 13:31:59 +0800 | [diff] [blame] | 545 | "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. If you experience problems getting traffic through try increasing the MTU to %i.\n", |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 546 | hard_iface->net_dev->name, hard_iface->net_dev->mtu, |
Marek Lindner | 411d6ed | 2013-05-08 13:31:59 +0800 | [diff] [blame] | 547 | ETH_DATA_LEN + max_header_len); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 548 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 549 | if (batadv_hardif_is_iface_up(hard_iface)) |
| 550 | batadv_hardif_activate_interface(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 551 | else |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 552 | batadv_err(hard_iface->soft_iface, |
| 553 | "Not using interface %s (retrying later): interface not active\n", |
| 554 | hard_iface->net_dev->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 555 | |
Sven Eckelmann | 7bca68c | 2015-08-07 19:28:42 +0200 | [diff] [blame] | 556 | batadv_hardif_recalc_extra_skbroom(soft_iface); |
| 557 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 558 | out: |
| 559 | return 0; |
| 560 | |
Sven Eckelmann | 3dbd550 | 2013-02-11 17:10:27 +0800 | [diff] [blame] | 561 | err_upper: |
| 562 | netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface); |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 563 | err_dev: |
Sven Eckelmann | 3dbd550 | 2013-02-11 17:10:27 +0800 | [diff] [blame] | 564 | hard_iface->soft_iface = NULL; |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 565 | dev_put(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 566 | err: |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 567 | batadv_hardif_put(hard_iface); |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 568 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Sven Eckelmann | a15fd36 | 2013-02-11 17:10:24 +0800 | [diff] [blame] | 571 | void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface, |
| 572 | enum batadv_hard_if_cleanup autodel) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 573 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 574 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
| 575 | struct batadv_hard_iface *primary_if = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 576 | |
Sven Eckelmann | f2d2386 | 2016-03-19 13:55:21 +0100 | [diff] [blame] | 577 | batadv_hardif_deactivate_interface(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 578 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 579 | if (hard_iface->if_status != BATADV_IF_INACTIVE) |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 580 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 581 | |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 582 | batadv_info(hard_iface->soft_iface, "Removing interface: %s\n", |
| 583 | hard_iface->net_dev->name); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 584 | dev_remove_pack(&hard_iface->batman_adv_ptype); |
Sven Eckelmann | d7d6de9 | 2016-03-05 16:09:18 +0100 | [diff] [blame] | 585 | batadv_hardif_put(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 586 | |
| 587 | bat_priv->num_ifaces--; |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 588 | batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 589 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 590 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 591 | if (hard_iface == primary_if) { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 592 | struct batadv_hard_iface *new_if; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 593 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 594 | new_if = batadv_hardif_get_active(hard_iface->soft_iface); |
| 595 | batadv_primary_if_select(bat_priv, new_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 596 | |
| 597 | if (new_if) |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 598 | batadv_hardif_put(new_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Marek Lindner | 00a5007 | 2012-02-07 17:20:47 +0800 | [diff] [blame] | 601 | bat_priv->bat_algo_ops->bat_iface_disable(hard_iface); |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 602 | hard_iface->if_status = BATADV_IF_NOT_IN_USE; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 603 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 604 | /* delete all references to this hard_iface */ |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 605 | batadv_purge_orig_ref(bat_priv); |
Sven Eckelmann | 9455e34 | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 606 | batadv_purge_outstanding_packets(bat_priv, hard_iface); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 607 | dev_put(hard_iface->soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 608 | |
Antonio Quartulli | a5256f7 | 2015-08-04 22:26:19 +0200 | [diff] [blame] | 609 | netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface); |
Sven Eckelmann | 7bca68c | 2015-08-07 19:28:42 +0200 | [diff] [blame] | 610 | batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface); |
Antonio Quartulli | a5256f7 | 2015-08-04 22:26:19 +0200 | [diff] [blame] | 611 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 612 | /* nobody uses this interface anymore */ |
Antonio Quartulli | 8257f55 | 2013-08-19 18:39:59 +0200 | [diff] [blame] | 613 | if (!bat_priv->num_ifaces) { |
| 614 | batadv_gw_check_client_stop(bat_priv); |
| 615 | |
| 616 | if (autodel == BATADV_IF_CLEANUP_AUTO) |
| 617 | batadv_softif_destroy_sysfs(hard_iface->soft_iface); |
| 618 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 619 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 620 | hard_iface->soft_iface = NULL; |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 621 | batadv_hardif_put(hard_iface); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 622 | |
| 623 | out: |
| 624 | if (primary_if) |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 625 | batadv_hardif_put(primary_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Simon Wunderlich | 5bc44dc | 2013-01-11 10:19:51 +0100 | [diff] [blame] | 628 | /** |
| 629 | * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif |
| 630 | * @work: work queue item |
| 631 | * |
| 632 | * Free the parts of the hard interface which can not be removed under |
| 633 | * rtnl lock (to prevent deadlock situations). |
| 634 | */ |
| 635 | static void batadv_hardif_remove_interface_finish(struct work_struct *work) |
| 636 | { |
| 637 | struct batadv_hard_iface *hard_iface; |
| 638 | |
| 639 | hard_iface = container_of(work, struct batadv_hard_iface, |
| 640 | cleanup_work); |
| 641 | |
Simon Wunderlich | 5bc7c1e | 2013-11-21 14:16:12 +0100 | [diff] [blame] | 642 | batadv_debugfs_del_hardif(hard_iface); |
Simon Wunderlich | 5bc44dc | 2013-01-11 10:19:51 +0100 | [diff] [blame] | 643 | batadv_sysfs_del_hardif(&hard_iface->hardif_obj); |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 644 | batadv_hardif_put(hard_iface); |
Simon Wunderlich | 5bc44dc | 2013-01-11 10:19:51 +0100 | [diff] [blame] | 645 | } |
| 646 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 647 | static struct batadv_hard_iface * |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 648 | batadv_hardif_add_interface(struct net_device *net_dev) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 649 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 650 | struct batadv_hard_iface *hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 651 | int ret; |
| 652 | |
Sven Eckelmann | c3caf51 | 2011-05-03 11:51:38 +0200 | [diff] [blame] | 653 | ASSERT_RTNL(); |
| 654 | |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 655 | if (!batadv_is_valid_iface(net_dev)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 656 | goto out; |
| 657 | |
| 658 | dev_hold(net_dev); |
| 659 | |
Antonio Quartulli | 7db3fc2 | 2013-04-02 12:16:53 +0200 | [diff] [blame] | 660 | hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC); |
Joe Perches | 320f422 | 2011-08-29 14:17:24 -0700 | [diff] [blame] | 661 | if (!hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 662 | goto release_dev; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 663 | |
Sven Eckelmann | 5853e22 | 2012-05-12 02:09:24 +0200 | [diff] [blame] | 664 | ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 665 | if (ret) |
| 666 | goto free_if; |
| 667 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 668 | hard_iface->if_num = -1; |
| 669 | hard_iface->net_dev = net_dev; |
| 670 | hard_iface->soft_iface = NULL; |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 671 | hard_iface->if_status = BATADV_IF_NOT_IN_USE; |
Simon Wunderlich | 5bc7c1e | 2013-11-21 14:16:12 +0100 | [diff] [blame] | 672 | |
| 673 | ret = batadv_debugfs_add_hardif(hard_iface); |
| 674 | if (ret) |
| 675 | goto free_sysfs; |
| 676 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 677 | INIT_LIST_HEAD(&hard_iface->list); |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 678 | INIT_HLIST_HEAD(&hard_iface->neigh_list); |
Simon Wunderlich | 5bc44dc | 2013-01-11 10:19:51 +0100 | [diff] [blame] | 679 | INIT_WORK(&hard_iface->cleanup_work, |
| 680 | batadv_hardif_remove_interface_finish); |
| 681 | |
Marek Lindner | cef6341 | 2015-08-04 21:09:55 +0800 | [diff] [blame] | 682 | spin_lock_init(&hard_iface->neigh_list_lock); |
| 683 | |
Matthias Schiffer | caf65bf | 2013-03-09 23:14:23 +0100 | [diff] [blame] | 684 | hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT; |
| 685 | if (batadv_is_wifi_netdev(net_dev)) |
| 686 | hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS; |
| 687 | |
Marek Lindner | 7db682d | 2016-05-10 22:31:59 +0800 | [diff] [blame] | 688 | batadv_v_hardif_init(hard_iface); |
| 689 | |
Marek Lindner | ed75ccb | 2011-02-10 14:33:51 +0000 | [diff] [blame] | 690 | /* extra reference for return */ |
Sven Eckelmann | 7a659d5 | 2016-01-16 10:29:54 +0100 | [diff] [blame] | 691 | kref_init(&hard_iface->refcount); |
| 692 | kref_get(&hard_iface->refcount); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 693 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 694 | batadv_check_known_mac_addr(hard_iface->net_dev); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 695 | list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 696 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 697 | return hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 698 | |
Simon Wunderlich | 5bc7c1e | 2013-11-21 14:16:12 +0100 | [diff] [blame] | 699 | free_sysfs: |
| 700 | batadv_sysfs_del_hardif(&hard_iface->hardif_obj); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 701 | free_if: |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 702 | kfree(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 703 | release_dev: |
| 704 | dev_put(net_dev); |
| 705 | out: |
| 706 | return NULL; |
| 707 | } |
| 708 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 709 | static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 710 | { |
Sven Eckelmann | c3caf51 | 2011-05-03 11:51:38 +0200 | [diff] [blame] | 711 | ASSERT_RTNL(); |
| 712 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 713 | /* first deactivate interface */ |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 714 | if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) |
Sven Eckelmann | a15fd36 | 2013-02-11 17:10:24 +0800 | [diff] [blame] | 715 | batadv_hardif_disable_interface(hard_iface, |
| 716 | BATADV_IF_CLEANUP_AUTO); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 717 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 718 | if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 719 | return; |
| 720 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 721 | hard_iface->if_status = BATADV_IF_TO_BE_REMOVED; |
Simon Wunderlich | 5bc44dc | 2013-01-11 10:19:51 +0100 | [diff] [blame] | 722 | queue_work(batadv_event_workqueue, &hard_iface->cleanup_work); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 725 | void batadv_hardif_remove_interfaces(void) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 726 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 727 | struct batadv_hard_iface *hard_iface, *hard_iface_tmp; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 728 | |
Sven Eckelmann | c3caf51 | 2011-05-03 11:51:38 +0200 | [diff] [blame] | 729 | rtnl_lock(); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 730 | list_for_each_entry_safe(hard_iface, hard_iface_tmp, |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 731 | &batadv_hardif_list, list) { |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 732 | list_del_rcu(&hard_iface->list); |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 733 | batadv_hardif_remove_interface(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 734 | } |
| 735 | rtnl_unlock(); |
| 736 | } |
| 737 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 738 | static int batadv_hard_if_event(struct notifier_block *this, |
| 739 | unsigned long event, void *ptr) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 740 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 741 | struct net_device *net_dev = netdev_notifier_info_to_dev(ptr); |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 742 | struct batadv_hard_iface *hard_iface; |
| 743 | struct batadv_hard_iface *primary_if = NULL; |
| 744 | struct batadv_priv *bat_priv; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 745 | |
Sven Eckelmann | 3713029 | 2013-02-11 17:10:22 +0800 | [diff] [blame] | 746 | if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) { |
| 747 | batadv_sysfs_add_meshif(net_dev); |
Antonio Quartulli | 5d2c05b | 2013-07-02 11:04:34 +0200 | [diff] [blame] | 748 | bat_priv = netdev_priv(net_dev); |
| 749 | batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS); |
Sven Eckelmann | 3713029 | 2013-02-11 17:10:22 +0800 | [diff] [blame] | 750 | return NOTIFY_DONE; |
| 751 | } |
| 752 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 753 | hard_iface = batadv_hardif_get_by_netdev(net_dev); |
Andrew Lunn | a1a66b1 | 2015-12-03 21:12:33 +0100 | [diff] [blame] | 754 | if (!hard_iface && (event == NETDEV_REGISTER || |
| 755 | event == NETDEV_POST_TYPE_CHANGE)) |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 756 | hard_iface = batadv_hardif_add_interface(net_dev); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 757 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 758 | if (!hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 759 | goto out; |
| 760 | |
| 761 | switch (event) { |
| 762 | case NETDEV_UP: |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 763 | batadv_hardif_activate_interface(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 764 | break; |
| 765 | case NETDEV_GOING_DOWN: |
| 766 | case NETDEV_DOWN: |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 767 | batadv_hardif_deactivate_interface(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 768 | break; |
| 769 | case NETDEV_UNREGISTER: |
Andrew Lunn | a1a66b1 | 2015-12-03 21:12:33 +0100 | [diff] [blame] | 770 | case NETDEV_PRE_TYPE_CHANGE: |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 771 | list_del_rcu(&hard_iface->list); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 772 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 773 | batadv_hardif_remove_interface(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 774 | break; |
| 775 | case NETDEV_CHANGEMTU: |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 776 | if (hard_iface->soft_iface) |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 777 | batadv_update_min_mtu(hard_iface->soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 778 | break; |
| 779 | case NETDEV_CHANGEADDR: |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 780 | if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 781 | goto hardif_put; |
| 782 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 783 | batadv_check_known_mac_addr(hard_iface->net_dev); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 784 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 785 | bat_priv = netdev_priv(hard_iface->soft_iface); |
Marek Lindner | c322939 | 2012-03-11 06:17:50 +0800 | [diff] [blame] | 786 | bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface); |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 787 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 788 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 789 | if (!primary_if) |
| 790 | goto hardif_put; |
| 791 | |
| 792 | if (hard_iface == primary_if) |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 793 | batadv_primary_if_update_addr(bat_priv, NULL); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 794 | break; |
| 795 | default: |
| 796 | break; |
Joe Perches | f81c622 | 2011-06-03 11:51:19 +0000 | [diff] [blame] | 797 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 798 | |
| 799 | hardif_put: |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 800 | batadv_hardif_put(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 801 | out: |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 802 | if (primary_if) |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 803 | batadv_hardif_put(primary_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 804 | return NOTIFY_DONE; |
| 805 | } |
| 806 | |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 807 | struct notifier_block batadv_hard_if_notifier = { |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 808 | .notifier_call = batadv_hard_if_event, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 809 | }; |