utils.c 18 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /*
  2. * firewall3 - 3rd OpenWrt UCI firewall implementation
  3. *
  4. * Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #define _GNU_SOURCE
  19. #include <net/if.h>
  20. #include <sys/ioctl.h>
  21. #include "utils.h"
  22. #include "options.h"
  23. #include "zones.h"
  24. #include "ipsets.h"
  25. static int fw3_lock_fd = -1;
  26. static pid_t pipe_pid = -1;
  27. static FILE *pipe_fd = NULL;
  28. bool fw3_pr_debug = false;
  29. static void
  30. warn_elem_section_name(struct uci_section *s, bool find_name)
  31. {
  32. int i = 0;
  33. struct uci_option *o;
  34. struct uci_element *tmp;
  35. if (s->anonymous)
  36. {
  37. uci_foreach_element(&s->package->sections, tmp)
  38. {
  39. if (strcmp(uci_to_section(tmp)->type, s->type))
  40. continue;
  41. if (&s->e == tmp)
  42. break;
  43. i++;
  44. }
  45. fprintf(stderr, "@%s[%d]", s->type, i);
  46. if (find_name)
  47. {
  48. uci_foreach_element(&s->options, tmp)
  49. {
  50. o = uci_to_option(tmp);
  51. if (!strcmp(tmp->name, "name") && (o->type == UCI_TYPE_STRING))
  52. {
  53. fprintf(stderr, " (%s)", o->v.string);
  54. break;
  55. }
  56. }
  57. }
  58. }
  59. else
  60. {
  61. fprintf(stderr, "'%s'", s->e.name);
  62. }
  63. if (find_name)
  64. fprintf(stderr, " ");
  65. }
  66. void
  67. warn_elem(struct uci_element *e, const char *format, ...)
  68. {
  69. if (e->type == UCI_TYPE_SECTION)
  70. {
  71. fprintf(stderr, "Warning: Section ");
  72. warn_elem_section_name(uci_to_section(e), true);
  73. }
  74. else if (e->type == UCI_TYPE_OPTION)
  75. {
  76. fprintf(stderr, "Warning: Option ");
  77. warn_elem_section_name(uci_to_option(e)->section, false);
  78. fprintf(stderr, ".%s ", e->name);
  79. }
  80. va_list argptr;
  81. va_start(argptr, format);
  82. vfprintf(stderr, format, argptr);
  83. va_end(argptr);
  84. fprintf(stderr, "\n");
  85. }
  86. void
  87. warn(const char* format, ...)
  88. {
  89. fprintf(stderr, "Warning: ");
  90. va_list argptr;
  91. va_start(argptr, format);
  92. vfprintf(stderr, format, argptr);
  93. va_end(argptr);
  94. fprintf(stderr, "\n");
  95. }
  96. void
  97. error(const char* format, ...)
  98. {
  99. fprintf(stderr, "Error: ");
  100. va_list argptr;
  101. va_start(argptr, format);
  102. vfprintf(stderr, format, argptr);
  103. va_end(argptr);
  104. fprintf(stderr, "\n");
  105. exit(1);
  106. }
  107. void
  108. info(const char* format, ...)
  109. {
  110. va_list argptr;
  111. va_start(argptr, format);
  112. vfprintf(stderr, format, argptr);
  113. va_end(argptr);
  114. fprintf(stderr, "\n");
  115. }
  116. void *
  117. fw3_alloc(size_t size)
  118. {
  119. void *mem;
  120. mem = calloc(1, size);
  121. if (!mem)
  122. error("Out of memory while allocating %zd bytes", size);
  123. return mem;
  124. }
  125. char *
  126. fw3_strdup(const char *s)
  127. {
  128. char *ns;
  129. ns = strdup(s);
  130. if (!ns)
  131. error("Out of memory while duplicating string '%s'", s);
  132. return ns;
  133. }
  134. const char *
  135. fw3_find_command(const char *cmd)
  136. {
  137. struct stat s;
  138. int plen = 0, clen = strlen(cmd) + 1;
  139. char *search, *p;
  140. static char path[PATH_MAX];
  141. if (!stat(cmd, &s) && S_ISREG(s.st_mode))
  142. return cmd;
  143. search = getenv("PATH");
  144. if (!search)
  145. search = "/bin:/usr/bin:/sbin:/usr/sbin";
  146. p = search;
  147. do
  148. {
  149. if (*p != ':' && *p != '\0')
  150. continue;
  151. plen = p - search;
  152. if ((plen + clen) >= sizeof(path))
  153. continue;
  154. snprintf(path, sizeof(path), "%.*s/%s", plen, search, cmd);
  155. if (!stat(path, &s) && S_ISREG(s.st_mode))
  156. return path;
  157. search = p + 1;
  158. }
  159. while (*p++);
  160. return NULL;
  161. }
  162. bool
  163. fw3_stdout_pipe(void)
  164. {
  165. pipe_fd = stdout;
  166. return true;
  167. }
  168. bool
  169. __fw3_command_pipe(bool silent, const char *command, ...)
  170. {
  171. pid_t pid;
  172. va_list argp;
  173. int pfds[2];
  174. int argn;
  175. char *arg, **args, **tmp;
  176. command = fw3_find_command(command);
  177. if (!command)
  178. return false;
  179. if (pipe(pfds))
  180. return false;
  181. argn = 2;
  182. args = calloc(argn, sizeof(arg));
  183. if (!args)
  184. return false;
  185. args[0] = (char *)command;
  186. args[1] = NULL;
  187. va_start(argp, command);
  188. while ((arg = va_arg(argp, char *)) != NULL)
  189. {
  190. tmp = realloc(args, ++argn * sizeof(arg));
  191. if (!tmp)
  192. break;
  193. args = tmp;
  194. args[argn-2] = arg;
  195. args[argn-1] = NULL;
  196. }
  197. va_end(argp);
  198. switch ((pid = fork()))
  199. {
  200. case -1:
  201. free(args);
  202. return false;
  203. case 0:
  204. dup2(pfds[0], 0);
  205. close(pfds[0]);
  206. close(pfds[1]);
  207. close(1);
  208. if (silent)
  209. close(2);
  210. execv(command, args);
  211. default:
  212. signal(SIGPIPE, SIG_IGN);
  213. pipe_pid = pid;
  214. close(pfds[0]);
  215. fcntl(pfds[1], F_SETFD, fcntl(pfds[1], F_GETFD) | FD_CLOEXEC);
  216. }
  217. pipe_fd = fdopen(pfds[1], "w");
  218. free(args);
  219. return true;
  220. }
  221. void
  222. fw3_pr(const char *fmt, ...)
  223. {
  224. va_list args;
  225. if (fw3_pr_debug && pipe_fd != stdout)
  226. {
  227. va_start(args, fmt);
  228. vfprintf(stderr, fmt, args);
  229. va_end(args);
  230. }
  231. va_start(args, fmt);
  232. vfprintf(pipe_fd, fmt, args);
  233. va_end(args);
  234. }
  235. void
  236. fw3_command_close(void)
  237. {
  238. if (pipe_fd && pipe_fd != stdout)
  239. fclose(pipe_fd);
  240. if (pipe_pid > -1)
  241. waitpid(pipe_pid, NULL, 0);
  242. signal(SIGPIPE, SIG_DFL);
  243. pipe_fd = NULL;
  244. pipe_pid = -1;
  245. }
  246. static bool
  247. file_contains(const char *path, const char *str)
  248. {
  249. FILE *f;
  250. char line[12];
  251. bool seen = false;
  252. if (!(f = fopen(path, "r")))
  253. return false;
  254. while (fgets(line, sizeof(line), f))
  255. {
  256. if (!strncmp(line, str, strlen(str)))
  257. {
  258. seen = true;
  259. break;
  260. }
  261. }
  262. fclose(f);
  263. return seen;
  264. }
  265. bool
  266. fw3_has_target(const bool ipv6, const char *target)
  267. {
  268. const char *path = ipv6
  269. ? "/proc/net/ip6_tables_targets" : "/proc/net/ip_tables_targets";
  270. return file_contains(path, target);
  271. }
  272. bool
  273. fw3_lock_path(int *fd, const char *path)
  274. {
  275. int lock_fd = open(path, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
  276. if (lock_fd < 0)
  277. {
  278. warn("Cannot create lock file %s: %s", path, strerror(errno));
  279. return false;
  280. }
  281. if (flock(lock_fd, LOCK_EX))
  282. {
  283. warn("Cannot acquire exclusive lock: %s", strerror(errno));
  284. close(lock_fd);
  285. return false;
  286. }
  287. *fd = lock_fd;
  288. return true;
  289. }
  290. bool
  291. fw3_lock()
  292. {
  293. return fw3_lock_path(&fw3_lock_fd, FW3_LOCKFILE);
  294. }
  295. void
  296. fw3_unlock_path(int *fd, const char *lockpath)
  297. {
  298. if (*fd < 0)
  299. return;
  300. if (flock(*fd, LOCK_UN))
  301. warn("Cannot release exclusive lock: %s", strerror(errno));
  302. close(*fd);
  303. *fd = -1;
  304. }
  305. void
  306. fw3_unlock(void)
  307. {
  308. fw3_unlock_path(&fw3_lock_fd, FW3_LOCKFILE);
  309. }
  310. static void
  311. write_defaults_uci(struct uci_context *ctx, struct fw3_defaults *d,
  312. struct uci_package *dest)
  313. {
  314. char buf[sizeof("0xffffffff")];
  315. struct uci_ptr ptr = { .p = dest };
  316. uci_add_section(ctx, dest, "defaults", &ptr.s);
  317. ptr.o = NULL;
  318. ptr.option = "input";
  319. ptr.value = fw3_flag_names[d->policy_input];
  320. uci_set(ctx, &ptr);
  321. ptr.o = NULL;
  322. ptr.option = "output";
  323. ptr.value = fw3_flag_names[d->policy_output];
  324. uci_set(ctx, &ptr);
  325. ptr.o = NULL;
  326. ptr.option = "forward";
  327. ptr.value = fw3_flag_names[d->policy_forward];
  328. uci_set(ctx, &ptr);
  329. snprintf(buf, sizeof(buf), "0x%x", d->flags[0]);
  330. ptr.o = NULL;
  331. ptr.option = "__flags_v4";
  332. ptr.value = buf;
  333. uci_set(ctx, &ptr);
  334. snprintf(buf, sizeof(buf), "0x%x", d->flags[1]);
  335. ptr.o = NULL;
  336. ptr.option = "__flags_v6";
  337. ptr.value = buf;
  338. uci_set(ctx, &ptr);
  339. }
  340. static void
  341. write_zone_uci(struct uci_context *ctx, struct fw3_zone *z,
  342. struct uci_package *dest, struct ifaddrs *ifaddr)
  343. {
  344. struct fw3_device *dev;
  345. struct fw3_address *sub;
  346. struct ifaddrs *ifa;
  347. enum fw3_family fam = FW3_FAMILY_ANY;
  348. char *p, buf[INET6_ADDRSTRLEN];
  349. struct uci_ptr ptr = { .p = dest };
  350. if (!z->enabled)
  351. return;
  352. if (fw3_no_table(z->flags[0]) && !fw3_no_table(z->flags[1]))
  353. fam = FW3_FAMILY_V6;
  354. else if (!fw3_no_table(z->flags[0]) && fw3_no_table(z->flags[1]))
  355. fam = FW3_FAMILY_V4;
  356. else if (fw3_no_table(z->flags[0]) && fw3_no_table(z->flags[1]))
  357. return;
  358. uci_add_section(ctx, dest, "zone", &ptr.s);
  359. ptr.o = NULL;
  360. ptr.option = "name";
  361. ptr.value = z->name;
  362. uci_set(ctx, &ptr);
  363. ptr.o = NULL;
  364. ptr.option = "input";
  365. ptr.value = fw3_flag_names[z->policy_input];
  366. uci_set(ctx, &ptr);
  367. ptr.o = NULL;
  368. ptr.option = "output";
  369. ptr.value = fw3_flag_names[z->policy_output];
  370. uci_set(ctx, &ptr);
  371. ptr.o = NULL;
  372. ptr.option = "forward";
  373. ptr.value = fw3_flag_names[z->policy_forward];
  374. uci_set(ctx, &ptr);
  375. ptr.o = NULL;
  376. ptr.option = "masq";
  377. ptr.value = z->masq ? "1" : "0";
  378. uci_set(ctx, &ptr);
  379. ptr.o = NULL;
  380. ptr.option = "mtu_fix";
  381. ptr.value = z->mtu_fix ? "1" : "0";
  382. uci_set(ctx, &ptr);
  383. ptr.o = NULL;
  384. ptr.option = "custom_chains";
  385. ptr.value = z->custom_chains ? "1" : "0";
  386. uci_set(ctx, &ptr);
  387. if (fam != FW3_FAMILY_ANY)
  388. {
  389. ptr.o = NULL;
  390. ptr.option = "family";
  391. ptr.value = fw3_flag_names[fam];
  392. uci_set(ctx, &ptr);
  393. }
  394. ptr.o = NULL;
  395. ptr.option = "device";
  396. fw3_foreach(dev, &z->devices)
  397. {
  398. char *ep;
  399. if (!dev)
  400. continue;
  401. p = buf;
  402. ep = buf + sizeof(buf);
  403. if (dev->invert)
  404. p += snprintf(p, ep - p, "!");
  405. if (*dev->network)
  406. p += snprintf(p, ep - p, "%s@%s", dev->name, dev->network);
  407. else
  408. p += snprintf(p, ep - p, "%s", dev->name);
  409. ptr.value = buf;
  410. uci_add_list(ctx, &ptr);
  411. }
  412. ptr.o = NULL;
  413. ptr.option = "subnet";
  414. fw3_foreach(sub, &z->subnets)
  415. {
  416. if (!sub)
  417. continue;
  418. ptr.value = fw3_address_to_string(sub, true, false);
  419. uci_add_list(ctx, &ptr);
  420. }
  421. ptr.o = NULL;
  422. ptr.option = "__addrs";
  423. fw3_foreach(dev, &z->devices)
  424. {
  425. if (!dev)
  426. continue;
  427. for (ifa = ifaddr; ifa; ifa = ifa->ifa_next)
  428. {
  429. if (!ifa->ifa_addr || strcmp(dev->name, ifa->ifa_name))
  430. continue;
  431. if (ifa->ifa_addr->sa_family == AF_INET)
  432. inet_ntop(AF_INET,
  433. &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr,
  434. buf, sizeof(buf));
  435. else if (ifa->ifa_addr->sa_family == AF_INET6)
  436. inet_ntop(AF_INET6,
  437. &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr,
  438. buf, sizeof(buf));
  439. else
  440. continue;
  441. ptr.value = buf;
  442. uci_add_list(ctx, &ptr);
  443. }
  444. }
  445. if (z->extra_src)
  446. {
  447. ptr.o = NULL;
  448. ptr.option = "extra_src";
  449. ptr.value = z->extra_src;
  450. uci_set(ctx, &ptr);
  451. }
  452. if (z->extra_dest)
  453. {
  454. ptr.o = NULL;
  455. ptr.option = "extra_dest";
  456. ptr.value = z->extra_dest;
  457. uci_set(ctx, &ptr);
  458. }
  459. snprintf(buf, sizeof(buf), "0x%x", z->flags[0]);
  460. ptr.o = NULL;
  461. ptr.option = "__flags_v4";
  462. ptr.value = buf;
  463. uci_set(ctx, &ptr);
  464. snprintf(buf, sizeof(buf), "0x%x", z->flags[1]);
  465. ptr.o = NULL;
  466. ptr.option = "__flags_v6";
  467. ptr.value = buf;
  468. uci_set(ctx, &ptr);
  469. }
  470. static void
  471. write_ipset_uci(struct uci_context *ctx, struct fw3_ipset *s,
  472. struct uci_package *dest)
  473. {
  474. struct fw3_ipset_datatype *type;
  475. char buf[sizeof("65535-65535")];
  476. struct uci_ptr ptr = { .p = dest };
  477. if (!s->enabled || s->external)
  478. return;
  479. uci_add_section(ctx, dest, "ipset", &ptr.s);
  480. ptr.o = NULL;
  481. ptr.option = "name";
  482. ptr.value = s->name;
  483. uci_set(ctx, &ptr);
  484. ptr.o = NULL;
  485. ptr.option = "family";
  486. if (s->family == FW3_FAMILY_V4)
  487. ptr.value = "ipv4";
  488. else
  489. ptr.value = "ipv6";
  490. uci_set(ctx, &ptr);
  491. ptr.o = NULL;
  492. ptr.option = "storage";
  493. ptr.value = fw3_ipset_method_names[s->method];
  494. uci_set(ctx, &ptr);
  495. list_for_each_entry(type, &s->datatypes, list)
  496. {
  497. snprintf(buf, sizeof(buf), "%s_%s", type->dir, fw3_ipset_type_names[type->type]);
  498. ptr.o = NULL;
  499. ptr.option = "match";
  500. ptr.value = buf;
  501. uci_add_list(ctx, &ptr);
  502. }
  503. if (s->iprange.set)
  504. {
  505. ptr.o = NULL;
  506. ptr.option = "iprange";
  507. ptr.value = fw3_address_to_string(&s->iprange, false, false);
  508. uci_set(ctx, &ptr);
  509. }
  510. if (s->portrange.set)
  511. {
  512. snprintf(buf, sizeof(buf), "%u-%u", s->portrange.port_min, s->portrange.port_max);
  513. ptr.o = NULL;
  514. ptr.option = "portrange";
  515. ptr.value = buf;
  516. uci_set(ctx, &ptr);
  517. }
  518. }
  519. void
  520. fw3_write_statefile(void *state)
  521. {
  522. FILE *sf;
  523. struct fw3_state *s = state;
  524. struct fw3_zone *z;
  525. struct fw3_ipset *i;
  526. struct ifaddrs *ifaddr;
  527. struct uci_package *p;
  528. if (fw3_no_family(s->defaults.flags[0]) &&
  529. fw3_no_family(s->defaults.flags[1]))
  530. {
  531. unlink(FW3_STATEFILE);
  532. }
  533. else
  534. {
  535. sf = fopen(FW3_STATEFILE, "w+");
  536. if (!sf)
  537. {
  538. warn("Cannot create state %s: %s", FW3_STATEFILE, strerror(errno));
  539. return;
  540. }
  541. if (getifaddrs(&ifaddr))
  542. {
  543. warn("Cannot get interface addresses: %s", strerror(errno));
  544. ifaddr = NULL;
  545. }
  546. if ((p = uci_lookup_package(s->uci, "fw3_state")) != NULL)
  547. uci_unload(s->uci, p);
  548. uci_import(s->uci, sf, "fw3_state", NULL, true);
  549. if ((p = uci_lookup_package(s->uci, "fw3_state")) != NULL)
  550. {
  551. write_defaults_uci(s->uci, &s->defaults, p);
  552. list_for_each_entry(z, &s->zones, list)
  553. write_zone_uci(s->uci, z, p, ifaddr);
  554. list_for_each_entry(i, &s->ipsets, list)
  555. write_ipset_uci(s->uci, i, p);
  556. uci_export(s->uci, sf, p, true);
  557. uci_unload(s->uci, p);
  558. }
  559. fsync(fileno(sf));
  560. fclose(sf);
  561. if (ifaddr)
  562. freeifaddrs(ifaddr);
  563. }
  564. }
  565. void
  566. fw3_free_object(void *obj, const void *opts)
  567. {
  568. const struct fw3_option *ol;
  569. struct list_head *list, *cur, *tmp;
  570. for (ol = opts; ol->name; ol++)
  571. {
  572. if (!ol->elem_size)
  573. continue;
  574. list = (struct list_head *)((char *)obj + ol->offset);
  575. list_for_each_safe(cur, tmp, list)
  576. {
  577. list_del(cur);
  578. free(cur);
  579. }
  580. }
  581. free(obj);
  582. }
  583. void
  584. fw3_free_list(struct list_head *head)
  585. {
  586. struct list_head *entry, *tmp;
  587. if (!head)
  588. return;
  589. list_for_each_safe(entry, tmp, head)
  590. {
  591. list_del(entry);
  592. free(entry);
  593. }
  594. free(head);
  595. }
  596. bool
  597. fw3_hotplug(bool add, void *zone, void *device)
  598. {
  599. struct fw3_zone *z = zone;
  600. struct fw3_device *d = device;
  601. if (!*d->network)
  602. return false;
  603. switch (fork())
  604. {
  605. case -1:
  606. warn("Unable to fork(): %s\n", strerror(errno));
  607. return false;
  608. case 0:
  609. break;
  610. default:
  611. return true;
  612. }
  613. close(0);
  614. close(1);
  615. close(2);
  616. if (chdir("/")) {};
  617. clearenv();
  618. setenv("ACTION", add ? "add" : "remove", 1);
  619. setenv("ZONE", z->name, 1);
  620. setenv("INTERFACE", d->network, 1);
  621. setenv("DEVICE", d->name, 1);
  622. execl(FW3_HOTPLUG, FW3_HOTPLUG, "firewall", NULL);
  623. /* unreached */
  624. return false;
  625. }
  626. int
  627. fw3_netmask2bitlen(int family, void *mask)
  628. {
  629. int bits;
  630. struct in_addr *v4;
  631. struct in6_addr *v6;
  632. if (family == FW3_FAMILY_V6)
  633. for (bits = 0, v6 = mask;
  634. bits < 128 && (v6->s6_addr[bits / 8] << (bits % 8)) & 128;
  635. bits++);
  636. else
  637. for (bits = 0, v4 = mask;
  638. bits < 32 && (ntohl(v4->s_addr) << bits) & 0x80000000;
  639. bits++);
  640. return bits;
  641. }
  642. bool
  643. fw3_bitlen2netmask(int family, int bits, void *mask)
  644. {
  645. int i;
  646. uint8_t rem, b;
  647. struct in_addr *v4;
  648. struct in6_addr *v6;
  649. if (family == FW3_FAMILY_V6)
  650. {
  651. if (bits < -128 || bits > 128)
  652. return false;
  653. v6 = mask;
  654. rem = abs(bits);
  655. for (i = 0; i < sizeof(v6->s6_addr); i++)
  656. {
  657. b = (rem > 8) ? 8 : rem;
  658. v6->s6_addr[i] = (uint8_t)(0xFF << (8 - b));
  659. rem -= b;
  660. }
  661. if (bits < 0)
  662. for (i = 0; i < sizeof(v6->s6_addr); i++)
  663. v6->s6_addr[i] = ~v6->s6_addr[i];
  664. }
  665. else
  666. {
  667. if (bits < -32 || bits > 32)
  668. return false;
  669. v4 = mask;
  670. v4->s_addr = bits ? htonl(~((1 << (32 - abs(bits))) - 1)) : 0;
  671. if (bits < 0)
  672. v4->s_addr = ~v4->s_addr;
  673. }
  674. return true;
  675. }
  676. void
  677. fw3_flush_conntrack(void *state)
  678. {
  679. bool found;
  680. struct fw3_state *s = state;
  681. struct fw3_address *addr;
  682. struct fw3_device *dev;
  683. struct fw3_zone *zone;
  684. struct ifaddrs *ifaddr, *ifa;
  685. struct sockaddr_in *sin;
  686. struct sockaddr_in6 *sin6;
  687. char buf[INET6_ADDRSTRLEN];
  688. FILE *ct;
  689. if (!state)
  690. {
  691. if ((ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
  692. {
  693. info(" * Flushing conntrack table ...");
  694. fwrite("f\n", 1, 2, ct);
  695. fclose(ct);
  696. }
  697. return;
  698. }
  699. if (getifaddrs(&ifaddr))
  700. {
  701. warn("Cannot get interface addresses: %s", strerror(errno));
  702. return;
  703. }
  704. if ((ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
  705. {
  706. list_for_each_entry(zone, &s->zones, list)
  707. list_for_each_entry(addr, &zone->old_addrs, list)
  708. {
  709. found = false;
  710. list_for_each_entry(dev, &zone->devices, list)
  711. {
  712. for (ifa = ifaddr; ifa && !found; ifa = ifa->ifa_next)
  713. {
  714. if (!ifa->ifa_addr || strcmp(dev->name, ifa->ifa_name))
  715. continue;
  716. sin = (struct sockaddr_in *)ifa->ifa_addr;
  717. sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
  718. if (addr->family == FW3_FAMILY_V4 &&
  719. sin->sin_family == AF_INET)
  720. {
  721. found = !memcmp(&addr->address.v4, &sin->sin_addr,
  722. sizeof(sin->sin_addr));
  723. }
  724. else if (addr->family == FW3_FAMILY_V6 &&
  725. sin6->sin6_family == AF_INET6)
  726. {
  727. found = !memcmp(&addr->address.v6, &sin6->sin6_addr,
  728. sizeof(sin6->sin6_addr));
  729. }
  730. }
  731. if (found)
  732. break;
  733. }
  734. if (!found)
  735. {
  736. inet_ntop(addr->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
  737. &addr->address.v4, buf, sizeof(buf));
  738. info(" * Flushing conntrack: %s", buf);
  739. fprintf(ct, "%s\n", buf);
  740. }
  741. }
  742. fclose(ct);
  743. }
  744. freeifaddrs(ifaddr);
  745. }
  746. bool fw3_attr_parse_name_type(struct blob_attr *entry, const char **name, const char **type)
  747. {
  748. struct blob_attr *opt;
  749. unsigned orem;
  750. if (!type || !name)
  751. return false;
  752. *type = NULL;
  753. blobmsg_for_each_attr(opt, entry, orem)
  754. if (!strcmp(blobmsg_name(opt), "type"))
  755. *type = blobmsg_get_string(opt);
  756. else if (!strcmp(blobmsg_name(opt), "name"))
  757. *name = blobmsg_get_string(opt);
  758. return *type != NULL ? true : false;
  759. }
  760. const char *
  761. fw3_protoname(void *proto)
  762. {
  763. static char buf[sizeof("4294967295")];
  764. struct fw3_protocol *p = proto;
  765. struct protoent *pe;
  766. if (!p)
  767. return "?";
  768. pe = getprotobynumber(p->protocol);
  769. if (!pe)
  770. {
  771. snprintf(buf, sizeof(buf), "%u", p->protocol);
  772. return buf;
  773. }
  774. return pe->p_name;
  775. }
  776. bool
  777. fw3_check_loopback_dev(const char *name)
  778. {
  779. struct ifreq ifr;
  780. int s;
  781. bool rv = false;
  782. s = socket(AF_LOCAL, SOCK_DGRAM, 0);
  783. if (s < 0)
  784. return false;
  785. memset(&ifr, 0, sizeof(ifr));
  786. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", name);
  787. if (ioctl(s, SIOCGIFFLAGS, &ifr) >= 0) {
  788. if (ifr.ifr_flags & IFF_LOOPBACK)
  789. rv = true;
  790. }
  791. close(s);
  792. return rv;
  793. }
  794. bool
  795. fw3_check_loopback_addr(struct fw3_address *addr)
  796. {
  797. if (addr->family == FW3_FAMILY_V4 &&
  798. (ntohl(addr->address.v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
  799. return true;
  800. if (addr->family == FW3_FAMILY_V6 && !addr->range &&
  801. fw3_netmask2bitlen(FW3_FAMILY_V6, &addr->mask.v6) == 128 &&
  802. IN6_IS_ADDR_LOOPBACK(&addr->address.v6))
  803. return true;
  804. return false;
  805. }