misc.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. /*
  2. * misc.cpp
  3. *
  4. * Created on: Sep 23, 2017
  5. * Author: root
  6. */
  7. #include "git_version.h"
  8. #include "common.h"
  9. #include "encrypt.h"
  10. #include "misc.h"
  11. #include "network.h"
  12. #include "connection.h"
  13. #include "fd_manager.h"
  14. int hb_mode=1;
  15. int hb_len=1200;
  16. char hb_buf[buf_len];
  17. int mtu_warn=1375;//if a packet larger than mtu warn is receviced,there will be a warning
  18. int max_rst_to_show=15;
  19. int max_rst_allowed=-1;
  20. int enable_dns_resolve=0;
  21. int ttl_value=64;
  22. fd_manager_t fd_manager;
  23. //char remote_address[max_address_len]="";
  24. //char local_ip[100]="0.0.0.0", remote_ip[100]="255.255.255.255",source_ip[100]="0.0.0.0";//local_ip is for -l option,remote_ip for -r option,source for --source-ip
  25. //u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32;//convert from last line.
  26. //int local_port = -1, remote_port=-1,source_port=0;//similiar to local_ip remote_ip,buf for port.source_port=0 indicates --source-port is not enabled
  27. address_t local_addr,remote_addr,source_addr;
  28. my_ip_t bind_addr;
  29. int source_port=-1;
  30. int bind_addr_used=0;
  31. int force_source_ip=0; //if --source-ip is enabled
  32. int force_source_port=0;
  33. my_id_t const_id=0;//an id used for connection recovery,its generated randomly,it never change since its generated
  34. int udp_fd=-1; //for client only. client use this fd to listen and handle udp connection
  35. int bind_fd=-1; //bind only,never send or recv. its just a dummy fd for bind,so that other program wont occupy the same port
  36. int epollfd=-1; //fd for epoll
  37. int timer_fd=-1; //the general timer fd for client and server.for server this is not the only timer find,every connection has a timer fd.
  38. int fail_time_counter=0;//determine if the max_fail_time is reached
  39. int epoll_trigger_counter=0;//for debug only
  40. int debug_flag=0;//for debug only
  41. int simple_rule=0; //deprecated.
  42. int keep_rule=0; //whether to monitor the iptables rule periodly,re-add if losted
  43. int auto_add_iptables_rule=0;//if -a is set
  44. int generate_iptables_rule=0;//if -g is set
  45. int generate_iptables_rule_add=0;// if --gen-add is set
  46. int retry_on_error=0;
  47. int debug_resend=0; // debug only
  48. char key_string[1000]= "secret key";// -k option
  49. char fifo_file[1000]="";
  50. int clear_iptables=0;
  51. int wait_xtables_lock=0;
  52. string iptables_command0="iptables/ip6tables ";
  53. string iptables_command="";
  54. string iptables_pattern="";
  55. int iptables_rule_added=0;
  56. int iptables_rule_keeped=0;
  57. int iptables_rule_keep_index=0;
  58. program_mode_t program_mode=unset_mode;//0 unset; 1client 2server
  59. raw_mode_t raw_mode=mode_faketcp;
  60. u32_t raw_ip_version=(u32_t)-1;
  61. unordered_map<int, const char*> raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}};
  62. int about_to_exit=0;
  63. int socket_buf_size=1024*1024;
  64. //int force_socket_buf=0;
  65. //char lower_level_arg[1000];
  66. int process_lower_level_arg()//handle --lower-level option
  67. {
  68. lower_level=1;
  69. if(strcmp(optarg,"auto")==0)
  70. {
  71. return 0;
  72. }
  73. lower_level_manual=1;
  74. if (strchr(optarg, '#') == 0) {
  75. mylog(log_fatal,
  76. "lower-level parameter invaild,check help page for format\n");
  77. myexit(-1);
  78. }
  79. lower_level = 1;
  80. u32_t hw[6];
  81. memset(hw, 0, sizeof(hw));
  82. sscanf(optarg, "%[^#]#%x:%x:%x:%x:%x:%x", if_name, &hw[0], &hw[1], &hw[2],
  83. &hw[3], &hw[4], &hw[5]);
  84. mylog(log_warn,
  85. "make sure this is correct: if_name=<%s> dest_mac_adress=<%02x:%02x:%02x:%02x:%02x:%02x> \n",
  86. if_name, hw[0], hw[1], hw[2], hw[3], hw[4], hw[5]);
  87. for (int i = 0; i < 6; i++) {
  88. dest_hw_addr[i] = uint8_t(hw[i]);
  89. }
  90. return 0;
  91. }
  92. void print_help()
  93. {
  94. char git_version_buf[100]={0};
  95. strncpy(git_version_buf,gitversion,10);
  96. printf("udp2raw-tunnel\n");
  97. printf("git version:%s ",git_version_buf);
  98. printf("build date:%s %s\n",__DATE__,__TIME__);
  99. printf("repository: https://github.com/wangyu-/udp2raw-tunnel\n");
  100. printf("\n");
  101. printf("usage:\n");
  102. printf(" run as client : ./this_program -c -l local_listen_ip:local_port -r server_address:server_port [options]\n");
  103. printf(" run as server : ./this_program -s -l server_listen_ip:server_port -r remote_address:remote_port [options]\n");
  104. printf("\n");
  105. printf("common options,these options must be same on both side:\n");
  106. printf(" --raw-mode <string> avaliable values:faketcp(default),udp,icmp\n");
  107. printf(" -k,--key <string> password to gen symetric key,default:\"secret key\"\n");
  108. printf(" --cipher-mode <string> avaliable values:aes128cfb,aes128cbc(default),xor,none\n");
  109. printf(" --auth-mode <string> avaliable values:hmac_sha1,md5(default),crc32,simple,none\n");
  110. printf(" -a,--auto-rule auto add (and delete) iptables rule\n");
  111. printf(" -g,--gen-rule generate iptables rule then exit,so that you can copy and\n");
  112. printf(" add it manually.overrides -a\n");
  113. printf(" --disable-anti-replay disable anti-replay,not suggested\n");
  114. printf(" --fix-gro try to fix huge packet caused by GRO. this option is at an early stage.\n");
  115. printf(" make sure client and server are at same version.\n");
  116. //printf("\n");
  117. printf("client options:\n");
  118. printf(" --source-ip <ip> force source-ip for raw socket\n");
  119. printf(" --source-port <port> force source-port for raw socket,tcp/udp only\n");
  120. printf(" this option disables port changing while re-connecting\n");
  121. // printf(" \n");
  122. printf("other options:\n");
  123. printf(" --conf-file <string> read options from a configuration file instead of command line.\n");
  124. printf(" check example.conf in repo for format\n");
  125. printf(" --fifo <string> use a fifo(named pipe) for sending commands to the running program,\n");
  126. printf(" check readme.md in repository for supported commands.\n");
  127. printf(" --log-level <number> 0:never 1:fatal 2:error 3:warn \n");
  128. printf(" 4:info (default) 5:debug 6:trace\n");
  129. // printf("\n");
  130. printf(" --log-position enable file name,function name,line number in log\n");
  131. printf(" --disable-color disable log color\n");
  132. printf(" --disable-bpf disable the kernel space filter,most time its not necessary\n");
  133. printf(" unless you suspect there is a bug\n");
  134. // printf("\n");
  135. printf(" --dev <string> bind raw socket to a device, not necessary but improves performance\n");
  136. printf(" --sock-buf <number> buf size for socket,>=10 and <=10240,unit:kbyte,default:1024\n");
  137. printf(" --force-sock-buf bypass system limitation while setting sock-buf\n");
  138. printf(" --seq-mode <number> seq increase mode for faketcp:\n");
  139. printf(" 0:static header,do not increase seq and ack_seq\n");
  140. printf(" 1:increase seq for every packet,simply ack last seq\n");
  141. printf(" 2:increase seq randomly, about every 3 packets,simply ack last seq\n");
  142. printf(" 3:simulate an almost real seq/ack procedure(default)\n");
  143. printf(" 4:similiar to 3,but do not consider TCP Option Window_Scale,\n");
  144. printf(" maybe useful when firewall doesnt support TCP Option \n");
  145. // printf("\n");
  146. printf(" --lower-level <string> send packets at OSI level 2, format:'if_name#dest_mac_adress'\n");
  147. printf(" ie:'eth0#00:23:45:67:89:b9'.or try '--lower-level auto' to obtain\n");
  148. printf(" the parameter automatically,specify it manually if 'auto' failed\n");
  149. printf(" --wait-lock wait for xtables lock while invoking iptables, need iptables v1.4.20+\n");
  150. printf(" --gen-add generate iptables rule and add it permanently,then exit.overrides -g\n");
  151. printf(" --keep-rule monitor iptables and auto re-add if necessary.implys -a\n");
  152. printf(" --hb-len <number> length of heart-beat packet, >=0 and <=1500\n");
  153. printf(" --mtu-warn <number> mtu warning threshold, unit:byte, default:1375\n");
  154. printf(" --clear clear any iptables rules added by this program.overrides everything\n");
  155. printf(" --retry-on-error retry on error, allow to start udp2raw before network is initialized\n");
  156. printf(" -h,--help print this help message\n");
  157. //printf("common options,these options must be same on both side\n");
  158. }
  159. int load_config(char *file_name, int &argc, vector<string> &argv) //load conf file and append to argv
  160. {
  161. // Load configurations from config_file instead of the command line.
  162. // See config.example for example configurations
  163. std::ifstream conf_file(file_name);
  164. std::string line;
  165. if(conf_file.fail())
  166. {
  167. mylog(log_fatal,"conf_file %s open failed,reason :%s\n",file_name,get_sock_error());
  168. myexit(-1);
  169. }
  170. while(std::getline(conf_file,line))
  171. {
  172. auto res=parse_conf_line(line);
  173. argc+=res.size();
  174. for(int i=0;i<(int)res.size();i++)
  175. {
  176. argv.push_back(res[i]);
  177. }
  178. }
  179. conf_file.close();
  180. return 0;
  181. }
  182. int process_log_level(int argc,char *argv[])//process --log-level and --disable-cloer --log-postion options
  183. {
  184. int i,j,k;
  185. for (i = 0; i < argc; i++)
  186. {
  187. if(strcmp(argv[i],"--log-level")==0)
  188. {
  189. if(i<argc -1)
  190. {
  191. sscanf(argv[i+1],"%d",&log_level);
  192. if(0<=log_level&&log_level<log_end)
  193. {
  194. }
  195. else
  196. {
  197. log_bare(log_fatal,"invalid log_level\n");
  198. myexit(-1);
  199. }
  200. }
  201. }
  202. if(strcmp(argv[i],"--enable-color")==0)
  203. {
  204. enable_log_color=1;
  205. }
  206. if(strcmp(argv[i],"--disable-color")==0)
  207. {
  208. enable_log_color=0;
  209. }
  210. if(strcmp(argv[i],"--log-position")==0)
  211. {
  212. enable_log_position=1;
  213. }
  214. }
  215. return 0;
  216. }
  217. void process_arg(int argc, char *argv[]) //process all options
  218. {
  219. int i,j,k,opt;
  220. int option_index = 0;
  221. char options[]="l:r:schk:ag";
  222. static struct option long_options[] =
  223. {
  224. /* These options set a flag. */
  225. {"source-ip", required_argument, 0, 1},
  226. {"source-port", required_argument, 0, 1},
  227. {"log-level", required_argument, 0, 1},
  228. {"key", required_argument, 0, 'k'},
  229. {"auth-mode", required_argument, 0, 1},
  230. {"cipher-mode", required_argument, 0, 1},
  231. {"raw-mode", required_argument, 0, 1},
  232. {"disable-color", no_argument, 0, 1},
  233. {"enable-color", no_argument, 0, 1},
  234. {"log-position", no_argument, 0, 1},
  235. {"disable-bpf", no_argument, 0, 1},
  236. {"disable-anti-replay", no_argument, 0, 1},
  237. {"auto-rule", no_argument, 0, 'a'},
  238. {"gen-rule", no_argument, 0, 'g'},
  239. {"gen-add", no_argument, 0, 1},
  240. {"debug", no_argument, 0, 1},
  241. {"retry-on-error", no_argument, 0, 1},
  242. {"clear", no_argument, 0, 1},
  243. {"simple-rule", no_argument, 0, 1},
  244. {"keep-rule", no_argument, 0, 1},
  245. {"lower-level", required_argument, 0, 1},
  246. {"sock-buf", required_argument, 0, 1},
  247. {"seq-mode", required_argument, 0, 1},
  248. {"conf-file", required_argument, 0, 1},
  249. {"force-sock-buf", no_argument, 0, 1},
  250. {"wait-lock", no_argument, 0, 1},
  251. {"random-drop", required_argument, 0, 1},
  252. {"fifo", required_argument, 0, 1},
  253. {"hb-mode", required_argument, 0, 1},
  254. {"hb-len", required_argument, 0, 1},
  255. {"mtu-warn", required_argument, 0, 1},
  256. {"max-rst-to-show", required_argument, 0, 1},
  257. {"max-rst-allowed", required_argument, 0, 1},
  258. {"set-ttl", required_argument, 0, 1},
  259. {"dev", required_argument, 0, 1},
  260. {"dns-resolve", no_argument, 0, 1},
  261. {"easy-tcp", no_argument, 0, 1},
  262. {"fix-gro", no_argument, 0, 1},
  263. {NULL, 0, 0, 0}
  264. };
  265. process_log_level(argc,argv);
  266. set<string> all_options;
  267. map<string,string> shortcut_map;
  268. all_options.insert("--help");
  269. all_options.insert("-h");
  270. string dummy="";
  271. for(i=0;i<(int)strlen(options);i++)
  272. {
  273. char val=options[i];
  274. if( ( val>='0'&&val<='9') ||( val>='a'&&val<='z')||(val>='A'&&val<='Z'))
  275. {
  276. all_options.insert(dummy+'-'+val);
  277. }
  278. }
  279. for(i=0;i<int( sizeof(long_options)/sizeof(long_options[0]) );i++)
  280. {
  281. if(long_options[i].name==NULL) break;
  282. int val=long_options[i].val;
  283. if( ( val>='0'&&val<='9') ||( val>='a'&&val<='z')||(val>='A'&&val<='Z'))
  284. {
  285. shortcut_map[dummy+"--"+long_options[i].name]= dummy+"-"+ char(val);
  286. }
  287. all_options.insert(dummy+"--"+long_options[i].name);
  288. }
  289. for (i = 0; i < argc; i++)
  290. {
  291. int len=strlen(argv[i]);
  292. if(len==0)
  293. {
  294. mylog(log_fatal,"found an empty string in options\n");
  295. myexit(-1);
  296. }
  297. if(len==1&&argv[i][0]=='-' )
  298. {
  299. mylog(log_fatal,"invaild option '-' in argv\n");
  300. myexit(-1);
  301. }
  302. if(len==2&&argv[i][0]=='-'&&argv[i][1]=='-' )
  303. {
  304. mylog(log_fatal,"invaild option '--' in argv\n");
  305. myexit(-1);
  306. }
  307. }
  308. mylog(log_info,"argc=%d ", argc);
  309. for (i = 0; i < argc; i++) {
  310. log_bare(log_info, "%s ", argv[i]);
  311. }
  312. log_bare(log_info, "\n");
  313. //string dummy="";
  314. for(i=+1;i<argc;i++)
  315. {
  316. if(argv[i][0]!='-') continue;
  317. string a=argv[i];
  318. if(a[0]=='-'&&a[1]!='-')
  319. a=dummy+a[0]+a[1];
  320. if(all_options.find(a.c_str())==all_options.end())
  321. {
  322. mylog(log_fatal,"invaild option %s\n",a.c_str());
  323. myexit(-1);
  324. }
  325. for(j=i+1;j<argc;j++)
  326. {
  327. if(argv[j][0]!='-') continue;
  328. string b=argv[j];
  329. if(b[0]=='-'&&b[1]!='-')
  330. b=dummy+b[0]+b[1];
  331. if(shortcut_map.find(a)!=shortcut_map.end())
  332. a=shortcut_map[a];
  333. if(shortcut_map.find(b)!=shortcut_map.end())
  334. b=shortcut_map[b];
  335. if(a==b)
  336. {
  337. mylog(log_fatal,"%s duplicates with %s\n",argv[i],argv[j]);
  338. myexit(-1);
  339. }
  340. }
  341. }
  342. int no_l = 1, no_r = 1;
  343. while ((opt = getopt_long(argc, argv,options,long_options,&option_index)) != -1) {
  344. //string opt_key;
  345. //opt_key+=opt;
  346. switch (opt) {
  347. case 'l':
  348. no_l = 0;
  349. local_addr.from_str(optarg);
  350. if(local_addr.get_port()==22)
  351. {
  352. mylog(log_fatal,"port 22 not allowed\n");
  353. myexit(-1);
  354. }
  355. /*
  356. if (strchr(optarg, ':') != 0) {
  357. sscanf(optarg, "%[^:]:%d", local_ip, &local_port);
  358. if(local_port==22)
  359. {
  360. mylog(log_fatal,"port 22 not allowed\n");
  361. myexit(-1);
  362. }
  363. } else {
  364. mylog(log_fatal,"invalid parameter for -l ,%s,should be ip:port\n",optarg);
  365. myexit(-1);
  366. }*/
  367. break;
  368. case 'r':
  369. no_r = 0;
  370. remote_addr.from_str(optarg);
  371. if(remote_addr.get_port()==22)
  372. {
  373. mylog(log_fatal,"port 22 not allowed\n");
  374. myexit(-1);
  375. }
  376. /*
  377. if (strchr(optarg, ':') != 0) {
  378. sscanf(optarg, "%[^:]:%d", remote_address, &remote_port);
  379. if(remote_port==22)
  380. {
  381. mylog(log_fatal,"port 22 not allowed\n");
  382. myexit(-1);
  383. }
  384. } else {
  385. mylog(log_fatal,"invalid parameter for -r ,%s,should be ip:port\n",optarg);
  386. myexit(-1);
  387. }*/
  388. break;
  389. case 's':
  390. if(program_mode==0)
  391. {
  392. program_mode=server_mode;
  393. }
  394. else
  395. {
  396. mylog(log_fatal,"-s /-c has already been set,conflict\n");
  397. myexit(-1);
  398. }
  399. break;
  400. case 'c':
  401. if(program_mode==0)
  402. {
  403. program_mode=client_mode;
  404. }
  405. else
  406. {
  407. mylog(log_fatal,"-s /-c has already been set,conflict\n");
  408. myexit(-1);
  409. }
  410. break;
  411. case 'h':
  412. break;
  413. case 'a':
  414. auto_add_iptables_rule=1;
  415. break;
  416. case 'g':
  417. generate_iptables_rule=1;
  418. break;
  419. case 'k':
  420. mylog(log_debug,"parsing key option\n");
  421. sscanf(optarg,"%s",key_string);
  422. break;
  423. case 1:
  424. mylog(log_debug,"option_index: %d\n",option_index);
  425. if(strcmp(long_options[option_index].name,"clear")==0)
  426. {
  427. clear_iptables=1;
  428. }
  429. else if(strcmp(long_options[option_index].name,"source-ip")==0)
  430. {
  431. mylog(log_debug,"parsing long option :source-ip\n");
  432. //sscanf(optarg, "%s", source_ip);
  433. source_addr.from_str_ip_only(optarg);
  434. mylog(log_debug,"source: %s\n",source_addr.get_ip());
  435. force_source_ip=1;
  436. }
  437. else if(strcmp(long_options[option_index].name,"source-port")==0)
  438. {
  439. mylog(log_debug,"parsing long option :source-port\n");
  440. sscanf(optarg, "%d", &source_port);
  441. mylog(log_info,"source: %d\n",source_port);
  442. force_source_port=1;
  443. }
  444. else if(strcmp(long_options[option_index].name,"raw-mode")==0)
  445. {
  446. /*
  447. for(i=0;i<mode_end;i++)
  448. {
  449. if(strcmp(optarg,raw_mode_tostring[i])==0)
  450. {
  451. //printf("%d i\n",i);
  452. //printf("%s",raw_mode_tostring[i]);
  453. raw_mode=(raw_mode_t)i;
  454. break;
  455. }
  456. }
  457. if(i==mode_end)
  458. {
  459. mylog(log_fatal,"no such raw_mode %s\n",optarg);
  460. myexit(-1);
  461. }
  462. */
  463. if(strcmp(optarg,"easyfaketcp")==0||strcmp(optarg,"easy_faketcp")==0||strcmp(optarg,"easy-faketcp")==0)
  464. {
  465. raw_mode=mode_faketcp;
  466. use_tcp_dummy_socket=1;
  467. }
  468. else
  469. {
  470. for(i=0;i<mode_end;i++)
  471. {
  472. if(strcmp(optarg,raw_mode_tostring[i])==0)
  473. {
  474. //printf("%d i\n",i);
  475. //printf("%s",raw_mode_tostring[i]);
  476. raw_mode=(raw_mode_t)i;
  477. break;
  478. }
  479. }
  480. if(i==mode_end)
  481. {
  482. mylog(log_fatal,"no such raw_mode %s\n",optarg);
  483. myexit(-1);
  484. }
  485. }
  486. }
  487. else if(strcmp(long_options[option_index].name,"auth-mode")==0)
  488. {
  489. for(i=0;i<auth_end;i++)
  490. {
  491. if(strcmp(optarg,auth_mode_tostring[i])==0)
  492. {
  493. auth_mode=(auth_mode_t)i;
  494. if(auth_mode==auth_none)
  495. {
  496. disable_anti_replay=1;
  497. }
  498. break;
  499. }
  500. }
  501. if(i==auth_end)
  502. {
  503. mylog(log_fatal,"no such auth_mode %s\n",optarg);
  504. myexit(-1);
  505. }
  506. }
  507. else if(strcmp(long_options[option_index].name,"cipher-mode")==0)
  508. {
  509. for(i=0;i<cipher_end;i++)
  510. {
  511. if(strcmp(optarg,cipher_mode_tostring[i])==0)
  512. {
  513. cipher_mode=(cipher_mode_t)i;
  514. break;
  515. }
  516. }
  517. if(i==cipher_end)
  518. {
  519. mylog(log_fatal,"no such cipher_mode %s\n",optarg);
  520. myexit(-1);
  521. }
  522. }
  523. else if(strcmp(long_options[option_index].name,"log-level")==0)
  524. {
  525. }
  526. else if(strcmp(long_options[option_index].name,"lower-level")==0)
  527. {
  528. process_lower_level_arg();
  529. //lower_level=1;
  530. //strcpy(lower_level_arg,optarg);
  531. }
  532. else if(strcmp(long_options[option_index].name,"simple-rule")==0)
  533. {
  534. simple_rule=1;
  535. }
  536. else if(strcmp(long_options[option_index].name,"keep-rule")==0)
  537. {
  538. keep_rule=1;
  539. }
  540. else if(strcmp(long_options[option_index].name,"gen-add")==0)
  541. {
  542. generate_iptables_rule_add=1;
  543. }
  544. else if(strcmp(long_options[option_index].name,"disable-color")==0)
  545. {
  546. //enable_log_color=0;
  547. }
  548. else if(strcmp(long_options[option_index].name,"enable-color")==0)
  549. {
  550. //enable_log_color=0;
  551. }
  552. else if(strcmp(long_options[option_index].name,"debug")==0)
  553. {
  554. debug_flag=1;
  555. //enable_log_color=0;
  556. }
  557. else if(strcmp(long_options[option_index].name,"dev")==0)
  558. {
  559. sscanf(optarg,"%s",dev);
  560. //enable_log_color=0;
  561. mylog(log_info,"dev=[%s]\n",dev);
  562. }
  563. else if(strcmp(long_options[option_index].name,"debug-resend")==0)
  564. {
  565. //debug_resend=1;
  566. //enable_log_color=0;
  567. }
  568. else if(strcmp(long_options[option_index].name,"log-position")==0)
  569. {
  570. //enable_log_position=1;
  571. }
  572. else if(strcmp(long_options[option_index].name,"force-sock-buf")==0)
  573. {
  574. force_socket_buf=1;
  575. }
  576. else if(strcmp(long_options[option_index].name,"retry-on-error")==0)
  577. {
  578. retry_on_error=1;
  579. }
  580. else if(strcmp(long_options[option_index].name,"wait-lock")==0)
  581. {
  582. wait_xtables_lock=1;
  583. }
  584. else if(strcmp(long_options[option_index].name,"disable-bpf")==0)
  585. {
  586. disable_bpf_filter=1;
  587. }
  588. else if(strcmp(long_options[option_index].name,"disable-anti-replay")==0)
  589. {
  590. disable_anti_replay=1;
  591. }
  592. else if(strcmp(long_options[option_index].name,"sock-buf")==0)
  593. {
  594. int tmp=-1;
  595. sscanf(optarg,"%d",&tmp);
  596. if(10<=tmp&&tmp<=10*1024)
  597. {
  598. socket_buf_size=tmp*1024;
  599. }
  600. else
  601. {
  602. mylog(log_fatal,"sock-buf value must be between 1 and 10240 (kbyte) \n");
  603. myexit(-1);
  604. }
  605. }
  606. else if(strcmp(long_options[option_index].name,"seq-mode")==0)
  607. {
  608. sscanf(optarg,"%d",&seq_mode);
  609. if(0<=seq_mode&&seq_mode<=max_seq_mode)
  610. {
  611. }
  612. else
  613. {
  614. mylog(log_fatal,"seq_mode value must be 0,1,or 2 \n");
  615. myexit(-1);
  616. }
  617. }
  618. else if(strcmp(long_options[option_index].name,"random-drop")==0)
  619. {
  620. sscanf(optarg,"%d",&random_drop);
  621. if(random_drop<0||random_drop>10000)
  622. {
  623. mylog(log_fatal,"random_drop must be between 0 10000 \n");
  624. myexit(-1);
  625. }
  626. mylog(log_info,"random_drop =%d \n",random_drop);
  627. }
  628. else if(strcmp(long_options[option_index].name,"fifo")==0)
  629. {
  630. sscanf(optarg,"%s",fifo_file);
  631. mylog(log_info,"fifo_file =%s \n",fifo_file);
  632. }
  633. else if(strcmp(long_options[option_index].name,"conf-file")==0)
  634. {
  635. mylog(log_info,"configuration loaded from %s\n",optarg);
  636. }
  637. else if(strcmp(long_options[option_index].name,"hb-mode")==0)
  638. {
  639. sscanf(optarg,"%d",&hb_mode);
  640. assert(hb_mode==0||hb_mode==1);
  641. mylog(log_info,"hb_mode =%d \n",hb_mode);
  642. }
  643. else if(strcmp(long_options[option_index].name,"hb-len")==0)
  644. {
  645. sscanf(optarg,"%d",&hb_len);
  646. assert(hb_len>=0&&hb_len<=1500);
  647. mylog(log_info,"hb_len =%d \n",hb_len);
  648. }
  649. else if(strcmp(long_options[option_index].name,"mtu-warn")==0)
  650. {
  651. sscanf(optarg,"%d",&mtu_warn);
  652. assert(mtu_warn>0);
  653. mylog(log_info,"mtu_warn=%d \n",mtu_warn);
  654. }
  655. else if(strcmp(long_options[option_index].name,"max-rst-to-show")==0)
  656. {
  657. sscanf(optarg,"%d",&max_rst_to_show);
  658. assert(max_rst_to_show>=-1);
  659. mylog(log_info,"max_rst_to_show=%d \n",max_rst_to_show);
  660. }
  661. else if(strcmp(long_options[option_index].name,"max-rst-allowed")==0)
  662. {
  663. sscanf(optarg,"%d",&max_rst_allowed);
  664. assert(max_rst_allowed>=-1);
  665. mylog(log_info,"max_rst_allowed=%d \n",max_rst_allowed);
  666. }
  667. else if(strcmp(long_options[option_index].name,"set-ttl")==0)
  668. {
  669. sscanf(optarg,"%d",&ttl_value);
  670. assert(ttl_value>=0&&ttl_value<=255);
  671. mylog(log_info,"ttl_value=%d\n",ttl_value);
  672. }
  673. else if(strcmp(long_options[option_index].name,"dns-resolve")==0) // currently not used
  674. {
  675. enable_dns_resolve=1;
  676. mylog(log_info,"dns-resolve enabled\n");
  677. }
  678. else if(strcmp(long_options[option_index].name,"easy-tcp")==0)
  679. {
  680. use_tcp_dummy_socket=1;
  681. mylog(log_info,"--easy-tcp enabled, now a dummy tcp socket will be created for handshake and block rst\n");
  682. }
  683. else if(strcmp(long_options[option_index].name,"fix-gro")==0)
  684. {
  685. mylog(log_info,"--fix-gro enabled\n");
  686. g_fix_gro=1;
  687. }
  688. else
  689. {
  690. mylog(log_warn,"ignored unknown long option ,option_index:%d code:<%x>\n",option_index, optopt);
  691. }
  692. break;
  693. default:
  694. mylog(log_fatal,"unknown option ,code:<%c>,<%x>\n",optopt, optopt);
  695. myexit(-1);
  696. }
  697. }
  698. if (no_l)
  699. mylog(log_fatal,"error: -l not found\n");
  700. if (no_r)
  701. mylog(log_fatal,"error: -r not found\n");
  702. if(program_mode==0)
  703. mylog(log_fatal,"error: -c /-s hasnt been set\n");
  704. if (no_l || no_r||program_mode==0)
  705. {
  706. print_help();
  707. myexit(-1);
  708. }
  709. if(program_mode==client_mode)
  710. {
  711. raw_ip_version=remote_addr.get_type();
  712. }
  713. else
  714. {
  715. raw_ip_version=local_addr.get_type();
  716. }
  717. if(auto_add_iptables_rule&& use_tcp_dummy_socket)
  718. {
  719. mylog(log_error,"-a,--auto-rule is not supposed to be used with easyfaketcp mode, you are likely making a mistake, but we can try to continue\n");
  720. }
  721. if(keep_rule&& use_tcp_dummy_socket)
  722. {
  723. mylog(log_error,"--keep-rule is not supposed to be used with easyfaketcp mode, you are likely making a mistake, but we can try to continue\n");
  724. }
  725. mylog(log_info,"important variables: ");
  726. log_bare(log_info,"log_level=%d:%s ",log_level,log_text[log_level]);
  727. log_bare(log_info,"raw_mode=%s ",raw_mode_tostring[raw_mode]);
  728. log_bare(log_info,"cipher_mode=%s ",cipher_mode_tostring[cipher_mode]);
  729. log_bare(log_info,"auth_mode=%s ",auth_mode_tostring[auth_mode]);
  730. log_bare(log_info,"key=%s ",key_string);
  731. log_bare(log_info,"local_addr=%s ",local_addr.get_str());
  732. log_bare(log_info,"remote_addr=%s ",remote_addr.get_str());
  733. if(force_source_ip)
  734. log_bare(log_info,"source_addr=%s ",source_addr.get_ip());
  735. if(force_source_port)
  736. log_bare(log_info,"source_port=%d ",source_port);
  737. log_bare(log_info,"socket_buf_size=%d ",socket_buf_size);
  738. log_bare(log_info,"\n");
  739. }
  740. void pre_process_arg(int argc, char *argv[])//mainly for load conf file
  741. {
  742. int i,j,k;
  743. for (i = 0; i < argc; i++)
  744. {
  745. if(strcmp(argv[i],"--unit-test")==0)
  746. {
  747. unit_test();
  748. myexit(0);
  749. }
  750. }
  751. for (i = 0; i < argc; i++)
  752. {
  753. if(strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0)
  754. {
  755. print_help();
  756. myexit(0);
  757. }
  758. }
  759. if (argc == 1)
  760. {
  761. print_help();
  762. myexit(-1);
  763. }
  764. process_log_level(argc,argv);
  765. int new_argc=0;
  766. vector<string> new_argv;
  767. int count=0;
  768. int pos=-1;
  769. for (i = 0; i < argc; i++)
  770. {
  771. if(strcmp(argv[i],"--conf-file")==0)
  772. {
  773. count++;
  774. pos=i;
  775. if(i==argc)
  776. {
  777. mylog(log_fatal,"--conf-file need a parameter\n");
  778. myexit(-1);
  779. }
  780. if(argv[i+1][0]=='-')
  781. {
  782. mylog(log_fatal,"--conf-file need a parameter\n");
  783. myexit(-1);
  784. }
  785. i++;
  786. }
  787. else
  788. {
  789. //printf("<%s>",argv[i]);
  790. new_argc++;
  791. new_argv.push_back(argv[i]);
  792. }
  793. }
  794. if(count>1)
  795. {
  796. mylog(log_fatal,"duplicated --conf-file option\n");
  797. myexit(-1);
  798. }
  799. if(count>0)
  800. {
  801. load_config(argv[pos+1],new_argc,new_argv);
  802. }
  803. char* new_argv_char[new_argv.size()];
  804. new_argc=0;
  805. for(i=0;i<(int)new_argv.size();i++)
  806. {
  807. if(strcmp(new_argv[i].c_str(),"--conf-file")==0)
  808. {
  809. mylog(log_fatal,"cant have --conf-file in a config file\n");
  810. myexit(-1);
  811. }
  812. new_argv_char[new_argc++]=(char *)new_argv[i].c_str();
  813. }
  814. process_arg(new_argc,new_argv_char);
  815. }
  816. void *run_keep(void *none) //called in a new thread for --keep-rule option
  817. {
  818. while(1)
  819. {
  820. sleep(iptables_rule_keep_interval);
  821. keep_iptables_rule();
  822. if(about_to_exit) //just incase it runs forever if there is some bug,not necessary
  823. {
  824. sleep(10);
  825. keep_thread_running=0; //not thread safe ,but wont cause problem
  826. break;
  827. }
  828. }
  829. return NULL;
  830. }
  831. void iptables_rule() // handles -a -g --gen-add --keep-rule --clear --wait-lock
  832. {
  833. assert(raw_ip_version==AF_INET||raw_ip_version==AF_INET6);
  834. if(raw_ip_version==AF_INET)
  835. {
  836. iptables_command0="iptables ";
  837. }
  838. else
  839. iptables_command0="ip6tables ";
  840. if(!wait_xtables_lock)
  841. {
  842. iptables_command=iptables_command0;
  843. }
  844. else
  845. {
  846. iptables_command=iptables_command0+"-w ";
  847. }
  848. if(clear_iptables)
  849. {
  850. char *output;
  851. //int ret =system("iptables-save |grep udp2raw_dWRwMnJhdw|sed -n 's/^-A/iptables -D/p'|sh");
  852. int ret =run_command(iptables_command+"-S|sed -n '/udp2rawDwrW/p'|sed -n 's/^-A/"+iptables_command+"-D/p'|sh",output);
  853. int ret2 =run_command(iptables_command+"-S|sed -n '/udp2rawDwrW/p'|sed -n 's/^-N/"+iptables_command+"-X/p'|sh",output);
  854. //system("iptables-save |grep udp2raw_dWRwMnJhdw|sed 's/^-A/iptables -D/'|sh");
  855. //system("iptables-save|grep -v udp2raw_dWRwMnJhdw|iptables-restore");
  856. mylog(log_info,"tried to clear all iptables rule created previously,return value %d %d\n",ret,ret2);
  857. myexit(-1);
  858. }
  859. if(auto_add_iptables_rule&&generate_iptables_rule)
  860. {
  861. mylog(log_warn," -g overrides -a\n");
  862. auto_add_iptables_rule=0;
  863. //myexit(-1);
  864. }
  865. if(generate_iptables_rule_add&&generate_iptables_rule)
  866. {
  867. mylog(log_warn," --gen-add overrides -g\n");
  868. generate_iptables_rule=0;
  869. //myexit(-1);
  870. }
  871. if(keep_rule&&auto_add_iptables_rule==0)
  872. {
  873. auto_add_iptables_rule=1;
  874. mylog(log_warn," --keep_rule implys -a\n");
  875. generate_iptables_rule=0;
  876. //myexit(-1);
  877. }
  878. char tmp_pattern[200];
  879. string pattern="";
  880. if(program_mode==client_mode)
  881. {
  882. tmp_pattern[0]=0;
  883. if(raw_mode==mode_faketcp)
  884. {
  885. sprintf(tmp_pattern,"-s %s -p tcp -m tcp --sport %d",remote_addr.get_ip(),remote_addr.get_port());
  886. }
  887. if(raw_mode==mode_udp)
  888. {
  889. sprintf(tmp_pattern,"-s %s -p udp -m udp --sport %d",remote_addr.get_ip(),remote_addr.get_port());
  890. }
  891. if(raw_mode==mode_icmp)
  892. {
  893. if(raw_ip_version==AF_INET)
  894. sprintf(tmp_pattern,"-s %s -p icmp --icmp-type 0",remote_addr.get_ip());
  895. else
  896. sprintf(tmp_pattern,"-s %s -p icmpv6 --icmpv6-type 129",remote_addr.get_ip());
  897. }
  898. pattern+=tmp_pattern;
  899. }
  900. if(program_mode==server_mode)
  901. {
  902. tmp_pattern[0]=0;
  903. if(raw_ip_version==AF_INET)
  904. {
  905. if(local_addr.inner.ipv4.sin_addr.s_addr!=0)
  906. {
  907. sprintf(tmp_pattern,"-d %s ",local_addr.get_ip());
  908. }
  909. }
  910. else
  911. {
  912. char zero_arr[16]={0};
  913. if(memcmp(&local_addr.inner.ipv6.sin6_addr,zero_arr,16)!=0)
  914. {
  915. sprintf(tmp_pattern,"-d %s ",local_addr.get_ip());
  916. }
  917. }
  918. pattern+=tmp_pattern;
  919. tmp_pattern[0]=0;
  920. if(raw_mode==mode_faketcp)
  921. {
  922. sprintf(tmp_pattern,"-p tcp -m tcp --dport %d",local_addr.get_port());
  923. }
  924. if(raw_mode==mode_udp)
  925. {
  926. sprintf(tmp_pattern,"-p udp -m udp --dport %d",local_addr.get_port());
  927. }
  928. if(raw_mode==mode_icmp)
  929. {
  930. if(raw_ip_version==AF_INET)
  931. sprintf(tmp_pattern,"-p icmp --icmp-type 8");
  932. else
  933. sprintf(tmp_pattern,"-p icmpv6 --icmpv6-type 128");
  934. }
  935. pattern+=tmp_pattern;
  936. }
  937. /*
  938. if(!simple_rule)
  939. {
  940. pattern += " -m comment --comment udp2rawDwrW_";
  941. char const_id_str[100];
  942. sprintf(const_id_str, "%x_", const_id);
  943. pattern += const_id_str;
  944. time_t timer;
  945. char buffer[26];
  946. struct tm* tm_info;
  947. time(&timer);
  948. tm_info = localtime(&timer);
  949. strftime(buffer, 26, "%Y-%m-%d-%H:%M:%S", tm_info);
  950. pattern += buffer;
  951. }*/
  952. if(generate_iptables_rule)
  953. {
  954. string rule=iptables_command+"-I INPUT ";
  955. rule+=pattern;
  956. rule+=" -j DROP";
  957. printf("generated iptables rule:\n");
  958. printf("%s\n",rule.c_str());
  959. myexit(0);
  960. }
  961. if(generate_iptables_rule_add)
  962. {
  963. iptables_gen_add(pattern.c_str(),const_id);
  964. myexit(0);
  965. }
  966. if(auto_add_iptables_rule)
  967. {
  968. iptables_rule_init(pattern.c_str(),const_id,keep_rule);
  969. if(keep_rule)
  970. {
  971. if(pthread_create(&keep_thread, NULL, run_keep, 0)) {
  972. mylog(log_fatal, "Error creating thread\n");
  973. myexit(-1);
  974. }
  975. keep_thread_running=1;
  976. }
  977. }
  978. else
  979. {
  980. mylog(log_warn," -a has not been set, make sure you have added the needed iptables rules manually\n");
  981. }
  982. }
  983. int unit_test()
  984. {
  985. printf("running unit test\n");
  986. vector<string> conf_lines= {"---aaa","--aaa bbb","-a bbb"," \t \t \t-a\t \t \t bbbbb\t \t \t "};
  987. for(int i=0;i<int(conf_lines.size());i++)
  988. {
  989. printf("orign:%s\n",conf_lines[i].c_str());
  990. auto res=parse_conf_line(conf_lines[i]);
  991. printf("pasrse_result: size %d",int(res.size()));
  992. for(int j=0;j<int(res.size());j++)
  993. {
  994. printf("<%s>",res[j].c_str());
  995. }
  996. printf("\n");
  997. }
  998. char s1[]={1,2,3,4,5};
  999. char s2[]={1};
  1000. short c1=csum((unsigned short*)s1,5);
  1001. short c2=csum((unsigned short*)s2,1);
  1002. //c2=0;
  1003. printf("%x %x\n",(int)c1,(int)c2);
  1004. const char buf[]={1,2,3,4,5,6,7,8,9,10,11,2,13,14,15,16};
  1005. char key[100]={0};
  1006. char buf2[100]={0};
  1007. char buf3[100]={0};
  1008. char buf4[100]={0};
  1009. int len=16;
  1010. for(int i=0;i<len;i++)
  1011. {
  1012. printf("<%d>",buf[i]);
  1013. }
  1014. printf("\n");
  1015. cipher_encrypt(buf,buf2,len,key);
  1016. for(int i=0;i<len;i++)
  1017. {
  1018. printf("<%d>",buf2[i]);
  1019. }
  1020. printf("\n");
  1021. int temp_len=len;
  1022. cipher_decrypt(buf2,buf3,len,key);
  1023. for(int i=0;i<len;i++)
  1024. {
  1025. printf("<%d>",buf3[i]);
  1026. }
  1027. printf("\n");
  1028. cipher_encrypt(buf2,buf4,temp_len,key);
  1029. for(int i=0;i<temp_len;i++)
  1030. {
  1031. printf("<%d>",buf4[i]);
  1032. }
  1033. return 0;
  1034. }
  1035. int set_timer(int epollfd,int &timer_fd)//put a timer_fd into epoll,general function,used both in client and server
  1036. {
  1037. int ret;
  1038. epoll_event ev;
  1039. itimerspec its;
  1040. memset(&its,0,sizeof(its));
  1041. if((timer_fd=timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK)) < 0)
  1042. {
  1043. mylog(log_fatal,"timer_fd create error\n");
  1044. myexit(1);
  1045. }
  1046. its.it_interval.tv_sec=(timer_interval/1000);
  1047. its.it_interval.tv_nsec=(timer_interval%1000)*1000ll*1000ll;
  1048. its.it_value.tv_nsec=1; //imidiately
  1049. timerfd_settime(timer_fd,0,&its,0);
  1050. ev.events = EPOLLIN;
  1051. ev.data.u64 = timer_fd;
  1052. ret=epoll_ctl(epollfd, EPOLL_CTL_ADD, timer_fd, &ev);
  1053. if (ret < 0) {
  1054. mylog(log_fatal,"epoll_ctl return %d\n", ret);
  1055. myexit(-1);
  1056. }
  1057. return 0;
  1058. }
  1059. int set_timer_server(int epollfd,int &timer_fd,fd64_t &fd64)//only for server
  1060. {
  1061. int ret;
  1062. epoll_event ev;
  1063. itimerspec its;
  1064. memset(&its,0,sizeof(its));
  1065. if((timer_fd=timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK)) < 0)
  1066. {
  1067. mylog(log_fatal,"timer_fd create error\n");
  1068. myexit(1);
  1069. }
  1070. its.it_interval.tv_sec=(timer_interval/1000);
  1071. its.it_interval.tv_nsec=(timer_interval%1000)*1000ll*1000ll;
  1072. its.it_value.tv_nsec=1; //imidiately
  1073. timerfd_settime(timer_fd,0,&its,0);
  1074. fd64=fd_manager.create(timer_fd);
  1075. ev.events = EPOLLIN;
  1076. ev.data.u64 = fd64;////difference
  1077. ret=epoll_ctl(epollfd, EPOLL_CTL_ADD, timer_fd, &ev);
  1078. if (ret < 0) {
  1079. mylog(log_fatal,"epoll_ctl return %d\n", ret);
  1080. myexit(-1);
  1081. }
  1082. return 0;
  1083. }
  1084. int handle_lower_level(raw_info_t &raw_info)//fill lower_level info,when --lower-level is enabled,only for server
  1085. {
  1086. packet_info_t &send_info=raw_info.send_info;
  1087. packet_info_t &recv_info=raw_info.recv_info;
  1088. if(lower_level_manual)
  1089. {
  1090. memset(&send_info.addr_ll,0,sizeof(send_info.addr_ll));
  1091. send_info.addr_ll.sll_family=AF_PACKET;
  1092. send_info.addr_ll.sll_ifindex=ifindex;
  1093. send_info.addr_ll.sll_halen=ETHER_ADDR_LEN;
  1094. send_info.addr_ll.sll_protocol=htons(ETH_P_IP);
  1095. memcpy(&send_info.addr_ll.sll_addr,dest_hw_addr,ETHER_ADDR_LEN);
  1096. mylog(log_debug,"[manual]lower level info %x %x\n ",send_info.addr_ll.sll_halen,send_info.addr_ll.sll_protocol);
  1097. }
  1098. else
  1099. {
  1100. memset(&send_info.addr_ll,0,sizeof(send_info.addr_ll));
  1101. send_info.addr_ll.sll_family=recv_info.addr_ll.sll_family;
  1102. send_info.addr_ll.sll_ifindex=recv_info.addr_ll.sll_ifindex;
  1103. send_info.addr_ll.sll_protocol=recv_info.addr_ll.sll_protocol;
  1104. send_info.addr_ll.sll_halen=recv_info.addr_ll.sll_halen;
  1105. memcpy(send_info.addr_ll.sll_addr,recv_info.addr_ll.sll_addr,sizeof(send_info.addr_ll.sll_addr));
  1106. //other bytes should be kept zero.
  1107. mylog(log_debug,"[auto]lower level info %x %x\n ",send_info.addr_ll.sll_halen,send_info.addr_ll.sll_protocol);
  1108. }
  1109. return 0;
  1110. }
  1111. string chain[2];
  1112. string rule_keep[2];
  1113. string rule_keep_add[2];
  1114. string rule_keep_del[2];
  1115. u64_t keep_rule_last_time=0;
  1116. pthread_t keep_thread;
  1117. int keep_thread_running=0;
  1118. int iptables_gen_add(const char * s,u32_t const_id)
  1119. {
  1120. string dummy="";
  1121. iptables_pattern=s;
  1122. chain[0] =dummy+ "udp2rawDwrW_C";
  1123. rule_keep[0]=dummy+ iptables_pattern+" -j " +chain[0];
  1124. rule_keep_add[0]=iptables_command+"-I INPUT "+rule_keep[0];
  1125. char *output;
  1126. run_command(iptables_command+"-N "+chain[0],output,show_none);
  1127. run_command(iptables_command+"-F "+chain[0],output);
  1128. run_command(iptables_command+"-I "+chain[0] + " -j DROP",output);
  1129. rule_keep_del[0]=iptables_command+"-D INPUT "+rule_keep[0];
  1130. run_command(rule_keep_del[0],output,show_none);
  1131. run_command(rule_keep_del[0],output,show_none);
  1132. if(run_command(rule_keep_add[0],output)!=0)
  1133. {
  1134. mylog(log_fatal,"auto added iptables failed by: %s\n",rule_keep_add[0].c_str());
  1135. myexit(-1);
  1136. }
  1137. return 0;
  1138. }
  1139. int iptables_rule_init(const char * s,u32_t const_id,int keep)
  1140. {
  1141. iptables_pattern=s;
  1142. iptables_rule_added=1;
  1143. iptables_rule_keeped=keep;
  1144. string dummy="";
  1145. char const_id_str[100];
  1146. sprintf(const_id_str, "%x", const_id);
  1147. chain[0] =dummy+ "udp2rawDwrW_"+const_id_str+"_C0";
  1148. chain[1] =dummy+ "udp2rawDwrW_"+const_id_str+"_C1";
  1149. rule_keep[0]=dummy+ iptables_pattern+" -j " +chain[0];
  1150. rule_keep[1]=dummy+ iptables_pattern+" -j " +chain[1];
  1151. rule_keep_add[0]=iptables_command+"-I INPUT "+rule_keep[0];
  1152. rule_keep_add[1]=iptables_command+"-I INPUT "+rule_keep[1];
  1153. rule_keep_del[0]=iptables_command+"-D INPUT "+rule_keep[0];
  1154. rule_keep_del[1]=iptables_command+"-D INPUT "+rule_keep[1];
  1155. keep_rule_last_time=get_current_time();
  1156. char *output;
  1157. for(int i=0;i<=iptables_rule_keeped;i++)
  1158. {
  1159. run_command(iptables_command+"-N "+chain[i],output);
  1160. run_command(iptables_command+"-F "+chain[i],output);
  1161. run_command(iptables_command+"-I "+chain[i] + " -j DROP",output);
  1162. if(run_command(rule_keep_add[i],output)!=0)
  1163. {
  1164. mylog(log_fatal,"auto added iptables failed by: %s\n",rule_keep_add[i].c_str());
  1165. myexit(-1);
  1166. }
  1167. }
  1168. mylog(log_warn,"auto added iptables rules\n");
  1169. return 0;
  1170. }
  1171. int keep_iptables_rule() //magic to work on a machine without grep/iptables --check/-m commment
  1172. {
  1173. /*
  1174. if(iptables_rule_keeped==0) return 0;
  1175. uint64_t tmp_current_time=get_current_time();
  1176. if(tmp_current_time-keep_rule_last_time<=iptables_rule_keep_interval)
  1177. {
  1178. return 0;
  1179. }
  1180. else
  1181. {
  1182. keep_rule_last_time=tmp_current_time;
  1183. }*/
  1184. mylog(log_debug,"keep_iptables_rule begin %llu\n",get_current_time());
  1185. iptables_rule_keep_index+=1;
  1186. iptables_rule_keep_index%=2;
  1187. string dummy="";
  1188. char *output;
  1189. int i=iptables_rule_keep_index;
  1190. run_command(iptables_command + "-N " + chain[i], output,show_none);
  1191. if (run_command(iptables_command + "-F " + chain[i], output,show_none) != 0)
  1192. mylog(log_warn, "iptables -F failed %d\n",i);
  1193. if (run_command(iptables_command + "-I " + chain[i] + " -j DROP",output,show_none) != 0)
  1194. mylog(log_warn, "iptables -I failed %d\n",i);
  1195. if (run_command(rule_keep_del[i], output,show_none) != 0)
  1196. mylog(log_warn, "rule_keep_del failed %d\n",i);
  1197. run_command(rule_keep_del[i], output,show_none); //do it twice,incase it fails for unknown random reason
  1198. if(run_command(rule_keep_add[i], output,show_log)!=0)
  1199. mylog(log_warn, "rule_keep_del failed %d\n",i);
  1200. mylog(log_debug,"keep_iptables_rule end %llu\n",get_current_time());
  1201. return 0;
  1202. }
  1203. int clear_iptables_rule()
  1204. {
  1205. char *output;
  1206. string dummy="";
  1207. if(!iptables_rule_added) return 0;
  1208. for(int i=0;i<=iptables_rule_keeped;i++ )
  1209. {
  1210. run_command(rule_keep_del[i],output);
  1211. run_command(iptables_command+"-F "+chain[i],output);
  1212. run_command(iptables_command+"-X "+chain[i],output);
  1213. }
  1214. return 0;
  1215. }
  1216. void signal_handler(int sig)
  1217. {
  1218. about_to_exit=1;
  1219. // myexit(0);
  1220. }