EDGESEC  0.1.0-alpha.0+sha.ca29a8277b72f80785649ea9ef9cd7edf642d939
Secure router - reference implementation
Data Structures | Macros | Functions
uci_wrt.c File Reference

File containing the implementation of the uci utilities. More...

#include <arpa/inet.h>
#include <inttypes.h>
#include <string.h>
#include <uci.h>
#include "uci_wrt.h"
#include "allocs.h"
#include "iface_mapper.h"
#include "log.h"
#include "net.h"
#include "squeue.h"
#include <uthash.h>
Include dependency graph for uci_wrt.c:

Data Structures

struct  uci_section_type_count
 Hashmap that stores the count of all the seen section types. More...
 

Macros

#define IFNAME_EXPR   ".ifname="
 
#define IPADDR_EXPR   ".ipaddr="
 
#define IP_SECTION_STR   "%d%d%d%d"
 

Functions

void uwrt_print_error (struct uci_context *ctx, const char *name)
 
int uwrt_lookup_package (struct uci_package *p, UT_array *kv)
 
int uwrt_lookup_key (struct uci_context *ctx, char *key, UT_array *kv)
 
const char * uwrt_extract_value (const char *str, const char *key)
 
int uwrt_get_net_if (UT_array *kv, netif_info_t *nif)
 
int uwrt_set_property (struct uci_context *ctx, char *property)
 
int uwrt_set_properties (struct uci_context *ctx, UT_array *properties)
 Set multiple OpenWRT UCI properties at once. More...
 
int uwrt_add_list (struct uci_context *ctx, char *property)
 
int uwrt_add_list_properties (struct uci_context *ctx, UT_array *properties)
 Set multiple OpenWRT UCI list properties at once. More...
 
int uwrt_delete_property (struct uci_context *ctx, char *property)
 
void uwrt_delete_properties (struct uci_context *ctx, UT_array *properties)
 Delete multiple OpenWRT UCI properties at once Errors with uwrt_delete_property will be logged and ignored. More...
 
void uwrt_free_context (struct uctx *context)
 Frees the uci context. More...
 
struct uctxuwrt_init_context (const char *path)
 Initialises the uci context. More...
 
UT_array * uwrt_get_interfaces (const struct uctx *context, const char *ifname)
 Get the array of struct netif_info_t for each available interface. More...
 
int uwrt_set_interface_ip (const struct uctx *context, const char *ifname, const char *ip_addr, const char *netmask)
 Assigns an IP to an interface. More...
 
int uwrt_create_interface (const struct uctx *context, const char *ifname, const char *type, const char *ip_addr, const char *brd_addr, const char *netmask)
 Creates and interface and assigns an IP. More...
 
int uwrt_commit_section (const struct uctx *context, const char *section)
 Commit a uci section. More...
 
int uwrt_gen_dnsmasq_instance (const struct uctx *context, const struct string_queue *ifname_queue, const UT_array *server_array, const char *leasefile, const char *scriptfile)
 Generates a dnsmasq uci instance. More...
 
int uwrt_add_dhcp_pool (const struct uctx *context, const char *ifname, const char *ip_addr_low, const char *ip_addr_upp, const char *subnet_mask, const char *lease_time)
 Adds a dhcp pool entry. More...
 
int uwrt_gen_hostapd_instance (const struct uctx *context, const struct hostapd_params *params)
 Generate the hostapd config. More...
 
int uwrt_gen_firewall_zone (const struct uctx *context, const char *brname)
 Generate a firewall zone for a bridge. More...
 
int uwrt_add_firewall_nat (const struct uctx *context, const char *brname, const char *ip_addr, const char *nat_name)
 Adds a firewall rule for an IP address. More...
 
int uwrt_delete_firewall_nat (const struct uctx *context, const char *ip_addr)
 Deletes a firewall rule for an IP address. More...
 
int uwrt_add_firewall_bridge (const struct uctx *context, const char *sip, const char *sbr, const char *dip, const char *dbr)
 Adds a firewall bridge rule for two IP addresses. More...
 
