📄 ip_tables.c
字号:
write_lock_bh(&t->lock); if (t->private->number != paddc->num_counters) { ret = -EINVAL; goto unlock_up_free; } i = 0; IPT_ENTRY_ITERATE(t->private->entries, t->private->size, add_counter_to_entry, paddc->counters, &i); unlock_up_free: write_unlock_bh(&t->lock); up(&ipt_mutex); free: vfree(paddc); return ret;}static intdo_ipt_set_ctl(struct sock *sk, int cmd, void *user, unsigned int len){ int ret; if (!capable(CAP_NET_ADMIN)) return -EPERM; switch (cmd) { case IPT_SO_SET_REPLACE: ret = do_replace(user, len); break; case IPT_SO_SET_ADD_COUNTERS: ret = do_add_counters(user, len); break; default: duprintf("do_ipt_set_ctl: unknown request %i\n", cmd); ret = -EINVAL; } return ret;}static intdo_ipt_get_ctl(struct sock *sk, int cmd, void *user, int *len){ int ret; if (!capable(CAP_NET_ADMIN)) return -EPERM; switch (cmd) { case IPT_SO_GET_INFO: { char name[IPT_TABLE_MAXNAMELEN]; struct ipt_table *t; if (*len != sizeof(struct ipt_getinfo)) { duprintf("length %u != %u\n", *len, sizeof(struct ipt_getinfo)); ret = -EINVAL; break; } if (copy_from_user(name, user, sizeof(name)) != 0) { ret = -EFAULT; break; } t = find_table_lock(name, &ret, &ipt_mutex); if (t) { struct ipt_getinfo info; info.valid_hooks = t->valid_hooks; memcpy(info.hook_entry, t->private->hook_entry, sizeof(info.hook_entry)); memcpy(info.underflow, t->private->underflow, sizeof(info.underflow)); info.num_entries = t->private->number; info.size = t->private->size; strcpy(info.name, name); if (copy_to_user(user, &info, *len) != 0) ret = -EFAULT; else ret = 0; up(&ipt_mutex); } } break; case IPT_SO_GET_ENTRIES: { struct ipt_get_entries get; if (*len < sizeof(get)) { duprintf("get_entries: %u < %u\n", *len, sizeof(get)); ret = -EINVAL; } else if (copy_from_user(&get, user, sizeof(get)) != 0) { ret = -EFAULT; } else if (*len != sizeof(struct ipt_get_entries) + get.size) { duprintf("get_entries: %u != %u\n", *len, sizeof(struct ipt_get_entries) + get.size); ret = -EINVAL; } else ret = get_entries(&get, user); break; } default: duprintf("do_ipt_get_ctl: unknown request %i\n", cmd); ret = -EINVAL; } return ret;}/* Registration hooks for targets. */intipt_register_target(struct ipt_target *target){ int ret; MOD_INC_USE_COUNT; ret = down_interruptible(&ipt_mutex); if (ret != 0) { MOD_DEC_USE_COUNT; return ret; } if (!list_named_insert(&ipt_target, target)) { duprintf("ipt_register_target: `%s' already in list!\n", target->name); ret = -EINVAL; MOD_DEC_USE_COUNT; } up(&ipt_mutex); return ret;}voidipt_unregister_target(struct ipt_target *target){ down(&ipt_mutex); LIST_DELETE(&ipt_target, target); up(&ipt_mutex); MOD_DEC_USE_COUNT;}intipt_register_match(struct ipt_match *match){ int ret; MOD_INC_USE_COUNT; ret = down_interruptible(&ipt_mutex); if (ret != 0) { MOD_DEC_USE_COUNT; return ret; } if (!list_named_insert(&ipt_match, match)) { duprintf("ipt_register_match: `%s' already in list!\n", match->name); MOD_DEC_USE_COUNT; ret = -EINVAL; } up(&ipt_mutex); return ret;}voidipt_unregister_match(struct ipt_match *match){ down(&ipt_mutex); LIST_DELETE(&ipt_match, match); up(&ipt_mutex); MOD_DEC_USE_COUNT;}int ipt_register_table(struct ipt_table *table){ int ret; struct ipt_table_info *newinfo; static struct ipt_table_info bootstrap = { 0, 0, { 0 }, { 0 }, { } }; MOD_INC_USE_COUNT; newinfo = vmalloc(sizeof(struct ipt_table_info) + SMP_ALIGN(table->table->size) * smp_num_cpus); if (!newinfo) { ret = -ENOMEM; MOD_DEC_USE_COUNT; return ret; } memcpy(newinfo->entries, table->table->entries, table->table->size); ret = translate_table(table->name, table->valid_hooks, newinfo, table->table->size, table->table->num_entries, table->table->hook_entry, table->table->underflow); if (ret != 0) { vfree(newinfo); MOD_DEC_USE_COUNT; return ret; } ret = down_interruptible(&ipt_mutex); if (ret != 0) { vfree(newinfo); MOD_DEC_USE_COUNT; return ret; } /* Don't autoload: we'd eat our tail... */ if (list_named_find(&ipt_tables, table->name)) { ret = -EEXIST; goto free_unlock; } /* Simplifies replace_table code. */ table->private = &bootstrap; if (!replace_table(table, 0, newinfo, &ret)) goto free_unlock; duprintf("table->private->number = %u\n", table->private->number); table->lock = RW_LOCK_UNLOCKED; list_prepend(&ipt_tables, table); unlock: up(&ipt_mutex); return ret; free_unlock: vfree(newinfo); MOD_DEC_USE_COUNT; goto unlock;}void ipt_unregister_table(struct ipt_table *table){ down(&ipt_mutex); LIST_DELETE(&ipt_tables, table); up(&ipt_mutex); /* Decrease module usage counts and free resources */ IPT_ENTRY_ITERATE(table->private->entries, table->private->size, cleanup_entry, NULL); vfree(table->private); MOD_DEC_USE_COUNT;}/* Returns 1 if the port is matched by the range, 0 otherwise */static inline intport_match(u_int16_t min, u_int16_t max, u_int16_t port, int invert){ int ret; ret = (port >= min && port <= max) ^ invert; return ret;}static inttcp_find_option(u_int8_t option, const struct tcphdr *tcp, u_int16_t datalen, int invert, int *hotdrop){ unsigned int i = sizeof(struct tcphdr); const u_int8_t *opt = (u_int8_t *)tcp; duprintf("tcp_match: finding option\n"); /* If we don't have the whole header, drop packet. */ if (tcp->doff * 4 > datalen) { *hotdrop = 1; return 0; } while (i < tcp->doff * 4) { if (opt[i] == option) return !invert; if (opt[i] < 2) i++; else i += opt[i+1]?:1; } return invert;}static inttcp_match(const struct sk_buff *skb, const struct net_device *in, const struct net_device *out, const void *matchinfo, int offset, const void *hdr, u_int16_t datalen, int *hotdrop){ const struct tcphdr *tcp = hdr; const struct ipt_tcp *tcpinfo = matchinfo; /* To quote Alan: Don't allow a fragment of TCP 8 bytes in. Nobody normal causes this. Its a cracker trying to break in by doing a flag overwrite to pass the direction checks. */ if (offset == 1) { duprintf("Dropping evil TCP offset=1 frag.\n"); *hotdrop = 1; return 0; } else if (offset == 0 && datalen < sizeof(struct tcphdr)) { /* We've been asked to examine this packet, and we can't. Hence, no choice but to drop. */ duprintf("Dropping evil TCP offset=0 tinygram.\n"); *hotdrop = 1; return 0; } /* FIXME: Try tcp doff >> packet len against various stacks --RR */#define FWINVTCP(bool,invflg) ((bool) ^ !!(tcpinfo->invflags & invflg)) /* Must not be a fragment. */ return !offset && port_match(tcpinfo->spts[0], tcpinfo->spts[1], ntohs(tcp->source), !!(tcpinfo->invflags & IPT_TCP_INV_SRCPT)) && port_match(tcpinfo->dpts[0], tcpinfo->dpts[1], ntohs(tcp->dest), !!(tcpinfo->invflags & IPT_TCP_INV_DSTPT)) && FWINVTCP((((unsigned char *)tcp)[13] & tcpinfo->flg_mask) == tcpinfo->flg_cmp, IPT_TCP_INV_FLAGS) && (!tcpinfo->option || tcp_find_option(tcpinfo->option, tcp, datalen, tcpinfo->invflags & IPT_TCP_INV_OPTION, hotdrop));}/* Called when user tries to insert an entry of this type. */static inttcp_checkentry(const char *tablename, const struct ipt_ip *ip, void *matchinfo, unsigned int matchsize, unsigned int hook_mask){ const struct ipt_tcp *tcpinfo = matchinfo; /* Must specify proto == TCP, and no unknown invflags */ return ip->proto == IPPROTO_TCP && !(ip->invflags & IPT_INV_PROTO) && matchsize == IPT_ALIGN(sizeof(struct ipt_tcp)) && !(tcpinfo->invflags & ~IPT_TCP_INV_MASK);}static intudp_match(const struct sk_buff *skb, const struct net_device *in, const struct net_device *out, const void *matchinfo, int offset, const void *hdr, u_int16_t datalen, int *hotdrop){ const struct udphdr *udp = hdr; const struct ipt_udp *udpinfo = matchinfo; if (offset == 0 && datalen < sizeof(struct udphdr)) { /* We've been asked to examine this packet, and we can't. Hence, no choice but to drop. */ duprintf("Dropping evil UDP tinygram.\n"); *hotdrop = 1; return 0; } /* Must not be a fragment. */ return !offset && port_match(udpinfo->spts[0], udpinfo->spts[1], ntohs(udp->source), !!(udpinfo->invflags & IPT_UDP_INV_SRCPT)) && port_match(udpinfo->dpts[0], udpinfo->dpts[1], ntohs(udp->dest), !!(udpinfo->invflags & IPT_UDP_INV_DSTPT));}/* Called when user tries to insert an entry of this type. */static intudp_checkentry(const char *tablename, const struct ipt_ip *ip, void *matchinfo, unsigned int matchinfosize, unsigned int hook_mask){ const struct ipt_udp *udpinfo = matchinfo; /* Must specify proto == UDP, and no unknown invflags */ if (ip->proto != IPPROTO_UDP || (ip->invflags & IPT_INV_PROTO)) { duprintf("ipt_udp: Protocol %u != %u\n", ip->proto, IPPROTO_UDP); return 0; } if (matchinfosize != IPT_ALIGN(sizeof(struct ipt_udp))) { duprintf("ipt_udp: matchsize %u != %u\n", matchinfosize, IPT_ALIGN(sizeof(struct ipt_udp))); return 0; } if (udpinfo->invflags & ~IPT_UDP_INV_MASK) { duprintf("ipt_udp: unknown flags %X\n", udpinfo->invflags); return 0; } return 1;}/* Returns 1 if the type and code is matched by the range, 0 otherwise */static inline inticmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code, u_int8_t type, u_int8_t code, int invert){ return (type == test_type && code >= min_code && code <= max_code) ^ invert;}static inticmp_match(const struct sk_buff *skb, const struct net_device *in, const struct net_device *out, const void *matchinfo, int offset, const void *hdr, u_int16_t datalen, int *hotdrop){ const struct icmphdr *icmp = hdr; const struct ipt_icmp *icmpinfo = matchinfo; if (offset == 0 && datalen < 2) { /* We've been asked to examine this packet, and we can't. Hence, no choice but to drop. */ duprintf("Dropping evil ICMP tinygram.\n"); *hotdrop = 1; return 0; } /* Must not be a fragment. */ return !offset && icmp_type_code_match(icmpinfo->type, icmpinfo->code[0], icmpinfo->code[1], icmp->type, icmp->code, !!(icmpinfo->invflags&IPT_ICMP_INV));}/* Called when user tries to insert an entry of this type. */static inticmp_checkentry(const char *tablename, const struct ipt_ip *ip, void *matchinfo, unsigned int matchsize, unsigned int hook_mask){ const struct ipt_icmp *icmpinfo = matchinfo; /* Must specify proto == ICMP, and no unknown invflags */ return ip->proto == IPPROTO_ICMP && !(ip->invflags & IPT_INV_PROTO) && matchsize == IPT_ALIGN(sizeof(struct ipt_icmp)) && !(icmpinfo->invflags & ~IPT_ICMP_INV);}/* The built-in targets: standard (NULL) and error. */static struct ipt_target ipt_standard_target= { { NULL, NULL }, IPT_STANDARD_TARGET, NULL, NULL, NULL };static struct ipt_target ipt_error_target= { { NULL, NULL }, IPT_ERROR_TARGET, ipt_error, NULL, NULL };static struct nf_sockopt_ops ipt_sockopts= { { NULL, NULL }, PF_INET, IPT_BASE_CTL, IPT_SO_SET_MAX+1, do_ipt_set_ctl, IPT_BASE_CTL, IPT_SO_GET_MAX+1, do_ipt_get_ctl, 0, NULL };static struct ipt_match tcp_matchstruct= { { NULL, NULL }, "tcp", &tcp_match, &tcp_checkentry, NULL };static struct ipt_match udp_matchstruct= { { NULL, NULL }, "udp", &udp_match, &udp_checkentry, NULL };static struct ipt_match icmp_matchstruct= { { NULL, NULL }, "icmp", &icmp_match, &icmp_checkentry, NULL };#ifdef CONFIG_PROC_FSstatic inline int print_name(const struct ipt_table *t, off_t start_offset, char *buffer, int length, off_t *pos, unsigned int *count){ if ((*count)++ >= start_offset) { unsigned int namelen; namelen = sprintf(buffer + *pos, "%s\n", t->name); if (*pos + namelen > length) { /* Stop iterating */ return 1; } *pos += namelen; } return 0;}static int ipt_get_tables(char *buffer, char **start, off_t offset, int length){ off_t pos = 0; unsigned int count = 0; if (down_interruptible(&ipt_mutex) != 0) return 0; LIST_FIND(&ipt_tables, print_name, struct ipt_table *, offset, buffer, length, &pos, &count); up(&ipt_mutex); /* `start' hack - see fs/proc/generic.c line ~105 */ *start=(char *)((unsigned long)count-offset); return pos;}#endif /*CONFIG_PROC_FS*/static int __init init(void){ int ret; /* Noone else will be downing sem now, so we won't sleep */ down(&ipt_mutex); list_append(&ipt_target, &ipt_standard_target); list_append(&ipt_target, &ipt_error_target); list_append(&ipt_match, &tcp_matchstruct); list_append(&ipt_match, &udp_matchstruct); list_append(&ipt_match, &icmp_matchstruct); up(&ipt_mutex); /* Register setsockopt */ ret = nf_register_sockopt(&ipt_sockopts); if (ret < 0) { duprintf("Unable to register sockopts.\n"); return ret; }#ifdef CONFIG_PROC_FS if (!proc_net_create("ip_tables_names", 0, ipt_get_tables)) { nf_unregister_sockopt(&ipt_sockopts); return -ENOMEM; }#endif printk("ip_tables: (c)2000 Netfilter core team\n"); return 0;}static void __exit fini(void){ nf_unregister_sockopt(&ipt_sockopts);#ifdef CONFIG_PROC_FS proc_net_remove("ip_tables_names");#endif}EXPORT_SYMBOL(ipt_register_table);EXPORT_SYMBOL(ipt_unregister_table);EXPORT_SYMBOL(ipt_register_match);EXPORT_SYMBOL(ipt_unregister_match);EXPORT_SYMBOL(ipt_do_table);EXPORT_SYMBOL(ipt_register_target);EXPORT_SYMBOL(ipt_unregister_target);module_init(init);module_exit(fini);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -