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