Skip to contentSkip to conversation

backports: rename magic functions for netlink parsing

PATCH Superseded Johannes Berg 1 participant 7 messages Oct 2, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 backports: rename magic functions for netlink parsing

From: Johannes Berg <[email protected]>

Make all the magic variable argument calls inlines and rename them so we can override them more easily later.

---
 backport/backport-include/net/netlink.h | 100 +++++++++++++++++++++-----------
 1 file changed, 65 insertions(+), 35 deletions(-)
backport/backport-include/net/netlink.h +65 -35
index 37c899b07b1d..7775bc8295c3 100644
@@ -7,70 +7,100 @@
 #if LINUX_VERSION_IS_LESS(4,12,0)
 #include <backport/magic.h>
 
-static inline int nla_validate5(const struct nlattr *head,
-				int len, int maxtype,
-				const struct nla_policy *policy,
-				struct netlink_ext_ack *extack)
+static inline int _nla_validate5(const struct nlattr *head,
+				 int len, int maxtype,
+				 const struct nla_policy *policy,
+				 struct netlink_ext_ack *extack)
 {
 	return nla_validate(head, len, maxtype, policy);
 }
-#define nla_validate4 nla_validate
+static inline int _nla_validate4(const struct nlattr *head,
+				 int len, int maxtype,
+				 const struct nla_policy *policy)
+{
+	return nla_validate(head, len, maxtype, policy);
+}
+#undef nla_validate
 #define nla_validate(...) \
-	macro_dispatcher(nla_validate, __VA_ARGS__)(__VA_ARGS__)
+	macro_dispatcher(_nla_validate, __VA_ARGS__)(__VA_ARGS__)
 
-static inline int nla_parse6(struct nlattr **tb, int maxtype,
-			     const struct nlattr *head,
-			     int len, const struct nla_policy *policy,
-			     struct netlink_ext_ack *extack)
+static inline int _nla_parse6(struct nlattr **tb, int maxtype,
+			      const struct nlattr *head,
+			      int len, const struct nla_policy *policy,
+			      struct netlink_ext_ack *extack)
+{
+	return nla_parse(tb, maxtype, head, len, policy);
+}
+static inline int _nla_parse5(struct nlattr **tb, int maxtype,
+			      const struct nlattr *head,
+			      int len, const struct nla_policy *policy)
 {
 	return nla_parse(tb, maxtype, head, len, policy);
 }
-#define nla_parse5(...) nla_parse(__VA_ARGS__)
 #define nla_parse(...) \
-	macro_dispatcher(nla_parse, __VA_ARGS__)(__VA_ARGS__)
+	macro_dispatcher(_nla_parse, __VA_ARGS__)(__VA_ARGS__)
 
-static inline int nlmsg_parse6(const struct nlmsghdr *nlh, int hdrlen,
-			       struct nlattr *tb[], int maxtype,
-			       const struct nla_policy *policy,
-			       struct netlink_ext_ack *extack)
+static inline int _nlmsg_parse6(const struct nlmsghdr *nlh, int hdrlen,
+			        struct nlattr *tb[], int maxtype,
+			        const struct nla_policy *policy,
+			        struct netlink_ext_ack *extack)
+{
+	return nlmsg_parse(nlh, hdrlen, tb, maxtype, policy);
+}
+static inline int _nlmsg_parse5(const struct nlmsghdr *nlh, int hdrlen,
+			        struct nlattr *tb[], int maxtype,
+			        const struct nla_policy *policy)
 {
 	return nlmsg_parse(nlh, hdrlen, tb, maxtype, policy);
 }
-#define nlmsg_parse5 nlmsg_parse
 #define nlmsg_parse(...) \
-	macro_dispatcher(nlmsg_parse, __VA_ARGS__)(__VA_ARGS__)
+	macro_dispatcher(_nlmsg_parse, __VA_ARGS__)(__VA_ARGS__)
 
-static inline int nlmsg_validate5(const struct nlmsghdr *nlh,
-				  int hdrlen, int maxtype,
-				  const struct nla_policy *policy,
-				  struct netlink_ext_ack *extack)
+static inline int _nlmsg_validate5(const struct nlmsghdr *nlh,
+				   int hdrlen, int maxtype,
+				   const struct nla_policy *policy,
+				   struct netlink_ext_ack *extack)
+{
+	return nlmsg_validate(nlh, hdrlen, maxtype, policy);
+}
+static inline int _nlmsg_validate4(const struct nlmsghdr *nlh,
+				   int hdrlen, int maxtype,
+				   const struct nla_policy *policy)
 {
 	return nlmsg_validate(nlh, hdrlen, maxtype, policy);
 }
