Commit | Line | Data |
---|---|---|
a61040d6 TJ |
1 | /*************************************************************************** |
2 | * Copyright (C) 2004 by Intra2net AG * | |
3 | * opensource@intra2net.com * | |
4 | * * | |
5 | * This program is free software; you can redistribute it and/or modify * | |
6 | * it under the terms of the GNU Lesser General Public License * | |
7 | * version 2.1 as published by the Free Software Foundation; * | |
8 | * * | |
9 | ***************************************************************************/ | |
10 | ||
322f2b5b TJ |
11 | #include <sys/types.h> |
12 | #include <sys/socket.h> | |
322f2b5b | 13 | |
49ba8949 TJ |
14 | #include <netinet/in.h> |
15 | #include <linux/if.h> | |
16 | #include <linux/netfilter_ipv4/ip_tables.h> | |
322f2b5b TJ |
17 | #include <ipt_ACCOUNT_cl.h> |
18 | ||
19 | int ipt_ACCOUNT_init(struct ipt_ACCOUNT_context *ctx) | |
20 | { | |
21 | memset (ctx, 0, sizeof(struct ipt_ACCOUNT_context)); | |
22 | ||
23 | ctx->sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); | |
e3ccc02f | 24 | if (ctx->sockfd < 0) { |
322f2b5b | 25 | ctx->sockfd = -1; |
e3ccc02f TJ |
26 | ctx->error_str = "Can't open socket to kernel. " |
27 | "Permission denied or ipt_ACCOUNT module not loaded"; | |
322f2b5b TJ |
28 | return -1; |
29 | } | |
30 | ||
31 | // 4096 bytes default buffer should save us from reallocations | |
32 | // as it fits 200 concurrent active clients | |
e3ccc02f | 33 | if((ctx->data = (void *)malloc(IPT_ACCOUNT_MIN_BUFSIZE)) == NULL) { |
322f2b5b TJ |
34 | close (ctx->sockfd); |
35 | ctx->sockfd = -1; | |
36 | ctx->error_str = "Out of memory for data buffer"; | |
37 | return -1; | |
38 | } | |
39 | ctx->data_size = IPT_ACCOUNT_MIN_BUFSIZE; | |
40 | ||
41 | return 0; | |
42 | } | |
43 | ||
44 | void ipt_ACCOUNT_free_entries(struct ipt_ACCOUNT_context *ctx) | |
45 | { | |
e3ccc02f TJ |
46 | if (ctx->handle.handle_nr != -1) { |
47 | setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE, | |
48 | &ctx->handle, sizeof (struct ipt_acc_handle_sockopt)); | |
322f2b5b TJ |
49 | ctx->handle.handle_nr = -1; |
50 | } | |
51 | ||
52 | ctx->handle.itemcount = 0; | |
53 | ctx->pos = 0; | |
54 | } | |
55 | ||
56 | void ipt_ACCOUNT_deinit(struct ipt_ACCOUNT_context *ctx) | |
57 | { | |
58 | free(ctx->data); | |
59 | ctx->data = NULL; | |
60 | ||
61 | ipt_ACCOUNT_free_entries(ctx); | |
62 | ||
63 | close(ctx->sockfd); | |
64 | ctx->sockfd =-1; | |
65 | } | |
66 | ||
e3ccc02f TJ |
67 | int ipt_ACCOUNT_read_entries(struct ipt_ACCOUNT_context *ctx, |
68 | const char *table, char dont_flush) | |
322f2b5b | 69 | { |
8721821e | 70 | unsigned int s = sizeof (struct ipt_acc_handle_sockopt); |
322f2b5b TJ |
71 | int rtn; |
72 | ||
73 | strncpy(ctx->handle.name, table, ACCOUNT_TABLE_NAME_LEN-1); | |
74 | ||
75 | // Get table information | |
76 | if (!dont_flush) | |
e3ccc02f TJ |
77 | rtn = getsockopt(ctx->sockfd, IPPROTO_IP, |
78 | IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH, &ctx->handle, &s); | |
322f2b5b | 79 | else |
e3ccc02f TJ |
80 | rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ, |
81 | &ctx->handle, &s); | |
322f2b5b | 82 | |
e3ccc02f TJ |
83 | if (rtn < 0) { |
84 | ctx->error_str = "Can't get table information from kernel. " | |
85 | "Is the table existing?"; | |
322f2b5b TJ |
86 | return -1; |
87 | } | |
88 | ||
89 | // Check data buffer size | |
90 | ctx->pos = 0; | |
e3ccc02f TJ |
91 | unsigned int new_size; |
92 | new_size = ctx->handle.itemcount * sizeof(struct ipt_acc_handle_ip); | |
322f2b5b TJ |
93 | // We want to prevent reallocations all the time |
94 | if (new_size < IPT_ACCOUNT_MIN_BUFSIZE) | |
95 | new_size = IPT_ACCOUNT_MIN_BUFSIZE; | |
96 | ||
97 | // Reallocate if it's too small or twice as big | |
e3ccc02f | 98 | if (ctx->data_size < new_size || ctx->data_size > new_size*2) { |
322f2b5b TJ |
99 | // Free old buffer |
100 | free (ctx->data); | |
101 | ctx->data_size = 0; | |
102 | ||
e3ccc02f | 103 | if ((ctx->data = (void*)malloc(new_size)) == NULL) { |
322f2b5b TJ |
104 | ctx->error_str = "Out of memory for data buffer"; |
105 | ipt_ACCOUNT_free_entries(ctx); | |
106 | return -1; | |
107 | } | |
108 | ||
109 | ctx->data_size = new_size; | |
110 | } | |
111 | ||
112 | // Copy data from kernel | |
8721821e | 113 | memcpy(ctx->data, &ctx->handle, sizeof(struct ipt_acc_handle_sockopt)); |
e3ccc02f TJ |
114 | rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_GET_DATA, |
115 | ctx->data, &ctx->data_size); | |
116 | if (rtn < 0) { | |
117 | ctx->error_str = "Can't get data from kernel. " | |
118 | "Check /var/log/messages for details."; | |
322f2b5b TJ |
119 | ipt_ACCOUNT_free_entries(ctx); |
120 | return -1; | |
121 | } | |
122 | ||
123 | // Free kernel handle but don't reset pos/itemcount | |
e3ccc02f TJ |
124 | setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE, |
125 | &ctx->handle, sizeof (struct ipt_acc_handle_sockopt)); | |
322f2b5b TJ |
126 | ctx->handle.handle_nr = -1; |
127 | ||
128 | return 0; | |
129 | } | |
130 | ||
8721821e | 131 | struct ipt_acc_handle_ip *ipt_ACCOUNT_get_next_entry(struct ipt_ACCOUNT_context *ctx) |
322f2b5b | 132 | { |
8721821e | 133 | struct ipt_acc_handle_ip *rtn; |
322f2b5b TJ |
134 | |
135 | // Empty or no more items left to return? | |
136 | if (!ctx->handle.itemcount || ctx->pos >= ctx->handle.itemcount) | |
137 | return NULL; | |
138 | ||
139 | // Get next entry | |
e3ccc02f TJ |
140 | rtn = (struct ipt_acc_handle_ip *)(ctx->data + ctx->pos |
141 | * sizeof(struct ipt_acc_handle_ip)); | |
322f2b5b TJ |
142 | ctx->pos++; |
143 | ||
144 | return rtn; | |
145 | } | |
d7e0bb9c TJ |
146 | |
147 | int ipt_ACCOUNT_get_handle_usage(struct ipt_ACCOUNT_context *ctx) | |
148 | { | |
8721821e | 149 | unsigned int s = sizeof (struct ipt_acc_handle_sockopt); |
e3ccc02f TJ |
150 | if (getsockopt(ctx->sockfd, IPPROTO_IP, |
151 | IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE, &ctx->handle, &s) < 0) { | |
d7e0bb9c TJ |
152 | ctx->error_str = "Can't get handle usage information from kernel"; |
153 | return -1; | |
154 | } | |
155 | ||
156 | return ctx->handle.itemcount; | |
157 | } | |
158 | ||
159 | int ipt_ACCOUNT_free_all_handles(struct ipt_ACCOUNT_context *ctx) | |
160 | { | |
e3ccc02f TJ |
161 | if (setsockopt(ctx->sockfd, IPPROTO_IP, |
162 | IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL, NULL, 0) < 0) { | |
d7e0bb9c TJ |
163 | ctx->error_str = "Can't free all kernel handles"; |
164 | return -1; | |
165 | } | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
170 | int ipt_ACCOUNT_get_table_names(struct ipt_ACCOUNT_context *ctx) | |
171 | { | |
e3ccc02f TJ |
172 | int rtn = getsockopt(ctx->sockfd, IPPROTO_IP, |
173 | IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES, | |
174 | ctx->data, &ctx->data_size); | |
175 | if (rtn < 0) { | |
176 | ctx->error_str = "Can't get table names from kernel. Out of memory, " | |
177 | "MINBUFISZE too small?"; | |
d7e0bb9c TJ |
178 | return -1; |
179 | } | |
180 | ctx->pos = 0; | |
181 | return 0; | |
182 | } | |
183 | ||
42b9e9f8 | 184 | const char *ipt_ACCOUNT_get_next_name(struct ipt_ACCOUNT_context *ctx) |
d7e0bb9c | 185 | { |
8721821e TJ |
186 | if (((char *)ctx->data)[ctx->pos] == 0) |
187 | return 0; | |
d7e0bb9c | 188 | |
8721821e | 189 | const char *rtn = ctx->data + ctx->pos; |
d7e0bb9c TJ |
190 | ctx->pos += strlen(ctx->data+ctx->pos) + 1; |
191 | ||
192 | return rtn; | |
193 | } |