mib.c 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright 2007-2012 Siemens AG
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Written by:
 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
 * Sergey Lapin <slapin@ossfans.org>
 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
 */

#include <linux/if_arp.h>

#include <net/mac802154.h>
23
#include <net/ieee802154_netdev.h>
24
#include <net/cfg802154.h>
25

26
#include "ieee802154_i.h"
27

28 29 30 31 32
struct phy_chan_notify_work {
	struct work_struct work;
	struct net_device *dev;
};

33 34 35 36 37 38
struct hw_addr_filt_notify_work {
	struct work_struct work;
	struct net_device *dev;
	unsigned long changed;
};

39
static struct ieee802154_local *mac802154_slave_get_priv(struct net_device *dev)
40
{
41
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
42 43 44

	BUG_ON(dev->type != ARPHRD_IEEE802154);

45
	return sdata->local;
46 47 48 49 50 51
}

static void hw_addr_notify(struct work_struct *work)
{
	struct hw_addr_filt_notify_work *nw = container_of(work,
			struct hw_addr_filt_notify_work, work);
52
	struct ieee802154_local *local = mac802154_slave_get_priv(nw->dev);
53 54
	int res;

55 56
	res = local->ops->set_hw_addr_filt(&local->hw, &local->hw.hw_filt,
					   nw->changed);
57 58 59 60 61 62 63 64
	if (res)
		pr_debug("failed changed mask %lx\n", nw->changed);

	kfree(nw);
}

static void set_hw_addr_filt(struct net_device *dev, unsigned long changed)
{
65
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
66 67 68 69 70 71 72 73 74
	struct hw_addr_filt_notify_work *work;

	work = kzalloc(sizeof(*work), GFP_ATOMIC);
	if (!work)
		return;

	INIT_WORK(&work->work, hw_addr_notify);
	work->dev = dev;
	work->changed = changed;
75
	queue_work(sdata->local->workqueue, &work->work);
76 77
}

78
void mac802154_dev_set_short_addr(struct net_device *dev, __le16 val)
79
{
80
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
81 82 83

	BUG_ON(dev->type != ARPHRD_IEEE802154);

84 85 86
	spin_lock_bh(&sdata->mib_lock);
	sdata->short_addr = val;
	spin_unlock_bh(&sdata->mib_lock);
87

88 89 90
	if ((sdata->local->ops->set_hw_addr_filt) &&
	    (sdata->local->hw.hw_filt.short_addr != sdata->short_addr)) {
		sdata->local->hw.hw_filt.short_addr = sdata->short_addr;
91
		set_hw_addr_filt(dev, IEEE802154_AFILT_SADDR_CHANGED);
92 93 94
	}
}

95
__le16 mac802154_dev_get_short_addr(const struct net_device *dev)
96
{
97
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
98
	__le16 ret;
99 100 101

	BUG_ON(dev->type != ARPHRD_IEEE802154);

102 103 104
	spin_lock_bh(&sdata->mib_lock);
	ret = sdata->short_addr;
	spin_unlock_bh(&sdata->mib_lock);
105 106 107 108

	return ret;
}

109 110
void mac802154_dev_set_ieee_addr(struct net_device *dev)
{
111
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
112
	struct ieee802154_local *local = sdata->local;
113

114
	sdata->extended_addr = ieee802154_devaddr_from_raw(dev->dev_addr);
115

116
	if (local->ops->set_hw_addr_filt &&
117 118
	    local->hw.hw_filt.ieee_addr != sdata->extended_addr) {
		local->hw.hw_filt.ieee_addr = sdata->extended_addr;
119
		set_hw_addr_filt(dev, IEEE802154_AFILT_IEEEADDR_CHANGED);
120 121
	}
}
122

123
__le16 mac802154_dev_get_pan_id(const struct net_device *dev)
124
{
125
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
126
	__le16 ret;
127 128 129

	BUG_ON(dev->type != ARPHRD_IEEE802154);

130 131 132
	spin_lock_bh(&sdata->mib_lock);
	ret = sdata->pan_id;
	spin_unlock_bh(&sdata->mib_lock);
133 134 135 136

	return ret;
}

