libnftnl 1.2.8
masq.c
1/*
2 * (C) 2014 by Arturo Borrero Gonzalez <arturo@debian.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10#include <stdio.h>
11#include <stdint.h>
12#include <arpa/inet.h>
13#include <errno.h>
14#include <inttypes.h>
15
16#include <linux/netfilter/nf_tables.h>
17
18#include "internal.h"
19#include <libmnl/libmnl.h>
20#include <libnftnl/expr.h>
21#include <libnftnl/rule.h>
22
24 uint32_t flags;
25 enum nft_registers sreg_proto_min;
26 enum nft_registers sreg_proto_max;
27};
28
29static int
30nftnl_expr_masq_set(struct nftnl_expr *e, uint16_t type,
31 const void *data, uint32_t data_len)
32{
33 struct nftnl_expr_masq *masq = nftnl_expr_data(e);
34
35 switch (type) {
36 case NFTNL_EXPR_MASQ_FLAGS:
37 memcpy(&masq->flags, data, data_len);
38 break;
39 case NFTNL_EXPR_MASQ_REG_PROTO_MIN:
40 memcpy(&masq->sreg_proto_min, data, data_len);
41 break;
42 case NFTNL_EXPR_MASQ_REG_PROTO_MAX:
43 memcpy(&masq->sreg_proto_max, data, data_len);
44 break;
45 }
46 return 0;
47}
48
49static const void *
50nftnl_expr_masq_get(const struct nftnl_expr *e, uint16_t type,
51 uint32_t *data_len)
52{
53 struct nftnl_expr_masq *masq = nftnl_expr_data(e);
54
55 switch (type) {
56 case NFTNL_EXPR_MASQ_FLAGS:
57 *data_len = sizeof(masq->flags);
58 return &masq->flags;
59 case NFTNL_EXPR_MASQ_REG_PROTO_MIN:
60 *data_len = sizeof(masq->sreg_proto_min);
61 return &masq->sreg_proto_min;
62 case NFTNL_EXPR_MASQ_REG_PROTO_MAX:
63 *data_len = sizeof(masq->sreg_proto_max);
64 return &masq->sreg_proto_max;
65 }
66 return NULL;
67}
68
69static int nftnl_expr_masq_cb(const struct nlattr *attr, void *data)
70{
71 const struct nlattr **tb = data;
72 int type = mnl_attr_get_type(attr);
73
74 if (mnl_attr_type_valid(attr, NFTA_MASQ_MAX) < 0)
75 return MNL_CB_OK;
76
77 switch (type) {
78 case NFTA_MASQ_REG_PROTO_MIN:
79 case NFTA_MASQ_REG_PROTO_MAX:
80 case NFTA_MASQ_FLAGS:
81 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
82 abi_breakage();
83 break;
84 }
85
86 tb[type] = attr;
87 return MNL_CB_OK;
88}
89
90static void
91nftnl_expr_masq_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
92{
93 struct nftnl_expr_masq *masq = nftnl_expr_data(e);
94
95 if (e->flags & (1 << NFTNL_EXPR_MASQ_FLAGS))
96 mnl_attr_put_u32(nlh, NFTA_MASQ_FLAGS, htobe32(masq->flags));
97 if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN))
98 mnl_attr_put_u32(nlh, NFTA_MASQ_REG_PROTO_MIN,
99 htobe32(masq->sreg_proto_min));
100 if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX))
101 mnl_attr_put_u32(nlh, NFTA_MASQ_REG_PROTO_MAX,
102 htobe32(masq->sreg_proto_max));
103}
104
105static int
106nftnl_expr_masq_parse(struct nftnl_expr *e, struct nlattr *attr)
107{
108 struct nftnl_expr_masq *masq = nftnl_expr_data(e);
109 struct nlattr *tb[NFTA_MASQ_MAX+1] = {};
110
111 if (mnl_attr_parse_nested(attr, nftnl_expr_masq_cb, tb) < 0)
112 return -1;
113
114 if (tb[NFTA_MASQ_FLAGS]) {
115 masq->flags = be32toh(mnl_attr_get_u32(tb[NFTA_MASQ_FLAGS]));
116 e->flags |= (1 << NFTNL_EXPR_MASQ_FLAGS);
117 }
118 if (tb[NFTA_MASQ_REG_PROTO_MIN]) {
119 masq->sreg_proto_min =
120 be32toh(mnl_attr_get_u32(tb[NFTA_MASQ_REG_PROTO_MIN]));
121 e->flags |= (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN);
122 }
123 if (tb[NFTA_MASQ_REG_PROTO_MAX]) {
124 masq->sreg_proto_max =
125 be32toh(mnl_attr_get_u32(tb[NFTA_MASQ_REG_PROTO_MAX]));
126 e->flags |= (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX);
127 }
128
129 return 0;
130}
131
132static int nftnl_expr_masq_snprintf(char *buf, size_t remain,
133 uint32_t flags, const struct nftnl_expr *e)
134{
135 struct nftnl_expr_masq *masq = nftnl_expr_data(e);
136 int offset = 0, ret = 0;
137
138 if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN)) {
139 ret = snprintf(buf + offset, remain, "proto_min reg %u ",
140 masq->sreg_proto_min);
141 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
142 }
143 if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX)) {
144 ret = snprintf(buf + offset, remain, "proto_max reg %u ",
145 masq->sreg_proto_max);
146 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
147 }
148 if (e->flags & (1 << NFTNL_EXPR_MASQ_FLAGS)) {
149 ret = snprintf(buf + offset, remain, "flags 0x%x ", masq->flags);
150 SNPRINTF_BUFFER_SIZE(ret, remain, offset);
151 }
152
153 return offset;
154}
155
156static struct attr_policy masq_attr_policy[__NFTNL_EXPR_MASQ_MAX] = {
157 [NFTNL_EXPR_MASQ_FLAGS] = { .maxlen = sizeof(uint32_t) },
158 [NFTNL_EXPR_MASQ_REG_PROTO_MIN] = { .maxlen = sizeof(uint32_t) },
159 [NFTNL_EXPR_MASQ_REG_PROTO_MAX] = { .maxlen = sizeof(uint32_t) },
160};
161
162struct expr_ops expr_ops_masq = {
163 .name = "masq",
164 .alloc_len = sizeof(struct nftnl_expr_masq),
165 .nftnl_max_attr = __NFTNL_EXPR_MASQ_MAX - 1,
166 .attr_policy = masq_attr_policy,
167 .set = nftnl_expr_masq_set,
168 .get = nftnl_expr_masq_get,
169 .parse = nftnl_expr_masq_parse,
170 .build = nftnl_expr_masq_build,
171 .output = nftnl_expr_masq_snprintf,
172};