📄 ip_tables.c
字号:
if (ret != 0) { vfree(newinfo); return ret; } ret = down_interruptible(&ipt_mutex); if (ret != 0) { vfree(newinfo); 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); /* save number of initial entries */ table->private->initial_entries = table->private->number; rwlock_init(&table->lock); list_prepend(&ipt_tables, table); unlock: up(&ipt_mutex); return ret; free_unlock: vfree(newinfo); 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);}/* 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 sk_buff *skb, unsigned int optlen, int invert, int *hotdrop){ /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */ u_int8_t _opt[60 - sizeof(struct tcphdr)], *op; unsigned int i; duprintf("tcp_match: finding option\n"); if (!optlen) return invert; /* If we don't have the whole header, drop packet. */ op = skb_header_pointer(skb, skb->nh.iph->ihl*4 + sizeof(struct tcphdr), optlen, _opt); if (op == NULL) { *hotdrop = 1; return 0; } for (i = 0; i < optlen; ) { if (op[i] == option) return !invert; if (op[i] < 2) i++; else i += op[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, int *hotdrop){ struct tcphdr _tcph, *th; const struct ipt_tcp *tcpinfo = matchinfo; if (offset) { /* 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; } /* Must not be a fragment. */ return 0; }#define FWINVTCP(bool,invflg) ((bool) ^ !!(tcpinfo->invflags & invflg)) th = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_tcph), &_tcph); if (th == NULL) { /* 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; } if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1], ntohs(th->source), !!(tcpinfo->invflags & IPT_TCP_INV_SRCPT))) return 0; if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1], ntohs(th->dest), !!(tcpinfo->invflags & IPT_TCP_INV_DSTPT))) return 0; if (!FWINVTCP((((unsigned char *)th)[13] & tcpinfo->flg_mask) == tcpinfo->flg_cmp, IPT_TCP_INV_FLAGS)) return 0; if (tcpinfo->option) { if (th->doff * 4 < sizeof(_tcph)) { *hotdrop = 1; return 0; } if (!tcp_find_option(tcpinfo->option, skb, th->doff*4 - sizeof(_tcph), tcpinfo->invflags & IPT_TCP_INV_OPTION, hotdrop)) return 0; } return 1;}/* 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, int *hotdrop){ struct udphdr _udph, *uh; const struct ipt_udp *udpinfo = matchinfo; /* Must not be a fragment. */ if (offset) return 0; uh = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_udph), &_udph); if (uh == NULL) { /* 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; } return port_match(udpinfo->spts[0], udpinfo->spts[1], ntohs(uh->source), !!(udpinfo->invflags & IPT_UDP_INV_SRCPT)) && port_match(udpinfo->dpts[0], udpinfo->dpts[1], ntohs(uh->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 ((test_type == 0xFF) || (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, int *hotdrop){ struct icmphdr _icmph, *ic; const struct ipt_icmp *icmpinfo = matchinfo; /* Must not be a fragment. */ if (offset) return 0; ic = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_icmph), &_icmph); if (ic == NULL) { /* 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; } return icmp_type_code_match(icmpinfo->type, icmpinfo->code[0], icmpinfo->code[1], ic->type, ic->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 = { .name = IPT_STANDARD_TARGET,};static struct ipt_target ipt_error_target = { .name = IPT_ERROR_TARGET, .target = ipt_error,};static struct nf_sockopt_ops ipt_sockopts = { .pf = PF_INET, .set_optmin = IPT_BASE_CTL, .set_optmax = IPT_SO_SET_MAX+1, .set = do_ipt_set_ctl, .get_optmin = IPT_BASE_CTL, .get_optmax = IPT_SO_GET_MAX+1, .get = do_ipt_get_ctl,};static struct ipt_match tcp_matchstruct = { .name = "tcp", .match = &tcp_match, .checkentry = &tcp_checkentry,};static struct ipt_match udp_matchstruct = { .name = "udp", .match = &udp_match, .checkentry = &udp_checkentry,};static struct ipt_match icmp_matchstruct = { .name = "icmp", .match = &icmp_match, .checkentry = &icmp_checkentry,};#ifdef CONFIG_PROC_FSstatic inline int print_name(const char *i, 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", i + sizeof(struct list_head)); if (*pos + namelen > length) { /* Stop iterating */ return 1; } *pos += namelen; } return 0;}static inline int print_target(const struct ipt_target *t, off_t start_offset, char *buffer, int length, off_t *pos, unsigned int *count){ if (t == &ipt_standard_target || t == &ipt_error_target) return 0; return print_name((char *)t, start_offset, buffer, length, pos, count);}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, void *, 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;}static int ipt_get_targets(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_target, print_target, struct ipt_target *, offset, buffer, length, &pos, &count); up(&ipt_mutex); *start = (char *)((unsigned long)count - offset); return pos;}static int ipt_get_matches(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_match, print_name, void *, offset, buffer, length, &pos, &count); up(&ipt_mutex); *start = (char *)((unsigned long)count - offset); return pos;}static const struct { char *name; get_info_t *get_info; } ipt_proc_entry[] ={ { "ip_tables_names", ipt_get_tables }, { "ip_tables_targets", ipt_get_targets }, { "ip_tables_matches", ipt_get_matches }, { NULL, NULL} };#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 { struct proc_dir_entry *proc; int i; for (i = 0; ipt_proc_entry[i].name; i++) { proc = proc_net_create(ipt_proc_entry[i].name, 0, ipt_proc_entry[i].get_info); if (!proc) { while (--i >= 0) proc_net_remove(ipt_proc_entry[i].name); nf_unregister_sockopt(&ipt_sockopts); return -ENOMEM; } proc->owner = THIS_MODULE; } }#endif printk("ip_tables: (C) 2000-2002 Netfilter core team\n"); return 0;}static void __exit fini(void){ nf_unregister_sockopt(&ipt_sockopts);#ifdef CONFIG_PROC_FS { int i; for (i = 0; ipt_proc_entry[i].name; i++) proc_net_remove(ipt_proc_entry[i].name); }#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);EXPORT_SYMBOL(ipt_find_target);module_init(init);module_exit(fini);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -