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 ***************************************************************************/
11 #include <sys/types.h>
12 #include <sys/socket.h>
14 #include <netinet/in.h>
16 #include <linux/netfilter_ipv4/ip_tables.h>
17 #include <ipt_ACCOUNT_cl.h>
22 int ipt_ACCOUNT_init(struct ipt_ACCOUNT_context *ctx)
24 memset (ctx, 0, sizeof(struct ipt_ACCOUNT_context));
25 ctx->handle.handle_nr = -1;
27 ctx->sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
28 if (ctx->sockfd < 0) {
30 ctx->error_str = "Can't open socket to kernel. "
31 "Permission denied or ipt_ACCOUNT module not loaded";
35 // 4096 bytes default buffer should save us from reallocations
36 // as it fits 200 concurrent active clients
37 if((ctx->data = (void *)malloc(IPT_ACCOUNT_MIN_BUFSIZE)) == NULL) {
40 ctx->error_str = "Out of memory for data buffer";
43 ctx->data_size = IPT_ACCOUNT_MIN_BUFSIZE;
48 void ipt_ACCOUNT_free_entries(struct ipt_ACCOUNT_context *ctx)
50 if (ctx->handle.handle_nr != -1) {
51 setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE,
52 &ctx->handle, sizeof (struct ipt_acc_handle_sockopt));
53 ctx->handle.handle_nr = -1;
56 ctx->handle.itemcount = 0;
60 void ipt_ACCOUNT_deinit(struct ipt_ACCOUNT_context *ctx)
65 ipt_ACCOUNT_free_entries(ctx);
71 int ipt_ACCOUNT_read_entries(struct ipt_ACCOUNT_context *ctx,
72 const char *table, char dont_flush)
74 unsigned int s = sizeof (struct ipt_acc_handle_sockopt);
75 unsigned int new_size;
78 strncpy(ctx->handle.name, table, ACCOUNT_TABLE_NAME_LEN-1);
80 // Get table information
82 rtn = getsockopt(ctx->sockfd, IPPROTO_IP,
83 IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH, &ctx->handle, &s);
85 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ,
89 ctx->error_str = "Can't get table information from kernel. "
90 "Is the table existing?";
94 // Check data buffer size
96 new_size = ctx->handle.itemcount * sizeof(struct ipt_acc_handle_ip);
97 // We want to prevent reallocations all the time
98 if (new_size < IPT_ACCOUNT_MIN_BUFSIZE)
99 new_size = IPT_ACCOUNT_MIN_BUFSIZE;
101 // Reallocate if it's too small or twice as big
102 if (ctx->data_size < new_size || ctx->data_size > new_size*2) {
107 if ((ctx->data = (void*)malloc(new_size)) == NULL) {
108 ctx->error_str = "Out of memory for data buffer";
109 ipt_ACCOUNT_free_entries(ctx);
113 ctx->data_size = new_size;
116 // Copy data from kernel
117 memcpy(ctx->data, &ctx->handle, sizeof(struct ipt_acc_handle_sockopt));
118 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_GET_DATA,
119 ctx->data, &ctx->data_size);
121 ctx->error_str = "Can't get data from kernel. "
122 "Check /var/log/messages for details.";
123 ipt_ACCOUNT_free_entries(ctx);
127 // Free kernel handle but don't reset pos/itemcount
128 setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE,
129 &ctx->handle, sizeof (struct ipt_acc_handle_sockopt));
130 ctx->handle.handle_nr = -1;
135 struct ipt_acc_handle_ip *ipt_ACCOUNT_get_next_entry(struct ipt_ACCOUNT_context *ctx)
137 struct ipt_acc_handle_ip *rtn;
139 // Empty or no more items left to return?
140 if (!ctx->handle.itemcount || ctx->pos >= ctx->handle.itemcount)
144 rtn = (struct ipt_acc_handle_ip *)(ctx->data + ctx->pos
145 * sizeof(struct ipt_acc_handle_ip));
151 int ipt_ACCOUNT_get_handle_usage(struct ipt_ACCOUNT_context *ctx)
153 unsigned int s = sizeof (struct ipt_acc_handle_sockopt);
154 if (getsockopt(ctx->sockfd, IPPROTO_IP,
155 IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE, &ctx->handle, &s) < 0) {
156 ctx->error_str = "Can't get handle usage information from kernel";
159 ctx->handle.handle_nr = -1;
161 return ctx->handle.itemcount;
164 int ipt_ACCOUNT_free_all_handles(struct ipt_ACCOUNT_context *ctx)
166 if (setsockopt(ctx->sockfd, IPPROTO_IP,
167 IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL, NULL, 0) < 0) {
168 ctx->error_str = "Can't free all kernel handles";
175 int ipt_ACCOUNT_get_table_names(struct ipt_ACCOUNT_context *ctx)
177 int rtn = getsockopt(ctx->sockfd, IPPROTO_IP,
178 IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES,
179 ctx->data, &ctx->data_size);
181 ctx->error_str = "Can't get table names from kernel. Out of memory, "
182 "MINBUFISZE too small?";
189 const char *ipt_ACCOUNT_get_next_name(struct ipt_ACCOUNT_context *ctx)
192 if (((char *)ctx->data)[ctx->pos] == 0)
195 rtn = ctx->data + ctx->pos;
196 ctx->pos += strlen(ctx->data+ctx->pos) + 1;