soft-interface.c 14 KB

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