int uwrt_delete_firewall_bridge (const struct uctx *context, const char *sip, const char *dip)
 Deletes a firewall bridge rule for two IP addresses. More...
 
int uwrt_cleanup_firewall (const struct uctx *context)
 Removes all the firewall rules. More...
 

Detailed Description

File containing the implementation of the uci utilities.

Author
Alexandru Mereacre
Date
2022

Utility functions for working with UCI (Unified Configuration Interface), which is most commonly used to configure OpenWRT services.

Please see https://openwrt.org/docs/guide-user/base-system/uci for a description of UCI data/object model.

Macro Definition Documentation

◆ IFNAME_EXPR

#define IFNAME_EXPR   ".ifname="

◆ IP_SECTION_STR

#define IP_SECTION_STR   "%d%d%d%d"

◆ IPADDR_EXPR

#define IPADDR_EXPR   ".ipaddr="

Function Documentation

◆ uwrt_add_dhcp_pool()

int uwrt_add_dhcp_pool ( const struct uctx context,
const char *  ifname,
const char *  ip_addr_low,
const char *  ip_addr_upp,
const char *  subnet_mask,
const char *  lease_time 
)

Adds a dhcp pool entry.

Parameters
contextThe uci context
ifnameThe interface name
ip_addr_lowInterface string IP address lower bound
ip_addr_uppInterface string IP address upper bound
subnet_maskInterface string IP subnet mask
lease_timeInterface lease time string
Returns
int 0 on success, -1 on failure

◆ uwrt_add_firewall_bridge()

int uwrt_add_firewall_bridge ( const struct uctx context,
const char *  sip,
const char *  sbr,
const char *  dip,
const char *  dbr 
)

Adds a firewall bridge rule for two IP addresses.

Parameters
contextThe uci context
sipThe source IP address
sbrThe source bridge interface name
dipThe destination IP address
dbrThe destination bridge interface name
Returns
int 0 on success, -1 on failure

◆ uwrt_add_firewall_nat()

int uwrt_add_firewall_nat ( const struct uctx context,
const char *  brname,
const char *  ip_addr,
const char *  nat_name 
)

Adds a firewall rule for an IP address.

Parameters
contextThe uci context
brnameThe bridge name
ip_addrThe IP address
nat_nameThe NAT bridge name
Returns
int 0 on success, -1 on failure

◆ uwrt_add_list()

int uwrt_add_list ( struct uci_context *  ctx,
char *  property 
)

◆ uwrt_add_list_properties()

int uwrt_add_list_properties ( struct uci_context *  ctx,
UT_array *  properties 
)

Set multiple OpenWRT UCI list properties at once.

Parameters
[in]ctxUCI context. The context ptr will be modified.
[in]propertiesArray of list properties to set. Warning, strings may be modified by UCI.
Return values
0Success
-1Error

◆ uwrt_cleanup_firewall()

int uwrt_cleanup_firewall ( const struct uctx context)

Removes all the firewall rules.

Parameters
contextThe uci context
Returns
int 0 on success, -1 on failure

◆ uwrt_commit_section()

int uwrt_commit_section ( const struct uctx context,
const char *  section 
)

Commit a uci section.

Parameters
contextThe uci context
sectionThe uci section
Returns
int 0 on success, -1 on failure

◆ uwrt_create_interface()

int uwrt_create_interface ( const struct uctx context,
const char *  ifname,
const char *  type,
const char *  ip_addr,
const char *  brd_addr,
const char *  netmask 
)

Creates and interface and assigns an IP.

Parameters
contextThe uci context
ifnameThe interface name
typeThe interface type
ip_addrThe interface IP4 address
brd_addrThe interface IP4 broadcast address
netmaskThe interface IP4 netmask
Returns
int 0 on success, -1 on failure

◆ uwrt_delete_firewall_bridge()

int uwrt_delete_firewall_bridge ( const struct uctx context,
const char *  sip,
const char *  dip 
)

Deletes a firewall bridge rule for two IP addresses.

Parameters
contextThe uci context
sipThe source IP address
dipThe destination IP address
Returns
int 0 on success, -1 on failure

◆ uwrt_delete_firewall_nat()