137
void mac802154_dev_set_pan_id(struct net_device *dev, __le16 val)
138
{
139
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
140 141 142

	BUG_ON(dev->type != ARPHRD_IEEE802154);

143 144 145
	spin_lock_bh(&sdata->mib_lock);
	sdata->pan_id = val;
	spin_unlock_bh(&sdata->mib_lock);
146

147 148 149
	if ((sdata->local->ops->set_hw_addr_filt) &&
	    (sdata->local->hw.hw_filt.pan_id != sdata->pan_id)) {
		sdata->local->hw.hw_filt.pan_id = sdata->pan_id;
150
		set_hw_addr_filt(dev, IEEE802154_AFILT_PANID_CHANGED);
151 152
	}
}
153

154 155
u8 mac802154_dev_get_dsn(const struct net_device *dev)
{
156
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
157 158 159

	BUG_ON(dev->type != ARPHRD_IEEE802154);

160
	return sdata->dsn++;
161 162
}

163 164 165 166
static void phy_chan_notify(struct work_struct *work)
{
	struct phy_chan_notify_work *nw = container_of(work,
					  struct phy_chan_notify_work, work);
167 168 169
	struct net_device *dev = nw->dev;
	struct ieee802154_local *local = mac802154_slave_get_priv(dev);
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
170 171
	int res;

172
	mutex_lock(&sdata->local->phy->pib_lock);
173
	res = local->ops->set_channel(&local->hw, sdata->page, sdata->chan);
174
	if (res) {
175
		pr_debug("set_channel failed\n");
176
	} else {
177 178
		sdata->local->phy->current_channel = sdata->chan;
		sdata->local->phy->current_page = sdata->page;
179
	}
180
	mutex_unlock(&sdata->local->phy->pib_lock);
181 182 183 184 185 186

	kfree(nw);
}

void mac802154_dev_set_page_channel(struct net_device *dev, u8 page, u8 chan)
{
187
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
188 189 190 191
	struct phy_chan_notify_work *work;

	BUG_ON(dev->type != ARPHRD_IEEE802154);

192 193 194 195
	spin_lock_bh(&sdata->mib_lock);
	sdata->page = page;
	sdata->chan = chan;
	spin_unlock_bh(&sdata->mib_lock);
196

197 198 199 200
	mutex_lock(&sdata->local->phy->pib_lock);
	if (sdata->local->phy->current_channel != sdata->chan ||
	    sdata->local->phy->current_page != sdata->page) {
		mutex_unlock(&sdata->local->phy->pib_lock);
201

202 203 204 205 206 207
		work = kzalloc(sizeof(*work), GFP_ATOMIC);
		if (!work)
			return;

		INIT_WORK(&work->work, phy_chan_notify);
		work->dev = dev;
208
		queue_work(sdata->local->workqueue, &work->work);
209
	} else {
210
		mutex_unlock(&sdata->local->phy->pib_lock);
211
	}
212
}
213 214 215 216 217


int mac802154_get_params(struct net_device *dev,
			 struct ieee802154_llsec_params *params)
{
218
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
219 220 221 222
	int res;

	BUG_ON(dev->type != ARPHRD_IEEE802154);

223 224 225
	mutex_lock(&sdata->sec_mtx);
	res = mac802154_llsec_get_params(&sdata->sec, params);
	mutex_unlock(&sdata->sec_mtx);
226 227 228 229 230 231 232 233

	return res;
}

int mac802154_set_params(struct net_device *dev,
			 const struct ieee802154_llsec_params *params,
			 int changed)
{
234
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
235 236 237 238
	int res;

	BUG_ON(dev->type != ARPHRD_IEEE802154);

239 240 241
	mutex_lock(&sdata->sec_mtx);
	res = mac802154_llsec_set_params(&sdata->sec, params, changed);
	mutex_unlock(&sdata->sec_mtx);
242 243 244 245 246 247 248 249 250

	return res;
}


int mac802154_add_key(struct net_device *dev,
		      const struct ieee802154_llsec_key_id *id,
		      const struct ieee802154_llsec_key *key)
{
251
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
252 253 254 255
	int res;

	BUG_ON(dev->type != ARPHRD_IEEE802154);

256 257 258
	mutex_lock(&sdata->sec_mtx);
	res = mac802154_llsec_key_add(&sdata->sec, id, key);
	mutex_unlock(&sdata->sec_mtx);
259 260 261 262 263 264 265

	return res;
}

