soft-interface.c 24 KB

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