soft-interface.c 31 KB

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