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