aboutsummaryrefslogtreecommitdiff
path: root/drivers/uwb/wlp/messages.c
blob: 75164866c2d867c6a1b5b9451600784e55a3cc58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
/*
 * WiMedia Logical Link Control Protocol (WLP)
 * Message construction and parsing
 *
 * Copyright (C) 2007 Intel Corporation
 * Reinette Chatre <reinette.chatre@intel.com>
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 *
 * FIXME: docs
 */

#include <linux/wlp.h>

#include "wlp-internal.h"

static
const char *__wlp_assoc_frame[] = {
	[WLP_ASSOC_D1] = "WLP_ASSOC_D1",
	[WLP_ASSOC_D2] = "WLP_ASSOC_D2",
	[WLP_ASSOC_M1] = "WLP_ASSOC_M1",
	[WLP_ASSOC_M2] = "WLP_ASSOC_M2",
	[WLP_ASSOC_M3] = "WLP_ASSOC_M3",
	[WLP_ASSOC_M4] = "WLP_ASSOC_M4",
	[WLP_ASSOC_M5] = "WLP_ASSOC_M5",
	[WLP_ASSOC_M6] = "WLP_ASSOC_M6",
	[WLP_ASSOC_M7] = "WLP_ASSOC_M7",
	[WLP_ASSOC_M8] = "WLP_ASSOC_M8",
	[WLP_ASSOC_F0] = "WLP_ASSOC_F0",
	[WLP_ASSOC_E1] = "WLP_ASSOC_E1",
	[WLP_ASSOC_E2] = "WLP_ASSOC_E2",
	[WLP_ASSOC_C1] = "WLP_ASSOC_C1",
	[WLP_ASSOC_C2] = "WLP_ASSOC_C2",
	[WLP_ASSOC_C3] = "WLP_ASSOC_C3",
	[WLP_ASSOC_C4] = "WLP_ASSOC_C4",
};

static const char *wlp_assoc_frame_str(unsigned id)
{
	if (id >= ARRAY_SIZE(__wlp_assoc_frame))
		return "unknown association frame";
	return __wlp_assoc_frame[id];
}

static const char *__wlp_assc_error[] = {
	"none",
	"Authenticator Failure",
	"Rogue activity suspected",
	"Device busy",
	"Setup Locked",
	"Registrar not ready",
	"Invalid WSS selection",
	"Message timeout",
	"Enrollment session timeout",
	"Device password invalid",
	"Unsupported version",
	"Internal error",
	"Undefined error",
	"Numeric comparison failure",
	"Waiting for user input",
};

static const char *wlp_assc_error_str(unsigned id)
{
	if (id >= ARRAY_SIZE(__wlp_assc_error))
		return "unknown WLP association error";
	return __wlp_assc_error[id];
}

static inline void wlp_set_attr_hdr(struct wlp_attr_hdr *hdr, unsigned type,
				    size_t len)
{
	hdr->type = cpu_to_le16(type);
	hdr->length = cpu_to_le16(len);
}

/*
 * Populate fields of a constant sized attribute
 *
 * @returns: total size of attribute including size of new value
 *
 * We have two instances of this function (wlp_pset and wlp_set): one takes
 * the value as a parameter, the other takes a pointer to the value as
 * parameter. They thus only differ in how the value is assigned to the
 * attribute.
 *
 * We use sizeof(*attr) - sizeof(struct wlp_attr_hdr) instead of
 * sizeof(type) to be able to use this same code for the structures that
 * contain 8bit enum values and be able to deal with pointer types.
 */
#define wlp_set(type, type_code, name)					\
static size_t wlp_set_##name(struct wlp_attr_##name *attr, type value)	\
{									\
	wlp_set_attr_hdr(&attr->hdr, type_code,				\
			 sizeof(*attr) - sizeof(struct wlp_attr_hdr));	\
	attr->name = value;						\
	return sizeof(*attr);						\
}

#define wlp_pset(type, type_code, name)					\
static size_t wlp_set_##name(struct wlp_attr_##name *attr, type value)	\
{									\
	wlp_set_attr_hdr(&attr->hdr, type_code,				\
			 sizeof(*attr) - sizeof(struct wlp_attr_hdr));	\
	attr->name = *value;						\
	return sizeof(*attr);						\
}

/**
 * Populate fields of a variable attribute
 *
 * @returns: total size of attribute including size of new value
 *
 * Provided with a pointer to the memory area reserved for the
 * attribute structure, the field is populated with the value. The
 * reserved memory has to contain enough space for the value.
 */
#define wlp_vset(type, type_code, name)					\
static size_t wlp_set_##name(struct wlp_attr_##name *attr, type value,	\
				size_t len)				\
{									\
	wlp_set_attr_hdr(&attr->hdr, type_code, len);			\
	memcpy(attr->name, value, len);					\
	return sizeof(*attr) + len;					\
}

wlp_vset(char *, WLP_ATTR_DEV_NAME, dev_name)
wlp_vset(char *, WLP_ATTR_MANUF, manufacturer)
wlp_set(enum wlp_assoc_type, WLP_ATTR_MSG_TYPE, msg_type)
wlp_vset(char *, WLP_ATTR_MODEL_NAME, model_name)
wlp_vset(char *, WLP_ATTR_MODEL_NR, model_nr)
wlp_vset(char *, WLP_ATTR_SERIAL, serial)
wlp_vset(char *, WLP_ATTR_WSS_NAME, wss_name)
wlp_pset(struct wlp_uuid *, WLP_ATTR_UUID_E, uuid_e)
wlp_pset(struct wlp_uuid *, WLP_ATTR_UUID_R, uuid_r)
wlp_pset(struct wlp_uuid *, WLP_ATTR_WSSID, wssid)
wlp_pset(struct wlp_dev_type *, WLP_ATTR_PRI_DEV_TYPE, prim_dev_type)
/*wlp_pset(struct wlp_dev_type *, WLP_ATTR_SEC_DEV_TYPE, sec_dev_type)*/
wlp_set(u8, WLP_ATTR_WLP_VER, version)
wlp_set(enum wlp_assc_error, WLP_ATTR_WLP_ASSC_ERR, wlp_assc_err)
wlp_set(enum wlp_wss_sel_mthd, WLP_ATTR_WSS_SEL_MTHD, wss_sel_mthd)
wlp_set(u8, WLP_ATTR_ACC_ENRL, accept_enrl)
wlp_set(u8, WLP_ATTR_WSS_SEC_STAT, wss_sec_status)
wlp_pset(struct uwb_mac_addr *, WLP_ATTR_WSS_BCAST, wss_bcast)
wlp_pset(struct wlp_nonce *, WLP_ATTR_ENRL_NONCE, enonce)
wlp_pset(struct wlp_nonce *, WLP_ATTR_REG_NONCE, rnonce)
wlp_set(u8, WLP_ATTR_WSS_TAG, wss_tag)
wlp_pset(struct uwb_mac_addr *, WLP_ATTR_WSS_VIRT, wss_virt)

/**
 * Fill in the WSS information attributes
 *
 * We currently only support one WSS, and this is assumed in this function
 * that can populate only one WSS information attribute.
 */
static size_t wlp_set_wss_info(struct wlp_attr_wss_info *attr,
			       struct wlp_wss *wss)
{
	size_t datalen;
	void *ptr = attr->wss_info;
	size_t used = sizeof(*attr);

	datalen = sizeof(struct wlp_wss_info) + strlen(wss->name);
	wlp_set_attr_hdr(&attr->hdr, WLP_ATTR_WSS_INFO, datalen);
	used = wlp_set_wssid(ptr, &wss->wssid);
	used += wlp_set_wss_name(ptr + used, wss->name, strlen(wss->name));
	used += wlp_set_accept_enrl(ptr + used, wss->accept_enroll);
	used += wlp_set_wss_sec_status(ptr + used, wss->secure_status);
	used += wlp_set_wss_bcast(ptr + used, &wss->bcast);
	return sizeof(*attr) + used;
}

/**
 * Verify attribute header
 *
 * @hdr:     Pointer to attribute header that will be verified.
 * @type:    Expected attribute type.
 * @len:     Expected length of attribute value (excluding header).
 *
 * Most attribute values have a known length even when they do have a
 * length field. This knowledge can be used via this function to verify
 * that the length field matches the expected value.
 */
