soft-interface.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  387. batadv_inc_counter(bat_priv, BATADV_CNT_RX);
  388. batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
  389. skb->len + ETH_HLEN);
  390. soft_iface->last_rx = jiffies;
  391. /* Let the bridge loop avoidance check the packet. If will
  392. * not handle it, we can safely push it up.
  393. */
  394. if (batadv_bla_rx(bat_priv, skb, vid, is_bcast))
  395. goto out;
  396. if (orig_node)
  397. batadv_tt_add_temporary_global_entry(bat_priv, orig_node,
  398. ethhdr->h_source, vid);
  399. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  400. /* set the mark on broadcast packets if AP isolation is ON and
  401. * the packet is coming from an "isolated" client
  402. */
  403. if (batadv_vlan_ap_isola_get(bat_priv, vid) &&
  404. batadv_tt_global_is_isolated(bat_priv, ethhdr->h_source,
  405. vid)) {
  406. /* save bits in skb->mark not covered by the mask and
  407. * apply the mark on the rest
  408. */
  409. skb->mark &= ~bat_priv->isolation_mark_mask;
  410. skb->mark |= bat_priv->isolation_mark;
  411. }
  412. } else if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source,
  413. ethhdr->h_dest, vid)) {
  414. goto dropped;
  415. }
  416. netif_rx(skb);
  417. goto out;
  418. dropped:
  419. kfree_skb(skb);
  420. out:
  421. return;
  422. }
  423. /**
  424. * batadv_softif_vlan_release - release vlan from lists and queue for free after
  425. * rcu grace period
  426. * @ref: kref pointer of the vlan object
  427. */
  428. static void batadv_softif_vlan_release(struct kref *ref)
  429. {
  430. struct batadv_softif_vlan *vlan;
  431. vlan = container_of(ref, struct batadv_softif_vlan, refcount);
  432. spin_lock_bh(&vlan->bat_priv->softif_vlan_list_lock);
  433. hlist_del_rcu(&vlan->list);
  434. spin_unlock_bh(&vlan->bat_priv->softif_vlan_list_lock);
  435. kfree_rcu(vlan, rcu);
  436. }
  437. /**
  438. * batadv_softif_vlan_put - decrease the vlan object refcounter and
  439. * possibly release it
  440. * @vlan: the vlan object to release
  441. */
  442. void batadv_softif_vlan_put(struct batadv_softif_vlan *vlan)
  443. {
  444. if (!vlan)
  445. return;
  446. kref_put(&vlan->refcount, batadv_softif_vlan_release);
  447. }
  448. /**
  449. * batadv_softif_vlan_get - get the vlan object for a specific vid
  450. * @bat_priv: the bat priv with all the soft interface information
  451. * @vid: the identifier of the vlan object to retrieve
  452. *
  453. * Return: the private data of the vlan matching the vid passed as argument or
  454. * NULL otherwise. The refcounter of the returned object is incremented by 1.
  455. */
  456. struct batadv_softif_vlan *batadv_softif_vlan_get(struct batadv_priv *bat_priv,
  457. unsigned short vid)
  458. {
  459. struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
  460. rcu_read_lock();
  461. hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
  462. if (vlan_tmp->vid != vid)
  463. continue;
  464. if (!kref_get_unless_zero(&vlan_tmp->refcount))
  465. continue;
  466. vlan = vlan_tmp;
  467. break;
  468. }
  469. rcu_read_unlock();
  470. return vlan;
  471. }
  472. /**
  473. * batadv_softif_create_vlan - allocate the needed resources for a new vlan
  474. * @bat_priv: the bat priv with all the soft interface information
  475. * @vid: the VLAN identifier
  476. *
  477. * Return: 0 on success, a negative error otherwise.
  478. */
  479. int batadv_softif_create_vlan(struct batadv_priv *bat_priv, unsigned short vid)
  480. {
  481. struct batadv_softif_vlan *vlan;
  482. int err;
  483. vlan = batadv_softif_vlan_get(bat_priv, vid);
  484. if (vlan) {
  485. batadv_softif_vlan_put(vlan);
  486. return -EEXIST;
  487. }
  488. vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
  489. if (!vlan)
  490. return -ENOMEM;
  491. vlan->bat_priv = bat_priv;
  492. vlan->vid = vid;
  493. kref_init(&vlan->refcount);
  494. atomic_set(&vlan->ap_isolation, 0);
  495. err = batadv_sysfs_add_vlan(bat_priv->soft_iface, vlan);
  496. if (err) {
  497. kfree(vlan);
  498. return err;
  499. }
  500. spin_lock_bh(&bat_priv->softif_vlan_list_lock);
  501. kref_get(&vlan->refcount);
  502. hlist_add_head_rcu(&vlan->list, &bat_priv->softif_vlan_list);
  503. spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
  504. /* add a new TT local entry. This one will be marked with the NOPURGE
  505. * flag
  506. */
  507. batadv_tt_local_add(bat_priv->soft_iface,
  508. bat_priv->soft_iface->dev_addr, vid,
  509. BATADV_NULL_IFINDEX, BATADV_NO_MARK);
  510. /* don't return reference to new softif_vlan */
  511. batadv_softif_vlan_put(vlan);
  512. return 0;
  513. }
  514. /**
  515. * batadv_softif_destroy_vlan - remove and destroy a softif_vlan object
  516. * @bat_priv: the bat priv with all the soft interface information
  517. * @vlan: the object to remove
  518. */
  519. static void batadv_softif_destroy_vlan(struct batadv_priv *bat_priv,
  520. struct batadv_softif_vlan *vlan)
  521. {
  522. /* explicitly remove the associated TT local entry because it is marked
  523. * with the NOPURGE flag
  524. */
  525. batadv_tt_local_remove(bat_priv, bat_priv->soft_iface->dev_addr,
  526. vlan->vid, "vlan interface destroyed", false);
  527. batadv_sysfs_del_vlan(bat_priv, vlan);
  528. batadv_softif_vlan_put(vlan);
  529. }
  530. /**
  531. * batadv_interface_add_vid - ndo_add_vid API implementation
  532. * @dev: the netdev of the mesh interface
  533. * @proto: protocol of the the vlan id
  534. * @vid: identifier of the new vlan
  535. *
  536. * Set up all the internal structures for handling the new vlan on top of the
  537. * mesh interface
  538. *
  539. * Return: 0 on success or a negative error code in case of failure.
  540. */
  541. static int batadv_interface_add_vid(struct net_device *dev, __be16 proto,
  542. unsigned short vid)
  543. {
  544. struct batadv_priv *bat_priv = netdev_priv(dev);
  545. struct batadv_softif_vlan *vlan;
  546. int ret;
  547. /* only 802.1Q vlans are supported.
  548. * batman-adv does not know how to handle other types
  549. */
  550. if (proto != htons(ETH_P_8021Q))
  551. return -EINVAL;
  552. vid |= BATADV_VLAN_HAS_TAG;
  553. /* if a new vlan is getting created and it already exists, it means that
  554. * it was not deleted yet. batadv_softif_vlan_get() increases the
  555. * refcount in order to revive the object.
  556. *
  557. * if it does not exist then create it.
  558. */
  559. vlan = batadv_softif_vlan_get(bat_priv, vid);
  560. if (!vlan)
  561. return batadv_softif_create_vlan(bat_priv, vid);
  562. /* recreate the sysfs object if it was already destroyed (and it should
  563. * be since we received a kill_vid() for this vlan
  564. */
  565. if (!vlan->kobj) {
  566. ret = batadv_sysfs_add_vlan(bat_priv->soft_iface, vlan);
  567. if (ret) {
  568. batadv_softif_vlan_put(vlan);
  569. return ret;
  570. }
  571. }
  572. /* add a new TT local entry. This one will be marked with the NOPURGE
  573. * flag. This must be added again, even if the vlan object already
  574. * exists, because the entry was deleted by kill_vid()
  575. */
  576. batadv_tt_local_add(bat_priv->soft_iface,
  577. bat_priv->soft_iface->dev_addr, vid,
  578. BATADV_NULL_IFINDEX, BATADV_NO_MARK);
  579. return 0;
  580. }
  581. /**
  582. * batadv_interface_kill_vid - ndo_kill_vid API implementation
  583. * @dev: the netdev of the mesh interface
  584. * @proto: protocol of the the vlan id
  585. * @vid: identifier of the deleted vlan
  586. *
  587. * Destroy all the internal structures used to handle the vlan identified by vid
  588. * on top of the mesh interface
  589. *
  590. * Return: 0 on success, -EINVAL if the specified prototype is not ETH_P_8021Q
  591. * or -ENOENT if the specified vlan id wasn't registered.
  592. */
  593. static int batadv_interface_kill_vid(struct net_device *dev, __be16 proto,
  594. unsigned short vid)
  595. {
  596. struct batadv_priv *bat_priv = netdev_priv(dev);
  597. struct batadv_softif_vlan *vlan;
  598. /* only 802.1Q vlans are supported. batman-adv does not know how to
  599. * handle other types
  600. */
  601. if (proto != htons(ETH_P_8021Q))
  602. return -EINVAL;
  603. vlan = batadv_softif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG);
  604. if (!vlan)
  605. return -ENOENT;
  606. batadv_softif_destroy_vlan(bat_priv, vlan);
  607. /* finally free the vlan object */
  608. batadv_softif_vlan_put(vlan);
  609. return 0;
  610. }
  611. /* batman-adv network devices have devices nesting below it and are a special
  612. * "super class" of normal network devices; split their locks off into a
  613. * separate class since they always nest.
  614. */
  615. static struct lock_class_key batadv_netdev_xmit_lock_key;
  616. static struct lock_class_key batadv_netdev_addr_lock_key;
  617. /**
  618. * batadv_set_lockdep_class_one - Set lockdep class for a single tx queue
  619. * @dev: device which owns the tx queue
  620. * @txq: tx queue to modify
  621. * @_unused: always NULL
  622. */
  623. static void batadv_set_lockdep_class_one(struct net_device *dev,
  624. struct netdev_queue *txq,
  625. void *_unused)
  626. {
  627. lockdep_set_class(&txq->_xmit_lock, &batadv_netdev_xmit_lock_key);
  628. }
  629. /**
  630. * batadv_set_lockdep_class - Set txq and addr_list lockdep class
  631. * @dev: network device to modify
  632. */
  633. static void batadv_set_lockdep_class(struct net_device *dev)
  634. {
  635. lockdep_set_class(&dev->addr_list_lock, &batadv_netdev_addr_lock_key);
  636. netdev_for_each_tx_queue(dev, batadv_set_lockdep_class_one, NULL);
  637. }
  638. /**
  639. * batadv_softif_init_late - late stage initialization of soft interface
  640. * @dev: registered network device to modify
  641. *
  642. * Return: error code on failures
  643. */
  644. static int batadv_softif_init_late(struct net_device *dev)
  645. {
  646. struct batadv_priv *bat_priv;
  647. u32 random_seqno;
  648. int ret;
  649. size_t cnt_len = sizeof(u64) * BATADV_CNT_NUM;
  650. batadv_set_lockdep_class(dev);
  651. bat_priv = netdev_priv(dev);
  652. bat_priv->soft_iface = dev;
  653. /* batadv_interface_stats() needs to be available as soon as
  654. * register_netdevice() has been called
  655. */
  656. bat_priv->bat_counters = __alloc_percpu(cnt_len, __alignof__(u64));
  657. if (!bat_priv->bat_counters)
  658. return -ENOMEM;
  659. atomic_set(&bat_priv->aggregated_ogms, 1);
  660. atomic_set(&bat_priv->bonding, 0);
  661. #ifdef CONFIG_BATMAN_ADV_BLA
  662. atomic_set(&bat_priv->bridge_loop_avoidance, 1);
  663. #endif
  664. #ifdef CONFIG_BATMAN_ADV_DAT
  665. atomic_set(&bat_priv->distributed_arp_table, 1);
  666. #endif
  667. #ifdef CONFIG_BATMAN_ADV_MCAST
  668. bat_priv->mcast.querier_ipv4.exists = false;
  669. bat_priv->mcast.querier_ipv4.shadowing = false;
  670. bat_priv->mcast.querier_ipv6.exists = false;
  671. bat_priv->mcast.querier_ipv6.shadowing = false;
  672. bat_priv->mcast.flags = BATADV_NO_FLAGS;
  673. atomic_set(&bat_priv->multicast_mode, 1);
  674. atomic_set(&bat_priv->mcast.num_disabled, 0);
  675. atomic_set(&bat_priv->mcast.num_want_all_unsnoopables, 0);
  676. atomic_set(&bat_priv->mcast.num_want_all_ipv4, 0);
  677. atomic_set(&bat_priv->mcast.num_want_all_ipv6, 0);
  678. #endif
  679. atomic_set(&bat_priv->gw.mode, BATADV_GW_MODE_OFF);
  680. atomic_set(&bat_priv->gw.sel_class, 20);
  681. atomic_set(&bat_priv->gw.bandwidth_down, 100);
  682. atomic_set(&bat_priv->gw.bandwidth_up, 20);
  683. atomic_set(&bat_priv->orig_interval, 1000);
  684. atomic_set(&bat_priv->hop_penalty, 30);
  685. #ifdef CONFIG_BATMAN_ADV_DEBUG
  686. atomic_set(&bat_priv->log_level, 0);
  687. #endif
  688. atomic_set(&bat_priv->fragmentation, 1);
  689. atomic_set(&bat_priv->packet_size_max, ETH_DATA_LEN);
  690. atomic_set(&bat_priv->bcast_queue_left, BATADV_BCAST_QUEUE_LEN);
  691. atomic_set(&bat_priv->batman_queue_left, BATADV_BATMAN_QUEUE_LEN);
  692. atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
  693. atomic_set(&bat_priv->bcast_seqno, 1);
  694. atomic_set(&bat_priv->tt.vn, 0);
  695. atomic_set(&bat_priv->tt.local_changes, 0);
  696. atomic_set(&bat_priv->tt.ogm_append_cnt, 0);
  697. #ifdef CONFIG_BATMAN_ADV_BLA
  698. atomic_set(&bat_priv->bla.num_requests, 0);
  699. #endif
  700. atomic_set(&bat_priv->tp_num, 0);
  701. bat_priv->tt.last_changeset = NULL;
  702. bat_priv->tt.last_changeset_len = 0;
  703. bat_priv->isolation_mark = 0;
  704. bat_priv->isolation_mark_mask = 0;
  705. /* randomize initial seqno to avoid collision */
  706. get_random_bytes(&random_seqno, sizeof(random_seqno));
  707. atomic_set(&bat_priv->frag_seqno, random_seqno);
  708. bat_priv->primary_if = NULL;
  709. bat_priv->num_ifaces = 0;
  710. batadv_nc_init_bat_priv(bat_priv);
  711. ret = batadv_algo_select(bat_priv, batadv_routing_algo);
  712. if (ret < 0)
  713. goto free_bat_counters;
  714. ret = batadv_debugfs_add_meshif(dev);
  715. if (ret < 0)
  716. goto free_bat_counters;
  717. ret = batadv_mesh_init(dev);
  718. if (ret < 0)
  719. goto unreg_debugfs;
  720. return 0;
  721. unreg_debugfs:
  722. batadv_debugfs_del_meshif(dev);
  723. free_bat_counters:
  724. free_percpu(bat_priv->bat_counters);
  725. bat_priv->bat_counters = NULL;
  726. return ret;
  727. }
  728. /**
  729. * batadv_softif_slave_add - Add a slave interface to a batadv_soft_interface
  730. * @dev: batadv_soft_interface used as master interface
  731. * @slave_dev: net_device which should become the slave interface
  732. *
  733. * Return: 0 if successful or error otherwise.
  734. */
  735. static int batadv_softif_slave_add(struct net_device *dev,
  736. struct net_device *slave_dev)
  737. {
  738. struct batadv_hard_iface *hard_iface;
  739. struct net *net = dev_net(dev);
  740. int ret = -EINVAL;
  741. hard_iface = batadv_hardif_get_by_netdev(slave_dev);
  742. if (!hard_iface || hard_iface->soft_iface)
  743. goto out;
  744. ret = batadv_hardif_enable_interface(hard_iface, net, dev->name);
  745. out:
  746. if (hard_iface)
  747. batadv_hardif_put(hard_iface);
  748. return ret;
  749. }
  750. /**
  751. * batadv_softif_slave_del - Delete a slave iface from a batadv_soft_interface
  752. * @dev: batadv_soft_interface used as master interface
  753. * @slave_dev: net_device which should be removed from the master interface
  754. *
  755. * Return: 0 if successful or error otherwise.
  756. */
  757. static int batadv_softif_slave_del(struct net_device *dev,
  758. struct net_device *slave_dev)
  759. {
  760. struct batadv_hard_iface *hard_iface;
  761. int ret = -EINVAL;
  762. hard_iface = batadv_hardif_get_by_netdev(slave_dev);
  763. if (!hard_iface || hard_iface->soft_iface != dev)
  764. goto out;
  765. batadv_hardif_disable_interface(hard_iface, BATADV_IF_CLEANUP_KEEP);
  766. ret = 0;
  767. out:
  768. if (hard_iface)
  769. batadv_hardif_put(hard_iface);
  770. return ret;
  771. }
  772. static const struct net_device_ops batadv_netdev_ops = {
  773. .ndo_init = batadv_softif_init_late,
  774. .ndo_open = batadv_interface_open,
  775. .ndo_stop = batadv_interface_release,
  776. .ndo_get_stats = batadv_interface_stats,
  777. .ndo_vlan_rx_add_vid = batadv_interface_add_vid,
  778. .ndo_vlan_rx_kill_vid = batadv_interface_kill_vid,
  779. .ndo_set_mac_address = batadv_interface_set_mac_addr,
  780. .ndo_change_mtu = batadv_interface_change_mtu,
  781. .ndo_set_rx_mode = batadv_interface_set_rx_mode,
  782. .ndo_start_xmit = batadv_interface_tx,
  783. .ndo_validate_addr = eth_validate_addr,
  784. .ndo_add_slave = batadv_softif_slave_add,
  785. .ndo_del_slave = batadv_softif_slave_del,
  786. };
  787. /**
  788. * batadv_softif_free - Deconstructor of batadv_soft_interface
  789. * @dev: Device to cleanup and remove
  790. */
  791. static void batadv_softif_free(struct net_device *dev)
  792. {
  793. batadv_debugfs_del_meshif(dev);
  794. batadv_mesh_free(dev);
  795. /* some scheduled RCU callbacks need the bat_priv struct to accomplish
  796. * their tasks. Wait for them all to be finished before freeing the
  797. * netdev and its private data (bat_priv)
  798. */
  799. rcu_barrier();
  800. free_netdev(dev);
  801. }
  802. /**
  803. * batadv_softif_init_early - early stage initialization of soft interface
  804. * @dev: registered network device to modify
  805. */
  806. static void batadv_softif_init_early(struct net_device *dev)
  807. {
  808. struct batadv_priv *priv = netdev_priv(dev);
  809. ether_setup(dev);
  810. dev->netdev_ops = &batadv_netdev_ops;
  811. dev->destructor = batadv_softif_free;
  812. dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_NETNS_LOCAL;
  813. dev->priv_flags |= IFF_NO_QUEUE;
  814. /* can't call min_mtu, because the needed variables
  815. * have not been initialized yet
  816. */
  817. dev->mtu = ETH_DATA_LEN;
  818. /* generate random address */
  819. eth_hw_addr_random(dev);
  820. dev->ethtool_ops = &batadv_ethtool_ops;
  821. memset(priv, 0, sizeof(*priv));
  822. }
  823. struct net_device *batadv_softif_create(struct net *net, const char *name)
  824. {
  825. struct net_device *soft_iface;
  826. int ret;
  827. soft_iface = alloc_netdev(sizeof(struct batadv_priv), name,
  828. NET_NAME_UNKNOWN, batadv_softif_init_early);
  829. if (!soft_iface)
  830. return NULL;
  831. dev_net_set(soft_iface, net);
  832. soft_iface->rtnl_link_ops = &batadv_link_ops;
  833. ret = register_netdevice(soft_iface);
  834. if (ret < 0) {
  835. pr_err("Unable to register the batman interface '%s': %i\n",
  836. name, ret);
  837. free_netdev(soft_iface);
  838. return NULL;
  839. }
  840. return soft_iface;
  841. }
  842. /**
  843. * batadv_softif_destroy_sysfs - deletion of batadv_soft_interface via sysfs
  844. * @soft_iface: the to-be-removed batman-adv interface
  845. */
  846. void batadv_softif_destroy_sysfs(struct net_device *soft_iface)
  847. {
  848. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  849. struct batadv_softif_vlan *vlan;
  850. ASSERT_RTNL();
  851. /* destroy the "untagged" VLAN */
  852. vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
  853. if (vlan) {
  854. batadv_softif_destroy_vlan(bat_priv, vlan);
  855. batadv_softif_vlan_put(vlan);
  856. }
  857. batadv_sysfs_del_meshif(soft_iface);
  858. unregister_netdevice(soft_iface);
  859. }
  860. /**
  861. * batadv_softif_destroy_netlink - deletion of batadv_soft_interface via netlink
  862. * @soft_iface: the to-be-removed batman-adv interface
  863. * @head: list pointer
  864. */
  865. static void batadv_softif_destroy_netlink(struct net_device *soft_iface,
  866. struct list_head *head)
  867. {
  868. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  869. struct batadv_hard_iface *hard_iface;
  870. struct batadv_softif_vlan *vlan;
  871. list_for_each_entry(hard_iface, &batadv_hardif_list, list) {
  872. if (hard_iface->soft_iface == soft_iface)
  873. batadv_hardif_disable_interface(hard_iface,
  874. BATADV_IF_CLEANUP_KEEP);
  875. }
  876. /* destroy the "untagged" VLAN */
  877. vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
  878. if (vlan) {
  879. batadv_softif_destroy_vlan(bat_priv, vlan);
  880. batadv_softif_vlan_put(vlan);
  881. }
  882. batadv_sysfs_del_meshif(soft_iface);
  883. unregister_netdevice_queue(soft_iface, head);
  884. }
  885. bool batadv_softif_is_valid(const struct net_device *net_dev)
  886. {
  887. if (net_dev->netdev_ops->ndo_start_xmit == batadv_interface_tx)
  888. return true;
  889. return false;
  890. }
  891. struct rtnl_link_ops batadv_link_ops __read_mostly = {
  892. .kind = "batadv",
  893. .priv_size = sizeof(struct batadv_priv),
  894. .setup = batadv_softif_init_early,
  895. .dellink = batadv_softif_destroy_netlink,
  896. };
  897. /* ethtool */
  898. static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  899. {
  900. cmd->supported = 0;
  901. cmd->advertising = 0;
  902. ethtool_cmd_speed_set(cmd, SPEED_10);
  903. cmd->duplex = DUPLEX_FULL;
  904. cmd->port = PORT_TP;
  905. cmd->phy_address = 0;
  906. cmd->transceiver = XCVR_INTERNAL;
  907. cmd->autoneg = AUTONEG_DISABLE;
  908. cmd->maxtxpkt = 0;
  909. cmd->maxrxpkt = 0;
  910. return 0;
  911. }
  912. static void batadv_get_drvinfo(struct net_device *dev,
  913. struct ethtool_drvinfo *info)
  914. {
  915. strlcpy(info->driver, "B.A.T.M.A.N. advanced", sizeof(info->driver));
  916. strlcpy(info->version, BATADV_SOURCE_VERSION, sizeof(info->version));
  917. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  918. strlcpy(info->bus_info, "batman", sizeof(info->bus_info));
  919. }
  920. static u32 batadv_get_msglevel(struct net_device *dev)
  921. {
  922. return -EOPNOTSUPP;
  923. }
  924. static void batadv_set_msglevel(struct net_device *dev, u32 value)
  925. {
  926. }
  927. static u32 batadv_get_link(struct net_device *dev)
  928. {
  929. return 1;
  930. }
  931. /* Inspired by drivers/net/ethernet/dlink/sundance.c:1702
  932. * Declare each description string in struct.name[] to get fixed sized buffer
  933. * and compile time checking for strings longer than ETH_GSTRING_LEN.
  934. */
  935. static const struct {
  936. const char name[ETH_GSTRING_LEN];
  937. } batadv_counters_strings[] = {
  938. { "tx" },
  939. { "tx_bytes" },
  940. { "tx_dropped" },
  941. { "rx" },
  942. { "rx_bytes" },
  943. { "forward" },
  944. { "forward_bytes" },
  945. { "mgmt_tx" },
  946. { "mgmt_tx_bytes" },
  947. { "mgmt_rx" },
  948. { "mgmt_rx_bytes" },
  949. { "frag_tx" },
  950. { "frag_tx_bytes" },
  951. { "frag_rx" },
  952. { "frag_rx_bytes" },
  953. { "frag_fwd" },
  954. { "frag_fwd_bytes" },
  955. { "tt_request_tx" },
  956. { "tt_request_rx" },
  957. { "tt_response_tx" },
  958. { "tt_response_rx" },
  959. { "tt_roam_adv_tx" },
  960. { "tt_roam_adv_rx" },
  961. #ifdef CONFIG_BATMAN_ADV_DAT
  962. { "dat_get_tx" },
  963. { "dat_get_rx" },
  964. { "dat_put_tx" },
  965. { "dat_put_rx" },
  966. { "dat_cached_reply_tx" },
  967. #endif
  968. #ifdef CONFIG_BATMAN_ADV_NC
  969. { "nc_code" },
  970. { "nc_code_bytes" },
  971. { "nc_recode" },
  972. { "nc_recode_bytes" },
  973. { "nc_buffer" },
  974. { "nc_decode" },
  975. { "nc_decode_bytes" },
  976. { "nc_decode_failed" },
  977. { "nc_sniffed" },
  978. #endif
  979. };
  980. static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  981. {
  982. if (stringset == ETH_SS_STATS)
  983. memcpy(data, batadv_counters_strings,
  984. sizeof(batadv_counters_strings));
  985. }
  986. static void batadv_get_ethtool_stats(struct net_device *dev,
  987. struct ethtool_stats *stats, u64 *data)
  988. {
  989. struct batadv_priv *bat_priv = netdev_priv(dev);
  990. int i;
  991. for (i = 0; i < BATADV_CNT_NUM; i++)
  992. data[i] = batadv_sum_counter(bat_priv, i);
  993. }
  994. static int batadv_get_sset_count(struct net_device *dev, int stringset)
  995. {
  996. if (stringset == ETH_SS_STATS)
  997. return BATADV_CNT_NUM;
  998. return -EOPNOTSUPP;
  999. }