soft-interface.c 22 KB

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