-#define nlmsg_validate4 nlmsg_validate
 #define nlmsg_validate(...) \
-	macro_dispatcher(nlmsg_validate, __VA_ARGS__)(__VA_ARGS__)
+	macro_dispatcher(_nlmsg_validate, __VA_ARGS__)(__VA_ARGS__)
 
-static inline int nla_parse_nested5(struct nlattr *tb[], int maxtype,
-				    const struct nlattr *nla,
-				    const struct nla_policy *policy,
-				    struct netlink_ext_ack *extack)
+static inline int _nla_parse_nested5(struct nlattr *tb[], int maxtype,
+				     const struct nlattr *nla,
+				     const struct nla_policy *policy,
+				     struct netlink_ext_ack *extack)
+{
+	return nla_parse_nested(tb, maxtype, nla, policy);
+}
+static inline int _nla_parse_nested4(struct nlattr *tb[], int maxtype,
+				     const struct nlattr *nla,
+				     const struct nla_policy *policy)
 {
 	return nla_parse_nested(tb, maxtype, nla, policy);
 }
-#define nla_parse_nested4 nla_parse_nested
 #define nla_parse_nested(...) \
-	macro_dispatcher(nla_parse_nested, __VA_ARGS__)(__VA_ARGS__)
+	macro_dispatcher(_nla_parse_nested, __VA_ARGS__)(__VA_ARGS__)
 
-static inline int nla_validate_nested4(const struct nlattr *start, int maxtype,
-				       const struct nla_policy *policy,
-				       struct netlink_ext_ack *extack)
+static inline int _nla_validate_nested4(const struct nlattr *start, int maxtype,
+					const struct nla_policy *policy,
+					struct netlink_ext_ack *extack)
+{
+	return nla_validate_nested(start, maxtype, policy);
+}
+static inline int _nla_validate_nested3(const struct nlattr *start, int maxtype,
+					const struct nla_policy *policy)
 {
 	return nla_validate_nested(start, maxtype, policy);
 }
-#define nla_validate_nested3 nla_validate_nested
 #define nla_validate_nested(...) \
-	macro_dispatcher(nla_validate_nested, __VA_ARGS__)(__VA_ARGS__)
+	macro_dispatcher(_nla_validate_nested, __VA_ARGS__)(__VA_ARGS__)
 #endif /* LINUX_VERSION_IS_LESS(4,12,0) */
 
 #if LINUX_VERSION_IS_LESS(3,7,0)
Signature · 4 lines
2.14.4

--
To unsubscribe from this list: send the line "unsubscribe backports" in
#1 lore raw

PATCH 2/6 backports: fix genlmsg_nlhdr() backport

From: Johannes Berg <[email protected]>

Since 4.15 this no longer takes a family argument, so add that to the backports instead of trying to work around the family argument having changed type.

---
 backport/backport-include/net/genetlink.h | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
backport/backport-include/net/genetlink.h +8 -2
index dfbacbf40723..d80d979ac423 100644
@@ -60,6 +60,14 @@ static inline void *genl_info_userhdr(struct genl_info *info)
 #endif
 
 #if LINUX_VERSION_IS_LESS(4,15,0)
+#define genlmsg_nlhdr LINUX_BACKPORT(genlmsg_nlhdr)
+static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr)
+{
+	return (struct nlmsghdr *)((char *)user_hdr -
+				   GENL_HDRLEN -
+				   NLMSG_HDRLEN);
+}
+
 #ifndef genl_dump_check_consistent
 static inline
 void backport_genl_dump_check_consistent(struct netlink_callback *cb,
@@ -153,8 +161,6 @@ extern void genl_notify(struct sk_buff *skb, struct net *net, u32 pid,
 		    (_fam)->mcgrps[_group].id, _info->nlhdr, _flags)
 #define genlmsg_put(_skb, _pid, _seq, _fam, _flags, _cmd)		\
 	genlmsg_put(_skb, _pid, _seq, &(_fam)->family, _flags, _cmd)
-#define genlmsg_nlhdr(_hdr, _fam)					\
-	genlmsg_nlhdr(_hdr, &(_fam)->family)
 
 #ifndef genlmsg_put_reply /* might already be there from _info override above */
 #define genlmsg_put_reply(_skb, _info, _fam, _flags, _cmd)		\
Signature · 4 lines
2.14.4

--
To unsubscribe from this list: send the line "unsubscribe backports" in
#2 lore raw

PATCH 3/6 backports: add copy-list.hwsim

In reply to Johannes Berg

From: Johannes Berg <[email protected]>

This is can be useful for minimal testing.

---
 copy-list.hwsim | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)
 create mode 100644 copy-list.hwsim
