• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/uwb/wlp/
1
2#include <linux/etherdevice.h> /* for is_valid_ether_addr */
3#include <linux/skbuff.h>
4#include <linux/slab.h>
5#include <linux/wlp.h>
6
7#include "wlp-internal.h"
8
9size_t wlp_wss_key_print(char *buf, size_t bufsize, u8 *key)
10{
11	size_t result;
12
13	result = scnprintf(buf, bufsize,
14			  "%02x %02x %02x %02x %02x %02x "
15			  "%02x %02x %02x %02x %02x %02x "
16			  "%02x %02x %02x %02x",
17			  key[0], key[1], key[2], key[3],
18			  key[4], key[5], key[6], key[7],
19			  key[8], key[9], key[10], key[11],
20			  key[12], key[13], key[14], key[15]);
21	return result;
22}
23
24/**
25 * Compute WSSID hash
26 * WLP Draft 0.99 [7.2.1]
27 *
28 * The WSSID hash for a WSSID is the result of an octet-wise exclusive-OR
29 * of all octets in the WSSID.
30 */
31static
32u8 wlp_wss_comp_wssid_hash(struct wlp_uuid *wssid)
33{
34	return wssid->data[0]  ^ wssid->data[1]  ^ wssid->data[2]
35	       ^ wssid->data[3]  ^ wssid->data[4]  ^ wssid->data[5]
36	       ^ wssid->data[6]  ^ wssid->data[7]  ^ wssid->data[8]
37	       ^ wssid->data[9]  ^ wssid->data[10] ^ wssid->data[11]
38	       ^ wssid->data[12] ^ wssid->data[13] ^ wssid->data[14]
39	       ^ wssid->data[15];
40}
41
42static
43struct uwb_mac_addr wlp_wss_sel_bcast_addr(struct wlp_wss *wss)
44{
45	struct uwb_mac_addr bcast = {
46		.data = { 0x01, 0x13, 0x88, 0x00, 0x01, 0x00 }
47	};
48	return bcast;
49}
50
51/**
52 * Clear the contents of the WSS structure - all except kobj, mutex, virtual
53 *
54 * We do not want to reinitialize - the internal kobj should not change as
55 * it still points to the parent received during setup. The mutex should
56 * remain also. We thus just reset values individually.
57 * The virutal address assigned to WSS will remain the same for the
58 * lifetime of the WSS. We only reset the fields that can change during its
59 * lifetime.
60 */
61void wlp_wss_reset(struct wlp_wss *wss)
62{
63	memset(&wss->wssid, 0, sizeof(wss->wssid));
64	wss->hash = 0;
65	memset(&wss->name[0], 0, sizeof(wss->name));
66	memset(&wss->bcast, 0, sizeof(wss->bcast));
67	wss->secure_status = WLP_WSS_UNSECURE;
68	memset(&wss->master_key[0], 0, sizeof(wss->master_key));
69	wss->tag = 0;
70	wss->state = WLP_WSS_STATE_NONE;
71}
72
73/**
74 * Create sysfs infrastructure for WSS
75 *
76 * The WSS is configured to have the interface as parent (see wlp_wss_setup())
77 * a new sysfs directory that includes wssid as its name is created in the
78 * interface's sysfs directory. The group of files interacting with WSS are
79 * created also.
80 */
81static
82int wlp_wss_sysfs_add(struct wlp_wss *wss, char *wssid_str)
83{
84	struct wlp *wlp = container_of(wss, struct wlp, wss);
85	struct device *dev = &wlp->rc->uwb_dev.dev;
86	int result;
87
88	result = kobject_set_name(&wss->kobj, "wss-%s", wssid_str);
89	if (result < 0)
90		return result;
91	wss->kobj.ktype = &wss_ktype;
92	result = kobject_init_and_add(&wss->kobj,
93			&wss_ktype, wss->kobj.parent, "wlp");
94	if (result < 0) {
95		dev_err(dev, "WLP: Cannot register WSS kobject.\n");
96		goto error_kobject_register;
97	}
98	result = sysfs_create_group(&wss->kobj, &wss_attr_group);
99	if (result < 0) {
100		dev_err(dev, "WLP: Cannot register WSS attributes: %d\n",
101			result);
102		goto error_sysfs_create_group;
103	}
104	return 0;
105error_sysfs_create_group:
106
107	kobject_put(&wss->kobj); /* will free name if needed */
108	return result;
109error_kobject_register:
110	kfree(wss->kobj.name);
111	wss->kobj.name = NULL;
112	wss->kobj.ktype = NULL;
113	return result;
114}
115
116
117/**
118 * Release WSS
119 *
120 * No more references exist to this WSS. We should undo everything that was
121 * done in wlp_wss_create_activate() except removing the group. The group
122 * is not removed because an object can be unregistered before the group is
123 * created. We also undo any additional operations on the WSS after this
124 * (addition of members).
125 *
126 * If memory was allocated for the kobject's name then it will
127 * be freed by the kobject system during this time.
128 *
129 * The EDA cache is removed and reinitialized when the WSS is removed. We
130 * thus loose knowledge of members of this WSS at that time and need not do
131 * it here.
132 */
133void wlp_wss_release(struct kobject *kobj)
134{
135	struct wlp_wss *wss = container_of(kobj, struct wlp_wss, kobj);
136
137	wlp_wss_reset(wss);
138}
139
140/**
141 * Enroll into a WSS using provided neighbor as registrar
142 *
143 * First search the neighborhood information to learn which neighbor is
144 * referred to, next proceed with enrollment.
145 *
146 * &wss->mutex is held
147 */
148static
149int wlp_wss_enroll_target(struct wlp_wss *wss, struct wlp_uuid *wssid,
150			  struct uwb_dev_addr *dest)
151{
152	struct wlp *wlp = container_of(wss, struct wlp, wss);
153	struct device *dev = &wlp->rc->uwb_dev.dev;
154	struct wlp_neighbor_e *neighbor;
155	int result = -ENXIO;
156	struct uwb_dev_addr *dev_addr;
157
158	mutex_lock(&wlp->nbmutex);
159	list_for_each_entry(neighbor, &wlp->neighbors, node) {
160		dev_addr = &neighbor->uwb_dev->dev_addr;
161		if (!memcmp(dest, dev_addr, sizeof(*dest))) {
162			result = wlp_enroll_neighbor(wlp, neighbor, wss, wssid);
163			break;
164		}
165	}
166	if (result == -ENXIO)
167		dev_err(dev, "WLP: Cannot find neighbor %02x:%02x. \n",
168			dest->data[1], dest->data[0]);
169	mutex_unlock(&wlp->nbmutex);
170	return result;
171}
172
173/**
174 * Enroll into a WSS previously discovered
175 *
176 * User provides WSSID of WSS, search for neighbor that has this WSS
177 * activated and attempt to enroll.
178 *
179 * &wss->mutex is held
180 */
181static
182int wlp_wss_enroll_discovered(struct wlp_wss *wss, struct wlp_uuid *wssid)
183{
184	struct wlp *wlp = container_of(wss, struct wlp, wss);
185	struct device *dev = &wlp->rc->uwb_dev.dev;
186	struct wlp_neighbor_e *neighbor;
187	struct wlp_wssid_e *wssid_e;
188	char buf[WLP_WSS_UUID_STRSIZE];
189	int result = -ENXIO;
190
191
192	mutex_lock(&wlp->nbmutex);
193	list_for_each_entry(neighbor, &wlp->neighbors, node) {
194		list_for_each_entry(wssid_e, &neighbor->wssid, node) {
195			if (!memcmp(wssid, &wssid_e->wssid, sizeof(*wssid))) {
196				result = wlp_enroll_neighbor(wlp, neighbor,
197							     wss, wssid);
198				if (result == 0) /* enrollment success */
199					goto out;
200				break;
201			}
202		}
203	}
204out:
205	if (result == -ENXIO) {
206		wlp_wss_uuid_print(buf, sizeof(buf), wssid);
207		dev_err(dev, "WLP: Cannot find WSSID %s in cache. \n", buf);
208	}
209	mutex_unlock(&wlp->nbmutex);
210	return result;
211}
212
213/**
214 * Enroll into WSS with provided WSSID, registrar may be provided
215 *
216 * @wss: out WSS that will be enrolled
217 * @wssid: wssid of neighboring WSS that we want to enroll in
218 * @devaddr: registrar can be specified, will be broadcast (ff:ff) if any
219 *           neighbor can be used as registrar.
220 *
221 * &wss->mutex is held
222 */
223static
224int wlp_wss_enroll(struct wlp_wss *wss, struct wlp_uuid *wssid,
225		   struct uwb_dev_addr *devaddr)
226{
227	int result;
228	struct wlp *wlp = container_of(wss, struct wlp, wss);
229	struct device *dev = &wlp->rc->uwb_dev.dev;
230	char buf[WLP_WSS_UUID_STRSIZE];
231	struct uwb_dev_addr bcast = {.data = {0xff, 0xff} };
232
233	wlp_wss_uuid_print(buf, sizeof(buf), wssid);
234
235	if (wss->state != WLP_WSS_STATE_NONE) {
236		dev_err(dev, "WLP: Already enrolled in WSS %s.\n", buf);
237		result = -EEXIST;
238		goto error;
239	}
240	if (!memcmp(&bcast, devaddr, sizeof(bcast)))
241		result = wlp_wss_enroll_discovered(wss, wssid);
242	else
243		result = wlp_wss_enroll_target(wss, wssid, devaddr);
244	if (result < 0) {
245		dev_err(dev, "WLP: Unable to enroll into WSS %s, result %d \n",
246			buf, result);
247		goto error;
248	}
249	dev_dbg(dev, "Successfully enrolled into WSS %s \n", buf);
250	result = wlp_wss_sysfs_add(wss, buf);
251	if (result < 0) {
252		dev_err(dev, "WLP: Unable to set up sysfs for WSS kobject.\n");
253		wlp_wss_reset(wss);
254	}
255error:
256	return result;
257
258}
259
260/**
261 * Activate given WSS
262 *
263 * Prior to activation a WSS must be enrolled. To activate a WSS a device
264 * includes the WSS hash in the WLP IE in its beacon in each superframe.
265 * WLP 0.99 [7.2.5].
266 *
267 * The WSS tag is also computed at this time. We only support one activated
268 * WSS so we can use the hash as a tag - there will never be a conflict.
269 *
270 * We currently only support one activated WSS so only one WSS hash is
271 * included in the WLP IE.
272 */
273static
274int wlp_wss_activate(struct wlp_wss *wss)
275{
276	struct wlp *wlp = container_of(wss, struct wlp, wss);
277	struct device *dev = &wlp->rc->uwb_dev.dev;
278	struct uwb_rc *uwb_rc = wlp->rc;
279	int result;
280	struct {
281		struct wlp_ie wlp_ie;
282		u8 hash; /* only include one hash */
283	} ie_data;
284
285	BUG_ON(wss->state != WLP_WSS_STATE_ENROLLED);
286	wss->hash = wlp_wss_comp_wssid_hash(&wss->wssid);
287	wss->tag = wss->hash;
288	memset(&ie_data, 0, sizeof(ie_data));
289	ie_data.wlp_ie.hdr.element_id = UWB_IE_WLP;
290	ie_data.wlp_ie.hdr.length = sizeof(ie_data) - sizeof(struct uwb_ie_hdr);
291	wlp_ie_set_hash_length(&ie_data.wlp_ie, sizeof(ie_data.hash));
292	ie_data.hash = wss->hash;
293	result = uwb_rc_ie_add(uwb_rc, &ie_data.wlp_ie.hdr,
294			       sizeof(ie_data));
295	if (result < 0) {
296		dev_err(dev, "WLP: Unable to add WLP IE to beacon. "
297			"result = %d.\n", result);
298		goto error_wlp_ie;
299	}
300	wss->state = WLP_WSS_STATE_ACTIVE;
301	result = 0;
302error_wlp_ie:
303	return result;
304}
305
306/**
307 * Enroll in and activate WSS identified by provided WSSID
308 *
309 * The neighborhood cache should contain a list of all neighbors and the
310 * WSS they have activated. Based on that cache we search which neighbor we
311 * can perform the association process with. The user also has option to
312 * specify which neighbor it prefers as registrar.
313 * Successful enrollment is followed by activation.
314 * Successful activation will create the sysfs directory containing
315 * specific information regarding this WSS.
316 */
317int wlp_wss_enroll_activate(struct wlp_wss *wss, struct wlp_uuid *wssid,
318			    struct uwb_dev_addr *devaddr)
319{
320	struct wlp *wlp = container_of(wss, struct wlp, wss);
321	struct device *dev = &wlp->rc->uwb_dev.dev;
322	int result = 0;
323	char buf[WLP_WSS_UUID_STRSIZE];
324
325	mutex_lock(&wss->mutex);
326	result = wlp_wss_enroll(wss, wssid, devaddr);
327	if (result < 0) {
328		wlp_wss_uuid_print(buf, sizeof(buf), &wss->wssid);
329		dev_err(dev, "WLP: Enrollment into WSS %s failed.\n", buf);
330		goto error_enroll;
331	}
332	result = wlp_wss_activate(wss);
333	if (result < 0) {
334		dev_err(dev, "WLP: Unable to activate WSS. Undoing enrollment "
335			"result = %d \n", result);
336		/* Undo enrollment */
337		wlp_wss_reset(wss);
338		goto error_activate;
339	}
340error_activate:
341error_enroll:
342	mutex_unlock(&wss->mutex);
343	return result;
344}
345
346/**
347 * Create, enroll, and activate a new WSS
348 *
349 * @wssid: new wssid provided by user
350 * @name:  WSS name requested by used.
351 * @sec_status: security status requested by user
352 *
353 * A user requested the creation of a new WSS. All operations are done
354 * locally. The new WSS will be stored locally, the hash will be included
355 * in the WLP IE, and the sysfs infrastructure for this WSS will be
356 * created.
357 */
358int wlp_wss_create_activate(struct wlp_wss *wss, struct wlp_uuid *wssid,
359			    char *name, unsigned sec_status, unsigned accept)
360{
361	struct wlp *wlp = container_of(wss, struct wlp, wss);
362	struct device *dev = &wlp->rc->uwb_dev.dev;
363	int result = 0;
364	char buf[WLP_WSS_UUID_STRSIZE];
365
366	result = wlp_wss_uuid_print(buf, sizeof(buf), wssid);
367
368	if (!mutex_trylock(&wss->mutex)) {
369		dev_err(dev, "WLP: WLP association session in progress.\n");
370		return -EBUSY;
371	}
372	if (wss->state != WLP_WSS_STATE_NONE) {
373		dev_err(dev, "WLP: WSS already exists. Not creating new.\n");
374		result = -EEXIST;
375		goto out;
376	}
377	if (wss->kobj.parent == NULL) {
378		dev_err(dev, "WLP: WSS parent not ready. Is network interface "
379		       "up?\n");
380		result = -ENXIO;
381		goto out;
382	}
383	if (sec_status == WLP_WSS_SECURE) {
384		dev_err(dev, "WLP: FIXME Creation of secure WSS not "
385			"supported yet.\n");
386		result = -EINVAL;
387		goto out;
388	}
389	wss->wssid = *wssid;
390	memcpy(wss->name, name, sizeof(wss->name));
391	wss->bcast = wlp_wss_sel_bcast_addr(wss);
392	wss->secure_status = sec_status;
393	wss->accept_enroll = accept;
394	/*wss->virtual_addr is initialized in call to wlp_wss_setup*/
395	/* sysfs infrastructure */
396	result = wlp_wss_sysfs_add(wss, buf);
397	if (result < 0) {
398		dev_err(dev, "Cannot set up sysfs for WSS kobject.\n");
399		wlp_wss_reset(wss);
400		goto out;
401	} else
402		result = 0;
403	wss->state = WLP_WSS_STATE_ENROLLED;
404	result = wlp_wss_activate(wss);
405	if (result < 0) {
406		dev_err(dev, "WLP: Unable to activate WSS. Undoing "
407			"enrollment\n");
408		wlp_wss_reset(wss);
409		goto out;
410	}
411	result = 0;
412out:
413	mutex_unlock(&wss->mutex);
414	return result;
415}
416
417/**
418 * Determine if neighbor has WSS activated
419 *
420 * @returns: 1 if neighbor has WSS activated, zero otherwise
421 *
422 * This can be done in two ways:
423 * - send a C1 frame, parse C2/F0 response
424 * - examine the WLP IE sent by the neighbor
425 *
426 * The WLP IE is not fully supported in hardware so we use the C1/C2 frame
427 * exchange to determine if a WSS is activated. Using the WLP IE should be
428 * faster and should be used when it becomes possible.
429 */
430int wlp_wss_is_active(struct wlp *wlp, struct wlp_wss *wss,
431		      struct uwb_dev_addr *dev_addr)
432{
433	int result = 0;
434	struct device *dev = &wlp->rc->uwb_dev.dev;
435	DECLARE_COMPLETION_ONSTACK(completion);
436	struct wlp_session session;
437	struct sk_buff  *skb;
438	struct wlp_frame_assoc *resp;
439	struct wlp_uuid wssid;
440
441	mutex_lock(&wlp->mutex);
442	/* Send C1 association frame */
443	result = wlp_send_assoc_frame(wlp, wss, dev_addr, WLP_ASSOC_C1);
444	if (result < 0) {
445		dev_err(dev, "Unable to send C1 frame to neighbor "
446			"%02x:%02x (%d)\n", dev_addr->data[1],
447			dev_addr->data[0], result);
448		result = 0;
449		goto out;
450	}
451	/* Create session, wait for response */
452	session.exp_message = WLP_ASSOC_C2;
453	session.cb = wlp_session_cb;
454	session.cb_priv = &completion;
455	session.neighbor_addr = *dev_addr;
456	BUG_ON(wlp->session != NULL);
457	wlp->session = &session;
458	/* Wait for C2/F0 frame */
459	result = wait_for_completion_interruptible_timeout(&completion,
460						   WLP_PER_MSG_TIMEOUT * HZ);
461	if (result == 0) {
462		dev_err(dev, "Timeout while sending C1 to neighbor "
463			     "%02x:%02x.\n", dev_addr->data[1],
464			     dev_addr->data[0]);
465		goto out;
466	}
467	if (result < 0) {
468		dev_err(dev, "Unable to send C1 to neighbor %02x:%02x.\n",
469			dev_addr->data[1], dev_addr->data[0]);
470		result = 0;
471		goto out;
472	}
473	/* Parse message in session->data: it will be either C2 or F0 */
474	skb = session.data;
475	resp = (void *) skb->data;
476	if (resp->type == WLP_ASSOC_F0) {
477		result = wlp_parse_f0(wlp, skb);
478		if (result < 0)
479			dev_err(dev, "WLP:  unable to parse incoming F0 "
480				"frame from neighbor %02x:%02x.\n",
481				dev_addr->data[1], dev_addr->data[0]);
482		result = 0;
483		goto error_resp_parse;
484	}
485	/* WLP version and message type fields have already been parsed */
486	result = wlp_get_wssid(wlp, (void *)resp + sizeof(*resp), &wssid,
487			       skb->len - sizeof(*resp));
488	if (result < 0) {
489		dev_err(dev, "WLP: unable to obtain WSSID from C2 frame.\n");
490		result = 0;
491		goto error_resp_parse;
492	}
493	if (!memcmp(&wssid, &wss->wssid, sizeof(wssid)))
494		result = 1;
495	else {
496		dev_err(dev, "WLP: Received a C2 frame without matching "
497			"WSSID.\n");
498		result = 0;
499	}
500error_resp_parse:
501	kfree_skb(skb);
502out:
503	wlp->session = NULL;
504	mutex_unlock(&wlp->mutex);
505	return result;
506}
507
508/**
509 * Activate connection with neighbor by updating EDA cache
510 *
511 * @wss:       local WSS to which neighbor wants to connect
512 * @dev_addr:  neighbor's address
513 * @wssid:     neighbor's WSSID - must be same as our WSS's WSSID
514 * @tag:       neighbor's WSS tag used to identify frames transmitted by it
515 * @virt_addr: neighbor's virtual EUI-48
516 */
517static
518int wlp_wss_activate_connection(struct wlp *wlp, struct wlp_wss *wss,
519				struct uwb_dev_addr *dev_addr,
520				struct wlp_uuid *wssid, u8 *tag,
521				struct uwb_mac_addr *virt_addr)
522{
523	struct device *dev = &wlp->rc->uwb_dev.dev;
524	int result = 0;
525
526	if (!memcmp(wssid, &wss->wssid, sizeof(*wssid))) {
527		/* Update EDA cache */
528		result = wlp_eda_update_node(&wlp->eda, dev_addr, wss,
529					     (void *) virt_addr->data, *tag,
530					     WLP_WSS_CONNECTED);
531		if (result < 0)
532			dev_err(dev, "WLP: Unable to update EDA cache "
533				"with new connected neighbor information.\n");
534	} else {
535		dev_err(dev, "WLP: Neighbor does not have matching WSSID.\n");
536		result = -EINVAL;
537	}
538	return result;
539}
540
541/**
542 * Connect to WSS neighbor
543 *
544 * Use C3/C4 exchange to determine if neighbor has WSS activated and
545 * retrieve the WSS tag and virtual EUI-48 of the neighbor.
546 */
547static
548int wlp_wss_connect_neighbor(struct wlp *wlp, struct wlp_wss *wss,
549			     struct uwb_dev_addr *dev_addr)
550{
551	int result;
552	struct device *dev = &wlp->rc->uwb_dev.dev;
553	struct wlp_uuid wssid;
554	u8 tag;
555	struct uwb_mac_addr virt_addr;
556	DECLARE_COMPLETION_ONSTACK(completion);
557	struct wlp_session session;
558	struct wlp_frame_assoc *resp;
559	struct sk_buff *skb;
560
561	mutex_lock(&wlp->mutex);
562	/* Send C3 association frame */
563	result = wlp_send_assoc_frame(wlp, wss, dev_addr, WLP_ASSOC_C3);
564	if (result < 0) {
565		dev_err(dev, "Unable to send C3 frame to neighbor "
566			"%02x:%02x (%d)\n", dev_addr->data[1],
567			dev_addr->data[0], result);
568		goto out;
569	}
570	/* Create session, wait for response */
571	session.exp_message = WLP_ASSOC_C4;
572	session.cb = wlp_session_cb;
573	session.cb_priv = &completion;
574	session.neighbor_addr = *dev_addr;
575	BUG_ON(wlp->session != NULL);
576	wlp->session = &session;
577	/* Wait for C4/F0 frame */
578	result = wait_for_completion_interruptible_timeout(&completion,
579						   WLP_PER_MSG_TIMEOUT * HZ);
580	if (result == 0) {
581		dev_err(dev, "Timeout while sending C3 to neighbor "
582			     "%02x:%02x.\n", dev_addr->data[1],
583			     dev_addr->data[0]);
584		result = -ETIMEDOUT;
585		goto out;
586	}
587	if (result < 0) {
588		dev_err(dev, "Unable to send C3 to neighbor %02x:%02x.\n",
589			dev_addr->data[1], dev_addr->data[0]);
590		goto out;
591	}
592	/* Parse message in session->data: it will be either C4 or F0 */
593	skb = session.data;
594	resp = (void *) skb->data;
595	if (resp->type == WLP_ASSOC_F0) {
596		result = wlp_parse_f0(wlp, skb);
597		if (result < 0)
598			dev_err(dev, "WLP: unable to parse incoming F0 "
599				"frame from neighbor %02x:%02x.\n",
600				dev_addr->data[1], dev_addr->data[0]);
601		result = -EINVAL;
602		goto error_resp_parse;
603	}
604	result = wlp_parse_c3c4_frame(wlp, skb, &wssid, &tag, &virt_addr);
605	if (result < 0) {
606		dev_err(dev, "WLP: Unable to parse C4 frame from neighbor.\n");
607		goto error_resp_parse;
608	}
609	result = wlp_wss_activate_connection(wlp, wss, dev_addr, &wssid, &tag,
610					     &virt_addr);
611	if (result < 0) {
612		dev_err(dev, "WLP: Unable to activate connection to "
613			"neighbor %02x:%02x.\n", dev_addr->data[1],
614			dev_addr->data[0]);
615		goto error_resp_parse;
616	}
617error_resp_parse:
618	kfree_skb(skb);
619out:
620	/* Record that we unsuccessfully tried to connect to this neighbor */
621	if (result < 0)
622		wlp_eda_update_node_state(&wlp->eda, dev_addr,
623					  WLP_WSS_CONNECT_FAILED);
624	wlp->session = NULL;
625	mutex_unlock(&wlp->mutex);
626	return result;
627}
628
629/**
630 * Connect to neighbor with common WSS, send pending frame
631 *
632 * This function is scheduled when a frame is destined to a neighbor with
633 * which we do not have a connection. A copy of the EDA cache entry is
634 * provided - not the actual cache entry (because it is protected by a
635 * spinlock).
636 *
637 * First determine if neighbor has the same WSS activated, connect if it
638 * does. The C3/C4 exchange is dual purpose to determine if neighbor has
639 * WSS activated and proceed with the connection.
640 *
641 * The frame that triggered the connection setup is sent after connection
642 * setup.
643 *
644 * network queue is stopped - we need to restart when done
645 *
646 */
647static
648void wlp_wss_connect_send(struct work_struct *ws)
649{
650	struct wlp_assoc_conn_ctx *conn_ctx = container_of(ws,
651						  struct wlp_assoc_conn_ctx,
652						  ws);
653	struct wlp *wlp = conn_ctx->wlp;
654	struct sk_buff *skb = conn_ctx->skb;
655	struct wlp_eda_node *eda_entry = &conn_ctx->eda_entry;
656	struct uwb_dev_addr *dev_addr = &eda_entry->dev_addr;
657	struct wlp_wss *wss = &wlp->wss;
658	int result;
659	struct device *dev = &wlp->rc->uwb_dev.dev;
660
661	mutex_lock(&wss->mutex);
662	if (wss->state < WLP_WSS_STATE_ACTIVE) {
663		if (printk_ratelimit())
664			dev_err(dev, "WLP: Attempting to connect with "
665				"WSS that is not active or connected.\n");
666		dev_kfree_skb(skb);
667		goto out;
668	}
669	/* Establish connection - send C3 rcv C4 */
670	result = wlp_wss_connect_neighbor(wlp, wss, dev_addr);
671	if (result < 0) {
672		if (printk_ratelimit())
673			dev_err(dev, "WLP: Unable to establish connection "
674				"with neighbor %02x:%02x.\n",
675				dev_addr->data[1], dev_addr->data[0]);
676		dev_kfree_skb(skb);
677		goto out;
678	}
679	/* EDA entry changed, update the local copy being used */
680	result = wlp_copy_eda_node(&wlp->eda, dev_addr, eda_entry);
681	if (result < 0) {
682		if (printk_ratelimit())
683			dev_err(dev, "WLP: Cannot find EDA entry for "
684				"neighbor %02x:%02x \n",
685				dev_addr->data[1], dev_addr->data[0]);
686	}
687	result = wlp_wss_prep_hdr(wlp, eda_entry, skb);
688	if (result < 0) {
689		if (printk_ratelimit())
690			dev_err(dev, "WLP: Unable to prepare frame header for "
691				"transmission (neighbor %02x:%02x). \n",
692				dev_addr->data[1], dev_addr->data[0]);
693		dev_kfree_skb(skb);
694		goto out;
695	}
696	BUG_ON(wlp->xmit_frame == NULL);
697	result = wlp->xmit_frame(wlp, skb, dev_addr);
698	if (result < 0) {
699		if (printk_ratelimit())
700			dev_err(dev, "WLP: Unable to transmit frame: %d\n",
701				result);
702		if (result == -ENXIO)
703			dev_err(dev, "WLP: Is network interface up? \n");
704		/* We could try again ... */
705		dev_kfree_skb(skb);/*we need to free if tx fails */
706	}
707out:
708	kfree(conn_ctx);
709	BUG_ON(wlp->start_queue == NULL);
710	wlp->start_queue(wlp);
711	mutex_unlock(&wss->mutex);
712}
713
714/**
715 * Add WLP header to outgoing skb
716 *
717 * @eda_entry: pointer to neighbor's entry in the EDA cache
718 * @_skb:      skb containing data destined to the neighbor
719 */
720int wlp_wss_prep_hdr(struct wlp *wlp, struct wlp_eda_node *eda_entry,
721		     void *_skb)
722{
723	struct device *dev = &wlp->rc->uwb_dev.dev;
724	int result = 0;
725	unsigned char *eth_addr = eda_entry->eth_addr;
726	struct uwb_dev_addr *dev_addr = &eda_entry->dev_addr;
727	struct sk_buff *skb = _skb;
728	struct wlp_frame_std_abbrv_hdr *std_hdr;
729
730	if (eda_entry->state == WLP_WSS_CONNECTED) {
731		/* Add WLP header */
732		BUG_ON(skb_headroom(skb) < sizeof(*std_hdr));
733		std_hdr = (void *) __skb_push(skb, sizeof(*std_hdr));
734		std_hdr->hdr.mux_hdr = cpu_to_le16(WLP_PROTOCOL_ID);
735		std_hdr->hdr.type = WLP_FRAME_STANDARD;
736		std_hdr->tag = eda_entry->wss->tag;
737	} else {
738		if (printk_ratelimit())
739			dev_err(dev, "WLP: Destination neighbor (Ethernet: "
740				"%02x:%02x:%02x:%02x:%02x:%02x, Dev: "
741				"%02x:%02x) is not connected. \n", eth_addr[0],
742				eth_addr[1], eth_addr[2], eth_addr[3],
743				eth_addr[4], eth_addr[5], dev_addr->data[1],
744				dev_addr->data[0]);
745		result = -EINVAL;
746	}
747	return result;
748}
749
750
751/**
752 * Prepare skb for neighbor: connect if not already and prep WLP header
753 *
754 * This function is called in interrupt context, but it needs to sleep. We
755 * temporarily stop the net queue to establish the WLP connection.
756 * Setup of the WLP connection and restart of queue is scheduled
757 * on the default work queue.
758 *
759 * run with eda->lock held (spinlock)
760 */
761int wlp_wss_connect_prep(struct wlp *wlp, struct wlp_eda_node *eda_entry,
762			 void *_skb)
763{
764	int result = 0;
765	struct device *dev = &wlp->rc->uwb_dev.dev;
766	struct sk_buff *skb = _skb;
767	struct wlp_assoc_conn_ctx *conn_ctx;
768
769	if (eda_entry->state == WLP_WSS_UNCONNECTED) {
770		/* We don't want any more packets while we set up connection */
771		BUG_ON(wlp->stop_queue == NULL);
772		wlp->stop_queue(wlp);
773		conn_ctx = kmalloc(sizeof(*conn_ctx), GFP_ATOMIC);
774		if (conn_ctx == NULL) {
775			if (printk_ratelimit())
776				dev_err(dev, "WLP: Unable to allocate memory "
777					"for connection handling.\n");
778			result = -ENOMEM;
779			goto out;
780		}
781		conn_ctx->wlp = wlp;
782		conn_ctx->skb = skb;
783		conn_ctx->eda_entry = *eda_entry;
784		INIT_WORK(&conn_ctx->ws, wlp_wss_connect_send);
785		schedule_work(&conn_ctx->ws);
786		result = 1;
787	} else if (eda_entry->state == WLP_WSS_CONNECT_FAILED) {
788		/* Previous connection attempts failed, don't retry - see
789		 * conditions for connection in WLP 0.99 [7.6.2] */
790		if (printk_ratelimit())
791			dev_err(dev, "Could not connect to neighbor "
792			 "previously. Not retrying. \n");
793		result = -ENONET;
794		goto out;
795	} else /* eda_entry->state == WLP_WSS_CONNECTED */
796		result = wlp_wss_prep_hdr(wlp, eda_entry, skb);
797out:
798	return result;
799}
800
801/**
802 * Emulate broadcast: copy skb, send copy to neighbor (connect if not already)
803 *
804 * We need to copy skbs in the case where we emulate broadcast through
805 * unicast. We copy instead of clone because we are modifying the data of
806 * the frame after copying ... clones share data so we cannot emulate
807 * broadcast using clones.
808 *
809 * run with eda->lock held (spinlock)
810 */
811int wlp_wss_send_copy(struct wlp *wlp, struct wlp_eda_node *eda_entry,
812		      void *_skb)
813{
814	int result = -ENOMEM;
815	struct device *dev = &wlp->rc->uwb_dev.dev;
816	struct sk_buff *skb = _skb;
817	struct sk_buff *copy;
818	struct uwb_dev_addr *dev_addr = &eda_entry->dev_addr;
819
820	copy = skb_copy(skb, GFP_ATOMIC);
821	if (copy == NULL) {
822		if (printk_ratelimit())
823			dev_err(dev, "WLP: Unable to copy skb for "
824				"transmission.\n");
825		goto out;
826	}
827	result = wlp_wss_connect_prep(wlp, eda_entry, copy);
828	if (result < 0) {
829		if (printk_ratelimit())
830			dev_err(dev, "WLP: Unable to connect/send skb "
831				"to neighbor.\n");
832		dev_kfree_skb_irq(copy);
833		goto out;
834	} else if (result == 1)
835		/* Frame will be transmitted separately */
836		goto out;
837	BUG_ON(wlp->xmit_frame == NULL);
838	result = wlp->xmit_frame(wlp, copy, dev_addr);
839	if (result < 0) {
840		if (printk_ratelimit())
841			dev_err(dev, "WLP: Unable to transmit frame: %d\n",
842				result);
843		if ((result == -ENXIO) && printk_ratelimit())
844			dev_err(dev, "WLP: Is network interface up? \n");
845		/* We could try again ... */
846		dev_kfree_skb_irq(copy);/*we need to free if tx fails */
847	}
848out:
849	return result;
850}
851
852
853/**
854 * Setup WSS
855 *
856 * Should be called by network driver after the interface has been given a
857 * MAC address.
858 */
859int wlp_wss_setup(struct net_device *net_dev, struct wlp_wss *wss)
860{
861	struct wlp *wlp = container_of(wss, struct wlp, wss);
862	struct device *dev = &wlp->rc->uwb_dev.dev;
863	int result = 0;
864
865	mutex_lock(&wss->mutex);
866	wss->kobj.parent = &net_dev->dev.kobj;
867	if (!is_valid_ether_addr(net_dev->dev_addr)) {
868		dev_err(dev, "WLP: Invalid MAC address. Cannot use for"
869		       "virtual.\n");
870		result = -EINVAL;
871		goto out;
872	}
873	memcpy(wss->virtual_addr.data, net_dev->dev_addr,
874	       sizeof(wss->virtual_addr.data));
875out:
876	mutex_unlock(&wss->mutex);
877	return result;
878}
879EXPORT_SYMBOL_GPL(wlp_wss_setup);
880
881/**
882 * Remove WSS
883 *
884 * Called by client that configured WSS through wlp_wss_setup(). This
885 * function is called when client no longer needs WSS, eg. client shuts
886 * down.
887 *
888 * We remove the WLP IE from the beacon before initiating local cleanup.
889 */
890void wlp_wss_remove(struct wlp_wss *wss)
891{
892	struct wlp *wlp = container_of(wss, struct wlp, wss);
893
894	mutex_lock(&wss->mutex);
895	if (wss->state == WLP_WSS_STATE_ACTIVE)
896		uwb_rc_ie_rm(wlp->rc, UWB_IE_WLP);
897	if (wss->state != WLP_WSS_STATE_NONE) {
898		sysfs_remove_group(&wss->kobj, &wss_attr_group);
899		kobject_put(&wss->kobj);
900	}
901	wss->kobj.parent = NULL;
902	memset(&wss->virtual_addr, 0, sizeof(wss->virtual_addr));
903	/* Cleanup EDA cache */
904	wlp_eda_release(&wlp->eda);
905	wlp_eda_init(&wlp->eda);
906	mutex_unlock(&wss->mutex);
907}
908EXPORT_SYMBOL_GPL(wlp_wss_remove);
909