static int wlp_check_attr_hdr(struct wlp *wlp, struct wlp_attr_hdr *hdr,
		       enum wlp_attr_type type, unsigned len)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;

	if (le16_to_cpu(hdr->type) != type) {
		dev_err(dev, "WLP: unexpected header type. Expected "
			"%u, got %u.\n", type, le16_to_cpu(hdr->type));
		return -EINVAL;
	}
	if (le16_to_cpu(hdr->length) != len) {
		dev_err(dev, "WLP: unexpected length in header. Expected "
			"%u, got %u.\n", len, le16_to_cpu(hdr->length));
		return -EINVAL;
	}
	return 0;
}

/**
 * Check if header of WSS information attribute valid
 *
 * @returns: length of WSS attributes (value of length attribute field) if
 *             valid WSS information attribute found
 *           -ENODATA if no WSS information attribute found
 *           -EIO other error occured
 *
 * The WSS information attribute is optional. The function will be provided
 * with a pointer to data that could _potentially_ be a WSS information
 * attribute. If a valid WSS information attribute is found it will return
 * 0, if no WSS information attribute is found it will return -ENODATA, and
 * another error will be returned if it is a WSS information attribute, but
 * some parsing failure occured.
 */
static int wlp_check_wss_info_attr_hdr(struct wlp *wlp,
				       struct wlp_attr_hdr *hdr, size_t buflen)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	size_t len;
	int result = 0;

	if (buflen < sizeof(*hdr)) {
		dev_err(dev, "WLP: Not enough space in buffer to parse"
			" WSS information attribute header.\n");
		result = -EIO;
		goto out;
	}
	if (le16_to_cpu(hdr->type) != WLP_ATTR_WSS_INFO) {
		/* WSS information is optional */
		result = -ENODATA;
		goto out;
	}
	len = le16_to_cpu(hdr->length);
	if (buflen < sizeof(*hdr) + len) {
		dev_err(dev, "WLP: Not enough space in buffer to parse "
			"variable data. Got %d, expected %d.\n",
			(int)buflen, (int)(sizeof(*hdr) + len));
		result = -EIO;
		goto out;
	}
	result = len;
out:
	return result;
}


static ssize_t wlp_get_attribute(struct wlp *wlp, u16 type_code,
	struct wlp_attr_hdr *attr_hdr, void *value, ssize_t value_len,
	ssize_t buflen)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	ssize_t attr_len = sizeof(*attr_hdr) + value_len;
	if (buflen < 0)
		return -EINVAL;
	if (buflen < attr_len) {
		dev_err(dev, "WLP: Not enough space in buffer to parse"
			" attribute field. Need %d, received %zu\n",
			(int)attr_len, buflen);
		return -EIO;
	}
	if (wlp_check_attr_hdr(wlp, attr_hdr, type_code, value_len) < 0) {
		dev_err(dev, "WLP: Header verification failed. \n");
		return -EINVAL;
	}
	memcpy(value, (void *)attr_hdr + sizeof(*attr_hdr), value_len);
	return attr_len;
}

static ssize_t wlp_vget_attribute(struct wlp *wlp, u16 type_code,
	struct wlp_attr_hdr *attr_hdr, void *value, ssize_t max_value_len,
	ssize_t buflen)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	size_t len;
	if (buflen < 0)
		return -EINVAL;
	if (buflen < sizeof(*attr_hdr)) {
		dev_err(dev, "WLP: Not enough space in buffer to parse"
			" header.\n");
		return -EIO;
	}
	if (le16_to_cpu(attr_hdr->type) != type_code) {
		dev_err(dev, "WLP: Unexpected attribute type. Got %u, "
			"expected %u.\n", le16_to_cpu(attr_hdr->type),
			type_code);
		return -EINVAL;
	}
	len = le16_to_cpu(attr_hdr->length);
	if (len > max_value_len) {
		dev_err(dev, "WLP: Attribute larger than maximum "
			"allowed. Received %zu, max is %d.\n", len,
			(int)max_value_len);
		return -EFBIG;
	}
	if (buflen < sizeof(*attr_hdr) + len) {
		dev_err(dev, "WLP: Not enough space in buffer to parse "
			"variable data.\n");
		return -EIO;
	}
	memcpy(value, (void *)attr_hdr + sizeof(*attr_hdr), len);
	return sizeof(*attr_hdr) + len;
}

/**
 * Get value of attribute from fixed size attribute field.
 *
 * @attr:    Pointer to attribute field.
 * @value:   Pointer to variable in which attribute value will be placed.
 * @buflen:  Size of buffer in which attribute field (including header)
 *           can be found.
 * @returns: Amount of given buffer consumed by parsing for this attribute.
 *
 * The size and type of the value is known by the type of the attribute.
 */
#define wlp_get(type, type_code, name)					\
ssize_t wlp_get_##name(struct wlp *wlp, struct wlp_attr_##name *attr,	\
		      type *value, ssize_t buflen)			\
{									\
	return wlp_get_attribute(wlp, (type_code), &attr->hdr,		\
				 value, sizeof(*value), buflen);	\
}

#define wlp_get_sparse(type, type_code, name) \
	static wlp_get(type, type_code, name)

/**
 * Get value of attribute from variable sized attribute field.
 *
 * @max:     The maximum size of this attribute. This value is dictated by
 *           the maximum value from the WLP specification.
 *
 * @attr:    Pointer to attribute field.
 * @value:   Pointer to variable that will contain the value. The memory
 *           must already have been allocated for this value.
 * @buflen:  Size of buffer in which attribute field (including header)
 *           can be found.
 * @returns: Amount of given bufferconsumed by parsing for this attribute.
 */
#define wlp_vget(type_val, type_code, name, max)			\
static ssize_t wlp_get_##name(struct wlp *wlp,				\
			      struct wlp_attr_##name *attr,		\
			      type_val *value, ssize_t buflen)		\
{									\
	return wlp_vget_attribute(wlp, (type_code), &attr->hdr, 	\
			      value, (max), buflen);			\
}

wlp_get(u8, WLP_ATTR_WLP_VER, version)
wlp_get_sparse(enum wlp_wss_sel_mthd, WLP_ATTR_WSS_SEL_MTHD, wss_sel_mthd)
wlp_get_sparse(struct wlp_dev_type, WLP_ATTR_PRI_DEV_TYPE, prim_dev_type)
wlp_get_sparse(enum wlp_assc_error, WLP_ATTR_WLP_ASSC_ERR, wlp_assc_err)
wlp_get_sparse(struct wlp_uuid, WLP_ATTR_UUID_E, uuid_e)
wlp_get_sparse(struct wlp_uuid, WLP_ATTR_UUID_R, uuid_r)
wlp_get(struct wlp_uuid, WLP_ATTR_WSSID, wssid)
wlp_get_sparse(u8, WLP_ATTR_ACC_ENRL, accept_enrl)
wlp_get_sparse(u8, WLP_ATTR_WSS_SEC_STAT, wss_sec_status)
wlp_get_sparse(struct uwb_mac_addr, WLP_ATTR_WSS_BCAST, wss_bcast)
wlp_get_sparse(u8, WLP_ATTR_WSS_TAG, wss_tag)
wlp_get_sparse(struct uwb_mac_addr, WLP_ATTR_WSS_VIRT, wss_virt)
wlp_get_sparse(struct wlp_nonce, WLP_ATTR_ENRL_NONCE, enonce)
wlp_get_sparse(struct wlp_nonce, WLP_ATTR_REG_NONCE, rnonce)

/* The buffers for the device info attributes can be found in the
 * wlp_device_info struct. These buffers contain one byte more than the
 * max allowed by the spec - this is done to be able to add the
 * terminating \0 for user display. This terminating byte is not required
 * in the actual attribute field (because it has a length field) so the
 * maximum allowed for this value is one less than its size in the
 * structure.
 */
wlp_vget(char, WLP_ATTR_WSS_NAME, wss_name,
	 FIELD_SIZEOF(struct wlp_wss, name) - 1)
wlp_vget(char, WLP_ATTR_DEV_NAME, dev_name,
	 FIELD_SIZEOF(struct wlp_device_info, name) - 1)
wlp_vget(char, WLP_ATTR_MANUF, manufacturer,
	 FIELD_SIZEOF(struct wlp_device_info, manufacturer) - 1)
wlp_vget(char, WLP_ATTR_MODEL_NAME, model_name,
	 FIELD_SIZEOF(struct wlp_device_info, model_name) - 1)
