libip6tc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /* Library which manipulates firewall rules. Version 0.1. */
  2. /* Architecture of firewall rules is as follows:
  3. *
  4. * Chains go INPUT, FORWARD, OUTPUT then user chains.
  5. * Each user chain starts with an ERROR node.
  6. * Every chain ends with an unconditional jump: a RETURN for user chains,
  7. * and a POLICY for built-ins.
  8. */
  9. /* (C)1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
  10. COPYING for details). */
  11. #include <assert.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <arpa/inet.h>
  18. #ifdef DEBUG_CONNTRACK
  19. #define inline
  20. #endif
  21. #if !defined(__GLIBC__) || (__GLIBC__ < 2)
  22. typedef unsigned int socklen_t;
  23. #endif
  24. #include "libiptc/libip6tc.h"
  25. #define HOOK_PRE_ROUTING NF_IP6_PRE_ROUTING
  26. #define HOOK_LOCAL_IN NF_IP6_LOCAL_IN
  27. #define HOOK_FORWARD NF_IP6_FORWARD
  28. #define HOOK_LOCAL_OUT NF_IP6_LOCAL_OUT
  29. #define HOOK_POST_ROUTING NF_IP6_POST_ROUTING
  30. #define STRUCT_ENTRY_TARGET struct xt_entry_target
  31. #define STRUCT_ENTRY struct ip6t_entry
  32. #define STRUCT_ENTRY_MATCH struct xt_entry_match
  33. #define STRUCT_GETINFO struct ip6t_getinfo
  34. #define STRUCT_GET_ENTRIES struct ip6t_get_entries
  35. #define STRUCT_COUNTERS struct xt_counters
  36. #define STRUCT_COUNTERS_INFO struct xt_counters_info
  37. #define STRUCT_STANDARD_TARGET struct xt_standard_target
  38. #define STRUCT_REPLACE struct ip6t_replace
  39. #define ENTRY_ITERATE IP6T_ENTRY_ITERATE
  40. #define TABLE_MAXNAMELEN XT_TABLE_MAXNAMELEN
  41. #define FUNCTION_MAXNAMELEN XT_FUNCTION_MAXNAMELEN
  42. #define GET_TARGET ip6t_get_target
  43. #define ERROR_TARGET XT_ERROR_TARGET
  44. #define NUMHOOKS NF_IP6_NUMHOOKS
  45. #define IPT_CHAINLABEL xt_chainlabel
  46. #define TC_DUMP_ENTRIES dump_entries6
  47. #define TC_IS_CHAIN ip6tc_is_chain
  48. #define TC_FIRST_CHAIN ip6tc_first_chain
  49. #define TC_NEXT_CHAIN ip6tc_next_chain
  50. #define TC_FIRST_RULE ip6tc_first_rule
  51. #define TC_NEXT_RULE ip6tc_next_rule
  52. #define TC_GET_TARGET ip6tc_get_target
  53. #define TC_BUILTIN ip6tc_builtin
  54. #define TC_GET_POLICY ip6tc_get_policy
  55. #define TC_INSERT_ENTRY ip6tc_insert_entry
  56. #define TC_REPLACE_ENTRY ip6tc_replace_entry
  57. #define TC_APPEND_ENTRY ip6tc_append_entry
  58. #define TC_CHECK_ENTRY ip6tc_check_entry
  59. #define TC_DELETE_ENTRY ip6tc_delete_entry
  60. #define TC_DELETE_NUM_ENTRY ip6tc_delete_num_entry
  61. #define TC_FLUSH_ENTRIES ip6tc_flush_entries
  62. #define TC_ZERO_ENTRIES ip6tc_zero_entries
  63. #define TC_ZERO_COUNTER ip6tc_zero_counter
  64. #define TC_READ_COUNTER ip6tc_read_counter
  65. #define TC_SET_COUNTER ip6tc_set_counter
  66. #define TC_CREATE_CHAIN ip6tc_create_chain
  67. #define TC_GET_REFERENCES ip6tc_get_references
  68. #define TC_DELETE_CHAIN ip6tc_delete_chain
  69. #define TC_RENAME_CHAIN ip6tc_rename_chain
  70. #define TC_SET_POLICY ip6tc_set_policy
  71. #define TC_GET_RAW_SOCKET ip6tc_get_raw_socket
  72. #define TC_INIT ip6tc_init
  73. #define TC_FREE ip6tc_free
  74. #define TC_COMMIT ip6tc_commit
  75. #define TC_STRERROR ip6tc_strerror
  76. #define TC_NUM_RULES ip6tc_num_rules
  77. #define TC_GET_RULE ip6tc_get_rule
  78. #define TC_OPS ip6tc_ops
  79. #define TC_AF AF_INET6
  80. #define TC_IPPROTO IPPROTO_IPV6
  81. #define SO_SET_REPLACE IP6T_SO_SET_REPLACE
  82. #define SO_SET_ADD_COUNTERS IP6T_SO_SET_ADD_COUNTERS
  83. #define SO_GET_INFO IP6T_SO_GET_INFO
  84. #define SO_GET_ENTRIES IP6T_SO_GET_ENTRIES
  85. #define SO_GET_VERSION IP6T_SO_GET_VERSION
  86. #define STANDARD_TARGET XT_STANDARD_TARGET
  87. #define LABEL_RETURN IP6TC_LABEL_RETURN
  88. #define LABEL_ACCEPT IP6TC_LABEL_ACCEPT
  89. #define LABEL_DROP IP6TC_LABEL_DROP
  90. #define LABEL_QUEUE IP6TC_LABEL_QUEUE
  91. #define ALIGN XT_ALIGN
  92. #define RETURN XT_RETURN
  93. #include "libiptc.c"
  94. #define BIT6(a, l) \
  95. ((ntohl(a->s6_addr32[(l) / 32]) >> (31 - ((l) & 31))) & 1)
  96. int
  97. ipv6_prefix_length(const struct in6_addr *a)
  98. {
  99. int l, i;
  100. for (l = 0; l < 128; l++) {
  101. if (BIT6(a, l) == 0)
  102. break;
  103. }
  104. for (i = l + 1; i < 128; i++) {
  105. if (BIT6(a, i) == 1)
  106. return -1;
  107. }
  108. return l;
  109. }
  110. static int
  111. dump_entry(struct ip6t_entry *e, struct xtc_handle *const handle)
  112. {
  113. size_t i;
  114. char buf[40];
  115. int len;
  116. struct xt_entry_target *t;
  117. printf("Entry %u (%lu):\n", iptcb_entry2index(handle, e),
  118. iptcb_entry2offset(handle, e));
  119. puts("SRC IP: ");
  120. inet_ntop(AF_INET6, &e->ipv6.src, buf, sizeof buf);
  121. puts(buf);
  122. putchar('/');
  123. len = ipv6_prefix_length(&e->ipv6.smsk);
  124. if (len != -1)
  125. printf("%d", len);
  126. else {
  127. inet_ntop(AF_INET6, &e->ipv6.smsk, buf, sizeof buf);
  128. puts(buf);
  129. }
  130. putchar('\n');
  131. puts("DST IP: ");
  132. inet_ntop(AF_INET6, &e->ipv6.dst, buf, sizeof buf);
  133. puts(buf);
  134. putchar('/');
  135. len = ipv6_prefix_length(&e->ipv6.dmsk);
  136. if (len != -1)
  137. printf("%d", len);
  138. else {
  139. inet_ntop(AF_INET6, &e->ipv6.dmsk, buf, sizeof buf);
  140. puts(buf);
  141. }
  142. putchar('\n');
  143. printf("Interface: `%s'/", e->ipv6.iniface);
  144. for (i = 0; i < IFNAMSIZ; i++)
  145. printf("%c", e->ipv6.iniface_mask[i] ? 'X' : '.');
  146. printf("to `%s'/", e->ipv6.outiface);
  147. for (i = 0; i < IFNAMSIZ; i++)
  148. printf("%c", e->ipv6.outiface_mask[i] ? 'X' : '.');
  149. printf("\nProtocol: %u\n", e->ipv6.proto);
  150. if (e->ipv6.flags & IP6T_F_TOS)
  151. printf("TOS: %u\n", e->ipv6.tos);
  152. printf("Flags: %02X\n", e->ipv6.flags);
  153. printf("Invflags: %02X\n", e->ipv6.invflags);
  154. printf("Counters: %llu packets, %llu bytes\n",
  155. (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
  156. printf("Cache: %08X\n", e->nfcache);
  157. IP6T_MATCH_ITERATE(e, print_match);
  158. t = ip6t_get_target(e);
  159. printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
  160. if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
  161. const unsigned char *data = t->data;
  162. int pos = *(const int *)data;
  163. if (pos < 0)
  164. printf("verdict=%s\n",
  165. pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
  166. : pos == -NF_DROP-1 ? "NF_DROP"
  167. : pos == XT_RETURN ? "RETURN"
  168. : "UNKNOWN");
  169. else
  170. printf("verdict=%u\n", pos);
  171. } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0)
  172. printf("error=`%s'\n", t->data);
  173. printf("\n");
  174. return 0;
  175. }
  176. static unsigned char *
  177. is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b,
  178. unsigned char *matchmask)
  179. {
  180. unsigned int i;
  181. unsigned char *mptr;
  182. /* Always compare head structures: ignore mask here. */
  183. if (memcmp(&a->ipv6.src, &b->ipv6.src, sizeof(struct in6_addr))
  184. || memcmp(&a->ipv6.dst, &b->ipv6.dst, sizeof(struct in6_addr))
  185. || memcmp(&a->ipv6.smsk, &b->ipv6.smsk, sizeof(struct in6_addr))
  186. || memcmp(&a->ipv6.dmsk, &b->ipv6.dmsk, sizeof(struct in6_addr))
  187. || a->ipv6.proto != b->ipv6.proto
  188. || a->ipv6.tos != b->ipv6.tos
  189. || a->ipv6.flags != b->ipv6.flags
  190. || a->ipv6.invflags != b->ipv6.invflags)
  191. return NULL;
  192. for (i = 0; i < IFNAMSIZ; i++) {
  193. if (a->ipv6.iniface_mask[i] != b->ipv6.iniface_mask[i])
  194. return NULL;
  195. if ((a->ipv6.iniface[i] & a->ipv6.iniface_mask[i])
  196. != (b->ipv6.iniface[i] & b->ipv6.iniface_mask[i]))
  197. return NULL;
  198. if (a->ipv6.outiface_mask[i] != b->ipv6.outiface_mask[i])
  199. return NULL;
  200. if ((a->ipv6.outiface[i] & a->ipv6.outiface_mask[i])
  201. != (b->ipv6.outiface[i] & b->ipv6.outiface_mask[i]))
  202. return NULL;
  203. }
  204. if (a->target_offset != b->target_offset
  205. || a->next_offset != b->next_offset)
  206. return NULL;
  207. mptr = matchmask + sizeof(STRUCT_ENTRY);
  208. if (IP6T_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
  209. return NULL;
  210. mptr += XT_ALIGN(sizeof(struct xt_entry_target));
  211. return mptr;
  212. }
  213. /* All zeroes == unconditional rule. */
  214. static inline int
  215. unconditional(const struct ip6t_ip6 *ipv6)
  216. {
  217. unsigned int i;
  218. for (i = 0; i < sizeof(*ipv6); i++)
  219. if (((char *)ipv6)[i])
  220. break;
  221. return (i == sizeof(*ipv6));
  222. }
  223. #ifdef IPTC_DEBUG
  224. /* Do every conceivable sanity check on the handle */
  225. static void
  226. do_check(struct xtc_handle *h, unsigned int line)
  227. {
  228. unsigned int i, n;
  229. unsigned int user_offset; /* Offset of first user chain */
  230. int was_return;
  231. assert(h->changed == 0 || h->changed == 1);
  232. if (strcmp(h->info.name, "filter") == 0) {
  233. assert(h->info.valid_hooks
  234. == (1 << NF_IP6_LOCAL_IN
  235. | 1 << NF_IP6_FORWARD
  236. | 1 << NF_IP6_LOCAL_OUT));
  237. /* Hooks should be first three */
  238. assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == 0);
  239. n = get_chain_end(h, 0);
  240. n += get_entry(h, n)->next_offset;
  241. assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
  242. n = get_chain_end(h, n);
  243. n += get_entry(h, n)->next_offset;
  244. assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
  245. user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
  246. } else if (strcmp(h->info.name, "nat") == 0) {
  247. assert((h->info.valid_hooks
  248. == (1 << NF_IP6_PRE_ROUTING
  249. | 1 << NF_IP6_LOCAL_OUT
  250. | 1 << NF_IP6_POST_ROUTING)) ||
  251. (h->info.valid_hooks
  252. == (1 << NF_IP6_PRE_ROUTING
  253. | 1 << NF_IP6_LOCAL_IN
  254. | 1 << NF_IP6_LOCAL_OUT
  255. | 1 << NF_IP6_POST_ROUTING)));
  256. assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
  257. n = get_chain_end(h, 0);
  258. n += get_entry(h, n)->next_offset;
  259. assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
  260. n = get_chain_end(h, n);
  261. n += get_entry(h, n)->next_offset;
  262. assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
  263. user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
  264. if (h->info.valid_hooks & (1 << NF_IP6_LOCAL_IN)) {
  265. n = get_chain_end(h, n);
  266. n += get_entry(h, n)->next_offset;
  267. assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n);
  268. user_offset = h->info.hook_entry[NF_IP6_LOCAL_IN];
  269. }
  270. } else if (strcmp(h->info.name, "mangle") == 0) {
  271. /* This code is getting ugly because linux < 2.4.18-pre6 had
  272. * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks
  273. * */
  274. assert((h->info.valid_hooks
  275. == (1 << NF_IP6_PRE_ROUTING
  276. | 1 << NF_IP6_LOCAL_OUT)) ||
  277. (h->info.valid_hooks
  278. == (1 << NF_IP6_PRE_ROUTING
  279. | 1 << NF_IP6_LOCAL_IN
  280. | 1 << NF_IP6_FORWARD
  281. | 1 << NF_IP6_LOCAL_OUT
  282. | 1 << NF_IP6_POST_ROUTING)));
  283. /* Hooks should be first five */
  284. assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
  285. n = get_chain_end(h, 0);
  286. if (h->info.valid_hooks & (1 << NF_IP6_LOCAL_IN)) {
  287. n += get_entry(h, n)->next_offset;
  288. assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n);
  289. n = get_chain_end(h, n);
  290. }
  291. if (h->info.valid_hooks & (1 << NF_IP6_FORWARD)) {
  292. n += get_entry(h, n)->next_offset;
  293. assert(h->info.hook_entry[NF_IP6_FORWARD] == n);
  294. n = get_chain_end(h, n);
  295. }
  296. n += get_entry(h, n)->next_offset;
  297. assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
  298. user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
  299. if (h->info.valid_hooks & (1 << NF_IP6_POST_ROUTING)) {
  300. n = get_chain_end(h, n);
  301. n += get_entry(h, n)->next_offset;
  302. assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n);
  303. user_offset = h->info.hook_entry[NF_IP6_POST_ROUTING];
  304. }
  305. } else if (strcmp(h->info.name, "raw") == 0) {
  306. assert(h->info.valid_hooks
  307. == (1 << NF_IP6_PRE_ROUTING
  308. | 1 << NF_IP6_LOCAL_OUT));
  309. /* Hooks should be first three */
  310. assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0);
  311. n = get_chain_end(h, n);
  312. n += get_entry(h, n)->next_offset;
  313. assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n);
  314. user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT];
  315. } else {
  316. fprintf(stderr, "Unknown table `%s'\n", h->info.name);
  317. abort();
  318. }
  319. /* User chain == end of last builtin + policy entry */
  320. user_offset = get_chain_end(h, user_offset);
  321. user_offset += get_entry(h, user_offset)->next_offset;
  322. /* Overflows should be end of entry chains, and unconditional
  323. policy nodes. */
  324. for (i = 0; i < NUMHOOKS; i++) {
  325. STRUCT_ENTRY *e;
  326. STRUCT_STANDARD_TARGET *t;
  327. if (!(h->info.valid_hooks & (1 << i)))
  328. continue;
  329. assert(h->info.underflow[i]
  330. == get_chain_end(h, h->info.hook_entry[i]));
  331. e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
  332. assert(unconditional(&e->ipv6));
  333. assert(e->target_offset == sizeof(*e));
  334. t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
  335. printf("target_size=%u, align=%u\n",
  336. t->target.u.target_size, ALIGN(sizeof(*t)));
  337. assert(t->target.u.target_size == ALIGN(sizeof(*t)));
  338. assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
  339. assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
  340. assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
  341. /* Hooks and underflows must be valid entries */
  342. iptcb_entry2index(h, get_entry(h, h->info.hook_entry[i]));
  343. iptcb_entry2index(h, get_entry(h, h->info.underflow[i]));
  344. }
  345. assert(h->info.size
  346. >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
  347. +sizeof(STRUCT_STANDARD_TARGET)));
  348. assert(h->entries.size
  349. >= (h->new_number
  350. * (sizeof(STRUCT_ENTRY)
  351. + sizeof(STRUCT_STANDARD_TARGET))));
  352. assert(strcmp(h->info.name, h->entries.name) == 0);
  353. i = 0; n = 0;
  354. was_return = 0;
  355. #if 0
  356. /* Check all the entries. */
  357. ENTRY_ITERATE(h->entries.entrytable, h->entries.size,
  358. check_entry, &i, &n, user_offset, &was_return, h);
  359. assert(i == h->new_number);
  360. assert(n == h->entries.size);
  361. /* Final entry must be error node */
  362. assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
  363. ->u.user.name,
  364. ERROR_TARGET) == 0);
  365. #endif
  366. }
  367. #endif /*IPTC_DEBUG*/