soft-interface.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 const struct ethtool_ops bat_ethtool_ops = {
  46. .get_settings = bat_get_settings,
  47. .get_drvinfo = bat_get_drvinfo,
  48. .get_msglevel = bat_get_msglevel,
  49. .set_msglevel = bat_set_msglevel,
  50. .get_link = bat_get_link,
  51. };
  52. int my_skb_head_push(struct sk_buff *skb, unsigned int len)
  53. {
  54. int result;
  55. /**
  56. * TODO: We must check if we can release all references to non-payload
  57. * data using skb_header_release in our skbs to allow skb_cow_header to
  58. * work optimally. This means that those skbs are not allowed to read
  59. * or write any data which is before the current position of skb->data
  60. * after that call and thus allow other skbs with the same data buffer
  61. * to write freely in that area.
  62. */
  63. result = skb_cow_head(skb, len);
  64. if (result < 0)
  65. return result;
  66. skb_push(skb, len);
  67. return 0;
  68. }
  69. static int interface_open(struct net_device *dev)
  70. {
  71. netif_start_queue(dev);
  72. return 0;
  73. }
  74. static int interface_release(struct net_device *dev)
  75. {
  76. netif_stop_queue(dev);
  77. return 0;
  78. }
  79. static struct net_device_stats *interface_stats(struct net_device *dev)
  80. {
  81. struct bat_priv *bat_priv = netdev_priv(dev);
  82. return &bat_priv->stats;
  83. }
  84. static int interface_set_mac_addr(struct net_device *dev, void *p)
  85. {
  86. struct bat_priv *bat_priv = netdev_priv(dev);
  87. struct sockaddr *addr = p;
  88. if (!is_valid_ether_addr(addr->sa_data))
  89. return -EADDRNOTAVAIL;
  90. /* only modify transtable if it has been initialized before */
  91. if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
  92. tt_local_remove(bat_priv, dev->dev_addr,
  93. "mac address changed", false);
  94. tt_local_add(dev, addr->sa_data, NULL_IFINDEX);
  95. }
  96. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  97. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  98. return 0;
  99. }
  100. static int interface_change_mtu(struct net_device *dev, int new_mtu)
  101. {
  102. /* check ranges */
  103. if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev)))
  104. return -EINVAL;
  105. dev->mtu = new_mtu;
  106. return 0;
  107. }
  108. static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
  109. {
  110. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  111. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  112. struct hard_iface *primary_if = NULL;
  113. struct bcast_packet *bcast_packet;
  114. struct vlan_ethhdr *vhdr;
  115. static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00,
  116. 0x00};
  117. unsigned int header_len = 0;
  118. int data_len = skb->len, ret;
  119. short vid __maybe_unused = -1;
  120. bool do_bcast = false;
  121. if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
  122. goto dropped;
  123. soft_iface->trans_start = jiffies;
  124. switch (ntohs(ethhdr->h_proto)) {
  125. case ETH_P_8021Q:
  126. vhdr = (struct vlan_ethhdr *)skb->data;
  127. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  128. if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
  129. break;
  130. /* fall through */
  131. case ETH_P_BATMAN:
  132. goto dropped;
  133. }
  134. if (bla_tx(bat_priv, skb, vid))
  135. goto dropped;
  136. /* Register the client MAC in the transtable */
  137. tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
  138. /* don't accept stp packets. STP does not help in meshes.
  139. * better use the bridge loop avoidance ...
  140. */
  141. if (compare_eth(ethhdr->h_dest, stp_addr))
  142. goto dropped;
  143. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  144. do_bcast = true;
  145. switch (atomic_read(&bat_priv->gw_mode)) {
  146. case GW_MODE_SERVER:
  147. /* gateway servers should not send dhcp
  148. * requests into the mesh */
  149. ret = gw_is_dhcp_target(skb, &header_len);
  150. if (ret)
  151. goto dropped;
  152. break;
  153. case GW_MODE_CLIENT:
  154. /* gateway clients should send dhcp requests
  155. * via unicast to their gateway */
  156. ret = gw_is_dhcp_target(skb, &header_len);
  157. if (ret)
  158. do_bcast = false;
  159. break;
  160. case GW_MODE_OFF:
  161. default:
  162. break;
  163. }
  164. }
  165. /* ethernet packet should be broadcasted */
  166. if (do_bcast) {
  167. primary_if = primary_if_get_selected(bat_priv);
  168. if (!primary_if)
  169. goto dropped;
  170. if (my_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
  171. goto dropped;
  172. bcast_packet = (struct bcast_packet *)skb->data;
  173. bcast_packet->header.version = COMPAT_VERSION;
  174. bcast_packet->header.ttl = TTL;
  175. /* batman packet type: broadcast */
  176. bcast_packet->header.packet_type = BAT_BCAST;
  177. /* hw address of first interface is the orig mac because only
  178. * this mac is known throughout the mesh */
  179. memcpy(bcast_packet->orig,
  180. primary_if->net_dev->dev_addr, ETH_ALEN);
  181. /* set broadcast sequence number */
  182. bcast_packet->seqno =
  183. htonl(atomic_inc_return(&bat_priv->bcast_seqno));
  184. add_bcast_packet_to_list(bat_priv, skb, 1);
  185. /* a copy is stored in the bcast list, therefore removing
  186. * the original skb. */
  187. kfree_skb(skb);
  188. /* unicast packet */
  189. } else {
  190. if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) {
  191. ret = gw_out_of_range(bat_priv, skb, ethhdr);
  192. if (ret)
  193. goto dropped;
  194. }
  195. ret = unicast_send_skb(skb, bat_priv);
  196. if (ret != 0)
  197. goto dropped_freed;
  198. }
  199. bat_priv->stats.tx_packets++;
  200. bat_priv->stats.tx_bytes += data_len;
  201. goto end;
  202. dropped:
  203. kfree_skb(skb);
  204. dropped_freed:
  205. bat_priv->stats.tx_dropped++;
  206. end:
  207. if (primary_if)
  208. hardif_free_ref(primary_if);
  209. return NETDEV_TX_OK;
  210. }
  211. void interface_rx(struct net_device *soft_iface,
  212. struct sk_buff *skb, struct hard_iface *recv_if,
  213. int hdr_size)
  214. {
  215. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  216. struct ethhdr *ethhdr;
  217. struct vlan_ethhdr *vhdr;
  218. short vid __maybe_unused = -1;
  219. /* check if enough space is available for pulling, and pull */
  220. if (!pskb_may_pull(skb, hdr_size))
  221. goto dropped;
  222. skb_pull_rcsum(skb, hdr_size);
  223. skb_reset_mac_header(skb);
  224. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  225. switch (ntohs(ethhdr->h_proto)) {
  226. case ETH_P_8021Q:
  227. vhdr = (struct vlan_ethhdr *)skb->data;
  228. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  229. if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
  230. break;
  231. /* fall through */
  232. case ETH_P_BATMAN:
  233. goto dropped;
  234. }
  235. /* skb->dev & skb->pkt_type are set here */
  236. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  237. goto dropped;
  238. skb->protocol = eth_type_trans(skb, soft_iface);
  239. /* should not be necessary anymore as we use skb_pull_rcsum()
  240. * TODO: please verify this and remove this TODO
  241. * -- Dec 21st 2009, Simon Wunderlich */
  242. /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
  243. bat_priv->stats.rx_packets++;
  244. bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
  245. soft_iface->last_rx = jiffies;
  246. if (is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
  247. goto dropped;
  248. /* Let the bridge loop avoidance check the packet. If will
  249. * not handle it, we can safely push it up.
  250. */
  251. if (bla_rx(bat_priv, skb, vid))
  252. goto out;
  253. netif_rx(skb);
  254. goto out;
  255. dropped:
  256. kfree_skb(skb);
  257. out:
  258. return;
  259. }
  260. static const struct net_device_ops bat_netdev_ops = {
  261. .ndo_open = interface_open,
  262. .ndo_stop = interface_release,
  263. .ndo_get_stats = interface_stats,
  264. .ndo_set_mac_address = interface_set_mac_addr,
  265. .ndo_change_mtu = interface_change_mtu,
  266. .ndo_start_xmit = interface_tx,
  267. .ndo_validate_addr = eth_validate_addr
  268. };
  269. static void interface_setup(struct net_device *dev)
  270. {
  271. struct bat_priv *priv = netdev_priv(dev);
  272. ether_setup(dev);
  273. dev->netdev_ops = &bat_netdev_ops;
  274. dev->destructor = free_netdev;
  275. dev->tx_queue_len = 0;
  276. /**
  277. * can't call min_mtu, because the needed variables
  278. * have not been initialized yet
  279. */
  280. dev->mtu = ETH_DATA_LEN;
  281. /* reserve more space in the skbuff for our header */
  282. dev->hard_header_len = BAT_HEADER_LEN;
  283. /* generate random address */
  284. eth_hw_addr_random(dev);
  285. SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
  286. memset(priv, 0, sizeof(*priv));
  287. }
  288. struct net_device *softif_create(const char *name)
  289. {
  290. struct net_device *soft_iface;
  291. struct bat_priv *bat_priv;
  292. int ret;
  293. soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup);
  294. if (!soft_iface)
  295. goto out;
  296. ret = register_netdevice(soft_iface);
  297. if (ret < 0) {
  298. pr_err("Unable to register the batman interface '%s': %i\n",
  299. name, ret);
  300. goto free_soft_iface;
  301. }
  302. bat_priv = netdev_priv(soft_iface);
  303. atomic_set(&bat_priv->aggregated_ogms, 1);
  304. atomic_set(&bat_priv->bonding, 0);
  305. atomic_set(&bat_priv->bridge_loop_avoidance, 0);
  306. atomic_set(&bat_priv->ap_isolation, 0);
  307. atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
  308. atomic_set(&bat_priv->gw_mode, GW_MODE_OFF);
  309. atomic_set(&bat_priv->gw_sel_class, 20);
  310. atomic_set(&bat_priv->gw_bandwidth, 41);
  311. atomic_set(&bat_priv->orig_interval, 1000);
  312. atomic_set(&bat_priv->hop_penalty, 30);
  313. atomic_set(&bat_priv->log_level, 0);
  314. atomic_set(&bat_priv->fragmentation, 1);
  315. atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
  316. atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
  317. atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
  318. atomic_set(&bat_priv->bcast_seqno, 1);
  319. atomic_set(&bat_priv->ttvn, 0);
  320. atomic_set(&bat_priv->tt_local_changes, 0);
  321. atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
  322. atomic_set(&bat_priv->bla_num_requests, 0);
  323. bat_priv->tt_buff = NULL;
  324. bat_priv->tt_buff_len = 0;
  325. bat_priv->tt_poss_change = false;
  326. bat_priv->primary_if = NULL;
  327. bat_priv->num_ifaces = 0;
  328. ret = bat_algo_select(bat_priv, bat_routing_algo);
  329. if (ret < 0)
  330. goto unreg_soft_iface;
  331. ret = sysfs_add_meshif(soft_iface);
  332. if (ret < 0)
  333. goto unreg_soft_iface;
  334. ret = debugfs_add_meshif(soft_iface);
  335. if (ret < 0)
  336. goto unreg_sysfs;
  337. ret = mesh_init(soft_iface);
  338. if (ret < 0)
  339. goto unreg_debugfs;
  340. return soft_iface;
  341. unreg_debugfs:
  342. debugfs_del_meshif(soft_iface);
  343. unreg_sysfs:
  344. sysfs_del_meshif(soft_iface);
  345. unreg_soft_iface:
  346. unregister_netdevice(soft_iface);
  347. return NULL;
  348. free_soft_iface:
  349. free_netdev(soft_iface);
  350. out:
  351. return NULL;
  352. }
  353. void softif_destroy(struct net_device *soft_iface)
  354. {
  355. debugfs_del_meshif(soft_iface);
  356. sysfs_del_meshif(soft_iface);
  357. mesh_free(soft_iface);
  358. unregister_netdevice(soft_iface);
  359. }
  360. int softif_is_valid(const struct net_device *net_dev)
  361. {
  362. if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
  363. return 1;
  364. return 0;
  365. }
  366. /* ethtool */
  367. static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  368. {
  369. cmd->supported = 0;
  370. cmd->advertising = 0;
  371. ethtool_cmd_speed_set(cmd, SPEED_10);
  372. cmd->duplex = DUPLEX_FULL;
  373. cmd->port = PORT_TP;
  374. cmd->phy_address = 0;
  375. cmd->transceiver = XCVR_INTERNAL;
  376. cmd->autoneg = AUTONEG_DISABLE;
  377. cmd->maxtxpkt = 0;
  378. cmd->maxrxpkt = 0;
  379. return 0;
  380. }
  381. static void bat_get_drvinfo(struct net_device *dev,
  382. struct ethtool_drvinfo *info)
  383. {
  384. strcpy(info->driver, "B.A.T.M.A.N. advanced");
  385. strcpy(info->version, SOURCE_VERSION);
  386. strcpy(info->fw_version, "N/A");
  387. strcpy(info->bus_info, "batman");
  388. }
  389. static u32 bat_get_msglevel(struct net_device *dev)
  390. {
  391. return -EOPNOTSUPP;
  392. }
  393. static void bat_set_msglevel(struct net_device *dev, u32 value)
  394. {
  395. }
  396. static u32 bat_get_link(struct net_device *dev)
  397. {
  398. return 1;
  399. }