wlp_vget(char, WLP_ATTR_MODEL_NR, model_nr,
	 FIELD_SIZEOF(struct wlp_device_info, model_nr) - 1)
wlp_vget(char, WLP_ATTR_SERIAL, serial,
	 FIELD_SIZEOF(struct wlp_device_info, serial) - 1)

/**
 * Retrieve WSS Name, Accept enroll, Secure status, Broadcast from WSS info
 *
 * @attr: pointer to WSS name attribute in WSS information attribute field
 * @info: structure that will be populated with data from WSS information
 *        field (WSS name, Accept enroll, secure status, broadcast address)
 * @buflen: size of buffer
 *
 * Although the WSSID attribute forms part of the WSS info attribute it is
 * retrieved separately and stored in a different location.
 */
static ssize_t wlp_get_wss_info_attrs(struct wlp *wlp,
				      struct wlp_attr_hdr *attr,
				      struct wlp_wss_tmp_info *info,
				      ssize_t buflen)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	void *ptr = attr;
	size_t used = 0;
	ssize_t result = -EINVAL;

	result = wlp_get_wss_name(wlp, ptr, info->name, buflen);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain WSS name from "
			"WSS info in D2 message.\n");
		goto error_parse;
	}
	used += result;

	result = wlp_get_accept_enrl(wlp, ptr + used, &info->accept_enroll,
				     buflen - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain accepting "
			"enrollment from WSS info in D2 message.\n");
		goto error_parse;
	}
	if (info->accept_enroll != 0 && info->accept_enroll != 1) {
		dev_err(dev, "WLP: invalid value for accepting "
			"enrollment in D2 message.\n");
		result = -EINVAL;
		goto error_parse;
	}
	used += result;

	result = wlp_get_wss_sec_status(wlp, ptr + used, &info->sec_status,
					buflen - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain secure "
			"status from WSS info in D2 message.\n");
		goto error_parse;
	}
	if (info->sec_status != 0 && info->sec_status != 1) {
		dev_err(dev, "WLP: invalid value for secure "
			"status in D2 message.\n");
		result = -EINVAL;
		goto error_parse;
	}
	used += result;

	result = wlp_get_wss_bcast(wlp, ptr + used, &info->bcast,
				   buflen - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain broadcast "
			"address from WSS info in D2 message.\n");
		goto error_parse;
	}
	used += result;
	result = used;
error_parse:
	return result;
}

/**
 * Create a new WSSID entry for the neighbor, allocate temporary storage
 *
 * Each neighbor can have many WSS active. We maintain a list of WSSIDs
 * advertised by neighbor. During discovery we also cache information about
 * these WSS in temporary storage.
 *
 * The temporary storage will be removed after it has been used (eg.
 * displayed to user), the wssid element will be removed from the list when
 * the neighbor is rediscovered or when it disappears.
 */
static struct wlp_wssid_e *wlp_create_wssid_e(struct wlp *wlp,
					      struct wlp_neighbor_e *neighbor)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	struct wlp_wssid_e *wssid_e;

	wssid_e = kzalloc(sizeof(*wssid_e), GFP_KERNEL);
	if (wssid_e == NULL) {
		dev_err(dev, "WLP: unable to allocate memory "
			"for WSS information.\n");
		goto error_alloc;
	}
	wssid_e->info = kzalloc(sizeof(struct wlp_wss_tmp_info), GFP_KERNEL);
	if (wssid_e->info == NULL) {
		dev_err(dev, "WLP: unable to allocate memory "
			"for temporary WSS information.\n");
		kfree(wssid_e);
		wssid_e = NULL;
		goto error_alloc;
	}
	list_add(&wssid_e->node, &neighbor->wssid);
error_alloc:
	return wssid_e;
}

/**
 * Parse WSS information attribute
 *
 * @attr: pointer to WSS information attribute header
 * @buflen: size of buffer in which WSS information attribute appears
 * @wssid: will place wssid from WSS info attribute in this location
 * @wss_info: will place other information from WSS information attribute
 * in this location
 *
 * memory for @wssid and @wss_info must be allocated when calling this
 */
static ssize_t wlp_get_wss_info(struct wlp *wlp, struct wlp_attr_wss_info *attr,
				size_t buflen, struct wlp_uuid *wssid,
				struct wlp_wss_tmp_info *wss_info)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	ssize_t result;
	size_t len;
	size_t used = 0;
	void *ptr;

	result = wlp_check_wss_info_attr_hdr(wlp, (struct wlp_attr_hdr *)attr,
					     buflen);
	if (result < 0)
		goto out;
	len = result;
	used = sizeof(*attr);
	ptr = attr;

	result = wlp_get_wssid(wlp, ptr + used, wssid, buflen - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain WSSID from WSS info.\n");
		goto out;
	}
	used += result;
	result = wlp_get_wss_info_attrs(wlp, ptr + used, wss_info,
					buflen - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain WSS information "
			"from WSS information attributes. \n");
		goto out;
	}
	used += result;
	if (len + sizeof(*attr) != used) {
		dev_err(dev, "WLP: Amount of data parsed does not "
			"match length field. Parsed %zu, length "
			"field %zu. \n", used, len);
		result = -EINVAL;
		goto out;
	}
	result = used;
out:
	return result;
}

/**
 * Retrieve WSS info from association frame
 *
 * @attr:     pointer to WSS information attribute
 * @neighbor: ptr to neighbor being discovered, NULL if enrollment in
 *            progress
 * @wss:      ptr to WSS being enrolled in, NULL if discovery in progress
 * @buflen:   size of buffer in which WSS information appears
 *
 * The WSS information attribute appears in the D2 association message.
 * This message is used in two ways: to discover all neighbors or to enroll
 * into a WSS activated by a neighbor. During discovery we only want to
 * store the WSS info in a cache, to be deleted right after it has been
 * used (eg. displayed to the user). During enrollment we store the WSS
 * information for the lifetime of enrollment.
 *
 * During discovery we are interested in all WSS information, during
 * enrollment we are only interested in the WSS being enrolled in. Even so,
 * when in enrollment we keep parsing the message after finding the WSS of
 * interest, this simplifies the calling routine in that it can be sure
 * that all WSS information attributes have been parsed out of the message.
 *
 * Association frame is process with nbmutex held. The list access is safe.
 */
static ssize_t wlp_get_all_wss_info(struct wlp *wlp,
				    struct wlp_attr_wss_info *attr,
				    struct wlp_neighbor_e *neighbor,
				    struct wlp_wss *wss, ssize_t buflen)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	size_t used = 0;
	ssize_t result = -EINVAL;
	struct wlp_attr_wss_info *cur;
	struct wlp_uuid wssid;
	struct wlp_wss_tmp_info wss_info;
	unsigned enroll; /* 0 - discovery to cache, 1 - enrollment */
	struct wlp_wssid_e *wssid_e;
	char buf[WLP_WSS_UUID_STRSIZE];

	if (buflen < 0)
		goto out;

	if (neighbor != NULL && wss == NULL)
		enroll = 0; /* discovery */
	else if (wss != NULL && neighbor == NULL)
		enroll = 1; /* enrollment */
	else
		goto out;

	cur = attr;
	while (buflen - used > 0) {
		memset(&wss_info, 0, sizeof(wss_info));
		cur = (void *)cur + used;
		result = wlp_get_wss_info(wlp, cur, buflen - used, &wssid,
					  &wss_info);
		if (result == -ENODATA) {
			result = used;
			goto out;
		} else if (result < 0) {
			dev_err(dev, "WLP: Unable to parse WSS information "
				"from WSS information attribute. \n");
			result = -EINVAL;
			goto error_parse;
		}
		if (enroll && !memcmp(&wssid, &wss->wssid, sizeof(wssid))) {
			if (wss_info.accept_enroll != 1) {
				dev_err(dev, "WLP: Requested WSS does "
					"not accept enrollment.\n");
				result = -EINVAL;
				goto out;
			}
			memcpy(wss->name, wss_info.name, sizeof(wss->name));
			wss->bcast = wss_info.bcast;
			wss->secure_status = wss_info.sec_status;
			wss->accept_enroll = wss_info.accept_enroll;
			wss->state = WLP_WSS_STATE_PART_ENROLLED;
			wlp_wss_uuid_print(buf, sizeof(buf), &wssid);
			dev_dbg(dev, "WLP: Found WSS %s. Enrolling.\n", buf);
		} else {
			wssid_e = wlp_create_wssid_e(wlp, neighbor);
			if (wssid_e == NULL) {
				dev_err(dev, "WLP: Cannot create new WSSID "
					"entry for neighbor %02x:%02x.\n",
					neighbor->uwb_dev->dev_addr.data[1],
					neighbor->uwb_dev->dev_addr.data[0]);
				result = -ENOMEM;
				goto out;
			}
			wssid_e->wssid = wssid;
			*wssid_e->info = wss_info;
		}
		used += result;
	}
	result = used;
