14#include <netlink-private/netlink.h>
15#include <netlink/netlink.h>
16#include <netlink/utils.h>
17#include <netlink/route/netconf.h>
18#include <linux/netconf.h>
19#include <linux/socket.h>
20#include <netlink/hashtable.h>
23#define NETCONF_ATTR_FAMILY 0x0001
24#define NETCONF_ATTR_IFINDEX 0x0002
25#define NETCONF_ATTR_RP_FILTER 0x0004
26#define NETCONF_ATTR_FWDING 0x0008
27#define NETCONF_ATTR_MC_FWDING 0x0010
28#define NETCONF_ATTR_PROXY_NEIGH 0x0020
29#define NETCONF_ATTR_IGNORE_RT_LINKDWN 0x0040
30#define NETCONF_ATTR_INPUT 0x0080
42 int ignore_routes_linkdown;
46static struct nl_cache_ops rtnl_netconf_ops;
47static struct nl_object_ops netconf_obj_ops;
50static struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = {
51 [NETCONFA_IFINDEX] = { .
type = NLA_S32 },
52 [NETCONFA_FORWARDING] = { .type = NLA_S32 },
53 [NETCONFA_MC_FORWARDING] = { .type = NLA_S32 },
54 [NETCONFA_RP_FILTER] = { .type = NLA_S32 },
55 [NETCONFA_PROXY_NEIGH] = { .type = NLA_S32 },
56 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .type = NLA_S32 },
59static struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
60 [NETCONFA_IFINDEX] = { .
type = NLA_S32 },
61 [NETCONFA_FORWARDING] = { .type = NLA_S32 },
62 [NETCONFA_MC_FORWARDING] = { .type = NLA_S32 },
63 [NETCONFA_PROXY_NEIGH] = { .type = NLA_S32 },
64 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .type = NLA_S32 },
67static struct nla_policy devconf_mpls_policy[NETCONFA_MAX+1] = {
68 [NETCONFA_IFINDEX] = { .
type = NLA_S32 },
69 [NETCONFA_INPUT] = { .type = NLA_S32 },
72static struct rtnl_netconf *rtnl_netconf_alloc(
void)
77static int netconf_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
78 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
80 struct nlattr *tb[NETCONFA_MAX+1], *attr;
81 struct rtnl_netconf *nc;
82 struct netconfmsg *ncm;
86 switch (ncm->ncm_family) {
88 err =
nlmsg_parse(nlh,
sizeof(*ncm), tb, NETCONFA_MAX,
94 err =
nlmsg_parse(nlh,
sizeof(*ncm), tb, NETCONFA_MAX,
100 err =
nlmsg_parse(nlh,
sizeof(*ncm), tb, NETCONFA_MAX,
101 devconf_mpls_policy);
106 printf(
"unexpected netconf family: %d\n", ncm->ncm_family);
110 if (!tb[NETCONFA_IFINDEX])
113 nc = rtnl_netconf_alloc();
117 nc->ce_msgtype = nlh->nlmsg_type;
118 nc->family = ncm->ncm_family;
121 nc->ce_mask = NETCONF_ATTR_FAMILY | NETCONF_ATTR_IFINDEX;
124 if (tb[NETCONFA_RP_FILTER]) {
125 attr = tb[NETCONFA_RP_FILTER];
127 nc->ce_mask |= NETCONF_ATTR_RP_FILTER;
130 if (tb[NETCONFA_FORWARDING]) {
131 attr = tb[NETCONFA_FORWARDING];
133 nc->ce_mask |= NETCONF_ATTR_FWDING;
136 if (tb[NETCONFA_MC_FORWARDING]) {
137 attr = tb[NETCONFA_MC_FORWARDING];
139 nc->ce_mask |= NETCONF_ATTR_MC_FWDING;
142 if (tb[NETCONFA_PROXY_NEIGH]) {
143 attr = tb[NETCONFA_PROXY_NEIGH];
145 nc->ce_mask |= NETCONF_ATTR_PROXY_NEIGH;
148 if (tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]) {
149 attr = tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN];
151 nc->ce_mask |= NETCONF_ATTR_IGNORE_RT_LINKDWN;
154 if (tb[NETCONFA_INPUT]) {
155 attr = tb[NETCONFA_INPUT];
157 nc->ce_mask |= NETCONF_ATTR_INPUT;
160 err = pp->pp_cb((
struct nl_object *) nc, pp);
162 rtnl_netconf_put(nc);
166static int netconf_request_update(
struct nl_cache *cache,
struct nl_sock *sk)
168 struct netconfmsg nc = {
169 .ncm_family = cache->c_iarg1,
172 return nl_send_simple(sk, RTM_GETNETCONF, NLM_F_DUMP, &nc,
sizeof(nc));
175static void netconf_dump_line(
struct nl_object *obj,
struct nl_dump_params *p)
177 struct rtnl_netconf *nc = (
struct rtnl_netconf *) obj;
178 struct nl_cache *link_cache;
195 switch(nc->ifindex) {
196 case NETCONFA_IFINDEX_ALL:
199 case NETCONFA_IFINDEX_DEFAULT:
208 nl_cache_put(link_cache);
210 nl_dump(p,
"dev %d ", nc->ifindex);
213 if (nc->ce_mask & NETCONF_ATTR_FWDING) {
215 nc->forwarding ?
"on" :
"off");
218 if (nc->ce_mask & NETCONF_ATTR_RP_FILTER) {
219 if (nc->rp_filter == 0)
221 else if (nc->rp_filter == 1)
222 nl_dump(p,
"rp_filter strict ");
223 else if (nc->rp_filter == 2)
224 nl_dump(p,
"rp_filter loose ");
226 nl_dump(p,
"rp_filter unknown-mode ");
229 if (nc->ce_mask & NETCONF_ATTR_MC_FWDING) {
230 nl_dump(p,
"mc_forwarding %s ",
231 nc->mc_forwarding ?
"on" :
"off");
234 if (nc->ce_mask & NETCONF_ATTR_PROXY_NEIGH)
235 nl_dump(p,
"proxy_neigh %d ", nc->proxy_neigh);
237 if (nc->ce_mask & NETCONF_ATTR_IGNORE_RT_LINKDWN) {
238 nl_dump(p,
"ignore_routes_with_linkdown %s ",
239 nc->ignore_routes_linkdown ?
"on" :
"off");
242 if (nc->ce_mask & NETCONF_ATTR_INPUT)
243 nl_dump(p,
"input %s ", nc->input ?
"on" :
"off");
248static const struct trans_tbl netconf_attrs[] = {
249 __ADD(NETCONF_ATTR_FAMILY, family),
250 __ADD(NETCONF_ATTR_IFINDEX, ifindex),
251 __ADD(NETCONF_ATTR_RP_FILTER, rp_filter),
252 __ADD(NETCONF_ATTR_FWDING, forwarding),
253 __ADD(NETCONF_ATTR_MC_FWDING, mc_forwarding),
254 __ADD(NETCONF_ATTR_PROXY_NEIGH, proxy_neigh),
255 __ADD(NETCONF_ATTR_IGNORE_RT_LINKDWN, ignore_routes_with_linkdown),
256 __ADD(NETCONF_ATTR_INPUT, input),
259static char *netconf_attrs2str(
int attrs,
char *buf,
size_t len)
261 return __flags2str(attrs, buf, len, netconf_attrs,
262 ARRAY_SIZE(netconf_attrs));
265static void netconf_keygen(
struct nl_object *obj, uint32_t *hashkey,
268 struct rtnl_netconf *nc = (
struct rtnl_netconf *) obj;
269 unsigned int nckey_sz;
273 } __attribute__((packed)) nckey;
275 nckey_sz =
sizeof(nckey);
276 nckey.nc_family = nc->family;
277 nckey.nc_index = nc->ifindex;
279 *hashkey = nl_hash(&nckey, nckey_sz, 0) % table_sz;
281 NL_DBG(5,
"netconf %p key (dev %d fam %d) keysz %d, hash 0x%x\n",
282 nc, nckey.nc_index, nckey.nc_family, nckey_sz, *hashkey);
285static uint64_t netconf_compare(
struct nl_object *_a,
struct nl_object *_b,
286 uint64_t attrs,
int flags)
288 struct rtnl_netconf *a = (
struct rtnl_netconf *) _a;
289 struct rtnl_netconf *b = (
struct rtnl_netconf *) _b;
292#define NETCONF_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, NETCONF_ATTR_##ATTR, a, b, EXPR)
294 diff |= NETCONF_DIFF(FAMILY, a->family != b->family);
295 diff |= NETCONF_DIFF(IFINDEX, a->ifindex != b->ifindex);
296 diff |= NETCONF_DIFF(RP_FILTER, a->rp_filter != b->rp_filter);
297 diff |= NETCONF_DIFF(FWDING, a->forwarding != b->forwarding);
298 diff |= NETCONF_DIFF(MC_FWDING, a->mc_forwarding != b->mc_forwarding);
299 diff |= NETCONF_DIFF(PROXY_NEIGH, a->proxy_neigh != b->proxy_neigh);
300 diff |= NETCONF_DIFF(IGNORE_RT_LINKDWN,
301 a->ignore_routes_linkdown != b->ignore_routes_linkdown);
302 diff |= NETCONF_DIFF(INPUT, a->input != b->input);
309static int netconf_update(
struct nl_object *old_obj,
struct nl_object *new_obj)
311 struct rtnl_netconf *new_nc = (
struct rtnl_netconf *) new_obj;
312 struct rtnl_netconf *old_nc = (
struct rtnl_netconf *) old_obj;
313 int action = new_obj->ce_msgtype;
317 if (new_nc->family != old_nc->family ||
318 new_nc->ifindex != old_nc->ifindex)
319 return -NLE_OPNOTSUPP;
321 if (new_nc->ce_mask & NETCONF_ATTR_RP_FILTER)
322 old_nc->rp_filter = new_nc->rp_filter;
323 if (new_nc->ce_mask & NETCONF_ATTR_FWDING)
324 old_nc->forwarding = new_nc->forwarding;
325 if (new_nc->ce_mask & NETCONF_ATTR_MC_FWDING)
326 old_nc->mc_forwarding = new_nc->mc_forwarding;
327 if (new_nc->ce_mask & NETCONF_ATTR_PROXY_NEIGH)
328 old_nc->proxy_neigh = new_nc->proxy_neigh;
329 if (new_nc->ce_mask & NETCONF_ATTR_IGNORE_RT_LINKDWN)
330 old_nc->ignore_routes_linkdown = new_nc->ignore_routes_linkdown;
334 return -NLE_OPNOTSUPP;
345int rtnl_netconf_alloc_cache(
struct nl_sock *sk,
struct nl_cache **result)
368 struct rtnl_netconf *nc;
370 if (!ifindex || !family || cache->c_ops != &rtnl_netconf_ops)
373 nl_list_for_each_entry(nc, &cache->c_items, ce_list) {
374 if (nc->ifindex == ifindex &&
375 nc->family == family) {
384void rtnl_netconf_put(
struct rtnl_netconf *nc)
434int rtnl_netconf_get_family(
struct rtnl_netconf *nc,
int *val)
438 if (!(nc->ce_mask & NETCONF_ATTR_FAMILY))
439 return -NLE_MISSING_ATTR;
444int rtnl_netconf_get_ifindex(
struct rtnl_netconf *nc,
int *val)
448 if (!(nc->ce_mask & NETCONF_ATTR_IFINDEX))
449 return -NLE_MISSING_ATTR;
454int rtnl_netconf_get_forwarding(
struct rtnl_netconf *nc,
int *val)
458 if (!(nc->ce_mask & NETCONF_ATTR_FWDING))
459 return -NLE_MISSING_ATTR;
461 *val = nc->forwarding;
464int rtnl_netconf_get_mc_forwarding(
struct rtnl_netconf *nc,
int *val)
468 if (!(nc->ce_mask & NETCONF_ATTR_MC_FWDING))
469 return -NLE_MISSING_ATTR;
471 *val = nc->mc_forwarding;
474int rtnl_netconf_get_rp_filter(
struct rtnl_netconf *nc,
int *val)
478 if (!(nc->ce_mask & NETCONF_ATTR_RP_FILTER))
479 return -NLE_MISSING_ATTR;
481 *val = nc->rp_filter;
484int rtnl_netconf_get_proxy_neigh(
struct rtnl_netconf *nc,
int *val)
488 if (!(nc->ce_mask & NETCONF_ATTR_PROXY_NEIGH))
489 return -NLE_MISSING_ATTR;
491 *val = nc->proxy_neigh;
494int rtnl_netconf_get_ignore_routes_linkdown(
struct rtnl_netconf *nc,
int *val)
498 if (!(nc->ce_mask & NETCONF_ATTR_IGNORE_RT_LINKDWN))
499 return -NLE_MISSING_ATTR;
501 *val = nc->ignore_routes_linkdown;
504int rtnl_netconf_get_input(
struct rtnl_netconf *nc,
int *val)
508 if (!(nc->ce_mask & NETCONF_ATTR_INPUT))
509 return -NLE_MISSING_ATTR;
518static struct nl_object_ops netconf_obj_ops = {
519 .oo_name =
"route/netconf",
520 .oo_size =
sizeof(
struct rtnl_netconf),
525 .oo_compare = netconf_compare,
526 .oo_keygen = netconf_keygen,
527 .oo_update = netconf_update,
528 .oo_attrs2str = netconf_attrs2str,
529 .oo_id_attrs = (NETCONF_ATTR_FAMILY |
530 NETCONF_ATTR_IFINDEX)
533static struct nl_af_group netconf_groups[] = {
534 { AF_INET, RTNLGRP_IPV4_NETCONF },
535 { AF_INET6, RTNLGRP_IPV6_NETCONF },
536 { AF_MPLS, RTNLGRP_MPLS_NETCONF },
537 { END_OF_GROUP_LIST },
540static struct nl_cache_ops rtnl_netconf_ops = {
541 .co_name =
"route/netconf",
542 .co_hdrsize =
sizeof(
struct netconfmsg),
544 { RTM_NEWNETCONF, NL_ACT_NEW,
"new" },
545 { RTM_DELNETCONF, NL_ACT_DEL,
"del" },
546 { RTM_GETNETCONF, NL_ACT_GET,
"get" },
547 END_OF_MSGTYPES_LIST,
549 .co_protocol = NETLINK_ROUTE,
550 .co_groups = netconf_groups,
551 .co_request_update = netconf_request_update,
552 .co_msg_parser = netconf_msg_parser,
553 .co_obj_ops = &netconf_obj_ops,
556static void __init netconf_init(
void)
561static void __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.