soft-interface.c 24 KB

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