soft-interface.c 22 KB

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