int uwrt_delete_firewall_nat ( const struct uctx context,
const char *  ip_addr 
)

Deletes a firewall rule for an IP address.

Parameters
contextThe uci context
ip_addrThe IP address
Returns
int 0 on success, -1 on failure

◆ uwrt_delete_properties()

void uwrt_delete_properties ( struct uci_context *  ctx,
UT_array *  properties 
)

Delete multiple OpenWRT UCI properties at once Errors with uwrt_delete_property will be logged and ignored.

Parameters
[in]ctxUCI context. The context ptr will be modified.
[in]propertiesArray of properties to delete. Warning, strings may be modified by UCI.

◆ uwrt_delete_property()

int uwrt_delete_property ( struct uci_context *  ctx,
char *  property 
)

◆ uwrt_extract_value()

const char* uwrt_extract_value ( const char *  str,
const char *  key 
)

◆ uwrt_free_context()

void uwrt_free_context ( struct uctx context)

Frees the uci context.

Parameters
contextThe uci context

◆ uwrt_gen_dnsmasq_instance()

int uwrt_gen_dnsmasq_instance ( const struct uctx context,
const struct string_queue ifname_queue,
const UT_array *  server_array,
const char *  leasefile,
const char *  scriptfile 
)

Generates a dnsmasq uci instance.

Parameters
contextThe uci context
ifname_queueThe interface queue
server_arrayThe array of servers
leasefileThe lease file path string
scriptfileThe script file path string
Returns
int 0 on success, -1 on failure

◆ uwrt_gen_firewall_zone()

int uwrt_gen_firewall_zone ( const struct uctx context,
const char *  brname 
)

Generate a firewall zone for a bridge.

Parameters
contextThe uci context
brnameThe bridge name
Returns
int 0 on success, -1 on failure

◆ uwrt_gen_hostapd_instance()

int uwrt_gen_hostapd_instance ( const struct uctx context,
const struct hostapd_params params 
)

Generate the hostapd config.

Parameters
contextThe uci context
paramsThe hostapd params
Returns
int 0 on success, -1 on failure

◆ uwrt_get_interfaces()

UT_array* uwrt_get_interfaces ( const struct uctx context,
const char *  ifname 
)

Get the array of struct netif_info_t for each available interface.

Parameters
contextThe uci context
ifnameThe interface name, if NULL return all interfaces
Returns
UT_array* The returned array of struct netif_info_t This return variable must be cleaned up by utarray_free().

◆ uwrt_get_net_if()

int uwrt_get_net_if ( UT_array *  kv,
netif_info_t nif 
)

◆ uwrt_init_context()

struct uctx* uwrt_init_context ( const char *  path)

Initialises the uci context.

Parameters
pathThe path string to the config folder
Returns
The uci context, or NULL on error. This return variable must be cleaned up by uwrt_free_context().

◆ uwrt_lookup_key()

int uwrt_lookup_key ( struct uci_context *  ctx,
char *  key,
UT_array *  kv 
)

◆ uwrt_lookup_package()

int uwrt_lookup_package ( struct uci_package *  p,
UT_array *  kv 
)

Counts the section types we've already encountered

◆ uwrt_print_error()

void uwrt_print_error ( struct uci_context *  ctx,
const char *  name 
)

◆ uwrt_set_interface_ip()

int uwrt_set_interface_ip ( const struct uctx context,
const char *  ifname,
const char *  ip_addr,
const char *  netmask 
)

Assigns an IP to an interface.

Parameters
contextThe uci context
ifnameThe interface name
ip_addrThe interface IP4 address
netmaskThe interface IP4 netmask
Returns
int 0 on success, -1 on failure

◆ uwrt_set_properties()

int uwrt_set_properties ( struct uci_context *  ctx,
UT_array *  properties 
)

Set multiple OpenWRT UCI properties at once.

Parameters
[in]ctxUCI context. The context ptr will be modified.
[in]propertiesArray of properties to set. Warning, strings may be modified by UCI.
Return values
0Success
-1Error

◆ uwrt_set_property()

int uwrt_set_property ( struct uci_context *  ctx,
char *  property 
)