4 Copyright (C) 2006-2009 Jonathan Zarate
10 #include <arpa/inet.h>
13 static const char tcpudp
[2][4] = {"tcp", "udp"};
15 void ipt_forward(ipt_table_t table
)
18 const char *proto
, *saddr
, *xports
, *iport
, *iaddr
;
25 nvp
= nv
= strdup(nvram_safe_get("portforward"));
28 while ((b
= strsep(&nvp
, ">")) != NULL
) {
30 [<1.01] 1<3<30,40-45<60<5<desc
31 [<1.07] 1<3<30,40-45<60<192.168.1.5<desc
33 1<3<71.72.73.74<30,40-45<60<192.168.1.5<desc
37 71.72.73.74 = src addr
40 192.168.1.5 = dst addr
44 if ((vstrsep(b
, "<", &c
, &proto
, &saddr
, &xports
, &iport
, &iaddr
) != 6) || (*c
!= '1')) continue;
48 if (strchr(saddr
, '.') == NULL
) {
55 else if (strlen(saddr
) < 32) {
56 sprintf(src
, "-%s %s", strchr(saddr
, '-') ? "m iprange --src-range" : "s", saddr
);
60 mdport
= (strchr(xports
, ',') != NULL
) ? "-m mport --dports" : "--dport";
61 for (i
= 0; i
< 2; ++i
) {
62 if ((1 << i
) & (*proto
- '0')) {
64 if (strchr(iaddr
, '.')) {
65 strlcpy(ip
, iaddr
, sizeof(ip
));
68 // < 1.01: 5 -> 192.168.1.5
69 strcpy(ip
, lan_cclass
);
70 strlcat(ip
, iaddr
, sizeof(ip
));
72 if (table
== IPT_TABLE_NAT
) {
73 ipt_write("-A PREROUTING -p %s %s -d %s %s %s -j DNAT --to-destination %s%s%s\n",
78 ip
, *iport
? ":" : "", iport
);
80 if (nvram_get_int("nf_loopback") == 1) {
81 ipt_write("-A POSTROUTING -p %s %s %s -s %s/%s -d %s -j SNAT --to-source %s\n",
83 mdport
, *iport
? iport
: xports
,
84 nvram_safe_get("lan_ipaddr"), // corrected by ipt
85 nvram_safe_get("lan_netmask"),
87 nvram_safe_get("wan_ipaddr"));
91 ipt_write("-A wanin %s -p %s -m %s -d %s %s %s -j %s\n",
96 mdport
, *iport
? iport
: xports
,
106 void ipt_triggered(ipt_table_t table
)
109 const char *proto
, *mports
, *fports
;
116 nvp
= nv
= strdup(nvram_safe_get("trigforward"));
120 while ((b
= strsep(&nvp
, ">")) != NULL
) {
121 if ((vstrsep(b
, "<", &c
, &proto
, &mports
, &fports
) != 4) || (*c
!= '1')) continue;
122 for (i
= 0; i
< 2; ++i
) {
123 if ((1 << i
) & (*proto
- '0')) {
125 // should only be created if there is at least one enabled
127 if (table
== IPT_TABLE_NAT
) {
128 ipt_write("-A PREROUTING -d %s -j TRIGGER --trigger-type dnat\n", wanaddr
);
132 ipt_write(":triggers - [0:0]\n"
133 "-A wanout -j triggers\n"
134 "-A wanin -j TRIGGER --trigger-type in\n");
138 strlcpy(s
, mports
, sizeof(s
));
139 if ((p
= strchr(s
, ':')) != NULL
) *p
= '-';
140 if ((p
= strchr(fports
, ':')) != NULL
) *p
= '-';
142 ipt_write("-A triggers -p %s -m %s --dport %s "
143 "-j TRIGGER --trigger-type out --trigger-proto %s --trigger-match %s --trigger-relate %s\n",
146 // can't use mport... trigger-match must be set to the same
147 // ports as dport since it's used to refresh timer during inbound -- zzz