soft-interface.c 32 KB

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