1 /***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
3 * opensource@intra2net.com *
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; *
9 ***************************************************************************/
12 #include <sys/types.h>
13 #include <sys/socket.h>
15 #include <netinet/in.h>
17 #include <linux/netfilter_ipv4/ip_tables.h>
18 #include <ipt_ACCOUNT_cl.h>
23 int ipt_ACCOUNT_init(struct ipt_ACCOUNT_context *ctx)
25 memset (ctx, 0, sizeof(struct ipt_ACCOUNT_context));
26 ctx->handle.handle_nr = -1;
28 ctx->sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
29 if (ctx->sockfd < 0) {
31 ctx->error_str = "Can't open socket to kernel. "
32 "Permission denied or ipt_ACCOUNT module not loaded";
36 // 4096 bytes default buffer should save us from reallocations
37 // as it fits 200 concurrent active clients
38 if((ctx->data = (void *)malloc(IPT_ACCOUNT_MIN_BUFSIZE)) == NULL) {
41 ctx->error_str = "Out of memory for data buffer";
44 ctx->data_size = IPT_ACCOUNT_MIN_BUFSIZE;
49 void ipt_ACCOUNT_free_entries(struct ipt_ACCOUNT_context *ctx)
51 if (ctx->handle.handle_nr != -1) {
52 setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE,
53 &ctx->handle, sizeof (struct ipt_acc_handle_sockopt));
54 ctx->handle.handle_nr = -1;
57 ctx->handle.itemcount = 0;
61 void ipt_ACCOUNT_deinit(struct ipt_ACCOUNT_context *ctx)
66 ipt_ACCOUNT_free_entries(ctx);
72 int ipt_ACCOUNT_read_entries(struct ipt_ACCOUNT_context *ctx,
73 const char *table, char dont_flush)
75 unsigned int s = sizeof (struct ipt_acc_handle_sockopt);
76 unsigned int new_size;
79 strncpy(ctx->handle.name, table, ACCOUNT_TABLE_NAME_LEN-1);
81 // Get table information
83 rtn = getsockopt(ctx->sockfd, IPPROTO_IP,
84 IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH, &ctx->handle, &s);
86 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ,
90 ctx->error_str = "Can't get table information from kernel. "
91 "Is the table existing?";
95 // Check data buffer size
97 new_size = ctx->handle.itemcount * sizeof(struct ipt_acc_handle_ip);
98 // We want to prevent reallocations all the time
99 if (new_size < IPT_ACCOUNT_MIN_BUFSIZE)
100 new_size = IPT_ACCOUNT_MIN_BUFSIZE;
102 // Reallocate if it's too small or twice as big
103 if (ctx->data_size < new_size || ctx->data_size > new_size*2) {
108 if ((ctx->data = (void*)malloc(new_size)) == NULL) {
109 ctx->error_str = "Out of memory for data buffer";
110 ipt_ACCOUNT_free_entries(ctx);
114 ctx->data_size = new_size;
117 // Copy data from kernel
118 memcpy(ctx->data, &ctx->handle, sizeof(struct ipt_acc_handle_sockopt));
119 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_GET_DATA,
120 ctx->data, &ctx->data_size);
122 ctx->error_str = "Can't get data from kernel. "
123 "Check /var/log/messages for details.";
124 ipt_ACCOUNT_free_entries(ctx);
128 // Free kernel handle but don't reset pos/itemcount
129 setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE,
130 &ctx->handle, sizeof (struct ipt_acc_handle_sockopt));
131 ctx->handle.handle_nr = -1;
136 struct ipt_acc_handle_ip *ipt_ACCOUNT_get_next_entry(struct ipt_ACCOUNT_context *ctx)
138 struct ipt_acc_handle_ip *rtn;
140 // Empty or no more items left to return?
141 if (!ctx->handle.itemcount || ctx->pos >= ctx->handle.itemcount)
145 rtn = (struct ipt_acc_handle_ip *)(ctx->data + ctx->pos
146 * sizeof(struct ipt_acc_handle_ip));
152 int ipt_ACCOUNT_get_handle_usage(struct ipt_ACCOUNT_context *ctx)
154 unsigned int s = sizeof (struct ipt_acc_handle_sockopt);
155 if (getsockopt(ctx->sockfd, IPPROTO_IP,
156 IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE, &ctx->handle, &s) < 0) {
157 ctx->error_str = "Can't get handle usage information from kernel";
160 ctx->handle.handle_nr = -1;
162 return ctx->handle.itemcount;
165 int ipt_ACCOUNT_free_all_handles(struct ipt_ACCOUNT_context *ctx)
167 if (setsockopt(ctx->sockfd, IPPROTO_IP,
168 IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL, NULL, 0) < 0) {
169 ctx->error_str = "Can't free all kernel handles";
176 int ipt_ACCOUNT_get_table_names(struct ipt_ACCOUNT_context *ctx)
178 int rtn = getsockopt(ctx->sockfd, IPPROTO_IP,
179 IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES,
180 ctx->data, &ctx->data_size);
182 ctx->error_str = "Can't get table names from kernel. Out of memory, "
183 "MINBUFISZE too small?";
190 const char *ipt_ACCOUNT_get_next_name(struct ipt_ACCOUNT_context *ctx)
193 if (((char *)ctx->data)[ctx->pos] == 0)
196 rtn = ctx->data + ctx->pos;
197 ctx->pos += strlen(ctx->data+ctx->pos) + 1;