error_parse:
	if (result < 0 && !enroll) /* this was a discovery */
		wlp_remove_neighbor_tmp_info(neighbor);
out:
	return result;

}

/**
 * Parse WSS information attributes into cache for discovery
 *
 * @attr: the first WSS information attribute in message
 * @neighbor: the neighbor whose cache will be populated
 * @buflen: size of the input buffer
 */
static ssize_t wlp_get_wss_info_to_cache(struct wlp *wlp,
					 struct wlp_attr_wss_info *attr,
					 struct wlp_neighbor_e *neighbor,
					 ssize_t buflen)
{
	return wlp_get_all_wss_info(wlp, attr, neighbor, NULL, buflen);
}

/**
 * Parse WSS information attributes into WSS struct for enrollment
 *
 * @attr: the first WSS information attribute in message
 * @wss: the WSS that will be enrolled
 * @buflen: size of the input buffer
 */
static ssize_t wlp_get_wss_info_to_enroll(struct wlp *wlp,
					  struct wlp_attr_wss_info *attr,
					  struct wlp_wss *wss, ssize_t buflen)
{
	return wlp_get_all_wss_info(wlp, attr, NULL, wss, buflen);
}

/**
 * Construct a D1 association frame
 *
 * We use the radio control functions to determine the values of the device
 * properties. These are of variable length and the total space needed is
 * tallied first before we start constructing the message. The radio
 * control functions return strings that are terminated with \0. This
 * character should not be included in the message (there is a length field
 * accompanying it in the attribute).
 */
static int wlp_build_assoc_d1(struct wlp *wlp, struct wlp_wss *wss,
			      struct sk_buff **skb)
{

	struct device *dev = &wlp->rc->uwb_dev.dev;
	int result = 0;
	struct wlp_device_info *info;
	size_t used = 0;
	struct wlp_frame_assoc *_d1;
	struct sk_buff *_skb;
	void *d1_itr;

	if (wlp->dev_info == NULL) {
		result = __wlp_setup_device_info(wlp);
		if (result < 0) {
			dev_err(dev, "WLP: Unable to setup device "
				"information for D1 message.\n");
			goto error;
		}
	}
	info = wlp->dev_info;
	_skb = dev_alloc_skb(sizeof(*_d1)
		      + sizeof(struct wlp_attr_uuid_e)
		      + sizeof(struct wlp_attr_wss_sel_mthd)
		      + sizeof(struct wlp_attr_dev_name)
		      + strlen(info->name)
		      + sizeof(struct wlp_attr_manufacturer)
		      + strlen(info->manufacturer)
		      + sizeof(struct wlp_attr_model_name)
		      + strlen(info->model_name)
		      + sizeof(struct wlp_attr_model_nr)
		      + strlen(info->model_nr)
		      + sizeof(struct wlp_attr_serial)
		      + strlen(info->serial)
		      + sizeof(struct wlp_attr_prim_dev_type)
		      + sizeof(struct wlp_attr_wlp_assc_err));
	if (_skb == NULL) {
		dev_err(dev, "WLP: Cannot allocate memory for association "
			"message.\n");
		result = -ENOMEM;
		goto error;
	}
	_d1 = (void *) _skb->data;
	_d1->hdr.mux_hdr = cpu_to_le16(WLP_PROTOCOL_ID);
	_d1->hdr.type = WLP_FRAME_ASSOCIATION;
	_d1->type = WLP_ASSOC_D1;

	wlp_set_version(&_d1->version, WLP_VERSION);
	wlp_set_msg_type(&_d1->msg_type, WLP_ASSOC_D1);
	d1_itr = _d1->attr;
	used = wlp_set_uuid_e(d1_itr, &wlp->uuid);
	used += wlp_set_wss_sel_mthd(d1_itr + used, WLP_WSS_REG_SELECT);
	used += wlp_set_dev_name(d1_itr + used, info->name,
				 strlen(info->name));
	used += wlp_set_manufacturer(d1_itr + used, info->manufacturer,
				     strlen(info->manufacturer));
	used += wlp_set_model_name(d1_itr + used, info->model_name,
				   strlen(info->model_name));
	used += wlp_set_model_nr(d1_itr + used, info->model_nr,
				 strlen(info->model_nr));
	used += wlp_set_serial(d1_itr + used, info->serial,
			       strlen(info->serial));
	used += wlp_set_prim_dev_type(d1_itr + used, &info->prim_dev_type);
	used += wlp_set_wlp_assc_err(d1_itr + used, WLP_ASSOC_ERROR_NONE);
	skb_put(_skb, sizeof(*_d1) + used);
	*skb = _skb;
error:
	return result;
}

/**
 * Construct a D2 association frame
 *
 * We use the radio control functions to determine the values of the device
 * properties. These are of variable length and the total space needed is
 * tallied first before we start constructing the message. The radio
 * control functions return strings that are terminated with \0. This
 * character should not be included in the message (there is a length field
 * accompanying it in the attribute).
 */
static
int wlp_build_assoc_d2(struct wlp *wlp, struct wlp_wss *wss,
		       struct sk_buff **skb, struct wlp_uuid *uuid_e)
{

	struct device *dev = &wlp->rc->uwb_dev.dev;
	int result = 0;
	struct wlp_device_info *info;
	size_t used = 0;
	struct wlp_frame_assoc *_d2;
	struct sk_buff *_skb;
	void *d2_itr;
	size_t mem_needed;

	if (wlp->dev_info == NULL) {
		result = __wlp_setup_device_info(wlp);
		if (result < 0) {
			dev_err(dev, "WLP: Unable to setup device "
				"information for D2 message.\n");
			goto error;
		}
	}
	info = wlp->dev_info;
	mem_needed = sizeof(*_d2)
		      + sizeof(struct wlp_attr_uuid_e)
		      + sizeof(struct wlp_attr_uuid_r)
		      + sizeof(struct wlp_attr_dev_name)
		      + strlen(info->name)
		      + sizeof(struct wlp_attr_manufacturer)
		      + strlen(info->manufacturer)
		      + sizeof(struct wlp_attr_model_name)
		      + strlen(info->model_name)
		      + sizeof(struct wlp_attr_model_nr)
		      + strlen(info->model_nr)
		      + sizeof(struct wlp_attr_serial)
		      + strlen(info->serial)
		      + sizeof(struct wlp_attr_prim_dev_type)
		      + sizeof(struct wlp_attr_wlp_assc_err);
	if (wlp->wss.state >= WLP_WSS_STATE_ACTIVE)
		mem_needed += sizeof(struct wlp_attr_wss_info)
			      + sizeof(struct wlp_wss_info)
			      + strlen(wlp->wss.name);
	_skb = dev_alloc_skb(mem_needed);
	if (_skb == NULL) {
		dev_err(dev, "WLP: Cannot allocate memory for association "
			"message.\n");
		result = -ENOMEM;
		goto error;
	}
	_d2 = (void *) _skb->data;
	_d2->hdr.mux_hdr = cpu_to_le16(WLP_PROTOCOL_ID);
	_d2->hdr.type = WLP_FRAME_ASSOCIATION;
	_d2->type = WLP_ASSOC_D2;

	wlp_set_version(&_d2->version, WLP_VERSION);
	wlp_set_msg_type(&_d2->msg_type, WLP_ASSOC_D2);
	d2_itr = _d2->attr;
	used = wlp_set_uuid_e(d2_itr, uuid_e);
	used += wlp_set_uuid_r(d2_itr + used, &wlp->uuid);
	if (wlp->wss.state >= WLP_WSS_STATE_ACTIVE)
		used += wlp_set_wss_info(d2_itr + used, &wlp->wss);
	used += wlp_set_dev_name(d2_itr + used, info->name,
				 strlen(info->name));
	used += wlp_set_manufacturer(d2_itr + used, info->manufacturer,
				     strlen(info->manufacturer));
	used += wlp_set_model_name(d2_itr + used, info->model_name,
				   strlen(info->model_name));
	used += wlp_set_model_nr(d2_itr + used, info->model_nr,
				 strlen(info->model_nr));
	used += wlp_set_serial(d2_itr + used, info->serial,
			       strlen(info->serial));
	used += wlp_set_prim_dev_type(d2_itr + used, &info->prim_dev_type);
	used += wlp_set_wlp_assc_err(d2_itr + used, WLP_ASSOC_ERROR_NONE);
	skb_put(_skb, sizeof(*_d2) + used);
	*skb = _skb;
error:
	return result;
}

