output device
Subsystems (3)
Hi,
I would like to set the output device in a rule (it is needed due to overlapping addresses). AFAIK, it cannot be done directly in a rule (if I am wrong, plese tell me immediately). I tried using iptables2 for this: it is working, but significantly slower (3...5 times) than a single routing rule. I think it should be added to the netfilter and conntrack system (keeping its speed), but I don't know how much work such an expansion costs, and I am not familiar enough in developing this field.
Do you have any idea?
Thanks in advance
Gyorgy Kovesdi
Quoted 5 lines · Kovesdi Gyorgy
Hi,
I would like to set the output device in a rule (it is needed due to overlapping addresses). AFAIK, it cannot be done directly in a rule (if I am wrong, plese tell me immediately). I tried using iptables2 for this: it is working, but significantly slower (3...5 times) than a single routing rule. I think it should be added to the netfilter and conntrack system (keeping its speed), but I don't know how much work such an expansion costs, and I am not familiar enough in developing this field.
Do you have any idea?
why can't you use ipt_route ?
Sam
Quoted 3 lines
Quoted 2 lines
I would like to set the output device in a rule (it is needed due to overlapping addresses). AFAIK, it cannot be done directly in a rule...
why can't you use
ipt_route?
If I use such a rule:
iptables -t mangle -A PREROUTING -p udp -d xxxx --dport yyyy -j ROUTE \
--oif eth0.2000 --continuethen nothing happens. If the "--continue" is missing, the given packets are lost.
What can be the problem here? I use kernel 2.6.15.5, iptables 1.2.11 and patch for the ipt_route.
Other question: AFAIK the conntrack does not del with the target device. To be as fast as possible the target device would be handled in the conntrack, and it would be given together with the address in the DNAT rule, because the device is part of the address in a schyzophrenic network.
Regards
Gyorgy Kovesdi
Quoted 3 lines
Quoted 2 lines
I would like to set the output device in a rule (it is needed due to overlapping addresses). AFAIK, it cannot be done directly in a rule...
why can't you use
ipt_route?
Not only the output device, but the input device handling is also missing from the conntrack.
So, if a packet arrives from a device, a conntrack rule is generated. If a packet arrives from another device with the same address and port, it is handled with the conntrack entry of the previous packet.
Regards
Gyorgy Kovesdi
Hi,
I created a patch for the kernel 2.6.16 and the iptables 1.3.5 to do the followings:
1) I sent some emails about necessity of selecting output device in a single DNAT rule. This patch does this, adding a --to-device option to the DNAT.
2) The conntrack handles the input and output devices to be able to route between overlapping VLAN interfaces.
3) This patch contains the icmp patch to resolve the same problem in 'ping'.
I tested it in some simple cases to route between overlapping VLAN interfaces. There is one problem which I could not resolve: the original (2.6.15 and 2.6.16) kernels can route to/from only one of the overlapping VLAN
interfaces: if I route back to the same (overlapping) address range, only one of the overlapping devices is working properly. If I change the input flow to the other device (with the same ip/port values), the packets gets dropped after 30 seconds. My patch resolves this only if the output device is explicitly given in the rule.
Regards
Gyorgy Kovesdi
extensions/libipt_DNAT.c +71 -15
@@ -24,30 +24,35 @@"DNAT v%s options:\n" " --to-destination <ipaddr>[-<ipaddr>][:port-port]\n" " Address to map destination to.\n" -" (You can use this more than once)\n\n" , +" --to-device <name>\n" +" The network device name to route\n" +" the package to\n\n" , IPTABLES_VERSION); }static struct option opts[] = { {"to-destination" ,1 ,0 ,'1' }, + {"to-device" ,1 ,0 ,'2' }, {0 } };static struct ipt_natinfo *append_range (struct ipt_natinfo *info,const struct ip_nat_range *range) { -unsigned int size; +if (info->mr.rangesize <1 ) { +unsigned int size; -/* One rangesize already in struct ipt_natinfo */ - size =IPT_ALIGN (sizeof (*info) + info->mr.rangesize *sizeof (*range)); +/* One rangesize already in struct ipt_natinfo */ + size =IPT_ALIGN (sizeof (*info) + info->mr.rangesize *sizeof (*range)); - info =realloc (info, size); -if (!info) -exit_error (OTHER_PROBLEM ,"Out of memory\n" ); - - info->t.u.target_size = size; - info->mr.range[info->mr.rangesize] = *range; - info->mr.rangesize++; + info =realloc (info, size); +if (!info) +exit_error (OTHER_PROBLEM ,"Out of memory\n" ); + info->t.u.target_size = size; + info->mr.rangesize++; + } + + info->mr.range[0 ] = *range;return info; }
@@ -132,6 +137,36 @@return &(append_range (info, &range)->t); } +static struct ipt_entry_target * +parse_dev (char *arg,struct ipt_natinfo *info) +{ +if (strlen (arg) >sizeof (info->mr.range->ifname) -1 ) +exit_error (PARAMETER_PROBLEM , +"The maximum interface name length is %u" , +sizeof (info->mr.range->ifname) -1 ); + +if (info->mr.rangesize <1 ) { +unsigned int size; + +/* One rangesize already in struct ipt_natinfo */ + size =IPT_ALIGN (sizeof (*info) + info->mr.rangesize *sizeof (info->mr.range[0 ])); + + info =realloc (info, size); +if (!info) +exit_error (OTHER_PROBLEM ,"Out of memory\n" ); + info->t.u.target_size = size; + info->mr.rangesize++; + } + +strcpy (info->mr.range[0 ].ifname, arg); + info->mr.range[0 ].flags |=IP_NAT_RANGE_DEV_SPECIFIED ; + +return &info->t; +} + +#define IPT_DNAT_OPT_DADDR 0x01 +#define IPT_DNAT_OPT_DDEVICE 0x02 +/* Function which parses command options; returns true if it ate an option */ static int
@@ -155,7 +190,7 @@exit_error (PARAMETER_PROBLEM ,"Unexpected `!' after --to-destination" ); -if (*flags) { +if (*flags &IPT_DNAT_OPT_DADDR ) {if (!kernel_version)get_kernel_version ();if (kernel_version >LINUX_VERSION (2 ,6 ,10 ))
@@ -163,7 +198,20 @@ "Multiple --to-destination not supported"); } *target =parse_to (optarg, portok, info); - *flags =1 ; + *flags |=IPT_DNAT_OPT_DADDR ; +return 1 ; + +case '2' : +if (check_inverse (optarg, &invert,NULL ,0 )) +exit_error (PARAMETER_PROBLEM , +"Unexpected `!' after --to-device" ); + +if (*flags &IPT_DNAT_OPT_DDEVICE ) { +exit_error (PARAMETER_PROBLEM , +"You cannot specify --to-device twice" ); + } + *flags |=IPT_DNAT_OPT_DDEVICE ; + *target =parse_dev (optarg, info);return 1 ;default :
@@ -176,23 +224,31 @@ {if (!flags)exit_error (PARAMETER_PROBLEM , -"You must specify --to-destination" ); +"You must specify --to-destination or " +"--to-device" ); }static void print_range (const struct ip_nat_range *r) { +int f=0 ; +if (r->flags &IP_NAT_RANGE_DEV_SPECIFIED ) { +printf ("%s" , r->ifname); + f=1 ; + }if (r->flags &IP_NAT_RANGE_MAP_IPS ) {struct in_addr a; +if (f==1 )printf (":" ); a.s_addr = r->min_ip;printf ("%s" ,addr_to_dotted (&a));if (r->max_ip != r->min_ip) { a.s_addr = r->max_ip;printf ("-%s" ,addr_to_dotted (&a)); } + f=1 ; }if (r->flags &IP_NAT_RANGE_PROTO_SPECIFIED ) { -printf (":" ); +if (f==1 )printf (":" );printf ("%hu" ,ntohs (r->min.tcp.port));if (r->max.tcp.port != r->min.tcp.port)printf ("-%hu" ,ntohs (r->max.tcp.port));
net/ipv4/route.c +15 -1
@@ -1784,7 +1784,11 @@ rth->u.dst.dev = (out_dev)->dev;dev_hold (rth->u.dst.dev); rth->idev =in_dev_get (rth->u.dst.dev); +#if 1 // CONFIG_SCHYZOPHRENIC_ROUTING + rth->fl.oif = skb->oif; +#else rth->fl.oif =0 ; +#endif rth->rt_spec_dst= spec_dst; rth->u.dst.input = ip_forward;
@@ -1922,6 +1926,11 @@if (!in_dev) goto out; +#if 1 // CONFIG_SCHYZOPHRENIC_ROUTING +if (skb->oif >0 ) { + fl.oif = skb->oif; + } +#endif /* Check for the most weird martians, which can be not detected by fib_lookup. */
@@ -2096,7 +2105,11 @@ if (rth->fl.fl4_dst == daddr && rth->fl.fl4_src == saddr && rth->fl.iif == iif && - rth->fl.oif == 0 && +#if 1 // CONFIG_SCHYZOPHRENIC_ROUTING + rth->fl.oif == skb->oif && +#else + rth->fl.oif == 0 && +#endif #ifdef CONFIG_IP_ROUTE_FWMARK rth->fl.fl4_fwmark == skb->nfmark &&#endif
@@ -2781,6 +2794,7 @@if (rta[RTA_OIF -1 ])memcpy (&oif,RTA_DATA (rta[RTA_OIF -1 ]),sizeof (int )); fl.oif = oif; +//printk("inet_rtm_getroute(): %d -> %d \n", oif, in_skb->oif); err =ip_route_output_key (&rt, &fl); }if (err)
net/ipv4/netfilter/ip_conntrack_core.c +20 -12
@@ -166,10 +166,10 @@int ip_ct_get_tuple (const struct iphdr *iph, -const struct sk_buff *skb, -unsigned int dataoff, -struct ip_conntrack_tuple *tuple, -const struct ip_conntrack_protocol *protocol) +const struct sk_buff *skb, +unsigned int dataoff, +struct ip_conntrack_tuple *tuple, +const struct ip_conntrack_protocol *protocol) {/* Never happen */ if (iph->frag_off &htons (IP_OFFSET )) {
@@ -179,6 +179,10 @@ } tuple->src.ip = iph->saddr; +#if 1 // CONFIG_SCHYZOPHRENIC_ROUTING + tuple->dst.ifindex =0 ; + tuple->src.ifindex = skb->dev !=NULL ? skb->dev->ifindex :0 ; +#endif tuple->dst.ip = iph->daddr; tuple->dst.protonum = iph->protocol; tuple->dst.dir =IP_CT_DIR_ORIGINAL ;
@@ -188,13 +192,17 @@int ip_ct_invert_tuple (struct ip_conntrack_tuple *inverse, -const struct ip_conntrack_tuple *orig, -const struct ip_conntrack_protocol *protocol) +const struct ip_conntrack_tuple *orig, +const struct ip_conntrack_protocol *protocol) { inverse->src.ip = orig->dst.ip; inverse->dst.ip = orig->src.ip; inverse->dst.protonum = orig->dst.protonum; inverse->dst.dir = !orig->dst.dir; +#if 1 // CONFIG_SCHYZOPHRENIC_ROUTING + inverse->src.ifindex = orig->dst.ifindex; + inverse->dst.ifindex = orig->src.ifindex; +#endif return protocol->invert_tuple (inverse, orig); }
@@ -374,8 +382,8 @@ const struct ip_conntrack *ignored_conntrack) {ASSERT_READ_LOCK (&ip_conntrack_lock); -return tuplehash_to_ctrack (i) != ignored_conntrack - &&ip_ct_tuple_equal (tuple, &i->tuple); +return tuplehash_to_ctrack (i) != ignored_conntrack && +ip_ct_tuple_equal_srcdev (tuple, &i->tuple); } struct ip_conntrack_tuple_hash *
@@ -632,7 +640,7 @@ }struct ip_conntrack *ip_conntrack_alloc (struct ip_conntrack_tuple *orig, -struct ip_conntrack_tuple *repl) +struct ip_conntrack_tuple *repl) {struct ip_conntrack *conntrack;
@@ -761,7 +769,7 @@IP_NF_ASSERT ((skb->nh.iph->frag_off &htons (IP_OFFSET )) ==0 );if (!ip_ct_get_tuple (skb->nh.iph, skb, skb->nh.iph->ihl*4 , - &tuple,proto)) + &tuple,proto))return NULL ;/* look for tuple match */
@@ -824,8 +832,8 @@/* Never happen */ if ((*pskb)->nh.iph->frag_off &htons (IP_OFFSET )) {if (net_ratelimit ()) { -printk (KERN_ERR "ip_conntrack_in: Frag of proto %u (hook=%u)\n" , - (*pskb)->nh.iph->protocol, hooknum); +printk (KERN_ERR "ip_conntrack_in: Frag of proto %u (hook=%u)\n" , + (*pskb)->nh.iph->protocol, hooknum); }return NF_DROP ; }
net/ipv4/netfilter/ip_nat_core.c +48 -38
129 lines not on this page · raw message
net/ipv4/netfilter/ip_nat_rule.c +24 -12
@@ -174,31 +174,32 @@unsigned int targinfosize, unsigned int hook_mask) { + int Status = 1;struct ip_nat_multi_range_compat *mr = targinfo;/* Must be a valid range */ if (mr->rangesize !=1 ) {printk ("SNAT: multiple ranges no longer supported\n" ); -return 0 ; + Status =0 ; }if (targinfosize !=IPT_ALIGN (sizeof (struct ip_nat_multi_range_compat ))) {DEBUGP ("SNAT: Target size %u wrong for %u ranges\n" , targinfosize, mr->rangesize); -return 0 ; + Status =0 ; }/* Only allow these for NAT. */ if (strcmp (tablename,"nat" ) !=0 ) {DEBUGP ("SNAT: wrong table %s\n" , tablename); -return 0 ; + Status =0 ; }if (hook_mask & ~(1 <<NF_IP_POST_ROUTING )) {DEBUGP ("SNAT: hook mask 0x%x bad\n" , hook_mask); -return 0 ; + Status =0 ; } -return 1 ; +return Status; } static int ipt_dnat_checkentry(const char *tablename,
@@ -207,32 +208,43 @@unsigned int targinfosize, unsigned int hook_mask) { + int status = 1;struct ip_nat_multi_range_compat *mr = targinfo;/* Must be a valid range */ -if (mr->rangesize !=1 ) { +if (mr->rangesize <1 ) { +printk ("DNAT: invalid rangesize.\n" ); + status =0 ; + } +if (mr->rangesize >1 ) {printk ("DNAT: multiple ranges no longer supported\n" ); -return 0 ; + status =0 ; } +if (mr->range[0 ].flags &IP_NAT_RANGE_DEV_SPECIFIED && +dev_get_by_name (mr->range[0 ].ifname) ==NULL ) { +printk ("DNAT: Unknown device: \"%s\"!\n" , mr->range[0 ].ifname); + status =0 ; + } +if (targinfosize !=IPT_ALIGN (sizeof (struct ip_nat_multi_range_compat ))) {DEBUGP ("DNAT: Target size %u wrong for %u ranges\n" , targinfosize, mr->rangesize); -return 0 ; + status =0 ; }/* Only allow these for NAT. */ if (strcmp (tablename,"nat" ) !=0 ) {DEBUGP ("DNAT: wrong table %s\n" , tablename); -return 0 ; + status =0 ; }if (hook_mask & ~((1 <<NF_IP_PRE_ROUTING ) | (1 <<NF_IP_LOCAL_OUT ))) {DEBUGP ("DNAT: hook mask 0x%x bad\n" , hook_mask); -return 0 ; + status =0 ; } - -return 1 ; + +return status; } inline unsigned int
net/ipv4/icmp.c +2 -1
@@ -406,7 +406,8 @@ { .daddr = daddr, .saddr = rt->rt_spec_dst, .tos = RT_TOS(skb->nh.iph->tos) } }, - .proto = IPPROTO_ICMP }; + .proto = IPPROTO_ICMP, + .oif = skb->dev->ifindex};if (ip_route_output_key (&rt, &fl)) goto out_unlock; }
include/linux/skbuff.h +3 -1
@@ -285,7 +285,9 @@__u16 tc_verd;/* traffic control verdict */ #endif #endif - +#if 1 // CONFIG_SCHYZOPHRENIC_ROUTING +int oif; +#endif /* These elements must be at the end, see alloc_skb() for details. */ unsigned int truesize;
include/linux/netfilter_ipv4/ip_nat.h +9 -0
@@ -16,6 +16,7 @@#define IP_NAT_RANGE_MAP_IPS 1#define IP_NAT_RANGE_PROTO_SPECIFIED 2 +#define IP_NAT_RANGE_DEV_SPECIFIED 4/* NAT sequence number modifications */ struct ip_nat_seq {
@@ -26,17 +27,25 @@int32_t offset_before, offset_after; }; +#define IPT_NAT_IFNAMESIZE 16 +/* Single range specification. */ struct ip_nat_range {/* Set to OR of flags above. */ unsigned int flags; +/* If the flag contains IP_NAT_RANGE_MAP_IPS */ /* Inclusive: network order. */ u_int32_t min_ip, max_ip; +/* if the flag contains IP_NAT_RANGE_PROTO_SPECIFIED */ /* Inclusive: network order */ union ip_conntrack_manip_proto min, max; + +/* If the flag contains IP_NAT_RANGE_DEV_SPECIFIED */ +/* The target device name */ +char ifname[IPT_NAT_IFNAMESIZE ]; };/* For backwards compat: don't use in modern code. */
include/linux/netfilter_ipv4/ip_conntrack_tuple.h +51 -8
79 lines not on this page · raw message