2007-10-12 17:08:40 +08:00
|
|
|
/*
|
|
|
|
* m_nat.c NAT module
|
|
|
|
*
|
|
|
|
* This program is free software; you can distribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Authors: Herbert Xu <herbert@gondor.apana.org.au>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "utils.h"
|
|
|
|
#include "tc_util.h"
|
|
|
|
#include <linux/tc_act/tc_nat.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
explain(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: ... nat NAT\n"
|
|
|
|
"NAT := DIRECTION OLD NEW\n"
|
|
|
|
"DIRECTION := { ingress | egress }\n"
|
|
|
|
"OLD := PREFIX\n"
|
|
|
|
"NEW := ADDRESS\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
explain();
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-03-22 02:48:36 +08:00
|
|
|
parse_nat_args(int *argc_p, char ***argv_p, struct tc_nat *sel)
|
2007-10-12 17:08:40 +08:00
|
|
|
{
|
|
|
|
int argc = *argc_p;
|
|
|
|
char **argv = *argv_p;
|
|
|
|
inet_prefix addr;
|
|
|
|
|
|
|
|
if (argc <= 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (matches(*argv, "egress") == 0)
|
|
|
|
sel->flags |= TCA_NAT_FLAG_EGRESS;
|
|
|
|
else if (matches(*argv, "ingress") != 0)
|
|
|
|
goto bad_val;
|
|
|
|
|
|
|
|
NEXT_ARG();
|
|
|
|
|
|
|
|
if (get_prefix_1(&addr, *argv, AF_INET))
|
|
|
|
goto bad_val;
|
|
|
|
|
|
|
|
sel->old_addr = addr.data[0];
|
|
|
|
sel->mask = htonl(~0u << (32 - addr.bitlen));
|
|
|
|
|
|
|
|
NEXT_ARG();
|
|
|
|
|
|
|
|
if (get_prefix_1(&addr, *argv, AF_INET))
|
|
|
|
goto bad_val;
|
|
|
|
|
|
|
|
sel->new_addr = addr.data[0];
|
|
|
|
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
|
|
|
|
*argc_p = argc;
|
|
|
|
*argv_p = argv;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bad_val:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_nat(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
|
|
|
|
{
|
2016-07-18 22:48:42 +08:00
|
|
|
struct tc_nat sel = {};
|
2007-10-12 17:08:40 +08:00
|
|
|
|
|
|
|
int argc = *argc_p;
|
|
|
|
char **argv = *argv_p;
|
|
|
|
int ok = 0;
|
|
|
|
struct rtattr *tail;
|
|
|
|
|
|
|
|
while (argc > 0) {
|
|
|
|
if (matches(*argv, "nat") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (parse_nat_args(&argc, &argv, &sel)) {
|
2016-03-22 02:48:36 +08:00
|
|
|
fprintf(stderr, "Illegal nat construct (%s)\n",
|
2007-10-12 17:08:40 +08:00
|
|
|
*argv);
|
|
|
|
explain();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ok++;
|
|
|
|
continue;
|
|
|
|
} else if (matches(*argv, "help") == 0) {
|
|
|
|
usage();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
explain();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-17 01:29:36 +08:00
|
|
|
parse_action_control_dflt(&argc, &argv, &sel.action, false, TC_ACT_OK);
|
2007-10-12 17:08:40 +08:00
|
|
|
|
|
|
|
if (argc) {
|
|
|
|
if (matches(*argv, "index") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (get_u32(&sel.index, *argv, 10)) {
|
2013-09-29 19:39:23 +08:00
|
|
|
fprintf(stderr, "Nat: Illegal \"index\"\n");
|
2007-10-12 17:08:40 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 16:15:08 +08:00
|
|
|
tail = addattr_nest(n, MAX_MSG, tca_id);
|
2007-10-12 17:08:40 +08:00
|
|
|
addattr_l(n, MAX_MSG, TCA_NAT_PARMS, &sel, sizeof(sel));
|
2018-01-31 16:15:08 +08:00
|
|
|
addattr_nest_end(n, tail);
|
2007-10-12 17:08:40 +08:00
|
|
|
|
|
|
|
*argc_p = argc;
|
|
|
|
*argv_p = argv;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-03-22 02:48:36 +08:00
|
|
|
print_nat(struct action_util *au, FILE * f, struct rtattr *arg)
|
2007-10-12 17:08:40 +08:00
|
|
|
{
|
|
|
|
struct tc_nat *sel;
|
|
|
|
struct rtattr *tb[TCA_NAT_MAX + 1];
|
tc: jsonify nat action
Add json output support for nat action
Example output:
~$ $TC actions add action nat egress 10.10.10.1 20.20.20.2 index 2
~$ $TC actions add action nat ingress 100.100.100.1/32 200.200.200.2 \
continue index 99
~$ $TC -j actions ls action nat
[{
"total acts": 2
}, {
"actions": [{
"order": 0,
"type": "nat",
"direction": "egress",
"old_addr": "10.10.10.1/32",
"new_addr": "20.20.20.2",
"control_action": {
"type": "pass"
},
"index": 2,
"ref": 1,
"bind": 0
}, {
"order": 1,
"type": "nat",
"direction": "ingress",
"old_addr": "100.100.100.1/32",
"new_addr": "200.200.200.2",
"control_action": {
"type": "continue"
},
"index": 99,
"ref": 1,
"bind": 0
}]
}]
Signed-off-by: Keara Leibovitz <kleib@mojatatu.com>
Signed-off-by: David Ahern <dsahern@gmail.com>
2018-06-19 02:57:49 +08:00
|
|
|
SPRINT_BUF(buf1);
|
|
|
|
SPRINT_BUF(buf2);
|
2007-10-12 17:08:40 +08:00
|
|
|
int len;
|
|
|
|
|
|
|
|
if (arg == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
parse_rtattr_nested(tb, TCA_NAT_MAX, arg);
|
|
|
|
|
|
|
|
if (tb[TCA_NAT_PARMS] == NULL) {
|
tc: jsonify nat action
Add json output support for nat action
Example output:
~$ $TC actions add action nat egress 10.10.10.1 20.20.20.2 index 2
~$ $TC actions add action nat ingress 100.100.100.1/32 200.200.200.2 \
continue index 99
~$ $TC -j actions ls action nat
[{
"total acts": 2
}, {
"actions": [{
"order": 0,
"type": "nat",
"direction": "egress",
"old_addr": "10.10.10.1/32",
"new_addr": "20.20.20.2",
"control_action": {
"type": "pass"
},
"index": 2,
"ref": 1,
"bind": 0
}, {
"order": 1,
"type": "nat",
"direction": "ingress",
"old_addr": "100.100.100.1/32",
"new_addr": "200.200.200.2",
"control_action": {
"type": "continue"
},
"index": 99,
"ref": 1,
"bind": 0
}]
}]
Signed-off-by: Keara Leibovitz <kleib@mojatatu.com>
Signed-off-by: David Ahern <dsahern@gmail.com>
2018-06-19 02:57:49 +08:00
|
|
|
print_string(PRINT_FP, NULL, "%s", "[NULL nat parameters]");
|
2007-10-12 17:08:40 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sel = RTA_DATA(tb[TCA_NAT_PARMS]);
|
|
|
|
|
|
|
|
len = ffs(sel->mask);
|
|
|
|
len = len ? 33 - len : 0;
|
|
|
|
|
tc: jsonify nat action
Add json output support for nat action
Example output:
~$ $TC actions add action nat egress 10.10.10.1 20.20.20.2 index 2
~$ $TC actions add action nat ingress 100.100.100.1/32 200.200.200.2 \
continue index 99
~$ $TC -j actions ls action nat
[{
"total acts": 2
}, {
"actions": [{
"order": 0,
"type": "nat",
"direction": "egress",
"old_addr": "10.10.10.1/32",
"new_addr": "20.20.20.2",
"control_action": {
"type": "pass"
},
"index": 2,
"ref": 1,
"bind": 0
}, {
"order": 1,
"type": "nat",
"direction": "ingress",
"old_addr": "100.100.100.1/32",
"new_addr": "200.200.200.2",
"control_action": {
"type": "continue"
},
"index": 99,
"ref": 1,
"bind": 0
}]
}]
Signed-off-by: Keara Leibovitz <kleib@mojatatu.com>
Signed-off-by: David Ahern <dsahern@gmail.com>
2018-06-19 02:57:49 +08:00
|
|
|
print_string(PRINT_ANY, "type", " %s ", "nat");
|
|
|
|
print_string(PRINT_ANY, "direction", "%s",
|
|
|
|
sel->flags & TCA_NAT_FLAG_EGRESS ? "egress" : "ingress");
|
2007-10-12 17:08:40 +08:00
|
|
|
|
tc: jsonify nat action
Add json output support for nat action
Example output:
~$ $TC actions add action nat egress 10.10.10.1 20.20.20.2 index 2
~$ $TC actions add action nat ingress 100.100.100.1/32 200.200.200.2 \
continue index 99
~$ $TC -j actions ls action nat
[{
"total acts": 2
}, {
"actions": [{
"order": 0,
"type": "nat",
"direction": "egress",
"old_addr": "10.10.10.1/32",
"new_addr": "20.20.20.2",
"control_action": {
"type": "pass"
},
"index": 2,
"ref": 1,
"bind": 0
}, {
"order": 1,
"type": "nat",
"direction": "ingress",
"old_addr": "100.100.100.1/32",
"new_addr": "200.200.200.2",
"control_action": {
"type": "continue"
},
"index": 99,
"ref": 1,
"bind": 0
}]
}]
Signed-off-by: Keara Leibovitz <kleib@mojatatu.com>
Signed-off-by: David Ahern <dsahern@gmail.com>
2018-06-19 02:57:49 +08:00
|
|
|
snprintf(buf2, sizeof(buf2), "%s/%d",
|
|
|
|
format_host_r(AF_INET, 4, &sel->old_addr, buf1, sizeof(buf1)),
|
|
|
|
len);
|
|
|
|
print_string(PRINT_ANY, "old_addr", " %s", buf2);
|
|
|
|
print_string(PRINT_ANY, "new_addr", " %s",
|
|
|
|
format_host_r(AF_INET, 4, &sel->new_addr, buf1, sizeof(buf1)));
|
|
|
|
|
|
|
|
print_action_control(f, " ", sel->action, "");
|
|
|
|
print_string(PRINT_FP, NULL, "%s", _SL_);
|
|
|
|
print_uint(PRINT_ANY, "index", "\t index %u", sel->index);
|
|
|
|
print_int(PRINT_ANY, "ref", " ref %d", sel->refcnt);
|
|
|
|
print_int(PRINT_ANY, "bind", " bind %d", sel->bindcnt);
|
2018-03-21 02:21:47 +08:00
|
|
|
|
2007-10-12 17:08:40 +08:00
|
|
|
if (show_stats) {
|
|
|
|
if (tb[TCA_NAT_TM]) {
|
|
|
|
struct tcf_t *tm = RTA_DATA(tb[TCA_NAT_TM]);
|
2016-03-22 02:48:36 +08:00
|
|
|
|
|
|
|
print_tm(f, tm);
|
2007-10-12 17:08:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
tc: jsonify nat action
Add json output support for nat action
Example output:
~$ $TC actions add action nat egress 10.10.10.1 20.20.20.2 index 2
~$ $TC actions add action nat ingress 100.100.100.1/32 200.200.200.2 \
continue index 99
~$ $TC -j actions ls action nat
[{
"total acts": 2
}, {
"actions": [{
"order": 0,
"type": "nat",
"direction": "egress",
"old_addr": "10.10.10.1/32",
"new_addr": "20.20.20.2",
"control_action": {
"type": "pass"
},
"index": 2,
"ref": 1,
"bind": 0
}, {
"order": 1,
"type": "nat",
"direction": "ingress",
"old_addr": "100.100.100.1/32",
"new_addr": "200.200.200.2",
"control_action": {
"type": "continue"
},
"index": 99,
"ref": 1,
"bind": 0
}]
}]
Signed-off-by: Keara Leibovitz <kleib@mojatatu.com>
Signed-off-by: David Ahern <dsahern@gmail.com>
2018-06-19 02:57:49 +08:00
|
|
|
print_string(PRINT_FP, NULL, "%s", _SL_);
|
2018-03-21 02:21:47 +08:00
|
|
|
|
2007-10-12 17:08:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct action_util nat_action_util = {
|
|
|
|
.id = "nat",
|
|
|
|
.parse_aopt = parse_nat,
|
|
|
|
.print_aopt = print_nat,
|
|
|
|
};
|