copy-list.hwsim new file +91 -0
new file mode 100644
index 000000000000..c56f0bcb915c
@@ -0,0 +1,91 @@
+#
+# This file specifies which sources are copied from the kernel
+# into the backports package.
+#
+# Directories must end with a slash (e.g. "net/wireless/") and
+# are then copied with all their contents.
+#
+# It is also possible to rename while copying, to do so list
+# old name -> new name
+# Note that in this case the spaces are needed: " -> "
+#
+
+COPYING
+MAINTAINERS
+include/linux/unaligned/
+include/linux/hashtable.h
+include/linux/fixp-arith.h
+include/linux/crc32poly.h
+
+# This just defines some macros, simply take it
+include/linux/bitfield.h
+include/linux/average.h
+include/linux/overflow.h
+
+# crypto_memneq
+crypto/memneq.c -> compat/memneq.c
+
+# 802.11
+drivers/net/wireless/Kconfig
+drivers/net/wireless/Makefile
+
+include/linux/wireless.h
+include/uapi/linux/wireless.h
+include/linux/ieee80211.h
+include/linux/pci_ids.h
+include/uapi/linux/pci_regs.h
+include/linux/mmc/sdio_ids.h
+include/linux/ath9k_platform.h
+include/linux/wl12xx.h
+include/linux/rndis.h
+include/linux/bcm47xx_wdt.h
+include/linux/usb/usbnet.h
+include/linux/usb/cdc.h
+include/uapi/linux/usb/cdc.h
+include/uapi/linux/usb/cdc-wdm.h
+include/linux/usb/cdc_ncm.h
+include/linux/usb/cdc-wdm.h
+include/linux/usb/rndis_host.h
+include/linux/spi/libertas_spi.h
+include/linux/platform_data/brcmfmac.h
+include/linux/platform_data/net-cw1200.h
+include/uapi/linux/nl80211.h
+include/net/rsi_91x.h
+
+include/net/cfg80211.h
+include/net/cfg80211-wext.h
+include/net/ieee80211_radiotap.h
+include/net/lib80211.h
+include/net/mac80211.h
+include/net/regulatory.h
+include/net/codel.h
+include/net/codel_impl.h
+include/net/codel_qdisc.h
+include/net/fq.h
+include/net/fq_impl.h
+
+# signature verification code
+include/linux/asn1.h
+include/linux/asn1_ber_bytecode.h
+include/linux/oid_registry.h
+lib/build_OID_registry -> compat/build_OID_registry
+crypto/asymmetric_keys/pkcs7_parser.c -> compat/verification/pkcs7_parser.c
+crypto/asymmetric_keys/pkcs7_parser.h -> compat/verification/pkcs7_parser.h
+crypto/asymmetric_keys/pkcs7_trust.c -> compat/verification/pkcs7_trust.c
+crypto/asymmetric_keys/pkcs7_verify.c -> compat/verification/pkcs7_verify.c
+crypto/asymmetric_keys/x509_cert_parser.c -> compat/verification/x509_cert_parser.c
+crypto/asymmetric_keys/x509_parser.h -> compat/verification/x509_parser.h
+crypto/asymmetric_keys/x509_public_key.c -> compat/verification/x509_public_key.c
+
+net/Makefile
+net/Kconfig
+net/wireless/
+net/mac80211/
+# MPLS labels, checked on skb->protocol on the
+# respective 802 family, only ethernet seems to use
+# this right now, see 960d97f95
+include/linux/mpls.h
+include/uapi/linux/mpls.h
+
+drivers/net/wireless/mac80211_hwsim.c
+drivers/net/wireless/mac80211_hwsim.h
Signature · 4 lines
2.14.4

--
To unsubscribe from this list: send the line "unsubscribe backports" in
#3 lore raw

PATCH 4/6 backports: backport most of improved netlink policy validation

In reply to Johannes Berg

From: Johannes Berg <[email protected]>

