-
Notifications
You must be signed in to change notification settings - Fork 103
/
Utils.cpp
732 lines (653 loc) · 21.8 KB
/
Utils.cpp
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
/*
* Copyright 2017 The Polycube Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Iptables.h"
int Iptables::protocolFromStringToInt(const std::string &proto) {
if (proto == "TCP" || proto == "tcp")
return IPPROTO_TCP;
if (proto == "UDP" || proto == "udp")
return IPPROTO_UDP;
if (proto == "ICMP" || proto == "icmp")
return IPPROTO_ICMP;
if (proto == "GRE" || proto == "gre")
return IPPROTO_GRE;
else
throw std::runtime_error("Protocol not supported.");
}
// used for replace strings in datapath code
void Iptables::replaceAll(std::string &str, const std::string &from,
const std::string &to) {
if (from.empty())
return;
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing
// 'x' with 'yx'
}
}
int ChainRule::protocolFromStringToInt(const std::string &proto) {
if (proto == "TCP" || proto == "tcp")
return IPPROTO_TCP;
if (proto == "UDP" || proto == "udp")
return IPPROTO_UDP;
if (proto == "ICMP" || proto == "icmp")
return IPPROTO_ICMP;
if (proto == "GRE" || proto == "gre")
return IPPROTO_GRE;
throw std::runtime_error("Protocol not supported.");
return 0;
}
std::string ChainRule::protocolFromIntToString(const int &proto) {
if (proto == IPPROTO_TCP)
return "TCP";
if (proto == IPPROTO_UDP)
return "UDP";
if (proto == IPPROTO_ICMP)
return "ICMP";
if (proto == IPPROTO_GRE)
return "GRE";
throw std::runtime_error("Protocol not supported.");
return "";
}
void ChainRule::flagsFromStringToMasks(const std::string &flags,
uint8_t &flags_set,
uint8_t &flags_not_set) {
std::string mod_flags = flags;
flags_not_set = 0;
if (mod_flags.find("!FIN") != std::string::npos) {
mod_flags.erase(mod_flags.find("!FIN"), 4);
SET_BIT(flags_not_set, 0);
}
if (mod_flags.find("!SYN") != std::string::npos) {
mod_flags.erase(mod_flags.find("!SYN"), 4);
SET_BIT(flags_not_set, 1);
}
if (mod_flags.find("!RST") != std::string::npos) {
mod_flags.erase(mod_flags.find("!RST"), 4);
SET_BIT(flags_not_set, 2);
}
if (mod_flags.find("!PSH") != std::string::npos) {
mod_flags.erase(mod_flags.find("!PSH"), 4);
SET_BIT(flags_not_set, 3);
}
if (mod_flags.find("!ACK") != std::string::npos) {
mod_flags.erase(mod_flags.find("!ACK"), 4);
SET_BIT(flags_not_set, 4);
}
if (mod_flags.find("!URG") != std::string::npos) {
mod_flags.erase(mod_flags.find("!URG"), 4);
SET_BIT(flags_not_set, 5);
}
if (mod_flags.find("!ECE") != std::string::npos) {
mod_flags.erase(mod_flags.find("!ECE"), 4);
SET_BIT(flags_not_set, 6);
}
if (mod_flags.find("!CWR") != std::string::npos) {
mod_flags.erase(mod_flags.find("!CWR"), 4);
SET_BIT(flags_not_set, 7);
}
flags_set = 0;
if (mod_flags.find("FIN") != std::string::npos) {
SET_BIT(flags_set, 0);
}
if (mod_flags.find("SYN") != std::string::npos) {
SET_BIT(flags_set, 1);
}
if (mod_flags.find("RST") != std::string::npos) {
SET_BIT(flags_set, 2);
}
if (mod_flags.find("PSH") != std::string::npos) {
SET_BIT(flags_set, 3);
}
if (mod_flags.find("ACK") != std::string::npos) {
SET_BIT(flags_set, 4);
}
if (mod_flags.find("URG") != std::string::npos) {
SET_BIT(flags_set, 5);
}
if (mod_flags.find("ECE") != std::string::npos) {
SET_BIT(flags_set, 6);
}
if (mod_flags.find("CWR") != std::string::npos) {
SET_BIT(flags_set, 7);
}
if ((flags_set & flags_not_set) != 0) {
throw std::runtime_error("A flag can't be both set and not set!");
}
}
void ChainRule::flagsFromMasksToString(std::string &flags,
const uint8_t &flags_set,
const uint8_t &flags_not_set) {
if (flags_set != 0) {
if (CHECK_BIT(flags_set, 0)) {
flags += "FIN ";
}
if (CHECK_BIT(flags_set, 1)) {
flags += "SYN ";
}
if (CHECK_BIT(flags_set, 2)) {
flags += "RST ";
}
if (CHECK_BIT(flags_set, 3)) {
flags += "PSH ";
}
if (CHECK_BIT(flags_set, 4)) {
flags += "ACK ";
}
if (CHECK_BIT(flags_set, 5)) {
flags += "URG ";
}
if (CHECK_BIT(flags_set, 6)) {
flags += "ECE ";
}
if (CHECK_BIT(flags_set, 7)) {
flags += "CWR ";
}
}
if (flags_not_set != 0) {
if (CHECK_BIT(flags_not_set, 0)) {
flags += "!FIN ";
}
if (CHECK_BIT(flags_not_set, 1)) {
flags += "!SYN ";
}
if (CHECK_BIT(flags_not_set, 2)) {
flags += "!RST ";
}
if (CHECK_BIT(flags_not_set, 3)) {
flags += "!PSH ";
}
if (CHECK_BIT(flags_not_set, 4)) {
flags += "!ACK ";
}
if (CHECK_BIT(flags_not_set, 5)) {
flags += "!URG ";
}
if (CHECK_BIT(flags_not_set, 6)) {
flags += "!ECE ";
}
if (CHECK_BIT(flags_not_set, 7)) {
flags += "!CWR ";
}
}
}
int ChainRule::ActionEnumToInt(const ActionEnum &action) {
if (action == ActionEnum::DROP)
return DROP_ACTION;
else if (action == ActionEnum::ACCEPT)
return ACCEPT_ACTION;
else
throw std::runtime_error("Action not supported.");
}
static int ChainRuleConntrackEnumToInt(const ConntrackstatusEnum &status) {
if (status == ConntrackstatusEnum::NEW) {
return 0;
} else if (status == ConntrackstatusEnum::ESTABLISHED) {
return 1;
} else if (status == ConntrackstatusEnum::RELATED) {
return 2;
} else if (status == ConntrackstatusEnum::INVALID) {
return 3;
}
}
// convert ip address list from internal rules representation, to Api
// representation
bool Chain::ipFromRulesToMap(
const uint8_t &type, std::map<struct IpAddr, std::vector<uint64_t>> &ips,
const std::vector<std::shared_ptr<ChainRule>> &rules) {
// track if, at least, one wildcard rule is present
std::vector<uint32_t> dont_care_rules;
// current rule ip and id
struct IpAddr current;
uint32_t rule_id;
bool brk = true;
// std::cout<< "+ITERATING ON RULES+ Adding ips in map (except 0.0.0.0 handled
// later)";
// iterate over all rules
// and keep track of different ips (except 0.0.0.0 hadled later)
// push distinct ips as keys in ips map
for (auto const &rule : rules) {
try {
if (type == SOURCE_TYPE) {
current.fromString(rule->getSrc());
// std::cout << "SRC IP RULE | ";
} else {
current.fromString(rule->getDst());
// std::cout << "DST IP RULE | ";
}
} catch (std::runtime_error re) {
// IP not set: don't care rule.
dont_care_rules.push_back(rule->getId());
// std::cout << "IP RULE DONT CARE | ID: " << rule->getId();
continue;
}
rule_id = rule->getId();
// std::cout << "ID: " << ruleId << " Current IP: " << current.toString() <<
// std::endl;
auto it = ips.find(current);
if (it == ips.end()) {
// std::cout << "FIRST TIME I SEE THIS IP -> insert " << std::endl;
std::vector<uint64_t> bitVector(
FROM_NRULES_TO_NELEMENTS(Iptables::max_rules_));
current.rule_id = rule_id;
ips.insert(
std::pair<struct IpAddr, std::vector<uint64_t>>(current, bitVector));
// for (auto eval : ips) {
// std::cout << eval.first.toString() << ": ";
// std::cout << std::bitset<32>(eval.second[0]) << " ";
// std::cout << std::endl;
// std::cout << std::endl;
// }
}
}
// For each ip in ips map
for (auto &eval : ips) {
auto &address = eval.first;
auto &bitVector = eval.second;
struct IpAddr current_rule_ip;
uint32_t current_rule_id;
// For each rule (if don't care rule, use 0.0.0.0), then apply netmask,
// compare and SET_BIT if match
for (auto const &rule : rules) {
try {
if (type == SOURCE_TYPE) {
current_rule_ip.fromString(rule->getSrc());
// std::cout << "SRC IP RULE | ";
} else {
current_rule_ip.fromString(rule->getDst());
// std::cout << "DST IP RULE | ";
}
} catch (std::runtime_error re) {
// IP not set: don't care rule.
current_rule_ip.fromString("0.0.0.0/0");
// std::cout << "IP RULE DONT CARE | ID: " << rule->getId();
}
current_rule_id = rule->getId();
// std::cout << "ID: " << currentRuleId << " Current rule IP: " <<
// currentRuleIp.toString() << std::endl;
auto netmask = (current_rule_ip.netmask);
auto mask = (netmask == 32 ? 0xffffffff : (((uint32_t)1 << netmask) - 1));
// std::cout << "ADDRESS(fixed): " << address.toString() << "
// RULE-IP(loop):" << currentRuleIp.toString() << std::endl;
// std::cout << "netmask: " << netmask << "mask: " <<
// std::bitset<32>(mask) << std::endl;
if (((address.ip & mask) == (current_rule_ip.ip & mask)) &&
(current_rule_ip.netmask <= address.netmask)) {
// std::cout << "Set BIT " << std::endl;
SET_BIT(bitVector[current_rule_id / 63], current_rule_id % 63);
}
}
}
// std::cout << "ips.size: " << ips.size() << " dontCareRules.size(): " <<
// dontCareRules.size() << std::endl;
// Don't care rules are in all entries. Anyway, this loops is useless if there
// are no rules at all requiring matching on this field.
if (ips.size() != 0 && dont_care_rules.size() != 0) {
// std::cout << "++ ADDING 0.0.0.0/0 rule :)" << std::endl;
std::vector<uint64_t> bitVector(
FROM_NRULES_TO_NELEMENTS(Iptables::max_rules_));
struct IpAddr wildcard_ip = {0, 0};
auto &address = wildcard_ip;
struct IpAddr current_rule_ip;
uint32_t current_rule_id;
for (auto const &rule : rules) {
try {
if (type == SOURCE_TYPE) {
current_rule_ip.fromString(rule->getSrc());
// std::cout << "SRC IP RULE | ";
} else {
current_rule_ip.fromString(rule->getDst());
// std::cout << "DST IP RULE | ";
}
} catch (std::runtime_error re) {
// IP not set: don't care rule.
current_rule_ip.fromString("0.0.0.0/0");
// std::cout << "IP RULE DONT CARE | ID: " << rule->getId();
}
current_rule_id = rule->getId();
// std::cout << "ID: " << currentRuleId << " Current rule IP: " <<
// currentRuleIp.toString() << std::endl;
auto netmask = (current_rule_ip.netmask);
auto mask = (netmask == 32 ? 0xffffffff : (((uint32_t)1 << netmask) - 1));
// std::cout << "ADDRESS(fixed): " << address.toString() << "
// RULE-IP(loop):" << currentRuleIp.toString() << std::endl;
// std::cout << "netmask: " << netmask << "mask: " <<
// std::bitset<32>(mask) << std::endl;
if (((address.ip & mask) == (current_rule_ip.ip & mask)) &&
(current_rule_ip.netmask <= address.netmask)) {
// std::cout << "Set BIT " << std::endl;
SET_BIT(bitVector[current_rule_id / 63], current_rule_id % 63);
}
}
ips.insert(std::pair<struct IpAddr, std::vector<uint64_t>>(wildcard_ip,
bitVector));
brk = false;
}
return brk;
// std::cout << "++ END ++ " << std::endl;
// for (auto eval : ips) {
// std::cout << eval.first.toString() << ": ";
// std::cout << std::bitset<32>(eval.second[0]) << " ";
// std::cout << std::endl;
// }
}
bool Chain::transportProtoFromRulesToMap(
std::map<int, std::vector<uint64_t>> &protocols,
const std::vector<std::shared_ptr<ChainRule>> &rules) {
std::vector<uint32_t> dont_care_rules;
int proto;
uint32_t rule_id;
bool brk = true;
for (auto const &rule : rules) {
try {
rule_id = rule->getId();
proto = Iptables::protocolFromStringToInt(rule->getL4proto());
} catch (std::runtime_error re) {
dont_care_rules.push_back(rule_id);
continue;
}
auto it = protocols.find(proto);
if (it == protocols.end()) {
// First entry
std::vector<uint64_t> bitVector(
FROM_NRULES_TO_NELEMENTS(Iptables::max_rules_));
SET_BIT(bitVector[rule_id / 63], rule_id % 63);
protocols.insert(std::pair<int, std::vector<uint64_t>>(proto, bitVector));
} else {
SET_BIT((it->second)[rule_id / 63], rule_id % 63);
}
}
// Don't care rules are in all entries. Anyway, this loops is useless if there
// are no rules at all requiring matching on this field.
if (protocols.size() != 0 && dont_care_rules.size() != 0) {
std::vector<uint64_t> bitVector(
FROM_NRULES_TO_NELEMENTS(Iptables::max_rules_));
protocols.insert(std::pair<int, std::vector<uint64_t>>(0, bitVector));
for (auto const &ruleNumber : dont_care_rules) {
for (auto &proto : protocols) {
SET_BIT((proto.second)[ruleNumber / 63], ruleNumber % 63);
}
}
brk = false;
}
return brk;
}
bool Chain::portFromRulesToMap(
const uint8_t &type, std::map<uint16_t, std::vector<uint64_t>> &ports,
const std::vector<std::shared_ptr<ChainRule>> &rules) {
std::vector<uint32_t> dont_care_rules;
uint32_t rule_id;
uint16_t port;
bool brk = true;
for (auto const &rule : rules) {
try {
rule_id = rule->getId();
if (type == SOURCE_TYPE)
port = rule->getSport();
else
port = rule->getDport();
} catch (std::runtime_error re) {
// IP not set: don't care rule.
dont_care_rules.push_back(rule_id);
continue;
}
auto it = ports.find(port);
if (it == ports.end()) {
// First entry
std::vector<uint64_t> bitVector(
FROM_NRULES_TO_NELEMENTS(Iptables::max_rules_));
SET_BIT(bitVector[rule_id / 63], rule_id % 63);
ports.insert(std::pair<uint16_t, std::vector<uint64_t>>(port, bitVector));
} else {
SET_BIT((it->second)[rule_id / 63], rule_id % 63);
}
}
// Don't care rules are in all entries. Anyway, this loop is useless if there
// are no rules at all requiring matching on this field.
if (ports.size() != 0 && dont_care_rules.size() != 0) {
std::vector<uint64_t> bitVector(
FROM_NRULES_TO_NELEMENTS(Iptables::max_rules_));
ports.insert(std::pair<uint16_t, std::vector<uint64_t>>(0, bitVector));
for (auto const &ruleNumber : dont_care_rules) {
for (auto &port : ports) {
SET_BIT((port.second)[ruleNumber / 63], ruleNumber % 63);
}
}
brk = false;
}
return brk;
}
bool Chain::interfaceFromRulesToMap(
const uint8_t &type, std::map<uint16_t, std::vector<uint64_t>> &interfaces,
const std::vector<std::shared_ptr<ChainRule>> &rules, Iptables &iptables) {
std::vector<uint32_t> dont_care_rules;
uint32_t rule_id;
uint16_t interface;
bool brk = true;
for (auto const &rule : rules) {
try {
rule_id = rule->getId();
interface = 0;
if (type == IN_TYPE) {
std::string interface_string = rule->getInIface();
interface = iptables.interfaceNameToIndex(interface_string);
} else {
std::string interface_string = rule->getOutIface();
interface = iptables.interfaceNameToIndex(interface_string);
}
} catch (std::runtime_error re) {
// Interface not set: don't care rule.
dont_care_rules.push_back(rule_id);
continue;
}
auto it = interfaces.find(interface);
if (it == interfaces.end()) {
// First entry
std::vector<uint64_t> bitVector(
FROM_NRULES_TO_NELEMENTS(Iptables::max_rules_));
SET_BIT(bitVector[rule_id / 63], rule_id % 63);
interfaces.insert(
std::pair<uint16_t, std::vector<uint64_t>>(interface, bitVector));
} else {
SET_BIT((it->second)[rule_id / 63], rule_id % 63);
}
}
// Don't care rules are in all entries. Anyway, this loop is useless if there
// are no rules at all requiring matching on this field.
if (interfaces.size() != 0 && dont_care_rules.size() != 0) {
std::vector<uint64_t> bitVector(
FROM_NRULES_TO_NELEMENTS(Iptables::max_rules_));
interfaces.insert(std::pair<uint16_t, std::vector<uint64_t>>(0, bitVector));
for (auto const &ruleNumber : dont_care_rules) {
for (auto &interface : interfaces) {
SET_BIT((interface.second)[ruleNumber / 63], ruleNumber % 63);
}
}
brk = false;
}
return brk;
}
bool Chain::fromRuleToHorusKeyValue(std::shared_ptr<ChainRule> rule,
struct HorusRule &key,
struct HorusValue &value) {
key.setFields = 0;
try {
rule->getConntrack();
return false;
} catch (std::runtime_error) {}
try {
IpAddr ips;
ips.fromString(rule->getSrc());
if (ips.netmask == 32) {
SET_BIT(key.setFields, HorusConst::SRCIP);
key.src_ip = ips.ip;
}
} catch (std::runtime_error) {
}
try {
IpAddr ipd;
ipd.fromString(rule->getDst());
if (ipd.netmask == 32) {
SET_BIT(key.setFields, HorusConst::DSTIP);
key.dst_ip = ipd.ip;
}
} catch (std::runtime_error) {
}
try {
uint8_t proto = Iptables::protocolFromStringToInt(rule->getL4proto());
SET_BIT(key.setFields, HorusConst::L4PROTO);
key.l4proto = proto;
} catch (std::runtime_error) {
}
try {
uint16_t srcport = rule->getSport();
SET_BIT(key.setFields, HorusConst::SRCPORT);
key.src_port = srcport;
} catch (std::runtime_error) {
}
try {
uint16_t dstport = rule->getDport();
SET_BIT(key.setFields, HorusConst::DSTPORT);
key.dst_port = dstport;
} catch (std::runtime_error) {
}
// TODO Check if ACCEPT/DROP semantic is valid
ActionEnum action = rule->getAction();
if (action == ActionEnum::DROP)
value.action = 0;
else
value.action = 1;
value.ruleID = rule->getId();
return true;
}
void Chain::horusFromRulesToMap(
std::map<struct HorusRule, struct HorusValue> &horus,
const std::vector<std::shared_ptr<ChainRule>> &rules) {
struct HorusRule key;
struct HorusValue value;
uint64_t set_fields = 0;
// find match pattern for first rule (e.g. 01101 means ips proto ports set)
int i = 0;
for (auto const &rule : rules) {
if (i >= HorusConst::MAX_RULE_SIZE_FOR_HORUS)
break;
if(!fromRuleToHorusKeyValue(rule, key, value))
return;
if (i == 0) {
if (key.setFields == 0) {
break;
}
set_fields = key.setFields;
}
if (key.setFields != set_fields) {
break;
}
horus.insert(std::pair<struct HorusRule, struct HorusValue>(key, value));
i++;
}
}
bool Chain::conntrackFromRulesToMap(
std::map<uint8_t, std::vector<uint64_t>> &statusMap,
const std::vector<std::shared_ptr<ChainRule>> &rules) {
std::vector<uint8_t> statesVector({NEW, ESTABLISHED, RELATED, INVALID});
uint32_t rule_id;
uint8_t rule_state;
bool conntrackRulePresent = false;
for (auto const &rule : rules) {
try {
rule_state = ChainRuleConntrackEnumToInt(rule->getConntrack());
conntrackRulePresent = true;
} catch (...) {
}
}
if (!conntrackRulePresent)
return false;
for (uint8_t state : statesVector) {
std::vector<uint64_t> bitVector(
FROM_NRULES_TO_NELEMENTS(Iptables::max_rules_));
for (auto const &rule : rules) {
try {
rule_id = rule->getId();
rule_state = ChainRuleConntrackEnumToInt(rule->getConntrack());
if (rule_state == state)
SET_BIT(bitVector[rule_id / 63], rule_id % 63);
} catch (std::runtime_error re) {
// wildcard rule, set bit to 1
SET_BIT(bitVector[rule_id / 63], rule_id % 63);
}
}
statusMap.insert(
std::pair<uint8_t, std::vector<uint64_t>>(state, bitVector));
}
return false;
}
bool Chain::flagsFromRulesToMap(
std::vector<std::vector<uint64_t>> &flags,
const std::vector<std::shared_ptr<ChainRule>> &rules) {
flags.clear();
// Preliminary check if there are rules requiring match on flags.
bool are_flags_present = false;
for (auto const &rule : rules) {
try {
rule->getTcpflags();
are_flags_present = true;
break;
} catch (std::runtime_error re) {
continue;
}
}
if (!are_flags_present) {
return false;
}
std::vector<uint64_t> bitVector(
FROM_NRULES_TO_NELEMENTS(Iptables::max_rules_));
for (int j = 0; j < 256; ++j) {
// Flags map is an ARRAY. All entries will be allocated, this requires to
// populate the entire table.
flags.push_back(bitVector);
}
uint8_t flags_set;
uint8_t flags_not_set;
uint32_t rule_id;
for (auto const &rule : rules) {
rule_id = rule->getId();
try {
ChainRule::flagsFromStringToMasks(rule->getTcpflags(), flags_set,
flags_not_set);
} catch (std::runtime_error re) {
// Don't care rule: it has to have a 1 in every entry.
for (auto &flag_entry : flags) {
SET_BIT(flag_entry[rule_id / 63], rule_id % 63);
}
continue;
}
if (flags_set == 0) {
// Default value needed for the next computation.
flags_set = 255;
}
for (int j = 0; j < 256; ++j) {
uint8_t candidate_flag =
Iptables::TcpFlagsLookup::possible_flags_combinations_[j];
if (((candidate_flag & flags_set) == flags_set) &&
((candidate_flag & flags_not_set) == 0)) {
SET_BIT((flags[candidate_flag])[rule_id / 63], rule_id % 63);
}
}
}
// tcp flags current implementation is based on array.
// no break optimization could be performed right now.
return false;
}