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>
19 int ipt_ACCOUNT_init(struct ipt_ACCOUNT_context *ctx)
21 memset (ctx, 0, sizeof(struct ipt_ACCOUNT_context));
23 ctx->sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
24 if (ctx->sockfd < 0) {
26 ctx->error_str = "Can't open socket to kernel. "
27 "Permission denied or ipt_ACCOUNT module not loaded";
31 // 4096 bytes default buffer should save us from reallocations
32 // as it fits 200 concurrent active clients
33 if((ctx->data = (void *)malloc(IPT_ACCOUNT_MIN_BUFSIZE)) == NULL) {
36 ctx->error_str = "Out of memory for data buffer";
39 ctx->data_size = IPT_ACCOUNT_MIN_BUFSIZE;
44 void ipt_ACCOUNT_free_entries(struct ipt_ACCOUNT_context *ctx)
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));
49 ctx->handle.handle_nr = -1;
52 ctx->handle.itemcount = 0;
56 void ipt_ACCOUNT_deinit(struct ipt_ACCOUNT_context *ctx)
61 ipt_ACCOUNT_free_entries(ctx);
67 int ipt_ACCOUNT_read_entries(struct ipt_ACCOUNT_context *ctx,
68 const char *table, char dont_flush)
70 unsigned int s = sizeof (struct ipt_acc_handle_sockopt);
73 strncpy(ctx->handle.name, table, ACCOUNT_TABLE_NAME_LEN-1);
75 // Get table information
77 rtn = getsockopt(ctx->sockfd, IPPROTO_IP,
78 IPT_SO_GET_ACCOUNT_PREPARE_READ_FLUSH, &ctx->handle, &s);
80 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_PREPARE_READ,
84 ctx->error_str = "Can't get table information from kernel. "
85 "Is the table existing?";
89 // Check data buffer size
91 unsigned int new_size;
92 new_size = ctx->handle.itemcount * sizeof(struct ipt_acc_handle_ip);
93 // We want to prevent reallocations all the time
94 if (new_size < IPT_ACCOUNT_MIN_BUFSIZE)
95 new_size = IPT_ACCOUNT_MIN_BUFSIZE;
97 // Reallocate if it's too small or twice as big
98 if (ctx->data_size < new_size || ctx->data_size > new_size*2) {
103 if ((ctx->data = (void*)malloc(new_size)) == NULL) {
104 ctx->error_str = "Out of memory for data buffer";
105 ipt_ACCOUNT_free_entries(ctx);
109 ctx->data_size = new_size;
112 // Copy data from kernel
113 memcpy(ctx->data, &ctx->handle, sizeof(struct ipt_acc_handle_sockopt));
114 rtn = getsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_GET_ACCOUNT_GET_DATA,
115 ctx->data, &ctx->data_size);
117 ctx->error_str = "Can't get data from kernel. "
118 "Check /var/log/messages for details.";
119 ipt_ACCOUNT_free_entries(ctx);
123 // Free kernel handle but don't reset pos/itemcount
124 setsockopt(ctx->sockfd, IPPROTO_IP, IPT_SO_SET_ACCOUNT_HANDLE_FREE,
125 &ctx->handle, sizeof (struct ipt_acc_handle_sockopt));
126 ctx->handle.handle_nr = -1;
131 struct ipt_acc_handle_ip *ipt_ACCOUNT_get_next_entry(struct ipt_ACCOUNT_context *ctx)
133 struct ipt_acc_handle_ip *rtn;
135 // Empty or no more items left to return?
136 if (!ctx->handle.itemcount || ctx->pos >= ctx->handle.itemcount)
140 rtn = (struct ipt_acc_handle_ip *)(ctx->data + ctx->pos
141 * sizeof(struct ipt_acc_handle_ip));
147 int ipt_ACCOUNT_get_handle_usage(struct ipt_ACCOUNT_context *ctx)
149 unsigned int s = sizeof (struct ipt_acc_handle_sockopt);
150 if (getsockopt(ctx->sockfd, IPPROTO_IP,
151 IPT_SO_GET_ACCOUNT_GET_HANDLE_USAGE, &ctx->handle, &s) < 0) {
152 ctx->error_str = "Can't get handle usage information from kernel";
156 return ctx->handle.itemcount;
159 int ipt_ACCOUNT_free_all_handles(struct ipt_ACCOUNT_context *ctx)
161 if (setsockopt(ctx->sockfd, IPPROTO_IP,
162 IPT_SO_SET_ACCOUNT_HANDLE_FREE_ALL, NULL, 0) < 0) {
163 ctx->error_str = "Can't free all kernel handles";
170 int ipt_ACCOUNT_get_table_names(struct ipt_ACCOUNT_context *ctx)
172 int rtn = getsockopt(ctx->sockfd, IPPROTO_IP,
173 IPT_SO_GET_ACCOUNT_GET_TABLE_NAMES,
174 ctx->data, &ctx->data_size);
176 ctx->error_str = "Can't get table names from kernel. Out of memory, "
177 "MINBUFISZE too small?";
184 const char *ipt_ACCOUNT_get_next_name(struct ipt_ACCOUNT_context *ctx)
186 if (((char *)ctx->data)[ctx->pos] == 0)
189 const char *rtn = ctx->data + ctx->pos;
190 ctx->pos += strlen(ctx->data+ctx->pos) + 1;