soft-interface.c 33 KB

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