soft-interface.c 24 KB

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