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