platform: mark static nla_policy variables as const

These static variables really never be modified.
Mark them as const, which allows the linker to mark them as
read-only.

The problem is libnl3's API, which has these parameters
not as const. Add a workaround for that. Clearly libnl3 is
not gonna modify the policy, that the API was fixed too [1]

[1] b4802a17a7
This commit is contained in:
Thomas Haller
2017-10-09 12:46:01 +02:00
parent 99376bd261
commit 637c6cb333
2 changed files with 68 additions and 26 deletions

View File

@@ -960,6 +960,30 @@ flags_done:
return b; return b;
} }
static int
_nl_nla_parse (struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
const struct nla_policy *policy)
{
return nla_parse (tb, maxtype, head, len, (struct nla_policy *) policy);
}
#define nla_parse(...) _nl_nla_parse(__VA_ARGS__)
static int
_nl_nlmsg_parse (struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[],
int maxtype, const struct nla_policy *policy)
{
return nlmsg_parse (nlh, hdrlen, tb, maxtype, (struct nla_policy *) policy);
}
#define nlmsg_parse(...) _nl_nlmsg_parse(__VA_ARGS__)
static int
_nl_nla_parse_nested (struct nlattr *tb[], int maxtype, struct nlattr *nla,
const struct nla_policy *policy)
{
return nla_parse_nested (tb, maxtype, nla, (struct nla_policy *) policy);
}
#define nla_parse_nested(...) _nl_nla_parse_nested(__VA_ARGS__)
/****************************************************************** /******************************************************************
* NMPObject/netlink functions * NMPObject/netlink functions
******************************************************************/ ******************************************************************/
@@ -987,7 +1011,7 @@ _parse_af_inet6 (NMPlatform *platform,
guint8 *out_addr_gen_mode_inv, guint8 *out_addr_gen_mode_inv,
gboolean *out_addr_gen_mode_valid) gboolean *out_addr_gen_mode_valid)
{ {
static struct nla_policy policy[IFLA_INET6_MAX+1] = { static const struct nla_policy policy[IFLA_INET6_MAX+1] = {
[IFLA_INET6_FLAGS] = { .type = NLA_U32 }, [IFLA_INET6_FLAGS] = { .type = NLA_U32 },
[IFLA_INET6_CACHEINFO] = { .minlen = nm_offsetofend (struct ifla_cacheinfo, retrans_time) }, [IFLA_INET6_CACHEINFO] = { .minlen = nm_offsetofend (struct ifla_cacheinfo, retrans_time) },
[IFLA_INET6_CONF] = { .minlen = 4 }, [IFLA_INET6_CONF] = { .minlen = 4 },
@@ -1054,7 +1078,7 @@ errout:
static NMPObject * static NMPObject *
_parse_lnk_gre (const char *kind, struct nlattr *info_data) _parse_lnk_gre (const char *kind, struct nlattr *info_data)
{ {
static struct nla_policy policy[IFLA_GRE_MAX + 1] = { static const struct nla_policy policy[IFLA_GRE_MAX + 1] = {
[IFLA_GRE_LINK] = { .type = NLA_U32 }, [IFLA_GRE_LINK] = { .type = NLA_U32 },
[IFLA_GRE_IFLAGS] = { .type = NLA_U16 }, [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
[IFLA_GRE_OFLAGS] = { .type = NLA_U16 }, [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
@@ -1114,7 +1138,7 @@ _parse_lnk_gre (const char *kind, struct nlattr *info_data)
static NMPObject * static NMPObject *
_parse_lnk_infiniband (const char *kind, struct nlattr *info_data) _parse_lnk_infiniband (const char *kind, struct nlattr *info_data)
{ {
static struct nla_policy policy[IFLA_IPOIB_MAX + 1] = { static const struct nla_policy policy[IFLA_IPOIB_MAX + 1] = {
[IFLA_IPOIB_PKEY] = { .type = NLA_U16 }, [IFLA_IPOIB_PKEY] = { .type = NLA_U16 },
[IFLA_IPOIB_MODE] = { .type = NLA_U16 }, [IFLA_IPOIB_MODE] = { .type = NLA_U16 },
[IFLA_IPOIB_UMCAST] = { .type = NLA_U16 }, [IFLA_IPOIB_UMCAST] = { .type = NLA_U16 },
@@ -1160,7 +1184,7 @@ _parse_lnk_infiniband (const char *kind, struct nlattr *info_data)
static NMPObject * static NMPObject *
_parse_lnk_ip6tnl (const char *kind, struct nlattr *info_data) _parse_lnk_ip6tnl (const char *kind, struct nlattr *info_data)
{ {
static struct nla_policy policy[IFLA_IPTUN_MAX + 1] = { static const struct nla_policy policy[IFLA_IPTUN_MAX + 1] = {
[IFLA_IPTUN_LINK] = { .type = NLA_U32 }, [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
[IFLA_IPTUN_LOCAL] = { .type = NLA_UNSPEC, [IFLA_IPTUN_LOCAL] = { .type = NLA_UNSPEC,
.minlen = sizeof (struct in6_addr)}, .minlen = sizeof (struct in6_addr)},
@@ -1213,7 +1237,7 @@ _parse_lnk_ip6tnl (const char *kind, struct nlattr *info_data)
static NMPObject * static NMPObject *
_parse_lnk_ipip (const char *kind, struct nlattr *info_data) _parse_lnk_ipip (const char *kind, struct nlattr *info_data)
{ {
static struct nla_policy policy[IFLA_IPTUN_MAX + 1] = { static const struct nla_policy policy[IFLA_IPTUN_MAX + 1] = {
[IFLA_IPTUN_LINK] = { .type = NLA_U32 }, [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
[IFLA_IPTUN_LOCAL] = { .type = NLA_U32 }, [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
[IFLA_IPTUN_REMOTE] = { .type = NLA_U32 }, [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
@@ -1251,7 +1275,7 @@ _parse_lnk_ipip (const char *kind, struct nlattr *info_data)
static NMPObject * static NMPObject *
_parse_lnk_macvlan (const char *kind, struct nlattr *info_data) _parse_lnk_macvlan (const char *kind, struct nlattr *info_data)
{ {
static struct nla_policy policy[IFLA_MACVLAN_MAX + 1] = { static const struct nla_policy policy[IFLA_MACVLAN_MAX + 1] = {
[IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
[IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 }, [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
}; };
@@ -1294,7 +1318,7 @@ _parse_lnk_macvlan (const char *kind, struct nlattr *info_data)
static NMPObject * static NMPObject *
_parse_lnk_macsec (const char *kind, struct nlattr *info_data) _parse_lnk_macsec (const char *kind, struct nlattr *info_data)
{ {
static struct nla_policy policy[__IFLA_MACSEC_MAX] = { static const struct nla_policy policy[__IFLA_MACSEC_MAX] = {
[IFLA_MACSEC_SCI] = { .type = NLA_U64 }, [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
[IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 }, [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
[IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 }, [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
@@ -1344,7 +1368,7 @@ _parse_lnk_macsec (const char *kind, struct nlattr *info_data)
static NMPObject * static NMPObject *
_parse_lnk_sit (const char *kind, struct nlattr *info_data) _parse_lnk_sit (const char *kind, struct nlattr *info_data)
{ {
static struct nla_policy policy[IFLA_IPTUN_MAX + 1] = { static const struct nla_policy policy[IFLA_IPTUN_MAX + 1] = {
[IFLA_IPTUN_LINK] = { .type = NLA_U32 }, [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
[IFLA_IPTUN_LOCAL] = { .type = NLA_U32 }, [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
[IFLA_IPTUN_REMOTE] = { .type = NLA_U32 }, [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
@@ -1446,7 +1470,7 @@ _vlan_qos_mapping_from_nla (struct nlattr *nlattr,
static NMPObject * static NMPObject *
_parse_lnk_vlan (const char *kind, struct nlattr *info_data) _parse_lnk_vlan (const char *kind, struct nlattr *info_data)
{ {
static struct nla_policy policy[IFLA_VLAN_MAX+1] = { static const struct nla_policy policy[IFLA_VLAN_MAX+1] = {
[IFLA_VLAN_ID] = { .type = NLA_U16 }, [IFLA_VLAN_ID] = { .type = NLA_U16 },
[IFLA_VLAN_FLAGS] = { .minlen = nm_offsetofend (struct ifla_vlan_flags, flags) }, [IFLA_VLAN_FLAGS] = { .minlen = nm_offsetofend (struct ifla_vlan_flags, flags) },
[IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED }, [IFLA_VLAN_INGRESS_QOS] = { .type = NLA_NESTED },
@@ -1532,7 +1556,7 @@ struct nm_ifla_vxlan_port_range {
static NMPObject * static NMPObject *
_parse_lnk_vxlan (const char *kind, struct nlattr *info_data) _parse_lnk_vxlan (const char *kind, struct nlattr *info_data)
{ {
static struct nla_policy policy[IFLA_VXLAN_MAX + 1] = { static const struct nla_policy policy[IFLA_VXLAN_MAX + 1] = {
[IFLA_VXLAN_ID] = { .type = NLA_U32 }, [IFLA_VXLAN_ID] = { .type = NLA_U32 },
[IFLA_VXLAN_GROUP] = { .type = NLA_U32 }, [IFLA_VXLAN_GROUP] = { .type = NLA_U32 },
[IFLA_VXLAN_GROUP6] = { .type = NLA_UNSPEC, [IFLA_VXLAN_GROUP6] = { .type = NLA_UNSPEC,
@@ -1622,7 +1646,7 @@ _parse_lnk_vxlan (const char *kind, struct nlattr *info_data)
static NMPObject * static NMPObject *
_new_from_nl_link (NMPlatform *platform, const NMPCache *cache, struct nlmsghdr *nlh, gboolean id_only) _new_from_nl_link (NMPlatform *platform, const NMPCache *cache, struct nlmsghdr *nlh, gboolean id_only)
{ {
static struct nla_policy policy[IFLA_MAX+1] = { static const struct nla_policy policy[IFLA_MAX+1] = {
[IFLA_IFNAME] = { .type = NLA_STRING, [IFLA_IFNAME] = { .type = NLA_STRING,
.maxlen = IFNAMSIZ }, .maxlen = IFNAMSIZ },
[IFLA_MTU] = { .type = NLA_U32 }, [IFLA_MTU] = { .type = NLA_U32 },
@@ -1650,7 +1674,7 @@ _new_from_nl_link (NMPlatform *platform, const NMPCache *cache, struct nlmsghdr
[IFLA_NET_NS_PID] = { .type = NLA_U32 }, [IFLA_NET_NS_PID] = { .type = NLA_U32 },
[IFLA_NET_NS_FD] = { .type = NLA_U32 }, [IFLA_NET_NS_FD] = { .type = NLA_U32 },
}; };
static struct nla_policy policy_link_info[IFLA_INFO_MAX+1] = { static const struct nla_policy policy_link_info[IFLA_INFO_MAX+1] = {
[IFLA_INFO_KIND] = { .type = NLA_STRING }, [IFLA_INFO_KIND] = { .type = NLA_STRING },
[IFLA_INFO_DATA] = { .type = NLA_NESTED }, [IFLA_INFO_DATA] = { .type = NLA_NESTED },
[IFLA_INFO_XSTATS] = { .type = NLA_NESTED }, [IFLA_INFO_XSTATS] = { .type = NLA_NESTED },
@@ -1876,7 +1900,7 @@ errout:
static NMPObject * static NMPObject *
_new_from_nl_addr (struct nlmsghdr *nlh, gboolean id_only) _new_from_nl_addr (struct nlmsghdr *nlh, gboolean id_only)
{ {
static struct nla_policy policy[IFA_MAX+1] = { static const struct nla_policy policy[IFA_MAX+1] = {
[IFA_LABEL] = { .type = NLA_STRING, [IFA_LABEL] = { .type = NLA_STRING,
.maxlen = IFNAMSIZ }, .maxlen = IFNAMSIZ },
[IFA_CACHEINFO] = { .minlen = nm_offsetofend (struct ifa_cacheinfo, tstamp) }, [IFA_CACHEINFO] = { .minlen = nm_offsetofend (struct ifa_cacheinfo, tstamp) },
@@ -1898,7 +1922,7 @@ _new_from_nl_addr (struct nlmsghdr *nlh, gboolean id_only)
goto errout; goto errout;
is_v4 = ifa->ifa_family == AF_INET; is_v4 = ifa->ifa_family == AF_INET;
err = nlmsg_parse(nlh, sizeof(*ifa), tb, IFA_MAX, policy); err = nlmsg_parse (nlh, sizeof(*ifa), tb, IFA_MAX, policy);
if (err < 0) if (err < 0)
goto errout; goto errout;
@@ -1991,7 +2015,7 @@ errout:
static NMPObject * static NMPObject *
_new_from_nl_route (struct nlmsghdr *nlh, gboolean id_only) _new_from_nl_route (struct nlmsghdr *nlh, gboolean id_only)
{ {
static struct nla_policy policy[RTA_MAX+1] = { static const struct nla_policy policy[RTA_MAX+1] = {
[RTA_TABLE] = { .type = NLA_U32 }, [RTA_TABLE] = { .type = NLA_U32 },
[RTA_IIF] = { .type = NLA_U32 }, [RTA_IIF] = { .type = NLA_U32 },
[RTA_OIF] = { .type = NLA_U32 }, [RTA_OIF] = { .type = NLA_U32 },
@@ -2115,7 +2139,7 @@ _new_from_nl_route (struct nlmsghdr *nlh, gboolean id_only)
mss = 0; mss = 0;
if (tb[RTA_METRICS]) { if (tb[RTA_METRICS]) {
struct nlattr *mtb[RTAX_MAX + 1]; struct nlattr *mtb[RTAX_MAX + 1];
static struct nla_policy rtax_policy[RTAX_MAX + 1] = { static const struct nla_policy rtax_policy[RTAX_MAX + 1] = {
[RTAX_LOCK] = { .type = NLA_U32 }, [RTAX_LOCK] = { .type = NLA_U32 },
[RTAX_ADVMSS] = { .type = NLA_U32 }, [RTAX_ADVMSS] = { .type = NLA_U32 },
[RTAX_WINDOW] = { .type = NLA_U32 }, [RTAX_WINDOW] = { .type = NLA_U32 },

View File

@@ -46,6 +46,24 @@
_NM_UTILS_MACRO_REST(__VA_ARGS__)); \ _NM_UTILS_MACRO_REST(__VA_ARGS__)); \
} G_STMT_END } G_STMT_END
/*****************************************************************************/
static int
_nl_nla_parse (struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
const struct nla_policy *policy)
{
return nla_parse (tb, maxtype, head, len, (struct nla_policy *) policy);
}
#define nla_parse(...) _nl_nla_parse(__VA_ARGS__)
static int
_nl_nla_parse_nested (struct nlattr *tb[], int maxtype, struct nlattr *nla,
const struct nla_policy *policy)
{
return nla_parse_nested (tb, maxtype, nla, (struct nla_policy *) policy);
}
#define nla_parse_nested(...) _nl_nla_parse_nested(__VA_ARGS__)
/***************************************************************************** /*****************************************************************************
* Copied from libnl3/genl: * Copied from libnl3/genl:
*****************************************************************************/ *****************************************************************************/
@@ -131,7 +149,7 @@ genlmsg_valid_hdr (struct nlmsghdr *nlh, int hdrlen)
static int static int
genlmsg_parse (struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], genlmsg_parse (struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[],
int maxtype, struct nla_policy *policy) int maxtype, const struct nla_policy *policy)
{ {
struct genlmsghdr *ghdr; struct genlmsghdr *ghdr;
@@ -150,7 +168,7 @@ genlmsg_parse (struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[],
static int static int
probe_response (struct nl_msg *msg, void *arg) probe_response (struct nl_msg *msg, void *arg)
{ {
static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = { static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
[CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 }, [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
[CTRL_ATTR_FAMILY_NAME] = { .type = NLA_STRING, [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_STRING,
.maxlen = GENL_NAMSIZ }, .maxlen = GENL_NAMSIZ },
@@ -389,7 +407,7 @@ nl80211_iface_info_handler (struct nl_msg *msg, void *arg)
struct nlattr *tb[NL80211_ATTR_MAX + 1]; struct nlattr *tb[NL80211_ATTR_MAX + 1];
if (nla_parse (tb, NL80211_ATTR_MAX, genlmsg_attrdata (gnlh, 0), if (nla_parse (tb, NL80211_ATTR_MAX, genlmsg_attrdata (gnlh, 0),
genlmsg_attrlen (gnlh, 0), NULL) < 0) genlmsg_attrlen (gnlh, 0), NULL) < 0)
return NL_SKIP; return NL_SKIP;
if (!tb[NL80211_ATTR_IFTYPE]) if (!tb[NL80211_ATTR_IFTYPE])
@@ -529,7 +547,7 @@ nl80211_bss_dump_handler (struct nl_msg *msg, void *arg)
struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg)); struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg));
struct nlattr *tb[NL80211_ATTR_MAX + 1]; struct nlattr *tb[NL80211_ATTR_MAX + 1];
struct nlattr *bss[NL80211_BSS_MAX + 1]; struct nlattr *bss[NL80211_BSS_MAX + 1];
static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { static const struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
[NL80211_BSS_TSF] = { .type = NLA_U64 }, [NL80211_BSS_TSF] = { .type = NLA_U64 },
[NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
[NL80211_BSS_BSSID] = { }, [NL80211_BSS_BSSID] = { },
@@ -543,15 +561,15 @@ nl80211_bss_dump_handler (struct nl_msg *msg, void *arg)
guint32 status; guint32 status;
if (nla_parse (tb, NL80211_ATTR_MAX, genlmsg_attrdata (gnlh, 0), if (nla_parse (tb, NL80211_ATTR_MAX, genlmsg_attrdata (gnlh, 0),
genlmsg_attrlen (gnlh, 0), NULL) < 0) genlmsg_attrlen (gnlh, 0), NULL) < 0)
return NL_SKIP; return NL_SKIP;
if (tb[NL80211_ATTR_BSS] == NULL) if (tb[NL80211_ATTR_BSS] == NULL)
return NL_SKIP; return NL_SKIP;
if (nla_parse_nested (bss, NL80211_BSS_MAX, if (nla_parse_nested (bss, NL80211_BSS_MAX,
tb[NL80211_ATTR_BSS], tb[NL80211_ATTR_BSS],
bss_policy)) bss_policy))
return NL_SKIP; return NL_SKIP;
if (bss[NL80211_BSS_STATUS] == NULL) if (bss[NL80211_BSS_STATUS] == NULL)
@@ -665,7 +683,7 @@ nl80211_station_handler (struct nl_msg *msg, void *arg)
struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg)); struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg));
struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1]; struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1]; struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = { static const struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
[NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 }, [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
[NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
[NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
@@ -678,7 +696,7 @@ nl80211_station_handler (struct nl_msg *msg, void *arg)
[NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 }, [NL80211_STA_INFO_PLINK_STATE] = { .type = NLA_U8 },
}; };
static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = { static const struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
[NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 }, [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
[NL80211_RATE_INFO_MCS] = { .type = NLA_U8 }, [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
[NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG }, [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
@@ -871,7 +889,7 @@ static int nl80211_wiphy_info_handler (struct nl_msg *msg, void *arg)
int rem_freq; int rem_freq;
int rem_band; int rem_band;
int freq_idx; int freq_idx;
static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { static const struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
[NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 }, [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
[NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG }, [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
#ifdef NL80211_FREQUENCY_ATTR_NO_IR #ifdef NL80211_FREQUENCY_ATTR_NO_IR