Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2002-2005, Instant802 Networks, Inc. |
| 3 | * Copyright 2005-2006, Devicescape Software, Inc. |
| 4 | * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/if_arp.h> |
| 12 | #include <linux/netdevice.h> |
| 13 | #include <linux/rtnetlink.h> |
| 14 | #include <net/mac80211.h> |
| 15 | #include "ieee80211_i.h" |
| 16 | #include "sta_info.h" |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 17 | #include "debugfs_netdev.h" |
Luis Carlos Cobo | ee38585 | 2008-02-23 15:17:11 +0100 | [diff] [blame] | 18 | #include "mesh.h" |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 19 | |
| 20 | void ieee80211_if_sdata_init(struct ieee80211_sub_if_data *sdata) |
| 21 | { |
| 22 | int i; |
| 23 | |
| 24 | /* Default values for sub-interface parameters */ |
| 25 | sdata->drop_unencrypted = 0; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 26 | for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) |
| 27 | skb_queue_head_init(&sdata->fragments[i].skb_list); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 28 | |
| 29 | INIT_LIST_HEAD(&sdata->key_list); |
Johannes Berg | 3e122be | 2008-07-09 14:40:34 +0200 | [diff] [blame^] | 30 | |
| 31 | sdata->force_unicast_rateidx = -1; |
| 32 | sdata->max_ratectrl_rateidx = -1; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | static void ieee80211_if_sdata_deinit(struct ieee80211_sub_if_data *sdata) |
| 36 | { |
| 37 | int i; |
| 38 | |
Johannes Berg | 988c0f7 | 2008-04-17 19:21:22 +0200 | [diff] [blame] | 39 | for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 40 | __skb_queue_purge(&sdata->fragments[i].skb_list); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* Must be called with rtnl lock held. */ |
Johannes Berg | 3e122be | 2008-07-09 14:40:34 +0200 | [diff] [blame^] | 44 | int ieee80211_if_add(struct ieee80211_local *local, const char *name, |
Luis Carlos Cobo | ee38585 | 2008-02-23 15:17:11 +0100 | [diff] [blame] | 45 | struct net_device **new_dev, int type, |
| 46 | struct vif_params *params) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 47 | { |
| 48 | struct net_device *ndev; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 49 | struct ieee80211_sub_if_data *sdata = NULL; |
| 50 | int ret; |
| 51 | |
| 52 | ASSERT_RTNL(); |
Johannes Berg | 32bfd35 | 2007-12-19 01:31:26 +0100 | [diff] [blame] | 53 | ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size, |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 54 | name, ieee80211_if_setup); |
| 55 | if (!ndev) |
| 56 | return -ENOMEM; |
| 57 | |
Johannes Berg | f3994ec | 2008-05-12 20:51:44 -0700 | [diff] [blame] | 58 | ndev->needed_headroom = local->tx_headroom + |
| 59 | 4*6 /* four MAC addresses */ |
| 60 | + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */ |
| 61 | + 6 /* mesh */ |
| 62 | + 8 /* rfc1042/bridge tunnel */ |
| 63 | - ETH_HLEN /* ethernet hard_header_len */ |
| 64 | + IEEE80211_ENCRYPT_HEADROOM; |
| 65 | ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM; |
| 66 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 67 | ret = dev_alloc_name(ndev, ndev->name); |
| 68 | if (ret < 0) |
| 69 | goto fail; |
| 70 | |
| 71 | memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 72 | SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy)); |
| 73 | |
Johannes Berg | 3e122be | 2008-07-09 14:40:34 +0200 | [diff] [blame^] | 74 | /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */ |
| 75 | sdata = netdev_priv(ndev); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 76 | ndev->ieee80211_ptr = &sdata->wdev; |
| 77 | sdata->wdev.wiphy = local->hw.wiphy; |
Johannes Berg | 51fb61e | 2007-12-19 01:31:27 +0100 | [diff] [blame] | 78 | sdata->vif.type = IEEE80211_IF_TYPE_AP; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 79 | sdata->dev = ndev; |
| 80 | sdata->local = local; |
| 81 | ieee80211_if_sdata_init(sdata); |
| 82 | |
| 83 | ret = register_netdevice(ndev); |
| 84 | if (ret) |
| 85 | goto fail; |
| 86 | |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 87 | ieee80211_debugfs_add_netdev(sdata); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 88 | ieee80211_if_set_type(ndev, type); |
| 89 | |
Johannes Berg | 902acc7 | 2008-02-23 15:17:19 +0100 | [diff] [blame] | 90 | if (ieee80211_vif_is_mesh(&sdata->vif) && |
| 91 | params && params->mesh_id_len) |
| 92 | ieee80211_if_sta_set_mesh_id(&sdata->u.sta, |
| 93 | params->mesh_id_len, |
| 94 | params->mesh_id); |
Luis Carlos Cobo | ee38585 | 2008-02-23 15:17:11 +0100 | [diff] [blame] | 95 | |
Johannes Berg | 7901042 | 2007-09-18 17:29:21 -0400 | [diff] [blame] | 96 | /* we're under RTNL so all this is fine */ |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 97 | if (unlikely(local->reg_state == IEEE80211_DEV_UNREGISTERED)) { |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 98 | __ieee80211_if_del(local, sdata); |
| 99 | return -ENODEV; |
| 100 | } |
Johannes Berg | 7901042 | 2007-09-18 17:29:21 -0400 | [diff] [blame] | 101 | list_add_tail_rcu(&sdata->list, &local->interfaces); |
| 102 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 103 | if (new_dev) |
| 104 | *new_dev = ndev; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 105 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 106 | return 0; |
| 107 | |
| 108 | fail: |
| 109 | free_netdev(ndev); |
| 110 | return ret; |
| 111 | } |
| 112 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 113 | void ieee80211_if_set_type(struct net_device *dev, int type) |
| 114 | { |
| 115 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
Johannes Berg | 51fb61e | 2007-12-19 01:31:27 +0100 | [diff] [blame] | 116 | int oldtype = sdata->vif.type; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 117 | |
Johannes Berg | 5b2812e | 2007-09-26 14:27:23 +0200 | [diff] [blame] | 118 | /* |
Johannes Berg | 5b2812e | 2007-09-26 14:27:23 +0200 | [diff] [blame] | 119 | * Called even when register_netdevice fails, it would |
| 120 | * oops if assigned before initialising the rest. |
| 121 | */ |
| 122 | dev->uninit = ieee80211_if_reinit; |
| 123 | |
| 124 | /* most have no BSS pointer */ |
| 125 | sdata->bss = NULL; |
Johannes Berg | 51fb61e | 2007-12-19 01:31:27 +0100 | [diff] [blame] | 126 | sdata->vif.type = type; |
Johannes Berg | 5b2812e | 2007-09-26 14:27:23 +0200 | [diff] [blame] | 127 | |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 128 | sdata->basic_rates = 0; |
| 129 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 130 | switch (type) { |
| 131 | case IEEE80211_IF_TYPE_WDS: |
Johannes Berg | 3e122be | 2008-07-09 14:40:34 +0200 | [diff] [blame^] | 132 | case IEEE80211_IF_TYPE_VLAN: |
Johannes Berg | 5b2812e | 2007-09-26 14:27:23 +0200 | [diff] [blame] | 133 | /* nothing special */ |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 134 | break; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 135 | case IEEE80211_IF_TYPE_AP: |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 136 | skb_queue_head_init(&sdata->u.ap.ps_bc_buf); |
Johannes Berg | 0ec3ca4 | 2007-09-17 01:29:24 -0400 | [diff] [blame] | 137 | INIT_LIST_HEAD(&sdata->u.ap.vlans); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 138 | break; |
Luis Carlos Cobo | ee38585 | 2008-02-23 15:17:11 +0100 | [diff] [blame] | 139 | case IEEE80211_IF_TYPE_MESH_POINT: |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 140 | case IEEE80211_IF_TYPE_STA: |
| 141 | case IEEE80211_IF_TYPE_IBSS: { |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 142 | struct ieee80211_if_sta *ifsta; |
| 143 | |
| 144 | ifsta = &sdata->u.sta; |
| 145 | INIT_WORK(&ifsta->work, ieee80211_sta_work); |
| 146 | setup_timer(&ifsta->timer, ieee80211_sta_timer, |
| 147 | (unsigned long) sdata); |
| 148 | skb_queue_head_init(&ifsta->skb_queue); |
| 149 | |
| 150 | ifsta->capab = WLAN_CAPABILITY_ESS; |
| 151 | ifsta->auth_algs = IEEE80211_AUTH_ALG_OPEN | |
| 152 | IEEE80211_AUTH_ALG_SHARED_KEY; |
Jiri Slaby | d6f2da5 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 153 | ifsta->flags |= IEEE80211_STA_CREATE_IBSS | |
Jiri Slaby | d6f2da5 | 2007-08-28 17:01:54 -0400 | [diff] [blame] | 154 | IEEE80211_STA_AUTO_BSSID_SEL | |
| 155 | IEEE80211_STA_AUTO_CHANNEL_SEL; |
Johannes Berg | e253008 | 2008-05-17 00:57:14 +0200 | [diff] [blame] | 156 | if (ieee80211_num_regular_queues(&sdata->local->hw) >= 4) |
Johannes Berg | 3434fbd | 2008-05-03 00:59:37 +0200 | [diff] [blame] | 157 | ifsta->flags |= IEEE80211_STA_WMM_ENABLED; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 158 | |
Johannes Berg | 902acc7 | 2008-02-23 15:17:19 +0100 | [diff] [blame] | 159 | if (ieee80211_vif_is_mesh(&sdata->vif)) |
| 160 | ieee80211_mesh_init_sdata(sdata); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 161 | break; |
| 162 | } |
| 163 | case IEEE80211_IF_TYPE_MNTR: |
| 164 | dev->type = ARPHRD_IEEE80211_RADIOTAP; |
Johannes Berg | 40f7cac | 2007-07-10 19:32:08 +0200 | [diff] [blame] | 165 | dev->hard_start_xmit = ieee80211_monitor_start_xmit; |
Michael Wu | 8cc9a73 | 2008-01-31 19:48:23 +0100 | [diff] [blame] | 166 | sdata->u.mntr_flags = MONITOR_FLAG_CONTROL | |
| 167 | MONITOR_FLAG_OTHER_BSS; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 168 | break; |
Johannes Berg | f4ea83d | 2008-06-30 15:10:46 +0200 | [diff] [blame] | 169 | case IEEE80211_IF_TYPE_INVALID: |
| 170 | BUG(); |
| 171 | break; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 172 | } |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 173 | ieee80211_debugfs_change_if_type(sdata, oldtype); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* Must be called with rtnl lock held. */ |
| 177 | void ieee80211_if_reinit(struct net_device *dev) |
| 178 | { |
| 179 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); |
| 180 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
Johannes Berg | 5b2812e | 2007-09-26 14:27:23 +0200 | [diff] [blame] | 181 | struct sk_buff *skb; |
Johannes Berg | 44213b5 | 2008-02-25 16:27:49 +0100 | [diff] [blame] | 182 | int flushed; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 183 | |
| 184 | ASSERT_RTNL(); |
Johannes Berg | 11a843b | 2007-08-28 17:01:55 -0400 | [diff] [blame] | 185 | |
| 186 | ieee80211_free_keys(sdata); |
| 187 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 188 | ieee80211_if_sdata_deinit(sdata); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 189 | |
Johannes Berg | 902acc7 | 2008-02-23 15:17:19 +0100 | [diff] [blame] | 190 | /* Need to handle mesh specially to allow eliding the function call */ |
| 191 | if (ieee80211_vif_is_mesh(&sdata->vif)) |
| 192 | mesh_rmc_free(dev); |
| 193 | |
Johannes Berg | 51fb61e | 2007-12-19 01:31:27 +0100 | [diff] [blame] | 194 | switch (sdata->vif.type) { |
Johannes Berg | a289755 | 2007-09-28 14:01:25 +0200 | [diff] [blame] | 195 | case IEEE80211_IF_TYPE_INVALID: |
| 196 | /* cannot happen */ |
| 197 | WARN_ON(1); |
| 198 | break; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 199 | case IEEE80211_IF_TYPE_AP: { |
Johannes Berg | e6a5ddf | 2008-02-25 16:27:42 +0100 | [diff] [blame] | 200 | struct beacon_data *beacon; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 201 | |
Johannes Berg | e6a5ddf | 2008-02-25 16:27:42 +0100 | [diff] [blame] | 202 | beacon = sdata->u.ap.beacon; |
| 203 | rcu_assign_pointer(sdata->u.ap.beacon, NULL); |
| 204 | synchronize_rcu(); |
| 205 | kfree(beacon); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 206 | |
Johannes Berg | 5b2812e | 2007-09-26 14:27:23 +0200 | [diff] [blame] | 207 | while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) { |
| 208 | local->total_ps_buffered--; |
| 209 | dev_kfree_skb(skb); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | break; |
| 213 | } |
| 214 | case IEEE80211_IF_TYPE_WDS: |
Johannes Berg | 3e122be | 2008-07-09 14:40:34 +0200 | [diff] [blame^] | 215 | case IEEE80211_IF_TYPE_VLAN: |
Johannes Berg | 44213b5 | 2008-02-25 16:27:49 +0100 | [diff] [blame] | 216 | /* nothing to do */ |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 217 | break; |
Johannes Berg | 6032f93 | 2008-02-23 15:17:07 +0100 | [diff] [blame] | 218 | case IEEE80211_IF_TYPE_MESH_POINT: |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 219 | case IEEE80211_IF_TYPE_STA: |
| 220 | case IEEE80211_IF_TYPE_IBSS: |
| 221 | kfree(sdata->u.sta.extra_ie); |
| 222 | sdata->u.sta.extra_ie = NULL; |
| 223 | kfree(sdata->u.sta.assocreq_ies); |
| 224 | sdata->u.sta.assocreq_ies = NULL; |
| 225 | kfree(sdata->u.sta.assocresp_ies); |
| 226 | sdata->u.sta.assocresp_ies = NULL; |
| 227 | if (sdata->u.sta.probe_resp) { |
| 228 | dev_kfree_skb(sdata->u.sta.probe_resp); |
| 229 | sdata->u.sta.probe_resp = NULL; |
| 230 | } |
| 231 | |
| 232 | break; |
| 233 | case IEEE80211_IF_TYPE_MNTR: |
| 234 | dev->type = ARPHRD_ETHER; |
| 235 | break; |
| 236 | } |
| 237 | |
Johannes Berg | 44213b5 | 2008-02-25 16:27:49 +0100 | [diff] [blame] | 238 | flushed = sta_info_flush(local, sdata); |
| 239 | WARN_ON(flushed); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 240 | |
| 241 | memset(&sdata->u, 0, sizeof(sdata->u)); |
| 242 | ieee80211_if_sdata_init(sdata); |
| 243 | } |
| 244 | |
| 245 | /* Must be called with rtnl lock held. */ |
| 246 | void __ieee80211_if_del(struct ieee80211_local *local, |
| 247 | struct ieee80211_sub_if_data *sdata) |
| 248 | { |
| 249 | struct net_device *dev = sdata->dev; |
| 250 | |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 251 | ieee80211_debugfs_remove_netdev(sdata); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 252 | unregister_netdevice(dev); |
Johannes Berg | 3e122be | 2008-07-09 14:40:34 +0200 | [diff] [blame^] | 253 | /* |
| 254 | * The net_device will be freed by its destructor, |
| 255 | * i.e. ieee80211_if_free. |
| 256 | */ |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* Must be called with rtnl lock held. */ |
| 260 | int ieee80211_if_remove(struct net_device *dev, const char *name, int id) |
| 261 | { |
| 262 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); |
| 263 | struct ieee80211_sub_if_data *sdata, *n; |
| 264 | |
| 265 | ASSERT_RTNL(); |
| 266 | |
Johannes Berg | 7901042 | 2007-09-18 17:29:21 -0400 | [diff] [blame] | 267 | list_for_each_entry_safe(sdata, n, &local->interfaces, list) { |
Johannes Berg | 51fb61e | 2007-12-19 01:31:27 +0100 | [diff] [blame] | 268 | if ((sdata->vif.type == id || id == -1) && |
Johannes Berg | 3e122be | 2008-07-09 14:40:34 +0200 | [diff] [blame^] | 269 | strcmp(name, sdata->dev->name) == 0) { |
Johannes Berg | 7901042 | 2007-09-18 17:29:21 -0400 | [diff] [blame] | 270 | list_del_rcu(&sdata->list); |
| 271 | synchronize_rcu(); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 272 | __ieee80211_if_del(local, sdata); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | } |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 276 | return -ENODEV; |
| 277 | } |
| 278 | |
| 279 | void ieee80211_if_free(struct net_device *dev) |
| 280 | { |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 281 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
| 282 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 283 | ieee80211_if_sdata_deinit(sdata); |
| 284 | free_netdev(dev); |
| 285 | } |