| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* (C) 2012-2016 by Pablo Neira Ayuso <[email protected]> |
| 4 |
* (C) 2016 by Carlos Falgueras GarcĂa <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <libnftnl/udata.h> |
| 8 |
#include <udata.h> |
| 9 |
#include <utils.h> |
| 10 |
|
| 11 |
#include <stdlib.h> |
| 12 |
#include <stdint.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
EXPORT_SYMBOL(nftnl_udata_buf_alloc); |
| 16 |
struct nftnl_udata_buf *nftnl_udata_buf_alloc(uint32_t data_size) |
| 17 |
{ |
| 18 |
struct nftnl_udata_buf *buf; |
| 19 |
|
| 20 |
buf = malloc(sizeof(struct nftnl_udata_buf) + data_size); |
| 21 |
if (!buf) |
| 22 |
return NULL; |
| 23 |
buf->size = data_size; |
| 24 |
buf->end = buf->data; |
| 25 |
|
| 26 |
return buf; |
| 27 |
} |
| 28 |
|
| 29 |
EXPORT_SYMBOL(nftnl_udata_buf_free); |
| 30 |
void nftnl_udata_buf_free(const struct nftnl_udata_buf *buf) |
| 31 |
{ |
| 32 |
xfree(buf); |
| 33 |
} |
| 34 |
|
| 35 |
EXPORT_SYMBOL(nftnl_udata_buf_len); |
| 36 |
uint32_t nftnl_udata_buf_len(const struct nftnl_udata_buf *buf) |
| 37 |
{ |
| 38 |
return (uint32_t)(buf->end - buf->data); |
| 39 |
} |
| 40 |
|
| 41 |
static uint32_t nftnl_udata_buf_space(const struct nftnl_udata_buf *buf) |
| 42 |
{ |
| 43 |
return buf->size - nftnl_udata_buf_len(buf); |
| 44 |
} |
| 45 |
|
| 46 |
EXPORT_SYMBOL(nftnl_udata_buf_data); |
| 47 |
void *nftnl_udata_buf_data(const struct nftnl_udata_buf *buf) |
| 48 |
{ |
| 49 |
return (void *)buf->data; |
| 50 |
} |
| 51 |
|
| 52 |
EXPORT_SYMBOL(nftnl_udata_buf_put); |
| 53 |
void nftnl_udata_buf_put(struct nftnl_udata_buf *buf, const void *data, |
| 54 |
uint32_t len) |
| 55 |
{ |
| 56 |
memcpy(buf->data, data, len <= buf->size ? len : buf->size); |
| 57 |
buf->end = buf->data + len; |
| 58 |
} |
| 59 |
|
| 60 |
EXPORT_SYMBOL(nftnl_udata_start); |
| 61 |
struct nftnl_udata *nftnl_udata_start(const struct nftnl_udata_buf *buf) |
| 62 |
{ |
| 63 |
return (struct nftnl_udata *)buf->data; |
| 64 |
} |
| 65 |
|
| 66 |
EXPORT_SYMBOL(nftnl_udata_end); |
| 67 |
struct nftnl_udata *nftnl_udata_end(const struct nftnl_udata_buf *buf) |
| 68 |
{ |
| 69 |
return (struct nftnl_udata *)buf->end; |
| 70 |
} |
| 71 |
|
| 72 |
EXPORT_SYMBOL(nftnl_udata_put); |
| 73 |
bool nftnl_udata_put(struct nftnl_udata_buf *buf, uint8_t type, uint32_t len, |
| 74 |
const void *value) |
| 75 |
{ |
| 76 |
struct nftnl_udata *attr; |
| 77 |
|
| 78 |
if (len > UINT8_MAX || |
| 79 |
nftnl_udata_buf_space(buf) < len + sizeof(struct nftnl_udata)) |
| 80 |
return false; |
| 81 |
|
| 82 |
attr = (struct nftnl_udata *)buf->end; |
| 83 |
attr->len = len; |
| 84 |
attr->type = type; |
| 85 |
memcpy(attr->value, value, len); |
| 86 |
|
| 87 |
buf->end = (char *)nftnl_udata_next(attr); |
| 88 |
|
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
EXPORT_SYMBOL(nftnl_udata_put_strz); |
| 93 |
bool nftnl_udata_put_strz(struct nftnl_udata_buf *buf, uint8_t type, |
| 94 |
const char *strz) |
| 95 |
{ |
| 96 |
return nftnl_udata_put(buf, type, strlen(strz) + 1, strz); |
| 97 |
} |
| 98 |
|
| 99 |
EXPORT_SYMBOL(nftnl_udata_put_u32); |
| 100 |
bool nftnl_udata_put_u32(struct nftnl_udata_buf *buf, uint8_t type, |
| 101 |
uint32_t data) |
| 102 |
{ |
| 103 |
return nftnl_udata_put(buf, type, sizeof(data), &data); |
| 104 |
} |
| 105 |
|
| 106 |
EXPORT_SYMBOL(nftnl_udata_type); |
| 107 |
uint8_t nftnl_udata_type(const struct nftnl_udata *attr) |
| 108 |
{ |
| 109 |
return attr->type; |
| 110 |
} |
| 111 |
|
| 112 |
EXPORT_SYMBOL(nftnl_udata_len); |
| 113 |
uint8_t nftnl_udata_len(const struct nftnl_udata *attr) |
| 114 |
{ |
| 115 |
return attr->len; |
| 116 |
} |
| 117 |
|
| 118 |
EXPORT_SYMBOL(nftnl_udata_get); |
| 119 |
void *nftnl_udata_get(const struct nftnl_udata *attr) |
| 120 |
{ |
| 121 |
return (void *)attr->value; |
| 122 |
} |
| 123 |
|
| 124 |
EXPORT_SYMBOL(nftnl_udata_get_u32); |
| 125 |
uint32_t nftnl_udata_get_u32(const struct nftnl_udata *attr) |
| 126 |
{ |
| 127 |
uint32_t data; |
| 128 |
|
| 129 |
memcpy(&data, attr->value, sizeof(data)); |
| 130 |
|
| 131 |
return data; |
| 132 |
} |
| 133 |
|
| 134 |
EXPORT_SYMBOL(nftnl_udata_next); |
| 135 |
struct nftnl_udata *nftnl_udata_next(const struct nftnl_udata *attr) |
| 136 |
{ |
| 137 |
return (struct nftnl_udata *)&attr->value[attr->len]; |
| 138 |
} |
| 139 |
|
| 140 |
EXPORT_SYMBOL(nftnl_udata_parse); |
| 141 |
int nftnl_udata_parse(const void *data, uint32_t data_len, nftnl_udata_cb_t cb, |
| 142 |
void *cb_data) |
| 143 |
{ |
| 144 |
int ret = 0; |
| 145 |
const struct nftnl_udata *attr; |
| 146 |
|
| 147 |
nftnl_udata_for_each_data(data, data_len, attr) { |
| 148 |
ret = cb(attr, cb_data); |
| 149 |
if (ret < 0) |
| 150 |
return ret; |
| 151 |
} |
| 152 |
|
| 153 |
return ret; |
| 154 |
} |
| 155 |
|
| 156 |
EXPORT_SYMBOL(nftnl_udata_nest_start); |
| 157 |
struct nftnl_udata *nftnl_udata_nest_start(struct nftnl_udata_buf *buf, |
| 158 |
uint8_t type) |
| 159 |
{ |
| 160 |
struct nftnl_udata *ud = nftnl_udata_end(buf); |
| 161 |
|
| 162 |
nftnl_udata_put(buf, type, 0, NULL); |
| 163 |
|
| 164 |
return ud; |
| 165 |
} |
| 166 |
|
| 167 |
EXPORT_SYMBOL(nftnl_udata_nest_end); |
| 168 |
void nftnl_udata_nest_end(struct nftnl_udata_buf *buf, struct nftnl_udata *ud) |
| 169 |
{ |
| 170 |
ud->len = buf->end - (char *)ud->value; |
| 171 |
} |