diff options
| author | David Lamparter <equinox@opensourcerouting.org> | 2021-10-20 13:07:47 +0200 | 
|---|---|---|
| committer | David Lamparter <equinox@opensourcerouting.org> | 2021-10-20 13:28:46 +0200 | 
| commit | a243d1db93aaa123413a754fe69fbad36d810ae7 (patch) | |
| tree | 3d2e74c2b3f4d4862f7a7029c2ff5d18d71999ae /nhrpd | |
| parent | bf4af4ffb5e2ffa0b34c5bd67b5b7d4aa912747f (diff) | |
*: convert zclient callbacks to table
This removes a giant `switch { }` block from lib/zclient.c and
harmonizes all zclient callback function types to be the same (some had
a subset of the args, some had a void return, now they all have
ZAPI_CALLBACK_ARGS and int return.)
Apart from getting rid of the giant switch, this is a minor security
benefit since the function pointers are now in a `const` array, so they
can't be overwritten by e.g. heap overflows for code execution anymore.
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Diffstat (limited to 'nhrpd')
| -rw-r--r-- | nhrpd/netlink_arp.c | 9 | ||||
| -rw-r--r-- | nhrpd/nhrp_route.c | 30 | ||||
| -rw-r--r-- | nhrpd/nhrpd.h | 4 | 
3 files changed, 25 insertions, 18 deletions
diff --git a/nhrpd/netlink_arp.c b/nhrpd/netlink_arp.c index 0a618056d5..3658cb16bb 100644 --- a/nhrpd/netlink_arp.c +++ b/nhrpd/netlink_arp.c @@ -147,7 +147,7 @@ void netlink_set_nflog_group(int nlgroup)  	}  } -void nhrp_neighbor_operation(ZAPI_CALLBACK_ARGS) +int nhrp_neighbor_operation(ZAPI_CALLBACK_ARGS)  {  	union sockunion addr = {}, lladdr = {};  	struct interface *ifp; @@ -157,7 +157,7 @@ void nhrp_neighbor_operation(ZAPI_CALLBACK_ARGS)  	zclient_neigh_ip_decode(zclient->ibuf, &api);  	if (api.ip_in.ipa_type == AF_UNSPEC) -		return; +		return 0;  	sockunion_family(&addr) = api.ip_in.ipa_type;  	memcpy((uint8_t *)sockunion_get_addr(&addr), &api.ip_in.ip.addr,  	       family2addrsize(api.ip_in.ipa_type)); @@ -172,10 +172,10 @@ void nhrp_neighbor_operation(ZAPI_CALLBACK_ARGS)  	ndm_state = api.ndm_state;  	if (!ifp) -		return; +		return 0;  	c = nhrp_cache_get(ifp, &addr, 0);  	if (!c) -		return; +		return 0;  	debugf(NHRP_DEBUG_KERNEL,  	       "Netlink: %s %pSU dev %s lladdr %pSU nud 0x%x cache used %u type %u",  	       (cmd == ZEBRA_NHRP_NEIGH_GET) @@ -200,4 +200,5 @@ void nhrp_neighbor_operation(ZAPI_CALLBACK_ARGS)  			: ZEBRA_NEIGH_STATE_FAILED;  		nhrp_cache_set_used(c, state == ZEBRA_NEIGH_STATE_REACHABLE);  	} +	return 0;  } diff --git a/nhrpd/nhrp_route.c b/nhrpd/nhrp_route.c index 12a2fc2fa0..76e0978cb6 100644 --- a/nhrpd/nhrp_route.c +++ b/nhrpd/nhrp_route.c @@ -366,21 +366,25 @@ static void nhrp_zebra_connected(struct zclient *zclient)  	nhrp_zebra_register_neigh(VRF_DEFAULT, AFI_IP6, true);  } +static zclient_handler *const nhrp_handlers[] = { +	[ZEBRA_INTERFACE_ADDRESS_ADD] = nhrp_interface_address_add, +	[ZEBRA_INTERFACE_ADDRESS_DELETE] = nhrp_interface_address_delete, +	[ZEBRA_REDISTRIBUTE_ROUTE_ADD] = nhrp_route_read, +	[ZEBRA_REDISTRIBUTE_ROUTE_DEL] = nhrp_route_read, +	[ZEBRA_NHRP_NEIGH_ADDED] = nhrp_neighbor_operation, +	[ZEBRA_NHRP_NEIGH_REMOVED] = nhrp_neighbor_operation, +	[ZEBRA_NHRP_NEIGH_GET] = nhrp_neighbor_operation, +	[ZEBRA_GRE_UPDATE] = nhrp_gre_update, +}; +  void nhrp_zebra_init(void)  {  	zebra_rib[AFI_IP] = route_table_init();  	zebra_rib[AFI_IP6] = route_table_init(); -	zclient = zclient_new(master, &zclient_options_default); +	zclient = zclient_new(master, &zclient_options_default, nhrp_handlers, +			      array_size(nhrp_handlers));  	zclient->zebra_connected = nhrp_zebra_connected; -	zclient->interface_address_add = nhrp_interface_address_add; -	zclient->interface_address_delete = nhrp_interface_address_delete; -	zclient->redistribute_route_add = nhrp_route_read; -	zclient->redistribute_route_del = nhrp_route_read; -	zclient->neighbor_added = nhrp_neighbor_operation; -	zclient->neighbor_removed = nhrp_neighbor_operation; -	zclient->neighbor_get = nhrp_neighbor_operation; -	zclient->gre_update = nhrp_gre_update;  	zclient_init(zclient, ZEBRA_ROUTE_NHRP, 0, &nhrpd_privs);  } @@ -476,7 +480,7 @@ void nhrp_zebra_terminate(void)  	route_table_finish(zebra_rib[AFI_IP6]);  } -void nhrp_gre_update(ZAPI_CALLBACK_ARGS) +int nhrp_gre_update(ZAPI_CALLBACK_ARGS)  {  	struct stream *s;  	struct nhrp_gre_info gre_info, *val; @@ -485,7 +489,7 @@ void nhrp_gre_update(ZAPI_CALLBACK_ARGS)  	/* result */  	s = zclient->ibuf;  	if (vrf_id != VRF_DEFAULT) -		return; +		return 0;  	/* read GRE information */  	STREAM_GETL(s, gre_info.ifindex); @@ -516,7 +520,9 @@ void nhrp_gre_update(ZAPI_CALLBACK_ARGS)  	       ifp ? ifp->name : "<none>", gre_info.ifindex, vrf_id);  	if (ifp)  		nhrp_interface_update_nbma(ifp, val); -	return; +	return 0; +  stream_failure:  	zlog_err("%s(): error reading response ..", __func__); +	return -1;  } diff --git a/nhrpd/nhrpd.h b/nhrpd/nhrpd.h index 17abb04762..96caa39040 100644 --- a/nhrpd/nhrpd.h +++ b/nhrpd/nhrpd.h @@ -363,8 +363,8 @@ int nhrp_interface_up(ZAPI_CALLBACK_ARGS);  int nhrp_interface_down(ZAPI_CALLBACK_ARGS);  int nhrp_interface_address_add(ZAPI_CALLBACK_ARGS);  int nhrp_interface_address_delete(ZAPI_CALLBACK_ARGS); -void nhrp_neighbor_operation(ZAPI_CALLBACK_ARGS); -void nhrp_gre_update(ZAPI_CALLBACK_ARGS); +int nhrp_neighbor_operation(ZAPI_CALLBACK_ARGS); +int nhrp_gre_update(ZAPI_CALLBACK_ARGS);  void nhrp_interface_notify_add(struct interface *ifp, struct notifier_block *n,  			       notifier_fn_t fn);  | 
