soft-interface.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /* Copyright (C) 2007-2012 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, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "soft-interface.h"
  21. #include "hard-interface.h"
  22. #include "routing.h"
  23. #include "send.h"
  24. #include "debugfs.h"
  25. #include "translation-table.h"
  26. #include "hash.h"
  27. #include "gateway_common.h"
  28. #include "gateway_client.h"
  29. #include "sysfs.h"
  30. #include "originator.h"
  31. #include <linux/slab.h>
  32. #include <linux/ethtool.h>
  33. #include <linux/etherdevice.h>
  34. #include <linux/if_vlan.h>
  35. #include "unicast.h"
  36. #include "bridge_loop_avoidance.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. if (!is_valid_ether_addr(addr->sa_data))
  99. return -EADDRNOTAVAIL;
  100. /* only modify transtable if it has been initialized before */
  101. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE) {
  102. batadv_tt_local_remove(bat_priv, dev->dev_addr,
  103. "mac address changed", false);
  104. batadv_tt_local_add(dev, addr->sa_data, BATADV_NULL_IFINDEX);
  105. }
  106. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  107. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  108. return 0;
  109. }
  110. static int batadv_interface_change_mtu(struct net_device *dev, int new_mtu)
  111. {
  112. /* check ranges */
  113. if ((new_mtu < 68) || (new_mtu > batadv_hardif_min_mtu(dev)))
  114. return -EINVAL;
  115. dev->mtu = new_mtu;
  116. return 0;
  117. }
  118. static int batadv_interface_tx(struct sk_buff *skb,
  119. struct net_device *soft_iface)
  120. {
  121. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  122. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  123. struct batadv_hard_iface *primary_if = NULL;
  124. struct batadv_bcast_packet *bcast_packet;
  125. struct vlan_ethhdr *vhdr;
  126. __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN);
  127. static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00,
  128. 0x00};
  129. unsigned int header_len = 0;
  130. int data_len = skb->len, ret;
  131. short vid __maybe_unused = -1;
  132. bool do_bcast = false;
  133. uint32_t seqno;
  134. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  135. goto dropped;
  136. soft_iface->trans_start = jiffies;
  137. switch (ntohs(ethhdr->h_proto)) {
  138. case ETH_P_8021Q:
  139. vhdr = (struct vlan_ethhdr *)skb->data;
  140. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  141. if (vhdr->h_vlan_encapsulated_proto != ethertype)
  142. break;
  143. /* fall through */
  144. case BATADV_ETH_P_BATMAN:
  145. goto dropped;
  146. }
  147. if (batadv_bla_tx(bat_priv, skb, vid))
  148. goto dropped;
  149. /* Register the client MAC in the transtable */
  150. batadv_tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
  151. /* don't accept stp packets. STP does not help in meshes.
  152. * better use the bridge loop avoidance ...
  153. */
  154. if (batadv_compare_eth(ethhdr->h_dest, stp_addr))
  155. goto dropped;
  156. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  157. do_bcast = true;
  158. switch (atomic_read(&bat_priv->gw_mode)) {
  159. case BATADV_GW_MODE_SERVER:
  160. /* gateway servers should not send dhcp
  161. * requests into the mesh
  162. */
  163. ret = batadv_gw_is_dhcp_target(skb, &header_len);
  164. if (ret)
  165. goto dropped;
  166. break;
  167. case BATADV_GW_MODE_CLIENT:
  168. /* gateway clients should send dhcp requests
  169. * via unicast to their gateway
  170. */
  171. ret = batadv_gw_is_dhcp_target(skb, &header_len);
  172. if (ret)
  173. do_bcast = false;
  174. break;
  175. case BATADV_GW_MODE_OFF:
  176. default:
  177. break;
  178. }
  179. }
  180. /* ethernet packet should be broadcasted */
  181. if (do_bcast) {
  182. primary_if = batadv_primary_if_get_selected(bat_priv);
  183. if (!primary_if)
  184. goto dropped;
  185. if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
  186. goto dropped;
  187. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  188. bcast_packet->header.version = BATADV_COMPAT_VERSION;
  189. bcast_packet->header.ttl = BATADV_TTL;
  190. /* batman packet type: broadcast */
  191. bcast_packet->header.packet_type = BATADV_BCAST;
  192. bcast_packet->reserved = 0;
  193. /* hw address of first interface is the orig mac because only
  194. * this mac is known throughout the mesh
  195. */
  196. memcpy(bcast_packet->orig,
  197. primary_if->net_dev->dev_addr, ETH_ALEN);
  198. /* set broadcast sequence number */
  199. seqno = atomic_inc_return(&bat_priv->bcast_seqno);
  200. bcast_packet->seqno = htonl(seqno);
  201. batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
  202. /* a copy is stored in the bcast list, therefore removing
  203. * the original skb.
  204. */
  205. kfree_skb(skb);
  206. /* unicast packet */
  207. } else {
  208. if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_OFF) {
  209. ret = batadv_gw_out_of_range(bat_priv, skb, ethhdr);
  210. if (ret)
  211. goto dropped;
  212. }
  213. ret = batadv_unicast_send_skb(skb, bat_priv);
  214. if (ret != 0)
  215. goto dropped_freed;
  216. }
  217. batadv_inc_counter(bat_priv, BATADV_CNT_TX);
  218. batadv_add_counter(bat_priv, BATADV_CNT_TX_BYTES, data_len);
  219. goto end;
  220. dropped:
  221. kfree_skb(skb);
  222. dropped_freed:
  223. batadv_inc_counter(bat_priv, BATADV_CNT_TX_DROPPED);
  224. end:
  225. if (primary_if)
  226. batadv_hardif_free_ref(primary_if);
  227. return NETDEV_TX_OK;
  228. }
  229. void batadv_interface_rx(struct net_device *soft_iface,
  230. struct sk_buff *skb, struct batadv_hard_iface *recv_if,
  231. int hdr_size, struct batadv_orig_node *orig_node)
  232. {
  233. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  234. struct ethhdr *ethhdr;
  235. struct vlan_ethhdr *vhdr;
  236. struct batadv_header *batadv_header = (struct batadv_header *)skb->data;
  237. short vid __maybe_unused = -1;
  238. __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN);
  239. bool is_bcast;
  240. is_bcast = (batadv_header->packet_type == BATADV_BCAST);
  241. /* check if enough space is available for pulling, and pull */
  242. if (!pskb_may_pull(skb, hdr_size))
  243. goto dropped;
  244. skb_pull_rcsum(skb, hdr_size);
  245. skb_reset_mac_header(skb);
  246. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  247. switch (ntohs(ethhdr->h_proto)) {
  248. case ETH_P_8021Q:
  249. vhdr = (struct vlan_ethhdr *)skb->data;
  250. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  251. if (vhdr->h_vlan_encapsulated_proto != ethertype)
  252. break;
  253. /* fall through */
  254. case BATADV_ETH_P_BATMAN:
  255. goto dropped;
  256. }
  257. /* skb->dev & skb->pkt_type are set here */
  258. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  259. goto dropped;
  260. skb->protocol = eth_type_trans(skb, soft_iface);
  261. /* should not be necessary anymore as we use skb_pull_rcsum()
  262. * TODO: please verify this and remove this TODO
  263. * -- Dec 21st 2009, Simon Wunderlich
  264. */
  265. /* skb->ip_summed = CHECKSUM_UNNECESSARY; */
  266. batadv_inc_counter(bat_priv, BATADV_CNT_RX);
  267. batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
  268. skb->len + ETH_HLEN);
  269. soft_iface->last_rx = jiffies;
  270. if (orig_node)
  271. batadv_tt_add_temporary_global_entry(bat_priv, orig_node,
  272. ethhdr->h_source);
  273. if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
  274. goto dropped;
  275. /* Let the bridge loop avoidance check the packet. If will
  276. * not handle it, we can safely push it up.
  277. */
  278. if (batadv_bla_rx(bat_priv, skb, vid, is_bcast))
  279. goto out;
  280. netif_rx(skb);
  281. goto out;
  282. dropped:
  283. kfree_skb(skb);
  284. out:
  285. return;
  286. }
  287. static const struct net_device_ops batadv_netdev_ops = {
  288. .ndo_open = batadv_interface_open,
  289. .ndo_stop = batadv_interface_release,
  290. .ndo_get_stats = batadv_interface_stats,
  291. .ndo_set_mac_address = batadv_interface_set_mac_addr,
  292. .ndo_change_mtu = batadv_interface_change_mtu,
  293. .ndo_start_xmit = batadv_interface_tx,
  294. .ndo_validate_addr = eth_validate_addr
  295. };
  296. static void batadv_interface_setup(struct net_device *dev)
  297. {
  298. struct batadv_priv *priv = netdev_priv(dev);
  299. ether_setup(dev);
  300. dev->netdev_ops = &batadv_netdev_ops;
  301. dev->destructor = free_netdev;
  302. dev->tx_queue_len = 0;
  303. /* can't call min_mtu, because the needed variables
  304. * have not been initialized yet
  305. */
  306. dev->mtu = ETH_DATA_LEN;
  307. /* reserve more space in the skbuff for our header */
  308. dev->hard_header_len = BATADV_HEADER_LEN;
  309. /* generate random address */
  310. eth_hw_addr_random(dev);
  311. SET_ETHTOOL_OPS(dev, &batadv_ethtool_ops);
  312. memset(priv, 0, sizeof(*priv));
  313. }
  314. struct net_device *batadv_softif_create(const char *name)
  315. {
  316. struct net_device *soft_iface;
  317. struct batadv_priv *bat_priv;
  318. int ret;
  319. size_t cnt_len = sizeof(uint64_t) * BATADV_CNT_NUM;
  320. soft_iface = alloc_netdev(sizeof(*bat_priv), name,
  321. batadv_interface_setup);
  322. if (!soft_iface)
  323. goto out;
  324. bat_priv = netdev_priv(soft_iface);
  325. /* batadv_interface_stats() needs to be available as soon as
  326. * register_netdevice() has been called
  327. */
  328. bat_priv->bat_counters = __alloc_percpu(cnt_len, __alignof__(uint64_t));
  329. if (!bat_priv->bat_counters)
  330. goto free_soft_iface;
  331. ret = register_netdevice(soft_iface);
  332. if (ret < 0) {
  333. pr_err("Unable to register the batman interface '%s': %i\n",
  334. name, ret);
  335. goto free_bat_counters;
  336. }
  337. atomic_set(&bat_priv->aggregated_ogms, 1);
  338. atomic_set(&bat_priv->bonding, 0);
  339. atomic_set(&bat_priv->bridge_loop_avoidance, 0);
  340. atomic_set(&bat_priv->ap_isolation, 0);
  341. atomic_set(&bat_priv->vis_mode, BATADV_VIS_TYPE_CLIENT_UPDATE);
  342. atomic_set(&bat_priv->gw_mode, BATADV_GW_MODE_OFF);
  343. atomic_set(&bat_priv->gw_sel_class, 20);
  344. atomic_set(&bat_priv->gw_bandwidth, 41);
  345. atomic_set(&bat_priv->orig_interval, 1000);
  346. atomic_set(&bat_priv->hop_penalty, 30);
  347. atomic_set(&bat_priv->log_level, 0);
  348. atomic_set(&bat_priv->fragmentation, 1);
  349. atomic_set(&bat_priv->bcast_queue_left, BATADV_BCAST_QUEUE_LEN);
  350. atomic_set(&bat_priv->batman_queue_left, BATADV_BATMAN_QUEUE_LEN);
  351. atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
  352. atomic_set(&bat_priv->bcast_seqno, 1);
  353. atomic_set(&bat_priv->tt.vn, 0);
  354. atomic_set(&bat_priv->tt.local_changes, 0);
  355. atomic_set(&bat_priv->tt.ogm_append_cnt, 0);
  356. #ifdef CONFIG_BATMAN_ADV_BLA
  357. atomic_set(&bat_priv->bla.num_requests, 0);
  358. #endif
  359. bat_priv->tt.last_changeset = NULL;
  360. bat_priv->tt.last_changeset_len = 0;
  361. bat_priv->tt.poss_change = false;
  362. bat_priv->primary_if = NULL;
  363. bat_priv->num_ifaces = 0;
  364. ret = batadv_algo_select(bat_priv, batadv_routing_algo);
  365. if (ret < 0)
  366. goto unreg_soft_iface;
  367. ret = batadv_sysfs_add_meshif(soft_iface);
  368. if (ret < 0)
  369. goto unreg_soft_iface;
  370. ret = batadv_debugfs_add_meshif(soft_iface);
  371. if (ret < 0)
  372. goto unreg_sysfs;
  373. ret = batadv_mesh_init(soft_iface);
  374. if (ret < 0)
  375. goto unreg_debugfs;
  376. return soft_iface;
  377. unreg_debugfs:
  378. batadv_debugfs_del_meshif(soft_iface);
  379. unreg_sysfs:
  380. batadv_sysfs_del_meshif(soft_iface);
  381. unreg_soft_iface:
  382. free_percpu(bat_priv->bat_counters);
  383. unregister_netdevice(soft_iface);
  384. return NULL;
  385. free_bat_counters:
  386. free_percpu(bat_priv->bat_counters);
  387. free_soft_iface:
  388. free_netdev(soft_iface);
  389. out:
  390. return NULL;
  391. }
  392. void batadv_softif_destroy(struct net_device *soft_iface)
  393. {
  394. batadv_debugfs_del_meshif(soft_iface);
  395. batadv_sysfs_del_meshif(soft_iface);
  396. batadv_mesh_free(soft_iface);
  397. unregister_netdevice(soft_iface);
  398. }
  399. int batadv_softif_is_valid(const struct net_device *net_dev)
  400. {
  401. if (net_dev->netdev_ops->ndo_start_xmit == batadv_interface_tx)
  402. return 1;
  403. return 0;
  404. }
  405. /* ethtool */
  406. static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  407. {
  408. cmd->supported = 0;
  409. cmd->advertising = 0;
  410. ethtool_cmd_speed_set(cmd, SPEED_10);
  411. cmd->duplex = DUPLEX_FULL;
  412. cmd->port = PORT_TP;
  413. cmd->phy_address = 0;
  414. cmd->transceiver = XCVR_INTERNAL;
  415. cmd->autoneg = AUTONEG_DISABLE;
  416. cmd->maxtxpkt = 0;
  417. cmd->maxrxpkt = 0;
  418. return 0;
  419. }
  420. static void batadv_get_drvinfo(struct net_device *dev,
  421. struct ethtool_drvinfo *info)
  422. {
  423. strcpy(info->driver, "B.A.T.M.A.N. advanced");
  424. strcpy(info->version, BATADV_SOURCE_VERSION);
  425. strcpy(info->fw_version, "N/A");
  426. strcpy(info->bus_info, "batman");
  427. }
  428. static u32 batadv_get_msglevel(struct net_device *dev)
  429. {
  430. return -EOPNOTSUPP;
  431. }
  432. static void batadv_set_msglevel(struct net_device *dev, u32 value)
  433. {
  434. }
  435. static u32 batadv_get_link(struct net_device *dev)
  436. {
  437. return 1;
  438. }
  439. /* Inspired by drivers/net/ethernet/dlink/sundance.c:1702
  440. * Declare each description string in struct.name[] to get fixed sized buffer
  441. * and compile time checking for strings longer than ETH_GSTRING_LEN.
  442. */
  443. static const struct {
  444. const char name[ETH_GSTRING_LEN];
  445. } batadv_counters_strings[] = {
  446. { "tx" },
  447. { "tx_bytes" },
  448. { "tx_dropped" },
  449. { "rx" },
  450. { "rx_bytes" },
  451. { "forward" },
  452. { "forward_bytes" },
  453. { "mgmt_tx" },
  454. { "mgmt_tx_bytes" },
  455. { "mgmt_rx" },
  456. { "mgmt_rx_bytes" },
  457. { "tt_request_tx" },
  458. { "tt_request_rx" },
  459. { "tt_response_tx" },
  460. { "tt_response_rx" },
  461. { "tt_roam_adv_tx" },
  462. { "tt_roam_adv_rx" },
  463. };
  464. static void batadv_get_strings(struct net_device *dev, uint32_t stringset,
  465. uint8_t *data)
  466. {
  467. if (stringset == ETH_SS_STATS)
  468. memcpy(data, batadv_counters_strings,
  469. sizeof(batadv_counters_strings));
  470. }
  471. static void batadv_get_ethtool_stats(struct net_device *dev,
  472. struct ethtool_stats *stats,
  473. uint64_t *data)
  474. {
  475. struct batadv_priv *bat_priv = netdev_priv(dev);
  476. int i;
  477. for (i = 0; i < BATADV_CNT_NUM; i++)
  478. data[i] = batadv_sum_counter(bat_priv, i);
  479. }
  480. static int batadv_get_sset_count(struct net_device *dev, int stringset)
  481. {
  482. if (stringset == ETH_SS_STATS)
  483. return BATADV_CNT_NUM;
  484. return -EOPNOTSUPP;
  485. }