1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/*
* lib/route/link/inet.c AF_INET link operations
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* Copyright (c) 2010 Thomas Graf <tgraf@suug.ch>
*/
/**
* @ingroup link_API
* @defgroup link_inet IPv4 Link Module
* @brief Implementation of IPv4 specific link attributes
*
*
*
* @par Example: Reading the value of IPV4_DEVCONF_FORWARDING
* @code
* struct nl_cache *cache;
* struct rtnl_link *link;
* uint32_t value;
*
* // Allocate a link cache
* rtnl_link_alloc_cache(sock, AF_UNSPEC, &cache);
*
* // Search for the link we wish to see the value from
* link = rtnl_link_get_by_name(cache, "eth0");
*
* // Read the value of the setting IPV4_DEVCONF_FORWARDING
* if (rtnl_link_inet_get_conf(link, IPV4_DEVCONF_FORWARDING, &value) < 0)
* // Error: Unable to read config setting
*
* printf("forwarding is %s\n", value ? "enabled" : "disabled");
* @endcode
*
* @par Example: Changing the value of IPV4_DEVCONF_FOWARDING
* @code
* //
* // ... Continueing from the previous example ...
* //
*
* struct rtnl_link *new;
*
* // Allocate a new link to store the changes we wish to make.
* new = rtnl_link_alloc();
*
* // Set IPV4_DEVCONF_FORWARDING to '1'
* rtnl_link_inet_set_conf(new, IPV4_DEVCONF_FORWARDING, 1);
*
* // Send the change request to the kernel.
* rtnl_link_change(sock, link, new, 0);
* @endcode
*
* @{
*/
#include <netlink-private/netlink.h>
#include <netlink/netlink.h>
#include <netlink/attr.h>
#include <netlink/route/rtnl.h>
#include <netlink/route/link/inet.h>
#include <netlink-private/route/link/api.h>
/** @cond SKIP */
struct inet_data
{
uint8_t i_confset[IPV4_DEVCONF_MAX];
uint32_t i_conf[IPV4_DEVCONF_MAX];
};
/** @endcond */
static void *inet_alloc(struct rtnl_link *link)
{
return calloc(1, sizeof(struct inet_data));
}
static void *inet_clone(struct rtnl_link *link, void *data)
{
struct inet_data *id;
if ((id = inet_alloc(link)))
memcpy(id, data, sizeof(*id));
return id;
}
static void inet_free(struct rtnl_link *link, void *data)
{
free(data);
}
static struct nla_policy inet_policy[IFLA_INET6_MAX+1] = {
[IFLA_INET_CONF] = { .minlen = 4 },
};
static int inet_parse_af(struct rtnl_link *link, struct nlattr *attr, void *data)
{
struct inet_data *id = data;
struct nlattr *tb[IFLA_INET_MAX+1];
int err;
err = nla_parse_nested(tb, IFLA_INET_MAX, attr, inet_policy);
if (err < 0)
return err;
if (tb[IFLA_INET_CONF] && nla_len(tb[IFLA_INET_CONF]) % 4)
return -EINVAL;
if (tb[IFLA_INET_CONF]) {
int i;
int len = min_t(int, IPV4_DEVCONF_MAX, nla_len(tb[IFLA_INET_CONF]) / 4);
for (i = 0; i < len; i++)
id->i_confset[i] = 1;
nla_memcpy(&id->i_conf, tb[IFLA_INET_CONF], sizeof(id->i_conf));
}
return 0;
}
static int inet_fill_af(struct rtnl_link *link, struct nl_msg *msg, void *data)
{
struct inet_data *id = data;
struct nlattr *nla;
int i;
if (!(nla = nla_nest_start(msg, IFLA_INET_CONF)))
return -NLE_MSGSIZE;
for (i = 0; i < IPV4_DEVCONF_MAX; i++)
if (id->i_confset[i])
NLA_PUT_U32(msg, i+1, id->i_conf[i]);
nla_nest_end(msg, nla);
return 0;
nla_put_failure:
return -NLE_MSGSIZE;
}
static const struct trans_tbl inet_devconf[] = {
__ADD(IPV4_DEVCONF_FORWARDING, forwarding),
__ADD(IPV4_DEVCONF_MC_FORWARDING, mc_forwarding),
__ADD(IPV4_DEVCONF_PROXY_ARP, proxy_arp),
__ADD(IPV4_DEVCONF_ACCEPT_REDIRECTS, accept_redirects),
__ADD(IPV4_DEVCONF_SECURE_REDIRECTS, secure_redirects),
__ADD(IPV4_DEVCONF_SEND_REDIRECTS, send_redirects),
__ADD(IPV4_DEVCONF_SHARED_MEDIA, shared_media),
__ADD(IPV4_DEVCONF_RP_FILTER, rp_filter),
__ADD(IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE, accept_source_route),
__ADD(IPV4_DEVCONF_BOOTP_RELAY, bootp_relay),
__ADD(IPV4_DEVCONF_LOG_MARTIANS, log_martians),
__ADD(IPV4_DEVCONF_TAG, tag),
__ADD(IPV4_DEVCONF_ARPFILTER, arpfilter),
__ADD(IPV4_DEVCONF_MEDIUM_ID, medium_id),
__ADD(IPV4_DEVCONF_NOXFRM, noxfrm),
__ADD(IPV4_DEVCONF_NOPOLICY, nopolicy),
__ADD(IPV4_DEVCONF_FORCE_IGMP_VERSION, force_igmp_version),
__ADD(IPV4_DEVCONF_ARP_ANNOUNCE, arp_announce),
__ADD(IPV4_DEVCONF_ARP_IGNORE, arp_ignore),
__ADD(IPV4_DEVCONF_PROMOTE_SECONDARIES, promote_secondaries),
__ADD(IPV4_DEVCONF_ARP_ACCEPT, arp_accept),
__ADD(IPV4_DEVCONF_ARP_NOTIFY, arp_notify),
__ADD(IPV4_DEVCONF_ACCEPT_LOCAL, accept_local),
__ADD(IPV4_DEVCONF_SRC_VMARK, src_vmark),
__ADD(IPV4_DEVCONF_PROXY_ARP_PVLAN, proxy_arp_pvlan),
__ADD(IPV4_DEVCONF_ROUTE_LOCALNET, route_localnet),
__ADD(IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL, igmpv2_unsolicited_report_interval),
__ADD(IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL, igmpv3_unsolicited_report_interval),
};
const char *rtnl_link_inet_devconf2str(int type, char *buf, size_t len)
{
return __type2str(type, buf, len, inet_devconf,
ARRAY_SIZE(inet_devconf));
}
int rtnl_link_inet_str2devconf(const char *name)
{
return __str2type(name, inet_devconf, ARRAY_SIZE(inet_devconf));
}
static void inet_dump_details(struct rtnl_link *link,
struct nl_dump_params *p, void *data)
{
struct inet_data *id = data;
char buf[64];
int i, n = 0;
nl_dump_line(p, " ipv4 devconf:\n");
nl_dump_line(p, " ");
for (i = 0; i < IPV4_DEVCONF_MAX; i++) {
nl_dump_line(p, "%-19s %3u",
rtnl_link_inet_devconf2str(i+1, buf, sizeof(buf)),
id->i_confset[i] ? id->i_conf[i] : 0);
if (++n == 3) {
nl_dump(p, "\n");
nl_dump_line(p, " ");
n = 0;
} else
nl_dump(p, " ");
}
if (n != 0)
nl_dump(p, "\n");
}
static struct rtnl_link_af_ops inet_ops = {
.ao_family = AF_INET,
.ao_alloc = &inet_alloc,
.ao_clone = &inet_clone,
.ao_free = &inet_free,
.ao_parse_af = &inet_parse_af,
.ao_fill_af = &inet_fill_af,
.ao_dump[NL_DUMP_DETAILS] = &inet_dump_details,
};
/**
* Get value of a ipv4 link configuration setting
* @arg link Link object
* @arg cfgid Configuration identifier
* @arg res Result pointer
*
* Stores the value of the specified configuration setting in the provided
* result pointer.
*
* @return 0 on success or a negative error code.
* @return -NLE_RANGE cfgid is out of range, 1..IPV4_DEVCONF_MAX
* @return -NLE_NOATTR configuration setting not available
* @return -NLE_INVAL cfgid not set. If the link was received via netlink,
* it means that the cfgid is not supported.
*/
int rtnl_link_inet_get_conf(struct rtnl_link *link, const unsigned int cfgid,
uint32_t *res)
{
struct inet_data *id;
if (cfgid == 0 || cfgid > IPV4_DEVCONF_MAX)
return -NLE_RANGE;
if (!(id = rtnl_link_af_data(link, &inet_ops)))
return -NLE_NOATTR;
if (!id->i_confset[cfgid - 1])
return -NLE_INVAL;
*res = id->i_conf[cfgid - 1];
return 0;
}
/**
* Change value of a ipv4 link configuration setting
* @arg link Link object
* @arg cfgid Configuration identifier
* @arg value New value
*
* Changes the value in the per link ipv4 configuration array.
*
* @return 0 on success or a negative error code.
* @return -NLE_RANGE cfgid is out of range, 1..IPV4_DEVCONF_MAX
* @return -NLE_NOMEM memory allocation failed
*/
int rtnl_link_inet_set_conf(struct rtnl_link *link, const unsigned int cfgid,
uint32_t value)
{
struct inet_data *id;
if (!(id = rtnl_link_af_alloc(link, &inet_ops)))
return -NLE_NOMEM;
if (cfgid == 0 || cfgid > IPV4_DEVCONF_MAX)
return -NLE_RANGE;
id->i_confset[cfgid - 1] = 1;
id->i_conf[cfgid - 1] = value;
return 0;
}
static void __init inet_init(void)
{
rtnl_link_af_register(&inet_ops);
}
static void __exit inet_exit(void)
{
rtnl_link_af_unregister(&inet_ops);
}
/** @} */
|