soft-interface.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "soft-interface.h"
  21. #include "hard-interface.h"
  22. #include "distributed-arp-table.h"
  23. #include "routing.h"
  24. #include "send.h"
  25. #include "debugfs.h"
  26. #include "translation-table.h"
  27. #include "hash.h"
  28. #include "gateway_common.h"
  29. #include "gateway_client.h"
  30. #include "sysfs.h"
  31. #include "originator.h"
  32. #include <linux/slab.h>
  33. #include <linux/ethtool.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/if_vlan.h>
  36. #include "bridge_loop_avoidance.h"
  37. #include "network-coding.h"
  38. static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
  39. static void batadv_get_drvinfo(struct net_device *dev,
  40. struct ethtool_drvinfo *info);
  41. static u32 batadv_get_msglevel(struct net_device *dev);
  42. static void batadv_set_msglevel(struct net_device *dev, u32 value);
  43. static u32 batadv_get_link(struct net_device *dev);
  44. static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data);
  45. static void batadv_get_ethtool_stats(struct net_device *dev,
  46. struct ethtool_stats *stats, u64 *data);
  47. static int batadv_get_sset_count(struct net_device *dev, int stringset);
  48. static const struct ethtool_ops batadv_ethtool_ops = {
  49. .get_settings = batadv_get_settings,
  50. .get_drvinfo = batadv_get_drvinfo,
  51. .get_msglevel = batadv_get_msglevel,
  52. .set_msglevel = batadv_set_msglevel,
  53. .get_link = batadv_get_link,
  54. .get_strings = batadv_get_strings,
  55. .get_ethtool_stats = batadv_get_ethtool_stats,
  56. .get_sset_count = batadv_get_sset_count,
  57. };
  58. int batadv_skb_head_push(struct sk_buff *skb, unsigned int len)
  59. {
  60. int result;
  61. /* TODO: We must check if we can release all references to non-payload
  62. * data using skb_header_release in our skbs to allow skb_cow_header to
  63. * work optimally. This means that those skbs are not allowed to read
  64. * or write any data which is before the current position of skb->data
  65. * after that call and thus allow other skbs with the same data buffer
  66. * to write freely in that area.
  67. */
  68. result = skb_cow_head(skb, len);
  69. if (result < 0)
  70. return result;
  71. skb_push(skb, len);
  72. return 0;
  73. }
  74. static int batadv_interface_open(struct net_device *dev)
  75. {
  76. netif_start_queue(dev);
  77. return 0;
  78. }
  79. static int batadv_interface_release(struct net_device *dev)
  80. {
  81. netif_stop_queue(dev);
  82. return 0;
  83. }
  84. static struct net_device_stats *batadv_interface_stats(struct net_device *dev)
  85. {
  86. struct batadv_priv *bat_priv = netdev_priv(dev);
  87. struct net_device_stats *stats = &bat_priv->stats;
  88. stats->tx_packets = batadv_sum_counter(bat_priv, BATADV_CNT_TX);
  89. stats->tx_bytes = batadv_sum_counter(bat_priv, BATADV_CNT_TX_BYTES);
  90. stats->tx_dropped = batadv_sum_counter(bat_priv, BATADV_CNT_TX_DROPPED);
  91. stats->rx_packets = batadv_sum_counter(bat_priv, BATADV_CNT_RX);
  92. stats->rx_bytes = batadv_sum_counter(bat_priv, BATADV_CNT_RX_BYTES);
  93. return stats;
  94. }
  95. static int batadv_interface_set_mac_addr(struct net_device *dev, void *p)
  96. {
  97. struct batadv_priv *bat_priv = netdev_priv(dev);
  98. struct sockaddr *addr = p;
  99. uint8_t old_addr[ETH_ALEN];
  100. if (!is_valid_ether_addr(addr->sa_data))
  101. return -EADDRNOTAVAIL;
  102. memcpy(old_addr, dev->dev_addr, ETH_ALEN);
  103. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  104. /* only modify transtable if it has been initialized before */
  105. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE) {
  106. batadv_tt_local_remove(bat_priv, old_addr,
  107. "mac address changed", false);
  108. batadv_tt_local_add(dev, addr->sa_data, BATADV_NULL_IFINDEX);
  109. }
  110. return 0;
  111. }
  112. static int batadv_interface_change_mtu(struct net_device *dev, int new_mtu)
  113. {
  114. /* check ranges */
  115. if ((new_mtu < 68) || (new_mtu > batadv_hardif_min_mtu(dev)))
  116. return -EINVAL;
  117. dev->mtu = new_mtu;
  118. return 0;
  119. }
  120. /**
  121. * batadv_interface_set_rx_mode - set the rx mode of a device
  122. * @dev: registered network device to modify
  123. *
  124. * We do not actually need to set any rx filters for the virtual batman
  125. * soft interface. However a dummy handler enables a user to set static
  126. * multicast listeners for instance.
  127. */
  128. static void batadv_interface_set_rx_mode(struct net_device *dev)
  129. {
  130. }
  131. static int batadv_interface_tx(struct sk_buff *skb,
  132. struct net_device *soft_iface)
  133. {
  134. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  135. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  136. struct batadv_hard_iface *primary_if = NULL;
  137. struct batadv_bcast_packet *bcast_packet;
  138. struct vlan_ethhdr *vhdr;
  139. __be16 ethertype = htons(ETH_P_BATMAN);
  140. static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00,
  141. 0x00, 0x00};
  142. static const uint8_t ectp_addr[ETH_ALEN] = {0xCF, 0x00, 0x00, 0x00,
  143. 0x00, 0x00};
  144. unsigned int header_len = 0;
  145. int data_len = skb->len, ret;
  146. unsigned short vid __maybe_unused = BATADV_NO_FLAGS;
  147. bool do_bcast = false;
  148. uint32_t seqno;
  149. unsigned long brd_delay = 1;
  150. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  151. goto dropped;
  152. soft_iface->trans_start = jiffies;
  153. switch (ntohs(ethhdr->h_proto)) {
  154. case ETH_P_8021Q:
  155. vhdr = (struct vlan_ethhdr *)skb->data;
  156. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  157. vid |= BATADV_VLAN_HAS_TAG;
  158. if (vhdr->h_vlan_encapsulated_proto != ethertype)
  159. break;
  160. /* fall through */
  161. case ETH_P_BATMAN:
  162. goto dropped;
  163. }
  164. if (batadv_bla_tx(bat_priv, skb, vid))
  165. goto dropped;
  166. /* skb->data might have been reallocated by batadv_bla_tx() */
  167. ethhdr = (struct ethhdr *)skb->data;
  168. /* Register the client MAC in the transtable */
  169. if (!is_multicast_ether_addr(ethhdr->h_source))
  170. batadv_tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
  171. /* don't accept stp packets. STP does not help in meshes.
  172. * better use the bridge loop avoidance ...
  173. *
  174. * The same goes for ECTP sent at least by some Cisco Switches,
  175. * it might confuse the mesh when used with bridge loop avoidance.
  176. */
  177. if (batadv_compare_eth(ethhdr->h_dest, stp_addr))
  178. goto dropped;
  179. if (batadv_compare_eth(ethhdr->h_dest, ectp_addr))
  180. goto dropped;
  181. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  182. do_bcast = true;
  183. switch (atomic_read(&bat_priv->gw_mode)) {
  184. case BATADV_GW_MODE_SERVER:
  185. /* gateway servers should not send dhcp
  186. * requests into the mesh
  187. */
  188. ret = batadv_gw_is_dhcp_target(skb, &header_len);
  189. if (ret)
  190. goto dropped;
  191. break;
  192. case BATADV_GW_MODE_CLIENT:
  193. /* gateway clients should send dhcp requests
  194. * via unicast to their gateway
  195. */
  196. ret = batadv_gw_is_dhcp_target(skb, &header_len);
  197. if (ret)
  198. do_bcast = false;
  199. break;
  200. case BATADV_GW_MODE_OFF:
  201. default:
  202. break;
  203. }
  204. /* reminder: ethhdr might have become unusable from here on
  205. * (batadv_gw_is_dhcp_target() might have reallocated skb data)
  206. */
  207. }
  208. batadv_skb_set_priority(skb, 0);
  209. /* ethernet packet should be broadcasted */
  210. if (do_bcast) {
  211. primary_if = batadv_primary_if_get_selected(bat_priv);
  212. if (!primary_if)
  213. goto dropped;
  214. /* in case of ARP request, we do not immediately broadcasti the
  215. * packet, instead we first wait for DAT to try to retrieve the
  216. * correct ARP entry
  217. */
  218. if (batadv_dat_snoop_outgoing_arp_request(bat_priv, skb))
  219. brd_delay = msecs_to_jiffies(ARP_REQ_DELAY);
  220. if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
  221. goto dropped;
  222. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  223. bcast_packet->header.version = BATADV_COMPAT_VERSION;
  224. bcast_packet->header.ttl = BATADV_TTL;
  225. /* batman packet type: broadcast */
  226. bcast_packet->header.packet_type = BATADV_BCAST;
  227. bcast_packet->reserved = 0;
  228. /* hw address of first interface is the orig mac because only
  229. * this mac is known throughout the mesh
  230. */
  231. memcpy(bcast_packet->orig,
  232. primary_if->net_dev->dev_addr, ETH_ALEN);
  233. /* set broadcast sequence number */
  234. seqno = atomic_inc_return(&bat_priv->bcast_seqno);
  235. bcast_packet->seqno = htonl(seqno);
  236. batadv_add_bcast_packet_to_list(bat_priv, skb, brd_delay);
  237. /* a copy is stored in the bcast list, therefore removing
  238. * the original skb.
  239. */
  240. kfree_skb(skb);
  241. /* unicast packet */
  242. } else {
  243. if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_OFF) {
  244. ret = batadv_gw_out_of_range(bat_priv, skb);
  245. if (ret)
  246. goto dropped;
  247. }
  248. if (batadv_dat_snoop_outgoing_arp_request(bat_priv, skb))
  249. goto dropped;
  250. batadv_dat_snoop_outgoing_arp_reply(bat_priv, skb);
  251. ret = batadv_send_skb_unicast(bat_priv, skb);
  252. if (ret != 0)
  253. goto dropped_freed;
  254. }
  255. batadv_inc_counter(bat_priv, BATADV_CNT_TX);
  256. batadv_add_counter(bat_priv, BATADV_CNT_TX_BYTES, data_len);
  257. goto end;
  258. dropped:
  259. kfree_skb(skb);
  260. dropped_freed:
  261. batadv_inc_counter(bat_priv, BATADV_CNT_TX_DROPPED);
  262. end:
  263. if (primary_if)
  264. batadv_hardif_free_ref(primary_if);
  265. return NETDEV_TX_OK;
  266. }
  267. void batadv_interface_rx(struct net_device *soft_iface,
  268. struct sk_buff *skb, struct batadv_hard_iface *recv_if,
  269. int hdr_size, struct batadv_orig_node *orig_node)
  270. {
  271. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  272. struct ethhdr *ethhdr;
  273. struct vlan_ethhdr *vhdr;
  274. struct batadv_header *batadv_header = (struct batadv_header *)skb->data;
  275. unsigned short vid __maybe_unused = BATADV_NO_FLAGS;
  276. __be16 ethertype = htons(ETH_P_BATMAN);
  277. bool is_bcast;
  278. is_bcast = (batadv_header->packet_type == BATADV_BCAST);
  279. /* check if enough space is available for pulling, and pull */
  280. if (!pskb_may_pull(skb, hdr_size))
  281. goto dropped;
  282. skb_pull_rcsum(skb, hdr_size);
  283. skb_reset_mac_header(skb);
  284. ethhdr = eth_hdr(skb);
  285. switch (ntohs(ethhdr->h_proto)) {
  286. case ETH_P_8021Q:
  287. vhdr = (struct vlan_ethhdr *)skb->data;
  288. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  289. vid |= BATADV_VLAN_HAS_TAG;
  290. if (vhdr->h_vlan_encapsulated_proto != ethertype)
  291. break;
  292. /* fall through */
  293. case ETH_P_BATMAN:
  294. goto dropped;
  295. }
  296. /* skb->dev & skb->pkt_type are set here */
  297. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  298. goto dropped;
  299. skb->protocol = eth_type_trans(skb, soft_iface);
  300. /* should not be necessary anymore as we use skb_pull_rcsum()
  301. * TODO: please verify this and remove this TODO
  302. * -- Dec 21st 2009, Simon Wunderlich
  303. */
  304. /* skb->ip_summed = CHECKSUM_UNNECESSARY; */
  305. batadv_inc_counter(bat_priv, BATADV_CNT_RX);
  306. batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
  307. skb->len + ETH_HLEN);
  308. soft_iface->last_rx = jiffies;
  309. /* Let the bridge loop avoidance check the packet. If will
  310. * not handle it, we can safely push it up.
  311. */
  312. if (batadv_bla_rx(bat_priv, skb, vid, is_bcast))
  313. goto out;
  314. if (orig_node)
  315. batadv_tt_add_temporary_global_entry(bat_priv, orig_node,
  316. ethhdr->h_source);
  317. if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
  318. goto dropped;
  319. netif_rx(skb);
  320. goto out;
  321. dropped:
  322. kfree_skb(skb);
  323. out:
  324. return;
  325. }
  326. /* batman-adv network devices have devices nesting below it and are a special
  327. * "super class" of normal network devices; split their locks off into a
  328. * separate class since they always nest.
  329. */
  330. static struct lock_class_key batadv_netdev_xmit_lock_key;
  331. static struct lock_class_key batadv_netdev_addr_lock_key;
  332. /**
  333. * batadv_set_lockdep_class_one - Set lockdep class for a single tx queue
  334. * @dev: device which owns the tx queue
  335. * @txq: tx queue to modify
  336. * @_unused: always NULL
  337. */
  338. static void batadv_set_lockdep_class_one(struct net_device *dev,
  339. struct netdev_queue *txq,
  340. void *_unused)
  341. {
  342. lockdep_set_class(&txq->_xmit_lock, &batadv_netdev_xmit_lock_key);
  343. }
  344. /**
  345. * batadv_set_lockdep_class - Set txq and addr_list lockdep class
  346. * @dev: network device to modify
  347. */
  348. static void batadv_set_lockdep_class(struct net_device *dev)
  349. {
  350. lockdep_set_class(&dev->addr_list_lock, &batadv_netdev_addr_lock_key);
  351. netdev_for_each_tx_queue(dev, batadv_set_lockdep_class_one, NULL);
  352. }
  353. /**
  354. * batadv_softif_destroy_finish - cleans up the remains of a softif
  355. * @work: work queue item
  356. *
  357. * Free the parts of the soft interface which can not be removed under
  358. * rtnl lock (to prevent deadlock situations).
  359. */
  360. static void batadv_softif_destroy_finish(struct work_struct *work)
  361. {
  362. struct batadv_priv *bat_priv;
  363. struct net_device *soft_iface;
  364. bat_priv = container_of(work, struct batadv_priv,
  365. cleanup_work);
  366. soft_iface = bat_priv->soft_iface;
  367. batadv_sysfs_del_meshif(soft_iface);
  368. rtnl_lock();
  369. unregister_netdevice(soft_iface);
  370. rtnl_unlock();
  371. }
  372. /**
  373. * batadv_softif_init_late - late stage initialization of soft interface
  374. * @dev: registered network device to modify
  375. *
  376. * Returns error code on failures
  377. */
  378. static int batadv_softif_init_late(struct net_device *dev)
  379. {
  380. struct batadv_priv *bat_priv;
  381. uint32_t random_seqno;
  382. int ret;
  383. size_t cnt_len = sizeof(uint64_t) * BATADV_CNT_NUM;
  384. batadv_set_lockdep_class(dev);
  385. bat_priv = netdev_priv(dev);
  386. bat_priv->soft_iface = dev;
  387. INIT_WORK(&bat_priv->cleanup_work, batadv_softif_destroy_finish);
  388. /* batadv_interface_stats() needs to be available as soon as
  389. * register_netdevice() has been called
  390. */
  391. bat_priv->bat_counters = __alloc_percpu(cnt_len, __alignof__(uint64_t));
  392. if (!bat_priv->bat_counters)
  393. return -ENOMEM;
  394. atomic_set(&bat_priv->aggregated_ogms, 1);
  395. atomic_set(&bat_priv->bonding, 0);
  396. #ifdef CONFIG_BATMAN_ADV_BLA
  397. atomic_set(&bat_priv->bridge_loop_avoidance, 0);
  398. #endif
  399. #ifdef CONFIG_BATMAN_ADV_DAT
  400. atomic_set(&bat_priv->distributed_arp_table, 1);
  401. #endif
  402. atomic_set(&bat_priv->ap_isolation, 0);
  403. atomic_set(&bat_priv->gw_mode, BATADV_GW_MODE_OFF);
  404. atomic_set(&bat_priv->gw_sel_class, 20);
  405. atomic_set(&bat_priv->gw.bandwidth_down, 100);
  406. atomic_set(&bat_priv->gw.bandwidth_up, 20);
  407. atomic_set(&bat_priv->orig_interval, 1000);
  408. atomic_set(&bat_priv->hop_penalty, 30);
  409. #ifdef CONFIG_BATMAN_ADV_DEBUG
  410. atomic_set(&bat_priv->log_level, 0);
  411. #endif
  412. atomic_set(&bat_priv->fragmentation, 1);
  413. atomic_set(&bat_priv->bcast_queue_left, BATADV_BCAST_QUEUE_LEN);
  414. atomic_set(&bat_priv->batman_queue_left, BATADV_BATMAN_QUEUE_LEN);
  415. atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
  416. atomic_set(&bat_priv->bcast_seqno, 1);
  417. atomic_set(&bat_priv->tt.vn, 0);
  418. atomic_set(&bat_priv->tt.local_changes, 0);
  419. atomic_set(&bat_priv->tt.ogm_append_cnt, 0);
  420. #ifdef CONFIG_BATMAN_ADV_BLA
  421. atomic_set(&bat_priv->bla.num_requests, 0);
  422. #endif
  423. bat_priv->tt.last_changeset = NULL;
  424. bat_priv->tt.last_changeset_len = 0;
  425. /* randomize initial seqno to avoid collision */
  426. get_random_bytes(&random_seqno, sizeof(random_seqno));
  427. atomic_set(&bat_priv->frag_seqno, random_seqno);
  428. bat_priv->primary_if = NULL;
  429. bat_priv->num_ifaces = 0;
  430. batadv_nc_init_bat_priv(bat_priv);
  431. ret = batadv_algo_select(bat_priv, batadv_routing_algo);
  432. if (ret < 0)
  433. goto free_bat_counters;
  434. ret = batadv_debugfs_add_meshif(dev);
  435. if (ret < 0)
  436. goto free_bat_counters;
  437. ret = batadv_mesh_init(dev);
  438. if (ret < 0)
  439. goto unreg_debugfs;
  440. return 0;
  441. unreg_debugfs:
  442. batadv_debugfs_del_meshif(dev);
  443. free_bat_counters:
  444. free_percpu(bat_priv->bat_counters);
  445. bat_priv->bat_counters = NULL;
  446. return ret;
  447. }
  448. /**
  449. * batadv_softif_slave_add - Add a slave interface to a batadv_soft_interface
  450. * @dev: batadv_soft_interface used as master interface
  451. * @slave_dev: net_device which should become the slave interface
  452. *
  453. * Return 0 if successful or error otherwise.
  454. */
  455. static int batadv_softif_slave_add(struct net_device *dev,
  456. struct net_device *slave_dev)
  457. {
  458. struct batadv_hard_iface *hard_iface;
  459. int ret = -EINVAL;
  460. hard_iface = batadv_hardif_get_by_netdev(slave_dev);
  461. if (!hard_iface || hard_iface->soft_iface != NULL)
  462. goto out;
  463. ret = batadv_hardif_enable_interface(hard_iface, dev->name);
  464. out:
  465. if (hard_iface)
  466. batadv_hardif_free_ref(hard_iface);
  467. return ret;
  468. }
  469. /**
  470. * batadv_softif_slave_del - Delete a slave iface from a batadv_soft_interface
  471. * @dev: batadv_soft_interface used as master interface
  472. * @slave_dev: net_device which should be removed from the master interface
  473. *
  474. * Return 0 if successful or error otherwise.
  475. */
  476. static int batadv_softif_slave_del(struct net_device *dev,
  477. struct net_device *slave_dev)
  478. {
  479. struct batadv_hard_iface *hard_iface;
  480. int ret = -EINVAL;
  481. hard_iface = batadv_hardif_get_by_netdev(slave_dev);
  482. if (!hard_iface || hard_iface->soft_iface != dev)
  483. goto out;
  484. batadv_hardif_disable_interface(hard_iface, BATADV_IF_CLEANUP_KEEP);
  485. ret = 0;
  486. out:
  487. if (hard_iface)
  488. batadv_hardif_free_ref(hard_iface);
  489. return ret;
  490. }
  491. static const struct net_device_ops batadv_netdev_ops = {
  492. .ndo_init = batadv_softif_init_late,
  493. .ndo_open = batadv_interface_open,
  494. .ndo_stop = batadv_interface_release,
  495. .ndo_get_stats = batadv_interface_stats,
  496. .ndo_set_mac_address = batadv_interface_set_mac_addr,
  497. .ndo_change_mtu = batadv_interface_change_mtu,
  498. .ndo_set_rx_mode = batadv_interface_set_rx_mode,
  499. .ndo_start_xmit = batadv_interface_tx,
  500. .ndo_validate_addr = eth_validate_addr,
  501. .ndo_add_slave = batadv_softif_slave_add,
  502. .ndo_del_slave = batadv_softif_slave_del,
  503. };
  504. /**
  505. * batadv_softif_free - Deconstructor of batadv_soft_interface
  506. * @dev: Device to cleanup and remove
  507. */
  508. static void batadv_softif_free(struct net_device *dev)
  509. {
  510. batadv_debugfs_del_meshif(dev);
  511. batadv_mesh_free(dev);
  512. /* some scheduled RCU callbacks need the bat_priv struct to accomplish
  513. * their tasks. Wait for them all to be finished before freeing the
  514. * netdev and its private data (bat_priv)
  515. */
  516. rcu_barrier();
  517. free_netdev(dev);
  518. }
  519. /**
  520. * batadv_softif_init_early - early stage initialization of soft interface
  521. * @dev: registered network device to modify
  522. */
  523. static void batadv_softif_init_early(struct net_device *dev)
  524. {
  525. struct batadv_priv *priv = netdev_priv(dev);
  526. ether_setup(dev);
  527. dev->netdev_ops = &batadv_netdev_ops;
  528. dev->destructor = batadv_softif_free;
  529. dev->tx_queue_len = 0;
  530. /* can't call min_mtu, because the needed variables
  531. * have not been initialized yet
  532. */
  533. dev->mtu = ETH_DATA_LEN;
  534. /* reserve more space in the skbuff for our header */
  535. dev->hard_header_len = batadv_max_header_len();
  536. /* generate random address */
  537. eth_hw_addr_random(dev);
  538. SET_ETHTOOL_OPS(dev, &batadv_ethtool_ops);
  539. memset(priv, 0, sizeof(*priv));
  540. }
  541. struct net_device *batadv_softif_create(const char *name)
  542. {
  543. struct net_device *soft_iface;
  544. int ret;
  545. soft_iface = alloc_netdev(sizeof(struct batadv_priv), name,
  546. batadv_softif_init_early);
  547. if (!soft_iface)
  548. return NULL;
  549. soft_iface->rtnl_link_ops = &batadv_link_ops;
  550. ret = register_netdevice(soft_iface);
  551. if (ret < 0) {
  552. pr_err("Unable to register the batman interface '%s': %i\n",
  553. name, ret);
  554. free_netdev(soft_iface);
  555. return NULL;
  556. }
  557. return soft_iface;
  558. }
  559. /**
  560. * batadv_softif_destroy_sysfs - deletion of batadv_soft_interface via sysfs
  561. * @soft_iface: the to-be-removed batman-adv interface
  562. */
  563. void batadv_softif_destroy_sysfs(struct net_device *soft_iface)
  564. {
  565. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  566. queue_work(batadv_event_workqueue, &bat_priv->cleanup_work);
  567. }
  568. /**
  569. * batadv_softif_destroy_netlink - deletion of batadv_soft_interface via netlink
  570. * @soft_iface: the to-be-removed batman-adv interface
  571. * @head: list pointer
  572. */
  573. static void batadv_softif_destroy_netlink(struct net_device *soft_iface,
  574. struct list_head *head)
  575. {
  576. struct batadv_hard_iface *hard_iface;
  577. list_for_each_entry(hard_iface, &batadv_hardif_list, list) {
  578. if (hard_iface->soft_iface == soft_iface)
  579. batadv_hardif_disable_interface(hard_iface,
  580. BATADV_IF_CLEANUP_KEEP);
  581. }
  582. batadv_sysfs_del_meshif(soft_iface);
  583. unregister_netdevice_queue(soft_iface, head);
  584. }
  585. int batadv_softif_is_valid(const struct net_device *net_dev)
  586. {
  587. if (net_dev->netdev_ops->ndo_start_xmit == batadv_interface_tx)
  588. return 1;
  589. return 0;
  590. }
  591. struct rtnl_link_ops batadv_link_ops __read_mostly = {
  592. .kind = "batadv",
  593. .priv_size = sizeof(struct batadv_priv),
  594. .setup = batadv_softif_init_early,
  595. .dellink = batadv_softif_destroy_netlink,
  596. };
  597. /* ethtool */
  598. static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  599. {
  600. cmd->supported = 0;
  601. cmd->advertising = 0;
  602. ethtool_cmd_speed_set(cmd, SPEED_10);
  603. cmd->duplex = DUPLEX_FULL;
  604. cmd->port = PORT_TP;
  605. cmd->phy_address = 0;
  606. cmd->transceiver = XCVR_INTERNAL;
  607. cmd->autoneg = AUTONEG_DISABLE;
  608. cmd->maxtxpkt = 0;
  609. cmd->maxrxpkt = 0;
  610. return 0;
  611. }
  612. static void batadv_get_drvinfo(struct net_device *dev,
  613. struct ethtool_drvinfo *info)
  614. {
  615. strlcpy(info->driver, "B.A.T.M.A.N. advanced", sizeof(info->driver));
  616. strlcpy(info->version, BATADV_SOURCE_VERSION, sizeof(info->version));
  617. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  618. strlcpy(info->bus_info, "batman", sizeof(info->bus_info));
  619. }
  620. static u32 batadv_get_msglevel(struct net_device *dev)
  621. {
  622. return -EOPNOTSUPP;
  623. }
  624. static void batadv_set_msglevel(struct net_device *dev, u32 value)
  625. {
  626. }
  627. static u32 batadv_get_link(struct net_device *dev)
  628. {
  629. return 1;
  630. }
  631. /* Inspired by drivers/net/ethernet/dlink/sundance.c:1702
  632. * Declare each description string in struct.name[] to get fixed sized buffer
  633. * and compile time checking for strings longer than ETH_GSTRING_LEN.
  634. */
  635. static const struct {
  636. const char name[ETH_GSTRING_LEN];
  637. } batadv_counters_strings[] = {
  638. { "tx" },
  639. { "tx_bytes" },
  640. { "tx_dropped" },
  641. { "rx" },
  642. { "rx_bytes" },
  643. { "forward" },
  644. { "forward_bytes" },
  645. { "mgmt_tx" },
  646. { "mgmt_tx_bytes" },
  647. { "mgmt_rx" },
  648. { "mgmt_rx_bytes" },
  649. { "frag_tx" },
  650. { "frag_tx_bytes" },
  651. { "frag_rx" },
  652. { "frag_rx_bytes" },
  653. { "frag_fwd" },
  654. { "frag_fwd_bytes" },
  655. { "tt_request_tx" },
  656. { "tt_request_rx" },
  657. { "tt_response_tx" },
  658. { "tt_response_rx" },
  659. { "tt_roam_adv_tx" },
  660. { "tt_roam_adv_rx" },
  661. #ifdef CONFIG_BATMAN_ADV_DAT
  662. { "dat_get_tx" },
  663. { "dat_get_rx" },
  664. { "dat_put_tx" },
  665. { "dat_put_rx" },
  666. { "dat_cached_reply_tx" },
  667. #endif
  668. #ifdef CONFIG_BATMAN_ADV_NC
  669. { "nc_code" },
  670. { "nc_code_bytes" },
  671. { "nc_recode" },
  672. { "nc_recode_bytes" },
  673. { "nc_buffer" },
  674. { "nc_decode" },
  675. { "nc_decode_bytes" },
  676. { "nc_decode_failed" },
  677. { "nc_sniffed" },
  678. #endif
  679. };
  680. static void batadv_get_strings(struct net_device *dev, uint32_t stringset,
  681. uint8_t *data)
  682. {
  683. if (stringset == ETH_SS_STATS)
  684. memcpy(data, batadv_counters_strings,
  685. sizeof(batadv_counters_strings));
  686. }
  687. static void batadv_get_ethtool_stats(struct net_device *dev,
  688. struct ethtool_stats *stats,
  689. uint64_t *data)
  690. {
  691. struct batadv_priv *bat_priv = netdev_priv(dev);
  692. int i;
  693. for (i = 0; i < BATADV_CNT_NUM; i++)
  694. data[i] = batadv_sum_counter(bat_priv, i);
  695. }
  696. static int batadv_get_sset_count(struct net_device *dev, int stringset)
  697. {
  698. if (stringset == ETH_SS_STATS)
  699. return BATADV_CNT_NUM;
  700. return -EOPNOTSUPP;
  701. }