soft-interface.c 29 KB

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