/**
 * Allocate memory for and populate fields of F0 association frame
 *
 * Currently (while focusing on unsecure enrollment) we ignore the
 * nonce's that could be placed in the message. Only the error field is
 * populated by the value provided by the caller.
 */
static
int wlp_build_assoc_f0(struct wlp *wlp, struct sk_buff **skb,
		       enum wlp_assc_error error)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	int result = -ENOMEM;
	struct {
		struct wlp_frame_assoc f0_hdr;
		struct wlp_attr_enonce enonce;
		struct wlp_attr_rnonce rnonce;
		struct wlp_attr_wlp_assc_err assc_err;
	} *f0;
	struct sk_buff *_skb;
	struct wlp_nonce tmp;

	_skb = dev_alloc_skb(sizeof(*f0));
	if (_skb == NULL) {
		dev_err(dev, "WLP: Unable to allocate memory for F0 "
			"association frame. \n");
		goto error_alloc;
	}
	f0 = (void *) _skb->data;
	f0->f0_hdr.hdr.mux_hdr = cpu_to_le16(WLP_PROTOCOL_ID);
	f0->f0_hdr.hdr.type = WLP_FRAME_ASSOCIATION;
	f0->f0_hdr.type = WLP_ASSOC_F0;
	wlp_set_version(&f0->f0_hdr.version, WLP_VERSION);
	wlp_set_msg_type(&f0->f0_hdr.msg_type, WLP_ASSOC_F0);
	memset(&tmp, 0, sizeof(tmp));
	wlp_set_enonce(&f0->enonce, &tmp);
	wlp_set_rnonce(&f0->rnonce, &tmp);
	wlp_set_wlp_assc_err(&f0->assc_err, error);
	skb_put(_skb, sizeof(*f0));
	*skb = _skb;
	result = 0;
error_alloc:
	return result;
}

/**
 * Parse F0 frame
 *
 * We just retrieve the values and print it as an error to the user.
 * Calling function already knows an error occured (F0 indicates error), so
 * we just parse the content as debug for higher layers.
 */
int wlp_parse_f0(struct wlp *wlp, struct sk_buff *skb)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	struct wlp_frame_assoc *f0 = (void *) skb->data;
	void *ptr = skb->data;
	size_t len = skb->len;
	size_t used;
	ssize_t result;
	struct wlp_nonce enonce, rnonce;
	enum wlp_assc_error assc_err;
	char enonce_buf[WLP_WSS_NONCE_STRSIZE];
	char rnonce_buf[WLP_WSS_NONCE_STRSIZE];

	used = sizeof(*f0);
	result = wlp_get_enonce(wlp, ptr + used, &enonce, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain Enrollee nonce "
			"attribute from F0 message.\n");
		goto error_parse;
	}
	used += result;
	result = wlp_get_rnonce(wlp, ptr + used, &rnonce, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain Registrar nonce "
			"attribute from F0 message.\n");
		goto error_parse;
	}
	used += result;
	result = wlp_get_wlp_assc_err(wlp, ptr + used, &assc_err, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain WLP Association error "
			"attribute from F0 message.\n");
		goto error_parse;
	}
	wlp_wss_nonce_print(enonce_buf, sizeof(enonce_buf), &enonce);
	wlp_wss_nonce_print(rnonce_buf, sizeof(rnonce_buf), &rnonce);
	dev_err(dev, "WLP: Received F0 error frame from neighbor. Enrollee "
		"nonce: %s, Registrar nonce: %s, WLP Association error: %s.\n",
		enonce_buf, rnonce_buf, wlp_assc_error_str(assc_err));
	result = 0;
error_parse:
	return result;
}

/**
 * Retrieve variable device information from association message
 *
 * The device information parsed is not required in any message. This
 * routine will thus not fail if an attribute is not present.
 * The attributes are expected in a certain order, even if all are not
 * present. The "attribute type" value is used to ensure the attributes
 * are parsed in the correct order.
 *
 * If an error is encountered during parsing the function will return an
 * error code, when this happens the given device_info structure may be
 * partially filled.
 */
static
int wlp_get_variable_info(struct wlp *wlp, void *data,
			  struct wlp_device_info *dev_info, ssize_t len)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	size_t used = 0;
	struct wlp_attr_hdr *hdr;
	ssize_t result = 0;
	unsigned last = 0;

	while (len - used > 0) {
		if (len - used < sizeof(*hdr)) {
			dev_err(dev, "WLP: Partial data in frame, cannot "
				"parse. \n");
			goto error_parse;
		}
		hdr = data + used;
		switch (le16_to_cpu(hdr->type)) {
		case WLP_ATTR_MANUF:
			if (last >= WLP_ATTR_MANUF) {
				dev_err(dev, "WLP: Incorrect order of "
					"attribute values in D1 msg.\n");
				goto error_parse;
			}
			result = wlp_get_manufacturer(wlp, data + used,
						      dev_info->manufacturer,
						      len - used);
			if (result < 0) {
				dev_err(dev, "WLP: Unable to obtain "
					"Manufacturer attribute from D1 "
					"message.\n");
				goto error_parse;
			}
			last = WLP_ATTR_MANUF;
			used += result;
			break;
		case WLP_ATTR_MODEL_NAME:
			if (last >= WLP_ATTR_MODEL_NAME) {
				dev_err(dev, "WLP: Incorrect order of "
					"attribute values in D1 msg.\n");
				goto error_parse;
			}
			result = wlp_get_model_name(wlp, data + used,
						    dev_info->model_name,
						    len - used);
			if (result < 0) {
				dev_err(dev, "WLP: Unable to obtain Model "
					"name attribute from D1 message.\n");
				goto error_parse;
			}
			last = WLP_ATTR_MODEL_NAME;
			used += result;
			break;
		case WLP_ATTR_MODEL_NR:
			if (last >= WLP_ATTR_MODEL_NR) {
				dev_err(dev, "WLP: Incorrect order of "
					"attribute values in D1 msg.\n");
				goto error_parse;
			}
			result = wlp_get_model_nr(wlp, data + used,
						  dev_info->model_nr,
						  len - used);
			if (result < 0) {
				dev_err(dev, "WLP: Unable to obtain Model "
					"number attribute from D1 message.\n");
				goto error_parse;
			}
			last = WLP_ATTR_MODEL_NR;
			used += result;
			break;
		case WLP_ATTR_SERIAL:
			if (last >= WLP_ATTR_SERIAL) {
				dev_err(dev, "WLP: Incorrect order of "
					"attribute values in D1 msg.\n");
				goto error_parse;
			}
			result = wlp_get_serial(wlp, data + used,
						dev_info->serial, len - used);
			if (result < 0) {
				dev_err(dev, "WLP: Unable to obtain Serial "
					"number attribute from D1 message.\n");
				goto error_parse;
			}
			last = WLP_ATTR_SERIAL;
			used += result;
			break;
		case WLP_ATTR_PRI_DEV_TYPE:
			if (last >= WLP_ATTR_PRI_DEV_TYPE) {
				dev_err(dev, "WLP: Incorrect order of "
					"attribute values in D1 msg.\n");
				goto error_parse;
			}
			result = wlp_get_prim_dev_type(wlp, data + used,
						       &dev_info->prim_dev_type,
						       len - used);
			if (result < 0) {
				dev_err(dev, "WLP: Unable to obtain Primary "
					"device type attribute from D1 "
					"message.\n");
				goto error_parse;
			}
			dev_info->prim_dev_type.category =
				le16_to_cpu(dev_info->prim_dev_type.category);
			dev_info->prim_dev_type.subID =
				le16_to_cpu(dev_info->prim_dev_type.subID);
			last = WLP_ATTR_PRI_DEV_TYPE;
			used += result;
			break;
		default:
			/* This is not variable device information. */
			goto out;
			break;
		}
	}
