soft-interface.c 31 KB

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