Antonio Quartulli | 0b87393 | 2013-01-04 03:05:31 +0100 | [diff] [blame] | 1 | /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 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 |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Sven Eckelmann | 95a066d | 2012-10-17 21:10:39 +0200 | [diff] [blame] | 20 | #include <linux/crc32c.h> |
| 21 | #include <linux/highmem.h> |
Simon Wunderlich | c54f38c | 2013-07-29 17:56:44 +0200 | [diff] [blame] | 22 | #include <linux/if_vlan.h> |
| 23 | #include <net/ip.h> |
| 24 | #include <net/ipv6.h> |
| 25 | #include <net/dsfield.h> |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 26 | #include "main.h" |
Sven Eckelmann | b706b13 | 2012-06-10 23:58:51 +0200 | [diff] [blame] | 27 | #include "sysfs.h" |
| 28 | #include "debugfs.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 29 | #include "routing.h" |
| 30 | #include "send.h" |
| 31 | #include "originator.h" |
| 32 | #include "soft-interface.h" |
| 33 | #include "icmp_socket.h" |
| 34 | #include "translation-table.h" |
| 35 | #include "hard-interface.h" |
| 36 | #include "gateway_client.h" |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 37 | #include "bridge_loop_avoidance.h" |
Antonio Quartulli | 2f1dfbe | 2012-06-30 20:01:19 +0200 | [diff] [blame] | 38 | #include "distributed-arp-table.h" |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame^] | 39 | #include "unicast.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 40 | #include "vis.h" |
| 41 | #include "hash.h" |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 42 | #include "bat_algo.h" |
Martin Hundebøll | d353d8d | 2013-01-25 11:12:38 +0100 | [diff] [blame] | 43 | #include "network-coding.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 44 | |
Sven Eckelmann | c3caf51 | 2011-05-03 11:51:38 +0200 | [diff] [blame] | 45 | |
| 46 | /* List manipulations on hardif_list have to be rtnl_lock()'ed, |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 47 | * list traversals just rcu-locked |
| 48 | */ |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 49 | struct list_head batadv_hardif_list; |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 50 | static int (*batadv_rx_handler[256])(struct sk_buff *, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 51 | struct batadv_hard_iface *); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 52 | char batadv_routing_algo[20] = "BATMAN_IV"; |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 53 | static struct hlist_head batadv_algo_list; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 54 | |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 55 | unsigned char batadv_broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 56 | |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 57 | struct workqueue_struct *batadv_event_workqueue; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 58 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 59 | static void batadv_recv_handler_init(void); |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 60 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 61 | static int __init batadv_init(void) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 62 | { |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 63 | INIT_LIST_HEAD(&batadv_hardif_list); |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 64 | INIT_HLIST_HEAD(&batadv_algo_list); |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 65 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 66 | batadv_recv_handler_init(); |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 67 | |
Sven Eckelmann | 81c524f | 2012-05-12 02:09:22 +0200 | [diff] [blame] | 68 | batadv_iv_init(); |
Matthias Schiffer | 6c519ba | 2013-09-27 18:03:39 +0200 | [diff] [blame] | 69 | batadv_nc_init(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 70 | |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 71 | batadv_event_workqueue = create_singlethread_workqueue("bat_events"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 72 | |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 73 | if (!batadv_event_workqueue) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 74 | return -ENOMEM; |
| 75 | |
Sven Eckelmann | 9039dc7 | 2012-05-12 02:09:33 +0200 | [diff] [blame] | 76 | batadv_socket_init(); |
Sven Eckelmann | 40a072d | 2012-05-12 02:09:23 +0200 | [diff] [blame] | 77 | batadv_debugfs_init(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 78 | |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 79 | register_netdevice_notifier(&batadv_hard_if_notifier); |
Sven Eckelmann | a4ac28c | 2013-02-11 17:10:26 +0800 | [diff] [blame] | 80 | rtnl_link_register(&batadv_link_ops); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 81 | |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 82 | pr_info("B.A.T.M.A.N. advanced %s (compatibility version %i) loaded\n", |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 83 | BATADV_SOURCE_VERSION, BATADV_COMPAT_VERSION); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 88 | static void __exit batadv_exit(void) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 89 | { |
Sven Eckelmann | 40a072d | 2012-05-12 02:09:23 +0200 | [diff] [blame] | 90 | batadv_debugfs_destroy(); |
Sven Eckelmann | a4ac28c | 2013-02-11 17:10:26 +0800 | [diff] [blame] | 91 | rtnl_link_unregister(&batadv_link_ops); |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 92 | unregister_netdevice_notifier(&batadv_hard_if_notifier); |
| 93 | batadv_hardif_remove_interfaces(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 94 | |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 95 | flush_workqueue(batadv_event_workqueue); |
| 96 | destroy_workqueue(batadv_event_workqueue); |
| 97 | batadv_event_workqueue = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 98 | |
| 99 | rcu_barrier(); |
| 100 | } |
| 101 | |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 102 | int batadv_mesh_init(struct net_device *soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 103 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 104 | struct batadv_priv *bat_priv = netdev_priv(soft_iface); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 105 | int ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 106 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 107 | spin_lock_init(&bat_priv->forw_bat_list_lock); |
| 108 | spin_lock_init(&bat_priv->forw_bcast_list_lock); |
Sven Eckelmann | 807736f | 2012-07-15 22:26:51 +0200 | [diff] [blame] | 109 | spin_lock_init(&bat_priv->tt.changes_list_lock); |
| 110 | spin_lock_init(&bat_priv->tt.req_list_lock); |
| 111 | spin_lock_init(&bat_priv->tt.roam_list_lock); |
| 112 | spin_lock_init(&bat_priv->tt.last_changeset_lock); |
| 113 | spin_lock_init(&bat_priv->gw.list_lock); |
| 114 | spin_lock_init(&bat_priv->vis.hash_lock); |
| 115 | spin_lock_init(&bat_priv->vis.list_lock); |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame^] | 116 | spin_lock_init(&bat_priv->tvlv.container_list_lock); |
| 117 | spin_lock_init(&bat_priv->tvlv.handler_list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 118 | |
| 119 | INIT_HLIST_HEAD(&bat_priv->forw_bat_list); |
| 120 | INIT_HLIST_HEAD(&bat_priv->forw_bcast_list); |
Sven Eckelmann | 807736f | 2012-07-15 22:26:51 +0200 | [diff] [blame] | 121 | INIT_HLIST_HEAD(&bat_priv->gw.list); |
| 122 | INIT_LIST_HEAD(&bat_priv->tt.changes_list); |
| 123 | INIT_LIST_HEAD(&bat_priv->tt.req_list); |
| 124 | INIT_LIST_HEAD(&bat_priv->tt.roam_list); |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame^] | 125 | INIT_HLIST_HEAD(&bat_priv->tvlv.container_list); |
| 126 | INIT_HLIST_HEAD(&bat_priv->tvlv.handler_list); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 127 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 128 | ret = batadv_originator_init(bat_priv); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 129 | if (ret < 0) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 130 | goto err; |
| 131 | |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 132 | ret = batadv_tt_init(bat_priv); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 133 | if (ret < 0) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 134 | goto err; |
| 135 | |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 136 | batadv_tt_local_add(soft_iface, soft_iface->dev_addr, |
| 137 | BATADV_NULL_IFINDEX); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 138 | |
Sven Eckelmann | d0f714f | 2012-05-12 02:09:41 +0200 | [diff] [blame] | 139 | ret = batadv_vis_init(bat_priv); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 140 | if (ret < 0) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 141 | goto err; |
| 142 | |
Sven Eckelmann | 08adf15 | 2012-05-12 13:38:47 +0200 | [diff] [blame] | 143 | ret = batadv_bla_init(bat_priv); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 144 | if (ret < 0) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 145 | goto err; |
| 146 | |
Antonio Quartulli | 2f1dfbe | 2012-06-30 20:01:19 +0200 | [diff] [blame] | 147 | ret = batadv_dat_init(bat_priv); |
| 148 | if (ret < 0) |
| 149 | goto err; |
| 150 | |
Matthias Schiffer | 6c519ba | 2013-09-27 18:03:39 +0200 | [diff] [blame] | 151 | ret = batadv_nc_mesh_init(bat_priv); |
Martin Hundebøll | d353d8d | 2013-01-25 11:12:38 +0100 | [diff] [blame] | 152 | if (ret < 0) |
| 153 | goto err; |
| 154 | |
Sven Eckelmann | 807736f | 2012-07-15 22:26:51 +0200 | [diff] [blame] | 155 | atomic_set(&bat_priv->gw.reselect, 0); |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 156 | atomic_set(&bat_priv->mesh_state, BATADV_MESH_ACTIVE); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 157 | |
| 158 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 159 | |
| 160 | err: |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 161 | batadv_mesh_free(soft_iface); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 162 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 165 | void batadv_mesh_free(struct net_device *soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 166 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 167 | struct batadv_priv *bat_priv = netdev_priv(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 168 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 169 | atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 170 | |
Sven Eckelmann | 9455e34 | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 171 | batadv_purge_outstanding_packets(bat_priv, NULL); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 172 | |
Sven Eckelmann | d0f714f | 2012-05-12 02:09:41 +0200 | [diff] [blame] | 173 | batadv_vis_quit(bat_priv); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 174 | |
Sven Eckelmann | 7cf06bc | 2012-05-12 02:09:29 +0200 | [diff] [blame] | 175 | batadv_gw_node_purge(bat_priv); |
Matthias Schiffer | 6c519ba | 2013-09-27 18:03:39 +0200 | [diff] [blame] | 176 | batadv_nc_mesh_free(bat_priv); |
Antonio Quartulli | a436186 | 2013-05-07 01:06:18 +0200 | [diff] [blame] | 177 | batadv_dat_free(bat_priv); |
Sven Eckelmann | 08adf15 | 2012-05-12 13:38:47 +0200 | [diff] [blame] | 178 | batadv_bla_free(bat_priv); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 179 | |
Antonio Quartulli | a436186 | 2013-05-07 01:06:18 +0200 | [diff] [blame] | 180 | /* Free the TT and the originator tables only after having terminated |
| 181 | * all the other depending components which may use these structures for |
| 182 | * their purposes. |
| 183 | */ |
| 184 | batadv_tt_free(bat_priv); |
| 185 | |
| 186 | /* Since the originator table clean up routine is accessing the TT |
| 187 | * tables as well, it has to be invoked after the TT tables have been |
| 188 | * freed and marked as empty. This ensures that no cleanup RCU callbacks |
| 189 | * accessing the TT data are scheduled for later execution. |
| 190 | */ |
| 191 | batadv_originator_free(bat_priv); |
Antonio Quartulli | 2f1dfbe | 2012-06-30 20:01:19 +0200 | [diff] [blame] | 192 | |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 193 | free_percpu(bat_priv->bat_counters); |
Martin Hundebøll | f69ae77 | 2013-04-17 21:13:16 +0200 | [diff] [blame] | 194 | bat_priv->bat_counters = NULL; |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 195 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 196 | atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 197 | } |
| 198 | |
David S. Miller | 6e0895c | 2013-04-22 20:32:51 -0400 | [diff] [blame] | 199 | /** |
| 200 | * batadv_is_my_mac - check if the given mac address belongs to any of the real |
| 201 | * interfaces in the current mesh |
| 202 | * @bat_priv: the bat priv with all the soft interface information |
| 203 | * @addr: the address to check |
| 204 | */ |
Antonio Quartulli | fe8a93b | 2013-04-03 19:10:26 +0200 | [diff] [blame] | 205 | int batadv_is_my_mac(struct batadv_priv *bat_priv, const uint8_t *addr) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 206 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 207 | const struct batadv_hard_iface *hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 208 | |
| 209 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 210 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 211 | if (hard_iface->if_status != BATADV_IF_ACTIVE) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 212 | continue; |
| 213 | |
Antonio Quartulli | fe8a93b | 2013-04-03 19:10:26 +0200 | [diff] [blame] | 214 | if (hard_iface->soft_iface != bat_priv->soft_iface) |
| 215 | continue; |
| 216 | |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 217 | if (batadv_compare_eth(hard_iface->net_dev->dev_addr, addr)) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 218 | rcu_read_unlock(); |
| 219 | return 1; |
| 220 | } |
| 221 | } |
| 222 | rcu_read_unlock(); |
| 223 | return 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Marek Lindner | 30da63a | 2012-08-03 17:15:46 +0200 | [diff] [blame] | 226 | /** |
| 227 | * batadv_seq_print_text_primary_if_get - called from debugfs table printing |
| 228 | * function that requires the primary interface |
| 229 | * @seq: debugfs table seq_file struct |
| 230 | * |
| 231 | * Returns primary interface if found or NULL otherwise. |
| 232 | */ |
| 233 | struct batadv_hard_iface * |
| 234 | batadv_seq_print_text_primary_if_get(struct seq_file *seq) |
| 235 | { |
| 236 | struct net_device *net_dev = (struct net_device *)seq->private; |
| 237 | struct batadv_priv *bat_priv = netdev_priv(net_dev); |
| 238 | struct batadv_hard_iface *primary_if; |
| 239 | |
| 240 | primary_if = batadv_primary_if_get_selected(bat_priv); |
| 241 | |
| 242 | if (!primary_if) { |
| 243 | seq_printf(seq, |
| 244 | "BATMAN mesh %s disabled - please specify interfaces to enable it\n", |
| 245 | net_dev->name); |
| 246 | goto out; |
| 247 | } |
| 248 | |
| 249 | if (primary_if->if_status == BATADV_IF_ACTIVE) |
| 250 | goto out; |
| 251 | |
| 252 | seq_printf(seq, |
| 253 | "BATMAN mesh %s disabled - primary interface not active\n", |
| 254 | net_dev->name); |
| 255 | batadv_hardif_free_ref(primary_if); |
| 256 | primary_if = NULL; |
| 257 | |
| 258 | out: |
| 259 | return primary_if; |
| 260 | } |
| 261 | |
Simon Wunderlich | c54f38c | 2013-07-29 17:56:44 +0200 | [diff] [blame] | 262 | /** |
| 263 | * batadv_skb_set_priority - sets skb priority according to packet content |
| 264 | * @skb: the packet to be sent |
| 265 | * @offset: offset to the packet content |
| 266 | * |
| 267 | * This function sets a value between 256 and 263 (802.1d priority), which |
| 268 | * can be interpreted by the cfg80211 or other drivers. |
| 269 | */ |
| 270 | void batadv_skb_set_priority(struct sk_buff *skb, int offset) |
| 271 | { |
| 272 | struct iphdr ip_hdr_tmp, *ip_hdr; |
| 273 | struct ipv6hdr ip6_hdr_tmp, *ip6_hdr; |
| 274 | struct ethhdr ethhdr_tmp, *ethhdr; |
| 275 | struct vlan_ethhdr *vhdr, vhdr_tmp; |
| 276 | u32 prio; |
| 277 | |
| 278 | /* already set, do nothing */ |
| 279 | if (skb->priority >= 256 && skb->priority <= 263) |
| 280 | return; |
| 281 | |
| 282 | ethhdr = skb_header_pointer(skb, offset, sizeof(*ethhdr), ðhdr_tmp); |
| 283 | if (!ethhdr) |
| 284 | return; |
| 285 | |
| 286 | switch (ethhdr->h_proto) { |
| 287 | case htons(ETH_P_8021Q): |
| 288 | vhdr = skb_header_pointer(skb, offset + sizeof(*vhdr), |
| 289 | sizeof(*vhdr), &vhdr_tmp); |
| 290 | if (!vhdr) |
| 291 | return; |
| 292 | prio = ntohs(vhdr->h_vlan_TCI) & VLAN_PRIO_MASK; |
| 293 | prio = prio >> VLAN_PRIO_SHIFT; |
| 294 | break; |
| 295 | case htons(ETH_P_IP): |
| 296 | ip_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr), |
| 297 | sizeof(*ip_hdr), &ip_hdr_tmp); |
| 298 | if (!ip_hdr) |
| 299 | return; |
| 300 | prio = (ipv4_get_dsfield(ip_hdr) & 0xfc) >> 5; |
| 301 | break; |
| 302 | case htons(ETH_P_IPV6): |
| 303 | ip6_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr), |
| 304 | sizeof(*ip6_hdr), &ip6_hdr_tmp); |
| 305 | if (!ip6_hdr) |
| 306 | return; |
| 307 | prio = (ipv6_get_dsfield(ip6_hdr) & 0xfc) >> 5; |
| 308 | break; |
| 309 | default: |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | skb->priority = prio + 256; |
| 314 | } |
| 315 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 316 | static int batadv_recv_unhandled_packet(struct sk_buff *skb, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 317 | struct batadv_hard_iface *recv_if) |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 318 | { |
| 319 | return NET_RX_DROP; |
| 320 | } |
| 321 | |
| 322 | /* incoming packets with the batman ethertype received on any active hard |
| 323 | * interface |
| 324 | */ |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 325 | int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev, |
| 326 | struct packet_type *ptype, |
| 327 | struct net_device *orig_dev) |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 328 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 329 | struct batadv_priv *bat_priv; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 330 | struct batadv_ogm_packet *batadv_ogm_packet; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 331 | struct batadv_hard_iface *hard_iface; |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 332 | uint8_t idx; |
| 333 | int ret; |
| 334 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 335 | hard_iface = container_of(ptype, struct batadv_hard_iface, |
| 336 | batman_adv_ptype); |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 337 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 338 | |
| 339 | /* skb was released by skb_share_check() */ |
| 340 | if (!skb) |
| 341 | goto err_out; |
| 342 | |
| 343 | /* packet should hold at least type and version */ |
| 344 | if (unlikely(!pskb_may_pull(skb, 2))) |
| 345 | goto err_free; |
| 346 | |
| 347 | /* expect a valid ethernet header here. */ |
| 348 | if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb))) |
| 349 | goto err_free; |
| 350 | |
| 351 | if (!hard_iface->soft_iface) |
| 352 | goto err_free; |
| 353 | |
| 354 | bat_priv = netdev_priv(hard_iface->soft_iface); |
| 355 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 356 | if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE) |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 357 | goto err_free; |
| 358 | |
| 359 | /* discard frames on not active interfaces */ |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 360 | if (hard_iface->if_status != BATADV_IF_ACTIVE) |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 361 | goto err_free; |
| 362 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 363 | batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data; |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 364 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 365 | if (batadv_ogm_packet->header.version != BATADV_COMPAT_VERSION) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 366 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 367 | "Drop packet: incompatible batman version (%i)\n", |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 368 | batadv_ogm_packet->header.version); |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 369 | goto err_free; |
| 370 | } |
| 371 | |
| 372 | /* all receive handlers return whether they received or reused |
| 373 | * the supplied skb. if not, we have to free the skb. |
| 374 | */ |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 375 | idx = batadv_ogm_packet->header.packet_type; |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 376 | ret = (*batadv_rx_handler[idx])(skb, hard_iface); |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 377 | |
| 378 | if (ret == NET_RX_DROP) |
| 379 | kfree_skb(skb); |
| 380 | |
| 381 | /* return NET_RX_SUCCESS in any case as we |
| 382 | * most probably dropped the packet for |
| 383 | * routing-logical reasons. |
| 384 | */ |
| 385 | return NET_RX_SUCCESS; |
| 386 | |
| 387 | err_free: |
| 388 | kfree_skb(skb); |
| 389 | err_out: |
| 390 | return NET_RX_DROP; |
| 391 | } |
| 392 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 393 | static void batadv_recv_handler_init(void) |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 394 | { |
| 395 | int i; |
| 396 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 397 | for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++) |
| 398 | batadv_rx_handler[i] = batadv_recv_unhandled_packet; |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 399 | |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 400 | /* batman icmp packet */ |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 401 | batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet; |
Antonio Quartulli | 7cdcf6d | 2012-10-01 09:57:35 +0200 | [diff] [blame] | 402 | /* unicast with 4 addresses packet */ |
| 403 | batadv_rx_handler[BATADV_UNICAST_4ADDR] = batadv_recv_unicast_packet; |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 404 | /* unicast packet */ |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 405 | batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet; |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 406 | /* fragmented unicast packet */ |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 407 | batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_ucast_frag_packet; |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 408 | /* broadcast packet */ |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 409 | batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet; |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 410 | /* vis packet */ |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 411 | batadv_rx_handler[BATADV_VIS] = batadv_recv_vis_packet; |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 412 | /* Translation table query (request or response) */ |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 413 | batadv_rx_handler[BATADV_TT_QUERY] = batadv_recv_tt_query; |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 414 | /* Roaming advertisement */ |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 415 | batadv_rx_handler[BATADV_ROAM_ADV] = batadv_recv_roam_adv; |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame^] | 416 | /* unicast tvlv packet */ |
| 417 | batadv_rx_handler[BATADV_UNICAST_TVLV] = batadv_recv_unicast_tvlv; |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 418 | } |
| 419 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 420 | int |
| 421 | batadv_recv_handler_register(uint8_t packet_type, |
| 422 | int (*recv_handler)(struct sk_buff *, |
| 423 | struct batadv_hard_iface *)) |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 424 | { |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 425 | if (batadv_rx_handler[packet_type] != &batadv_recv_unhandled_packet) |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 426 | return -EBUSY; |
| 427 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 428 | batadv_rx_handler[packet_type] = recv_handler; |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 429 | return 0; |
| 430 | } |
| 431 | |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 432 | void batadv_recv_handler_unregister(uint8_t packet_type) |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 433 | { |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 434 | batadv_rx_handler[packet_type] = batadv_recv_unhandled_packet; |
Marek Lindner | ffa995e | 2012-03-01 15:35:17 +0800 | [diff] [blame] | 435 | } |
| 436 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 437 | static struct batadv_algo_ops *batadv_algo_get(char *name) |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 438 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 439 | struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp; |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 440 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 441 | hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) { |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 442 | if (strcmp(bat_algo_ops_tmp->name, name) != 0) |
| 443 | continue; |
| 444 | |
| 445 | bat_algo_ops = bat_algo_ops_tmp; |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | return bat_algo_ops; |
| 450 | } |
| 451 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 452 | int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops) |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 453 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 454 | struct batadv_algo_ops *bat_algo_ops_tmp; |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 455 | int ret; |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 456 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 457 | bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name); |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 458 | if (bat_algo_ops_tmp) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 459 | pr_info("Trying to register already registered routing algorithm: %s\n", |
| 460 | bat_algo_ops->name); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 461 | ret = -EEXIST; |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 462 | goto out; |
| 463 | } |
| 464 | |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 465 | /* all algorithms must implement all ops (for now) */ |
Marek Lindner | c2aca02 | 2012-02-07 17:20:45 +0800 | [diff] [blame] | 466 | if (!bat_algo_ops->bat_iface_enable || |
Marek Lindner | 00a5007 | 2012-02-07 17:20:47 +0800 | [diff] [blame] | 467 | !bat_algo_ops->bat_iface_disable || |
Marek Lindner | c322939 | 2012-03-11 06:17:50 +0800 | [diff] [blame] | 468 | !bat_algo_ops->bat_iface_update_mac || |
Marek Lindner | cd8b78e | 2012-02-07 17:20:49 +0800 | [diff] [blame] | 469 | !bat_algo_ops->bat_primary_iface_set || |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 470 | !bat_algo_ops->bat_ogm_schedule || |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 471 | !bat_algo_ops->bat_ogm_emit) { |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 472 | pr_info("Routing algo '%s' does not implement required ops\n", |
| 473 | bat_algo_ops->name); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 474 | ret = -EINVAL; |
Marek Lindner | 01c4224 | 2011-11-28 21:31:55 +0800 | [diff] [blame] | 475 | goto out; |
| 476 | } |
| 477 | |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 478 | INIT_HLIST_NODE(&bat_algo_ops->list); |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 479 | hlist_add_head(&bat_algo_ops->list, &batadv_algo_list); |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 480 | ret = 0; |
| 481 | |
| 482 | out: |
| 483 | return ret; |
| 484 | } |
| 485 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 486 | int batadv_algo_select(struct batadv_priv *bat_priv, char *name) |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 487 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 488 | struct batadv_algo_ops *bat_algo_ops; |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 489 | int ret = -EINVAL; |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 490 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 491 | bat_algo_ops = batadv_algo_get(name); |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 492 | if (!bat_algo_ops) |
| 493 | goto out; |
| 494 | |
| 495 | bat_priv->bat_algo_ops = bat_algo_ops; |
| 496 | ret = 0; |
| 497 | |
| 498 | out: |
| 499 | return ret; |
| 500 | } |
| 501 | |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 502 | int batadv_algo_seq_print_text(struct seq_file *seq, void *offset) |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 503 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 504 | struct batadv_algo_ops *bat_algo_ops; |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 505 | |
Antonio Quartulli | 0c81465 | 2013-03-21 09:23:29 +0100 | [diff] [blame] | 506 | seq_puts(seq, "Available routing algorithms:\n"); |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 507 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 508 | hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) { |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 509 | seq_printf(seq, "%s\n", bat_algo_ops->name); |
| 510 | } |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
Sven Eckelmann | 95a066d | 2012-10-17 21:10:39 +0200 | [diff] [blame] | 515 | /** |
| 516 | * batadv_skb_crc32 - calculate CRC32 of the whole packet and skip bytes in |
| 517 | * the header |
| 518 | * @skb: skb pointing to fragmented socket buffers |
| 519 | * @payload_ptr: Pointer to position inside the head buffer of the skb |
| 520 | * marking the start of the data to be CRC'ed |
| 521 | * |
| 522 | * payload_ptr must always point to an address in the skb head buffer and not to |
| 523 | * a fragment. |
| 524 | */ |
| 525 | __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr) |
| 526 | { |
| 527 | u32 crc = 0; |
| 528 | unsigned int from; |
| 529 | unsigned int to = skb->len; |
| 530 | struct skb_seq_state st; |
| 531 | const u8 *data; |
| 532 | unsigned int len; |
| 533 | unsigned int consumed = 0; |
| 534 | |
| 535 | from = (unsigned int)(payload_ptr - skb->data); |
| 536 | |
| 537 | skb_prepare_seq_read(skb, from, to, &st); |
| 538 | while ((len = skb_seq_read(consumed, &data, &st)) != 0) { |
| 539 | crc = crc32c(crc, data, len); |
| 540 | consumed += len; |
| 541 | } |
Sven Eckelmann | 95a066d | 2012-10-17 21:10:39 +0200 | [diff] [blame] | 542 | |
| 543 | return htonl(crc); |
| 544 | } |
| 545 | |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame^] | 546 | /** |
| 547 | * batadv_tvlv_handler_free_ref - decrement the tvlv handler refcounter and |
| 548 | * possibly free it |
| 549 | * @tvlv_handler: the tvlv handler to free |
| 550 | */ |
| 551 | static void |
| 552 | batadv_tvlv_handler_free_ref(struct batadv_tvlv_handler *tvlv_handler) |
| 553 | { |
| 554 | if (atomic_dec_and_test(&tvlv_handler->refcount)) |
| 555 | kfree_rcu(tvlv_handler, rcu); |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * batadv_tvlv_handler_get - retrieve tvlv handler from the tvlv handler list |
| 560 | * based on the provided type and version (both need to match) |
| 561 | * @bat_priv: the bat priv with all the soft interface information |
| 562 | * @type: tvlv handler type to look for |
| 563 | * @version: tvlv handler version to look for |
| 564 | * |
| 565 | * Returns tvlv handler if found or NULL otherwise. |
| 566 | */ |
| 567 | static struct batadv_tvlv_handler |
| 568 | *batadv_tvlv_handler_get(struct batadv_priv *bat_priv, |
| 569 | uint8_t type, uint8_t version) |
| 570 | { |
| 571 | struct batadv_tvlv_handler *tvlv_handler_tmp, *tvlv_handler = NULL; |
| 572 | |
| 573 | rcu_read_lock(); |
| 574 | hlist_for_each_entry_rcu(tvlv_handler_tmp, |
| 575 | &bat_priv->tvlv.handler_list, list) { |
| 576 | if (tvlv_handler_tmp->type != type) |
| 577 | continue; |
| 578 | |
| 579 | if (tvlv_handler_tmp->version != version) |
| 580 | continue; |
| 581 | |
| 582 | if (!atomic_inc_not_zero(&tvlv_handler_tmp->refcount)) |
| 583 | continue; |
| 584 | |
| 585 | tvlv_handler = tvlv_handler_tmp; |
| 586 | break; |
| 587 | } |
| 588 | rcu_read_unlock(); |
| 589 | |
| 590 | return tvlv_handler; |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * batadv_tvlv_container_free_ref - decrement the tvlv container refcounter and |
| 595 | * possibly free it |
| 596 | * @tvlv_handler: the tvlv container to free |
| 597 | */ |
| 598 | static void batadv_tvlv_container_free_ref(struct batadv_tvlv_container *tvlv) |
| 599 | { |
| 600 | if (atomic_dec_and_test(&tvlv->refcount)) |
| 601 | kfree(tvlv); |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * batadv_tvlv_container_get - retrieve tvlv container from the tvlv container |
| 606 | * list based on the provided type and version (both need to match) |
| 607 | * @bat_priv: the bat priv with all the soft interface information |
| 608 | * @type: tvlv container type to look for |
| 609 | * @version: tvlv container version to look for |
| 610 | * |
| 611 | * Has to be called with the appropriate locks being acquired |
| 612 | * (tvlv.container_list_lock). |
| 613 | * |
| 614 | * Returns tvlv container if found or NULL otherwise. |
| 615 | */ |
| 616 | static struct batadv_tvlv_container |
| 617 | *batadv_tvlv_container_get(struct batadv_priv *bat_priv, |
| 618 | uint8_t type, uint8_t version) |
| 619 | { |
| 620 | struct batadv_tvlv_container *tvlv_tmp, *tvlv = NULL; |
| 621 | |
| 622 | hlist_for_each_entry(tvlv_tmp, &bat_priv->tvlv.container_list, list) { |
| 623 | if (tvlv_tmp->tvlv_hdr.type != type) |
| 624 | continue; |
| 625 | |
| 626 | if (tvlv_tmp->tvlv_hdr.version != version) |
| 627 | continue; |
| 628 | |
| 629 | if (!atomic_inc_not_zero(&tvlv_tmp->refcount)) |
| 630 | continue; |
| 631 | |
| 632 | tvlv = tvlv_tmp; |
| 633 | break; |
| 634 | } |
| 635 | |
| 636 | return tvlv; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * batadv_tvlv_container_list_size - calculate the size of the tvlv container |
| 641 | * list entries |
| 642 | * @bat_priv: the bat priv with all the soft interface information |
| 643 | * |
| 644 | * Has to be called with the appropriate locks being acquired |
| 645 | * (tvlv.container_list_lock). |
| 646 | * |
| 647 | * Returns size of all currently registered tvlv containers in bytes. |
| 648 | */ |
| 649 | static uint16_t batadv_tvlv_container_list_size(struct batadv_priv *bat_priv) |
| 650 | { |
| 651 | struct batadv_tvlv_container *tvlv; |
| 652 | uint16_t tvlv_len = 0; |
| 653 | |
| 654 | hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) { |
| 655 | tvlv_len += sizeof(struct batadv_tvlv_hdr); |
| 656 | tvlv_len += ntohs(tvlv->tvlv_hdr.len); |
| 657 | } |
| 658 | |
| 659 | return tvlv_len; |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * batadv_tvlv_container_remove - remove tvlv container from the tvlv container |
| 664 | * list |
| 665 | * @tvlv: the to be removed tvlv container |
| 666 | * |
| 667 | * Has to be called with the appropriate locks being acquired |
| 668 | * (tvlv.container_list_lock). |
| 669 | */ |
| 670 | static void batadv_tvlv_container_remove(struct batadv_tvlv_container *tvlv) |
| 671 | { |
| 672 | if (!tvlv) |
| 673 | return; |
| 674 | |
| 675 | hlist_del(&tvlv->list); |
| 676 | |
| 677 | /* first call to decrement the counter, second call to free */ |
| 678 | batadv_tvlv_container_free_ref(tvlv); |
| 679 | batadv_tvlv_container_free_ref(tvlv); |
| 680 | } |
| 681 | |
| 682 | /** |
| 683 | * batadv_tvlv_container_unregister - unregister tvlv container based on the |
| 684 | * provided type and version (both need to match) |
| 685 | * @bat_priv: the bat priv with all the soft interface information |
| 686 | * @type: tvlv container type to unregister |
| 687 | * @version: tvlv container type to unregister |
| 688 | */ |
| 689 | void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv, |
| 690 | uint8_t type, uint8_t version) |
| 691 | { |
| 692 | struct batadv_tvlv_container *tvlv; |
| 693 | |
| 694 | spin_lock_bh(&bat_priv->tvlv.container_list_lock); |
| 695 | tvlv = batadv_tvlv_container_get(bat_priv, type, version); |
| 696 | batadv_tvlv_container_remove(tvlv); |
| 697 | spin_unlock_bh(&bat_priv->tvlv.container_list_lock); |
| 698 | } |
| 699 | |
| 700 | /** |
| 701 | * batadv_tvlv_container_register - register tvlv type, version and content |
| 702 | * to be propagated with each (primary interface) OGM |
| 703 | * @bat_priv: the bat priv with all the soft interface information |
| 704 | * @type: tvlv container type |
| 705 | * @version: tvlv container version |
| 706 | * @tvlv_value: tvlv container content |
| 707 | * @tvlv_value_len: tvlv container content length |
| 708 | * |
| 709 | * If a container of the same type and version was already registered the new |
| 710 | * content is going to replace the old one. |
| 711 | */ |
| 712 | void batadv_tvlv_container_register(struct batadv_priv *bat_priv, |
| 713 | uint8_t type, uint8_t version, |
| 714 | void *tvlv_value, uint16_t tvlv_value_len) |
| 715 | { |
| 716 | struct batadv_tvlv_container *tvlv_old, *tvlv_new; |
| 717 | |
| 718 | if (!tvlv_value) |
| 719 | tvlv_value_len = 0; |
| 720 | |
| 721 | tvlv_new = kzalloc(sizeof(*tvlv_new) + tvlv_value_len, GFP_ATOMIC); |
| 722 | if (!tvlv_new) |
| 723 | return; |
| 724 | |
| 725 | tvlv_new->tvlv_hdr.version = version; |
| 726 | tvlv_new->tvlv_hdr.type = type; |
| 727 | tvlv_new->tvlv_hdr.len = htons(tvlv_value_len); |
| 728 | |
| 729 | memcpy(tvlv_new + 1, tvlv_value, ntohs(tvlv_new->tvlv_hdr.len)); |
| 730 | INIT_HLIST_NODE(&tvlv_new->list); |
| 731 | atomic_set(&tvlv_new->refcount, 1); |
| 732 | |
| 733 | spin_lock_bh(&bat_priv->tvlv.container_list_lock); |
| 734 | tvlv_old = batadv_tvlv_container_get(bat_priv, type, version); |
| 735 | batadv_tvlv_container_remove(tvlv_old); |
| 736 | hlist_add_head(&tvlv_new->list, &bat_priv->tvlv.container_list); |
| 737 | spin_unlock_bh(&bat_priv->tvlv.container_list_lock); |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * batadv_tvlv_realloc_packet_buff - reallocate packet buffer to accomodate |
| 742 | * requested packet size |
| 743 | * @packet_buff: packet buffer |
| 744 | * @packet_buff_len: packet buffer size |
| 745 | * @packet_min_len: requested packet minimum size |
| 746 | * @additional_packet_len: requested additional packet size on top of minimum |
| 747 | * size |
| 748 | * |
| 749 | * Returns true of the packet buffer could be changed to the requested size, |
| 750 | * false otherwise. |
| 751 | */ |
| 752 | static bool batadv_tvlv_realloc_packet_buff(unsigned char **packet_buff, |
| 753 | int *packet_buff_len, |
| 754 | int min_packet_len, |
| 755 | int additional_packet_len) |
| 756 | { |
| 757 | unsigned char *new_buff; |
| 758 | |
| 759 | new_buff = kmalloc(min_packet_len + additional_packet_len, GFP_ATOMIC); |
| 760 | |
| 761 | /* keep old buffer if kmalloc should fail */ |
| 762 | if (new_buff) { |
| 763 | memcpy(new_buff, *packet_buff, min_packet_len); |
| 764 | kfree(*packet_buff); |
| 765 | *packet_buff = new_buff; |
| 766 | *packet_buff_len = min_packet_len + additional_packet_len; |
| 767 | return true; |
| 768 | } |
| 769 | |
| 770 | return false; |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * batadv_tvlv_container_ogm_append - append tvlv container content to given |
| 775 | * OGM packet buffer |
| 776 | * @bat_priv: the bat priv with all the soft interface information |
| 777 | * @packet_buff: ogm packet buffer |
| 778 | * @packet_buff_len: ogm packet buffer size including ogm header and tvlv |
| 779 | * content |
| 780 | * @packet_min_len: ogm header size to be preserved for the OGM itself |
| 781 | * |
| 782 | * The ogm packet might be enlarged or shrunk depending on the current size |
| 783 | * and the size of the to-be-appended tvlv containers. |
| 784 | * |
| 785 | * Returns size of all appended tvlv containers in bytes. |
| 786 | */ |
| 787 | uint16_t batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv, |
| 788 | unsigned char **packet_buff, |
| 789 | int *packet_buff_len, |
| 790 | int packet_min_len) |
| 791 | { |
| 792 | struct batadv_tvlv_container *tvlv; |
| 793 | struct batadv_tvlv_hdr *tvlv_hdr; |
| 794 | uint16_t tvlv_value_len; |
| 795 | void *tvlv_value; |
| 796 | bool ret; |
| 797 | |
| 798 | spin_lock_bh(&bat_priv->tvlv.container_list_lock); |
| 799 | tvlv_value_len = batadv_tvlv_container_list_size(bat_priv); |
| 800 | |
| 801 | ret = batadv_tvlv_realloc_packet_buff(packet_buff, packet_buff_len, |
| 802 | packet_min_len, tvlv_value_len); |
| 803 | |
| 804 | if (!ret) |
| 805 | goto end; |
| 806 | |
| 807 | if (!tvlv_value_len) |
| 808 | goto end; |
| 809 | |
| 810 | tvlv_value = (*packet_buff) + packet_min_len; |
| 811 | |
| 812 | hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) { |
| 813 | tvlv_hdr = tvlv_value; |
| 814 | tvlv_hdr->type = tvlv->tvlv_hdr.type; |
| 815 | tvlv_hdr->version = tvlv->tvlv_hdr.version; |
| 816 | tvlv_hdr->len = tvlv->tvlv_hdr.len; |
| 817 | tvlv_value = tvlv_hdr + 1; |
| 818 | memcpy(tvlv_value, tvlv + 1, ntohs(tvlv->tvlv_hdr.len)); |
| 819 | tvlv_value = (uint8_t *)tvlv_value + ntohs(tvlv->tvlv_hdr.len); |
| 820 | } |
| 821 | |
| 822 | end: |
| 823 | spin_unlock_bh(&bat_priv->tvlv.container_list_lock); |
| 824 | return tvlv_value_len; |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * batadv_tvlv_call_handler - parse the given tvlv buffer to call the |
| 829 | * appropriate handlers |
| 830 | * @bat_priv: the bat priv with all the soft interface information |
| 831 | * @tvlv_handler: tvlv callback function handling the tvlv content |
| 832 | * @ogm_source: flag indicating wether the tvlv is an ogm or a unicast packet |
| 833 | * @orig_node: orig node emitting the ogm packet |
| 834 | * @src: source mac address of the unicast packet |
| 835 | * @dst: destination mac address of the unicast packet |
| 836 | * @tvlv_value: tvlv content |
| 837 | * @tvlv_value_len: tvlv content length |
| 838 | * |
| 839 | * Returns success if handler was not found or the return value of the handler |
| 840 | * callback. |
| 841 | */ |
| 842 | static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv, |
| 843 | struct batadv_tvlv_handler *tvlv_handler, |
| 844 | bool ogm_source, |
| 845 | struct batadv_orig_node *orig_node, |
| 846 | uint8_t *src, uint8_t *dst, |
| 847 | void *tvlv_value, uint16_t tvlv_value_len) |
| 848 | { |
| 849 | if (!tvlv_handler) |
| 850 | return NET_RX_SUCCESS; |
| 851 | |
| 852 | if (ogm_source) { |
| 853 | if (!tvlv_handler->ogm_handler) |
| 854 | return NET_RX_SUCCESS; |
| 855 | |
| 856 | if (!orig_node) |
| 857 | return NET_RX_SUCCESS; |
| 858 | |
| 859 | tvlv_handler->ogm_handler(bat_priv, orig_node, |
| 860 | BATADV_NO_FLAGS, |
| 861 | tvlv_value, tvlv_value_len); |
| 862 | tvlv_handler->flags |= BATADV_TVLV_HANDLER_OGM_CALLED; |
| 863 | } else { |
| 864 | if (!src) |
| 865 | return NET_RX_SUCCESS; |
| 866 | |
| 867 | if (!dst) |
| 868 | return NET_RX_SUCCESS; |
| 869 | |
| 870 | if (!tvlv_handler->unicast_handler) |
| 871 | return NET_RX_SUCCESS; |
| 872 | |
| 873 | return tvlv_handler->unicast_handler(bat_priv, src, |
| 874 | dst, tvlv_value, |
| 875 | tvlv_value_len); |
| 876 | } |
| 877 | |
| 878 | return NET_RX_SUCCESS; |
| 879 | } |
| 880 | |
| 881 | /** |
| 882 | * batadv_tvlv_containers_process - parse the given tvlv buffer to call the |
| 883 | * appropriate handlers |
| 884 | * @bat_priv: the bat priv with all the soft interface information |
| 885 | * @ogm_source: flag indicating wether the tvlv is an ogm or a unicast packet |
| 886 | * @orig_node: orig node emitting the ogm packet |
| 887 | * @src: source mac address of the unicast packet |
| 888 | * @dst: destination mac address of the unicast packet |
| 889 | * @tvlv_value: tvlv content |
| 890 | * @tvlv_value_len: tvlv content length |
| 891 | * |
| 892 | * Returns success when processing an OGM or the return value of all called |
| 893 | * handler callbacks. |
| 894 | */ |
| 895 | int batadv_tvlv_containers_process(struct batadv_priv *bat_priv, |
| 896 | bool ogm_source, |
| 897 | struct batadv_orig_node *orig_node, |
| 898 | uint8_t *src, uint8_t *dst, |
| 899 | void *tvlv_value, uint16_t tvlv_value_len) |
| 900 | { |
| 901 | struct batadv_tvlv_handler *tvlv_handler; |
| 902 | struct batadv_tvlv_hdr *tvlv_hdr; |
| 903 | uint16_t tvlv_value_cont_len; |
| 904 | uint8_t cifnotfound = BATADV_TVLV_HANDLER_OGM_CIFNOTFND; |
| 905 | int ret = NET_RX_SUCCESS; |
| 906 | |
| 907 | while (tvlv_value_len >= sizeof(*tvlv_hdr)) { |
| 908 | tvlv_hdr = tvlv_value; |
| 909 | tvlv_value_cont_len = ntohs(tvlv_hdr->len); |
| 910 | tvlv_value = tvlv_hdr + 1; |
| 911 | tvlv_value_len -= sizeof(*tvlv_hdr); |
| 912 | |
| 913 | if (tvlv_value_cont_len > tvlv_value_len) |
| 914 | break; |
| 915 | |
| 916 | tvlv_handler = batadv_tvlv_handler_get(bat_priv, |
| 917 | tvlv_hdr->type, |
| 918 | tvlv_hdr->version); |
| 919 | |
| 920 | ret |= batadv_tvlv_call_handler(bat_priv, tvlv_handler, |
| 921 | ogm_source, orig_node, |
| 922 | src, dst, tvlv_value, |
| 923 | tvlv_value_cont_len); |
| 924 | if (tvlv_handler) |
| 925 | batadv_tvlv_handler_free_ref(tvlv_handler); |
| 926 | tvlv_value = (uint8_t *)tvlv_value + tvlv_value_cont_len; |
| 927 | tvlv_value_len -= tvlv_value_cont_len; |
| 928 | } |
| 929 | |
| 930 | if (!ogm_source) |
| 931 | return ret; |
| 932 | |
| 933 | rcu_read_lock(); |
| 934 | hlist_for_each_entry_rcu(tvlv_handler, |
| 935 | &bat_priv->tvlv.handler_list, list) { |
| 936 | if ((tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) && |
| 937 | !(tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CALLED)) |
| 938 | tvlv_handler->ogm_handler(bat_priv, orig_node, |
| 939 | cifnotfound, NULL, 0); |
| 940 | |
| 941 | tvlv_handler->flags &= ~BATADV_TVLV_HANDLER_OGM_CALLED; |
| 942 | } |
| 943 | rcu_read_unlock(); |
| 944 | |
| 945 | return NET_RX_SUCCESS; |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * batadv_tvlv_ogm_receive - process an incoming ogm and call the appropriate |
| 950 | * handlers |
| 951 | * @bat_priv: the bat priv with all the soft interface information |
| 952 | * @batadv_ogm_packet: ogm packet containing the tvlv containers |
| 953 | * @orig_node: orig node emitting the ogm packet |
| 954 | */ |
| 955 | void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv, |
| 956 | struct batadv_ogm_packet *batadv_ogm_packet, |
| 957 | struct batadv_orig_node *orig_node) |
| 958 | { |
| 959 | void *tvlv_value; |
| 960 | uint16_t tvlv_value_len; |
| 961 | |
| 962 | if (!batadv_ogm_packet) |
| 963 | return; |
| 964 | |
| 965 | tvlv_value_len = ntohs(batadv_ogm_packet->tvlv_len); |
| 966 | if (!tvlv_value_len) |
| 967 | return; |
| 968 | |
| 969 | tvlv_value = batadv_ogm_packet + 1; |
| 970 | |
| 971 | batadv_tvlv_containers_process(bat_priv, true, orig_node, NULL, NULL, |
| 972 | tvlv_value, tvlv_value_len); |
| 973 | } |
| 974 | |
| 975 | /** |
| 976 | * batadv_tvlv_handler_register - register tvlv handler based on the provided |
| 977 | * type and version (both need to match) for ogm tvlv payload and/or unicast |
| 978 | * payload |
| 979 | * @bat_priv: the bat priv with all the soft interface information |
| 980 | * @optr: ogm tvlv handler callback function. This function receives the orig |
| 981 | * node, flags and the tvlv content as argument to process. |
| 982 | * @uptr: unicast tvlv handler callback function. This function receives the |
| 983 | * source & destination of the unicast packet as well as the tvlv content |
| 984 | * to process. |
| 985 | * @type: tvlv handler type to be registered |
| 986 | * @version: tvlv handler version to be registered |
| 987 | * @flags: flags to enable or disable TVLV API behavior |
| 988 | */ |
| 989 | void batadv_tvlv_handler_register(struct batadv_priv *bat_priv, |
| 990 | void (*optr)(struct batadv_priv *bat_priv, |
| 991 | struct batadv_orig_node *orig, |
| 992 | uint8_t flags, |
| 993 | void *tvlv_value, |
| 994 | uint16_t tvlv_value_len), |
| 995 | int (*uptr)(struct batadv_priv *bat_priv, |
| 996 | uint8_t *src, uint8_t *dst, |
| 997 | void *tvlv_value, |
| 998 | uint16_t tvlv_value_len), |
| 999 | uint8_t type, uint8_t version, uint8_t flags) |
| 1000 | { |
| 1001 | struct batadv_tvlv_handler *tvlv_handler; |
| 1002 | |
| 1003 | tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version); |
| 1004 | if (tvlv_handler) { |
| 1005 | batadv_tvlv_handler_free_ref(tvlv_handler); |
| 1006 | return; |
| 1007 | } |
| 1008 | |
| 1009 | tvlv_handler = kzalloc(sizeof(*tvlv_handler), GFP_ATOMIC); |
| 1010 | if (!tvlv_handler) |
| 1011 | return; |
| 1012 | |
| 1013 | tvlv_handler->ogm_handler = optr; |
| 1014 | tvlv_handler->unicast_handler = uptr; |
| 1015 | tvlv_handler->type = type; |
| 1016 | tvlv_handler->version = version; |
| 1017 | tvlv_handler->flags = flags; |
| 1018 | atomic_set(&tvlv_handler->refcount, 1); |
| 1019 | INIT_HLIST_NODE(&tvlv_handler->list); |
| 1020 | |
| 1021 | spin_lock_bh(&bat_priv->tvlv.handler_list_lock); |
| 1022 | hlist_add_head_rcu(&tvlv_handler->list, &bat_priv->tvlv.handler_list); |
| 1023 | spin_unlock_bh(&bat_priv->tvlv.handler_list_lock); |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * batadv_tvlv_handler_unregister - unregister tvlv handler based on the |
| 1028 | * provided type and version (both need to match) |
| 1029 | * @bat_priv: the bat priv with all the soft interface information |
| 1030 | * @type: tvlv handler type to be unregistered |
| 1031 | * @version: tvlv handler version to be unregistered |
| 1032 | */ |
| 1033 | void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv, |
| 1034 | uint8_t type, uint8_t version) |
| 1035 | { |
| 1036 | struct batadv_tvlv_handler *tvlv_handler; |
| 1037 | |
| 1038 | tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version); |
| 1039 | if (!tvlv_handler) |
| 1040 | return; |
| 1041 | |
| 1042 | batadv_tvlv_handler_free_ref(tvlv_handler); |
| 1043 | spin_lock_bh(&bat_priv->tvlv.handler_list_lock); |
| 1044 | hlist_del_rcu(&tvlv_handler->list); |
| 1045 | spin_unlock_bh(&bat_priv->tvlv.handler_list_lock); |
| 1046 | batadv_tvlv_handler_free_ref(tvlv_handler); |
| 1047 | } |
| 1048 | |
| 1049 | /** |
| 1050 | * batadv_tvlv_unicast_send - send a unicast packet with tvlv payload to the |
| 1051 | * specified host |
| 1052 | * @bat_priv: the bat priv with all the soft interface information |
| 1053 | * @src: source mac address of the unicast packet |
| 1054 | * @dst: destination mac address of the unicast packet |
| 1055 | * @type: tvlv type |
| 1056 | * @version: tvlv version |
| 1057 | * @tvlv_value: tvlv content |
| 1058 | * @tvlv_value_len: tvlv content length |
| 1059 | */ |
| 1060 | void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, uint8_t *src, |
| 1061 | uint8_t *dst, uint8_t type, uint8_t version, |
| 1062 | void *tvlv_value, uint16_t tvlv_value_len) |
| 1063 | { |
| 1064 | struct batadv_unicast_tvlv_packet *unicast_tvlv_packet; |
| 1065 | struct batadv_tvlv_hdr *tvlv_hdr; |
| 1066 | struct batadv_orig_node *orig_node; |
| 1067 | struct sk_buff *skb = NULL; |
| 1068 | unsigned char *tvlv_buff; |
| 1069 | unsigned int tvlv_len; |
| 1070 | ssize_t hdr_len = sizeof(*unicast_tvlv_packet); |
| 1071 | bool ret = false; |
| 1072 | |
| 1073 | orig_node = batadv_orig_hash_find(bat_priv, dst); |
| 1074 | if (!orig_node) |
| 1075 | goto out; |
| 1076 | |
| 1077 | tvlv_len = sizeof(*tvlv_hdr) + tvlv_value_len; |
| 1078 | |
| 1079 | skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + hdr_len + tvlv_len); |
| 1080 | if (!skb) |
| 1081 | goto out; |
| 1082 | |
| 1083 | skb->priority = TC_PRIO_CONTROL; |
| 1084 | skb_reserve(skb, ETH_HLEN); |
| 1085 | tvlv_buff = skb_put(skb, sizeof(*unicast_tvlv_packet) + tvlv_len); |
| 1086 | unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)tvlv_buff; |
| 1087 | unicast_tvlv_packet->header.packet_type = BATADV_UNICAST_TVLV; |
| 1088 | unicast_tvlv_packet->header.version = BATADV_COMPAT_VERSION; |
| 1089 | unicast_tvlv_packet->header.ttl = BATADV_TTL; |
| 1090 | unicast_tvlv_packet->reserved = 0; |
| 1091 | unicast_tvlv_packet->tvlv_len = htons(tvlv_len); |
| 1092 | unicast_tvlv_packet->align = 0; |
| 1093 | memcpy(unicast_tvlv_packet->src, src, ETH_ALEN); |
| 1094 | memcpy(unicast_tvlv_packet->dst, dst, ETH_ALEN); |
| 1095 | |
| 1096 | tvlv_buff = (unsigned char *)(unicast_tvlv_packet + 1); |
| 1097 | tvlv_hdr = (struct batadv_tvlv_hdr *)tvlv_buff; |
| 1098 | tvlv_hdr->version = version; |
| 1099 | tvlv_hdr->type = type; |
| 1100 | tvlv_hdr->len = htons(tvlv_value_len); |
| 1101 | tvlv_buff += sizeof(*tvlv_hdr); |
| 1102 | memcpy(tvlv_buff, tvlv_value, tvlv_value_len); |
| 1103 | |
| 1104 | if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP) |
| 1105 | ret = true; |
| 1106 | |
| 1107 | out: |
| 1108 | if (skb && !ret) |
| 1109 | kfree_skb(skb); |
| 1110 | if (orig_node) |
| 1111 | batadv_orig_node_free_ref(orig_node); |
| 1112 | } |
| 1113 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 1114 | static int batadv_param_set_ra(const char *val, const struct kernel_param *kp) |
Marek Lindner | d419be1 | 2011-12-10 19:45:53 +0800 | [diff] [blame] | 1115 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1116 | struct batadv_algo_ops *bat_algo_ops; |
Marek Lindner | d8cb5486 | 2012-04-18 17:16:39 +0800 | [diff] [blame] | 1117 | char *algo_name = (char *)val; |
| 1118 | size_t name_len = strlen(algo_name); |
Marek Lindner | d419be1 | 2011-12-10 19:45:53 +0800 | [diff] [blame] | 1119 | |
Marek Lindner | 293c9c1 | 2013-04-27 16:22:28 +0800 | [diff] [blame] | 1120 | if (name_len > 0 && algo_name[name_len - 1] == '\n') |
Marek Lindner | d8cb5486 | 2012-04-18 17:16:39 +0800 | [diff] [blame] | 1121 | algo_name[name_len - 1] = '\0'; |
| 1122 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 1123 | bat_algo_ops = batadv_algo_get(algo_name); |
Marek Lindner | d419be1 | 2011-12-10 19:45:53 +0800 | [diff] [blame] | 1124 | if (!bat_algo_ops) { |
Marek Lindner | d8cb5486 | 2012-04-18 17:16:39 +0800 | [diff] [blame] | 1125 | pr_err("Routing algorithm '%s' is not supported\n", algo_name); |
Marek Lindner | d419be1 | 2011-12-10 19:45:53 +0800 | [diff] [blame] | 1126 | return -EINVAL; |
| 1127 | } |
| 1128 | |
Marek Lindner | d8cb5486 | 2012-04-18 17:16:39 +0800 | [diff] [blame] | 1129 | return param_set_copystring(algo_name, kp); |
Marek Lindner | d419be1 | 2011-12-10 19:45:53 +0800 | [diff] [blame] | 1130 | } |
| 1131 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 1132 | static const struct kernel_param_ops batadv_param_ops_ra = { |
| 1133 | .set = batadv_param_set_ra, |
Marek Lindner | d419be1 | 2011-12-10 19:45:53 +0800 | [diff] [blame] | 1134 | .get = param_get_string, |
| 1135 | }; |
| 1136 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 1137 | static struct kparam_string batadv_param_string_ra = { |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 1138 | .maxlen = sizeof(batadv_routing_algo), |
| 1139 | .string = batadv_routing_algo, |
Marek Lindner | d419be1 | 2011-12-10 19:45:53 +0800 | [diff] [blame] | 1140 | }; |
| 1141 | |
Sven Eckelmann | ee11ad6 | 2012-05-16 20:23:19 +0200 | [diff] [blame] | 1142 | module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra, |
| 1143 | 0644); |
| 1144 | module_init(batadv_init); |
| 1145 | module_exit(batadv_exit); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1146 | |
| 1147 | MODULE_LICENSE("GPL"); |
| 1148 | |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1149 | MODULE_AUTHOR(BATADV_DRIVER_AUTHOR); |
| 1150 | MODULE_DESCRIPTION(BATADV_DRIVER_DESC); |
| 1151 | MODULE_SUPPORTED_DEVICE(BATADV_DRIVER_DEVICE); |
| 1152 | MODULE_VERSION(BATADV_SOURCE_VERSION); |