| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* m_nat.c NAT module |
| 4 |
* |
| 5 |
* Authors: Herbert Xu <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <sys/socket.h> |
| 13 |
#include <netinet/in.h> |
| 14 |
#include <arpa/inet.h> |
| 15 |
#include <string.h> |
| 16 |
#include "utils.h" |
| 17 |
#include "tc_util.h" |
| 18 |
#include <linux/tc_act/tc_nat.h> |
| 19 |
|
| 20 |
static void |
| 21 |
explain(void) |
| 22 |
{ |
| 23 |
fprintf(stderr, "Usage: ... nat NAT\n" |
| 24 |
"NAT := DIRECTION OLD NEW\n" |
| 25 |
"DIRECTION := { ingress | egress }\n" |
| 26 |
"OLD := PREFIX\n" |
| 27 |
"NEW := ADDRESS\n"); |
| 28 |
} |
| 29 |
|
| 30 |
static void |
| 31 |
usage(void) |
| 32 |
{ |
| 33 |
explain(); |
| 34 |
exit(-1); |
| 35 |
} |
| 36 |
|
| 37 |
static int |
| 38 |
parse_nat_args(int *argc_p, char ***argv_p, struct tc_nat *sel) |
| 39 |
{ |
| 40 |
int argc = *argc_p; |
| 41 |
char **argv = *argv_p; |
| 42 |
inet_prefix addr; |
| 43 |
|
| 44 |
if (argc <= 0) |
| 45 |
return -1; |
| 46 |
|
| 47 |
if (matches(*argv, "egress") == 0) |
| 48 |
sel->flags |= TCA_NAT_FLAG_EGRESS; |
| 49 |
else if (matches(*argv, "ingress") != 0) |
| 50 |
goto bad_val; |
| 51 |
|
| 52 |
NEXT_ARG(); |
| 53 |
|
| 54 |
if (get_prefix_1(&addr, *argv, AF_INET)) |
| 55 |
goto bad_val; |
| 56 |
|
| 57 |
sel->old_addr = addr.data[0]; |
| 58 |
sel->mask = htonl(~(uint64_t)0 << (32 - addr.bitlen)); |
| 59 |
|
| 60 |
NEXT_ARG(); |
| 61 |
|
| 62 |
if (get_prefix_1(&addr, *argv, AF_INET)) |
| 63 |
goto bad_val; |
| 64 |
|
| 65 |
sel->new_addr = addr.data[0]; |
| 66 |
|
| 67 |
argc--; |
| 68 |
argv++; |
| 69 |
|
| 70 |
*argc_p = argc; |
| 71 |
*argv_p = argv; |
| 72 |
return 0; |
| 73 |
|
| 74 |
bad_val: |
| 75 |
return -1; |
| 76 |
} |
| 77 |
|
| 78 |
static int |
| 79 |
parse_nat(const struct action_util *a, int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n) |
| 80 |
{ |
| 81 |
struct tc_nat sel = {}; |
| 82 |
|
| 83 |
int argc = *argc_p; |
| 84 |
char **argv = *argv_p; |
| 85 |
int ok = 0; |
| 86 |
struct rtattr *tail; |
| 87 |
|
| 88 |
while (argc > 0) { |
| 89 |
if (matches(*argv, "nat") == 0) { |
| 90 |
NEXT_ARG(); |
| 91 |
if (strcmp(*argv, "index") == 0) { |
| 92 |
goto skip_args; |
| 93 |
} else if (parse_nat_args(&argc, &argv, &sel)) { |
| 94 |
fprintf(stderr, "Illegal nat construct (%s)\n", |
| 95 |
*argv); |
| 96 |
explain(); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
ok++; |
| 100 |
continue; |
| 101 |
} else if (matches(*argv, "help") == 0) { |
| 102 |
usage(); |
| 103 |
} else { |
| 104 |
break; |
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
if (!ok) { |
| 110 |
explain(); |
| 111 |
return -1; |
| 112 |
} |
| 113 |
|
| 114 |
parse_action_control_dflt(&argc, &argv, &sel.action, false, TC_ACT_OK); |
| 115 |
|
| 116 |
if (argc) { |
| 117 |
if (matches(*argv, "index") == 0) { |
| 118 |
skip_args: |
| 119 |
NEXT_ARG(); |
| 120 |
if (get_u32(&sel.index, *argv, 10)) { |
| 121 |
fprintf(stderr, "Nat: Illegal \"index\"\n"); |
| 122 |
return -1; |
| 123 |
} |
| 124 |
argc--; |
| 125 |
argv++; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
tail = addattr_nest(n, MAX_MSG, tca_id); |
| 130 |
addattr_l(n, MAX_MSG, TCA_NAT_PARMS, &sel, sizeof(sel)); |
| 131 |
addattr_nest_end(n, tail); |
| 132 |
|
| 133 |
*argc_p = argc; |
| 134 |
*argv_p = argv; |
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
static int |
| 139 |
print_nat(const struct action_util *au, FILE * f, struct rtattr *arg) |
| 140 |
{ |
| 141 |
struct tc_nat *sel; |
| 142 |
struct rtattr *tb[TCA_NAT_MAX + 1]; |
| 143 |
SPRINT_BUF(buf1); |
| 144 |
SPRINT_BUF(buf2); |
| 145 |
int len; |
| 146 |
|
| 147 |
print_string(PRINT_ANY, "type", " %s ", "nat"); |
| 148 |
if (arg == NULL) |
| 149 |
return 0; |
| 150 |
|
| 151 |
parse_rtattr_nested(tb, TCA_NAT_MAX, arg); |
| 152 |
|
| 153 |
if (tb[TCA_NAT_PARMS] == NULL) { |
| 154 |
fprintf(stderr, "Missing nat parameters\n"); |
| 155 |
return -1; |
| 156 |
} |
| 157 |
sel = RTA_DATA(tb[TCA_NAT_PARMS]); |
| 158 |
|
| 159 |
len = ffs(ntohl(sel->mask)); |
| 160 |
len = len ? 33 - len : 0; |
| 161 |
|
| 162 |
print_string(PRINT_ANY, "direction", "%s", |
| 163 |
sel->flags & TCA_NAT_FLAG_EGRESS ? "egress" : "ingress"); |
| 164 |
|
| 165 |
snprintf(buf2, sizeof(buf2), "%s/%d", |
| 166 |
format_host_r(AF_INET, 4, &sel->old_addr, buf1, sizeof(buf1)), |
| 167 |
len); |
| 168 |
print_string(PRINT_ANY, "old_addr", " %s", buf2); |
| 169 |
print_string(PRINT_ANY, "new_addr", " %s", |
| 170 |
format_host_r(AF_INET, 4, &sel->new_addr, buf1, sizeof(buf1))); |
| 171 |
|
| 172 |
print_action_control(" ", sel->action, ""); |
| 173 |
print_nl(); |
| 174 |
print_uint(PRINT_ANY, "index", "\t index %u", sel->index); |
| 175 |
print_int(PRINT_ANY, "ref", " ref %d", sel->refcnt); |
| 176 |
print_int(PRINT_ANY, "bind", " bind %d", sel->bindcnt); |
| 177 |
|
| 178 |
if (show_stats) { |
| 179 |
if (tb[TCA_NAT_TM]) { |
| 180 |
struct tcf_t *tm = RTA_DATA(tb[TCA_NAT_TM]); |
| 181 |
|
| 182 |
print_tm(tm); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
print_nl(); |
| 187 |
|
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
struct action_util nat_action_util = { |
| 192 |
.id = "nat", |
| 193 |
.parse_aopt = parse_nat, |
| 194 |
.print_aopt = print_nat, |
| 195 |
}; |