out:
	return used;
error_parse:
	return -EINVAL;
}

/**
 * Parse incoming D1 frame, populate attribute values
 *
 * Caller provides pointers to memory already allocated for attributes
 * expected in the D1 frame. These variables will be populated.
 */
static
int wlp_parse_d1_frame(struct wlp *wlp, struct sk_buff *skb,
		       struct wlp_uuid *uuid_e,
		       enum wlp_wss_sel_mthd *sel_mthd,
		       struct wlp_device_info *dev_info,
		       enum wlp_assc_error *assc_err)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	struct wlp_frame_assoc *d1 = (void *) skb->data;
	void *ptr = skb->data;
	size_t len = skb->len;
	size_t used;
	ssize_t result;

	used = sizeof(*d1);
	result = wlp_get_uuid_e(wlp, ptr + used, uuid_e, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain UUID-E attribute from D1 "
			"message.\n");
		goto error_parse;
	}
	used += result;
	result = wlp_get_wss_sel_mthd(wlp, ptr + used, sel_mthd, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain WSS selection method "
			"from D1 message.\n");
		goto error_parse;
	}
	used += result;
	result = wlp_get_dev_name(wlp, ptr + used, dev_info->name,
				     len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain Device Name from D1 "
			"message.\n");
		goto error_parse;
	}
	used += result;
	result = wlp_get_variable_info(wlp, ptr + used, dev_info, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain Device Information from "
			"D1 message.\n");
		goto error_parse;
	}
	used += result;
	result = wlp_get_wlp_assc_err(wlp, ptr + used, assc_err, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain WLP Association Error "
			"Information from D1 message.\n");
		goto error_parse;
	}
	result = 0;
error_parse:
	return result;
}
/**
 * Handle incoming D1 frame
 *
 * The frame has already been verified to contain an Association header with
 * the correct version number. Parse the incoming frame, construct and send
 * a D2 frame in response.
 *
 * It is not clear what to do with most fields in the incoming D1 frame. We
 * retrieve and discard the information here for now.
 */
void wlp_handle_d1_frame(struct work_struct *ws)
{
	struct wlp_assoc_frame_ctx *frame_ctx = container_of(ws,
						  struct wlp_assoc_frame_ctx,
						  ws);
	struct wlp *wlp = frame_ctx->wlp;
	struct wlp_wss *wss = &wlp->wss;
	struct sk_buff *skb = frame_ctx->skb;
	struct uwb_dev_addr *src = &frame_ctx->src;
	int result;
	struct device *dev = &wlp->rc->uwb_dev.dev;
	struct wlp_uuid uuid_e;
	enum wlp_wss_sel_mthd sel_mthd = 0;
	struct wlp_device_info dev_info;
	enum wlp_assc_error assc_err;
	struct sk_buff *resp = NULL;

	/* Parse D1 frame */
	mutex_lock(&wss->mutex);
	mutex_lock(&wlp->mutex); /* to access wlp->uuid */
	memset(&dev_info, 0, sizeof(dev_info));
	result = wlp_parse_d1_frame(wlp, skb, &uuid_e, &sel_mthd, &dev_info,
				    &assc_err);
	if (result < 0) {
		dev_err(dev, "WLP: Unable to parse incoming D1 frame.\n");
		kfree_skb(skb);
		goto out;
	}

	kfree_skb(skb);
	if (!wlp_uuid_is_set(&wlp->uuid)) {
		dev_err(dev, "WLP: UUID is not set. Set via sysfs to "
			"proceed. Respong to D1 message with error F0.\n");
		result = wlp_build_assoc_f0(wlp, &resp,
					    WLP_ASSOC_ERROR_NOT_READY);
		if (result < 0) {
			dev_err(dev, "WLP: Unable to construct F0 message.\n");
			goto out;
		}
	} else {
		/* Construct D2 frame */
		result = wlp_build_assoc_d2(wlp, wss, &resp, &uuid_e);
		if (result < 0) {
			dev_err(dev, "WLP: Unable to construct D2 message.\n");
			goto out;
		}
	}
	/* Send D2 frame */
	BUG_ON(wlp->xmit_frame == NULL);
	result = wlp->xmit_frame(wlp, resp, src);
	if (result < 0) {
		dev_err(dev, "WLP: Unable to transmit D2 association "
			"message: %d\n", result);
		if (result == -ENXIO)
			dev_err(dev, "WLP: Is network interface up? \n");
		/* We could try again ... */
		dev_kfree_skb_any(resp); /* we need to free if tx fails */
	}
out:
	kfree(frame_ctx);
	mutex_unlock(&wlp->mutex);
	mutex_unlock(&wss->mutex);
}

/**
 * Parse incoming D2 frame, create and populate temporary cache
 *
 * @skb: socket buffer in which D2 frame can be found
 * @neighbor: the neighbor that sent the D2 frame
 *
 * Will allocate memory for temporary storage of information learned during
 * discovery.
 */
int wlp_parse_d2_frame_to_cache(struct wlp *wlp, struct sk_buff *skb,
				struct wlp_neighbor_e *neighbor)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	struct wlp_frame_assoc *d2 = (void *) skb->data;
	void *ptr = skb->data;
	size_t len = skb->len;
	size_t used;
	ssize_t result;
	struct wlp_uuid uuid_e;
	struct wlp_device_info *nb_info;
	enum wlp_assc_error assc_err;

	used = sizeof(*d2);
	result = wlp_get_uuid_e(wlp, ptr + used, &uuid_e, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain UUID-E attribute from D2 "
			"message.\n");
		goto error_parse;
	}
	if (memcmp(&uuid_e, &wlp->uuid, sizeof(uuid_e))) {
		dev_err(dev, "WLP: UUID-E in incoming D2 does not match "
			"local UUID sent in D1. \n");
		goto error_parse;
	}
	used += result;
	result = wlp_get_uuid_r(wlp, ptr + used, &neighbor->uuid, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain UUID-R attribute from D2 "
			"message.\n");
		goto error_parse;
	}
	used += result;
	result = wlp_get_wss_info_to_cache(wlp, ptr + used, neighbor,
					   len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain WSS information "
			"from D2 message.\n");
		goto error_parse;
	}
	used += result;
	neighbor->info = kzalloc(sizeof(struct wlp_device_info), GFP_KERNEL);
	if (neighbor->info == NULL) {
		dev_err(dev, "WLP: cannot allocate memory to store device "
			"info.\n");
		result = -ENOMEM;
		goto error_parse;
	}
	nb_info = neighbor->info;
	result = wlp_get_dev_name(wlp, ptr + used, nb_info->name,
				  len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain Device Name from D2 "
			"message.\n");
		goto error_parse;
	}
	used += result;
	result = wlp_get_variable_info(wlp, ptr + used, nb_info, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain Device Information from "
			"D2 message.\n");
		goto error_parse;
	}
	used += result;
	result = wlp_get_wlp_assc_err(wlp, ptr + used, &assc_err, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain WLP Association Error "
			"Information from D2 message.\n");
		goto error_parse;
	}
	if (assc_err != WLP_ASSOC_ERROR_NONE) {
		dev_err(dev, "WLP: neighbor device returned association "
			"error %d\n", assc_err);
		result = -EINVAL;
		goto error_parse;
	}
	result = 0;
error_parse:
	if (result < 0)
		wlp_remove_neighbor_tmp_info(neighbor);
	return result;
}

/**
 * Parse incoming D2 frame, populate attribute values of WSS bein enrolled in
 *
 * @wss: our WSS that will be enrolled
 * @skb: socket buffer in which D2 frame can be found
 * @neighbor: the neighbor that sent the D2 frame
 * @wssid: the wssid of the WSS in which we want to enroll
 *
 * Forms part of enrollment sequence. We are trying to enroll in WSS with
 * @wssid by using @neighbor as registrar. A D1 message was sent to
 * @neighbor and now we need to parse the D2 response. The neighbor's
 * response is searched for the requested WSS and if found (and it accepts
 * enrollment), we store the information.
 */
