soft-interface.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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 struct softif_neigh *softif_neigh_get_selected(struct bat_priv *bat_priv)
  79. {
  80. struct softif_neigh *neigh;
  81. rcu_read_lock();
  82. neigh = rcu_dereference(bat_priv->softif_neigh);
  83. if (neigh && !atomic_inc_not_zero(&neigh->refcount))
  84. neigh = NULL;
  85. rcu_read_unlock();
  86. return neigh;
  87. }
  88. static void softif_neigh_select(struct bat_priv *bat_priv,
  89. struct softif_neigh *new_neigh)
  90. {
  91. struct softif_neigh *curr_neigh;
  92. spin_lock_bh(&bat_priv->softif_neigh_lock);
  93. if (new_neigh && !atomic_inc_not_zero(&new_neigh->refcount))
  94. new_neigh = NULL;
  95. curr_neigh = bat_priv->softif_neigh;
  96. rcu_assign_pointer(bat_priv->softif_neigh, new_neigh);
  97. if (curr_neigh)
  98. softif_neigh_free_ref(curr_neigh);
  99. spin_unlock_bh(&bat_priv->softif_neigh_lock);
  100. }
  101. static void softif_neigh_deselect(struct bat_priv *bat_priv)
  102. {
  103. softif_neigh_select(bat_priv, NULL);
  104. }
  105. void softif_neigh_purge(struct bat_priv *bat_priv)
  106. {
  107. struct softif_neigh *softif_neigh, *curr_softif_neigh;
  108. struct hlist_node *node, *node_tmp;
  109. char do_deselect = 0;
  110. curr_softif_neigh = softif_neigh_get_selected(bat_priv);
  111. spin_lock_bh(&bat_priv->softif_neigh_lock);
  112. hlist_for_each_entry_safe(softif_neigh, node, node_tmp,
  113. &bat_priv->softif_neigh_list, list) {
  114. if ((!time_after(jiffies, softif_neigh->last_seen +
  115. msecs_to_jiffies(SOFTIF_NEIGH_TIMEOUT))) &&
  116. (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE))
  117. continue;
  118. if (curr_softif_neigh == softif_neigh) {
  119. bat_dbg(DBG_ROUTES, bat_priv,
  120. "Current mesh exit point '%pM' vanished "
  121. "(vid: %d).\n",
  122. softif_neigh->addr, softif_neigh->vid);
  123. do_deselect = 1;
  124. }
  125. hlist_del_rcu(&softif_neigh->list);
  126. softif_neigh_free_ref(softif_neigh);
  127. }
  128. spin_unlock_bh(&bat_priv->softif_neigh_lock);
  129. /* soft_neigh_deselect() needs to acquire the softif_neigh_lock */
  130. if (do_deselect)
  131. softif_neigh_deselect(bat_priv);
  132. if (curr_softif_neigh)
  133. softif_neigh_free_ref(curr_softif_neigh);
  134. }
  135. static struct softif_neigh *softif_neigh_get(struct bat_priv *bat_priv,
  136. uint8_t *addr, short vid)
  137. {
  138. struct softif_neigh *softif_neigh;
  139. struct hlist_node *node;
  140. rcu_read_lock();
  141. hlist_for_each_entry_rcu(softif_neigh, node,
  142. &bat_priv->softif_neigh_list, list) {
  143. if (!compare_eth(softif_neigh->addr, addr))
  144. continue;
  145. if (softif_neigh->vid != vid)
  146. continue;
  147. if (!atomic_inc_not_zero(&softif_neigh->refcount))
  148. continue;
  149. softif_neigh->last_seen = jiffies;
  150. goto out;
  151. }
  152. softif_neigh = kzalloc(sizeof(struct softif_neigh), GFP_ATOMIC);
  153. if (!softif_neigh)
  154. goto out;
  155. memcpy(softif_neigh->addr, addr, ETH_ALEN);
  156. softif_neigh->vid = vid;
  157. softif_neigh->last_seen = jiffies;
  158. /* initialize with 2 - caller decrements counter by one */
  159. atomic_set(&softif_neigh->refcount, 2);
  160. INIT_HLIST_NODE(&softif_neigh->list);
  161. spin_lock_bh(&bat_priv->softif_neigh_lock);
  162. hlist_add_head_rcu(&softif_neigh->list, &bat_priv->softif_neigh_list);
  163. spin_unlock_bh(&bat_priv->softif_neigh_lock);
  164. out:
  165. rcu_read_unlock();
  166. return softif_neigh;
  167. }
  168. int softif_neigh_seq_print_text(struct seq_file *seq, void *offset)
  169. {
  170. struct net_device *net_dev = (struct net_device *)seq->private;
  171. struct bat_priv *bat_priv = netdev_priv(net_dev);
  172. struct softif_neigh *softif_neigh;
  173. struct hard_iface *primary_if;
  174. struct hlist_node *node;
  175. struct softif_neigh *curr_softif_neigh;
  176. int ret = 0;
  177. primary_if = primary_if_get_selected(bat_priv);
  178. if (!primary_if) {
  179. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  180. "please specify interfaces to enable it\n",
  181. net_dev->name);
  182. goto out;
  183. }
  184. if (primary_if->if_status != IF_ACTIVE) {
  185. ret = seq_printf(seq, "BATMAN mesh %s "
  186. "disabled - primary interface not active\n",
  187. net_dev->name);
  188. goto out;
  189. }
  190. seq_printf(seq, "Softif neighbor list (%s)\n", net_dev->name);
  191. curr_softif_neigh = softif_neigh_get_selected(bat_priv);
  192. rcu_read_lock();
  193. hlist_for_each_entry_rcu(softif_neigh, node,
  194. &bat_priv->softif_neigh_list, list)
  195. seq_printf(seq, "%s %pM (vid: %d)\n",
  196. curr_softif_neigh == softif_neigh
  197. ? "=>" : " ", softif_neigh->addr,
  198. softif_neigh->vid);
  199. rcu_read_unlock();
  200. if (curr_softif_neigh)
  201. softif_neigh_free_ref(curr_softif_neigh);
  202. out:
  203. if (primary_if)
  204. hardif_free_ref(primary_if);
  205. return ret;
  206. }
  207. static void softif_batman_recv(struct sk_buff *skb, struct net_device *dev,
  208. short vid)
  209. {
  210. struct bat_priv *bat_priv = netdev_priv(dev);
  211. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  212. struct batman_packet *batman_packet;
  213. struct softif_neigh *softif_neigh = NULL;
  214. struct hard_iface *primary_if = NULL;
  215. struct softif_neigh *curr_softif_neigh = NULL;
  216. if (ntohs(ethhdr->h_proto) == ETH_P_8021Q)
  217. batman_packet = (struct batman_packet *)
  218. (skb->data + ETH_HLEN + VLAN_HLEN);
  219. else
  220. batman_packet = (struct batman_packet *)(skb->data + ETH_HLEN);
  221. if (batman_packet->version != COMPAT_VERSION)
  222. goto out;
  223. if (batman_packet->packet_type != BAT_PACKET)
  224. goto out;
  225. if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
  226. goto out;
  227. if (is_my_mac(batman_packet->orig))
  228. goto out;
  229. softif_neigh = softif_neigh_get(bat_priv, batman_packet->orig, vid);
  230. if (!softif_neigh)
  231. goto out;
  232. curr_softif_neigh = softif_neigh_get_selected(bat_priv);
  233. if (!curr_softif_neigh)
  234. goto out;
  235. if (curr_softif_neigh == softif_neigh)
  236. goto out;
  237. primary_if = primary_if_get_selected(bat_priv);
  238. if (!primary_if)
  239. goto out;
  240. /* we got a neighbor but its mac is 'bigger' than ours */
  241. if (memcmp(primary_if->net_dev->dev_addr,
  242. softif_neigh->addr, ETH_ALEN) < 0)
  243. goto out;
  244. /* switch to new 'smallest neighbor' */
  245. if ((curr_softif_neigh) &&
  246. (memcmp(softif_neigh->addr, curr_softif_neigh->addr,
  247. ETH_ALEN) < 0)) {
  248. bat_dbg(DBG_ROUTES, bat_priv,
  249. "Changing mesh exit point from %pM (vid: %d) "
  250. "to %pM (vid: %d).\n",
  251. curr_softif_neigh->addr,
  252. curr_softif_neigh->vid,
  253. softif_neigh->addr, softif_neigh->vid);
  254. softif_neigh_select(bat_priv, softif_neigh);
  255. goto out;
  256. }
  257. /* close own batX device and use softif_neigh as exit node */
  258. if ((!curr_softif_neigh) &&
  259. (memcmp(softif_neigh->addr,
  260. primary_if->net_dev->dev_addr, ETH_ALEN) < 0)) {
  261. bat_dbg(DBG_ROUTES, bat_priv,
  262. "Setting mesh exit point to %pM (vid: %d).\n",
  263. softif_neigh->addr, softif_neigh->vid);
  264. softif_neigh_select(bat_priv, softif_neigh);
  265. goto out;
  266. }
  267. out:
  268. kfree_skb(skb);
  269. if (softif_neigh)
  270. softif_neigh_free_ref(softif_neigh);
  271. if (curr_softif_neigh)
  272. softif_neigh_free_ref(curr_softif_neigh);
  273. if (primary_if)
  274. hardif_free_ref(primary_if);
  275. return;
  276. }
  277. static int interface_open(struct net_device *dev)
  278. {
  279. netif_start_queue(dev);
  280. return 0;
  281. }
  282. static int interface_release(struct net_device *dev)
  283. {
  284. netif_stop_queue(dev);
  285. return 0;
  286. }
  287. static struct net_device_stats *interface_stats(struct net_device *dev)
  288. {
  289. struct bat_priv *bat_priv = netdev_priv(dev);
  290. return &bat_priv->stats;
  291. }
  292. static int interface_set_mac_addr(struct net_device *dev, void *p)
  293. {
  294. struct bat_priv *bat_priv = netdev_priv(dev);
  295. struct sockaddr *addr = p;
  296. if (!is_valid_ether_addr(addr->sa_data))
  297. return -EADDRNOTAVAIL;
  298. /* only modify hna-table if it has been initialised before */
  299. if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
  300. hna_local_remove(bat_priv, dev->dev_addr,
  301. "mac address changed");
  302. hna_local_add(dev, addr->sa_data);
  303. }
  304. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  305. return 0;
  306. }
  307. static int interface_change_mtu(struct net_device *dev, int new_mtu)
  308. {
  309. /* check ranges */
  310. if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev)))
  311. return -EINVAL;
  312. dev->mtu = new_mtu;
  313. return 0;
  314. }
  315. int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
  316. {
  317. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  318. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  319. struct hard_iface *primary_if = NULL;
  320. struct bcast_packet *bcast_packet;
  321. struct vlan_ethhdr *vhdr;
  322. struct softif_neigh *curr_softif_neigh = NULL;
  323. int data_len = skb->len, ret;
  324. short vid = -1;
  325. bool do_bcast = false;
  326. if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
  327. goto dropped;
  328. soft_iface->trans_start = jiffies;
  329. switch (ntohs(ethhdr->h_proto)) {
  330. case ETH_P_8021Q:
  331. vhdr = (struct vlan_ethhdr *)skb->data;
  332. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  333. if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
  334. break;
  335. /* fall through */
  336. case ETH_P_BATMAN:
  337. softif_batman_recv(skb, soft_iface, vid);
  338. goto end;
  339. }
  340. /**
  341. * if we have a another chosen mesh exit node in range
  342. * it will transport the packets to the mesh
  343. */
  344. curr_softif_neigh = softif_neigh_get_selected(bat_priv);
  345. if ((curr_softif_neigh) && (curr_softif_neigh->vid == vid))
  346. goto dropped;
  347. /* TODO: check this for locks */
  348. hna_local_add(soft_iface, ethhdr->h_source);
  349. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  350. ret = gw_is_target(bat_priv, skb);
  351. if (ret < 0)
  352. goto dropped;
  353. if (ret == 0)
  354. do_bcast = true;
  355. }
  356. /* ethernet packet should be broadcasted */
  357. if (do_bcast) {
  358. primary_if = primary_if_get_selected(bat_priv);
  359. if (!primary_if)
  360. goto dropped;
  361. if (my_skb_head_push(skb, sizeof(struct bcast_packet)) < 0)
  362. goto dropped;
  363. bcast_packet = (struct bcast_packet *)skb->data;
  364. bcast_packet->version = COMPAT_VERSION;
  365. bcast_packet->ttl = TTL;
  366. /* batman packet type: broadcast */
  367. bcast_packet->packet_type = BAT_BCAST;
  368. /* hw address of first interface is the orig mac because only
  369. * this mac is known throughout the mesh */
  370. memcpy(bcast_packet->orig,
  371. primary_if->net_dev->dev_addr, ETH_ALEN);
  372. /* set broadcast sequence number */
  373. bcast_packet->seqno =
  374. htonl(atomic_inc_return(&bat_priv->bcast_seqno));
  375. add_bcast_packet_to_list(bat_priv, skb);
  376. /* a copy is stored in the bcast list, therefore removing
  377. * the original skb. */
  378. kfree_skb(skb);
  379. /* unicast packet */
  380. } else {
  381. ret = unicast_send_skb(skb, bat_priv);
  382. if (ret != 0)
  383. goto dropped_freed;
  384. }
  385. bat_priv->stats.tx_packets++;
  386. bat_priv->stats.tx_bytes += data_len;
  387. goto end;
  388. dropped:
  389. kfree_skb(skb);
  390. dropped_freed:
  391. bat_priv->stats.tx_dropped++;
  392. end:
  393. if (curr_softif_neigh)
  394. softif_neigh_free_ref(curr_softif_neigh);
  395. if (primary_if)
  396. hardif_free_ref(primary_if);
  397. return NETDEV_TX_OK;
  398. }
  399. void interface_rx(struct net_device *soft_iface,
  400. struct sk_buff *skb, struct hard_iface *recv_if,
  401. int hdr_size)
  402. {
  403. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  404. struct unicast_packet *unicast_packet;
  405. struct ethhdr *ethhdr;
  406. struct vlan_ethhdr *vhdr;
  407. struct softif_neigh *curr_softif_neigh = NULL;
  408. short vid = -1;
  409. int ret;
  410. /* check if enough space is available for pulling, and pull */
  411. if (!pskb_may_pull(skb, hdr_size))
  412. goto dropped;
  413. skb_pull_rcsum(skb, hdr_size);
  414. skb_reset_mac_header(skb);
  415. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  416. switch (ntohs(ethhdr->h_proto)) {
  417. case ETH_P_8021Q:
  418. vhdr = (struct vlan_ethhdr *)skb->data;
  419. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  420. if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
  421. break;
  422. /* fall through */
  423. case ETH_P_BATMAN:
  424. goto dropped;
  425. }
  426. /**
  427. * if we have a another chosen mesh exit node in range
  428. * it will transport the packets to the non-mesh network
  429. */
  430. curr_softif_neigh = softif_neigh_get_selected(bat_priv);
  431. if (curr_softif_neigh && (curr_softif_neigh->vid == vid)) {
  432. skb_push(skb, hdr_size);
  433. unicast_packet = (struct unicast_packet *)skb->data;
  434. if ((unicast_packet->packet_type != BAT_UNICAST) &&
  435. (unicast_packet->packet_type != BAT_UNICAST_FRAG))
  436. goto dropped;
  437. skb_reset_mac_header(skb);
  438. memcpy(unicast_packet->dest,
  439. curr_softif_neigh->addr, ETH_ALEN);
  440. ret = route_unicast_packet(skb, recv_if);
  441. if (ret == NET_RX_DROP)
  442. goto dropped;
  443. goto out;
  444. }
  445. /* skb->dev & skb->pkt_type are set here */
  446. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  447. goto dropped;
  448. skb->protocol = eth_type_trans(skb, soft_iface);
  449. /* should not be necessary anymore as we use skb_pull_rcsum()
  450. * TODO: please verify this and remove this TODO
  451. * -- Dec 21st 2009, Simon Wunderlich */
  452. /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
  453. bat_priv->stats.rx_packets++;
  454. bat_priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr);
  455. soft_iface->last_rx = jiffies;
  456. netif_rx(skb);
  457. goto out;
  458. dropped:
  459. kfree_skb(skb);
  460. out:
  461. if (curr_softif_neigh)
  462. softif_neigh_free_ref(curr_softif_neigh);
  463. return;
  464. }
  465. #ifdef HAVE_NET_DEVICE_OPS
  466. static const struct net_device_ops bat_netdev_ops = {
  467. .ndo_open = interface_open,
  468. .ndo_stop = interface_release,
  469. .ndo_get_stats = interface_stats,
  470. .ndo_set_mac_address = interface_set_mac_addr,
  471. .ndo_change_mtu = interface_change_mtu,
  472. .ndo_start_xmit = interface_tx,
  473. .ndo_validate_addr = eth_validate_addr
  474. };
  475. #endif
  476. static void interface_setup(struct net_device *dev)
  477. {
  478. struct bat_priv *priv = netdev_priv(dev);
  479. char dev_addr[ETH_ALEN];
  480. ether_setup(dev);
  481. #ifdef HAVE_NET_DEVICE_OPS
  482. dev->netdev_ops = &bat_netdev_ops;
  483. #else
  484. dev->open = interface_open;
  485. dev->stop = interface_release;
  486. dev->get_stats = interface_stats;
  487. dev->set_mac_address = interface_set_mac_addr;
  488. dev->change_mtu = interface_change_mtu;
  489. dev->hard_start_xmit = interface_tx;
  490. #endif
  491. dev->destructor = free_netdev;
  492. dev->tx_queue_len = 0;
  493. /**
  494. * can't call min_mtu, because the needed variables
  495. * have not been initialized yet
  496. */
  497. dev->mtu = ETH_DATA_LEN;
  498. dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
  499. * skbuff for our header */
  500. /* generate random address */
  501. random_ether_addr(dev_addr);
  502. memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
  503. SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
  504. memset(priv, 0, sizeof(struct bat_priv));
  505. }
  506. struct net_device *softif_create(char *name)
  507. {
  508. struct net_device *soft_iface;
  509. struct bat_priv *bat_priv;
  510. int ret;
  511. soft_iface = alloc_netdev(sizeof(struct bat_priv) , name,
  512. interface_setup);
  513. if (!soft_iface) {
  514. pr_err("Unable to allocate the batman interface: %s\n", name);
  515. goto out;
  516. }
  517. ret = register_netdev(soft_iface);
  518. if (ret < 0) {
  519. pr_err("Unable to register the batman interface '%s': %i\n",
  520. name, ret);
  521. goto free_soft_iface;
  522. }
  523. bat_priv = netdev_priv(soft_iface);
  524. atomic_set(&bat_priv->aggregated_ogms, 1);
  525. atomic_set(&bat_priv->bonding, 0);
  526. atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
  527. atomic_set(&bat_priv->gw_mode, GW_MODE_OFF);
  528. atomic_set(&bat_priv->gw_sel_class, 20);
  529. atomic_set(&bat_priv->gw_bandwidth, 41);
  530. atomic_set(&bat_priv->orig_interval, 1000);
  531. atomic_set(&bat_priv->hop_penalty, 10);
  532. atomic_set(&bat_priv->log_level, 0);
  533. atomic_set(&bat_priv->fragmentation, 1);
  534. atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
  535. atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
  536. atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
  537. atomic_set(&bat_priv->bcast_seqno, 1);
  538. atomic_set(&bat_priv->hna_local_changed, 0);
  539. bat_priv->primary_if = NULL;
  540. bat_priv->num_ifaces = 0;
  541. bat_priv->softif_neigh = NULL;
  542. ret = sysfs_add_meshif(soft_iface);
  543. if (ret < 0)
  544. goto unreg_soft_iface;
  545. ret = debugfs_add_meshif(soft_iface);
  546. if (ret < 0)
  547. goto unreg_sysfs;
  548. ret = mesh_init(soft_iface);
  549. if (ret < 0)
  550. goto unreg_debugfs;
  551. return soft_iface;
  552. unreg_debugfs:
  553. debugfs_del_meshif(soft_iface);
  554. unreg_sysfs:
  555. sysfs_del_meshif(soft_iface);
  556. unreg_soft_iface:
  557. unregister_netdev(soft_iface);
  558. return NULL;
  559. free_soft_iface:
  560. free_netdev(soft_iface);
  561. out:
  562. return NULL;
  563. }
  564. void softif_destroy(struct net_device *soft_iface)
  565. {
  566. debugfs_del_meshif(soft_iface);
  567. sysfs_del_meshif(soft_iface);
  568. mesh_free(soft_iface);
  569. unregister_netdevice(soft_iface);
  570. }
  571. int softif_is_valid(struct net_device *net_dev)
  572. {
  573. #ifdef HAVE_NET_DEVICE_OPS
  574. if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
  575. return 1;
  576. #else
  577. if (net_dev->hard_start_xmit == interface_tx)
  578. return 1;
  579. #endif
  580. return 0;
  581. }
  582. /* ethtool */
  583. static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  584. {
  585. cmd->supported = 0;
  586. cmd->advertising = 0;
  587. ethtool_cmd_speed_set(cmd, SPEED_10);
  588. cmd->duplex = DUPLEX_FULL;
  589. cmd->port = PORT_TP;
  590. cmd->phy_address = 0;
  591. cmd->transceiver = XCVR_INTERNAL;
  592. cmd->autoneg = AUTONEG_DISABLE;
  593. cmd->maxtxpkt = 0;
  594. cmd->maxrxpkt = 0;
  595. return 0;
  596. }
  597. static void bat_get_drvinfo(struct net_device *dev,
  598. struct ethtool_drvinfo *info)
  599. {
  600. strcpy(info->driver, "B.A.T.M.A.N. advanced");
  601. strcpy(info->version, SOURCE_VERSION);
  602. strcpy(info->fw_version, "N/A");
  603. strcpy(info->bus_info, "batman");
  604. }
  605. static u32 bat_get_msglevel(struct net_device *dev)
  606. {
  607. return -EOPNOTSUPP;
  608. }
  609. static void bat_set_msglevel(struct net_device *dev, u32 value)
  610. {
  611. }
  612. static u32 bat_get_link(struct net_device *dev)
  613. {
  614. return 1;
  615. }