soft-interface.c 30 KB

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