soft-interface.c 24 KB

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