---
 backport/backport-include/linux/netlink.h |  19 ++
 backport/backport-include/net/netlink.h   | 185 ++++++++++++++-
 backport/compat/Makefile                  |   1 +
 backport/compat/backport-4.20.c           | 379 ++++++++++++++++++++++++++++++
 4 files changed, 572 insertions(+), 12 deletions(-)
 create mode 100644 backport/compat/backport-4.20.c
backport/backport-include/linux/netlink.h +19 -0
index 468a12d15dbe..366c9e27e8c2 100644
@@ -3,6 +3,13 @@
 #include_next <linux/netlink.h>
 #include <linux/version.h>
 
+#if LINUX_VERSION_IS_LESS(4,14,0)
+struct nla_bitfield32 {
+	__u32 value;
+	__u32 selector;
+};
+#endif
+
 #if LINUX_VERSION_IS_LESS(4,12,0)
 #define NETLINK_MAX_COOKIE_LEN  20
 
@@ -23,6 +30,18 @@ struct netlink_ext_ack {
 } while (0)
 #endif
 
+#ifndef NL_SET_ERR_MSG_ATTR
+#define NL_SET_ERR_MSG_ATTR(extack, attr, msg) do {	\
+	static const char __msg[] = msg;		\
+	struct netlink_ext_ack *__extack = (extack);	\
+							\
+	if (__extack) {					\
+		__extack->_msg = __msg;			\
+		__extack->bad_attr = (attr);		\
+	}						\
+} while (0)
+#endif
+
 /* this is for patches we apply */
 #if LINUX_VERSION_IS_LESS(3,7,0)
 #define netlink_notify_portid(__notify) (__notify->pid)
backport/backport-include/net/netlink.h +173 -12

261 lines not on this page · raw message

backport/compat/Makefile +1 -0
index f5b1886e2d8e..ff6c7e658380 100644
@@ -38,6 +38,7 @@ compat-$(CPTCFG_KERNEL_4_8) += backport-4.8.o
 compat-$(CPTCFG_KERNEL_4_10) += backport-4.10.o
 compat-$(CPTCFG_KERNEL_4_12) += backport-4.12.o
 compat-$(CPTCFG_KERNEL_4_18) += backport-4.18.o
+compat-$(CPTCFG_KERNEL_4_20) += backport-4.20.o
 
 compat-$(CPTCFG_BPAUTO_CRYPTO_SKCIPHER) += crypto-skcipher.o
 
backport/compat/backport-4.20.c new file +379 -0

379 lines not on this page · raw message

Signature · 4 lines
2.14.4

--
To unsubscribe from this list: send the line "unsubscribe backports" in
#4 lore raw

PATCH 5/6 backports: add __skb_peek()

In reply to Johannes Berg

From: Johannes Berg <[email protected]>

New API slated to show up in 4.20.

---
 backport/backport-include/linux/skbuff.h | 7 +++++++
 1 file changed, 7 insertions(+)
backport/backport-include/linux/skbuff.h +7 -0
index 61133c4277cc..44b2a5de0478 100644
@@ -382,4 +382,11 @@ static inline void skb_put_u8(struct sk_buff *skb, u8 val)
 }
 #endif
 
+#if LINUX_VERSION_IS_LESS(4,20,0)
+static inline struct sk_buff *__skb_peek(const struct sk_buff_head *list_)
+{
+	return list_->next;
+}
+#endif
+
 #endif /* __BACKPORT_SKBUFF_H */
Signature · 4 lines
2.14.4

--
To unsubscribe from this list: send the line "unsubscribe backports" in
#5 lore raw

PATCH 6/6 backports: genetlink: update completely

In reply to Johannes Berg

From: Johannes Berg <[email protected]>

Replace all the different nested versions of generic netlink backport with a single one, covering from < 3.13 all the way to the upcoming netlink policy improvements in 4.20.

---
 backport/backport-include/linux/netlink.h |   2 +-
 backport/backport-include/net/genetlink.h | 238 ++++++-----------
 backport/compat/Makefile                  |   3 +-
 backport/compat/backport-3.13.c           |  66 -----
 backport/compat/backport-4.12.c           | 274 --------------------
 backport/compat/backport-genetlink.c      | 418 ++++++++++++++++++++++++++++++
 backport/compat/compat-3.3.c              |  13 -
 7 files changed, 502 insertions(+), 512 deletions(-)
 delete mode 100644 backport/compat/backport-4.12.c
 create mode 100644 backport/compat/backport-genetlink.c
