/net/batman-adv/soft-interface.c

http://github.com/mirrors/linux · C · 1147 lines · 718 code · 171 blank · 258 comment · 105 complexity · f28881d4d2519e742703b65f94f91f3b MD5 · raw file

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