soft-interface.c 32 KB

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