soft-interface.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /* Copyright (C) 2007-2015 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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "soft-interface.h"
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/byteorder/generic.h>
  21. #include <linux/cache.h>
  22. #include <linux/compiler.h>
  23. #include <linux/errno.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/fs.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/if_vlan.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/kernel.h>
  31. #include <linux/list.h>
  32. #include <linux/lockdep.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/percpu.h>
  35. #include <linux/printk.h>
  36. #include <linux/random.h>
  37. #include <linux/rculist.h>
  38. #include <linux/rcupdate.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/slab.h>
  41. #include <linux/socket.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/stddef.h>
  44. #include <linux/string.h>
  45. #include <linux/types.h>
  46. #include <linux/workqueue.h>
  47. #include "bridge_loop_avoidance.h"
  48. #include "debugfs.h"
  49. #include "distributed-arp-table.h"
  50. #include "gateway_client.h"
  51. #include "gateway_common.h"
  52. #include "hard-interface.h"
  53. #include "multicast.h"
  54. #include "network-coding.h"
  55. #include "packet.h"
  56. #include "send.h"
  57. #include "sysfs.h"
  58. #include "translation-table.h"
  59. static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
  60. static void batadv_get_drvinfo(struct net_device *dev,
  61. struct ethtool_drvinfo *info);
  62. static u32 batadv_get_msglevel(struct net_device *dev);
  63. static void batadv_set_msglevel(struct net_device *dev, u32 value);
  64. static u32 batadv_get_link(struct net_device *dev);
  65. static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data);
  66. static void batadv_get_ethtool_stats(struct net_device *dev,
  67. struct ethtool_stats *stats, u64 *data);
  68. static int batadv_get_sset_count(struct net_device *dev, int stringset);
  69. static const struct ethtool_ops batadv_ethtool_ops = {
  70. .get_settings = batadv_get_settings,
  71. .get_drvinfo = batadv_get_drvinfo,
  72. .get_msglevel = batadv_get_msglevel,
  73. .set_msglevel = batadv_set_msglevel,
  74. .get_link = batadv_get_link,
  75. .get_strings = batadv_get_strings,
  76. .get_ethtool_stats = batadv_get_ethtool_stats,
  77. .get_sset_count = batadv_get_sset_count,
  78. };
  79. int batadv_skb_head_push(struct sk_buff *skb, unsigned int len)
  80. {
  81. int result;
  82. /* TODO: We must check if we can release all references to non-payload
  83. * data using skb_header_release in our skbs to allow skb_cow_header to
  84. * work optimally. This means that those skbs are not allowed to read
  85. * or write any data which is before the current position of skb->data
  86. * after that call and thus allow other skbs with the same data buffer
  87. * to write freely in that area.
  88. */
  89. result = skb_cow_head(skb, len);
  90. if (result < 0)
  91. return result;
  92. skb_push(skb, len);
  93. return 0;
  94. }
  95. static int batadv_interface_open(struct net_device *dev)
  96. {
  97. netif_start_queue(dev);
  98. return 0;
  99. }
  100. static int batadv_interface_release(struct net_device *dev)
  101. {
  102. netif_stop_queue(dev);
  103. return 0;
  104. }
  105. static struct net_device_stats *batadv_interface_stats(struct net_device *dev)
  106. {
  107. struct batadv_priv *bat_priv = netdev_priv(dev);
  108. struct net_device_stats *stats = &bat_priv->stats;
  109. stats->tx_packets = batadv_sum_counter(bat_priv, BATADV_CNT_TX);
  110. stats->tx_bytes = batadv_sum_counter(bat_priv, BATADV_CNT_TX_BYTES);
  111. stats->tx_dropped = batadv_sum_counter(bat_priv, BATADV_CNT_TX_DROPPED);
  112. stats->rx_packets = batadv_sum_counter(bat_priv, BATADV_CNT_RX);
  113. stats->rx_bytes = batadv_sum_counter(bat_priv, BATADV_CNT_RX_BYTES);
  114. return stats;
  115. }
  116. static int batadv_interface_set_mac_addr(struct net_device *dev, void *p)
  117. {
  118. struct batadv_priv *bat_priv = netdev_priv(dev);
  119. struct batadv_softif_vlan *vlan;
  120. struct sockaddr *addr = p;
  121. uint8_t old_addr[ETH_ALEN];
  122. if (!is_valid_ether_addr(addr->sa_data))
  123. return -EADDRNOTAVAIL;
  124. ether_addr_copy(old_addr, dev->dev_addr);
  125. ether_addr_copy(dev->dev_addr, addr->sa_data);
  126. /* only modify transtable if it has been initialized before */
  127. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  128. return 0;
  129. rcu_read_lock();
  130. hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
  131. batadv_tt_local_remove(bat_priv, old_addr, vlan->vid,
  132. "mac address changed", false);
  133. batadv_tt_local_add(dev, addr->sa_data, vlan->vid,
  134. BATADV_NULL_IFINDEX, BATADV_NO_MARK);
  135. }
  136. rcu_read_unlock();
  137. return 0;
  138. }
  139. static int batadv_interface_change_mtu(struct net_device *dev, int new_mtu)
  140. {
  141. /* check ranges */
  142. if ((new_mtu < 68) || (new_mtu > batadv_hardif_min_mtu(dev)))
  143. return -EINVAL;
  144. dev->mtu = new_mtu;
  145. return 0;
  146. }
  147. /**
  148. * batadv_interface_set_rx_mode - set the rx mode of a device
  149. * @dev: registered network device to modify
  150. *
  151. * We do not actually need to set any rx filters for the virtual batman
  152. * soft interface. However a dummy handler enables a user to set static
  153. * multicast listeners for instance.
  154. */
  155. static void batadv_interface_set_rx_mode(struct net_device *dev)
  156. {
  157. }
  158. static int batadv_interface_tx(struct sk_buff *skb,
  159. struct net_device *soft_iface)
  160. {
  161. struct ethhdr *ethhdr;
  162. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  163. struct batadv_hard_iface *primary_if = NULL;
  164. struct batadv_bcast_packet *bcast_packet;
  165. __be16 ethertype = htons(ETH_P_BATMAN);
  166. static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00,
  167. 0x00, 0x00};
  168. static const uint8_t ectp_addr[ETH_ALEN] = {0xCF, 0x00, 0x00, 0x00,
  169. 0x00, 0x00};
  170. enum batadv_dhcp_recipient dhcp_rcp = BATADV_DHCP_NO;
  171. uint8_t *dst_hint = NULL, chaddr[ETH_ALEN];
  172. struct vlan_ethhdr *vhdr;
  173. unsigned int header_len = 0;
  174. int data_len = skb->len, ret;
  175. unsigned long brd_delay = 1;
  176. bool do_bcast = false, client_added;
  177. unsigned short vid;
  178. uint32_t seqno;
  179. int gw_mode;
  180. enum batadv_forw_mode forw_mode;
  181. struct batadv_orig_node *mcast_single_orig = NULL;
  182. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  183. goto dropped;
  184. soft_iface->trans_start = jiffies;
  185. vid = batadv_get_vid(skb, 0);
  186. ethhdr = eth_hdr(skb);
  187. switch (ntohs(ethhdr->h_proto)) {
  188. case ETH_P_8021Q:
  189. vhdr = vlan_eth_hdr(skb);
  190. if (vhdr->h_vlan_encapsulated_proto != ethertype)
  191. break;
  192. /* fall through */
  193. case ETH_P_BATMAN:
  194. goto dropped;
  195. }
  196. if (batadv_bla_tx(bat_priv, skb, vid))
  197. goto dropped;
  198. /* skb->data might have been reallocated by batadv_bla_tx() */
  199. ethhdr = eth_hdr(skb);
  200. /* Register the client MAC in the transtable */
  201. if (!is_multicast_ether_addr(ethhdr->h_source)) {
  202. client_added = batadv_tt_local_add(soft_iface, ethhdr->h_source,
  203. vid, skb->skb_iif,
  204. skb->mark);
  205. if (!client_added)
  206. goto dropped;
  207. }
  208. /* don't accept stp packets. STP does not help in meshes.
  209. * better use the bridge loop avoidance ...
  210. *
  211. * The same goes for ECTP sent at least by some Cisco Switches,
  212. * it might confuse the mesh when used with bridge loop avoidance.
  213. */
  214. if (batadv_compare_eth(ethhdr->h_dest, stp_addr))
  215. goto dropped;
  216. if (batadv_compare_eth(ethhdr->h_dest, ectp_addr))
  217. goto dropped;
  218. gw_mode = atomic_read(&bat_priv->gw_mode);
  219. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  220. /* if gw mode is off, broadcast every packet */
  221. if (gw_mode == BATADV_GW_MODE_OFF) {
  222. do_bcast = true;
  223. goto send;
  224. }
  225. dhcp_rcp = batadv_gw_dhcp_recipient_get(skb, &header_len,
  226. chaddr);
  227. /* skb->data may have been modified by
  228. * batadv_gw_dhcp_recipient_get()
  229. */
  230. ethhdr = eth_hdr(skb);
  231. /* if gw_mode is on, broadcast any non-DHCP message.
  232. * All the DHCP packets are going to be sent as unicast
  233. */
  234. if (dhcp_rcp == BATADV_DHCP_NO) {
  235. do_bcast = true;
  236. goto send;
  237. }
  238. if (dhcp_rcp == BATADV_DHCP_TO_CLIENT)
  239. dst_hint = chaddr;
  240. else if ((gw_mode == BATADV_GW_MODE_SERVER) &&
  241. (dhcp_rcp == BATADV_DHCP_TO_SERVER))
  242. /* gateways should not forward any DHCP message if
  243. * directed to a DHCP server
  244. */
  245. goto dropped;
  246. send:
  247. if (do_bcast && !is_broadcast_ether_addr(ethhdr->h_dest)) {
  248. forw_mode = batadv_mcast_forw_mode(bat_priv, skb,
  249. &mcast_single_orig);
  250. if (forw_mode == BATADV_FORW_NONE)
  251. goto dropped;
  252. if (forw_mode == BATADV_FORW_SINGLE)
  253. do_bcast = false;
  254. }
  255. }
  256. batadv_skb_set_priority(skb, 0);
  257. /* ethernet packet should be broadcasted */
  258. if (do_bcast) {
  259. primary_if = batadv_primary_if_get_selected(bat_priv);
  260. if (!primary_if)
  261. goto dropped;
  262. /* in case of ARP request, we do not immediately broadcasti the
  263. * packet, instead we first wait for DAT to try to retrieve the
  264. * correct ARP entry
  265. */
  266. if (batadv_dat_snoop_outgoing_arp_request(bat_priv, skb))
  267. brd_delay = msecs_to_jiffies(ARP_REQ_DELAY);
  268. if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
  269. goto dropped;
  270. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  271. bcast_packet->version = BATADV_COMPAT_VERSION;
  272. bcast_packet->ttl = BATADV_TTL;
  273. /* batman packet type: broadcast */
  274. bcast_packet->packet_type = BATADV_BCAST;
  275. bcast_packet->reserved = 0;
  276. /* hw address of first interface is the orig mac because only
  277. * this mac is known throughout the mesh
  278. */
  279. ether_addr_copy(bcast_packet->orig,
  280. primary_if->net_dev->dev_addr);
  281. /* set broadcast sequence number */
  282. seqno = atomic_inc_return(&bat_priv->bcast_seqno);
  283. bcast_packet->seqno = htonl(seqno);
  284. batadv_add_bcast_packet_to_list(bat_priv, skb, brd_delay);
  285. /* a copy is stored in the bcast list, therefore removing
  286. * the original skb.
  287. */
  288. kfree_skb(skb);
  289. /* unicast packet */
  290. } else {
  291. /* DHCP packets going to a server will use the GW feature */
  292. if (dhcp_rcp == BATADV_DHCP_TO_SERVER) {
  293. ret = batadv_gw_out_of_range(bat_priv, skb);
  294. if (ret)
  295. goto dropped;
  296. ret = batadv_send_skb_via_gw(bat_priv, skb, vid);
  297. } else if (mcast_single_orig) {
  298. ret = batadv_send_skb_unicast(bat_priv, skb,
  299. BATADV_UNICAST, 0,
  300. mcast_single_orig, vid);
  301. } else {
  302. if (batadv_dat_snoop_outgoing_arp_request(bat_priv,
  303. skb))
  304. goto dropped;
  305. batadv_dat_snoop_outgoing_arp_reply(bat_priv, skb);
  306. ret = batadv_send_skb_via_tt(bat_priv, skb, dst_hint,
  307. vid);
  308. }
  309. if (ret == NET_XMIT_DROP)
  310. goto dropped_freed;
  311. }
  312. batadv_inc_counter(bat_priv, BATADV_CNT_TX);
  313. batadv_add_counter(bat_priv, BATADV_CNT_TX_BYTES, data_len);
  314. goto end;
  315. dropped:
  316. kfree_skb(skb);
  317. dropped_freed:
  318. batadv_inc_counter(bat_priv, BATADV_CNT_TX_DROPPED);
  319. end:
  320. if (primary_if)
  321. batadv_hardif_free_ref(primary_if);
  322. return NETDEV_TX_OK;
  323. }
  324. void batadv_interface_rx(struct net_device *soft_iface,
  325. struct sk_buff *skb, struct batadv_hard_iface *recv_if,
  326. int hdr_size, struct batadv_orig_node *orig_node)
  327. {
  328. struct batadv_bcast_packet *batadv_bcast_packet;
  329. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  330. __be16 ethertype = htons(ETH_P_BATMAN);
  331. struct vlan_ethhdr *vhdr;
  332. struct ethhdr *ethhdr;
  333. unsigned short vid;
  334. bool is_bcast;
  335. batadv_bcast_packet = (struct batadv_bcast_packet *)skb->data;
  336. is_bcast = (batadv_bcast_packet->packet_type == BATADV_BCAST);
  337. /* check if enough space is available for pulling, and pull */
  338. if (!pskb_may_pull(skb, hdr_size))
  339. goto dropped;
  340. skb_pull_rcsum(skb, hdr_size);
  341. skb_reset_mac_header(skb);
  342. /* clean the netfilter state now that the batman-adv header has been
  343. * removed
  344. */
  345. nf_reset(skb);
  346. vid = batadv_get_vid(skb, 0);
  347. ethhdr = eth_hdr(skb);
  348. switch (ntohs(ethhdr->h_proto)) {
  349. case ETH_P_8021Q:
  350. vhdr = (struct vlan_ethhdr *)skb->data;
  351. if (vhdr->h_vlan_encapsulated_proto != ethertype)
  352. break;
  353. /* fall through */
  354. case ETH_P_BATMAN:
  355. goto dropped;
  356. }
  357. /* skb->dev & skb->pkt_type are set here */
  358. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  359. goto dropped;
  360. skb->protocol = eth_type_trans(skb, soft_iface);
  361. /* should not be necessary anymore as we use skb_pull_rcsum()
  362. * TODO: please verify this and remove this TODO
  363. * -- Dec 21st 2009, Simon Wunderlich
  364. */
  365. /* skb->ip_summed = CHECKSUM_UNNECESSARY; */
  366. batadv_inc_counter(bat_priv, BATADV_CNT_RX);
  367. batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
  368. skb->len + ETH_HLEN);
  369. soft_iface->last_rx = jiffies;
  370. /* Let the bridge loop avoidance check the packet. If will
  371. * not handle it, we can safely push it up.
  372. */
  373. if (batadv_bla_rx(bat_priv, skb, vid, is_bcast))
  374. goto out;
  375. if (orig_node)
  376. batadv_tt_add_temporary_global_entry(bat_priv, orig_node,
  377. ethhdr->h_source, vid);
  378. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  379. /* set the mark on broadcast packets if AP isolation is ON and
  380. * the packet is coming from an "isolated" client
  381. */
  382. if (batadv_vlan_ap_isola_get(bat_priv, vid) &&
  383. batadv_tt_global_is_isolated(bat_priv, ethhdr->h_source,
  384. vid)) {
  385. /* save bits in skb->mark not covered by the mask and
  386. * apply the mark on the rest
  387. */
  388. skb->mark &= ~bat_priv->isolation_mark_mask;
  389. skb->mark |= bat_priv->isolation_mark;
  390. }
  391. } else if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source,
  392. ethhdr->h_dest, vid)) {
  393. goto dropped;
  394. }
  395. netif_rx(skb);
  396. goto out;
  397. dropped:
  398. kfree_skb(skb);
  399. out:
  400. return;
  401. }
  402. /**
  403. * batadv_softif_vlan_free_ref - decrease the vlan object refcounter and
  404. * possibly free it
  405. * @softif_vlan: the vlan object to release
  406. */
  407. void batadv_softif_vlan_free_ref(struct batadv_softif_vlan *vlan)
  408. {
  409. if (atomic_dec_and_test(&vlan->refcount)) {
  410. spin_lock_bh(&vlan->bat_priv->softif_vlan_list_lock);
  411. hlist_del_rcu(&vlan->list);
  412. spin_unlock_bh(&vlan->bat_priv->softif_vlan_list_lock);
  413. kfree_rcu(vlan, rcu);
  414. }
  415. }
  416. /**
  417. * batadv_softif_vlan_get - get the vlan object for a specific vid
  418. * @bat_priv: the bat priv with all the soft interface information
  419. * @vid: the identifier of the vlan object to retrieve
  420. *
  421. * Returns the private data of the vlan matching the vid passed as argument or
  422. * NULL otherwise. The refcounter of the returned object is incremented by 1.
  423. */
  424. struct batadv_softif_vlan *batadv_softif_vlan_get(struct batadv_priv *bat_priv,
  425. unsigned short vid)
  426. {
  427. struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
  428. rcu_read_lock();
  429. hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
  430. if (vlan_tmp->vid != vid)
  431. continue;
  432. if (!atomic_inc_not_zero(&vlan_tmp->refcount))
  433. continue;
  434. vlan = vlan_tmp;
  435. break;
  436. }
  437. rcu_read_unlock();
  438. return vlan;
  439. }
  440. /**
  441. * batadv_create_vlan - allocate the needed resources for a new vlan
  442. * @bat_priv: the bat priv with all the soft interface information
  443. * @vid: the VLAN identifier
  444. *
  445. * Returns 0 on success, a negative error otherwise.
  446. */
  447. int batadv_softif_create_vlan(struct batadv_priv *bat_priv, unsigned short vid)
  448. {
  449. struct batadv_softif_vlan *vlan;
  450. int err;
  451. vlan = batadv_softif_vlan_get(bat_priv, vid);
  452. if (vlan) {
  453. batadv_softif_vlan_free_ref(vlan);
  454. return -EEXIST;
  455. }
  456. vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
  457. if (!vlan)
  458. return -ENOMEM;
  459. vlan->bat_priv = bat_priv;
  460. vlan->vid = vid;
  461. atomic_set(&vlan->refcount, 1);
  462. atomic_set(&vlan->ap_isolation, 0);
  463. err = batadv_sysfs_add_vlan(bat_priv->soft_iface, vlan);
  464. if (err) {
  465. kfree(vlan);
  466. return err;
  467. }
  468. spin_lock_bh(&bat_priv->softif_vlan_list_lock);
  469. hlist_add_head_rcu(&vlan->list, &bat_priv->softif_vlan_list);
  470. spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
  471. /* add a new TT local entry. This one will be marked with the NOPURGE
  472. * flag
  473. */
  474. batadv_tt_local_add(bat_priv->soft_iface,
  475. bat_priv->soft_iface->dev_addr, vid,
  476. BATADV_NULL_IFINDEX, BATADV_NO_MARK);
  477. return 0;
  478. }
  479. /**
  480. * batadv_softif_destroy_vlan - remove and destroy a softif_vlan object
  481. * @bat_priv: the bat priv with all the soft interface information
  482. * @vlan: the object to remove
  483. */
  484. static void batadv_softif_destroy_vlan(struct batadv_priv *bat_priv,
  485. struct batadv_softif_vlan *vlan)
  486. {
  487. /* explicitly remove the associated TT local entry because it is marked
  488. * with the NOPURGE flag
  489. */
  490. batadv_tt_local_remove(bat_priv, bat_priv->soft_iface->dev_addr,
  491. vlan->vid, "vlan interface destroyed", false);
  492. batadv_sysfs_del_vlan(bat_priv, vlan);
  493. batadv_softif_vlan_free_ref(vlan);
  494. }
  495. /**
  496. * batadv_interface_add_vid - ndo_add_vid API implementation
  497. * @dev: the netdev of the mesh interface
  498. * @vid: identifier of the new vlan
  499. *
  500. * Set up all the internal structures for handling the new vlan on top of the
  501. * mesh interface
  502. *
  503. * Returns 0 on success or a negative error code in case of failure.
  504. */
  505. static int batadv_interface_add_vid(struct net_device *dev, __be16 proto,
  506. unsigned short vid)
  507. {
  508. struct batadv_priv *bat_priv = netdev_priv(dev);
  509. struct batadv_softif_vlan *vlan;
  510. int ret;
  511. /* only 802.1Q vlans are supported.
  512. * batman-adv does not know how to handle other types
  513. */
  514. if (proto != htons(ETH_P_8021Q))
  515. return -EINVAL;
  516. vid |= BATADV_VLAN_HAS_TAG;
  517. /* if a new vlan is getting created and it already exists, it means that
  518. * it was not deleted yet. batadv_softif_vlan_get() increases the
  519. * refcount in order to revive the object.
  520. *
  521. * if it does not exist then create it.
  522. */
  523. vlan = batadv_softif_vlan_get(bat_priv, vid);
  524. if (!vlan)
  525. return batadv_softif_create_vlan(bat_priv, vid);
  526. /* recreate the sysfs object if it was already destroyed (and it should
  527. * be since we received a kill_vid() for this vlan
  528. */
  529. if (!vlan->kobj) {
  530. ret = batadv_sysfs_add_vlan(bat_priv->soft_iface, vlan);
  531. if (ret) {
  532. batadv_softif_vlan_free_ref(vlan);
  533. return ret;
  534. }
  535. }
  536. /* add a new TT local entry. This one will be marked with the NOPURGE
  537. * flag. This must be added again, even if the vlan object already
  538. * exists, because the entry was deleted by kill_vid()
  539. */
  540. batadv_tt_local_add(bat_priv->soft_iface,
  541. bat_priv->soft_iface->dev_addr, vid,
  542. BATADV_NULL_IFINDEX, BATADV_NO_MARK);
  543. return 0;
  544. }
  545. /**
  546. * batadv_interface_kill_vid - ndo_kill_vid API implementation
  547. * @dev: the netdev of the mesh interface
  548. * @vid: identifier of the deleted vlan
  549. *
  550. * Destroy all the internal structures used to handle the vlan identified by vid
  551. * on top of the mesh interface
  552. *
  553. * Returns 0 on success, -EINVAL if the specified prototype is not ETH_P_8021Q
  554. * or -ENOENT if the specified vlan id wasn't registered.
  555. */
  556. static int batadv_interface_kill_vid(struct net_device *dev, __be16 proto,
  557. unsigned short vid)
  558. {
  559. struct batadv_priv *bat_priv = netdev_priv(dev);
  560. struct batadv_softif_vlan *vlan;
  561. /* only 802.1Q vlans are supported. batman-adv does not know how to
  562. * handle other types
  563. */
  564. if (proto != htons(ETH_P_8021Q))
  565. return -EINVAL;
  566. vlan = batadv_softif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG);
  567. if (!vlan)
  568. return -ENOENT;
  569. batadv_softif_destroy_vlan(bat_priv, vlan);
  570. /* finally free the vlan object */
  571. batadv_softif_vlan_free_ref(vlan);
  572. return 0;
  573. }
  574. /* batman-adv network devices have devices nesting below it and are a special
  575. * "super class" of normal network devices; split their locks off into a
  576. * separate class since they always nest.
  577. */
  578. static struct lock_class_key batadv_netdev_xmit_lock_key;
  579. static struct lock_class_key batadv_netdev_addr_lock_key;
  580. /**
  581. * batadv_set_lockdep_class_one - Set lockdep class for a single tx queue
  582. * @dev: device which owns the tx queue
  583. * @txq: tx queue to modify
  584. * @_unused: always NULL
  585. */
  586. static void batadv_set_lockdep_class_one(struct net_device *dev,
  587. struct netdev_queue *txq,
  588. void *_unused)
  589. {
  590. lockdep_set_class(&txq->_xmit_lock, &batadv_netdev_xmit_lock_key);
  591. }
  592. /**
  593. * batadv_set_lockdep_class - Set txq and addr_list lockdep class
  594. * @dev: network device to modify
  595. */
  596. static void batadv_set_lockdep_class(struct net_device *dev)
  597. {
  598. lockdep_set_class(&dev->addr_list_lock, &batadv_netdev_addr_lock_key);
  599. netdev_for_each_tx_queue(dev, batadv_set_lockdep_class_one, NULL);
  600. }
  601. /**
  602. * batadv_softif_destroy_finish - cleans up the remains of a softif
  603. * @work: work queue item
  604. *
  605. * Free the parts of the soft interface which can not be removed under
  606. * rtnl lock (to prevent deadlock situations).
  607. */
  608. static void batadv_softif_destroy_finish(struct work_struct *work)
  609. {
  610. struct batadv_softif_vlan *vlan;
  611. struct batadv_priv *bat_priv;
  612. struct net_device *soft_iface;
  613. bat_priv = container_of(work, struct batadv_priv,
  614. cleanup_work);
  615. soft_iface = bat_priv->soft_iface;
  616. /* destroy the "untagged" VLAN */
  617. vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
  618. if (vlan) {
  619. batadv_softif_destroy_vlan(bat_priv, vlan);
  620. batadv_softif_vlan_free_ref(vlan);
  621. }
  622. batadv_sysfs_del_meshif(soft_iface);
  623. unregister_netdev(soft_iface);
  624. }
  625. /**
  626. * batadv_softif_init_late - late stage initialization of soft interface
  627. * @dev: registered network device to modify
  628. *
  629. * Returns error code on failures
  630. */
  631. static int batadv_softif_init_late(struct net_device *dev)
  632. {
  633. struct batadv_priv *bat_priv;
  634. uint32_t random_seqno;
  635. int ret;
  636. size_t cnt_len = sizeof(uint64_t) * BATADV_CNT_NUM;
  637. batadv_set_lockdep_class(dev);
  638. bat_priv = netdev_priv(dev);
  639. bat_priv->soft_iface = dev;
  640. INIT_WORK(&bat_priv->cleanup_work, batadv_softif_destroy_finish);
  641. /* batadv_interface_stats() needs to be available as soon as
  642. * register_netdevice() has been called
  643. */
  644. bat_priv->bat_counters = __alloc_percpu(cnt_len, __alignof__(uint64_t));
  645. if (!bat_priv->bat_counters)
  646. return -ENOMEM;
  647. atomic_set(&bat_priv->aggregated_ogms, 1);
  648. atomic_set(&bat_priv->bonding, 0);
  649. #ifdef CONFIG_BATMAN_ADV_BLA
  650. atomic_set(&bat_priv->bridge_loop_avoidance, 1);
  651. #endif
  652. #ifdef CONFIG_BATMAN_ADV_DAT
  653. atomic_set(&bat_priv->distributed_arp_table, 1);
  654. #endif
  655. #ifdef CONFIG_BATMAN_ADV_MCAST
  656. bat_priv->mcast.flags = BATADV_NO_FLAGS;
  657. atomic_set(&bat_priv->multicast_mode, 1);
  658. atomic_set(&bat_priv->mcast.num_disabled, 0);
  659. atomic_set(&bat_priv->mcast.num_want_all_unsnoopables, 0);
  660. atomic_set(&bat_priv->mcast.num_want_all_ipv4, 0);
  661. atomic_set(&bat_priv->mcast.num_want_all_ipv6, 0);
  662. #endif
  663. atomic_set(&bat_priv->gw_mode, BATADV_GW_MODE_OFF);
  664. atomic_set(&bat_priv->gw_sel_class, 20);
  665. atomic_set(&bat_priv->gw.bandwidth_down, 100);
  666. atomic_set(&bat_priv->gw.bandwidth_up, 20);
  667. atomic_set(&bat_priv->orig_interval, 1000);
  668. atomic_set(&bat_priv->hop_penalty, 30);
  669. #ifdef CONFIG_BATMAN_ADV_DEBUG
  670. atomic_set(&bat_priv->log_level, 0);
  671. #endif
  672. atomic_set(&bat_priv->fragmentation, 1);
  673. atomic_set(&bat_priv->packet_size_max, ETH_DATA_LEN);
  674. atomic_set(&bat_priv->bcast_queue_left, BATADV_BCAST_QUEUE_LEN);
  675. atomic_set(&bat_priv->batman_queue_left, BATADV_BATMAN_QUEUE_LEN);
  676. atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
  677. atomic_set(&bat_priv->bcast_seqno, 1);
  678. atomic_set(&bat_priv->tt.vn, 0);
  679. atomic_set(&bat_priv->tt.local_changes, 0);
  680. atomic_set(&bat_priv->tt.ogm_append_cnt, 0);
  681. #ifdef CONFIG_BATMAN_ADV_BLA
  682. atomic_set(&bat_priv->bla.num_requests, 0);
  683. #endif
  684. bat_priv->tt.last_changeset = NULL;
  685. bat_priv->tt.last_changeset_len = 0;
  686. bat_priv->isolation_mark = 0;
  687. bat_priv->isolation_mark_mask = 0;
  688. /* randomize initial seqno to avoid collision */
  689. get_random_bytes(&random_seqno, sizeof(random_seqno));
  690. atomic_set(&bat_priv->frag_seqno, random_seqno);
  691. bat_priv->primary_if = NULL;
  692. bat_priv->num_ifaces = 0;
  693. batadv_nc_init_bat_priv(bat_priv);
  694. ret = batadv_algo_select(bat_priv, batadv_routing_algo);
  695. if (ret < 0)
  696. goto free_bat_counters;
  697. ret = batadv_debugfs_add_meshif(dev);
  698. if (ret < 0)
  699. goto free_bat_counters;
  700. ret = batadv_mesh_init(dev);
  701. if (ret < 0)
  702. goto unreg_debugfs;
  703. return 0;
  704. unreg_debugfs:
  705. batadv_debugfs_del_meshif(dev);
  706. free_bat_counters:
  707. free_percpu(bat_priv->bat_counters);
  708. bat_priv->bat_counters = NULL;
  709. return ret;
  710. }
  711. /**
  712. * batadv_softif_slave_add - Add a slave interface to a batadv_soft_interface
  713. * @dev: batadv_soft_interface used as master interface
  714. * @slave_dev: net_device which should become the slave interface
  715. *
  716. * Return 0 if successful or error otherwise.
  717. */
  718. static int batadv_softif_slave_add(struct net_device *dev,
  719. struct net_device *slave_dev)
  720. {
  721. struct batadv_hard_iface *hard_iface;
  722. int ret = -EINVAL;
  723. hard_iface = batadv_hardif_get_by_netdev(slave_dev);
  724. if (!hard_iface || hard_iface->soft_iface)
  725. goto out;
  726. ret = batadv_hardif_enable_interface(hard_iface, dev->name);
  727. out:
  728. if (hard_iface)
  729. batadv_hardif_free_ref(hard_iface);
  730. return ret;
  731. }
  732. /**
  733. * batadv_softif_slave_del - Delete a slave iface from a batadv_soft_interface
  734. * @dev: batadv_soft_interface used as master interface
  735. * @slave_dev: net_device which should be removed from the master interface
  736. *
  737. * Return 0 if successful or error otherwise.
  738. */
  739. static int batadv_softif_slave_del(struct net_device *dev,
  740. struct net_device *slave_dev)
  741. {
  742. struct batadv_hard_iface *hard_iface;
  743. int ret = -EINVAL;
  744. hard_iface = batadv_hardif_get_by_netdev(slave_dev);
  745. if (!hard_iface || hard_iface->soft_iface != dev)
  746. goto out;
  747. batadv_hardif_disable_interface(hard_iface, BATADV_IF_CLEANUP_KEEP);
  748. ret = 0;
  749. out:
  750. if (hard_iface)
  751. batadv_hardif_free_ref(hard_iface);
  752. return ret;
  753. }
  754. static const struct net_device_ops batadv_netdev_ops = {
  755. .ndo_init = batadv_softif_init_late,
  756. .ndo_open = batadv_interface_open,
  757. .ndo_stop = batadv_interface_release,
  758. .ndo_get_stats = batadv_interface_stats,
  759. .ndo_vlan_rx_add_vid = batadv_interface_add_vid,
  760. .ndo_vlan_rx_kill_vid = batadv_interface_kill_vid,
  761. .ndo_set_mac_address = batadv_interface_set_mac_addr,
  762. .ndo_change_mtu = batadv_interface_change_mtu,
  763. .ndo_set_rx_mode = batadv_interface_set_rx_mode,
  764. .ndo_start_xmit = batadv_interface_tx,
  765. .ndo_validate_addr = eth_validate_addr,
  766. .ndo_add_slave = batadv_softif_slave_add,
  767. .ndo_del_slave = batadv_softif_slave_del,
  768. };
  769. /**
  770. * batadv_softif_free - Deconstructor of batadv_soft_interface
  771. * @dev: Device to cleanup and remove
  772. */
  773. static void batadv_softif_free(struct net_device *dev)
  774. {
  775. batadv_debugfs_del_meshif(dev);
  776. batadv_mesh_free(dev);
  777. /* some scheduled RCU callbacks need the bat_priv struct to accomplish
  778. * their tasks. Wait for them all to be finished before freeing the
  779. * netdev and its private data (bat_priv)
  780. */
  781. rcu_barrier();
  782. free_netdev(dev);
  783. }
  784. /**
  785. * batadv_softif_init_early - early stage initialization of soft interface
  786. * @dev: registered network device to modify
  787. */
  788. static void batadv_softif_init_early(struct net_device *dev)
  789. {
  790. struct batadv_priv *priv = netdev_priv(dev);
  791. ether_setup(dev);
  792. dev->netdev_ops = &batadv_netdev_ops;
  793. dev->destructor = batadv_softif_free;
  794. dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  795. dev->tx_queue_len = 0;
  796. /* can't call min_mtu, because the needed variables
  797. * have not been initialized yet
  798. */
  799. dev->mtu = ETH_DATA_LEN;
  800. /* reserve more space in the skbuff for our header */
  801. dev->hard_header_len = batadv_max_header_len();
  802. /* generate random address */
  803. eth_hw_addr_random(dev);
  804. dev->ethtool_ops = &batadv_ethtool_ops;
  805. memset(priv, 0, sizeof(*priv));
  806. }
  807. struct net_device *batadv_softif_create(const char *name)
  808. {
  809. struct net_device *soft_iface;
  810. int ret;
  811. soft_iface = alloc_netdev(sizeof(struct batadv_priv), name,
  812. NET_NAME_UNKNOWN, batadv_softif_init_early);
  813. if (!soft_iface)
  814. return NULL;
  815. soft_iface->rtnl_link_ops = &batadv_link_ops;
  816. ret = register_netdevice(soft_iface);
  817. if (ret < 0) {
  818. pr_err("Unable to register the batman interface '%s': %i\n",
  819. name, ret);
  820. free_netdev(soft_iface);
  821. return NULL;
  822. }
  823. return soft_iface;
  824. }
  825. /**
  826. * batadv_softif_destroy_sysfs - deletion of batadv_soft_interface via sysfs
  827. * @soft_iface: the to-be-removed batman-adv interface
  828. */
  829. void batadv_softif_destroy_sysfs(struct net_device *soft_iface)
  830. {
  831. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  832. queue_work(batadv_event_workqueue, &bat_priv->cleanup_work);
  833. }
  834. /**
  835. * batadv_softif_destroy_netlink - deletion of batadv_soft_interface via netlink
  836. * @soft_iface: the to-be-removed batman-adv interface
  837. * @head: list pointer
  838. */
  839. static void batadv_softif_destroy_netlink(struct net_device *soft_iface,
  840. struct list_head *head)
  841. {
  842. struct batadv_hard_iface *hard_iface;
  843. list_for_each_entry(hard_iface, &batadv_hardif_list, list) {
  844. if (hard_iface->soft_iface == soft_iface)
  845. batadv_hardif_disable_interface(hard_iface,
  846. BATADV_IF_CLEANUP_KEEP);
  847. }
  848. batadv_sysfs_del_meshif(soft_iface);
  849. unregister_netdevice_queue(soft_iface, head);
  850. }
  851. int batadv_softif_is_valid(const struct net_device *net_dev)
  852. {
  853. if (net_dev->netdev_ops->ndo_start_xmit == batadv_interface_tx)
  854. return 1;
  855. return 0;
  856. }
  857. struct rtnl_link_ops batadv_link_ops __read_mostly = {
  858. .kind = "batadv",
  859. .priv_size = sizeof(struct batadv_priv),
  860. .setup = batadv_softif_init_early,
  861. .dellink = batadv_softif_destroy_netlink,
  862. };
  863. /* ethtool */
  864. static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  865. {
  866. cmd->supported = 0;
  867. cmd->advertising = 0;
  868. ethtool_cmd_speed_set(cmd, SPEED_10);
  869. cmd->duplex = DUPLEX_FULL;
  870. cmd->port = PORT_TP;
  871. cmd->phy_address = 0;
  872. cmd->transceiver = XCVR_INTERNAL;
  873. cmd->autoneg = AUTONEG_DISABLE;
  874. cmd->maxtxpkt = 0;
  875. cmd->maxrxpkt = 0;
  876. return 0;
  877. }
  878. static void batadv_get_drvinfo(struct net_device *dev,
  879. struct ethtool_drvinfo *info)
  880. {
  881. strlcpy(info->driver, "B.A.T.M.A.N. advanced", sizeof(info->driver));
  882. strlcpy(info->version, BATADV_SOURCE_VERSION, sizeof(info->version));
  883. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  884. strlcpy(info->bus_info, "batman", sizeof(info->bus_info));
  885. }
  886. static u32 batadv_get_msglevel(struct net_device *dev)
  887. {
  888. return -EOPNOTSUPP;
  889. }
  890. static void batadv_set_msglevel(struct net_device *dev, u32 value)
  891. {
  892. }
  893. static u32 batadv_get_link(struct net_device *dev)
  894. {
  895. return 1;
  896. }
  897. /* Inspired by drivers/net/ethernet/dlink/sundance.c:1702
  898. * Declare each description string in struct.name[] to get fixed sized buffer
  899. * and compile time checking for strings longer than ETH_GSTRING_LEN.
  900. */
  901. static const struct {
  902. const char name[ETH_GSTRING_LEN];
  903. } batadv_counters_strings[] = {
  904. { "tx" },
  905. { "tx_bytes" },
  906. { "tx_dropped" },
  907. { "rx" },
  908. { "rx_bytes" },
  909. { "forward" },
  910. { "forward_bytes" },
  911. { "mgmt_tx" },
  912. { "mgmt_tx_bytes" },
  913. { "mgmt_rx" },
  914. { "mgmt_rx_bytes" },
  915. { "frag_tx" },
  916. { "frag_tx_bytes" },
  917. { "frag_rx" },
  918. { "frag_rx_bytes" },
  919. { "frag_fwd" },
  920. { "frag_fwd_bytes" },
  921. { "tt_request_tx" },
  922. { "tt_request_rx" },
  923. { "tt_response_tx" },
  924. { "tt_response_rx" },
  925. { "tt_roam_adv_tx" },
  926. { "tt_roam_adv_rx" },
  927. #ifdef CONFIG_BATMAN_ADV_DAT
  928. { "dat_get_tx" },
  929. { "dat_get_rx" },
  930. { "dat_put_tx" },
  931. { "dat_put_rx" },
  932. { "dat_cached_reply_tx" },
  933. #endif
  934. #ifdef CONFIG_BATMAN_ADV_NC
  935. { "nc_code" },
  936. { "nc_code_bytes" },
  937. { "nc_recode" },
  938. { "nc_recode_bytes" },
  939. { "nc_buffer" },
  940. { "nc_decode" },
  941. { "nc_decode_bytes" },
  942. { "nc_decode_failed" },
  943. { "nc_sniffed" },
  944. #endif
  945. };
  946. static void batadv_get_strings(struct net_device *dev, uint32_t stringset,
  947. uint8_t *data)
  948. {
  949. if (stringset == ETH_SS_STATS)
  950. memcpy(data, batadv_counters_strings,
  951. sizeof(batadv_counters_strings));
  952. }
  953. static void batadv_get_ethtool_stats(struct net_device *dev,
  954. struct ethtool_stats *stats,
  955. uint64_t *data)
  956. {
  957. struct batadv_priv *bat_priv = netdev_priv(dev);
  958. int i;
  959. for (i = 0; i < BATADV_CNT_NUM; i++)
  960. data[i] = batadv_sum_counter(bat_priv, i);
  961. }
  962. static int batadv_get_sset_count(struct net_device *dev, int stringset)
  963. {
  964. if (stringset == ETH_SS_STATS)
  965. return BATADV_CNT_NUM;
  966. return -EOPNOTSUPP;
  967. }