2 ==================================================================================
4 Copyright (c) 2018-2019 AT&T Intellectual Property.
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
21 #ifndef NETWORKPROTECTOR_H
22 #define NETWORKPROTECTOR_H
24 #include "sliding_window.hpp"
27 #include <sgnb_addition_request.hpp> // to decode the X2AP payload
28 #include <sgnb_addition_response.hpp> // to respond
31 #define likely(x) __builtin_expect((x), 1)
32 #define unlikely(x) __builtin_expect((x), 0)
35 #define unlikely(x) (x)
38 // each policy corresponds to a specific X2 subscriber profile ID
39 // and applies sliding window logic to UEs in that class (if enforce)
40 class protector_policy {
42 protector_policy(bool enforce=true, int window_size=60, int threshold=10, double block_rate=10): _enforce(enforce), _window_size(window_size), _threshold(threshold), _block_rate(block_rate){
46 _window_ref = std::make_unique<sliding_window>(_window_size);
49 bool _enforce; // do we enforce policy ?
50 int _counter; // count the # of attaching access
51 int _window_size; // time in seconds window for the # of counts
52 int _threshold; // count above which we start enforcing if enforce set
53 double _block_rate; // % of rejecting rate for counter > threshold
54 std::unique_ptr<sliding_window> _window_ref;
55 unsigned long int _req; // number of requests
56 unsigned long int _rej; // number of rejects
65 protector( bool rep=true);
66 protector(bool enforce, int window_size, int threshold, double block_rate, bool rep=true);
67 bool operator()(unsigned char *, size_t , unsigned char *, size_t *);
69 bool configure(bool enforce, int windowSize_, int threshold_, double blockRate_, int id);
70 bool add_policy (bool enforce, int windowSize_, int threshold_, double blockRate_, int id);
71 bool delete_policy(int id);
72 bool query_policy(int , std::vector<double> &);
73 void get_active_policies(std::vector<int> & );
74 bool is_active(int id);
77 bool selectiveBlock(double);
79 long int get_requests(int id) const;
80 long int get_rejects(int id) const;
81 std::string get_error(void) { return error_string;};
85 std::map<int, protector_policy> policy_list;
86 std::unique_ptr<std::mutex> m_access;
88 sgnb_addition_helper sgnb_data;
89 sgnb_addition_request sgnb_req;
90 sgnb_addition_response sgnb_resp;
92 unsigned long int net_requests = 0;
93 unsigned long int net_rejects = 0;
95 std::string error_string;
96 bool report_mode_only;