mesh-interface.c 31 KB

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