soft-interface.c 33 KB

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