PageRenderTime 63ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/xp/lnx/libip6tc.c

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