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
|
--- /dev/null
+++ b/dcwlinux/uci_configuration_provider.h
@@ -0,0 +1,104 @@
+#ifndef UCI_CONFIGURATION_PROVIDER_H_INCLUDED
+#define UCI_CONFIGURATION_PROVIDER_H_INCLUDED
+
+#include "./ap_configuration.h"
+
+namespace dcwlinux {
+
+class UciConfigurationProvider : public APConfigurationProvider {
+
+ static const char *SECTION_TYPE_GENERAL;
+ static const char *SECTION_TYPE_CHANNEL_SET;
+ static const char *SECTION_TYPE_DATA_CHANNEL;
+ static const char *SECTION_TYPE_FILTER_SET;
+ static const char *SECTION_TYPE_FILTER;
+ static const char *DEFAULT_FILTER_SET_NAME;
+
+ static const char *OPTION_TMPDIR;
+ static const char *OPTION_ENABLED;
+ static const char *OPTION_SSID;
+ static const char *OPTION_BRIDGE;
+ static const char *OPTION_DATA_CHANNELS;
+ static const char *OPTION_INTERFACES;
+ static const char *OPTION_MAC_ADDRESS;
+ static const char *OPTION_FILTERS;
+ static const char *OPTION_PACKET_SIZE;
+ static const char *OPTION_SOURCE_IP;
+ static const char *OPTION_SOURCE_PORT;
+ static const char *OPTION_PROTOCOL;
+ static const char *OPTION_DEST_PORT;
+
+ static const char *FILTER_FILE_EXTENSION;
+
+ UciConfigurationProvider(const UciConfigurationProvider&); //no copy
+
+ typedef std::map<std::string, std::string> DataChannelBridgeMap;
+ struct PrimaryChannel {
+ std::string bridgeName;
+ DataChannelBridgeMap dataChannels;
+ };
+ typedef std::map<std::string, PrimaryChannel> PrimaryChannelMap;
+ typedef std::map<dcw::MacAddress, std::string> StationFilterMap;
+
+ struct uci_context *_uciContext;
+ struct uci_package *_uciPackage;
+ const char *_uciConfig;
+
+ std::string _filterDirectory;
+ PrimaryChannelMap _primaryChannels;
+ StationFilterMap _stationFilters;
+
+public:
+ UciConfigurationProvider(const char * const uciConfig); // the "config" part of UCI commands
+ virtual ~UciConfigurationProvider();
+
+ virtual void InstanciateCFileTrafficFilterProfiles(CFTFPList& output) const;
+ virtual void GetPrimarySsids(SsidSet& output) const;
+ virtual void GetDataSsids(SsidSet& output, const char * const primarySsid) const;
+ virtual const char *GetSsidIfname(const char * const ssid) const;
+ virtual void GetStationTrafficFilterProfiles(StationTFPMap& output) const;
+};
+
+}; //namespace dcwlinux {
+
+#endif //#ifndef UCI_CONFIGURATION_PROVIDER_H_INCLUDED
+#ifndef UCI_CONFIGURATION_PROVIDER_H_INCLUDED
+#define UCI_CONFIGURATION_PROVIDER_H_INCLUDED
+
+#include "./ap_configuration.h"
+
+namespace dcwlinux {
+
+class UciConfigurationProvider : public APConfigurationProvider {
+ UciConfigurationProvider(const UciConfigurationProvider&); //no copy
+
+ typedef std::map<std::string, std::string> DataChannelBridgeMap;
+ struct PrimaryChannel {
+ std::string bridgeName;
+ DataChannelBridgeMap dataChannels;
+ };
+ typedef std::map<std::string, PrimaryChannel> PrimaryChannelMap;
+ typedef std::map<dcw::MacAddress, std::string> StationFilterMap;
+
+ struct uci_context *_uciContext;
+ struct uci_package *_uciPackage;
+ const char *_uciConfig;
+
+ PrimaryChannelMap _primaryChannels;
+ StationFilterMap _stationFilters;
+ CFTFPList _defaultFilters;
+
+public:
+ UciConfigurationProvider(const char * const uciConfig); // the "config" part of UCI commands
+ virtual ~UciConfigurationProvider();
+
+ virtual void InstanciateCFileTrafficFilterProfiles(CFTFPList& output) const;
+ virtual void GetPrimarySsids(SsidSet& output) const;
+ virtual void GetDataSsids(SsidSet& output, const char * const primarySsid) const;
+ virtual const char *GetSsidIfname(const char * const ssid) const;
+ virtual void GetStationTrafficFilterProfiles(StationTFPMap& output) const;
+};
+
+}; //namespace dcwlinux {
+
+#endif //#ifndef UCI_CONFIGURATION_PROVIDER_H_INCLUDED
--- /dev/null
+++ b/dcwlinux/uci_configuration_provider.cxx
@@ -0,0 +1,365 @@
+
+#include <uci.h>
+#include <string.h>
+
+#include <stdlib.h>
+#include <stdexcept>
+#include <sys/stat.h>
+#include <cerrno>
+#include <iostream>
+#include <fstream>
+
+#include "./uci_configuration_provider.h"
+
+#include "dcwposix/filterdirscanner.h"
+#include "dcw/macaddress.h"
+#include "dcw/dcwlog.h"
+
+using namespace dcwlinux;
+
+ const char *UciConfigurationProvider::SECTION_TYPE_GENERAL = "general";
+ const char *UciConfigurationProvider::SECTION_TYPE_CHANNEL_SET = "channel-set";
+ const char *UciConfigurationProvider::SECTION_TYPE_DATA_CHANNEL = "datachannel";
+ const char *UciConfigurationProvider::SECTION_TYPE_FILTER_SET = "filter-set";
+ const char *UciConfigurationProvider::SECTION_TYPE_FILTER = "filter";
+ const char *UciConfigurationProvider::DEFAULT_FILTER_SET_NAME = "TFP_Default";
+
+ const char *UciConfigurationProvider::OPTION_TMPDIR = "tmpdir";
+ const char *UciConfigurationProvider::OPTION_ENABLED = "enabled";
+ const char *UciConfigurationProvider::OPTION_SSID = "ssid";
+ const char *UciConfigurationProvider::OPTION_BRIDGE = "bridge";
+ const char *UciConfigurationProvider::OPTION_DATA_CHANNELS = "data_channels";
+ const char *UciConfigurationProvider::OPTION_INTERFACES = "interfaces";
+ const char *UciConfigurationProvider::OPTION_MAC_ADDRESS = "mac";
+ const char *UciConfigurationProvider::OPTION_FILTERS = "filters";
+ const char *UciConfigurationProvider::OPTION_PACKET_SIZE = "packet_size";
+ const char *UciConfigurationProvider::OPTION_SOURCE_IP = "source_ip";
+ const char *UciConfigurationProvider::OPTION_SOURCE_PORT = "source_port";
+ const char *UciConfigurationProvider::OPTION_PROTOCOL = "protocol";
+ const char *UciConfigurationProvider::OPTION_DEST_PORT = "dest_port";
+
+ const char *UciConfigurationProvider::FILTER_FILE_EXTENSION = ".tfp";
+
+ UciConfigurationProvider::UciConfigurationProvider(const char * const uciConfig) : _uciConfig(uciConfig) {
+
+ //printf("*** Start UciConfigurationProvider(%s)\n", _uciConfig);
+ //printf("*** About to uci_alloc_context()\n");
+
+ _uciContext = uci_alloc_context();
+
+ //printf("*** uci_alloc_context() complete\n");
+ //printf("*** About to uci_load()\n");
+
+ if (_uciContext == NULL)
+ {
+ std::string err = "Error creating UCI context ";
+ throw std::runtime_error(err);
+ }
+
+ uci_load(_uciContext, _uciConfig, &_uciPackage);
+
+ //printf("*** uci_load complete()\n");
+
+ if (_uciPackage == NULL)
+ {
+ std::string err = "Error loading UCI package " + std::string(_uciConfig);
+ throw std::runtime_error(err);
+ }
+
+ uci_section *generalSection = uci_lookup_section(_uciContext, _uciPackage, UciConfigurationProvider::SECTION_TYPE_GENERAL);
+ if (generalSection == NULL)
+ {
+ std::string err = "Error: A general section (" + std::string(UciConfigurationProvider::SECTION_TYPE_GENERAL) + ") must be specified!";
+ throw std::runtime_error(err);
+ }
+
+ uci_option *opt_tmpdir = uci_lookup_option(_uciContext, generalSection, UciConfigurationProvider::OPTION_TMPDIR);
+ if (opt_tmpdir == NULL)
+ {
+ std::string err = "Error: A temporary directory (" + std::string(UciConfigurationProvider::OPTION_TMPDIR) + ") must be specified!";
+ throw std::runtime_error(err);
+ }
+ char *tmpdir = opt_tmpdir->v.string;
+ //printf(" *** Set tmpdir: %s\n", tmpdir);
+
+ // make sure that tmpdir exists
+ int status = mkdir(tmpdir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+ if ((status != 0) && // failure
+ (errno != EEXIST)) // the failure was not that the directory already existed
+ {
+ std::string err = "Error: Unable to create the temporary directory (tmpdir), error # " + errno;
+ throw std::runtime_error(err);
+ }
+ _filterDirectory = std::string(tmpdir);
+
+ if (uci_lookup_section(_uciContext, _uciPackage, UciConfigurationProvider::DEFAULT_FILTER_SET_NAME) == NULL)
+ {
+ std::string err = "Error: A default traffic filter profile named " + std::string(UciConfigurationProvider::DEFAULT_FILTER_SET_NAME) + " MUST exist!";
+ throw std::runtime_error(err);
+ }
+
+ // iterate over all of the sections in the package
+ uci_element *elem;
+ uci_foreach_element(&_uciPackage->sections, elem)
+ {
+ //printf("--==-- element.type: %d\n", elem->type);
+ //printf("--==-- element.name: %s\n", elem->name);
+
+ if (elem->type == UCI_TYPE_SECTION)
+ {
+ // look up the section and get it's type
+
+ uci_section *section = NULL;
+ //printf("*** Looking up section: %s\n", elem->name);
+
+ section = uci_lookup_section(_uciContext, _uciPackage, elem->name);
+
+ if ((section != NULL) && (section->type != NULL))
+ {
+ //printf(" *** Section type: %s\n", section->type);
+ if (strcmp(elem->name, UciConfigurationProvider::SECTION_TYPE_GENERAL) == 0)
+ {
+ // we already processed the general section for the tmpdir
+ }
+ else if (strcmp(section->type, UciConfigurationProvider::SECTION_TYPE_CHANNEL_SET) == 0)
+ {
+ // the section is a channel set, populate it with the specified values
+
+ uci_option *enabled = uci_lookup_option(_uciContext, section, UciConfigurationProvider::OPTION_ENABLED);
+ if ((enabled == NULL) || (strcmp(enabled->v.string, "1") != 0))
+ {
+ // found a disabled channel set, ignore it
+ continue;
+ }
+
+ uci_option *ssid = uci_lookup_option(_uciContext, section, UciConfigurationProvider::OPTION_SSID);
+ uci_option *bridge = uci_lookup_option(_uciContext, section, UciConfigurationProvider::OPTION_BRIDGE);
+ uci_option *dataChannels = uci_lookup_option(_uciContext, section, UciConfigurationProvider::OPTION_DATA_CHANNELS);
+
+ if ((ssid != NULL) && (bridge != NULL) && (dataChannels != NULL))
+ {
+ PrimaryChannel &pc = _primaryChannels[ssid->v.string];
+ pc.bridgeName = bridge->v.string;
+
+ char dataChannels_list[255];
+ // The dataChannels option is not a list
+ //if (dataChannels->type == UCI_TYPE_LIST)
+ if (dataChannels->v.string != NULL)
+ {
+ strcpy(dataChannels_list, dataChannels->v.string);
+ std::string str_dataChannels = dataChannels->v.string;
+ size_t start_pos = 0;
+ size_t pos = 0;
+ while(start_pos != std::string::npos)
+ {
+ pos = str_dataChannels.find(" ", start_pos);
+ //printf("****** start_pos: %u, pos: %u\n", start_pos, pos);
+ std::string str_dataChannel = str_dataChannels.substr(start_pos,
+ pos == std::string::npos ? pos : pos-start_pos);
+ //printf("*** dataChannel: %s\n", str_dataChannel.c_str());
+
+ // update the start position for next loop
+ start_pos = (pos == std::string::npos ? pos : pos+1);
+
+ uci_section *dcSection = uci_lookup_section(_uciContext, _uciPackage, str_dataChannel.c_str());
+ if (dcSection != NULL)
+ {
+ uci_option *dcSsid = uci_lookup_option(_uciContext, dcSection, UciConfigurationProvider::OPTION_SSID);
+ uci_option *dcBridge = uci_lookup_option(_uciContext, dcSection, UciConfigurationProvider::OPTION_BRIDGE);
+
+ // TODO: configure dcBridge and dcInterfaces
+ //uci_option *dcInterfaces = uci_lookup_option(_uciContext, dcSection, UciConfigurationProvider::OPTION_INTERFACES);
+
+ if ((dcSsid != NULL) && (dcBridge != NULL))
+ {
+ pc.dataChannels[dcSsid->v.string];
+ pc.dataChannels[dcSsid->v.string] = dcBridge->v.string;
+ }
+ }
+ }
+ }
+
+ //printf("Section: %s, SSID: %s, Bridge: %s, Data Channels: %s\n", section->e.name, ssid->v.string, bridge->v.string, dataChannels_list);
+ }
+ }
+ else if (strcmp(section->type, UciConfigurationProvider::SECTION_TYPE_DATA_CHANNEL) == 0)
+ {
+ // data channels are processed by the channel set
+ }
+ else if (strcmp(section->type, UciConfigurationProvider::SECTION_TYPE_FILTER_SET) == 0)
+ {
+ // the section is a filter set, populate it with the specified values
+ //printf("*** filter set: %s\n", elem->name);
+
+ // create a tfp file for the sectionName
+ std::ofstream tfpFile;
+ std::string tfpFilePath =
+ tmpdir + std::string("/") +
+ std::string(elem->name) +
+ std::string(UciConfigurationProvider::FILTER_FILE_EXTENSION);
+ tfpFile.open(tfpFilePath.c_str(), std::ios::out | std::ios::trunc);
+ if (!tfpFile.is_open())
+ {
+ std::string err = "Error: Unable to open the filter file: " + tfpFilePath;
+ throw std::runtime_error(err);
+ }
+
+ const char *filterDelimiter = "\n";
+ char sFilterContents[2048];
+ sFilterContents[0] = '\0';
+
+ uci_option *filters = uci_lookup_option(_uciContext, section, UciConfigurationProvider::OPTION_FILTERS);
+ // The filters option is not a list
+ //if ((filters != NULL) && (filters->type == UCI_TYPE_LIST))
+ if (filters != NULL)
+ {
+ //printf("*** %s.filters is a list.\n", elem->name);
+ //struct uci_element *e;
+ //uci_foreach_element(&filters->v.list, e)
+
+ std::string str_filters = filters->v.string;
+ //printf("*** STR_FILTERS: %s\n", str_filters.c_str());
+ size_t start_pos = 0;
+ size_t pos = 0;
+ while(start_pos != std::string::npos)
+ {
+ pos = str_filters.find(" ", start_pos);
+ //printf("****** start_pos: %u, pos: %u\n", start_pos, pos);
+ std::string str_filter = str_filters.substr(start_pos,
+ pos == std::string::npos ? pos : pos-start_pos);
+ //printf("*** Looking for filter section named: %s ...\n", str_filter.c_str());
+
+ // update the start position for next loop
+ start_pos = (pos == std::string::npos ? pos : pos+1);
+
+ uci_section *fSection = uci_lookup_section(_uciContext, _uciPackage, str_filter.c_str());
+ if (fSection != NULL)
+ {
+ uci_option *fPacketSize = uci_lookup_option(_uciContext, fSection, UciConfigurationProvider::OPTION_PACKET_SIZE);
+ uci_option *fSourceIp = uci_lookup_option(_uciContext, fSection, UciConfigurationProvider::OPTION_SOURCE_IP);
+ uci_option *fSourcePort = uci_lookup_option(_uciContext, fSection, UciConfigurationProvider::OPTION_SOURCE_PORT);
+ uci_option *fProtocol = uci_lookup_option(_uciContext, fSection, UciConfigurationProvider::OPTION_PROTOCOL);
+ uci_option *fDestPort = uci_lookup_option(_uciContext, fSection, UciConfigurationProvider::OPTION_DEST_PORT);
+
+ if ((fPacketSize != NULL) &&
+ (fSourceIp != NULL) &&
+ (fSourcePort != NULL) &&
+ (fProtocol != NULL) &&
+ (fDestPort != NULL))
+ {
+ //printf("*** filter: %s %s:%s:%s:%s:%s\n", e->name,
+ // fPacketSize->v.string, fSourceIp->v.string, fSourcePort->v.string,
+ // fProtocol->v.string, fDestPort->v.string);
+
+ strcpy(sFilterContents, fPacketSize->v.string);
+ strcat(sFilterContents, ":");
+ strcat(sFilterContents, fSourceIp->v.string);
+ strcat(sFilterContents, ":");
+ strcat(sFilterContents, fSourcePort->v.string);
+ strcat(sFilterContents, ":");
+ strcat(sFilterContents, fProtocol->v.string);
+ strcat(sFilterContents, ":");
+ strcat(sFilterContents, fDestPort->v.string);
+ strcat(sFilterContents, filterDelimiter);
+
+ //printf("*** Writing filter contents to file: %s\n", sFilterContents);
+ tfpFile << sFilterContents;
+ }
+ else
+ {
+ std::string err = "Error parsing filter: " + str_filter;
+ throw std::runtime_error(err);
+ }
+ }
+ }
+ }
+ tfpFile.close();
+
+ // if there is a MAC address for the filter set, we need to add it to the station filters list
+ uci_option *mac = uci_lookup_option(_uciContext, section, UciConfigurationProvider::OPTION_MAC_ADDRESS);
+ if (mac != NULL)
+ {
+ // ignore wildcard MAC address
+ if (strcmp(mac->v.string,"*") != 0)
+ {
+ //printf(" *** MAC Address: %s\n", mac->v.string);
+ _stationFilters[::dcw::MacAddress(mac->v.string)] = elem->name;
+ }
+ }
+ }
+ else if (strcmp(section->type, UciConfigurationProvider::SECTION_TYPE_FILTER) == 0)
+ {
+ // filters are processed by the filter set
+ }
+ else
+ {
+ //std::string err = "Error: Unknown UCI section type: " + std::string(section->type);
+ //throw std::runtime_error(err);
+
+ // Don't throw an exception. It is fine for UCI to contain things that we do not know about
+ // that it may use for other purposes, like UI or internal state
+ dcwlogdbgf("Ignoring UCI section type: %s\n", section->type);
+ }
+ }
+ }
+ }
+ }
+
+ UciConfigurationProvider::~UciConfigurationProvider() {
+ uci_free_context(_uciContext);
+ }
+
+ void UciConfigurationProvider::InstanciateCFileTrafficFilterProfiles(CFTFPList& output) const {
+ ::dcwposix::FilterdirScanner::FileFilterProfileList ffpl;
+ ::dcwposix::FilterdirScanner dirScanner(_filterDirectory.c_str());
+ dirScanner.Scan(ffpl);
+
+ for (::dcwposix::FilterdirScanner::FileFilterProfileList::const_iterator i = ffpl.begin(); i != ffpl.end(); i++) {
+ output.push_back(new ::dcw::FileTrafficFilterProfile(*i));
+ }
+ }
+
+
+ void UciConfigurationProvider::GetPrimarySsids(SsidSet& output) const {
+ for (PrimaryChannelMap::const_iterator i = _primaryChannels.begin(); i != _primaryChannels.end(); i++) {
+ output.insert(i->first);
+ }
+ }
+
+ void UciConfigurationProvider::GetDataSsids(SsidSet& output, const char * const primarySsid) const {
+ const PrimaryChannelMap::const_iterator pssid = _primaryChannels.find(primarySsid);
+ if (pssid == _primaryChannels.end()) return;
+
+ for (DataChannelBridgeMap::const_iterator i = pssid->second.dataChannels.begin(); i != pssid->second.dataChannels.end(); i++) {
+ output.insert(i->first);
+ }
+ }
+
+ const char *UciConfigurationProvider::GetSsidIfname(const char * const ssid) const {
+ PrimaryChannelMap::const_iterator pssid = _primaryChannels.find(ssid);
+ if (pssid != _primaryChannels.end()) {
+ if (pssid->second.bridgeName.empty()) {
+ return NULL;
+ }
+ return pssid->second.bridgeName.c_str();
+ }
+
+ for (pssid = _primaryChannels.begin(); pssid != _primaryChannels.end(); pssid++) {
+ const DataChannelBridgeMap& dataChannels = pssid->second.dataChannels;
+ const DataChannelBridgeMap::const_iterator dc = dataChannels.find(ssid);
+ if (dc == dataChannels.end()) continue;
+ if (dc->second.empty()) {
+ return NULL;
+ }
+ return dc->second.c_str();
+ }
+
+ return NULL;
+ }
+
+ void UciConfigurationProvider::GetStationTrafficFilterProfiles(StationTFPMap& output) const {
+ for (StationFilterMap::const_iterator i = _stationFilters.begin(); i != _stationFilters.end(); i++) {
+ output[i->first] = i->second;
+ }
+
+ }
|