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