soft-interface.c 24 KB

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