• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * (C) 2012-2014 by Pablo Neira Ayuso <pablo@netfilter.org>
3  * (C) 2013 by Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
11  */
12 
13 #include <string.h>
14 #include <stdio.h>
15 
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <arpa/inet.h>
19 #include <netinet/ip6.h>
20 #include <netdb.h>
21 
22 #include <xtables.h>
23 
24 #include <linux/netfilter/nf_tables.h>
25 #include "nft.h"
26 #include "nft-shared.h"
27 
nft_ipv6_add(struct nft_handle * h,struct nft_rule_ctx * ctx,struct nftnl_rule * r,struct iptables_command_state * cs)28 static int nft_ipv6_add(struct nft_handle *h, struct nft_rule_ctx *ctx,
29 			struct nftnl_rule *r, struct iptables_command_state *cs)
30 {
31 	struct xtables_rule_match *matchp;
32 	uint32_t op;
33 	int ret;
34 
35 	if (!IN6_IS_ADDR_UNSPECIFIED(&cs->fw6.ipv6.src) ||
36 	    !IN6_IS_ADDR_UNSPECIFIED(&cs->fw6.ipv6.smsk) ||
37 	    (cs->fw6.ipv6.invflags & IPT_INV_SRCIP)) {
38 		op = nft_invflags2cmp(cs->fw6.ipv6.invflags, IPT_INV_SRCIP);
39 		add_addr(h, r, NFT_PAYLOAD_NETWORK_HEADER,
40 			 offsetof(struct ip6_hdr, ip6_src),
41 			 &cs->fw6.ipv6.src, &cs->fw6.ipv6.smsk,
42 			 sizeof(struct in6_addr), op);
43 	}
44 
45 	if (!IN6_IS_ADDR_UNSPECIFIED(&cs->fw6.ipv6.dst) ||
46 	    !IN6_IS_ADDR_UNSPECIFIED(&cs->fw6.ipv6.dmsk) ||
47 	    (cs->fw6.ipv6.invflags & IPT_INV_DSTIP)) {
48 		op = nft_invflags2cmp(cs->fw6.ipv6.invflags, IPT_INV_DSTIP);
49 		add_addr(h, r, NFT_PAYLOAD_NETWORK_HEADER,
50 			 offsetof(struct ip6_hdr, ip6_dst),
51 			 &cs->fw6.ipv6.dst, &cs->fw6.ipv6.dmsk,
52 			 sizeof(struct in6_addr), op);
53 	}
54 
55 	if (cs->fw6.ipv6.iniface[0] != '\0') {
56 		op = nft_invflags2cmp(cs->fw6.ipv6.invflags, IPT_INV_VIA_IN);
57 		add_iface(h, r, cs->fw6.ipv6.iniface, NFT_META_IIFNAME, op);
58 	}
59 
60 	if (cs->fw6.ipv6.outiface[0] != '\0') {
61 		op = nft_invflags2cmp(cs->fw6.ipv6.invflags, IPT_INV_VIA_OUT);
62 		add_iface(h, r, cs->fw6.ipv6.outiface, NFT_META_OIFNAME, op);
63 	}
64 
65 	if (cs->fw6.ipv6.proto != 0) {
66 		op = nft_invflags2cmp(cs->fw6.ipv6.invflags, XT_INV_PROTO);
67 		add_l4proto(h, r, cs->fw6.ipv6.proto, op);
68 	}
69 
70 	add_compat(r, cs->fw6.ipv6.proto, cs->fw6.ipv6.invflags & XT_INV_PROTO);
71 
72 	for (matchp = cs->matches; matchp; matchp = matchp->next) {
73 		ret = add_match(h, ctx, r, matchp->match->m);
74 		if (ret < 0)
75 			return ret;
76 	}
77 
78 	/* Counters need to me added before the target, otherwise they are
79 	 * increased for each rule because of the way nf_tables works.
80 	 */
81 	if (add_counters(r, cs->counters.pcnt, cs->counters.bcnt) < 0)
82 		return -1;
83 
84 	return add_action(r, cs, !!(cs->fw6.ipv6.flags & IP6T_F_GOTO));
85 }
86 
nft_ipv6_is_same(const struct iptables_command_state * a,const struct iptables_command_state * b)87 static bool nft_ipv6_is_same(const struct iptables_command_state *a,
88 			     const struct iptables_command_state *b)
89 {
90 	if (memcmp(a->fw6.ipv6.src.s6_addr, b->fw6.ipv6.src.s6_addr,
91 		   sizeof(struct in6_addr)) != 0
92 	    || memcmp(a->fw6.ipv6.dst.s6_addr, b->fw6.ipv6.dst.s6_addr,
93 		    sizeof(struct in6_addr)) != 0
94 	    || a->fw6.ipv6.proto != b->fw6.ipv6.proto
95 	    || a->fw6.ipv6.flags != b->fw6.ipv6.flags
96 	    || a->fw6.ipv6.invflags != b->fw6.ipv6.invflags) {
97 		DEBUGP("different src/dst/proto/flags/invflags\n");
98 		return false;
99 	}
100 
101 	return is_same_interfaces(a->fw6.ipv6.iniface, a->fw6.ipv6.outiface,
102 				  a->fw6.ipv6.iniface_mask,
103 				  a->fw6.ipv6.outiface_mask,
104 				  b->fw6.ipv6.iniface, b->fw6.ipv6.outiface,
105 				  b->fw6.ipv6.iniface_mask,
106 				  b->fw6.ipv6.outiface_mask);
107 }
108 
nft_ipv6_set_goto_flag(struct iptables_command_state * cs)109 static void nft_ipv6_set_goto_flag(struct iptables_command_state *cs)
110 {
111 	cs->fw6.ipv6.flags |= IP6T_F_GOTO;
112 }
113 
nft_ipv6_print_rule(struct nft_handle * h,struct nftnl_rule * r,unsigned int num,unsigned int format)114 static void nft_ipv6_print_rule(struct nft_handle *h, struct nftnl_rule *r,
115 				unsigned int num, unsigned int format)
116 {
117 	struct iptables_command_state cs = {};
118 
119 	nft_rule_to_iptables_command_state(h, r, &cs);
120 
121 	print_rule_details(num, &cs.counters, cs.jumpto, cs.fw6.ipv6.proto,
122 			   cs.fw6.ipv6.flags, cs.fw6.ipv6.invflags, format);
123 	print_fragment(cs.fw6.ipv6.flags, cs.fw6.ipv6.invflags, format, true);
124 	print_ifaces(cs.fw6.ipv6.iniface, cs.fw6.ipv6.outiface,
125 		     cs.fw6.ipv6.invflags, format);
126 	print_ipv6_addresses(&cs.fw6, format);
127 
128 	if (format & FMT_NOTABLE)
129 		fputs("  ", stdout);
130 
131 	if (cs.fw6.ipv6.flags & IP6T_F_GOTO)
132 		printf("[goto] ");
133 
134 	print_matches_and_target(&cs, format);
135 
136 	if (!(format & FMT_NONEWLINE))
137 		fputc('\n', stdout);
138 
139 	xtables_clear_iptables_command_state(&cs);
140 }
141 
nft_ipv6_save_rule(const struct iptables_command_state * cs,unsigned int format)142 static void nft_ipv6_save_rule(const struct iptables_command_state *cs,
143 			       unsigned int format)
144 {
145 	save_ipv6_addr('s', &cs->fw6.ipv6.src, &cs->fw6.ipv6.smsk,
146 		       cs->fw6.ipv6.invflags & IP6T_INV_SRCIP);
147 	save_ipv6_addr('d', &cs->fw6.ipv6.dst, &cs->fw6.ipv6.dmsk,
148 		       cs->fw6.ipv6.invflags & IP6T_INV_DSTIP);
149 
150 	save_rule_details(cs->fw6.ipv6.iniface, cs->fw6.ipv6.outiface,
151 			  cs->fw6.ipv6.proto, 0, cs->fw6.ipv6.invflags);
152 
153 	save_matches_and_target(cs, cs->fw6.ipv6.flags & IP6T_F_GOTO,
154 				&cs->fw6, format);
155 }
156 
xlate_ipv6_addr(const char * selector,const struct in6_addr * addr,const struct in6_addr * mask,int invert,struct xt_xlate * xl)157 static void xlate_ipv6_addr(const char *selector, const struct in6_addr *addr,
158 			    const struct in6_addr *mask,
159 			    int invert, struct xt_xlate *xl)
160 {
161 	const char *op = invert ? "!= " : "";
162 	char addr_str[INET6_ADDRSTRLEN];
163 	int cidr;
164 
165 	if (!invert && IN6_IS_ADDR_UNSPECIFIED(addr) && IN6_IS_ADDR_UNSPECIFIED(mask))
166 		return;
167 
168 	inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
169 	cidr = xtables_ip6mask_to_cidr(mask);
170 	switch (cidr) {
171 	case -1:
172 		xt_xlate_add(xl, "%s & %s %s %s ", selector,
173 			     xtables_ip6addr_to_numeric(mask),
174 			     invert ? "!=" : "==", addr_str);
175 		break;
176 	case 128:
177 		xt_xlate_add(xl, "%s %s%s ", selector, op, addr_str);
178 		break;
179 	default:
180 		xt_xlate_add(xl, "%s %s%s/%d ", selector, op, addr_str, cidr);
181 	}
182 }
183 
nft_ipv6_xlate(const struct iptables_command_state * cs,struct xt_xlate * xl)184 static int nft_ipv6_xlate(const struct iptables_command_state *cs,
185 			  struct xt_xlate *xl)
186 {
187 	uint16_t proto = cs->fw6.ipv6.proto;
188 	const char *comment;
189 	int ret;
190 
191 	xlate_ifname(xl, "iifname", cs->fw6.ipv6.iniface,
192 		     cs->fw6.ipv6.invflags & IP6T_INV_VIA_IN);
193 	xlate_ifname(xl, "oifname", cs->fw6.ipv6.outiface,
194 		     cs->fw6.ipv6.invflags & IP6T_INV_VIA_OUT);
195 
196 	if (proto != 0 && !xlate_find_protomatch(cs, proto)) {
197 		const char *pname = proto_to_name(proto, 0);
198 
199 		xt_xlate_add(xl, "meta l4proto");
200 		if (cs->fw6.ipv6.invflags & IP6T_INV_PROTO)
201 			xt_xlate_add(xl, " !=");
202 		if (pname)
203 			xt_xlate_add(xl, "%s", pname);
204 		else
205 			xt_xlate_add(xl, "%hu", proto);
206 	}
207 
208 	xlate_ipv6_addr("ip6 saddr", &cs->fw6.ipv6.src, &cs->fw6.ipv6.smsk,
209 			cs->fw6.ipv6.invflags & IP6T_INV_SRCIP, xl);
210 	xlate_ipv6_addr("ip6 daddr", &cs->fw6.ipv6.dst, &cs->fw6.ipv6.dmsk,
211 			cs->fw6.ipv6.invflags & IP6T_INV_DSTIP, xl);
212 
213 	ret = xlate_matches(cs, xl);
214 	if (!ret)
215 		return ret;
216 
217 	/* Always add counters per rule, as in iptables */
218 	xt_xlate_add(xl, "counter");
219 	ret = xlate_action(cs, !!(cs->fw6.ipv6.flags & IP6T_F_GOTO), xl);
220 
221 	comment = xt_xlate_get_comment(xl);
222 	if (comment)
223 		xt_xlate_add(xl, " comment %s", comment);
224 
225 	return ret;
226 }
227 
228 static int
nft_ipv6_add_entry(struct nft_handle * h,const char * chain,const char * table,struct iptables_command_state * cs,struct xtables_args * args,bool verbose,bool append,int rulenum)229 nft_ipv6_add_entry(struct nft_handle *h,
230 		   const char *chain, const char *table,
231 		   struct iptables_command_state *cs,
232 		   struct xtables_args *args, bool verbose,
233 		   bool append, int rulenum)
234 {
235 	unsigned int i, j;
236 	int ret = 1;
237 
238 	for (i = 0; i < args->s.naddrs; i++) {
239 		memcpy(&cs->fw6.ipv6.src,
240 		       &args->s.addr.v6[i], sizeof(struct in6_addr));
241 		memcpy(&cs->fw6.ipv6.smsk,
242 		       &args->s.mask.v6[i], sizeof(struct in6_addr));
243 		for (j = 0; j < args->d.naddrs; j++) {
244 			memcpy(&cs->fw6.ipv6.dst,
245 			       &args->d.addr.v6[j], sizeof(struct in6_addr));
246 			memcpy(&cs->fw6.ipv6.dmsk,
247 			       &args->d.mask.v6[j], sizeof(struct in6_addr));
248 			if (append) {
249 				ret = nft_cmd_rule_append(h, chain, table,
250 						      cs, verbose);
251 			} else {
252 				ret = nft_cmd_rule_insert(h, chain, table,
253 						      cs, rulenum, verbose);
254 			}
255 		}
256 	}
257 
258 	return ret;
259 }
260 
261 static int
nft_ipv6_delete_entry(struct nft_handle * h,const char * chain,const char * table,struct iptables_command_state * cs,struct xtables_args * args,bool verbose)262 nft_ipv6_delete_entry(struct nft_handle *h,
263 		      const char *chain, const char *table,
264 		      struct iptables_command_state *cs,
265 		      struct xtables_args *args, bool verbose)
266 {
267 	unsigned int i, j;
268 	int ret = 1;
269 
270 	for (i = 0; i < args->s.naddrs; i++) {
271 		memcpy(&cs->fw6.ipv6.src,
272 		       &args->s.addr.v6[i], sizeof(struct in6_addr));
273 		memcpy(&cs->fw6.ipv6.smsk,
274 		       &args->s.mask.v6[i], sizeof(struct in6_addr));
275 		for (j = 0; j < args->d.naddrs; j++) {
276 			memcpy(&cs->fw6.ipv6.dst,
277 			       &args->d.addr.v6[j], sizeof(struct in6_addr));
278 			memcpy(&cs->fw6.ipv6.dmsk,
279 			       &args->d.mask.v6[j], sizeof(struct in6_addr));
280 			ret = nft_cmd_rule_delete(h, chain, table, cs, verbose);
281 		}
282 	}
283 
284 	return ret;
285 }
286 
287 static int
nft_ipv6_check_entry(struct nft_handle * h,const char * chain,const char * table,struct iptables_command_state * cs,struct xtables_args * args,bool verbose)288 nft_ipv6_check_entry(struct nft_handle *h,
289 		     const char *chain, const char *table,
290 		     struct iptables_command_state *cs,
291 		     struct xtables_args *args, bool verbose)
292 {
293 	unsigned int i, j;
294 	int ret = 1;
295 
296 	for (i = 0; i < args->s.naddrs; i++) {
297 		memcpy(&cs->fw6.ipv6.src,
298 		       &args->s.addr.v6[i], sizeof(struct in6_addr));
299 		memcpy(&cs->fw6.ipv6.smsk,
300 		       &args->s.mask.v6[i], sizeof(struct in6_addr));
301 		for (j = 0; j < args->d.naddrs; j++) {
302 			memcpy(&cs->fw6.ipv6.dst,
303 			       &args->d.addr.v6[j], sizeof(struct in6_addr));
304 			memcpy(&cs->fw6.ipv6.dmsk,
305 			       &args->d.mask.v6[j], sizeof(struct in6_addr));
306 			ret = nft_cmd_rule_check(h, chain, table, cs, verbose);
307 		}
308 	}
309 
310 	return ret;
311 }
312 
313 static int
nft_ipv6_replace_entry(struct nft_handle * h,const char * chain,const char * table,struct iptables_command_state * cs,struct xtables_args * args,bool verbose,int rulenum)314 nft_ipv6_replace_entry(struct nft_handle *h,
315 		       const char *chain, const char *table,
316 		       struct iptables_command_state *cs,
317 		       struct xtables_args *args, bool verbose,
318 		       int rulenum)
319 {
320 	memcpy(&cs->fw6.ipv6.src, args->s.addr.v6, sizeof(struct in6_addr));
321 	memcpy(&cs->fw6.ipv6.dst, args->d.addr.v6, sizeof(struct in6_addr));
322 	memcpy(&cs->fw6.ipv6.smsk, args->s.mask.v6, sizeof(struct in6_addr));
323 	memcpy(&cs->fw6.ipv6.dmsk, args->d.mask.v6, sizeof(struct in6_addr));
324 
325 	return nft_cmd_rule_replace(h, chain, table, cs, rulenum, verbose);
326 }
327 
328 struct nft_family_ops nft_family_ops_ipv6 = {
329 	.add			= nft_ipv6_add,
330 	.is_same		= nft_ipv6_is_same,
331 	.set_goto_flag		= nft_ipv6_set_goto_flag,
332 	.print_header		= print_header,
333 	.print_rule		= nft_ipv6_print_rule,
334 	.save_rule		= nft_ipv6_save_rule,
335 	.save_chain		= nft_ipv46_save_chain,
336 	.rule_parse		= &nft_ruleparse_ops_ipv6,
337 	.cmd_parse		= {
338 		.proto_parse	= ipv6_proto_parse,
339 		.post_parse	= ipv6_post_parse,
340 		.option_name	= ip46t_option_name,
341 		.option_invert	= ip46t_option_invert,
342 		.command_default = command_default,
343 		.print_help	= xtables_printhelp,
344 	},
345 	.rule_to_cs		= nft_rule_to_iptables_command_state,
346 	.clear_cs		= xtables_clear_iptables_command_state,
347 	.xlate			= nft_ipv6_xlate,
348 	.add_entry		= nft_ipv6_add_entry,
349 	.delete_entry		= nft_ipv6_delete_entry,
350 	.check_entry		= nft_ipv6_check_entry,
351 	.replace_entry		= nft_ipv6_replace_entry,
352 };
353