int wlp_parse_d2_frame_to_enroll(struct wlp_wss *wss, struct sk_buff *skb,
				 struct wlp_neighbor_e *neighbor,
				 struct wlp_uuid *wssid)
{
	struct wlp *wlp = container_of(wss, struct wlp, wss);
	struct device *dev = &wlp->rc->uwb_dev.dev;
	void *ptr = skb->data;
	size_t len = skb->len;
	size_t used;
	ssize_t result;
	struct wlp_uuid uuid_e;
	struct wlp_uuid uuid_r;
	struct wlp_device_info nb_info;
	enum wlp_assc_error assc_err;
	char uuid_bufA[WLP_WSS_UUID_STRSIZE];
	char uuid_bufB[WLP_WSS_UUID_STRSIZE];

	used = sizeof(struct wlp_frame_assoc);
	result = wlp_get_uuid_e(wlp, ptr + used, &uuid_e, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain UUID-E attribute from D2 "
			"message.\n");
		goto error_parse;
	}
	if (memcmp(&uuid_e, &wlp->uuid, sizeof(uuid_e))) {
		dev_err(dev, "WLP: UUID-E in incoming D2 does not match "
			"local UUID sent in D1. \n");
		goto error_parse;
	}
	used += result;
	result = wlp_get_uuid_r(wlp, ptr + used, &uuid_r, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain UUID-R attribute from D2 "
			"message.\n");
		goto error_parse;
	}
	if (memcmp(&uuid_r, &neighbor->uuid, sizeof(uuid_r))) {
		wlp_wss_uuid_print(uuid_bufA, sizeof(uuid_bufA),
				   &neighbor->uuid);
		wlp_wss_uuid_print(uuid_bufB, sizeof(uuid_bufB), &uuid_r);
		dev_err(dev, "WLP: UUID of neighbor does not match UUID "
			"learned during discovery. Originally discovered: %s, "
			"now from D2 message: %s\n", uuid_bufA, uuid_bufB);
		result = -EINVAL;
		goto error_parse;
	}
	used += result;
	wss->wssid = *wssid;
	result = wlp_get_wss_info_to_enroll(wlp, ptr + used, wss, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain WSS information "
			"from D2 message.\n");
		goto error_parse;
	}
	if (wss->state != WLP_WSS_STATE_PART_ENROLLED) {
		dev_err(dev, "WLP: D2 message did not contain information "
			"for successful enrollment. \n");
		result = -EINVAL;
		goto error_parse;
	}
	used += result;
	/* Place device information on stack to continue parsing of message */
	result = wlp_get_dev_name(wlp, ptr + used, nb_info.name,
				  len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain Device Name from D2 "
			"message.\n");
		goto error_parse;
	}
	used += result;
	result = wlp_get_variable_info(wlp, ptr + used, &nb_info, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain Device Information from "
			"D2 message.\n");
		goto error_parse;
	}
	used += result;
	result = wlp_get_wlp_assc_err(wlp, ptr + used, &assc_err, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain WLP Association Error "
			"Information from D2 message.\n");
		goto error_parse;
	}
	if (assc_err != WLP_ASSOC_ERROR_NONE) {
		dev_err(dev, "WLP: neighbor device returned association "
			"error %d\n", assc_err);
		if (wss->state == WLP_WSS_STATE_PART_ENROLLED) {
			dev_err(dev, "WLP: Enrolled in WSS (should not "
				"happen according to spec). Undoing. \n");
			wlp_wss_reset(wss);
		}
		result = -EINVAL;
		goto error_parse;
	}
	result = 0;
error_parse:
	return result;
}

/**
 * Parse C3/C4 frame into provided variables
 *
 * @wssid: will point to copy of wssid retrieved from C3/C4 frame
 * @tag:   will point to copy of tag retrieved from C3/C4 frame
 * @virt_addr: will point to copy of virtual address retrieved from C3/C4
 * frame.
 *
 * Calling function has to allocate memory for these values.
 *
 * skb contains a valid C3/C4 frame, return the individual fields of this
 * frame in the provided variables.
 */
int wlp_parse_c3c4_frame(struct wlp *wlp, struct sk_buff *skb,
		       struct wlp_uuid *wssid, u8 *tag,
		       struct uwb_mac_addr *virt_addr)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	int result;
	void *ptr = skb->data;
	size_t len = skb->len;
	size_t used;
	struct wlp_frame_assoc *assoc = ptr;

	used = sizeof(*assoc);
	result = wlp_get_wssid(wlp, ptr + used, wssid, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain WSSID attribute from "
			"%s message.\n", wlp_assoc_frame_str(assoc->type));
		goto error_parse;
	}
	used += result;
	result = wlp_get_wss_tag(wlp, ptr + used, tag, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain WSS tag attribute from "
			"%s message.\n", wlp_assoc_frame_str(assoc->type));
		goto error_parse;
	}
	used += result;
	result = wlp_get_wss_virt(wlp, ptr + used, virt_addr, len - used);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain WSS virtual address "
			"attribute from %s message.\n",
			wlp_assoc_frame_str(assoc->type));
		goto error_parse;
	}
error_parse:
	return result;
}

/**
 * Allocate memory for and populate fields of C1 or C2 association frame
 *
 * The C1 and C2 association frames appear identical - except for the type.
 */
static
int wlp_build_assoc_c1c2(struct wlp *wlp, struct wlp_wss *wss,
			 struct sk_buff **skb, enum wlp_assoc_type type)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	int result  = -ENOMEM;
	struct {
		struct wlp_frame_assoc c_hdr;
		struct wlp_attr_wssid wssid;
	} *c;
	struct sk_buff *_skb;

	_skb = dev_alloc_skb(sizeof(*c));
	if (_skb == NULL) {
		dev_err(dev, "WLP: Unable to allocate memory for C1/C2 "
			"association frame. \n");
		goto error_alloc;
	}
	c = (void *) _skb->data;
	c->c_hdr.hdr.mux_hdr = cpu_to_le16(WLP_PROTOCOL_ID);
	c->c_hdr.hdr.type = WLP_FRAME_ASSOCIATION;
	c->c_hdr.type = type;
	wlp_set_version(&c->c_hdr.version, WLP_VERSION);
	wlp_set_msg_type(&c->c_hdr.msg_type, type);
	wlp_set_wssid(&c->wssid, &wss->wssid);
	skb_put(_skb, sizeof(*c));
	*skb = _skb;
	result = 0;
error_alloc:
	return result;
}


static
int wlp_build_assoc_c1(struct wlp *wlp, struct wlp_wss *wss,
		       struct sk_buff **skb)
{
	return wlp_build_assoc_c1c2(wlp, wss, skb, WLP_ASSOC_C1);
}

static
int wlp_build_assoc_c2(struct wlp *wlp, struct wlp_wss *wss,
		       struct sk_buff **skb)
{
	return wlp_build_assoc_c1c2(wlp, wss, skb, WLP_ASSOC_C2);
}


/**
 * Allocate memory for and populate fields of C3 or C4 association frame
 *
 * The C3 and C4 association frames appear identical - except for the type.
 */
static
int wlp_build_assoc_c3c4(struct wlp *wlp, struct wlp_wss *wss,
			 struct sk_buff **skb, enum wlp_assoc_type type)
{
	struct device *dev = &wlp->rc->uwb_dev.dev;
	int result  = -ENOMEM;
	struct {
		struct wlp_frame_assoc c_hdr;
		struct wlp_attr_wssid wssid;
		struct wlp_attr_wss_tag wss_tag;
		struct wlp_attr_wss_virt wss_virt;
	} *c;
	struct sk_buff *_skb;

	_skb = dev_alloc_skb(sizeof(*c));
	if (_skb == NULL) {
		dev_err(dev, "WLP: Unable to allocate memory for C3/C4 "
			"association frame. \n");
		goto error_alloc;
	}
	c = (void *) _skb->data;
	c->c_hdr.hdr.mux_hdr = cpu_to_le16(WLP_PROTOCOL_ID);
	c->c_hdr.hdr.type = WLP_FRAME_ASSOCIATION;
	c->c_hdr.type = type;
	wlp_set_version(&c->c_hdr.version, WLP_VERSION);
	wlp_set_msg_type(&c->c_hdr.msg_type, type);
	wlp_set_wssid(&c->wssid, &wss->wssid);
	wlp_set_wss_tag(&c->wss_tag, wss->tag);
	wlp_set_wss_virt(&c->wss_virt, &wss->virtual_addr);
	skb_put(_skb, sizeof(*c));
	*skb = _skb;
	result = 0;
error_alloc:
	return result;
}

