soft-interface.c 31 KB

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