/*
* New IP firewall options for [gs]esockopt at the RAW IP level.
* Unlike BSD Linux inherits IP options so you don't have to use a raw socket
* socket for this. Instead we check tights in the calls.
*
* ATTENTION: check linux/in.h before adding new number here.
*/
#define IPT_BASE_CTL 64
#define IPT_SO_SET_REPLACE (IPT_BASE_CTL)
#define IPT_SO_SET_ADD_COUNTERS (IPT_BASE_CTL + 1)
#define IPT_SO_SET_MAX IPT_SO_SET_ADD_COUNTERS
#define IPT_SO_GET_INFO (IPT_BASE_CTL)
#define IPT_SO_GET_ENTRIES (IPT_BASE_CTL + 1)
#define IPT_SO_GET_REVISION_MATCH (IPT_BASE_CTL + 2)
#define IPT_SO_GET_REVISION_TARGET (IPT_BASE_CTL + 3)
#define IPT_SO_GET_MAX IPT_SO_SET_REVISION_TARGET
/* The argument to IPT_SO_GET_ENTRIES */
struct ipt_get_entries {
/* Which table: user fills this in */
char name[XT_TABLE_MAXNAMELEN];
/* User fills this in: total entry size. */
unsigned int size; // 该size应该等于xt_table_info->size
/* The entries. */
struct ipt_entry entrytable[0];
};
static int
get_entries(struct net *net, struct ipt_get_entries __user *uptr,
const int *len)
{
int ret;
struct ipt_get_entries get;
struct xt_table *t;
if (*len < sizeof(get)) {
duprintf("get_entries: %u < %zu\n", *len, sizeof(get));
return -EINVAL;
}
if (copy_from_user(&get, uptr, sizeof(get)) != 0)
return -EFAULT;
if (*len != sizeof(struct ipt_get_entries) + get.size) {
duprintf("get_entries: %u != %zu\n",
*len, sizeof(get) + get.size);
return -EINVAL;
}
get.name[sizeof(get.name) - 1] = '\0';
t = xt_find_table_lock(net, AF_INET, get.name);
if (!IS_ERR_OR_NULL(t)) {
const struct xt_table_info *private = t->private;
duprintf("t-?private->number = %u\n", private->number);
if (get.size == private->size)
ret = copy_entries_to_user(private->size, t, uptr->entrytable);
else {
duprintf("get_entries: I've got %u not %u!\n",
private->size, get.size);
ret = -EAGAIN;
}
module_put(t->me);
xt_table_unlock(t);
} else
ret = t ? PTR_ERR(t) : -ENOENT;
return ret;
}
// 本函数是将table->private->entries拷贝到ipt_get_entries的entrytable
static int
copy_entries_to_user(unsigned int total_size,
const struct xt_table *table,
void __user *userprt)
{
unsigned int off, num;
const struct ipt_entry *e;
struct xt_counter *counters;
const struct xt_table_info *private = table->private;
int ret = 0;
const void *loc_cpu_entry;
counters = alloc_counters(table);
if (IS_ERR(counters))
return PTR_ERR(counters);
// 将entries的信息拷贝到用户空间
loc_cpu_entry = private->entries;
if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
ret = -EFAULT;
goto free_counters;
}
// 以下由于用户空间和内核空间使用的结构不一致,进行转换(由于使用的是联合体,空间能够有所保证)
for (off = 0, num = 0; off < total_size; off += e->next_offset, num++) {
unsigned int i;
const struct xt_entry_match *m;
const struct xt_entry_target *t;
// entry的包和流量计数
e = (struct ipt_entry *)(loc_cpu_entry + off);
if (copy_to_user(userptr + off
+ offsetof(struct ipt_entry, counters),
&counters[num],
sizeof(counters[num])) != 0) {
ret = -EFAULT;
goto free_counters;
}
// entry的match模块
for (i = sizeof(struct ipt_entry);
i <e->target_offset;
i += m->u.match_size) {
m = (void *)e + i;
if (copy_to_user(userptr + off +i
+ offset(struct xt_entry_match, u.user.name),
m->u.kernel.match->name,
strlen(m->u.kernel.match->name) + 1) != 0) {
ret = -EFAULT;
goto free_counters;
}
}
// entry的target
t = ipt_get_target_c(e);
if (copy_to_user(userptr + off +e->target_offset
+ offsetof(struct xt_entry_target, u.user.name),
t->u.kernel.target->name,
strlen(t->u.kernel.target->name) + 1) != 0) {
ret = -EFAULT;
goto free_counters;
}
}
free_counters:
vfree(counters);
return ret;
}
版权声明:本文为sidemap原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。