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