soft-interface.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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, BATADV_NO_FLAGS,
  107. "mac address changed", false);
  108. batadv_tt_local_add(dev, addr->sa_data, BATADV_NO_FLAGS,
  109. BATADV_NULL_IFINDEX);
  110. }
  111. return 0;
  112. }
  113. static int batadv_interface_change_mtu(struct net_device *dev, int new_mtu)
  114. {
  115. /* check ranges */
  116. if ((new_mtu < 68) || (new_mtu > batadv_hardif_min_mtu(dev)))
  117. return -EINVAL;
  118. dev->mtu = new_mtu;
  119. return 0;
  120. }
  121. /**
  122. * batadv_interface_set_rx_mode - set the rx mode of a device
  123. * @dev: registered network device to modify
  124. *
  125. * We do not actually need to set any rx filters for the virtual batman
  126. * soft interface. However a dummy handler enables a user to set static
  127. * multicast listeners for instance.
  128. */
  129. static void batadv_interface_set_rx_mode(struct net_device *dev)
  130. {
  131. }
  132. static int batadv_interface_tx(struct sk_buff *skb,
  133. struct net_device *soft_iface)
  134. {
  135. struct ethhdr *ethhdr;
  136. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  137. struct batadv_hard_iface *primary_if = NULL;
  138. struct batadv_bcast_packet *bcast_packet;
  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. struct vlan_ethhdr *vhdr;
  145. unsigned int header_len = 0;
  146. int data_len = skb->len, ret;
  147. unsigned long brd_delay = 1;
  148. bool do_bcast = false;
  149. unsigned short vid;
  150. uint32_t seqno;
  151. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  152. goto dropped;
  153. soft_iface->trans_start = jiffies;
  154. vid = batadv_get_vid(skb, 0);
  155. ethhdr = (struct ethhdr *)skb->data;
  156. switch (ntohs(ethhdr->h_proto)) {
  157. case ETH_P_8021Q:
  158. vhdr = (struct vlan_ethhdr *)skb->data;
  159. if (vhdr->h_vlan_encapsulated_proto != ethertype)
  160. break;
  161. /* fall through */
  162. case ETH_P_BATMAN:
  163. goto dropped;
  164. }
  165. if (batadv_bla_tx(bat_priv, skb, vid))
  166. goto dropped;
  167. /* skb->data might have been reallocated by batadv_bla_tx() */
  168. ethhdr = (struct ethhdr *)skb->data;
  169. /* Register the client MAC in the transtable */
  170. if (!is_multicast_ether_addr(ethhdr->h_source))
  171. batadv_tt_local_add(soft_iface, ethhdr->h_source, vid,
  172. skb->skb_iif);
  173. /* don't accept stp packets. STP does not help in meshes.
  174. * better use the bridge loop avoidance ...
  175. *
  176. * The same goes for ECTP sent at least by some Cisco Switches,
  177. * it might confuse the mesh when used with bridge loop avoidance.
  178. */
  179. if (batadv_compare_eth(ethhdr->h_dest, stp_addr))
  180. goto dropped;
  181. if (batadv_compare_eth(ethhdr->h_dest, ectp_addr))
  182. goto dropped;
  183. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  184. do_bcast = true;
  185. switch (atomic_read(&bat_priv->gw_mode)) {
  186. case BATADV_GW_MODE_SERVER:
  187. /* gateway servers should not send dhcp
  188. * requests into the mesh
  189. */
  190. ret = batadv_gw_is_dhcp_target(skb, &header_len);
  191. if (ret)
  192. goto dropped;
  193. break;
  194. case BATADV_GW_MODE_CLIENT:
  195. /* gateway clients should send dhcp requests
  196. * via unicast to their gateway
  197. */
  198. ret = batadv_gw_is_dhcp_target(skb, &header_len);
  199. if (ret)
  200. do_bcast = false;
  201. break;
  202. case BATADV_GW_MODE_OFF:
  203. default:
  204. break;
  205. }
  206. /* reminder: ethhdr might have become unusable from here on
  207. * (batadv_gw_is_dhcp_target() might have reallocated skb data)
  208. */
  209. }
  210. batadv_skb_set_priority(skb, 0);
  211. /* ethernet packet should be broadcasted */
  212. if (do_bcast) {
  213. primary_if = batadv_primary_if_get_selected(bat_priv);
  214. if (!primary_if)
  215. goto dropped;
  216. /* in case of ARP request, we do not immediately broadcasti the
  217. * packet, instead we first wait for DAT to try to retrieve the
  218. * correct ARP entry
  219. */
  220. if (batadv_dat_snoop_outgoing_arp_request(bat_priv, skb))
  221. brd_delay = msecs_to_jiffies(ARP_REQ_DELAY);
  222. if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
  223. goto dropped;
  224. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  225. bcast_packet->header.version = BATADV_COMPAT_VERSION;
  226. bcast_packet->header.ttl = BATADV_TTL;
  227. /* batman packet type: broadcast */
  228. bcast_packet->header.packet_type = BATADV_BCAST;
  229. bcast_packet->reserved = 0;
  230. /* hw address of first interface is the orig mac because only
  231. * this mac is known throughout the mesh
  232. */
  233. memcpy(bcast_packet->orig,
  234. primary_if->net_dev->dev_addr, ETH_ALEN);
  235. /* set broadcast sequence number */
  236. seqno = atomic_inc_return(&bat_priv->bcast_seqno);
  237. bcast_packet->seqno = htonl(seqno);
  238. batadv_add_bcast_packet_to_list(bat_priv, skb, brd_delay);
  239. /* a copy is stored in the bcast list, therefore removing
  240. * the original skb.
  241. */
  242. kfree_skb(skb);
  243. /* unicast packet */
  244. } else {
  245. if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_OFF) {
  246. ret = batadv_gw_out_of_range(bat_priv, skb);
  247. if (ret)
  248. goto dropped;
  249. }
  250. if (batadv_dat_snoop_outgoing_arp_request(bat_priv, skb))
  251. goto dropped;
  252. batadv_dat_snoop_outgoing_arp_reply(bat_priv, skb);
  253. if (is_multicast_ether_addr(ethhdr->h_dest))
  254. ret = batadv_send_skb_via_gw(bat_priv, skb, vid);
  255. else
  256. ret = batadv_send_skb_via_tt(bat_priv, skb, vid);
  257. if (ret == NET_XMIT_DROP)
  258. goto dropped_freed;
  259. }
  260. batadv_inc_counter(bat_priv, BATADV_CNT_TX);
  261. batadv_add_counter(bat_priv, BATADV_CNT_TX_BYTES, data_len);
  262. goto end;
  263. dropped:
  264. kfree_skb(skb);
  265. dropped_freed:
  266. batadv_inc_counter(bat_priv, BATADV_CNT_TX_DROPPED);
  267. end:
  268. if (primary_if)
  269. batadv_hardif_free_ref(primary_if);
  270. return NETDEV_TX_OK;
  271. }
  272. void batadv_interface_rx(struct net_device *soft_iface,
  273. struct sk_buff *skb, struct batadv_hard_iface *recv_if,
  274. int hdr_size, struct batadv_orig_node *orig_node)
  275. {
  276. struct batadv_header *batadv_header = (struct batadv_header *)skb->data;
  277. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  278. __be16 ethertype = htons(ETH_P_BATMAN);
  279. struct vlan_ethhdr *vhdr;
  280. struct ethhdr *ethhdr;
  281. unsigned short vid;
  282. bool is_bcast;
  283. is_bcast = (batadv_header->packet_type == BATADV_BCAST);
  284. /* check if enough space is available for pulling, and pull */
  285. if (!pskb_may_pull(skb, hdr_size))
  286. goto dropped;
  287. skb_pull_rcsum(skb, hdr_size);
  288. skb_reset_mac_header(skb);
  289. vid = batadv_get_vid(skb, hdr_size);
  290. ethhdr = eth_hdr(skb);
  291. switch (ntohs(ethhdr->h_proto)) {
  292. case ETH_P_8021Q:
  293. vhdr = (struct vlan_ethhdr *)skb->data;
  294. if (vhdr->h_vlan_encapsulated_proto != ethertype)
  295. break;
  296. /* fall through */
  297. case ETH_P_BATMAN:
  298. goto dropped;
  299. }
  300. /* skb->dev & skb->pkt_type are set here */
  301. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  302. goto dropped;
  303. skb->protocol = eth_type_trans(skb, soft_iface);
  304. /* should not be necessary anymore as we use skb_pull_rcsum()
  305. * TODO: please verify this and remove this TODO
  306. * -- Dec 21st 2009, Simon Wunderlich
  307. */
  308. /* skb->ip_summed = CHECKSUM_UNNECESSARY; */
  309. batadv_inc_counter(bat_priv, BATADV_CNT_RX);
  310. batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
  311. skb->len + ETH_HLEN);
  312. soft_iface->last_rx = jiffies;
  313. /* Let the bridge loop avoidance check the packet. If will
  314. * not handle it, we can safely push it up.
  315. */
  316. if (batadv_bla_rx(bat_priv, skb, vid, is_bcast))
  317. goto out;
  318. if (orig_node)
  319. batadv_tt_add_temporary_global_entry(bat_priv, orig_node,
  320. ethhdr->h_source, vid);
  321. if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest,
  322. vid))
  323. goto dropped;
  324. netif_rx(skb);
  325. goto out;
  326. dropped:
  327. kfree_skb(skb);
  328. out:
  329. return;
  330. }
  331. /**
  332. * batadv_softif_vlan_free_ref - decrease the vlan object refcounter and
  333. * possibly free it
  334. * @softif_vlan: the vlan object to release
  335. */
  336. void batadv_softif_vlan_free_ref(struct batadv_softif_vlan *softif_vlan)
  337. {
  338. if (atomic_dec_and_test(&softif_vlan->refcount))
  339. kfree_rcu(softif_vlan, rcu);
  340. }
  341. /**
  342. * batadv_softif_vlan_get - get the vlan object for a specific vid
  343. * @bat_priv: the bat priv with all the soft interface information
  344. * @vid: the identifier of the vlan object to retrieve
  345. *
  346. * Returns the private data of the vlan matching the vid passed as argument or
  347. * NULL otherwise. The refcounter of the returned object is incremented by 1.
  348. */
  349. struct batadv_softif_vlan *batadv_softif_vlan_get(struct batadv_priv *bat_priv,
  350. unsigned short vid)
  351. {
  352. struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
  353. rcu_read_lock();
  354. hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
  355. if (vlan_tmp->vid != vid)
  356. continue;
  357. if (!atomic_inc_not_zero(&vlan_tmp->refcount))
  358. continue;
  359. vlan = vlan_tmp;
  360. break;
  361. }
  362. rcu_read_unlock();
  363. return vlan;
  364. }
  365. /**
  366. * batadv_create_vlan - allocate the needed resources for a new vlan
  367. * @bat_priv: the bat priv with all the soft interface information
  368. * @vid: the VLAN identifier
  369. *
  370. * Returns 0 on success, a negative error otherwise.
  371. */
  372. int batadv_softif_create_vlan(struct batadv_priv *bat_priv, unsigned short vid)
  373. {
  374. struct batadv_softif_vlan *vlan;
  375. int err;
  376. vlan = batadv_softif_vlan_get(bat_priv, vid);
  377. if (vlan) {
  378. batadv_softif_vlan_free_ref(vlan);
  379. return -EEXIST;
  380. }
  381. vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
  382. if (!vlan)
  383. return -ENOMEM;
  384. vlan->vid = vid;
  385. atomic_set(&vlan->refcount, 1);
  386. atomic_set(&vlan->ap_isolation, 0);
  387. err = batadv_sysfs_add_vlan(bat_priv->soft_iface, vlan);
  388. if (err) {
  389. kfree(vlan);
  390. return err;
  391. }
  392. /* add a new TT local entry. This one will be marked with the NOPURGE
  393. * flag
  394. */
  395. batadv_tt_local_add(bat_priv->soft_iface,
  396. bat_priv->soft_iface->dev_addr, vid,
  397. BATADV_NULL_IFINDEX);
  398. spin_lock_bh(&bat_priv->softif_vlan_list_lock);
  399. hlist_add_head_rcu(&vlan->list, &bat_priv->softif_vlan_list);
  400. spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
  401. return 0;
  402. }
  403. /**
  404. * batadv_softif_destroy_vlan - remove and destroy a softif_vlan object
  405. * @bat_priv: the bat priv with all the soft interface information
  406. * @vlan: the object to remove
  407. */
  408. static void batadv_softif_destroy_vlan(struct batadv_priv *bat_priv,
  409. struct batadv_softif_vlan *vlan)
  410. {
  411. spin_lock_bh(&bat_priv->softif_vlan_list_lock);
  412. hlist_del_rcu(&vlan->list);
  413. spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
  414. batadv_sysfs_del_vlan(bat_priv, vlan);
  415. /* explicitly remove the associated TT local entry because it is marked
  416. * with the NOPURGE flag
  417. */
  418. batadv_tt_local_remove(bat_priv, bat_priv->soft_iface->dev_addr,
  419. vlan->vid, "vlan interface destroyed", false);
  420. batadv_softif_vlan_free_ref(vlan);
  421. }
  422. /**
  423. * batadv_interface_add_vid - ndo_add_vid API implementation
  424. * @dev: the netdev of the mesh interface
  425. * @vid: identifier of the new vlan
  426. *
  427. * Set up all the internal structures for handling the new vlan on top of the
  428. * mesh interface
  429. *
  430. * Returns 0 on success or a negative error code in case of failure.
  431. */
  432. static int batadv_interface_add_vid(struct net_device *dev, __be16 proto,
  433. unsigned short vid)
  434. {
  435. struct batadv_priv *bat_priv = netdev_priv(dev);
  436. /* only 802.1Q vlans are supported.
  437. * batman-adv does not know how to handle other types
  438. */
  439. if (proto != htons(ETH_P_8021Q))
  440. return -EINVAL;
  441. vid |= BATADV_VLAN_HAS_TAG;
  442. return batadv_softif_create_vlan(bat_priv, vid);
  443. }
  444. /**
  445. * batadv_interface_kill_vid - ndo_kill_vid API implementation
  446. * @dev: the netdev of the mesh interface
  447. * @vid: identifier of the deleted vlan
  448. *
  449. * Destroy all the internal structures used to handle the vlan identified by vid
  450. * on top of the mesh interface
  451. *
  452. * Returns 0 on success, -EINVAL if the specified prototype is not ETH_P_8021Q
  453. * or -ENOENT if the specified vlan id wasn't registered.
  454. */
  455. static int batadv_interface_kill_vid(struct net_device *dev, __be16 proto,
  456. unsigned short vid)
  457. {
  458. struct batadv_priv *bat_priv = netdev_priv(dev);
  459. struct batadv_softif_vlan *vlan;
  460. /* only 802.1Q vlans are supported. batman-adv does not know how to
  461. * handle other types
  462. */
  463. if (proto != htons(ETH_P_8021Q))
  464. return -EINVAL;
  465. vlan = batadv_softif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG);
  466. if (!vlan)
  467. return -ENOENT;
  468. batadv_softif_destroy_vlan(bat_priv, vlan);
  469. /* finally free the vlan object */
  470. batadv_softif_vlan_free_ref(vlan);
  471. return 0;
  472. }
  473. /* batman-adv network devices have devices nesting below it and are a special
  474. * "super class" of normal network devices; split their locks off into a
  475. * separate class since they always nest.
  476. */
  477. static struct lock_class_key batadv_netdev_xmit_lock_key;
  478. static struct lock_class_key batadv_netdev_addr_lock_key;
  479. /**
  480. * batadv_set_lockdep_class_one - Set lockdep class for a single tx queue
  481. * @dev: device which owns the tx queue
  482. * @txq: tx queue to modify
  483. * @_unused: always NULL
  484. */
  485. static void batadv_set_lockdep_class_one(struct net_device *dev,
  486. struct netdev_queue *txq,
  487. void *_unused)
  488. {
  489. lockdep_set_class(&txq->_xmit_lock, &batadv_netdev_xmit_lock_key);
  490. }
  491. /**
  492. * batadv_set_lockdep_class - Set txq and addr_list lockdep class
  493. * @dev: network device to modify
  494. */
  495. static void batadv_set_lockdep_class(struct net_device *dev)
  496. {
  497. lockdep_set_class(&dev->addr_list_lock, &batadv_netdev_addr_lock_key);
  498. netdev_for_each_tx_queue(dev, batadv_set_lockdep_class_one, NULL);
  499. }
  500. /**
  501. * batadv_softif_destroy_finish - cleans up the remains of a softif
  502. * @work: work queue item
  503. *
  504. * Free the parts of the soft interface which can not be removed under
  505. * rtnl lock (to prevent deadlock situations).
  506. */
  507. static void batadv_softif_destroy_finish(struct work_struct *work)
  508. {
  509. struct batadv_softif_vlan *vlan;
  510. struct batadv_priv *bat_priv;
  511. struct net_device *soft_iface;
  512. bat_priv = container_of(work, struct batadv_priv,
  513. cleanup_work);
  514. soft_iface = bat_priv->soft_iface;
  515. /* destroy the "untagged" VLAN */
  516. vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
  517. if (vlan) {
  518. batadv_softif_destroy_vlan(bat_priv, vlan);
  519. batadv_softif_vlan_free_ref(vlan);
  520. }
  521. batadv_sysfs_del_meshif(soft_iface);
  522. rtnl_lock();
  523. unregister_netdevice(soft_iface);
  524. rtnl_unlock();
  525. }
  526. /**
  527. * batadv_softif_init_late - late stage initialization of soft interface
  528. * @dev: registered network device to modify
  529. *
  530. * Returns error code on failures
  531. */
  532. static int batadv_softif_init_late(struct net_device *dev)
  533. {
  534. struct batadv_priv *bat_priv;
  535. uint32_t random_seqno;
  536. int ret;
  537. size_t cnt_len = sizeof(uint64_t) * BATADV_CNT_NUM;
  538. batadv_set_lockdep_class(dev);
  539. bat_priv = netdev_priv(dev);
  540. bat_priv->soft_iface = dev;
  541. INIT_WORK(&bat_priv->cleanup_work, batadv_softif_destroy_finish);
  542. /* batadv_interface_stats() needs to be available as soon as
  543. * register_netdevice() has been called
  544. */
  545. bat_priv->bat_counters = __alloc_percpu(cnt_len, __alignof__(uint64_t));
  546. if (!bat_priv->bat_counters)
  547. return -ENOMEM;
  548. atomic_set(&bat_priv->aggregated_ogms, 1);
  549. atomic_set(&bat_priv->bonding, 0);
  550. #ifdef CONFIG_BATMAN_ADV_BLA
  551. atomic_set(&bat_priv->bridge_loop_avoidance, 0);
  552. #endif
  553. #ifdef CONFIG_BATMAN_ADV_DAT
  554. atomic_set(&bat_priv->distributed_arp_table, 1);
  555. #endif
  556. atomic_set(&bat_priv->gw_mode, BATADV_GW_MODE_OFF);
  557. atomic_set(&bat_priv->gw_sel_class, 20);
  558. atomic_set(&bat_priv->gw.bandwidth_down, 100);
  559. atomic_set(&bat_priv->gw.bandwidth_up, 20);
  560. atomic_set(&bat_priv->orig_interval, 1000);
  561. atomic_set(&bat_priv->hop_penalty, 30);
  562. #ifdef CONFIG_BATMAN_ADV_DEBUG
  563. atomic_set(&bat_priv->log_level, 0);
  564. #endif
  565. atomic_set(&bat_priv->fragmentation, 1);
  566. atomic_set(&bat_priv->bcast_queue_left, BATADV_BCAST_QUEUE_LEN);
  567. atomic_set(&bat_priv->batman_queue_left, BATADV_BATMAN_QUEUE_LEN);
  568. atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
  569. atomic_set(&bat_priv->bcast_seqno, 1);
  570. atomic_set(&bat_priv->tt.vn, 0);
  571. atomic_set(&bat_priv->tt.local_changes, 0);
  572. atomic_set(&bat_priv->tt.ogm_append_cnt, 0);
  573. #ifdef CONFIG_BATMAN_ADV_BLA
  574. atomic_set(&bat_priv->bla.num_requests, 0);
  575. #endif
  576. bat_priv->tt.last_changeset = NULL;
  577. bat_priv->tt.last_changeset_len = 0;
  578. /* randomize initial seqno to avoid collision */
  579. get_random_bytes(&random_seqno, sizeof(random_seqno));
  580. atomic_set(&bat_priv->frag_seqno, random_seqno);
  581. bat_priv->primary_if = NULL;
  582. bat_priv->num_ifaces = 0;
  583. batadv_nc_init_bat_priv(bat_priv);
  584. ret = batadv_algo_select(bat_priv, batadv_routing_algo);
  585. if (ret < 0)
  586. goto free_bat_counters;
  587. ret = batadv_debugfs_add_meshif(dev);
  588. if (ret < 0)
  589. goto free_bat_counters;
  590. ret = batadv_mesh_init(dev);
  591. if (ret < 0)
  592. goto unreg_debugfs;
  593. return 0;
  594. unreg_debugfs:
  595. batadv_debugfs_del_meshif(dev);
  596. free_bat_counters:
  597. free_percpu(bat_priv->bat_counters);
  598. bat_priv->bat_counters = NULL;
  599. return ret;
  600. }
  601. /**
  602. * batadv_softif_slave_add - Add a slave interface to a batadv_soft_interface
  603. * @dev: batadv_soft_interface used as master interface
  604. * @slave_dev: net_device which should become the slave interface
  605. *
  606. * Return 0 if successful or error otherwise.
  607. */
  608. static int batadv_softif_slave_add(struct net_device *dev,
  609. struct net_device *slave_dev)
  610. {
  611. struct batadv_hard_iface *hard_iface;
  612. int ret = -EINVAL;
  613. hard_iface = batadv_hardif_get_by_netdev(slave_dev);
  614. if (!hard_iface || hard_iface->soft_iface != NULL)
  615. goto out;
  616. ret = batadv_hardif_enable_interface(hard_iface, dev->name);
  617. out:
  618. if (hard_iface)
  619. batadv_hardif_free_ref(hard_iface);
  620. return ret;
  621. }
  622. /**
  623. * batadv_softif_slave_del - Delete a slave iface from a batadv_soft_interface
  624. * @dev: batadv_soft_interface used as master interface
  625. * @slave_dev: net_device which should be removed from the master interface
  626. *
  627. * Return 0 if successful or error otherwise.
  628. */
  629. static int batadv_softif_slave_del(struct net_device *dev,
  630. struct net_device *slave_dev)
  631. {
  632. struct batadv_hard_iface *hard_iface;
  633. int ret = -EINVAL;
  634. hard_iface = batadv_hardif_get_by_netdev(slave_dev);
  635. if (!hard_iface || hard_iface->soft_iface != dev)
  636. goto out;
  637. batadv_hardif_disable_interface(hard_iface, BATADV_IF_CLEANUP_KEEP);
  638. ret = 0;
  639. out:
  640. if (hard_iface)
  641. batadv_hardif_free_ref(hard_iface);
  642. return ret;
  643. }
  644. static const struct net_device_ops batadv_netdev_ops = {
  645. .ndo_init = batadv_softif_init_late,
  646. .ndo_open = batadv_interface_open,
  647. .ndo_stop = batadv_interface_release,
  648. .ndo_get_stats = batadv_interface_stats,
  649. .ndo_vlan_rx_add_vid = batadv_interface_add_vid,
  650. .ndo_vlan_rx_kill_vid = batadv_interface_kill_vid,
  651. .ndo_set_mac_address = batadv_interface_set_mac_addr,
  652. .ndo_change_mtu = batadv_interface_change_mtu,
  653. .ndo_set_rx_mode = batadv_interface_set_rx_mode,
  654. .ndo_start_xmit = batadv_interface_tx,
  655. .ndo_validate_addr = eth_validate_addr,
  656. .ndo_add_slave = batadv_softif_slave_add,
  657. .ndo_del_slave = batadv_softif_slave_del,
  658. };
  659. /**
  660. * batadv_softif_free - Deconstructor of batadv_soft_interface
  661. * @dev: Device to cleanup and remove
  662. */
  663. static void batadv_softif_free(struct net_device *dev)
  664. {
  665. batadv_debugfs_del_meshif(dev);
  666. batadv_mesh_free(dev);
  667. /* some scheduled RCU callbacks need the bat_priv struct to accomplish
  668. * their tasks. Wait for them all to be finished before freeing the
  669. * netdev and its private data (bat_priv)
  670. */
  671. rcu_barrier();
  672. free_netdev(dev);
  673. }
  674. /**
  675. * batadv_softif_init_early - early stage initialization of soft interface
  676. * @dev: registered network device to modify
  677. */
  678. static void batadv_softif_init_early(struct net_device *dev)
  679. {
  680. struct batadv_priv *priv = netdev_priv(dev);
  681. ether_setup(dev);
  682. dev->netdev_ops = &batadv_netdev_ops;
  683. dev->destructor = batadv_softif_free;
  684. dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  685. dev->tx_queue_len = 0;
  686. /* can't call min_mtu, because the needed variables
  687. * have not been initialized yet
  688. */
  689. dev->mtu = ETH_DATA_LEN;
  690. /* reserve more space in the skbuff for our header */
  691. dev->hard_header_len = batadv_max_header_len();
  692. /* generate random address */
  693. eth_hw_addr_random(dev);
  694. SET_ETHTOOL_OPS(dev, &batadv_ethtool_ops);
  695. memset(priv, 0, sizeof(*priv));
  696. }
  697. struct net_device *batadv_softif_create(const char *name)
  698. {
  699. struct net_device *soft_iface;
  700. int ret;
  701. soft_iface = alloc_netdev(sizeof(struct batadv_priv), name,
  702. batadv_softif_init_early);
  703. if (!soft_iface)
  704. return NULL;
  705. soft_iface->rtnl_link_ops = &batadv_link_ops;
  706. ret = register_netdevice(soft_iface);
  707. if (ret < 0) {
  708. pr_err("Unable to register the batman interface '%s': %i\n",
  709. name, ret);
  710. free_netdev(soft_iface);
  711. return NULL;
  712. }
  713. return soft_iface;
  714. }
  715. /**
  716. * batadv_softif_destroy_sysfs - deletion of batadv_soft_interface via sysfs
  717. * @soft_iface: the to-be-removed batman-adv interface
  718. */
  719. void batadv_softif_destroy_sysfs(struct net_device *soft_iface)
  720. {
  721. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  722. queue_work(batadv_event_workqueue, &bat_priv->cleanup_work);
  723. }
  724. /**
  725. * batadv_softif_destroy_netlink - deletion of batadv_soft_interface via netlink
  726. * @soft_iface: the to-be-removed batman-adv interface
  727. * @head: list pointer
  728. */
  729. static void batadv_softif_destroy_netlink(struct net_device *soft_iface,
  730. struct list_head *head)
  731. {
  732. struct batadv_hard_iface *hard_iface;
  733. list_for_each_entry(hard_iface, &batadv_hardif_list, list) {
  734. if (hard_iface->soft_iface == soft_iface)
  735. batadv_hardif_disable_interface(hard_iface,
  736. BATADV_IF_CLEANUP_KEEP);
  737. }
  738. batadv_sysfs_del_meshif(soft_iface);
  739. unregister_netdevice_queue(soft_iface, head);
  740. }
  741. int batadv_softif_is_valid(const struct net_device *net_dev)
  742. {
  743. if (net_dev->netdev_ops->ndo_start_xmit == batadv_interface_tx)
  744. return 1;
  745. return 0;
  746. }
  747. struct rtnl_link_ops batadv_link_ops __read_mostly = {
  748. .kind = "batadv",
  749. .priv_size = sizeof(struct batadv_priv),
  750. .setup = batadv_softif_init_early,
  751. .dellink = batadv_softif_destroy_netlink,
  752. };
  753. /* ethtool */
  754. static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  755. {
  756. cmd->supported = 0;
  757. cmd->advertising = 0;
  758. ethtool_cmd_speed_set(cmd, SPEED_10);
  759. cmd->duplex = DUPLEX_FULL;
  760. cmd->port = PORT_TP;
  761. cmd->phy_address = 0;
  762. cmd->transceiver = XCVR_INTERNAL;
  763. cmd->autoneg = AUTONEG_DISABLE;
  764. cmd->maxtxpkt = 0;
  765. cmd->maxrxpkt = 0;
  766. return 0;
  767. }
  768. static void batadv_get_drvinfo(struct net_device *dev,
  769. struct ethtool_drvinfo *info)
  770. {
  771. strlcpy(info->driver, "B.A.T.M.A.N. advanced", sizeof(info->driver));
  772. strlcpy(info->version, BATADV_SOURCE_VERSION, sizeof(info->version));
  773. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  774. strlcpy(info->bus_info, "batman", sizeof(info->bus_info));
  775. }
  776. static u32 batadv_get_msglevel(struct net_device *dev)
  777. {
  778. return -EOPNOTSUPP;
  779. }
  780. static void batadv_set_msglevel(struct net_device *dev, u32 value)
  781. {
  782. }
  783. static u32 batadv_get_link(struct net_device *dev)
  784. {
  785. return 1;
  786. }
  787. /* Inspired by drivers/net/ethernet/dlink/sundance.c:1702
  788. * Declare each description string in struct.name[] to get fixed sized buffer
  789. * and compile time checking for strings longer than ETH_GSTRING_LEN.
  790. */
  791. static const struct {
  792. const char name[ETH_GSTRING_LEN];
  793. } batadv_counters_strings[] = {
  794. { "tx" },
  795. { "tx_bytes" },
  796. { "tx_dropped" },
  797. { "rx" },
  798. { "rx_bytes" },
  799. { "forward" },
  800. { "forward_bytes" },
  801. { "mgmt_tx" },
  802. { "mgmt_tx_bytes" },
  803. { "mgmt_rx" },
  804. { "mgmt_rx_bytes" },
  805. { "frag_tx" },
  806. { "frag_tx_bytes" },
  807. { "frag_rx" },
  808. { "frag_rx_bytes" },
  809. { "frag_fwd" },
  810. { "frag_fwd_bytes" },
  811. { "tt_request_tx" },
  812. { "tt_request_rx" },
  813. { "tt_response_tx" },
  814. { "tt_response_rx" },
  815. { "tt_roam_adv_tx" },
  816. { "tt_roam_adv_rx" },
  817. #ifdef CONFIG_BATMAN_ADV_DAT
  818. { "dat_get_tx" },
  819. { "dat_get_rx" },
  820. { "dat_put_tx" },
  821. { "dat_put_rx" },
  822. { "dat_cached_reply_tx" },
  823. #endif
  824. #ifdef CONFIG_BATMAN_ADV_NC
  825. { "nc_code" },
  826. { "nc_code_bytes" },
  827. { "nc_recode" },
  828. { "nc_recode_bytes" },
  829. { "nc_buffer" },
  830. { "nc_decode" },
  831. { "nc_decode_bytes" },
  832. { "nc_decode_failed" },
  833. { "nc_sniffed" },
  834. #endif
  835. };
  836. static void batadv_get_strings(struct net_device *dev, uint32_t stringset,
  837. uint8_t *data)
  838. {
  839. if (stringset == ETH_SS_STATS)
  840. memcpy(data, batadv_counters_strings,
  841. sizeof(batadv_counters_strings));
  842. }
  843. static void batadv_get_ethtool_stats(struct net_device *dev,
  844. struct ethtool_stats *stats,
  845. uint64_t *data)
  846. {
  847. struct batadv_priv *bat_priv = netdev_priv(dev);
  848. int i;
  849. for (i = 0; i < BATADV_CNT_NUM; i++)
  850. data[i] = batadv_sum_counter(bat_priv, i);
  851. }
  852. static int batadv_get_sset_count(struct net_device *dev, int stringset)
  853. {
  854. if (stringset == ETH_SS_STATS)
  855. return BATADV_CNT_NUM;
  856. return -EOPNOTSUPP;
  857. }