backport/backport-include/linux/netlink.h +1 -1
index 366c9e27e8c2..f956a76966f3 100644
@@ -20,7 +20,7 @@ struct netlink_ext_ack {
 	u8 cookie_len;
 
 	/* backport only field */
-	void *__bp_genl_real_ops;
+	void *__bp_doit;
 };
 
 #define NL_SET_ERR_MSG(extack, msg) do {	\
backport/backport-include/net/genetlink.h +81 -157

288 lines not on this page · raw message

backport/compat/Makefile +2 -1
index ff6c7e658380..fdc68c8b4600 100644
@@ -36,10 +36,11 @@ compat-$(CPTCFG_KERNEL_4_6) += backport-4.6.o
 compat-$(CPTCFG_KERNEL_4_7) += backport-4.7.o
 compat-$(CPTCFG_KERNEL_4_8) += backport-4.8.o
 compat-$(CPTCFG_KERNEL_4_10) += backport-4.10.o
-compat-$(CPTCFG_KERNEL_4_12) += backport-4.12.o
 compat-$(CPTCFG_KERNEL_4_18) += backport-4.18.o
 compat-$(CPTCFG_KERNEL_4_20) += backport-4.20.o
 
+compat-$(CPTCFG_KERNEL_4_20) += backport-genetlink.o
+
 compat-$(CPTCFG_BPAUTO_CRYPTO_SKCIPHER) += crypto-skcipher.o
 
 compat-$(CPTCFG_BPAUTO_BUILD_SYSTEM_DATA_VERIFICATION) += verification/verify.o
backport/compat/backport-3.13.c +0 -66

77 lines not on this page · raw message

backport/compat/backport-4.12.c deleted +0 -274

274 lines not on this page · raw message

backport/compat/backport-genetlink.c new file +418 -0

418 lines not on this page · raw message

backport/compat/compat-3.3.c +0 -13
index c064f09d9abe..1185a5d2d4e0 100644
@@ -224,16 +224,3 @@ void backport_destroy_workqueue(struct workqueue_struct *wq)
 	spin_unlock(&wq_name_lock);
 }
 EXPORT_SYMBOL_GPL(backport_destroy_workqueue);
-
-void genl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
-		 struct nlmsghdr *nlh, gfp_t flags)
-{
-	struct sock *sk = net->genl_sock;
-	int report = 0;
-
-	if (nlh)
-		report = nlmsg_report(nlh);
-
-	nlmsg_notify(sk, skb, pid, group, report, flags);
-}
-EXPORT_SYMBOL_GPL(genl_notify);
Signature · 4 lines
2.14.4

--
To unsubscribe from this list: send the line "unsubscribe backports" in
#6 lore raw

Re: [PATCH 6/6] backports: genetlink: update completely

Quoted 25 lines · Johannes Berg
backport/compat/backport-genetlink.c new file +24 -0
+static int backport_pre_doit(__genl_const struct genl_ops *ops,
+			     struct sk_buff *skb,
+			     struct genl_info *info)
+{
+	const struct genl_family *family = find_family_real_ops(&ops);
+	int err;
+#if LINUX_VERSION_IS_LESS(4,12,0)
+	struct netlink_ext_ack *extack = kzalloc(sizeof(*extack), GFP_KERNEL);
+
+	__bp_genl_info_userhdr_set(info, extack);
+
+	if (!extack) {
+		__bp_genl_info_userhdr_set(info, ERR_PTR(-ENOMEM));
+		return -ENOMEM;
+	}
+
+	extack->__bp_doit = ops->doit;
+#else
+	struct netlink_ext_ack *extack = info->extack;
+#endif
+
+	err = nlmsg_validate(info->nlhdr, GENL_HDRLEN + family->hdrsize,
+			     family->maxattr, ops->policy, extack);
+	if (err)

backport/compat/backport-genetlink.c line 188

Quoted 10 lines
backport/compat/backport-genetlink.c new file +10 -0
+		return err;
+
+	err = family->pre_doit(ops, skb, info);
+
+#if LINUX_VERSION_IS_LESS(4,12,0)
+	if (err) {
+		__bp_genl_info_userhdr_set(info, ERR_PTR(err));
+		kfree(extack);
+	}
+#endif

This is wrong, it doesn't free the extack on errors.

We could also immediately send the ACK out, I think, and include the error there.

johannes
--
To unsubscribe from this list: send the line "unsubscribe backports" in