Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1 | /* Copyright (C) 2007-2012 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 |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 | * 02110-1301, USA |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "main.h" |
Antonio Quartulli | 785ea11 | 2011-11-23 11:35:44 +0100 | [diff] [blame] | 21 | #include "distributed-arp-table.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 22 | #include "hard-interface.h" |
| 23 | #include "soft-interface.h" |
| 24 | #include "send.h" |
| 25 | #include "translation-table.h" |
| 26 | #include "routing.h" |
Sven Eckelmann | b706b13 | 2012-06-10 23:58:51 +0200 | [diff] [blame] | 27 | #include "sysfs.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 28 | #include "originator.h" |
| 29 | #include "hash.h" |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 30 | #include "bridge_loop_avoidance.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 31 | |
| 32 | #include <linux/if_arp.h> |
| 33 | |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 34 | void batadv_hardif_free_rcu(struct rcu_head *rcu) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 35 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 36 | struct batadv_hard_iface *hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 37 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 38 | hard_iface = container_of(rcu, struct batadv_hard_iface, rcu); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 39 | dev_put(hard_iface->net_dev); |
| 40 | kfree(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 43 | struct batadv_hard_iface * |
| 44 | batadv_hardif_get_by_netdev(const struct net_device *net_dev) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 45 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 46 | struct batadv_hard_iface *hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 47 | |
| 48 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 49 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 50 | if (hard_iface->net_dev == net_dev && |
| 51 | atomic_inc_not_zero(&hard_iface->refcount)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 52 | goto out; |
| 53 | } |
| 54 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 55 | hard_iface = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 56 | |
| 57 | out: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 58 | rcu_read_unlock(); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 59 | return hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Antonio Quartulli | b7eddd0 | 2012-09-09 10:46:46 +0200 | [diff] [blame^] | 62 | /** |
| 63 | * batadv_is_on_batman_iface - check if a device is a batman iface descendant |
| 64 | * @net_dev: the device to check |
| 65 | * |
| 66 | * If the user creates any virtual device on top of a batman-adv interface, it |
| 67 | * is important to prevent this new interface to be used to create a new mesh |
| 68 | * network (this behaviour would lead to a batman-over-batman configuration). |
| 69 | * This function recursively checks all the fathers of the device passed as |
| 70 | * argument looking for a batman-adv soft interface. |
| 71 | * |
| 72 | * Returns true if the device is descendant of a batman-adv mesh interface (or |
| 73 | * if it is a batman-adv interface itself), false otherwise |
| 74 | */ |
| 75 | static bool batadv_is_on_batman_iface(const struct net_device *net_dev) |
| 76 | { |
| 77 | struct net_device *parent_dev; |
| 78 | bool ret; |
| 79 | |
| 80 | /* check if this is a batman-adv mesh interface */ |
| 81 | if (batadv_softif_is_valid(net_dev)) |
| 82 | return true; |
| 83 | |
| 84 | /* no more parents..stop recursion */ |
| 85 | if (net_dev->iflink == net_dev->ifindex) |
| 86 | return false; |
| 87 | |
| 88 | /* recurse over the parent device */ |
| 89 | parent_dev = dev_get_by_index(&init_net, net_dev->iflink); |
| 90 | /* if we got a NULL parent_dev there is something broken.. */ |
| 91 | if (WARN(!parent_dev, "Cannot find parent device")) |
| 92 | return false; |
| 93 | |
| 94 | ret = batadv_is_on_batman_iface(parent_dev); |
| 95 | |
| 96 | if (parent_dev) |
| 97 | dev_put(parent_dev); |
| 98 | return ret; |
| 99 | } |
| 100 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 101 | static int batadv_is_valid_iface(const struct net_device *net_dev) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 102 | { |
| 103 | if (net_dev->flags & IFF_LOOPBACK) |
| 104 | return 0; |
| 105 | |
| 106 | if (net_dev->type != ARPHRD_ETHER) |
| 107 | return 0; |
| 108 | |
| 109 | if (net_dev->addr_len != ETH_ALEN) |
| 110 | return 0; |
| 111 | |
| 112 | /* no batman over batman */ |
Antonio Quartulli | b7eddd0 | 2012-09-09 10:46:46 +0200 | [diff] [blame^] | 113 | if (batadv_is_on_batman_iface(net_dev)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 114 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 115 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 116 | return 1; |
| 117 | } |
| 118 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 119 | static struct batadv_hard_iface * |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 120 | batadv_hardif_get_active(const struct net_device *soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 121 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 122 | struct batadv_hard_iface *hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 123 | |
| 124 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 125 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 126 | if (hard_iface->soft_iface != soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 127 | continue; |
| 128 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 129 | if (hard_iface->if_status == BATADV_IF_ACTIVE && |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 130 | atomic_inc_not_zero(&hard_iface->refcount)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 131 | goto out; |
| 132 | } |
| 133 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 134 | hard_iface = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 135 | |
| 136 | out: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 137 | rcu_read_unlock(); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 138 | return hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 141 | static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv, |
| 142 | struct batadv_hard_iface *oldif) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 143 | { |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 144 | struct batadv_vis_packet *vis_packet; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 145 | struct batadv_hard_iface *primary_if; |
Sven Eckelmann | 807736f | 2012-07-15 22:26:51 +0200 | [diff] [blame] | 146 | struct sk_buff *skb; |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 147 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 148 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 149 | if (!primary_if) |
| 150 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 151 | |
Antonio Quartulli | 785ea11 | 2011-11-23 11:35:44 +0100 | [diff] [blame] | 152 | batadv_dat_init_own_addr(bat_priv, primary_if); |
| 153 | |
Sven Eckelmann | 807736f | 2012-07-15 22:26:51 +0200 | [diff] [blame] | 154 | skb = bat_priv->vis.my_info->skb_packet; |
| 155 | vis_packet = (struct batadv_vis_packet *)skb->data; |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 156 | memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 157 | memcpy(vis_packet->sender_orig, |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 158 | primary_if->net_dev->dev_addr, ETH_ALEN); |
| 159 | |
Sven Eckelmann | 08adf15 | 2012-05-12 13:38:47 +0200 | [diff] [blame] | 160 | batadv_bla_update_orig_address(bat_priv, primary_if, oldif); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 161 | out: |
| 162 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 163 | batadv_hardif_free_ref(primary_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 166 | static void batadv_primary_if_select(struct batadv_priv *bat_priv, |
| 167 | struct batadv_hard_iface *new_hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 168 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 169 | struct batadv_hard_iface *curr_hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 170 | |
Sven Eckelmann | c3caf51 | 2011-05-03 11:51:38 +0200 | [diff] [blame] | 171 | ASSERT_RTNL(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 172 | |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 173 | if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount)) |
| 174 | new_hard_iface = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 175 | |
Sven Eckelmann | 728cbc6 | 2011-05-15 00:50:21 +0200 | [diff] [blame] | 176 | curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 177 | rcu_assign_pointer(bat_priv->primary_if, new_hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 178 | |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 179 | if (!new_hard_iface) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 180 | goto out; |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 181 | |
Marek Lindner | cd8b78e | 2012-02-07 17:20:49 +0800 | [diff] [blame] | 182 | bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface); |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 183 | batadv_primary_if_update_addr(bat_priv, curr_hard_iface); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 184 | |
| 185 | out: |
| 186 | if (curr_hard_iface) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 187 | batadv_hardif_free_ref(curr_hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 190 | static bool |
| 191 | batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 192 | { |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 193 | if (hard_iface->net_dev->flags & IFF_UP) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 194 | return true; |
| 195 | |
| 196 | return false; |
| 197 | } |
| 198 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 199 | 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] | 200 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 201 | const struct batadv_hard_iface *hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 202 | |
| 203 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 204 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 205 | if ((hard_iface->if_status != BATADV_IF_ACTIVE) && |
| 206 | (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 207 | continue; |
| 208 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 209 | if (hard_iface->net_dev == net_dev) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 210 | continue; |
| 211 | |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 212 | if (!batadv_compare_eth(hard_iface->net_dev->dev_addr, |
| 213 | net_dev->dev_addr)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 214 | continue; |
| 215 | |
Sven Eckelmann | 6796958 | 2012-03-26 16:22:45 +0200 | [diff] [blame] | 216 | pr_warn("The newly added mac address (%pM) already exists on: %s\n", |
| 217 | net_dev->dev_addr, hard_iface->net_dev->name); |
| 218 | 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] | 219 | } |
| 220 | rcu_read_unlock(); |
| 221 | } |
| 222 | |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 223 | int batadv_hardif_min_mtu(struct net_device *soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 224 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 225 | const struct batadv_priv *bat_priv = netdev_priv(soft_iface); |
| 226 | const struct batadv_hard_iface *hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 227 | /* allow big frames if all devices are capable to do so |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 228 | * (have MTU > 1500 + BAT_HEADER_LEN) |
| 229 | */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 230 | int min_mtu = ETH_DATA_LEN; |
| 231 | |
| 232 | if (atomic_read(&bat_priv->fragmentation)) |
| 233 | goto out; |
| 234 | |
| 235 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 236 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 237 | if ((hard_iface->if_status != BATADV_IF_ACTIVE) && |
| 238 | (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 239 | continue; |
| 240 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 241 | if (hard_iface->soft_iface != soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 242 | continue; |
| 243 | |
Sven Eckelmann | c11fdfa | 2012-06-03 22:19:14 +0200 | [diff] [blame] | 244 | min_mtu = min_t(int, |
| 245 | hard_iface->net_dev->mtu - BATADV_HEADER_LEN, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 246 | min_mtu); |
| 247 | } |
| 248 | rcu_read_unlock(); |
| 249 | out: |
| 250 | return min_mtu; |
| 251 | } |
| 252 | |
| 253 | /* adjusts the MTU if a new interface with a smaller MTU appeared. */ |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 254 | void batadv_update_min_mtu(struct net_device *soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 255 | { |
| 256 | int min_mtu; |
| 257 | |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 258 | min_mtu = batadv_hardif_min_mtu(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 259 | if (soft_iface->mtu != min_mtu) |
| 260 | soft_iface->mtu = min_mtu; |
| 261 | } |
| 262 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 263 | static void |
| 264 | batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 265 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 266 | struct batadv_priv *bat_priv; |
| 267 | struct batadv_hard_iface *primary_if = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 268 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 269 | if (hard_iface->if_status != BATADV_IF_INACTIVE) |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 270 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 271 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 272 | bat_priv = netdev_priv(hard_iface->soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 273 | |
Marek Lindner | c322939 | 2012-03-11 06:17:50 +0800 | [diff] [blame] | 274 | bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface); |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 275 | hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 276 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 277 | /* the first active interface becomes our primary interface or |
Antonio Quartulli | 015758d | 2011-07-09 17:52:13 +0200 | [diff] [blame] | 278 | * the next active interface after the old primary interface was removed |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 279 | */ |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 280 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 281 | if (!primary_if) |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 282 | batadv_primary_if_select(bat_priv, hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 283 | |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 284 | batadv_info(hard_iface->soft_iface, "Interface activated: %s\n", |
| 285 | hard_iface->net_dev->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 286 | |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 287 | batadv_update_min_mtu(hard_iface->soft_iface); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 288 | |
| 289 | out: |
| 290 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 291 | batadv_hardif_free_ref(primary_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 294 | static void |
| 295 | batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 296 | { |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 297 | if ((hard_iface->if_status != BATADV_IF_ACTIVE) && |
| 298 | (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 299 | return; |
| 300 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 301 | hard_iface->if_status = BATADV_IF_INACTIVE; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 302 | |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 303 | batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n", |
| 304 | hard_iface->net_dev->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 305 | |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 306 | batadv_update_min_mtu(hard_iface->soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 309 | int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface, |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 310 | const char *iface_name) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 311 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 312 | struct batadv_priv *bat_priv; |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 313 | struct net_device *soft_iface; |
Sven Eckelmann | 7e071c7 | 2012-06-03 22:19:13 +0200 | [diff] [blame] | 314 | __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN); |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 315 | int ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 316 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 317 | if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 318 | goto out; |
| 319 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 320 | if (!atomic_inc_not_zero(&hard_iface->refcount)) |
Marek Lindner | ed75ccb | 2011-02-10 14:33:51 +0000 | [diff] [blame] | 321 | goto out; |
| 322 | |
Marek Lindner | 6e242f9 | 2011-12-07 18:02:50 +0800 | [diff] [blame] | 323 | /* hard-interface is part of a bridge */ |
| 324 | if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT) |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 325 | pr_err("You are about to enable batman-adv on '%s' which already is part of a bridge. Unless you know exactly what you are doing this is probably wrong and won't work the way you think it would.\n", |
Marek Lindner | 6e242f9 | 2011-12-07 18:02:50 +0800 | [diff] [blame] | 326 | hard_iface->net_dev->name); |
| 327 | |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 328 | soft_iface = dev_get_by_name(&init_net, iface_name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 329 | |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 330 | if (!soft_iface) { |
Sven Eckelmann | 04b482a | 2012-05-12 02:09:38 +0200 | [diff] [blame] | 331 | soft_iface = batadv_softif_create(iface_name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 332 | |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 333 | if (!soft_iface) { |
| 334 | ret = -ENOMEM; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 335 | goto err; |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 336 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 337 | |
| 338 | /* dev_get_by_name() increases the reference counter for us */ |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 339 | dev_hold(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Sven Eckelmann | 04b482a | 2012-05-12 02:09:38 +0200 | [diff] [blame] | 342 | if (!batadv_softif_is_valid(soft_iface)) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 343 | 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] | 344 | soft_iface->name); |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 345 | ret = -EINVAL; |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 346 | goto err_dev; |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | hard_iface->soft_iface = soft_iface; |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 350 | bat_priv = netdev_priv(hard_iface->soft_iface); |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 351 | |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 352 | ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 353 | if (ret < 0) |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 354 | goto err_dev; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 355 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 356 | hard_iface->if_num = bat_priv->num_ifaces; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 357 | bat_priv->num_ifaces++; |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 358 | hard_iface->if_status = BATADV_IF_INACTIVE; |
Simon Wunderlich | 6244630 | 2012-07-01 22:51:55 +0200 | [diff] [blame] | 359 | ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces); |
| 360 | if (ret < 0) { |
| 361 | bat_priv->bat_algo_ops->bat_iface_disable(hard_iface); |
| 362 | bat_priv->num_ifaces--; |
| 363 | hard_iface->if_status = BATADV_IF_NOT_IN_USE; |
| 364 | goto err_dev; |
| 365 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 366 | |
Sven Eckelmann | 7e071c7 | 2012-06-03 22:19:13 +0200 | [diff] [blame] | 367 | hard_iface->batman_adv_ptype.type = ethertype; |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 368 | hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv; |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 369 | hard_iface->batman_adv_ptype.dev = hard_iface->net_dev; |
| 370 | dev_add_pack(&hard_iface->batman_adv_ptype); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 371 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 372 | atomic_set(&hard_iface->frag_seqno, 1); |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 373 | batadv_info(hard_iface->soft_iface, "Adding interface: %s\n", |
| 374 | hard_iface->net_dev->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 375 | |
Sven Eckelmann | 0aca236 | 2012-06-19 20:26:30 +0200 | [diff] [blame] | 376 | if (atomic_read(&bat_priv->fragmentation) && |
| 377 | hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN) |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 378 | batadv_info(hard_iface->soft_iface, |
| 379 | "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 %zi would solve the problem.\n", |
| 380 | hard_iface->net_dev->name, hard_iface->net_dev->mtu, |
Sven Eckelmann | c11fdfa | 2012-06-03 22:19:14 +0200 | [diff] [blame] | 381 | ETH_DATA_LEN + BATADV_HEADER_LEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 382 | |
Sven Eckelmann | 0aca236 | 2012-06-19 20:26:30 +0200 | [diff] [blame] | 383 | if (!atomic_read(&bat_priv->fragmentation) && |
| 384 | hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN) |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 385 | batadv_info(hard_iface->soft_iface, |
| 386 | "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 %zi.\n", |
| 387 | hard_iface->net_dev->name, hard_iface->net_dev->mtu, |
Sven Eckelmann | c11fdfa | 2012-06-03 22:19:14 +0200 | [diff] [blame] | 388 | ETH_DATA_LEN + BATADV_HEADER_LEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 389 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 390 | if (batadv_hardif_is_iface_up(hard_iface)) |
| 391 | batadv_hardif_activate_interface(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 392 | else |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 393 | batadv_err(hard_iface->soft_iface, |
| 394 | "Not using interface %s (retrying later): interface not active\n", |
| 395 | hard_iface->net_dev->name); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 396 | |
| 397 | /* begin scheduling originator messages on that interface */ |
Sven Eckelmann | 9455e34 | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 398 | batadv_schedule_bat_ogm(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 399 | |
| 400 | out: |
| 401 | return 0; |
| 402 | |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 403 | err_dev: |
| 404 | dev_put(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 405 | err: |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 406 | batadv_hardif_free_ref(hard_iface); |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 407 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 410 | void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 411 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 412 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
| 413 | struct batadv_hard_iface *primary_if = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 414 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 415 | if (hard_iface->if_status == BATADV_IF_ACTIVE) |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 416 | batadv_hardif_deactivate_interface(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 417 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 418 | if (hard_iface->if_status != BATADV_IF_INACTIVE) |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 419 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 420 | |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 421 | batadv_info(hard_iface->soft_iface, "Removing interface: %s\n", |
| 422 | hard_iface->net_dev->name); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 423 | dev_remove_pack(&hard_iface->batman_adv_ptype); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 424 | |
| 425 | bat_priv->num_ifaces--; |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 426 | batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 427 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 428 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 429 | if (hard_iface == primary_if) { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 430 | struct batadv_hard_iface *new_if; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 431 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 432 | new_if = batadv_hardif_get_active(hard_iface->soft_iface); |
| 433 | batadv_primary_if_select(bat_priv, new_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 434 | |
| 435 | if (new_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 436 | batadv_hardif_free_ref(new_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Marek Lindner | 00a5007 | 2012-02-07 17:20:47 +0800 | [diff] [blame] | 439 | bat_priv->bat_algo_ops->bat_iface_disable(hard_iface); |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 440 | hard_iface->if_status = BATADV_IF_NOT_IN_USE; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 441 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 442 | /* delete all references to this hard_iface */ |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 443 | batadv_purge_orig_ref(bat_priv); |
Sven Eckelmann | 9455e34 | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 444 | batadv_purge_outstanding_packets(bat_priv, hard_iface); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 445 | dev_put(hard_iface->soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 446 | |
| 447 | /* nobody uses this interface anymore */ |
| 448 | if (!bat_priv->num_ifaces) |
Sven Eckelmann | 04b482a | 2012-05-12 02:09:38 +0200 | [diff] [blame] | 449 | batadv_softif_destroy(hard_iface->soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 450 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 451 | hard_iface->soft_iface = NULL; |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 452 | batadv_hardif_free_ref(hard_iface); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 453 | |
| 454 | out: |
| 455 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 456 | batadv_hardif_free_ref(primary_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 459 | static struct batadv_hard_iface * |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 460 | batadv_hardif_add_interface(struct net_device *net_dev) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 461 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 462 | struct batadv_hard_iface *hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 463 | int ret; |
| 464 | |
Sven Eckelmann | c3caf51 | 2011-05-03 11:51:38 +0200 | [diff] [blame] | 465 | ASSERT_RTNL(); |
| 466 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 467 | ret = batadv_is_valid_iface(net_dev); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 468 | if (ret != 1) |
| 469 | goto out; |
| 470 | |
| 471 | dev_hold(net_dev); |
| 472 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 473 | hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC); |
Joe Perches | 320f422 | 2011-08-29 14:17:24 -0700 | [diff] [blame] | 474 | if (!hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 475 | goto release_dev; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 476 | |
Sven Eckelmann | 5853e22 | 2012-05-12 02:09:24 +0200 | [diff] [blame] | 477 | ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 478 | if (ret) |
| 479 | goto free_if; |
| 480 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 481 | hard_iface->if_num = -1; |
| 482 | hard_iface->net_dev = net_dev; |
| 483 | hard_iface->soft_iface = NULL; |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 484 | hard_iface->if_status = BATADV_IF_NOT_IN_USE; |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 485 | INIT_LIST_HEAD(&hard_iface->list); |
Marek Lindner | ed75ccb | 2011-02-10 14:33:51 +0000 | [diff] [blame] | 486 | /* extra reference for return */ |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 487 | atomic_set(&hard_iface->refcount, 2); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 488 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 489 | batadv_check_known_mac_addr(hard_iface->net_dev); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 490 | list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 491 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 492 | /* This can't be called via a bat_priv callback because |
Marek Lindner | 8140625 | 2012-02-07 17:19:58 +0800 | [diff] [blame] | 493 | * we have no bat_priv yet. |
| 494 | */ |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 495 | atomic_set(&hard_iface->bat_iv.ogm_seqno, 1); |
| 496 | hard_iface->bat_iv.ogm_buff = NULL; |
Marek Lindner | 8140625 | 2012-02-07 17:19:58 +0800 | [diff] [blame] | 497 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 498 | return hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 499 | |
| 500 | free_if: |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 501 | kfree(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 502 | release_dev: |
| 503 | dev_put(net_dev); |
| 504 | out: |
| 505 | return NULL; |
| 506 | } |
| 507 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 508 | static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 509 | { |
Sven Eckelmann | c3caf51 | 2011-05-03 11:51:38 +0200 | [diff] [blame] | 510 | ASSERT_RTNL(); |
| 511 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 512 | /* first deactivate interface */ |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 513 | if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 514 | batadv_hardif_disable_interface(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 515 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 516 | if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 517 | return; |
| 518 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 519 | hard_iface->if_status = BATADV_IF_TO_BE_REMOVED; |
Sven Eckelmann | 5853e22 | 2012-05-12 02:09:24 +0200 | [diff] [blame] | 520 | batadv_sysfs_del_hardif(&hard_iface->hardif_obj); |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 521 | batadv_hardif_free_ref(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 524 | void batadv_hardif_remove_interfaces(void) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 525 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 526 | struct batadv_hard_iface *hard_iface, *hard_iface_tmp; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 527 | |
Sven Eckelmann | c3caf51 | 2011-05-03 11:51:38 +0200 | [diff] [blame] | 528 | rtnl_lock(); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 529 | list_for_each_entry_safe(hard_iface, hard_iface_tmp, |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 530 | &batadv_hardif_list, list) { |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 531 | list_del_rcu(&hard_iface->list); |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 532 | batadv_hardif_remove_interface(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 533 | } |
| 534 | rtnl_unlock(); |
| 535 | } |
| 536 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 537 | static int batadv_hard_if_event(struct notifier_block *this, |
| 538 | unsigned long event, void *ptr) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 539 | { |
Sven Eckelmann | 5f718c2 | 2011-05-14 23:14:52 +0200 | [diff] [blame] | 540 | struct net_device *net_dev = ptr; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 541 | struct batadv_hard_iface *hard_iface; |
| 542 | struct batadv_hard_iface *primary_if = NULL; |
| 543 | struct batadv_priv *bat_priv; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 544 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 545 | hard_iface = batadv_hardif_get_by_netdev(net_dev); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 546 | if (!hard_iface && event == NETDEV_REGISTER) |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 547 | hard_iface = batadv_hardif_add_interface(net_dev); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 548 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 549 | if (!hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 550 | goto out; |
| 551 | |
| 552 | switch (event) { |
| 553 | case NETDEV_UP: |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 554 | batadv_hardif_activate_interface(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 555 | break; |
| 556 | case NETDEV_GOING_DOWN: |
| 557 | case NETDEV_DOWN: |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 558 | batadv_hardif_deactivate_interface(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 559 | break; |
| 560 | case NETDEV_UNREGISTER: |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 561 | list_del_rcu(&hard_iface->list); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 562 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 563 | batadv_hardif_remove_interface(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 564 | break; |
| 565 | case NETDEV_CHANGEMTU: |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 566 | if (hard_iface->soft_iface) |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 567 | batadv_update_min_mtu(hard_iface->soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 568 | break; |
| 569 | case NETDEV_CHANGEADDR: |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 570 | if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 571 | goto hardif_put; |
| 572 | |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 573 | batadv_check_known_mac_addr(hard_iface->net_dev); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 574 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 575 | bat_priv = netdev_priv(hard_iface->soft_iface); |
Marek Lindner | c322939 | 2012-03-11 06:17:50 +0800 | [diff] [blame] | 576 | bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface); |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 577 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 578 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 579 | if (!primary_if) |
| 580 | goto hardif_put; |
| 581 | |
| 582 | if (hard_iface == primary_if) |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 583 | batadv_primary_if_update_addr(bat_priv, NULL); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 584 | break; |
| 585 | default: |
| 586 | break; |
Joe Perches | f81c622 | 2011-06-03 11:51:19 +0000 | [diff] [blame] | 587 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 588 | |
| 589 | hardif_put: |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 590 | batadv_hardif_free_ref(hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 591 | out: |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 592 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 593 | batadv_hardif_free_ref(primary_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 594 | return NOTIFY_DONE; |
| 595 | } |
| 596 | |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 597 | /* This function returns true if the interface represented by ifindex is a |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 598 | * 802.11 wireless device |
| 599 | */ |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 600 | bool batadv_is_wifi_iface(int ifindex) |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 601 | { |
| 602 | struct net_device *net_device = NULL; |
| 603 | bool ret = false; |
| 604 | |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 605 | if (ifindex == BATADV_NULL_IFINDEX) |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 606 | goto out; |
| 607 | |
| 608 | net_device = dev_get_by_index(&init_net, ifindex); |
| 609 | if (!net_device) |
| 610 | goto out; |
| 611 | |
| 612 | #ifdef CONFIG_WIRELESS_EXT |
| 613 | /* pre-cfg80211 drivers have to implement WEXT, so it is possible to |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 614 | * check for wireless_handlers != NULL |
| 615 | */ |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 616 | if (net_device->wireless_handlers) |
| 617 | ret = true; |
| 618 | else |
| 619 | #endif |
| 620 | /* cfg80211 drivers have to set ieee80211_ptr */ |
| 621 | if (net_device->ieee80211_ptr) |
| 622 | ret = true; |
| 623 | out: |
| 624 | if (net_device) |
| 625 | dev_put(net_device); |
| 626 | return ret; |
| 627 | } |
| 628 | |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 629 | struct notifier_block batadv_hard_if_notifier = { |
Sven Eckelmann | 18a1cb6 | 2012-05-12 18:33:57 +0200 | [diff] [blame] | 630 | .notifier_call = batadv_hard_if_event, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 631 | }; |