21 #include <netlink-private/netlink.h> 22 #include <netlink/netlink.h> 23 #include <netlink/utils.h> 24 #include <netlink/route/netconf.h> 25 #include <linux/netconf.h> 26 #include <linux/socket.h> 27 #include <netlink/hashtable.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;
53 static struct nl_cache_ops rtnl_netconf_ops;
54 static struct nl_object_ops netconf_obj_ops;
57 static 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 },
66 static 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 },
74 static struct nla_policy devconf_mpls_policy[NETCONFA_MAX+1] = {
75 [NETCONFA_IFINDEX] = { .
type = NLA_S32 },
76 [NETCONFA_INPUT] = { .type = NLA_S32 },
79 static struct rtnl_netconf *rtnl_netconf_alloc(
void)
84 static int netconf_clone(
struct nl_object *_dst,
struct nl_object *_src)
86 struct rtnl_netconf *dst = nl_object_priv(_dst);
87 struct rtnl_netconf *src = nl_object_priv(_src);
94 static int netconf_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
95 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
97 struct nlattr *tb[NETCONFA_MAX+1], *attr;
98 struct rtnl_netconf *nc;
99 struct netconfmsg *ncm;
103 switch (ncm->ncm_family) {
105 err =
nlmsg_parse(nlh,
sizeof(*ncm), tb, NETCONFA_MAX,
106 devconf_ipv4_policy);
111 err =
nlmsg_parse(nlh,
sizeof(*ncm), tb, NETCONFA_MAX,
112 devconf_ipv6_policy);
117 err =
nlmsg_parse(nlh,
sizeof(*ncm), tb, NETCONFA_MAX,
118 devconf_mpls_policy);
123 printf(
"unexpected netconf family: %d\n", ncm->ncm_family);
127 if (!tb[NETCONFA_IFINDEX])
130 nc = rtnl_netconf_alloc();
134 nc->ce_msgtype = nlh->nlmsg_type;
135 nc->family = ncm->ncm_family;
138 nc->ce_mask = NETCONF_ATTR_FAMILY | NETCONF_ATTR_IFINDEX;
141 if (tb[NETCONFA_RP_FILTER]) {
142 attr = tb[NETCONFA_RP_FILTER];
144 nc->ce_mask |= NETCONF_ATTR_RP_FILTER;
147 if (tb[NETCONFA_FORWARDING]) {
148 attr = tb[NETCONFA_FORWARDING];
150 nc->ce_mask |= NETCONF_ATTR_FWDING;
153 if (tb[NETCONFA_MC_FORWARDING]) {
154 attr = tb[NETCONFA_MC_FORWARDING];
156 nc->ce_mask |= NETCONF_ATTR_MC_FWDING;
159 if (tb[NETCONFA_PROXY_NEIGH]) {
160 attr = tb[NETCONFA_PROXY_NEIGH];
162 nc->ce_mask |= NETCONF_ATTR_PROXY_NEIGH;
165 if (tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]) {
166 attr = tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN];
168 nc->ce_mask |= NETCONF_ATTR_IGNORE_RT_LINKDWN;
171 if (tb[NETCONFA_INPUT]) {
172 attr = tb[NETCONFA_INPUT];
174 nc->ce_mask |= NETCONF_ATTR_INPUT;
177 err = pp->pp_cb((
struct nl_object *) nc, pp);
179 rtnl_netconf_put(nc);
183 static int netconf_request_update(
struct nl_cache *cache,
struct nl_sock *sk)
185 struct netconfmsg nc = {
186 .ncm_family = cache->c_iarg1,
189 return nl_send_simple(sk, RTM_GETNETCONF, NLM_F_DUMP, &nc,
sizeof(nc));
192 static void netconf_dump_line(
struct nl_object *obj,
struct nl_dump_params *p)
194 struct rtnl_netconf *nc = (
struct rtnl_netconf *) obj;
195 struct nl_cache *link_cache;
212 switch(nc->ifindex) {
213 case NETCONFA_IFINDEX_ALL:
216 case NETCONFA_IFINDEX_DEFAULT:
225 nl_cache_put(link_cache);
227 nl_dump(p,
"dev %d ", nc->ifindex);
230 if (nc->ce_mask & NETCONF_ATTR_FWDING) {
232 nc->forwarding ?
"on" :
"off");
235 if (nc->ce_mask & NETCONF_ATTR_RP_FILTER) {
236 if (nc->rp_filter == 0)
238 else if (nc->rp_filter == 1)
239 nl_dump(p,
"rp_filter strict ");
240 else if (nc->rp_filter == 2)
241 nl_dump(p,
"rp_filter loose ");
243 nl_dump(p,
"rp_filter unknown-mode ");
246 if (nc->ce_mask & NETCONF_ATTR_MC_FWDING) {
247 nl_dump(p,
"mc_forwarding %s ",
248 nc->mc_forwarding ?
"on" :
"off");
251 if (nc->ce_mask & NETCONF_ATTR_PROXY_NEIGH)
252 nl_dump(p,
"proxy_neigh %d ", nc->proxy_neigh);
254 if (nc->ce_mask & NETCONF_ATTR_IGNORE_RT_LINKDWN) {
255 nl_dump(p,
"ignore_routes_with_linkdown %s ",
256 nc->ignore_routes_linkdown ?
"on" :
"off");
259 if (nc->ce_mask & NETCONF_ATTR_INPUT)
260 nl_dump(p,
"input %s ", nc->input ?
"on" :
"off");
265 static const struct trans_tbl netconf_attrs[] = {
266 __ADD(NETCONF_ATTR_FAMILY, family),
267 __ADD(NETCONF_ATTR_IFINDEX, ifindex),
268 __ADD(NETCONF_ATTR_RP_FILTER, rp_filter),
269 __ADD(NETCONF_ATTR_FWDING, forwarding),
270 __ADD(NETCONF_ATTR_MC_FWDING, mc_forwarding),
271 __ADD(NETCONF_ATTR_PROXY_NEIGH, proxy_neigh),
272 __ADD(NETCONF_ATTR_IGNORE_RT_LINKDWN, ignore_routes_with_linkdown),
273 __ADD(NETCONF_ATTR_INPUT, input),
276 static char *netconf_attrs2str(
int attrs,
char *buf,
size_t len)
278 return __flags2str(attrs, buf, len, netconf_attrs,
279 ARRAY_SIZE(netconf_attrs));
282 static void netconf_keygen(
struct nl_object *obj, uint32_t *hashkey,
285 struct rtnl_netconf *nc = (
struct rtnl_netconf *) obj;
286 unsigned int nckey_sz;
290 } __attribute__((packed)) nckey;
292 nckey_sz =
sizeof(nckey);
293 nckey.nc_family = nc->family;
294 nckey.nc_index = nc->ifindex;
296 *hashkey = nl_hash(&nckey, nckey_sz, 0) % table_sz;
298 NL_DBG(5,
"netconf %p key (dev %d fam %d) keysz %d, hash 0x%x\n",
299 nc, nckey.nc_index, nckey.nc_family, nckey_sz, *hashkey);
302 static uint64_t netconf_compare(
struct nl_object *_a,
struct nl_object *_b,
303 uint64_t attrs,
int flags)
305 struct rtnl_netconf *a = (
struct rtnl_netconf *) _a;
306 struct rtnl_netconf *b = (
struct rtnl_netconf *) _b;
309 #define NETCONF_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, NETCONF_ATTR_##ATTR, a, b, EXPR) 311 diff |= NETCONF_DIFF(FAMILY, a->family != b->family);
312 diff |= NETCONF_DIFF(IFINDEX, a->ifindex != b->ifindex);
313 diff |= NETCONF_DIFF(RP_FILTER, a->rp_filter != b->rp_filter);
314 diff |= NETCONF_DIFF(FWDING, a->forwarding != b->forwarding);
315 diff |= NETCONF_DIFF(MC_FWDING, a->mc_forwarding != b->mc_forwarding);
316 diff |= NETCONF_DIFF(PROXY_NEIGH, a->proxy_neigh != b->proxy_neigh);
317 diff |= NETCONF_DIFF(IGNORE_RT_LINKDWN,
318 a->ignore_routes_linkdown != b->ignore_routes_linkdown);
319 diff |= NETCONF_DIFF(INPUT, a->input != b->input);
326 static int netconf_update(
struct nl_object *old_obj,
struct nl_object *new_obj)
328 struct rtnl_netconf *new_nc = (
struct rtnl_netconf *) new_obj;
329 struct rtnl_netconf *old_nc = (
struct rtnl_netconf *) old_obj;
330 int action = new_obj->ce_msgtype;
334 if (new_nc->family != old_nc->family ||
335 new_nc->ifindex != old_nc->ifindex)
336 return -NLE_OPNOTSUPP;
338 if (new_nc->ce_mask & NETCONF_ATTR_RP_FILTER)
339 old_nc->rp_filter = new_nc->rp_filter;
340 if (new_nc->ce_mask & NETCONF_ATTR_FWDING)
341 old_nc->forwarding = new_nc->forwarding;
342 if (new_nc->ce_mask & NETCONF_ATTR_MC_FWDING)
343 old_nc->mc_forwarding = new_nc->mc_forwarding;
344 if (new_nc->ce_mask & NETCONF_ATTR_PROXY_NEIGH)
345 old_nc->proxy_neigh = new_nc->proxy_neigh;
346 if (new_nc->ce_mask & NETCONF_ATTR_IGNORE_RT_LINKDWN)
347 old_nc->ignore_routes_linkdown = new_nc->ignore_routes_linkdown;
351 return -NLE_OPNOTSUPP;
362 int rtnl_netconf_alloc_cache(
struct nl_sock *sk,
struct nl_cache **result)
385 struct rtnl_netconf *nc;
387 if (!ifindex || !family || cache->c_ops != &rtnl_netconf_ops)
390 nl_list_for_each_entry(nc, &cache->c_items, ce_list) {
391 if (nc->ifindex == ifindex &&
392 nc->family == family) {
401 void rtnl_netconf_put(
struct rtnl_netconf *nc)
451 int rtnl_netconf_get_family(
struct rtnl_netconf *nc,
int *val)
455 if (!(nc->ce_mask & NETCONF_ATTR_FAMILY))
456 return -NLE_MISSING_ATTR;
461 int rtnl_netconf_get_ifindex(
struct rtnl_netconf *nc,
int *val)
465 if (!(nc->ce_mask & NETCONF_ATTR_IFINDEX))
466 return -NLE_MISSING_ATTR;
471 int rtnl_netconf_get_forwarding(
struct rtnl_netconf *nc,
int *val)
475 if (!(nc->ce_mask & NETCONF_ATTR_FWDING))
476 return -NLE_MISSING_ATTR;
478 *val = nc->forwarding;
481 int rtnl_netconf_get_mc_forwarding(
struct rtnl_netconf *nc,
int *val)
485 if (!(nc->ce_mask & NETCONF_ATTR_MC_FWDING))
486 return -NLE_MISSING_ATTR;
488 *val = nc->mc_forwarding;
491 int rtnl_netconf_get_rp_filter(
struct rtnl_netconf *nc,
int *val)
495 if (!(nc->ce_mask & NETCONF_ATTR_RP_FILTER))
496 return -NLE_MISSING_ATTR;
498 *val = nc->rp_filter;
501 int rtnl_netconf_get_proxy_neigh(
struct rtnl_netconf *nc,
int *val)
505 if (!(nc->ce_mask & NETCONF_ATTR_PROXY_NEIGH))
506 return -NLE_MISSING_ATTR;
508 *val = nc->proxy_neigh;
511 int rtnl_netconf_get_ignore_routes_linkdown(
struct rtnl_netconf *nc,
int *val)
515 if (!(nc->ce_mask & NETCONF_ATTR_IGNORE_RT_LINKDWN))
516 return -NLE_MISSING_ATTR;
518 *val = nc->ignore_routes_linkdown;
521 int rtnl_netconf_get_input(
struct rtnl_netconf *nc,
int *val)
525 if (!(nc->ce_mask & NETCONF_ATTR_INPUT))
526 return -NLE_MISSING_ATTR;
535 static struct nl_object_ops netconf_obj_ops = {
536 .oo_name =
"route/netconf",
537 .oo_size =
sizeof(
struct rtnl_netconf),
538 .oo_clone = netconf_clone,
543 .oo_compare = netconf_compare,
544 .oo_keygen = netconf_keygen,
545 .oo_update = netconf_update,
546 .oo_attrs2str = netconf_attrs2str,
547 .oo_id_attrs = (NETCONF_ATTR_FAMILY |
548 NETCONF_ATTR_IFINDEX)
551 static struct nl_af_group netconf_groups[] = {
552 { AF_INET, RTNLGRP_IPV4_NETCONF },
553 { AF_INET6, RTNLGRP_IPV6_NETCONF },
554 { AF_MPLS, RTNLGRP_MPLS_NETCONF },
555 { END_OF_GROUP_LIST },
558 static struct nl_cache_ops rtnl_netconf_ops = {
559 .co_name =
"route/netconf",
560 .co_hdrsize =
sizeof(
struct netconfmsg),
562 { RTM_NEWNETCONF, NL_ACT_NEW,
"new" },
563 { RTM_DELNETCONF, NL_ACT_DEL,
"del" },
564 { RTM_GETNETCONF, NL_ACT_GET,
"get" },
565 END_OF_MSGTYPES_LIST,
567 .co_protocol = NETLINK_ROUTE,
568 .co_groups = netconf_groups,
569 .co_request_update = netconf_request_update,
570 .co_msg_parser = netconf_msg_parser,
571 .co_obj_ops = &netconf_obj_ops,
574 static void __init netconf_init(
void)
579 static void __exit netconf_exit(
void)
Dump object briefly on one line.
int32_t nla_get_s32(const struct nlattr *nla)
Return payload of 32 bit signed integer attribute.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
Attribute validation policy.
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
struct rtnl_netconf * rtnl_netconf_get_by_idx(struct nl_cache *cache, int family, int ifindex)
Search netconf in cache.
Dump all attributes but no statistics.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
struct rtnl_netconf * rtnl_netconf_get_all(struct nl_cache *cache, int family)
Search netconf in cache.
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_object_put(struct nl_object *obj)
Release a reference from an object.
uint16_t type
Type of attribute or NLA_UNSPEC.
char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst, size_t len)
Translate interface index to corresponding link name.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
struct rtnl_netconf * rtnl_netconf_get_default(struct nl_cache *cache, int family)
Search netconf in cache.
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.