libnftnl  1.1.2
osf.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <string.h>
4 #include <arpa/inet.h>
5 #include <errno.h>
6 #include <linux/netfilter/nf_tables.h>
7 
8 #include "internal.h"
9 #include <libmnl/libmnl.h>
10 #include <libnftnl/expr.h>
11 #include <libnftnl/rule.h>
12 
13 #define OSF_GENRE_SIZE 32
14 
16  enum nft_registers dreg;
17  uint8_t ttl;
18 };
19 
20 static int nftnl_expr_osf_set(struct nftnl_expr *e, uint16_t type,
21  const void *data, uint32_t data_len)
22 {
23  struct nftnl_expr_osf *osf = nftnl_expr_data(e);
24 
25  switch(type) {
26  case NFTNL_EXPR_OSF_DREG:
27  memcpy(&osf->dreg, data, sizeof(osf->dreg));
28  break;
29  case NFTNL_EXPR_OSF_TTL:
30  memcpy(&osf->ttl, data, sizeof(osf->ttl));
31  break;
32  }
33  return 0;
34 }
35 
36 static const void *
37 nftnl_expr_osf_get(const struct nftnl_expr *e, uint16_t type,
38  uint32_t *data_len)
39 {
40  struct nftnl_expr_osf *osf = nftnl_expr_data(e);
41 
42  switch(type) {
43  case NFTNL_EXPR_OSF_DREG:
44  *data_len = sizeof(osf->dreg);
45  return &osf->dreg;
46  case NFTNL_EXPR_OSF_TTL:
47  *data_len = sizeof(osf->ttl);
48  return &osf->ttl;
49  }
50  return NULL;
51 }
52 
53 static int nftnl_expr_osf_cb(const struct nlattr *attr, void *data)
54 {
55  const struct nlattr **tb = data;
56  int type = mnl_attr_get_type(attr);
57 
58  if (mnl_attr_type_valid(attr, NFTA_OSF_MAX) < 0)
59  return MNL_CB_OK;
60 
61  switch(type) {
62  case NFTNL_EXPR_OSF_DREG:
63  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
64  abi_breakage();
65  break;
66 
67  case NFTNL_EXPR_OSF_TTL:
68  if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
69  abi_breakage();
70  break;
71  }
72 
73  tb[type] = attr;
74  return MNL_CB_OK;
75 }
76 
77 static void
78 nftnl_expr_osf_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
79 {
80  struct nftnl_expr_osf *osf = nftnl_expr_data(e);
81 
82  if (e->flags & (1 << NFTNL_EXPR_OSF_DREG))
83  mnl_attr_put_u32(nlh, NFTNL_EXPR_OSF_DREG, htonl(osf->dreg));
84  if (e->flags & (1 << NFTNL_EXPR_OSF_TTL))
85  mnl_attr_put_u8(nlh, NFTNL_EXPR_OSF_TTL, osf->ttl);
86 }
87 
88 static int
89 nftnl_expr_osf_parse(struct nftnl_expr *e, struct nlattr *attr)
90 {
91  struct nftnl_expr_osf *osf = nftnl_expr_data(e);
92  struct nlattr *tb[NFTA_OSF_MAX + 1] = {};
93 
94  if (mnl_attr_parse_nested(attr, nftnl_expr_osf_cb, tb) < 0)
95  return -1;
96 
97  if (tb[NFTA_OSF_DREG]) {
98  osf->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_OSF_DREG]));
99  e->flags |= (1 << NFTNL_EXPR_OSF_DREG);
100  }
101 
102  if (tb[NFTA_OSF_TTL]) {
103  osf->ttl = mnl_attr_get_u8(tb[NFTA_OSF_TTL]);
104  e->flags |= (1 << NFTNL_EXPR_OSF_TTL);
105  }
106 
107  return 0;
108 }
109 
110 static int nftnl_expr_osf_snprintf_default(char *buf, size_t size,
111  const struct nftnl_expr *e)
112 {
113  struct nftnl_expr_osf *osf = nftnl_expr_data(e);
114  int ret, offset = 0, len = size;
115 
116  if (e->flags & (1 << NFTNL_EXPR_OSF_DREG)) {
117  ret = snprintf(buf, len, "dreg %u ", osf->dreg);
118  SNPRINTF_BUFFER_SIZE(ret, len, offset);
119  }
120 
121  return offset;
122 }
123 
124 static int
125 nftnl_expr_osf_snprintf(char *buf, size_t len, uint32_t type,
126  uint32_t flags, const struct nftnl_expr *e)
127 {
128  switch(type) {
129  case NFTNL_OUTPUT_DEFAULT:
130  return nftnl_expr_osf_snprintf_default(buf, len, e);
131  case NFTNL_OUTPUT_XML:
132  case NFTNL_OUTPUT_JSON:
133  default:
134  break;
135  }
136  return -1;
137 }
138 
139 struct expr_ops expr_ops_osf = {
140  .name = "osf",
141  .alloc_len = sizeof(struct nftnl_expr_osf),
142  .max_attr = NFTA_OSF_MAX,
143  .set = nftnl_expr_osf_set,
144  .get = nftnl_expr_osf_get,
145  .parse = nftnl_expr_osf_parse,
146  .build = nftnl_expr_osf_build,
147  .snprintf = nftnl_expr_osf_snprintf,
148 };