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 "soft-interface.h" |
| 24 | #include "hard-interface.h" |
| 25 | #include "routing.h" |
| 26 | #include "send.h" |
| 27 | #include "bat_debugfs.h" |
| 28 | #include "translation-table.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 29 | #include "hash.h" |
| 30 | #include "gateway_common.h" |
| 31 | #include "gateway_client.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 32 | #include "bat_sysfs.h" |
Antonio Quartulli | 43676ab | 2011-04-26 21:31:45 +0200 | [diff] [blame] | 33 | #include "originator.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 34 | #include <linux/slab.h> |
| 35 | #include <linux/ethtool.h> |
| 36 | #include <linux/etherdevice.h> |
| 37 | #include <linux/if_vlan.h> |
| 38 | #include "unicast.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd); |
| 42 | static void bat_get_drvinfo(struct net_device *dev, |
| 43 | struct ethtool_drvinfo *info); |
| 44 | static u32 bat_get_msglevel(struct net_device *dev); |
| 45 | static void bat_set_msglevel(struct net_device *dev, u32 value); |
| 46 | static u32 bat_get_link(struct net_device *dev); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 47 | |
| 48 | static const struct ethtool_ops bat_ethtool_ops = { |
| 49 | .get_settings = bat_get_settings, |
| 50 | .get_drvinfo = bat_get_drvinfo, |
| 51 | .get_msglevel = bat_get_msglevel, |
| 52 | .set_msglevel = bat_set_msglevel, |
| 53 | .get_link = bat_get_link, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | int my_skb_head_push(struct sk_buff *skb, unsigned int len) |
| 57 | { |
| 58 | int result; |
| 59 | |
| 60 | /** |
| 61 | * TODO: We must check if we can release all references to non-payload |
| 62 | * data using skb_header_release in our skbs to allow skb_cow_header to |
| 63 | * work optimally. This means that those skbs are not allowed to read |
| 64 | * or write any data which is before the current position of skb->data |
| 65 | * after that call and thus allow other skbs with the same data buffer |
| 66 | * to write freely in that area. |
| 67 | */ |
| 68 | result = skb_cow_head(skb, len); |
| 69 | if (result < 0) |
| 70 | return result; |
| 71 | |
| 72 | skb_push(skb, len); |
| 73 | return 0; |
| 74 | } |
| 75 | |
Marek Lindner | 7d2b554 | 2011-02-10 14:33:50 +0000 | [diff] [blame] | 76 | static void softif_neigh_free_ref(struct softif_neigh *softif_neigh) |
| 77 | { |
| 78 | if (atomic_dec_and_test(&softif_neigh->refcount)) |
Paul E. McKenney | 8e3572c | 2011-05-02 00:52:23 -0700 | [diff] [blame] | 79 | kfree_rcu(softif_neigh, rcu); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 82 | static void softif_neigh_vid_free_rcu(struct rcu_head *rcu) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 83 | { |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 84 | struct softif_neigh_vid *softif_neigh_vid; |
| 85 | struct softif_neigh *softif_neigh; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 86 | struct hlist_node *node, *node_tmp; |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 87 | struct bat_priv *bat_priv; |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 88 | |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 89 | softif_neigh_vid = container_of(rcu, struct softif_neigh_vid, rcu); |
| 90 | bat_priv = softif_neigh_vid->bat_priv; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 91 | |
| 92 | spin_lock_bh(&bat_priv->softif_neigh_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 93 | hlist_for_each_entry_safe(softif_neigh, node, node_tmp, |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 94 | &softif_neigh_vid->softif_neigh_list, list) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 95 | hlist_del_rcu(&softif_neigh->list); |
Marek Lindner | 7d2b554 | 2011-02-10 14:33:50 +0000 | [diff] [blame] | 96 | softif_neigh_free_ref(softif_neigh); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 97 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 98 | spin_unlock_bh(&bat_priv->softif_neigh_lock); |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 99 | |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 100 | kfree(softif_neigh_vid); |
| 101 | } |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 102 | |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 103 | static void softif_neigh_vid_free_ref(struct softif_neigh_vid *softif_neigh_vid) |
| 104 | { |
| 105 | if (atomic_dec_and_test(&softif_neigh_vid->refcount)) |
| 106 | call_rcu(&softif_neigh_vid->rcu, softif_neigh_vid_free_rcu); |
| 107 | } |
| 108 | |
| 109 | static struct softif_neigh_vid *softif_neigh_vid_get(struct bat_priv *bat_priv, |
| 110 | short vid) |
| 111 | { |
| 112 | struct softif_neigh_vid *softif_neigh_vid; |
| 113 | struct hlist_node *node; |
| 114 | |
| 115 | rcu_read_lock(); |
| 116 | hlist_for_each_entry_rcu(softif_neigh_vid, node, |
| 117 | &bat_priv->softif_neigh_vids, list) { |
| 118 | if (softif_neigh_vid->vid != vid) |
| 119 | continue; |
| 120 | |
| 121 | if (!atomic_inc_not_zero(&softif_neigh_vid->refcount)) |
| 122 | continue; |
| 123 | |
| 124 | goto out; |
| 125 | } |
| 126 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 127 | softif_neigh_vid = kzalloc(sizeof(*softif_neigh_vid), GFP_ATOMIC); |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 128 | if (!softif_neigh_vid) |
| 129 | goto out; |
| 130 | |
| 131 | softif_neigh_vid->vid = vid; |
| 132 | softif_neigh_vid->bat_priv = bat_priv; |
| 133 | |
| 134 | /* initialize with 2 - caller decrements counter by one */ |
| 135 | atomic_set(&softif_neigh_vid->refcount, 2); |
| 136 | INIT_HLIST_HEAD(&softif_neigh_vid->softif_neigh_list); |
| 137 | INIT_HLIST_NODE(&softif_neigh_vid->list); |
| 138 | spin_lock_bh(&bat_priv->softif_neigh_vid_lock); |
| 139 | hlist_add_head_rcu(&softif_neigh_vid->list, |
| 140 | &bat_priv->softif_neigh_vids); |
| 141 | spin_unlock_bh(&bat_priv->softif_neigh_vid_lock); |
| 142 | |
| 143 | out: |
| 144 | rcu_read_unlock(); |
| 145 | return softif_neigh_vid; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static struct softif_neigh *softif_neigh_get(struct bat_priv *bat_priv, |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 149 | const uint8_t *addr, short vid) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 150 | { |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 151 | struct softif_neigh_vid *softif_neigh_vid; |
| 152 | struct softif_neigh *softif_neigh = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 153 | struct hlist_node *node; |
| 154 | |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 155 | softif_neigh_vid = softif_neigh_vid_get(bat_priv, vid); |
| 156 | if (!softif_neigh_vid) |
| 157 | goto out; |
| 158 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 159 | rcu_read_lock(); |
| 160 | hlist_for_each_entry_rcu(softif_neigh, node, |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 161 | &softif_neigh_vid->softif_neigh_list, |
| 162 | list) { |
Marek Lindner | 39901e7 | 2011-02-18 12:28:08 +0000 | [diff] [blame] | 163 | if (!compare_eth(softif_neigh->addr, addr)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 164 | continue; |
| 165 | |
Marek Lindner | 7d2b554 | 2011-02-10 14:33:50 +0000 | [diff] [blame] | 166 | if (!atomic_inc_not_zero(&softif_neigh->refcount)) |
| 167 | continue; |
| 168 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 169 | softif_neigh->last_seen = jiffies; |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 170 | goto unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 173 | softif_neigh = kzalloc(sizeof(*softif_neigh), GFP_ATOMIC); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 174 | if (!softif_neigh) |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 175 | goto unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 176 | |
| 177 | memcpy(softif_neigh->addr, addr, ETH_ALEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 178 | softif_neigh->last_seen = jiffies; |
Marek Lindner | 7d2b554 | 2011-02-10 14:33:50 +0000 | [diff] [blame] | 179 | /* initialize with 2 - caller decrements counter by one */ |
| 180 | atomic_set(&softif_neigh->refcount, 2); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 181 | |
| 182 | INIT_HLIST_NODE(&softif_neigh->list); |
| 183 | spin_lock_bh(&bat_priv->softif_neigh_lock); |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 184 | hlist_add_head_rcu(&softif_neigh->list, |
| 185 | &softif_neigh_vid->softif_neigh_list); |
| 186 | spin_unlock_bh(&bat_priv->softif_neigh_lock); |
| 187 | |
| 188 | unlock: |
| 189 | rcu_read_unlock(); |
| 190 | out: |
| 191 | if (softif_neigh_vid) |
| 192 | softif_neigh_vid_free_ref(softif_neigh_vid); |
| 193 | return softif_neigh; |
| 194 | } |
| 195 | |
| 196 | static struct softif_neigh *softif_neigh_get_selected( |
| 197 | struct softif_neigh_vid *softif_neigh_vid) |
| 198 | { |
| 199 | struct softif_neigh *softif_neigh; |
| 200 | |
| 201 | rcu_read_lock(); |
| 202 | softif_neigh = rcu_dereference(softif_neigh_vid->softif_neigh); |
| 203 | |
| 204 | if (softif_neigh && !atomic_inc_not_zero(&softif_neigh->refcount)) |
| 205 | softif_neigh = NULL; |
| 206 | |
| 207 | rcu_read_unlock(); |
| 208 | return softif_neigh; |
| 209 | } |
| 210 | |
| 211 | static struct softif_neigh *softif_neigh_vid_get_selected( |
| 212 | struct bat_priv *bat_priv, |
| 213 | short vid) |
| 214 | { |
| 215 | struct softif_neigh_vid *softif_neigh_vid; |
| 216 | struct softif_neigh *softif_neigh = NULL; |
| 217 | |
| 218 | softif_neigh_vid = softif_neigh_vid_get(bat_priv, vid); |
| 219 | if (!softif_neigh_vid) |
| 220 | goto out; |
| 221 | |
| 222 | softif_neigh = softif_neigh_get_selected(softif_neigh_vid); |
| 223 | out: |
| 224 | if (softif_neigh_vid) |
| 225 | softif_neigh_vid_free_ref(softif_neigh_vid); |
| 226 | return softif_neigh; |
| 227 | } |
| 228 | |
| 229 | static void softif_neigh_vid_select(struct bat_priv *bat_priv, |
| 230 | struct softif_neigh *new_neigh, |
| 231 | short vid) |
| 232 | { |
| 233 | struct softif_neigh_vid *softif_neigh_vid; |
| 234 | struct softif_neigh *curr_neigh; |
| 235 | |
| 236 | softif_neigh_vid = softif_neigh_vid_get(bat_priv, vid); |
| 237 | if (!softif_neigh_vid) |
| 238 | goto out; |
| 239 | |
| 240 | spin_lock_bh(&bat_priv->softif_neigh_lock); |
| 241 | |
| 242 | if (new_neigh && !atomic_inc_not_zero(&new_neigh->refcount)) |
| 243 | new_neigh = NULL; |
| 244 | |
Sven Eckelmann | 728cbc6 | 2011-05-15 00:50:21 +0200 | [diff] [blame] | 245 | curr_neigh = rcu_dereference_protected(softif_neigh_vid->softif_neigh, |
| 246 | 1); |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 247 | rcu_assign_pointer(softif_neigh_vid->softif_neigh, new_neigh); |
| 248 | |
| 249 | if ((curr_neigh) && (!new_neigh)) |
| 250 | bat_dbg(DBG_ROUTES, bat_priv, |
| 251 | "Removing mesh exit point on vid: %d (prev: %pM).\n", |
| 252 | vid, curr_neigh->addr); |
| 253 | else if ((curr_neigh) && (new_neigh)) |
| 254 | bat_dbg(DBG_ROUTES, bat_priv, |
| 255 | "Changing mesh exit point on vid: %d from %pM " |
| 256 | "to %pM.\n", vid, curr_neigh->addr, new_neigh->addr); |
| 257 | else if ((!curr_neigh) && (new_neigh)) |
| 258 | bat_dbg(DBG_ROUTES, bat_priv, |
| 259 | "Setting mesh exit point on vid: %d to %pM.\n", |
| 260 | vid, new_neigh->addr); |
| 261 | |
| 262 | if (curr_neigh) |
| 263 | softif_neigh_free_ref(curr_neigh); |
| 264 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 265 | spin_unlock_bh(&bat_priv->softif_neigh_lock); |
| 266 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 267 | out: |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 268 | if (softif_neigh_vid) |
| 269 | softif_neigh_vid_free_ref(softif_neigh_vid); |
| 270 | } |
| 271 | |
| 272 | static void softif_neigh_vid_deselect(struct bat_priv *bat_priv, |
| 273 | struct softif_neigh_vid *softif_neigh_vid) |
| 274 | { |
| 275 | struct softif_neigh *curr_neigh; |
| 276 | struct softif_neigh *softif_neigh = NULL, *softif_neigh_tmp; |
| 277 | struct hard_iface *primary_if = NULL; |
| 278 | struct hlist_node *node; |
| 279 | |
| 280 | primary_if = primary_if_get_selected(bat_priv); |
| 281 | if (!primary_if) |
| 282 | goto out; |
| 283 | |
| 284 | /* find new softif_neigh immediately to avoid temporary loops */ |
| 285 | rcu_read_lock(); |
| 286 | curr_neigh = rcu_dereference(softif_neigh_vid->softif_neigh); |
| 287 | |
| 288 | hlist_for_each_entry_rcu(softif_neigh_tmp, node, |
| 289 | &softif_neigh_vid->softif_neigh_list, |
| 290 | list) { |
| 291 | if (softif_neigh_tmp == curr_neigh) |
| 292 | continue; |
| 293 | |
| 294 | /* we got a neighbor but its mac is 'bigger' than ours */ |
| 295 | if (memcmp(primary_if->net_dev->dev_addr, |
| 296 | softif_neigh_tmp->addr, ETH_ALEN) < 0) |
| 297 | continue; |
| 298 | |
| 299 | if (!atomic_inc_not_zero(&softif_neigh_tmp->refcount)) |
| 300 | continue; |
| 301 | |
| 302 | softif_neigh = softif_neigh_tmp; |
| 303 | goto unlock; |
| 304 | } |
| 305 | |
| 306 | unlock: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 307 | rcu_read_unlock(); |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 308 | out: |
| 309 | softif_neigh_vid_select(bat_priv, softif_neigh, softif_neigh_vid->vid); |
| 310 | |
| 311 | if (primary_if) |
| 312 | hardif_free_ref(primary_if); |
| 313 | if (softif_neigh) |
| 314 | softif_neigh_free_ref(softif_neigh); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | int softif_neigh_seq_print_text(struct seq_file *seq, void *offset) |
| 318 | { |
| 319 | struct net_device *net_dev = (struct net_device *)seq->private; |
| 320 | struct bat_priv *bat_priv = netdev_priv(net_dev); |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 321 | struct softif_neigh_vid *softif_neigh_vid; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 322 | struct softif_neigh *softif_neigh; |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 323 | struct hard_iface *primary_if; |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 324 | struct hlist_node *node, *node_tmp; |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 325 | struct softif_neigh *curr_softif_neigh; |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 326 | int ret = 0, last_seen_secs, last_seen_msecs; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 327 | |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 328 | primary_if = primary_if_get_selected(bat_priv); |
| 329 | if (!primary_if) { |
| 330 | ret = seq_printf(seq, "BATMAN mesh %s disabled - " |
| 331 | "please specify interfaces to enable it\n", |
| 332 | net_dev->name); |
| 333 | goto out; |
| 334 | } |
| 335 | |
| 336 | if (primary_if->if_status != IF_ACTIVE) { |
| 337 | ret = seq_printf(seq, "BATMAN mesh %s " |
| 338 | "disabled - primary interface not active\n", |
| 339 | net_dev->name); |
| 340 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | seq_printf(seq, "Softif neighbor list (%s)\n", net_dev->name); |
| 344 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 345 | rcu_read_lock(); |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 346 | hlist_for_each_entry_rcu(softif_neigh_vid, node, |
| 347 | &bat_priv->softif_neigh_vids, list) { |
| 348 | seq_printf(seq, " %-15s %s on vid: %d\n", |
| 349 | "Originator", "last-seen", softif_neigh_vid->vid); |
| 350 | |
| 351 | curr_softif_neigh = softif_neigh_get_selected(softif_neigh_vid); |
| 352 | |
| 353 | hlist_for_each_entry_rcu(softif_neigh, node_tmp, |
| 354 | &softif_neigh_vid->softif_neigh_list, |
| 355 | list) { |
| 356 | last_seen_secs = jiffies_to_msecs(jiffies - |
| 357 | softif_neigh->last_seen) / 1000; |
| 358 | last_seen_msecs = jiffies_to_msecs(jiffies - |
| 359 | softif_neigh->last_seen) % 1000; |
| 360 | seq_printf(seq, "%s %pM %3i.%03is\n", |
| 361 | curr_softif_neigh == softif_neigh |
| 362 | ? "=>" : " ", softif_neigh->addr, |
| 363 | last_seen_secs, last_seen_msecs); |
| 364 | } |
| 365 | |
| 366 | if (curr_softif_neigh) |
| 367 | softif_neigh_free_ref(curr_softif_neigh); |
| 368 | |
| 369 | seq_printf(seq, "\n"); |
| 370 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 371 | rcu_read_unlock(); |
| 372 | |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 373 | out: |
| 374 | if (primary_if) |
| 375 | hardif_free_ref(primary_if); |
| 376 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 379 | void softif_neigh_purge(struct bat_priv *bat_priv) |
| 380 | { |
| 381 | struct softif_neigh *softif_neigh, *curr_softif_neigh; |
| 382 | struct softif_neigh_vid *softif_neigh_vid; |
| 383 | struct hlist_node *node, *node_tmp, *node_tmp2; |
Sven Eckelmann | b4e1705 | 2011-06-15 09:41:37 +0200 | [diff] [blame] | 384 | int do_deselect; |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 385 | |
| 386 | rcu_read_lock(); |
| 387 | hlist_for_each_entry_rcu(softif_neigh_vid, node, |
| 388 | &bat_priv->softif_neigh_vids, list) { |
| 389 | if (!atomic_inc_not_zero(&softif_neigh_vid->refcount)) |
| 390 | continue; |
| 391 | |
| 392 | curr_softif_neigh = softif_neigh_get_selected(softif_neigh_vid); |
| 393 | do_deselect = 0; |
| 394 | |
| 395 | spin_lock_bh(&bat_priv->softif_neigh_lock); |
| 396 | hlist_for_each_entry_safe(softif_neigh, node_tmp, node_tmp2, |
| 397 | &softif_neigh_vid->softif_neigh_list, |
| 398 | list) { |
| 399 | if ((!time_after(jiffies, softif_neigh->last_seen + |
| 400 | msecs_to_jiffies(SOFTIF_NEIGH_TIMEOUT))) && |
| 401 | (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE)) |
| 402 | continue; |
| 403 | |
| 404 | if (curr_softif_neigh == softif_neigh) { |
| 405 | bat_dbg(DBG_ROUTES, bat_priv, |
| 406 | "Current mesh exit point on vid: %d " |
| 407 | "'%pM' vanished.\n", |
| 408 | softif_neigh_vid->vid, |
| 409 | softif_neigh->addr); |
| 410 | do_deselect = 1; |
| 411 | } |
| 412 | |
| 413 | hlist_del_rcu(&softif_neigh->list); |
| 414 | softif_neigh_free_ref(softif_neigh); |
| 415 | } |
| 416 | spin_unlock_bh(&bat_priv->softif_neigh_lock); |
| 417 | |
| 418 | /* soft_neigh_vid_deselect() needs to acquire the |
| 419 | * softif_neigh_lock */ |
| 420 | if (do_deselect) |
| 421 | softif_neigh_vid_deselect(bat_priv, softif_neigh_vid); |
| 422 | |
| 423 | if (curr_softif_neigh) |
| 424 | softif_neigh_free_ref(curr_softif_neigh); |
| 425 | |
| 426 | softif_neigh_vid_free_ref(softif_neigh_vid); |
| 427 | } |
| 428 | rcu_read_unlock(); |
| 429 | |
| 430 | spin_lock_bh(&bat_priv->softif_neigh_vid_lock); |
| 431 | hlist_for_each_entry_safe(softif_neigh_vid, node, node_tmp, |
| 432 | &bat_priv->softif_neigh_vids, list) { |
| 433 | if (!hlist_empty(&softif_neigh_vid->softif_neigh_list)) |
| 434 | continue; |
| 435 | |
| 436 | hlist_del_rcu(&softif_neigh_vid->list); |
| 437 | softif_neigh_vid_free_ref(softif_neigh_vid); |
| 438 | } |
| 439 | spin_unlock_bh(&bat_priv->softif_neigh_vid_lock); |
| 440 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static void softif_batman_recv(struct sk_buff *skb, struct net_device *dev, |
| 444 | short vid) |
| 445 | { |
| 446 | struct bat_priv *bat_priv = netdev_priv(dev); |
| 447 | struct ethhdr *ethhdr = (struct ethhdr *)skb->data; |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame] | 448 | struct batman_ogm_packet *batman_ogm_packet; |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 449 | struct softif_neigh *softif_neigh = NULL; |
| 450 | struct hard_iface *primary_if = NULL; |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 451 | struct softif_neigh *curr_softif_neigh = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 452 | |
| 453 | if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame] | 454 | batman_ogm_packet = (struct batman_ogm_packet *) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 455 | (skb->data + ETH_HLEN + VLAN_HLEN); |
| 456 | else |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame] | 457 | batman_ogm_packet = (struct batman_ogm_packet *) |
| 458 | (skb->data + ETH_HLEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 459 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame] | 460 | if (batman_ogm_packet->version != COMPAT_VERSION) |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 461 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 462 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame] | 463 | if (batman_ogm_packet->packet_type != BAT_OGM) |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 464 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 465 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame] | 466 | if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP)) |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 467 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 468 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame] | 469 | if (is_my_mac(batman_ogm_packet->orig)) |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 470 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 471 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame] | 472 | softif_neigh = softif_neigh_get(bat_priv, batman_ogm_packet->orig, vid); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 473 | if (!softif_neigh) |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 474 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 475 | |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 476 | curr_softif_neigh = softif_neigh_vid_get_selected(bat_priv, vid); |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 477 | if (curr_softif_neigh == softif_neigh) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 478 | goto out; |
| 479 | |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 480 | primary_if = primary_if_get_selected(bat_priv); |
| 481 | if (!primary_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 482 | goto out; |
| 483 | |
| 484 | /* we got a neighbor but its mac is 'bigger' than ours */ |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 485 | if (memcmp(primary_if->net_dev->dev_addr, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 486 | softif_neigh->addr, ETH_ALEN) < 0) |
| 487 | goto out; |
| 488 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 489 | /* close own batX device and use softif_neigh as exit node */ |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 490 | if (!curr_softif_neigh) { |
| 491 | softif_neigh_vid_select(bat_priv, softif_neigh, vid); |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 492 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 495 | /* switch to new 'smallest neighbor' */ |
| 496 | if (memcmp(softif_neigh->addr, curr_softif_neigh->addr, ETH_ALEN) < 0) |
| 497 | softif_neigh_vid_select(bat_priv, softif_neigh, vid); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 498 | |
| 499 | out: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 500 | kfree_skb(skb); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 501 | if (softif_neigh) |
| 502 | softif_neigh_free_ref(softif_neigh); |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 503 | if (curr_softif_neigh) |
| 504 | softif_neigh_free_ref(curr_softif_neigh); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 505 | if (primary_if) |
| 506 | hardif_free_ref(primary_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 507 | return; |
| 508 | } |
| 509 | |
| 510 | static int interface_open(struct net_device *dev) |
| 511 | { |
| 512 | netif_start_queue(dev); |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | static int interface_release(struct net_device *dev) |
| 517 | { |
| 518 | netif_stop_queue(dev); |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static struct net_device_stats *interface_stats(struct net_device *dev) |
| 523 | { |
| 524 | struct bat_priv *bat_priv = netdev_priv(dev); |
| 525 | return &bat_priv->stats; |
| 526 | } |
| 527 | |
| 528 | static int interface_set_mac_addr(struct net_device *dev, void *p) |
| 529 | { |
| 530 | struct bat_priv *bat_priv = netdev_priv(dev); |
| 531 | struct sockaddr *addr = p; |
| 532 | |
| 533 | if (!is_valid_ether_addr(addr->sa_data)) |
| 534 | return -EADDRNOTAVAIL; |
| 535 | |
Antonio Quartulli | 015758d | 2011-07-09 17:52:13 +0200 | [diff] [blame] | 536 | /* only modify transtable if it has been initialized before */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 537 | if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) { |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 538 | tt_local_remove(bat_priv, dev->dev_addr, |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 539 | "mac address changed", false); |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 540 | tt_local_add(dev, addr->sa_data, NULL_IFINDEX); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | static int interface_change_mtu(struct net_device *dev, int new_mtu) |
| 548 | { |
| 549 | /* check ranges */ |
| 550 | if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev))) |
| 551 | return -EINVAL; |
| 552 | |
| 553 | dev->mtu = new_mtu; |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
Sven Eckelmann | db69ecf | 2011-06-15 15:17:21 +0200 | [diff] [blame] | 558 | static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 559 | { |
| 560 | struct ethhdr *ethhdr = (struct ethhdr *)skb->data; |
| 561 | struct bat_priv *bat_priv = netdev_priv(soft_iface); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 562 | struct hard_iface *primary_if = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 563 | struct bcast_packet *bcast_packet; |
| 564 | struct vlan_ethhdr *vhdr; |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 565 | struct softif_neigh *curr_softif_neigh = NULL; |
Antonio Quartulli | 43676ab | 2011-04-26 21:31:45 +0200 | [diff] [blame] | 566 | struct orig_node *orig_node = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 567 | int data_len = skb->len, ret; |
| 568 | short vid = -1; |
Antonio Quartulli | 8b267b31 | 2011-09-21 16:06:42 +0200 | [diff] [blame] | 569 | bool do_bcast; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 570 | |
| 571 | if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE) |
| 572 | goto dropped; |
| 573 | |
| 574 | soft_iface->trans_start = jiffies; |
| 575 | |
| 576 | switch (ntohs(ethhdr->h_proto)) { |
| 577 | case ETH_P_8021Q: |
| 578 | vhdr = (struct vlan_ethhdr *)skb->data; |
| 579 | vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK; |
| 580 | |
| 581 | if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN) |
| 582 | break; |
| 583 | |
| 584 | /* fall through */ |
| 585 | case ETH_P_BATMAN: |
| 586 | softif_batman_recv(skb, soft_iface, vid); |
| 587 | goto end; |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * if we have a another chosen mesh exit node in range |
| 592 | * it will transport the packets to the mesh |
| 593 | */ |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 594 | curr_softif_neigh = softif_neigh_vid_get_selected(bat_priv, vid); |
| 595 | if (curr_softif_neigh) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 596 | goto dropped; |
| 597 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 598 | /* Register the client MAC in the transtable */ |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 599 | tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 600 | |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 601 | orig_node = transtable_search(bat_priv, ethhdr->h_source, |
| 602 | ethhdr->h_dest); |
Antonio Quartulli | 8b267b31 | 2011-09-21 16:06:42 +0200 | [diff] [blame] | 603 | do_bcast = is_multicast_ether_addr(ethhdr->h_dest); |
David S. Miller | 88c5100 | 2011-10-07 13:38:43 -0400 | [diff] [blame] | 604 | if (do_bcast || (orig_node && orig_node->gw_flags)) { |
Antonio Quartulli | 43676ab | 2011-04-26 21:31:45 +0200 | [diff] [blame] | 605 | ret = gw_is_target(bat_priv, skb, orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 606 | |
| 607 | if (ret < 0) |
| 608 | goto dropped; |
| 609 | |
Antonio Quartulli | 8b267b31 | 2011-09-21 16:06:42 +0200 | [diff] [blame] | 610 | if (ret) |
| 611 | do_bcast = false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* ethernet packet should be broadcasted */ |
| 615 | if (do_bcast) { |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 616 | primary_if = primary_if_get_selected(bat_priv); |
| 617 | if (!primary_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 618 | goto dropped; |
| 619 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 620 | if (my_skb_head_push(skb, sizeof(*bcast_packet)) < 0) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 621 | goto dropped; |
| 622 | |
| 623 | bcast_packet = (struct bcast_packet *)skb->data; |
| 624 | bcast_packet->version = COMPAT_VERSION; |
| 625 | bcast_packet->ttl = TTL; |
| 626 | |
| 627 | /* batman packet type: broadcast */ |
| 628 | bcast_packet->packet_type = BAT_BCAST; |
| 629 | |
| 630 | /* hw address of first interface is the orig mac because only |
| 631 | * this mac is known throughout the mesh */ |
| 632 | memcpy(bcast_packet->orig, |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 633 | primary_if->net_dev->dev_addr, ETH_ALEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 634 | |
| 635 | /* set broadcast sequence number */ |
| 636 | bcast_packet->seqno = |
| 637 | htonl(atomic_inc_return(&bat_priv->bcast_seqno)); |
| 638 | |
Antonio Quartulli | 8698529 | 2011-06-25 19:09:12 +0200 | [diff] [blame] | 639 | add_bcast_packet_to_list(bat_priv, skb, 1); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 640 | |
| 641 | /* a copy is stored in the bcast list, therefore removing |
| 642 | * the original skb. */ |
| 643 | kfree_skb(skb); |
| 644 | |
| 645 | /* unicast packet */ |
| 646 | } else { |
| 647 | ret = unicast_send_skb(skb, bat_priv); |
| 648 | if (ret != 0) |
| 649 | goto dropped_freed; |
| 650 | } |
| 651 | |
| 652 | bat_priv->stats.tx_packets++; |
| 653 | bat_priv->stats.tx_bytes += data_len; |
| 654 | goto end; |
| 655 | |
| 656 | dropped: |
| 657 | kfree_skb(skb); |
| 658 | dropped_freed: |
| 659 | bat_priv->stats.tx_dropped++; |
| 660 | end: |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 661 | if (curr_softif_neigh) |
| 662 | softif_neigh_free_ref(curr_softif_neigh); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 663 | if (primary_if) |
| 664 | hardif_free_ref(primary_if); |
Antonio Quartulli | 43676ab | 2011-04-26 21:31:45 +0200 | [diff] [blame] | 665 | if (orig_node) |
| 666 | orig_node_free_ref(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 667 | return NETDEV_TX_OK; |
| 668 | } |
| 669 | |
| 670 | void interface_rx(struct net_device *soft_iface, |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 671 | struct sk_buff *skb, struct hard_iface *recv_if, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 672 | int hdr_size) |
| 673 | { |
| 674 | struct bat_priv *bat_priv = netdev_priv(soft_iface); |
| 675 | struct unicast_packet *unicast_packet; |
| 676 | struct ethhdr *ethhdr; |
| 677 | struct vlan_ethhdr *vhdr; |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 678 | struct softif_neigh *curr_softif_neigh = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 679 | short vid = -1; |
| 680 | int ret; |
| 681 | |
| 682 | /* check if enough space is available for pulling, and pull */ |
| 683 | if (!pskb_may_pull(skb, hdr_size)) |
| 684 | goto dropped; |
| 685 | |
| 686 | skb_pull_rcsum(skb, hdr_size); |
| 687 | skb_reset_mac_header(skb); |
| 688 | |
| 689 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 690 | |
| 691 | switch (ntohs(ethhdr->h_proto)) { |
| 692 | case ETH_P_8021Q: |
| 693 | vhdr = (struct vlan_ethhdr *)skb->data; |
| 694 | vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK; |
| 695 | |
| 696 | if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN) |
| 697 | break; |
| 698 | |
| 699 | /* fall through */ |
| 700 | case ETH_P_BATMAN: |
| 701 | goto dropped; |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * if we have a another chosen mesh exit node in range |
| 706 | * it will transport the packets to the non-mesh network |
| 707 | */ |
Marek Lindner | 61906ae | 2011-04-21 15:52:17 +0200 | [diff] [blame] | 708 | curr_softif_neigh = softif_neigh_vid_get_selected(bat_priv, vid); |
| 709 | if (curr_softif_neigh) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 710 | skb_push(skb, hdr_size); |
| 711 | unicast_packet = (struct unicast_packet *)skb->data; |
| 712 | |
| 713 | if ((unicast_packet->packet_type != BAT_UNICAST) && |
| 714 | (unicast_packet->packet_type != BAT_UNICAST_FRAG)) |
| 715 | goto dropped; |
| 716 | |
| 717 | skb_reset_mac_header(skb); |
| 718 | |
| 719 | memcpy(unicast_packet->dest, |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 720 | curr_softif_neigh->addr, ETH_ALEN); |
Linus Lüssing | 7cefb14 | 2011-03-02 17:39:31 +0000 | [diff] [blame] | 721 | ret = route_unicast_packet(skb, recv_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 722 | if (ret == NET_RX_DROP) |
| 723 | goto dropped; |
| 724 | |
| 725 | goto out; |
| 726 | } |
| 727 | |
| 728 | /* skb->dev & skb->pkt_type are set here */ |
| 729 | if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) |
| 730 | goto dropped; |
| 731 | skb->protocol = eth_type_trans(skb, soft_iface); |
| 732 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 733 | /* should not be necessary anymore as we use skb_pull_rcsum() |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 734 | * TODO: please verify this and remove this TODO |
| 735 | * -- Dec 21st 2009, Simon Wunderlich */ |
| 736 | |
| 737 | /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/ |
| 738 | |
| 739 | bat_priv->stats.rx_packets++; |
| 740 | bat_priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr); |
| 741 | |
| 742 | soft_iface->last_rx = jiffies; |
| 743 | |
Antonio Quartulli | 59b699c | 2011-07-07 15:35:36 +0200 | [diff] [blame] | 744 | if (is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest)) |
| 745 | goto dropped; |
| 746 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 747 | netif_rx(skb); |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 748 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 749 | |
| 750 | dropped: |
| 751 | kfree_skb(skb); |
| 752 | out: |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 753 | if (curr_softif_neigh) |
| 754 | softif_neigh_free_ref(curr_softif_neigh); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 755 | return; |
| 756 | } |
| 757 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 758 | static const struct net_device_ops bat_netdev_ops = { |
| 759 | .ndo_open = interface_open, |
| 760 | .ndo_stop = interface_release, |
| 761 | .ndo_get_stats = interface_stats, |
| 762 | .ndo_set_mac_address = interface_set_mac_addr, |
| 763 | .ndo_change_mtu = interface_change_mtu, |
| 764 | .ndo_start_xmit = interface_tx, |
| 765 | .ndo_validate_addr = eth_validate_addr |
| 766 | }; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 767 | |
| 768 | static void interface_setup(struct net_device *dev) |
| 769 | { |
| 770 | struct bat_priv *priv = netdev_priv(dev); |
| 771 | char dev_addr[ETH_ALEN]; |
| 772 | |
| 773 | ether_setup(dev); |
| 774 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 775 | dev->netdev_ops = &bat_netdev_ops; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 776 | dev->destructor = free_netdev; |
Andrew Lunn | af20b71 | 2011-04-17 20:39:07 +0200 | [diff] [blame] | 777 | dev->tx_queue_len = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 778 | |
| 779 | /** |
| 780 | * can't call min_mtu, because the needed variables |
| 781 | * have not been initialized yet |
| 782 | */ |
| 783 | dev->mtu = ETH_DATA_LEN; |
Sven Eckelmann | 6e215fd | 2011-05-08 12:45:45 +0200 | [diff] [blame] | 784 | /* reserve more space in the skbuff for our header */ |
| 785 | dev->hard_header_len = BAT_HEADER_LEN; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 786 | |
| 787 | /* generate random address */ |
| 788 | random_ether_addr(dev_addr); |
| 789 | memcpy(dev->dev_addr, dev_addr, ETH_ALEN); |
| 790 | |
| 791 | SET_ETHTOOL_OPS(dev, &bat_ethtool_ops); |
| 792 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 793 | memset(priv, 0, sizeof(*priv)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 796 | struct net_device *softif_create(const char *name) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 797 | { |
| 798 | struct net_device *soft_iface; |
| 799 | struct bat_priv *bat_priv; |
| 800 | int ret; |
| 801 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 802 | soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 803 | |
Joe Perches | 320f422 | 2011-08-29 14:17:24 -0700 | [diff] [blame] | 804 | if (!soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 805 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 806 | |
Sven Eckelmann | c3caf51 | 2011-05-03 11:51:38 +0200 | [diff] [blame] | 807 | ret = register_netdevice(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 808 | if (ret < 0) { |
| 809 | pr_err("Unable to register the batman interface '%s': %i\n", |
| 810 | name, ret); |
| 811 | goto free_soft_iface; |
| 812 | } |
| 813 | |
| 814 | bat_priv = netdev_priv(soft_iface); |
| 815 | |
| 816 | atomic_set(&bat_priv->aggregated_ogms, 1); |
| 817 | atomic_set(&bat_priv->bonding, 0); |
Antonio Quartulli | 59b699c | 2011-07-07 15:35:36 +0200 | [diff] [blame] | 818 | atomic_set(&bat_priv->ap_isolation, 0); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 819 | atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE); |
| 820 | atomic_set(&bat_priv->gw_mode, GW_MODE_OFF); |
| 821 | atomic_set(&bat_priv->gw_sel_class, 20); |
| 822 | atomic_set(&bat_priv->gw_bandwidth, 41); |
| 823 | atomic_set(&bat_priv->orig_interval, 1000); |
| 824 | atomic_set(&bat_priv->hop_penalty, 10); |
| 825 | atomic_set(&bat_priv->log_level, 0); |
| 826 | atomic_set(&bat_priv->fragmentation, 1); |
| 827 | atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN); |
| 828 | atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN); |
| 829 | |
| 830 | atomic_set(&bat_priv->mesh_state, MESH_INACTIVE); |
| 831 | atomic_set(&bat_priv->bcast_seqno, 1); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 832 | atomic_set(&bat_priv->ttvn, 0); |
| 833 | atomic_set(&bat_priv->tt_local_changes, 0); |
| 834 | atomic_set(&bat_priv->tt_ogm_append_cnt, 0); |
| 835 | |
| 836 | bat_priv->tt_buff = NULL; |
| 837 | bat_priv->tt_buff_len = 0; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 838 | bat_priv->tt_poss_change = false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 839 | |
| 840 | bat_priv->primary_if = NULL; |
| 841 | bat_priv->num_ifaces = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 842 | |
| 843 | ret = sysfs_add_meshif(soft_iface); |
| 844 | if (ret < 0) |
| 845 | goto unreg_soft_iface; |
| 846 | |
| 847 | ret = debugfs_add_meshif(soft_iface); |
| 848 | if (ret < 0) |
| 849 | goto unreg_sysfs; |
| 850 | |
| 851 | ret = mesh_init(soft_iface); |
| 852 | if (ret < 0) |
| 853 | goto unreg_debugfs; |
| 854 | |
| 855 | return soft_iface; |
| 856 | |
| 857 | unreg_debugfs: |
| 858 | debugfs_del_meshif(soft_iface); |
| 859 | unreg_sysfs: |
| 860 | sysfs_del_meshif(soft_iface); |
| 861 | unreg_soft_iface: |
| 862 | unregister_netdev(soft_iface); |
| 863 | return NULL; |
| 864 | |
| 865 | free_soft_iface: |
| 866 | free_netdev(soft_iface); |
| 867 | out: |
| 868 | return NULL; |
| 869 | } |
| 870 | |
| 871 | void softif_destroy(struct net_device *soft_iface) |
| 872 | { |
| 873 | debugfs_del_meshif(soft_iface); |
| 874 | sysfs_del_meshif(soft_iface); |
| 875 | mesh_free(soft_iface); |
| 876 | unregister_netdevice(soft_iface); |
| 877 | } |
| 878 | |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 879 | int softif_is_valid(const struct net_device *net_dev) |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 880 | { |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 881 | if (net_dev->netdev_ops->ndo_start_xmit == interface_tx) |
| 882 | return 1; |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 887 | /* ethtool */ |
| 888 | static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 889 | { |
| 890 | cmd->supported = 0; |
| 891 | cmd->advertising = 0; |
David Decotigny | 7073949 | 2011-04-27 18:32:40 +0000 | [diff] [blame] | 892 | ethtool_cmd_speed_set(cmd, SPEED_10); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 893 | cmd->duplex = DUPLEX_FULL; |
| 894 | cmd->port = PORT_TP; |
| 895 | cmd->phy_address = 0; |
| 896 | cmd->transceiver = XCVR_INTERNAL; |
| 897 | cmd->autoneg = AUTONEG_DISABLE; |
| 898 | cmd->maxtxpkt = 0; |
| 899 | cmd->maxrxpkt = 0; |
| 900 | |
| 901 | return 0; |
| 902 | } |
| 903 | |
| 904 | static void bat_get_drvinfo(struct net_device *dev, |
| 905 | struct ethtool_drvinfo *info) |
| 906 | { |
| 907 | strcpy(info->driver, "B.A.T.M.A.N. advanced"); |
| 908 | strcpy(info->version, SOURCE_VERSION); |
| 909 | strcpy(info->fw_version, "N/A"); |
| 910 | strcpy(info->bus_info, "batman"); |
| 911 | } |
| 912 | |
| 913 | static u32 bat_get_msglevel(struct net_device *dev) |
| 914 | { |
| 915 | return -EOPNOTSUPP; |
| 916 | } |
| 917 | |
| 918 | static void bat_set_msglevel(struct net_device *dev, u32 value) |
| 919 | { |
| 920 | } |
| 921 | |
| 922 | static u32 bat_get_link(struct net_device *dev) |
| 923 | { |
| 924 | return 1; |
| 925 | } |