utils.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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 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 %d 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. strncpy(path, search, plen);
  155. sprintf(path + plen, "/%s", cmd);
  156. if (!stat(path, &s) && S_ISREG(s.st_mode))
  157. return path;
  158. search = p + 1;
  159. }
  160. while (*p++);
  161. return NULL;
  162. }
  163. bool
  164. fw3_stdout_pipe(void)
  165. {
  166. pipe_fd = stdout;
  167. return true;
  168. }
  169. bool
  170. __fw3_command_pipe(bool silent, const char *command, ...)
  171. {
  172. pid_t pid;
  173. va_list argp;
  174. int pfds[2];
  175. int argn;
  176. char *arg, **args, **tmp;
  177. command = fw3_find_command(command);
  178. if (!command)
  179. return false;
  180. if (pipe(pfds))
  181. return false;
  182. argn = 2;
  183. args = calloc(argn, sizeof(arg));
  184. if (!args)
  185. return false;
  186. args[0] = (char *)command;
  187. args[1] = NULL;
  188. va_start(argp, command);
  189. while ((arg = va_arg(argp, char *)) != NULL)
  190. {
  191. tmp = realloc(args, ++argn * sizeof(arg));
  192. if (!tmp)
  193. break;
  194. args = tmp;
  195. args[argn-2] = arg;
  196. args[argn-1] = NULL;
  197. }
  198. va_end(argp);
  199. switch ((pid = fork()))
  200. {
  201. case -1:
  202. free(args);
  203. return false;
  204. case 0:
  205. dup2(pfds[0], 0);
  206. close(pfds[0]);
  207. close(pfds[1]);
  208. close(1);
  209. if (silent)
  210. close(2);
  211. execv(command, args);
  212. default:
  213. signal(SIGPIPE, SIG_IGN);
  214. pipe_pid = pid;
  215. close(pfds[0]);
  216. fcntl(pfds[1], F_SETFD, fcntl(pfds[1], F_GETFD) | FD_CLOEXEC);
  217. }
  218. pipe_fd = fdopen(pfds[1], "w");
  219. free(args);
  220. return true;
  221. }
  222. void
  223. fw3_pr(const char *fmt, ...)
  224. {
  225. va_list args;
  226. if (fw3_pr_debug && pipe_fd != stdout)
  227. {
  228. va_start(args, fmt);
  229. vfprintf(stderr, fmt, args);
  230. va_end(args);
  231. }
  232. va_start(args, fmt);
  233. vfprintf(pipe_fd, fmt, args);
  234. va_end(args);
  235. }
  236. void
  237. fw3_command_close(void)
  238. {
  239. if (pipe_fd && pipe_fd != stdout)
  240. fclose(pipe_fd);
  241. if (pipe_pid > -1)
  242. waitpid(pipe_pid, NULL, 0);
  243. signal(SIGPIPE, SIG_DFL);
  244. pipe_fd = NULL;
  245. pipe_pid = -1;
  246. }
  247. bool
  248. fw3_has_table(bool ipv6, const char *table)
  249. {
  250. FILE *f;
  251. char line[12];
  252. bool seen = false;
  253. const char *path = ipv6
  254. ? "/proc/net/ip6_tables_names" : "/proc/net/ip_tables_names";
  255. if (!(f = fopen(path, "r")))
  256. return false;
  257. while (fgets(line, sizeof(line), f))
  258. {
  259. if (!strncmp(line, table, strlen(table)))
  260. {
  261. seen = true;
  262. break;
  263. }
  264. }
  265. fclose(f);
  266. return seen;
  267. }
  268. bool
  269. fw3_lock(void)
  270. {
  271. lock_fd = open(FW3_LOCKFILE, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
  272. if (lock_fd < 0)
  273. {
  274. warn("Cannot create lock file %s: %s", FW3_LOCKFILE, strerror(errno));
  275. return false;
  276. }
  277. if (flock(lock_fd, LOCK_EX))
  278. {
  279. warn("Cannot acquire exclusive lock: %s", strerror(errno));
  280. return false;
  281. }
  282. return true;
  283. }
  284. void
  285. fw3_unlock(void)
  286. {
  287. if (lock_fd < 0)
  288. return;
  289. if (flock(lock_fd, LOCK_UN))
  290. warn("Cannot release exclusive lock: %s", strerror(errno));
  291. close(lock_fd);
  292. unlink(FW3_LOCKFILE);
  293. lock_fd = -1;
  294. }
  295. static void
  296. write_defaults_uci(struct uci_context *ctx, struct fw3_defaults *d,
  297. struct uci_package *dest)
  298. {
  299. char buf[sizeof("0xffffffff\0")];
  300. struct uci_ptr ptr = { .p = dest };
  301. uci_add_section(ctx, dest, "defaults", &ptr.s);
  302. ptr.o = NULL;
  303. ptr.option = "input";
  304. ptr.value = fw3_flag_names[d->policy_input];
  305. uci_set(ctx, &ptr);
  306. ptr.o = NULL;
  307. ptr.option = "output";
  308. ptr.value = fw3_flag_names[d->policy_output];
  309. uci_set(ctx, &ptr);
  310. ptr.o = NULL;
  311. ptr.option = "forward";
  312. ptr.value = fw3_flag_names[d->policy_forward];
  313. uci_set(ctx, &ptr);
  314. sprintf(buf, "0x%x", d->flags[0]);
  315. ptr.o = NULL;
  316. ptr.option = "__flags_v4";
  317. ptr.value = buf;
  318. uci_set(ctx, &ptr);
  319. sprintf(buf, "0x%x", d->flags[1]);
  320. ptr.o = NULL;
  321. ptr.option = "__flags_v6";
  322. ptr.value = buf;
  323. uci_set(ctx, &ptr);
  324. }
  325. static void
  326. write_zone_uci(struct uci_context *ctx, struct fw3_zone *z,
  327. struct uci_package *dest, struct ifaddrs *ifaddr)
  328. {
  329. struct fw3_device *dev;
  330. struct fw3_address *sub;
  331. struct ifaddrs *ifa;
  332. enum fw3_family fam = FW3_FAMILY_ANY;
  333. char *p, buf[INET6_ADDRSTRLEN];
  334. struct uci_ptr ptr = { .p = dest };
  335. if (!z->enabled)
  336. return;
  337. if (fw3_no_table(z->flags[0]) && !fw3_no_table(z->flags[1]))
  338. fam = FW3_FAMILY_V6;
  339. else if (!fw3_no_table(z->flags[0]) && fw3_no_table(z->flags[1]))
  340. fam = FW3_FAMILY_V4;
  341. else if (fw3_no_table(z->flags[0]) && fw3_no_table(z->flags[1]))
  342. return;
  343. uci_add_section(ctx, dest, "zone", &ptr.s);
  344. ptr.o = NULL;
  345. ptr.option = "name";
  346. ptr.value = z->name;
  347. uci_set(ctx, &ptr);
  348. ptr.o = NULL;
  349. ptr.option = "input";
  350. ptr.value = fw3_flag_names[z->policy_input];
  351. uci_set(ctx, &ptr);
  352. ptr.o = NULL;
  353. ptr.option = "output";
  354. ptr.value = fw3_flag_names[z->policy_output];
  355. uci_set(ctx, &ptr);
  356. ptr.o = NULL;
  357. ptr.option = "forward";
  358. ptr.value = fw3_flag_names[z->policy_forward];
  359. uci_set(ctx, &ptr);
  360. ptr.o = NULL;
  361. ptr.option = "masq";
  362. ptr.value = z->masq ? "1" : "0";
  363. uci_set(ctx, &ptr);
  364. ptr.o = NULL;
  365. ptr.option = "mtu_fix";
  366. ptr.value = z->mtu_fix ? "1" : "0";
  367. uci_set(ctx, &ptr);
  368. ptr.o = NULL;
  369. ptr.option = "custom_chains";
  370. ptr.value = z->custom_chains ? "1" : "0";
  371. uci_set(ctx, &ptr);
  372. if (fam != FW3_FAMILY_ANY)
  373. {
  374. ptr.o = NULL;
  375. ptr.option = "family";
  376. ptr.value = fw3_flag_names[fam];
  377. uci_set(ctx, &ptr);
  378. }
  379. ptr.o = NULL;
  380. ptr.option = "device";
  381. fw3_foreach(dev, &z->devices)
  382. {
  383. char *ep;
  384. if (!dev)
  385. continue;
  386. p = buf;
  387. ep = buf + sizeof(buf);
  388. if (dev->invert)
  389. p += snprintf(p, ep - p, "!");
  390. if (*dev->network)
  391. p += snprintf(p, ep - p, "%s@%s", dev->name, dev->network);
  392. else
  393. p += snprintf(p, ep - p, "%s", dev->name);
  394. ptr.value = buf;
  395. uci_add_list(ctx, &ptr);
  396. }
  397. ptr.o = NULL;
  398. ptr.option = "subnet";
  399. fw3_foreach(sub, &z->subnets)
  400. {
  401. if (!sub)
  402. continue;
  403. ptr.value = fw3_address_to_string(sub, true, false);
  404. uci_add_list(ctx, &ptr);
  405. }
  406. ptr.o = NULL;
  407. ptr.option = "__addrs";
  408. fw3_foreach(dev, &z->devices)
  409. {
  410. if (!dev)
  411. continue;
  412. for (ifa = ifaddr; ifa; ifa = ifa->ifa_next)
  413. {
  414. if (!ifa->ifa_addr || strcmp(dev->name, ifa->ifa_name))
  415. continue;
  416. if (ifa->ifa_addr->sa_family == AF_INET)
  417. inet_ntop(AF_INET,
  418. &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr,
  419. buf, sizeof(buf));
  420. else if (ifa->ifa_addr->sa_family == AF_INET6)
  421. inet_ntop(AF_INET6,
  422. &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr,
  423. buf, sizeof(buf));
  424. else
  425. continue;
  426. ptr.value = buf;
  427. uci_add_list(ctx, &ptr);
  428. }
  429. }
  430. sprintf(buf, "0x%x", z->flags[0]);
  431. ptr.o = NULL;
  432. ptr.option = "__flags_v4";
  433. ptr.value = buf;
  434. uci_set(ctx, &ptr);
  435. sprintf(buf, "0x%x", z->flags[1]);
  436. ptr.o = NULL;
  437. ptr.option = "__flags_v6";
  438. ptr.value = buf;
  439. uci_set(ctx, &ptr);
  440. }
  441. static void
  442. write_ipset_uci(struct uci_context *ctx, struct fw3_ipset *s,
  443. struct uci_package *dest)
  444. {
  445. struct fw3_ipset_datatype *type;
  446. char buf[sizeof("65535-65535\0")];
  447. struct uci_ptr ptr = { .p = dest };
  448. if (!s->enabled || s->external)
  449. return;
  450. uci_add_section(ctx, dest, "ipset", &ptr.s);
  451. ptr.o = NULL;
  452. ptr.option = "name";
  453. ptr.value = s->name;
  454. uci_set(ctx, &ptr);
  455. ptr.o = NULL;
  456. ptr.option = "storage";
  457. ptr.value = fw3_ipset_method_names[s->method];
  458. uci_set(ctx, &ptr);
  459. list_for_each_entry(type, &s->datatypes, list)
  460. {
  461. sprintf(buf, "%s_%s", type->dir, fw3_ipset_type_names[type->type]);
  462. ptr.o = NULL;
  463. ptr.option = "match";
  464. ptr.value = buf;
  465. uci_add_list(ctx, &ptr);
  466. }
  467. if (s->iprange.set)
  468. {
  469. ptr.o = NULL;
  470. ptr.option = "iprange";
  471. ptr.value = fw3_address_to_string(&s->iprange, false, false);
  472. uci_set(ctx, &ptr);
  473. }
  474. if (s->portrange.set)
  475. {
  476. sprintf(buf, "%u-%u", s->portrange.port_min, s->portrange.port_max);
  477. ptr.o = NULL;
  478. ptr.option = "portrange";
  479. ptr.value = buf;
  480. uci_set(ctx, &ptr);
  481. }
  482. }
  483. void
  484. fw3_write_statefile(void *state)
  485. {
  486. FILE *sf;
  487. struct fw3_state *s = state;
  488. struct fw3_zone *z;
  489. struct fw3_ipset *i;
  490. struct ifaddrs *ifaddr;
  491. struct uci_package *p;
  492. if (fw3_no_family(s->defaults.flags[0]) &&
  493. fw3_no_family(s->defaults.flags[1]))
  494. {
  495. unlink(FW3_STATEFILE);
  496. }
  497. else
  498. {
  499. sf = fopen(FW3_STATEFILE, "w+");
  500. if (!sf)
  501. {
  502. warn("Cannot create state %s: %s", FW3_STATEFILE, strerror(errno));
  503. return;
  504. }
  505. if (getifaddrs(&ifaddr))
  506. {
  507. warn("Cannot get interface addresses: %s", strerror(errno));
  508. ifaddr = NULL;
  509. }
  510. if ((p = uci_lookup_package(s->uci, "fw3_state")) != NULL)
  511. uci_unload(s->uci, p);
  512. uci_import(s->uci, sf, "fw3_state", NULL, true);
  513. if ((p = uci_lookup_package(s->uci, "fw3_state")) != NULL)
  514. {
  515. write_defaults_uci(s->uci, &s->defaults, p);
  516. list_for_each_entry(z, &s->zones, list)
  517. write_zone_uci(s->uci, z, p, ifaddr);
  518. list_for_each_entry(i, &s->ipsets, list)
  519. write_ipset_uci(s->uci, i, p);
  520. uci_export(s->uci, sf, p, true);
  521. uci_unload(s->uci, p);
  522. }
  523. fsync(fileno(sf));
  524. fclose(sf);
  525. if (ifaddr)
  526. freeifaddrs(ifaddr);
  527. }
  528. }
  529. void
  530. fw3_free_object(void *obj, const void *opts)
  531. {
  532. const struct fw3_option *ol;
  533. struct list_head *list, *cur, *tmp;
  534. for (ol = opts; ol->name; ol++)
  535. {
  536. if (!ol->elem_size)
  537. continue;
  538. list = (struct list_head *)((char *)obj + ol->offset);
  539. list_for_each_safe(cur, tmp, list)
  540. {
  541. list_del(cur);
  542. free(cur);
  543. }
  544. }
  545. free(obj);
  546. }
  547. void
  548. fw3_free_list(struct list_head *head)
  549. {
  550. struct list_head *entry, *tmp;
  551. if (!head)
  552. return;
  553. list_for_each_safe(entry, tmp, head)
  554. {
  555. list_del(entry);
  556. free(entry);
  557. }
  558. free(head);
  559. }
  560. bool
  561. fw3_hotplug(bool add, void *zone, void *device)
  562. {
  563. struct fw3_zone *z = zone;
  564. struct fw3_device *d = device;
  565. if (!*d->network)
  566. return false;
  567. switch (fork())
  568. {
  569. case -1:
  570. warn("Unable to fork(): %s\n", strerror(errno));
  571. return false;
  572. case 0:
  573. break;
  574. default:
  575. return true;
  576. }
  577. close(0);
  578. close(1);
  579. close(2);
  580. if (chdir("/")) {};
  581. clearenv();
  582. setenv("ACTION", add ? "add" : "remove", 1);
  583. setenv("ZONE", z->name, 1);
  584. setenv("INTERFACE", d->network, 1);
  585. setenv("DEVICE", d->name, 1);
  586. execl(FW3_HOTPLUG, FW3_HOTPLUG, "firewall", NULL);
  587. /* unreached */
  588. return false;
  589. }
  590. int
  591. fw3_netmask2bitlen(int family, void *mask)
  592. {
  593. int bits;
  594. struct in_addr *v4;
  595. struct in6_addr *v6;
  596. if (family == FW3_FAMILY_V6)
  597. for (bits = 0, v6 = mask;
  598. bits < 128 && (v6->s6_addr[bits / 8] << (bits % 8)) & 128;
  599. bits++);
  600. else
  601. for (bits = 0, v4 = mask;
  602. bits < 32 && (ntohl(v4->s_addr) << bits) & 0x80000000;
  603. bits++);
  604. return bits;
  605. }
  606. bool
  607. fw3_bitlen2netmask(int family, int bits, void *mask)
  608. {
  609. int i;
  610. uint8_t rem, b;
  611. struct in_addr *v4;
  612. struct in6_addr *v6;
  613. if (family == FW3_FAMILY_V6)
  614. {
  615. if (bits < -128 || bits > 128)
  616. return false;
  617. v6 = mask;
  618. rem = abs(bits);
  619. for (i = 0; i < sizeof(v6->s6_addr); i++)
  620. {
  621. b = (rem > 8) ? 8 : rem;
  622. v6->s6_addr[i] = (uint8_t)(0xFF << (8 - b));
  623. rem -= b;
  624. }
  625. if (bits < 0)
  626. for (i = 0; i < sizeof(v6->s6_addr); i++)
  627. v6->s6_addr[i] = ~v6->s6_addr[i];
  628. }
  629. else
  630. {
  631. if (bits < -32 || bits > 32)
  632. return false;
  633. v4 = mask;
  634. v4->s_addr = bits ? htonl(~((1 << (32 - abs(bits))) - 1)) : 0;
  635. if (bits < 0)
  636. v4->s_addr = ~v4->s_addr;
  637. }
  638. return true;
  639. }
  640. void
  641. fw3_flush_conntrack(void *state)
  642. {
  643. bool found;
  644. struct fw3_state *s = state;
  645. struct fw3_address *addr;
  646. struct fw3_device *dev;
  647. struct fw3_zone *zone;
  648. struct ifaddrs *ifaddr, *ifa;
  649. struct sockaddr_in *sin;
  650. struct sockaddr_in6 *sin6;
  651. char buf[INET6_ADDRSTRLEN];
  652. FILE *ct;
  653. if (!state)
  654. {
  655. if ((ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
  656. {
  657. info(" * Flushing conntrack table ...");
  658. fwrite("f\n", 1, 2, ct);
  659. fclose(ct);
  660. }
  661. return;
  662. }
  663. if (getifaddrs(&ifaddr))
  664. {
  665. warn("Cannot get interface addresses: %s", strerror(errno));
  666. return;
  667. }
  668. if ((ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
  669. {
  670. list_for_each_entry(zone, &s->zones, list)
  671. list_for_each_entry(addr, &zone->old_addrs, list)
  672. {
  673. found = false;
  674. list_for_each_entry(dev, &zone->devices, list)
  675. {
  676. for (ifa = ifaddr; ifa && !found; ifa = ifa->ifa_next)
  677. {
  678. if (!ifa->ifa_addr || strcmp(dev->name, ifa->ifa_name))
  679. continue;
  680. sin = (struct sockaddr_in *)ifa->ifa_addr;
  681. sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
  682. if (addr->family == FW3_FAMILY_V4 &&
  683. sin->sin_family == AF_INET)
  684. {
  685. found = !memcmp(&addr->address.v4, &sin->sin_addr,
  686. sizeof(sin->sin_addr));
  687. }
  688. else if (addr->family == FW3_FAMILY_V6 &&
  689. sin6->sin6_family == AF_INET6)
  690. {
  691. found = !memcmp(&addr->address.v6, &sin6->sin6_addr,
  692. sizeof(sin6->sin6_addr));
  693. }
  694. }
  695. if (found)
  696. break;
  697. }
  698. if (!found)
  699. {
  700. inet_ntop(addr->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
  701. &addr->address.v4, buf, sizeof(buf));
  702. info(" * Flushing conntrack: %s", buf);
  703. fprintf(ct, "%s\n", buf);
  704. }
  705. }
  706. fclose(ct);
  707. }
  708. freeifaddrs(ifaddr);
  709. }
  710. bool fw3_attr_parse_name_type(struct blob_attr *entry, const char **name, const char **type)
  711. {
  712. struct blob_attr *opt;
  713. unsigned orem;
  714. if (!type || !name)
  715. return false;
  716. *type = NULL;
  717. blobmsg_for_each_attr(opt, entry, orem)
  718. if (!strcmp(blobmsg_name(opt), "type"))
  719. *type = blobmsg_get_string(opt);
  720. else if (!strcmp(blobmsg_name(opt), "name"))
  721. *name = blobmsg_get_string(opt);
  722. return *type != NULL ? true : false;
  723. }
  724. const char *
  725. fw3_protoname(void *proto)
  726. {
  727. static char buf[sizeof("4294967295")];
  728. struct fw3_protocol *p = proto;
  729. struct protoent *pe;
  730. if (!p)
  731. return "?";
  732. pe = getprotobynumber(p->protocol);
  733. if (!pe)
  734. {
  735. snprintf(buf, sizeof(buf), "%u", p->protocol);
  736. return buf;
  737. }
  738. return pe->p_name;
  739. }
  740. bool
  741. fw3_check_loopback_dev(const char *name)
  742. {
  743. struct ifreq ifr;
  744. int s;
  745. bool rv = false;
  746. s = socket(AF_LOCAL, SOCK_DGRAM, 0);
  747. if (s < 0)
  748. return false;
  749. memset(&ifr, 0, sizeof(ifr));
  750. strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name) - 1);
  751. if (ioctl(s, SIOCGIFFLAGS, &ifr) >= 0) {
  752. if (ifr.ifr_flags & IFF_LOOPBACK)
  753. rv = true;
  754. }
  755. close(s);
  756. return rv;
  757. }
  758. bool
  759. fw3_check_loopback_addr(struct fw3_address *addr)
  760. {
  761. if (addr->family == FW3_FAMILY_V4 &&
  762. (ntohl(addr->address.v4.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
  763. return true;
  764. if (addr->family == FW3_FAMILY_V6 && !addr->range &&
  765. fw3_netmask2bitlen(FW3_FAMILY_V6, &addr->mask.v6) == 128 &&
  766. IN6_IS_ADDR_LOOPBACK(&addr->address.v6))
  767. return true;
  768. return false;
  769. }