soft-interface.c 28 KB

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