sync_mikrotik.pl 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use open ":encoding(utf8)";
  7. use Encode;
  8. no warnings 'utf8';
  9. use English;
  10. use base;
  11. use FindBin '$Bin';
  12. use lib "/opt/Eye/scripts";
  13. use strict;
  14. use Time::Local;
  15. use FileHandle;
  16. use Data::Dumper;
  17. use eyelib::config;
  18. use eyelib::main;
  19. use eyelib::cmd;
  20. use Net::Patricia;
  21. use Date::Parse;
  22. use eyelib::net_utils;
  23. use eyelib::database;
  24. use DBI;
  25. use Fcntl qw(:flock);
  26. use Parallel::ForkManager;
  27. use Net::DNS;
  28. #$debug = 1;
  29. open(SELF,"<",$0) or die "Cannot open $0 - $!";
  30. flock(SELF, LOCK_EX|LOCK_NB) or exit 1;
  31. $|=1;
  32. if (IsNotRun($SPID)) { Add_PID($SPID); } else { die "Warning!!! $SPID already runnning!\n"; }
  33. my $fork_count = $cpu_count*10;
  34. #flag for operation status
  35. my $all_ok = 1;
  36. my @gateways =();
  37. #select undeleted mikrotik routers only
  38. if ($ARGV[0]) {
  39. my $router = get_record_sql($dbh,'SELECT * FROM devices WHERE (device_type=2 OR device_type=0) and (user_acl=1 or dhcp=1) and deleted=0 and vendor_id=9 and id='.$ARGV[0]);
  40. if ($router) { push(@gateways,$router); }
  41. } else {
  42. @gateways = get_records_sql($dbh,'SELECT * FROM devices WHERE (device_type=2 OR device_type=0) and (user_acl=1 or dhcp=1) and deleted=0 and vendor_id=9');
  43. }
  44. #все сети организации, работающие по dhcp
  45. my $dhcp_networks = new Net::Patricia;
  46. my %dhcp_conf;
  47. my @subnets=get_records_sql($dbh,'SELECT * FROM subnets WHERE dhcp=1 and office=1 and vpn=0 ORDER BY ip_int_start');
  48. foreach my $subnet (@subnets) {
  49. next if (!$subnet->{gateway});
  50. my $dhcp_info=GetDhcpRange($subnet->{subnet});
  51. $dhcp_networks->add_string($subnet->{subnet},$subnet->{subnet});
  52. $dhcp_conf{$subnet->{subnet}}->{first_pool_ip}=IpToStr($subnet->{dhcp_start});
  53. $dhcp_conf{$subnet->{subnet}}->{last_pool_ip}=IpToStr($subnet->{dhcp_stop});
  54. $dhcp_conf{$subnet->{subnet}}->{relay_ip}=IpToStr($subnet->{gateway});
  55. $dhcp_conf{$subnet->{subnet}}->{gateway}=IpToStr($subnet->{gateway});
  56. #раскрываем подсеть
  57. $dhcp_conf{$subnet->{subnet}}->{network} = $dhcp_info->{network};
  58. $dhcp_conf{$subnet->{subnet}}->{masklen} = $dhcp_info->{masklen};
  59. $dhcp_conf{$subnet->{subnet}}->{first_ip} = $dhcp_info->{first_ip};
  60. $dhcp_conf{$subnet->{subnet}}->{last_ip} = $dhcp_info->{last_ip};
  61. $dhcp_conf{$subnet->{subnet}}->{first_ip_aton}=StrToIp($dhcp_info->{first_ip});
  62. $dhcp_conf{$subnet->{subnet}}->{last_ip_aton}=StrToIp($dhcp_info->{last_ip});
  63. }
  64. my $pm = Parallel::ForkManager->new($fork_count);
  65. #save changed records
  66. my @changes_found = get_records_sql($dbh,"SELECT id FROM User_auth WHERE changed=1");
  67. foreach my $gate (@gateways) {
  68. next if (!$gate);
  69. my $gate_ident = $gate->{device_name}." [$gate->{ip}]:: ";
  70. $pm->start and next;
  71. $dbh = init_db();
  72. my @cmd_list=();
  73. $gate = netdev_set_auth($gate);
  74. $gate->{login}.='+ct400w';
  75. my $t = netdev_login($gate);
  76. if (!$t) {
  77. log_error($gate_ident."Login to $gate->{device_name} [$gate->{ip}] failed! Skip gateway.");
  78. $dbh->disconnect();
  79. $pm->finish;
  80. next;
  81. }
  82. my $router_name=$gate->{device_name};
  83. my $router_ip=$gate->{ip};
  84. my $shaper_enabled = $gate->{queue_enabled};
  85. my $connected_users_only = $gate->{connected_user_only};
  86. my @changed_ref=();
  87. #все сети роутера, которые к нему подключены по информации из БД - Patricia Object
  88. my $connected_users = new Net::Patricia;
  89. #сети, которые должен отдавать роутер по dhcp - simple hash
  90. my %connected_nets_hash;
  91. #исключения из авторизации хот-спота
  92. my %hotspot_exceptions;
  93. my @lan_int=();
  94. my @wan_int=();
  95. my @l3_int = get_records_sql($dbh,'SELECT * FROM device_l3_interfaces WHERE device_id='.$gate->{'id'});
  96. foreach my $l3 (@l3_int) {
  97. $l3->{'name'}=~s/\"//g;
  98. if ($l3->{'interface_type'} eq '0') { push(@lan_int,$l3->{'name'}); }
  99. if ($l3->{'interface_type'} eq '1') { push(@wan_int,$l3->{'name'}); }
  100. }
  101. #формируем список подключенных к роутеру сетей
  102. my @gw_subnets = get_records_sql($dbh,"SELECT gateway_subnets.*,subnets.subnet FROM gateway_subnets LEFT JOIN subnets ON gateway_subnets.subnet_id = subnets.id WHERE gateway_subnets.device_id=".$gate->{'id'});
  103. if (@gw_subnets and scalar @gw_subnets) {
  104. foreach my $gw_subnet (@gw_subnets) {
  105. if ($gw_subnet and $gw_subnet->{'subnet'}) {
  106. $connected_users->add_string($gw_subnet->{'subnet'});
  107. $connected_nets_hash{$gw_subnet->{'subnet'}} = $gw_subnet;
  108. }
  109. }
  110. }
  111. #dhcp config
  112. if ($gate->{dhcp}) {
  113. #все сети роутера, которые к нему подключены напрямую фактически - Patricia Object
  114. my $fact_connected_nets = new Net::Patricia;
  115. #интерфейсы, которые будут использоваться для конфигурирования dhcp-сервера
  116. my @work_int=();
  117. #dhcp-сервер, обрабатывающий запросы от dhcp-relay
  118. my @relayed_dhcp = netdev_cmd($gate,$t,"/ip dhcp-server print terse without-paging where relay=255.255.255.255",1);
  119. my $relayed_dhcp_server;
  120. my $relayed_dhcp_interface;
  121. if (@relayed_dhcp and scalar @relayed_dhcp) {
  122. my $dhcp_server = $relayed_dhcp[0];
  123. if ($dhcp_server and $dhcp_server=~/name=(\S+)\s+/i) { $relayed_dhcp_server = $1; }
  124. if ($dhcp_server and $dhcp_server=~/interface=(\S+)\s+/i) { $relayed_dhcp_interface= $1; }
  125. }
  126. #ищем интерфейсы, на которых поднята необходимая для dhcp сервера сеть
  127. foreach my $int (@lan_int) { #interface loop
  128. next if (!$int);
  129. $int=trim($int);
  130. #get ip addr at interface
  131. my @int_addr=netdev_cmd($gate,$t,'/ip address print terse without-paging where interface='.$int,1);
  132. log_debug($gate_ident."Get interfaces: ".Dumper(\@int_addr));
  133. my $found_subnet;
  134. foreach my $int_str(@int_addr) {
  135. $int_str=trim($int_str);
  136. next if (!$int_str);
  137. if ($int_str=~/\s+address=(\S*)\s+/i) {
  138. my $gate_interface=$1;
  139. if ($gate_interface) {
  140. my $gate_ip=$gate_interface;
  141. my $gate_net = GetDhcpRange($gate_interface);
  142. $fact_connected_nets->add_string($gate_net->{network}."/".$gate_net->{masklen});
  143. $gate_ip=~s/\/.*$//;
  144. #search for first match
  145. $found_subnet=$dhcp_networks->match_string($gate_ip);
  146. last;
  147. }
  148. }
  149. }
  150. if (!$found_subnet) { db_log_verbose($dbh,$gate_ident."DHCP subnet for interface $int not found! Skip interface."); next; }
  151. my $dhcp_state;
  152. $dhcp_state->{subnet}=$found_subnet;
  153. $dhcp_state->{interface}=$int;
  154. #формируем список локальных интерфейсов, на котором есть dhcp-сеть
  155. push(@work_int,$dhcp_state);
  156. }
  157. #формируем список сетей, не подключенных к роутеру непосредтственно
  158. my @relayed_subnets=();
  159. foreach my $gw_subnet (keys %connected_nets_hash ) {
  160. next if (!$gw_subnet);
  161. next if ($fact_connected_nets->match_string($gw_subnet));
  162. push(@relayed_subnets,$gw_subnet);
  163. }
  164. if (scalar @relayed_subnets and $relayed_dhcp_interface) {
  165. my $dhcp_state;
  166. $dhcp_state->{interface} = $relayed_dhcp_interface;
  167. $dhcp_state->{subnet} = join(",",@relayed_subnets);
  168. push(@work_int,$dhcp_state);
  169. }
  170. #interface dhcp loop
  171. foreach my $dhcpd_int (@work_int) {
  172. my $found_subnet=$dhcpd_int->{subnet};
  173. my @dhcp_subnets = split(/\,/,$found_subnet);
  174. my $int=$dhcpd_int->{interface};
  175. db_log_verbose($dbh,$gate_ident."Analyze interface $int. Found: ".Dumper($dhcp_conf{$found_subnet}));
  176. #fetch current dhcp records
  177. my @ret_static_leases=netdev_cmd($gate,$t,'/ip dhcp-server lease print terse without-paging where server=dhcp-'.$int,1);
  178. log_debug($gate_ident."Get dhcp leases:".Dumper(\@ret_static_leases));
  179. my @current_static_leases=();
  180. foreach my $str (@ret_static_leases) {
  181. next if (!$str);
  182. $str=trim($str);
  183. if ($str=~/^\d/) {
  184. log_debug($gate_ident."Found current static lease record: ".$str);
  185. push(@current_static_leases,$str);
  186. }
  187. }
  188. #select users for this interface
  189. my @auth_records=();
  190. foreach my $dhcp_subnet (@dhcp_subnets) {
  191. next if (!$dhcp_subnet);
  192. next if (!exists $dhcp_conf{$dhcp_subnet});
  193. my @tmp1=get_records_sql($dbh,"SELECT * from User_auth WHERE dhcp=1 and `ip_int`>=".$dhcp_conf{$dhcp_subnet}->{first_ip_aton}." and `ip_int`<=".$dhcp_conf{$dhcp_subnet}->{last_ip_aton}." and deleted=0 and ou_id !=".$default_user_ou_id." and ou_id !=".$default_hotspot_ou_id." ORDER BY ip_int");
  194. push(@auth_records,@tmp1);
  195. undef @tmp1;
  196. }
  197. my %leases;
  198. foreach my $lease (@auth_records) {
  199. next if (!$lease);
  200. next if (!$lease->{mac});
  201. next if (!$lease->{ip});
  202. my $found_subnet = $dhcp_networks->match_string($lease->{ip});
  203. next if (!$found_subnet);
  204. next if ($lease->{ip} eq $dhcp_conf{$found_subnet}->{relay_ip});
  205. $leases{$lease->{ip}}{ip}=$lease->{ip};
  206. $leases{$lease->{ip}}{comment}=$lease->{id};
  207. $leases{$lease->{ip}}{id}=$lease->{id};
  208. $leases{$lease->{ip}}{dns_name}=$lease->{dns_name};
  209. if ($lease->{comments}) { $leases{$lease->{ip}}{comment}=translit($lease->{comments}); }
  210. $leases{$lease->{ip}}{mac}=uc(mac_splitted($lease->{mac}));
  211. if ($lease->{dhcp_acl}) {
  212. $leases{$lease->{ip}}{acl}=trim($lease->{dhcp_acl});
  213. $leases{$lease->{ip}}{acl}=~s/;/,/g;
  214. if ($leases{$lease->{ip}}{acl}=~/hotspot\-free/) {
  215. $hotspot_exceptions{$leases{$lease->{ip}}{mac}}=$leases{$lease->{ip}}{mac};
  216. $hotspot_exceptions{$leases{$lease->{ip}}{mac}}=$leases{$lease->{ip}}{comment} if ($leases{$lease->{ip}}{comment});
  217. }
  218. }
  219. if ($lease->{dhcp_option_set}) {
  220. $leases{$lease->{ip}}{dhcp_option_set}=trim($lease->{dhcp_option_set});
  221. }
  222. $leases{$lease->{ip}}{acl}='' if (!$leases{$lease->{ip}}{acl});
  223. $leases{$lease->{ip}}{dhcp_option_set}='' if (!$leases{$lease->{ip}}{dhcp_option_set});
  224. }
  225. my %active_leases;
  226. foreach my $lease (@current_static_leases) {
  227. my @words = split(/\s+/,$lease);
  228. my %tmp_lease;
  229. if ($lease=~/^(\d*)\s+/) { $tmp_lease{id}=$1; };
  230. next if (!defined($tmp_lease{id}));
  231. foreach my $option (@words) {
  232. next if (!$option);
  233. $option=trim($option);
  234. next if (!$option);
  235. my @tmp = split(/\=/,$option);
  236. my $token = trim($tmp[0]);
  237. my $value = trim($tmp[1]);
  238. next if (!$token);
  239. next if (!$value);
  240. $value=~s/\"//g;
  241. if ($token=~/^address$/i) { $tmp_lease{ip}=GetIP($value); }
  242. if ($token=~/^mac-address$/i) { $tmp_lease{mac}=uc(mac_splitted($value)); }
  243. if ($token=~/^address-lists$/i) { $tmp_lease{acl}=$value; }
  244. if ($token=~/^dhcp-option-set$/i) { $tmp_lease{dhcp_option_set}=$value; }
  245. }
  246. next if (!$tmp_lease{ip});
  247. next if (!$tmp_lease{mac});
  248. next if ($lease=~/^(\d*)\s+D\s+/);
  249. $active_leases{$tmp_lease{ip}}{ip}=$tmp_lease{ip};
  250. $active_leases{$tmp_lease{ip}}{mac}=$tmp_lease{mac};
  251. $active_leases{$tmp_lease{ip}}{id}=$tmp_lease{id};
  252. $active_leases{$tmp_lease{ip}}{acl}='';
  253. $active_leases{$tmp_lease{ip}}{dhcp_option_set}='';
  254. if ($tmp_lease{acl}) {
  255. $active_leases{$tmp_lease{ip}}{acl}=$tmp_lease{acl};
  256. }
  257. if ($tmp_lease{dhcp_option_set}) {
  258. $active_leases{$tmp_lease{ip}}{dhcp_option_set}=$tmp_lease{dhcp_option_set};
  259. }
  260. }
  261. log_debug($gate_ident."Active leases: ".Dumper(\%active_leases));
  262. #sync state
  263. foreach my $ip (keys %active_leases) {
  264. if (!exists $leases{$ip}) {
  265. db_log_verbose($dbh,$gate_ident."Address $ip not found in stat. Remove from router.");
  266. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  267. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  268. push(@cmd_list,'/ip arp remove [find address='.$ip.']');
  269. next;
  270. }
  271. if ($leases{$ip}{mac}!~/$active_leases{$ip}{mac}/i) {
  272. db_log_verbose($dbh,$gate_ident."Mac-address mismatch for ip $ip. stat: $leases{$ip}{mac} active: $active_leases{$ip}{mac}. Remove lease from router.");
  273. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  274. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  275. push(@cmd_list,'/ip arp remove [find address='.$ip.']');
  276. next;
  277. }
  278. if (!(!$leases{$ip}{acl} and !$active_leases{$ip}{acl}) and $leases{$ip}{acl} ne $active_leases{$ip}{acl}) {
  279. db_log_error($dbh,$gate_ident."Acl mismatch for ip $ip. stat: $leases{$ip}{acl} active: $active_leases{$ip}{acl}. Remove lease from router.");
  280. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  281. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  282. push(@cmd_list,'/ip arp remove [find address='.$ip.']');
  283. next;
  284. }
  285. if (!(!$leases{$ip}{dhcp_option_set} and !$active_leases{$ip}{dhcp_option_set}) and $leases{$ip}{dhcp_option_set} ne $active_leases{$ip}{dhcp_option_set}) {
  286. db_log_error($dbh,$gate_ident."DHCP option-set mismatch for ip $ip. stat: $leases{$ip}{dhcp_option_set} active: $active_leases{$ip}{dhcp_option_set}. Remove lease from router.");
  287. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  288. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  289. push(@cmd_list,'/ip arp remove [find address='.$ip.']');
  290. next;
  291. }
  292. }
  293. foreach my $ip (keys %leases) {
  294. my $acl='';
  295. if ($leases{$ip}{acl}) { $acl = 'address-lists='.$leases{$ip}{acl}; }
  296. my $dhcp_option_set='';
  297. if ($leases{$ip}{dhcp_option_set}) { $dhcp_option_set = 'dhcp-option-set='.$leases{$ip}{dhcp_option_set}; }
  298. my $comment = $leases{$ip}{comment};
  299. $comment =~s/\=//g;
  300. my $dns_name='';
  301. if ($leases{$ip}{dns_name}) { $dns_name = $leases{$ip}{dns_name}; }
  302. $dns_name =~s/\=//g;
  303. if ($dns_name) { $comment = 'comment="'.$dns_name." - ".$comment.'"'; } else { $comment = 'comment="'.$comment.'"'; }
  304. if (!exists $active_leases{$ip}) {
  305. db_log_verbose($dbh,$gate_ident."Address $ip not found in router. Create static lease record.");
  306. #remove static and dynamic records for mac
  307. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where mac-address='.uc($leases{$ip}{mac}).' ] do={/ip dhcp-server lease remove $i};');
  308. push(@cmd_list,'/ip dhcp-server lease remove [find mac-address='.uc($leases{$ip}{mac}).']');
  309. #remove current ip binding
  310. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  311. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  312. #add new bind
  313. push(@cmd_list,'/ip dhcp-server lease add address='.$ip.' mac-address='.$leases{$ip}{mac}.' '.$acl.' '.$dhcp_option_set.' server=dhcp-'.$int.' '.$comment);
  314. #clear arp record
  315. push(@cmd_list,'/ip arp remove [find mac-address='.uc($leases{$ip}{mac}).']');
  316. next;
  317. }
  318. if ($leases{$ip}{mac}!~/$active_leases{$ip}{mac}/i) {
  319. db_log_error($dbh,$gate_ident."Mac-address mismatch for ip $ip. stat: $leases{$ip}{mac} active: $active_leases{$ip}{mac}. Create static lease record.");
  320. #remove static and dynamic records for mac
  321. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where mac-address='.uc($leases{$ip}{mac}).' ] do={/ip dhcp-server lease remove $i};');
  322. push(@cmd_list,'/ip dhcp-server lease remove [find mac-address='.uc($leases{$ip}{mac}).']');
  323. #remove current ip binding
  324. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where address='.$ip.' ] do={/ip dhcp-server lease remove $i};');
  325. push(@cmd_list,'/ip dhcp-server lease remove [find address='.$ip.']');
  326. #add new bind
  327. push(@cmd_list,'/ip dhcp-server lease add address='.$ip.' mac-address='.$leases{$ip}{mac}.' '.$acl.' '.$dhcp_option_set.' server=dhcp-'.$int.' '.$comment);
  328. #clear arp record
  329. push(@cmd_list,'/ip arp remove [find mac-address='.uc($leases{$ip}{mac}).']');
  330. next;
  331. }
  332. if (!(!$leases{$ip}{acl} and !$active_leases{$ip}{acl}) and $leases{$ip}{acl} ne $active_leases{$ip}{acl}) {
  333. db_log_error($dbh,$gate_ident."Acl mismatch for ip $ip. stat: $leases{$ip}{acl} active: $active_leases{$ip}{acl}. Create static lease record.");
  334. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where mac-address='.uc($leases{$ip}{mac}).' ] do={/ip dhcp-server lease remove $i};');
  335. push(@cmd_list,'/ip dhcp-server lease remove [find mac-address='.uc($leases{$ip}{mac}).']');
  336. push(@cmd_list,'/ip dhcp-server lease add address='.$ip.' mac-address='.$leases{$ip}{mac}.' '.$acl.' '.$dhcp_option_set.' server=dhcp-'.$int.' '.$comment);
  337. #clear arp record
  338. push(@cmd_list,'/ip arp remove [find mac-address='.uc($leases{$ip}{mac}).']');
  339. next;
  340. }
  341. if (!(!$leases{$ip}{dhcp_option_set} and !$active_leases{$ip}{dhcp_option_set}) and $leases{$ip}{dhcp_option_set} ne $active_leases{$ip}{dhcp_option_set}) {
  342. db_log_error($dbh,$gate_ident."Acl mismatch for ip $ip. stat: $leases{$ip}{acl} active: $active_leases{$ip}{acl}. Create static lease record.");
  343. push(@cmd_list,':foreach i in [/ip dhcp-server lease find where mac-address='.uc($leases{$ip}{mac}).' ] do={/ip dhcp-server lease remove $i};');
  344. push(@cmd_list,'/ip dhcp-server lease remove [find mac-address='.uc($leases{$ip}{mac}).']');
  345. push(@cmd_list,'/ip dhcp-server lease add address='.$ip.' mac-address='.$leases{$ip}{mac}.' '.$acl.' '.$dhcp_option_set.' server=dhcp-'.$int.' '.$comment);
  346. #clear arp record
  347. push(@cmd_list,'/ip arp remove [find mac-address='.uc($leases{$ip}{mac}).']');
  348. next;
  349. }
  350. }
  351. }#end interface dhcp loop
  352. #hotspot exceptions
  353. my @ret_hotspot_bindings=netdev_cmd($gate,$t,'/ip hotspot ip-binding print terse without-paging where type=bypassed',1);
  354. my %actual_hotspot_bindings;
  355. foreach my $row (@ret_hotspot_bindings) {
  356. next if (!$row or $row !~ /^\s*\d/);
  357. my %data;
  358. # Используем регулярное выражение для извлечения пар ключ=значение
  359. while (/\b(\S+?)=([^\s=]+(?:\s(?!\S+=)[^\s=]+)*)/g) {
  360. my ($key, $value) = ($1, $2);
  361. $data{$key} = $value;
  362. }
  363. if (exists $data{'mac-address'}) {
  364. $actual_hotspot_bindings{$data{'mac-address'}} = $data{'mac-address'};
  365. $actual_hotspot_bindings{$data{'mac-address'}} = $data{comment} if (exists $data{comment});
  366. }
  367. }
  368. #update binding
  369. foreach my $actual_mac (keys %actual_hotspot_bindings) {
  370. if (!exists $hotspot_exceptions{$actual_mac}) {
  371. db_log_verbose($dbh,$gate_ident."Address $actual_mac removed from hotspot ip-binding");
  372. push(@cmd_list,':foreach i in [/ip hotspot ip-binding where mac-address='.uc($actual_mac).' ] do={//ip hotspot ip-binding remove $i};');
  373. }
  374. }
  375. foreach my $actual_mac (keys %hotspot_exceptions) {
  376. if (!exists $actual_hotspot_bindings{$actual_mac}) {
  377. db_log_verbose($dbh,$gate_ident."Address $actual_mac added to hotspot ip-binding");
  378. push(@cmd_list,'/ip hotspot ip-binding add mac-address='.uc($actual_mac).' type=bypassed comment="'.$hotspot_exceptions{$actual_mac});
  379. }
  380. }
  381. }#end dhcp config
  382. #access lists config
  383. if ($gate->{user_acl}) {
  384. db_log_verbose($dbh,$gate_ident."Sync user state at router $router_name [".$router_ip."] started.");
  385. #get userid list
  386. my $user_auth_sql="SELECT User_auth.ip, User_auth.filter_group_id, User_auth.queue_id, User_auth.id
  387. FROM User_auth, User_list
  388. WHERE User_auth.user_id = User_list.id
  389. AND User_auth.deleted =0
  390. AND User_auth.enabled =1
  391. AND User_auth.blocked =0
  392. AND User_list.blocked =0
  393. AND User_list.enabled =1
  394. AND User_auth.ou_id <> $default_hotspot_ou_id
  395. ORDER BY ip_int";
  396. my @authlist_ref = get_records_sql($dbh,$user_auth_sql);
  397. my %users;
  398. my %lists;
  399. my %found_users;
  400. foreach my $row (@authlist_ref) {
  401. if ($connected_users_only) { next if (!$connected_users->match_string($row->{ip})); }
  402. #skip not office ip's
  403. next if (!$office_networks->match_string($row->{ip}));
  404. $found_users{$row->{'id'}}=$row->{ip};
  405. #filter group acl's
  406. $users{'group_'.$row->{filter_group_id}}->{$row->{ip}}=1;
  407. $users{'group_all'}->{$row->{ip}}=1;
  408. $lists{'group_'.$row->{filter_group_id}}=1;
  409. #queue acl's
  410. if ($row->{queue_id}) { $users{'queue_'.$row->{queue_id}}->{$row->{ip}}=1; }
  411. }
  412. log_debug($gate_ident."Users status:".Dumper(\%users));
  413. #full list
  414. $lists{'group_all'}=1;
  415. #get queue list
  416. my @queuelist_ref = get_records_sql($dbh,"SELECT * FROM Queue_list");
  417. my %queues;
  418. foreach my $row (@queuelist_ref) {
  419. $lists{'queue_'.$row->{id}}=1;
  420. next if ((!$row->{Download}) and !($row->{Upload}));
  421. $queues{'queue_'.$row->{id}}{id}=$row->{id};
  422. $queues{'queue_'.$row->{id}}{down}=$row->{Download};
  423. $queues{'queue_'.$row->{id}}{up}=$row->{Upload};
  424. }
  425. log_debug($gate_ident."Queues status:".Dumper(\%queues));
  426. my @filter_instances = get_records_sql($dbh,"SELECT * FROM filter_instances");
  427. my @filterlist_ref = get_records_sql($dbh,"SELECT * FROM Filter_list where type=0");
  428. my %filters;
  429. my %dyn_filters;
  430. my $max_filter_rec = get_record_sql($dbh,"SELECT MAX(id) FROM Filter_list");
  431. my $max_filter_id = $max_filter_rec->{id};
  432. my $dyn_filters_base = $max_filter_id+1000;
  433. my $dyn_filters_index = $dyn_filters_base;
  434. foreach my $row (@filterlist_ref) {
  435. #if dst - ip address
  436. if (is_ip($row->{dst})) {
  437. $filters{$row->{id}}->{id}=$row->{id};
  438. $filters{$row->{id}}->{proto}=$row->{proto};
  439. $filters{$row->{id}}->{dst}=$row->{dst};
  440. $filters{$row->{id}}->{dstport}=$row->{dstport};
  441. $filters{$row->{id}}->{srcport}=$row->{srcport};
  442. #set false for dns dst flag
  443. $filters{$row->{id}}->{dns_dst}=0;
  444. } else {
  445. #if dst not ip - check dns record
  446. my @dns_record=ResolveNames($row->{dst},undef);
  447. my $resolved_ips = (scalar @dns_record>0);
  448. next if (!$resolved_ips);
  449. foreach my $resolved_ip (sort @dns_record) {
  450. next if (!$resolved_ip);
  451. #enable dns dst filters
  452. $filters{$row->{id}}->{dns_dst}=1;
  453. #add dynamic dns filter
  454. $filters{$dyn_filters_index}->{id}=$row->{id};
  455. $filters{$dyn_filters_index}->{proto}=$row->{proto};
  456. $filters{$dyn_filters_index}->{dst}=$resolved_ip;
  457. $filters{$dyn_filters_index}->{dstport}=$row->{dstport};
  458. $filters{$dyn_filters_index}->{srcport}=$row->{srcport};
  459. $filters{$dyn_filters_index}->{dns_dst}=0;
  460. #save new filter dns id for original filter id
  461. push(@{$dyn_filters{$row->{id}}},$dyn_filters_index);
  462. $dyn_filters_index++;
  463. }
  464. }
  465. }
  466. log_debug($gate_ident."Filters status:". Dumper(\%filters));
  467. log_debug($gate_ident."DNS-filters status:". Dumper(\%dyn_filters));
  468. #clean unused filter records
  469. do_sql($dbh,"DELETE FROM Group_filters WHERE group_id NOT IN (SELECT id FROM Group_list)");
  470. do_sql($dbh,"DELETE FROM Group_filters WHERE filter_id NOT IN (SELECT id FROM Filter_list)");
  471. my @groups_list = get_records_sql($dbh,"SELECT * FROM Group_list");
  472. my %groups;
  473. foreach my $group (@groups_list) { $groups{'group_'.$group->{id}}=$group; }
  474. my @grouplist_ref = get_records_sql($dbh,"SELECT `group_id`,`filter_id`,`order`,`action` FROM Group_filters ORDER BY Group_filters.group_id,Group_filters.order");
  475. my %group_filters;
  476. my $index = 0;
  477. my $cur_group;
  478. foreach my $row (@grouplist_ref) {
  479. if (!$cur_group) { $cur_group = $row->{group_id}; }
  480. if ($cur_group != $row->{group_id}) {
  481. $index = 0;
  482. $cur_group = $row->{group_id};
  483. }
  484. #if dst dns filter not found
  485. if (!$filters{$row->{filter_id}}->{dns_dst}) {
  486. $group_filters{'group_'.$row->{group_id}}->{$index}->{filter_id}=$row->{filter_id};
  487. $group_filters{'group_'.$row->{group_id}}->{$index}->{action}=$row->{action};
  488. $index++;
  489. } else {
  490. #if found dns dst filters - add
  491. if (exists $dyn_filters{$row->{filter_id}}) {
  492. my @dyn_ips = @{$dyn_filters{$row->{filter_id}}};
  493. if (scalar @dyn_ips >0) {
  494. for (my $i = 0; $i < scalar @dyn_ips; $i++) {
  495. $group_filters{'group_'.$row->{group_id}}->{$index}->{filter_id}=$dyn_ips[$i];
  496. $group_filters{'group_'.$row->{group_id}}->{$index}->{action}=$row->{action};
  497. $index++;
  498. }
  499. }
  500. }
  501. }
  502. }
  503. log_debug($gate_ident."Group filters: ".Dumper(\%group_filters));
  504. my %cur_users;
  505. foreach my $group_name (keys %lists) {
  506. my @address_lists=netdev_cmd($gate,$t,'/ip firewall address-list print terse without-paging where list='.$group_name,1);
  507. log_debug($gate_ident."Get address lists:".Dumper(\@address_lists));
  508. foreach my $row (@address_lists) {
  509. $row=trim($row);
  510. next if (!$row);
  511. my @address=split(' ',$row);
  512. foreach my $row (@address) {
  513. if ($row=~/address\=(.*)/i) { $cur_users{$group_name}{$1}=1; }
  514. }
  515. }
  516. }
  517. #new-ips
  518. foreach my $group_name (keys %users) {
  519. foreach my $user_ip (keys %{$users{$group_name}}) {
  520. if (!exists($cur_users{$group_name}{$user_ip})) {
  521. db_log_verbose($dbh,$gate_ident."Add user with ip: $user_ip to access-list $group_name");
  522. push(@cmd_list,"/ip firewall address-list add address=".$user_ip." list=".$group_name);
  523. }
  524. }
  525. }
  526. #old-ips
  527. foreach my $group_name (keys %cur_users) {
  528. foreach my $user_ip (keys %{$cur_users{$group_name}}) {
  529. if (!exists($users{$group_name}{$user_ip})) {
  530. db_log_verbose($dbh,$gate_ident."Remove user with ip: $user_ip from access-list $group_name");
  531. push(@cmd_list,":foreach i in [/ip firewall address-list find where address=".$user_ip." and list=".$group_name."] do={/ip firewall address-list remove \$i};");
  532. }
  533. }
  534. }
  535. timestamp;
  536. #sync firewall rules
  537. #sync group chains
  538. foreach my $filter_instance (@filter_instances) {
  539. my $instance_name = 'Users';
  540. if ($filter_instance->{id}>1) {
  541. $instance_name = 'Users-'.$filter_instance->{name};
  542. #check filter instance exist at gateway
  543. my $instance_ok = get_record_sql($dbh,"SELECT * FROM device_filter_instances WHERE device_id=$gate->{'id'} AND instance_id=$filter_instance->{id}");
  544. #skip insatnce if not found
  545. if (!$instance_ok) { next; }
  546. }
  547. my @chain_list=netdev_cmd($gate,$t,'/ip firewall filter print terse without-paging where chain='.$instance_name.' and action=jump',1);
  548. log_debug($gate_ident."Get firewall chains:".Dumper(\@chain_list));
  549. my %cur_chain;
  550. foreach my $jump_list (@chain_list) {
  551. next if (!$jump_list);
  552. $jump_list=trim($jump_list);
  553. if ($jump_list=~/jump-target=(\S*)\s+/i) {
  554. if ($1) { $cur_chain{$1}++; }
  555. }
  556. }
  557. #old chains
  558. foreach my $group_name (keys %cur_chain) {
  559. if (!exists($group_filters{$group_name}) or $groups{$group_name}->{instance_id} ne $filter_instance->{id}) {
  560. push (@cmd_list,":foreach i in [/ip firewall filter find where chain=".$instance_name." and action=jump and jump-target=".$group_name."] do={/ip firewall filter remove \$i};");
  561. } else {
  562. if ($cur_chain{$group_name} != 2) {
  563. push (@cmd_list,":foreach i in [/ip firewall filter find where chain=".$instance_name." and action=jump and jump-target=".$group_name."] do={/ip firewall filter remove \$i};");
  564. push (@cmd_list,"/ip firewall filter add chain=".$instance_name." action=jump jump-target=".$group_name." src-address-list=".$group_name);
  565. push (@cmd_list,"/ip firewall filter add chain=".$instance_name." action=jump jump-target=".$group_name." dst-address-list=".$group_name);
  566. }
  567. }
  568. }
  569. #new chains
  570. foreach my $group_name (keys %group_filters) {
  571. if (!exists($cur_chain{$group_name}) and $groups{$group_name}->{instance_id} eq $filter_instance->{id}) {
  572. push (@cmd_list,"/ip firewall filter add chain=".$instance_name." action=jump jump-target=".$group_name." src-address-list=".$group_name);
  573. push (@cmd_list,"/ip firewall filter add chain=".$instance_name." action=jump jump-target=".$group_name." dst-address-list=".$group_name);
  574. }
  575. }
  576. }
  577. my %chain_rules;
  578. foreach my $group_name (sort keys %group_filters) {
  579. next if (!$group_name);
  580. next if (!exists($group_filters{$group_name}));
  581. my %group_filter = %{$group_filters{$group_name}};
  582. foreach my $filter_index (sort keys %group_filter) {
  583. my $filter = $group_filter{$filter_index};
  584. my $filter_id=$filter->{filter_id};
  585. next if (!$filters{$filter_id});
  586. next if ($filters{$filter_id}->{dns_dst});
  587. my $src_rule='chain='.$group_name;
  588. my $dst_rule='chain='.$group_name;
  589. if ($filter->{action}) {
  590. $src_rule=$src_rule." action=accept";
  591. $dst_rule=$dst_rule." action=accept";
  592. } else {
  593. $src_rule=$src_rule." action=reject";
  594. $dst_rule=$dst_rule." action=reject";
  595. }
  596. if ($filters{$filter_id}->{proto} and ($filters{$filter_id}->{proto}!~/all/i)) {
  597. $src_rule=$src_rule." protocol=".$filters{$filter_id}->{proto};
  598. $dst_rule=$dst_rule." protocol=".$filters{$filter_id}->{proto};
  599. }
  600. if ($filters{$filter_id}->{dst} and $filters{$filter_id}->{dst} ne '0/0') {
  601. $src_rule=$src_rule." src-address=".trim($filters{$filter_id}->{dst});
  602. $dst_rule=$dst_rule." dst-address=".trim($filters{$filter_id}->{dst});
  603. }
  604. #dstport and srcport
  605. if (!$filters{$filter_id}->{dstport}) { $filters{$filter_id}->{dstport}=0; }
  606. if (!$filters{$filter_id}->{srcport}) { $filters{$filter_id}->{srcport}=0; }
  607. if ($filters{$filter_id}->{dstport} ne '0' and $filters{$filter_id}->{srcport} ne '0') {
  608. $src_rule=$src_rule." dst-port=".trim($filters{$filter_id}->{srcport})." src-port=".trim($filters{$filter_id}->{dstport});
  609. $dst_rule=$dst_rule." src-port=".trim($filters{$filter_id}->{srcport})." dst-port=".trim($filters{$filter_id}->{dstport});
  610. }
  611. if ($filters{$filter_id}->{dstport} eq '0' and $filters{$filter_id}->{srcport} ne '0') {
  612. $src_rule=$src_rule." dst-port=".trim($filters{$filter_id}->{srcport});
  613. $dst_rule=$dst_rule." src-port=".trim($filters{$filter_id}->{srcport});
  614. }
  615. if ($filters{$filter_id}->{dstport} ne '0' and $filters{$filter_id}->{srcport} eq '0') {
  616. $src_rule=$src_rule." src-port=".trim($filters{$filter_id}->{dstport});
  617. $dst_rule=$dst_rule." dst-port=".trim($filters{$filter_id}->{dstport});
  618. }
  619. if ($src_rule ne $dst_rule) {
  620. push(@{$chain_rules{$group_name}},$src_rule);
  621. push(@{$chain_rules{$group_name}},$dst_rule);
  622. } else {
  623. push(@{$chain_rules{$group_name}},$src_rule);
  624. }
  625. }
  626. }
  627. #chain filters
  628. foreach my $group_name (sort keys %group_filters) {
  629. next if (!$group_name);
  630. my @get_filter=netdev_cmd($gate,$t,'/ip firewall filter print terse without-paging where chain='.$group_name,1);
  631. chomp(@get_filter);
  632. my @cur_filter=();
  633. my $chain_ok=1;
  634. foreach (my $f_index=0; $f_index<scalar(@get_filter); $f_index++) {
  635. my $filter_str=trim($get_filter[$f_index]);
  636. next if (!$filter_str);
  637. next if ($filter_str!~/^(\d){1,3}/);
  638. $filter_str=~s/[^[:ascii:]]//g;
  639. $filter_str=~s/^\d{1,3}\s+//;
  640. $filter_str=trim($filter_str);
  641. next if (!$filter_str);
  642. push(@cur_filter,$filter_str);
  643. }
  644. log_debug($gate_ident."Current filters:".Dumper(\@cur_filter));
  645. log_debug($gate_ident."New filters:".Dumper($chain_rules{$group_name}));
  646. #current state rules
  647. foreach (my $f_index=0; $f_index<scalar(@cur_filter); $f_index++) {
  648. my $filter_str=trim($cur_filter[$f_index]);
  649. if (!$chain_rules{$group_name}[$f_index] or $filter_str!~/$chain_rules{$group_name}[$f_index]/i) {
  650. print "Check chain $group_name error! $filter_str not found in new config. Recreate chain.\n";
  651. $chain_ok=0;
  652. last;
  653. }
  654. }
  655. #new rules
  656. if ($chain_ok and $chain_rules{$group_name} and scalar(@{$chain_rules{$group_name}})) {
  657. foreach (my $f_index=0; $f_index<scalar(@{$chain_rules{$group_name}}); $f_index++) {
  658. my $filter_str=trim($cur_filter[$f_index]);
  659. if (!$filter_str) {
  660. print "Check chain $group_name error! Not found: $chain_rules{$group_name}[$f_index]. Recreate chain.\n";
  661. $chain_ok=0;
  662. last;
  663. }
  664. $filter_str=~s/^\d//;
  665. $filter_str=trim($filter_str);
  666. if ($filter_str!~/$chain_rules{$group_name}[$f_index]/i) {
  667. print "Check chain $group_name error! Expected: $chain_rules{$group_name}[$f_index] Found: $filter_str. Recreate chain.\n";
  668. $chain_ok=0;
  669. last;
  670. }
  671. }
  672. }
  673. if (!$chain_ok) {
  674. push(@cmd_list,":foreach i in [/ip firewall filter find where chain=".$group_name." ] do={/ip firewall filter remove \$i};");
  675. foreach my $filter_str (@{$chain_rules{$group_name}}) {
  676. push(@cmd_list,'/ip firewall filter add '.$filter_str);
  677. }
  678. }
  679. }
  680. if ($shaper_enabled) {
  681. #shapers
  682. my %get_queue_type=();
  683. my %get_queue_tree=();
  684. my %get_filter_mangle=();
  685. my @tmp=netdev_cmd($gate,$t,'/queue type print terse without-paging where name~"pcq_(down|up)load"',1);
  686. log_debug($gate_ident."Get queues: ".Dumper(\@tmp));
  687. # 0 name=pcq_upload_3 kind=pcq pcq-rate=102401k pcq-limit=500KiB pcq-classifier=src-address pcq-total-limit=2000KiB pcq-burst-rate=0 pcq-burst-threshold=0 pcq-burst-time=10s
  688. #pcq-src-address-mask=32 pcq-dst-address-mask=32 pcq-src-address6-mask=64 pcq-dst-address6-mask=64
  689. foreach my $row (@tmp) {
  690. next if (!$row);
  691. $row = trim($row);
  692. next if ($row!~/^(\d){1,3}/);
  693. $row=~s/^\d{1,3}\s+//;
  694. next if (!$row);
  695. if ($row=~/name=pcq_(down|up)load_(\d){1,3}\s+/i) {
  696. next if (!$1);
  697. next if (!$2);
  698. my $direct = $1;
  699. my $index = $2;
  700. $get_queue_type{$index}{$direct}=$row;
  701. if ($row=~/pcq-rate=(\S*)\s+\S/i) {
  702. my $rate = $1;
  703. if ($rate=~/k$/i) { $rate =~s/k$//i; }
  704. $get_queue_type{$index}{$direct."-rate"}=$rate;
  705. }
  706. if ($row=~/pcq-classifier=(\S*)\s+\S/i) { $get_queue_type{$index}{$direct."-classifier"}=$1; }
  707. if ($row=~/pcq-src-address-mask=(\S*)\s+\S/i) { $get_queue_type{$index}{$direct."-src-address-mask"}=$1; }
  708. if ($row=~/pcq-dst-address-mask=(\S*)\s+\S/i) { $get_queue_type{$index}{$direct."-dst-address-mask"}=$1; }
  709. }
  710. }
  711. @tmp=();
  712. @tmp=netdev_cmd($gate,$t,'/queue tree print terse without-paging where parent~"(download|upload)_root"',1);
  713. log_debug($gate_ident."Get root queues: ".Dumper(\@tmp));
  714. #print Dumper(\@tmp);
  715. # 0 I name=queue_3_out parent=upload_root packet-mark=upload_3 limit-at=0 queue=*2A priority=8 max-limit=0 burst-limit=0 burst-threshold=0 burst-time=0s bucket-size=0.1
  716. # 5 I name=queue_3_vlan2_in parent=download_root_vlan2 packet-mark=download_3_vlan2 limit-at=0 queue=*2B priority=8 max-limit=0 burst-limit=0 burst-threshold=0 burst-time=0s bucket-size=0.1
  717. foreach my $row (@tmp) {
  718. next if (!$row);
  719. $row = trim($row);
  720. next if ($row!~/^(\d)/);
  721. $row=~s/^(\d*)\s+//;
  722. next if (!$row);
  723. if ($row=~/queue=pcq_(down|up)load_(\d){1,3}/i) {
  724. if ($row=~/name=queue_(\d){1,3}_(\S*)_out\s+/i) {
  725. next if (!$1);
  726. next if (!$2);
  727. my $index = $1;
  728. my $int_name = $2;
  729. $get_queue_tree{$index}{$int_name}{up}=$row;
  730. if ($row=~/parent=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'up-parent'}=$1; }
  731. if ($row=~/packet-mark=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'up-mark'}=$1; }
  732. if ($row=~/queue=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'up-queue'}=$1; }
  733. }
  734. if ($row=~/name=queue_(\d){1,3}_(\S*)_in\s+/i) {
  735. next if (!$1);
  736. next if (!$2);
  737. my $index = $1;
  738. my $int_name = $2;
  739. $get_queue_tree{$index}{$int_name}{down}=$row;
  740. if ($row=~/parent=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'down-parent'}=$1; }
  741. if ($row=~/packet-mark=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'down-mark'}=$1; }
  742. if ($row=~/queue=(\S*)\s+\S/i) { $get_queue_tree{$index}{$int_name}{'down-queue'}=$1; }
  743. }
  744. }
  745. }
  746. @tmp=();
  747. @tmp=netdev_cmd($gate,$t,'/ip firewall mangle print terse without-paging where action=mark-packet and new-packet-mark~"(upload|download)_[0-9]{1,3}"',1);
  748. log_debug($gate_ident."Get firewall mangle rules for queues:".Dumper(\@tmp));
  749. # 0 chain=forward action=mark-packet new-packet-mark=upload_0 passthrough=yes src-address-list=queue_0 out-interface=sfp-sfpplus1-wan log=no log-prefix=""
  750. # 0 chain=forward action=mark-packet new-packet-mark=download_3_vlan2 passthrough=yes dst-address-list=queue_3 out-interface=vlan2 in-interface-list=WAN log=no log-prefix=""
  751. foreach my $row (@tmp) {
  752. next if (!$row);
  753. $row = trim($row);
  754. next if ($row!~/^(\d){1,3}/);
  755. $row=~s/^\d{1,3}\s+//;
  756. next if (!$row);
  757. if ($row=~/new-packet-mark=upload_(\d){1,3}_(\S*)\s+/i) {
  758. next if (!$1);
  759. next if (!$2);
  760. my $index = $1;
  761. my $int_name = $2;
  762. $get_filter_mangle{$index}{$int_name}{up}=$row;
  763. if ($row=~/src-address-list=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'up-list'}=$1; }
  764. if ($row=~/out-interface=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'up-dev'}=$1; }
  765. if ($row=~/new-packet-mark=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'up-mark'}=$1; }
  766. }
  767. if ($row=~/new-packet-mark=download_(\d){1,3}_(\S*)\s+/i) {
  768. next if (!$1);
  769. next if (!$2);
  770. my $index = $1;
  771. my $int_name = $2;
  772. $get_filter_mangle{$index}{$int_name}{down}=$row;
  773. if ($row=~/dst-address-list=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'down-list'}=$1; }
  774. if ($row=~/new-packet-mark=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'down-mark'}=$1; }
  775. if ($row=~/out-interface=(\S*)\s+\S/i) { $get_filter_mangle{$index}{$int_name}{'down-dev'}=$1; }
  776. }
  777. }
  778. log_debug($gate_ident."Queues type status:".Dumper(\%get_queue_type));
  779. log_debug($gate_ident."Queues tree status:".Dumper(\%get_queue_tree));
  780. log_debug($gate_ident."Firewall mangle status:".Dumper(\%get_filter_mangle));
  781. my %queue_type;
  782. my %queue_tree;
  783. my %filter_mangle;
  784. #generate new config
  785. foreach my $queue_name (keys %queues) {
  786. my $q_id=$queues{$queue_name}{id};
  787. my $q_up=$queues{$queue_name}{up}+1;
  788. my $q_down=$queues{$queue_name}{down}+1;
  789. #queue_types
  790. $queue_type{$q_id}{up}="name=pcq_upload_".$q_id." kind=pcq pcq-rate=".$q_up."k pcq-limit=500KiB pcq-classifier=src-address pcq-total-limit=2000KiB pcq-burst-rate=0 pcq-burst-threshold=0 pcq-burst-time=10s pcq-src-address-mask=32 pcq-dst-address-mask=32 pcq-src-address6-mask=64 pcq-dst-address6-mask=64";
  791. $queue_type{$q_id}{down}="name=pcq_download_".$q_id." kind=pcq pcq-rate=".$q_down."k pcq-limit=500KiB pcq-classifier=dst-address pcq-total-limit=2000KiB pcq-burst-rate=0 pcq-burst-threshold=0 pcq-burst-time=10s pcq-src-address-mask=32 pcq-dst-address-mask=32 pcq-src-address6-mask=64 pcq-dst-address6-mask=64";
  792. my $queue_ok=1;
  793. if (!$get_queue_type{$q_id}{up}) { $queue_ok=0; }
  794. if ($queue_ok and abs($q_up - $get_queue_type{$q_id}{'up-rate'})>10) { $queue_ok=0; }
  795. if ($queue_ok and $get_queue_type{$q_id}{'up-classifier'}!~/src-address/i) { $queue_ok=0; }
  796. if (!$queue_ok) {
  797. push(@cmd_list,':foreach i in [/queue type find where name~"pcq_upload_'.$q_id.'" ] do={/queue type remove $i};');
  798. push(@cmd_list,'/queue type add '.$queue_type{$q_id}{up});
  799. }
  800. $queue_ok=1;
  801. if (!$get_queue_type{$q_id}{down}) { $queue_ok=0; }
  802. if ($queue_ok and abs($q_up - $get_queue_type{$q_id}{'down-rate'})>10) { $queue_ok=0; }
  803. if ($queue_ok and $get_queue_type{$q_id}{'down-classifier'}!~/dst-address/i) { $queue_ok=0; }
  804. if (!$queue_ok) {
  805. push(@cmd_list,':foreach i in [/queue type find where name~"pcq_download_'.$q_id.'" ] do={/queue type remove $i};');
  806. push(@cmd_list,'/queue type add '.$queue_type{$q_id}{down});
  807. }
  808. #upload queue
  809. foreach my $int (@wan_int) {
  810. $queue_tree{$q_id}{$int}{up}="name=queue_".$q_id."_".$int."_out parent=upload_root_".$int." packet-mark=upload_".$q_id."_".$int." limit-at=0 queue=pcq_upload_".$q_id." priority=8 max-limit=0 burst-limit=0 burst-threshold=0 burst-time=0s bucket-size=0.1";
  811. $filter_mangle{$q_id}{$int}{up}="chain=forward action=mark-packet new-packet-mark=upload_".$q_id."_".$int." passthrough=yes src-address-list=queue_".$q_id." out-interface=".$int." log=no log-prefix=\"\"";
  812. $queue_ok=1;
  813. if (!$get_queue_tree{$q_id}{$int}{up}) { $queue_ok=0; }
  814. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'up-parent'} ne "upload_root_".$int)) { $queue_ok=0;}
  815. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'up-mark'} ne "upload_".$q_id."_".$int)) { $queue_ok=0; }
  816. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'up-queue'} ne "pcq_upload_".$q_id)) { $queue_ok=0; }
  817. if (!$queue_ok) {
  818. push(@cmd_list,':foreach i in [/queue tree find where name~"queue_'.$q_id."_".$int."_out".'" ] do={/queue tree remove $i};');
  819. push(@cmd_list,'/queue tree add '.$queue_tree{$q_id}{$int}{up});
  820. }
  821. $queue_ok=1;
  822. if (!$get_filter_mangle{$q_id}{$int}{up}) { $queue_ok=0; }
  823. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'up-mark'} ne "upload_".$q_id."_".$int)) { $queue_ok=0; }
  824. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'up-list'} ne "queue_".$q_id)) { $queue_ok=0; }
  825. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'up-dev'} ne $int)) { $queue_ok=0; }
  826. if (!$queue_ok) {
  827. push(@cmd_list,':foreach i in [/ip firewall mangle find where action=mark-packet and new-packet-mark~"upload_'.$q_id."_".$int.'" ] do={/ip firewall mangle remove $i};');
  828. push(@cmd_list,'/ip firewall mangle add '.$filter_mangle{$q_id}{$int}{up});
  829. }
  830. }
  831. #download
  832. foreach my $int (@lan_int) {
  833. next if (!$int);
  834. $queue_tree{$q_id}{$int}{down}="name=queue_".$q_id."_".$int."_in parent=download_root_".$int." packet-mark=download_".$q_id."_".$int." limit-at=0 queue=pcq_download_".$q_id." priority=8 max-limit=0 burst-limit=0 burst-threshold=0 burst-time=0s bucket-size=0.1";
  835. $filter_mangle{$q_id}{$int}{down}="chain=forward action=mark-packet new-packet-mark=download_".$q_id."_".$int." passthrough=yes dst-address-list=queue_".$q_id." out-interface=".$int." in-interface-list=WAN log=no log-prefix=\"\"";
  836. $queue_ok=1;
  837. if (!$get_queue_tree{$q_id}{$int}{down}) { $queue_ok=0; }
  838. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'down-parent'} ne "download_root_".$int)) { $queue_ok=0; }
  839. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'down-mark'} ne "download_".$q_id."_".$int)) { $queue_ok=0; }
  840. if ($queue_ok and ($get_queue_tree{$q_id}{$int}{'down-queue'} ne "pcq_download_".$q_id)) { $queue_ok=0; }
  841. if (!$queue_ok) {
  842. push(@cmd_list,':foreach i in [/queue tree find where name~"queue_'.$q_id."_".$int."_in".'" ] do={/queue tree remove $i};');
  843. push(@cmd_list,'/queue tree add '.$queue_tree{$q_id}{$int}{down});
  844. }
  845. $queue_ok=1;
  846. if (!$get_filter_mangle{$q_id}{$int}{down}) { $queue_ok=0; }
  847. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'down-mark'} ne "download_".$q_id."_".$int)) { $queue_ok=0; }
  848. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'down-list'} ne "queue_".$q_id)) { $queue_ok=0; }
  849. if ($queue_ok and ($get_filter_mangle{$q_id}{$int}{'down-dev'} ne $int)) { $queue_ok=0; }
  850. if (!$queue_ok) {
  851. push(@cmd_list,':foreach i in [/ip firewall mangle find where action=mark-packet and new-packet-mark~"download_'.$q_id."_".$int.'" ] do={/ip firewall mangle remove $i};');
  852. push(@cmd_list,'/ip firewall mangle add '.$filter_mangle{$q_id}{$int}{down});
  853. }
  854. }
  855. #end shaper
  856. }
  857. }
  858. }#end access lists config
  859. if (scalar(@cmd_list)) {
  860. log_debug($gate_ident."Apply:");
  861. if ($debug) { foreach my $cmd (@cmd_list) { log_debug($gate_ident."$cmd"); } }
  862. eval {
  863. netdev_cmd($gate,$t,\@cmd_list,1);
  864. };
  865. if ($@) {
  866. $all_ok = 0;
  867. log_debug($gate_ident."Error programming gateway! Err: ".$@);
  868. }
  869. }
  870. db_log_verbose($dbh,$gate_ident."Sync user state stopped.");
  871. $dbh->disconnect();
  872. $pm->finish;
  873. }
  874. $pm->wait_all_children;
  875. #clear changed
  876. if ($all_ok) {
  877. foreach my $row (@changes_found) {
  878. do_sql($dbh,"UPDATE User_auth SET changed=0 WHERE id=".$row->{id});
  879. }
  880. }
  881. if (IsMyPID($SPID)) { Remove_PID($SPID); };
  882. do_exit 0;