soft-interface.c 11 KB

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