libnftnl  1.2.1
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 
29 static int
30 nftnl_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, sizeof(masq->flags));
38  break;
39  case NFTNL_EXPR_MASQ_REG_PROTO_MIN:
40  memcpy(&masq->sreg_proto_min, data, sizeof(masq->sreg_proto_min));
41  break;
42  case NFTNL_EXPR_MASQ_REG_PROTO_MAX:
43  memcpy(&masq->sreg_proto_max, data, sizeof(masq->sreg_proto_max));
44  break;
45  default:
46  return -1;
47  }
48  return 0;
49 }
50 
51 static const void *
52 nftnl_expr_masq_get(const struct nftnl_expr *e, uint16_t type,
53  uint32_t *data_len)
54 {
55  struct nftnl_expr_masq *masq = nftnl_expr_data(e);
56 
57  switch (type) {
58  case NFTNL_EXPR_MASQ_FLAGS:
59  *data_len = sizeof(masq->flags);
60  return &masq->flags;
61  case NFTNL_EXPR_MASQ_REG_PROTO_MIN:
62  *data_len = sizeof(masq->sreg_proto_min);
63  return &masq->sreg_proto_min;
64  case NFTNL_EXPR_MASQ_REG_PROTO_MAX:
65  *data_len = sizeof(masq->sreg_proto_max);
66  return &masq->sreg_proto_max;
67  }
68  return NULL;
69 }
70 
71 static int nftnl_expr_masq_cb(const struct nlattr *attr, void *data)
72 {
73  const struct nlattr **tb = data;
74  int type = mnl_attr_get_type(attr);
75 
76  if (mnl_attr_type_valid(attr, NFTA_MASQ_MAX) < 0)
77  return MNL_CB_OK;
78 
79  switch (type) {
80  case NFTA_MASQ_REG_PROTO_MIN:
81  case NFTA_MASQ_REG_PROTO_MAX:
82  case NFTA_MASQ_FLAGS:
83  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
84  abi_breakage();
85  break;
86  }
87 
88  tb[type] = attr;
89  return MNL_CB_OK;
90 }
91 
92 static void
93 nftnl_expr_masq_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
94 {
95  struct nftnl_expr_masq *masq = nftnl_expr_data(e);
96 
97  if (e->flags & (1 << NFTNL_EXPR_MASQ_FLAGS))
98  mnl_attr_put_u32(nlh, NFTA_MASQ_FLAGS, htobe32(masq->flags));
99  if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN))
100  mnl_attr_put_u32(nlh, NFTA_MASQ_REG_PROTO_MIN,
101  htobe32(masq->sreg_proto_min));
102  if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX))
103  mnl_attr_put_u32(nlh, NFTA_MASQ_REG_PROTO_MAX,
104  htobe32(masq->sreg_proto_max));
105 }
106 
107 static int
108 nftnl_expr_masq_parse(struct nftnl_expr *e, struct nlattr *attr)
109 {
110  struct nftnl_expr_masq *masq = nftnl_expr_data(e);
111  struct nlattr *tb[NFTA_MASQ_MAX+1] = {};
112 
113  if (mnl_attr_parse_nested(attr, nftnl_expr_masq_cb, tb) < 0)
114  return -1;
115 
116  if (tb[NFTA_MASQ_FLAGS]) {
117  masq->flags = be32toh(mnl_attr_get_u32(tb[NFTA_MASQ_FLAGS]));
118  e->flags |= (1 << NFTNL_EXPR_MASQ_FLAGS);
119  }
120  if (tb[NFTA_MASQ_REG_PROTO_MIN]) {
121  masq->sreg_proto_min =
122  be32toh(mnl_attr_get_u32(tb[NFTA_MASQ_REG_PROTO_MIN]));
123  e->flags |= (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN);
124  }
125  if (tb[NFTA_MASQ_REG_PROTO_MAX]) {
126  masq->sreg_proto_max =
127  be32toh(mnl_attr_get_u32(tb[NFTA_MASQ_REG_PROTO_MAX]));
128  e->flags |= (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX);
129  }
130 
131  return 0;
132 }
133 
134 static int nftnl_expr_masq_snprintf(char *buf, size_t remain,
135  uint32_t flags, const struct nftnl_expr *e)
136 {
137  struct nftnl_expr_masq *masq = nftnl_expr_data(e);
138  int offset = 0, ret = 0;
139 
140  if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN)) {
141  ret = snprintf(buf + offset, remain, "proto_min reg %u ",
142  masq->sreg_proto_min);
143  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
144  }
145  if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX)) {
146  ret = snprintf(buf + offset, remain, "proto_max reg %u ",
147  masq->sreg_proto_max);
148  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
149  }
150  if (e->flags & (1 << NFTNL_EXPR_MASQ_FLAGS)) {
151  ret = snprintf(buf + offset, remain, "flags 0x%x ", masq->flags);
152  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
153  }
154 
155  return offset;
156 }
157 
158 struct expr_ops expr_ops_masq = {
159  .name = "masq",
160  .alloc_len = sizeof(struct nftnl_expr_masq),
161  .max_attr = NFTA_MASQ_MAX,
162  .set = nftnl_expr_masq_set,
163  .get = nftnl_expr_masq_get,
164  .parse = nftnl_expr_masq_parse,
165  .build = nftnl_expr_masq_build,
166  .snprintf = nftnl_expr_masq_snprintf,
167 };