soft-interface.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "soft-interface.h"
  23. #include "hard-interface.h"
  24. #include "routing.h"
  25. #include "send.h"
  26. #include "bat_debugfs.h"
  27. #include "translation-table.h"
  28. #include "hash.h"
  29. #include "gateway_common.h"
  30. #include "gateway_client.h"
  31. #include "bat_sysfs.h"
  32. #include "originator.h"
  33. #include <linux/slab.h>
  34. #include <linux/ethtool.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/if_vlan.h>
  37. #include "unicast.h"
  38. #include "bridge_loop_avoidance.h"
  39. static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
  40. static void bat_get_drvinfo(struct net_device *dev,
  41. struct ethtool_drvinfo *info);
  42. static u32 bat_get_msglevel(struct net_device *dev);
  43. static void bat_set_msglevel(struct net_device *dev, u32 value);
  44. static u32 bat_get_link(struct net_device *dev);
  45. static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data);
  46. static void batadv_get_ethtool_stats(struct net_device *dev,
  47. struct ethtool_stats *stats, u64 *data);
  48. static int batadv_get_sset_count(struct net_device *dev, int stringset);
  49. static const struct ethtool_ops bat_ethtool_ops = {
  50. .get_settings = bat_get_settings,
  51. .get_drvinfo = bat_get_drvinfo,
  52. .get_msglevel = bat_get_msglevel,
  53. .set_msglevel = bat_set_msglevel,
  54. .get_link = bat_get_link,
  55. .get_strings = batadv_get_strings,
  56. .get_ethtool_stats = batadv_get_ethtool_stats,
  57. .get_sset_count = batadv_get_sset_count,
  58. };
  59. int my_skb_head_push(struct sk_buff *skb, unsigned int len)
  60. {
  61. int result;
  62. /**
  63. * TODO: We must check if we can release all references to non-payload
  64. * data using skb_header_release in our skbs to allow skb_cow_header to
  65. * work optimally. This means that those skbs are not allowed to read
  66. * or write any data which is before the current position of skb->data
  67. * after that call and thus allow other skbs with the same data buffer
  68. * to write freely in that area.
  69. */
  70. result = skb_cow_head(skb, len);
  71. if (result < 0)
  72. return result;
  73. skb_push(skb, len);
  74. return 0;
  75. }
  76. static int interface_open(struct net_device *dev)
  77. {
  78. netif_start_queue(dev);
  79. return 0;
  80. }
  81. static int interface_release(struct net_device *dev)
  82. {
  83. netif_stop_queue(dev);
  84. return 0;
  85. }
  86. static struct net_device_stats *interface_stats(struct net_device *dev)
  87. {
  88. struct bat_priv *bat_priv = netdev_priv(dev);
  89. return &bat_priv->stats;
  90. }
  91. static int interface_set_mac_addr(struct net_device *dev, void *p)
  92. {
  93. struct bat_priv *bat_priv = netdev_priv(dev);
  94. struct sockaddr *addr = p;
  95. if (!is_valid_ether_addr(addr->sa_data))
  96. return -EADDRNOTAVAIL;
  97. /* only modify transtable if it has been initialized before */
  98. if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
  99. tt_local_remove(bat_priv, dev->dev_addr,
  100. "mac address changed", false);
  101. tt_local_add(dev, addr->sa_data, NULL_IFINDEX);
  102. }
  103. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  104. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  105. return 0;
  106. }
  107. static int interface_change_mtu(struct net_device *dev, int new_mtu)
  108. {
  109. /* check ranges */
  110. if ((new_mtu < 68) || (new_mtu > batadv_hardif_min_mtu(dev)))
  111. return -EINVAL;
  112. dev->mtu = new_mtu;
  113. return 0;
  114. }
  115. static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
  116. {
  117. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  118. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  119. struct hard_iface *primary_if = NULL;
  120. struct bcast_packet *bcast_packet;
  121. struct vlan_ethhdr *vhdr;
  122. static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00,
  123. 0x00};
  124. unsigned int header_len = 0;
  125. int data_len = skb->len, ret;
  126. short vid __maybe_unused = -1;
  127. bool do_bcast = false;
  128. if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
  129. goto dropped;
  130. soft_iface->trans_start = jiffies;
  131. switch (ntohs(ethhdr->h_proto)) {
  132. case ETH_P_8021Q:
  133. vhdr = (struct vlan_ethhdr *)skb->data;
  134. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  135. if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
  136. break;
  137. /* fall through */
  138. case ETH_P_BATMAN:
  139. goto dropped;
  140. }
  141. if (batadv_bla_tx(bat_priv, skb, vid))
  142. goto dropped;
  143. /* Register the client MAC in the transtable */
  144. tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
  145. /* don't accept stp packets. STP does not help in meshes.
  146. * better use the bridge loop avoidance ...
  147. */
  148. if (compare_eth(ethhdr->h_dest, stp_addr))
  149. goto dropped;
  150. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  151. do_bcast = true;
  152. switch (atomic_read(&bat_priv->gw_mode)) {
  153. case GW_MODE_SERVER:
  154. /* gateway servers should not send dhcp
  155. * requests into the mesh */
  156. ret = batadv_gw_is_dhcp_target(skb, &header_len);
  157. if (ret)
  158. goto dropped;
  159. break;
  160. case GW_MODE_CLIENT:
  161. /* gateway clients should send dhcp requests
  162. * via unicast to their gateway */
  163. ret = batadv_gw_is_dhcp_target(skb, &header_len);
  164. if (ret)
  165. do_bcast = false;
  166. break;
  167. case GW_MODE_OFF:
  168. default:
  169. break;
  170. }
  171. }
  172. /* ethernet packet should be broadcasted */
  173. if (do_bcast) {
  174. primary_if = primary_if_get_selected(bat_priv);
  175. if (!primary_if)
  176. goto dropped;
  177. if (my_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
  178. goto dropped;
  179. bcast_packet = (struct bcast_packet *)skb->data;
  180. bcast_packet->header.version = COMPAT_VERSION;
  181. bcast_packet->header.ttl = TTL;
  182. /* batman packet type: broadcast */
  183. bcast_packet->header.packet_type = BAT_BCAST;
  184. /* hw address of first interface is the orig mac because only
  185. * this mac is known throughout the mesh */
  186. memcpy(bcast_packet->orig,
  187. primary_if->net_dev->dev_addr, ETH_ALEN);
  188. /* set broadcast sequence number */
  189. bcast_packet->seqno =
  190. htonl(atomic_inc_return(&bat_priv->bcast_seqno));
  191. add_bcast_packet_to_list(bat_priv, skb, 1);
  192. /* a copy is stored in the bcast list, therefore removing
  193. * the original skb. */
  194. kfree_skb(skb);
  195. /* unicast packet */
  196. } else {
  197. if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) {
  198. ret = batadv_gw_out_of_range(bat_priv, skb, ethhdr);
  199. if (ret)
  200. goto dropped;
  201. }
  202. ret = unicast_send_skb(skb, bat_priv);
  203. if (ret != 0)
  204. goto dropped_freed;
  205. }
  206. bat_priv->stats.tx_packets++;
  207. bat_priv->stats.tx_bytes += data_len;
  208. goto end;
  209. dropped:
  210. kfree_skb(skb);
  211. dropped_freed:
  212. bat_priv->stats.tx_dropped++;
  213. end:
  214. if (primary_if)
  215. hardif_free_ref(primary_if);
  216. return NETDEV_TX_OK;
  217. }
  218. void interface_rx(struct net_device *soft_iface,
  219. struct sk_buff *skb, struct hard_iface *recv_if,
  220. int hdr_size)
  221. {
  222. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  223. struct ethhdr *ethhdr;
  224. struct vlan_ethhdr *vhdr;
  225. short vid __maybe_unused = -1;
  226. /* check if enough space is available for pulling, and pull */
  227. if (!pskb_may_pull(skb, hdr_size))
  228. goto dropped;
  229. skb_pull_rcsum(skb, hdr_size);
  230. skb_reset_mac_header(skb);
  231. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  232. switch (ntohs(ethhdr->h_proto)) {
  233. case ETH_P_8021Q:
  234. vhdr = (struct vlan_ethhdr *)skb->data;
  235. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  236. if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
  237. break;
  238. /* fall through */
  239. case ETH_P_BATMAN:
  240. goto dropped;
  241. }
  242. /* skb->dev & skb->pkt_type are set here */
  243. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  244. goto dropped;
  245. skb->protocol = eth_type_trans(skb, soft_iface);
  246. /* should not be necessary anymore as we use skb_pull_rcsum()
  247. * TODO: please verify this and remove this TODO
  248. * -- Dec 21st 2009, Simon Wunderlich */
  249. /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
  250. bat_priv->stats.rx_packets++;
  251. bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
  252. soft_iface->last_rx = jiffies;
  253. if (is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
  254. goto dropped;
  255. /* Let the bridge loop avoidance check the packet. If will
  256. * not handle it, we can safely push it up.
  257. */
  258. if (batadv_bla_rx(bat_priv, skb, vid))
  259. goto out;
  260. netif_rx(skb);
  261. goto out;
  262. dropped:
  263. kfree_skb(skb);
  264. out:
  265. return;
  266. }
  267. static const struct net_device_ops bat_netdev_ops = {
  268. .ndo_open = interface_open,
  269. .ndo_stop = interface_release,
  270. .ndo_get_stats = interface_stats,
  271. .ndo_set_mac_address = interface_set_mac_addr,
  272. .ndo_change_mtu = interface_change_mtu,
  273. .ndo_start_xmit = interface_tx,
  274. .ndo_validate_addr = eth_validate_addr
  275. };
  276. static void interface_setup(struct net_device *dev)
  277. {
  278. struct bat_priv *priv = netdev_priv(dev);
  279. ether_setup(dev);
  280. dev->netdev_ops = &bat_netdev_ops;
  281. dev->destructor = free_netdev;
  282. dev->tx_queue_len = 0;
  283. /**
  284. * can't call min_mtu, because the needed variables
  285. * have not been initialized yet
  286. */
  287. dev->mtu = ETH_DATA_LEN;
  288. /* reserve more space in the skbuff for our header */
  289. dev->hard_header_len = BAT_HEADER_LEN;
  290. /* generate random address */
  291. eth_hw_addr_random(dev);
  292. SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
  293. memset(priv, 0, sizeof(*priv));
  294. }
  295. struct net_device *softif_create(const char *name)
  296. {
  297. struct net_device *soft_iface;
  298. struct bat_priv *bat_priv;
  299. int ret;
  300. soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup);
  301. if (!soft_iface)
  302. goto out;
  303. ret = register_netdevice(soft_iface);
  304. if (ret < 0) {
  305. pr_err("Unable to register the batman interface '%s': %i\n",
  306. name, ret);
  307. goto free_soft_iface;
  308. }
  309. bat_priv = netdev_priv(soft_iface);
  310. atomic_set(&bat_priv->aggregated_ogms, 1);
  311. atomic_set(&bat_priv->bonding, 0);
  312. atomic_set(&bat_priv->bridge_loop_avoidance, 0);
  313. atomic_set(&bat_priv->ap_isolation, 0);
  314. atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
  315. atomic_set(&bat_priv->gw_mode, GW_MODE_OFF);
  316. atomic_set(&bat_priv->gw_sel_class, 20);
  317. atomic_set(&bat_priv->gw_bandwidth, 41);
  318. atomic_set(&bat_priv->orig_interval, 1000);
  319. atomic_set(&bat_priv->hop_penalty, 30);
  320. atomic_set(&bat_priv->log_level, 0);
  321. atomic_set(&bat_priv->fragmentation, 1);
  322. atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
  323. atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
  324. atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
  325. atomic_set(&bat_priv->bcast_seqno, 1);
  326. atomic_set(&bat_priv->ttvn, 0);
  327. atomic_set(&bat_priv->tt_local_changes, 0);
  328. atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
  329. atomic_set(&bat_priv->bla_num_requests, 0);
  330. bat_priv->tt_buff = NULL;
  331. bat_priv->tt_buff_len = 0;
  332. bat_priv->tt_poss_change = false;
  333. bat_priv->primary_if = NULL;
  334. bat_priv->num_ifaces = 0;
  335. bat_priv->bat_counters = __alloc_percpu(sizeof(uint64_t) * BAT_CNT_NUM,
  336. __alignof__(uint64_t));
  337. if (!bat_priv->bat_counters)
  338. goto unreg_soft_iface;
  339. ret = bat_algo_select(bat_priv, bat_routing_algo);
  340. if (ret < 0)
  341. goto free_bat_counters;
  342. ret = batadv_sysfs_add_meshif(soft_iface);
  343. if (ret < 0)
  344. goto free_bat_counters;
  345. ret = batadv_debugfs_add_meshif(soft_iface);
  346. if (ret < 0)
  347. goto unreg_sysfs;
  348. ret = mesh_init(soft_iface);
  349. if (ret < 0)
  350. goto unreg_debugfs;
  351. return soft_iface;
  352. unreg_debugfs:
  353. batadv_debugfs_del_meshif(soft_iface);
  354. unreg_sysfs:
  355. batadv_sysfs_del_meshif(soft_iface);
  356. free_bat_counters:
  357. free_percpu(bat_priv->bat_counters);
  358. unreg_soft_iface:
  359. unregister_netdevice(soft_iface);
  360. return NULL;
  361. free_soft_iface:
  362. free_netdev(soft_iface);
  363. out:
  364. return NULL;
  365. }
  366. void softif_destroy(struct net_device *soft_iface)
  367. {
  368. batadv_debugfs_del_meshif(soft_iface);
  369. batadv_sysfs_del_meshif(soft_iface);
  370. mesh_free(soft_iface);
  371. unregister_netdevice(soft_iface);
  372. }
  373. int softif_is_valid(const struct net_device *net_dev)
  374. {
  375. if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
  376. return 1;
  377. return 0;
  378. }
  379. /* ethtool */
  380. static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  381. {
  382. cmd->supported = 0;
  383. cmd->advertising = 0;
  384. ethtool_cmd_speed_set(cmd, SPEED_10);
  385. cmd->duplex = DUPLEX_FULL;
  386. cmd->port = PORT_TP;
  387. cmd->phy_address = 0;
  388. cmd->transceiver = XCVR_INTERNAL;
  389. cmd->autoneg = AUTONEG_DISABLE;
  390. cmd->maxtxpkt = 0;
  391. cmd->maxrxpkt = 0;
  392. return 0;
  393. }
  394. static void bat_get_drvinfo(struct net_device *dev,
  395. struct ethtool_drvinfo *info)
  396. {
  397. strcpy(info->driver, "B.A.T.M.A.N. advanced");
  398. strcpy(info->version, SOURCE_VERSION);
  399. strcpy(info->fw_version, "N/A");
  400. strcpy(info->bus_info, "batman");
  401. }
  402. static u32 bat_get_msglevel(struct net_device *dev)
  403. {
  404. return -EOPNOTSUPP;
  405. }
  406. static void bat_set_msglevel(struct net_device *dev, u32 value)
  407. {
  408. }
  409. static u32 bat_get_link(struct net_device *dev)
  410. {
  411. return 1;
  412. }
  413. /* Inspired by drivers/net/ethernet/dlink/sundance.c:1702
  414. * Declare each description string in struct.name[] to get fixed sized buffer
  415. * and compile time checking for strings longer than ETH_GSTRING_LEN.
  416. */
  417. static const struct {
  418. const char name[ETH_GSTRING_LEN];
  419. } bat_counters_strings[] = {
  420. { "forward" },
  421. { "forward_bytes" },
  422. { "mgmt_tx" },
  423. { "mgmt_tx_bytes" },
  424. { "mgmt_rx" },
  425. { "mgmt_rx_bytes" },
  426. { "tt_request_tx" },
  427. { "tt_request_rx" },
  428. { "tt_response_tx" },
  429. { "tt_response_rx" },
  430. { "tt_roam_adv_tx" },
  431. { "tt_roam_adv_rx" },
  432. };
  433. static void batadv_get_strings(struct net_device *dev, uint32_t stringset,
  434. uint8_t *data)
  435. {
  436. if (stringset == ETH_SS_STATS)
  437. memcpy(data, bat_counters_strings,
  438. sizeof(bat_counters_strings));
  439. }
  440. static void batadv_get_ethtool_stats(struct net_device *dev,
  441. struct ethtool_stats *stats,
  442. uint64_t *data)
  443. {
  444. struct bat_priv *bat_priv = netdev_priv(dev);
  445. int i;
  446. for (i = 0; i < BAT_CNT_NUM; i++)
  447. data[i] = batadv_sum_counter(bat_priv, i);
  448. }
  449. static int batadv_get_sset_count(struct net_device *dev, int stringset)
  450. {
  451. if (stringset == ETH_SS_STATS)
  452. return BAT_CNT_NUM;
  453. return -EOPNOTSUPP;
  454. }