14#include "nl-default.h"
16#include <linux/netconf.h>
17#include <linux/socket.h>
19#include <netlink/netlink.h>
20#include <netlink/utils.h>
21#include <netlink/route/netconf.h>
22#include <netlink/hashtable.h>
25#include "nl-priv-dynamic-core/nl-core.h"
26#include "nl-priv-dynamic-core/cache-api.h"
27#include "nl-priv-dynamic-core/object-api.h"
30#define NETCONF_ATTR_FAMILY 0x0001
31#define NETCONF_ATTR_IFINDEX 0x0002
32#define NETCONF_ATTR_RP_FILTER 0x0004
33#define NETCONF_ATTR_FWDING 0x0008
34#define NETCONF_ATTR_MC_FWDING 0x0010
35#define NETCONF_ATTR_PROXY_NEIGH 0x0020
36#define NETCONF_ATTR_IGNORE_RT_LINKDWN 0x0040
37#define NETCONF_ATTR_INPUT 0x0080
49 int ignore_routes_linkdown;
53static struct nl_cache_ops rtnl_netconf_ops;
54static struct nl_object_ops netconf_obj_ops;
57static struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = {
58 [NETCONFA_IFINDEX] = { .
type = NLA_S32 },
59 [NETCONFA_FORWARDING] = { .type = NLA_S32 },
60 [NETCONFA_MC_FORWARDING] = { .type = NLA_S32 },
61 [NETCONFA_RP_FILTER] = { .type = NLA_S32 },
62 [NETCONFA_PROXY_NEIGH] = { .type = NLA_S32 },
63 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .type = NLA_S32 },
66static struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
67 [NETCONFA_IFINDEX] = { .
type = NLA_S32 },
68 [NETCONFA_FORWARDING] = { .type = NLA_S32 },
69 [NETCONFA_MC_FORWARDING] = { .type = NLA_S32 },
70 [NETCONFA_PROXY_NEIGH] = { .type = NLA_S32 },
71 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .type = NLA_S32 },
74static struct nla_policy devconf_mpls_policy[NETCONFA_MAX+1] = {
75 [NETCONFA_IFINDEX] = { .
type = NLA_S32 },
76 [NETCONFA_INPUT] = { .type = NLA_S32 },
79static struct rtnl_netconf *rtnl_netconf_alloc(
void)
84static int netconf_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
85 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
87 struct nlattr *tb[NETCONFA_MAX+1], *attr;
88 struct rtnl_netconf *nc;
89 struct netconfmsg *ncm;
93 switch (ncm->ncm_family) {
95 err =
nlmsg_parse(nlh,
sizeof(*ncm), tb, NETCONFA_MAX,
101 err =
nlmsg_parse(nlh,
sizeof(*ncm), tb, NETCONFA_MAX,
102 devconf_ipv6_policy);
107 err =
nlmsg_parse(nlh,
sizeof(*ncm), tb, NETCONFA_MAX,
108 devconf_mpls_policy);
113 printf(
"unexpected netconf family: %d\n", ncm->ncm_family);
117 if (!tb[NETCONFA_IFINDEX])
120 nc = rtnl_netconf_alloc();
124 nc->ce_msgtype = nlh->nlmsg_type;
125 nc->family = ncm->ncm_family;
128 nc->ce_mask = NETCONF_ATTR_FAMILY | NETCONF_ATTR_IFINDEX;
131 if (tb[NETCONFA_RP_FILTER]) {
132 attr = tb[NETCONFA_RP_FILTER];
134 nc->ce_mask |= NETCONF_ATTR_RP_FILTER;
137 if (tb[NETCONFA_FORWARDING]) {
138 attr = tb[NETCONFA_FORWARDING];
140 nc->ce_mask |= NETCONF_ATTR_FWDING;
143 if (tb[NETCONFA_MC_FORWARDING]) {
144 attr = tb[NETCONFA_MC_FORWARDING];
146 nc->ce_mask |= NETCONF_ATTR_MC_FWDING;
149 if (tb[NETCONFA_PROXY_NEIGH]) {
150 attr = tb[NETCONFA_PROXY_NEIGH];
152 nc->ce_mask |= NETCONF_ATTR_PROXY_NEIGH;
155 if (tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]) {
156 attr = tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN];
158 nc->ce_mask |= NETCONF_ATTR_IGNORE_RT_LINKDWN;
161 if (tb[NETCONFA_INPUT]) {
162 attr = tb[NETCONFA_INPUT];
164 nc->ce_mask |= NETCONF_ATTR_INPUT;
167 err = pp->pp_cb((
struct nl_object *) nc, pp);
169 rtnl_netconf_put(nc);
173static int netconf_request_update(
struct nl_cache *cache,
struct nl_sock *sk)
175 struct netconfmsg nc = {
176 .ncm_family = cache->c_iarg1,
179 return nl_send_simple(sk, RTM_GETNETCONF, NLM_F_DUMP, &nc,
sizeof(nc));
182static void netconf_dump_line(
struct nl_object *obj,
struct nl_dump_params *p)
184 struct rtnl_netconf *nc = (
struct rtnl_netconf *) obj;
185 struct nl_cache *link_cache;
202 switch(nc->ifindex) {
203 case NETCONFA_IFINDEX_ALL:
206 case NETCONFA_IFINDEX_DEFAULT:
215 nl_cache_put(link_cache);
217 nl_dump(p,
"dev %d ", nc->ifindex);
220 if (nc->ce_mask & NETCONF_ATTR_FWDING) {
222 nc->forwarding ?
"on" :
"off");
225 if (nc->ce_mask & NETCONF_ATTR_RP_FILTER) {
226 if (nc->rp_filter == 0)
228 else if (nc->rp_filter == 1)
229 nl_dump(p,
"rp_filter strict ");
230 else if (nc->rp_filter == 2)
231 nl_dump(p,
"rp_filter loose ");
233 nl_dump(p,
"rp_filter unknown-mode ");
236 if (nc->ce_mask & NETCONF_ATTR_MC_FWDING) {
237 nl_dump(p,
"mc_forwarding %s ",
238 nc->mc_forwarding ?
"on" :
"off");
241 if (nc->ce_mask & NETCONF_ATTR_PROXY_NEIGH)
242 nl_dump(p,
"proxy_neigh %d ", nc->proxy_neigh);
244 if (nc->ce_mask & NETCONF_ATTR_IGNORE_RT_LINKDWN) {
245 nl_dump(p,
"ignore_routes_with_linkdown %s ",
246 nc->ignore_routes_linkdown ?
"on" :
"off");
249 if (nc->ce_mask & NETCONF_ATTR_INPUT)
250 nl_dump(p,
"input %s ", nc->input ?
"on" :
"off");
255static const struct trans_tbl netconf_attrs[] = {
256 __ADD(NETCONF_ATTR_FAMILY, family),
257 __ADD(NETCONF_ATTR_IFINDEX, ifindex),
258 __ADD(NETCONF_ATTR_RP_FILTER, rp_filter),
259 __ADD(NETCONF_ATTR_FWDING, forwarding),
260 __ADD(NETCONF_ATTR_MC_FWDING, mc_forwarding),
261 __ADD(NETCONF_ATTR_PROXY_NEIGH, proxy_neigh),
262 __ADD(NETCONF_ATTR_IGNORE_RT_LINKDWN, ignore_routes_with_linkdown),
263 __ADD(NETCONF_ATTR_INPUT, input),
266static char *netconf_attrs2str(
int attrs,
char *buf,
size_t len)
268 return __flags2str(attrs, buf, len, netconf_attrs,
269 ARRAY_SIZE(netconf_attrs));
272static void netconf_keygen(
struct nl_object *obj, uint32_t *hashkey,
275 struct rtnl_netconf *nc = (
struct rtnl_netconf *) obj;
276 unsigned int nckey_sz;
282 nckey_sz =
sizeof(nckey);
283 nckey.nc_family = nc->family;
284 nckey.nc_index = nc->ifindex;
286 *hashkey = nl_hash(&nckey, nckey_sz, 0) % table_sz;
288 NL_DBG(5,
"netconf %p key (dev %d fam %d) keysz %d, hash 0x%x\n",
289 nc, nckey.nc_index, nckey.nc_family, nckey_sz, *hashkey);
292static uint64_t netconf_compare(
struct nl_object *_a,
struct nl_object *_b,
293 uint64_t attrs,
int flags)
295 struct rtnl_netconf *a = (
struct rtnl_netconf *) _a;
296 struct rtnl_netconf *b = (
struct rtnl_netconf *) _b;
299#define _DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ATTR, a, b, EXPR)
300 diff |= _DIFF(NETCONF_ATTR_FAMILY, a->family != b->family);
301 diff |= _DIFF(NETCONF_ATTR_IFINDEX, a->ifindex != b->ifindex);
302 diff |= _DIFF(NETCONF_ATTR_RP_FILTER, a->rp_filter != b->rp_filter);
303 diff |= _DIFF(NETCONF_ATTR_FWDING, a->forwarding != b->forwarding);
304 diff |= _DIFF(NETCONF_ATTR_MC_FWDING,
305 a->mc_forwarding != b->mc_forwarding);
306 diff |= _DIFF(NETCONF_ATTR_PROXY_NEIGH,
307 a->proxy_neigh != b->proxy_neigh);
308 diff |= _DIFF(NETCONF_ATTR_IGNORE_RT_LINKDWN,
309 a->ignore_routes_linkdown != b->ignore_routes_linkdown);
310 diff |= _DIFF(NETCONF_ATTR_INPUT, a->input != b->input);
316static int netconf_update(
struct nl_object *old_obj,
struct nl_object *new_obj)
318 struct rtnl_netconf *new_nc = (
struct rtnl_netconf *) new_obj;
319 struct rtnl_netconf *old_nc = (
struct rtnl_netconf *) old_obj;
320 int action = new_obj->ce_msgtype;
324 if (new_nc->family != old_nc->family ||
325 new_nc->ifindex != old_nc->ifindex)
326 return -NLE_OPNOTSUPP;
328 if (new_nc->ce_mask & NETCONF_ATTR_RP_FILTER)
329 old_nc->rp_filter = new_nc->rp_filter;
330 if (new_nc->ce_mask & NETCONF_ATTR_FWDING)
331 old_nc->forwarding = new_nc->forwarding;
332 if (new_nc->ce_mask & NETCONF_ATTR_MC_FWDING)
333 old_nc->mc_forwarding = new_nc->mc_forwarding;
334 if (new_nc->ce_mask & NETCONF_ATTR_PROXY_NEIGH)
335 old_nc->proxy_neigh = new_nc->proxy_neigh;
336 if (new_nc->ce_mask & NETCONF_ATTR_IGNORE_RT_LINKDWN)
337 old_nc->ignore_routes_linkdown = new_nc->ignore_routes_linkdown;
341 return -NLE_OPNOTSUPP;
352int rtnl_netconf_alloc_cache(
struct nl_sock *sk,
struct nl_cache **result)
375 struct rtnl_netconf *nc;
377 if (!ifindex || !family || cache->c_ops != &rtnl_netconf_ops)
380 nl_list_for_each_entry(nc, &cache->c_items, ce_list) {
381 if (nc->ifindex == ifindex &&
382 nc->family == family) {
391void rtnl_netconf_put(
struct rtnl_netconf *nc)
441int rtnl_netconf_get_family(
struct rtnl_netconf *nc,
int *val)
445 if (!(nc->ce_mask & NETCONF_ATTR_FAMILY))
446 return -NLE_MISSING_ATTR;
451int rtnl_netconf_get_ifindex(
struct rtnl_netconf *nc,
int *val)
455 if (!(nc->ce_mask & NETCONF_ATTR_IFINDEX))
456 return -NLE_MISSING_ATTR;
461int rtnl_netconf_get_forwarding(
struct rtnl_netconf *nc,
int *val)
465 if (!(nc->ce_mask & NETCONF_ATTR_FWDING))
466 return -NLE_MISSING_ATTR;
468 *val = nc->forwarding;
471int rtnl_netconf_get_mc_forwarding(
struct rtnl_netconf *nc,
int *val)
475 if (!(nc->ce_mask & NETCONF_ATTR_MC_FWDING))
476 return -NLE_MISSING_ATTR;
478 *val = nc->mc_forwarding;
481int rtnl_netconf_get_rp_filter(
struct rtnl_netconf *nc,
int *val)
485 if (!(nc->ce_mask & NETCONF_ATTR_RP_FILTER))
486 return -NLE_MISSING_ATTR;
488 *val = nc->rp_filter;
491int rtnl_netconf_get_proxy_neigh(
struct rtnl_netconf *nc,
int *val)
495 if (!(nc->ce_mask & NETCONF_ATTR_PROXY_NEIGH))
496 return -NLE_MISSING_ATTR;
498 *val = nc->proxy_neigh;
501int rtnl_netconf_get_ignore_routes_linkdown(
struct rtnl_netconf *nc,
int *val)
505 if (!(nc->ce_mask & NETCONF_ATTR_IGNORE_RT_LINKDWN))
506 return -NLE_MISSING_ATTR;
508 *val = nc->ignore_routes_linkdown;
511int rtnl_netconf_get_input(
struct rtnl_netconf *nc,
int *val)
515 if (!(nc->ce_mask & NETCONF_ATTR_INPUT))
516 return -NLE_MISSING_ATTR;
525static struct nl_object_ops netconf_obj_ops = {
526 .oo_name =
"route/netconf",
527 .oo_size =
sizeof(
struct rtnl_netconf),
532 .oo_compare = netconf_compare,
533 .oo_keygen = netconf_keygen,
534 .oo_update = netconf_update,
535 .oo_attrs2str = netconf_attrs2str,
536 .oo_id_attrs = (NETCONF_ATTR_FAMILY |
537 NETCONF_ATTR_IFINDEX)
540static struct nl_af_group netconf_groups[] = {
541 { AF_INET, RTNLGRP_IPV4_NETCONF },
542 { AF_INET6, RTNLGRP_IPV6_NETCONF },
543 { AF_MPLS, RTNLGRP_MPLS_NETCONF },
544 { END_OF_GROUP_LIST },
547static struct nl_cache_ops rtnl_netconf_ops = {
548 .co_name =
"route/netconf",
549 .co_hdrsize =
sizeof(
struct netconfmsg),
551 { RTM_NEWNETCONF, NL_ACT_NEW,
"new" },
552 { RTM_DELNETCONF, NL_ACT_DEL,
"del" },
553 { RTM_GETNETCONF, NL_ACT_GET,
"get" },
554 END_OF_MSGTYPES_LIST,
556 .co_protocol = NETLINK_ROUTE,
557 .co_groups = netconf_groups,
558 .co_request_update = netconf_request_update,
559 .co_msg_parser = netconf_msg_parser,
560 .co_obj_ops = &netconf_obj_ops,
563static void _nl_init netconf_init(
void)
568static void _nl_exit netconf_exit(
void)
int32_t nla_get_s32(const struct nlattr *nla)
Return payload of 32 bit signed integer attribute.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock, struct nl_cache **result)
Allocate new cache and fill it.
char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst, size_t len)
Translate interface index to corresponding link name.
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
struct rtnl_netconf * rtnl_netconf_get_by_idx(struct nl_cache *cache, int family, int ifindex)
Search netconf in cache.
struct rtnl_netconf * rtnl_netconf_get_default(struct nl_cache *cache, int family)
Search netconf in cache.
struct rtnl_netconf * rtnl_netconf_get_all(struct nl_cache *cache, int family)
Search netconf in cache.
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, size_t size)
Construct and transmit a Netlink message.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Attribute validation policy.
uint16_t type
Type of attribute or NLA_UNSPEC.