用户空间获取entries IPT_SO_GET_ENTRIES

 

/*
 * 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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值