static
int wlp_build_assoc_c3(struct wlp *wlp, struct wlp_wss *wss,
		       struct sk_buff **skb)
{
	return wlp_build_assoc_c3c4(wlp, wss, skb, WLP_ASSOC_C3);
}

static
int wlp_build_assoc_c4(struct wlp *wlp, struct wlp_wss *wss,
		       struct sk_buff **skb)
{
	return wlp_build_assoc_c3c4(wlp, wss, skb, WLP_ASSOC_C4);
}


#define wlp_send_assoc(type, id)					\
static int wlp_send_assoc_##type(struct wlp *wlp, struct wlp_wss *wss,	\
				 struct uwb_dev_addr *dev_addr)		\
{									\
	struct device *dev = &wlp->rc->uwb_dev.dev;			\
	int result;							\
	struct sk_buff *skb = NULL;					\
									\
	/* Build the frame */						\
	result = wlp_build_assoc_##type(wlp, wss, &skb);		\
	if (result < 0) {						\
		dev_err(dev, "WLP: Unable to construct %s association "	\
			"frame: %d\n", wlp_assoc_frame_str(id), result);\
		goto error_build_assoc;					\
	}								\
	/* Send the frame */						\
	BUG_ON(wlp->xmit_frame == NULL);				\
	result = wlp->xmit_frame(wlp, skb, dev_addr);			\
	if (result < 0) {						\
		dev_err(dev, "WLP: Unable to transmit %s association "	\
			"message: %d\n", wlp_assoc_frame_str(id),	\
			result);					\
		if (result == -ENXIO)					\
			dev_err(dev, "WLP: Is network interface "	\
				"up? \n");				\
		goto error_xmit;					\
	}								\
	return 0;							\
error_xmit:								\
	/* We could try again ... */					\
	dev_kfree_skb_any(skb);/*we need to free if tx fails*/		\
error_build_assoc:							\
	return result;							\
}

wlp_send_assoc(d1, WLP_ASSOC_D1)
wlp_send_assoc(c1, WLP_ASSOC_C1)
wlp_send_assoc(c3, WLP_ASSOC_C3)

int wlp_send_assoc_frame(struct wlp *wlp, struct wlp_wss *wss,
			 struct uwb_dev_addr *dev_addr,
			 enum wlp_assoc_type type)
{
	int result = 0;
	struct device *dev = &wlp->rc->uwb_dev.dev;
	switch (type) {
	case WLP_ASSOC_D1:
		result = wlp_send_assoc_d1(wlp, wss, dev_addr);
		break;
	case WLP_ASSOC_C1:
		result = wlp_send_assoc_c1(wlp, wss, dev_addr);
		break;
	case WLP_ASSOC_C3:
		result = wlp_send_assoc_c3(wlp, wss, dev_addr);
		break;
	default:
		dev_err(dev, "WLP: Received request to send unknown "
			"association message.\n");
		result = -EINVAL;
		break;
	}
	return result;
}

/**
 * Handle incoming C1 frame
 *
 * The frame has already been verified to contain an Association header with
 * the correct version number. Parse the incoming frame, construct and send
 * a C2 frame in response.
 */
void wlp_handle_c1_frame(struct work_struct *ws)
{
	struct wlp_assoc_frame_ctx *frame_ctx = container_of(ws,
						  struct wlp_assoc_frame_ctx,
						  ws);
	struct wlp *wlp = frame_ctx->wlp;
	struct wlp_wss *wss = &wlp->wss;
	struct device *dev = &wlp->rc->uwb_dev.dev;
	struct wlp_frame_assoc *c1 = (void *) frame_ctx->skb->data;
	unsigned int len = frame_ctx->skb->len;
	struct uwb_dev_addr *src = &frame_ctx->src;
	int result;
	struct wlp_uuid wssid;
	struct sk_buff *resp = NULL;

	/* Parse C1 frame */
	mutex_lock(&wss->mutex);
	result = wlp_get_wssid(wlp, (void *)c1 + sizeof(*c1), &wssid,
			       len - sizeof(*c1));
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain WSSID from C1 frame.\n");
		goto out;
	}
	if (!memcmp(&wssid, &wss->wssid, sizeof(wssid))
	    && wss->state == WLP_WSS_STATE_ACTIVE) {
		/* Construct C2 frame */
		result = wlp_build_assoc_c2(wlp, wss, &resp);
		if (result < 0) {
			dev_err(dev, "WLP: Unable to construct C2 message.\n");
			goto out;
		}
	} else {
		/* Construct F0 frame */
		result = wlp_build_assoc_f0(wlp, &resp, WLP_ASSOC_ERROR_INV);
		if (result < 0) {
			dev_err(dev, "WLP: Unable to construct F0 message.\n");
			goto out;
		}
	}
	/* Send C2 frame */
	BUG_ON(wlp->xmit_frame == NULL);
	result = wlp->xmit_frame(wlp, resp, src);
	if (result < 0) {
		dev_err(dev, "WLP: Unable to transmit response association "
			"message: %d\n", result);
		if (result == -ENXIO)
			dev_err(dev, "WLP: Is network interface up? \n");
		/* We could try again ... */
		dev_kfree_skb_any(resp); /* we need to free if tx fails */
	}
out:
	kfree_skb(frame_ctx->skb);
	kfree(frame_ctx);
	mutex_unlock(&wss->mutex);
}

/**
 * Handle incoming C3 frame
 *
 * The frame has already been verified to contain an Association header with
 * the correct version number. Parse the incoming frame, construct and send
 * a C4 frame in response. If the C3 frame identifies a WSS that is locally
 * active then we connect to this neighbor (add it to our EDA cache).
 */
void wlp_handle_c3_frame(struct work_struct *ws)
{
	struct wlp_assoc_frame_ctx *frame_ctx = container_of(ws,
						  struct wlp_assoc_frame_ctx,
						  ws);
	struct wlp *wlp = frame_ctx->wlp;
	struct wlp_wss *wss = &wlp->wss;
	struct device *dev = &wlp->rc->uwb_dev.dev;
	struct sk_buff *skb = frame_ctx->skb;
	struct uwb_dev_addr *src = &frame_ctx->src;
	int result;
	struct sk_buff *resp = NULL;
	struct wlp_uuid wssid;
	u8 tag;
	struct uwb_mac_addr virt_addr;

	/* Parse C3 frame */
	mutex_lock(&wss->mutex);
	result = wlp_parse_c3c4_frame(wlp, skb, &wssid, &tag, &virt_addr);
	if (result < 0) {
		dev_err(dev, "WLP: unable to obtain values from C3 frame.\n");
		goto out;
	}
	if (!memcmp(&wssid, &wss->wssid, sizeof(wssid))
	    && wss->state >= WLP_WSS_STATE_ACTIVE) {
		result = wlp_eda_update_node(&wlp->eda, src, wss,
					     (void *) virt_addr.data, tag,
					     WLP_WSS_CONNECTED);
		if (result < 0) {
			dev_err(dev, "WLP: Unable to update EDA cache "
				"with new connected neighbor information.\n");
			result = wlp_build_assoc_f0(wlp, &resp,
						    WLP_ASSOC_ERROR_INT);
			if (result < 0) {
				dev_err(dev, "WLP: Unable to construct F0 "
					"message.\n");
				goto out;
			}
		} else {
			wss->state = WLP_WSS_STATE_CONNECTED;
			/* Construct C4 frame */
			result = wlp_build_assoc_c4(wlp, wss, &resp);
			if (result < 0) {
				dev_err(dev, "WLP: Unable to construct C4 "
					"message.\n");
				goto out;
			}
		}
	} else {
		/* Construct F0 frame */
		result = wlp_build_assoc_f0(wlp, &resp, WLP_ASSOC_ERROR_INV);
		if (result < 0) {
			dev_err(dev, "WLP: Unable to construct F0 message.\n");
			goto out;
		}
	}
	/* Send C4 frame */
	BUG_ON(wlp->xmit_frame == NULL);
	result = wlp->xmit_frame(wlp, resp, src);
	if (result < 0) {
		dev_err(dev, "WLP: Unable to transmit response association "
			"message: %d\n", result);
		if (result == -ENXIO)
			dev_err(dev, "WLP: Is network interface up? \n");
		/* We could try again ... */
		dev_kfree_skb_any(resp); /* we need to free if tx fails */
	}
out:
	kfree_skb(frame_ctx->skb);
	kfree(frame_ctx);
	mutex_unlock(&wss->mutex);
}