]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_nat.c
Merge branch 'master' into net-next
[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 <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
22 #include "utils.h"
23 #include "tc_util.h"
24 #include <linux/tc_act/tc_nat.h>
25
26 static void
27 explain(void)
28 {
29 fprintf(stderr, "Usage: ... nat NAT\n"
30 "NAT := DIRECTION OLD NEW\n"
31 "DIRECTION := { ingress | egress }\n"
32 "OLD := PREFIX\n"
33 "NEW := ADDRESS\n");
34 }
35
36 static void
37 usage(void)
38 {
39 explain();
40 exit(-1);
41 }
42
43 static int
44 parse_nat_args(int *argc_p, char ***argv_p, struct tc_nat *sel)
45 {
46 int argc = *argc_p;
47 char **argv = *argv_p;
48 inet_prefix addr;
49
50 if (argc <= 0)
51 return -1;
52
53 if (matches(*argv, "egress") == 0)
54 sel->flags |= TCA_NAT_FLAG_EGRESS;
55 else if (matches(*argv, "ingress") != 0)
56 goto bad_val;
57
58 NEXT_ARG();
59
60 if (get_prefix_1(&addr, *argv, AF_INET))
61 goto bad_val;
62
63 sel->old_addr = addr.data[0];
64 sel->mask = htonl(~0u << (32 - addr.bitlen));
65
66 NEXT_ARG();
67
68 if (get_prefix_1(&addr, *argv, AF_INET))
69 goto bad_val;
70
71 sel->new_addr = addr.data[0];
72
73 argc--;
74 argv++;
75
76 *argc_p = argc;
77 *argv_p = argv;
78 return 0;
79
80 bad_val:
81 return -1;
82 }
83
84 static int
85 parse_nat(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
86 {
87 struct tc_nat sel = {};
88
89 int argc = *argc_p;
90 char **argv = *argv_p;
91 int ok = 0;
92 struct rtattr *tail;
93
94 while (argc > 0) {
95 if (matches(*argv, "nat") == 0) {
96 NEXT_ARG();
97 if (parse_nat_args(&argc, &argv, &sel)) {
98 fprintf(stderr, "Illegal nat construct (%s)\n",
99 *argv);
100 explain();
101 return -1;
102 }
103 ok++;
104 continue;
105 } else if (matches(*argv, "help") == 0) {
106 usage();
107 } else {
108 break;
109 }
110
111 }
112
113 if (!ok) {
114 explain();
115 return -1;
116 }
117
118 if (argc) {
119 if (matches(*argv, "reclassify") == 0) {
120 sel.action = TC_ACT_RECLASSIFY;
121 argc--;
122 argv++;
123 } else if (matches(*argv, "pipe") == 0) {
124 sel.action = TC_ACT_PIPE;
125 argc--;
126 argv++;
127 } else if (matches(*argv, "drop") == 0 ||
128 matches(*argv, "shot") == 0) {
129 sel.action = TC_ACT_SHOT;
130 argc--;
131 argv++;
132 } else if (matches(*argv, "continue") == 0) {
133 sel.action = TC_ACT_UNSPEC;
134 argc--;
135 argv++;
136 } else if (matches(*argv, "pass") == 0 ||
137 matches(*argv, "ok") == 0) {
138 sel.action = TC_ACT_OK;
139 argc--;
140 argv++;
141 }
142 }
143
144 if (argc) {
145 if (matches(*argv, "index") == 0) {
146 NEXT_ARG();
147 if (get_u32(&sel.index, *argv, 10)) {
148 fprintf(stderr, "Nat: Illegal \"index\"\n");
149 return -1;
150 }
151 argc--;
152 argv++;
153 }
154 }
155
156 tail = NLMSG_TAIL(n);
157 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
158 addattr_l(n, MAX_MSG, TCA_NAT_PARMS, &sel, sizeof(sel));
159 tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail;
160
161 *argc_p = argc;
162 *argv_p = argv;
163 return 0;
164 }
165
166 static int
167 print_nat(struct action_util *au, FILE * f, struct rtattr *arg)
168 {
169 struct tc_nat *sel;
170 struct rtattr *tb[TCA_NAT_MAX + 1];
171 char buf1[256];
172 char buf2[256];
173
174 SPRINT_BUF(buf3);
175 int len;
176
177 if (arg == NULL)
178 return -1;
179
180 parse_rtattr_nested(tb, TCA_NAT_MAX, arg);
181
182 if (tb[TCA_NAT_PARMS] == NULL) {
183 fprintf(f, "[NULL nat parameters]");
184 return -1;
185 }
186 sel = RTA_DATA(tb[TCA_NAT_PARMS]);
187
188 len = ffs(sel->mask);
189 len = len ? 33 - len : 0;
190
191 fprintf(f, " nat %s %s/%d %s %s", sel->flags & TCA_NAT_FLAG_EGRESS ?
192 "egress" : "ingress",
193 format_host_r(AF_INET, 4, &sel->old_addr, buf1, sizeof(buf1)),
194 len,
195 format_host_r(AF_INET, 4, &sel->new_addr, buf2, sizeof(buf2)),
196 action_n2a(sel->action, buf3, sizeof(buf3)));
197
198 if (show_stats) {
199 if (tb[TCA_NAT_TM]) {
200 struct tcf_t *tm = RTA_DATA(tb[TCA_NAT_TM]);
201
202 print_tm(f, tm);
203 }
204 }
205
206 return 0;
207 }
208
209 struct action_util nat_action_util = {
210 .id = "nat",
211 .parse_aopt = parse_nat,
212 .print_aopt = print_nat,
213 };