int mac802154_del_key(struct net_device *dev,
		      const struct ieee802154_llsec_key_id *id)
{
266
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
267 268 269 270
	int res;

	BUG_ON(dev->type != ARPHRD_IEEE802154);

271 272 273
	mutex_lock(&sdata->sec_mtx);
	res = mac802154_llsec_key_del(&sdata->sec, id);
	mutex_unlock(&sdata->sec_mtx);
274 275 276 277 278 279 280 281

	return res;
}


int mac802154_add_dev(struct net_device *dev,
		      const struct ieee802154_llsec_device *llsec_dev)
{
282
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
283 284 285 286
	int res;

	BUG_ON(dev->type != ARPHRD_IEEE802154);

287 288 289
	mutex_lock(&sdata->sec_mtx);
	res = mac802154_llsec_dev_add(&sdata->sec, llsec_dev);
	mutex_unlock(&sdata->sec_mtx);
290 291 292 293 294 295

	return res;
}

int mac802154_del_dev(struct net_device *dev, __le64 dev_addr)
{
296
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
297 298 299 300
	int res;

	BUG_ON(dev->type != ARPHRD_IEEE802154);

301 302 303
	mutex_lock(&sdata->sec_mtx);
	res = mac802154_llsec_dev_del(&sdata->sec, dev_addr);
	mutex_unlock(&sdata->sec_mtx);
304 305 306 307 308 309 310 311 312

	return res;
}


int mac802154_add_devkey(struct net_device *dev,
			 __le64 device_addr,
			 const struct ieee802154_llsec_device_key *key)
{
313
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
314 315 316 317
	int res;

	BUG_ON(dev->type != ARPHRD_IEEE802154);

318 319 320
	mutex_lock(&sdata->sec_mtx);
	res = mac802154_llsec_devkey_add(&sdata->sec, device_addr, key);
	mutex_unlock(&sdata->sec_mtx);
321 322 323 324 325 326 327 328

	return res;
}

int mac802154_del_devkey(struct net_device *dev,
			 __le64 device_addr,
			 const struct ieee802154_llsec_device_key *key)
{
329
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
330 331 332 333
	int res;

	BUG_ON(dev->type != ARPHRD_IEEE802154);

334 335 336
	mutex_lock(&sdata->sec_mtx);
	res = mac802154_llsec_devkey_del(&sdata->sec, device_addr, key);
	mutex_unlock(&sdata->sec_mtx);
337 338 339 340 341 342 343 344

	return res;
}


int mac802154_add_seclevel(struct net_device *dev,
			   const struct ieee802154_llsec_seclevel *sl)
{
345
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
346 347 348 349
	int res;

	BUG_ON(dev->type != ARPHRD_IEEE802154);

350 351 352
	mutex_lock(&sdata->sec_mtx);
	res = mac802154_llsec_seclevel_add(&sdata->sec, sl);
	mutex_unlock(&sdata->sec_mtx);
353 354 355 356 357 358 359

	return res;
}

int mac802154_del_seclevel(struct net_device *dev,
			   const struct ieee802154_llsec_seclevel *sl)
{
360
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
361 362 363 364
	int res;

	BUG_ON(dev->type != ARPHRD_IEEE802154);

365 366 367
	mutex_lock(&sdata->sec_mtx);
	res = mac802154_llsec_seclevel_del(&sdata->sec, sl);
	mutex_unlock(&sdata->sec_mtx);
368 369 370 371 372 373 374

	return res;
}


void mac802154_lock_table(struct net_device *dev)
{
375
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
376 377 378

	BUG_ON(dev->type != ARPHRD_IEEE802154);

379
	mutex_lock(&sdata->sec_mtx);
380 381 382 383 384
}

void mac802154_get_table(struct net_device *dev,
			 struct ieee802154_llsec_table **t)
{
385
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
386 387 388

	BUG_ON(dev->type != ARPHRD_IEEE802154);

389
	*t = &sdata->sec.table;
390 391 392 393
}

void mac802154_unlock_table(struct net_device *dev)
{
394
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
395 396 397

	BUG_ON(dev->type != ARPHRD_IEEE802154);

398
	mutex_unlock(&sdata->sec_mtx);
399
}