soft-interface.c 32 KB

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